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);
57 struct lov_lock_handles {
58 struct portals_handle llh_handle;
59 atomic_t llh_refcount;
61 struct lustre_handle llh_handles[0];
64 static void lov_llh_addref(void *llhp)
66 struct lov_lock_handles *llh = llhp;
68 atomic_inc(&llh->llh_refcount);
69 CDEBUG(D_INFO, "GETting llh %p : new refcount %d\n", llh,
70 atomic_read(&llh->llh_refcount));
73 static struct lov_lock_handles *lov_llh_new(struct lov_stripe_md *lsm)
75 struct lov_lock_handles *llh;
77 OBD_ALLOC(llh, sizeof *llh +
78 sizeof(*llh->llh_handles) * lsm->lsm_stripe_count);
80 CERROR("out of memory\n");
83 atomic_set(&llh->llh_refcount, 2);
84 llh->llh_stripe_count = lsm->lsm_stripe_count;
85 INIT_LIST_HEAD(&llh->llh_handle.h_link);
86 class_handle_hash(&llh->llh_handle, lov_llh_addref);
90 static struct lov_lock_handles *lov_handle2llh(struct lustre_handle *handle)
93 LASSERT(handle != NULL);
94 RETURN(class_handle2object(handle->cookie));
97 static void lov_llh_put(struct lov_lock_handles *llh)
99 CDEBUG(D_INFO, "PUTting llh %p : new refcount %d\n", llh,
100 atomic_read(&llh->llh_refcount) - 1);
101 LASSERT(atomic_read(&llh->llh_refcount) > 0 &&
102 atomic_read(&llh->llh_refcount) < 0x5a5a);
103 if (atomic_dec_and_test(&llh->llh_refcount)) {
104 LASSERT(list_empty(&llh->llh_handle.h_link));
105 OBD_FREE(llh, sizeof *llh +
106 sizeof(*llh->llh_handles) * llh->llh_stripe_count);
110 static void lov_llh_destroy(struct lov_lock_handles *llh)
112 class_handle_unhash(&llh->llh_handle);
117 int lov_attach(struct obd_device *dev, obd_count len, void *data)
119 struct lprocfs_static_vars lvars;
122 lprocfs_init_vars(lov, &lvars);
123 rc = lprocfs_obd_attach(dev, lvars.obd_vars);
126 struct proc_dir_entry *entry;
128 entry = create_proc_entry("target_obd", 0444,
129 dev->obd_proc_entry);
133 entry->proc_fops = &lov_proc_target_fops;
141 int lov_detach(struct obd_device *dev)
143 return lprocfs_obd_detach(dev);
146 static int lov_connect(struct lustre_handle *conn, struct obd_device *obd,
147 struct obd_uuid *cluuid)
149 struct ptlrpc_request *req = NULL;
150 struct lov_obd *lov = &obd->u.lov;
151 struct lov_desc *desc = &lov->desc;
152 struct lov_tgt_desc *tgts;
153 struct obd_export *exp;
157 rc = class_connect(conn, obd, cluuid);
161 exp = class_conn2export(conn);
163 /* We don't want to actually do the underlying connections more than
164 * once, so keep track. */
166 if (lov->refcount > 1) {
167 class_export_put(exp);
171 for (i = 0, tgts = lov->tgts; i < desc->ld_tgt_count; i++, tgts++) {
172 struct obd_uuid *tgt_uuid = &tgts->uuid;
173 struct obd_device *tgt_obd;
174 struct obd_uuid lov_osc_uuid = { "LOV_OSC_UUID" };
175 struct lustre_handle conn = {0, };
177 LASSERT( tgt_uuid != NULL);
179 tgt_obd = class_find_client_obd(tgt_uuid, LUSTRE_OSC_NAME,
183 CERROR("Target %s not attached\n", tgt_uuid->uuid);
184 GOTO(out_disc, rc = -EINVAL);
187 if (!tgt_obd->obd_set_up) {
188 CERROR("Target %s not set up\n", tgt_uuid->uuid);
189 GOTO(out_disc, rc = -EINVAL);
192 if (tgt_obd->u.cli.cl_import->imp_invalid) {
193 CERROR("not connecting OSC %s; administratively "
194 "disabled\n", tgt_uuid->uuid);
195 rc = obd_register_observer(tgt_obd, obd);
197 CERROR("Target %s register_observer error %d; "
198 "will not be able to reactivate\n",
204 rc = obd_connect(&conn, tgt_obd, &lov_osc_uuid);
206 CERROR("Target %s connect error %d\n", tgt_uuid->uuid,
210 tgts->ltd_exp = class_conn2export(&conn);
212 rc = obd_register_observer(tgt_obd, obd);
214 CERROR("Target %s register_observer error %d\n",
216 obd_disconnect(tgts->ltd_exp, 0);
220 desc->ld_active_tgt_count++;
224 ptlrpc_req_finished(req);
225 class_export_put(exp);
230 struct obd_uuid uuid;
232 --desc->ld_active_tgt_count;
234 /* save for CERROR below; (we know it's terminated) */
236 rc2 = obd_disconnect(tgts->ltd_exp, 0);
238 CERROR("error: LOV target %s disconnect on OST idx %d: "
239 "rc = %d\n", uuid.uuid, i, rc2);
241 class_disconnect(exp, 0);
245 static int lov_disconnect(struct obd_export *exp, int flags)
247 struct obd_device *obd = class_exp2obd(exp);
248 struct lov_obd *lov = &obd->u.lov;
255 /* Only disconnect the underlying layers on the final disconnect. */
257 if (lov->refcount != 0)
260 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
261 if (lov->tgts[i].ltd_exp == NULL)
264 if (obd->obd_no_recov) {
265 /* Pass it on to our clients.
266 * XXX This should be an argument to disconnect,
267 * XXX not a back-door flag on the OBD. Ah well.
269 struct obd_device *osc_obd;
270 osc_obd = class_exp2obd(lov->tgts[i].ltd_exp);
272 osc_obd->obd_no_recov = 1;
275 obd_register_observer(lov->tgts[i].ltd_exp->exp_obd, NULL);
277 rc = obd_disconnect(lov->tgts[i].ltd_exp, flags);
279 if (lov->tgts[i].active) {
280 CERROR("Target %s disconnect error %d\n",
281 lov->tgts[i].uuid.uuid, rc);
285 if (lov->tgts[i].active) {
286 lov->desc.ld_active_tgt_count--;
287 lov->tgts[i].active = 0;
289 lov->tgts[i].ltd_exp = NULL;
293 rc = class_disconnect(exp, 0);
299 * -EINVAL : UUID can't be found in the LOV's target list
300 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
301 * -EBADF : The UUID is found, but the OBD is the wrong type (!)
303 static int lov_set_osc_active(struct lov_obd *lov, struct obd_uuid *uuid,
306 struct obd_device *obd;
307 struct lov_tgt_desc *tgt;
311 CDEBUG(D_INFO, "Searching in lov %p for uuid %s (activate=%d)\n",
312 lov, uuid->uuid, activate);
314 spin_lock(&lov->lov_lock);
315 for (i = 0, tgt = lov->tgts; i < lov->desc.ld_tgt_count; i++, tgt++) {
316 CDEBUG(D_INFO, "lov idx %d is %s conn "LPX64"\n",
317 i, tgt->uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
318 if (strncmp(uuid->uuid, tgt->uuid.uuid, sizeof uuid->uuid) == 0)
322 if (i == lov->desc.ld_tgt_count)
323 GOTO(out, rc = -EINVAL);
325 obd = class_exp2obd(tgt->ltd_exp);
327 /* This can happen if OST failure races with node shutdown */
328 GOTO(out, rc = -ENOTCONN);
331 CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LOV idx %d\n",
332 obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
333 obd->obd_type->typ_name, i);
334 LASSERT(strcmp(obd->obd_type->typ_name, "osc") == 0);
336 if (tgt->active == activate) {
337 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
338 activate ? "" : "in");
342 CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd, activate ? "" : "in");
344 tgt->active = activate;
346 lov->desc.ld_active_tgt_count++;
348 lov->desc.ld_active_tgt_count--;
352 spin_unlock(&lov->lov_lock);
356 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
360 struct obd_uuid *uuid;
362 if (strcmp(watched->obd_type->typ_name, "osc")) {
363 CERROR("unexpected notification of %s %s!\n",
364 watched->obd_type->typ_name,
368 uuid = &watched->u.cli.cl_import->imp_target_uuid;
371 * Must notify (MDS) before we mark the OSC as active, so that
372 * the orphan deletion happens without interference from racing
375 if (obd->obd_observer) {
376 /* Pass the notification up the chain. */
377 rc = obd_notify(obd->obd_observer, watched, active);
382 rc = lov_set_osc_active(&obd->u.lov, uuid, active);
385 CERROR("%sactivation of %s failed: %d\n",
386 active ? "" : "de", uuid->uuid, rc);
391 static int lov_setup(struct obd_device *obd, obd_count len, void *buf)
393 struct lustre_cfg *lcfg = buf;
394 struct lov_desc *desc;
395 struct lov_obd *lov = &obd->u.lov;
396 struct obd_uuid *uuids;
397 struct lov_tgt_desc *tgts;
403 if (lcfg->lcfg_inllen1 < 1) {
404 CERROR("LOV setup requires a descriptor\n");
408 if (lcfg->lcfg_inllen2 < 1) {
409 CERROR("LOV setup requires an OST UUID list\n");
413 desc = (struct lov_desc *)lcfg->lcfg_inlbuf1;
414 if (sizeof(*desc) > lcfg->lcfg_inllen1) {
415 CERROR("descriptor size wrong: %d > %d\n",
416 (int)sizeof(*desc), lcfg->lcfg_inllen1);
420 count = desc->ld_tgt_count;
421 uuids = (struct obd_uuid *)lcfg->lcfg_inlbuf2;
422 if (sizeof(*uuids) * count != lcfg->lcfg_inllen2) {
423 CERROR("UUID array size wrong: %u * %u != %u\n",
424 (int)sizeof(*uuids), count, lcfg->lcfg_inllen2);
428 /* Because of 64-bit divide/mod operations only work with a 32-bit
429 * divisor in a 32-bit kernel, we cannot support a stripe width
430 * of 4GB or larger on 32-bit CPUs.
432 if ((desc->ld_default_stripe_count ?
433 desc->ld_default_stripe_count : desc->ld_tgt_count) *
434 desc->ld_default_stripe_size > ~0UL) {
435 CERROR("LOV: stripe width "LPU64"x%u > %lu on 32-bit system\n",
436 desc->ld_default_stripe_size,
437 desc->ld_default_stripe_count ?
438 desc->ld_default_stripe_count : desc->ld_tgt_count,~0UL);
442 lov->bufsize = sizeof(struct lov_tgt_desc) * count;
443 OBD_ALLOC(lov->tgts, lov->bufsize);
444 if (lov->tgts == NULL) {
445 CERROR("Out of memory\n");
450 spin_lock_init(&lov->lov_lock);
452 for (i = 0, tgts = lov->tgts; i < desc->ld_tgt_count; i++, tgts++) {
453 struct obd_uuid *uuid = &tgts->uuid;
455 /* NULL termination already checked */
463 static int lov_cleanup(struct obd_device *obd, int flags)
465 struct lov_obd *lov = &obd->u.lov;
467 OBD_FREE(lov->tgts, lov->bufsize);
472 /* compute object size given "stripeno" and the ost size */
473 static obd_size lov_stripe_size(struct lov_stripe_md *lsm, obd_size ost_size,
476 unsigned long ssize = lsm->lsm_stripe_size;
477 unsigned long swidth = ssize * lsm->lsm_stripe_count;
478 unsigned long stripe_size;
484 /* do_div(a, b) returns a % b, and a = a / b */
485 stripe_size = do_div(ost_size, ssize);
488 lov_size = ost_size * swidth + stripeno * ssize + stripe_size;
490 lov_size = (ost_size - 1) * swidth + (stripeno + 1) * ssize;
495 static void lov_merge_attrs(struct obdo *tgt, struct obdo *src, obd_flag valid,
496 struct lov_stripe_md *lsm, int stripeno, int *set)
498 valid &= src->o_valid;
501 if (valid & OBD_MD_FLSIZE) {
502 /* this handles sparse files properly */
505 lov_size = lov_stripe_size(lsm, src->o_size, stripeno);
506 if (lov_size > tgt->o_size)
507 tgt->o_size = lov_size;
509 if (valid & OBD_MD_FLBLOCKS)
510 tgt->o_blocks += src->o_blocks;
511 if (valid & OBD_MD_FLBLKSZ)
512 tgt->o_blksize += src->o_blksize;
513 if (valid & OBD_MD_FLCTIME && tgt->o_ctime < src->o_ctime)
514 tgt->o_ctime = src->o_ctime;
515 if (valid & OBD_MD_FLMTIME && tgt->o_mtime < src->o_mtime)
516 tgt->o_mtime = src->o_mtime;
518 memcpy(tgt, src, sizeof(*tgt));
519 tgt->o_id = lsm->lsm_object_id;
520 if (valid & OBD_MD_FLSIZE)
521 tgt->o_size = lov_stripe_size(lsm,src->o_size,stripeno);
527 #define log2(n) ffz(~(n))
530 static int lov_clear_orphans(struct obd_export *export, struct obdo *src_oa,
531 struct lov_stripe_md **ea,
532 struct obd_trans_info *oti)
536 struct obd_uuid *ost_uuid = NULL;
540 LASSERT(src_oa->o_valid & OBD_MD_FLFLAGS &&
541 src_oa->o_flags == OBD_FL_DELORPHAN);
543 lov = &export->exp_obd->u.lov;
545 tmp_oa = obdo_alloc();
549 if (src_oa->o_valid & OBD_MD_FLINLINE) {
550 ost_uuid = (struct obd_uuid *)src_oa->o_inline;
551 CDEBUG(D_HA, "clearing orphans only for %s\n",
555 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
556 struct lov_stripe_md obj_md;
557 struct lov_stripe_md *obj_mdp = &obj_md;
560 /* if called for a specific target, we don't
561 care if it is not active. */
562 if (lov->tgts[i].active == 0 && ost_uuid == NULL) {
563 CDEBUG(D_HA, "lov idx %d inactive\n", i);
567 if (ost_uuid && !obd_uuid_equals(ost_uuid, &lov->tgts[i].uuid))
570 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
572 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
573 err = obd_create(lov->tgts[i].ltd_exp, tmp_oa, &obj_mdp, oti);
575 /* This export will be disabled until it is recovered,
576 and then orphan recovery will be completed. */
577 CERROR("error in orphan recovery on OST idx %d/%d: "
578 "rc = %d\n", i, lov->desc.ld_tgt_count, err);
587 #define LOV_CREATE_RESEED_INTERVAL 1000
589 /* the LOV expects oa->o_id to be set to the LOV object id */
590 static int lov_create(struct obd_export *exp, struct obdo *src_oa,
591 struct lov_stripe_md **ea, struct obd_trans_info *oti)
593 static int ost_start_idx, ost_start_count;
595 struct lov_stripe_md *lsm;
596 struct lov_oinfo *loi = NULL;
597 struct obdo *tmp_oa, *ret_oa;
598 struct llog_cookie *cookies = NULL;
599 unsigned ost_count, ost_idx;
600 int set = 0, obj_alloc = 0, cookie_sent = 0, rc = 0, i;
605 if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
606 src_oa->o_flags == OBD_FL_DELORPHAN) {
607 rc = lov_clear_orphans(exp, src_oa, ea, oti);
614 lov = &exp->exp_obd->u.lov;
616 if (!lov->desc.ld_active_tgt_count)
619 /* Recreate a specific object id at the given OST index */
620 if (src_oa->o_valid & OBD_MD_FLFLAGS && src_oa->o_flags &
621 OBD_FL_RECREATE_OBJS) {
622 struct lov_stripe_md obj_md;
623 struct lov_stripe_md *obj_mdp = &obj_md;
625 ost_idx = src_oa->o_nlink;
629 if (ost_idx >= lov->desc.ld_tgt_count)
631 for (i = 0; i < lsm->lsm_stripe_count; i++) {
632 if (lsm->lsm_oinfo[i].loi_ost_idx == ost_idx) {
633 if (lsm->lsm_oinfo[i].loi_id != src_oa->o_id)
638 if (i == lsm->lsm_stripe_count)
641 rc = obd_create(lov->tgts[ost_idx].ltd_exp, src_oa,
646 ret_oa = obdo_alloc();
650 tmp_oa = obdo_alloc();
652 GOTO(out_oa, rc = -ENOMEM);
657 ost_count = lov_get_stripecnt(lov, 0);
659 /* If the MDS file was truncated up to some size, stripe over
660 * enough OSTs to allow the file to be created at that size. */
661 if (src_oa->o_valid & OBD_MD_FLSIZE) {
662 stripes=((src_oa->o_size+LUSTRE_STRIPE_MAXBYTES)>>12)-1;
663 do_div(stripes, (__u32)(LUSTRE_STRIPE_MAXBYTES >> 12));
665 if (stripes > lov->desc.ld_active_tgt_count)
667 if (stripes > ost_count)
673 rc = lov_alloc_memmd(&lsm, stripes, lov->desc.ld_pattern ?
674 lov->desc.ld_pattern : LOV_PATTERN_RAID0);
681 ost_count = lov->desc.ld_tgt_count;
683 LASSERT(src_oa->o_valid & OBD_MD_FLID);
684 lsm->lsm_object_id = src_oa->o_id;
685 if (!lsm->lsm_stripe_size)
686 lsm->lsm_stripe_size = lov->desc.ld_default_stripe_size;
687 if (!lsm->lsm_pattern) {
688 lsm->lsm_pattern = lov->desc.ld_pattern ?
689 lov->desc.ld_pattern : LOV_PATTERN_RAID0;
692 if (*ea == NULL || lsm->lsm_oinfo[0].loi_ost_idx >= ost_count) {
693 if (--ost_start_count <= 0) {
694 ost_start_idx = ll_insecure_random_int();
695 ost_start_count = LOV_CREATE_RESEED_INTERVAL;
696 } else if (lsm->lsm_stripe_count >=
697 lov->desc.ld_active_tgt_count) {
698 /* If we allocate from all of the stripes, make the
699 * next file start on the next OST. */
702 ost_idx = ost_start_idx % ost_count;
704 ost_idx = lsm->lsm_oinfo[0].loi_ost_idx;
707 CDEBUG(D_INODE, "allocating %d subobjs for objid "LPX64" at idx %d\n",
708 lsm->lsm_stripe_count, lsm->lsm_object_id, ost_idx);
710 /* XXX LOV STACKING: need to figure out how many real OSCs */
711 if (oti && (src_oa->o_valid & OBD_MD_FLCOOKIE)) {
712 oti_alloc_cookies(oti, lsm->lsm_stripe_count);
713 if (!oti->oti_logcookies)
714 GOTO(out_cleanup, rc = -ENOMEM);
715 cookies = oti->oti_logcookies;
718 loi = lsm->lsm_oinfo;
719 for (i = 0; i < ost_count; i++, ost_idx = (ost_idx + 1) % ost_count) {
720 struct lov_stripe_md obj_md;
721 struct lov_stripe_md *obj_mdp = &obj_md;
725 if (lov->tgts[ost_idx].active == 0) {
726 CDEBUG(D_HA, "lov idx %d inactive\n", ost_idx);
730 /* create data objects with "parent" OA */
731 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
733 /* XXX When we start creating objects on demand, we need to
734 * make sure that we always create the object on the
735 * stripe which holds the existing file size.
737 if (src_oa->o_valid & OBD_MD_FLSIZE) {
738 if (lov_stripe_offset(lsm, src_oa->o_size, i,
739 &tmp_oa->o_size) < 0 &&
743 CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
744 i, tmp_oa->o_size, src_oa->o_size);
748 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
749 err = obd_create(lov->tgts[ost_idx].ltd_exp, tmp_oa, &obj_mdp,
752 if (lov->tgts[ost_idx].active) {
753 CERROR("error creating objid "LPX64" sub-object"
754 " on OST idx %d/%d: rc = %d\n",
755 src_oa->o_id, ost_idx,
756 lsm->lsm_stripe_count, err);
758 CERROR("obd_create returned invalid "
768 oti->oti_objid[ost_idx] = tmp_oa->o_id;
769 loi->loi_id = tmp_oa->o_id;
770 loi->loi_ost_idx = ost_idx;
771 CDEBUG(D_INODE, "objid "LPX64" has subobj "LPX64" at idx %d\n",
772 lsm->lsm_object_id, loi->loi_id, ost_idx);
774 lov_merge_attrs(ret_oa, tmp_oa, tmp_oa->o_valid, lsm,
779 ++oti->oti_logcookies;
780 if (tmp_oa->o_valid & OBD_MD_FLCOOKIE)
785 /* If we have allocated enough objects, we are OK */
786 if (obj_alloc == lsm->lsm_stripe_count)
787 GOTO(out_done, rc = 0);
790 if (obj_alloc == 0) {
793 GOTO(out_cleanup, rc);
796 /* If we were passed specific striping params, then a failure to
797 * meet those requirements is an error, since we can't reallocate
798 * that memory (it might be part of a larger array or something).
800 * We can only get here if lsm_stripe_count was originally > 1.
803 CERROR("can't lstripe objid "LPX64": have %u want %u, rc %d\n",
804 lsm->lsm_object_id, obj_alloc, lsm->lsm_stripe_count,rc);
807 GOTO(out_cleanup, rc);
809 struct lov_stripe_md *lsm_new;
810 /* XXX LOV STACKING call into osc for sizes */
811 unsigned oldsize, newsize;
813 if (oti && cookies && cookie_sent) {
814 oldsize = lsm->lsm_stripe_count * sizeof(*cookies);
815 newsize = obj_alloc * sizeof(*cookies);
817 oti_alloc_cookies(oti, obj_alloc);
818 if (oti->oti_logcookies) {
819 memcpy(oti->oti_logcookies, cookies, newsize);
820 OBD_FREE(cookies, oldsize);
821 cookies = oti->oti_logcookies;
823 CWARN("'leaking' %d bytes\n", oldsize-newsize);
827 CWARN("using fewer stripes for object "LPX64": old %u new %u\n",
828 lsm->lsm_object_id, lsm->lsm_stripe_count, obj_alloc);
829 oldsize = lov_stripe_md_size(lsm->lsm_stripe_count);
830 newsize = lov_stripe_md_size(obj_alloc);
831 OBD_ALLOC(lsm_new, newsize);
832 if (lsm_new != NULL) {
833 memcpy(lsm_new, lsm, newsize);
834 lsm_new->lsm_stripe_count = obj_alloc;
835 OBD_FREE(lsm, newsize);
838 CWARN("'leaking' %d bytes\n", oldsize - newsize);
845 if (src_oa->o_valid & OBD_MD_FLSIZE &&
846 ret_oa->o_size != src_oa->o_size) {
847 CERROR("original size "LPU64" isn't new object size "LPU64"\n",
848 src_oa->o_size, ret_oa->o_size);
851 ret_oa->o_id = src_oa->o_id;
852 memcpy(src_oa, ret_oa, sizeof(*src_oa));
858 if (oti && cookies) {
859 oti->oti_logcookies = cookies;
861 oti_free_cookies(oti);
862 src_oa->o_valid &= ~OBD_MD_FLCOOKIE;
864 src_oa->o_valid |= OBD_MD_FLCOOKIE;
870 while (obj_alloc-- > 0) {
871 struct obd_export *sub_exp;
875 sub_exp = lov->tgts[loi->loi_ost_idx].ltd_exp;
876 /* destroy already created objects here */
877 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
878 tmp_oa->o_id = loi->loi_id;
880 err = obd_destroy(sub_exp, tmp_oa, NULL, oti);
882 CERROR("Failed to uncreate objid "LPX64" subobj "LPX64
883 " on OST idx %d: rc = %d\n", src_oa->o_id,
884 loi->loi_id, loi->loi_ost_idx, err);
887 obd_free_memmd(exp, &lsm);
891 #define lsm_bad_magic(LSMP) \
893 struct lov_stripe_md *_lsm__ = (LSMP); \
896 CERROR("LOV requires striping ea\n"); \
898 } else if (_lsm__->lsm_magic != LOV_MAGIC) { \
899 CERROR("LOV striping magic bad %#x != %#x\n", \
900 _lsm__->lsm_magic, LOV_MAGIC); \
906 static int lov_destroy(struct obd_export *exp, struct obdo *oa,
907 struct lov_stripe_md *lsm, struct obd_trans_info *oti)
911 struct lov_oinfo *loi;
915 if (lsm_bad_magic(lsm))
918 if (!exp || !exp->exp_obd)
921 lov = &exp->exp_obd->u.lov;
922 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
924 if (lov->tgts[loi->loi_ost_idx].active == 0) {
925 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
926 /* Orphan clean up will (someday) fix this up. */
930 memcpy(&tmp, oa, sizeof(tmp));
931 tmp.o_id = loi->loi_id;
932 err = obd_destroy(lov->tgts[loi->loi_ost_idx].ltd_exp, &tmp,
934 if (err && lov->tgts[loi->loi_ost_idx].active) {
935 CERROR("error: destroying objid "LPX64" subobj "
936 LPX64" on OST idx %d: rc = %d\n",
937 oa->o_id, loi->loi_id, loi->loi_ost_idx, err);
945 static int lov_getattr(struct obd_export *exp, struct obdo *oa,
946 struct lov_stripe_md *lsm)
950 struct lov_oinfo *loi;
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 CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
963 lsm->lsm_object_id, lsm->lsm_stripe_count, lsm->lsm_stripe_size);
964 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
967 if (lov->tgts[loi->loi_ost_idx].active == 0) {
968 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
972 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
973 "%u\n", oa->o_id, i, loi->loi_id, loi->loi_ost_idx);
974 /* create data objects with "parent" OA */
975 memcpy(&tmp, oa, sizeof(tmp));
976 tmp.o_id = loi->loi_id;
978 err = obd_getattr(lov->tgts[loi->loi_ost_idx].ltd_exp, &tmp,
981 if (lov->tgts[loi->loi_ost_idx].active) {
982 CERROR("error: getattr objid "LPX64" subobj "
983 LPX64" on OST idx %d: rc = %d\n",
984 oa->o_id, loi->loi_id, loi->loi_ost_idx,
989 lov_merge_attrs(oa, &tmp, tmp.o_valid, lsm, i, &set);
997 static int lov_getattr_interpret(struct ptlrpc_request_set *rqset, void *data,
1000 struct lov_getattr_async_args *aa = data;
1001 struct lov_stripe_md *lsm = aa->aa_lsm;
1002 struct obdo *oa = aa->aa_oa;
1003 struct obdo *obdos = aa->aa_obdos;
1004 struct lov_oinfo *loi;
1010 /* NB all stripe requests succeeded to get here */
1012 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
1014 if (obdos[i].o_valid == 0) /* inactive stripe */
1017 lov_merge_attrs(oa, &obdos[i], obdos[i].o_valid, lsm,
1022 CERROR ("No stripes had valid attrs\n");
1027 OBD_FREE (obdos, lsm->lsm_stripe_count * sizeof (*obdos));
1031 static int lov_getattr_async(struct obd_export *exp, struct obdo *oa,
1032 struct lov_stripe_md *lsm,
1033 struct ptlrpc_request_set *rqset)
1036 struct lov_obd *lov;
1037 struct lov_oinfo *loi;
1038 struct lov_getattr_async_args *aa;
1039 int i, rc = 0, set = 0;
1042 if (lsm_bad_magic(lsm))
1045 if (!exp || !exp->exp_obd)
1048 lov = &exp->exp_obd->u.lov;
1050 OBD_ALLOC (obdos, lsm->lsm_stripe_count * sizeof (*obdos));
1054 CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
1055 lsm->lsm_object_id, lsm->lsm_stripe_count, lsm->lsm_stripe_size);
1056 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1059 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1060 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1061 /* leaves obdos[i].obd_valid unset */
1065 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1066 "%u\n", oa->o_id, i, loi->loi_id, loi->loi_ost_idx);
1067 /* create data objects with "parent" OA */
1068 memcpy(&obdos[i], oa, sizeof(obdos[i]));
1069 obdos[i].o_id = loi->loi_id;
1071 err = obd_getattr_async(lov->tgts[loi->loi_ost_idx].ltd_exp,
1072 &obdos[i], NULL, rqset);
1074 CERROR("error: getattr objid "LPX64" subobj "
1075 LPX64" on OST idx %d: rc = %d\n",
1076 oa->o_id, loi->loi_id, loi->loi_ost_idx,
1078 GOTO(out_obdos, rc = err);
1083 GOTO (out_obdos, rc = -EIO);
1085 LASSERT (rqset->set_interpret == NULL);
1086 rqset->set_interpret = lov_getattr_interpret;
1087 LASSERT (sizeof (rqset->set_args) >= sizeof (*aa));
1088 aa = (struct lov_getattr_async_args *)&rqset->set_args;
1091 aa->aa_obdos = obdos;
1096 OBD_FREE (obdos, lsm->lsm_stripe_count * sizeof (*obdos));
1102 static int lov_setattr(struct obd_export *exp, struct obdo *src_oa,
1103 struct lov_stripe_md *lsm, struct obd_trans_info *oti)
1105 struct obdo *tmp_oa, *ret_oa;
1106 struct lov_obd *lov;
1107 struct lov_oinfo *loi;
1108 int rc = 0, i, set = 0;
1111 if (lsm_bad_magic(lsm))
1114 if (!exp || !exp->exp_obd)
1117 /* for now, we only expect time updates here */
1118 LASSERT(!(src_oa->o_valid & ~(OBD_MD_FLID|OBD_MD_FLTYPE | OBD_MD_FLMODE|
1119 OBD_MD_FLATIME | OBD_MD_FLMTIME |
1120 OBD_MD_FLCTIME | OBD_MD_FLFLAGS |
1122 ret_oa = obdo_alloc();
1126 tmp_oa = obdo_alloc();
1128 GOTO(out_oa, rc = -ENOMEM);
1130 lov = &exp->exp_obd->u.lov;
1131 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1134 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1135 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1139 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1140 tmp_oa->o_id = loi->loi_id;
1142 if (src_oa->o_valid & OBD_MD_FLSIZE) {
1143 if (lov_stripe_offset(lsm, src_oa->o_size, i,
1144 &tmp_oa->o_size) < 0 &&
1148 CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
1149 i, tmp_oa->o_size, src_oa->o_size);
1152 err = obd_setattr(lov->tgts[loi->loi_ost_idx].ltd_exp, tmp_oa,
1155 if (lov->tgts[loi->loi_ost_idx].active) {
1156 CERROR("error: setattr objid "LPX64" subobj "
1157 LPX64" on OST idx %d: rc = %d\n",
1158 src_oa->o_id, loi->loi_id,
1159 loi->loi_ost_idx, err);
1165 lov_merge_attrs(ret_oa, tmp_oa, tmp_oa->o_valid, lsm, i, &set);
1170 ret_oa->o_id = src_oa->o_id;
1171 memcpy(src_oa, ret_oa, sizeof(*src_oa));
1180 /* we have an offset in file backed by an lov and want to find out where
1181 * that offset lands in our given stripe of the file. for the easy
1182 * case where the offset is within the stripe, we just have to scale the
1183 * offset down to make it relative to the stripe instead of the lov.
1185 * the harder case is what to do when the offset doesn't intersect the
1186 * stripe. callers will want start offsets clamped ahead to the start
1187 * of the nearest stripe in the file. end offsets similarly clamped to the
1188 * nearest ending byte of a stripe in the file:
1190 * all this function does is move offsets to the nearest region of the
1191 * stripe, and it does its work "mod" the full length of all the stripes.
1192 * consider a file with 3 stripes:
1195 * ---------------------------------------------------------------------
1196 * | 0 | 1 | 2 | 0 | 1 | 2 |
1197 * ---------------------------------------------------------------------
1199 * to find stripe 1's offsets for S and E, it divides by the full stripe
1200 * width and does its math in the context of a single set of stripes:
1203 * -----------------------------------
1205 * -----------------------------------
1207 * it'll notice that E is outside stripe 1 and clamp it to the end of the
1208 * stripe, then multiply it back out by lov_off to give the real offsets in
1212 * ---------------------------------------------------------------------
1213 * | 1 | 1 | 1 | 1 | 1 | 1 |
1214 * ---------------------------------------------------------------------
1216 * it would have done similarly and pulled S forward to the start of a 1
1217 * stripe if, say, S had landed in a 0 stripe.
1219 * this rounding isn't always correct. consider an E lov offset that lands
1220 * on a 0 stripe, the "mod stripe width" math will pull it forward to the
1221 * start of a 1 stripe, when in fact it wanted to be rounded back to the end
1222 * of a previous 1 stripe. this logic is handled by callers and this is why:
1224 * this function returns < 0 when the offset was "before" the stripe and
1225 * was moved forward to the start of the stripe in question; 0 when it
1226 * falls in the stripe and no shifting was done; > 0 when the offset
1227 * was outside the stripe and was pulled back to its final byte. */
1228 static int lov_stripe_offset(struct lov_stripe_md *lsm, obd_off lov_off,
1229 int stripeno, obd_off *obd_off)
1231 unsigned long ssize = lsm->lsm_stripe_size;
1232 unsigned long swidth = ssize * lsm->lsm_stripe_count;
1233 unsigned long stripe_off, this_stripe;
1236 if (lov_off == OBD_OBJECT_EOF) {
1237 *obd_off = OBD_OBJECT_EOF;
1241 /* do_div(a, b) returns a % b, and a = a / b */
1242 stripe_off = do_div(lov_off, swidth);
1244 this_stripe = stripeno * ssize;
1245 if (stripe_off < this_stripe) {
1249 stripe_off -= this_stripe;
1251 if (stripe_off >= ssize) {
1257 *obd_off = lov_off * ssize + stripe_off;
1261 /* Given a whole-file size and a stripe number, give the file size which
1262 * corresponds to the individual object of that stripe.
1264 * This behaves basically in the same was as lov_stripe_offset, except that
1265 * file sizes falling before the beginning of a stripe are clamped to the end
1266 * of the previous stripe, not the beginning of the next:
1269 * ---------------------------------------------------------------------
1270 * | 0 | 1 | 2 | 0 | 1 | 2 |
1271 * ---------------------------------------------------------------------
1273 * if clamped to stripe 2 becomes:
1276 * ---------------------------------------------------------------------
1277 * | 0 | 1 | 2 | 0 | 1 | 2 |
1278 * ---------------------------------------------------------------------
1280 static obd_off lov_size_to_stripe(struct lov_stripe_md *lsm, obd_off file_size,
1283 unsigned long ssize = lsm->lsm_stripe_size;
1284 unsigned long swidth = ssize * lsm->lsm_stripe_count;
1285 unsigned long stripe_off, this_stripe;
1287 if (file_size == OBD_OBJECT_EOF)
1288 return OBD_OBJECT_EOF;
1290 /* do_div(a, b) returns a % b, and a = a / b */
1291 stripe_off = do_div(file_size, swidth);
1293 this_stripe = stripeno * ssize;
1294 if (stripe_off < this_stripe) {
1295 /* Move to end of previous stripe, or zero */
1296 if (file_size > 0) {
1303 stripe_off -= this_stripe;
1305 if (stripe_off >= ssize) {
1306 /* Clamp to end of this stripe */
1311 return (file_size * ssize + stripe_off);
1314 /* given an extent in an lov and a stripe, calculate the extent of the stripe
1315 * that is contained within the lov extent. this returns true if the given
1316 * stripe does intersect with the lov extent. */
1317 static int lov_stripe_intersects(struct lov_stripe_md *lsm, int stripeno,
1318 obd_off start, obd_off end,
1319 obd_off *obd_start, obd_off *obd_end)
1321 int start_side, end_side;
1323 start_side = lov_stripe_offset(lsm, start, stripeno, obd_start);
1324 end_side = lov_stripe_offset(lsm, end, stripeno, obd_end);
1326 CDEBUG(D_INODE, "["LPU64"->"LPU64"] -> [(%d) "LPU64"->"LPU64" (%d)]\n",
1327 start, end, start_side, *obd_start, *obd_end, end_side);
1329 /* this stripe doesn't intersect the file extent when neither
1330 * start or the end intersected the stripe and obd_start and
1331 * obd_end got rounded up to the save value. */
1332 if (start_side != 0 && end_side != 0 && *obd_start == *obd_end)
1335 /* as mentioned in the lov_stripe_offset commentary, end
1336 * might have been shifted in the wrong direction. This
1337 * happens when an end offset is before the stripe when viewed
1338 * through the "mod stripe size" math. we detect it being shifted
1339 * in the wrong direction and touch it up.
1340 * interestingly, this can't underflow since end must be > start
1341 * if we passed through the previous check.
1342 * (should we assert for that somewhere?) */
1349 /* compute which stripe number "lov_off" will be written into */
1350 static int lov_stripe_number(struct lov_stripe_md *lsm, obd_off lov_off)
1352 unsigned long ssize = lsm->lsm_stripe_size;
1353 unsigned long swidth = ssize * lsm->lsm_stripe_count;
1354 unsigned long stripe_off;
1356 stripe_off = do_div(lov_off, swidth);
1358 return stripe_off / ssize;
1361 /* FIXME: maybe we'll just make one node the authoritative attribute node, then
1362 * we can send this 'punch' to just the authoritative node and the nodes
1363 * that the punch will affect. */
1364 static int lov_punch(struct obd_export *exp, struct obdo *oa,
1365 struct lov_stripe_md *lsm,
1366 obd_off start, obd_off end, struct obd_trans_info *oti)
1369 struct lov_obd *lov;
1370 struct lov_oinfo *loi;
1374 if (lsm_bad_magic(lsm))
1377 if (!exp || !exp->exp_obd)
1380 lov = &exp->exp_obd->u.lov;
1381 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1382 obd_off starti, endi;
1385 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1386 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1390 if (!lov_stripe_intersects(lsm, i, start, end, &starti, &endi))
1393 /* create data objects with "parent" OA */
1394 memcpy(&tmp, oa, sizeof(tmp));
1395 tmp.o_id = loi->loi_id;
1397 err = obd_punch(lov->tgts[loi->loi_ost_idx].ltd_exp, &tmp, NULL,
1398 starti, endi, NULL);
1400 if (lov->tgts[loi->loi_ost_idx].active) {
1401 CERROR("error: punch objid "LPX64" subobj "LPX64
1402 " on OST idx %d: rc = %d\n", oa->o_id,
1403 loi->loi_id, loi->loi_ost_idx, err);
1408 loi->loi_kms = loi->loi_rss = starti;
1414 static int lov_sync(struct obd_export *exp, struct obdo *oa,
1415 struct lov_stripe_md *lsm, obd_off start, obd_off end)
1418 struct lov_obd *lov;
1419 struct lov_oinfo *loi;
1423 if (lsm_bad_magic(lsm))
1433 lov = &exp->exp_obd->u.lov;
1434 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1435 obd_off starti, endi;
1438 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1439 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1443 if (!lov_stripe_intersects(lsm, i, start, end, &starti, &endi))
1446 memcpy(tmp, oa, sizeof(*tmp));
1447 tmp->o_id = loi->loi_id;
1449 err = obd_sync(lov->tgts[loi->loi_ost_idx].ltd_exp, tmp, NULL,
1452 if (lov->tgts[loi->loi_ost_idx].active) {
1453 CERROR("error: fsync objid "LPX64" subobj "LPX64
1454 " on OST idx %d: rc = %d\n", oa->o_id,
1455 loi->loi_id, loi->loi_ost_idx, err);
1466 static int lov_brw_check(struct lov_obd *lov, struct obdo *oa,
1467 struct lov_stripe_md *lsm,
1468 obd_count oa_bufs, struct brw_page *pga)
1472 /* The caller just wants to know if there's a chance that this
1473 * I/O can succeed */
1474 for (i = 0; i < oa_bufs; i++) {
1475 int stripe = lov_stripe_number(lsm, pga[i].off);
1476 int ost = lsm->lsm_oinfo[stripe].loi_ost_idx;
1477 struct ldlm_extent ext, subext;
1478 ext.start = pga[i].off;
1479 ext.end = pga[i].off + pga[i].count;
1481 if (!lov_stripe_intersects(lsm, i, ext.start, ext.end,
1482 &subext.start, &subext.end))
1485 if (lov->tgts[ost].active == 0) {
1486 CDEBUG(D_HA, "lov idx %d inactive\n", ost);
1489 rc = obd_brw(OBD_BRW_CHECK, lov->tgts[stripe].ltd_exp, oa,
1490 NULL, 1, &pga[i], NULL);
1497 static int lov_brw(int cmd, struct obd_export *exp, struct obdo *src_oa,
1498 struct lov_stripe_md *lsm, obd_count oa_bufs,
1499 struct brw_page *pga, struct obd_trans_info *oti)
1505 struct lov_stripe_md lsm;
1507 } *stripeinfo, *si, *si_last;
1508 struct obdo *ret_oa = NULL, *tmp_oa = NULL;
1509 struct lov_obd *lov;
1510 struct brw_page *ioarr;
1511 struct lov_oinfo *loi;
1512 int rc = 0, i, *where, stripe_count = lsm->lsm_stripe_count, set = 0;
1515 if (lsm_bad_magic(lsm))
1518 lov = &exp->exp_obd->u.lov;
1520 if (cmd == OBD_BRW_CHECK) {
1521 rc = lov_brw_check(lov, src_oa, lsm, oa_bufs, pga);
1525 OBD_ALLOC(stripeinfo, stripe_count * sizeof(*stripeinfo));
1529 OBD_ALLOC(where, sizeof(*where) * oa_bufs);
1531 GOTO(out_sinfo, rc = -ENOMEM);
1533 OBD_ALLOC(ioarr, sizeof(*ioarr) * oa_bufs);
1535 GOTO(out_where, rc = -ENOMEM);
1538 ret_oa = obdo_alloc();
1540 GOTO(out_ioarr, rc = -ENOMEM);
1542 tmp_oa = obdo_alloc();
1544 GOTO(out_oa, rc = -ENOMEM);
1547 for (i = 0; i < oa_bufs; i++) {
1548 where[i] = lov_stripe_number(lsm, pga[i].off);
1549 stripeinfo[where[i]].bufct++;
1552 for (i = 0, loi = lsm->lsm_oinfo, si_last = si = stripeinfo;
1553 i < stripe_count; i++, loi++, si_last = si, si++) {
1555 si->index = si_last->index + si_last->bufct;
1556 si->lsm.lsm_object_id = loi->loi_id;
1557 si->ost_idx = loi->loi_ost_idx;
1560 for (i = 0; i < oa_bufs; i++) {
1561 int which = where[i];
1564 shift = stripeinfo[which].index + stripeinfo[which].subcount;
1565 LASSERT(shift < oa_bufs);
1566 ioarr[shift] = pga[i];
1567 lov_stripe_offset(lsm, pga[i].off, which, &ioarr[shift].off);
1568 stripeinfo[which].subcount++;
1571 for (i = 0, si = stripeinfo; i < stripe_count; i++, si++) {
1572 int shift = si->index;
1574 if (lov->tgts[si->ost_idx].active == 0) {
1575 CDEBUG(D_HA, "lov idx %d inactive\n", si->ost_idx);
1576 GOTO(out_oa, rc = -EIO);
1580 LASSERT(shift < oa_bufs);
1582 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1584 tmp_oa->o_id = si->lsm.lsm_object_id;
1585 rc = obd_brw(cmd, lov->tgts[si->ost_idx].ltd_exp,
1586 tmp_oa, &si->lsm, si->bufct,
1587 &ioarr[shift], oti);
1589 GOTO(out_ioarr, rc);
1591 lov_merge_attrs(ret_oa, tmp_oa, tmp_oa->o_valid, lsm,
1596 ret_oa->o_id = src_oa->o_id;
1597 memcpy(src_oa, ret_oa, sizeof(*src_oa));
1606 OBD_FREE(ioarr, sizeof(*ioarr) * oa_bufs);
1608 OBD_FREE(where, sizeof(*where) * oa_bufs);
1610 OBD_FREE(stripeinfo, stripe_count * sizeof(*stripeinfo));
1614 static int lov_brw_interpret(struct ptlrpc_request_set *reqset, void *data,
1617 struct lov_brw_async_args *aa = data;
1618 struct lov_stripe_md *lsm = aa->aa_lsm;
1619 obd_count oa_bufs = aa->aa_oa_bufs;
1620 struct obdo *oa = aa->aa_oa;
1621 struct obdo *obdos = aa->aa_obdos;
1622 struct brw_page *ioarr = aa->aa_ioarr;
1623 struct lov_oinfo *loi;
1628 /* NB all stripe requests succeeded to get here */
1630 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
1632 if (obdos[i].o_valid == 0) /* inactive stripe */
1635 lov_merge_attrs(oa, &obdos[i], obdos[i].o_valid, lsm,
1640 CERROR("No stripes had valid attrs\n");
1644 oa->o_id = lsm->lsm_object_id;
1646 OBD_FREE(obdos, lsm->lsm_stripe_count * sizeof(*obdos));
1647 OBD_FREE(ioarr, sizeof(*ioarr) * oa_bufs);
1651 static int lov_brw_async(int cmd, struct obd_export *exp, struct obdo *oa,
1652 struct lov_stripe_md *lsm, obd_count oa_bufs,
1653 struct brw_page *pga, struct ptlrpc_request_set *set,
1654 struct obd_trans_info *oti)
1660 struct lov_stripe_md lsm;
1662 } *stripeinfo, *si, *si_last;
1663 struct lov_obd *lov;
1664 struct brw_page *ioarr;
1665 struct obdo *obdos = NULL;
1666 struct lov_oinfo *loi;
1667 struct lov_brw_async_args *aa;
1668 int rc = 0, i, *where, stripe_count = lsm->lsm_stripe_count;
1671 if (lsm_bad_magic(lsm))
1674 lov = &exp->exp_obd->u.lov;
1676 if (cmd == OBD_BRW_CHECK) {
1677 rc = lov_brw_check(lov, oa, lsm, oa_bufs, pga);
1681 OBD_ALLOC(stripeinfo, stripe_count * sizeof(*stripeinfo));
1685 OBD_ALLOC(where, sizeof(*where) * oa_bufs);
1687 GOTO(out_sinfo, rc = -ENOMEM);
1690 OBD_ALLOC(obdos, sizeof(*obdos) * stripe_count);
1692 GOTO(out_where, rc = -ENOMEM);
1695 OBD_ALLOC(ioarr, sizeof(*ioarr) * oa_bufs);
1697 GOTO(out_obdos, rc = -ENOMEM);
1699 for (i = 0; i < oa_bufs; i++) {
1700 where[i] = lov_stripe_number(lsm, pga[i].off);
1701 stripeinfo[where[i]].bufct++;
1704 for (i = 0, loi = lsm->lsm_oinfo, si_last = si = stripeinfo;
1705 i < stripe_count; i++, loi++, si_last = si, si++) {
1707 si->index = si_last->index + si_last->bufct;
1708 si->lsm.lsm_object_id = loi->loi_id;
1709 si->ost_idx = loi->loi_ost_idx;
1712 memcpy(&obdos[i], oa, sizeof(*obdos));
1713 obdos[i].o_id = si->lsm.lsm_object_id;
1717 for (i = 0; i < oa_bufs; i++) {
1718 int which = where[i];
1721 shift = stripeinfo[which].index + stripeinfo[which].subcount;
1722 LASSERT(shift < oa_bufs);
1723 ioarr[shift] = pga[i];
1724 lov_stripe_offset(lsm, pga[i].off, which, &ioarr[shift].off);
1725 stripeinfo[which].subcount++;
1728 for (i = 0, si = stripeinfo; i < stripe_count; i++, si++) {
1729 int shift = si->index;
1734 if (lov->tgts[si->ost_idx].active == 0) {
1735 CDEBUG(D_HA, "lov idx %d inactive\n", si->ost_idx);
1736 GOTO(out_ioarr, rc = -EIO);
1739 LASSERT(shift < oa_bufs);
1741 rc = obd_brw_async(cmd, lov->tgts[si->ost_idx].ltd_exp,
1742 &obdos[i], &si->lsm, si->bufct,
1743 &ioarr[shift], set, oti);
1745 GOTO(out_ioarr, rc);
1748 LASSERT(set->set_interpret == NULL);
1749 set->set_interpret = (set_interpreter_func)lov_brw_interpret;
1750 LASSERT(sizeof(set->set_args) >= sizeof(struct lov_brw_async_args));
1751 aa = (struct lov_brw_async_args *)&set->set_args;
1753 aa->aa_obdos = obdos;
1755 aa->aa_ioarr = ioarr;
1756 aa->aa_oa_bufs = oa_bufs;
1758 /* Don't free ioarr or obdos - that's done in lov_brw_interpret */
1759 GOTO(out_where, rc);
1762 OBD_FREE(ioarr, sizeof(*ioarr) * oa_bufs);
1764 OBD_FREE(obdos, stripe_count * sizeof(*obdos));
1766 OBD_FREE(where, sizeof(*where) * oa_bufs);
1768 OBD_FREE(stripeinfo, stripe_count * sizeof(*stripeinfo));
1772 struct lov_async_page *lap_from_cookie(void *cookie)
1774 struct lov_async_page *lap = cookie;
1775 if (lap->lap_magic != LAP_MAGIC)
1776 return ERR_PTR(-EINVAL);
1780 static int lov_ap_make_ready(void *data, int cmd)
1782 struct lov_async_page *lap = lap_from_cookie(data);
1783 /* XXX should these assert? */
1787 return lap->lap_caller_ops->ap_make_ready(lap->lap_caller_data, cmd);
1789 static int lov_ap_refresh_count(void *data, int cmd)
1791 struct lov_async_page *lap = lap_from_cookie(data);
1795 return lap->lap_caller_ops->ap_refresh_count(lap->lap_caller_data,
1798 static void lov_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
1800 struct lov_async_page *lap = lap_from_cookie(data);
1801 /* XXX should these assert? */
1805 lap->lap_caller_ops->ap_fill_obdo(lap->lap_caller_data, cmd, oa);
1806 /* XXX woah, shouldn't we be altering more here? size? */
1807 oa->o_id = lap->lap_loi_id;
1809 static void lov_ap_completion(void *data, int cmd, int rc)
1811 struct lov_async_page *lap = lap_from_cookie(data);
1815 /* in a raid1 regime this would down a count of many ios
1816 * in flight, onl calling the caller_ops completion when all
1817 * the raid1 ios are complete */
1818 lap->lap_caller_ops->ap_completion(lap->lap_caller_data, cmd, rc);
1821 static struct obd_async_page_ops lov_async_page_ops = {
1822 .ap_make_ready = lov_ap_make_ready,
1823 .ap_refresh_count = lov_ap_refresh_count,
1824 .ap_fill_obdo = lov_ap_fill_obdo,
1825 .ap_completion = lov_ap_completion,
1828 int lov_prep_async_page(struct obd_export *exp, struct lov_stripe_md *lsm,
1829 struct lov_oinfo *loi, struct page *page,
1830 obd_off offset, struct obd_async_page_ops *ops,
1831 void *data, void **res)
1833 struct lov_obd *lov = &exp->exp_obd->u.lov;
1834 struct lov_async_page *lap;
1838 if (lsm_bad_magic(lsm))
1840 LASSERT(loi == NULL);
1842 OBD_ALLOC(lap, sizeof(*lap));
1846 lap->lap_magic = LAP_MAGIC;
1847 lap->lap_caller_ops = ops;
1848 lap->lap_caller_data = data;
1850 /* for now only raid 0 which passes through */
1851 lap->lap_stripe = lov_stripe_number(lsm, offset);
1852 lov_stripe_offset(lsm, offset, lap->lap_stripe, &lap->lap_sub_offset);
1853 loi = &lsm->lsm_oinfo[lap->lap_stripe];
1855 /* so the callback doesn't need the lsm */
1856 lap->lap_loi_id = loi->loi_id;
1858 rc = obd_prep_async_page(lov->tgts[loi->loi_ost_idx].ltd_exp,
1859 lsm, loi, page, lap->lap_sub_offset,
1860 &lov_async_page_ops, lap,
1861 &lap->lap_sub_cookie);
1863 OBD_FREE(lap, sizeof(*lap));
1866 CDEBUG(D_CACHE, "lap %p page %p cookie %p off "LPU64"\n", lap, page,
1867 lap->lap_sub_cookie, offset);
1872 static int lov_queue_async_io(struct obd_export *exp,
1873 struct lov_stripe_md *lsm,
1874 struct lov_oinfo *loi, void *cookie,
1875 int cmd, obd_off off, int count,
1876 obd_flag brw_flags, obd_flag async_flags)
1878 struct lov_obd *lov = &exp->exp_obd->u.lov;
1879 struct lov_async_page *lap;
1882 LASSERT(loi == NULL);
1884 if (lsm_bad_magic(lsm))
1887 lap = lap_from_cookie(cookie);
1889 RETURN(PTR_ERR(lap));
1891 loi = &lsm->lsm_oinfo[lap->lap_stripe];
1892 rc = obd_queue_async_io(lov->tgts[loi->loi_ost_idx].ltd_exp, lsm,
1893 loi, lap->lap_sub_cookie, cmd, off, count,
1894 brw_flags, async_flags);
1898 static int lov_set_async_flags(struct obd_export *exp,
1899 struct lov_stripe_md *lsm,
1900 struct lov_oinfo *loi, void *cookie,
1901 obd_flag async_flags)
1903 struct lov_obd *lov = &exp->exp_obd->u.lov;
1904 struct lov_async_page *lap;
1907 LASSERT(loi == NULL);
1909 if (lsm_bad_magic(lsm))
1912 lap = lap_from_cookie(cookie);
1914 RETURN(PTR_ERR(lap));
1916 loi = &lsm->lsm_oinfo[lap->lap_stripe];
1917 rc = obd_set_async_flags(lov->tgts[loi->loi_ost_idx].ltd_exp,
1918 lsm, loi, lap->lap_sub_cookie, async_flags);
1922 static int lov_queue_group_io(struct obd_export *exp,
1923 struct lov_stripe_md *lsm,
1924 struct lov_oinfo *loi,
1925 struct obd_io_group *oig, void *cookie,
1926 int cmd, obd_off off, int count,
1927 obd_flag brw_flags, obd_flag async_flags)
1929 struct lov_obd *lov = &exp->exp_obd->u.lov;
1930 struct lov_async_page *lap;
1933 LASSERT(loi == NULL);
1935 if (lsm_bad_magic(lsm))
1938 lap = lap_from_cookie(cookie);
1940 RETURN(PTR_ERR(lap));
1942 loi = &lsm->lsm_oinfo[lap->lap_stripe];
1943 rc = obd_queue_group_io(lov->tgts[loi->loi_ost_idx].ltd_exp, lsm, loi,
1944 oig, lap->lap_sub_cookie, cmd, off, count,
1945 brw_flags, async_flags);
1949 /* this isn't exactly optimal. we may have queued sync io in oscs on
1950 * all stripes, but we don't record that fact at queue time. so we
1951 * trigger sync io on all stripes. */
1952 static int lov_trigger_group_io(struct obd_export *exp,
1953 struct lov_stripe_md *lsm,
1954 struct lov_oinfo *loi,
1955 struct obd_io_group *oig)
1957 struct lov_obd *lov = &exp->exp_obd->u.lov;
1960 LASSERT(loi == NULL);
1962 if (lsm_bad_magic(lsm))
1965 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
1967 err = obd_trigger_group_io(lov->tgts[loi->loi_ost_idx].ltd_exp,
1969 if (rc == 0 && err != 0)
1975 static int lov_teardown_async_page(struct obd_export *exp,
1976 struct lov_stripe_md *lsm,
1977 struct lov_oinfo *loi, void *cookie)
1979 struct lov_obd *lov = &exp->exp_obd->u.lov;
1980 struct lov_async_page *lap;
1983 LASSERT(loi == NULL);
1985 if (lsm_bad_magic(lsm))
1988 lap = lap_from_cookie(cookie);
1990 RETURN(PTR_ERR(lap));
1992 loi = &lsm->lsm_oinfo[lap->lap_stripe];
1993 rc = obd_teardown_async_page(lov->tgts[loi->loi_ost_idx].ltd_exp,
1994 lsm, loi, lap->lap_sub_cookie);
1996 CERROR("unable to teardown sub cookie %p: %d\n",
1997 lap->lap_sub_cookie, rc);
2000 OBD_FREE(lap, sizeof(*lap));
2004 static int lov_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
2005 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
2006 int *flags, void *bl_cb, void *cp_cb, void *gl_cb,
2007 void *data,__u32 lvb_len, void *lvb_swabber,
2008 struct lustre_handle *lockh)
2010 struct lov_lock_handles *lov_lockh = NULL;
2011 struct lustre_handle *lov_lockhp;
2012 struct lov_obd *lov;
2013 struct lov_oinfo *loi;
2014 char submd_buf[sizeof(struct lov_stripe_md) + sizeof(struct lov_oinfo)];
2015 struct lov_stripe_md *submd = (void *)submd_buf;
2017 int i, save_flags = *flags;
2020 if (lsm_bad_magic(lsm))
2023 /* we should never be asked to replay a lock this way. */
2024 LASSERT((*flags & LDLM_FL_REPLAY) == 0);
2026 if (!exp || !exp->exp_obd)
2029 if (lsm->lsm_stripe_count > 1) {
2030 lov_lockh = lov_llh_new(lsm);
2031 if (lov_lockh == NULL)
2034 lockh->cookie = lov_lockh->llh_handle.h_cookie;
2035 lov_lockhp = lov_lockh->llh_handles;
2040 lov = &exp->exp_obd->u.lov;
2041 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2042 i++, loi++, lov_lockhp++) {
2043 ldlm_policy_data_t sub_ext;
2045 if (!lov_stripe_intersects(lsm, i, policy->l_extent.start,
2046 policy->l_extent.end,
2047 &sub_ext.l_extent.start,
2048 &sub_ext.l_extent.end))
2051 if (lov->tgts[loi->loi_ost_idx].active == 0) {
2052 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2056 /* XXX LOV STACKING: submd should be from the subobj */
2057 submd->lsm_object_id = loi->loi_id;
2058 submd->lsm_stripe_count = 0;
2059 submd->lsm_oinfo->loi_kms_valid = loi->loi_kms_valid;
2060 submd->lsm_oinfo->loi_rss = loi->loi_rss;
2061 submd->lsm_oinfo->loi_kms = loi->loi_kms;
2062 loi->loi_mtime = submd->lsm_oinfo->loi_mtime;
2063 /* XXX submd is not fully initialized here */
2064 *flags = save_flags;
2065 rc = obd_enqueue(lov->tgts[loi->loi_ost_idx].ltd_exp, submd,
2066 type, &sub_ext, mode, flags, bl_cb, cp_cb,
2067 gl_cb, data, lvb_len, lvb_swabber, lov_lockhp);
2069 /* XXX FIXME: This unpleasantness doesn't belong here at *all*.
2070 * It belongs in the OSC, except that the OSC doesn't have
2071 * access to the real LOI -- it gets a copy, that we created
2072 * above, and that copy can be arbitrarily out of date.
2074 * The LOV API is due for a serious rewriting anyways, and this
2075 * can be addressed then. */
2076 if (rc == ELDLM_OK) {
2077 struct ldlm_lock *lock = ldlm_handle2lock(lov_lockhp);
2078 __u64 tmp = submd->lsm_oinfo->loi_rss;
2080 LASSERT(lock != NULL);
2082 /* Extend KMS up to the end of this lock and no further
2083 * A lock on [x,y] means a KMS of up to y + 1 bytes! */
2084 if (tmp > lock->l_policy_data.l_extent.end)
2085 tmp = lock->l_policy_data.l_extent.end + 1;
2086 if (tmp >= loi->loi_kms) {
2087 CDEBUG(D_INODE, "lock acquired, setting rss="
2088 LPU64", kms="LPU64"\n", loi->loi_rss,
2091 loi->loi_kms_valid = 1;
2093 CDEBUG(D_INODE, "lock acquired, setting rss="
2094 LPU64"; leaving kms="LPU64", end="LPU64
2095 "\n", loi->loi_rss, loi->loi_kms,
2096 lock->l_policy_data.l_extent.end);
2098 ldlm_lock_allow_match(lock);
2099 LDLM_LOCK_PUT(lock);
2100 } else if (rc == ELDLM_LOCK_ABORTED &&
2101 save_flags & LDLM_FL_HAS_INTENT) {
2102 memset(lov_lockhp, 0, sizeof(*lov_lockhp));
2103 loi->loi_rss = submd->lsm_oinfo->loi_rss;
2104 CDEBUG(D_INODE, "glimpsed, setting rss="LPU64"; leaving"
2105 " kms="LPU64"\n", loi->loi_rss, loi->loi_kms);
2107 memset(lov_lockhp, 0, sizeof(*lov_lockhp));
2108 if (lov->tgts[loi->loi_ost_idx].active) {
2109 CERROR("error: enqueue objid "LPX64" subobj "
2110 LPX64" on OST idx %d: rc = %d\n",
2111 lsm->lsm_object_id, loi->loi_id,
2112 loi->loi_ost_idx, rc);
2113 GOTO(out_locks, rc);
2117 if (lsm->lsm_stripe_count > 1)
2118 lov_llh_put(lov_lockh);
2122 while (loi--, lov_lockhp--, i-- > 0) {
2123 struct lov_stripe_md submd;
2126 if (lov_lockhp->cookie == 0)
2129 /* XXX LOV STACKING: submd should be from the subobj */
2130 submd.lsm_object_id = loi->loi_id;
2131 submd.lsm_stripe_count = 0;
2132 err = obd_cancel(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd,
2134 if (err && lov->tgts[loi->loi_ost_idx].active) {
2135 CERROR("error: cancelling objid "LPX64" on OST "
2136 "idx %d after enqueue error: rc = %d\n",
2137 loi->loi_id, loi->loi_ost_idx, err);
2141 if (lsm->lsm_stripe_count > 1) {
2142 lov_llh_destroy(lov_lockh);
2143 lov_llh_put(lov_lockh);
2148 static int lov_match(struct obd_export *exp, struct lov_stripe_md *lsm,
2149 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
2150 int *flags, void *data, struct lustre_handle *lockh)
2152 struct lov_lock_handles *lov_lockh = NULL;
2153 struct lustre_handle *lov_lockhp;
2154 struct lov_obd *lov;
2155 struct lov_oinfo *loi;
2156 struct lov_stripe_md submd;
2157 ldlm_error_t rc = 0;
2161 if (lsm_bad_magic(lsm))
2164 if (!exp || !exp->exp_obd)
2167 if (lsm->lsm_stripe_count > 1) {
2168 lov_lockh = lov_llh_new(lsm);
2169 if (lov_lockh == NULL)
2172 lockh->cookie = lov_lockh->llh_handle.h_cookie;
2173 lov_lockhp = lov_lockh->llh_handles;
2178 lov = &exp->exp_obd->u.lov;
2179 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2180 i++, loi++, lov_lockhp++) {
2181 ldlm_policy_data_t sub_ext;
2184 if (!lov_stripe_intersects(lsm, i, policy->l_extent.start,
2185 policy->l_extent.end,
2186 &sub_ext.l_extent.start,
2187 &sub_ext.l_extent.end))
2190 if (lov->tgts[loi->loi_ost_idx].active == 0) {
2191 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2196 /* XXX LOV STACKING: submd should be from the subobj */
2197 submd.lsm_object_id = loi->loi_id;
2198 submd.lsm_stripe_count = 0;
2200 /* XXX submd is not fully initialized here */
2201 rc = obd_match(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd,
2202 type, &sub_ext, mode, &lov_flags, data,
2208 if (lsm->lsm_stripe_count > 1) {
2209 if (*flags & LDLM_FL_TEST_LOCK)
2210 lov_llh_destroy(lov_lockh);
2211 lov_llh_put(lov_lockh);
2216 while (loi--, lov_lockhp--, i-- > 0) {
2217 struct lov_stripe_md submd;
2220 if (lov_lockhp->cookie == 0)
2223 /* XXX LOV STACKING: submd should be from the subobj */
2224 submd.lsm_object_id = loi->loi_id;
2225 submd.lsm_stripe_count = 0;
2226 err = obd_cancel(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd,
2228 if (err && lov->tgts[loi->loi_ost_idx].active) {
2229 CERROR("error: cancelling objid "LPX64" on OST "
2230 "idx %d after match failure: rc = %d\n",
2231 loi->loi_id, loi->loi_ost_idx, err);
2235 if (lsm->lsm_stripe_count > 1) {
2236 lov_llh_destroy(lov_lockh);
2237 lov_llh_put(lov_lockh);
2242 static int lov_change_cbdata(struct obd_export *exp,
2243 struct lov_stripe_md *lsm, ldlm_iterator_t it,
2246 struct lov_obd *lov;
2247 struct lov_oinfo *loi;
2251 if (lsm_bad_magic(lsm))
2254 if (!exp || !exp->exp_obd)
2257 lov = &exp->exp_obd->u.lov;
2258 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
2259 struct lov_stripe_md submd;
2260 if (lov->tgts[loi->loi_ost_idx].active == 0)
2261 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2263 submd.lsm_object_id = loi->loi_id;
2264 submd.lsm_stripe_count = 0;
2265 rc = obd_change_cbdata(lov->tgts[loi->loi_ost_idx].ltd_exp,
2271 static int lov_cancel(struct obd_export *exp, struct lov_stripe_md *lsm,
2272 __u32 mode, struct lustre_handle *lockh)
2274 struct lov_lock_handles *lov_lockh = NULL;
2275 struct lustre_handle *lov_lockhp;
2276 struct lov_obd *lov;
2277 struct lov_oinfo *loi;
2281 if (lsm_bad_magic(lsm))
2284 if (!exp || !exp->exp_obd)
2288 if (lsm->lsm_stripe_count > 1) {
2289 lov_lockh = lov_handle2llh(lockh);
2291 CERROR("LOV: invalid lov lock handle %p\n", lockh);
2295 lov_lockhp = lov_lockh->llh_handles;
2300 lov = &exp->exp_obd->u.lov;
2301 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2302 i++, loi++, lov_lockhp++) {
2303 struct lov_stripe_md submd;
2306 if (lov_lockhp->cookie == 0) {
2307 CDEBUG(D_HA, "lov idx %d subobj "LPX64" no lock?\n",
2308 loi->loi_ost_idx, loi->loi_id);
2312 /* XXX LOV STACKING: submd should be from the subobj */
2313 submd.lsm_object_id = loi->loi_id;
2314 submd.lsm_stripe_count = 0;
2315 err = obd_cancel(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd,
2318 if (lov->tgts[loi->loi_ost_idx].active) {
2319 CERROR("error: cancel objid "LPX64" subobj "
2320 LPX64" on OST idx %d: rc = %d\n",
2322 loi->loi_id, loi->loi_ost_idx, err);
2329 if (lsm->lsm_stripe_count > 1)
2330 lov_llh_destroy(lov_lockh);
2331 if (lov_lockh != NULL)
2332 lov_llh_put(lov_lockh);
2336 static int lov_cancel_unused(struct obd_export *exp,
2337 struct lov_stripe_md *lsm, int flags, void *opaque)
2339 struct lov_obd *lov;
2340 struct lov_oinfo *loi;
2344 if (lsm_bad_magic(lsm))
2347 if (!exp || !exp->exp_obd)
2350 lov = &exp->exp_obd->u.lov;
2351 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
2352 struct lov_stripe_md submd;
2355 if (lov->tgts[loi->loi_ost_idx].active == 0)
2356 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2358 submd.lsm_object_id = loi->loi_id;
2359 submd.lsm_stripe_count = 0;
2360 err = obd_cancel_unused(lov->tgts[loi->loi_ost_idx].ltd_exp,
2361 &submd, flags, opaque);
2362 if (err && lov->tgts[loi->loi_ost_idx].active) {
2363 CERROR("error: cancel unused objid "LPX64" subobj "LPX64
2364 " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
2365 loi->loi_id, loi->loi_ost_idx, err);
2373 #define LOV_U64_MAX ((__u64)~0ULL)
2374 #define LOV_SUM_MAX(tot, add) \
2376 if ((tot) + (add) < (tot)) \
2377 (tot) = LOV_U64_MAX; \
2382 static int lov_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2383 unsigned long max_age)
2385 struct lov_obd *lov = &obd->u.lov;
2386 struct obd_statfs lov_sfs;
2393 /* We only get block data from the OBD */
2394 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2397 if (!lov->tgts[i].active) {
2398 CDEBUG(D_HA, "lov idx %d inactive\n", i);
2402 err = obd_statfs(class_exp2obd(lov->tgts[i].ltd_exp), &lov_sfs,
2405 if (lov->tgts[i].active && !rc)
2411 memcpy(osfs, &lov_sfs, sizeof(lov_sfs));
2414 osfs->os_bfree += lov_sfs.os_bfree;
2415 osfs->os_bavail += lov_sfs.os_bavail;
2416 osfs->os_blocks += lov_sfs.os_blocks;
2417 /* XXX not sure about this one - depends on policy.
2418 * - could be minimum if we always stripe on all OBDs
2419 * (but that would be wrong for any other policy,
2420 * if one of the OBDs has no more objects left)
2421 * - could be sum if we stripe whole objects
2422 * - could be average, just to give a nice number
2424 * To give a "reasonable" (if not wholly accurate)
2425 * number, we divide the total number of free objects
2426 * by expected stripe count (watch out for overflow).
2428 LOV_SUM_MAX(osfs->os_files, lov_sfs.os_files);
2429 LOV_SUM_MAX(osfs->os_ffree, lov_sfs.os_ffree);
2434 __u32 expected_stripes = lov->desc.ld_default_stripe_count ?
2435 lov->desc.ld_default_stripe_count :
2436 lov->desc.ld_active_tgt_count;
2438 if (osfs->os_files != LOV_U64_MAX)
2439 do_div(osfs->os_files, expected_stripes);
2440 if (osfs->os_ffree != LOV_U64_MAX)
2441 do_div(osfs->os_ffree, expected_stripes);
2448 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2449 void *karg, void *uarg)
2451 struct obd_device *obddev = class_exp2obd(exp);
2452 struct lov_obd *lov = &obddev->u.lov;
2453 int i, count = lov->desc.ld_tgt_count;
2454 struct obd_uuid *uuidp;
2460 case OBD_IOC_LOV_GET_CONFIG: {
2461 struct obd_ioctl_data *data = karg;
2462 struct lov_tgt_desc *tgtdesc;
2463 struct lov_desc *desc;
2468 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2471 data = (struct obd_ioctl_data *)buf;
2473 if (sizeof(*desc) > data->ioc_inllen1) {
2478 if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
2483 desc = (struct lov_desc *)data->ioc_inlbuf1;
2484 memcpy(desc, &(lov->desc), sizeof(*desc));
2486 uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
2487 tgtdesc = lov->tgts;
2488 for (i = 0; i < count; i++, uuidp++, tgtdesc++)
2489 obd_str2uuid(uuidp, tgtdesc->uuid.uuid);
2491 rc = copy_to_user((void *)uarg, buf, len);
2494 obd_ioctl_freedata(buf, len);
2497 case LL_IOC_LOV_SETSTRIPE:
2498 rc = lov_setstripe(exp, karg, uarg);
2500 case LL_IOC_LOV_GETSTRIPE:
2501 rc = lov_getstripe(exp, karg, uarg);
2503 case LL_IOC_LOV_SETEA:
2504 rc = lov_setea(exp, karg, uarg);
2511 for (i = 0; i < count; i++) {
2514 err = obd_iocontrol(cmd, lov->tgts[i].ltd_exp,
2517 if (lov->tgts[i].active) {
2518 CERROR("error: iocontrol OSC %s on OST"
2519 "idx %d: err = %d\n",
2520 lov->tgts[i].uuid.uuid, i, err);
2535 static int lov_get_info(struct obd_export *exp, __u32 keylen,
2536 void *key, __u32 *vallen, void *val)
2538 struct obd_device *obddev = class_exp2obd(exp);
2539 struct lov_obd *lov = &obddev->u.lov;
2543 if (!vallen || !val)
2546 if (keylen > strlen("lock_to_stripe") &&
2547 strcmp(key, "lock_to_stripe") == 0) {
2550 struct ldlm_lock *lock;
2551 struct lov_stripe_md *lsm;
2553 struct lov_oinfo *loi;
2554 __u32 *stripe = val;
2556 if (*vallen < sizeof(*stripe))
2558 *vallen = sizeof(*stripe);
2560 /* XXX This is another one of those bits that will need to
2561 * change if we ever actually support nested LOVs. It uses
2562 * the lock's export to find out which stripe it is. */
2563 for (i = 0, loi = data->lsm->lsm_oinfo;
2564 i < data->lsm->lsm_stripe_count;
2566 if (lov->tgts[loi->loi_ost_idx].ltd_exp ==
2567 data->lock->l_conn_export) {
2573 } else if (keylen >= strlen("size_to_stripe") &&
2574 strcmp(key, "size_to_stripe") == 0) {
2578 struct lov_stripe_md *lsm;
2581 if (*vallen < sizeof(*data))
2584 data->size = lov_size_to_stripe(data->lsm, data->size,
2585 data->stripe_number);
2587 } else if (keylen >= strlen("last_id") && strcmp(key, "last_id") == 0) {
2589 int rc, size = sizeof(obd_id);
2590 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2591 if (!lov->tgts[i].active)
2593 rc = obd_get_info(lov->tgts[i].ltd_exp, keylen, key,
2599 } else if (keylen >= strlen("lovdesc") && strcmp(key, "lovdesc") == 0) {
2600 struct lov_desc *desc_ret = val;
2601 *desc_ret = lov->desc;
2609 static int lov_set_info(struct obd_export *exp, obd_count keylen,
2610 void *key, obd_count vallen, void *val)
2612 struct obd_device *obddev = class_exp2obd(exp);
2613 struct lov_obd *lov = &obddev->u.lov;
2617 #define KEY_IS(str) \
2618 (keylen == strlen(str) && memcmp(key, str, keylen) == 0)
2620 if (KEY_IS("next_id")) {
2621 if (vallen != lov->desc.ld_tgt_count)
2623 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2626 /* initialize all OSCs, even inactive ones */
2628 er = obd_set_info(lov->tgts[i].ltd_exp, keylen, key,
2629 sizeof(obd_id), ((obd_id*)val) + i);
2636 if (KEY_IS("growth_count")) {
2637 if (vallen != sizeof(int))
2639 } else if (KEY_IS("mds_conn") || KEY_IS("unlinked")) {
2646 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2649 if (val && !obd_uuid_equals(val, &lov->tgts[i].uuid))
2652 if (!val && !lov->tgts[i].active)
2655 er = obd_set_info(lov->tgts[i].ltd_exp, keylen, key, vallen,
2665 /* Merge rss if kms == 0
2667 * Even when merging RSS, we will take the KMS value if it's larger.
2668 * This prevents getattr from stomping on dirty cached pages which
2669 * extend the file size. */
2670 __u64 lov_merge_size(struct lov_stripe_md *lsm, int kms)
2672 struct lov_oinfo *loi;
2676 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2678 obd_size lov_size, tmpsize;
2680 tmpsize = loi->loi_kms;
2681 if (kms == 0 && loi->loi_rss > tmpsize)
2682 tmpsize = loi->loi_rss;
2684 lov_size = lov_stripe_size(lsm, tmpsize, i);
2685 if (lov_size > size)
2690 EXPORT_SYMBOL(lov_merge_size);
2692 __u64 lov_merge_mtime(struct lov_stripe_md *lsm, __u64 current_time)
2694 struct lov_oinfo *loi;
2697 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2699 if (loi->loi_mtime > current_time)
2700 current_time = loi->loi_mtime;
2702 return current_time;
2704 EXPORT_SYMBOL(lov_merge_mtime);
2707 struct lov_multi_wait {
2708 struct ldlm_lock *lock;
2714 int lov_complete_many(struct obd_export *exp, struct lov_stripe_md *lsm,
2715 struct lustre_handle *lockh)
2717 struct lov_lock_handles *lov_lockh = NULL;
2718 struct lustre_handle *lov_lockhp;
2719 struct lov_obd *lov;
2720 struct lov_oinfo *loi;
2721 struct lov_multi_wait *queues;
2725 if (lsm_bad_magic(lsm))
2728 if (!exp || !exp->exp_obd)
2731 LASSERT(lockh != NULL);
2732 if (lsm->lsm_stripe_count > 1) {
2733 lov_lockh = lov_handle2llh(lockh);
2734 if (lov_lockh == NULL) {
2735 CERROR("LOV: invalid lov lock handle %p\n", lockh);
2739 lov_lockhp = lov_lockh->llh_handles;
2744 OBD_ALLOC(queues, lsm->lsm_stripe_count * sizeof(*queues));
2746 GOTO(out, rc = -ENOMEM);
2748 lov = &exp->exp_obd->u.lov;
2749 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2750 i++, loi++, lov_lockhp++) {
2751 struct ldlm_lock *lock;
2752 struct obd_device *obd;
2753 unsigned long irqflags;
2755 lock = ldlm_handle2lock(lov_lockhp);
2757 CDEBUG(D_HA, "lov idx %d subobj "LPX64" no lock?\n",
2758 loi->loi_ost_idx, loi->loi_id);
2759 queues[i].completed = 1;
2763 queues[i].lock = lock;
2764 init_waitqueue_entry(&(queues[i].wait), current);
2765 add_wait_queue(lock->l_waitq, &(queues[i].wait));
2767 obd = class_exp2obd(lock->l_conn_export);
2769 imp = obd->u.cli.cl_import;
2771 spin_lock_irqsave(&imp->imp_lock, irqflags);
2772 queues[i].generation = imp->imp_generation;
2773 spin_unlock_irqrestore(&imp->imp_lock, irqflags);
2777 lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, ldlm_expired_completion_wait,
2778 interrupted_completion_wait, &lwd);
2779 rc = l_wait_event_added(check_multi_complete(queues, lsm), &lwi);
2781 for (i = 0; i < lsm->lsm_stripe_count; i++)
2782 remove_wait_queue(lock->l_waitq, &(queues[i].wait));
2784 if (rc == -EINTR || rc == -ETIMEDOUT) {
2790 if (lov_lockh != NULL)
2791 lov_llh_put(lov_lockh);
2796 void lov_increase_kms(struct obd_export *exp, struct lov_stripe_md *lsm,
2799 struct lov_oinfo *loi;
2805 stripe = lov_stripe_number(lsm, size - 1);
2806 kms = lov_size_to_stripe(lsm, size, stripe);
2807 loi = &(lsm->lsm_oinfo[stripe]);
2809 CDEBUG(D_INODE, "stripe %d KMS %sincreasing "LPU64"->"LPU64"\n",
2810 stripe, kms > loi->loi_kms ? "" : "not ", loi->loi_kms, kms);
2811 if (kms > loi->loi_kms)
2815 EXPORT_SYMBOL(lov_increase_kms);
2817 struct obd_ops lov_obd_ops = {
2818 o_owner: THIS_MODULE,
2819 o_attach: lov_attach,
2820 o_detach: lov_detach,
2822 o_cleanup: lov_cleanup,
2823 o_connect: lov_connect,
2824 o_disconnect: lov_disconnect,
2825 o_statfs: lov_statfs,
2826 o_packmd: lov_packmd,
2827 o_unpackmd: lov_unpackmd,
2828 o_create: lov_create,
2829 o_destroy: lov_destroy,
2830 o_getattr: lov_getattr,
2831 o_getattr_async: lov_getattr_async,
2832 o_setattr: lov_setattr,
2834 o_brw_async: lov_brw_async,
2835 .o_prep_async_page = lov_prep_async_page,
2836 .o_queue_async_io = lov_queue_async_io,
2837 .o_set_async_flags = lov_set_async_flags,
2838 .o_queue_group_io = lov_queue_group_io,
2839 .o_trigger_group_io = lov_trigger_group_io,
2840 .o_teardown_async_page lov_teardown_async_page,
2843 o_enqueue: lov_enqueue,
2845 o_change_cbdata: lov_change_cbdata,
2846 o_cancel: lov_cancel,
2847 o_cancel_unused: lov_cancel_unused,
2848 o_iocontrol: lov_iocontrol,
2849 o_get_info: lov_get_info,
2850 o_set_info: lov_set_info,
2851 o_llog_init: lov_llog_init,
2852 o_llog_finish: lov_llog_finish,
2853 o_notify: lov_notify,
2856 int __init lov_init(void)
2858 struct lprocfs_static_vars lvars;
2861 lprocfs_init_vars(lov, &lvars);
2862 rc = class_register_type(&lov_obd_ops, lvars.module_vars,
2863 OBD_LOV_DEVICENAME);
2868 static void /*__exit*/ lov_exit(void)
2870 class_unregister_type(OBD_LOV_DEVICENAME);
2873 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2874 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver");
2875 MODULE_LICENSE("GPL");
2877 module_init(lov_init);
2878 module_exit(lov_exit);