3 * Storage Target Handling functions
5 * Lustre Object Server Module (OST)
7 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
9 * This code is issued under the GNU General Public License.
10 * See the file COPYING in this distribution
12 * by Peter Braam <braam@clusterfs.com>
14 * This server is single threaded at present (but can easily be multi
15 * threaded). For testing and management it is treated as an
16 * obd_device, although it does not export a full OBD method table
17 * (the requests are coming in over the wire, so object target
18 * modules do not have a full method table.)
24 #include <linux/version.h>
25 #include <linux/module.h>
27 #include <linux/stat.h>
28 #include <linux/locks.h>
29 #include <linux/ext2_fs.h>
30 #include <linux/quotaops.h>
31 #include <asm/unistd.h>
33 #define DEBUG_SUBSYSTEM S_OST
35 #include <linux/obd_support.h>
36 #include <linux/obd.h>
37 #include <linux/obd_class.h>
38 #include <linux/lustre_lib.h>
39 #include <linux/lustre_idl.h>
40 #include <linux/lustre_mds.h>
41 #include <linux/obd_class.h>
44 static int ost_queue_req(struct obd_device *obddev, struct ptlrpc_request *req)
46 struct ptlrpc_request *srv_req;
47 struct ost_obd *ost = &obddev->u.ost;
54 OBD_ALLOC(srv_req, sizeof(*srv_req));
60 CDEBUG(0, "---> OST at %d %p, incoming req %p, srv_req %p\n",
61 __LINE__, ost, req, srv_req);
63 memset(srv_req, 0, sizeof(*req));
65 /* move the request buffer */
66 srv_req->rq_reqbuf = req->rq_reqbuf;
67 srv_req->rq_reqlen = req->rq_reqlen;
68 srv_req->rq_ost = ost;
70 /* remember where it came from */
71 srv_req->rq_reply_handle = req;
73 list_add(&srv_req->rq_list, &ost->ost_reqs);
74 wake_up(&ost->ost_waitq);
78 int ost_reply(struct obd_device *obddev, struct ptlrpc_request *req)
80 struct ptlrpc_request *clnt_req = req->rq_reply_handle;
84 if (req->rq_ost->ost_service != NULL) {
85 /* This is a request that came from the network via portals. */
87 /* FIXME: we need to increment the count of handled events */
88 ptl_send_buf(req, &req->rq_peer, OST_REPLY_PORTAL, 0);
90 /* This is a local request that came from another thread. */
92 /* move the reply to the client */
93 clnt_req->rq_replen = req->rq_replen;
94 clnt_req->rq_repbuf = req->rq_repbuf;
95 req->rq_repbuf = NULL;
98 /* free the request buffer */
99 OBD_FREE(req->rq_reqbuf, req->rq_reqlen);
100 req->rq_reqbuf = NULL;
102 /* wake up the client */
103 wake_up_interruptible(&clnt_req->rq_wait_for_rep);
110 int ost_error(struct obd_device *obddev, struct ptlrpc_request *req)
112 struct ptlrep_hdr *hdr;
116 OBD_ALLOC(hdr, sizeof(*hdr));
122 memset(hdr, 0, sizeof(*hdr));
124 hdr->seqno = req->rq_reqhdr->seqno;
125 hdr->status = req->rq_status;
126 hdr->type = OST_TYPE_ERR;
128 req->rq_repbuf = (char *)hdr;
129 req->rq_replen = sizeof(*hdr);
132 return ost_reply(obddev, req);
135 static int ost_destroy(struct ost_obd *ost, struct ptlrpc_request *req)
137 struct obd_conn conn;
142 conn.oc_id = req->rq_req.ost->connid;
143 conn.oc_dev = ost->ost_tgt;
145 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep.ost,
146 &req->rq_replen, &req->rq_repbuf);
148 CERROR("cannot pack reply\n");
152 req->rq_rep.ost->result =ost->ost_tgt->obd_type->typ_ops->o_destroy
153 (&conn, &req->rq_req.ost->oa);
159 static int ost_getattr(struct ost_obd *ost, struct ptlrpc_request *req)
161 struct obd_conn conn;
166 conn.oc_id = req->rq_req.ost->connid;
167 conn.oc_dev = ost->ost_tgt;
169 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep.ost,
170 &req->rq_replen, &req->rq_repbuf);
172 CERROR("cannot pack reply\n");
175 req->rq_rep.ost->oa.o_id = req->rq_req.ost->oa.o_id;
176 req->rq_rep.ost->oa.o_valid = req->rq_req.ost->oa.o_valid;
178 req->rq_rep.ost->result =ost->ost_tgt->obd_type->typ_ops->o_getattr
179 (&conn, &req->rq_rep.ost->oa);
185 static int ost_create(struct ost_obd *ost, struct ptlrpc_request *req)
187 struct obd_conn conn;
192 conn.oc_id = req->rq_req.ost->connid;
193 conn.oc_dev = ost->ost_tgt;
195 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep.ost,
196 &req->rq_replen, &req->rq_repbuf);
198 CERROR("cannot pack reply\n");
202 memcpy(&req->rq_rep.ost->oa, &req->rq_req.ost->oa, sizeof(req->rq_req.ost->oa));
204 req->rq_rep.ost->result =ost->ost_tgt->obd_type->typ_ops->o_create
205 (&conn, &req->rq_rep.ost->oa);
212 static int ost_setattr(struct ost_obd *ost, struct ptlrpc_request *req)
214 struct obd_conn conn;
219 conn.oc_id = req->rq_req.ost->connid;
220 conn.oc_dev = ost->ost_tgt;
222 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep.ost,
223 &req->rq_replen, &req->rq_repbuf);
225 CERROR("cannot pack reply\n");
229 memcpy(&req->rq_rep.ost->oa, &req->rq_req.ost->oa,
230 sizeof(req->rq_req.ost->oa));
232 req->rq_rep.ost->result =ost->ost_tgt->obd_type->typ_ops->o_setattr
233 (&conn, &req->rq_rep.ost->oa);
239 static int ost_connect(struct ost_obd *ost, struct ptlrpc_request *req)
241 struct obd_conn conn;
246 conn.oc_dev = ost->ost_tgt;
248 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep.ost,
249 &req->rq_replen, &req->rq_repbuf);
251 CERROR("cannot pack reply\n");
255 req->rq_rep.ost->result =ost->ost_tgt->obd_type->typ_ops->o_connect(&conn);
257 CDEBUG(0, "rep buffer %p, id %d\n", req->rq_repbuf,
259 req->rq_rep.ost->connid = conn.oc_id;
264 static int ost_disconnect(struct ost_obd *ost, struct ptlrpc_request *req)
266 struct obd_conn conn;
271 conn.oc_dev = ost->ost_tgt;
272 conn.oc_id = req->rq_req.ost->connid;
274 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep.ost,
275 &req->rq_replen, &req->rq_repbuf);
277 CERROR("cannot pack reply\n");
281 req->rq_rep.ost->result =ost->ost_tgt->obd_type->typ_ops->o_disconnect(&conn);
287 static int ost_get_info(struct ost_obd *ost, struct ptlrpc_request *req)
289 struct obd_conn conn;
297 conn.oc_id = req->rq_req.ost->connid;
298 conn.oc_dev = ost->ost_tgt;
300 ptr = ost_req_buf1(req->rq_req.ost);
301 req->rq_rep.ost->result =ost->ost_tgt->obd_type->typ_ops->o_get_info
302 (&conn, req->rq_req.ost->buflen1, ptr, &vallen, &val);
304 rc = ost_pack_rep(val, vallen, NULL, 0, &req->rq_rephdr,
305 &req->rq_rep.ost, &req->rq_replen, &req->rq_repbuf);
307 CERROR("cannot pack reply\n");
315 int ost_brw(struct ost_obd *obddev, struct ptlrpc_request *req)
317 struct obd_conn conn;
320 int objcount, niocount;
321 char *tmp1, *tmp2, *end2;
324 struct niobuf *nb, *src, *dst;
325 struct obd_ioobj *ioo;
326 struct ost_req *r = req->rq_req.ost;
330 tmp1 = ost_req_buf1(r);
331 tmp2 = ost_req_buf2(r);
332 end2 = tmp2 + req->rq_req.ost->buflen2;
333 objcount = r->buflen1 / sizeof(*ioo);
334 niocount = r->buflen2 / sizeof(*nb);
337 conn.oc_id = req->rq_req.ost->connid;
338 conn.oc_dev = req->rq_ost->ost_tgt;
340 rc = ost_pack_rep(NULL, niocount, NULL, 0,
341 &req->rq_rephdr, &req->rq_rep.ost,
342 &req->rq_replen, &req->rq_repbuf);
344 CERROR("cannot pack reply\n");
347 res = ost_rep_buf1(req->rq_rep.ost);
349 for (i=0; i < objcount; i++) {
350 ost_unpack_ioo((void *)&tmp1, &ioo);
351 if (tmp2 + ioo->ioo_bufcnt > end2) {
355 for (j = 0 ; j < ioo->ioo_bufcnt ; j++) {
356 ost_unpack_niobuf((void *)&tmp2, &nb);
360 /* The unpackers move tmp1 and tmp2, so reset them before using */
361 tmp1 = ost_req_buf1(r);
362 tmp2 = ost_req_buf2(r);
363 req->rq_rep.ost->result =
364 req->rq_ost->ost_tgt->obd_type->typ_ops->o_preprw
365 (cmd, &conn, objcount, (struct obd_ioobj *)tmp1,
366 niocount, (struct niobuf *)tmp2, (struct niobuf *)res);
368 if (cmd == OBD_BRW_WRITE) {
369 for (i=0; i<niocount; i++) {
370 src = &((struct niobuf *)tmp2)[i];
371 dst = &((struct niobuf *)res)[i];
372 memcpy((void *)(unsigned long)dst->addr,
373 (void *)(unsigned long)src->addr,
377 for (i=0; i<niocount; i++) {
378 dst = &((struct niobuf *)tmp2)[i];
379 src = &((struct niobuf *)res)[i];
380 memcpy((void *)(unsigned long)dst->addr,
381 (void *)(unsigned long)src->addr,
386 req->rq_rep.ost->result =
387 req->rq_ost->ost_tgt->obd_type->typ_ops->o_commitrw
388 (cmd, &conn, objcount, (struct obd_ioobj *)tmp1,
389 niocount, (struct niobuf *)res);
395 int ost_handle(struct obd_device *obddev, struct ptlrpc_request *req)
398 struct ost_obd *ost = &obddev->u.ost;
399 struct ptlreq_hdr *hdr;
402 CDEBUG(0, "req at %p\n", req);
404 hdr = (struct ptlreq_hdr *)req->rq_reqbuf;
405 if (NTOH__u32(hdr->type) != OST_TYPE_REQ) {
406 CERROR("lustre_ost: wrong packet type sent %d\n",
407 NTOH__u32(hdr->type));
412 rc = ost_unpack_req(req->rq_reqbuf, req->rq_reqlen,
413 &req->rq_reqhdr, &req->rq_req.ost);
415 CERROR("lustre_ost: Invalid request\n");
420 switch (req->rq_reqhdr->opc) {
423 CDEBUG(D_INODE, "connect\n");
424 rc = ost_connect(ost, req);
427 CDEBUG(D_INODE, "disconnect\n");
428 rc = ost_disconnect(ost, req);
431 CDEBUG(D_INODE, "get_info\n");
432 rc = ost_get_info(ost, req);
435 CDEBUG(D_INODE, "create\n");
436 rc = ost_create(ost, req);
439 CDEBUG(D_INODE, "destroy\n");
440 rc = ost_destroy(ost, req);
443 CDEBUG(D_INODE, "getattr\n");
444 rc = ost_getattr(ost, req);
447 CDEBUG(D_INODE, "setattr\n");
448 rc = ost_setattr(ost, req);
451 CDEBUG(D_INODE, "brw\n");
452 rc = ost_brw(ost, req);
455 req->rq_status = -ENOTSUPP;
456 return ost_error(obddev, req);
462 CERROR("ost: processing error %d\n", rc);
463 ost_error(obddev, req);
465 CDEBUG(D_INODE, "sending reply\n");
466 ost_reply(obddev, req);
472 int ost_main(void *arg)
474 struct obd_device *obddev = (struct obd_device *) arg;
475 struct ost_obd *ost = &obddev->u.ost;
480 spin_lock_irq(¤t->sigmask_lock);
481 sigfillset(¤t->blocked);
482 recalc_sigpending(current);
483 spin_unlock_irq(¤t->sigmask_lock);
485 sprintf(current->comm, "lustre_ost");
487 /* Record that the thread is running */
488 ost->ost_thread = current;
489 wake_up(&ost->ost_done_waitq);
491 /* XXX maintain a list of all managed devices: insert here */
493 /* And now, wait forever for commit wakeup events. */
497 if (ost->ost_flags & OST_EXIT)
500 wake_up(&ost->ost_done_waitq);
501 interruptible_sleep_on(&ost->ost_waitq);
503 CDEBUG(D_INODE, "lustre_ost wakes\n");
504 CDEBUG(D_INODE, "pick up req here and continue\n");
507 if (ost->ost_service != NULL) {
510 CDEBUG(D_INODE, "\n");
512 struct ptlrpc_request request;
513 struct ptlrpc_service *service;
514 CDEBUG(D_INODE, "\n");
515 rc = PtlEQGet(ost->ost_service->srv_eq_h, &ev);
516 if (rc != PTL_OK && rc != PTL_EQ_DROPPED)
518 CDEBUG(D_INODE, "\n");
520 service = (struct ptlrpc_service *)ev.mem_desc.user_ptr;
522 /* FIXME: If we move to an event-driven model,
523 * we should put the request on the stack of
524 * mds_handle instead. */
525 memset(&request, 0, sizeof(request));
526 request.rq_reqbuf = ev.mem_desc.start +
528 request.rq_reqlen = ev.mem_desc.length;
529 request.rq_ost = ost;
530 request.rq_xid = ev.match_bits;
532 request.rq_peer.peer_nid = ev.initiator.nid;
533 /* FIXME: this NI should be the incoming NI.
534 * We don't know how to find that from here. */
535 request.rq_peer.peer_ni =
536 ost->ost_service->srv_self.peer_ni;
537 rc = ost_handle(obddev, &request);
539 /* Inform the rpc layer the event has been handled */
540 ptl_received_rpc(service);
543 struct ptlrpc_request *request;
545 if (list_empty(&ost->ost_reqs)) {
546 CDEBUG(D_INODE, "woke because of timer\n");
548 request = list_entry(ost->ost_reqs.next,
549 struct ptlrpc_request,
551 list_del(&request->rq_list);
552 rc = ost_handle(obddev, request);
557 /* XXX maintain a list of all managed devices: cleanup here */
559 ost->ost_thread = NULL;
560 wake_up(&ost->ost_done_waitq);
561 CERROR("lustre_ost: exiting\n");
565 static void ost_stop_srv_thread(struct ost_obd *ost)
567 ost->ost_flags |= OST_EXIT;
569 while (ost->ost_thread) {
570 wake_up(&ost->ost_waitq);
571 sleep_on(&ost->ost_done_waitq);
575 static void ost_start_srv_thread(struct obd_device *obd)
577 struct ost_obd *ost = &obd->u.ost;
580 init_waitqueue_head(&ost->ost_waitq);
581 init_waitqueue_head(&ost->ost_done_waitq);
582 kernel_thread(ost_main, (void *)obd,
583 CLONE_VM | CLONE_FS | CLONE_FILES);
584 while (!ost->ost_thread)
585 sleep_on(&ost->ost_done_waitq);
589 /* mount the file system (secretly) */
590 static int ost_setup(struct obd_device *obddev, obd_count len,
594 struct obd_ioctl_data* data = buf;
595 struct ost_obd *ost = &obddev->u.ost;
596 struct obd_device *tgt;
597 struct lustre_peer peer;
601 if (data->ioc_dev < 0 || data->ioc_dev > MAX_OBD_DEVICES) {
606 tgt = &obd_dev[data->ioc_dev];
608 if ( ! (tgt->obd_flags & OBD_ATTACHED) ||
609 ! (tgt->obd_flags & OBD_SET_UP) ){
610 CERROR("device not attached or not set up (%d)\n",
616 ost->ost_conn.oc_dev = tgt;
617 err = tgt->obd_type->typ_ops->o_connect(&ost->ost_conn);
619 CERROR("lustre ost: fail to connect to device %d\n",
624 INIT_LIST_HEAD(&ost->ost_reqs);
625 ost->ost_thread = NULL;
628 spin_lock_init(&obddev->u.ost.ost_lock);
630 err = kportal_uuid_to_peer("self", &peer);
632 OBD_ALLOC(ost->ost_service, sizeof(*ost->ost_service));
633 if (ost->ost_service == NULL)
635 ost->ost_service->srv_buf_size = 64 * 1024;
636 ost->ost_service->srv_portal = OST_REQUEST_PORTAL;
637 memcpy(&ost->ost_service->srv_self, &peer, sizeof(peer));
638 ost->ost_service->srv_wait_queue = &ost->ost_waitq;
640 rpc_register_service(ost->ost_service, "self");
643 ost_start_srv_thread(obddev);
650 static int ost_cleanup(struct obd_device * obddev)
652 struct ost_obd *ost = &obddev->u.ost;
653 struct obd_device *tgt;
658 if ( !(obddev->obd_flags & OBD_SET_UP) ) {
663 if ( !list_empty(&obddev->obd_gen_clients) ) {
664 CERROR("still has clients!\n");
669 ost_stop_srv_thread(ost);
670 rpc_unregister_service(ost->ost_service);
671 OBD_FREE(ost->ost_service, sizeof(*ost->ost_service));
673 if (!list_empty(&ost->ost_reqs)) {
674 // XXX reply with errors and clean up
675 CDEBUG(D_INODE, "Request list not empty!\n");
679 err = tgt->obd_type->typ_ops->o_disconnect(&ost->ost_conn);
681 CERROR("lustre ost: fail to disconnect device\n");
691 /* use obd ops to offer management infrastructure */
692 static struct obd_ops ost_obd_ops = {
694 o_cleanup: ost_cleanup,
697 static int __init ost_init(void)
699 obd_register_type(&ost_obd_ops, LUSTRE_OST_NAME);
703 static void __exit ost_exit(void)
705 obd_unregister_type(LUSTRE_OST_NAME);
708 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
709 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
710 MODULE_LICENSE("GPL");
712 // for testing (maybe this stays)
713 EXPORT_SYMBOL(ost_queue_req);
715 module_init(ost_init);
716 module_exit(ost_exit);