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>
28 #include <linux/lustre_ha.h>
30 static int osc_getattr(struct lustre_handle *conn, struct obdo *oa,
31 struct lov_stripe_md *md)
33 struct ptlrpc_request *request;
34 struct ost_body *body;
35 int rc, size = sizeof(*body);
38 request = ptlrpc_prep_req2(conn, OST_GETATTR, 1, &size, NULL);
42 body = lustre_msg_buf(request->rq_reqmsg, 0);
43 #warning FIXME: pack only valid fields instead of memcpy, endianness
44 memcpy(&body->oa, oa, sizeof(*oa));
46 request->rq_replen = lustre_msg_size(1, &size);
48 rc = ptlrpc_queue_wait(request);
49 rc = ptlrpc_check_status(request, rc);
51 CERROR("%s failed: rc = %d\n", __FUNCTION__, rc);
55 body = lustre_msg_buf(request->rq_repmsg, 0);
56 CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
58 memcpy(oa, &body->oa, sizeof(*oa));
62 ptlrpc_free_req(request);
66 static int osc_open(struct lustre_handle *conn, struct obdo *oa,
67 struct lov_stripe_md *md)
69 struct ptlrpc_request *request;
70 struct ost_body *body;
71 int rc, size = sizeof(*body);
74 request = ptlrpc_prep_req2(conn, OST_OPEN, 1, &size, NULL);
78 body = lustre_msg_buf(request->rq_reqmsg, 0);
79 #warning FIXME: pack only valid fields instead of memcpy, endianness
80 memcpy(&body->oa, oa, sizeof(*oa));
82 request->rq_replen = lustre_msg_size(1, &size);
84 rc = ptlrpc_queue_wait(request);
85 rc = ptlrpc_check_status(request, rc);
89 body = lustre_msg_buf(request->rq_repmsg, 0);
90 CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
92 memcpy(oa, &body->oa, sizeof(*oa));
96 ptlrpc_free_req(request);
100 static int osc_close(struct lustre_handle *conn, struct obdo *oa,
101 struct lov_stripe_md *md)
103 struct ptlrpc_request *request;
104 struct ost_body *body;
105 int rc, size = sizeof(*body);
108 request = ptlrpc_prep_req2(conn, OST_CLOSE, 1, &size, NULL);
112 body = lustre_msg_buf(request->rq_reqmsg, 0);
113 #warning FIXME: pack only valid fields instead of memcpy, endianness
114 memcpy(&body->oa, oa, sizeof(*oa));
116 request->rq_replen = lustre_msg_size(1, &size);
118 rc = ptlrpc_queue_wait(request);
119 rc = ptlrpc_check_status(request, rc);
123 body = lustre_msg_buf(request->rq_repmsg, 0);
124 CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
126 memcpy(oa, &body->oa, sizeof(*oa));
130 ptlrpc_free_req(request);
134 static int osc_setattr(struct lustre_handle *conn, struct obdo *oa,
135 struct lov_stripe_md *md)
137 struct ptlrpc_request *request;
138 struct ost_body *body;
139 int rc, size = sizeof(*body);
142 request = ptlrpc_prep_req2(conn, OST_SETATTR, 1, &size, NULL);
146 body = lustre_msg_buf(request->rq_reqmsg, 0);
147 memcpy(&body->oa, oa, sizeof(*oa));
149 request->rq_replen = lustre_msg_size(1, &size);
151 rc = ptlrpc_queue_wait(request);
152 rc = ptlrpc_check_status(request, rc);
156 ptlrpc_free_req(request);
160 static int osc_create(struct lustre_handle *conn, struct obdo *oa,
161 struct lov_stripe_md **ea)
163 struct ptlrpc_request *request;
164 struct ost_body *body;
165 int rc, size = sizeof(*body);
178 OBD_ALLOC(*ea, oa->o_easize);
181 (*ea)->lmd_easize = oa->o_easize;
184 request = ptlrpc_prep_req2(conn, OST_CREATE, 1, &size, NULL);
188 body = lustre_msg_buf(request->rq_reqmsg, 0);
189 memcpy(&body->oa, oa, sizeof(*oa));
191 request->rq_replen = lustre_msg_size(1, &size);
193 rc = ptlrpc_queue_wait(request);
194 rc = ptlrpc_check_status(request, rc);
198 body = lustre_msg_buf(request->rq_repmsg, 0);
199 memcpy(oa, &body->oa, sizeof(*oa));
201 (*ea)->lmd_object_id = oa->o_id;
202 (*ea)->lmd_stripe_count = 1;
205 ptlrpc_free_req(request);
209 static int osc_punch(struct lustre_handle *conn, struct obdo *oa,
210 struct lov_stripe_md *md, obd_size start,
213 struct ptlrpc_request *request;
214 struct ost_body *body;
215 int rc, size = sizeof(*body);
223 request = ptlrpc_prep_req2(conn, OST_PUNCH, 1, &size, NULL);
227 body = lustre_msg_buf(request->rq_reqmsg, 0);
228 #warning FIXME: pack only valid fields instead of memcpy, endianness, valid
229 memcpy(&body->oa, oa, sizeof(*oa));
231 /* overload the blocks and size fields in the oa with start/end */
232 #warning FIXME: endianness, size=start, blocks=end?
233 body->oa.o_blocks = start;
234 body->oa.o_size = end;
235 body->oa.o_valid |= OBD_MD_FLBLOCKS | OBD_MD_FLSIZE;
237 request->rq_replen = lustre_msg_size(1, &size);
239 rc = ptlrpc_queue_wait(request);
240 rc = ptlrpc_check_status(request, rc);
244 body = lustre_msg_buf(request->rq_repmsg, 0);
245 memcpy(oa, &body->oa, sizeof(*oa));
249 ptlrpc_free_req(request);
253 static int osc_destroy(struct lustre_handle *conn, struct obdo *oa,
254 struct lov_stripe_md *ea)
256 struct ptlrpc_request *request;
257 struct ost_body *body;
258 int rc, size = sizeof(*body);
265 request = ptlrpc_prep_req2(conn, OST_DESTROY, 1, &size, NULL);
269 body = lustre_msg_buf(request->rq_reqmsg, 0);
270 #warning FIXME: pack only valid fields instead of memcpy, endianness
271 memcpy(&body->oa, oa, sizeof(*oa));
273 request->rq_replen = lustre_msg_size(1, &size);
275 rc = ptlrpc_queue_wait(request);
276 rc = ptlrpc_check_status(request, rc);
280 body = lustre_msg_buf(request->rq_repmsg, 0);
281 memcpy(oa, &body->oa, sizeof(*oa));
285 ptlrpc_free_req(request);
289 struct osc_brw_cb_data {
290 brw_callback_t callback;
296 /* Our bulk-unmapping bottom half. */
297 static void unmap_and_decref_bulk_desc(void *data)
299 struct ptlrpc_bulk_desc *desc = data;
300 struct list_head *tmp;
303 /* This feels wrong to me. */
304 list_for_each(tmp, &desc->b_page_list) {
305 struct ptlrpc_bulk_page *bulk;
306 bulk = list_entry(tmp, struct ptlrpc_bulk_page, b_link);
308 kunmap(bulk->b_page);
311 ptlrpc_bulk_decref(desc);
315 static void brw_finish(struct ptlrpc_bulk_desc *desc, void *data)
317 struct osc_brw_cb_data *cb_data = data;
321 if (desc->b_flags & PTL_RPC_FL_TIMEOUT) {
322 err = (desc->b_flags & PTL_RPC_FL_INTR ? -ERESTARTSYS :
326 if (cb_data->callback)
327 cb_data->callback(cb_data->cb_data, err, CB_PHASE_FINISH);
329 OBD_FREE(cb_data->obd_data, cb_data->obd_size);
330 OBD_FREE(cb_data, sizeof(*cb_data));
332 /* We can't kunmap the desc from interrupt context, so we do it from
333 * the bottom half above. */
334 INIT_TQUEUE(&desc->b_queue, 0, 0);
335 PREPARE_TQUEUE(&desc->b_queue, unmap_and_decref_bulk_desc, desc);
336 schedule_task(&desc->b_queue);
341 static int osc_brw_read(struct lustre_handle *conn, struct lov_stripe_md *md,
342 obd_count page_count, struct brw_page *pga,
343 brw_callback_t callback, struct io_cb_data *data)
345 struct ptlrpc_connection *connection = client_conn2cli(conn)->cl_conn;
346 struct ptlrpc_request *request = NULL;
347 struct ptlrpc_bulk_desc *desc = NULL;
348 struct ost_body *body;
349 struct osc_brw_cb_data *cb_data = NULL;
350 int rc, size[3] = {sizeof(*body)};
351 void *iooptr, *nioptr;
356 size[1] = sizeof(struct obd_ioobj);
357 size[2] = page_count * sizeof(struct niobuf_remote);
359 request = ptlrpc_prep_req2(conn, OST_READ, 3, size, NULL);
363 body = lustre_msg_buf(request->rq_reqmsg, 0);
365 desc = ptlrpc_prep_bulk(connection);
367 GOTO(out_req, rc = -ENOMEM);
368 desc->b_portal = OST_BULK_PORTAL;
369 desc->b_cb = brw_finish;
370 OBD_ALLOC(cb_data, sizeof(*cb_data));
372 GOTO(out_desc, rc = -ENOMEM);
374 cb_data->callback = callback;
375 cb_data->cb_data = data;
377 desc->b_cb_data = cb_data;
379 iooptr = lustre_msg_buf(request->rq_reqmsg, 1);
380 nioptr = lustre_msg_buf(request->rq_reqmsg, 2);
381 ost_pack_ioo(&iooptr, md, page_count);
382 /* end almost identical to brw_write case */
384 spin_lock(&connection->c_lock);
385 xid = ++connection->c_xid_out; /* single xid for all pages */
386 spin_unlock(&connection->c_lock);
388 for (mapped = 0; mapped < page_count; mapped++) {
389 struct ptlrpc_bulk_page *bulk = ptlrpc_prep_bulk_page(desc);
391 GOTO(out_unmap, rc = -ENOMEM);
393 bulk->b_xid = xid; /* single xid for all pages */
395 bulk->b_buf = kmap(pga[mapped].pg);
396 bulk->b_page = pga[mapped].pg;
397 bulk->b_buflen = PAGE_SIZE;
398 ost_pack_niobuf(&nioptr, pga[mapped].off, pga[mapped].count,
399 pga[mapped].flag, bulk->b_xid);
403 * Register the bulk first, because the reply could arrive out of order,
404 * and we want to be ready for the bulk data.
406 * One reference is released when brw_finish is complete, the
407 * other here when we finish waiting on it if we don't have a callback.
409 * We don't reference the bulk descriptor again here if there is a
410 * callback, so we don't need an additional refcount on it.
412 * On error, we never do the brw_finish, so we handle all decrefs.
414 rc = ptlrpc_register_bulk(desc);
418 request->rq_replen = lustre_msg_size(1, size);
419 rc = ptlrpc_queue_wait(request);
420 rc = ptlrpc_check_status(request, rc);
422 /* XXX: Mike, this is the only place I'm not sure of. If we had
423 * an error here, will we always call brw_finish? If yes, then
424 * out_desc_2 will do too much and we should jump to out_desc.
425 * If maybe, then we are screwed, and we need to set things up
426 * so that bulk_sink_callback is called for each bulk page,
427 * even on error so brw_finish is always called. It would need
428 * to be passed an error code as a parameter to know what to do.
430 * That would also help with the partial completion case, so
431 * we could say in brw_finish "these pages are done, don't
432 * restart them" and osc_brw callers can know this.
437 /* Callbacks cause asynchronous handling. */
438 rc = callback(data, 0, CB_PHASE_START);
442 ptlrpc_bulk_decref(desc);
444 ptlrpc_req_finished(request);
447 /* Clean up on error. */
450 kunmap(page_array[mapped]);
451 OBD_FREE(cb_data, sizeof(*cb_data));
455 static int osc_brw_write(struct lustre_handle *conn,
456 struct lov_stripe_md *md, obd_count page_count,
457 struct brw_page *pga,
458 brw_callback_t callback, struct io_cb_data *data)
460 struct ptlrpc_connection *connection = client_conn2cli(conn)->cl_conn;
461 struct ptlrpc_request *request = NULL;
462 struct ptlrpc_bulk_desc *desc = NULL;
463 struct ost_body *body;
464 struct niobuf_local *local = NULL;
465 struct niobuf_remote *remote;
466 struct osc_brw_cb_data *cb_data = NULL;
467 int rc, j, size[3] = {sizeof(*body)};
468 void *iooptr, *nioptr;
472 size[1] = sizeof(struct obd_ioobj);
473 size[2] = page_count * sizeof(*remote);
475 request = ptlrpc_prep_req2(conn, OST_WRITE, 3, size, NULL);
479 body = lustre_msg_buf(request->rq_reqmsg, 0);
481 desc = ptlrpc_prep_bulk(connection);
483 GOTO(out_req, rc = -ENOMEM);
484 desc->b_portal = OSC_BULK_PORTAL;
485 desc->b_cb = brw_finish;
486 OBD_ALLOC(cb_data, sizeof(*cb_data));
488 GOTO(out_desc, rc = -ENOMEM);
490 cb_data->callback = callback;
491 cb_data->cb_data = data;
493 desc->b_cb_data = cb_data;
495 iooptr = lustre_msg_buf(request->rq_reqmsg, 1);
496 nioptr = lustre_msg_buf(request->rq_reqmsg, 2);
497 ost_pack_ioo(&iooptr, md, page_count);
498 /* end almost identical to brw_read case */
500 OBD_ALLOC(local, page_count * sizeof(*local));
502 GOTO(out_cb, rc = -ENOMEM);
504 cb_data->obd_data = local;
505 cb_data->obd_size = page_count * sizeof(*local);
507 for (mapped = 0; mapped < page_count; mapped++) {
508 local[mapped].addr = kmap(pga[mapped].pg);
509 local[mapped].offset = pga[mapped].off;
510 local[mapped].len = pga[mapped].count;
511 ost_pack_niobuf(&nioptr, pga[mapped].off, pga[mapped].count,
512 pga[mapped].flag, 0);
515 size[1] = page_count * sizeof(*remote);
516 request->rq_replen = lustre_msg_size(2, size);
517 rc = ptlrpc_queue_wait(request);
518 rc = ptlrpc_check_status(request, rc);
522 nioptr = lustre_msg_buf(request->rq_repmsg, 1);
524 GOTO(out_unmap, rc = -EINVAL);
526 if (request->rq_repmsg->buflens[1] != size[1]) {
527 CERROR("buffer length wrong (%d vs. %d)\n",
528 request->rq_repmsg->buflens[1], size[1]);
529 GOTO(out_unmap, rc = -EINVAL);
532 for (j = 0; j < page_count; j++) {
533 struct ptlrpc_bulk_page *bulk;
535 ost_unpack_niobuf(&nioptr, &remote);
537 bulk = ptlrpc_prep_bulk_page(desc);
539 GOTO(out_unmap, rc = -ENOMEM);
541 bulk->b_buf = (void *)(unsigned long)local[j].addr;
542 bulk->b_buflen = local[j].len;
543 bulk->b_xid = remote->xid;
544 bulk->b_page = pga[j].pg;
547 if (desc->b_page_count != page_count)
551 * One reference is released when brw_finish is complete, the
552 * other here when we finish waiting on it if we don't have a callback.
554 rc = ptlrpc_send_bulk(desc);
556 /* XXX: Mike, same question as in osc_brw_read. */
560 /* Callbacks cause asynchronous handling. */
561 rc = callback(data, 0, CB_PHASE_START);
565 ptlrpc_bulk_decref(desc);
567 ptlrpc_req_finished(request);
570 /* Clean up on error. */
573 ptlrpc_bulk_decref(desc);
576 kunmap(pagearray[mapped]);
578 OBD_FREE(local, page_count * sizeof(*local));
580 OBD_FREE(cb_data, sizeof(*cb_data));
584 static int osc_brw(int cmd, struct lustre_handle *conn,
585 struct lov_stripe_md *md, obd_count page_count,
586 struct brw_page *pagear, brw_callback_t callback,
587 struct io_cb_data *data)
589 if (cmd & OBD_BRW_WRITE)
590 return osc_brw_write(conn, md, page_count, pagear, callback, data);
592 return osc_brw_read(conn, md, page_count, pagear, callback, data);
595 static int osc_enqueue(struct lustre_handle *connh, struct lov_stripe_md *md,
596 struct lustre_handle *parent_lock,
597 __u32 type, void *extentp, int extent_len, __u32 mode,
598 int *flags, void *callback, void *data, int datalen,
599 struct lustre_handle *lockh)
601 __u64 res_id = { md->lmd_object_id };
602 struct obd_device *obddev = class_conn2obd(connh);
603 struct ldlm_extent *extent = extentp;
607 /* Filesystem locks are given a bit of special treatment: first we
608 * fixup the lock to start and end on page boundaries. */
609 extent->start &= PAGE_MASK;
610 extent->end = (extent->end + PAGE_SIZE - 1) & PAGE_MASK;
612 /* Next, search for already existing extent locks that will cover us */
613 //osc_con2dlmcl(conn, &cl, &connection, &rconn);
614 rc = ldlm_lock_match(obddev->obd_namespace, &res_id, type, extent,
615 sizeof(extent), mode, lockh);
617 /* We already have a lock, and it's referenced */
621 /* Next, search for locks that we can upgrade (if we're trying to write)
622 * or are more than we need (if we're trying to read). Because the VFS
623 * and page cache already protect us locally, lots of readers/writers
624 * can share a single PW lock. */
630 rc = ldlm_lock_match(obddev->obd_namespace, &res_id, type, extent,
631 sizeof(extent), mode2, lockh);
634 /* FIXME: This is not incredibly elegant, but it might
635 * be more elegant than adding another parameter to
636 * lock_match. I want a second opinion. */
637 ldlm_lock_addref(lockh, mode);
638 ldlm_lock_decref(lockh, mode2);
643 rc = ldlm_cli_convert(lockh, mode, &flags);
650 rc = ldlm_cli_enqueue(connh, NULL,obddev->obd_namespace,
651 parent_lock, &res_id, type, extent,
652 sizeof(extent), mode, flags, ldlm_completion_ast,
653 callback, data, datalen, lockh);
657 static int osc_cancel(struct lustre_handle *oconn, struct lov_stripe_md *md,
658 __u32 mode, struct lustre_handle *lockh)
662 ldlm_lock_decref(lockh, mode);
667 static int osc_statfs(struct lustre_handle *conn, struct statfs *sfs)
669 struct ptlrpc_request *request;
670 struct obd_statfs *osfs;
671 int rc, size = sizeof(*osfs);
674 request = ptlrpc_prep_req2(conn, OST_STATFS, 0, NULL, NULL);
678 request->rq_replen = lustre_msg_size(1, &size);
680 rc = ptlrpc_queue_wait(request);
681 rc = ptlrpc_check_status(request, rc);
683 CERROR("%s failed: rc = %d\n", __FUNCTION__, rc);
687 osfs = lustre_msg_buf(request->rq_repmsg, 0);
688 obd_statfs_unpack(osfs, sfs);
692 ptlrpc_free_req(request);
696 struct obd_ops osc_obd_ops = {
697 o_setup: client_obd_setup,
698 o_cleanup: client_obd_cleanup,
699 o_statfs: osc_statfs,
700 o_create: osc_create,
701 o_destroy: osc_destroy,
702 o_getattr: osc_getattr,
703 o_setattr: osc_setattr,
706 o_connect: client_obd_connect,
707 o_disconnect: client_obd_disconnect,
710 o_enqueue: osc_enqueue,
714 static int __init osc_init(void)
716 return class_register_type(&osc_obd_ops, LUSTRE_OSC_NAME);
719 static void __exit osc_exit(void)
721 class_unregister_type(LUSTRE_OSC_NAME);
724 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
725 MODULE_DESCRIPTION("Lustre Object Storage Client (OSC) v1.0");
726 MODULE_LICENSE("GPL");
728 module_init(osc_init);
729 module_exit(osc_exit);