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 static int lov_connect(struct lustre_handle *conn, struct obd_device *obd,
118 struct obd_uuid *cluuid)
120 struct ptlrpc_request *req = NULL;
121 struct lov_obd *lov = &obd->u.lov;
122 struct lov_desc *desc = &lov->desc;
123 struct lov_tgt_desc *tgts;
124 struct obd_export *exp;
128 rc = class_connect(conn, obd, cluuid);
132 exp = class_conn2export(conn);
134 /* We don't want to actually do the underlying connections more than
135 * once, so keep track. */
137 if (lov->refcount > 1) {
138 class_export_put(exp);
142 for (i = 0, tgts = lov->tgts; i < desc->ld_tgt_count; i++, tgts++) {
143 struct obd_uuid *tgt_uuid = &tgts->uuid;
144 struct obd_device *tgt_obd;
145 struct obd_uuid lov_osc_uuid = { "LOV_OSC_UUID" };
146 struct lustre_handle conn = {0, };
148 LASSERT( tgt_uuid != NULL);
150 tgt_obd = class_find_client_obd(tgt_uuid, LUSTRE_OSC_NAME,
154 CERROR("Target %s not attached\n", tgt_uuid->uuid);
155 GOTO(out_disc, rc = -EINVAL);
158 if (!tgt_obd->obd_set_up) {
159 CERROR("Target %s not set up\n", tgt_uuid->uuid);
160 GOTO(out_disc, rc = -EINVAL);
163 if (tgt_obd->u.cli.cl_import->imp_invalid) {
164 CERROR("not connecting OSC %s; administratively "
165 "disabled\n", tgt_uuid->uuid);
166 rc = obd_register_observer(tgt_obd, obd);
168 CERROR("Target %s register_observer error %d; "
169 "will not be able to reactivate\n",
175 rc = obd_connect(&conn, tgt_obd, &lov_osc_uuid);
177 CERROR("Target %s connect error %d\n", tgt_uuid->uuid,
181 tgts->ltd_exp = class_conn2export(&conn);
183 rc = obd_register_observer(tgt_obd, obd);
185 CERROR("Target %s register_observer error %d\n",
187 obd_disconnect(tgts->ltd_exp, 0);
191 desc->ld_active_tgt_count++;
195 ptlrpc_req_finished(req);
196 class_export_put(exp);
201 struct obd_uuid uuid;
203 --desc->ld_active_tgt_count;
205 /* save for CERROR below; (we know it's terminated) */
207 rc2 = obd_disconnect(tgts->ltd_exp, 0);
209 CERROR("error: LOV target %s disconnect on OST idx %d: "
210 "rc = %d\n", uuid.uuid, i, rc2);
212 class_disconnect(exp, 0);
216 static int lov_disconnect(struct obd_export *exp, int flags)
218 struct obd_device *obd = class_exp2obd(exp);
219 struct lov_obd *lov = &obd->u.lov;
226 /* Only disconnect the underlying layers on the final disconnect. */
228 if (lov->refcount != 0)
231 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
232 if (lov->tgts[i].ltd_exp == NULL)
235 if (obd->obd_no_recov) {
236 /* Pass it on to our clients.
237 * XXX This should be an argument to disconnect,
238 * XXX not a back-door flag on the OBD. Ah well.
240 struct obd_device *osc_obd;
241 osc_obd = class_exp2obd(lov->tgts[i].ltd_exp);
243 osc_obd->obd_no_recov = 1;
246 obd_register_observer(lov->tgts[i].ltd_exp->exp_obd, NULL);
248 rc = obd_disconnect(lov->tgts[i].ltd_exp, flags);
250 if (lov->tgts[i].active) {
251 CERROR("Target %s disconnect error %d\n",
252 lov->tgts[i].uuid.uuid, rc);
256 if (lov->tgts[i].active) {
257 lov->desc.ld_active_tgt_count--;
258 lov->tgts[i].active = 0;
260 lov->tgts[i].ltd_exp = NULL;
264 rc = class_disconnect(exp, 0);
270 * -EINVAL : UUID can't be found in the LOV's target list
271 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
272 * -EBADF : The UUID is found, but the OBD is the wrong type (!)
274 static int lov_set_osc_active(struct lov_obd *lov, struct obd_uuid *uuid,
277 struct obd_device *obd;
278 struct lov_tgt_desc *tgt;
282 CDEBUG(D_INFO, "Searching in lov %p for uuid %s (activate=%d)\n",
283 lov, uuid->uuid, activate);
285 spin_lock(&lov->lov_lock);
286 for (i = 0, tgt = lov->tgts; i < lov->desc.ld_tgt_count; i++, tgt++) {
287 CDEBUG(D_INFO, "lov idx %d is %s conn "LPX64"\n",
288 i, tgt->uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
289 if (strncmp(uuid->uuid, tgt->uuid.uuid, sizeof uuid->uuid) == 0)
293 if (i == lov->desc.ld_tgt_count)
294 GOTO(out, rc = -EINVAL);
296 obd = class_exp2obd(tgt->ltd_exp);
298 /* This can happen if OST failure races with node shutdown */
299 GOTO(out, rc = -ENOTCONN);
302 CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LOV idx %d\n",
303 obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
304 obd->obd_type->typ_name, i);
305 LASSERT(strcmp(obd->obd_type->typ_name, "osc") == 0);
307 if (tgt->active == activate) {
308 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
309 activate ? "" : "in");
313 CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd, activate ? "" : "in");
315 tgt->active = activate;
317 lov->desc.ld_active_tgt_count++;
319 lov->desc.ld_active_tgt_count--;
323 spin_unlock(&lov->lov_lock);
327 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
331 struct obd_uuid *uuid;
333 if (strcmp(watched->obd_type->typ_name, "osc")) {
334 CERROR("unexpected notification of %s %s!\n",
335 watched->obd_type->typ_name,
339 uuid = &watched->u.cli.cl_import->imp_target_uuid;
342 * Must notify (MDS) before we mark the OSC as active, so that
343 * the orphan deletion happens without interference from racing
346 if (obd->obd_observer) {
347 /* Pass the notification up the chain. */
348 rc = obd_notify(obd->obd_observer, watched, active);
353 rc = lov_set_osc_active(&obd->u.lov, uuid, active);
356 CERROR("%sactivation of %s failed: %d\n",
357 active ? "" : "de", uuid->uuid, rc);
362 static int lov_setup(struct obd_device *obd, obd_count len, void *buf)
364 struct lprocfs_static_vars lvars;
365 struct lustre_cfg *lcfg = buf;
366 struct lov_desc *desc;
367 struct lov_obd *lov = &obd->u.lov;
368 struct obd_uuid *uuids;
369 struct lov_tgt_desc *tgts;
374 if (lcfg->lcfg_inllen1 < 1) {
375 CERROR("LOV setup requires a descriptor\n");
379 if (lcfg->lcfg_inllen2 < 1) {
380 CERROR("LOV setup requires an OST UUID list\n");
384 desc = (struct lov_desc *)lcfg->lcfg_inlbuf1;
385 if (sizeof(*desc) > lcfg->lcfg_inllen1) {
386 CERROR("descriptor size wrong: %d > %d\n",
387 (int)sizeof(*desc), lcfg->lcfg_inllen1);
391 count = desc->ld_tgt_count;
392 uuids = (struct obd_uuid *)lcfg->lcfg_inlbuf2;
393 if (sizeof(*uuids) * count != lcfg->lcfg_inllen2) {
394 CERROR("UUID array size wrong: %u * %u != %u\n",
395 (int)sizeof(*uuids), count, lcfg->lcfg_inllen2);
399 if (desc->ld_default_stripe_size < PTLRPC_MAX_BRW_SIZE) {
400 CWARN("Increasing default_stripe_size "LPU64" to %u\n",
401 desc->ld_default_stripe_size, PTLRPC_MAX_BRW_SIZE);
402 CWARN("Please update config and run --write-conf on MDS\n");
404 desc->ld_default_stripe_size = PTLRPC_MAX_BRW_SIZE;
407 /* Because of 64-bit divide/mod operations only work with a 32-bit
408 * divisor in a 32-bit kernel, we cannot support a stripe width
409 * of 4GB or larger on 32-bit CPUs.
411 if ((desc->ld_default_stripe_count ?
412 desc->ld_default_stripe_count : desc->ld_tgt_count) *
413 desc->ld_default_stripe_size > ~0UL) {
414 CERROR("LOV: stripe width "LPU64"x%u > %lu on 32-bit system\n",
415 desc->ld_default_stripe_size,
416 desc->ld_default_stripe_count ?
417 desc->ld_default_stripe_count : desc->ld_tgt_count,~0UL);
421 lov->bufsize = sizeof(struct lov_tgt_desc) * count;
422 OBD_ALLOC(lov->tgts, lov->bufsize);
423 if (lov->tgts == NULL) {
424 CERROR("Out of memory\n");
429 spin_lock_init(&lov->lov_lock);
431 for (i = 0, tgts = lov->tgts; i < desc->ld_tgt_count; i++, tgts++) {
432 struct obd_uuid *uuid = &tgts->uuid;
434 /* NULL termination already checked */
438 lprocfs_init_vars(lov, &lvars);
439 lprocfs_obd_setup(obd, lvars.obd_vars);
442 struct proc_dir_entry *entry;
444 entry = create_proc_entry("target_obd", 0444,
445 obd->obd_proc_entry);
447 entry->proc_fops = &lov_proc_target_fops;
456 static int lov_cleanup(struct obd_device *obd, int flags)
458 struct lov_obd *lov = &obd->u.lov;
460 lprocfs_obd_cleanup(obd);
461 OBD_FREE(lov->tgts, lov->bufsize);
467 /* compute object size given "stripeno" and the ost size */
468 static obd_size lov_stripe_size(struct lov_stripe_md *lsm, obd_size ost_size,
471 unsigned long ssize = lsm->lsm_stripe_size;
472 unsigned long swidth = ssize * lsm->lsm_stripe_count;
473 unsigned long stripe_size;
479 /* do_div(a, b) returns a % b, and a = a / b */
480 stripe_size = do_div(ost_size, ssize);
482 lov_size = ost_size * swidth + stripeno * ssize + stripe_size;
484 lov_size = (ost_size - 1) * swidth + (stripeno + 1) * ssize;
489 static void lov_merge_attrs(struct obdo *tgt, struct obdo *src, obd_flag valid,
490 struct lov_stripe_md *lsm, int stripeno, int *set)
492 valid &= src->o_valid;
495 if (valid & OBD_MD_FLSIZE) {
496 /* this handles sparse files properly */
499 lov_size = lov_stripe_size(lsm, src->o_size, stripeno);
500 if (lov_size > tgt->o_size)
501 tgt->o_size = lov_size;
503 if (valid & OBD_MD_FLBLOCKS)
504 tgt->o_blocks += src->o_blocks;
505 if (valid & OBD_MD_FLBLKSZ)
506 tgt->o_blksize += src->o_blksize;
507 if (valid & OBD_MD_FLCTIME && tgt->o_ctime < src->o_ctime)
508 tgt->o_ctime = src->o_ctime;
509 if (valid & OBD_MD_FLMTIME && tgt->o_mtime < src->o_mtime)
510 tgt->o_mtime = src->o_mtime;
512 memcpy(tgt, src, sizeof(*tgt));
513 tgt->o_id = lsm->lsm_object_id;
514 if (valid & OBD_MD_FLSIZE)
515 tgt->o_size = lov_stripe_size(lsm,src->o_size,stripeno);
521 #define log2(n) ffz(~(n))
524 static int lov_clear_orphans(struct obd_export *export, struct obdo *src_oa,
525 struct lov_stripe_md **ea,
526 struct obd_trans_info *oti)
530 struct obd_uuid *ost_uuid = NULL;
534 LASSERT(src_oa->o_valid & OBD_MD_FLFLAGS &&
535 src_oa->o_flags == OBD_FL_DELORPHAN);
537 lov = &export->exp_obd->u.lov;
539 tmp_oa = obdo_alloc();
543 if (src_oa->o_valid & OBD_MD_FLINLINE) {
544 ost_uuid = (struct obd_uuid *)src_oa->o_inline;
545 CDEBUG(D_HA, "clearing orphans only for %s\n",
549 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
550 struct lov_stripe_md obj_md;
551 struct lov_stripe_md *obj_mdp = &obj_md;
554 /* if called for a specific target, we don't
555 care if it is not active. */
556 if (lov->tgts[i].active == 0 && ost_uuid == NULL) {
557 CDEBUG(D_HA, "lov idx %d inactive\n", i);
561 if (ost_uuid && !obd_uuid_equals(ost_uuid, &lov->tgts[i].uuid))
564 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
566 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
567 err = obd_create(lov->tgts[i].ltd_exp, tmp_oa, &obj_mdp, oti);
569 /* This export will be disabled until it is recovered,
570 and then orphan recovery will be completed. */
571 CERROR("error in orphan recovery on OST idx %d/%d: "
572 "rc = %d\n", i, lov->desc.ld_tgt_count, err);
581 #define LOV_CREATE_RESEED_INTERVAL 1000
583 /* the LOV expects oa->o_id to be set to the LOV object id */
584 static int lov_create(struct obd_export *exp, struct obdo *src_oa,
585 struct lov_stripe_md **ea, struct obd_trans_info *oti)
587 static int ost_start_idx, ost_start_count;
589 struct lov_stripe_md *lsm;
590 struct lov_oinfo *loi = NULL;
591 struct obdo *tmp_oa, *ret_oa;
592 struct llog_cookie *cookies = NULL;
593 unsigned ost_count, ost_idx;
594 int set = 0, obj_alloc = 0, cookie_sent = 0, rc = 0, i;
599 if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
600 src_oa->o_flags == OBD_FL_DELORPHAN) {
601 rc = lov_clear_orphans(exp, src_oa, ea, oti);
608 lov = &exp->exp_obd->u.lov;
610 if (!lov->desc.ld_active_tgt_count)
613 /* Recreate a specific object id at the given OST index */
614 if (src_oa->o_valid & OBD_MD_FLFLAGS && src_oa->o_flags &
615 OBD_FL_RECREATE_OBJS) {
616 struct lov_stripe_md obj_md;
617 struct lov_stripe_md *obj_mdp = &obj_md;
619 ost_idx = src_oa->o_nlink;
623 if (ost_idx >= lov->desc.ld_tgt_count)
625 for (i = 0; i < lsm->lsm_stripe_count; i++) {
626 if (lsm->lsm_oinfo[i].loi_ost_idx == ost_idx) {
627 if (lsm->lsm_oinfo[i].loi_id != src_oa->o_id)
632 if (i == lsm->lsm_stripe_count)
635 rc = obd_create(lov->tgts[ost_idx].ltd_exp, src_oa,
640 ret_oa = obdo_alloc();
644 tmp_oa = obdo_alloc();
646 GOTO(out_oa, rc = -ENOMEM);
651 ost_count = lov_get_stripecnt(lov, 0);
653 /* If the MDS file was truncated up to some size, stripe over
654 * enough OSTs to allow the file to be created at that size. */
655 if (src_oa->o_valid & OBD_MD_FLSIZE) {
656 stripes=((src_oa->o_size+LUSTRE_STRIPE_MAXBYTES)>>12)-1;
657 do_div(stripes, (__u32)(LUSTRE_STRIPE_MAXBYTES >> 12));
659 if (stripes > lov->desc.ld_active_tgt_count)
661 if (stripes < ost_count)
667 rc = lov_alloc_memmd(&lsm, stripes, lov->desc.ld_pattern ?
668 lov->desc.ld_pattern : LOV_PATTERN_RAID0);
675 ost_count = lov->desc.ld_tgt_count;
677 LASSERT(src_oa->o_valid & OBD_MD_FLID);
678 lsm->lsm_object_id = src_oa->o_id;
679 if (!lsm->lsm_stripe_size)
680 lsm->lsm_stripe_size = lov->desc.ld_default_stripe_size;
681 if (!lsm->lsm_pattern) {
682 lsm->lsm_pattern = lov->desc.ld_pattern ?
683 lov->desc.ld_pattern : LOV_PATTERN_RAID0;
686 if (*ea == NULL || lsm->lsm_oinfo[0].loi_ost_idx >= ost_count) {
687 if (--ost_start_count <= 0) {
688 ost_start_idx = ll_insecure_random_int();
689 ost_start_count = LOV_CREATE_RESEED_INTERVAL;
690 } else if (lsm->lsm_stripe_count >=
691 lov->desc.ld_active_tgt_count) {
692 /* If we allocate from all of the stripes, make the
693 * next file start on the next OST. */
696 ost_idx = ost_start_idx % ost_count;
698 ost_idx = lsm->lsm_oinfo[0].loi_ost_idx;
701 CDEBUG(D_INODE, "allocating %d subobjs for objid "LPX64" at idx %d\n",
702 lsm->lsm_stripe_count, lsm->lsm_object_id, ost_idx);
704 /* XXX LOV STACKING: need to figure out how many real OSCs */
705 if (oti && (src_oa->o_valid & OBD_MD_FLCOOKIE)) {
706 oti_alloc_cookies(oti, lsm->lsm_stripe_count);
707 if (!oti->oti_logcookies)
708 GOTO(out_cleanup, rc = -ENOMEM);
709 cookies = oti->oti_logcookies;
712 loi = lsm->lsm_oinfo;
713 for (i = 0; i < ost_count; i++, ost_idx = (ost_idx + 1) % ost_count) {
714 struct lov_stripe_md obj_md;
715 struct lov_stripe_md *obj_mdp = &obj_md;
719 if (lov->tgts[ost_idx].active == 0) {
720 CDEBUG(D_HA, "lov idx %d inactive\n", ost_idx);
724 /* create data objects with "parent" OA */
725 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
727 /* XXX When we start creating objects on demand, we need to
728 * make sure that we always create the object on the
729 * stripe which holds the existing file size.
731 if (src_oa->o_valid & OBD_MD_FLSIZE) {
732 if (lov_stripe_offset(lsm, src_oa->o_size, i,
733 &tmp_oa->o_size) < 0 &&
737 CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
738 i, tmp_oa->o_size, src_oa->o_size);
742 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
743 err = obd_create(lov->tgts[ost_idx].ltd_exp, tmp_oa, &obj_mdp,
746 if (lov->tgts[ost_idx].active) {
747 CERROR("error creating objid "LPX64" sub-object"
748 " on OST idx %d/%d: rc = %d\n",
749 src_oa->o_id, ost_idx,
750 lsm->lsm_stripe_count, err);
752 CERROR("obd_create returned invalid "
762 oti->oti_objid[ost_idx] = tmp_oa->o_id;
763 loi->loi_id = tmp_oa->o_id;
764 loi->loi_ost_idx = ost_idx;
765 CDEBUG(D_INODE, "objid "LPX64" has subobj "LPX64" at idx %d\n",
766 lsm->lsm_object_id, loi->loi_id, ost_idx);
768 lov_merge_attrs(ret_oa, tmp_oa, tmp_oa->o_valid, lsm,
773 ++oti->oti_logcookies;
774 if (tmp_oa->o_valid & OBD_MD_FLCOOKIE)
779 /* If we have allocated enough objects, we are OK */
780 if (obj_alloc == lsm->lsm_stripe_count)
781 GOTO(out_done, rc = 0);
784 if (obj_alloc == 0) {
787 GOTO(out_cleanup, rc);
790 /* If we were passed specific striping params, then a failure to
791 * meet those requirements is an error, since we can't reallocate
792 * that memory (it might be part of a larger array or something).
794 * We can only get here if lsm_stripe_count was originally > 1.
797 CERROR("can't lstripe objid "LPX64": have %u want %u, rc %d\n",
798 lsm->lsm_object_id, obj_alloc, lsm->lsm_stripe_count,rc);
801 GOTO(out_cleanup, rc);
803 struct lov_stripe_md *lsm_new;
804 /* XXX LOV STACKING call into osc for sizes */
805 unsigned oldsize, newsize;
807 if (oti && cookies && cookie_sent) {
808 oldsize = lsm->lsm_stripe_count * sizeof(*cookies);
809 newsize = obj_alloc * sizeof(*cookies);
811 oti_alloc_cookies(oti, obj_alloc);
812 if (oti->oti_logcookies) {
813 memcpy(oti->oti_logcookies, cookies, newsize);
814 OBD_FREE(cookies, oldsize);
815 cookies = oti->oti_logcookies;
817 CWARN("'leaking' %d bytes\n", oldsize-newsize);
821 CWARN("using fewer stripes for object "LPX64": old %u new %u\n",
822 lsm->lsm_object_id, lsm->lsm_stripe_count, obj_alloc);
823 oldsize = lov_stripe_md_size(lsm->lsm_stripe_count);
824 newsize = lov_stripe_md_size(obj_alloc);
825 OBD_ALLOC(lsm_new, newsize);
826 if (lsm_new != NULL) {
827 memcpy(lsm_new, lsm, newsize);
828 lsm_new->lsm_stripe_count = obj_alloc;
829 OBD_FREE(lsm, oldsize);
832 CWARN("'leaking' %d bytes\n", oldsize - newsize);
839 if (src_oa->o_valid & OBD_MD_FLSIZE &&
840 ret_oa->o_size != src_oa->o_size) {
841 CERROR("original size "LPU64" isn't new object size "LPU64"\n",
842 src_oa->o_size, ret_oa->o_size);
845 ret_oa->o_id = src_oa->o_id;
846 memcpy(src_oa, ret_oa, sizeof(*src_oa));
852 if (oti && cookies) {
853 oti->oti_logcookies = cookies;
855 oti_free_cookies(oti);
856 src_oa->o_valid &= ~OBD_MD_FLCOOKIE;
858 src_oa->o_valid |= OBD_MD_FLCOOKIE;
864 while (obj_alloc-- > 0) {
865 struct obd_export *sub_exp;
869 sub_exp = lov->tgts[loi->loi_ost_idx].ltd_exp;
870 /* destroy already created objects here */
871 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
872 tmp_oa->o_id = loi->loi_id;
874 err = obd_destroy(sub_exp, tmp_oa, NULL, oti);
876 CERROR("Failed to uncreate objid "LPX64" subobj "LPX64
877 " on OST idx %d: rc = %d\n", src_oa->o_id,
878 loi->loi_id, loi->loi_ost_idx, err);
881 obd_free_memmd(exp, &lsm);
885 #define lsm_bad_magic(LSMP) \
887 struct lov_stripe_md *_lsm__ = (LSMP); \
890 CERROR("LOV requires striping ea\n"); \
892 } else if (_lsm__->lsm_magic != LOV_MAGIC) { \
893 CERROR("LOV striping magic bad %#x != %#x\n", \
894 _lsm__->lsm_magic, LOV_MAGIC); \
900 static int lov_destroy(struct obd_export *exp, struct obdo *oa,
901 struct lov_stripe_md *lsm, struct obd_trans_info *oti)
905 struct lov_oinfo *loi;
909 if (lsm_bad_magic(lsm))
912 if (!exp || !exp->exp_obd)
915 lov = &exp->exp_obd->u.lov;
916 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
918 if (lov->tgts[loi->loi_ost_idx].active == 0) {
919 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
920 /* Orphan clean up will (someday) fix this up. */
921 if (oti != NULL && oa->o_valid & OBD_MD_FLCOOKIE)
922 oti->oti_logcookies++;
926 memcpy(&tmp, oa, sizeof(tmp));
927 tmp.o_id = loi->loi_id;
928 err = obd_destroy(lov->tgts[loi->loi_ost_idx].ltd_exp, &tmp,
930 if (err && lov->tgts[loi->loi_ost_idx].active) {
931 CERROR("error: destroying objid "LPX64" subobj "
932 LPX64" on OST idx %d: rc = %d\n",
933 oa->o_id, loi->loi_id, loi->loi_ost_idx, err);
941 static int lov_getattr(struct obd_export *exp, struct obdo *oa,
942 struct lov_stripe_md *lsm)
946 struct lov_oinfo *loi;
947 int i, rc = 0, set = 0;
950 if (lsm_bad_magic(lsm))
953 if (!exp || !exp->exp_obd)
956 lov = &exp->exp_obd->u.lov;
958 CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
959 lsm->lsm_object_id, lsm->lsm_stripe_count, lsm->lsm_stripe_size);
960 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
963 if (lov->tgts[loi->loi_ost_idx].active == 0) {
964 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
968 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
969 "%u\n", oa->o_id, i, loi->loi_id, loi->loi_ost_idx);
970 /* create data objects with "parent" OA */
971 memcpy(&tmp, oa, sizeof(tmp));
972 tmp.o_id = loi->loi_id;
974 err = obd_getattr(lov->tgts[loi->loi_ost_idx].ltd_exp, &tmp,
977 if (lov->tgts[loi->loi_ost_idx].active) {
978 CERROR("error: getattr objid "LPX64" subobj "
979 LPX64" on OST idx %d: rc = %d\n",
980 oa->o_id, loi->loi_id, loi->loi_ost_idx,
985 lov_merge_attrs(oa, &tmp, tmp.o_valid, lsm, i, &set);
993 static int lov_getattr_interpret(struct ptlrpc_request_set *rqset, void *data,
996 struct lov_getattr_async_args *aa = data;
997 struct lov_stripe_md *lsm = aa->aa_lsm;
998 struct obdo *oa = aa->aa_oa;
999 struct obdo *obdos = aa->aa_obdos;
1000 struct lov_oinfo *loi;
1006 /* NB all stripe requests succeeded to get here */
1008 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
1010 if (obdos[i].o_valid == 0) /* inactive stripe */
1013 lov_merge_attrs(oa, &obdos[i], obdos[i].o_valid, lsm,
1018 CERROR ("No stripes had valid attrs\n");
1023 OBD_FREE (obdos, lsm->lsm_stripe_count * sizeof (*obdos));
1027 static int lov_getattr_async(struct obd_export *exp, struct obdo *oa,
1028 struct lov_stripe_md *lsm,
1029 struct ptlrpc_request_set *rqset)
1032 struct lov_obd *lov;
1033 struct lov_oinfo *loi;
1034 struct lov_getattr_async_args *aa;
1035 int i, rc = 0, set = 0;
1038 if (lsm_bad_magic(lsm))
1041 if (!exp || !exp->exp_obd)
1044 lov = &exp->exp_obd->u.lov;
1046 OBD_ALLOC (obdos, lsm->lsm_stripe_count * sizeof (*obdos));
1050 CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
1051 lsm->lsm_object_id, lsm->lsm_stripe_count, lsm->lsm_stripe_size);
1052 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1055 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1056 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1057 /* leaves obdos[i].obd_valid unset */
1061 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1062 "%u\n", oa->o_id, i, loi->loi_id, loi->loi_ost_idx);
1063 /* create data objects with "parent" OA */
1064 memcpy(&obdos[i], oa, sizeof(obdos[i]));
1065 obdos[i].o_id = loi->loi_id;
1067 err = obd_getattr_async(lov->tgts[loi->loi_ost_idx].ltd_exp,
1068 &obdos[i], NULL, rqset);
1070 CERROR("error: getattr objid "LPX64" subobj "
1071 LPX64" on OST idx %d: rc = %d\n",
1072 oa->o_id, loi->loi_id, loi->loi_ost_idx,
1074 GOTO(out_obdos, rc = err);
1079 GOTO (out_obdos, rc = -EIO);
1081 LASSERT (rqset->set_interpret == NULL);
1082 rqset->set_interpret = lov_getattr_interpret;
1083 LASSERT (sizeof (rqset->set_args) >= sizeof (*aa));
1084 aa = (struct lov_getattr_async_args *)&rqset->set_args;
1087 aa->aa_obdos = obdos;
1092 OBD_FREE (obdos, lsm->lsm_stripe_count * sizeof (*obdos));
1098 static int lov_setattr(struct obd_export *exp, struct obdo *src_oa,
1099 struct lov_stripe_md *lsm, struct obd_trans_info *oti)
1101 struct obdo *tmp_oa, *ret_oa;
1102 struct lov_obd *lov;
1103 struct lov_oinfo *loi;
1104 int rc = 0, i, set = 0;
1107 if (lsm_bad_magic(lsm))
1110 if (!exp || !exp->exp_obd)
1113 /* for now, we only expect time updates here */
1114 LASSERT(!(src_oa->o_valid & ~(OBD_MD_FLID|OBD_MD_FLTYPE | OBD_MD_FLMODE|
1115 OBD_MD_FLATIME | OBD_MD_FLMTIME |
1116 OBD_MD_FLCTIME | OBD_MD_FLFLAGS |
1118 ret_oa = obdo_alloc();
1122 tmp_oa = obdo_alloc();
1124 GOTO(out_oa, rc = -ENOMEM);
1126 lov = &exp->exp_obd->u.lov;
1127 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1130 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1131 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1135 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1136 tmp_oa->o_id = loi->loi_id;
1138 if (src_oa->o_valid & OBD_MD_FLSIZE) {
1139 if (lov_stripe_offset(lsm, src_oa->o_size, i,
1140 &tmp_oa->o_size) < 0 &&
1144 CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
1145 i, tmp_oa->o_size, src_oa->o_size);
1148 err = obd_setattr(lov->tgts[loi->loi_ost_idx].ltd_exp, tmp_oa,
1151 if (lov->tgts[loi->loi_ost_idx].active) {
1152 CERROR("error: setattr objid "LPX64" subobj "
1153 LPX64" on OST idx %d: rc = %d\n",
1154 src_oa->o_id, loi->loi_id,
1155 loi->loi_ost_idx, err);
1161 lov_merge_attrs(ret_oa, tmp_oa, tmp_oa->o_valid, lsm, i, &set);
1166 ret_oa->o_id = src_oa->o_id;
1167 memcpy(src_oa, ret_oa, sizeof(*src_oa));
1176 /* we have an offset in file backed by an lov and want to find out where
1177 * that offset lands in our given stripe of the file. for the easy
1178 * case where the offset is within the stripe, we just have to scale the
1179 * offset down to make it relative to the stripe instead of the lov.
1181 * the harder case is what to do when the offset doesn't intersect the
1182 * stripe. callers will want start offsets clamped ahead to the start
1183 * of the nearest stripe in the file. end offsets similarly clamped to the
1184 * nearest ending byte of a stripe in the file:
1186 * all this function does is move offsets to the nearest region of the
1187 * stripe, and it does its work "mod" the full length of all the stripes.
1188 * consider a file with 3 stripes:
1191 * ---------------------------------------------------------------------
1192 * | 0 | 1 | 2 | 0 | 1 | 2 |
1193 * ---------------------------------------------------------------------
1195 * to find stripe 1's offsets for S and E, it divides by the full stripe
1196 * width and does its math in the context of a single set of stripes:
1199 * -----------------------------------
1201 * -----------------------------------
1203 * it'll notice that E is outside stripe 1 and clamp it to the end of the
1204 * stripe, then multiply it back out by lov_off to give the real offsets in
1208 * ---------------------------------------------------------------------
1209 * | 1 | 1 | 1 | 1 | 1 | 1 |
1210 * ---------------------------------------------------------------------
1212 * it would have done similarly and pulled S forward to the start of a 1
1213 * stripe if, say, S had landed in a 0 stripe.
1215 * this rounding isn't always correct. consider an E lov offset that lands
1216 * on a 0 stripe, the "mod stripe width" math will pull it forward to the
1217 * start of a 1 stripe, when in fact it wanted to be rounded back to the end
1218 * of a previous 1 stripe. this logic is handled by callers and this is why:
1220 * this function returns < 0 when the offset was "before" the stripe and
1221 * was moved forward to the start of the stripe in question; 0 when it
1222 * falls in the stripe and no shifting was done; > 0 when the offset
1223 * was outside the stripe and was pulled back to its final byte. */
1224 static int lov_stripe_offset(struct lov_stripe_md *lsm, obd_off lov_off,
1225 int stripeno, obd_off *obd_off)
1227 unsigned long ssize = lsm->lsm_stripe_size;
1228 unsigned long swidth = ssize * lsm->lsm_stripe_count;
1229 unsigned long stripe_off, this_stripe;
1232 if (lov_off == OBD_OBJECT_EOF) {
1233 *obd_off = OBD_OBJECT_EOF;
1237 /* do_div(a, b) returns a % b, and a = a / b */
1238 stripe_off = do_div(lov_off, swidth);
1240 this_stripe = stripeno * ssize;
1241 if (stripe_off < this_stripe) {
1245 stripe_off -= this_stripe;
1247 if (stripe_off >= ssize) {
1253 *obd_off = lov_off * ssize + stripe_off;
1257 /* Given a whole-file size and a stripe number, give the file size which
1258 * corresponds to the individual object of that stripe.
1260 * This behaves basically in the same was as lov_stripe_offset, except that
1261 * file sizes falling before the beginning of a stripe are clamped to the end
1262 * of the previous stripe, not the beginning of the next:
1265 * ---------------------------------------------------------------------
1266 * | 0 | 1 | 2 | 0 | 1 | 2 |
1267 * ---------------------------------------------------------------------
1269 * if clamped to stripe 2 becomes:
1272 * ---------------------------------------------------------------------
1273 * | 0 | 1 | 2 | 0 | 1 | 2 |
1274 * ---------------------------------------------------------------------
1276 static obd_off lov_size_to_stripe(struct lov_stripe_md *lsm, obd_off file_size,
1279 unsigned long ssize = lsm->lsm_stripe_size;
1280 unsigned long swidth = ssize * lsm->lsm_stripe_count;
1281 unsigned long stripe_off, this_stripe;
1283 if (file_size == OBD_OBJECT_EOF)
1284 return OBD_OBJECT_EOF;
1286 /* do_div(a, b) returns a % b, and a = a / b */
1287 stripe_off = do_div(file_size, swidth);
1289 this_stripe = stripeno * ssize;
1290 if (stripe_off < this_stripe) {
1291 /* Move to end of previous stripe, or zero */
1292 if (file_size > 0) {
1299 stripe_off -= this_stripe;
1301 if (stripe_off >= ssize) {
1302 /* Clamp to end of this stripe */
1307 return (file_size * ssize + stripe_off);
1310 /* given an extent in an lov and a stripe, calculate the extent of the stripe
1311 * that is contained within the lov extent. this returns true if the given
1312 * stripe does intersect with the lov extent. */
1313 static int lov_stripe_intersects(struct lov_stripe_md *lsm, int stripeno,
1314 obd_off start, obd_off end,
1315 obd_off *obd_start, obd_off *obd_end)
1317 int start_side, end_side;
1319 start_side = lov_stripe_offset(lsm, start, stripeno, obd_start);
1320 end_side = lov_stripe_offset(lsm, end, stripeno, obd_end);
1322 CDEBUG(D_INODE, "["LPU64"->"LPU64"] -> [(%d) "LPU64"->"LPU64" (%d)]\n",
1323 start, end, start_side, *obd_start, *obd_end, end_side);
1325 /* this stripe doesn't intersect the file extent when neither
1326 * start or the end intersected the stripe and obd_start and
1327 * obd_end got rounded up to the save value. */
1328 if (start_side != 0 && end_side != 0 && *obd_start == *obd_end)
1331 /* as mentioned in the lov_stripe_offset commentary, end
1332 * might have been shifted in the wrong direction. This
1333 * happens when an end offset is before the stripe when viewed
1334 * through the "mod stripe size" math. we detect it being shifted
1335 * in the wrong direction and touch it up.
1336 * interestingly, this can't underflow since end must be > start
1337 * if we passed through the previous check.
1338 * (should we assert for that somewhere?) */
1345 /* compute which stripe number "lov_off" will be written into */
1346 static int lov_stripe_number(struct lov_stripe_md *lsm, obd_off lov_off)
1348 unsigned long ssize = lsm->lsm_stripe_size;
1349 unsigned long swidth = ssize * lsm->lsm_stripe_count;
1350 unsigned long stripe_off;
1352 stripe_off = do_div(lov_off, swidth);
1354 return stripe_off / ssize;
1357 /* FIXME: maybe we'll just make one node the authoritative attribute node, then
1358 * we can send this 'punch' to just the authoritative node and the nodes
1359 * that the punch will affect. */
1360 static int lov_punch(struct obd_export *exp, struct obdo *oa,
1361 struct lov_stripe_md *lsm,
1362 obd_off start, obd_off end, struct obd_trans_info *oti)
1365 struct lov_obd *lov;
1366 struct lov_oinfo *loi;
1370 if (lsm_bad_magic(lsm))
1373 if (!exp || !exp->exp_obd)
1376 lov = &exp->exp_obd->u.lov;
1377 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1378 obd_off starti, endi;
1381 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1382 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1386 if (!lov_stripe_intersects(lsm, i, start, end, &starti, &endi))
1389 /* create data objects with "parent" OA */
1390 memcpy(&tmp, oa, sizeof(tmp));
1391 tmp.o_id = loi->loi_id;
1393 err = obd_punch(lov->tgts[loi->loi_ost_idx].ltd_exp, &tmp, NULL,
1394 starti, endi, NULL);
1396 if (lov->tgts[loi->loi_ost_idx].active) {
1397 CERROR("error: punch objid "LPX64" subobj "LPX64
1398 " on OST idx %d: rc = %d\n", oa->o_id,
1399 loi->loi_id, loi->loi_ost_idx, err);
1404 loi->loi_kms = loi->loi_rss = starti;
1410 static int lov_sync(struct obd_export *exp, struct obdo *oa,
1411 struct lov_stripe_md *lsm, obd_off start, obd_off end)
1414 struct lov_obd *lov;
1415 struct lov_oinfo *loi;
1419 if (lsm_bad_magic(lsm))
1429 lov = &exp->exp_obd->u.lov;
1430 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1431 obd_off starti, endi;
1434 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1435 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1439 if (!lov_stripe_intersects(lsm, i, start, end, &starti, &endi))
1442 memcpy(tmp, oa, sizeof(*tmp));
1443 tmp->o_id = loi->loi_id;
1445 err = obd_sync(lov->tgts[loi->loi_ost_idx].ltd_exp, tmp, NULL,
1448 if (lov->tgts[loi->loi_ost_idx].active) {
1449 CERROR("error: fsync objid "LPX64" subobj "LPX64
1450 " on OST idx %d: rc = %d\n", oa->o_id,
1451 loi->loi_id, loi->loi_ost_idx, err);
1462 static int lov_brw_check(struct lov_obd *lov, struct obdo *oa,
1463 struct lov_stripe_md *lsm,
1464 obd_count oa_bufs, struct brw_page *pga)
1468 /* The caller just wants to know if there's a chance that this
1469 * I/O can succeed */
1470 for (i = 0; i < oa_bufs; i++) {
1471 int stripe = lov_stripe_number(lsm, pga[i].disk_offset);
1472 int ost = lsm->lsm_oinfo[stripe].loi_ost_idx;
1475 if (!lov_stripe_intersects(lsm, i, pga[i].disk_offset,
1476 pga[i].disk_offset + pga[i].count,
1480 if (lov->tgts[ost].active == 0) {
1481 CDEBUG(D_HA, "lov idx %d inactive\n", ost);
1484 rc = obd_brw(OBD_BRW_CHECK, lov->tgts[stripe].ltd_exp, oa,
1485 NULL, 1, &pga[i], NULL);
1492 static int lov_brw(int cmd, struct obd_export *exp, struct obdo *src_oa,
1493 struct lov_stripe_md *lsm, obd_count oa_bufs,
1494 struct brw_page *pga, struct obd_trans_info *oti)
1500 struct lov_stripe_md lsm;
1502 } *stripeinfo, *si, *si_last;
1503 struct obdo *ret_oa = NULL, *tmp_oa = NULL;
1504 struct lov_obd *lov;
1505 struct brw_page *ioarr;
1506 struct lov_oinfo *loi;
1507 int rc = 0, i, *where, stripe_count = lsm->lsm_stripe_count, set = 0;
1510 if (lsm_bad_magic(lsm))
1513 lov = &exp->exp_obd->u.lov;
1515 if (cmd == OBD_BRW_CHECK) {
1516 rc = lov_brw_check(lov, src_oa, lsm, oa_bufs, pga);
1520 OBD_ALLOC(stripeinfo, stripe_count * sizeof(*stripeinfo));
1524 OBD_ALLOC(where, sizeof(*where) * oa_bufs);
1526 GOTO(out_sinfo, rc = -ENOMEM);
1528 OBD_ALLOC(ioarr, sizeof(*ioarr) * oa_bufs);
1530 GOTO(out_where, rc = -ENOMEM);
1533 ret_oa = obdo_alloc();
1535 GOTO(out_ioarr, rc = -ENOMEM);
1537 tmp_oa = obdo_alloc();
1539 GOTO(out_oa, rc = -ENOMEM);
1542 for (i = 0; i < oa_bufs; i++) {
1543 where[i] = lov_stripe_number(lsm, pga[i].disk_offset);
1544 stripeinfo[where[i]].bufct++;
1547 for (i = 0, loi = lsm->lsm_oinfo, si_last = si = stripeinfo;
1548 i < stripe_count; i++, loi++, si_last = si, si++) {
1550 si->index = si_last->index + si_last->bufct;
1551 si->lsm.lsm_object_id = loi->loi_id;
1552 si->ost_idx = loi->loi_ost_idx;
1555 for (i = 0; i < oa_bufs; i++) {
1556 int which = where[i];
1559 shift = stripeinfo[which].index + stripeinfo[which].subcount;
1560 LASSERT(shift < oa_bufs);
1561 ioarr[shift] = pga[i];
1562 lov_stripe_offset(lsm, pga[i].disk_offset, which,
1563 &ioarr[shift].disk_offset);
1564 stripeinfo[which].subcount++;
1567 for (i = 0, si = stripeinfo; i < stripe_count; i++, si++) {
1568 int shift = si->index;
1570 if (lov->tgts[si->ost_idx].active == 0) {
1571 CDEBUG(D_HA, "lov idx %d inactive\n", si->ost_idx);
1572 GOTO(out_oa, rc = -EIO);
1576 LASSERT(shift < oa_bufs);
1578 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1580 tmp_oa->o_id = si->lsm.lsm_object_id;
1581 rc = obd_brw(cmd, lov->tgts[si->ost_idx].ltd_exp,
1582 tmp_oa, &si->lsm, si->bufct,
1583 &ioarr[shift], oti);
1585 GOTO(out_ioarr, rc);
1587 lov_merge_attrs(ret_oa, tmp_oa, tmp_oa->o_valid, lsm,
1592 ret_oa->o_id = src_oa->o_id;
1593 memcpy(src_oa, ret_oa, sizeof(*src_oa));
1602 OBD_FREE(ioarr, sizeof(*ioarr) * oa_bufs);
1604 OBD_FREE(where, sizeof(*where) * oa_bufs);
1606 OBD_FREE(stripeinfo, stripe_count * sizeof(*stripeinfo));
1610 static int lov_brw_interpret(struct ptlrpc_request_set *reqset, void *data,
1613 struct lov_brw_async_args *aa = data;
1614 struct lov_stripe_md *lsm = aa->aa_lsm;
1615 obd_count oa_bufs = aa->aa_oa_bufs;
1616 struct obdo *oa = aa->aa_oa;
1617 struct obdo *obdos = aa->aa_obdos;
1618 struct brw_page *ioarr = aa->aa_ioarr;
1619 struct lov_oinfo *loi;
1624 /* NB all stripe requests succeeded to get here */
1626 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
1628 if (obdos[i].o_valid == 0) /* inactive stripe */
1631 lov_merge_attrs(oa, &obdos[i], obdos[i].o_valid, lsm,
1636 CERROR("No stripes had valid attrs\n");
1640 oa->o_id = lsm->lsm_object_id;
1642 OBD_FREE(obdos, lsm->lsm_stripe_count * sizeof(*obdos));
1643 OBD_FREE(ioarr, sizeof(*ioarr) * oa_bufs);
1647 static int lov_brw_async(int cmd, struct obd_export *exp, struct obdo *oa,
1648 struct lov_stripe_md *lsm, obd_count oa_bufs,
1649 struct brw_page *pga, struct ptlrpc_request_set *set,
1650 struct obd_trans_info *oti)
1656 struct lov_stripe_md lsm;
1658 } *stripeinfo, *si, *si_last;
1659 struct lov_obd *lov;
1660 struct brw_page *ioarr;
1661 struct obdo *obdos = NULL;
1662 struct lov_oinfo *loi;
1663 struct lov_brw_async_args *aa;
1664 int rc = 0, i, *where, stripe_count = lsm->lsm_stripe_count;
1667 if (lsm_bad_magic(lsm))
1670 lov = &exp->exp_obd->u.lov;
1672 if (cmd == OBD_BRW_CHECK) {
1673 rc = lov_brw_check(lov, oa, lsm, oa_bufs, pga);
1677 OBD_ALLOC(stripeinfo, stripe_count * sizeof(*stripeinfo));
1681 OBD_ALLOC(where, sizeof(*where) * oa_bufs);
1683 GOTO(out_sinfo, rc = -ENOMEM);
1686 OBD_ALLOC(obdos, sizeof(*obdos) * stripe_count);
1688 GOTO(out_where, rc = -ENOMEM);
1691 OBD_ALLOC(ioarr, sizeof(*ioarr) * oa_bufs);
1693 GOTO(out_obdos, rc = -ENOMEM);
1695 for (i = 0; i < oa_bufs; i++) {
1696 where[i] = lov_stripe_number(lsm, pga[i].disk_offset);
1697 stripeinfo[where[i]].bufct++;
1700 for (i = 0, loi = lsm->lsm_oinfo, si_last = si = stripeinfo;
1701 i < stripe_count; i++, loi++, si_last = si, si++) {
1703 si->index = si_last->index + si_last->bufct;
1704 si->lsm.lsm_object_id = loi->loi_id;
1705 si->ost_idx = loi->loi_ost_idx;
1708 memcpy(&obdos[i], oa, sizeof(*obdos));
1709 obdos[i].o_id = si->lsm.lsm_object_id;
1713 for (i = 0; i < oa_bufs; i++) {
1714 int which = where[i];
1717 shift = stripeinfo[which].index + stripeinfo[which].subcount;
1718 LASSERT(shift < oa_bufs);
1719 ioarr[shift] = pga[i];
1720 lov_stripe_offset(lsm, pga[i].disk_offset, which,
1721 &ioarr[shift].disk_offset);
1722 stripeinfo[which].subcount++;
1725 for (i = 0, si = stripeinfo; i < stripe_count; i++, si++) {
1726 int shift = si->index;
1731 if (lov->tgts[si->ost_idx].active == 0) {
1732 CDEBUG(D_HA, "lov idx %d inactive\n", si->ost_idx);
1733 GOTO(out_ioarr, rc = -EIO);
1736 LASSERT(shift < oa_bufs);
1738 rc = obd_brw_async(cmd, lov->tgts[si->ost_idx].ltd_exp,
1739 &obdos[i], &si->lsm, si->bufct,
1740 &ioarr[shift], set, oti);
1742 GOTO(out_ioarr, rc);
1745 LASSERT(set->set_interpret == NULL);
1746 set->set_interpret = (set_interpreter_func)lov_brw_interpret;
1747 LASSERT(sizeof(set->set_args) >= sizeof(struct lov_brw_async_args));
1748 aa = (struct lov_brw_async_args *)&set->set_args;
1750 aa->aa_obdos = obdos;
1752 aa->aa_ioarr = ioarr;
1753 aa->aa_oa_bufs = oa_bufs;
1755 /* Don't free ioarr or obdos - that's done in lov_brw_interpret */
1756 GOTO(out_where, rc);
1759 OBD_FREE(ioarr, sizeof(*ioarr) * oa_bufs);
1761 OBD_FREE(obdos, stripe_count * sizeof(*obdos));
1763 OBD_FREE(where, sizeof(*where) * oa_bufs);
1765 OBD_FREE(stripeinfo, stripe_count * sizeof(*stripeinfo));
1769 struct lov_async_page *lap_from_cookie(void *cookie)
1771 struct lov_async_page *lap = cookie;
1772 if (lap->lap_magic != LAP_MAGIC)
1773 return ERR_PTR(-EINVAL);
1777 static int lov_ap_make_ready(void *data, int cmd)
1779 struct lov_async_page *lap = lap_from_cookie(data);
1780 /* XXX should these assert? */
1784 return lap->lap_caller_ops->ap_make_ready(lap->lap_caller_data, cmd);
1786 static int lov_ap_refresh_count(void *data, int cmd)
1788 struct lov_async_page *lap = lap_from_cookie(data);
1792 return lap->lap_caller_ops->ap_refresh_count(lap->lap_caller_data,
1795 static void lov_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
1797 struct lov_async_page *lap = lap_from_cookie(data);
1798 /* XXX should these assert? */
1802 lap->lap_caller_ops->ap_fill_obdo(lap->lap_caller_data, cmd, oa);
1803 /* XXX woah, shouldn't we be altering more here? size? */
1804 oa->o_id = lap->lap_loi_id;
1807 static void lov_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
1809 struct lov_async_page *lap = lap_from_cookie(data);
1813 /* in a raid1 regime this would down a count of many ios
1814 * in flight, onl calling the caller_ops completion when all
1815 * the raid1 ios are complete */
1816 lap->lap_caller_ops->ap_completion(lap->lap_caller_data, cmd, oa, rc);
1819 static struct obd_async_page_ops lov_async_page_ops = {
1820 .ap_make_ready = lov_ap_make_ready,
1821 .ap_refresh_count = lov_ap_refresh_count,
1822 .ap_fill_obdo = lov_ap_fill_obdo,
1823 .ap_completion = lov_ap_completion,
1826 int lov_prep_async_page(struct obd_export *exp, struct lov_stripe_md *lsm,
1827 struct lov_oinfo *loi, struct page *page,
1828 obd_off offset, struct obd_async_page_ops *ops,
1829 void *data, void **res)
1831 struct lov_obd *lov = &exp->exp_obd->u.lov;
1832 struct lov_async_page *lap;
1836 if (lsm_bad_magic(lsm))
1838 LASSERT(loi == NULL);
1840 OBD_ALLOC(lap, sizeof(*lap));
1844 lap->lap_magic = LAP_MAGIC;
1845 lap->lap_caller_ops = ops;
1846 lap->lap_caller_data = data;
1848 /* for now only raid 0 which passes through */
1849 lap->lap_stripe = lov_stripe_number(lsm, offset);
1850 lov_stripe_offset(lsm, offset, lap->lap_stripe, &lap->lap_sub_offset);
1851 loi = &lsm->lsm_oinfo[lap->lap_stripe];
1853 /* so the callback doesn't need the lsm */
1854 lap->lap_loi_id = loi->loi_id;
1856 rc = obd_prep_async_page(lov->tgts[loi->loi_ost_idx].ltd_exp,
1857 lsm, loi, page, lap->lap_sub_offset,
1858 &lov_async_page_ops, lap,
1859 &lap->lap_sub_cookie);
1861 OBD_FREE(lap, sizeof(*lap));
1864 CDEBUG(D_CACHE, "lap %p page %p cookie %p off "LPU64"\n", lap, page,
1865 lap->lap_sub_cookie, offset);
1870 static int lov_queue_async_io(struct obd_export *exp,
1871 struct lov_stripe_md *lsm,
1872 struct lov_oinfo *loi, void *cookie,
1873 int cmd, obd_off off, int count,
1874 obd_flag brw_flags, obd_flag async_flags)
1876 struct lov_obd *lov = &exp->exp_obd->u.lov;
1877 struct lov_async_page *lap;
1880 LASSERT(loi == NULL);
1882 if (lsm_bad_magic(lsm))
1885 lap = lap_from_cookie(cookie);
1887 RETURN(PTR_ERR(lap));
1889 loi = &lsm->lsm_oinfo[lap->lap_stripe];
1890 rc = obd_queue_async_io(lov->tgts[loi->loi_ost_idx].ltd_exp, lsm,
1891 loi, lap->lap_sub_cookie, cmd, off, count,
1892 brw_flags, async_flags);
1896 static int lov_set_async_flags(struct obd_export *exp,
1897 struct lov_stripe_md *lsm,
1898 struct lov_oinfo *loi, void *cookie,
1899 obd_flag async_flags)
1901 struct lov_obd *lov = &exp->exp_obd->u.lov;
1902 struct lov_async_page *lap;
1905 LASSERT(loi == NULL);
1907 if (lsm_bad_magic(lsm))
1910 lap = lap_from_cookie(cookie);
1912 RETURN(PTR_ERR(lap));
1914 loi = &lsm->lsm_oinfo[lap->lap_stripe];
1915 rc = obd_set_async_flags(lov->tgts[loi->loi_ost_idx].ltd_exp,
1916 lsm, loi, lap->lap_sub_cookie, async_flags);
1920 static int lov_queue_group_io(struct obd_export *exp,
1921 struct lov_stripe_md *lsm,
1922 struct lov_oinfo *loi,
1923 struct obd_io_group *oig, void *cookie,
1924 int cmd, obd_off off, int count,
1925 obd_flag brw_flags, obd_flag async_flags)
1927 struct lov_obd *lov = &exp->exp_obd->u.lov;
1928 struct lov_async_page *lap;
1931 LASSERT(loi == NULL);
1933 if (lsm_bad_magic(lsm))
1936 lap = lap_from_cookie(cookie);
1938 RETURN(PTR_ERR(lap));
1940 loi = &lsm->lsm_oinfo[lap->lap_stripe];
1941 rc = obd_queue_group_io(lov->tgts[loi->loi_ost_idx].ltd_exp, lsm, loi,
1942 oig, lap->lap_sub_cookie, cmd, off, count,
1943 brw_flags, async_flags);
1947 /* this isn't exactly optimal. we may have queued sync io in oscs on
1948 * all stripes, but we don't record that fact at queue time. so we
1949 * trigger sync io on all stripes. */
1950 static int lov_trigger_group_io(struct obd_export *exp,
1951 struct lov_stripe_md *lsm,
1952 struct lov_oinfo *loi,
1953 struct obd_io_group *oig)
1955 struct lov_obd *lov = &exp->exp_obd->u.lov;
1958 LASSERT(loi == NULL);
1960 if (lsm_bad_magic(lsm))
1963 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
1965 err = obd_trigger_group_io(lov->tgts[loi->loi_ost_idx].ltd_exp,
1967 if (rc == 0 && err != 0)
1973 static int lov_teardown_async_page(struct obd_export *exp,
1974 struct lov_stripe_md *lsm,
1975 struct lov_oinfo *loi, void *cookie)
1977 struct lov_obd *lov = &exp->exp_obd->u.lov;
1978 struct lov_async_page *lap;
1981 LASSERT(loi == NULL);
1983 if (lsm_bad_magic(lsm))
1986 lap = lap_from_cookie(cookie);
1988 RETURN(PTR_ERR(lap));
1990 loi = &lsm->lsm_oinfo[lap->lap_stripe];
1991 rc = obd_teardown_async_page(lov->tgts[loi->loi_ost_idx].ltd_exp,
1992 lsm, loi, lap->lap_sub_cookie);
1994 CERROR("unable to teardown sub cookie %p: %d\n",
1995 lap->lap_sub_cookie, rc);
1998 OBD_FREE(lap, sizeof(*lap));
2002 static int lov_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
2003 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
2004 int *flags, void *bl_cb, void *cp_cb, void *gl_cb,
2005 void *data,__u32 lvb_len, void *lvb_swabber,
2006 struct lustre_handle *lockh)
2008 struct lov_lock_handles *lov_lockh = NULL;
2009 struct lustre_handle *lov_lockhp;
2010 struct lov_obd *lov;
2011 struct lov_oinfo *loi;
2012 char submd_buf[sizeof(struct lov_stripe_md) + sizeof(struct lov_oinfo)];
2013 struct lov_stripe_md *submd = (void *)submd_buf;
2015 int i, save_flags = *flags;
2018 if (lsm_bad_magic(lsm))
2021 /* we should never be asked to replay a lock this way. */
2022 LASSERT((*flags & LDLM_FL_REPLAY) == 0);
2024 if (!exp || !exp->exp_obd)
2027 if (lsm->lsm_stripe_count > 1) {
2028 lov_lockh = lov_llh_new(lsm);
2029 if (lov_lockh == NULL)
2032 lockh->cookie = lov_lockh->llh_handle.h_cookie;
2033 lov_lockhp = lov_lockh->llh_handles;
2038 lov = &exp->exp_obd->u.lov;
2039 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2040 i++, loi++, lov_lockhp++) {
2041 ldlm_policy_data_t sub_ext;
2044 if (!lov_stripe_intersects(lsm, i, policy->l_extent.start,
2045 policy->l_extent.end, &start,
2049 sub_ext.l_extent.start = start;
2050 sub_ext.l_extent.end = end;
2051 sub_ext.l_extent.gid = policy->l_extent.gid;
2053 if (lov->tgts[loi->loi_ost_idx].active == 0) {
2054 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2058 /* XXX LOV STACKING: submd should be from the subobj */
2059 submd->lsm_object_id = loi->loi_id;
2060 submd->lsm_stripe_count = 0;
2061 submd->lsm_oinfo->loi_kms_valid = loi->loi_kms_valid;
2062 submd->lsm_oinfo->loi_rss = loi->loi_rss;
2063 submd->lsm_oinfo->loi_kms = loi->loi_kms;
2064 submd->lsm_oinfo->loi_blocks = loi->loi_blocks;
2065 loi->loi_mtime = submd->lsm_oinfo->loi_mtime;
2066 /* XXX submd is not fully initialized here */
2067 *flags = save_flags;
2068 rc = obd_enqueue(lov->tgts[loi->loi_ost_idx].ltd_exp, submd,
2069 type, &sub_ext, mode, flags, bl_cb, cp_cb,
2070 gl_cb, data, lvb_len, lvb_swabber, lov_lockhp);
2072 /* XXX FIXME: This unpleasantness doesn't belong here at *all*.
2073 * It belongs in the OSC, except that the OSC doesn't have
2074 * access to the real LOI -- it gets a copy, that we created
2075 * above, and that copy can be arbitrarily out of date.
2077 * The LOV API is due for a serious rewriting anyways, and this
2078 * can be addressed then. */
2079 if (rc == ELDLM_OK) {
2080 struct ldlm_lock *lock = ldlm_handle2lock(lov_lockhp);
2081 __u64 tmp = submd->lsm_oinfo->loi_rss;
2083 LASSERT(lock != NULL);
2085 loi->loi_blocks = submd->lsm_oinfo->loi_blocks;
2086 /* Extend KMS up to the end of this lock and no further
2087 * A lock on [x,y] means a KMS of up to y + 1 bytes! */
2088 if (tmp > lock->l_policy_data.l_extent.end)
2089 tmp = lock->l_policy_data.l_extent.end + 1;
2090 if (tmp >= loi->loi_kms) {
2091 CDEBUG(D_INODE, "lock acquired, setting rss="
2092 LPU64", kms="LPU64"\n", loi->loi_rss,
2095 loi->loi_kms_valid = 1;
2097 CDEBUG(D_INODE, "lock acquired, setting rss="
2098 LPU64"; leaving kms="LPU64", end="LPU64
2099 "\n", loi->loi_rss, loi->loi_kms,
2100 lock->l_policy_data.l_extent.end);
2102 ldlm_lock_allow_match(lock);
2103 LDLM_LOCK_PUT(lock);
2104 } else if (rc == ELDLM_LOCK_ABORTED &&
2105 save_flags & LDLM_FL_HAS_INTENT) {
2106 memset(lov_lockhp, 0, sizeof(*lov_lockhp));
2107 loi->loi_rss = submd->lsm_oinfo->loi_rss;
2108 loi->loi_blocks = submd->lsm_oinfo->loi_blocks;
2109 CDEBUG(D_INODE, "glimpsed, setting rss="LPU64"; leaving"
2110 " kms="LPU64"\n", loi->loi_rss, loi->loi_kms);
2112 memset(lov_lockhp, 0, sizeof(*lov_lockhp));
2113 if (lov->tgts[loi->loi_ost_idx].active) {
2114 CERROR("error: enqueue objid "LPX64" subobj "
2115 LPX64" on OST idx %d: rc = %d\n",
2116 lsm->lsm_object_id, loi->loi_id,
2117 loi->loi_ost_idx, rc);
2118 GOTO(out_locks, rc);
2122 if (lsm->lsm_stripe_count > 1)
2123 lov_llh_put(lov_lockh);
2127 while (loi--, lov_lockhp--, i-- > 0) {
2128 struct lov_stripe_md submd;
2131 if (lov_lockhp->cookie == 0)
2134 /* XXX LOV STACKING: submd should be from the subobj */
2135 submd.lsm_object_id = loi->loi_id;
2136 submd.lsm_stripe_count = 0;
2137 err = obd_cancel(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd,
2139 if (err && lov->tgts[loi->loi_ost_idx].active) {
2140 CERROR("error: cancelling objid "LPX64" on OST "
2141 "idx %d after enqueue error: rc = %d\n",
2142 loi->loi_id, loi->loi_ost_idx, err);
2146 if (lsm->lsm_stripe_count > 1) {
2147 lov_llh_destroy(lov_lockh);
2148 lov_llh_put(lov_lockh);
2153 static int lov_match(struct obd_export *exp, struct lov_stripe_md *lsm,
2154 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
2155 int *flags, void *data, struct lustre_handle *lockh)
2157 struct lov_lock_handles *lov_lockh = NULL;
2158 struct lustre_handle *lov_lockhp;
2159 struct lov_obd *lov;
2160 struct lov_oinfo *loi;
2161 struct lov_stripe_md submd;
2162 ldlm_error_t rc = 0;
2166 if (lsm_bad_magic(lsm))
2169 if (!exp || !exp->exp_obd)
2172 if (lsm->lsm_stripe_count > 1) {
2173 lov_lockh = lov_llh_new(lsm);
2174 if (lov_lockh == NULL)
2177 lockh->cookie = lov_lockh->llh_handle.h_cookie;
2178 lov_lockhp = lov_lockh->llh_handles;
2183 lov = &exp->exp_obd->u.lov;
2184 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2185 i++, loi++, lov_lockhp++) {
2186 ldlm_policy_data_t sub_ext;
2190 if (!lov_stripe_intersects(lsm, i, policy->l_extent.start,
2191 policy->l_extent.end, &start, &end))
2194 sub_ext.l_extent.start = start;
2195 sub_ext.l_extent.end = end;
2197 if (lov->tgts[loi->loi_ost_idx].active == 0) {
2198 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2203 /* XXX LOV STACKING: submd should be from the subobj */
2204 submd.lsm_object_id = loi->loi_id;
2205 submd.lsm_stripe_count = 0;
2207 /* XXX submd is not fully initialized here */
2208 rc = obd_match(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd,
2209 type, &sub_ext, mode, &lov_flags, data,
2215 if (lsm->lsm_stripe_count > 1) {
2216 if (*flags & LDLM_FL_TEST_LOCK)
2217 lov_llh_destroy(lov_lockh);
2218 lov_llh_put(lov_lockh);
2223 while (loi--, lov_lockhp--, i-- > 0) {
2224 struct lov_stripe_md submd;
2227 if (lov_lockhp->cookie == 0)
2230 /* XXX LOV STACKING: submd should be from the subobj */
2231 submd.lsm_object_id = loi->loi_id;
2232 submd.lsm_stripe_count = 0;
2233 err = obd_cancel(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd,
2235 if (err && lov->tgts[loi->loi_ost_idx].active) {
2236 CERROR("error: cancelling objid "LPX64" on OST "
2237 "idx %d after match failure: rc = %d\n",
2238 loi->loi_id, loi->loi_ost_idx, err);
2242 if (lsm->lsm_stripe_count > 1) {
2243 lov_llh_destroy(lov_lockh);
2244 lov_llh_put(lov_lockh);
2249 static int lov_change_cbdata(struct obd_export *exp,
2250 struct lov_stripe_md *lsm, ldlm_iterator_t it,
2253 struct lov_obd *lov;
2254 struct lov_oinfo *loi;
2258 if (lsm_bad_magic(lsm))
2261 if (!exp || !exp->exp_obd)
2264 lov = &exp->exp_obd->u.lov;
2265 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
2266 struct lov_stripe_md submd;
2267 if (lov->tgts[loi->loi_ost_idx].active == 0)
2268 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2270 submd.lsm_object_id = loi->loi_id;
2271 submd.lsm_stripe_count = 0;
2272 rc = obd_change_cbdata(lov->tgts[loi->loi_ost_idx].ltd_exp,
2278 static int lov_cancel(struct obd_export *exp, struct lov_stripe_md *lsm,
2279 __u32 mode, struct lustre_handle *lockh)
2281 struct lov_lock_handles *lov_lockh = NULL;
2282 struct lustre_handle *lov_lockhp;
2283 struct lov_obd *lov;
2284 struct lov_oinfo *loi;
2288 if (lsm_bad_magic(lsm))
2291 if (!exp || !exp->exp_obd)
2295 if (lsm->lsm_stripe_count > 1) {
2296 lov_lockh = lov_handle2llh(lockh);
2298 CERROR("LOV: invalid lov lock handle %p\n", lockh);
2302 lov_lockhp = lov_lockh->llh_handles;
2307 lov = &exp->exp_obd->u.lov;
2308 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2309 i++, loi++, lov_lockhp++) {
2310 struct lov_stripe_md submd;
2313 if (lov_lockhp->cookie == 0) {
2314 CDEBUG(D_HA, "lov idx %d subobj "LPX64" no lock?\n",
2315 loi->loi_ost_idx, loi->loi_id);
2319 /* XXX LOV STACKING: submd should be from the subobj */
2320 submd.lsm_object_id = loi->loi_id;
2321 submd.lsm_stripe_count = 0;
2322 err = obd_cancel(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd,
2325 if (lov->tgts[loi->loi_ost_idx].active) {
2326 CERROR("error: cancel objid "LPX64" subobj "
2327 LPX64" on OST idx %d: rc = %d\n",
2329 loi->loi_id, loi->loi_ost_idx, err);
2336 if (lsm->lsm_stripe_count > 1)
2337 lov_llh_destroy(lov_lockh);
2338 if (lov_lockh != NULL)
2339 lov_llh_put(lov_lockh);
2343 static int lov_cancel_unused(struct obd_export *exp,
2344 struct lov_stripe_md *lsm, int flags, void *opaque)
2346 struct lov_obd *lov;
2347 struct lov_oinfo *loi;
2351 if (lsm_bad_magic(lsm))
2354 if (!exp || !exp->exp_obd)
2357 lov = &exp->exp_obd->u.lov;
2358 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
2359 struct lov_stripe_md submd;
2362 if (lov->tgts[loi->loi_ost_idx].active == 0)
2363 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2365 submd.lsm_object_id = loi->loi_id;
2366 submd.lsm_stripe_count = 0;
2367 err = obd_cancel_unused(lov->tgts[loi->loi_ost_idx].ltd_exp,
2368 &submd, flags, opaque);
2369 if (err && lov->tgts[loi->loi_ost_idx].active) {
2370 CERROR("error: cancel unused objid "LPX64" subobj "LPX64
2371 " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
2372 loi->loi_id, loi->loi_ost_idx, err);
2380 #define LOV_U64_MAX ((__u64)~0ULL)
2381 #define LOV_SUM_MAX(tot, add) \
2383 if ((tot) + (add) < (tot)) \
2384 (tot) = LOV_U64_MAX; \
2389 static int lov_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2390 unsigned long max_age)
2392 struct lov_obd *lov = &obd->u.lov;
2393 struct obd_statfs lov_sfs;
2400 /* We only get block data from the OBD */
2401 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2404 if (!lov->tgts[i].active) {
2405 CDEBUG(D_HA, "lov idx %d inactive\n", i);
2409 err = obd_statfs(class_exp2obd(lov->tgts[i].ltd_exp), &lov_sfs,
2412 if (lov->tgts[i].active && !rc)
2418 memcpy(osfs, &lov_sfs, sizeof(lov_sfs));
2421 osfs->os_bfree += lov_sfs.os_bfree;
2422 osfs->os_bavail += lov_sfs.os_bavail;
2423 osfs->os_blocks += lov_sfs.os_blocks;
2424 /* XXX not sure about this one - depends on policy.
2425 * - could be minimum if we always stripe on all OBDs
2426 * (but that would be wrong for any other policy,
2427 * if one of the OBDs has no more objects left)
2428 * - could be sum if we stripe whole objects
2429 * - could be average, just to give a nice number
2431 * To give a "reasonable" (if not wholly accurate)
2432 * number, we divide the total number of free objects
2433 * by expected stripe count (watch out for overflow).
2435 LOV_SUM_MAX(osfs->os_files, lov_sfs.os_files);
2436 LOV_SUM_MAX(osfs->os_ffree, lov_sfs.os_ffree);
2441 __u32 expected_stripes = lov->desc.ld_default_stripe_count ?
2442 lov->desc.ld_default_stripe_count :
2443 lov->desc.ld_active_tgt_count;
2445 if (osfs->os_files != LOV_U64_MAX)
2446 do_div(osfs->os_files, expected_stripes);
2447 if (osfs->os_ffree != LOV_U64_MAX)
2448 do_div(osfs->os_ffree, expected_stripes);
2455 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2456 void *karg, void *uarg)
2458 struct obd_device *obddev = class_exp2obd(exp);
2459 struct lov_obd *lov = &obddev->u.lov;
2460 int i, count = lov->desc.ld_tgt_count;
2461 struct obd_uuid *uuidp;
2467 case OBD_IOC_LOV_GET_CONFIG: {
2468 struct obd_ioctl_data *data = karg;
2469 struct lov_tgt_desc *tgtdesc;
2470 struct lov_desc *desc;
2475 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2478 data = (struct obd_ioctl_data *)buf;
2480 if (sizeof(*desc) > data->ioc_inllen1) {
2485 if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
2490 desc = (struct lov_desc *)data->ioc_inlbuf1;
2491 memcpy(desc, &(lov->desc), sizeof(*desc));
2493 uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
2494 tgtdesc = lov->tgts;
2495 for (i = 0; i < count; i++, uuidp++, tgtdesc++)
2496 obd_str2uuid(uuidp, tgtdesc->uuid.uuid);
2498 rc = copy_to_user((void *)uarg, buf, len);
2501 obd_ioctl_freedata(buf, len);
2504 case LL_IOC_LOV_SETSTRIPE:
2505 rc = lov_setstripe(exp, karg, uarg);
2507 case LL_IOC_LOV_GETSTRIPE:
2508 rc = lov_getstripe(exp, karg, uarg);
2510 case LL_IOC_LOV_SETEA:
2511 rc = lov_setea(exp, karg, uarg);
2518 for (i = 0; i < count; i++) {
2521 err = obd_iocontrol(cmd, lov->tgts[i].ltd_exp,
2524 if (lov->tgts[i].active) {
2525 CERROR("error: iocontrol OSC %s on OST"
2526 "idx %d: err = %d\n",
2527 lov->tgts[i].uuid.uuid, i, err);
2542 static int lov_get_info(struct obd_export *exp, __u32 keylen,
2543 void *key, __u32 *vallen, void *val)
2545 struct obd_device *obddev = class_exp2obd(exp);
2546 struct lov_obd *lov = &obddev->u.lov;
2550 if (!vallen || !val)
2553 if (keylen > strlen("lock_to_stripe") &&
2554 strcmp(key, "lock_to_stripe") == 0) {
2557 struct ldlm_lock *lock;
2558 struct lov_stripe_md *lsm;
2560 struct lov_oinfo *loi;
2561 __u32 *stripe = val;
2563 if (*vallen < sizeof(*stripe))
2565 *vallen = sizeof(*stripe);
2567 /* XXX This is another one of those bits that will need to
2568 * change if we ever actually support nested LOVs. It uses
2569 * the lock's export to find out which stripe it is. */
2570 for (i = 0, loi = data->lsm->lsm_oinfo;
2571 i < data->lsm->lsm_stripe_count;
2573 if (lov->tgts[loi->loi_ost_idx].ltd_exp ==
2574 data->lock->l_conn_export) {
2580 } else if (keylen >= strlen("size_to_stripe") &&
2581 strcmp(key, "size_to_stripe") == 0) {
2585 struct lov_stripe_md *lsm;
2588 if (*vallen < sizeof(*data))
2591 data->size = lov_size_to_stripe(data->lsm, data->size,
2592 data->stripe_number);
2594 } else if (keylen >= strlen("last_id") && strcmp(key, "last_id") == 0) {
2596 int rc, size = sizeof(obd_id);
2597 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2598 if (!lov->tgts[i].active)
2600 rc = obd_get_info(lov->tgts[i].ltd_exp, keylen, key,
2606 } else if (keylen >= strlen("lovdesc") && strcmp(key, "lovdesc") == 0) {
2607 struct lov_desc *desc_ret = val;
2608 *desc_ret = lov->desc;
2616 static int lov_set_info(struct obd_export *exp, obd_count keylen,
2617 void *key, obd_count vallen, void *val)
2619 struct obd_device *obddev = class_exp2obd(exp);
2620 struct lov_obd *lov = &obddev->u.lov;
2624 #define KEY_IS(str) \
2625 (keylen == strlen(str) && memcmp(key, str, keylen) == 0)
2627 if (KEY_IS("next_id")) {
2628 if (vallen != lov->desc.ld_tgt_count)
2630 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2633 /* initialize all OSCs, even inactive ones */
2635 er = obd_set_info(lov->tgts[i].ltd_exp, keylen, key,
2636 sizeof(obd_id), ((obd_id*)val) + i);
2643 if (KEY_IS("growth_count")) {
2644 if (vallen != sizeof(int))
2646 } else if (KEY_IS("mds_conn") || KEY_IS("unlinked")) {
2653 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2656 if (val && !obd_uuid_equals(val, &lov->tgts[i].uuid))
2659 if (!val && !lov->tgts[i].active)
2662 er = obd_set_info(lov->tgts[i].ltd_exp, keylen, key, vallen,
2672 /* Merge rss if kms == 0
2674 * Even when merging RSS, we will take the KMS value if it's larger.
2675 * This prevents getattr from stomping on dirty cached pages which
2676 * extend the file size. */
2677 __u64 lov_merge_size(struct lov_stripe_md *lsm, int kms)
2679 struct lov_oinfo *loi;
2683 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2685 obd_size lov_size, tmpsize;
2687 tmpsize = loi->loi_kms;
2688 if (kms == 0 && loi->loi_rss > tmpsize)
2689 tmpsize = loi->loi_rss;
2691 lov_size = lov_stripe_size(lsm, tmpsize, i);
2692 if (lov_size > size)
2698 EXPORT_SYMBOL(lov_merge_size);
2701 __u64 lov_merge_blocks(struct lov_stripe_md *lsm)
2703 struct lov_oinfo *loi;
2707 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2709 blocks += loi->loi_blocks;
2713 EXPORT_SYMBOL(lov_merge_blocks);
2715 __u64 lov_merge_mtime(struct lov_stripe_md *lsm, __u64 current_time)
2717 struct lov_oinfo *loi;
2720 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2722 if (loi->loi_mtime > current_time)
2723 current_time = loi->loi_mtime;
2725 return current_time;
2727 EXPORT_SYMBOL(lov_merge_mtime);
2730 struct lov_multi_wait {
2731 struct ldlm_lock *lock;
2737 int lov_complete_many(struct obd_export *exp, struct lov_stripe_md *lsm,
2738 struct lustre_handle *lockh)
2740 struct lov_lock_handles *lov_lockh = NULL;
2741 struct lustre_handle *lov_lockhp;
2742 struct lov_obd *lov;
2743 struct lov_oinfo *loi;
2744 struct lov_multi_wait *queues;
2748 if (lsm_bad_magic(lsm))
2751 if (!exp || !exp->exp_obd)
2754 LASSERT(lockh != NULL);
2755 if (lsm->lsm_stripe_count > 1) {
2756 lov_lockh = lov_handle2llh(lockh);
2757 if (lov_lockh == NULL) {
2758 CERROR("LOV: invalid lov lock handle %p\n", lockh);
2762 lov_lockhp = lov_lockh->llh_handles;
2767 OBD_ALLOC(queues, lsm->lsm_stripe_count * sizeof(*queues));
2769 GOTO(out, rc = -ENOMEM);
2771 lov = &exp->exp_obd->u.lov;
2772 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2773 i++, loi++, lov_lockhp++) {
2774 struct ldlm_lock *lock;
2775 struct obd_device *obd;
2776 unsigned long irqflags;
2778 lock = ldlm_handle2lock(lov_lockhp);
2780 CDEBUG(D_HA, "lov idx %d subobj "LPX64" no lock?\n",
2781 loi->loi_ost_idx, loi->loi_id);
2782 queues[i].completed = 1;
2786 queues[i].lock = lock;
2787 init_waitqueue_entry(&(queues[i].wait), current);
2788 add_wait_queue(lock->l_waitq, &(queues[i].wait));
2790 obd = class_exp2obd(lock->l_conn_export);
2792 imp = obd->u.cli.cl_import;
2794 spin_lock_irqsave(&imp->imp_lock, irqflags);
2795 queues[i].generation = imp->imp_generation;
2796 spin_unlock_irqrestore(&imp->imp_lock, irqflags);
2800 lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, ldlm_expired_completion_wait,
2801 interrupted_completion_wait, &lwd);
2802 rc = l_wait_event_added(check_multi_complete(queues, lsm), &lwi);
2804 for (i = 0; i < lsm->lsm_stripe_count; i++)
2805 remove_wait_queue(lock->l_waitq, &(queues[i].wait));
2807 if (rc == -EINTR || rc == -ETIMEDOUT) {
2813 if (lov_lockh != NULL)
2814 lov_llh_put(lov_lockh);
2819 void lov_increase_kms(struct obd_export *exp, struct lov_stripe_md *lsm,
2822 struct lov_oinfo *loi;
2828 stripe = lov_stripe_number(lsm, size - 1);
2829 kms = lov_size_to_stripe(lsm, size, stripe);
2830 loi = &(lsm->lsm_oinfo[stripe]);
2832 CDEBUG(D_INODE, "stripe %d KMS %sincreasing "LPU64"->"LPU64"\n",
2833 stripe, kms > loi->loi_kms ? "" : "not ", loi->loi_kms, kms);
2834 if (kms > loi->loi_kms)
2838 EXPORT_SYMBOL(lov_increase_kms);
2840 struct obd_ops lov_obd_ops = {
2841 .o_owner = THIS_MODULE,
2842 .o_setup = lov_setup,
2843 .o_cleanup = lov_cleanup,
2844 .o_connect = lov_connect,
2845 .o_disconnect = lov_disconnect,
2846 .o_statfs = lov_statfs,
2847 .o_packmd = lov_packmd,
2848 .o_unpackmd = lov_unpackmd,
2849 .o_create = lov_create,
2850 .o_destroy = lov_destroy,
2851 .o_getattr = lov_getattr,
2852 .o_getattr_async = lov_getattr_async,
2853 .o_setattr = lov_setattr,
2855 .o_brw_async = lov_brw_async,
2856 .o_prep_async_page = lov_prep_async_page,
2857 .o_queue_async_io = lov_queue_async_io,
2858 .o_set_async_flags = lov_set_async_flags,
2859 .o_queue_group_io = lov_queue_group_io,
2860 .o_trigger_group_io = lov_trigger_group_io,
2861 .o_teardown_async_page = lov_teardown_async_page,
2862 .o_punch = lov_punch,
2864 .o_enqueue = lov_enqueue,
2865 .o_match = lov_match,
2866 .o_change_cbdata = lov_change_cbdata,
2867 .o_cancel = lov_cancel,
2868 .o_cancel_unused = lov_cancel_unused,
2869 .o_iocontrol = lov_iocontrol,
2870 .o_get_info = lov_get_info,
2871 .o_set_info = lov_set_info,
2872 .o_llog_init = lov_llog_init,
2873 .o_llog_finish = lov_llog_finish,
2874 .o_notify = lov_notify,
2877 int __init lov_init(void)
2879 struct lprocfs_static_vars lvars;
2882 lprocfs_init_vars(lov, &lvars);
2883 rc = class_register_type(&lov_obd_ops, lvars.module_vars,
2884 OBD_LOV_DEVICENAME);
2889 static void /*__exit*/ lov_exit(void)
2891 class_unregister_type(OBD_LOV_DEVICENAME);
2894 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2895 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver");
2896 MODULE_LICENSE("GPL");
2898 module_init(lov_init);
2899 module_exit(lov_exit);