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,
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,
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);
161 ptlrpc_free_req(request);
165 static int osc_create(struct lustre_handle *conn, struct obdo *oa,
166 struct lov_stripe_md **ea)
168 struct ptlrpc_request *request;
169 struct ost_body *body;
170 int rc, size = sizeof(*body);
183 OBD_ALLOC(*ea, oa->o_easize);
186 (*ea)->lmd_easize = oa->o_easize;
189 request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_CREATE, 1, &size,
194 body = lustre_msg_buf(request->rq_reqmsg, 0);
195 memcpy(&body->oa, oa, sizeof(*oa));
197 request->rq_replen = lustre_msg_size(1, &size);
199 rc = ptlrpc_queue_wait(request);
200 rc = ptlrpc_check_status(request, rc);
204 body = lustre_msg_buf(request->rq_repmsg, 0);
205 memcpy(oa, &body->oa, sizeof(*oa));
207 (*ea)->lmd_object_id = oa->o_id;
208 (*ea)->lmd_stripe_count = 1;
211 ptlrpc_free_req(request);
215 static int osc_punch(struct lustre_handle *conn, struct obdo *oa,
216 struct lov_stripe_md *md, obd_size start,
219 struct ptlrpc_request *request;
220 struct ost_body *body;
221 int rc, size = sizeof(*body);
229 request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_PUNCH, 1, &size,
234 body = lustre_msg_buf(request->rq_reqmsg, 0);
235 #warning FIXME: pack only valid fields instead of memcpy, endianness, valid
236 memcpy(&body->oa, oa, sizeof(*oa));
238 /* overload the blocks and size fields in the oa with start/end */
239 #warning FIXME: endianness, size=start, blocks=end?
240 body->oa.o_blocks = start;
241 body->oa.o_size = end;
242 body->oa.o_valid |= OBD_MD_FLBLOCKS | OBD_MD_FLSIZE;
244 request->rq_replen = lustre_msg_size(1, &size);
246 rc = ptlrpc_queue_wait(request);
247 rc = ptlrpc_check_status(request, rc);
251 body = lustre_msg_buf(request->rq_repmsg, 0);
252 memcpy(oa, &body->oa, sizeof(*oa));
256 ptlrpc_free_req(request);
260 static int osc_destroy(struct lustre_handle *conn, struct obdo *oa,
261 struct lov_stripe_md *ea)
263 struct ptlrpc_request *request;
264 struct ost_body *body;
265 int rc, size = sizeof(*body);
272 request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_DESTROY, 1,
277 body = lustre_msg_buf(request->rq_reqmsg, 0);
278 #warning FIXME: pack only valid fields instead of memcpy, endianness
279 memcpy(&body->oa, oa, sizeof(*oa));
281 request->rq_replen = lustre_msg_size(1, &size);
283 rc = ptlrpc_queue_wait(request);
284 rc = ptlrpc_check_status(request, rc);
288 body = lustre_msg_buf(request->rq_repmsg, 0);
289 memcpy(oa, &body->oa, sizeof(*oa));
293 ptlrpc_free_req(request);
297 struct osc_brw_cb_data {
298 brw_callback_t callback;
304 /* Our bulk-unmapping bottom half. */
305 static void unmap_and_decref_bulk_desc(void *data)
307 struct ptlrpc_bulk_desc *desc = data;
308 struct list_head *tmp;
311 /* This feels wrong to me. */
312 list_for_each(tmp, &desc->bd_page_list) {
313 struct ptlrpc_bulk_page *bulk;
314 bulk = list_entry(tmp, struct ptlrpc_bulk_page, bp_link);
316 kunmap(bulk->bp_page);
319 ptlrpc_bulk_decref(desc);
323 static void brw_finish(struct ptlrpc_bulk_desc *desc, void *data)
325 struct osc_brw_cb_data *cb_data = data;
329 if (desc->bd_flags & PTL_RPC_FL_TIMEOUT) {
330 err = (desc->bd_flags & PTL_RPC_FL_INTR ? -ERESTARTSYS :
334 if (cb_data->callback)
335 cb_data->callback(cb_data->cb_data, err, CB_PHASE_FINISH);
337 OBD_FREE(cb_data->obd_data, cb_data->obd_size);
338 OBD_FREE(cb_data, sizeof(*cb_data));
340 /* We can't kunmap the desc from interrupt context, so we do it from
341 * the bottom half above. */
342 INIT_TQUEUE(&desc->bd_queue, 0, 0);
343 PREPARE_TQUEUE(&desc->bd_queue, unmap_and_decref_bulk_desc, desc);
344 schedule_task(&desc->bd_queue);
349 static int osc_brw_read(struct lustre_handle *conn, struct lov_stripe_md *md,
350 obd_count page_count, struct brw_page *pga,
351 brw_callback_t callback, struct io_cb_data *data)
353 struct ptlrpc_connection *connection =
354 client_conn2cli(conn)->cl_import.imp_connection;
355 struct ptlrpc_request *request = NULL;
356 struct ptlrpc_bulk_desc *desc = NULL;
357 struct ost_body *body;
358 struct osc_brw_cb_data *cb_data = NULL;
359 int rc, size[3] = {sizeof(*body)};
360 void *iooptr, *nioptr;
365 size[1] = sizeof(struct obd_ioobj);
366 size[2] = page_count * sizeof(struct niobuf_remote);
368 request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_READ, 3, size,
373 body = lustre_msg_buf(request->rq_reqmsg, 0);
375 desc = ptlrpc_prep_bulk(connection);
377 GOTO(out_req, rc = -ENOMEM);
378 desc->bd_portal = OST_BULK_PORTAL;
379 desc->bd_cb = brw_finish;
380 OBD_ALLOC(cb_data, sizeof(*cb_data));
382 GOTO(out_desc, rc = -ENOMEM);
384 cb_data->callback = callback;
385 cb_data->cb_data = data;
387 desc->bd_cb_data = cb_data;
389 iooptr = lustre_msg_buf(request->rq_reqmsg, 1);
390 nioptr = lustre_msg_buf(request->rq_reqmsg, 2);
391 ost_pack_ioo(&iooptr, md, page_count);
392 /* end almost identical to brw_write case */
394 spin_lock(&connection->c_lock);
395 xid = ++connection->c_xid_out; /* single xid for all pages */
396 spin_unlock(&connection->c_lock);
398 for (mapped = 0; mapped < page_count; mapped++) {
399 struct ptlrpc_bulk_page *bulk = ptlrpc_prep_bulk_page(desc);
401 GOTO(out_unmap, rc = -ENOMEM);
403 bulk->bp_xid = xid; /* single xid for all pages */
405 bulk->bp_buf = kmap(pga[mapped].pg);
406 bulk->bp_page = pga[mapped].pg;
407 bulk->bp_buflen = PAGE_SIZE;
408 ost_pack_niobuf(&nioptr, pga[mapped].off, pga[mapped].count,
409 pga[mapped].flag, bulk->bp_xid);
413 * Register the bulk first, because the reply could arrive out of order,
414 * and we want to be ready for the bulk data.
416 * The reference is released when brw_finish is complete.
418 * On error, we never do the brw_finish, so we handle all decrefs.
420 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_BRW_READ_BULK)) {
421 CERROR("obd_fail_loc=%x, skipping register_bulk\n",
422 OBD_FAIL_OSC_BRW_READ_BULK);
424 rc = ptlrpc_register_bulk(desc);
429 request->rq_replen = lustre_msg_size(1, size);
430 rc = ptlrpc_queue_wait(request);
431 rc = ptlrpc_check_status(request, rc);
434 * XXX: If there is an error during the processing of the callback,
435 * such as a timeout in a sleep that it performs, brw_finish
436 * will never get called, and we'll leak the desc, fail to kunmap
437 * things, cats will live with dogs. One solution would be to
438 * export brw_finish as osc_brw_finish, so that the timeout case
439 * and its kin could call it for proper cleanup. An alternative
440 * would be for an error return from the callback to cause us to
441 * clean up, but that doesn't help the truly async cases (like
442 * LOV), which will immediately return from their PHASE_START
443 * callback, before any such cleanup-requiring error condition can
449 /* Callbacks cause asynchronous handling. */
450 rc = callback(data, 0, CB_PHASE_START);
453 ptlrpc_req_finished(request);
456 /* Clean up on error. */
459 kunmap(pga[mapped].pg);
460 OBD_FREE(cb_data, sizeof(*cb_data));
462 ptlrpc_bulk_decref(desc);
466 static int osc_brw_write(struct lustre_handle *conn, struct lov_stripe_md *md,
467 obd_count page_count, struct brw_page *pga,
468 brw_callback_t callback, struct io_cb_data *data)
470 struct ptlrpc_connection *connection =
471 client_conn2cli(conn)->cl_import.imp_connection;
472 struct ptlrpc_request *request = NULL;
473 struct ptlrpc_bulk_desc *desc = NULL;
474 struct ost_body *body;
475 struct niobuf_local *local = NULL;
476 struct niobuf_remote *remote;
477 struct osc_brw_cb_data *cb_data = NULL;
478 int rc, j, size[3] = {sizeof(*body)};
479 void *iooptr, *nioptr;
483 size[1] = sizeof(struct obd_ioobj);
484 size[2] = page_count * sizeof(*remote);
486 request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_WRITE, 3, size,
491 body = lustre_msg_buf(request->rq_reqmsg, 0);
493 desc = ptlrpc_prep_bulk(connection);
495 GOTO(out_req, rc = -ENOMEM);
496 desc->bd_portal = OSC_BULK_PORTAL;
497 desc->bd_cb = brw_finish;
498 OBD_ALLOC(cb_data, sizeof(*cb_data));
500 GOTO(out_desc, rc = -ENOMEM);
502 cb_data->callback = callback;
503 cb_data->cb_data = data;
505 desc->bd_cb_data = cb_data;
507 iooptr = lustre_msg_buf(request->rq_reqmsg, 1);
508 nioptr = lustre_msg_buf(request->rq_reqmsg, 2);
509 ost_pack_ioo(&iooptr, md, page_count);
510 /* end almost identical to brw_read case */
512 OBD_ALLOC(local, page_count * sizeof(*local));
514 GOTO(out_cb, rc = -ENOMEM);
516 cb_data->obd_data = local;
517 cb_data->obd_size = page_count * sizeof(*local);
519 for (mapped = 0; mapped < page_count; mapped++) {
520 local[mapped].addr = kmap(pga[mapped].pg);
521 local[mapped].offset = pga[mapped].off;
522 local[mapped].len = pga[mapped].count;
523 ost_pack_niobuf(&nioptr, pga[mapped].off, pga[mapped].count,
524 pga[mapped].flag, 0);
527 size[1] = page_count * sizeof(*remote);
528 request->rq_replen = lustre_msg_size(2, size);
529 rc = ptlrpc_queue_wait(request);
530 rc = ptlrpc_check_status(request, rc);
534 nioptr = lustre_msg_buf(request->rq_repmsg, 1);
536 GOTO(out_unmap, rc = -EINVAL);
538 if (request->rq_repmsg->buflens[1] != size[1]) {
539 CERROR("buffer length wrong (%d vs. %d)\n",
540 request->rq_repmsg->buflens[1], size[1]);
541 GOTO(out_unmap, rc = -EINVAL);
544 for (j = 0; j < page_count; j++) {
545 struct ptlrpc_bulk_page *bulk;
547 ost_unpack_niobuf(&nioptr, &remote);
549 bulk = ptlrpc_prep_bulk_page(desc);
551 GOTO(out_unmap, rc = -ENOMEM);
553 bulk->bp_buf = (void *)(unsigned long)local[j].addr;
554 bulk->bp_buflen = local[j].len;
555 bulk->bp_xid = remote->xid;
556 bulk->bp_page = pga[j].pg;
559 if (desc->bd_page_count != page_count)
562 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_BRW_WRITE_BULK))
563 GOTO(out_unmap, rc = 0);
565 /* Our reference is released when brw_finish is complete. */
566 rc = ptlrpc_send_bulk(desc);
568 /* XXX: Mike, same question as in osc_brw_read. */
572 /* Callbacks cause asynchronous handling. */
573 rc = callback(data, 0, CB_PHASE_START);
576 ptlrpc_req_finished(request);
579 /* Clean up on error. */
582 kunmap(pga[mapped].pg);
584 OBD_FREE(local, page_count * sizeof(*local));
586 OBD_FREE(cb_data, sizeof(*cb_data));
588 ptlrpc_bulk_decref(desc);
592 static int osc_brw(int cmd, struct lustre_handle *conn,
593 struct lov_stripe_md *md, obd_count page_count,
594 struct brw_page *pga, brw_callback_t callback,
595 struct io_cb_data *data)
597 if (cmd & OBD_BRW_WRITE)
598 return osc_brw_write(conn, md, page_count, pga, callback, data);
600 return osc_brw_read(conn, md, page_count, pga, callback, data);
603 static int osc_enqueue(struct lustre_handle *connh, struct lov_stripe_md *md,
604 struct lustre_handle *parent_lock,
605 __u32 type, void *extentp, int extent_len, __u32 mode,
606 int *flags, void *callback, void *data, int datalen,
607 struct lustre_handle *lockh)
609 __u64 res_id[RES_NAME_SIZE] = { md->lmd_object_id };
610 struct obd_device *obddev = class_conn2obd(connh);
611 struct ldlm_extent *extent = extentp;
615 /* Filesystem locks are given a bit of special treatment: first we
616 * fixup the lock to start and end on page boundaries. */
617 extent->start &= PAGE_MASK;
618 extent->end = (extent->end + PAGE_SIZE - 1) & PAGE_MASK;
620 /* Next, search for already existing extent locks that will cover us */
621 //osc_con2dlmcl(conn, &cl, &connection, &rconn);
622 rc = ldlm_lock_match(obddev->obd_namespace, res_id, type, extent,
623 sizeof(extent), mode, lockh);
625 /* We already have a lock, and it's referenced */
629 /* Next, search for locks that we can upgrade (if we're trying to write)
630 * or are more than we need (if we're trying to read). Because the VFS
631 * and page cache already protect us locally, lots of readers/writers
632 * can share a single PW lock. */
638 rc = ldlm_lock_match(obddev->obd_namespace, res_id, type, extent,
639 sizeof(extent), mode2, lockh);
642 /* FIXME: This is not incredibly elegant, but it might
643 * be more elegant than adding another parameter to
644 * lock_match. I want a second opinion. */
645 ldlm_lock_addref(lockh, mode);
646 ldlm_lock_decref(lockh, mode2);
651 rc = ldlm_cli_convert(lockh, mode, &flags);
658 rc = ldlm_cli_enqueue(connh, NULL, obddev->obd_namespace,
659 parent_lock, res_id, type, extent,
660 sizeof(extent), mode, flags, ldlm_completion_ast,
661 callback, data, datalen, lockh);
665 static int osc_cancel(struct lustre_handle *oconn, struct lov_stripe_md *md,
666 __u32 mode, struct lustre_handle *lockh)
670 ldlm_lock_decref(lockh, mode);
675 static int osc_statfs(struct lustre_handle *conn, struct statfs *sfs)
677 struct ptlrpc_request *request;
678 struct obd_statfs *osfs;
679 int rc, size = sizeof(*osfs);
682 request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_STATFS, 0, NULL,
687 request->rq_replen = lustre_msg_size(1, &size);
689 rc = ptlrpc_queue_wait(request);
690 rc = ptlrpc_check_status(request, rc);
692 CERROR("%s failed: rc = %d\n", __FUNCTION__, rc);
696 osfs = lustre_msg_buf(request->rq_repmsg, 0);
697 obd_statfs_unpack(osfs, sfs);
701 ptlrpc_free_req(request);
705 static int osc_iocontrol(long cmd, struct lustre_handle *conn, int len,
706 void *karg, void *uarg)
708 struct obd_device *obddev = class_conn2obd(conn);
709 struct obd_ioctl_data *data = karg;
713 if (_IOC_TYPE(cmd) != IOC_LDLM_TYPE ||
714 _IOC_NR(cmd) < IOC_LDLM_MIN_NR || _IOC_NR(cmd) > IOC_LDLM_MAX_NR) {
715 CDEBUG(D_IOCTL, "invalid ioctl (type %ld, nr %ld, size %ld)\n",
716 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
721 case IOC_LDLM_TEST: {
722 err = ldlm_test(obddev, conn);
723 CERROR("-- done err %d\n", err);
726 case IOC_LDLM_REGRESS_START: {
727 unsigned int numthreads = 1;
728 unsigned int numheld = 10;
729 unsigned int numres = 10;
730 unsigned int numext = 10;
733 if (data->ioc_inllen1) {
734 parse = data->ioc_inlbuf1;
735 if (*parse != '\0') {
736 while(isspace(*parse)) parse++;
737 numthreads = simple_strtoul(parse, &parse, 0);
738 while(isspace(*parse)) parse++;
740 if (*parse != '\0') {
741 while(isspace(*parse)) parse++;
742 numheld = simple_strtoul(parse, &parse, 0);
743 while(isspace(*parse)) parse++;
745 if (*parse != '\0') {
746 while(isspace(*parse)) parse++;
747 numres = simple_strtoul(parse, &parse, 0);
748 while(isspace(*parse)) parse++;
750 if (*parse != '\0') {
751 while(isspace(*parse)) parse++;
752 numext = simple_strtoul(parse, &parse, 0);
753 while(isspace(*parse)) parse++;
757 err = ldlm_regression_start(obddev, conn, numthreads,
758 numheld, numres, numext);
760 CERROR("-- done err %d\n", err);
763 case IOC_LDLM_REGRESS_STOP: {
764 err = ldlm_regression_stop();
765 CERROR("-- done err %d\n", err);
769 GOTO(out, err = -EINVAL);
775 struct obd_ops osc_obd_ops = {
776 o_setup: client_obd_setup,
777 o_cleanup: client_obd_cleanup,
778 o_statfs: osc_statfs,
779 o_create: osc_create,
780 o_destroy: osc_destroy,
781 o_getattr: osc_getattr,
782 o_setattr: osc_setattr,
785 o_connect: client_obd_connect,
786 o_disconnect: client_obd_disconnect,
789 o_enqueue: osc_enqueue,
790 o_cancel: osc_cancel,
791 o_iocontrol: osc_iocontrol
794 static int __init osc_init(void)
796 return class_register_type(&osc_obd_ops, LUSTRE_OSC_NAME);
799 static void __exit osc_exit(void)
801 class_unregister_type(LUSTRE_OSC_NAME);
804 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
805 MODULE_DESCRIPTION("Lustre Object Storage Client (OSC) v1.0");
806 MODULE_LICENSE("GPL");
808 module_init(osc_init);
809 module_exit(osc_exit);