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 <linux/random.h>
28 #include <linux/slab.h>
29 #include <asm/div64.h>
30 #include <linux/lprocfs_status.h>
32 extern struct lprocfs_vars status_var_nm_1[];
33 extern struct lprocfs_vars status_class_var[];
35 static kmem_cache_t *lov_file_cache;
37 struct lov_file_handles {
38 struct list_head lfh_list;
41 struct lustre_handle *lfh_handles;
45 static int lov_connect(struct lustre_handle *conn, struct obd_device *obd,
46 obd_uuid_t cluuid, struct recovd_obd *recovd,
47 ptlrpc_recovery_cb_t recover)
49 struct ptlrpc_request *req = NULL;
50 struct lov_obd *lov = &obd->u.lov;
51 struct client_obd *mdc = &lov->mdcobd->u.cli;
52 struct lov_desc *desc = &lov->desc;
53 struct obd_export *exp;
54 struct lustre_handle mdc_conn;
55 obd_uuid_t *uuidarray;
59 rc = class_connect(conn, obd, cluuid);
65 /* We don't want to actually do the underlying connections more than
66 * once, so keep track. */
68 if (lov->refcount > 1)
71 exp = class_conn2export(conn);
72 INIT_LIST_HEAD(&exp->exp_lov_data.led_open_head);
74 /* retrieve LOV metadata from MDS */
75 rc = obd_connect(&mdc_conn, lov->mdcobd, NULL, recovd, recover);
77 CERROR("cannot connect to mdc: rc = %d\n", rc);
81 rc = mdc_getlovinfo(obd, &mdc_conn, &req);
82 rc2 = obd_disconnect(&mdc_conn);
84 CERROR("cannot get lov info %d\n", rc);
89 CERROR("error disconnecting from MDS %d\n", rc2);
90 GOTO(out_conn, rc = rc2);
94 if (req->rq_repmsg->bufcount < 2 ||
95 req->rq_repmsg->buflens[0] < sizeof(*desc)) {
96 CERROR("LOV desc: invalid descriptor returned\n");
97 GOTO(out_conn, rc = -EINVAL);
100 memcpy(desc, lustre_msg_buf(req->rq_repmsg, 0), sizeof(*desc));
101 lov_unpackdesc(desc);
103 if (req->rq_repmsg->buflens[1] < sizeof(*uuidarray)*desc->ld_tgt_count){
104 CERROR("LOV desc: invalid uuid array returned\n");
105 GOTO(out_conn, rc = -EINVAL);
108 mdc->cl_max_mds_easize = lov_mds_md_size(desc->ld_tgt_count);
109 mdc->cl_max_ost_easize = lov_stripe_md_size(desc->ld_tgt_count);
111 if (memcmp(obd->obd_uuid, desc->ld_uuid, sizeof(desc->ld_uuid))) {
112 CERROR("LOV desc: uuid %s not on mds device (%s)\n",
113 obd->obd_uuid, desc->ld_uuid);
114 GOTO(out_conn, rc = -EINVAL);
117 if (desc->ld_tgt_count > 1000) {
118 CERROR("LOV desc: target count > 1000 (%d)\n",
120 GOTO(out_conn, rc = -EINVAL);
123 /* Because of 64-bit divide/mod operations only work with a 32-bit
124 * divisor in a 32-bit kernel, we cannot support a stripe width
125 * of 4GB or larger on 32-bit CPUs.
127 if ((desc->ld_default_stripe_count ?
128 desc->ld_default_stripe_count : desc->ld_tgt_count) *
129 desc->ld_default_stripe_size > ~0UL) {
130 CERROR("LOV: stripe width "LPU64"x%u > %lu on 32-bit system\n",
131 desc->ld_default_stripe_size,
132 desc->ld_default_stripe_count ?
133 desc->ld_default_stripe_count : desc->ld_tgt_count,~0UL);
134 GOTO(out_conn, rc = -EINVAL);
137 lov->bufsize = sizeof(struct lov_tgt_desc) * desc->ld_tgt_count;
138 OBD_ALLOC(lov->tgts, lov->bufsize);
140 CERROR("Out of memory\n");
141 GOTO(out_conn, rc = -ENOMEM);
144 uuidarray = lustre_msg_buf(req->rq_repmsg, 1);
145 for (i = 0; i < desc->ld_tgt_count; i++)
146 memcpy(lov->tgts[i].uuid, uuidarray[i], sizeof(*uuidarray));
148 for (i = 0; i < desc->ld_tgt_count; i++) {
149 struct obd_device *tgt = class_uuid2obd(uuidarray[i]);
152 CERROR("Target %s not attached\n", uuidarray[i]);
153 GOTO(out_disc, rc = -EINVAL);
156 if (!(tgt->obd_flags & OBD_SET_UP)) {
157 CERROR("Target %s not set up\n", uuidarray[i]);
158 GOTO(out_disc, rc = -EINVAL);
161 rc = obd_connect(&lov->tgts[i].conn, tgt, NULL, recovd,
164 CERROR("Target %s connect error %d\n",
168 rc = obd_iocontrol(IOC_OSC_REGISTER_LOV, &lov->tgts[i].conn,
169 sizeof(struct obd_device *), obd, NULL);
171 CERROR("Target %s REGISTER_LOV error %d\n",
175 desc->ld_active_tgt_count++;
176 lov->tgts[i].active = 1;
180 ptlrpc_req_finished(req);
185 desc->ld_active_tgt_count--;
186 lov->tgts[i].active = 0;
187 rc2 = obd_disconnect(&lov->tgts[i].conn);
189 CERROR("LOV Target %s disconnect error: rc = %d\n",
192 OBD_FREE(lov->tgts, lov->bufsize);
194 class_disconnect(conn);
198 static int lov_disconnect(struct lustre_handle *conn)
200 struct obd_device *obd = class_conn2obd(conn);
201 struct lov_obd *lov = &obd->u.lov;
202 struct obd_export *exp;
203 struct list_head *p, *n;
209 /* Only disconnect the underlying layers on the final disconnect. */
211 if (lov->refcount != 0)
214 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
215 if (!lov->tgts[i].active) {
216 CERROR("Skipping disconnect for inactive OSC %s\n",
221 lov->desc.ld_active_tgt_count--;
222 lov->tgts[i].active = 0;
223 rc = obd_disconnect(&lov->tgts[i].conn);
225 CERROR("Target %s disconnect error %d\n",
226 lov->tgts[i].uuid, rc);
230 OBD_FREE(lov->tgts, lov->bufsize);
234 exp = class_conn2export(conn);
235 list_for_each_safe(p, n, &exp->exp_lov_data.led_open_head) {
236 /* XXX close these, instead of just discarding them? */
237 struct lov_file_handles *lfh;
238 lfh = list_entry(p, typeof(*lfh), lfh_list);
239 CERROR("discarding open LOV handle %p:"LPX64"\n",
240 lfh, lfh->lfh_cookie);
241 list_del(&lfh->lfh_list);
242 OBD_FREE(lfh->lfh_handles,
243 lfh->lfh_count * sizeof(*lfh->lfh_handles));
244 kmem_cache_free(lov_file_cache, lfh);
248 rc = class_disconnect(conn);
256 * -EINVAL : UUID can't be found in the LOV's target list
257 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
258 * -EBADF : The UUID is found, but the OBD is the wrong type (!)
259 * -EALREADY: The OSC is already marked (in)active
261 static int lov_set_osc_active(struct lov_obd *lov, obd_uuid_t uuid,
264 struct obd_device *obd;
268 CDEBUG(D_INFO, "Searching in lov %p for uuid %s (activate=%d)\n",
269 lov, uuid, activate);
271 spin_lock(&lov->lov_lock);
272 for (i = 0; i < lov->desc.ld_tgt_count; i++)
273 if (strncmp(uuid, lov->tgts[i].uuid,
274 sizeof(lov->tgts[i].uuid)) == 0)
277 if (i == lov->desc.ld_tgt_count)
278 GOTO(out, rc = -EINVAL);
280 obd = class_conn2obd(&lov->tgts[i].conn);
283 GOTO(out, rc = -ENOTCONN);
286 CDEBUG(D_INFO, "Found OBD %p type %s\n", obd, obd->obd_type->typ_name);
287 if (strcmp(obd->obd_type->typ_name, "osc") != 0) {
289 GOTO(out, rc = -EBADF);
292 if (lov->tgts[i].active == activate) {
293 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
294 activate ? "" : "in");
295 GOTO(out, rc = -EALREADY);
298 CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd, activate ? "" : "in");
300 lov->tgts[i].active = activate;
302 lov->desc.ld_active_tgt_count++;
304 lov->desc.ld_active_tgt_count--;
308 spin_unlock(&lov->lov_lock);
312 static int lov_setup(struct obd_device *obd, obd_count len, void *buf)
314 struct obd_ioctl_data* data = buf;
315 struct lov_obd *lov = &obd->u.lov;
319 if (data->ioc_inllen1 < 1) {
320 CERROR("osc setup requires an MDC UUID\n");
324 if (data->ioc_inllen1 > 37) {
325 CERROR("mdc UUID must be 36 characters or less\n");
329 spin_lock_init(&lov->lov_lock);
330 lov->mdcobd = class_uuid2obd(data->ioc_inlbuf1);
332 CERROR("LOV %s cannot locate MDC %s\n", obd->obd_uuid,
339 static struct lov_file_handles *lov_handle2lfh(struct lustre_handle *handle)
341 struct lov_file_handles *lfh = NULL;
343 if (!handle || !handle->addr)
346 lfh = (struct lov_file_handles *)(unsigned long)(handle->addr);
347 if (!kmem_cache_validate(lov_file_cache, lfh))
350 if (lfh->lfh_cookie != handle->cookie)
356 /* the LOV expects oa->o_id to be set to the LOV object id */
357 static int lov_create(struct lustre_handle *conn, struct obdo *oa,
358 struct lov_stripe_md **ea)
360 struct obd_export *export = class_conn2export(conn);
362 struct lov_stripe_md *lsm;
363 struct lov_oinfo *loi;
365 int ost_count, ost_idx = 1, i, rc = 0;
377 lov = &export->exp_obd->u.lov;
379 spin_lock(&lov->lov_lock);
380 ost_count = lov->desc.ld_tgt_count;
381 oa->o_easize = lov_stripe_md_size(ost_count);
385 OBD_ALLOC(lsm, oa->o_easize);
387 spin_unlock(&lov->lov_lock);
388 GOTO(out_tmp, rc = -ENOMEM);
390 lsm->lsm_magic = LOV_MAGIC;
391 lsm->lsm_mds_easize = lov_mds_md_size(ost_count);
392 ost_idx = 0; /* if lsm->lsm_stripe_offset is set yet */
395 LASSERT(oa->o_valid & OBD_MD_FLID);
396 lsm->lsm_object_id = oa->o_id;
397 if (!lsm->lsm_stripe_count)
398 lsm->lsm_stripe_count = lov->desc.ld_default_stripe_count;
399 if (!lsm->lsm_stripe_count)
400 lsm->lsm_stripe_count = lov->desc.ld_active_tgt_count;
401 else if (lsm->lsm_stripe_count > lov->desc.ld_active_tgt_count)
402 lsm->lsm_stripe_count = lov->desc.ld_active_tgt_count;
404 if (!lsm->lsm_stripe_size)
405 lsm->lsm_stripe_size = lov->desc.ld_default_stripe_size;
407 /* Because of 64-bit divide/mod operations only work with a 32-bit
408 * divisor in a 32-bit kernel, we cannot support a stripe width
409 * of 4GB or larger on 32-bit CPUs.
411 if (lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL) {
412 CERROR("LOV: stripe width "LPU64"x%u > %lu on 32-bit system\n",
413 lsm->lsm_stripe_size, lsm->lsm_stripe_count, ~0UL);
414 spin_unlock(&lov->lov_lock);
415 GOTO(out_free, rc = -EINVAL);
418 lsm->lsm_ost_count = ost_count;
419 if (!ost_idx || lsm->lsm_stripe_offset >= ost_count) {
420 int mult = lsm->lsm_object_id * lsm->lsm_stripe_count;
421 int stripe_offset = mult % ost_count;
422 int sub_offset = (mult / ost_count) % lsm->lsm_stripe_count;
424 lsm->lsm_stripe_offset = stripe_offset + sub_offset;
427 while (!lov->tgts[lsm->lsm_stripe_offset].active)
428 lsm->lsm_stripe_offset = (lsm->lsm_stripe_offset+1) % ost_count;
430 /* Pick the OSTs before we release the lock */
431 ost_idx = lsm->lsm_stripe_offset;
432 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
433 CDEBUG(D_INODE, "objid "LPX64"[%d] is ost_idx %d (uuid %s)\n",
434 lsm->lsm_object_id, i, ost_idx, lov->tgts[ost_idx].uuid);
435 loi->loi_ost_idx = ost_idx;
437 ost_idx = (ost_idx + 1) % ost_count;
438 } while (!lov->tgts[ost_idx].active);
441 spin_unlock(&lov->lov_lock);
443 CDEBUG(D_INODE, "allocating %d subobjs for objid "LPX64" at idx %d\n",
444 lsm->lsm_stripe_count,lsm->lsm_object_id,lsm->lsm_stripe_offset);
446 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
447 struct lov_stripe_md obj_md;
448 struct lov_stripe_md *obj_mdp = &obj_md;
450 ost_idx = loi->loi_ost_idx;
452 /* create data objects with "parent" OA */
453 memcpy(tmp, oa, sizeof(*tmp));
454 tmp->o_easize = sizeof(struct lov_stripe_md);
455 rc = obd_create(&lov->tgts[ost_idx].conn, tmp, &obj_mdp);
457 CERROR("error creating objid "LPX64" sub-object on "
458 "OST idx %d: rc = %d\n", oa->o_id, ost_idx, rc);
459 GOTO(out_cleanup, rc);
461 loi->loi_id = tmp->o_id;
462 loi->loi_size = tmp->o_size;
463 CDEBUG(D_INODE, "objid "LPX64" has subobj "LPX64" at idx %d\n",
464 lsm->lsm_object_id, loi->loi_id, ost_idx);
478 /* destroy already created objects here */
479 memcpy(tmp, oa, sizeof(*tmp));
480 tmp->o_id = loi->loi_id;
481 err = obd_destroy(&lov->tgts[loi->loi_ost_idx].conn, tmp, NULL);
483 CERROR("Failed to uncreate objid "LPX64" subobj "
484 LPX64" on OST idx %d: rc = %d\n",
485 oa->o_id, loi->loi_id, loi->loi_ost_idx,
489 OBD_FREE(lsm, oa->o_easize);
493 static int lov_destroy(struct lustre_handle *conn, struct obdo *oa,
494 struct lov_stripe_md *lsm)
497 struct obd_export *export = class_conn2export(conn);
499 struct lov_oinfo *loi;
500 struct lov_file_handles *lfh = NULL;
505 CERROR("LOV requires striping ea for destruction\n");
509 if (lsm->lsm_magic != LOV_MAGIC) {
510 CERROR("LOV striping magic bad %#lx != %#lx\n",
511 lsm->lsm_magic, LOV_MAGIC);
515 if (!export || !export->exp_obd)
518 if (oa->o_valid & OBD_MD_FLHANDLE)
519 lfh = lov_handle2lfh(obdo_handle(oa));
521 lov = &export->exp_obd->u.lov;
522 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
523 memcpy(&tmp, oa, sizeof(tmp));
524 tmp.o_id = loi->loi_id;
526 memcpy(obdo_handle(&tmp), &lfh->lfh_handles[i],
527 sizeof(lfh->lfh_handles[i]));
529 tmp.o_valid &= ~OBD_MD_FLHANDLE;
530 rc = obd_destroy(&lov->tgts[loi->loi_ost_idx].conn, &tmp, NULL);
532 CERROR("Error destroying objid "LPX64" subobj "LPX64
533 " on OST idx %d\n: rc = %d",
534 oa->o_id, loi->loi_id, loi->loi_ost_idx, rc);
539 /* compute object size given "stripeno" and the ost size */
540 static obd_size lov_stripe_size(struct lov_stripe_md *lsm, obd_size ost_size,
543 unsigned long ssize = lsm->lsm_stripe_size;
544 unsigned long swidth = ssize * lsm->lsm_stripe_count;
545 unsigned long stripe_size;
551 /* do_div(a, b) returns a % b, and a = a / b */
552 stripe_size = do_div(ost_size, ssize);
555 lov_size = ost_size * swidth + stripeno * ssize + stripe_size;
557 lov_size = (ost_size - 1) * swidth + (stripeno + 1) * ssize;
562 static void lov_merge_attrs(struct obdo *tgt, struct obdo *src, obd_flag valid,
563 struct lov_stripe_md *lsm, int stripeno, int *new)
566 obdo_cpy_md(tgt, src, valid);
567 if (valid & OBD_MD_FLSIZE)
568 tgt->o_size = lov_stripe_size(lsm,src->o_size,stripeno);
571 if (valid & OBD_MD_FLSIZE) {
572 /* this handles sparse files properly */
575 lov_size = lov_stripe_size(lsm, src->o_size, stripeno);
576 if (lov_size > tgt->o_size)
577 tgt->o_size = lov_size;
579 if (valid & OBD_MD_FLBLOCKS)
580 tgt->o_blocks += src->o_blocks;
581 if (valid & OBD_MD_FLCTIME && tgt->o_ctime < src->o_ctime)
582 tgt->o_ctime = src->o_ctime;
583 if (valid & OBD_MD_FLMTIME && tgt->o_mtime < src->o_mtime)
584 tgt->o_mtime = src->o_mtime;
588 static int lov_getattr(struct lustre_handle *conn, struct obdo *oa,
589 struct lov_stripe_md *lsm)
592 struct obd_export *export = class_conn2export(conn);
594 struct lov_oinfo *loi;
595 struct lov_file_handles *lfh = NULL;
601 CERROR("LOV requires striping ea\n");
605 if (lsm->lsm_magic != LOV_MAGIC) {
606 CERROR("LOV striping magic bad %#lx != %#lx\n",
607 lsm->lsm_magic, LOV_MAGIC);
611 if (!export || !export->exp_obd)
614 lov = &export->exp_obd->u.lov;
616 if (oa->o_valid & OBD_MD_FLHANDLE)
617 lfh = lov_handle2lfh(obdo_handle(oa));
619 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
622 if (loi->loi_id == 0)
625 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
626 "%u\n", oa->o_id, i, loi->loi_id, loi->loi_ost_idx);
627 /* create data objects with "parent" OA */
628 memcpy(&tmp, oa, sizeof(tmp));
629 tmp.o_id = loi->loi_id;
631 memcpy(obdo_handle(&tmp), &lfh->lfh_handles[i],
632 sizeof(lfh->lfh_handles[i]));
634 tmp.o_valid &= ~OBD_MD_FLHANDLE;
636 err = obd_getattr(&lov->tgts[loi->loi_ost_idx].conn, &tmp,NULL);
638 CERROR("Error getattr objid "LPX64" subobj "LPX64
639 " on OST idx %d: rc = %d\n",
640 oa->o_id, loi->loi_id, loi->loi_ost_idx, err);
643 continue; /* XXX or break? */
645 lov_merge_attrs(oa, &tmp, tmp.o_valid, lsm, i, &new);
650 static int lov_setattr(struct lustre_handle *conn, struct obdo *oa,
651 struct lov_stripe_md *lsm)
654 struct obd_export *export = class_conn2export(conn);
656 struct lov_oinfo *loi;
657 struct lov_file_handles *lfh = NULL;
661 /* Note that this code is currently unused, hence LBUG(), just
662 * to know when/if it is ever revived that it needs cleanups.
667 CERROR("LOV requires striping ea\n");
671 if (lsm->lsm_magic != LOV_MAGIC) {
672 CERROR("LOV striping magic bad %#lx != %#lx\n",
673 lsm->lsm_magic, LOV_MAGIC);
677 if (!export || !export->exp_obd)
680 /* size changes should go through punch and not setattr */
681 LASSERT(!(oa->o_valid & OBD_MD_FLSIZE));
687 if (oa->o_valid & OBD_MD_FLHANDLE)
688 lfh = lov_handle2lfh(obdo_handle(oa));
690 lov = &export->exp_obd->u.lov;
691 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
694 obdo_cpy_md(tmp, oa, oa->o_valid);
697 memcpy(obdo_handle(tmp), &lfh->lfh_handles[i],
698 sizeof(lfh->lfh_handles[i]));
700 tmp->o_valid &= ~OBD_MD_FLHANDLE;
702 tmp->o_id = loi->loi_id;
704 err = obd_setattr(&lov->tgts[loi->loi_ost_idx].conn, tmp, NULL);
706 CERROR("Error setattr objid "LPX64" subobj "LPX64
707 " on OST idx %d: rc = %d\n",
708 oa->o_id, loi->loi_id, loi->loi_ost_idx, err);
717 static int lov_open(struct lustre_handle *conn, struct obdo *oa,
718 struct lov_stripe_md *lsm)
721 struct obd_export *export = class_conn2export(conn);
723 struct lov_oinfo *loi;
724 struct lov_file_handles *lfh = NULL;
730 CERROR("LOV requires striping ea for opening\n");
734 if (lsm->lsm_magic != LOV_MAGIC) {
735 CERROR("LOV striping magic bad %#lx != %#lx\n",
736 lsm->lsm_magic, LOV_MAGIC);
740 if (!export || !export->exp_obd)
747 lfh = kmem_cache_alloc(lov_file_cache, GFP_KERNEL);
749 GOTO(out_tmp, rc = -ENOMEM);
750 OBD_ALLOC(lfh->lfh_handles,
751 lsm->lsm_stripe_count * sizeof(*lfh->lfh_handles));
752 if (!lfh->lfh_handles)
753 GOTO(out_lfh, rc = -ENOMEM);
755 lov = &export->exp_obd->u.lov;
758 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
761 /* create data objects with "parent" OA */
762 memcpy(tmp, oa, sizeof(*tmp));
763 tmp->o_id = loi->loi_id;
765 err = obd_open(&lov->tgts[loi->loi_ost_idx].conn, tmp, NULL);
767 CERROR("Error open objid "LPX64" subobj "LPX64
768 " on OST idx %d: rc = %d\n",
769 oa->o_id, lsm->lsm_oinfo[i].loi_id,
770 loi->loi_ost_idx, rc);
775 lov_merge_attrs(oa, tmp, tmp->o_valid, lsm, i, &new);
777 if (tmp->o_valid & OBD_MD_FLHANDLE)
778 memcpy(&lfh->lfh_handles[i], obdo_handle(tmp),
779 sizeof(lfh->lfh_handles[i]));
782 if (tmp->o_valid & OBD_MD_FLHANDLE) {
783 struct lustre_handle *handle = obdo_handle(oa);
785 lfh->lfh_count = lsm->lsm_stripe_count;
786 get_random_bytes(&lfh->lfh_cookie, sizeof(lfh->lfh_cookie));
788 handle->addr = (__u64)(unsigned long)lfh;
789 handle->cookie = lfh->lfh_cookie;
790 oa->o_valid |= OBD_MD_FLHANDLE;
791 list_add(&lfh->lfh_list, &export->exp_lov_data.led_open_head);
795 /* FIXME: returning an error, but having opened some objects is a bad
796 * idea, since they will likely never be closed. We either
797 * need to not return an error if _some_ objects could be
798 * opened, and leave it to read/write to return -EIO (with
799 * hopefully partial error status) or close all opened objects
800 * and return an error. I think the former is preferred.
807 OBD_FREE(lfh->lfh_handles,
808 lsm->lsm_stripe_count * sizeof(*lfh->lfh_handles));
810 lfh->lfh_cookie = DEAD_HANDLE_MAGIC;
811 kmem_cache_free(lov_file_cache, lfh);
815 static int lov_close(struct lustre_handle *conn, struct obdo *oa,
816 struct lov_stripe_md *lsm)
819 struct obd_export *export = class_conn2export(conn);
821 struct lov_oinfo *loi;
822 struct lov_file_handles *lfh = NULL;
827 CERROR("LOV requires striping ea\n");
831 if (lsm->lsm_magic != LOV_MAGIC) {
832 CERROR("LOV striping magic bad %#lx != %#lx\n",
833 lsm->lsm_magic, LOV_MAGIC);
837 if (!export || !export->exp_obd)
840 if (oa->o_valid & OBD_MD_FLHANDLE)
841 lfh = lov_handle2lfh(obdo_handle(oa));
843 lov = &export->exp_obd->u.lov;
844 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
847 /* create data objects with "parent" OA */
848 memcpy(&tmp, oa, sizeof(tmp));
849 tmp.o_id = loi->loi_id;
851 memcpy(obdo_handle(&tmp), &lfh->lfh_handles[i],
852 sizeof(lfh->lfh_handles[i]));
854 tmp.o_valid &= ~OBD_MD_FLHANDLE;
856 err = obd_close(&lov->tgts[loi->loi_ost_idx].conn, &tmp, NULL);
858 CERROR("Error close objid "LPX64" subobj "LPX64
859 " on OST idx %d: rc = %d\n",
860 oa->o_id, loi->loi_id, loi->loi_ost_idx, err);
866 list_del(&lfh->lfh_list);
867 OBD_FREE(lfh->lfh_handles,
868 lsm->lsm_stripe_count * sizeof(*lfh->lfh_handles));
869 lfh->lfh_cookie = DEAD_HANDLE_MAGIC;
870 kmem_cache_free(lov_file_cache, lfh);
877 #define log2(n) ffz(~(n))
880 #warning FIXME: merge these two functions now that they are nearly the same
882 /* compute ost offset in stripe "stripeno" corresponding to offset "lov_off" */
883 static obd_off lov_stripe_offset(struct lov_stripe_md *lsm, obd_off lov_off,
886 unsigned long ssize = lsm->lsm_stripe_size;
887 unsigned long swidth = ssize * lsm->lsm_stripe_count;
888 unsigned long stripe_off, this_stripe;
890 if (lov_off == OBD_OBJECT_EOF || lov_off == 0)
893 /* do_div(a, b) returns a % b, and a = a / b */
894 stripe_off = do_div(lov_off, swidth);
896 this_stripe = stripeno * ssize;
897 if (stripe_off <= this_stripe)
900 stripe_off -= this_stripe;
902 if (stripe_off > ssize)
907 return lov_off * ssize + stripe_off;
910 /* compute which stripe number "lov_off" will be written into */
911 static int lov_stripe_number(struct lov_stripe_md *lsm, obd_off lov_off)
913 unsigned long ssize = lsm->lsm_stripe_size;
914 unsigned long swidth = ssize * lsm->lsm_stripe_count;
915 unsigned long stripe_off;
917 stripe_off = do_div(lov_off, swidth);
919 return stripe_off / ssize;
923 /* FIXME: maybe we'll just make one node the authoritative attribute node, then
924 * we can send this 'punch' to just the authoritative node and the nodes
925 * that the punch will affect. */
926 static int lov_punch(struct lustre_handle *conn, struct obdo *oa,
927 struct lov_stripe_md *lsm,
928 obd_off start, obd_off end)
931 struct obd_export *export = class_conn2export(conn);
933 struct lov_oinfo *loi;
934 struct lov_file_handles *lfh = NULL;
939 CERROR("LOV requires striping ea\n");
943 if (lsm->lsm_magic != LOV_MAGIC) {
944 CERROR("LOV striping magic bad %#lx != %#lx\n",
945 lsm->lsm_magic, LOV_MAGIC);
949 if (!export || !export->exp_obd)
952 if (oa->o_valid & OBD_MD_FLHANDLE)
953 lfh = lov_handle2lfh(obdo_handle(oa));
955 lov = &export->exp_obd->u.lov;
956 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
957 obd_off starti = lov_stripe_offset(lsm, start, i);
958 obd_off endi = lov_stripe_offset(lsm, end, i);
963 /* create data objects with "parent" OA */
964 memcpy(&tmp, oa, sizeof(tmp));
965 tmp.o_id = loi->loi_id;
967 memcpy(obdo_handle(&tmp), &lfh->lfh_handles[i],
968 sizeof(lfh->lfh_handles[i]));
970 tmp.o_valid &= ~OBD_MD_FLHANDLE;
972 err = obd_punch(&lov->tgts[loi->loi_ost_idx].conn, &tmp, NULL,
975 CERROR("Error punch objid "LPX64" subobj "LPX64
976 " on OST idx %d: rc = %d\n",
977 oa->o_id, loi->loi_id, loi->loi_ost_idx, err);
985 static int lov_osc_brw_callback(struct io_cb_data *cbd, int err, int phase)
990 if (phase == CB_PHASE_START)
993 if (phase == CB_PHASE_FINISH) {
996 if (atomic_dec_and_test(&cbd->refcount))
997 ret = cbd->cb(cbd->data, cbd->err, phase);
1005 static inline int lov_brw(int cmd, struct lustre_handle *conn,
1006 struct lov_stripe_md *lsm, obd_count oa_bufs,
1007 struct brw_page *pga,
1008 brw_callback_t callback, struct io_cb_data *cbd)
1010 int stripe_count = lsm->lsm_stripe_count;
1011 struct obd_export *export = class_conn2export(conn);
1012 struct lov_obd *lov;
1017 struct lov_stripe_md lsm;
1019 } *stripeinfo, *si, *si_last;
1020 struct brw_page *ioarr;
1022 struct io_cb_data *our_cb;
1023 struct lov_oinfo *loi;
1028 CERROR("LOV requires striping ea\n");
1032 if (lsm->lsm_magic != LOV_MAGIC) {
1033 CERROR("LOV striping magic bad %#lx != %#lx\n",
1034 lsm->lsm_magic, LOV_MAGIC);
1038 lov = &export->exp_obd->u.lov;
1040 our_cb = ll_init_cb();
1044 OBD_ALLOC(stripeinfo, stripe_count * sizeof(*stripeinfo));
1046 GOTO(out_cbdata, rc = -ENOMEM);
1048 OBD_ALLOC(where, sizeof(*where) * oa_bufs);
1050 GOTO(out_sinfo, rc = -ENOMEM);
1052 OBD_ALLOC(ioarr, sizeof(*ioarr) * oa_bufs);
1054 GOTO(out_where, rc = -ENOMEM);
1056 /* This is the only race-free way I can think of to get the refcount
1058 atomic_set(&our_cb->refcount, 0);
1059 our_cb->cb = callback;
1062 for (i = 0; i < oa_bufs; i++) {
1063 where[i] = lov_stripe_number(lsm, pga[i].off);
1064 if (stripeinfo[where[i]].bufct++ == 0)
1065 atomic_inc(&our_cb->refcount);
1068 for (i = 0, loi = lsm->lsm_oinfo, si_last = si = stripeinfo;
1069 i < stripe_count; i++, loi++, si_last = si, si++) {
1071 si->index = si_last->index + si_last->bufct;
1072 si->lsm.lsm_object_id = loi->loi_id;
1073 si->ost_idx = loi->loi_ost_idx;
1076 for (i = 0; i < oa_bufs; i++) {
1077 int which = where[i];
1080 shift = stripeinfo[which].index + stripeinfo[which].subcount;
1081 LASSERT(shift < oa_bufs);
1082 ioarr[shift] = pga[i];
1083 ioarr[shift].off = lov_stripe_offset(lsm, pga[i].off, which);
1084 stripeinfo[which].subcount++;
1087 for (i = 0, si = stripeinfo; i < stripe_count; i++, si++) {
1088 int shift = si->index;
1091 LASSERT(shift < oa_bufs);
1092 /* XXX handle error returns here */
1093 obd_brw(cmd, &lov->tgts[si->ost_idx].conn,
1094 &si->lsm, si->bufct, &ioarr[shift],
1095 lov_osc_brw_callback, our_cb);
1099 rc = callback(cbd, 0, CB_PHASE_START);
1101 OBD_FREE(ioarr, sizeof(*ioarr) * oa_bufs);
1103 OBD_FREE(where, sizeof(*where) * oa_bufs);
1105 OBD_FREE(stripeinfo, stripe_count * sizeof(*stripeinfo));
1107 OBD_FREE(our_cb, sizeof(*our_cb));
1111 static int lov_enqueue(struct lustre_handle *conn, struct lov_stripe_md *lsm,
1112 struct lustre_handle *parent_lock,
1113 __u32 type, void *cookie, int cookielen, __u32 mode,
1114 int *flags, void *cb, void *data, int datalen,
1115 struct lustre_handle *lockhs)
1117 struct obd_export *export = class_conn2export(conn);
1118 struct lov_obd *lov;
1119 struct lov_oinfo *loi;
1124 CERROR("LOV requires striping ea\n");
1128 if (lsm->lsm_magic != LOV_MAGIC) {
1129 CERROR("LOV striping magic bad %#lx != %#lx\n",
1130 lsm->lsm_magic, LOV_MAGIC);
1134 if (!export || !export->exp_obd)
1137 lov = &export->exp_obd->u.lov;
1138 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1139 struct ldlm_extent *extent = (struct ldlm_extent *)cookie;
1140 struct ldlm_extent sub_ext;
1141 struct lov_stripe_md submd;
1143 sub_ext.start = lov_stripe_offset(lsm, extent->start, i);
1144 sub_ext.end = lov_stripe_offset(lsm, extent->end, i);
1145 if (sub_ext.start == sub_ext.end)
1148 submd.lsm_object_id = loi->loi_id;
1149 /* XXX submd lsm_mds_easize should be that from the subobj,
1150 * and the subobj should get it opaquely from the LOV.
1152 submd.lsm_mds_easize = lov_mds_md_size(lsm->lsm_ost_count);
1153 submd.lsm_stripe_count = 0;
1154 /* XXX submd is not fully initialized here */
1155 rc = obd_enqueue(&(lov->tgts[loi->loi_ost_idx].conn), &submd,
1156 parent_lock, type, &sub_ext, sizeof(sub_ext),
1157 mode, flags, cb, data, datalen, &(lockhs[i]));
1158 // XXX add a lock debug statement here
1160 CERROR("Error enqueue objid "LPX64" subobj "LPX64
1161 " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
1162 loi->loi_id, loi->loi_ost_idx, rc);
1167 static int lov_cancel(struct lustre_handle *conn, struct lov_stripe_md *lsm,
1168 __u32 mode, struct lustre_handle *lockhs)
1170 struct obd_export *export = class_conn2export(conn);
1171 struct lov_obd *lov;
1172 struct lov_oinfo *loi;
1177 CERROR("LOV requires striping ea\n");
1181 if (lsm->lsm_magic != LOV_MAGIC) {
1182 CERROR("LOV striping magic bad %#lx != %#lx\n",
1183 lsm->lsm_magic, LOV_MAGIC);
1187 if (!export || !export->exp_obd)
1190 lov = &export->exp_obd->u.lov;
1191 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1192 struct lov_stripe_md submd;
1194 if (lockhs[i].addr == 0)
1197 submd.lsm_object_id = loi->loi_id;
1198 submd.lsm_mds_easize = lov_mds_md_size(lsm->lsm_ost_count);
1199 submd.lsm_stripe_count = 0;
1200 rc = obd_cancel(&lov->tgts[loi->loi_ost_idx].conn, &submd,
1203 CERROR("Error cancel objid "LPX64" subobj "LPX64
1204 " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
1205 loi->loi_id, loi->loi_ost_idx, rc);
1210 static int lov_cancel_unused(struct lustre_handle *conn,
1211 struct lov_stripe_md *lsm, int flags)
1213 struct obd_export *export = class_conn2export(conn);
1214 struct lov_obd *lov;
1215 struct lov_oinfo *loi;
1220 CERROR("LOV requires striping ea for lock cancellation\n");
1224 if (!export || !export->exp_obd)
1227 lov = &export->exp_obd->u.lov;
1228 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1229 struct lov_stripe_md submd;
1231 submd.lsm_object_id = loi->loi_id;
1232 submd.lsm_mds_easize = lov_mds_md_size(lsm->lsm_ost_count);
1233 submd.lsm_stripe_count = 0;
1234 rc = obd_cancel_unused(&lov->tgts[loi->loi_ost_idx].conn,
1237 CERROR("Error cancel unused objid "LPX64" subobj "LPX64
1238 " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
1239 loi->loi_id, loi->loi_ost_idx, rc);
1244 static int lov_statfs(struct lustre_handle *conn, struct obd_statfs *osfs)
1246 struct obd_export *export = class_conn2export(conn);
1247 struct lov_obd *lov;
1248 struct obd_statfs lov_sfs;
1254 if (!export || !export->exp_obd)
1257 lov = &export->exp_obd->u.lov;
1259 /* We only get block data from the OBD */
1260 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1263 if (!lov->tgts[i].active)
1266 err = obd_statfs(&lov->tgts[i].conn, &lov_sfs);
1268 CERROR("Error statfs OSC %s idx %d: err = %d\n",
1269 lov->tgts[i].uuid, i, err);
1272 continue; /* XXX or break? - probably OK to continue */
1275 memcpy(osfs, &lov_sfs, sizeof(lov_sfs));
1278 osfs->os_bfree += lov_sfs.os_bfree;
1279 osfs->os_bavail += lov_sfs.os_bavail;
1280 osfs->os_blocks += lov_sfs.os_blocks;
1281 /* XXX not sure about this one - depends on policy.
1282 * - could be minimum if we always stripe on all OBDs
1283 * (but that would be wrong for any other policy,
1284 * if one of the OBDs has no more objects left)
1285 * - could be sum if we stripe whole objects
1286 * - could be average, just to give a nice number
1287 * - we just pick first OST and hope it is enough
1288 sfs->f_ffree += lov_sfs.f_ffree;
1295 static int lov_iocontrol(long cmd, struct lustre_handle *conn, int len,
1296 void *karg, void *uarg)
1298 struct obd_device *obddev = class_conn2obd(conn);
1299 struct obd_ioctl_data *data = karg;
1300 struct lov_obd *lov = &obddev->u.lov;
1301 struct lov_desc *desc;
1302 struct lov_tgt_desc *tgtdesc;
1309 case IOC_LOV_SET_OSC_ACTIVE:
1310 rc = lov_set_osc_active(lov,data->ioc_inlbuf1,data->ioc_offset);
1312 case OBD_IOC_LOV_GET_CONFIG:
1315 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
1318 data = (struct obd_ioctl_data *)buf;
1320 if (sizeof(*desc) > data->ioc_inllen1) {
1325 count = lov->desc.ld_tgt_count;
1327 if (sizeof(*uuidp) * count > data->ioc_inllen2) {
1332 desc = (struct lov_desc *)data->ioc_inlbuf1;
1333 uuidp = (obd_uuid_t *)data->ioc_inlbuf2;
1334 memcpy(desc, &(lov->desc), sizeof(*desc));
1336 tgtdesc = lov->tgts;
1337 for (i = 0; i < count; i++, uuidp++, tgtdesc++)
1338 memcpy(uuidp, tgtdesc->uuid, sizeof(*uuidp));
1340 rc = copy_to_user((void *)uarg, buf, len);
1344 if (lov->desc.ld_tgt_count == 0)
1347 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1348 int err = obd_iocontrol(cmd, &lov->tgts[i].conn,
1358 int lov_attach(struct obd_device *dev,
1359 obd_count len, void *data)
1362 rc = lprocfs_reg_obd(dev, (struct lprocfs_vars*)status_var_nm_1,
1367 int lov_detach(struct obd_device *dev)
1370 rc = lprocfs_dereg_obd(dev);
1375 struct obd_ops lov_obd_ops = {
1376 o_attach: lov_attach,
1377 o_detach: lov_detach,
1379 o_connect: lov_connect,
1380 o_disconnect: lov_disconnect,
1381 o_create: lov_create,
1382 o_destroy: lov_destroy,
1383 o_getattr: lov_getattr,
1384 o_setattr: lov_setattr,
1385 o_statfs: lov_statfs,
1390 o_enqueue: lov_enqueue,
1391 o_cancel: lov_cancel,
1392 o_cancel_unused: lov_cancel_unused,
1393 o_iocontrol: lov_iocontrol
1397 #define LOV_VERSION "v0.1"
1399 static int __init lov_init(void)
1403 printk(KERN_INFO "Lustre Logical Object Volume driver " LOV_VERSION
1404 ", info@clusterfs.com\n");
1405 lov_file_cache = kmem_cache_create("ll_lov_file_data",
1406 sizeof(struct lov_file_handles),
1408 if (!lov_file_cache)
1411 rc = class_register_type(&lov_obd_ops,
1412 (struct lprocfs_vars*)status_class_var,
1413 OBD_LOV_DEVICENAME);
1421 static void __exit lov_exit(void)
1424 if (kmem_cache_destroy(lov_file_cache))
1425 CERROR("couldn't free LOV open cache\n");
1426 class_unregister_type(OBD_LOV_DEVICENAME);
1430 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1431 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver " LOV_VERSION);
1432 MODULE_LICENSE("GPL");
1434 module_init(lov_init);
1435 module_exit(lov_exit);