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>
27 #include <asm/div64.h>
30 static int lov_connect(struct lustre_handle *conn, struct obd_device *obd,
31 obd_uuid_t cluuid, struct recovd_obd *recovd,
32 ptlrpc_recovery_cb_t recover)
34 struct ptlrpc_request *req = NULL;
35 struct lov_obd *lov = &obd->u.lov;
36 struct client_obd *mdc = &lov->mdcobd->u.cli;
37 struct lov_desc *desc = &lov->desc;
38 struct lustre_handle mdc_conn;
39 obd_uuid_t *uuidarray;
43 rc = class_connect(conn, obd, cluuid);
49 /* We don't want to actually do the underlying connections more than
50 * once, so keep track. */
52 if (lov->refcount > 1)
55 /* retrieve LOV metadata from MDS */
56 rc = obd_connect(&mdc_conn, lov->mdcobd, NULL, recovd, recover);
58 CERROR("cannot connect to mdc: rc = %d\n", rc);
62 rc = mdc_getlovinfo(obd, &mdc_conn, &req);
63 rc2 = obd_disconnect(&mdc_conn);
65 CERROR("cannot get lov info %d\n", rc);
70 CERROR("error disconnecting from MDS %d\n", rc2);
71 GOTO(out_conn, rc = rc2);
75 if (req->rq_repmsg->bufcount < 2 ||
76 req->rq_repmsg->buflens[0] < sizeof(*desc)) {
77 CERROR("LOV desc: invalid descriptor returned\n");
78 GOTO(out_conn, rc = -EINVAL);
81 memcpy(desc, lustre_msg_buf(req->rq_repmsg, 0), sizeof(*desc));
84 if (req->rq_repmsg->buflens[1] < sizeof(*uuidarray)*desc->ld_tgt_count){
85 CERROR("LOV desc: invalid uuid array returned\n");
86 GOTO(out_conn, rc = -EINVAL);
89 mdc->cl_max_mds_easize = lov_mds_md_size(desc->ld_tgt_count);
90 mdc->cl_max_ost_easize = lov_stripe_md_size(desc->ld_tgt_count);
92 if (memcmp(obd->obd_uuid, desc->ld_uuid, sizeof(desc->ld_uuid))) {
93 CERROR("LOV desc: uuid %s not on mds device (%s)\n",
94 obd->obd_uuid, desc->ld_uuid);
95 GOTO(out_conn, rc = -EINVAL);
98 if (desc->ld_tgt_count > 1000) {
99 CERROR("LOV desc: target count > 1000 (%d)\n",
101 GOTO(out_conn, rc = -EINVAL);
104 if (desc->ld_default_stripe_count == 0)
105 desc->ld_default_stripe_count = desc->ld_tgt_count;
107 /* Because of 64-bit divide/mod operations only work with a 32-bit
108 * divisor in a 32-bit kernel, we cannot support a stripe width
111 if (desc->ld_default_stripe_size * desc->ld_tgt_count > ~0UL) {
112 CERROR("LOV desc: stripe width > %lu on 32-bit system\n",
114 GOTO(out_conn, rc = -EINVAL);
117 lov->bufsize = sizeof(struct lov_tgt_desc) * desc->ld_tgt_count;
118 OBD_ALLOC(lov->tgts, lov->bufsize);
120 CERROR("Out of memory\n");
121 GOTO(out_conn, rc = -ENOMEM);
124 uuidarray = lustre_msg_buf(req->rq_repmsg, 1);
125 for (i = 0; i < desc->ld_tgt_count; i++)
126 memcpy(lov->tgts[i].uuid, uuidarray[i], sizeof(*uuidarray));
128 for (i = 0; i < desc->ld_tgt_count; i++) {
129 struct obd_device *tgt = class_uuid2obd(uuidarray[i]);
131 CERROR("Target %s not attached\n", uuidarray[i]);
132 GOTO(out_disc, rc = -EINVAL);
134 if (!(tgt->obd_flags & OBD_SET_UP)) {
135 CERROR("Target %s not set up\n", uuidarray[i]);
136 GOTO(out_disc, rc = -EINVAL);
138 rc = obd_connect(&lov->tgts[i].conn, tgt, NULL, recovd,
141 CERROR("Target %s connect error %d\n",
145 desc->ld_active_tgt_count++;
146 lov->tgts[i].active = 1;
150 ptlrpc_req_finished(req);
155 desc->ld_active_tgt_count--;
156 lov->tgts[i].active = 0;
157 rc2 = obd_disconnect(&lov->tgts[i].conn);
159 CERROR("LOV Target %s disconnect error: rc = %d\n",
162 OBD_FREE(lov->tgts, lov->bufsize);
164 class_disconnect(conn);
168 static int lov_disconnect(struct lustre_handle *conn)
170 struct obd_device *obd = class_conn2obd(conn);
171 struct lov_obd *lov = &obd->u.lov;
177 /* Only disconnect the underlying laters on the final disconnect. */
179 if (lov->refcount != 0)
182 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
183 if (!lov->tgts[i].active) {
184 CERROR("Skipping disconnect for inactive OSC %s\n",
189 lov->desc.ld_active_tgt_count--;
190 lov->tgts[i].active = 0;
191 rc = obd_disconnect(&lov->tgts[i].conn);
193 CERROR("Target %s disconnect error %d\n",
194 lov->tgts[i].uuid, rc);
198 OBD_FREE(lov->tgts, lov->bufsize);
203 rc = class_disconnect(conn);
211 * -EINVAL : UUID can't be found in the LOV's target list
212 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
213 * -EBADF : The UUID is found, but the OBD is the wrong type (!)
214 * -EALREADY: The OSC is already marked (in)active
216 static int lov_set_osc_active(struct lov_obd *lov, obd_uuid_t uuid,
219 struct obd_device *obd;
223 CDEBUG(D_INFO, "Searching in lov %p for uuid %s (activate=%d)\n",
224 lov, uuid, activate);
226 spin_lock(&lov->lov_lock);
227 for (i = 0; i < lov->desc.ld_tgt_count; i++)
228 if (strncmp(uuid, lov->tgts[i].uuid,
229 sizeof(lov->tgts[i].uuid)) == 0)
232 if (i == lov->desc.ld_tgt_count)
233 GOTO(out, rc = -EINVAL);
235 obd = class_conn2obd(&lov->tgts[i].conn);
238 GOTO(out, rc = -ENOTCONN);
241 CDEBUG(D_INFO, "Found OBD %p type %s\n", obd, obd->obd_type->typ_name);
242 if (strcmp(obd->obd_type->typ_name, "osc") != 0) {
244 GOTO(out, rc = -EBADF);
247 if (lov->tgts[i].active == activate) {
248 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
249 activate ? "" : "in");
250 GOTO(out, rc = -EALREADY);
253 CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd, activate ? "" : "in");
255 lov->tgts[i].active = activate;
257 lov->desc.ld_active_tgt_count++;
259 lov->desc.ld_active_tgt_count--;
263 spin_unlock(&lov->lov_lock);
267 static int lov_setup(struct obd_device *obd, obd_count len, void *buf)
269 struct obd_ioctl_data* data = buf;
270 struct lov_obd *lov = &obd->u.lov;
274 if (data->ioc_inllen1 < 1) {
275 CERROR("osc setup requires an MDC UUID\n");
279 if (data->ioc_inllen1 > 37) {
280 CERROR("mdc UUID must be 36 characters or less\n");
284 spin_lock_init(&lov->lov_lock);
285 lov->mdcobd = class_uuid2obd(data->ioc_inlbuf1);
287 CERROR("LOV %s cannot locate MDC %s\n", obd->obd_uuid,
295 /* the LOV expects oa->o_id to be set to the LOV object id */
296 static int lov_create(struct lustre_handle *conn, struct obdo *oa,
297 struct lov_stripe_md **ea)
299 struct obd_export *export = class_conn2export(conn);
301 struct lov_stripe_md *lsm;
302 struct lov_oinfo *loi;
304 int sub_offset, stripe_offset, ost_count, ost_idx, i, rc = 0;
316 lov = &export->exp_obd->u.lov;
318 spin_lock(&lov->lov_lock);
319 ost_count = lov->desc.ld_active_tgt_count;
320 oa->o_easize = lov_stripe_md_size(ost_count);
324 OBD_ALLOC(lsm, oa->o_easize);
326 spin_unlock(&lov->lov_lock);
327 GOTO(out_tmp, rc = -ENOMEM);
331 LASSERT(oa->o_valid & OBD_MD_FLID);
332 lsm->lsm_magic = LOV_MAGIC;
333 lsm->lsm_mds_easize = lov_mds_md_size(ost_count);
334 lsm->lsm_object_id = oa->o_id;
335 if (!lsm->lsm_stripe_count)
336 lsm->lsm_stripe_count = lov->desc.ld_default_stripe_count;
337 if (lsm->lsm_stripe_count > ost_count)
338 lsm->lsm_stripe_count = ost_count;
340 if (!lsm->lsm_stripe_size)
341 lsm->lsm_stripe_size = lov->desc.ld_default_stripe_size;
343 lsm->lsm_ost_count = ost_count;
344 stripe_offset = (((int)lsm->lsm_object_id * lsm->lsm_stripe_count) %
346 sub_offset = ((int)lsm->lsm_object_id*lsm->lsm_stripe_count/ost_count) %
347 lsm->lsm_stripe_count;
348 /* We don't use lsm_stripe_offset anywhere else, so it's not clear why
349 * we save it. -phil */
350 lsm->lsm_stripe_offset = stripe_offset + sub_offset;
352 /* Pick the OSTs before we release the lock */
353 ost_idx = lsm->lsm_stripe_offset;
354 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
356 ost_idx = (ost_idx + 1) % lov->desc.ld_tgt_count;
357 } while (!lov->tgts[ost_idx].active);
358 CDEBUG(D_INFO, "Using ost_idx %d (uuid %s)\n", ost_idx,
359 lov->tgts[ost_idx].uuid);
360 loi->loi_ost_idx = ost_idx;
363 spin_unlock(&lov->lov->lock);
365 CDEBUG(D_INODE, "allocating %d subobjs for objid "LPX64" at idx %d\n",
366 lsm->lsm_stripe_count,lsm->lsm_object_id,lsm->lsm_stripe_offset);
368 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
369 struct lov_stripe_md obj_md;
370 struct lov_stripe_md *obj_mdp = &obj_md;
372 ost_idx = loi->loi_ost_idx;
374 /* create data objects with "parent" OA */
375 memcpy(tmp, oa, sizeof(*tmp));
376 tmp->o_easize = sizeof(struct lov_stripe_md);
377 rc = obd_create(&lov->tgts[ost_idx].conn, tmp, &obj_mdp);
379 CERROR("error creating objid "LPX64" sub-object on "
380 "OST idx %d: rc = %d\n", oa->o_id, ost_idx, rc);
381 GOTO(out_cleanup, rc);
383 loi->loi_id = tmp->o_id;
384 loi->loi_size = tmp->o_size;
385 CDEBUG(D_INODE, "objid "LPX64" has subobj "LPX64" at idx %d\n",
386 lsm->lsm_object_id, loi->loi_id, ost_idx);
400 /* destroy already created objects here */
401 memcpy(tmp, oa, sizeof(*tmp));
402 tmp->o_id = loi->loi_id;
403 err = obd_destroy(&lov->tgts[loi->loi_ost_idx].conn, tmp, NULL);
405 CERROR("Failed to uncreate objid "LPX64" subobj "
406 LPX64" on OST idx %d: rc = %d\n",
407 oa->o_id, loi->loi_id, loi->loi_ost_idx,
410 OBD_FREE(lsm, oa->o_easize);
414 static int lov_destroy(struct lustre_handle *conn, struct obdo *oa,
415 struct lov_stripe_md *lsm)
418 struct obd_export *export = class_conn2export(conn);
420 struct lov_oinfo *loi;
425 CERROR("LOV requires striping ea for destruction\n");
429 if (lsm->lsm_magic != LOV_MAGIC) {
430 CERROR("LOV striping magic bad %#lx != %#lx\n",
431 lsm->lsm_magic, LOV_MAGIC);
435 if (!export || !export->exp_obd)
438 lov = &export->exp_obd->u.lov;
439 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
440 /* create data objects with "parent" OA */
441 memcpy(&tmp, oa, sizeof(tmp));
442 tmp.o_id = loi->loi_id;
443 rc = obd_destroy(&lov->tgts[loi->loi_ost_idx].conn, &tmp, NULL);
445 CERROR("Error destroying objid "LPX64" subobj "LPX64
446 " on OST idx %d\n: rc = %d",
447 oa->o_id, loi->loi_id, loi->loi_ost_idx, rc);
452 /* compute object size given "stripeno" and the ost size */
453 static obd_size lov_stripe_size(struct lov_stripe_md *lsm, obd_size ost_size,
456 unsigned long ssize = lsm->lsm_stripe_size;
457 unsigned long swidth = ssize * lsm->lsm_stripe_count;
458 unsigned long stripe_size;
464 /* do_div(a, b) returns a % b, and a = a / b */
465 stripe_size = do_div(ost_size, ssize);
468 lov_size = ost_size * swidth + stripeno * ssize + stripe_size;
470 lov_size = (ost_size - 1) * swidth + (stripeno + 1) * ssize;
475 static void lov_merge_attrs(struct obdo *tgt, struct obdo *src, obd_flag valid,
476 struct lov_stripe_md *lsm, int stripeno, int *new)
479 obdo_cpy_md(tgt, src, valid);
480 if (valid & OBD_MD_FLSIZE)
481 tgt->o_size = lov_stripe_size(lsm,src->o_size,stripeno);
484 if (valid & OBD_MD_FLSIZE) {
485 /* this handles sparse files properly */
488 lov_size = lov_stripe_size(lsm, src->o_size, stripeno);
489 if (lov_size > tgt->o_size)
490 tgt->o_size = lov_size;
492 if (valid & OBD_MD_FLBLOCKS)
493 tgt->o_blocks += src->o_blocks;
494 if (valid & OBD_MD_FLCTIME && tgt->o_ctime < src->o_ctime)
495 tgt->o_ctime = src->o_ctime;
496 if (valid & OBD_MD_FLMTIME && tgt->o_mtime < src->o_mtime)
497 tgt->o_mtime = src->o_mtime;
501 static int lov_getattr(struct lustre_handle *conn, struct obdo *oa,
502 struct lov_stripe_md *lsm)
505 struct obd_export *export = class_conn2export(conn);
507 struct lov_oinfo *loi;
513 CERROR("LOV requires striping ea\n");
517 if (lsm->lsm_magic != LOV_MAGIC) {
518 CERROR("LOV striping magic bad %#lx != %#lx\n",
519 lsm->lsm_magic, LOV_MAGIC);
523 if (!export || !export->exp_obd)
526 lov = &export->exp_obd->u.lov;
529 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
532 if (loi->loi_id == 0)
535 /* create data objects with "parent" OA */
536 memcpy(&tmp, oa, sizeof(tmp));
537 tmp.o_id = loi->loi_id;
539 err = obd_getattr(&lov->tgts[loi->loi_ost_idx].conn, &tmp,NULL);
541 CERROR("Error getattr objid "LPX64" subobj "LPX64
542 " on OST idx %d: rc = %d\n",
543 oa->o_id, loi->loi_id, loi->loi_ost_idx, err);
546 continue; /* XXX or break? */
548 lov_merge_attrs(oa, &tmp, tmp.o_valid, lsm, i, &new);
553 static int lov_setattr(struct lustre_handle *conn, struct obdo *oa,
554 struct lov_stripe_md *lsm)
557 struct obd_export *export = class_conn2export(conn);
559 struct lov_oinfo *loi;
563 /* Note that this code is currently unused, hence LBUG(), just
564 * to know when/if it is ever revived that it needs cleanups.
569 CERROR("LOV requires striping ea\n");
573 if (lsm->lsm_magic != LOV_MAGIC) {
574 CERROR("LOV striping magic bad %#lx != %#lx\n",
575 lsm->lsm_magic, LOV_MAGIC);
579 if (!export || !export->exp_obd)
582 /* size changes should go through punch and not setattr */
583 LASSERT(!(oa->o_valid & OBD_MD_FLSIZE));
585 lov = &export->exp_obd->u.lov;
586 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
589 /* create data objects with "parent" OA */
590 memcpy(&tmp, oa, sizeof(tmp));
591 tmp.o_id = loi->loi_id;
593 err = obd_setattr(&lov->tgts[loi->loi_ost_idx].conn, &tmp,NULL);
595 CERROR("Error setattr objid "LPX64" subobj "LPX64
596 " on OST idx %d: rc = %d\n",
597 oa->o_id, loi->loi_id, loi->loi_ost_idx, err);
605 static int lov_open(struct lustre_handle *conn, struct obdo *oa,
606 struct lov_stripe_md *lsm)
609 struct obd_export *export = class_conn2export(conn);
611 struct lov_oinfo *loi;
617 CERROR("LOV requires striping ea for opening\n");
621 if (lsm->lsm_magic != LOV_MAGIC) {
622 CERROR("LOV striping magic bad %#lx != %#lx\n",
623 lsm->lsm_magic, LOV_MAGIC);
627 if (!export || !export->exp_obd)
634 lov = &export->exp_obd->u.lov;
637 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
640 /* create data objects with "parent" OA */
641 memcpy(tmp, oa, sizeof(*tmp));
642 tmp->o_id = loi->loi_id;
644 err = obd_open(&lov->tgts[loi->loi_ost_idx].conn, tmp, NULL);
646 CERROR("Error open objid "LPX64" subobj "LPX64
647 " on OST idx %d: rc = %d\n",
648 oa->o_id, lsm->lsm_oinfo[i].loi_id,
649 loi->loi_ost_idx, rc);
654 lov_merge_attrs(oa, tmp, tmp->o_valid, lsm, i, &new);
656 /* FIXME: returning an error, but having opened some objects is a bad
657 * idea, since they will likely never be closed. We either
658 * need to not return an error if _some_ objects could be
659 * opened, and leave it to read/write to return -EIO (with
660 * hopefully partial error status) or close all opened objects
661 * and return an error. I think the former is preferred.
667 static int lov_close(struct lustre_handle *conn, struct obdo *oa,
668 struct lov_stripe_md *lsm)
671 struct obd_export *export = class_conn2export(conn);
673 struct lov_oinfo *loi;
678 CERROR("LOV requires striping ea\n");
682 if (lsm->lsm_magic != LOV_MAGIC) {
683 CERROR("LOV striping magic bad %#lx != %#lx\n",
684 lsm->lsm_magic, LOV_MAGIC);
688 if (!export || !export->exp_obd)
691 lov = &export->exp_obd->u.lov;
692 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
695 /* create data objects with "parent" OA */
696 memcpy(&tmp, oa, sizeof(tmp));
697 tmp.o_id = loi->loi_id;
699 err = obd_close(&lov->tgts[loi->loi_ost_idx].conn, &tmp, NULL);
701 CERROR("Error close objid "LPX64" subobj "LPX64
702 " on OST idx %d: rc = %d\n",
703 oa->o_id, loi->loi_id, loi->loi_ost_idx, err);
712 #define log2(n) ffz(~(n))
715 #warning FIXME: merge these two functions now that they are nearly the same
717 /* compute ost offset in stripe "stripeno" corresponding to offset "lov_off" */
718 static obd_off lov_stripe_offset(struct lov_stripe_md *lsm, obd_off lov_off,
721 unsigned long ssize = lsm->lsm_stripe_size;
722 unsigned long swidth = ssize * lsm->lsm_stripe_count;
723 unsigned long stripe_off, this_stripe;
725 if (lov_off == OBD_OBJECT_EOF || lov_off == 0)
728 /* do_div(a, b) returns a % b, and a = a / b */
729 stripe_off = do_div(lov_off, swidth);
731 this_stripe = stripeno * ssize;
732 if (stripe_off <= this_stripe)
735 stripe_off -= this_stripe;
737 if (stripe_off > ssize)
742 return lov_off * ssize + stripe_off;
745 /* compute which stripe number "lov_off" will be written into */
746 static int lov_stripe_number(struct lov_stripe_md *lsm, obd_off lov_off)
748 unsigned long ssize = lsm->lsm_stripe_size;
749 unsigned long swidth = ssize * lsm->lsm_stripe_count;
750 unsigned long stripe_off;
752 stripe_off = do_div(lov_off, swidth);
754 return stripe_off / ssize;
758 /* FIXME: maybe we'll just make one node the authoritative attribute node, then
759 * we can send this 'punch' to just the authoritative node and the nodes
760 * that the punch will affect. */
761 static int lov_punch(struct lustre_handle *conn, struct obdo *oa,
762 struct lov_stripe_md *lsm,
763 obd_off start, obd_off end)
766 struct obd_export *export = class_conn2export(conn);
768 struct lov_oinfo *loi;
773 CERROR("LOV requires striping ea\n");
777 if (lsm->lsm_magic != LOV_MAGIC) {
778 CERROR("LOV striping magic bad %#lx != %#lx\n",
779 lsm->lsm_magic, LOV_MAGIC);
783 if (!export || !export->exp_obd)
786 lov = &export->exp_obd->u.lov;
787 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
788 obd_off starti = lov_stripe_offset(lsm, start, i);
789 obd_off endi = lov_stripe_offset(lsm, end, i);
794 /* create data objects with "parent" OA */
795 memcpy(&tmp, oa, sizeof(tmp));
796 tmp.o_id = loi->loi_id;
798 err = obd_punch(&lov->tgts[loi->loi_ost_idx].conn, &tmp, NULL,
801 CERROR("Error punch objid "LPX64" subobj "LPX64
802 " on OST idx %d: rc = %d\n",
803 oa->o_id, loi->loi_id, loi->loi_ost_idx, err);
811 static int lov_osc_brw_callback(struct io_cb_data *cbd, int err, int phase)
816 if (phase == CB_PHASE_START)
819 if (phase == CB_PHASE_FINISH) {
822 if (atomic_dec_and_test(&cbd->refcount))
823 ret = cbd->cb(cbd->data, cbd->err, phase);
831 static inline int lov_brw(int cmd, struct lustre_handle *conn,
832 struct lov_stripe_md *lsm, obd_count oa_bufs,
833 struct brw_page *pga,
834 brw_callback_t callback, struct io_cb_data *cbd)
836 int stripe_count = lsm->lsm_stripe_count;
837 struct obd_export *export = class_conn2export(conn);
843 struct lov_stripe_md lsm;
845 } *stripeinfo, *si, *si_last;
846 struct brw_page *ioarr;
848 struct io_cb_data *our_cb;
849 struct lov_oinfo *loi;
854 CERROR("LOV requires striping ea\n");
858 if (lsm->lsm_magic != LOV_MAGIC) {
859 CERROR("LOV striping magic bad %#lx != %#lx\n",
860 lsm->lsm_magic, LOV_MAGIC);
864 lov = &export->exp_obd->u.lov;
866 our_cb = ll_init_cb();
870 OBD_ALLOC(stripeinfo, stripe_count * sizeof(*stripeinfo));
872 GOTO(out_cbdata, rc = -ENOMEM);
874 OBD_ALLOC(where, sizeof(*where) * oa_bufs);
876 GOTO(out_sinfo, rc = -ENOMEM);
878 OBD_ALLOC(ioarr, sizeof(*ioarr) * oa_bufs);
880 GOTO(out_where, rc = -ENOMEM);
882 /* This is the only race-free way I can think of to get the refcount
884 atomic_set(&our_cb->refcount, 0);
885 our_cb->cb = callback;
888 for (i = 0; i < oa_bufs; i++) {
889 where[i] = lov_stripe_number(lsm, pga[i].off);
890 if (stripeinfo[where[i]].bufct++ == 0)
891 atomic_inc(&our_cb->refcount);
894 for (i = 0, loi = lsm->lsm_oinfo, si_last = si = stripeinfo;
895 i < stripe_count; i++, loi++, si_last = si, si++) {
897 si->index = si_last->index + si_last->bufct;
898 si->lsm.lsm_object_id = loi->loi_id;
899 si->ost_idx = loi->loi_ost_idx;
902 for (i = 0; i < oa_bufs; i++) {
903 int which = where[i];
906 shift = stripeinfo[which].index + stripeinfo[which].subcount;
907 LASSERT(shift < oa_bufs);
908 ioarr[shift] = pga[i];
909 ioarr[shift].off = lov_stripe_offset(lsm, pga[i].off, which);
910 stripeinfo[which].subcount++;
913 for (i = 0, si = stripeinfo; i < stripe_count; i++, si++) {
914 int shift = si->index;
917 LASSERT(shift < oa_bufs);
918 /* XXX handle error returns here */
919 obd_brw(cmd, &lov->tgts[si->ost_idx].conn,
920 &si->lsm, si->bufct, &ioarr[shift],
921 lov_osc_brw_callback, our_cb);
925 rc = callback(cbd, 0, CB_PHASE_START);
927 OBD_FREE(ioarr, sizeof(*ioarr) * oa_bufs);
929 OBD_FREE(where, sizeof(*where) * oa_bufs);
931 OBD_FREE(stripeinfo, stripe_count * sizeof(*stripeinfo));
933 OBD_FREE(our_cb, sizeof(*our_cb));
937 static int lov_enqueue(struct lustre_handle *conn, struct lov_stripe_md *lsm,
938 struct lustre_handle *parent_lock,
939 __u32 type, void *cookie, int cookielen, __u32 mode,
940 int *flags, void *cb, void *data, int datalen,
941 struct lustre_handle *lockhs)
943 struct obd_export *export = class_conn2export(conn);
945 struct lov_oinfo *loi;
950 CERROR("LOV requires striping ea\n");
954 if (lsm->lsm_magic != LOV_MAGIC) {
955 CERROR("LOV striping magic bad %#lx != %#lx\n",
956 lsm->lsm_magic, LOV_MAGIC);
960 if (!export || !export->exp_obd)
963 lov = &export->exp_obd->u.lov;
964 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
965 struct ldlm_extent *extent = (struct ldlm_extent *)cookie;
966 struct ldlm_extent sub_ext;
967 struct lov_stripe_md submd;
969 sub_ext.start = lov_stripe_offset(lsm, extent->start, i);
970 sub_ext.end = lov_stripe_offset(lsm, extent->end, i);
971 if (sub_ext.start == sub_ext.end)
974 submd.lsm_object_id = loi->loi_id;
975 /* XXX submd lsm_mds_easize should be that from the subobj,
976 * and the subobj should get it opaquely from the LOV.
978 submd.lsm_mds_easize = lov_mds_md_size(lsm->lsm_ost_count);
979 submd.lsm_stripe_count = 0;
980 /* XXX submd is not fully initialized here */
981 rc = obd_enqueue(&(lov->tgts[loi->loi_ost_idx].conn), &submd,
982 parent_lock, type, &sub_ext, sizeof(sub_ext),
983 mode, flags, cb, data, datalen, &(lockhs[i]));
984 // XXX add a lock debug statement here
986 CERROR("Error enqueue objid "LPX64" subobj "LPX64
987 " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
988 loi->loi_id, loi->loi_ost_idx, rc);
993 static int lov_cancel(struct lustre_handle *conn, struct lov_stripe_md *lsm,
994 __u32 mode, struct lustre_handle *lockhs)
996 struct obd_export *export = class_conn2export(conn);
998 struct lov_oinfo *loi;
1003 CERROR("LOV requires striping ea\n");
1007 if (lsm->lsm_magic != LOV_MAGIC) {
1008 CERROR("LOV striping magic bad %#lx != %#lx\n",
1009 lsm->lsm_magic, LOV_MAGIC);
1013 if (!export || !export->exp_obd)
1016 lov = &export->exp_obd->u.lov;
1017 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1018 struct lov_stripe_md submd;
1020 if (lockhs[i].addr == 0)
1023 submd.lsm_object_id = loi->loi_id;
1024 submd.lsm_mds_easize = lov_mds_md_size(lsm->lsm_ost_count);
1025 submd.lsm_stripe_count = 0;
1026 rc = obd_cancel(&lov->tgts[loi->loi_ost_idx].conn, &submd,
1029 CERROR("Error cancel objid "LPX64" subobj "LPX64
1030 " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
1031 loi->loi_id, loi->loi_ost_idx, rc);
1036 static int lov_cancel_unused(struct lustre_handle *conn,
1037 struct lov_stripe_md *lsm, int local_only)
1039 struct obd_export *export = class_conn2export(conn);
1040 struct lov_obd *lov;
1041 struct lov_oinfo *loi;
1046 CERROR("LOV requires striping ea for lock cancellation\n");
1050 if (!export || !export->exp_obd)
1053 lov = &export->exp_obd->u.lov;
1054 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1055 struct lov_stripe_md submd;
1057 submd.lsm_object_id = loi->loi_id;
1058 submd.lsm_mds_easize = lov_mds_md_size(lsm->lsm_ost_count);
1059 submd.lsm_stripe_count = 0;
1060 rc = obd_cancel_unused(&lov->tgts[loi->loi_ost_idx].conn,
1061 &submd, local_only);
1063 CERROR("Error cancel unused objid "LPX64" subobj "LPX64
1064 " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
1065 loi->loi_id, loi->loi_ost_idx, rc);
1070 static int lov_statfs(struct lustre_handle *conn, struct obd_statfs *osfs)
1072 struct obd_export *export = class_conn2export(conn);
1073 struct lov_obd *lov;
1074 struct obd_statfs lov_sfs;
1080 if (!export || !export->exp_obd)
1083 lov = &export->exp_obd->u.lov;
1085 /* We only get block data from the OBD */
1086 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1089 err = obd_statfs(&lov->tgts[i].conn, &lov_sfs);
1091 CERROR("Error statfs OSC %s idx %d: err = %d\n",
1092 lov->tgts[i].uuid, i, err);
1095 continue; /* XXX or break? - probably OK to continue */
1098 memcpy(osfs, &lov_sfs, sizeof(lov_sfs));
1101 osfs->os_bfree += lov_sfs.os_bfree;
1102 osfs->os_bavail += lov_sfs.os_bavail;
1103 osfs->os_blocks += lov_sfs.os_blocks;
1104 /* XXX not sure about this one - depends on policy.
1105 * - could be minimum if we always stripe on all OBDs
1106 * (but that would be wrong for any other policy,
1107 * if one of the OBDs has no more objects left)
1108 * - could be sum if we stripe whole objects
1109 * - could be average, just to give a nice number
1110 * - we just pick first OST and hope it is enough
1111 sfs->f_ffree += lov_sfs.f_ffree;
1118 static int lov_iocontrol(long cmd, struct lustre_handle *conn, int len,
1119 void *karg, void *uarg)
1121 struct obd_device *obddev = class_conn2obd(conn);
1122 struct obd_ioctl_data *data = karg;
1126 if (_IOC_TYPE(cmd) != IOC_LOV_TYPE ||
1127 _IOC_NR(cmd) < IOC_LOV_MIN_NR || _IOC_NR(cmd) > IOC_LOV_MAX_NR) {
1128 CDEBUG(D_IOCTL, "invalid ioctl (type %ld, nr %ld, size %ld)\n",
1129 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
1134 case IOC_LOV_SET_OSC_ACTIVE:
1135 rc = lov_set_osc_active(&obddev->u.lov, data->ioc_inlbuf1,
1145 struct obd_ops lov_obd_ops = {
1147 o_connect: lov_connect,
1148 o_disconnect: lov_disconnect,
1149 o_create: lov_create,
1150 o_destroy: lov_destroy,
1151 o_getattr: lov_getattr,
1152 o_setattr: lov_setattr,
1153 o_statfs: lov_statfs,
1158 o_enqueue: lov_enqueue,
1159 o_cancel: lov_cancel,
1160 o_cancel_unused: lov_cancel_unused,
1161 o_iocontrol: lov_iocontrol
1165 #define LOV_VERSION "v0.1"
1167 static int __init lov_init(void)
1169 printk(KERN_INFO "Lustre Logical Object Volume driver " LOV_VERSION
1170 ", info@clusterfs.com\n");
1171 return class_register_type(&lov_obd_ops, OBD_LOV_DEVICENAME);
1174 static void __exit lov_exit(void)
1176 class_unregister_type(OBD_LOV_DEVICENAME);
1179 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1180 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver v0.1");
1181 MODULE_LICENSE("GPL");
1183 module_init(lov_init);
1184 module_exit(lov_exit);