1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
6 * This code is issued under the GNU General Public License.
7 * See the file COPYING in this distribution
9 * Author Peter Braam <braam@clusterfs.com>
11 * This server is single threaded at present (but can easily be multi
12 * threaded). For testing and management it is treated as an
13 * obd_device, although it does not export a full OBD method table
14 * (the requests are coming in over the wire, so object target
15 * modules do not have a full method table.)
20 #define DEBUG_SUBSYSTEM S_OSC
22 #include <linux/module.h>
23 #include <linux/lustre_dlm.h>
24 #include <linux/lustre_mds.h> /* for mds_objid */
25 #include <linux/obd_ost.h>
26 #include <linux/obd_lov.h>
27 #include <linux/ctype.h>
28 #include <linux/init.h>
29 #include <linux/lustre_ha.h>
30 #include <linux/obd_support.h> /* for OBD_FAIL_CHECK */
31 #include <linux/lustre_lite.h> /* for ll_i2info */
33 static int osc_getattr(struct lustre_handle *conn, struct obdo *oa,
34 struct lov_stripe_md *md)
36 struct ptlrpc_request *request;
37 struct ost_body *body;
38 int rc, size = sizeof(*body);
41 request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_GETATTR, 1, &size,
46 body = lustre_msg_buf(request->rq_reqmsg, 0);
47 #warning FIXME: pack only valid fields instead of memcpy, endianness
48 memcpy(&body->oa, oa, sizeof(*oa));
50 request->rq_replen = lustre_msg_size(1, &size);
52 rc = ptlrpc_queue_wait(request);
53 rc = ptlrpc_check_status(request, rc);
55 CERROR("%s failed: rc = %d\n", __FUNCTION__, rc);
59 body = lustre_msg_buf(request->rq_repmsg, 0);
60 CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
62 memcpy(oa, &body->oa, sizeof(*oa));
66 ptlrpc_free_req(request);
70 static int osc_open(struct lustre_handle *conn, struct obdo *oa,
71 struct lov_stripe_md *md)
73 struct ptlrpc_request *request;
74 struct ost_body *body;
75 int rc, size = sizeof(*body);
78 request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_OPEN, 1, &size,
83 body = lustre_msg_buf(request->rq_reqmsg, 0);
84 #warning FIXME: pack only valid fields instead of memcpy, endianness
85 memcpy(&body->oa, oa, sizeof(*oa));
87 request->rq_replen = lustre_msg_size(1, &size);
89 rc = ptlrpc_queue_wait(request);
90 rc = ptlrpc_check_status(request, rc);
94 body = lustre_msg_buf(request->rq_repmsg, 0);
95 CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
97 memcpy(oa, &body->oa, sizeof(*oa));
101 ptlrpc_free_req(request);
105 static int osc_close(struct lustre_handle *conn, struct obdo *oa,
106 struct lov_stripe_md *md)
108 struct ptlrpc_request *request;
109 struct ost_body *body;
110 int rc, size = sizeof(*body);
113 request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_CLOSE, 1, &size,
118 body = lustre_msg_buf(request->rq_reqmsg, 0);
119 #warning FIXME: pack only valid fields instead of memcpy, endianness
120 memcpy(&body->oa, oa, sizeof(*oa));
122 request->rq_replen = lustre_msg_size(1, &size);
124 rc = ptlrpc_queue_wait(request);
125 rc = ptlrpc_check_status(request, rc);
129 body = lustre_msg_buf(request->rq_repmsg, 0);
130 CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
132 memcpy(oa, &body->oa, sizeof(*oa));
136 ptlrpc_free_req(request);
140 static int osc_setattr(struct lustre_handle *conn, struct obdo *oa,
141 struct lov_stripe_md *md)
143 struct ptlrpc_request *request;
144 struct ost_body *body;
145 int rc, size = sizeof(*body);
148 request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_SETATTR, 1, &size,
153 body = lustre_msg_buf(request->rq_reqmsg, 0);
154 memcpy(&body->oa, oa, sizeof(*oa));
156 request->rq_replen = lustre_msg_size(1, &size);
158 rc = ptlrpc_queue_wait(request);
159 rc = ptlrpc_check_status(request, rc);
163 ptlrpc_free_req(request);
167 static int osc_create(struct lustre_handle *conn, struct obdo *oa,
168 struct lov_stripe_md **ea)
170 struct ptlrpc_request *request;
171 struct ost_body *body;
172 int rc, size = sizeof(*body);
185 OBD_ALLOC(*ea, oa->o_easize);
188 (*ea)->lmd_easize = oa->o_easize;
191 request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_CREATE, 1, &size,
196 body = lustre_msg_buf(request->rq_reqmsg, 0);
197 memcpy(&body->oa, oa, sizeof(*oa));
199 request->rq_replen = lustre_msg_size(1, &size);
201 rc = ptlrpc_queue_wait(request);
202 rc = ptlrpc_check_status(request, rc);
206 body = lustre_msg_buf(request->rq_repmsg, 0);
207 memcpy(oa, &body->oa, sizeof(*oa));
209 (*ea)->lmd_object_id = oa->o_id;
210 (*ea)->lmd_stripe_count = 1;
213 ptlrpc_free_req(request);
217 static int osc_punch(struct lustre_handle *conn, struct obdo *oa,
218 struct lov_stripe_md *md, obd_size start,
221 struct ptlrpc_request *request;
222 struct ost_body *body;
223 int rc, size = sizeof(*body);
231 request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_PUNCH, 1, &size,
236 body = lustre_msg_buf(request->rq_reqmsg, 0);
237 #warning FIXME: pack only valid fields instead of memcpy, endianness, valid
238 memcpy(&body->oa, oa, sizeof(*oa));
240 /* overload the blocks and size fields in the oa with start/end */
241 #warning FIXME: endianness, size=start, blocks=end?
242 body->oa.o_blocks = start;
243 body->oa.o_size = end;
244 body->oa.o_valid |= OBD_MD_FLBLOCKS | OBD_MD_FLSIZE;
246 request->rq_replen = lustre_msg_size(1, &size);
248 rc = ptlrpc_queue_wait(request);
249 rc = ptlrpc_check_status(request, rc);
253 body = lustre_msg_buf(request->rq_repmsg, 0);
254 memcpy(oa, &body->oa, sizeof(*oa));
258 ptlrpc_free_req(request);
262 static int osc_destroy(struct lustre_handle *conn, struct obdo *oa,
263 struct lov_stripe_md *ea)
265 struct ptlrpc_request *request;
266 struct ost_body *body;
267 int rc, size = sizeof(*body);
274 request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_DESTROY, 1, &size,
279 body = lustre_msg_buf(request->rq_reqmsg, 0);
280 #warning FIXME: pack only valid fields instead of memcpy, endianness
281 memcpy(&body->oa, oa, sizeof(*oa));
283 request->rq_replen = lustre_msg_size(1, &size);
285 rc = ptlrpc_queue_wait(request);
286 rc = ptlrpc_check_status(request, rc);
290 body = lustre_msg_buf(request->rq_repmsg, 0);
291 memcpy(oa, &body->oa, sizeof(*oa));
295 ptlrpc_free_req(request);
299 struct osc_brw_cb_data {
300 brw_callback_t callback;
306 /* Our bulk-unmapping bottom half. */
307 static void unmap_and_decref_bulk_desc(void *data)
309 struct ptlrpc_bulk_desc *desc = data;
310 struct list_head *tmp;
313 /* This feels wrong to me. */
314 list_for_each(tmp, &desc->bd_page_list) {
315 struct ptlrpc_bulk_page *bulk;
316 bulk = list_entry(tmp, struct ptlrpc_bulk_page, bp_link);
318 kunmap(bulk->bp_page);
321 ptlrpc_bulk_decref(desc);
325 static void brw_finish(struct ptlrpc_bulk_desc *desc, void *data)
327 struct osc_brw_cb_data *cb_data = data;
331 if (desc->bd_flags & PTL_RPC_FL_TIMEOUT) {
332 err = (desc->bd_flags & PTL_RPC_FL_INTR ? -ERESTARTSYS :
336 if (cb_data->callback)
337 cb_data->callback(cb_data->cb_data, err, CB_PHASE_FINISH);
339 OBD_FREE(cb_data->obd_data, cb_data->obd_size);
340 OBD_FREE(cb_data, sizeof(*cb_data));
342 /* We can't kunmap the desc from interrupt context, so we do it from
343 * the bottom half above. */
344 INIT_TQUEUE(&desc->bd_queue, 0, 0);
345 PREPARE_TQUEUE(&desc->bd_queue, unmap_and_decref_bulk_desc, desc);
346 schedule_task(&desc->bd_queue);
351 static int osc_brw_read(struct lustre_handle *conn, struct lov_stripe_md *md,
352 obd_count page_count, struct brw_page *pga,
353 brw_callback_t callback, struct io_cb_data *data)
355 struct ptlrpc_connection *connection =
356 client_conn2cli(conn)->cl_import.imp_connection;
357 struct ptlrpc_request *request = NULL;
358 struct ptlrpc_bulk_desc *desc = NULL;
359 struct ost_body *body;
360 struct osc_brw_cb_data *cb_data = NULL;
361 int rc, size[3] = {sizeof(*body)};
362 void *iooptr, *nioptr;
367 size[1] = sizeof(struct obd_ioobj);
368 size[2] = page_count * sizeof(struct niobuf_remote);
370 request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_READ, 3, size,
375 body = lustre_msg_buf(request->rq_reqmsg, 0);
377 desc = ptlrpc_prep_bulk(connection);
379 GOTO(out_req, rc = -ENOMEM);
380 desc->bd_portal = OST_BULK_PORTAL;
381 desc->bd_cb = brw_finish;
382 OBD_ALLOC(cb_data, sizeof(*cb_data));
384 GOTO(out_desc, rc = -ENOMEM);
386 cb_data->callback = callback;
387 cb_data->cb_data = data;
389 desc->bd_cb_data = cb_data;
391 iooptr = lustre_msg_buf(request->rq_reqmsg, 1);
392 nioptr = lustre_msg_buf(request->rq_reqmsg, 2);
393 ost_pack_ioo(&iooptr, md, page_count);
394 /* end almost identical to brw_write case */
396 spin_lock(&connection->c_lock);
397 xid = ++connection->c_xid_out; /* single xid for all pages */
398 spin_unlock(&connection->c_lock);
400 for (mapped = 0; mapped < page_count; mapped++) {
401 struct ptlrpc_bulk_page *bulk = ptlrpc_prep_bulk_page(desc);
403 GOTO(out_unmap, rc = -ENOMEM);
405 bulk->bp_xid = xid; /* single xid for all pages */
407 bulk->bp_buf = kmap(pga[mapped].pg);
408 bulk->bp_page = pga[mapped].pg;
409 bulk->bp_buflen = PAGE_SIZE;
410 ost_pack_niobuf(&nioptr, pga[mapped].off, pga[mapped].count,
411 pga[mapped].flag, bulk->bp_xid);
415 * Register the bulk first, because the reply could arrive out of order,
416 * and we want to be ready for the bulk data.
418 * The reference is released when brw_finish is complete.
420 * On error, we never do the brw_finish, so we handle all decrefs.
422 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_BRW_READ_BULK)) {
423 CERROR("obd_fail_loc=%x, skipping register_bulk\n",
424 OBD_FAIL_OSC_BRW_READ_BULK);
426 rc = ptlrpc_register_bulk(desc);
431 request->rq_replen = lustre_msg_size(1, size);
432 rc = ptlrpc_queue_wait(request);
433 rc = ptlrpc_check_status(request, rc);
436 * XXX: If there is an error during the processing of the callback,
437 * such as a timeout in a sleep that it performs, brw_finish
438 * will never get called, and we'll leak the desc, fail to kunmap
439 * things, cats will live with dogs. One solution would be to
440 * export brw_finish as osc_brw_finish, so that the timeout case and
441 * its kin could call it for proper cleanup. An alternative would
442 * be for an error return from the callback to cause us to clean up,
443 * but that doesn't help the truly async cases (like LOV), which
444 * will immediately return from their PHASE_START callback, before
445 * any such cleanup-requiring error condition can be detected.
450 /* Callbacks cause asynchronous handling. */
451 rc = callback(data, 0, CB_PHASE_START);
454 ptlrpc_req_finished(request);
457 /* Clean up on error. */
460 kunmap(pga[mapped].pg);
461 OBD_FREE(cb_data, sizeof(*cb_data));
463 ptlrpc_bulk_decref(desc);
467 static int osc_brw_write(struct lustre_handle *conn, struct lov_stripe_md *md,
468 obd_count page_count, struct brw_page *pga,
469 brw_callback_t callback, struct io_cb_data *data)
471 struct ptlrpc_connection *connection =
472 client_conn2cli(conn)->cl_import.imp_connection;
473 struct ptlrpc_request *request = NULL;
474 struct ptlrpc_bulk_desc *desc = NULL;
475 struct ost_body *body;
476 struct niobuf_local *local = NULL;
477 struct niobuf_remote *remote;
478 struct osc_brw_cb_data *cb_data = NULL;
479 int rc, j, size[3] = {sizeof(*body)};
480 void *iooptr, *nioptr;
484 size[1] = sizeof(struct obd_ioobj);
485 size[2] = page_count * sizeof(*remote);
487 request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_WRITE, 3, size,
492 body = lustre_msg_buf(request->rq_reqmsg, 0);
494 desc = ptlrpc_prep_bulk(connection);
496 GOTO(out_req, rc = -ENOMEM);
497 desc->bd_portal = OSC_BULK_PORTAL;
498 desc->bd_cb = brw_finish;
499 OBD_ALLOC(cb_data, sizeof(*cb_data));
501 GOTO(out_desc, rc = -ENOMEM);
503 cb_data->callback = callback;
504 cb_data->cb_data = data;
506 desc->bd_cb_data = cb_data;
508 iooptr = lustre_msg_buf(request->rq_reqmsg, 1);
509 nioptr = lustre_msg_buf(request->rq_reqmsg, 2);
510 ost_pack_ioo(&iooptr, md, page_count);
511 /* end almost identical to brw_read case */
513 OBD_ALLOC(local, page_count * sizeof(*local));
515 GOTO(out_cb, rc = -ENOMEM);
517 cb_data->obd_data = local;
518 cb_data->obd_size = page_count * sizeof(*local);
520 for (mapped = 0; mapped < page_count; mapped++) {
521 local[mapped].addr = kmap(pga[mapped].pg);
522 local[mapped].offset = pga[mapped].off;
523 local[mapped].len = pga[mapped].count;
524 ost_pack_niobuf(&nioptr, pga[mapped].off, pga[mapped].count,
525 pga[mapped].flag, 0);
528 size[1] = page_count * sizeof(*remote);
529 request->rq_replen = lustre_msg_size(2, size);
530 rc = ptlrpc_queue_wait(request);
531 rc = ptlrpc_check_status(request, rc);
535 nioptr = lustre_msg_buf(request->rq_repmsg, 1);
537 GOTO(out_unmap, rc = -EINVAL);
539 if (request->rq_repmsg->buflens[1] != size[1]) {
540 CERROR("buffer length wrong (%d vs. %d)\n",
541 request->rq_repmsg->buflens[1], size[1]);
542 GOTO(out_unmap, rc = -EINVAL);
545 for (j = 0; j < page_count; j++) {
546 struct ptlrpc_bulk_page *bulk;
548 ost_unpack_niobuf(&nioptr, &remote);
550 bulk = ptlrpc_prep_bulk_page(desc);
552 GOTO(out_unmap, rc = -ENOMEM);
554 bulk->bp_buf = (void *)(unsigned long)local[j].addr;
555 bulk->bp_buflen = local[j].len;
556 bulk->bp_xid = remote->xid;
557 bulk->bp_page = pga[j].pg;
560 if (desc->bd_page_count != page_count)
563 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_BRW_WRITE_BULK))
564 GOTO(out_unmap, rc = 0);
566 /* Our reference is released when brw_finish is complete. */
567 rc = ptlrpc_send_bulk(desc);
569 /* XXX: Mike, same question as in osc_brw_read. */
573 /* Callbacks cause asynchronous handling. */
574 rc = callback(data, 0, CB_PHASE_START);
577 ptlrpc_req_finished(request);
580 /* Clean up on error. */
583 kunmap(pga[mapped].pg);
585 OBD_FREE(local, page_count * sizeof(*local));
587 OBD_FREE(cb_data, sizeof(*cb_data));
589 ptlrpc_bulk_decref(desc);
593 static int osc_brw(int cmd, struct lustre_handle *conn,
594 struct lov_stripe_md *md, obd_count page_count,
595 struct brw_page *pga, brw_callback_t callback,
596 struct io_cb_data *data)
598 if (cmd & OBD_BRW_WRITE)
599 return osc_brw_write(conn, md, page_count, pga, callback, data);
601 return osc_brw_read(conn, md, page_count, pga, callback, data);
604 static int osc_enqueue(struct lustre_handle *connh, struct lov_stripe_md *md,
605 struct lustre_handle *parent_lock,
606 __u32 type, void *extentp, int extent_len, __u32 mode,
607 int *flags, void *callback, void *data, int datalen,
608 struct lustre_handle *lockh)
610 __u64 res_id[RES_NAME_SIZE] = { md->lmd_object_id };
611 struct obd_device *obddev = class_conn2obd(connh);
612 struct ldlm_extent *extent = extentp;
616 /* Filesystem locks are given a bit of special treatment: first we
617 * fixup the lock to start and end on page boundaries. */
618 extent->start &= PAGE_MASK;
619 extent->end = (extent->end + PAGE_SIZE - 1) & PAGE_MASK;
621 /* Next, search for already existing extent locks that will cover us */
622 //osc_con2dlmcl(conn, &cl, &connection, &rconn);
623 rc = ldlm_lock_match(obddev->obd_namespace, res_id, type, extent,
624 sizeof(extent), mode, lockh);
626 /* We already have a lock, and it's referenced */
630 /* Next, search for locks that we can upgrade (if we're trying to write)
631 * or are more than we need (if we're trying to read). Because the VFS
632 * and page cache already protect us locally, lots of readers/writers
633 * can share a single PW lock. */
639 rc = ldlm_lock_match(obddev->obd_namespace, res_id, type, extent,
640 sizeof(extent), mode2, lockh);
643 /* FIXME: This is not incredibly elegant, but it might
644 * be more elegant than adding another parameter to
645 * lock_match. I want a second opinion. */
646 ldlm_lock_addref(lockh, mode);
647 ldlm_lock_decref(lockh, mode2);
652 rc = ldlm_cli_convert(lockh, mode, &flags);
659 rc = ldlm_cli_enqueue(connh, NULL, obddev->obd_namespace,
660 parent_lock, res_id, type, extent,
661 sizeof(extent), mode, flags, ldlm_completion_ast,
662 callback, data, datalen, lockh);
666 static int osc_cancel(struct lustre_handle *oconn, struct lov_stripe_md *md,
667 __u32 mode, struct lustre_handle *lockh)
671 ldlm_lock_decref(lockh, mode);
676 static int osc_statfs(struct lustre_handle *conn, struct statfs *sfs)
678 struct ptlrpc_request *request;
679 struct obd_statfs *osfs;
680 int rc, size = sizeof(*osfs);
683 request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_STATFS, 0, NULL,
688 request->rq_replen = lustre_msg_size(1, &size);
690 rc = ptlrpc_queue_wait(request);
691 rc = ptlrpc_check_status(request, rc);
693 CERROR("%s failed: rc = %d\n", __FUNCTION__, rc);
697 osfs = lustre_msg_buf(request->rq_repmsg, 0);
698 obd_statfs_unpack(osfs, sfs);
702 ptlrpc_free_req(request);
706 static int osc_iocontrol(long cmd, struct lustre_handle *conn, int len,
707 void *karg, void *uarg)
709 struct obd_device *obddev = class_conn2obd(conn);
710 struct obd_ioctl_data *data = karg;
714 if (_IOC_TYPE(cmd) != IOC_LDLM_TYPE || _IOC_NR(cmd) <
715 IOC_LDLM_MIN_NR || _IOC_NR(cmd) > IOC_LDLM_MAX_NR) {
716 CDEBUG(D_IOCTL, "invalid ioctl (type %ld, nr %ld, size %ld)\n",
717 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
722 case IOC_LDLM_TEST: {
723 err = ldlm_test(obddev, conn);
724 CERROR("-- done err %d\n", err);
727 case IOC_LDLM_REGRESS_START: {
728 unsigned int numthreads = 1;
729 unsigned int numheld = 10;
730 unsigned int numres = 10;
731 unsigned int numext = 10;
734 if (data->ioc_inllen1) {
735 parse = data->ioc_inlbuf1;
736 if (*parse != '\0') {
737 while(isspace(*parse)) parse++;
738 numthreads = simple_strtoul(parse, &parse, 0);
739 while(isspace(*parse)) parse++;
741 if (*parse != '\0') {
742 while(isspace(*parse)) parse++;
743 numheld = simple_strtoul(parse, &parse, 0);
744 while(isspace(*parse)) parse++;
746 if (*parse != '\0') {
747 while(isspace(*parse)) parse++;
748 numres = simple_strtoul(parse, &parse, 0);
749 while(isspace(*parse)) parse++;
751 if (*parse != '\0') {
752 while(isspace(*parse)) parse++;
753 numext = simple_strtoul(parse, &parse, 0);
754 while(isspace(*parse)) parse++;
758 err = ldlm_regression_start(obddev, conn, numthreads,
759 numheld, numres, numext);
761 CERROR("-- done err %d\n", err);
764 case IOC_LDLM_REGRESS_STOP: {
765 err = ldlm_regression_stop();
766 CERROR("-- done err %d\n", err);
770 GOTO(out, err = -EINVAL);
776 struct obd_ops osc_obd_ops = {
777 o_setup: client_obd_setup,
778 o_cleanup: client_obd_cleanup,
779 o_statfs: osc_statfs,
780 o_create: osc_create,
781 o_destroy: osc_destroy,
782 o_getattr: osc_getattr,
783 o_setattr: osc_setattr,
786 o_connect: client_obd_connect,
787 o_disconnect: client_obd_disconnect,
790 o_enqueue: osc_enqueue,
791 o_cancel: osc_cancel,
792 o_iocontrol: osc_iocontrol
795 static int __init osc_init(void)
797 return class_register_type(&osc_obd_ops, LUSTRE_OSC_NAME);
800 static void __exit osc_exit(void)
802 class_unregister_type(LUSTRE_OSC_NAME);
805 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
806 MODULE_DESCRIPTION("Lustre Object Storage Client (OSC) v1.0");
807 MODULE_LICENSE("GPL");
809 module_init(osc_init);
810 module_exit(osc_exit);