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.)
25 #include <linux/version.h>
26 #include <linux/module.h>
28 #include <linux/stat.h>
29 #include <linux/locks.h>
30 #include <linux/ext2_fs.h>
31 #include <linux/quotaops.h>
32 #include <asm/unistd.h>
33 #include <linux/obd_support.h>
34 #include <linux/obd.h>
35 #include <linux/obd_class.h>
36 #include <linux/lustre_lib.h>
37 #include <linux/lustre_idl.h>
38 #include <linux/lustre_mds.h>
39 #include <linux/obd_class.h>
42 static int ost_queue_req(struct obd_device *obddev, struct ptlrpc_request *req)
44 struct ptlrpc_request *srv_req;
45 struct ost_obd *ost = &obddev->u.ost;
52 OBD_ALLOC(srv_req, sizeof(*srv_req));
58 CDEBUG(D_OST, "---> OST at %d %p, incoming req %p, srv_req %p\n",
59 __LINE__, ost, req, srv_req);
61 memset(srv_req, 0, sizeof(*req));
63 /* move the request buffer */
64 srv_req->rq_reqbuf = req->rq_reqbuf;
65 srv_req->rq_reqlen = req->rq_reqlen;
66 srv_req->rq_ost = ost;
68 /* remember where it came from */
69 srv_req->rq_reply_handle = req;
71 list_add(&srv_req->rq_list, &ost->ost_reqs);
72 wake_up(&ost->ost_waitq);
76 int ost_reply(struct obd_device *obddev, struct ptlrpc_request *req)
78 struct ptlrpc_request *clnt_req = req->rq_reply_handle;
82 if (req->rq_ost->ost_service != NULL) {
83 /* This is a request that came from the network via portals. */
85 /* FIXME: we need to increment the count of handled events */
86 ptl_send_buf(req, &req->rq_peer, OST_REPLY_PORTAL, 0);
88 /* This is a local request that came from another thread. */
90 /* move the reply to the client */
91 clnt_req->rq_replen = req->rq_replen;
92 clnt_req->rq_repbuf = req->rq_repbuf;
93 req->rq_repbuf = NULL;
96 /* free the request buffer */
97 OBD_FREE(req->rq_reqbuf, req->rq_reqlen);
98 req->rq_reqbuf = NULL;
100 /* wake up the client */
101 wake_up_interruptible(&clnt_req->rq_wait_for_rep);
108 int ost_error(struct obd_device *obddev, struct ptlrpc_request *req)
110 struct ptlrep_hdr *hdr;
114 OBD_ALLOC(hdr, sizeof(*hdr));
120 memset(hdr, 0, sizeof(*hdr));
122 hdr->seqno = req->rq_reqhdr->seqno;
123 hdr->status = req->rq_status;
124 hdr->type = OST_TYPE_ERR;
126 req->rq_repbuf = (char *)hdr;
127 req->rq_replen = sizeof(*hdr);
130 return ost_reply(obddev, req);
133 static int ost_destroy(struct ost_obd *ost, struct ptlrpc_request *req)
135 struct obd_conn conn;
140 conn.oc_id = req->rq_req.ost->connid;
141 conn.oc_dev = ost->ost_tgt;
143 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep.ost,
144 &req->rq_replen, &req->rq_repbuf);
146 printk("ost_destroy: cannot pack reply\n");
150 req->rq_rep.ost->result =ost->ost_tgt->obd_type->typ_ops->o_destroy
151 (&conn, &req->rq_req.ost->oa);
157 static int ost_getattr(struct ost_obd *ost, struct ptlrpc_request *req)
159 struct obd_conn conn;
164 conn.oc_id = req->rq_req.ost->connid;
165 conn.oc_dev = ost->ost_tgt;
167 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep.ost,
168 &req->rq_replen, &req->rq_repbuf);
170 printk("ost_getattr: cannot pack reply\n");
173 req->rq_rep.ost->oa.o_id = req->rq_req.ost->oa.o_id;
174 req->rq_rep.ost->oa.o_valid = req->rq_req.ost->oa.o_valid;
176 req->rq_rep.ost->result =ost->ost_tgt->obd_type->typ_ops->o_getattr
177 (&conn, &req->rq_rep.ost->oa);
183 static int ost_create(struct ost_obd *ost, struct ptlrpc_request *req)
185 struct obd_conn conn;
190 conn.oc_id = req->rq_req.ost->connid;
191 conn.oc_dev = ost->ost_tgt;
193 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep.ost,
194 &req->rq_replen, &req->rq_repbuf);
196 printk("ost_create: cannot pack reply\n");
200 memcpy(&req->rq_rep.ost->oa, &req->rq_req.ost->oa, sizeof(req->rq_req.ost->oa));
202 req->rq_rep.ost->result =ost->ost_tgt->obd_type->typ_ops->o_create
203 (&conn, &req->rq_rep.ost->oa);
210 static int ost_setattr(struct ost_obd *ost, struct ptlrpc_request *req)
212 struct obd_conn conn;
217 conn.oc_id = req->rq_req.ost->connid;
218 conn.oc_dev = ost->ost_tgt;
220 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep.ost,
221 &req->rq_replen, &req->rq_repbuf);
223 printk("ost_setattr: cannot pack reply\n");
227 memcpy(&req->rq_rep.ost->oa, &req->rq_req.ost->oa,
228 sizeof(req->rq_req.ost->oa));
230 req->rq_rep.ost->result =ost->ost_tgt->obd_type->typ_ops->o_setattr
231 (&conn, &req->rq_rep.ost->oa);
237 static int ost_connect(struct ost_obd *ost, struct ptlrpc_request *req)
239 struct obd_conn conn;
244 conn.oc_dev = ost->ost_tgt;
246 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep.ost,
247 &req->rq_replen, &req->rq_repbuf);
249 printk("ost_setattr: cannot pack reply\n");
253 req->rq_rep.ost->result =ost->ost_tgt->obd_type->typ_ops->o_connect(&conn);
255 CDEBUG(D_OST, "ost_connect: rep buffer %p, id %d\n", req->rq_repbuf,
257 req->rq_rep.ost->connid = conn.oc_id;
262 static int ost_disconnect(struct ost_obd *ost, struct ptlrpc_request *req)
264 struct obd_conn conn;
269 conn.oc_dev = ost->ost_tgt;
270 conn.oc_id = req->rq_req.ost->connid;
272 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep.ost,
273 &req->rq_replen, &req->rq_repbuf);
275 printk("ost_setattr: cannot pack reply\n");
279 req->rq_rep.ost->result =ost->ost_tgt->obd_type->typ_ops->o_disconnect(&conn);
285 static int ost_get_info(struct ost_obd *ost, struct ptlrpc_request *req)
287 struct obd_conn conn;
295 conn.oc_id = req->rq_req.ost->connid;
296 conn.oc_dev = ost->ost_tgt;
298 ptr = ost_req_buf1(req->rq_req.ost);
299 req->rq_rep.ost->result =ost->ost_tgt->obd_type->typ_ops->o_get_info
300 (&conn, req->rq_req.ost->buflen1, ptr, &vallen, &val);
302 rc = ost_pack_rep(val, vallen, NULL, 0, &req->rq_rephdr, &req->rq_rep.ost,
303 &req->rq_replen, &req->rq_repbuf);
305 printk("ost_setattr: cannot pack reply\n");
313 int ost_brw(struct ost_obd *obddev, struct ptlrpc_request *req)
315 struct obd_conn conn;
318 int objcount, niocount;
319 char *tmp1, *tmp2, *end2;
322 struct niobuf *nb, *src, *dst;
323 struct obd_ioobj *ioo;
324 struct ost_req *r = req->rq_req.ost;
328 tmp1 = ost_req_buf1(r);
329 tmp2 = ost_req_buf2(r);
330 end2 = tmp2 + req->rq_req.ost->buflen2;
331 objcount = r->buflen1 / sizeof(*ioo);
332 niocount = r->buflen2 / sizeof(*nb);
335 conn.oc_id = req->rq_req.ost->connid;
336 conn.oc_dev = req->rq_ost->ost_tgt;
338 rc = ost_pack_rep(NULL, niocount, NULL, 0,
339 &req->rq_rephdr, &req->rq_rep.ost,
340 &req->rq_replen, &req->rq_repbuf);
342 printk("ost_create: cannot pack reply\n");
345 res = ost_rep_buf1(req->rq_rep.ost);
347 for (i=0; i < objcount; i++) {
348 ost_unpack_ioo((void *)&tmp1, &ioo);
349 if (tmp2 + ioo->ioo_bufcnt > end2) {
353 for (j = 0 ; j < ioo->ioo_bufcnt ; j++) {
354 ost_unpack_niobuf((void *)&tmp2, &nb);
358 /* The unpackers move tmp1 and tmp2, so reset them before using */
359 tmp1 = ost_req_buf1(r);
360 tmp2 = ost_req_buf2(r);
361 req->rq_rep.ost->result =
362 req->rq_ost->ost_tgt->obd_type->typ_ops->o_preprw
363 (cmd, &conn, objcount, (struct obd_ioobj *)tmp1,
364 niocount, (struct niobuf *)tmp2, (struct niobuf *)res);
366 if (cmd == OBD_BRW_WRITE) {
367 for (i=0; i<niocount; i++) {
368 src = &((struct niobuf *)tmp2)[i];
369 dst = &((struct niobuf *)res)[i];
370 memcpy((void *)(unsigned long)dst->addr,
371 (void *)(unsigned long)src->addr,
375 for (i=0; i<niocount; i++) {
376 dst = &((struct niobuf *)tmp2)[i];
377 src = &((struct niobuf *)res)[i];
378 memcpy((void *)(unsigned long)dst->addr,
379 (void *)(unsigned long)src->addr,
384 req->rq_rep.ost->result =
385 req->rq_ost->ost_tgt->obd_type->typ_ops->o_commitrw
386 (cmd, &conn, objcount, (struct obd_ioobj *)tmp1,
387 niocount, (struct niobuf *)res);
393 int ost_handle(struct obd_device *obddev, struct ptlrpc_request *req)
396 struct ost_obd *ost = &obddev->u.ost;
397 struct ptlreq_hdr *hdr;
400 CDEBUG(D_OST, "req at %p\n", req);
402 hdr = (struct ptlreq_hdr *)req->rq_reqbuf;
403 if (NTOH__u32(hdr->type) != OST_TYPE_REQ) {
404 printk("lustre_ost: wrong packet type sent %d\n",
405 NTOH__u32(hdr->type));
410 rc = ost_unpack_req(req->rq_reqbuf, req->rq_reqlen,
411 &req->rq_reqhdr, &req->rq_req.ost);
413 printk("lustre_ost: Invalid request\n");
418 switch (req->rq_reqhdr->opc) {
421 CDEBUG(D_INODE, "connect\n");
422 rc = ost_connect(ost, req);
425 CDEBUG(D_INODE, "disconnect\n");
426 rc = ost_disconnect(ost, req);
429 CDEBUG(D_INODE, "get_info\n");
430 rc = ost_get_info(ost, req);
433 CDEBUG(D_INODE, "create\n");
434 rc = ost_create(ost, req);
437 CDEBUG(D_INODE, "destroy\n");
438 rc = ost_destroy(ost, req);
441 CDEBUG(D_INODE, "getattr\n");
442 rc = ost_getattr(ost, req);
445 CDEBUG(D_INODE, "setattr\n");
446 rc = ost_setattr(ost, req);
449 CDEBUG(D_INODE, "brw\n");
450 rc = ost_brw(ost, req);
453 req->rq_status = -ENOTSUPP;
454 return ost_error(obddev, req);
460 printk("ost: processing error %d\n", rc);
461 ost_error(obddev, req);
463 CDEBUG(D_INODE, "sending reply\n");
464 ost_reply(obddev, req);
470 int ost_main(void *arg)
472 struct obd_device *obddev = (struct obd_device *) arg;
473 struct ost_obd *ost = &obddev->u.ost;
478 spin_lock_irq(¤t->sigmask_lock);
479 sigfillset(¤t->blocked);
480 recalc_sigpending(current);
481 spin_unlock_irq(¤t->sigmask_lock);
483 sprintf(current->comm, "lustre_ost");
485 /* Record that the thread is running */
486 ost->ost_thread = current;
487 wake_up(&ost->ost_done_waitq);
489 /* XXX maintain a list of all managed devices: insert here */
491 /* And now, wait forever for commit wakeup events. */
495 if (ost->ost_flags & OST_EXIT)
498 wake_up(&ost->ost_done_waitq);
499 interruptible_sleep_on(&ost->ost_waitq);
501 CDEBUG(D_INODE, "lustre_ost wakes\n");
502 CDEBUG(D_INODE, "pick up req here and continue\n");
505 if (ost->ost_service != NULL) {
509 struct ptlrpc_request request;
510 struct ptlrpc_service *service;
512 rc = PtlEQGet(ost->ost_service->srv_eq_h, &ev);
513 if (rc != PTL_OK && rc != PTL_EQ_DROPPED)
516 service = (struct ptlrpc_service *)ev.mem_desc.user_ptr;
518 /* FIXME: If we move to an event-driven model,
519 * we should put the request on the stack of
520 * mds_handle instead. */
521 memset(&request, 0, sizeof(request));
522 request.rq_reqbuf = ev.mem_desc.start +
524 request.rq_reqlen = ev.mem_desc.length;
525 request.rq_ost = ost;
526 request.rq_xid = ev.match_bits;
528 request.rq_peer.peer_nid = ev.initiator.nid;
529 /* FIXME: this NI should be the incoming NI.
530 * We don't know how to find that from here. */
531 request.rq_peer.peer_ni =
532 ost->ost_service->srv_self.peer_ni;
533 rc = ost_handle(obddev, &request);
535 /* Inform the rpc layer the event has been handled */
536 ptl_received_rpc(service);
539 struct ptlrpc_request *request;
541 if (list_empty(&ost->ost_reqs)) {
542 CDEBUG(D_INODE, "woke because of timer\n");
544 request = list_entry(ost->ost_reqs.next,
545 struct ptlrpc_request,
547 list_del(&request->rq_list);
548 rc = ost_handle(obddev, request);
553 /* XXX maintain a list of all managed devices: cleanup here */
555 ost->ost_thread = NULL;
556 wake_up(&ost->ost_done_waitq);
557 printk("lustre_ost: exiting\n");
561 static void ost_stop_srv_thread(struct ost_obd *ost)
563 ost->ost_flags |= OST_EXIT;
565 while (ost->ost_thread) {
566 wake_up(&ost->ost_waitq);
567 sleep_on(&ost->ost_done_waitq);
571 static void ost_start_srv_thread(struct obd_device *obd)
573 struct ost_obd *ost = &obd->u.ost;
576 init_waitqueue_head(&ost->ost_waitq);
577 init_waitqueue_head(&ost->ost_done_waitq);
578 kernel_thread(ost_main, (void *)obd,
579 CLONE_VM | CLONE_FS | CLONE_FILES);
580 while (!ost->ost_thread)
581 sleep_on(&ost->ost_done_waitq);
585 /* mount the file system (secretly) */
586 static int ost_setup(struct obd_device *obddev, obd_count len,
590 struct obd_ioctl_data* data = buf;
591 struct ost_obd *ost = &obddev->u.ost;
592 struct obd_device *tgt;
593 struct lustre_peer peer;
597 if (data->ioc_dev < 0 || data->ioc_dev > MAX_OBD_DEVICES) {
602 tgt = &obd_dev[data->ioc_dev];
604 if ( ! (tgt->obd_flags & OBD_ATTACHED) ||
605 ! (tgt->obd_flags & OBD_SET_UP) ){
606 printk("device not attached or not set up (%d)\n",
612 ost->ost_conn.oc_dev = tgt;
613 err = tgt->obd_type->typ_ops->o_connect(&ost->ost_conn);
615 printk("lustre ost: fail to connect to device %d\n",
620 INIT_LIST_HEAD(&ost->ost_reqs);
621 ost->ost_thread = NULL;
624 spin_lock_init(&obddev->u.ost.ost_lock);
626 err = kportal_uuid_to_peer("self", &peer);
628 OBD_ALLOC(ost->ost_service, sizeof(*ost->ost_service));
629 if (ost->ost_service == NULL)
631 ost->ost_service->srv_buf_size = 64 * 1024;
632 ost->ost_service->srv_portal = OST_REQUEST_PORTAL;
633 memcpy(&ost->ost_service->srv_self, &peer, sizeof(peer));
634 ost->ost_service->srv_wait_queue = &ost->ost_waitq;
636 rpc_register_service(ost->ost_service, "self");
639 ost_start_srv_thread(obddev);
646 static int ost_cleanup(struct obd_device * obddev)
648 struct ost_obd *ost = &obddev->u.ost;
649 struct obd_device *tgt;
654 if ( !(obddev->obd_flags & OBD_SET_UP) ) {
659 if ( !list_empty(&obddev->obd_gen_clients) ) {
660 printk(KERN_WARNING __FUNCTION__ ": still has clients!\n");
665 ost_stop_srv_thread(ost);
666 rpc_unregister_service(ost->ost_service);
667 OBD_FREE(ost->ost_service, sizeof(*ost->ost_service));
669 if (!list_empty(&ost->ost_reqs)) {
670 // XXX reply with errors and clean up
671 CDEBUG(D_INODE, "Request list not empty!\n");
675 err = tgt->obd_type->typ_ops->o_disconnect(&ost->ost_conn);
677 printk("lustre ost: fail to disconnect device\n");
687 /* use obd ops to offer management infrastructure */
688 static struct obd_ops ost_obd_ops = {
690 o_cleanup: ost_cleanup,
693 static int __init ost_init(void)
695 obd_register_type(&ost_obd_ops, LUSTRE_OST_NAME);
699 static void __exit ost_exit(void)
701 obd_unregister_type(LUSTRE_OST_NAME);
704 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
705 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
706 MODULE_LICENSE("GPL");
708 // for testing (maybe this stays)
709 EXPORT_SYMBOL(ost_queue_req);
711 module_init(ost_init);
712 module_exit(ost_exit);