1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
5 * Author: Phil Schwan <phil@clusterfs.com>
6 * Peter Braam <braam@clusterfs.com>
7 * Mike Shaver <shaver@clusterfs.com>
9 * This file is part of Lustre, http://www.lustre.org.
11 * Lustre is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General Public
13 * License as published by the Free Software Foundation.
15 * Lustre is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Lustre; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 # define EXPORT_SYMTAB
28 #define DEBUG_SUBSYSTEM S_LOV
30 #include <linux/slab.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/slab.h>
34 #include <linux/pagemap.h>
35 #include <linux/seq_file.h>
36 #include <asm/div64.h>
38 #include <liblustre.h>
41 #include <linux/obd_support.h>
42 #include <linux/lustre_lib.h>
43 #include <linux/lustre_net.h>
44 #include <linux/lustre_idl.h>
45 #include <linux/lustre_dlm.h>
46 #include <linux/lustre_mds.h>
47 #include <linux/obd_class.h>
48 #include <linux/obd_lov.h>
49 #include <linux/obd_ost.h>
50 #include <linux/lprocfs_status.h>
52 #include "lov_internal.h"
54 static int lov_stripe_offset(struct lov_stripe_md *lsm, obd_off lov_off,
55 int stripeno, obd_off *obd_off);
58 int lov_attach(struct obd_device *dev, obd_count len, void *data)
60 struct lprocfs_static_vars lvars;
63 lprocfs_init_vars(lov, &lvars);
64 rc = lprocfs_obd_attach(dev, lvars.obd_vars);
67 struct proc_dir_entry *entry;
69 entry = create_proc_entry("target_obd", 0444,
74 entry->proc_fops = &lov_proc_target_fops;
82 int lov_detach(struct obd_device *dev)
84 return lprocfs_obd_detach(dev);
87 static int lov_connect(struct lustre_handle *conn, struct obd_device *obd,
88 struct obd_uuid *cluuid)
90 struct ptlrpc_request *req = NULL;
91 struct lov_obd *lov = &obd->u.lov;
92 struct lov_desc *desc = &lov->desc;
93 struct lov_tgt_desc *tgts;
94 struct obd_export *exp;
98 rc = class_connect(conn, obd, cluuid);
102 exp = class_conn2export(conn);
104 /* We don't want to actually do the underlying connections more than
105 * once, so keep track. */
107 if (lov->refcount > 1) {
108 class_export_put(exp);
112 for (i = 0, tgts = lov->tgts; i < desc->ld_tgt_count; i++, tgts++) {
113 struct obd_uuid *tgt_uuid = &tgts->uuid;
114 struct obd_device *tgt_obd;
115 struct obd_uuid lov_osc_uuid = { "LOV_OSC_UUID" };
116 struct lustre_handle conn = {0, };
118 LASSERT( tgt_uuid != NULL);
120 tgt_obd = class_find_client_obd(tgt_uuid, LUSTRE_OSC_NAME,
124 CERROR("Target %s not attached\n", tgt_uuid->uuid);
125 GOTO(out_disc, rc = -EINVAL);
128 if (!tgt_obd->obd_set_up) {
129 CERROR("Target %s not set up\n", tgt_uuid->uuid);
130 GOTO(out_disc, rc = -EINVAL);
133 if (tgt_obd->u.cli.cl_import->imp_invalid) {
134 CERROR("not connecting OSC %s; administratively "
135 "disabled\n", tgt_uuid->uuid);
136 rc = obd_register_observer(tgt_obd, obd);
138 CERROR("Target %s register_observer error %d; "
139 "will not be able to reactivate\n",
145 rc = obd_connect(&conn, tgt_obd, &lov_osc_uuid);
147 CERROR("Target %s connect error %d\n", tgt_uuid->uuid,
151 tgts->ltd_exp = class_conn2export(&conn);
153 rc = obd_register_observer(tgt_obd, obd);
155 CERROR("Target %s register_observer error %d\n",
157 obd_disconnect(tgts->ltd_exp, 0);
161 desc->ld_active_tgt_count++;
165 ptlrpc_req_finished(req);
166 class_export_put(exp);
171 struct obd_uuid uuid;
173 --desc->ld_active_tgt_count;
175 /* save for CERROR below; (we know it's terminated) */
177 rc2 = obd_disconnect(tgts->ltd_exp, 0);
179 CERROR("error: LOV target %s disconnect on OST idx %d: "
180 "rc = %d\n", uuid.uuid, i, rc2);
182 class_disconnect(exp, 0);
186 static int lov_disconnect(struct obd_export *exp, int flags)
188 struct obd_device *obd = class_exp2obd(exp);
189 struct lov_obd *lov = &obd->u.lov;
196 /* Only disconnect the underlying layers on the final disconnect. */
198 if (lov->refcount != 0)
201 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
202 if (lov->tgts[i].ltd_exp == NULL)
205 if (obd->obd_no_recov) {
206 /* Pass it on to our clients.
207 * XXX This should be an argument to disconnect,
208 * XXX not a back-door flag on the OBD. Ah well.
210 struct obd_device *osc_obd;
211 osc_obd = class_exp2obd(lov->tgts[i].ltd_exp);
213 osc_obd->obd_no_recov = 1;
216 obd_register_observer(lov->tgts[i].ltd_exp->exp_obd, NULL);
218 rc = obd_disconnect(lov->tgts[i].ltd_exp, flags);
220 if (lov->tgts[i].active) {
221 CERROR("Target %s disconnect error %d\n",
222 lov->tgts[i].uuid.uuid, rc);
226 if (lov->tgts[i].active) {
227 lov->desc.ld_active_tgt_count--;
228 lov->tgts[i].active = 0;
230 lov->tgts[i].ltd_exp = NULL;
234 rc = class_disconnect(exp, 0);
240 * -EINVAL : UUID can't be found in the LOV's target list
241 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
242 * -EBADF : The UUID is found, but the OBD is the wrong type (!)
244 static int lov_set_osc_active(struct lov_obd *lov, struct obd_uuid *uuid,
247 struct obd_device *obd;
248 struct lov_tgt_desc *tgt;
252 CDEBUG(D_INFO, "Searching in lov %p for uuid %s (activate=%d)\n",
253 lov, uuid->uuid, activate);
255 spin_lock(&lov->lov_lock);
256 for (i = 0, tgt = lov->tgts; i < lov->desc.ld_tgt_count; i++, tgt++) {
257 CDEBUG(D_INFO, "lov idx %d is %s conn "LPX64"\n",
258 i, tgt->uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
259 if (strncmp(uuid->uuid, tgt->uuid.uuid, sizeof uuid->uuid) == 0)
263 if (i == lov->desc.ld_tgt_count)
264 GOTO(out, rc = -EINVAL);
266 obd = class_exp2obd(tgt->ltd_exp);
268 /* This can happen if OST failure races with node shutdown */
269 GOTO(out, rc = -ENOTCONN);
272 CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LOV idx %d\n",
273 obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
274 obd->obd_type->typ_name, i);
275 LASSERT(strcmp(obd->obd_type->typ_name, "osc") == 0);
277 if (tgt->active == activate) {
278 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
279 activate ? "" : "in");
283 CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd, activate ? "" : "in");
285 tgt->active = activate;
287 lov->desc.ld_active_tgt_count++;
289 lov->desc.ld_active_tgt_count--;
293 spin_unlock(&lov->lov_lock);
297 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
301 struct obd_uuid *uuid;
303 if (strcmp(watched->obd_type->typ_name, "osc")) {
304 CERROR("unexpected notification of %s %s!\n",
305 watched->obd_type->typ_name,
309 uuid = &watched->u.cli.cl_import->imp_target_uuid;
312 * Must notify (MDS) before we mark the OSC as active, so that
313 * the orphan deletion happens without interference from racing
316 if (obd->obd_observer) {
317 /* Pass the notification up the chain. */
318 rc = obd_notify(obd->obd_observer, watched, active);
323 rc = lov_set_osc_active(&obd->u.lov, uuid, active);
326 CERROR("%sactivation of %s failed: %d\n",
327 active ? "" : "de", uuid->uuid, rc);
332 static int lov_setup(struct obd_device *obd, obd_count len, void *buf)
334 struct lustre_cfg *lcfg = buf;
335 struct lov_desc *desc;
336 struct lov_obd *lov = &obd->u.lov;
337 struct obd_uuid *uuids;
338 struct lov_tgt_desc *tgts;
344 if (lcfg->lcfg_inllen1 < 1) {
345 CERROR("LOV setup requires a descriptor\n");
349 if (lcfg->lcfg_inllen2 < 1) {
350 CERROR("LOV setup requires an OST UUID list\n");
354 desc = (struct lov_desc *)lcfg->lcfg_inlbuf1;
355 if (sizeof(*desc) > lcfg->lcfg_inllen1) {
356 CERROR("descriptor size wrong: %d > %d\n",
357 (int)sizeof(*desc), lcfg->lcfg_inllen1);
361 count = desc->ld_tgt_count;
362 uuids = (struct obd_uuid *)lcfg->lcfg_inlbuf2;
363 if (sizeof(*uuids) * count != lcfg->lcfg_inllen2) {
364 CERROR("UUID array size wrong: %u * %u != %u\n",
365 sizeof(*uuids), count, lcfg->lcfg_inllen2);
369 /* Because of 64-bit divide/mod operations only work with a 32-bit
370 * divisor in a 32-bit kernel, we cannot support a stripe width
371 * of 4GB or larger on 32-bit CPUs.
373 if ((desc->ld_default_stripe_count ?
374 desc->ld_default_stripe_count : desc->ld_tgt_count) *
375 desc->ld_default_stripe_size > ~0UL) {
376 CERROR("LOV: stripe width "LPU64"x%u > %lu on 32-bit system\n",
377 desc->ld_default_stripe_size,
378 desc->ld_default_stripe_count ?
379 desc->ld_default_stripe_count : desc->ld_tgt_count,~0UL);
383 lov->bufsize = sizeof(struct lov_tgt_desc) * count;
384 OBD_ALLOC(lov->tgts, lov->bufsize);
385 if (lov->tgts == NULL) {
386 CERROR("Out of memory\n");
391 spin_lock_init(&lov->lov_lock);
393 for (i = 0, tgts = lov->tgts; i < desc->ld_tgt_count; i++, tgts++) {
394 struct obd_uuid *uuid = &tgts->uuid;
396 /* NULL termination already checked */
404 static int lov_cleanup(struct obd_device *obd, int flags)
406 struct lov_obd *lov = &obd->u.lov;
408 OBD_FREE(lov->tgts, lov->bufsize);
413 /* compute object size given "stripeno" and the ost size */
414 static obd_size lov_stripe_size(struct lov_stripe_md *lsm, obd_size ost_size,
417 unsigned long ssize = lsm->lsm_stripe_size;
418 unsigned long swidth = ssize * lsm->lsm_stripe_count;
419 unsigned long stripe_size;
425 /* do_div(a, b) returns a % b, and a = a / b */
426 stripe_size = do_div(ost_size, ssize);
429 lov_size = ost_size * swidth + stripeno * ssize + stripe_size;
431 lov_size = (ost_size - 1) * swidth + (stripeno + 1) * ssize;
436 static void lov_merge_attrs(struct obdo *tgt, struct obdo *src, obd_flag valid,
437 struct lov_stripe_md *lsm, int stripeno, int *set)
439 valid &= src->o_valid;
442 if (valid & OBD_MD_FLSIZE) {
443 /* this handles sparse files properly */
446 lov_size = lov_stripe_size(lsm, src->o_size, stripeno);
447 if (lov_size > tgt->o_size)
448 tgt->o_size = lov_size;
450 if (valid & OBD_MD_FLBLOCKS)
451 tgt->o_blocks += src->o_blocks;
452 if (valid & OBD_MD_FLBLKSZ)
453 tgt->o_blksize += src->o_blksize;
454 if (valid & OBD_MD_FLCTIME && tgt->o_ctime < src->o_ctime)
455 tgt->o_ctime = src->o_ctime;
456 if (valid & OBD_MD_FLMTIME && tgt->o_mtime < src->o_mtime)
457 tgt->o_mtime = src->o_mtime;
459 memcpy(tgt, src, sizeof(*tgt));
460 tgt->o_id = lsm->lsm_object_id;
461 if (valid & OBD_MD_FLSIZE)
462 tgt->o_size = lov_stripe_size(lsm,src->o_size,stripeno);
468 #define log2(n) ffz(~(n))
471 static int lov_clear_orphans(struct obd_export *export, struct obdo *src_oa,
472 struct lov_stripe_md **ea,
473 struct obd_trans_info *oti)
477 struct obd_uuid *ost_uuid = NULL;
481 LASSERT(src_oa->o_valid & OBD_MD_FLFLAGS &&
482 src_oa->o_flags == OBD_FL_DELORPHAN);
484 lov = &export->exp_obd->u.lov;
486 tmp_oa = obdo_alloc();
490 if (src_oa->o_valid & OBD_MD_FLINLINE) {
491 ost_uuid = (struct obd_uuid *)src_oa->o_inline;
492 CDEBUG(D_HA, "clearing orphans only for %s\n",
496 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
497 struct lov_stripe_md obj_md;
498 struct lov_stripe_md *obj_mdp = &obj_md;
501 /* if called for a specific target, we don't
502 care if it is not active. */
503 if (lov->tgts[i].active == 0 && ost_uuid == NULL) {
504 CDEBUG(D_HA, "lov idx %d inactive\n", i);
508 if (ost_uuid && !obd_uuid_equals(ost_uuid, &lov->tgts[i].uuid))
511 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
513 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
514 err = obd_create(lov->tgts[i].ltd_exp, tmp_oa, &obj_mdp, oti);
516 CERROR("error in orphan recovery on OST idx %d/%d: "
517 "rc = %d\n", i, lov->desc.ld_tgt_count, err);
529 #define LOV_CREATE_RESEED_INTERVAL 1000
531 /* the LOV expects oa->o_id to be set to the LOV object id */
532 static int lov_create(struct obd_export *exp, struct obdo *src_oa,
533 struct lov_stripe_md **ea, struct obd_trans_info *oti)
535 static int ost_start_idx, ost_start_count;
537 struct lov_stripe_md *lsm;
538 struct lov_oinfo *loi = NULL;
539 struct obdo *tmp_oa, *ret_oa;
540 struct llog_cookie *cookies = NULL;
541 unsigned ost_count, ost_idx;
542 int set = 0, obj_alloc = 0, cookie_sent = 0, rc = 0, i;
547 if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
548 src_oa->o_flags == OBD_FL_DELORPHAN) {
549 rc = lov_clear_orphans(exp, src_oa, ea, oti);
556 lov = &exp->exp_obd->u.lov;
558 if (!lov->desc.ld_active_tgt_count)
561 ret_oa = obdo_alloc();
565 tmp_oa = obdo_alloc();
567 GOTO(out_oa, rc = -ENOMEM);
572 ost_count = lov_get_stripecnt(lov, 0);
574 /* If the MDS file was truncated up to some size, stripe over
575 * enough OSTs to allow the file to be created at that size. */
576 if (src_oa->o_valid & OBD_MD_FLSIZE) {
577 stripes=((src_oa->o_size+LUSTRE_STRIPE_MAXBYTES)>>12)-1;
578 do_div(stripes, (__u32)(LUSTRE_STRIPE_MAXBYTES >> 12));
580 if (stripes > lov->desc.ld_active_tgt_count)
582 if (stripes > ost_count)
588 rc = lov_alloc_memmd(&lsm, stripes, lov->desc.ld_pattern ?
589 lov->desc.ld_pattern : LOV_PATTERN_RAID0);
596 ost_count = lov->desc.ld_tgt_count;
598 LASSERT(src_oa->o_valid & OBD_MD_FLID);
599 lsm->lsm_object_id = src_oa->o_id;
600 if (!lsm->lsm_stripe_size)
601 lsm->lsm_stripe_size = lov->desc.ld_default_stripe_size;
602 if (!lsm->lsm_pattern) {
603 lsm->lsm_pattern = lov->desc.ld_pattern ?
604 lov->desc.ld_pattern : LOV_PATTERN_RAID0;
607 if (*ea == NULL || lsm->lsm_oinfo[0].loi_ost_idx >= ost_count) {
608 if (ost_start_count <= 0) {
609 ost_start_idx = ll_insecure_random_int();
610 ost_start_count = LOV_CREATE_RESEED_INTERVAL;
613 ost_start_idx += lsm->lsm_stripe_count;
615 ost_idx = ost_start_idx % ost_count;
617 ost_idx = lsm->lsm_oinfo[0].loi_ost_idx;
620 CDEBUG(D_INODE, "allocating %d subobjs for objid "LPX64" at idx %d\n",
621 lsm->lsm_stripe_count, lsm->lsm_object_id, ost_idx);
623 /* XXX LOV STACKING: need to figure out how many real OSCs */
624 if (oti && (src_oa->o_valid & OBD_MD_FLCOOKIE)) {
625 oti_alloc_cookies(oti, lsm->lsm_stripe_count);
626 if (!oti->oti_logcookies)
627 GOTO(out_cleanup, rc = -ENOMEM);
628 cookies = oti->oti_logcookies;
631 loi = lsm->lsm_oinfo;
632 for (i = 0; i < ost_count; i++, ost_idx = (ost_idx + 1) % ost_count) {
633 struct lov_stripe_md obj_md;
634 struct lov_stripe_md *obj_mdp = &obj_md;
637 if (lov->tgts[ost_idx].active == 0) {
638 CDEBUG(D_HA, "lov idx %d inactive\n", ost_idx);
642 /* create data objects with "parent" OA */
643 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
645 /* XXX When we start creating objects on demand, we need to
646 * make sure that we always create the object on the
647 * stripe which holds the existing file size.
649 if (src_oa->o_valid & OBD_MD_FLSIZE) {
650 if (lov_stripe_offset(lsm, src_oa->o_size, i,
651 &tmp_oa->o_size) < 0 &&
655 CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
656 i, tmp_oa->o_size, src_oa->o_size);
660 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
661 err = obd_create(lov->tgts[ost_idx].ltd_exp, tmp_oa, &obj_mdp,
664 if (lov->tgts[ost_idx].active) {
665 CERROR("error creating objid "LPX64" sub-object"
666 " on OST idx %d/%d: rc = %d\n",
667 src_oa->o_id, ost_idx,
668 lsm->lsm_stripe_count, err);
670 CERROR("obd_create returned invalid "
680 oti->oti_objid[ost_idx] = tmp_oa->o_id;
681 loi->loi_id = tmp_oa->o_id;
682 loi->loi_ost_idx = ost_idx;
683 CDEBUG(D_INODE, "objid "LPX64" has subobj "LPX64" at idx %d\n",
684 lsm->lsm_object_id, loi->loi_id, ost_idx);
686 lov_merge_attrs(ret_oa, tmp_oa, tmp_oa->o_valid, lsm,
691 ++oti->oti_logcookies;
692 if (tmp_oa->o_valid & OBD_MD_FLCOOKIE)
697 /* If we have allocated enough objects, we are OK */
698 if (obj_alloc == lsm->lsm_stripe_count)
699 GOTO(out_done, rc = 0);
702 if (obj_alloc == 0) {
705 GOTO(out_cleanup, rc);
708 /* If we were passed specific striping params, then a failure to
709 * meet those requirements is an error, since we can't reallocate
710 * that memory (it might be part of a larger array or something).
712 * We can only get here if lsm_stripe_count was originally > 1.
715 CERROR("can't lstripe objid "LPX64": have %u want %u, rc %d\n",
716 lsm->lsm_object_id, obj_alloc, lsm->lsm_stripe_count,rc);
719 GOTO(out_cleanup, rc);
721 struct lov_stripe_md *lsm_new;
722 /* XXX LOV STACKING call into osc for sizes */
723 unsigned oldsize, newsize;
725 if (oti && cookies && cookie_sent) {
726 oldsize = lsm->lsm_stripe_count * sizeof(*cookies);
727 newsize = obj_alloc * sizeof(*cookies);
729 oti_alloc_cookies(oti, obj_alloc);
730 if (oti->oti_logcookies) {
731 memcpy(oti->oti_logcookies, cookies, newsize);
732 OBD_FREE(cookies, oldsize);
733 cookies = oti->oti_logcookies;
735 CWARN("'leaking' %d bytes\n", oldsize-newsize);
739 CERROR("reallocating LSM for objid "LPX64": old %u new %u\n",
740 lsm->lsm_object_id, lsm->lsm_stripe_count, obj_alloc);
741 oldsize = lov_stripe_md_size(lsm->lsm_stripe_count);
742 newsize = lov_stripe_md_size(obj_alloc);
743 OBD_ALLOC(lsm_new, newsize);
744 if (lsm_new != NULL) {
745 memcpy(lsm_new, lsm, newsize);
746 lsm_new->lsm_stripe_count = obj_alloc;
747 OBD_FREE(lsm, newsize);
750 CWARN("'leaking' %d bytes\n", oldsize - newsize);
757 if (src_oa->o_valid & OBD_MD_FLSIZE &&
758 ret_oa->o_size != src_oa->o_size) {
759 CERROR("original size "LPU64" isn't new object size "LPU64"\n",
760 src_oa->o_size, ret_oa->o_size);
763 ret_oa->o_id = src_oa->o_id;
764 memcpy(src_oa, ret_oa, sizeof(*src_oa));
770 if (oti && cookies) {
771 oti->oti_logcookies = cookies;
773 oti_free_cookies(oti);
774 src_oa->o_valid &= ~OBD_MD_FLCOOKIE;
776 src_oa->o_valid |= OBD_MD_FLCOOKIE;
782 while (obj_alloc-- > 0) {
783 struct obd_export *sub_exp;
787 sub_exp = lov->tgts[loi->loi_ost_idx].ltd_exp;
788 /* destroy already created objects here */
789 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
790 tmp_oa->o_id = loi->loi_id;
792 err = obd_destroy(sub_exp, tmp_oa, NULL, oti);
794 CERROR("Failed to uncreate objid "LPX64" subobj "LPX64
795 " on OST idx %d: rc = %d\n", src_oa->o_id,
796 loi->loi_id, loi->loi_ost_idx, err);
799 obd_free_memmd(exp, &lsm);
803 #define lsm_bad_magic(LSMP) \
805 struct lov_stripe_md *_lsm__ = (LSMP); \
808 CERROR("LOV requires striping ea\n"); \
810 } else if (_lsm__->lsm_magic != LOV_MAGIC) { \
811 CERROR("LOV striping magic bad %#x != %#x\n", \
812 _lsm__->lsm_magic, LOV_MAGIC); \
818 static int lov_destroy(struct obd_export *exp, struct obdo *oa,
819 struct lov_stripe_md *lsm, struct obd_trans_info *oti)
823 struct lov_oinfo *loi;
827 if (lsm_bad_magic(lsm))
830 if (!exp || !exp->exp_obd)
833 lov = &exp->exp_obd->u.lov;
834 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
836 if (lov->tgts[loi->loi_ost_idx].active == 0) {
837 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
838 /* Orphan clean up will (someday) fix this up. */
842 memcpy(&tmp, oa, sizeof(tmp));
843 tmp.o_id = loi->loi_id;
844 err = obd_destroy(lov->tgts[loi->loi_ost_idx].ltd_exp, &tmp,
846 if (err && lov->tgts[loi->loi_ost_idx].active) {
847 CERROR("error: destroying objid "LPX64" subobj "
848 LPX64" on OST idx %d: rc = %d\n",
849 oa->o_id, loi->loi_id, loi->loi_ost_idx, err);
857 static int lov_getattr(struct obd_export *exp, struct obdo *oa,
858 struct lov_stripe_md *lsm)
862 struct lov_oinfo *loi;
863 int i, rc = 0, set = 0;
866 if (lsm_bad_magic(lsm))
869 if (!exp || !exp->exp_obd)
872 lov = &exp->exp_obd->u.lov;
874 CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
875 lsm->lsm_object_id, lsm->lsm_stripe_count, lsm->lsm_stripe_size);
876 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
879 if (lov->tgts[loi->loi_ost_idx].active == 0) {
880 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
884 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
885 "%u\n", oa->o_id, i, loi->loi_id, loi->loi_ost_idx);
886 /* create data objects with "parent" OA */
887 memcpy(&tmp, oa, sizeof(tmp));
888 tmp.o_id = loi->loi_id;
890 err = obd_getattr(lov->tgts[loi->loi_ost_idx].ltd_exp, &tmp,
893 if (lov->tgts[loi->loi_ost_idx].active) {
894 CERROR("error: getattr objid "LPX64" subobj "
895 LPX64" on OST idx %d: rc = %d\n",
896 oa->o_id, loi->loi_id, loi->loi_ost_idx,
901 lov_merge_attrs(oa, &tmp, tmp.o_valid, lsm, i, &set);
909 static int lov_getattr_interpret(struct ptlrpc_request_set *rqset, void *data,
912 struct lov_getattr_async_args *aa = data;
913 struct lov_stripe_md *lsm = aa->aa_lsm;
914 struct obdo *oa = aa->aa_oa;
915 struct obdo *obdos = aa->aa_obdos;
916 struct lov_oinfo *loi;
922 /* NB all stripe requests succeeded to get here */
924 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
926 if (obdos[i].o_valid == 0) /* inactive stripe */
929 lov_merge_attrs(oa, &obdos[i], obdos[i].o_valid, lsm,
934 CERROR ("No stripes had valid attrs\n");
939 OBD_FREE (obdos, lsm->lsm_stripe_count * sizeof (*obdos));
943 static int lov_getattr_async(struct obd_export *exp, struct obdo *oa,
944 struct lov_stripe_md *lsm,
945 struct ptlrpc_request_set *rqset)
949 struct lov_oinfo *loi;
950 struct lov_getattr_async_args *aa;
951 int i, rc = 0, set = 0;
954 if (lsm_bad_magic(lsm))
957 if (!exp || !exp->exp_obd)
960 lov = &exp->exp_obd->u.lov;
962 OBD_ALLOC (obdos, lsm->lsm_stripe_count * sizeof (*obdos));
966 CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
967 lsm->lsm_object_id, lsm->lsm_stripe_count, lsm->lsm_stripe_size);
968 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
971 if (lov->tgts[loi->loi_ost_idx].active == 0) {
972 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
973 /* leaves obdos[i].obd_valid unset */
977 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
978 "%u\n", oa->o_id, i, loi->loi_id, loi->loi_ost_idx);
979 /* create data objects with "parent" OA */
980 memcpy(&obdos[i], oa, sizeof(obdos[i]));
981 obdos[i].o_id = loi->loi_id;
983 err = obd_getattr_async(lov->tgts[loi->loi_ost_idx].ltd_exp,
984 &obdos[i], NULL, rqset);
986 CERROR("error: getattr objid "LPX64" subobj "
987 LPX64" on OST idx %d: rc = %d\n",
988 oa->o_id, loi->loi_id, loi->loi_ost_idx,
990 GOTO(out_obdos, rc = err);
995 GOTO (out_obdos, rc = -EIO);
997 LASSERT (rqset->set_interpret == NULL);
998 rqset->set_interpret = lov_getattr_interpret;
999 LASSERT (sizeof (rqset->set_args) >= sizeof (*aa));
1000 aa = (struct lov_getattr_async_args *)&rqset->set_args;
1003 aa->aa_obdos = obdos;
1008 OBD_FREE (obdos, lsm->lsm_stripe_count * sizeof (*obdos));
1014 static int lov_setattr(struct obd_export *exp, struct obdo *src_oa,
1015 struct lov_stripe_md *lsm, struct obd_trans_info *oti)
1017 struct obdo *tmp_oa, *ret_oa;
1018 struct lov_obd *lov;
1019 struct lov_oinfo *loi;
1020 int rc = 0, i, set = 0;
1023 if (lsm_bad_magic(lsm))
1026 if (!exp || !exp->exp_obd)
1029 /* for now, we only expect time updates here */
1030 LASSERT(!(src_oa->o_valid & ~(OBD_MD_FLID|OBD_MD_FLTYPE | OBD_MD_FLMODE|
1031 OBD_MD_FLATIME | OBD_MD_FLMTIME |
1032 OBD_MD_FLCTIME | OBD_MD_FLFLAGS |
1034 ret_oa = obdo_alloc();
1038 tmp_oa = obdo_alloc();
1040 GOTO(out_oa, rc = -ENOMEM);
1042 lov = &exp->exp_obd->u.lov;
1043 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1046 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1047 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1051 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1052 tmp_oa->o_id = loi->loi_id;
1054 if (src_oa->o_valid & OBD_MD_FLSIZE) {
1055 if (lov_stripe_offset(lsm, src_oa->o_size, i,
1056 &tmp_oa->o_size) < 0 &&
1060 CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
1061 i, tmp_oa->o_size, src_oa->o_size);
1064 err = obd_setattr(lov->tgts[loi->loi_ost_idx].ltd_exp, tmp_oa,
1067 if (lov->tgts[loi->loi_ost_idx].active) {
1068 CERROR("error: setattr objid "LPX64" subobj "
1069 LPX64" on OST idx %d: rc = %d\n",
1070 src_oa->o_id, loi->loi_id,
1071 loi->loi_ost_idx, err);
1077 lov_merge_attrs(ret_oa, tmp_oa, tmp_oa->o_valid, lsm, i, &set);
1082 ret_oa->o_id = src_oa->o_id;
1083 memcpy(src_oa, ret_oa, sizeof(*src_oa));
1092 /* we have an offset in file backed by an lov and want to find out where
1093 * that offset lands in our given stripe of the file. for the easy
1094 * case where the offset is within the stripe, we just have to scale the
1095 * offset down to make it relative to the stripe instead of the lov.
1097 * the harder case is what to do when the offset doesn't intersect the
1098 * stripe. callers will want start offsets clamped ahead to the start
1099 * of the nearest stripe in the file. end offsets similarly clamped to the
1100 * nearest ending byte of a stripe in the file:
1102 * all this function does is move offsets to the nearest region of the
1103 * stripe, and it does its work "mod" the full length of all the stripes.
1104 * consider a file with 3 stripes:
1107 * ---------------------------------------------------------------------
1108 * | 0 | 1 | 2 | 0 | 1 | 2 |
1109 * ---------------------------------------------------------------------
1111 * to find stripe 1's offsets for S and E, it divides by the full stripe
1112 * width and does its math in the context of a single set of stripes:
1115 * -----------------------------------
1117 * -----------------------------------
1119 * it'll notice that E is outside stripe 1 and clamp it to the end of the
1120 * stripe, then multiply it back out by lov_off to give the real offsets in
1124 * ---------------------------------------------------------------------
1125 * | 1 | 1 | 1 | 1 | 1 | 1 |
1126 * ---------------------------------------------------------------------
1128 * it would have done similarly and pulled S forward to the start of a 1
1129 * stripe if, say, S had landed in a 0 stripe.
1131 * this rounding isn't always correct. consider an E lov offset that lands
1132 * on a 0 stripe, the "mod stripe width" math will pull it forward to the
1133 * start of a 1 stripe, when in fact it wanted to be rounded back to the end
1134 * of a previous 1 stripe. this logic is handled by callers and this is why:
1136 * this function returns < 0 when the offset was "before" the stripe and
1137 * was moved forward to the start of the stripe in question; 0 when it
1138 * falls in the stripe and no shifting was done; > 0 when the offset
1139 * was outside the stripe and was pulled back to its final byte. */
1140 static int lov_stripe_offset(struct lov_stripe_md *lsm, obd_off lov_off,
1141 int stripeno, obd_off *obd_off)
1143 unsigned long ssize = lsm->lsm_stripe_size;
1144 unsigned long swidth = ssize * lsm->lsm_stripe_count;
1145 unsigned long stripe_off, this_stripe;
1148 if (lov_off == OBD_OBJECT_EOF) {
1149 *obd_off = OBD_OBJECT_EOF;
1153 /* do_div(a, b) returns a % b, and a = a / b */
1154 stripe_off = do_div(lov_off, swidth);
1156 this_stripe = stripeno * ssize;
1157 if (stripe_off < this_stripe) {
1161 stripe_off -= this_stripe;
1163 if (stripe_off >= ssize) {
1169 *obd_off = lov_off * ssize + stripe_off;
1173 /* given an extent in an lov and a stripe, calculate the extent of the stripe
1174 * that is contained within the lov extent. this returns true if the given
1175 * stripe does intersect with the lov extent. */
1176 static int lov_stripe_intersects(struct lov_stripe_md *lsm, int stripeno,
1177 obd_off start, obd_off end,
1178 obd_off *obd_start, obd_off *obd_end)
1180 int start_side, end_side;
1182 start_side = lov_stripe_offset(lsm, start, stripeno, obd_start);
1183 end_side = lov_stripe_offset(lsm, end, stripeno, obd_end);
1185 CDEBUG(D_INODE, "["LPU64"->"LPU64"] -> [(%d) "LPU64"->"LPU64" (%d)]\n",
1186 start, end, start_side, *obd_start, *obd_end, end_side);
1188 /* this stripe doesn't intersect the file extent when neither
1189 * start or the end intersected the stripe and obd_start and
1190 * obd_end got rounded up to the save value. */
1191 if (start_side != 0 && end_side != 0 && *obd_start == *obd_end)
1194 /* as mentioned in the lov_stripe_offset commentary, end
1195 * might have been shifted in the wrong direction. This
1196 * happens when an end offset is before the stripe when viewed
1197 * through the "mod stripe size" math. we detect it being shifted
1198 * in the wrong direction and touch it up.
1199 * interestingly, this can't underflow since end must be > start
1200 * if we passed through the previous check.
1201 * (should we assert for that somewhere?) */
1208 /* compute which stripe number "lov_off" will be written into */
1209 static int lov_stripe_number(struct lov_stripe_md *lsm, obd_off lov_off)
1211 unsigned long ssize = lsm->lsm_stripe_size;
1212 unsigned long swidth = ssize * lsm->lsm_stripe_count;
1213 unsigned long stripe_off;
1215 stripe_off = do_div(lov_off, swidth);
1217 return stripe_off / ssize;
1220 /* FIXME: maybe we'll just make one node the authoritative attribute node, then
1221 * we can send this 'punch' to just the authoritative node and the nodes
1222 * that the punch will affect. */
1223 static int lov_punch(struct obd_export *exp, struct obdo *oa,
1224 struct lov_stripe_md *lsm,
1225 obd_off start, obd_off end, struct obd_trans_info *oti)
1228 struct lov_obd *lov;
1229 struct lov_oinfo *loi;
1233 if (lsm_bad_magic(lsm))
1236 if (!exp || !exp->exp_obd)
1239 lov = &exp->exp_obd->u.lov;
1240 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1241 obd_off starti, endi;
1244 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1245 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1249 if (!lov_stripe_intersects(lsm, i, start, end, &starti, &endi))
1252 /* create data objects with "parent" OA */
1253 memcpy(&tmp, oa, sizeof(tmp));
1254 tmp.o_id = loi->loi_id;
1256 err = obd_punch(lov->tgts[loi->loi_ost_idx].ltd_exp, &tmp, NULL,
1257 starti, endi, NULL);
1259 if (lov->tgts[loi->loi_ost_idx].active) {
1260 CERROR("error: punch objid "LPX64" subobj "LPX64
1261 " on OST idx %d: rc = %d\n", oa->o_id,
1262 loi->loi_id, loi->loi_ost_idx, err);
1271 static int lov_sync(struct obd_export *exp, struct obdo *oa,
1272 struct lov_stripe_md *lsm, obd_off start, obd_off end)
1275 struct lov_obd *lov;
1276 struct lov_oinfo *loi;
1280 if (lsm_bad_magic(lsm))
1290 lov = &exp->exp_obd->u.lov;
1291 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1292 obd_off starti, endi;
1295 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1296 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1300 if (!lov_stripe_intersects(lsm, i, start, end, &starti, &endi))
1303 memcpy(tmp, oa, sizeof(*tmp));
1304 tmp->o_id = loi->loi_id;
1306 err = obd_sync(lov->tgts[loi->loi_ost_idx].ltd_exp, tmp, NULL,
1309 if (lov->tgts[loi->loi_ost_idx].active) {
1310 CERROR("error: fsync objid "LPX64" subobj "LPX64
1311 " on OST idx %d: rc = %d\n", oa->o_id,
1312 loi->loi_id, loi->loi_ost_idx, err);
1323 static int lov_brw_check(struct lov_obd *lov, struct lov_stripe_md *lsm,
1324 obd_count oa_bufs, struct brw_page *pga)
1328 /* The caller just wants to know if there's a chance that this
1329 * I/O can succeed */
1330 for (i = 0; i < oa_bufs; i++) {
1331 int stripe = lov_stripe_number(lsm, pga[i].off);
1332 int ost = lsm->lsm_oinfo[stripe].loi_ost_idx;
1333 struct ldlm_extent ext, subext;
1334 ext.start = pga[i].off;
1335 ext.start = pga[i].off + pga[i].count;
1337 if (!lov_stripe_intersects(lsm, i, ext.start, ext.end,
1338 &subext.start, &subext.end))
1341 if (lov->tgts[ost].active == 0) {
1342 CDEBUG(D_HA, "lov idx %d inactive\n", ost);
1349 static int lov_brw(int cmd, struct obd_export *exp, struct obdo *src_oa,
1350 struct lov_stripe_md *lsm, obd_count oa_bufs,
1351 struct brw_page *pga, struct obd_trans_info *oti)
1357 struct lov_stripe_md lsm;
1359 } *stripeinfo, *si, *si_last;
1360 struct obdo *ret_oa = NULL, *tmp_oa = NULL;
1361 struct lov_obd *lov;
1362 struct brw_page *ioarr;
1363 struct lov_oinfo *loi;
1364 int rc = 0, i, *where, stripe_count = lsm->lsm_stripe_count, set = 0;
1367 if (lsm_bad_magic(lsm))
1370 lov = &exp->exp_obd->u.lov;
1372 if (cmd == OBD_BRW_CHECK) {
1373 rc = lov_brw_check(lov, lsm, oa_bufs, pga);
1377 OBD_ALLOC(stripeinfo, stripe_count * sizeof(*stripeinfo));
1381 OBD_ALLOC(where, sizeof(*where) * oa_bufs);
1383 GOTO(out_sinfo, rc = -ENOMEM);
1385 OBD_ALLOC(ioarr, sizeof(*ioarr) * oa_bufs);
1387 GOTO(out_where, rc = -ENOMEM);
1390 ret_oa = obdo_alloc();
1392 GOTO(out_ioarr, rc = -ENOMEM);
1394 tmp_oa = obdo_alloc();
1396 GOTO(out_oa, rc = -ENOMEM);
1399 for (i = 0; i < oa_bufs; i++) {
1400 where[i] = lov_stripe_number(lsm, pga[i].off);
1401 stripeinfo[where[i]].bufct++;
1404 for (i = 0, loi = lsm->lsm_oinfo, si_last = si = stripeinfo;
1405 i < stripe_count; i++, loi++, si_last = si, si++) {
1407 si->index = si_last->index + si_last->bufct;
1408 si->lsm.lsm_object_id = loi->loi_id;
1409 si->ost_idx = loi->loi_ost_idx;
1412 for (i = 0; i < oa_bufs; i++) {
1413 int which = where[i];
1416 shift = stripeinfo[which].index + stripeinfo[which].subcount;
1417 LASSERT(shift < oa_bufs);
1418 ioarr[shift] = pga[i];
1419 lov_stripe_offset(lsm, pga[i].off, which, &ioarr[shift].off);
1420 stripeinfo[which].subcount++;
1423 for (i = 0, si = stripeinfo; i < stripe_count; i++, si++) {
1424 int shift = si->index;
1426 if (lov->tgts[si->ost_idx].active == 0) {
1427 CDEBUG(D_HA, "lov idx %d inactive\n", si->ost_idx);
1428 GOTO(out_oa, rc = -EIO);
1432 LASSERT(shift < oa_bufs);
1434 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1436 tmp_oa->o_id = si->lsm.lsm_object_id;
1437 rc = obd_brw(cmd, lov->tgts[si->ost_idx].ltd_exp,
1438 tmp_oa, &si->lsm, si->bufct,
1439 &ioarr[shift], oti);
1441 GOTO(out_ioarr, rc);
1443 lov_merge_attrs(ret_oa, tmp_oa, tmp_oa->o_valid, lsm,
1448 ret_oa->o_id = src_oa->o_id;
1449 memcpy(src_oa, ret_oa, sizeof(*src_oa));
1458 OBD_FREE(ioarr, sizeof(*ioarr) * oa_bufs);
1460 OBD_FREE(where, sizeof(*where) * oa_bufs);
1462 OBD_FREE(stripeinfo, stripe_count * sizeof(*stripeinfo));
1466 static int lov_brw_interpret(struct ptlrpc_request_set *reqset, void *data,
1469 struct lov_brw_async_args *aa = data;
1470 struct lov_stripe_md *lsm = aa->aa_lsm;
1471 obd_count oa_bufs = aa->aa_oa_bufs;
1472 struct obdo *oa = aa->aa_oa;
1473 struct obdo *obdos = aa->aa_obdos;
1474 struct brw_page *ioarr = aa->aa_ioarr;
1475 struct lov_oinfo *loi;
1480 /* NB all stripe requests succeeded to get here */
1482 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
1484 if (obdos[i].o_valid == 0) /* inactive stripe */
1487 lov_merge_attrs(oa, &obdos[i], obdos[i].o_valid, lsm,
1492 CERROR("No stripes had valid attrs\n");
1496 oa->o_id = lsm->lsm_object_id;
1498 OBD_FREE(obdos, lsm->lsm_stripe_count * sizeof(*obdos));
1499 OBD_FREE(ioarr, sizeof(*ioarr) * oa_bufs);
1503 static int lov_brw_async(int cmd, struct obd_export *exp, struct obdo *oa,
1504 struct lov_stripe_md *lsm, obd_count oa_bufs,
1505 struct brw_page *pga, struct ptlrpc_request_set *set,
1506 struct obd_trans_info *oti)
1512 struct lov_stripe_md lsm;
1514 } *stripeinfo, *si, *si_last;
1515 struct lov_obd *lov;
1516 struct brw_page *ioarr;
1517 struct obdo *obdos = NULL;
1518 struct lov_oinfo *loi;
1519 struct lov_brw_async_args *aa;
1520 int rc = 0, i, *where, stripe_count = lsm->lsm_stripe_count;
1523 if (lsm_bad_magic(lsm))
1526 lov = &exp->exp_obd->u.lov;
1528 if (cmd == OBD_BRW_CHECK) {
1529 rc = lov_brw_check(lov, lsm, oa_bufs, pga);
1533 OBD_ALLOC(stripeinfo, stripe_count * sizeof(*stripeinfo));
1537 OBD_ALLOC(where, sizeof(*where) * oa_bufs);
1539 GOTO(out_sinfo, rc = -ENOMEM);
1542 OBD_ALLOC(obdos, sizeof(*obdos) * stripe_count);
1544 GOTO(out_where, rc = -ENOMEM);
1547 OBD_ALLOC(ioarr, sizeof(*ioarr) * oa_bufs);
1549 GOTO(out_obdos, rc = -ENOMEM);
1551 for (i = 0; i < oa_bufs; i++) {
1552 where[i] = lov_stripe_number(lsm, pga[i].off);
1553 stripeinfo[where[i]].bufct++;
1556 for (i = 0, loi = lsm->lsm_oinfo, si_last = si = stripeinfo;
1557 i < stripe_count; i++, loi++, si_last = si, si++) {
1559 si->index = si_last->index + si_last->bufct;
1560 si->lsm.lsm_object_id = loi->loi_id;
1561 si->ost_idx = loi->loi_ost_idx;
1564 memcpy(&obdos[i], oa, sizeof(*obdos));
1565 obdos[i].o_id = si->lsm.lsm_object_id;
1569 for (i = 0; i < oa_bufs; i++) {
1570 int which = where[i];
1573 shift = stripeinfo[which].index + stripeinfo[which].subcount;
1574 LASSERT(shift < oa_bufs);
1575 ioarr[shift] = pga[i];
1576 lov_stripe_offset(lsm, pga[i].off, which, &ioarr[shift].off);
1577 stripeinfo[which].subcount++;
1580 for (i = 0, si = stripeinfo; i < stripe_count; i++, si++) {
1581 int shift = si->index;
1586 if (lov->tgts[si->ost_idx].active == 0) {
1587 CDEBUG(D_HA, "lov idx %d inactive\n", si->ost_idx);
1588 GOTO(out_ioarr, rc = -EIO);
1591 LASSERT(shift < oa_bufs);
1593 rc = obd_brw_async(cmd, lov->tgts[si->ost_idx].ltd_exp,
1594 &obdos[i], &si->lsm, si->bufct,
1595 &ioarr[shift], set, oti);
1597 GOTO(out_ioarr, rc);
1600 LASSERT(set->set_interpret == NULL);
1601 set->set_interpret = (set_interpreter_func)lov_brw_interpret;
1602 LASSERT(sizeof(set->set_args) >= sizeof(struct lov_brw_async_args));
1603 aa = (struct lov_brw_async_args *)&set->set_args;
1605 aa->aa_obdos = obdos;
1607 aa->aa_ioarr = ioarr;
1608 aa->aa_oa_bufs = oa_bufs;
1610 /* Don't free ioarr or obdos - that's done in lov_brw_interpret */
1611 GOTO(out_where, rc);
1614 OBD_FREE(ioarr, sizeof(*ioarr) * oa_bufs);
1616 OBD_FREE(obdos, stripe_count * sizeof(*obdos));
1618 OBD_FREE(where, sizeof(*where) * oa_bufs);
1620 OBD_FREE(stripeinfo, stripe_count * sizeof(*stripeinfo));
1624 struct lov_async_page *lap_from_cookie(void *cookie)
1626 struct lov_async_page *lap = cookie;
1627 if (lap->lap_magic != LAP_MAGIC)
1628 return ERR_PTR(-EINVAL);
1632 static int lov_ap_make_ready(void *data, int cmd)
1634 struct lov_async_page *lap = lap_from_cookie(data);
1635 /* XXX should these assert? */
1639 return lap->lap_caller_ops->ap_make_ready(lap->lap_caller_data, cmd);
1641 static int lov_ap_refresh_count(void *data, int cmd)
1643 struct lov_async_page *lap = lap_from_cookie(data);
1647 return lap->lap_caller_ops->ap_refresh_count(lap->lap_caller_data,
1650 static void lov_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
1652 struct lov_async_page *lap = lap_from_cookie(data);
1653 /* XXX should these assert? */
1657 lap->lap_caller_ops->ap_fill_obdo(lap->lap_caller_data, cmd, oa);
1658 /* XXX woah, shouldn't we be altering more here? size? */
1659 oa->o_id = lap->lap_loi_id;
1661 static void lov_ap_completion(void *data, int cmd, int rc)
1663 struct lov_async_page *lap = lap_from_cookie(data);
1667 /* in a raid1 regime this would down a count of many ios
1668 * in flight, onl calling the caller_ops completion when all
1669 * the raid1 ios are complete */
1670 lap->lap_caller_ops->ap_completion(lap->lap_caller_data, cmd, rc);
1673 static struct obd_async_page_ops lov_async_page_ops = {
1674 .ap_make_ready = lov_ap_make_ready,
1675 .ap_refresh_count = lov_ap_refresh_count,
1676 .ap_fill_obdo = lov_ap_fill_obdo,
1677 .ap_completion = lov_ap_completion,
1680 int lov_prep_async_page(struct obd_export *exp, struct lov_stripe_md *lsm,
1681 struct lov_oinfo *loi, struct page *page,
1682 obd_off offset, struct obd_async_page_ops *ops,
1683 void *data, void **res)
1685 struct lov_obd *lov = &exp->exp_obd->u.lov;
1686 struct lov_async_page *lap;
1690 if (lsm_bad_magic(lsm))
1692 LASSERT(loi == NULL);
1694 OBD_ALLOC(lap, sizeof(*lap));
1698 lap->lap_magic = LAP_MAGIC;
1699 lap->lap_caller_ops = ops;
1700 lap->lap_caller_data = data;
1702 /* for now only raid 0 which passes through */
1703 lap->lap_stripe = lov_stripe_number(lsm, offset);
1704 lov_stripe_offset(lsm, offset, lap->lap_stripe, &lap->lap_sub_offset);
1705 loi = &lsm->lsm_oinfo[lap->lap_stripe];
1707 /* so the callback doesn't need the lsm */
1708 lap->lap_loi_id = loi->loi_id;
1710 rc = obd_prep_async_page(lov->tgts[loi->loi_ost_idx].ltd_exp,
1711 lsm, loi, page, lap->lap_sub_offset,
1712 &lov_async_page_ops, lap,
1713 &lap->lap_sub_cookie);
1715 OBD_FREE(lap, sizeof(*lap));
1718 CDEBUG(D_CACHE, "lap %p page %p cookie %p off "LPU64"\n", lap, page,
1719 lap->lap_sub_cookie, offset);
1724 static int lov_queue_async_io(struct obd_export *exp,
1725 struct lov_stripe_md *lsm,
1726 struct lov_oinfo *loi, void *cookie,
1727 int cmd, obd_off off, int count,
1728 obd_flag brw_flags, obd_flag async_flags)
1730 struct lov_obd *lov = &exp->exp_obd->u.lov;
1731 struct lov_async_page *lap;
1734 LASSERT(loi == NULL);
1736 if (lsm_bad_magic(lsm))
1739 lap = lap_from_cookie(cookie);
1741 RETURN(PTR_ERR(lap));
1743 loi = &lsm->lsm_oinfo[lap->lap_stripe];
1744 rc = obd_queue_async_io(lov->tgts[loi->loi_ost_idx].ltd_exp, lsm,
1745 loi, lap->lap_sub_cookie, cmd, off, count,
1746 brw_flags, async_flags);
1750 static int lov_set_async_flags(struct obd_export *exp,
1751 struct lov_stripe_md *lsm,
1752 struct lov_oinfo *loi, void *cookie,
1753 obd_flag async_flags)
1755 struct lov_obd *lov = &exp->exp_obd->u.lov;
1756 struct lov_async_page *lap;
1759 LASSERT(loi == NULL);
1761 if (lsm_bad_magic(lsm))
1764 lap = lap_from_cookie(cookie);
1766 RETURN(PTR_ERR(lap));
1768 loi = &lsm->lsm_oinfo[lap->lap_stripe];
1769 rc = obd_set_async_flags(lov->tgts[loi->loi_ost_idx].ltd_exp,
1770 lsm, loi, lap->lap_sub_cookie, async_flags);
1774 static int lov_queue_sync_io(struct obd_export *exp,
1775 struct lov_stripe_md *lsm,
1776 struct lov_oinfo *loi,
1777 struct obd_sync_io_container *osic, void *cookie,
1778 int cmd, obd_off off, int count,
1781 struct lov_obd *lov = &exp->exp_obd->u.lov;
1782 struct lov_async_page *lap;
1785 LASSERT(loi == NULL);
1787 if (lsm_bad_magic(lsm))
1790 lap = lap_from_cookie(cookie);
1792 RETURN(PTR_ERR(lap));
1794 loi = &lsm->lsm_oinfo[lap->lap_stripe];
1795 rc = obd_queue_sync_io(lov->tgts[loi->loi_ost_idx].ltd_exp, lsm, loi,
1796 osic, lap->lap_sub_cookie, cmd, off, count,
1801 /* this isn't exactly optimal. we may have queued sync io in oscs on
1802 * all stripes, but we don't record that fact at queue time. so we
1803 * trigger sync io on all stripes. */
1804 static int lov_trigger_sync_io(struct obd_export *exp,
1805 struct lov_stripe_md *lsm,
1806 struct lov_oinfo *loi,
1807 struct obd_sync_io_container *osic)
1809 struct lov_obd *lov = &exp->exp_obd->u.lov;
1812 LASSERT(loi == NULL);
1814 if (lsm_bad_magic(lsm))
1817 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
1819 err = obd_trigger_sync_io(lov->tgts[loi->loi_ost_idx].ltd_exp,
1821 if (rc == 0 && err != 0)
1827 static int lov_teardown_async_page(struct obd_export *exp,
1828 struct lov_stripe_md *lsm,
1829 struct lov_oinfo *loi, void *cookie)
1831 struct lov_obd *lov = &exp->exp_obd->u.lov;
1832 struct lov_async_page *lap;
1835 LASSERT(loi == NULL);
1837 if (lsm_bad_magic(lsm))
1840 lap = lap_from_cookie(cookie);
1842 RETURN(PTR_ERR(lap));
1844 loi = &lsm->lsm_oinfo[lap->lap_stripe];
1845 rc = obd_teardown_async_page(lov->tgts[loi->loi_ost_idx].ltd_exp,
1846 lsm, loi, lap->lap_sub_cookie);
1848 CERROR("unable to teardown sub cookie %p: %d\n",
1849 lap->lap_sub_cookie, rc);
1852 OBD_FREE(lap, sizeof(*lap));
1856 static int lov_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
1857 struct lustre_handle *parent_lock,
1858 __u32 type, void *cookie, int cookielen, __u32 mode,
1859 int *flags, void *cb, void *data,
1860 struct lustre_handle *lockh)
1862 struct lov_obd *lov;
1863 struct lov_oinfo *loi;
1864 struct lov_stripe_md submd;
1865 struct ldlm_extent *extent = cookie;
1869 if (lsm_bad_magic(lsm))
1872 /* we should never be asked to replay a lock this way. */
1873 LASSERT((*flags & LDLM_FL_REPLAY) == 0);
1875 if (!exp || !exp->exp_obd)
1878 lov = &exp->exp_obd->u.lov;
1879 loi = lsm->lsm_oinfo;
1880 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1881 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1885 /* XXX LOV STACKING: submd should be from the subobj */
1886 submd.lsm_object_id = loi->loi_id;
1887 submd.lsm_stripe_count = 0;
1888 /* XXX submd is not fully initialized here */
1890 rc = obd_enqueue(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd,
1891 parent_lock, type, extent, sizeof(*extent),
1892 mode, flags, cb, data, lockh);
1894 if (rc != ELDLM_OK) {
1895 memset(lockh, 0, sizeof(*lockh));
1896 if (lov->tgts[loi->loi_ost_idx].active)
1897 CERROR("error: enqueue objid "LPX64" subobj "
1898 LPX64" on OST idx %d: rc = %d\n",
1899 lsm->lsm_object_id, loi->loi_id,
1900 loi->loi_ost_idx, rc);
1905 static int lov_match(struct obd_export *exp, struct lov_stripe_md *lsm,
1906 __u32 type, void *cookie, int cookielen, __u32 mode,
1907 int *flags, void *data, struct lustre_handle *lockh)
1909 struct lov_obd *lov;
1910 struct lov_oinfo *loi;
1911 struct lov_stripe_md submd;
1912 struct ldlm_extent *extent = cookie;
1916 if (lsm_bad_magic(lsm))
1919 if (!exp || !exp->exp_obd)
1922 lov = &exp->exp_obd->u.lov;
1923 loi = lsm->lsm_oinfo;
1924 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1925 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1929 /* XXX LOV STACKING: submd should be from the subobj */
1930 submd.lsm_object_id = loi->loi_id;
1931 submd.lsm_stripe_count = 0;
1932 /* XXX submd is not fully initialized here */
1933 rc = obd_match(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd, type,
1934 extent, sizeof(*extent), mode, flags, data, lockh);
1938 static int lov_change_cbdata(struct obd_export *exp,
1939 struct lov_stripe_md *lsm, ldlm_iterator_t it,
1942 struct lov_obd *lov;
1943 struct lov_oinfo *loi;
1947 if (lsm_bad_magic(lsm))
1950 if (!exp || !exp->exp_obd)
1953 lov = &exp->exp_obd->u.lov;
1954 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1955 struct lov_stripe_md submd;
1956 if (lov->tgts[loi->loi_ost_idx].active == 0)
1957 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1959 submd.lsm_object_id = loi->loi_id;
1960 submd.lsm_stripe_count = 0;
1961 rc = obd_change_cbdata(lov->tgts[loi->loi_ost_idx].ltd_exp,
1967 static int lov_cancel(struct obd_export *exp, struct lov_stripe_md *lsm,
1968 __u32 mode, struct lustre_handle *lockh)
1970 struct lov_obd *lov;
1971 struct lov_oinfo *loi;
1972 struct lov_stripe_md submd;
1976 if (lsm_bad_magic(lsm))
1979 if (!exp || !exp->exp_obd)
1982 lov = &exp->exp_obd->u.lov;
1983 loi = lsm->lsm_oinfo;
1985 /* XXX LOV STACKING: submd should be from the subobj */
1986 submd.lsm_object_id = loi->loi_id;
1987 submd.lsm_stripe_count = 0;
1988 rc = obd_cancel(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd,
1990 if (rc && lov->tgts[loi->loi_ost_idx].active)
1991 CERROR("error: cancel objid "LPX64" subobj "
1992 LPX64" on OST idx %d: rc = %d\n",
1993 lsm->lsm_object_id, loi->loi_id, loi->loi_ost_idx, rc);
1999 static int lov_cancel_unused(struct obd_export *exp,
2000 struct lov_stripe_md *lsm, int flags, void *opaque)
2002 struct lov_obd *lov;
2003 struct lov_oinfo *loi;
2007 if (lsm_bad_magic(lsm))
2010 if (!exp || !exp->exp_obd)
2013 lov = &exp->exp_obd->u.lov;
2014 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
2015 struct lov_stripe_md submd;
2018 if (lov->tgts[loi->loi_ost_idx].active == 0)
2019 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2021 submd.lsm_object_id = loi->loi_id;
2022 submd.lsm_stripe_count = 0;
2023 err = obd_cancel_unused(lov->tgts[loi->loi_ost_idx].ltd_exp,
2024 &submd, flags, opaque);
2025 if (err && lov->tgts[loi->loi_ost_idx].active) {
2026 CERROR("error: cancel unused objid "LPX64" subobj "LPX64
2027 " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
2028 loi->loi_id, loi->loi_ost_idx, err);
2036 #define LOV_U64_MAX ((__u64)~0ULL)
2037 #define LOV_SUM_MAX(tot, add) \
2039 if ((tot) + (add) < (tot)) \
2040 (tot) = LOV_U64_MAX; \
2045 static int lov_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2046 unsigned long max_age)
2048 struct lov_obd *lov = &obd->u.lov;
2049 struct obd_statfs lov_sfs;
2056 /* We only get block data from the OBD */
2057 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2060 if (!lov->tgts[i].active) {
2061 CDEBUG(D_HA, "lov idx %d inactive\n", i);
2065 err = obd_statfs(class_exp2obd(lov->tgts[i].ltd_exp), &lov_sfs,
2068 if (lov->tgts[i].active && !rc)
2074 memcpy(osfs, &lov_sfs, sizeof(lov_sfs));
2077 osfs->os_bfree += lov_sfs.os_bfree;
2078 osfs->os_bavail += lov_sfs.os_bavail;
2079 osfs->os_blocks += lov_sfs.os_blocks;
2080 /* XXX not sure about this one - depends on policy.
2081 * - could be minimum if we always stripe on all OBDs
2082 * (but that would be wrong for any other policy,
2083 * if one of the OBDs has no more objects left)
2084 * - could be sum if we stripe whole objects
2085 * - could be average, just to give a nice number
2087 * To give a "reasonable" (if not wholly accurate)
2088 * number, we divide the total number of free objects
2089 * by expected stripe count (watch out for overflow).
2091 LOV_SUM_MAX(osfs->os_files, lov_sfs.os_files);
2092 LOV_SUM_MAX(osfs->os_ffree, lov_sfs.os_ffree);
2097 __u32 expected_stripes = lov->desc.ld_default_stripe_count ?
2098 lov->desc.ld_default_stripe_count :
2099 lov->desc.ld_active_tgt_count;
2101 if (osfs->os_files != LOV_U64_MAX)
2102 do_div(osfs->os_files, expected_stripes);
2103 if (osfs->os_ffree != LOV_U64_MAX)
2104 do_div(osfs->os_ffree, expected_stripes);
2111 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2112 void *karg, void *uarg)
2114 struct obd_device *obddev = class_exp2obd(exp);
2115 struct lov_obd *lov = &obddev->u.lov;
2116 int i, count = lov->desc.ld_tgt_count;
2117 struct obd_uuid *uuidp;
2123 case OBD_IOC_LOV_GET_CONFIG: {
2124 struct obd_ioctl_data *data = karg;
2125 struct lov_tgt_desc *tgtdesc;
2126 struct lov_desc *desc;
2131 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2134 data = (struct obd_ioctl_data *)buf;
2136 if (sizeof(*desc) > data->ioc_inllen1) {
2141 if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
2146 desc = (struct lov_desc *)data->ioc_inlbuf1;
2147 memcpy(desc, &(lov->desc), sizeof(*desc));
2149 uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
2150 tgtdesc = lov->tgts;
2151 for (i = 0; i < count; i++, uuidp++, tgtdesc++)
2152 obd_str2uuid(uuidp, tgtdesc->uuid.uuid);
2154 rc = copy_to_user((void *)uarg, buf, len);
2157 obd_ioctl_freedata(buf, len);
2160 case LL_IOC_LOV_SETSTRIPE:
2161 rc = lov_setstripe(exp, karg, uarg);
2163 case LL_IOC_LOV_GETSTRIPE:
2164 rc = lov_getstripe(exp, karg, uarg);
2171 for (i = 0; i < count; i++) {
2174 err = obd_iocontrol(cmd, lov->tgts[i].ltd_exp,
2177 if (lov->tgts[i].active) {
2178 CERROR("error: iocontrol OSC %s on OST"
2179 "idx %d: err = %d\n",
2180 lov->tgts[i].uuid.uuid, i, err);
2195 static int lov_get_info(struct obd_export *exp, __u32 keylen,
2196 void *key, __u32 *vallen, void *val)
2198 struct obd_device *obddev = class_exp2obd(exp);
2199 struct lov_obd *lov = &obddev->u.lov;
2203 if (!vallen || !val)
2206 if (keylen > strlen("lock_to_stripe") &&
2207 strcmp(key, "lock_to_stripe") == 0) {
2210 struct ldlm_lock *lock;
2211 struct lov_stripe_md *lsm;
2213 struct lov_oinfo *loi;
2214 __u32 *stripe = val;
2216 if (*vallen < sizeof(*stripe))
2218 *vallen = sizeof(*stripe);
2220 /* XXX This is another one of those bits that will need to
2221 * change if we ever actually support nested LOVs. It uses
2222 * the lock's export to find out which stripe it is. */
2223 for (i = 0, loi = data->lsm->lsm_oinfo;
2224 i < data->lsm->lsm_stripe_count;
2226 if (lov->tgts[loi->loi_ost_idx].ltd_exp ==
2227 data->lock->l_conn_export) {
2233 } else if (keylen >= strlen("last_id") && strcmp(key, "last_id") == 0) {
2235 int rc, size = sizeof(obd_id);
2236 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2237 if (!lov->tgts[i].active)
2239 rc = obd_get_info(lov->tgts[i].ltd_exp, keylen, key,
2245 } else if (keylen >= strlen("lovdesc") && strcmp(key, "lovdesc") == 0) {
2246 struct lov_desc *desc_ret = val;
2247 *desc_ret = lov->desc;
2255 static int lov_set_info(struct obd_export *exp, obd_count keylen,
2256 void *key, obd_count vallen, void *val)
2258 struct obd_device *obddev = class_exp2obd(exp);
2259 struct lov_obd *lov = &obddev->u.lov;
2263 #define KEY_IS(str) \
2264 (keylen == strlen(str) && memcmp(key, str, keylen) == 0)
2266 if (KEY_IS("next_id")) {
2267 if (vallen != lov->desc.ld_tgt_count)
2269 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2272 if (!lov->tgts[i].active)
2275 er = obd_set_info(lov->tgts[i].ltd_exp, keylen, key,
2276 sizeof(obd_id), ((obd_id*)val) + i);
2283 if (KEY_IS("growth_count")) {
2284 if (vallen != sizeof(int))
2286 } else if (KEY_IS("mds_conn") || KEY_IS("unlinked")) {
2293 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2296 if (!lov->tgts[i].active)
2299 er = obd_set_info(lov->tgts[i].ltd_exp, keylen, key, vallen,
2310 static int lov_lock_contains(struct obd_export *exp, struct lov_stripe_md *lsm,
2311 struct ldlm_lock *lock, obd_off offset)
2313 struct lov_obd *lov;
2314 struct lov_oinfo *loi;
2315 struct lov_stripe_md submd;
2319 LASSERT(lsm != NULL);
2323 lov = &exp->exp_obd->u.lov;
2324 loi = lsm->lsm_oinfo;
2326 if (lov->tgts[loi->loi_ost_idx].active == 0) {
2327 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2331 /* XXX submd is not fully initialized here */
2332 /* XXX LOV STACKING: submd should be from the subobj */
2333 submd.lsm_object_id = loi->loi_id;
2334 submd.lsm_stripe_count = 0;
2336 rc = obd_lock_contains(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd,
2341 struct obd_ops lov_obd_ops = {
2342 o_owner: THIS_MODULE,
2343 o_attach: lov_attach,
2344 o_detach: lov_detach,
2346 o_cleanup: lov_cleanup,
2347 o_connect: lov_connect,
2348 o_disconnect: lov_disconnect,
2349 o_statfs: lov_statfs,
2350 o_packmd: lov_packmd,
2351 o_unpackmd: lov_unpackmd,
2352 o_create: lov_create,
2353 o_destroy: lov_destroy,
2354 o_getattr: lov_getattr,
2355 o_getattr_async: lov_getattr_async,
2356 o_setattr: lov_setattr,
2358 o_brw_async: lov_brw_async,
2359 .o_prep_async_page = lov_prep_async_page,
2360 .o_queue_async_io = lov_queue_async_io,
2361 .o_set_async_flags = lov_set_async_flags,
2362 .o_queue_sync_io = lov_queue_sync_io,
2363 .o_trigger_sync_io = lov_trigger_sync_io,
2364 .o_teardown_async_page lov_teardown_async_page,
2367 o_enqueue: lov_enqueue,
2369 o_change_cbdata: lov_change_cbdata,
2370 o_cancel: lov_cancel,
2371 o_cancel_unused: lov_cancel_unused,
2372 o_iocontrol: lov_iocontrol,
2373 o_get_info: lov_get_info,
2374 o_set_info: lov_set_info,
2375 o_llog_init: lov_llog_init,
2376 o_llog_finish: lov_llog_finish,
2377 o_lock_contains:lov_lock_contains,
2378 o_notify: lov_notify,
2381 int __init lov_init(void)
2383 struct lprocfs_static_vars lvars;
2386 lprocfs_init_vars(lov, &lvars);
2387 rc = class_register_type(&lov_obd_ops, lvars.module_vars,
2388 OBD_LOV_DEVICENAME);
2393 static void /*__exit*/ lov_exit(void)
2395 class_unregister_type(OBD_LOV_DEVICENAME);
2398 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2399 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver");
2400 MODULE_LICENSE("GPL");
2402 module_init(lov_init);
2403 module_exit(lov_exit);