4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * Implementation of cl_object for LOV layer.
34 * Author: Nikita Danilov <nikita.danilov@sun.com>
35 * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
38 #define DEBUG_SUBSYSTEM S_LOV
40 #include <linux/random.h>
42 #include "lov_cl_internal.h"
44 static inline struct lov_device *lov_object_dev(struct lov_object *obj)
46 return lu2lov_dev(obj->lo_cl.co_lu.lo_dev);
53 /*****************************************************************************
59 struct lov_layout_operations {
60 int (*llo_init)(const struct lu_env *env, struct lov_device *dev,
61 struct lov_object *lov, struct lov_stripe_md *lsm,
62 const struct cl_object_conf *conf,
63 union lov_layout_state *state);
64 int (*llo_delete)(const struct lu_env *env, struct lov_object *lov,
65 union lov_layout_state *state);
66 void (*llo_fini)(const struct lu_env *env, struct lov_object *lov,
67 union lov_layout_state *state);
68 int (*llo_print)(const struct lu_env *env, void *cookie,
69 lu_printer_t p, const struct lu_object *o);
70 int (*llo_page_init)(const struct lu_env *env, struct cl_object *obj,
71 struct cl_page *page, pgoff_t index);
72 int (*llo_lock_init)(const struct lu_env *env,
73 struct cl_object *obj, struct cl_lock *lock,
74 const struct cl_io *io);
75 int (*llo_io_init)(const struct lu_env *env,
76 struct cl_object *obj, struct cl_io *io);
77 int (*llo_getattr)(const struct lu_env *env, struct cl_object *obj,
78 struct cl_attr *attr);
79 int (*llo_flush)(const struct lu_env *env, struct cl_object *obj,
80 struct ldlm_lock *lock);
83 static int lov_layout_wait(const struct lu_env *env, struct lov_object *lov);
85 static void lov_lsm_put(struct lov_stripe_md *lsm)
91 /*****************************************************************************
93 * Lov object layout operations.
97 static struct cl_object *lov_sub_find(const struct lu_env *env,
98 struct cl_device *dev,
99 const struct lu_fid *fid,
100 const struct cl_object_conf *conf)
106 o = lu_object_find_at(env, cl2lu_dev(dev), fid, &conf->coc_lu);
107 LASSERT(ergo(!IS_ERR(o), o->lo_dev->ld_type == &lovsub_device_type));
111 static int lov_page_slice_fixup(struct lov_object *lov,
112 struct cl_object *stripe)
114 struct cl_object_header *hdr = cl_object_header(&lov->lo_cl);
118 return hdr->coh_page_bufsize - lov->lo_cl.co_slice_off -
119 cfs_size_round(sizeof(struct lov_page));
121 cl_object_for_each(o, stripe)
122 o->co_slice_off += hdr->coh_page_bufsize;
124 return cl_object_header(stripe)->coh_page_bufsize;
127 static int lov_init_sub(const struct lu_env *env, struct lov_object *lov,
128 struct cl_object *subobj, struct lov_oinfo *oinfo,
131 struct cl_object_header *hdr;
132 struct cl_object_header *subhdr;
133 struct cl_object_header *parent;
134 int entry = lov_comp_entry(idx);
135 int stripe = lov_comp_stripe(idx);
138 if (OBD_FAIL_CHECK(OBD_FAIL_LOV_INIT)) {
139 /* For sanity:test_206.
140 * Do not leave the object in cache to avoid accessing
141 * freed memory. This is because osc_object is referring to
142 * lov_oinfo of lsm_stripe_data which will be freed due to
144 cl_object_kill(env, subobj);
145 cl_object_put(env, subobj);
149 hdr = cl_object_header(lov2cl(lov));
150 subhdr = cl_object_header(subobj);
152 CDEBUG(D_INODE, DFID"@%p[%d:%d] -> "DFID"@%p: ostid: "DOSTID
153 " ost idx: %d gen: %d\n",
154 PFID(lu_object_fid(&subobj->co_lu)), subhdr, entry, stripe,
155 PFID(lu_object_fid(lov2lu(lov))), hdr, POSTID(&oinfo->loi_oi),
156 oinfo->loi_ost_idx, oinfo->loi_ost_gen);
158 /* reuse ->coh_attr_guard to protect coh_parent change */
159 spin_lock(&subhdr->coh_attr_guard);
160 parent = subhdr->coh_parent;
161 if (parent == NULL) {
162 struct lovsub_object *lso = cl2lovsub(subobj);
164 subhdr->coh_parent = hdr;
165 spin_unlock(&subhdr->coh_attr_guard);
166 subhdr->coh_nesting = hdr->coh_nesting + 1;
167 lu_object_ref_add(&subobj->co_lu, "lov-parent", lov);
168 lso->lso_super = lov;
169 lso->lso_index = idx;
172 struct lu_object *old_obj;
173 struct lov_object *old_lov;
174 unsigned int mask = D_INODE;
176 spin_unlock(&subhdr->coh_attr_guard);
177 old_obj = lu_object_locate(&parent->coh_lu, &lov_device_type);
178 LASSERT(old_obj != NULL);
179 old_lov = cl2lov(lu2cl(old_obj));
180 if (old_lov->lo_layout_invalid) {
181 /* the object's layout has already changed but isn't
183 lu_object_unhash(env, &subobj->co_lu);
190 LU_OBJECT_DEBUG(mask, env, &subobj->co_lu,
191 "stripe %d is already owned.", idx);
192 LU_OBJECT_DEBUG(mask, env, old_obj, "owned.");
193 LU_OBJECT_HEADER(mask, env, lov2lu(lov), "try to own.\n");
194 cl_object_put(env, subobj);
199 static int lov_init_raid0(const struct lu_env *env, struct lov_device *dev,
200 struct lov_object *lov, unsigned int index,
201 const struct cl_object_conf *conf,
202 struct lov_layout_entry *lle)
204 struct lov_layout_raid0 *r0 = &lle->lle_raid0;
205 struct lov_thread_info *lti = lov_env_info(env);
206 struct cl_object_conf *subconf = <i->lti_stripe_conf;
207 struct lu_fid *ofid = <i->lti_fid;
208 struct cl_object *stripe;
209 struct lov_stripe_md_entry *lse = lov_lse(lov, index);
216 spin_lock_init(&r0->lo_sub_lock);
217 r0->lo_nr = lse->lsme_stripe_count;
219 OBD_ALLOC_PTR_ARRAY_LARGE(r0->lo_sub, r0->lo_nr);
220 if (r0->lo_sub == NULL)
221 GOTO(out, result = -ENOMEM);
225 memset(subconf, 0, sizeof(*subconf));
228 * Create stripe cl_objects.
230 for (i = 0; i < r0->lo_nr; ++i) {
231 struct cl_device *subdev;
232 struct lov_oinfo *oinfo = lse->lsme_oinfo[i];
233 int ost_idx = oinfo->loi_ost_idx;
235 if (lov_oinfo_is_dummy(oinfo))
238 result = ostid_to_fid(ofid, &oinfo->loi_oi, oinfo->loi_ost_idx);
242 if (dev->ld_target[ost_idx] == NULL) {
243 CERROR("%s: OST %04x is not initialized\n",
244 lov2obd(dev->ld_lov)->obd_name, ost_idx);
245 GOTO(out, result = -EIO);
248 subdev = lovsub2cl_dev(dev->ld_target[ost_idx]);
249 subconf->u.coc_oinfo = oinfo;
250 LASSERTF(subdev != NULL, "not init ost %d\n", ost_idx);
251 /* In the function below, .hs_keycmp resolves to
252 * lu_obj_hop_keycmp() */
253 /* coverity[overrun-buffer-val] */
254 stripe = lov_sub_find(env, subdev, ofid, subconf);
256 GOTO(out, result = PTR_ERR(stripe));
258 result = lov_init_sub(env, lov, stripe, oinfo,
259 lov_comp_index(index, i));
260 if (result == -EAGAIN) { /* try again */
267 r0->lo_sub[i] = cl2lovsub(stripe);
269 sz = lov_page_slice_fixup(lov, stripe);
270 LASSERT(ergo(psz > 0, psz == sz));
280 static void lov_subobject_kill(const struct lu_env *env, struct lov_object *lov,
281 struct lov_layout_raid0 *r0,
282 struct lovsub_object *los, int idx)
284 struct cl_object *sub;
285 struct lu_site *site;
286 wait_queue_head_t *wq;
288 LASSERT(r0->lo_sub[idx] == los);
290 sub = lovsub2cl(los);
291 site = sub->co_lu.lo_dev->ld_site;
292 wq = lu_site_wq_from_fid(site, &sub->co_lu.lo_header->loh_fid);
294 cl_object_kill(env, sub);
295 /* release a reference to the sub-object and ... */
296 lu_object_ref_del(&sub->co_lu, "lov-parent", lov);
297 cl_object_put(env, sub);
299 /* ... wait until it is actually destroyed---sub-object clears its
300 * ->lo_sub[] slot in lovsub_object_free() */
301 wait_event(*wq, r0->lo_sub[idx] != los);
302 LASSERT(r0->lo_sub[idx] == NULL);
305 static void lov_delete_raid0(const struct lu_env *env, struct lov_object *lov,
306 struct lov_layout_entry *lle)
308 struct lov_layout_raid0 *r0 = &lle->lle_raid0;
312 if (r0->lo_sub != NULL) {
315 for (i = 0; i < r0->lo_nr; ++i) {
316 struct lovsub_object *los = r0->lo_sub[i];
319 cl_object_prune(env, &los->lso_cl);
321 * If top-level object is to be evicted from
322 * the cache, so are its sub-objects.
324 lov_subobject_kill(env, lov, r0, los, i);
332 static void lov_fini_raid0(const struct lu_env *env,
333 struct lov_layout_entry *lle)
335 struct lov_layout_raid0 *r0 = &lle->lle_raid0;
337 if (r0->lo_sub != NULL) {
338 OBD_FREE_PTR_ARRAY_LARGE(r0->lo_sub, r0->lo_nr);
343 static int lov_print_raid0(const struct lu_env *env, void *cookie,
344 lu_printer_t p, const struct lov_layout_entry *lle)
346 const struct lov_layout_raid0 *r0 = &lle->lle_raid0;
349 for (i = 0; i < r0->lo_nr; ++i) {
350 struct lu_object *sub;
352 if (r0->lo_sub[i] != NULL) {
353 sub = lovsub2lu(r0->lo_sub[i]);
354 lu_object_print(env, cookie, p, sub);
356 (*p)(env, cookie, "sub %d absent\n", i);
362 static int lov_attr_get_raid0(const struct lu_env *env, struct lov_object *lov,
363 unsigned int index, struct lov_layout_entry *lle,
364 struct cl_attr **lov_attr)
366 struct lov_layout_raid0 *r0 = &lle->lle_raid0;
367 struct lov_stripe_md *lsm = lov->lo_lsm;
368 struct ost_lvb *lvb = &lov_env_info(env)->lti_lvb;
369 struct cl_attr *attr = &r0->lo_attr;
373 if (r0->lo_attr_valid) {
378 memset(lvb, 0, sizeof(*lvb));
380 /* XXX: timestamps can be negative by sanity:test_39m,
382 lvb->lvb_atime = LLONG_MIN;
383 lvb->lvb_ctime = LLONG_MIN;
384 lvb->lvb_mtime = LLONG_MIN;
387 * XXX that should be replaced with a loop over sub-objects,
388 * doing cl_object_attr_get() on them. But for now, let's
389 * reuse old lov code.
393 * XXX take lsm spin-lock to keep lov_merge_lvb_kms()
394 * happy. It's not needed, because new code uses
395 * ->coh_attr_guard spin-lock to protect consistency of
396 * sub-object attributes.
398 lov_stripe_lock(lsm);
399 result = lov_merge_lvb_kms(lsm, index, lvb, &kms);
400 lov_stripe_unlock(lsm);
402 cl_lvb2attr(attr, lvb);
404 r0->lo_attr_valid = 1;
411 static struct lov_comp_layout_entry_ops raid0_ops = {
412 .lco_init = lov_init_raid0,
413 .lco_fini = lov_fini_raid0,
414 .lco_getattr = lov_attr_get_raid0,
417 static int lov_attr_get_dom(const struct lu_env *env, struct lov_object *lov,
418 unsigned int index, struct lov_layout_entry *lle,
419 struct cl_attr **lov_attr)
421 struct lov_layout_dom *dom = &lle->lle_dom;
422 struct lov_oinfo *loi = dom->lo_loi;
423 struct cl_attr *attr = &dom->lo_dom_r0.lo_attr;
425 if (dom->lo_dom_r0.lo_attr_valid) {
430 if (OST_LVB_IS_ERR(loi->loi_lvb.lvb_blocks))
431 return OST_LVB_GET_ERR(loi->loi_lvb.lvb_blocks);
433 cl_lvb2attr(attr, &loi->loi_lvb);
435 /* DoM component size can be bigger than stripe size after
436 * client's setattr RPC, so do not count anything beyond
437 * component end. Alternatively, check that limit on server
438 * and do not allow size overflow there. */
439 if (attr->cat_size > lle->lle_extent->e_end)
440 attr->cat_size = lle->lle_extent->e_end;
442 attr->cat_kms = attr->cat_size;
444 dom->lo_dom_r0.lo_attr_valid = 1;
451 * Lookup FLD to get MDS index of the given DOM object FID.
453 * \param[in] ld LOV device
454 * \param[in] fid FID to lookup
455 * \param[out] nr index in MDC array to return back
457 * \retval 0 and \a mds filled with MDS index if successful
458 * \retval negative value on error
460 static int lov_fld_lookup(struct lov_device *ld, const struct lu_fid *fid,
468 rc = fld_client_lookup(&ld->ld_lmv->u.lmv.lmv_fld, fid_seq(fid),
469 &mds_idx, LU_SEQ_RANGE_MDT, NULL);
471 CERROR("%s: error while looking for mds number. Seq %#llx"
472 ", err = %d\n", lu_dev_name(cl2lu_dev(&ld->ld_cl)),
477 CDEBUG(D_INODE, "FLD lookup got mds #%x for fid="DFID"\n",
480 /* find proper MDC device in the array */
481 for (i = 0; i < ld->ld_md_tgts_nr; i++) {
482 if (ld->ld_md_tgts[i].ldm_mdc != NULL &&
483 ld->ld_md_tgts[i].ldm_idx == mds_idx)
487 if (i == ld->ld_md_tgts_nr) {
488 CERROR("%s: cannot find corresponding MDC device for mds #%x "
489 "for fid="DFID"\n", lu_dev_name(cl2lu_dev(&ld->ld_cl)),
499 * Implementation of lov_comp_layout_entry_ops::lco_init for DOM object.
501 * Init the DOM object for the first time. It prepares also RAID0 entry
502 * for it to use in common methods with ordinary RAID0 layout entries.
504 * \param[in] env execution environment
505 * \param[in] dev LOV device
506 * \param[in] lov LOV object
507 * \param[in] index Composite layout entry index in LSM
508 * \param[in] lle Composite LOV layout entry
510 static int lov_init_dom(const struct lu_env *env, struct lov_device *dev,
511 struct lov_object *lov, unsigned int index,
512 const struct cl_object_conf *conf,
513 struct lov_layout_entry *lle)
515 struct lov_thread_info *lti = lov_env_info(env);
516 struct lov_stripe_md_entry *lsme = lov_lse(lov, index);
517 struct cl_object *clo;
518 struct lu_object *o = lov2lu(lov);
519 const struct lu_fid *fid = lu_object_fid(o);
520 struct cl_device *mdcdev;
521 struct lov_oinfo *loi = NULL;
522 struct cl_object_conf *sconf = <i->lti_stripe_conf;
528 /* DOM entry may be not zero index due to FLR but must start from 0 */
529 if (unlikely(lle->lle_extent->e_start != 0)) {
530 CERROR("%s: DOM entry must be the first stripe in a mirror\n",
531 lov2obd(dev->ld_lov)->obd_name);
532 dump_lsm(D_ERROR, lov->lo_lsm);
536 /* find proper MDS device */
537 rc = lov_fld_lookup(dev, fid, &idx);
541 LASSERTF(dev->ld_md_tgts[idx].ldm_mdc != NULL,
542 "LOV md target[%u] is NULL\n", idx);
544 /* check lsm is DOM, more checks are needed */
545 LASSERT(lsme->lsme_stripe_count == 0);
548 * Create lower cl_objects.
550 mdcdev = dev->ld_md_tgts[idx].ldm_mdc;
552 LASSERTF(mdcdev != NULL, "non-initialized mdc subdev\n");
554 /* DoM object has no oinfo in LSM entry, create it exclusively */
555 OBD_SLAB_ALLOC_PTR_GFP(loi, lov_oinfo_slab, GFP_NOFS);
559 fid_to_ostid(lu_object_fid(lov2lu(lov)), &loi->loi_oi);
561 sconf->u.coc_oinfo = loi;
563 clo = lov_sub_find(env, mdcdev, fid, sconf);
565 GOTO(out, rc = PTR_ERR(clo));
567 rc = lov_init_sub(env, lov, clo, loi, lov_comp_index(index, 0));
568 if (rc == -EAGAIN) /* try again */
573 lle->lle_dom.lo_dom = cl2lovsub(clo);
574 spin_lock_init(&lle->lle_dom.lo_dom_r0.lo_sub_lock);
575 lle->lle_dom.lo_dom_r0.lo_nr = 1;
576 lle->lle_dom.lo_dom_r0.lo_sub = &lle->lle_dom.lo_dom;
577 lle->lle_dom.lo_loi = loi;
579 rc = lov_page_slice_fixup(lov, clo);
584 OBD_SLAB_FREE_PTR(loi, lov_oinfo_slab);
589 * Implementation of lov_layout_operations::llo_fini for DOM object.
591 * Finish the DOM object and free related memory.
593 * \param[in] env execution environment
594 * \param[in] lov LOV object
595 * \param[in] state LOV layout state
597 static void lov_fini_dom(const struct lu_env *env,
598 struct lov_layout_entry *lle)
600 if (lle->lle_dom.lo_dom != NULL)
601 lle->lle_dom.lo_dom = NULL;
602 if (lle->lle_dom.lo_loi != NULL)
603 OBD_SLAB_FREE_PTR(lle->lle_dom.lo_loi, lov_oinfo_slab);
606 static struct lov_comp_layout_entry_ops dom_ops = {
607 .lco_init = lov_init_dom,
608 .lco_fini = lov_fini_dom,
609 .lco_getattr = lov_attr_get_dom,
612 static int lov_init_composite(const struct lu_env *env, struct lov_device *dev,
613 struct lov_object *lov, struct lov_stripe_md *lsm,
614 const struct cl_object_conf *conf,
615 union lov_layout_state *state)
617 struct lov_layout_composite *comp = &state->composite;
618 struct lov_layout_entry *lle;
619 struct lov_mirror_entry *lre;
620 unsigned int entry_count;
621 unsigned int psz = 0;
622 unsigned int mirror_count;
623 int flr_state = lsm->lsm_flags & LCM_FL_FLR_MASK;
631 LASSERT(lsm->lsm_entry_count > 0);
632 LASSERT(lov->lo_lsm == NULL);
633 lov->lo_lsm = lsm_addref(lsm);
634 lov->lo_layout_invalid = true;
636 dump_lsm(D_INODE, lsm);
638 entry_count = lsm->lsm_entry_count;
640 spin_lock_init(&comp->lo_write_lock);
641 comp->lo_flags = lsm->lsm_flags;
642 comp->lo_mirror_count = lsm->lsm_mirror_count + 1;
643 comp->lo_entry_count = lsm->lsm_entry_count;
644 comp->lo_preferred_mirror = -1;
646 if (equi(flr_state == LCM_FL_NONE, comp->lo_mirror_count > 1))
649 OBD_ALLOC_PTR_ARRAY(comp->lo_mirrors, comp->lo_mirror_count);
650 if (comp->lo_mirrors == NULL)
653 OBD_ALLOC_PTR_ARRAY(comp->lo_entries, entry_count);
654 if (comp->lo_entries == NULL)
657 /* Initiate all entry types and extents data at first */
658 for (i = 0, j = 0, mirror_count = 1; i < entry_count; i++) {
661 lle = &comp->lo_entries[i];
663 lle->lle_lsme = lsm->lsm_entries[i];
664 lle->lle_type = lov_entry_type(lle->lle_lsme);
665 switch (lle->lle_type) {
666 case LOV_PATTERN_RAID0:
667 lle->lle_comp_ops = &raid0_ops;
669 case LOV_PATTERN_MDT:
670 /* Allowed to have several DOM stripes in different
671 * mirrors with the same DoM size.
674 dom_size = lle->lle_lsme->lsme_extent.e_end;
675 } else if (dom_size !=
676 lle->lle_lsme->lsme_extent.e_end) {
677 CERROR("%s: DOM entries with different sizes\n",
678 lov2obd(dev->ld_lov)->obd_name);
679 dump_lsm(D_ERROR, lsm);
682 lle->lle_comp_ops = &dom_ops;
685 CERROR("%s: unknown composite layout entry type %i\n",
686 lov2obd(dev->ld_lov)->obd_name,
687 lsm->lsm_entries[i]->lsme_pattern);
688 dump_lsm(D_ERROR, lsm);
692 lle->lle_extent = &lle->lle_lsme->lsme_extent;
693 lle->lle_valid = !(lle->lle_lsme->lsme_flags & LCME_FL_STALE);
695 if (flr_state != LCM_FL_NONE)
696 mirror_id = mirror_id_of(lle->lle_lsme->lsme_id);
698 lre = &comp->lo_mirrors[j];
700 if (mirror_id == lre->lre_mirror_id) {
701 lre->lre_valid |= lle->lle_valid;
702 lre->lre_stale |= !lle->lle_valid;
707 /* new mirror detected, assume that the mirrors
708 * are shorted in layout */
711 if (j >= comp->lo_mirror_count)
714 lre = &comp->lo_mirrors[j];
717 /* entries must be sorted by mirrors */
718 lre->lre_mirror_id = mirror_id;
719 lre->lre_start = lre->lre_end = i;
720 lre->lre_preferred = !!(lle->lle_lsme->lsme_flags &
722 lre->lre_valid = lle->lle_valid;
723 lre->lre_stale = !lle->lle_valid;
726 /* sanity check for FLR */
727 if (mirror_count != comp->lo_mirror_count) {
729 " doesn't have the # of mirrors it claims, %u/%u\n",
730 PFID(lu_object_fid(lov2lu(lov))), mirror_count,
731 comp->lo_mirror_count + 1);
733 GOTO(out, result = -EINVAL);
736 lov_foreach_layout_entry(lov, lle) {
737 int index = lov_layout_entry_index(lov, lle);
740 * If the component has not been init-ed on MDS side, for
741 * PFL layout, we'd know that the components beyond this one
742 * will be dynamically init-ed later on file write/trunc ops.
744 if (!lsme_inited(lle->lle_lsme))
747 result = lle->lle_comp_ops->lco_init(env, dev, lov, index,
752 LASSERT(ergo(psz > 0, psz == result));
757 cl_object_header(&lov->lo_cl)->coh_page_bufsize += psz;
759 /* decide the preferred mirror. It uses the hash value of lov_object
760 * so that different clients would use different mirrors for read. */
762 seq = hash_long((unsigned long)lov, 8);
763 for (i = 0; i < comp->lo_mirror_count; i++) {
764 unsigned int idx = (i + seq) % comp->lo_mirror_count;
766 lre = lov_mirror_entry(lov, idx);
770 mirror_count++; /* valid mirror */
772 if (lre->lre_preferred || comp->lo_preferred_mirror < 0)
773 comp->lo_preferred_mirror = idx;
777 " doesn't have any valid mirrors\n",
778 PFID(lu_object_fid(lov2lu(lov))));
780 comp->lo_preferred_mirror = 0;
783 LASSERT(comp->lo_preferred_mirror >= 0);
787 return result > 0 ? 0 : result;
790 static int lov_init_empty(const struct lu_env *env, struct lov_device *dev,
791 struct lov_object *lov, struct lov_stripe_md *lsm,
792 const struct cl_object_conf *conf,
793 union lov_layout_state *state)
798 static int lov_init_released(const struct lu_env *env,
799 struct lov_device *dev, struct lov_object *lov,
800 struct lov_stripe_md *lsm,
801 const struct cl_object_conf *conf,
802 union lov_layout_state *state)
804 LASSERT(lsm != NULL);
805 LASSERT(lsm->lsm_is_released);
806 LASSERT(lov->lo_lsm == NULL);
808 lov->lo_lsm = lsm_addref(lsm);
812 static int lov_init_foreign(const struct lu_env *env,
813 struct lov_device *dev, struct lov_object *lov,
814 struct lov_stripe_md *lsm,
815 const struct cl_object_conf *conf,
816 union lov_layout_state *state)
818 LASSERT(lsm != NULL);
819 LASSERT(lov->lo_type == LLT_FOREIGN);
820 LASSERT(lov->lo_lsm == NULL);
822 lov->lo_lsm = lsm_addref(lsm);
826 static int lov_delete_empty(const struct lu_env *env, struct lov_object *lov,
827 union lov_layout_state *state)
829 LASSERT(lov->lo_type == LLT_EMPTY || lov->lo_type == LLT_RELEASED ||
830 lov->lo_type == LLT_FOREIGN);
832 lov_layout_wait(env, lov);
836 static int lov_delete_composite(const struct lu_env *env,
837 struct lov_object *lov,
838 union lov_layout_state *state)
840 struct lov_layout_entry *entry;
841 struct lov_layout_composite *comp = &state->composite;
845 dump_lsm(D_INODE, lov->lo_lsm);
847 lov_layout_wait(env, lov);
848 if (comp->lo_entries)
849 lov_foreach_layout_entry(lov, entry)
850 lov_delete_raid0(env, lov, entry);
855 static void lov_fini_empty(const struct lu_env *env, struct lov_object *lov,
856 union lov_layout_state *state)
858 LASSERT(lov->lo_type == LLT_EMPTY || lov->lo_type == LLT_RELEASED);
861 static void lov_fini_composite(const struct lu_env *env,
862 struct lov_object *lov,
863 union lov_layout_state *state)
865 struct lov_layout_composite *comp = &state->composite;
868 if (comp->lo_entries != NULL) {
869 struct lov_layout_entry *entry;
871 lov_foreach_layout_entry(lov, entry)
872 if (entry->lle_comp_ops)
873 entry->lle_comp_ops->lco_fini(env, entry);
875 OBD_FREE_PTR_ARRAY(comp->lo_entries, comp->lo_entry_count);
876 comp->lo_entries = NULL;
879 if (comp->lo_mirrors != NULL) {
880 OBD_FREE_PTR_ARRAY(comp->lo_mirrors, comp->lo_mirror_count);
881 comp->lo_mirrors = NULL;
884 memset(comp, 0, sizeof(*comp));
886 dump_lsm(D_INODE, lov->lo_lsm);
887 lov_free_memmd(&lov->lo_lsm);
892 static void lov_fini_released(const struct lu_env *env, struct lov_object *lov,
893 union lov_layout_state *state)
896 dump_lsm(D_INODE, lov->lo_lsm);
897 lov_free_memmd(&lov->lo_lsm);
901 static int lov_print_empty(const struct lu_env *env, void *cookie,
902 lu_printer_t p, const struct lu_object *o)
904 (*p)(env, cookie, "empty %d\n", lu2lov(o)->lo_layout_invalid);
908 static int lov_print_composite(const struct lu_env *env, void *cookie,
909 lu_printer_t p, const struct lu_object *o)
911 struct lov_object *lov = lu2lov(o);
912 struct lov_stripe_md *lsm = lov->lo_lsm;
915 (*p)(env, cookie, "entries: %d, %s, lsm{%p 0x%08X %d %u}:\n",
916 lsm->lsm_entry_count,
917 lov->lo_layout_invalid ? "invalid" : "valid", lsm,
918 lsm->lsm_magic, atomic_read(&lsm->lsm_refc),
919 lsm->lsm_layout_gen);
921 for (i = 0; i < lsm->lsm_entry_count; i++) {
922 struct lov_stripe_md_entry *lse = lsm->lsm_entries[i];
923 struct lov_layout_entry *lle = lov_entry(lov, i);
926 DEXT ": { 0x%08X, %u, %#x, %u, %#x, %u, %u }\n",
927 PEXT(&lse->lsme_extent), lse->lsme_magic,
928 lse->lsme_id, lse->lsme_pattern, lse->lsme_layout_gen,
929 lse->lsme_flags, lse->lsme_stripe_count,
930 lse->lsme_stripe_size);
931 lov_print_raid0(env, cookie, p, lle);
937 static int lov_print_released(const struct lu_env *env, void *cookie,
938 lu_printer_t p, const struct lu_object *o)
940 struct lov_object *lov = lu2lov(o);
941 struct lov_stripe_md *lsm = lov->lo_lsm;
944 "released: %s, lsm{%p 0x%08X %d %u}:\n",
945 lov->lo_layout_invalid ? "invalid" : "valid", lsm,
946 lsm->lsm_magic, atomic_read(&lsm->lsm_refc),
947 lsm->lsm_layout_gen);
951 static int lov_print_foreign(const struct lu_env *env, void *cookie,
952 lu_printer_t p, const struct lu_object *o)
954 struct lov_object *lov = lu2lov(o);
955 struct lov_stripe_md *lsm = lov->lo_lsm;
958 "foreign: %s, lsm{%p 0x%08X %d %u}:\n",
959 lov->lo_layout_invalid ? "invalid" : "valid", lsm,
960 lsm->lsm_magic, atomic_read(&lsm->lsm_refc),
961 lsm->lsm_layout_gen);
963 "raw_ea_content '%.*s'\n",
964 (int)lsm->lsm_foreign_size, (char *)lsm_foreign(lsm));
969 * Implements cl_object_operations::coo_attr_get() method for an object
970 * without stripes (LLT_EMPTY layout type).
972 * The only attributes this layer is authoritative in this case is
973 * cl_attr::cat_blocks---it's 0.
975 static int lov_attr_get_empty(const struct lu_env *env, struct cl_object *obj,
976 struct cl_attr *attr)
978 attr->cat_blocks = 0;
982 static int lov_attr_get_composite(const struct lu_env *env,
983 struct cl_object *obj,
984 struct cl_attr *attr)
986 struct lov_object *lov = cl2lov(obj);
987 struct lov_layout_entry *entry;
993 attr->cat_blocks = 0;
994 lov_foreach_layout_entry(lov, entry) {
995 struct cl_attr *lov_attr = NULL;
996 int index = lov_layout_entry_index(lov, entry);
998 if (!entry->lle_valid)
1001 /* PFL: This component has not been init-ed. */
1002 if (!lsm_entry_inited(lov->lo_lsm, index))
1005 result = entry->lle_comp_ops->lco_getattr(env, lov, index,
1010 if (lov_attr == NULL)
1013 CDEBUG(D_INODE, "COMP ID #%i: s=%llu m=%llu a=%llu c=%llu "
1014 "b=%llu\n", index - 1, lov_attr->cat_size,
1015 lov_attr->cat_mtime, lov_attr->cat_atime,
1016 lov_attr->cat_ctime, lov_attr->cat_blocks);
1019 attr->cat_blocks += lov_attr->cat_blocks;
1020 if (attr->cat_size < lov_attr->cat_size)
1021 attr->cat_size = lov_attr->cat_size;
1022 if (attr->cat_kms < lov_attr->cat_kms)
1023 attr->cat_kms = lov_attr->cat_kms;
1024 if (attr->cat_atime < lov_attr->cat_atime)
1025 attr->cat_atime = lov_attr->cat_atime;
1026 if (attr->cat_ctime < lov_attr->cat_ctime)
1027 attr->cat_ctime = lov_attr->cat_ctime;
1028 if (attr->cat_mtime < lov_attr->cat_mtime)
1029 attr->cat_mtime = lov_attr->cat_mtime;
1035 static int lov_flush_composite(const struct lu_env *env,
1036 struct cl_object *obj,
1037 struct ldlm_lock *lock)
1039 struct lov_object *lov = cl2lov(obj);
1040 struct lov_layout_entry *lle;
1045 lov_foreach_layout_entry(lov, lle) {
1046 if (!lsme_is_dom(lle->lle_lsme))
1048 rc = cl_object_flush(env, lovsub2cl(lle->lle_dom.lo_dom), lock);
1055 static int lov_flush_empty(const struct lu_env *env, struct cl_object *obj,
1056 struct ldlm_lock *lock)
1061 const static struct lov_layout_operations lov_dispatch[] = {
1063 .llo_init = lov_init_empty,
1064 .llo_delete = lov_delete_empty,
1065 .llo_fini = lov_fini_empty,
1066 .llo_print = lov_print_empty,
1067 .llo_page_init = lov_page_init_empty,
1068 .llo_lock_init = lov_lock_init_empty,
1069 .llo_io_init = lov_io_init_empty,
1070 .llo_getattr = lov_attr_get_empty,
1071 .llo_flush = lov_flush_empty,
1074 .llo_init = lov_init_released,
1075 .llo_delete = lov_delete_empty,
1076 .llo_fini = lov_fini_released,
1077 .llo_print = lov_print_released,
1078 .llo_page_init = lov_page_init_empty,
1079 .llo_lock_init = lov_lock_init_empty,
1080 .llo_io_init = lov_io_init_released,
1081 .llo_getattr = lov_attr_get_empty,
1082 .llo_flush = lov_flush_empty,
1085 .llo_init = lov_init_composite,
1086 .llo_delete = lov_delete_composite,
1087 .llo_fini = lov_fini_composite,
1088 .llo_print = lov_print_composite,
1089 .llo_page_init = lov_page_init_composite,
1090 .llo_lock_init = lov_lock_init_composite,
1091 .llo_io_init = lov_io_init_composite,
1092 .llo_getattr = lov_attr_get_composite,
1093 .llo_flush = lov_flush_composite,
1096 .llo_init = lov_init_foreign,
1097 .llo_delete = lov_delete_empty,
1098 .llo_fini = lov_fini_released,
1099 .llo_print = lov_print_foreign,
1100 .llo_page_init = lov_page_init_foreign,
1101 .llo_lock_init = lov_lock_init_empty,
1102 .llo_io_init = lov_io_init_empty,
1103 .llo_getattr = lov_attr_get_empty,
1104 .llo_flush = lov_flush_empty,
1109 * Performs a double-dispatch based on the layout type of an object.
1111 #define LOV_2DISPATCH_NOLOCK(obj, op, ...) \
1113 struct lov_object *__obj = (obj); \
1114 enum lov_layout_type __llt; \
1116 __llt = __obj->lo_type; \
1117 LASSERT(__llt < ARRAY_SIZE(lov_dispatch)); \
1118 lov_dispatch[__llt].op(__VA_ARGS__); \
1122 * Return lov_layout_type associated with a given lsm
1124 static enum lov_layout_type lov_type(struct lov_stripe_md *lsm)
1129 if (lsm->lsm_is_released)
1130 return LLT_RELEASED;
1132 if (lsm->lsm_magic == LOV_MAGIC_V1 ||
1133 lsm->lsm_magic == LOV_MAGIC_V3 ||
1134 lsm->lsm_magic == LOV_MAGIC_COMP_V1)
1137 if (lsm->lsm_magic == LOV_MAGIC_FOREIGN)
1143 static inline void lov_conf_freeze(struct lov_object *lov)
1145 CDEBUG(D_INODE, "To take share lov(%p) owner %p/%p\n",
1146 lov, lov->lo_owner, current);
1147 if (lov->lo_owner != current)
1148 down_read(&lov->lo_type_guard);
1151 static inline void lov_conf_thaw(struct lov_object *lov)
1153 CDEBUG(D_INODE, "To release share lov(%p) owner %p/%p\n",
1154 lov, lov->lo_owner, current);
1155 if (lov->lo_owner != current)
1156 up_read(&lov->lo_type_guard);
1159 #define LOV_2DISPATCH_MAYLOCK(obj, op, lock, ...) \
1161 struct lov_object *__obj = (obj); \
1162 int __lock = !!(lock); \
1163 typeof(lov_dispatch[0].op(__VA_ARGS__)) __result; \
1166 lov_conf_freeze(__obj); \
1167 __result = LOV_2DISPATCH_NOLOCK(obj, op, __VA_ARGS__); \
1169 lov_conf_thaw(__obj); \
1174 * Performs a locked double-dispatch based on the layout type of an object.
1176 #define LOV_2DISPATCH(obj, op, ...) \
1177 LOV_2DISPATCH_MAYLOCK(obj, op, 1, __VA_ARGS__)
1179 #define LOV_2DISPATCH_VOID(obj, op, ...) \
1181 struct lov_object *__obj = (obj); \
1182 enum lov_layout_type __llt; \
1184 lov_conf_freeze(__obj); \
1185 __llt = __obj->lo_type; \
1186 LASSERT(__llt < ARRAY_SIZE(lov_dispatch)); \
1187 lov_dispatch[__llt].op(__VA_ARGS__); \
1188 lov_conf_thaw(__obj); \
1191 static void lov_conf_lock(struct lov_object *lov)
1193 LASSERT(lov->lo_owner != current);
1194 down_write(&lov->lo_type_guard);
1195 LASSERT(lov->lo_owner == NULL);
1196 lov->lo_owner = current;
1197 CDEBUG(D_INODE, "Took exclusive lov(%p) owner %p\n",
1198 lov, lov->lo_owner);
1201 static void lov_conf_unlock(struct lov_object *lov)
1203 CDEBUG(D_INODE, "To release exclusive lov(%p) owner %p\n",
1204 lov, lov->lo_owner);
1205 lov->lo_owner = NULL;
1206 up_write(&lov->lo_type_guard);
1209 static int lov_layout_wait(const struct lu_env *env, struct lov_object *lov)
1213 while (atomic_read(&lov->lo_active_ios) > 0) {
1214 CDEBUG(D_INODE, "file:"DFID" wait for active IO, now: %d.\n",
1215 PFID(lu_object_fid(lov2lu(lov))),
1216 atomic_read(&lov->lo_active_ios));
1218 wait_event_idle(lov->lo_waitq,
1219 atomic_read(&lov->lo_active_ios) == 0);
1224 static int lov_layout_change(const struct lu_env *unused,
1225 struct lov_object *lov, struct lov_stripe_md *lsm,
1226 const struct cl_object_conf *conf)
1228 enum lov_layout_type llt = lov_type(lsm);
1229 union lov_layout_state *state = &lov->u;
1230 const struct lov_layout_operations *old_ops;
1231 const struct lov_layout_operations *new_ops;
1232 struct lov_device *lov_dev = lov_object_dev(lov);
1238 LASSERT(lov->lo_type < ARRAY_SIZE(lov_dispatch));
1240 env = cl_env_get(&refcheck);
1242 RETURN(PTR_ERR(env));
1244 LASSERT(llt < ARRAY_SIZE(lov_dispatch));
1246 CDEBUG(D_INODE, DFID" from %s to %s\n",
1247 PFID(lu_object_fid(lov2lu(lov))),
1248 llt2str(lov->lo_type), llt2str(llt));
1250 old_ops = &lov_dispatch[lov->lo_type];
1251 new_ops = &lov_dispatch[llt];
1253 rc = cl_object_prune(env, &lov->lo_cl);
1257 rc = old_ops->llo_delete(env, lov, &lov->u);
1261 old_ops->llo_fini(env, lov, &lov->u);
1263 LASSERT(atomic_read(&lov->lo_active_ios) == 0);
1265 CDEBUG(D_INODE, DFID "Apply new layout lov %p, type %d\n",
1266 PFID(lu_object_fid(lov2lu(lov))), lov, llt);
1268 /* page bufsize fixup */
1269 cl_object_header(&lov->lo_cl)->coh_page_bufsize -=
1270 lov_page_slice_fixup(lov, NULL);
1273 rc = new_ops->llo_init(env, lov_dev, lov, lsm, conf, state);
1275 struct obd_device *obd = lov2obd(lov_dev->ld_lov);
1277 CERROR("%s: cannot apply new layout on "DFID" : rc = %d\n",
1278 obd->obd_name, PFID(lu_object_fid(lov2lu(lov))), rc);
1279 new_ops->llo_delete(env, lov, state);
1280 new_ops->llo_fini(env, lov, state);
1281 /* this file becomes an EMPTY file. */
1282 lov->lo_type = LLT_EMPTY;
1287 cl_env_put(env, &refcheck);
1291 /*****************************************************************************
1293 * Lov object operations.
1296 int lov_object_init(const struct lu_env *env, struct lu_object *obj,
1297 const struct lu_object_conf *conf)
1299 struct lov_object *lov = lu2lov(obj);
1300 struct lov_device *dev = lov_object_dev(lov);
1301 const struct cl_object_conf *cconf = lu2cl_conf(conf);
1302 union lov_layout_state *set = &lov->u;
1303 const struct lov_layout_operations *ops;
1304 struct lov_stripe_md *lsm = NULL;
1308 init_rwsem(&lov->lo_type_guard);
1309 atomic_set(&lov->lo_active_ios, 0);
1310 init_waitqueue_head(&lov->lo_waitq);
1311 cl_object_page_init(lu2cl(obj), sizeof(struct lov_page));
1313 lov->lo_type = LLT_EMPTY;
1314 if (cconf->u.coc_layout.lb_buf != NULL) {
1315 lsm = lov_unpackmd(dev->ld_lov,
1316 cconf->u.coc_layout.lb_buf,
1317 cconf->u.coc_layout.lb_len);
1319 RETURN(PTR_ERR(lsm));
1321 dump_lsm(D_INODE, lsm);
1324 /* no locking is necessary, as object is being created */
1325 lov->lo_type = lov_type(lsm);
1326 ops = &lov_dispatch[lov->lo_type];
1327 rc = ops->llo_init(env, dev, lov, lsm, cconf, set);
1337 static int lov_conf_set(const struct lu_env *env, struct cl_object *obj,
1338 const struct cl_object_conf *conf)
1340 struct lov_stripe_md *lsm = NULL;
1341 struct lov_object *lov = cl2lov(obj);
1345 if (conf->coc_opc == OBJECT_CONF_SET &&
1346 conf->u.coc_layout.lb_buf != NULL) {
1347 lsm = lov_unpackmd(lov_object_dev(lov)->ld_lov,
1348 conf->u.coc_layout.lb_buf,
1349 conf->u.coc_layout.lb_len);
1351 RETURN(PTR_ERR(lsm));
1352 dump_lsm(D_INODE, lsm);
1356 if (conf->coc_opc == OBJECT_CONF_INVALIDATE) {
1357 lov->lo_layout_invalid = true;
1358 GOTO(out, result = 0);
1361 if (conf->coc_opc == OBJECT_CONF_WAIT) {
1362 if (lov->lo_layout_invalid &&
1363 atomic_read(&lov->lo_active_ios) > 0) {
1364 lov_conf_unlock(lov);
1365 result = lov_layout_wait(env, lov);
1371 LASSERT(conf->coc_opc == OBJECT_CONF_SET);
1373 if ((lsm == NULL && lov->lo_lsm == NULL) ||
1374 ((lsm != NULL && lov->lo_lsm != NULL) &&
1375 (lov->lo_lsm->lsm_layout_gen == lsm->lsm_layout_gen) &&
1376 (lov->lo_lsm->lsm_entries[0]->lsme_pattern ==
1377 lsm->lsm_entries[0]->lsme_pattern))) {
1378 /* same version of layout */
1379 lov->lo_layout_invalid = false;
1380 GOTO(out, result = 0);
1383 /* will change layout - check if there still exists active IO. */
1384 if (atomic_read(&lov->lo_active_ios) > 0) {
1385 lov->lo_layout_invalid = true;
1386 GOTO(out, result = -EBUSY);
1389 result = lov_layout_change(env, lov, lsm, conf);
1390 lov->lo_layout_invalid = result != 0;
1394 lov_conf_unlock(lov);
1396 CDEBUG(D_INODE, DFID" lo_layout_invalid=%d\n",
1397 PFID(lu_object_fid(lov2lu(lov))), lov->lo_layout_invalid);
1401 static void lov_object_delete(const struct lu_env *env, struct lu_object *obj)
1403 struct lov_object *lov = lu2lov(obj);
1406 LOV_2DISPATCH_VOID(lov, llo_delete, env, lov, &lov->u);
1410 static void lov_object_free(const struct lu_env *env, struct lu_object *obj)
1412 struct lov_object *lov = lu2lov(obj);
1415 LOV_2DISPATCH_VOID(lov, llo_fini, env, lov, &lov->u);
1416 lu_object_fini(obj);
1417 OBD_SLAB_FREE_PTR(lov, lov_object_kmem);
1421 static int lov_object_print(const struct lu_env *env, void *cookie,
1422 lu_printer_t p, const struct lu_object *o)
1424 return LOV_2DISPATCH_NOLOCK(lu2lov(o), llo_print, env, cookie, p, o);
1427 int lov_page_init(const struct lu_env *env, struct cl_object *obj,
1428 struct cl_page *page, pgoff_t index)
1430 return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_page_init, env, obj, page,
1435 * Implements cl_object_operations::clo_io_init() method for lov
1436 * layer. Dispatches to the appropriate layout io initialization method.
1438 int lov_io_init(const struct lu_env *env, struct cl_object *obj,
1441 CL_IO_SLICE_CLEAN(lov_env_io(env), lis_preserved);
1443 CDEBUG(D_INODE, DFID "io %p type %d ignore/verify layout %d/%d\n",
1444 PFID(lu_object_fid(&obj->co_lu)), io, io->ci_type,
1445 io->ci_ignore_layout, io->ci_verify_layout);
1447 /* IO type CIT_MISC with ci_ignore_layout set are usually invoked from
1448 * the OSC layer. It shouldn't take lov layout conf lock in that case,
1449 * because as long as the OSC object exists, the layout can't be
1451 return LOV_2DISPATCH_MAYLOCK(cl2lov(obj), llo_io_init,
1452 !(io->ci_ignore_layout && io->ci_type == CIT_MISC),
1457 * An implementation of cl_object_operations::clo_attr_get() method for lov
1458 * layer. For raid0 layout this collects and merges attributes of all
1461 static int lov_attr_get(const struct lu_env *env, struct cl_object *obj,
1462 struct cl_attr *attr)
1464 /* do not take lock, as this function is called under a
1465 * spin-lock. Layout is protected from changing by ongoing IO. */
1466 return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_getattr, env, obj, attr);
1469 static int lov_attr_update(const struct lu_env *env, struct cl_object *obj,
1470 const struct cl_attr *attr, unsigned valid)
1473 * No dispatch is required here, as no layout implements this.
1478 int lov_lock_init(const struct lu_env *env, struct cl_object *obj,
1479 struct cl_lock *lock, const struct cl_io *io)
1481 /* No need to lock because we've taken one refcount of layout. */
1482 return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_lock_init, env, obj, lock,
1487 * We calculate on which OST the mapping will end. If the length of mapping
1488 * is greater than (stripe_size * stripe_count) then the last_stripe will
1489 * will be one just before start_stripe. Else we check if the mapping
1490 * intersects each OST and find last_stripe.
1491 * This function returns the last_stripe and also sets the stripe_count
1492 * over which the mapping is spread
1494 * \param lsm [in] striping information for the file
1495 * \param index [in] stripe component index
1496 * \param ext [in] logical extent of mapping
1497 * \param start_stripe [in] starting stripe of the mapping
1498 * \param stripe_count [out] the number of stripes across which to map is
1501 * \retval last_stripe return the last stripe of the mapping
1503 static int fiemap_calc_last_stripe(struct lov_stripe_md *lsm, int index,
1504 struct lu_extent *ext,
1505 int start_stripe, int *stripe_count)
1507 struct lov_stripe_md_entry *lsme = lsm->lsm_entries[index];
1513 if (ext->e_end - ext->e_start >
1514 lsme->lsme_stripe_size * lsme->lsme_stripe_count) {
1515 last_stripe = (start_stripe < 1 ? lsme->lsme_stripe_count - 1 :
1517 *stripe_count = lsme->lsme_stripe_count;
1519 for (j = 0, i = start_stripe; j < lsme->lsme_stripe_count;
1520 i = (i + 1) % lsme->lsme_stripe_count, j++) {
1521 if ((lov_stripe_intersects(lsm, index, i, ext,
1522 &obd_start, &obd_end)) == 0)
1526 last_stripe = (start_stripe + j - 1) % lsme->lsme_stripe_count;
1533 * Set fe_device and copy extents from local buffer into main return buffer.
1535 * \param fiemap [out] fiemap to hold all extents
1536 * \param lcl_fm_ext [in] array of fiemap extents get from OSC layer
1537 * \param ost_index [in] OST index to be written into the fm_device
1538 * field for each extent
1539 * \param ext_count [in] number of extents to be copied
1540 * \param current_extent [in] where to start copying in the extent array
1542 static void fiemap_prepare_and_copy_exts(struct fiemap *fiemap,
1543 struct fiemap_extent *lcl_fm_ext,
1544 int ost_index, unsigned int ext_count,
1550 for (ext = 0; ext < ext_count; ext++) {
1551 lcl_fm_ext[ext].fe_device = ost_index;
1552 lcl_fm_ext[ext].fe_flags |= FIEMAP_EXTENT_NET;
1555 /* Copy fm_extent's from fm_local to return buffer */
1556 to = (char *)fiemap + fiemap_count_to_size(current_extent);
1557 memcpy(to, lcl_fm_ext, ext_count * sizeof(struct fiemap_extent));
1560 #define FIEMAP_BUFFER_SIZE 4096
1563 * Non-zero fe_logical indicates that this is a continuation FIEMAP
1564 * call. The local end offset and the device are sent in the first
1565 * fm_extent. This function calculates the stripe number from the index.
1566 * This function returns a stripe_no on which mapping is to be restarted.
1568 * This function returns fm_end_offset which is the in-OST offset at which
1569 * mapping should be restarted. If fm_end_offset=0 is returned then caller
1570 * will re-calculate proper offset in next stripe.
1571 * Note that the first extent is passed to lov_get_info via the value field.
1573 * \param fiemap [in] fiemap request header
1574 * \param lsm [in] striping information for the file
1575 * \param index [in] stripe component index
1576 * \param ext [in] logical extent of mapping
1577 * \param start_stripe [out] starting stripe will be returned in this
1579 static u64 fiemap_calc_fm_end_offset(struct fiemap *fiemap,
1580 struct lov_stripe_md *lsm,
1581 int index, struct lu_extent *ext,
1584 struct lov_stripe_md_entry *lsme = lsm->lsm_entries[index];
1585 u64 local_end = fiemap->fm_extents[0].fe_logical;
1592 if (fiemap->fm_extent_count == 0 ||
1593 fiemap->fm_extents[0].fe_logical == 0)
1596 /* Find out stripe_no from ost_index saved in the fe_device */
1597 for (i = 0; i < lsme->lsme_stripe_count; i++) {
1598 struct lov_oinfo *oinfo = lsme->lsme_oinfo[i];
1600 if (lov_oinfo_is_dummy(oinfo))
1603 if (oinfo->loi_ost_idx == fiemap->fm_extents[0].fe_device) {
1609 if (stripe_no == -1)
1612 /* If we have finished mapping on previous device, shift logical
1613 * offset to start of next device */
1614 if (lov_stripe_intersects(lsm, index, stripe_no, ext,
1615 &lun_start, &lun_end) != 0 &&
1616 local_end < lun_end) {
1617 fm_end_offset = local_end;
1618 *start_stripe = stripe_no;
1620 /* This is a special value to indicate that caller should
1621 * calculate offset in next stripe. */
1623 *start_stripe = (stripe_no + 1) % lsme->lsme_stripe_count;
1626 return fm_end_offset;
1629 struct fiemap_state {
1630 struct fiemap *fs_fm;
1631 struct lu_extent fs_ext;
1636 int fs_start_stripe;
1638 bool fs_device_done;
1639 bool fs_finish_stripe;
1643 static struct cl_object *lov_find_subobj(const struct lu_env *env,
1644 struct lov_object *lov,
1645 struct lov_stripe_md *lsm,
1648 struct lov_device *dev = lu2lov_dev(lov2lu(lov)->lo_dev);
1649 struct lov_thread_info *lti = lov_env_info(env);
1650 struct lu_fid *ofid = <i->lti_fid;
1651 struct lov_oinfo *oinfo;
1652 struct cl_device *subdev;
1653 int entry = lov_comp_entry(index);
1654 int stripe = lov_comp_stripe(index);
1657 struct cl_object *result;
1659 if (lov->lo_type != LLT_COMP)
1660 GOTO(out, result = NULL);
1662 if (entry >= lsm->lsm_entry_count ||
1663 stripe >= lsm->lsm_entries[entry]->lsme_stripe_count)
1664 GOTO(out, result = NULL);
1666 oinfo = lsm->lsm_entries[entry]->lsme_oinfo[stripe];
1667 ost_idx = oinfo->loi_ost_idx;
1668 rc = ostid_to_fid(ofid, &oinfo->loi_oi, ost_idx);
1670 GOTO(out, result = NULL);
1672 subdev = lovsub2cl_dev(dev->ld_target[ost_idx]);
1673 result = lov_sub_find(env, subdev, ofid, NULL);
1676 result = ERR_PTR(-EINVAL);
1680 int fiemap_for_stripe(const struct lu_env *env, struct cl_object *obj,
1681 struct lov_stripe_md *lsm, struct fiemap *fiemap,
1682 size_t *buflen, struct ll_fiemap_info_key *fmkey,
1683 int index, int stripeno, struct fiemap_state *fs)
1685 struct lov_stripe_md_entry *lsme = lsm->lsm_entries[index];
1686 struct cl_object *subobj;
1687 struct lov_obd *lov = lu2lov_dev(obj->co_lu.lo_dev)->ld_lov;
1688 struct fiemap_extent *fm_ext = &fs->fs_fm->fm_extents[0];
1689 u64 req_fm_len; /* Stores length of required mapping */
1690 u64 len_mapped_single_call;
1694 unsigned int ext_count;
1695 /* EOF for object */
1696 bool ost_eof = false;
1697 /* done with required mapping for this OST? */
1698 bool ost_done = false;
1702 fs->fs_device_done = false;
1703 /* Find out range of mapping on this stripe */
1704 if ((lov_stripe_intersects(lsm, index, stripeno, &fs->fs_ext,
1705 &lun_start, &obd_object_end)) == 0)
1708 if (lov_oinfo_is_dummy(lsme->lsme_oinfo[stripeno]))
1711 /* If this is a continuation FIEMAP call and we are on
1712 * starting stripe then lun_start needs to be set to
1714 if (fs->fs_end_offset != 0 && stripeno == fs->fs_start_stripe)
1715 lun_start = fs->fs_end_offset;
1716 lun_end = lov_size_to_stripe(lsm, index, fs->fs_ext.e_end, stripeno);
1717 if (lun_start == lun_end)
1720 req_fm_len = obd_object_end - lun_start + 1;
1721 fs->fs_fm->fm_length = 0;
1722 len_mapped_single_call = 0;
1724 /* find lobsub object */
1725 subobj = lov_find_subobj(env, cl2lov(obj), lsm,
1726 lov_comp_index(index, stripeno));
1728 return PTR_ERR(subobj);
1729 /* If the output buffer is very large and the objects have many
1730 * extents we may need to loop on a single OST repeatedly */
1732 if (fiemap->fm_extent_count > 0) {
1733 /* Don't get too many extents. */
1734 if (fs->fs_cur_extent + fs->fs_cnt_need >
1735 fiemap->fm_extent_count)
1736 fs->fs_cnt_need = fiemap->fm_extent_count -
1740 lun_start += len_mapped_single_call;
1741 fs->fs_fm->fm_length = req_fm_len - len_mapped_single_call;
1742 req_fm_len = fs->fs_fm->fm_length;
1744 * If we've collected enough extent map, we'd request 1 more,
1745 * to see whether we coincidentally finished all available
1746 * extent map, so that FIEMAP_EXTENT_LAST would be set.
1748 fs->fs_fm->fm_extent_count = fs->fs_enough ?
1749 1 : fs->fs_cnt_need;
1750 fs->fs_fm->fm_mapped_extents = 0;
1751 fs->fs_fm->fm_flags = fiemap->fm_flags;
1753 ost_index = lsme->lsme_oinfo[stripeno]->loi_ost_idx;
1755 if (ost_index < 0 || ost_index >= lov->desc.ld_tgt_count)
1756 GOTO(obj_put, rc = -EINVAL);
1757 /* If OST is inactive, return extent with UNKNOWN flag. */
1758 if (!lov->lov_tgts[ost_index]->ltd_active) {
1759 fs->fs_fm->fm_flags |= FIEMAP_EXTENT_LAST;
1760 fs->fs_fm->fm_mapped_extents = 1;
1762 fm_ext[0].fe_logical = lun_start;
1763 fm_ext[0].fe_length = obd_object_end - lun_start + 1;
1764 fm_ext[0].fe_flags |= FIEMAP_EXTENT_UNKNOWN;
1769 fs->fs_fm->fm_start = lun_start;
1770 fs->fs_fm->fm_flags &= ~FIEMAP_FLAG_DEVICE_ORDER;
1771 memcpy(&fmkey->lfik_fiemap, fs->fs_fm, sizeof(*fs->fs_fm));
1772 *buflen = fiemap_count_to_size(fs->fs_fm->fm_extent_count);
1774 rc = cl_object_fiemap(env, subobj, fmkey, fs->fs_fm, buflen);
1778 ext_count = fs->fs_fm->fm_mapped_extents;
1779 if (ext_count == 0) {
1781 fs->fs_device_done = true;
1782 /* If last stripe has hold at the end,
1783 * we need to return */
1784 if (stripeno == fs->fs_last_stripe) {
1785 fiemap->fm_mapped_extents = 0;
1786 fs->fs_finish_stripe = true;
1790 } else if (fs->fs_enough) {
1792 * We've collected enough extents and there are
1793 * more extents after it.
1798 /* If we just need num of extents, got to next device */
1799 if (fiemap->fm_extent_count == 0) {
1800 fs->fs_cur_extent += ext_count;
1804 /* prepare to copy retrived map extents */
1805 len_mapped_single_call = fm_ext[ext_count - 1].fe_logical +
1806 fm_ext[ext_count - 1].fe_length -
1809 /* Have we finished mapping on this device? */
1810 if (req_fm_len <= len_mapped_single_call) {
1812 fs->fs_device_done = true;
1815 /* Clear the EXTENT_LAST flag which can be present on
1816 * the last extent */
1817 if (fm_ext[ext_count - 1].fe_flags & FIEMAP_EXTENT_LAST)
1818 fm_ext[ext_count - 1].fe_flags &= ~FIEMAP_EXTENT_LAST;
1819 if (lov_stripe_size(lsm, index,
1820 fm_ext[ext_count - 1].fe_logical +
1821 fm_ext[ext_count - 1].fe_length,
1822 stripeno) >= fmkey->lfik_oa.o_size) {
1824 fs->fs_device_done = true;
1827 fiemap_prepare_and_copy_exts(fiemap, fm_ext, ost_index,
1828 ext_count, fs->fs_cur_extent);
1829 fs->fs_cur_extent += ext_count;
1831 /* Ran out of available extents? */
1832 if (fs->fs_cur_extent >= fiemap->fm_extent_count)
1833 fs->fs_enough = true;
1834 } while (!ost_done && !ost_eof);
1836 if (stripeno == fs->fs_last_stripe)
1837 fs->fs_finish_stripe = true;
1839 cl_object_put(env, subobj);
1845 * Break down the FIEMAP request and send appropriate calls to individual OSTs.
1846 * This also handles the restarting of FIEMAP calls in case mapping overflows
1847 * the available number of extents in single call.
1849 * \param env [in] lustre environment
1850 * \param obj [in] file object
1851 * \param fmkey [in] fiemap request header and other info
1852 * \param fiemap [out] fiemap buffer holding retrived map extents
1853 * \param buflen [in/out] max buffer length of @fiemap, when iterate
1854 * each OST, it is used to limit max map needed
1858 static int lov_object_fiemap(const struct lu_env *env, struct cl_object *obj,
1859 struct ll_fiemap_info_key *fmkey,
1860 struct fiemap *fiemap, size_t *buflen)
1862 struct lov_stripe_md_entry *lsme;
1863 struct lov_stripe_md *lsm;
1864 struct fiemap *fm_local = NULL;
1872 unsigned int buffer_size = FIEMAP_BUFFER_SIZE;
1874 struct fiemap_state fs = { 0 };
1877 lsm = lov_lsm_addref(cl2lov(obj));
1879 /* no extent: there is no object for mapping */
1880 fiemap->fm_mapped_extents = 0;
1884 if (!(fiemap->fm_flags & FIEMAP_FLAG_DEVICE_ORDER)) {
1886 * If the entry count > 1 or stripe_count > 1 and the
1887 * application does not understand DEVICE_ORDER flag,
1888 * it cannot interpret the extents correctly.
1890 if (lsm->lsm_entry_count > 1 ||
1891 (lsm->lsm_entry_count == 1 &&
1892 lsm->lsm_entries[0]->lsme_stripe_count > 1))
1893 GOTO(out_lsm, rc = -ENOTSUPP);
1896 /* No support for DOM layout yet. */
1897 if (lsme_is_dom(lsm->lsm_entries[0]))
1898 GOTO(out_lsm, rc = -ENOTSUPP);
1900 if (lsm->lsm_is_released) {
1901 if (fiemap->fm_start < fmkey->lfik_oa.o_size) {
1903 * released file, return a minimal FIEMAP if
1904 * request fits in file-size.
1906 fiemap->fm_mapped_extents = 1;
1907 fiemap->fm_extents[0].fe_logical = fiemap->fm_start;
1908 if (fiemap->fm_start + fiemap->fm_length <
1909 fmkey->lfik_oa.o_size)
1910 fiemap->fm_extents[0].fe_length =
1913 fiemap->fm_extents[0].fe_length =
1914 fmkey->lfik_oa.o_size -
1916 fiemap->fm_extents[0].fe_flags |=
1917 FIEMAP_EXTENT_UNKNOWN | FIEMAP_EXTENT_LAST;
1919 GOTO(out_lsm, rc = 0);
1922 /* buffer_size is small to hold fm_extent_count of extents. */
1923 if (fiemap_count_to_size(fiemap->fm_extent_count) < buffer_size)
1924 buffer_size = fiemap_count_to_size(fiemap->fm_extent_count);
1926 OBD_ALLOC_LARGE(fm_local, buffer_size);
1927 if (fm_local == NULL)
1928 GOTO(out_lsm, rc = -ENOMEM);
1931 * Requested extent count exceeds the fiemap buffer size, shrink our
1934 if (fiemap_count_to_size(fiemap->fm_extent_count) > *buflen)
1935 fiemap->fm_extent_count = fiemap_size_to_count(*buflen);
1936 if (fiemap->fm_extent_count == 0)
1939 fs.fs_enough = false;
1940 fs.fs_cur_extent = 0;
1941 fs.fs_fm = fm_local;
1942 fs.fs_cnt_need = fiemap_size_to_count(buffer_size);
1944 whole_start = fiemap->fm_start;
1945 /* whole_start is beyond the end of the file */
1946 if (whole_start > fmkey->lfik_oa.o_size)
1947 GOTO(out_fm_local, rc = -EINVAL);
1948 whole_end = (fiemap->fm_length == OBD_OBJECT_EOF) ?
1949 fmkey->lfik_oa.o_size :
1950 whole_start + fiemap->fm_length - 1;
1952 * If fiemap->fm_length != OBD_OBJECT_EOF but whole_end exceeds file
1955 if (whole_end > fmkey->lfik_oa.o_size)
1956 whole_end = fmkey->lfik_oa.o_size;
1958 start_entry = lov_lsm_entry(lsm, whole_start);
1959 end_entry = lov_lsm_entry(lsm, whole_end);
1960 if (end_entry == -1)
1961 end_entry = lsm->lsm_entry_count - 1;
1963 if (start_entry == -1 || end_entry == -1)
1964 GOTO(out_fm_local, rc = -EINVAL);
1966 /* TODO: rewrite it with lov_foreach_io_layout() */
1967 for (entry = start_entry; entry <= end_entry; entry++) {
1968 lsme = lsm->lsm_entries[entry];
1970 if (!lsme_inited(lsme))
1973 if (entry == start_entry)
1974 fs.fs_ext.e_start = whole_start;
1976 fs.fs_ext.e_start = lsme->lsme_extent.e_start;
1977 if (entry == end_entry)
1978 fs.fs_ext.e_end = whole_end;
1980 fs.fs_ext.e_end = lsme->lsme_extent.e_end - 1;
1981 fs.fs_length = fs.fs_ext.e_end - fs.fs_ext.e_start + 1;
1983 /* Calculate start stripe, last stripe and length of mapping */
1984 fs.fs_start_stripe = lov_stripe_number(lsm, entry,
1986 fs.fs_last_stripe = fiemap_calc_last_stripe(lsm, entry,
1987 &fs.fs_ext, fs.fs_start_stripe,
1989 fs.fs_end_offset = fiemap_calc_fm_end_offset(fiemap, lsm, entry,
1990 &fs.fs_ext, &fs.fs_start_stripe);
1991 /* Check each stripe */
1992 for (cur_stripe = fs.fs_start_stripe; stripe_count > 0;
1994 cur_stripe = (cur_stripe + 1) % lsme->lsme_stripe_count) {
1995 rc = fiemap_for_stripe(env, obj, lsm, fiemap, buflen,
1996 fmkey, entry, cur_stripe, &fs);
1998 GOTO(out_fm_local, rc);
2001 if (fs.fs_finish_stripe)
2003 } /* for each stripe */
2004 } /* for covering layout component */
2006 * We've traversed all components, set @entry to the last component
2007 * entry, it's for the last stripe check.
2011 /* Indicate that we are returning device offsets unless file just has
2013 if (lsm->lsm_entry_count > 1 ||
2014 (lsm->lsm_entry_count == 1 &&
2015 lsm->lsm_entries[0]->lsme_stripe_count > 1))
2016 fiemap->fm_flags |= FIEMAP_FLAG_DEVICE_ORDER;
2018 if (fiemap->fm_extent_count == 0)
2019 goto skip_last_device_calc;
2021 /* Check if we have reached the last stripe and whether mapping for that
2022 * stripe is done. */
2023 if ((cur_stripe == fs.fs_last_stripe) && fs.fs_device_done)
2024 fiemap->fm_extents[fs.fs_cur_extent - 1].fe_flags |=
2026 skip_last_device_calc:
2027 fiemap->fm_mapped_extents = fs.fs_cur_extent;
2029 OBD_FREE_LARGE(fm_local, buffer_size);
2036 static int lov_object_getstripe(const struct lu_env *env, struct cl_object *obj,
2037 struct lov_user_md __user *lum, size_t size)
2039 struct lov_object *lov = cl2lov(obj);
2040 struct lov_stripe_md *lsm;
2044 lsm = lov_lsm_addref(lov);
2048 rc = lov_getstripe(env, cl2lov(obj), lsm, lum, size);
2053 static int lov_object_layout_get(const struct lu_env *env,
2054 struct cl_object *obj,
2055 struct cl_layout *cl)
2057 struct lov_object *lov = cl2lov(obj);
2058 struct lov_stripe_md *lsm = lov_lsm_addref(lov);
2059 struct lu_buf *buf = &cl->cl_buf;
2065 cl->cl_layout_gen = CL_LAYOUT_GEN_EMPTY;
2070 cl->cl_size = lov_comp_md_size(lsm);
2071 cl->cl_layout_gen = lsm->lsm_layout_gen;
2072 cl->cl_is_released = lsm->lsm_is_released;
2073 cl->cl_is_composite = lsm_is_composite(lsm->lsm_magic);
2075 rc = lov_lsm_pack(lsm, buf->lb_buf, buf->lb_len);
2078 RETURN(rc < 0 ? rc : 0);
2081 static loff_t lov_object_maxbytes(struct cl_object *obj)
2083 struct lov_object *lov = cl2lov(obj);
2084 struct lov_stripe_md *lsm = lov_lsm_addref(lov);
2090 maxbytes = lsm->lsm_maxbytes;
2097 static int lov_object_flush(const struct lu_env *env, struct cl_object *obj,
2098 struct ldlm_lock *lock)
2100 return LOV_2DISPATCH_MAYLOCK(cl2lov(obj), llo_flush, true, env, obj,
2104 static const struct cl_object_operations lov_ops = {
2105 .coo_page_init = lov_page_init,
2106 .coo_lock_init = lov_lock_init,
2107 .coo_io_init = lov_io_init,
2108 .coo_attr_get = lov_attr_get,
2109 .coo_attr_update = lov_attr_update,
2110 .coo_conf_set = lov_conf_set,
2111 .coo_getstripe = lov_object_getstripe,
2112 .coo_layout_get = lov_object_layout_get,
2113 .coo_maxbytes = lov_object_maxbytes,
2114 .coo_fiemap = lov_object_fiemap,
2115 .coo_object_flush = lov_object_flush
2118 static const struct lu_object_operations lov_lu_obj_ops = {
2119 .loo_object_init = lov_object_init,
2120 .loo_object_delete = lov_object_delete,
2121 .loo_object_release = NULL,
2122 .loo_object_free = lov_object_free,
2123 .loo_object_print = lov_object_print,
2124 .loo_object_invariant = NULL
2127 struct lu_object *lov_object_alloc(const struct lu_env *env,
2128 const struct lu_object_header *unused,
2129 struct lu_device *dev)
2131 struct lov_object *lov;
2132 struct lu_object *obj;
2135 OBD_SLAB_ALLOC_PTR_GFP(lov, lov_object_kmem, GFP_NOFS);
2138 lu_object_init(obj, NULL, dev);
2139 lov->lo_cl.co_ops = &lov_ops;
2140 lov->lo_type = -1; /* invalid, to catch uninitialized type */
2142 * object io operation vector (cl_object::co_iop) is installed
2143 * later in lov_object_init(), as different vectors are used
2144 * for object with different layouts.
2146 obj->lo_ops = &lov_lu_obj_ops;
2152 struct lov_stripe_md *lov_lsm_addref(struct lov_object *lov)
2154 struct lov_stripe_md *lsm = NULL;
2156 lov_conf_freeze(lov);
2157 if (lov->lo_lsm != NULL) {
2158 lsm = lsm_addref(lov->lo_lsm);
2159 CDEBUG(D_INODE, "lsm %p addref %d/%d by %p.\n",
2160 lsm, atomic_read(&lsm->lsm_refc),
2161 lov->lo_layout_invalid, current);
2167 int lov_read_and_clear_async_rc(struct cl_object *clob)
2169 struct lu_object *luobj;
2173 luobj = lu_object_locate(&cl_object_header(clob)->coh_lu,
2175 if (luobj != NULL) {
2176 struct lov_object *lov = lu2lov(luobj);
2178 lov_conf_freeze(lov);
2179 switch (lov->lo_type) {
2181 struct lov_stripe_md *lsm;
2185 LASSERT(lsm != NULL);
2186 for (i = 0; i < lsm->lsm_entry_count; i++) {
2187 struct lov_stripe_md_entry *lse =
2188 lsm->lsm_entries[i];
2191 if (!lsme_inited(lse))
2194 for (j = 0; j < lse->lsme_stripe_count; j++) {
2195 struct lov_oinfo *loi =
2198 if (lov_oinfo_is_dummy(loi))
2201 if (loi->loi_ar.ar_rc && !rc)
2202 rc = loi->loi_ar.ar_rc;
2203 loi->loi_ar.ar_rc = 0;
2219 EXPORT_SYMBOL(lov_read_and_clear_async_rc);