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/init.h>
31 static int osc_getattr(struct lustre_handle *conn, struct obdo *oa,
32 struct lov_stripe_md *md)
34 struct ptlrpc_request *request;
35 struct ost_body *body;
36 int rc, size = sizeof(*body);
39 request = ptlrpc_prep_req2(conn, OST_GETATTR, 1, &size, NULL);
43 body = lustre_msg_buf(request->rq_reqmsg, 0);
44 #warning FIXME: pack only valid fields instead of memcpy, endianness
45 memcpy(&body->oa, oa, sizeof(*oa));
47 request->rq_replen = lustre_msg_size(1, &size);
49 rc = ptlrpc_queue_wait(request);
50 rc = ptlrpc_check_status(request, rc);
52 CERROR("%s failed: rc = %d\n", __FUNCTION__, rc);
56 body = lustre_msg_buf(request->rq_repmsg, 0);
57 CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
59 memcpy(oa, &body->oa, sizeof(*oa));
63 ptlrpc_free_req(request);
67 static int osc_open(struct lustre_handle *conn, struct obdo *oa,
68 struct lov_stripe_md *md)
70 struct ptlrpc_request *request;
71 struct ost_body *body;
72 int rc, size = sizeof(*body);
75 request = ptlrpc_prep_req2(conn, OST_OPEN, 1, &size, NULL);
79 body = lustre_msg_buf(request->rq_reqmsg, 0);
80 #warning FIXME: pack only valid fields instead of memcpy, endianness
81 memcpy(&body->oa, oa, sizeof(*oa));
83 request->rq_replen = lustre_msg_size(1, &size);
85 rc = ptlrpc_queue_wait(request);
86 rc = ptlrpc_check_status(request, rc);
90 body = lustre_msg_buf(request->rq_repmsg, 0);
91 CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
93 memcpy(oa, &body->oa, sizeof(*oa));
97 ptlrpc_free_req(request);
101 static int osc_close(struct lustre_handle *conn, struct obdo *oa,
102 struct lov_stripe_md *md)
104 struct ptlrpc_request *request;
105 struct ost_body *body;
106 int rc, size = sizeof(*body);
109 request = ptlrpc_prep_req2(conn, OST_CLOSE, 1, &size, NULL);
113 body = lustre_msg_buf(request->rq_reqmsg, 0);
114 #warning FIXME: pack only valid fields instead of memcpy, endianness
115 memcpy(&body->oa, oa, sizeof(*oa));
117 request->rq_replen = lustre_msg_size(1, &size);
119 rc = ptlrpc_queue_wait(request);
120 rc = ptlrpc_check_status(request, rc);
124 body = lustre_msg_buf(request->rq_repmsg, 0);
125 CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
127 memcpy(oa, &body->oa, sizeof(*oa));
131 ptlrpc_free_req(request);
135 static int osc_setattr(struct lustre_handle *conn, struct obdo *oa,
136 struct lov_stripe_md *md)
138 struct ptlrpc_request *request;
139 struct ost_body *body;
140 int rc, size = sizeof(*body);
143 request = ptlrpc_prep_req2(conn, OST_SETATTR, 1, &size, NULL);
147 body = lustre_msg_buf(request->rq_reqmsg, 0);
148 memcpy(&body->oa, oa, sizeof(*oa));
150 request->rq_replen = lustre_msg_size(1, &size);
152 rc = ptlrpc_queue_wait(request);
153 rc = ptlrpc_check_status(request, rc);
157 ptlrpc_free_req(request);
161 static int osc_create(struct lustre_handle *conn, struct obdo *oa,
162 struct lov_stripe_md **ea)
164 struct ptlrpc_request *request;
165 struct ost_body *body;
166 int rc, size = sizeof(*body);
179 OBD_ALLOC(*ea, oa->o_easize);
182 (*ea)->lmd_easize = oa->o_easize;
185 request = ptlrpc_prep_req2(conn, OST_CREATE, 1, &size, NULL);
189 body = lustre_msg_buf(request->rq_reqmsg, 0);
190 memcpy(&body->oa, oa, sizeof(*oa));
192 request->rq_replen = lustre_msg_size(1, &size);
194 rc = ptlrpc_queue_wait(request);
195 rc = ptlrpc_check_status(request, rc);
199 body = lustre_msg_buf(request->rq_repmsg, 0);
200 memcpy(oa, &body->oa, sizeof(*oa));
202 (*ea)->lmd_object_id = oa->o_id;
203 (*ea)->lmd_stripe_count = 1;
206 ptlrpc_free_req(request);
210 static int osc_punch(struct lustre_handle *conn, struct obdo *oa,
211 struct lov_stripe_md *md, obd_size start,
214 struct ptlrpc_request *request;
215 struct ost_body *body;
216 int rc, size = sizeof(*body);
224 request = ptlrpc_prep_req2(conn, OST_PUNCH, 1, &size, NULL);
228 body = lustre_msg_buf(request->rq_reqmsg, 0);
229 #warning FIXME: pack only valid fields instead of memcpy, endianness, valid
230 memcpy(&body->oa, oa, sizeof(*oa));
232 /* overload the blocks and size fields in the oa with start/end */
233 #warning FIXME: endianness, size=start, blocks=end?
234 body->oa.o_blocks = start;
235 body->oa.o_size = end;
236 body->oa.o_valid |= OBD_MD_FLBLOCKS | OBD_MD_FLSIZE;
238 request->rq_replen = lustre_msg_size(1, &size);
240 rc = ptlrpc_queue_wait(request);
241 rc = ptlrpc_check_status(request, rc);
245 body = lustre_msg_buf(request->rq_repmsg, 0);
246 memcpy(oa, &body->oa, sizeof(*oa));
250 ptlrpc_free_req(request);
254 static int osc_destroy(struct lustre_handle *conn, struct obdo *oa,
255 struct lov_stripe_md *ea)
257 struct ptlrpc_request *request;
258 struct ost_body *body;
259 int rc, size = sizeof(*body);
266 request = ptlrpc_prep_req2(conn, OST_DESTROY, 1, &size, NULL);
270 body = lustre_msg_buf(request->rq_reqmsg, 0);
271 #warning FIXME: pack only valid fields instead of memcpy, endianness
272 memcpy(&body->oa, oa, sizeof(*oa));
274 request->rq_replen = lustre_msg_size(1, &size);
276 rc = ptlrpc_queue_wait(request);
277 rc = ptlrpc_check_status(request, rc);
281 body = lustre_msg_buf(request->rq_repmsg, 0);
282 memcpy(oa, &body->oa, sizeof(*oa));
286 ptlrpc_free_req(request);
290 struct osc_brw_cb_data {
291 brw_callback_t callback;
297 /* Our bulk-unmapping bottom half. */
298 static void unmap_and_decref_bulk_desc(void *data)
300 struct ptlrpc_bulk_desc *desc = data;
301 struct list_head *tmp;
304 /* This feels wrong to me. */
305 list_for_each(tmp, &desc->b_page_list) {
306 struct ptlrpc_bulk_page *bulk;
307 bulk = list_entry(tmp, struct ptlrpc_bulk_page, b_link);
309 kunmap(bulk->b_page);
312 ptlrpc_bulk_decref(desc);
316 static void brw_finish(struct ptlrpc_bulk_desc *desc, void *data)
318 struct osc_brw_cb_data *cb_data = data;
322 if (desc->b_flags & PTL_RPC_FL_INTR) {
324 CERROR("got signal\n");
327 if (cb_data->callback)
328 cb_data->callback(cb_data->cb_data, err, CB_PHASE_FINISH);
330 OBD_FREE(cb_data->obd_data, cb_data->obd_size);
331 OBD_FREE(cb_data, sizeof(*cb_data));
333 /* We can't kunmap the desc from interrupt context, so we do it from
334 * the bottom half above. */
335 INIT_TQUEUE(&desc->b_queue, 0, 0);
336 PREPARE_TQUEUE(&desc->b_queue, unmap_and_decref_bulk_desc, desc);
337 schedule_task(&desc->b_queue);
342 static int osc_brw_read(struct lustre_handle *conn, struct lov_stripe_md *md, obd_count page_count, struct brw_page *pga, brw_callback_t callback, void *data)
344 struct ptlrpc_connection *connection = client_conn2cli(conn)->cl_conn;
345 struct ptlrpc_request *request = NULL;
346 struct ptlrpc_bulk_desc *desc = NULL;
347 struct ost_body *body;
348 struct osc_brw_cb_data *cb_data = NULL;
349 int rc, size[3] = {sizeof(*body)};
350 void *iooptr, *nioptr;
355 size[1] = sizeof(struct obd_ioobj);
356 size[2] = page_count * sizeof(struct niobuf_remote);
358 request = ptlrpc_prep_req2(conn, OST_READ, 3, size, NULL);
362 body = lustre_msg_buf(request->rq_reqmsg, 0);
364 desc = ptlrpc_prep_bulk(connection);
366 GOTO(out_req, rc = -ENOMEM);
367 desc->b_portal = OST_BULK_PORTAL;
368 desc->b_cb = brw_finish;
369 OBD_ALLOC(cb_data, sizeof(*cb_data));
371 GOTO(out_desc, rc = -ENOMEM);
373 cb_data->callback = callback;
374 cb_data->cb_data = data;
375 desc->b_cb_data = cb_data;
377 iooptr = lustre_msg_buf(request->rq_reqmsg, 1);
378 nioptr = lustre_msg_buf(request->rq_reqmsg, 2);
379 ost_pack_ioo(&iooptr, md, page_count);
380 /* end almost identical to brw_write case */
382 spin_lock(&connection->c_lock);
383 xid = ++connection->c_xid_out; /* single xid for all pages */
384 spin_unlock(&connection->c_lock);
386 for (mapped = 0; mapped < page_count; mapped++) {
387 struct ptlrpc_bulk_page *bulk = ptlrpc_prep_bulk_page(desc);
389 GOTO(out_unmap, rc = -ENOMEM);
391 bulk->b_xid = xid; /* single xid for all pages */
393 bulk->b_buf = kmap(pga[mapped].pg);
394 bulk->b_page = pga[mapped].pg;
395 bulk->b_buflen = PAGE_SIZE;
396 ost_pack_niobuf(&nioptr, pga[mapped].off, pga[mapped].count,
397 pga[mapped].flag, bulk->b_xid);
401 * Register the bulk first, because the reply could arrive out of order,
402 * and we want to be ready for the bulk data.
404 * One reference is released when brw_finish is complete, the
405 * other here when we finish waiting on it if we don't have a callback.
407 * We don't reference the bulk descriptor again here if there is a
408 * callback, so we don't need an additional refcount on it.
410 * On error, we never do the brw_finish, so we handle all decrefs.
412 rc = ptlrpc_register_bulk(desc);
416 request->rq_replen = lustre_msg_size(1, size);
417 rc = ptlrpc_queue_wait(request);
418 rc = ptlrpc_check_status(request, rc);
420 /* XXX: Mike, this is the only place I'm not sure of. If we had
421 * an error here, will we always call brw_finish? If yes, then
422 * out_desc_2 will do too much and we should jump to out_desc.
423 * If maybe, then we are screwed, and we need to set things up
424 * so that bulk_sink_callback is called for each bulk page,
425 * even on error so brw_finish is always called. It would need
426 * to be passed an error code as a parameter to know what to do.
428 * That would also help with the partial completion case, so
429 * we could say in brw_finish "these pages are done, don't
430 * restart them" and osc_brw callers can know this.
435 /* Callbacks cause asynchronous handling. */
436 rc = callback(data, 0, CB_PHASE_START);
440 ptlrpc_bulk_decref(desc);
442 ptlrpc_req_finished(request);
445 /* Clean up on error. */
448 kunmap(page_array[mapped]);
449 OBD_FREE(cb_data, sizeof(*cb_data));
453 static int osc_brw_write(struct lustre_handle *conn,
454 struct lov_stripe_md *md, obd_count page_count,
455 struct brw_page *pga,
456 brw_callback_t callback, void *data)
458 struct ptlrpc_connection *connection = client_conn2cli(conn)->cl_conn;
459 struct ptlrpc_request *request = NULL;
460 struct ptlrpc_bulk_desc *desc = NULL;
461 struct ost_body *body;
462 struct niobuf_local *local = NULL;
463 struct niobuf_remote *remote;
464 struct osc_brw_cb_data *cb_data = NULL;
465 int rc, j, size[3] = {sizeof(*body)};
466 void *iooptr, *nioptr;
470 size[1] = sizeof(struct obd_ioobj);
471 size[2] = page_count * sizeof(*remote);
473 request = ptlrpc_prep_req2(conn, OST_WRITE, 3, size, NULL);
477 body = lustre_msg_buf(request->rq_reqmsg, 0);
479 desc = ptlrpc_prep_bulk(connection);
481 GOTO(out_req, rc = -ENOMEM);
482 desc->b_portal = OSC_BULK_PORTAL;
483 desc->b_cb = brw_finish;
484 OBD_ALLOC(cb_data, sizeof(*cb_data));
486 GOTO(out_desc, rc = -ENOMEM);
488 cb_data->callback = callback;
489 cb_data->cb_data = data;
490 desc->b_cb_data = cb_data;
492 iooptr = lustre_msg_buf(request->rq_reqmsg, 1);
493 nioptr = lustre_msg_buf(request->rq_reqmsg, 2);
494 ost_pack_ioo(&iooptr, md, page_count);
495 /* end almost identical to brw_read case */
497 OBD_ALLOC(local, page_count * sizeof(*local));
499 GOTO(out_cb, rc = -ENOMEM);
501 cb_data->obd_data = local;
502 cb_data->obd_size = page_count * sizeof(*local);
504 for (mapped = 0; mapped < page_count; mapped++) {
505 local[mapped].addr = kmap(pga[mapped].pg);
506 local[mapped].offset = pga[mapped].off;
507 local[mapped].len = pga[mapped].count;
508 ost_pack_niobuf(&nioptr, pga[mapped].off, pga[mapped].count,
509 pga[mapped].flag, 0);
512 size[1] = page_count * sizeof(*remote);
513 request->rq_replen = lustre_msg_size(2, size);
514 rc = ptlrpc_queue_wait(request);
515 rc = ptlrpc_check_status(request, rc);
519 nioptr = lustre_msg_buf(request->rq_repmsg, 1);
521 GOTO(out_unmap, rc = -EINVAL);
523 if (request->rq_repmsg->buflens[1] != size[1]) {
524 CERROR("buffer length wrong (%d vs. %d)\n",
525 request->rq_repmsg->buflens[1], size[1]);
526 GOTO(out_unmap, rc = -EINVAL);
529 for (j = 0; j < page_count; j++) {
530 struct ptlrpc_bulk_page *bulk;
532 ost_unpack_niobuf(&nioptr, &remote);
534 bulk = ptlrpc_prep_bulk_page(desc);
536 GOTO(out_unmap, rc = -ENOMEM);
538 bulk->b_buf = (void *)(unsigned long)local[j].addr;
539 bulk->b_buflen = local[j].len;
540 bulk->b_xid = remote->xid;
541 bulk->b_page = pga[j].pg;
544 if (desc->b_page_count != page_count)
548 * One reference is released when brw_finish is complete, the
549 * other here when we finish waiting on it if we don't have a callback.
551 rc = ptlrpc_send_bulk(desc);
553 /* XXX: Mike, same question as in osc_brw_read. */
557 /* Callbacks cause asynchronous handling. */
558 rc = callback(data, 0, CB_PHASE_START);
562 ptlrpc_bulk_decref(desc);
564 ptlrpc_req_finished(request);
567 /* Clean up on error. */
570 ptlrpc_bulk_decref(desc);
573 kunmap(pagearray[mapped]);
575 OBD_FREE(local, page_count * sizeof(*local));
577 OBD_FREE(cb_data, sizeof(*cb_data));
581 static int osc_brw(int cmd, struct lustre_handle *conn,
582 struct lov_stripe_md *md, obd_count page_count,
583 struct brw_page *pagear, brw_callback_t callback,
586 if (cmd & OBD_BRW_WRITE)
587 return osc_brw_write(conn, md, page_count, pagear, callback, data);
589 return osc_brw_read(conn, md, page_count, pagear, callback, data);
592 static int osc_enqueue(struct lustre_handle *connh, struct lov_stripe_md *md,
593 struct lustre_handle *parent_lock,
594 __u32 type, void *extentp, int extent_len, __u32 mode,
595 int *flags, void *callback, void *data, int datalen,
596 struct lustre_handle *lockh)
598 __u64 res_id = { md->lmd_object_id };
599 struct obd_device *obddev = class_conn2obd(connh);
600 struct ldlm_extent *extent = extentp;
604 /* Filesystem locks are given a bit of special treatment: first we
605 * fixup the lock to start and end on page boundaries. */
606 extent->start &= PAGE_MASK;
607 extent->end = (extent->end + PAGE_SIZE - 1) & PAGE_MASK;
609 /* Next, search for already existing extent locks that will cover us */
610 //osc_con2dlmcl(conn, &cl, &connection, &rconn);
611 rc = ldlm_lock_match(obddev->obd_namespace, &res_id, type, extent,
612 sizeof(extent), mode, lockh);
614 /* We already have a lock, and it's referenced */
618 /* Next, search for locks that we can upgrade (if we're trying to write)
619 * or are more than we need (if we're trying to read). Because the VFS
620 * and page cache already protect us locally, lots of readers/writers
621 * can share a single PW lock. */
627 rc = ldlm_lock_match(obddev->obd_namespace, &res_id, type, extent,
628 sizeof(extent), mode2, lockh);
631 /* FIXME: This is not incredibly elegant, but it might
632 * be more elegant than adding another parameter to
633 * lock_match. I want a second opinion. */
634 ldlm_lock_addref(lockh, mode);
635 ldlm_lock_decref(lockh, mode2);
640 rc = ldlm_cli_convert(lockh, mode, &flags);
647 rc = ldlm_cli_enqueue(connh, NULL,obddev->obd_namespace,
648 parent_lock, &res_id, type, extent, sizeof(extent),
649 mode, flags, ldlm_completion_ast, callback, data, datalen, lockh);
653 static int osc_cancel(struct lustre_handle *oconn, struct lov_stripe_md *md, __u32 mode,
654 struct lustre_handle *lockh)
658 ldlm_lock_decref(lockh, mode);
663 static int osc_statfs(struct lustre_handle *conn, struct statfs *sfs)
665 struct ptlrpc_request *request;
666 struct obd_statfs *osfs;
667 int rc, size = sizeof(*osfs);
670 request = ptlrpc_prep_req2(conn, OST_STATFS, 0, NULL, NULL);
674 request->rq_replen = lustre_msg_size(1, &size);
676 rc = ptlrpc_queue_wait(request);
677 rc = ptlrpc_check_status(request, rc);
679 CERROR("%s failed: rc = %d\n", __FUNCTION__, rc);
683 osfs = lustre_msg_buf(request->rq_repmsg, 0);
684 obd_statfs_unpack(osfs, sfs);
688 ptlrpc_free_req(request);
692 struct obd_ops osc_obd_ops = {
693 o_setup: client_obd_setup,
694 o_cleanup: client_obd_cleanup,
695 o_statfs: osc_statfs,
696 o_create: osc_create,
697 o_destroy: osc_destroy,
698 o_getattr: osc_getattr,
699 o_setattr: osc_setattr,
702 o_connect: client_obd_connect,
703 o_disconnect: client_obd_disconnect,
706 o_enqueue: osc_enqueue,
710 static int __init osc_init(void)
712 return class_register_type(&osc_obd_ops, LUSTRE_OSC_NAME);
715 static void __exit osc_exit(void)
717 class_unregister_type(LUSTRE_OSC_NAME);
720 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
721 MODULE_DESCRIPTION("Lustre Object Storage Client (OSC) v1.0");
722 MODULE_LICENSE("GPL");
724 module_init(osc_init);
725 module_exit(osc_exit);