1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * Copyright (C) 2002 Cluster File Systems, Inc.
7 * Author: Phil Schwan <phil@off.net>
8 * Peter Braam <braam@clusterfs.com>
10 * This code is issued under the GNU General Public License.
11 * See the file COPYING in this distribution
15 #define DEBUG_SUBSYSTEM S_LOV
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/obd_support.h>
20 #include <linux/lustre_lib.h>
21 #include <linux/lustre_net.h>
22 #include <linux/lustre_idl.h>
23 #include <linux/lustre_mds.h>
24 #include <linux/obd_class.h>
25 #include <linux/obd_lov.h>
26 #include <linux/init.h>
28 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
31 static int lov_connect(struct lustre_handle *conn, struct obd_device *obd,
34 struct ptlrpc_request *req;
35 struct lov_obd *lov = &obd->u.lov;
36 struct lustre_handle mdc_conn;
42 rc = class_connect(conn, obd, cluuid);
48 /* retrieve LOV metadata from MDS */
49 rc = obd_connect(&mdc_conn, lov->mdcobd, NULL);
51 CERROR("cannot connect to mdc: rc = %d\n", rc);
52 GOTO(out, rc = -EINVAL);
55 rc = mdc_getlovinfo(obd, &mdc_conn, &uuidarray, &req);
56 rc2 = obd_disconnect(&mdc_conn);
58 CERROR("cannot get lov info or disconnect %d/%d\n", rc, rc2);
59 GOTO(out, (rc) ? rc : rc2 );
63 if (strcmp(obd->obd_uuid, lov->desc.ld_uuid)) {
64 CERROR("lov uuid %s not on mds device (%s)\n",
65 obd->obd_uuid, lov->desc.ld_uuid);
66 GOTO(out, rc = -EINVAL);
68 if (lov->desc.ld_tgt_count > 1000) {
69 CERROR("configuration error: target count > 1000 (%d)\n",
70 lov->desc.ld_tgt_count);
71 GOTO(out, rc = -EINVAL);
73 if (req->rq_repmsg->bufcount < 2 || req->rq_repmsg->buflens[1] <
74 sizeof(uuid_t) * lov->desc.ld_tgt_count) {
75 CERROR("invalid uuid array returned\n");
76 GOTO(out, rc = -EINVAL);
79 lov->bufsize = sizeof(struct lov_tgt_desc) * lov->desc.ld_tgt_count;
80 OBD_ALLOC(lov->tgts, lov->bufsize);
82 CERROR("Out of memory\n");
83 GOTO(out, rc = -ENOMEM);
86 uuidarray = lustre_msg_buf(req->rq_repmsg, 1);
87 for (i = 0 ; i < lov->desc.ld_tgt_count; i++)
88 memcpy(lov->tgts[i].uuid, uuidarray[i], sizeof(uuid_t));
90 for (i = 0 ; i < lov->desc.ld_tgt_count; i++) {
91 struct obd_device *tgt = class_uuid2obd(uuidarray[i]);
93 CERROR("Target %s not attached\n", uuidarray[i]);
94 GOTO(out_mem, rc = -EINVAL);
96 if (!(tgt->obd_flags & OBD_SET_UP)) {
97 CERROR("Target %s not set up\n", uuidarray[i]);
98 GOTO(out_mem, rc = -EINVAL);
100 rc = obd_connect(&lov->tgts[i].conn, tgt, NULL);
102 CERROR("Target %s connect error %d\n",
110 for (i = 0 ; i < lov->desc.ld_tgt_count; i++) {
111 rc2 = obd_disconnect(&lov->tgts[i].conn);
113 CERROR("BAD: Target %s disconnect error %d\n",
116 OBD_FREE(lov->tgts, lov->bufsize);
120 class_disconnect(conn);
121 ptlrpc_free_req(req);
125 static int lov_disconnect(struct lustre_handle *conn)
127 struct obd_device *obd = class_conn2obd(conn);
128 struct lov_obd *lov = &obd->u.lov;
135 for (i = 0 ; i < lov->desc.ld_tgt_count; i++) {
136 rc = obd_disconnect(&lov->tgts[i].conn);
138 CERROR("Target %s disconnect error %d\n",
139 lov->tgts[i].uuid, rc);
143 OBD_FREE(lov->tgts, lov->bufsize);
148 rc = class_disconnect(conn);
154 static int lov_setup(struct obd_device *obd, obd_count len, void *buf)
156 struct obd_ioctl_data* data = buf;
157 struct lov_obd *lov = &obd->u.lov;
161 if (data->ioc_inllen1 < 1) {
162 CERROR("osc setup requires an MDC UUID\n");
166 if (data->ioc_inllen1 > 37) {
167 CERROR("mdc UUID must be less than 38 characters\n");
171 lov->mdcobd = class_uuid2obd(data->ioc_inlbuf1);
173 CERROR("LOV %s cannot locate MDC %s\n", obd->obd_uuid,
181 static inline int lov_stripe_md_size(struct obd_device *obd)
183 struct lov_obd *lov = &obd->u.lov;
186 size = sizeof(struct lov_stripe_md) +
187 lov->desc.ld_tgt_count * sizeof(struct lov_oinfo);
191 static inline int lov_mds_md_size(struct obd_device *obd)
193 struct lov_obd *lov = &obd->u.lov;
196 size = sizeof(struct lov_mds_md) +
197 lov->desc.ld_tgt_count * sizeof(struct lov_object_id);
201 /* the LOV counts on oa->o_id to be set as the LOV object id */
202 static int lov_create(struct lustre_handle *conn, struct obdo *oa, struct lov_stripe_md **ea)
206 struct obd_export *export = class_conn2export(conn);
208 struct lov_stripe_md *md;
212 CERROR("lov_create needs EA for striping information\n");
217 lov = &export->exp_obd->u.lov;
219 oa->o_easize = lov_stripe_md_size(export->exp_obd);
221 OBD_ALLOC(*ea, oa->o_easize);
227 md->lmd_easize = lov_mds_md_size(export->exp_obd);
228 md->lmd_object_id = oa->o_id;
229 if (!md->lmd_stripe_count) {
230 md->lmd_stripe_count = lov->desc.ld_default_stripe_count;
233 if (!md->lmd_stripe_size)
234 md->lmd_stripe_size = lov->desc.ld_default_stripe_size;
238 for (i = 0; i < md->lmd_stripe_count; i++) {
239 struct lov_stripe_md obj_md;
240 struct lov_stripe_md *obj_mdp = &obj_md;
241 /* create data objects with "parent" OA */
242 memcpy(&tmp, oa, sizeof(tmp));
243 tmp.o_easize = sizeof(struct lov_stripe_md);
244 rc = obd_create(&lov->tgts[i].conn, &tmp, &obj_mdp);
246 GOTO(out_cleanup, rc);
247 md->lmd_oinfo[i].loi_id = tmp.o_id;
248 md->lmd_oinfo[i].loi_size = tmp.o_size;
254 for (i2 = 0 ; i2 < i ; i2++) {
255 /* destroy already created objects here */
256 tmp.o_id = md->lmd_oinfo[i].loi_id;
257 rc2 = obd_destroy(&lov->tgts[i].conn, &tmp, NULL);
259 CERROR("Failed to remove object from target %d\n",
267 static int lov_destroy(struct lustre_handle *conn, struct obdo *oa,
268 struct lov_stripe_md *md)
272 struct obd_export *export = class_conn2export(conn);
277 CERROR("LOV requires striping ea for desctruction\n");
281 if (!export || !export->exp_obd)
284 lov = &export->exp_obd->u.lov;
285 for (i = 0; i < md->lmd_stripe_count; i++) {
286 /* create data objects with "parent" OA */
287 memcpy(&tmp, oa, sizeof(tmp));
288 tmp.o_id = md->lmd_oinfo[i].loi_id;
289 rc = obd_destroy(&lov->tgts[i].conn, &tmp, NULL);
291 CERROR("Error destroying object %Ld on %d\n",
298 static int lov_getattr(struct lustre_handle *conn, struct obdo *oa,
299 struct lov_stripe_md *md)
303 struct obd_export *export = class_conn2export(conn);
308 CERROR("LOV requires striping ea for desctruction\n");
312 if (!export || !export->exp_obd)
315 lov = &export->exp_obd->u.lov;
316 for (i = 0; i < md->lmd_stripe_count; i++) {
317 /* create data objects with "parent" OA */
318 memcpy(&tmp, oa, sizeof(tmp));
319 oa->o_id = md->lmd_oinfo[i].loi_id;
321 rc = obd_getattr(&lov->tgts[i].conn, &tmp, NULL);
323 CERROR("Error getattr object %Ld on %d\n",
326 /* XXX can do something more sophisticated here... */
328 obd_id id = oa->o_id;
329 memcpy(oa, &tmp, sizeof(tmp));
332 oa->o_size += tmp.o_size;
339 static int lov_setattr(struct lustre_handle *conn, struct obdo *oa,
340 struct lov_stripe_md *md)
344 struct obd_export *export = class_conn2export(conn);
349 CERROR("LOV requires striping ea for desctruction\n");
353 if (!export || !export->exp_obd)
356 lov = &export->exp_obd->u.lov;
357 for (i = 0; i < md->lmd_stripe_count; i++) {
358 /* create data objects with "parent" OA */
359 memcpy(&tmp, oa, sizeof(tmp));
360 oa->o_id = md->lmd_oinfo[i].loi_id;
362 rc = obd_setattr(&lov->tgts[i].conn, &tmp, NULL);
364 CERROR("Error setattr object %Ld on %d\n",
371 static int lov_open(struct lustre_handle *conn, struct obdo *oa,
372 struct lov_stripe_md *md)
374 int rc = 0, rc2 = 0, i;
376 struct obd_export *export = class_conn2export(conn);
381 CERROR("LOV requires striping ea for opening\n");
385 if (!export || !export->exp_obd)
388 lov = &export->exp_obd->u.lov;
389 for (i = 0; i < md->lmd_stripe_count; i++) {
390 /* create data objects with "parent" OA */
391 memcpy(&tmp, oa, sizeof(tmp));
392 tmp.o_id = md->lmd_oinfo[i].loi_id;
394 rc = obd_open(&lov->tgts[i].conn, &tmp, NULL);
397 CERROR("Error getattr object %Ld on %d\n",
405 static int lov_close(struct lustre_handle *conn, struct obdo *oa,
406 struct lov_stripe_md *md)
410 struct obd_export *export = class_conn2export(conn);
415 CERROR("LOV requires striping ea for desctruction\n");
419 if (!export || !export->exp_obd)
422 lov = &export->exp_obd->u.lov;
423 for (i = 0; i < md->lmd_stripe_count; i++) {
424 /* create data objects with "parent" OA */
425 memcpy(&tmp, oa, sizeof(tmp));
426 tmp.o_id = md->lmd_oinfo[i].loi_id;
428 rc = obd_close(&lov->tgts[i].conn, &tmp, NULL);
430 CERROR("Error getattr object %Ld on %d\n",
438 #define log2(n) ffz(~(n))
441 /* compute offset in stripe i corresponds to offset "in" */
442 __u64 lov_offset(struct lov_stripe_md *md, __u64 in, int i)
444 __u32 ssz = md->lmd_stripe_size;
445 /* full stripes across all * stripe size */
446 __u32 out = ( ((__u32)in) / (md->lmd_stripe_count * ssz)) * ssz;
447 __u32 off = (__u32)in % (md->lmd_stripe_count * ssz);
449 if ( in == 0xffffffffffffffff ) {
450 return 0xffffffffffffffff;
453 if ( (i+1) * ssz <= off )
455 else if ( i * ssz > off )
458 out += (off - (i * ssz)) % ssz;
463 /* compute offset in stripe i corresponds to offset "in" */
464 __u64 lov_stripe(struct lov_stripe_md *md, __u64 in, int *j)
466 __u32 ssz = md->lmd_stripe_size;
468 /* full stripes across all * stripe size */
469 *j = (((__u32) in)/ssz) % md->lmd_stripe_count;
470 off = (__u32)in % (md->lmd_stripe_count * ssz);
471 out = ( ((__u32)in) / (md->lmd_stripe_count * ssz)) * ssz +
472 (off - ((*j) * ssz)) % ssz;;
477 int lov_stripe_which(struct lov_stripe_md *md, __u64 in)
479 __u32 ssz = md->lmd_stripe_size;
481 j = (((__u32) in)/ssz) % md->lmd_stripe_count;
486 /* FIXME: maybe we'll just make one node the authoritative attribute node, then
487 * we can send this 'punch' to just the authoritative node and the nodes
488 * that the punch will affect. */
489 static int lov_punch(struct lustre_handle *conn, struct obdo *oa,
490 struct lov_stripe_md *md,
491 obd_off start, obd_off end)
495 struct obd_export *export = class_conn2export(conn);
500 CERROR("LOV requires striping ea for desctruction\n");
504 if (!export || !export->exp_obd)
507 lov = &export->exp_obd->u.lov;
508 for (i = 0; i < md->lmd_stripe_count; i++) {
509 __u64 starti = lov_offset(md, start, i);
510 __u64 endi = lov_offset(md, end, i);
514 /* create data objects with "parent" OA */
515 memcpy(&tmp, oa, sizeof(tmp));
516 oa->o_id = md->lmd_oinfo[i].loi_id;
518 rc = obd_punch(&lov->tgts[i].conn, &tmp, NULL,
521 CERROR("Error punch object %Ld on %d\n",
528 int lov_osc_brw_callback(struct io_cb_data *cbd, int err, int phase)
533 if (phase == CB_PHASE_START)
536 if (phase == CB_PHASE_FINISH) {
539 if (atomic_dec_and_test(&cbd->refcount))
540 ret = cbd->cb(cbd, cbd->err, phase);
548 static inline int lov_brw(int cmd, struct lustre_handle *conn,
549 struct lov_stripe_md *md,
551 struct brw_page *pga,
552 brw_callback_t callback, struct io_cb_data *cbd)
554 int stripe_count = md->lmd_stripe_count;
555 struct obd_export *export = class_conn2export(conn);
561 struct lov_stripe_md md;
563 struct brw_page *ioarr;
567 lov = &export->exp_obd->u.lov;
570 OBD_ALLOC(stripeinfo, stripe_count * sizeof(*stripeinfo));
574 OBD_ALLOC(ioarr, sizeof(*ioarr) * oa_bufs);
576 OBD_FREE(stripeinfo, stripe_count * sizeof(*stripeinfo));
580 for (i=0 ; i < oa_bufs ; i++ ) {
582 which = lov_stripe_which(md, pga[i].pg->index * PAGE_SIZE);
583 stripeinfo[which].bufct++;
586 for (i=0 ; i < stripe_count ; i++) {
588 stripeinfo[i].index =
589 stripeinfo[i-1].index + stripeinfo[i-1].bufct;
590 stripeinfo[i].md.lmd_object_id =
591 md->lmd_oinfo[i].loi_id;
594 for (i=0 ; i < oa_bufs ; i++ ) {
596 which = lov_stripe_which(md, pga[i].pg->index * PAGE_SIZE);
598 shift = stripeinfo[which].index;
599 ioarr[shift + stripeinfo[which].subcount] = pga[i];
600 ioarr[shift + stripeinfo[which].subcount].off = lov_offset(md, pga[i].pg->index * PAGE_SIZE, which);
601 stripeinfo[which].subcount++;
605 atomic_set(&cbd->refcount, oa_bufs);
606 for (i=0 ; i < stripe_count ; i++) {
607 int shift = stripeinfo[i].index;
608 if (stripeinfo[i].bufct)
609 obd_brw(cmd, &lov->tgts[i].conn, &stripeinfo[i].md,
610 stripeinfo[i].bufct, &ioarr[shift],
611 lov_osc_brw_callback, cbd);
614 rc = callback(cbd, 0, CB_PHASE_START);
619 static int lov_enqueue(struct lustre_handle *conn, struct lov_stripe_md *md,
620 struct lustre_handle *parent_lock,
621 __u32 type, void *cookie, int cookielen, __u32 mode,
622 int *flags, void *cb, void *data, int datalen,
623 struct lustre_handle *lockhs)
626 struct obd_export *export = class_conn2export(conn);
628 struct lov_stripe_md submd;
632 CERROR("LOV requires striping ea for desctruction\n");
636 if (!export || !export->exp_obd)
639 lov = &export->exp_obd->u.lov;
640 for (i = 0; i < md->lmd_stripe_count; i++) {
641 struct ldlm_extent *extent = (struct ldlm_extent *)cookie;
642 struct ldlm_extent sub_ext;
644 sub_ext.start = lov_offset(md, extent->start, i);
645 sub_ext.end = lov_offset(md, extent->end, i);
646 if ( sub_ext.start == sub_ext.end )
649 submd.lmd_object_id = md->lmd_oinfo[i].loi_id;
650 submd.lmd_easize = sizeof(submd);
651 submd.lmd_stripe_count = md->lmd_stripe_count;
652 /* XXX submd is not fully initialized here */
653 rc = obd_enqueue(&(lov->tgts[i].conn), &submd, parent_lock, type,
654 &sub_ext, sizeof(sub_ext), mode, flags, cb, data, datalen, &(lockhs[i]));
655 // XXX add a lock debug statement here
657 CERROR("Error obd_enqueu object %Ld subobj %Ld\n", md->lmd_object_id,
658 md->lmd_oinfo[i].loi_id);
664 static int lov_cancel(struct lustre_handle *conn, struct lov_stripe_md *md, __u32 mode,
665 struct lustre_handle *lockhs)
668 struct obd_export *export = class_conn2export(conn);
673 CERROR("LOV requires striping ea for lock cancellation\n");
677 if (!export || !export->exp_obd)
680 lov = &export->exp_obd->u.lov;
681 for (i = 0; i < md->lmd_stripe_count; i++) {
682 struct lov_stripe_md submd;
684 if ( lockhs[i].addr == 0 )
687 submd.lmd_object_id = md->lmd_oinfo[i].loi_id;
688 submd.lmd_easize = sizeof(submd);
689 rc = obd_cancel(&lov->tgts[i].conn, &submd, mode, &lockhs[i]);
691 CERROR("Error punch object %Ld subobj %Ld\n", md->lmd_object_id,
692 md->lmd_oinfo[i].loi_id);
701 struct obd_ops lov_obd_ops = {
703 o_connect: lov_connect,
704 o_disconnect: lov_disconnect,
705 o_create: lov_create,
706 o_destroy: lov_destroy,
707 o_getattr: lov_getattr,
708 o_setattr: lov_setattr,
713 o_enqueue: lov_enqueue,
718 #define LOV_VERSION "v0.1"
720 static int __init lov_init(void)
722 printk(KERN_INFO "Lustre Logical Object Volume driver " LOV_VERSION
723 ", info@clusterfs.com\n");
724 return class_register_type(&lov_obd_ops, OBD_LOV_DEVICENAME);
727 static void __exit lov_exit(void)
729 class_unregister_type(OBD_LOV_DEVICENAME);
732 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
733 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver v0.1");
734 MODULE_LICENSE("GPL");
736 module_init(lov_init);
737 module_exit(lov_exit);