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);
81 static int lov_layout_wait(const struct lu_env *env, struct lov_object *lov);
83 static void lov_lsm_put(struct lov_stripe_md *lsm)
89 /*****************************************************************************
91 * Lov object layout operations.
95 static struct cl_object *lov_sub_find(const struct lu_env *env,
96 struct cl_device *dev,
97 const struct lu_fid *fid,
98 const struct cl_object_conf *conf)
104 o = lu_object_find_at(env, cl2lu_dev(dev), fid, &conf->coc_lu);
105 LASSERT(ergo(!IS_ERR(o), o->lo_dev->ld_type == &lovsub_device_type));
109 static int lov_page_slice_fixup(struct lov_object *lov,
110 struct cl_object *stripe)
112 struct cl_object_header *hdr = cl_object_header(&lov->lo_cl);
116 return hdr->coh_page_bufsize - lov->lo_cl.co_slice_off -
117 cfs_size_round(sizeof(struct lov_page));
119 cl_object_for_each(o, stripe)
120 o->co_slice_off += hdr->coh_page_bufsize;
122 return cl_object_header(stripe)->coh_page_bufsize;
125 static int lov_init_sub(const struct lu_env *env, struct lov_object *lov,
126 struct cl_object *subobj, struct lov_oinfo *oinfo,
129 struct cl_object_header *hdr;
130 struct cl_object_header *subhdr;
131 struct cl_object_header *parent;
132 int entry = lov_comp_entry(idx);
133 int stripe = lov_comp_stripe(idx);
136 if (OBD_FAIL_CHECK(OBD_FAIL_LOV_INIT)) {
137 /* For sanity:test_206.
138 * Do not leave the object in cache to avoid accessing
139 * freed memory. This is because osc_object is referring to
140 * lov_oinfo of lsm_stripe_data which will be freed due to
142 cl_object_kill(env, subobj);
143 cl_object_put(env, subobj);
147 hdr = cl_object_header(lov2cl(lov));
148 subhdr = cl_object_header(subobj);
150 CDEBUG(D_INODE, DFID"@%p[%d:%d] -> "DFID"@%p: ostid: "DOSTID
151 " ost idx: %d gen: %d\n",
152 PFID(lu_object_fid(&subobj->co_lu)), subhdr, entry, stripe,
153 PFID(lu_object_fid(lov2lu(lov))), hdr, POSTID(&oinfo->loi_oi),
154 oinfo->loi_ost_idx, oinfo->loi_ost_gen);
156 /* reuse ->coh_attr_guard to protect coh_parent change */
157 spin_lock(&subhdr->coh_attr_guard);
158 parent = subhdr->coh_parent;
159 if (parent == NULL) {
160 struct lovsub_object *lso = cl2lovsub(subobj);
162 subhdr->coh_parent = hdr;
163 spin_unlock(&subhdr->coh_attr_guard);
164 subhdr->coh_nesting = hdr->coh_nesting + 1;
165 lu_object_ref_add(&subobj->co_lu, "lov-parent", lov);
166 lso->lso_super = lov;
167 lso->lso_index = idx;
170 struct lu_object *old_obj;
171 struct lov_object *old_lov;
172 unsigned int mask = D_INODE;
174 spin_unlock(&subhdr->coh_attr_guard);
175 old_obj = lu_object_locate(&parent->coh_lu, &lov_device_type);
176 LASSERT(old_obj != NULL);
177 old_lov = cl2lov(lu2cl(old_obj));
178 if (old_lov->lo_layout_invalid) {
179 /* the object's layout has already changed but isn't
181 lu_object_unhash(env, &subobj->co_lu);
188 LU_OBJECT_DEBUG(mask, env, &subobj->co_lu,
189 "stripe %d is already owned.", idx);
190 LU_OBJECT_DEBUG(mask, env, old_obj, "owned.");
191 LU_OBJECT_HEADER(mask, env, lov2lu(lov), "try to own.\n");
192 cl_object_put(env, subobj);
197 static int lov_init_raid0(const struct lu_env *env, struct lov_device *dev,
198 struct lov_object *lov, unsigned int index,
199 const struct cl_object_conf *conf,
200 struct lov_layout_entry *lle)
202 struct lov_layout_raid0 *r0 = &lle->lle_raid0;
203 struct lov_thread_info *lti = lov_env_info(env);
204 struct cl_object_conf *subconf = <i->lti_stripe_conf;
205 struct lu_fid *ofid = <i->lti_fid;
206 struct cl_object *stripe;
207 struct lov_stripe_md_entry *lse = lov_lse(lov, index);
214 spin_lock_init(&r0->lo_sub_lock);
215 r0->lo_nr = lse->lsme_stripe_count;
217 OBD_ALLOC_LARGE(r0->lo_sub, r0->lo_nr * sizeof(r0->lo_sub[0]));
218 if (r0->lo_sub == NULL)
219 GOTO(out, result = -ENOMEM);
223 memset(subconf, 0, sizeof(*subconf));
226 * Create stripe cl_objects.
228 for (i = 0; i < r0->lo_nr; ++i) {
229 struct cl_device *subdev;
230 struct lov_oinfo *oinfo = lse->lsme_oinfo[i];
231 int ost_idx = oinfo->loi_ost_idx;
233 if (lov_oinfo_is_dummy(oinfo))
236 result = ostid_to_fid(ofid, &oinfo->loi_oi, oinfo->loi_ost_idx);
240 if (dev->ld_target[ost_idx] == NULL) {
241 CERROR("%s: OST %04x is not initialized\n",
242 lov2obd(dev->ld_lov)->obd_name, ost_idx);
243 GOTO(out, result = -EIO);
246 subdev = lovsub2cl_dev(dev->ld_target[ost_idx]);
247 subconf->u.coc_oinfo = oinfo;
248 LASSERTF(subdev != NULL, "not init ost %d\n", ost_idx);
249 /* In the function below, .hs_keycmp resolves to
250 * lu_obj_hop_keycmp() */
251 /* coverity[overrun-buffer-val] */
252 stripe = lov_sub_find(env, subdev, ofid, subconf);
254 GOTO(out, result = PTR_ERR(stripe));
256 result = lov_init_sub(env, lov, stripe, oinfo,
257 lov_comp_index(index, i));
258 if (result == -EAGAIN) { /* try again */
265 r0->lo_sub[i] = cl2lovsub(stripe);
267 sz = lov_page_slice_fixup(lov, stripe);
268 LASSERT(ergo(psz > 0, psz == sz));
278 static void lov_subobject_kill(const struct lu_env *env, struct lov_object *lov,
279 struct lov_layout_raid0 *r0,
280 struct lovsub_object *los, int idx)
282 struct cl_object *sub;
283 struct lu_site *site;
284 wait_queue_head_t *wq;
285 wait_queue_entry_t *waiter;
287 LASSERT(r0->lo_sub[idx] == los);
289 sub = lovsub2cl(los);
290 site = sub->co_lu.lo_dev->ld_site;
291 wq = lu_site_wq_from_fid(site, &sub->co_lu.lo_header->loh_fid);
293 cl_object_kill(env, sub);
294 /* release a reference to the sub-object and ... */
295 lu_object_ref_del(&sub->co_lu, "lov-parent", lov);
296 cl_object_put(env, sub);
298 /* ... wait until it is actually destroyed---sub-object clears its
299 * ->lo_sub[] slot in lovsub_object_free() */
300 if (r0->lo_sub[idx] == los) {
301 waiter = &lov_env_info(env)->lti_waiter;
302 init_waitqueue_entry(waiter, current);
303 add_wait_queue(wq, waiter);
304 set_current_state(TASK_UNINTERRUPTIBLE);
306 /* this wait-queue is signaled at the end of
307 * lu_object_free(). */
308 set_current_state(TASK_UNINTERRUPTIBLE);
309 spin_lock(&r0->lo_sub_lock);
310 if (r0->lo_sub[idx] == los) {
311 spin_unlock(&r0->lo_sub_lock);
314 spin_unlock(&r0->lo_sub_lock);
315 set_current_state(TASK_RUNNING);
319 remove_wait_queue(wq, waiter);
321 LASSERT(r0->lo_sub[idx] == NULL);
324 static void lov_delete_raid0(const struct lu_env *env, struct lov_object *lov,
325 struct lov_layout_entry *lle)
327 struct lov_layout_raid0 *r0 = &lle->lle_raid0;
331 if (r0->lo_sub != NULL) {
334 for (i = 0; i < r0->lo_nr; ++i) {
335 struct lovsub_object *los = r0->lo_sub[i];
338 cl_object_prune(env, &los->lso_cl);
340 * If top-level object is to be evicted from
341 * the cache, so are its sub-objects.
343 lov_subobject_kill(env, lov, r0, los, i);
351 static void lov_fini_raid0(const struct lu_env *env,
352 struct lov_layout_entry *lle)
354 struct lov_layout_raid0 *r0 = &lle->lle_raid0;
356 if (r0->lo_sub != NULL) {
357 OBD_FREE_LARGE(r0->lo_sub, r0->lo_nr * sizeof r0->lo_sub[0]);
362 static int lov_print_raid0(const struct lu_env *env, void *cookie,
363 lu_printer_t p, const struct lov_layout_entry *lle)
365 const struct lov_layout_raid0 *r0 = &lle->lle_raid0;
368 for (i = 0; i < r0->lo_nr; ++i) {
369 struct lu_object *sub;
371 if (r0->lo_sub[i] != NULL) {
372 sub = lovsub2lu(r0->lo_sub[i]);
373 lu_object_print(env, cookie, p, sub);
375 (*p)(env, cookie, "sub %d absent\n", i);
381 static int lov_attr_get_raid0(const struct lu_env *env, struct lov_object *lov,
382 unsigned int index, struct lov_layout_entry *lle,
383 struct cl_attr **lov_attr)
385 struct lov_layout_raid0 *r0 = &lle->lle_raid0;
386 struct lov_stripe_md *lsm = lov->lo_lsm;
387 struct ost_lvb *lvb = &lov_env_info(env)->lti_lvb;
388 struct cl_attr *attr = &r0->lo_attr;
392 if (r0->lo_attr_valid) {
397 memset(lvb, 0, sizeof(*lvb));
399 /* XXX: timestamps can be negative by sanity:test_39m,
401 lvb->lvb_atime = LLONG_MIN;
402 lvb->lvb_ctime = LLONG_MIN;
403 lvb->lvb_mtime = LLONG_MIN;
406 * XXX that should be replaced with a loop over sub-objects,
407 * doing cl_object_attr_get() on them. But for now, let's
408 * reuse old lov code.
412 * XXX take lsm spin-lock to keep lov_merge_lvb_kms()
413 * happy. It's not needed, because new code uses
414 * ->coh_attr_guard spin-lock to protect consistency of
415 * sub-object attributes.
417 lov_stripe_lock(lsm);
418 result = lov_merge_lvb_kms(lsm, index, lvb, &kms);
419 lov_stripe_unlock(lsm);
421 cl_lvb2attr(attr, lvb);
423 r0->lo_attr_valid = 1;
430 static struct lov_comp_layout_entry_ops raid0_ops = {
431 .lco_init = lov_init_raid0,
432 .lco_fini = lov_fini_raid0,
433 .lco_getattr = lov_attr_get_raid0,
436 static int lov_attr_get_dom(const struct lu_env *env, struct lov_object *lov,
437 unsigned int index, struct lov_layout_entry *lle,
438 struct cl_attr **lov_attr)
440 struct lov_layout_dom *dom = &lle->lle_dom;
441 struct lov_oinfo *loi = dom->lo_loi;
442 struct cl_attr *attr = &dom->lo_dom_r0.lo_attr;
444 if (dom->lo_dom_r0.lo_attr_valid) {
449 if (OST_LVB_IS_ERR(loi->loi_lvb.lvb_blocks))
450 return OST_LVB_GET_ERR(loi->loi_lvb.lvb_blocks);
452 cl_lvb2attr(attr, &loi->loi_lvb);
454 /* DoM component size can be bigger than stripe size after
455 * client's setattr RPC, so do not count anything beyond
456 * component end. Alternatively, check that limit on server
457 * and do not allow size overflow there. */
458 if (attr->cat_size > lle->lle_extent->e_end)
459 attr->cat_size = lle->lle_extent->e_end;
461 attr->cat_kms = attr->cat_size;
463 dom->lo_dom_r0.lo_attr_valid = 1;
470 * Lookup FLD to get MDS index of the given DOM object FID.
472 * \param[in] ld LOV device
473 * \param[in] fid FID to lookup
474 * \param[out] nr index in MDC array to return back
476 * \retval 0 and \a mds filled with MDS index if successful
477 * \retval negative value on error
479 static int lov_fld_lookup(struct lov_device *ld, const struct lu_fid *fid,
487 rc = fld_client_lookup(&ld->ld_lmv->u.lmv.lmv_fld, fid_seq(fid),
488 &mds_idx, LU_SEQ_RANGE_MDT, NULL);
490 CERROR("%s: error while looking for mds number. Seq %#llx"
491 ", err = %d\n", lu_dev_name(cl2lu_dev(&ld->ld_cl)),
496 CDEBUG(D_INODE, "FLD lookup got mds #%x for fid="DFID"\n",
499 /* find proper MDC device in the array */
500 for (i = 0; i < ld->ld_md_tgts_nr; i++) {
501 if (ld->ld_md_tgts[i].ldm_mdc != NULL &&
502 ld->ld_md_tgts[i].ldm_idx == mds_idx)
506 if (i == ld->ld_md_tgts_nr) {
507 CERROR("%s: cannot find corresponding MDC device for mds #%x "
508 "for fid="DFID"\n", lu_dev_name(cl2lu_dev(&ld->ld_cl)),
518 * Implementation of lov_comp_layout_entry_ops::lco_init for DOM object.
520 * Init the DOM object for the first time. It prepares also RAID0 entry
521 * for it to use in common methods with ordinary RAID0 layout entries.
523 * \param[in] env execution environment
524 * \param[in] dev LOV device
525 * \param[in] lov LOV object
526 * \param[in] index Composite layout entry index in LSM
527 * \param[in] lle Composite LOV layout entry
529 static int lov_init_dom(const struct lu_env *env, struct lov_device *dev,
530 struct lov_object *lov, unsigned int index,
531 const struct cl_object_conf *conf,
532 struct lov_layout_entry *lle)
534 struct lov_thread_info *lti = lov_env_info(env);
535 struct lov_stripe_md_entry *lsme = lov_lse(lov, index);
536 struct cl_object *clo;
537 struct lu_object *o = lov2lu(lov);
538 const struct lu_fid *fid = lu_object_fid(o);
539 struct cl_device *mdcdev;
540 struct lov_oinfo *loi = NULL;
541 struct cl_object_conf *sconf = <i->lti_stripe_conf;
550 /* find proper MDS device */
551 rc = lov_fld_lookup(dev, fid, &idx);
555 LASSERTF(dev->ld_md_tgts[idx].ldm_mdc != NULL,
556 "LOV md target[%u] is NULL\n", idx);
558 /* check lsm is DOM, more checks are needed */
559 LASSERT(lsme->lsme_stripe_count == 0);
562 * Create lower cl_objects.
564 mdcdev = dev->ld_md_tgts[idx].ldm_mdc;
566 LASSERTF(mdcdev != NULL, "non-initialized mdc subdev\n");
568 /* DoM object has no oinfo in LSM entry, create it exclusively */
569 OBD_SLAB_ALLOC_PTR_GFP(loi, lov_oinfo_slab, GFP_NOFS);
573 fid_to_ostid(lu_object_fid(lov2lu(lov)), &loi->loi_oi);
575 sconf->u.coc_oinfo = loi;
577 clo = lov_sub_find(env, mdcdev, fid, sconf);
579 GOTO(out, rc = PTR_ERR(clo));
581 rc = lov_init_sub(env, lov, clo, loi, lov_comp_index(index, 0));
582 if (rc == -EAGAIN) /* try again */
587 lle->lle_dom.lo_dom = cl2lovsub(clo);
588 spin_lock_init(&lle->lle_dom.lo_dom_r0.lo_sub_lock);
589 lle->lle_dom.lo_dom_r0.lo_nr = 1;
590 lle->lle_dom.lo_dom_r0.lo_sub = &lle->lle_dom.lo_dom;
591 lle->lle_dom.lo_loi = loi;
593 rc = lov_page_slice_fixup(lov, clo);
598 OBD_SLAB_FREE_PTR(loi, lov_oinfo_slab);
603 * Implementation of lov_layout_operations::llo_fini for DOM object.
605 * Finish the DOM object and free related memory.
607 * \param[in] env execution environment
608 * \param[in] lov LOV object
609 * \param[in] state LOV layout state
611 static void lov_fini_dom(const struct lu_env *env,
612 struct lov_layout_entry *lle)
614 if (lle->lle_dom.lo_dom != NULL)
615 lle->lle_dom.lo_dom = NULL;
616 if (lle->lle_dom.lo_loi != NULL)
617 OBD_SLAB_FREE_PTR(lle->lle_dom.lo_loi, lov_oinfo_slab);
620 static struct lov_comp_layout_entry_ops dom_ops = {
621 .lco_init = lov_init_dom,
622 .lco_fini = lov_fini_dom,
623 .lco_getattr = lov_attr_get_dom,
626 static int lov_init_composite(const struct lu_env *env, struct lov_device *dev,
627 struct lov_object *lov, struct lov_stripe_md *lsm,
628 const struct cl_object_conf *conf,
629 union lov_layout_state *state)
631 struct lov_layout_composite *comp = &state->composite;
632 struct lov_layout_entry *lle;
633 struct lov_mirror_entry *lre;
634 unsigned int entry_count;
635 unsigned int psz = 0;
636 unsigned int mirror_count;
637 int flr_state = lsm->lsm_flags & LCM_FL_FLR_MASK;
644 LASSERT(lsm->lsm_entry_count > 0);
645 LASSERT(lov->lo_lsm == NULL);
646 lov->lo_lsm = lsm_addref(lsm);
647 lov->lo_layout_invalid = true;
649 dump_lsm(D_INODE, lsm);
651 entry_count = lsm->lsm_entry_count;
653 spin_lock_init(&comp->lo_write_lock);
654 comp->lo_flags = lsm->lsm_flags;
655 comp->lo_mirror_count = lsm->lsm_mirror_count + 1;
656 comp->lo_entry_count = lsm->lsm_entry_count;
657 comp->lo_preferred_mirror = -1;
659 if (equi(flr_state == LCM_FL_NONE, comp->lo_mirror_count > 1))
662 OBD_ALLOC(comp->lo_mirrors,
663 comp->lo_mirror_count * sizeof(*comp->lo_mirrors));
664 if (comp->lo_mirrors == NULL)
667 OBD_ALLOC(comp->lo_entries, entry_count * sizeof(*comp->lo_entries));
668 if (comp->lo_entries == NULL)
671 /* Initiate all entry types and extents data at first */
672 for (i = 0, j = 0, mirror_count = 1; i < entry_count; i++) {
675 lle = &comp->lo_entries[i];
677 lle->lle_lsme = lsm->lsm_entries[i];
678 lle->lle_type = lov_entry_type(lle->lle_lsme);
679 switch (lle->lle_type) {
680 case LOV_PATTERN_RAID0:
681 lle->lle_comp_ops = &raid0_ops;
683 case LOV_PATTERN_MDT:
684 lle->lle_comp_ops = &dom_ops;
687 CERROR("%s: unknown composite layout entry type %i\n",
688 lov2obd(dev->ld_lov)->obd_name,
689 lsm->lsm_entries[i]->lsme_pattern);
690 dump_lsm(D_ERROR, lsm);
694 lle->lle_extent = &lle->lle_lsme->lsme_extent;
695 lle->lle_valid = !(lle->lle_lsme->lsme_flags & LCME_FL_STALE);
697 if (flr_state != LCM_FL_NONE)
698 mirror_id = mirror_id_of(lle->lle_lsme->lsme_id);
700 lre = &comp->lo_mirrors[j];
702 if (mirror_id == lre->lre_mirror_id) {
703 lre->lre_valid |= lle->lle_valid;
704 lre->lre_stale |= !lle->lle_valid;
709 /* new mirror detected, assume that the mirrors
710 * are shorted in layout */
713 if (j >= comp->lo_mirror_count)
716 lre = &comp->lo_mirrors[j];
719 /* entries must be sorted by mirrors */
720 lre->lre_mirror_id = mirror_id;
721 lre->lre_start = lre->lre_end = i;
722 lre->lre_preferred = !!(lle->lle_lsme->lsme_flags &
724 lre->lre_valid = lle->lle_valid;
725 lre->lre_stale = !lle->lle_valid;
728 /* sanity check for FLR */
729 if (mirror_count != comp->lo_mirror_count) {
731 " doesn't have the # of mirrors it claims, %u/%u\n",
732 PFID(lu_object_fid(lov2lu(lov))), mirror_count,
733 comp->lo_mirror_count + 1);
735 GOTO(out, result = -EINVAL);
738 lov_foreach_layout_entry(lov, lle) {
739 int index = lov_layout_entry_index(lov, lle);
742 * If the component has not been init-ed on MDS side, for
743 * PFL layout, we'd know that the components beyond this one
744 * will be dynamically init-ed later on file write/trunc ops.
746 if (!lsme_inited(lle->lle_lsme))
749 result = lle->lle_comp_ops->lco_init(env, dev, lov, index,
754 LASSERT(ergo(psz > 0, psz == result));
759 cl_object_header(&lov->lo_cl)->coh_page_bufsize += psz;
761 /* decide the preferred mirror. It uses the hash value of lov_object
762 * so that different clients would use different mirrors for read. */
764 seq = hash_long((unsigned long)lov, 8);
765 for (i = 0; i < comp->lo_mirror_count; i++) {
766 unsigned int idx = (i + seq) % comp->lo_mirror_count;
768 lre = lov_mirror_entry(lov, idx);
772 mirror_count++; /* valid mirror */
774 if (lre->lre_preferred || comp->lo_preferred_mirror < 0)
775 comp->lo_preferred_mirror = idx;
779 " doesn't have any valid mirrors\n",
780 PFID(lu_object_fid(lov2lu(lov))));
782 comp->lo_preferred_mirror = 0;
785 LASSERT(comp->lo_preferred_mirror >= 0);
789 return result > 0 ? 0 : result;
792 static int lov_init_empty(const struct lu_env *env, struct lov_device *dev,
793 struct lov_object *lov, struct lov_stripe_md *lsm,
794 const struct cl_object_conf *conf,
795 union lov_layout_state *state)
800 static int lov_init_released(const struct lu_env *env,
801 struct lov_device *dev, struct lov_object *lov,
802 struct lov_stripe_md *lsm,
803 const struct cl_object_conf *conf,
804 union lov_layout_state *state)
806 LASSERT(lsm != NULL);
807 LASSERT(lsm->lsm_is_released);
808 LASSERT(lov->lo_lsm == NULL);
810 lov->lo_lsm = lsm_addref(lsm);
814 static int lov_delete_empty(const struct lu_env *env, struct lov_object *lov,
815 union lov_layout_state *state)
817 LASSERT(lov->lo_type == LLT_EMPTY || lov->lo_type == LLT_RELEASED);
819 lov_layout_wait(env, lov);
823 static int lov_delete_composite(const struct lu_env *env,
824 struct lov_object *lov,
825 union lov_layout_state *state)
827 struct lov_layout_entry *entry;
828 struct lov_layout_composite *comp = &state->composite;
832 dump_lsm(D_INODE, lov->lo_lsm);
834 lov_layout_wait(env, lov);
835 if (comp->lo_entries)
836 lov_foreach_layout_entry(lov, entry)
837 lov_delete_raid0(env, lov, entry);
842 static void lov_fini_empty(const struct lu_env *env, struct lov_object *lov,
843 union lov_layout_state *state)
845 LASSERT(lov->lo_type == LLT_EMPTY || lov->lo_type == LLT_RELEASED);
848 static void lov_fini_composite(const struct lu_env *env,
849 struct lov_object *lov,
850 union lov_layout_state *state)
852 struct lov_layout_composite *comp = &state->composite;
855 if (comp->lo_entries != NULL) {
856 struct lov_layout_entry *entry;
858 lov_foreach_layout_entry(lov, entry)
859 entry->lle_comp_ops->lco_fini(env, entry);
861 OBD_FREE(comp->lo_entries,
862 comp->lo_entry_count * sizeof(*comp->lo_entries));
863 comp->lo_entries = NULL;
866 if (comp->lo_mirrors != NULL) {
867 OBD_FREE(comp->lo_mirrors,
868 comp->lo_mirror_count * sizeof(*comp->lo_mirrors));
869 comp->lo_mirrors = NULL;
872 memset(comp, 0, sizeof(*comp));
874 dump_lsm(D_INODE, lov->lo_lsm);
875 lov_free_memmd(&lov->lo_lsm);
880 static void lov_fini_released(const struct lu_env *env, struct lov_object *lov,
881 union lov_layout_state *state)
884 dump_lsm(D_INODE, lov->lo_lsm);
885 lov_free_memmd(&lov->lo_lsm);
889 static int lov_print_empty(const struct lu_env *env, void *cookie,
890 lu_printer_t p, const struct lu_object *o)
892 (*p)(env, cookie, "empty %d\n", lu2lov(o)->lo_layout_invalid);
896 static int lov_print_composite(const struct lu_env *env, void *cookie,
897 lu_printer_t p, const struct lu_object *o)
899 struct lov_object *lov = lu2lov(o);
900 struct lov_stripe_md *lsm = lov->lo_lsm;
903 (*p)(env, cookie, "entries: %d, %s, lsm{%p 0x%08X %d %u}:\n",
904 lsm->lsm_entry_count,
905 lov->lo_layout_invalid ? "invalid" : "valid", lsm,
906 lsm->lsm_magic, atomic_read(&lsm->lsm_refc),
907 lsm->lsm_layout_gen);
909 for (i = 0; i < lsm->lsm_entry_count; i++) {
910 struct lov_stripe_md_entry *lse = lsm->lsm_entries[i];
911 struct lov_layout_entry *lle = lov_entry(lov, i);
914 DEXT ": { 0x%08X, %u, %#x, %u, %#x, %u, %u }\n",
915 PEXT(&lse->lsme_extent), lse->lsme_magic,
916 lse->lsme_id, lse->lsme_pattern, lse->lsme_layout_gen,
917 lse->lsme_flags, lse->lsme_stripe_count,
918 lse->lsme_stripe_size);
919 lov_print_raid0(env, cookie, p, lle);
925 static int lov_print_released(const struct lu_env *env, void *cookie,
926 lu_printer_t p, const struct lu_object *o)
928 struct lov_object *lov = lu2lov(o);
929 struct lov_stripe_md *lsm = lov->lo_lsm;
932 "released: %s, lsm{%p 0x%08X %d %u}:\n",
933 lov->lo_layout_invalid ? "invalid" : "valid", lsm,
934 lsm->lsm_magic, atomic_read(&lsm->lsm_refc),
935 lsm->lsm_layout_gen);
940 * Implements cl_object_operations::coo_attr_get() method for an object
941 * without stripes (LLT_EMPTY layout type).
943 * The only attributes this layer is authoritative in this case is
944 * cl_attr::cat_blocks---it's 0.
946 static int lov_attr_get_empty(const struct lu_env *env, struct cl_object *obj,
947 struct cl_attr *attr)
949 attr->cat_blocks = 0;
953 static int lov_attr_get_composite(const struct lu_env *env,
954 struct cl_object *obj,
955 struct cl_attr *attr)
957 struct lov_object *lov = cl2lov(obj);
958 struct lov_layout_entry *entry;
964 attr->cat_blocks = 0;
965 lov_foreach_layout_entry(lov, entry) {
966 struct cl_attr *lov_attr = NULL;
967 int index = lov_layout_entry_index(lov, entry);
969 if (!entry->lle_valid)
972 /* PFL: This component has not been init-ed. */
973 if (!lsm_entry_inited(lov->lo_lsm, index))
976 result = entry->lle_comp_ops->lco_getattr(env, lov, index,
981 if (lov_attr == NULL)
984 CDEBUG(D_INODE, "COMP ID #%i: s=%llu m=%llu a=%llu c=%llu "
985 "b=%llu\n", index - 1, lov_attr->cat_size,
986 lov_attr->cat_mtime, lov_attr->cat_atime,
987 lov_attr->cat_ctime, lov_attr->cat_blocks);
990 attr->cat_blocks += lov_attr->cat_blocks;
991 if (attr->cat_size < lov_attr->cat_size)
992 attr->cat_size = lov_attr->cat_size;
993 if (attr->cat_kms < lov_attr->cat_kms)
994 attr->cat_kms = lov_attr->cat_kms;
995 if (attr->cat_atime < lov_attr->cat_atime)
996 attr->cat_atime = lov_attr->cat_atime;
997 if (attr->cat_ctime < lov_attr->cat_ctime)
998 attr->cat_ctime = lov_attr->cat_ctime;
999 if (attr->cat_mtime < lov_attr->cat_mtime)
1000 attr->cat_mtime = lov_attr->cat_mtime;
1006 const static struct lov_layout_operations lov_dispatch[] = {
1008 .llo_init = lov_init_empty,
1009 .llo_delete = lov_delete_empty,
1010 .llo_fini = lov_fini_empty,
1011 .llo_print = lov_print_empty,
1012 .llo_page_init = lov_page_init_empty,
1013 .llo_lock_init = lov_lock_init_empty,
1014 .llo_io_init = lov_io_init_empty,
1015 .llo_getattr = lov_attr_get_empty,
1018 .llo_init = lov_init_released,
1019 .llo_delete = lov_delete_empty,
1020 .llo_fini = lov_fini_released,
1021 .llo_print = lov_print_released,
1022 .llo_page_init = lov_page_init_empty,
1023 .llo_lock_init = lov_lock_init_empty,
1024 .llo_io_init = lov_io_init_released,
1025 .llo_getattr = lov_attr_get_empty,
1028 .llo_init = lov_init_composite,
1029 .llo_delete = lov_delete_composite,
1030 .llo_fini = lov_fini_composite,
1031 .llo_print = lov_print_composite,
1032 .llo_page_init = lov_page_init_composite,
1033 .llo_lock_init = lov_lock_init_composite,
1034 .llo_io_init = lov_io_init_composite,
1035 .llo_getattr = lov_attr_get_composite,
1040 * Performs a double-dispatch based on the layout type of an object.
1042 #define LOV_2DISPATCH_NOLOCK(obj, op, ...) \
1044 struct lov_object *__obj = (obj); \
1045 enum lov_layout_type __llt; \
1047 __llt = __obj->lo_type; \
1048 LASSERT(__llt < ARRAY_SIZE(lov_dispatch)); \
1049 lov_dispatch[__llt].op(__VA_ARGS__); \
1053 * Return lov_layout_type associated with a given lsm
1055 static enum lov_layout_type lov_type(struct lov_stripe_md *lsm)
1060 if (lsm->lsm_is_released)
1061 return LLT_RELEASED;
1063 if (lsm->lsm_magic == LOV_MAGIC_V1 ||
1064 lsm->lsm_magic == LOV_MAGIC_V3 ||
1065 lsm->lsm_magic == LOV_MAGIC_COMP_V1)
1071 static inline void lov_conf_freeze(struct lov_object *lov)
1073 CDEBUG(D_INODE, "To take share lov(%p) owner %p/%p\n",
1074 lov, lov->lo_owner, current);
1075 if (lov->lo_owner != current)
1076 down_read(&lov->lo_type_guard);
1079 static inline void lov_conf_thaw(struct lov_object *lov)
1081 CDEBUG(D_INODE, "To release share lov(%p) owner %p/%p\n",
1082 lov, lov->lo_owner, current);
1083 if (lov->lo_owner != current)
1084 up_read(&lov->lo_type_guard);
1087 #define LOV_2DISPATCH_MAYLOCK(obj, op, lock, ...) \
1089 struct lov_object *__obj = (obj); \
1090 int __lock = !!(lock); \
1091 typeof(lov_dispatch[0].op(__VA_ARGS__)) __result; \
1094 lov_conf_freeze(__obj); \
1095 __result = LOV_2DISPATCH_NOLOCK(obj, op, __VA_ARGS__); \
1097 lov_conf_thaw(__obj); \
1102 * Performs a locked double-dispatch based on the layout type of an object.
1104 #define LOV_2DISPATCH(obj, op, ...) \
1105 LOV_2DISPATCH_MAYLOCK(obj, op, 1, __VA_ARGS__)
1107 #define LOV_2DISPATCH_VOID(obj, op, ...) \
1109 struct lov_object *__obj = (obj); \
1110 enum lov_layout_type __llt; \
1112 lov_conf_freeze(__obj); \
1113 __llt = __obj->lo_type; \
1114 LASSERT(__llt < ARRAY_SIZE(lov_dispatch)); \
1115 lov_dispatch[__llt].op(__VA_ARGS__); \
1116 lov_conf_thaw(__obj); \
1119 static void lov_conf_lock(struct lov_object *lov)
1121 LASSERT(lov->lo_owner != current);
1122 down_write(&lov->lo_type_guard);
1123 LASSERT(lov->lo_owner == NULL);
1124 lov->lo_owner = current;
1125 CDEBUG(D_INODE, "Took exclusive lov(%p) owner %p\n",
1126 lov, lov->lo_owner);
1129 static void lov_conf_unlock(struct lov_object *lov)
1131 CDEBUG(D_INODE, "To release exclusive lov(%p) owner %p\n",
1132 lov, lov->lo_owner);
1133 lov->lo_owner = NULL;
1134 up_write(&lov->lo_type_guard);
1137 static int lov_layout_wait(const struct lu_env *env, struct lov_object *lov)
1139 struct l_wait_info lwi = { 0 };
1142 while (atomic_read(&lov->lo_active_ios) > 0) {
1143 CDEBUG(D_INODE, "file:"DFID" wait for active IO, now: %d.\n",
1144 PFID(lu_object_fid(lov2lu(lov))),
1145 atomic_read(&lov->lo_active_ios));
1147 l_wait_event(lov->lo_waitq,
1148 atomic_read(&lov->lo_active_ios) == 0, &lwi);
1153 static int lov_layout_change(const struct lu_env *unused,
1154 struct lov_object *lov, struct lov_stripe_md *lsm,
1155 const struct cl_object_conf *conf)
1157 enum lov_layout_type llt = lov_type(lsm);
1158 union lov_layout_state *state = &lov->u;
1159 const struct lov_layout_operations *old_ops;
1160 const struct lov_layout_operations *new_ops;
1161 struct lov_device *lov_dev = lov_object_dev(lov);
1167 LASSERT(lov->lo_type < ARRAY_SIZE(lov_dispatch));
1169 env = cl_env_get(&refcheck);
1171 RETURN(PTR_ERR(env));
1173 LASSERT(llt < ARRAY_SIZE(lov_dispatch));
1175 CDEBUG(D_INODE, DFID" from %s to %s\n",
1176 PFID(lu_object_fid(lov2lu(lov))),
1177 llt2str(lov->lo_type), llt2str(llt));
1179 old_ops = &lov_dispatch[lov->lo_type];
1180 new_ops = &lov_dispatch[llt];
1182 rc = cl_object_prune(env, &lov->lo_cl);
1186 rc = old_ops->llo_delete(env, lov, &lov->u);
1190 old_ops->llo_fini(env, lov, &lov->u);
1192 LASSERT(atomic_read(&lov->lo_active_ios) == 0);
1194 CDEBUG(D_INODE, DFID "Apply new layout lov %p, type %d\n",
1195 PFID(lu_object_fid(lov2lu(lov))), lov, llt);
1197 /* page bufsize fixup */
1198 cl_object_header(&lov->lo_cl)->coh_page_bufsize -=
1199 lov_page_slice_fixup(lov, NULL);
1202 rc = new_ops->llo_init(env, lov_dev, lov, lsm, conf, state);
1204 struct obd_device *obd = lov2obd(lov_dev->ld_lov);
1206 CERROR("%s: cannot apply new layout on "DFID" : rc = %d\n",
1207 obd->obd_name, PFID(lu_object_fid(lov2lu(lov))), rc);
1208 new_ops->llo_delete(env, lov, state);
1209 new_ops->llo_fini(env, lov, state);
1210 /* this file becomes an EMPTY file. */
1211 lov->lo_type = LLT_EMPTY;
1216 cl_env_put(env, &refcheck);
1220 /*****************************************************************************
1222 * Lov object operations.
1225 int lov_object_init(const struct lu_env *env, struct lu_object *obj,
1226 const struct lu_object_conf *conf)
1228 struct lov_object *lov = lu2lov(obj);
1229 struct lov_device *dev = lov_object_dev(lov);
1230 const struct cl_object_conf *cconf = lu2cl_conf(conf);
1231 union lov_layout_state *set = &lov->u;
1232 const struct lov_layout_operations *ops;
1233 struct lov_stripe_md *lsm = NULL;
1237 init_rwsem(&lov->lo_type_guard);
1238 atomic_set(&lov->lo_active_ios, 0);
1239 init_waitqueue_head(&lov->lo_waitq);
1240 cl_object_page_init(lu2cl(obj), sizeof(struct lov_page));
1242 lov->lo_type = LLT_EMPTY;
1243 if (cconf->u.coc_layout.lb_buf != NULL) {
1244 lsm = lov_unpackmd(dev->ld_lov,
1245 cconf->u.coc_layout.lb_buf,
1246 cconf->u.coc_layout.lb_len);
1248 RETURN(PTR_ERR(lsm));
1250 dump_lsm(D_INODE, lsm);
1253 /* no locking is necessary, as object is being created */
1254 lov->lo_type = lov_type(lsm);
1255 ops = &lov_dispatch[lov->lo_type];
1256 rc = ops->llo_init(env, dev, lov, lsm, cconf, set);
1266 static int lov_conf_set(const struct lu_env *env, struct cl_object *obj,
1267 const struct cl_object_conf *conf)
1269 struct lov_stripe_md *lsm = NULL;
1270 struct lov_object *lov = cl2lov(obj);
1274 if (conf->coc_opc == OBJECT_CONF_SET &&
1275 conf->u.coc_layout.lb_buf != NULL) {
1276 lsm = lov_unpackmd(lov_object_dev(lov)->ld_lov,
1277 conf->u.coc_layout.lb_buf,
1278 conf->u.coc_layout.lb_len);
1280 RETURN(PTR_ERR(lsm));
1281 dump_lsm(D_INODE, lsm);
1285 if (conf->coc_opc == OBJECT_CONF_INVALIDATE) {
1286 lov->lo_layout_invalid = true;
1287 GOTO(out, result = 0);
1290 if (conf->coc_opc == OBJECT_CONF_WAIT) {
1291 if (lov->lo_layout_invalid &&
1292 atomic_read(&lov->lo_active_ios) > 0) {
1293 lov_conf_unlock(lov);
1294 result = lov_layout_wait(env, lov);
1300 LASSERT(conf->coc_opc == OBJECT_CONF_SET);
1302 if ((lsm == NULL && lov->lo_lsm == NULL) ||
1303 ((lsm != NULL && lov->lo_lsm != NULL) &&
1304 (lov->lo_lsm->lsm_layout_gen == lsm->lsm_layout_gen) &&
1305 (lov->lo_lsm->lsm_entries[0]->lsme_pattern ==
1306 lsm->lsm_entries[0]->lsme_pattern))) {
1307 /* same version of layout */
1308 lov->lo_layout_invalid = false;
1309 GOTO(out, result = 0);
1312 /* will change layout - check if there still exists active IO. */
1313 if (atomic_read(&lov->lo_active_ios) > 0) {
1314 lov->lo_layout_invalid = true;
1315 GOTO(out, result = -EBUSY);
1318 result = lov_layout_change(env, lov, lsm, conf);
1319 lov->lo_layout_invalid = result != 0;
1323 lov_conf_unlock(lov);
1325 CDEBUG(D_INODE, DFID" lo_layout_invalid=%d\n",
1326 PFID(lu_object_fid(lov2lu(lov))), lov->lo_layout_invalid);
1330 static void lov_object_delete(const struct lu_env *env, struct lu_object *obj)
1332 struct lov_object *lov = lu2lov(obj);
1335 LOV_2DISPATCH_VOID(lov, llo_delete, env, lov, &lov->u);
1339 static void lov_object_free(const struct lu_env *env, struct lu_object *obj)
1341 struct lov_object *lov = lu2lov(obj);
1344 LOV_2DISPATCH_VOID(lov, llo_fini, env, lov, &lov->u);
1345 lu_object_fini(obj);
1346 OBD_SLAB_FREE_PTR(lov, lov_object_kmem);
1350 static int lov_object_print(const struct lu_env *env, void *cookie,
1351 lu_printer_t p, const struct lu_object *o)
1353 return LOV_2DISPATCH_NOLOCK(lu2lov(o), llo_print, env, cookie, p, o);
1356 int lov_page_init(const struct lu_env *env, struct cl_object *obj,
1357 struct cl_page *page, pgoff_t index)
1359 return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_page_init, env, obj, page,
1364 * Implements cl_object_operations::clo_io_init() method for lov
1365 * layer. Dispatches to the appropriate layout io initialization method.
1367 int lov_io_init(const struct lu_env *env, struct cl_object *obj,
1370 CL_IO_SLICE_CLEAN(lov_env_io(env), lis_preserved);
1372 CDEBUG(D_INODE, DFID "io %p type %d ignore/verify layout %d/%d\n",
1373 PFID(lu_object_fid(&obj->co_lu)), io, io->ci_type,
1374 io->ci_ignore_layout, io->ci_verify_layout);
1376 /* IO type CIT_MISC with ci_ignore_layout set are usually invoked from
1377 * the OSC layer. It shouldn't take lov layout conf lock in that case,
1378 * because as long as the OSC object exists, the layout can't be
1380 return LOV_2DISPATCH_MAYLOCK(cl2lov(obj), llo_io_init,
1381 !(io->ci_ignore_layout && io->ci_type == CIT_MISC),
1386 * An implementation of cl_object_operations::clo_attr_get() method for lov
1387 * layer. For raid0 layout this collects and merges attributes of all
1390 static int lov_attr_get(const struct lu_env *env, struct cl_object *obj,
1391 struct cl_attr *attr)
1393 /* do not take lock, as this function is called under a
1394 * spin-lock. Layout is protected from changing by ongoing IO. */
1395 return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_getattr, env, obj, attr);
1398 static int lov_attr_update(const struct lu_env *env, struct cl_object *obj,
1399 const struct cl_attr *attr, unsigned valid)
1402 * No dispatch is required here, as no layout implements this.
1407 int lov_lock_init(const struct lu_env *env, struct cl_object *obj,
1408 struct cl_lock *lock, const struct cl_io *io)
1410 /* No need to lock because we've taken one refcount of layout. */
1411 return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_lock_init, env, obj, lock,
1416 * We calculate on which OST the mapping will end. If the length of mapping
1417 * is greater than (stripe_size * stripe_count) then the last_stripe will
1418 * will be one just before start_stripe. Else we check if the mapping
1419 * intersects each OST and find last_stripe.
1420 * This function returns the last_stripe and also sets the stripe_count
1421 * over which the mapping is spread
1423 * \param lsm [in] striping information for the file
1424 * \param index [in] stripe component index
1425 * \param ext [in] logical extent of mapping
1426 * \param start_stripe [in] starting stripe of the mapping
1427 * \param stripe_count [out] the number of stripes across which to map is
1430 * \retval last_stripe return the last stripe of the mapping
1432 static int fiemap_calc_last_stripe(struct lov_stripe_md *lsm, int index,
1433 struct lu_extent *ext,
1434 int start_stripe, int *stripe_count)
1436 struct lov_stripe_md_entry *lsme = lsm->lsm_entries[index];
1442 if (ext->e_end - ext->e_start >
1443 lsme->lsme_stripe_size * lsme->lsme_stripe_count) {
1444 last_stripe = (start_stripe < 1 ? lsme->lsme_stripe_count - 1 :
1446 *stripe_count = lsme->lsme_stripe_count;
1448 for (j = 0, i = start_stripe; j < lsme->lsme_stripe_count;
1449 i = (i + 1) % lsme->lsme_stripe_count, j++) {
1450 if ((lov_stripe_intersects(lsm, index, i, ext,
1451 &obd_start, &obd_end)) == 0)
1455 last_stripe = (start_stripe + j - 1) % lsme->lsme_stripe_count;
1462 * Set fe_device and copy extents from local buffer into main return buffer.
1464 * \param fiemap [out] fiemap to hold all extents
1465 * \param lcl_fm_ext [in] array of fiemap extents get from OSC layer
1466 * \param ost_index [in] OST index to be written into the fm_device
1467 * field for each extent
1468 * \param ext_count [in] number of extents to be copied
1469 * \param current_extent [in] where to start copying in the extent array
1471 static void fiemap_prepare_and_copy_exts(struct fiemap *fiemap,
1472 struct fiemap_extent *lcl_fm_ext,
1473 int ost_index, unsigned int ext_count,
1479 for (ext = 0; ext < ext_count; ext++) {
1480 lcl_fm_ext[ext].fe_device = ost_index;
1481 lcl_fm_ext[ext].fe_flags |= FIEMAP_EXTENT_NET;
1484 /* Copy fm_extent's from fm_local to return buffer */
1485 to = (char *)fiemap + fiemap_count_to_size(current_extent);
1486 memcpy(to, lcl_fm_ext, ext_count * sizeof(struct fiemap_extent));
1489 #define FIEMAP_BUFFER_SIZE 4096
1492 * Non-zero fe_logical indicates that this is a continuation FIEMAP
1493 * call. The local end offset and the device are sent in the first
1494 * fm_extent. This function calculates the stripe number from the index.
1495 * This function returns a stripe_no on which mapping is to be restarted.
1497 * This function returns fm_end_offset which is the in-OST offset at which
1498 * mapping should be restarted. If fm_end_offset=0 is returned then caller
1499 * will re-calculate proper offset in next stripe.
1500 * Note that the first extent is passed to lov_get_info via the value field.
1502 * \param fiemap [in] fiemap request header
1503 * \param lsm [in] striping information for the file
1504 * \param index [in] stripe component index
1505 * \param ext [in] logical extent of mapping
1506 * \param start_stripe [out] starting stripe will be returned in this
1508 static u64 fiemap_calc_fm_end_offset(struct fiemap *fiemap,
1509 struct lov_stripe_md *lsm,
1510 int index, struct lu_extent *ext,
1513 struct lov_stripe_md_entry *lsme = lsm->lsm_entries[index];
1514 u64 local_end = fiemap->fm_extents[0].fe_logical;
1521 if (fiemap->fm_extent_count == 0 ||
1522 fiemap->fm_extents[0].fe_logical == 0)
1525 /* Find out stripe_no from ost_index saved in the fe_device */
1526 for (i = 0; i < lsme->lsme_stripe_count; i++) {
1527 struct lov_oinfo *oinfo = lsme->lsme_oinfo[i];
1529 if (lov_oinfo_is_dummy(oinfo))
1532 if (oinfo->loi_ost_idx == fiemap->fm_extents[0].fe_device) {
1538 if (stripe_no == -1)
1541 /* If we have finished mapping on previous device, shift logical
1542 * offset to start of next device */
1543 if (lov_stripe_intersects(lsm, index, stripe_no, ext,
1544 &lun_start, &lun_end) != 0 &&
1545 local_end < lun_end) {
1546 fm_end_offset = local_end;
1547 *start_stripe = stripe_no;
1549 /* This is a special value to indicate that caller should
1550 * calculate offset in next stripe. */
1552 *start_stripe = (stripe_no + 1) % lsme->lsme_stripe_count;
1555 return fm_end_offset;
1558 struct fiemap_state {
1559 struct fiemap *fs_fm;
1560 struct lu_extent fs_ext;
1565 int fs_start_stripe;
1567 bool fs_device_done;
1568 bool fs_finish_stripe;
1572 static struct cl_object *lov_find_subobj(const struct lu_env *env,
1573 struct lov_object *lov,
1574 struct lov_stripe_md *lsm,
1577 struct lov_device *dev = lu2lov_dev(lov2lu(lov)->lo_dev);
1578 struct lov_thread_info *lti = lov_env_info(env);
1579 struct lu_fid *ofid = <i->lti_fid;
1580 struct lov_oinfo *oinfo;
1581 struct cl_device *subdev;
1582 int entry = lov_comp_entry(index);
1583 int stripe = lov_comp_stripe(index);
1586 struct cl_object *result;
1588 if (lov->lo_type != LLT_COMP)
1589 GOTO(out, result = NULL);
1591 if (entry >= lsm->lsm_entry_count ||
1592 stripe >= lsm->lsm_entries[entry]->lsme_stripe_count)
1593 GOTO(out, result = NULL);
1595 oinfo = lsm->lsm_entries[entry]->lsme_oinfo[stripe];
1596 ost_idx = oinfo->loi_ost_idx;
1597 rc = ostid_to_fid(ofid, &oinfo->loi_oi, ost_idx);
1599 GOTO(out, result = NULL);
1601 subdev = lovsub2cl_dev(dev->ld_target[ost_idx]);
1602 result = lov_sub_find(env, subdev, ofid, NULL);
1605 result = ERR_PTR(-EINVAL);
1609 int fiemap_for_stripe(const struct lu_env *env, struct cl_object *obj,
1610 struct lov_stripe_md *lsm, struct fiemap *fiemap,
1611 size_t *buflen, struct ll_fiemap_info_key *fmkey,
1612 int index, int stripeno, struct fiemap_state *fs)
1614 struct lov_stripe_md_entry *lsme = lsm->lsm_entries[index];
1615 struct cl_object *subobj;
1616 struct lov_obd *lov = lu2lov_dev(obj->co_lu.lo_dev)->ld_lov;
1617 struct fiemap_extent *fm_ext = &fs->fs_fm->fm_extents[0];
1618 u64 req_fm_len; /* Stores length of required mapping */
1619 u64 len_mapped_single_call;
1623 unsigned int ext_count;
1624 /* EOF for object */
1625 bool ost_eof = false;
1626 /* done with required mapping for this OST? */
1627 bool ost_done = false;
1631 fs->fs_device_done = false;
1632 /* Find out range of mapping on this stripe */
1633 if ((lov_stripe_intersects(lsm, index, stripeno, &fs->fs_ext,
1634 &lun_start, &obd_object_end)) == 0)
1637 if (lov_oinfo_is_dummy(lsme->lsme_oinfo[stripeno]))
1640 /* If this is a continuation FIEMAP call and we are on
1641 * starting stripe then lun_start needs to be set to
1643 if (fs->fs_end_offset != 0 && stripeno == fs->fs_start_stripe)
1644 lun_start = fs->fs_end_offset;
1645 lun_end = lov_size_to_stripe(lsm, index, fs->fs_ext.e_end, stripeno);
1646 if (lun_start == lun_end)
1649 req_fm_len = obd_object_end - lun_start;
1650 fs->fs_fm->fm_length = 0;
1651 len_mapped_single_call = 0;
1653 /* find lobsub object */
1654 subobj = lov_find_subobj(env, cl2lov(obj), lsm,
1655 lov_comp_index(index, stripeno));
1657 return PTR_ERR(subobj);
1658 /* If the output buffer is very large and the objects have many
1659 * extents we may need to loop on a single OST repeatedly */
1661 if (fiemap->fm_extent_count > 0) {
1662 /* Don't get too many extents. */
1663 if (fs->fs_cur_extent + fs->fs_cnt_need >
1664 fiemap->fm_extent_count)
1665 fs->fs_cnt_need = fiemap->fm_extent_count -
1669 lun_start += len_mapped_single_call;
1670 fs->fs_fm->fm_length = req_fm_len - len_mapped_single_call;
1671 req_fm_len = fs->fs_fm->fm_length;
1673 * If we've collected enough extent map, we'd request 1 more,
1674 * to see whether we coincidentally finished all available
1675 * extent map, so that FIEMAP_EXTENT_LAST would be set.
1677 fs->fs_fm->fm_extent_count = fs->fs_enough ?
1678 1 : fs->fs_cnt_need;
1679 fs->fs_fm->fm_mapped_extents = 0;
1680 fs->fs_fm->fm_flags = fiemap->fm_flags;
1682 ost_index = lsme->lsme_oinfo[stripeno]->loi_ost_idx;
1684 if (ost_index < 0 || ost_index >= lov->desc.ld_tgt_count)
1685 GOTO(obj_put, rc = -EINVAL);
1686 /* If OST is inactive, return extent with UNKNOWN flag. */
1687 if (!lov->lov_tgts[ost_index]->ltd_active) {
1688 fs->fs_fm->fm_flags |= FIEMAP_EXTENT_LAST;
1689 fs->fs_fm->fm_mapped_extents = 1;
1691 fm_ext[0].fe_logical = lun_start;
1692 fm_ext[0].fe_length = obd_object_end - lun_start;
1693 fm_ext[0].fe_flags |= FIEMAP_EXTENT_UNKNOWN;
1698 fs->fs_fm->fm_start = lun_start;
1699 fs->fs_fm->fm_flags &= ~FIEMAP_FLAG_DEVICE_ORDER;
1700 memcpy(&fmkey->lfik_fiemap, fs->fs_fm, sizeof(*fs->fs_fm));
1701 *buflen = fiemap_count_to_size(fs->fs_fm->fm_extent_count);
1703 rc = cl_object_fiemap(env, subobj, fmkey, fs->fs_fm, buflen);
1707 ext_count = fs->fs_fm->fm_mapped_extents;
1708 if (ext_count == 0) {
1710 fs->fs_device_done = true;
1711 /* If last stripe has hold at the end,
1712 * we need to return */
1713 if (stripeno == fs->fs_last_stripe) {
1714 fiemap->fm_mapped_extents = 0;
1715 fs->fs_finish_stripe = true;
1719 } else if (fs->fs_enough) {
1721 * We've collected enough extents and there are
1722 * more extents after it.
1727 /* If we just need num of extents, got to next device */
1728 if (fiemap->fm_extent_count == 0) {
1729 fs->fs_cur_extent += ext_count;
1733 /* prepare to copy retrived map extents */
1734 len_mapped_single_call = fm_ext[ext_count - 1].fe_logical +
1735 fm_ext[ext_count - 1].fe_length -
1738 /* Have we finished mapping on this device? */
1739 if (req_fm_len <= len_mapped_single_call) {
1741 fs->fs_device_done = true;
1744 /* Clear the EXTENT_LAST flag which can be present on
1745 * the last extent */
1746 if (fm_ext[ext_count - 1].fe_flags & FIEMAP_EXTENT_LAST)
1747 fm_ext[ext_count - 1].fe_flags &= ~FIEMAP_EXTENT_LAST;
1748 if (lov_stripe_size(lsm, index,
1749 fm_ext[ext_count - 1].fe_logical +
1750 fm_ext[ext_count - 1].fe_length,
1751 stripeno) >= fmkey->lfik_oa.o_size) {
1753 fs->fs_device_done = true;
1756 fiemap_prepare_and_copy_exts(fiemap, fm_ext, ost_index,
1757 ext_count, fs->fs_cur_extent);
1758 fs->fs_cur_extent += ext_count;
1760 /* Ran out of available extents? */
1761 if (fs->fs_cur_extent >= fiemap->fm_extent_count)
1762 fs->fs_enough = true;
1763 } while (!ost_done && !ost_eof);
1765 if (stripeno == fs->fs_last_stripe)
1766 fs->fs_finish_stripe = true;
1768 cl_object_put(env, subobj);
1774 * Break down the FIEMAP request and send appropriate calls to individual OSTs.
1775 * This also handles the restarting of FIEMAP calls in case mapping overflows
1776 * the available number of extents in single call.
1778 * \param env [in] lustre environment
1779 * \param obj [in] file object
1780 * \param fmkey [in] fiemap request header and other info
1781 * \param fiemap [out] fiemap buffer holding retrived map extents
1782 * \param buflen [in/out] max buffer length of @fiemap, when iterate
1783 * each OST, it is used to limit max map needed
1787 static int lov_object_fiemap(const struct lu_env *env, struct cl_object *obj,
1788 struct ll_fiemap_info_key *fmkey,
1789 struct fiemap *fiemap, size_t *buflen)
1791 struct lov_stripe_md_entry *lsme;
1792 struct lov_stripe_md *lsm;
1793 struct fiemap *fm_local = NULL;
1801 unsigned int buffer_size = FIEMAP_BUFFER_SIZE;
1803 struct fiemap_state fs = { 0 };
1806 lsm = lov_lsm_addref(cl2lov(obj));
1808 /* no extent: there is no object for mapping */
1809 fiemap->fm_mapped_extents = 0;
1813 if (!(fiemap->fm_flags & FIEMAP_FLAG_DEVICE_ORDER)) {
1815 * If the entry count > 1 or stripe_count > 1 and the
1816 * application does not understand DEVICE_ORDER flag,
1817 * it cannot interpret the extents correctly.
1819 if (lsm->lsm_entry_count > 1 ||
1820 (lsm->lsm_entry_count == 1 &&
1821 lsm->lsm_entries[0]->lsme_stripe_count > 1))
1822 GOTO(out_lsm, rc = -ENOTSUPP);
1825 /* No support for DOM layout yet. */
1826 if (lsme_is_dom(lsm->lsm_entries[0]))
1827 GOTO(out_lsm, rc = -ENOTSUPP);
1829 if (lsm->lsm_is_released) {
1830 if (fiemap->fm_start < fmkey->lfik_oa.o_size) {
1832 * released file, return a minimal FIEMAP if
1833 * request fits in file-size.
1835 fiemap->fm_mapped_extents = 1;
1836 fiemap->fm_extents[0].fe_logical = fiemap->fm_start;
1837 if (fiemap->fm_start + fiemap->fm_length <
1838 fmkey->lfik_oa.o_size)
1839 fiemap->fm_extents[0].fe_length =
1842 fiemap->fm_extents[0].fe_length =
1843 fmkey->lfik_oa.o_size -
1845 fiemap->fm_extents[0].fe_flags |=
1846 FIEMAP_EXTENT_UNKNOWN | FIEMAP_EXTENT_LAST;
1848 GOTO(out_lsm, rc = 0);
1851 /* buffer_size is small to hold fm_extent_count of extents. */
1852 if (fiemap_count_to_size(fiemap->fm_extent_count) < buffer_size)
1853 buffer_size = fiemap_count_to_size(fiemap->fm_extent_count);
1855 OBD_ALLOC_LARGE(fm_local, buffer_size);
1856 if (fm_local == NULL)
1857 GOTO(out_lsm, rc = -ENOMEM);
1860 * Requested extent count exceeds the fiemap buffer size, shrink our
1863 if (fiemap_count_to_size(fiemap->fm_extent_count) > *buflen)
1864 fiemap->fm_extent_count = fiemap_size_to_count(*buflen);
1865 if (fiemap->fm_extent_count == 0)
1868 fs.fs_enough = false;
1869 fs.fs_cur_extent = 0;
1870 fs.fs_fm = fm_local;
1871 fs.fs_cnt_need = fiemap_size_to_count(buffer_size);
1873 whole_start = fiemap->fm_start;
1874 /* whole_start is beyond the end of the file */
1875 if (whole_start > fmkey->lfik_oa.o_size)
1876 GOTO(out_fm_local, rc = -EINVAL);
1877 whole_end = (fiemap->fm_length == OBD_OBJECT_EOF) ?
1878 fmkey->lfik_oa.o_size :
1879 whole_start + fiemap->fm_length - 1;
1881 * If fiemap->fm_length != OBD_OBJECT_EOF but whole_end exceeds file
1884 if (whole_end > fmkey->lfik_oa.o_size)
1885 whole_end = fmkey->lfik_oa.o_size;
1887 start_entry = lov_lsm_entry(lsm, whole_start);
1888 end_entry = lov_lsm_entry(lsm, whole_end);
1889 if (end_entry == -1)
1890 end_entry = lsm->lsm_entry_count - 1;
1892 if (start_entry == -1 || end_entry == -1)
1893 GOTO(out_fm_local, rc = -EINVAL);
1895 /* TODO: rewrite it with lov_foreach_io_layout() */
1896 for (entry = start_entry; entry <= end_entry; entry++) {
1897 lsme = lsm->lsm_entries[entry];
1899 if (!lsme_inited(lsme))
1902 if (entry == start_entry)
1903 fs.fs_ext.e_start = whole_start;
1905 fs.fs_ext.e_start = lsme->lsme_extent.e_start;
1906 if (entry == end_entry)
1907 fs.fs_ext.e_end = whole_end;
1909 fs.fs_ext.e_end = lsme->lsme_extent.e_end - 1;
1910 fs.fs_length = fs.fs_ext.e_end - fs.fs_ext.e_start + 1;
1912 /* Calculate start stripe, last stripe and length of mapping */
1913 fs.fs_start_stripe = lov_stripe_number(lsm, entry,
1915 fs.fs_last_stripe = fiemap_calc_last_stripe(lsm, entry,
1916 &fs.fs_ext, fs.fs_start_stripe,
1918 fs.fs_end_offset = fiemap_calc_fm_end_offset(fiemap, lsm, entry,
1919 &fs.fs_ext, &fs.fs_start_stripe);
1920 /* Check each stripe */
1921 for (cur_stripe = fs.fs_start_stripe; stripe_count > 0;
1923 cur_stripe = (cur_stripe + 1) % lsme->lsme_stripe_count) {
1924 rc = fiemap_for_stripe(env, obj, lsm, fiemap, buflen,
1925 fmkey, entry, cur_stripe, &fs);
1927 GOTO(out_fm_local, rc);
1930 if (fs.fs_finish_stripe)
1932 } /* for each stripe */
1933 } /* for covering layout component */
1935 * We've traversed all components, set @entry to the last component
1936 * entry, it's for the last stripe check.
1940 /* Indicate that we are returning device offsets unless file just has
1942 if (lsm->lsm_entry_count > 1 ||
1943 (lsm->lsm_entry_count == 1 &&
1944 lsm->lsm_entries[0]->lsme_stripe_count > 1))
1945 fiemap->fm_flags |= FIEMAP_FLAG_DEVICE_ORDER;
1947 if (fiemap->fm_extent_count == 0)
1948 goto skip_last_device_calc;
1950 /* Check if we have reached the last stripe and whether mapping for that
1951 * stripe is done. */
1952 if ((cur_stripe == fs.fs_last_stripe) && fs.fs_device_done)
1953 fiemap->fm_extents[fs.fs_cur_extent - 1].fe_flags |=
1955 skip_last_device_calc:
1956 fiemap->fm_mapped_extents = fs.fs_cur_extent;
1958 OBD_FREE_LARGE(fm_local, buffer_size);
1965 static int lov_object_getstripe(const struct lu_env *env, struct cl_object *obj,
1966 struct lov_user_md __user *lum, size_t size)
1968 struct lov_object *lov = cl2lov(obj);
1969 struct lov_stripe_md *lsm;
1973 lsm = lov_lsm_addref(lov);
1977 rc = lov_getstripe(env, cl2lov(obj), lsm, lum, size);
1982 static int lov_object_layout_get(const struct lu_env *env,
1983 struct cl_object *obj,
1984 struct cl_layout *cl)
1986 struct lov_object *lov = cl2lov(obj);
1987 struct lov_stripe_md *lsm = lov_lsm_addref(lov);
1988 struct lu_buf *buf = &cl->cl_buf;
1994 cl->cl_layout_gen = CL_LAYOUT_GEN_EMPTY;
1999 cl->cl_size = lov_comp_md_size(lsm);
2000 cl->cl_layout_gen = lsm->lsm_layout_gen;
2001 cl->cl_dom_comp_size = 0;
2002 if (lsm_is_composite(lsm->lsm_magic)) {
2003 struct lov_stripe_md_entry *lsme = lsm->lsm_entries[0];
2005 cl->cl_is_composite = true;
2007 if (lsme_is_dom(lsme))
2008 cl->cl_dom_comp_size = lsme->lsme_extent.e_end;
2010 cl->cl_is_composite = false;
2013 rc = lov_lsm_pack(lsm, buf->lb_buf, buf->lb_len);
2016 RETURN(rc < 0 ? rc : 0);
2019 static loff_t lov_object_maxbytes(struct cl_object *obj)
2021 struct lov_object *lov = cl2lov(obj);
2022 struct lov_stripe_md *lsm = lov_lsm_addref(lov);
2028 maxbytes = lsm->lsm_maxbytes;
2035 static const struct cl_object_operations lov_ops = {
2036 .coo_page_init = lov_page_init,
2037 .coo_lock_init = lov_lock_init,
2038 .coo_io_init = lov_io_init,
2039 .coo_attr_get = lov_attr_get,
2040 .coo_attr_update = lov_attr_update,
2041 .coo_conf_set = lov_conf_set,
2042 .coo_getstripe = lov_object_getstripe,
2043 .coo_layout_get = lov_object_layout_get,
2044 .coo_maxbytes = lov_object_maxbytes,
2045 .coo_fiemap = lov_object_fiemap,
2048 static const struct lu_object_operations lov_lu_obj_ops = {
2049 .loo_object_init = lov_object_init,
2050 .loo_object_delete = lov_object_delete,
2051 .loo_object_release = NULL,
2052 .loo_object_free = lov_object_free,
2053 .loo_object_print = lov_object_print,
2054 .loo_object_invariant = NULL
2057 struct lu_object *lov_object_alloc(const struct lu_env *env,
2058 const struct lu_object_header *unused,
2059 struct lu_device *dev)
2061 struct lov_object *lov;
2062 struct lu_object *obj;
2065 OBD_SLAB_ALLOC_PTR_GFP(lov, lov_object_kmem, GFP_NOFS);
2068 lu_object_init(obj, NULL, dev);
2069 lov->lo_cl.co_ops = &lov_ops;
2070 lov->lo_type = -1; /* invalid, to catch uninitialized type */
2072 * object io operation vector (cl_object::co_iop) is installed
2073 * later in lov_object_init(), as different vectors are used
2074 * for object with different layouts.
2076 obj->lo_ops = &lov_lu_obj_ops;
2082 struct lov_stripe_md *lov_lsm_addref(struct lov_object *lov)
2084 struct lov_stripe_md *lsm = NULL;
2086 lov_conf_freeze(lov);
2087 if (lov->lo_lsm != NULL) {
2088 lsm = lsm_addref(lov->lo_lsm);
2089 CDEBUG(D_INODE, "lsm %p addref %d/%d by %p.\n",
2090 lsm, atomic_read(&lsm->lsm_refc),
2091 lov->lo_layout_invalid, current);
2097 int lov_read_and_clear_async_rc(struct cl_object *clob)
2099 struct lu_object *luobj;
2103 luobj = lu_object_locate(&cl_object_header(clob)->coh_lu,
2105 if (luobj != NULL) {
2106 struct lov_object *lov = lu2lov(luobj);
2108 lov_conf_freeze(lov);
2109 switch (lov->lo_type) {
2111 struct lov_stripe_md *lsm;
2115 LASSERT(lsm != NULL);
2116 for (i = 0; i < lsm->lsm_entry_count; i++) {
2117 struct lov_stripe_md_entry *lse =
2118 lsm->lsm_entries[i];
2121 if (!lsme_inited(lse))
2124 for (j = 0; j < lse->lsme_stripe_count; j++) {
2125 struct lov_oinfo *loi =
2128 if (lov_oinfo_is_dummy(loi))
2131 if (loi->loi_ar.ar_rc && !rc)
2132 rc = loi->loi_ar.ar_rc;
2133 loi->loi_ar.ar_rc = 0;
2147 EXPORT_SYMBOL(lov_read_and_clear_async_rc);