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_LARGE(r0->lo_sub, r0->lo_nr * sizeof(r0->lo_sub[0]));
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;
287 wait_queue_entry_t *waiter;
289 LASSERT(r0->lo_sub[idx] == los);
291 sub = lovsub2cl(los);
292 site = sub->co_lu.lo_dev->ld_site;
293 wq = lu_site_wq_from_fid(site, &sub->co_lu.lo_header->loh_fid);
295 cl_object_kill(env, sub);
296 /* release a reference to the sub-object and ... */
297 lu_object_ref_del(&sub->co_lu, "lov-parent", lov);
298 cl_object_put(env, sub);
300 /* ... wait until it is actually destroyed---sub-object clears its
301 * ->lo_sub[] slot in lovsub_object_free() */
302 if (r0->lo_sub[idx] == los) {
303 waiter = &lov_env_info(env)->lti_waiter;
304 init_waitqueue_entry(waiter, current);
305 add_wait_queue(wq, waiter);
306 set_current_state(TASK_UNINTERRUPTIBLE);
308 /* this wait-queue is signaled at the end of
309 * lu_object_free(). */
310 set_current_state(TASK_UNINTERRUPTIBLE);
311 spin_lock(&r0->lo_sub_lock);
312 if (r0->lo_sub[idx] == los) {
313 spin_unlock(&r0->lo_sub_lock);
316 spin_unlock(&r0->lo_sub_lock);
317 set_current_state(TASK_RUNNING);
321 remove_wait_queue(wq, waiter);
323 LASSERT(r0->lo_sub[idx] == NULL);
326 static void lov_delete_raid0(const struct lu_env *env, struct lov_object *lov,
327 struct lov_layout_entry *lle)
329 struct lov_layout_raid0 *r0 = &lle->lle_raid0;
333 if (r0->lo_sub != NULL) {
336 for (i = 0; i < r0->lo_nr; ++i) {
337 struct lovsub_object *los = r0->lo_sub[i];
340 cl_object_prune(env, &los->lso_cl);
342 * If top-level object is to be evicted from
343 * the cache, so are its sub-objects.
345 lov_subobject_kill(env, lov, r0, los, i);
353 static void lov_fini_raid0(const struct lu_env *env,
354 struct lov_layout_entry *lle)
356 struct lov_layout_raid0 *r0 = &lle->lle_raid0;
358 if (r0->lo_sub != NULL) {
359 OBD_FREE_LARGE(r0->lo_sub, r0->lo_nr * sizeof r0->lo_sub[0]);
364 static int lov_print_raid0(const struct lu_env *env, void *cookie,
365 lu_printer_t p, const struct lov_layout_entry *lle)
367 const struct lov_layout_raid0 *r0 = &lle->lle_raid0;
370 for (i = 0; i < r0->lo_nr; ++i) {
371 struct lu_object *sub;
373 if (r0->lo_sub[i] != NULL) {
374 sub = lovsub2lu(r0->lo_sub[i]);
375 lu_object_print(env, cookie, p, sub);
377 (*p)(env, cookie, "sub %d absent\n", i);
383 static int lov_attr_get_raid0(const struct lu_env *env, struct lov_object *lov,
384 unsigned int index, struct lov_layout_entry *lle,
385 struct cl_attr **lov_attr)
387 struct lov_layout_raid0 *r0 = &lle->lle_raid0;
388 struct lov_stripe_md *lsm = lov->lo_lsm;
389 struct ost_lvb *lvb = &lov_env_info(env)->lti_lvb;
390 struct cl_attr *attr = &r0->lo_attr;
394 if (r0->lo_attr_valid) {
399 memset(lvb, 0, sizeof(*lvb));
401 /* XXX: timestamps can be negative by sanity:test_39m,
403 lvb->lvb_atime = LLONG_MIN;
404 lvb->lvb_ctime = LLONG_MIN;
405 lvb->lvb_mtime = LLONG_MIN;
408 * XXX that should be replaced with a loop over sub-objects,
409 * doing cl_object_attr_get() on them. But for now, let's
410 * reuse old lov code.
414 * XXX take lsm spin-lock to keep lov_merge_lvb_kms()
415 * happy. It's not needed, because new code uses
416 * ->coh_attr_guard spin-lock to protect consistency of
417 * sub-object attributes.
419 lov_stripe_lock(lsm);
420 result = lov_merge_lvb_kms(lsm, index, lvb, &kms);
421 lov_stripe_unlock(lsm);
423 cl_lvb2attr(attr, lvb);
425 r0->lo_attr_valid = 1;
432 static struct lov_comp_layout_entry_ops raid0_ops = {
433 .lco_init = lov_init_raid0,
434 .lco_fini = lov_fini_raid0,
435 .lco_getattr = lov_attr_get_raid0,
438 static int lov_attr_get_dom(const struct lu_env *env, struct lov_object *lov,
439 unsigned int index, struct lov_layout_entry *lle,
440 struct cl_attr **lov_attr)
442 struct lov_layout_dom *dom = &lle->lle_dom;
443 struct lov_oinfo *loi = dom->lo_loi;
444 struct cl_attr *attr = &dom->lo_dom_r0.lo_attr;
446 if (dom->lo_dom_r0.lo_attr_valid) {
451 if (OST_LVB_IS_ERR(loi->loi_lvb.lvb_blocks))
452 return OST_LVB_GET_ERR(loi->loi_lvb.lvb_blocks);
454 cl_lvb2attr(attr, &loi->loi_lvb);
456 /* DoM component size can be bigger than stripe size after
457 * client's setattr RPC, so do not count anything beyond
458 * component end. Alternatively, check that limit on server
459 * and do not allow size overflow there. */
460 if (attr->cat_size > lle->lle_extent->e_end)
461 attr->cat_size = lle->lle_extent->e_end;
463 attr->cat_kms = attr->cat_size;
465 dom->lo_dom_r0.lo_attr_valid = 1;
472 * Lookup FLD to get MDS index of the given DOM object FID.
474 * \param[in] ld LOV device
475 * \param[in] fid FID to lookup
476 * \param[out] nr index in MDC array to return back
478 * \retval 0 and \a mds filled with MDS index if successful
479 * \retval negative value on error
481 static int lov_fld_lookup(struct lov_device *ld, const struct lu_fid *fid,
489 rc = fld_client_lookup(&ld->ld_lmv->u.lmv.lmv_fld, fid_seq(fid),
490 &mds_idx, LU_SEQ_RANGE_MDT, NULL);
492 CERROR("%s: error while looking for mds number. Seq %#llx"
493 ", err = %d\n", lu_dev_name(cl2lu_dev(&ld->ld_cl)),
498 CDEBUG(D_INODE, "FLD lookup got mds #%x for fid="DFID"\n",
501 /* find proper MDC device in the array */
502 for (i = 0; i < ld->ld_md_tgts_nr; i++) {
503 if (ld->ld_md_tgts[i].ldm_mdc != NULL &&
504 ld->ld_md_tgts[i].ldm_idx == mds_idx)
508 if (i == ld->ld_md_tgts_nr) {
509 CERROR("%s: cannot find corresponding MDC device for mds #%x "
510 "for fid="DFID"\n", lu_dev_name(cl2lu_dev(&ld->ld_cl)),
520 * Implementation of lov_comp_layout_entry_ops::lco_init for DOM object.
522 * Init the DOM object for the first time. It prepares also RAID0 entry
523 * for it to use in common methods with ordinary RAID0 layout entries.
525 * \param[in] env execution environment
526 * \param[in] dev LOV device
527 * \param[in] lov LOV object
528 * \param[in] index Composite layout entry index in LSM
529 * \param[in] lle Composite LOV layout entry
531 static int lov_init_dom(const struct lu_env *env, struct lov_device *dev,
532 struct lov_object *lov, unsigned int index,
533 const struct cl_object_conf *conf,
534 struct lov_layout_entry *lle)
536 struct lov_thread_info *lti = lov_env_info(env);
537 struct lov_stripe_md_entry *lsme = lov_lse(lov, index);
538 struct cl_object *clo;
539 struct lu_object *o = lov2lu(lov);
540 const struct lu_fid *fid = lu_object_fid(o);
541 struct cl_device *mdcdev;
542 struct lov_oinfo *loi = NULL;
543 struct cl_object_conf *sconf = <i->lti_stripe_conf;
549 /* DOM entry may be not zero index due to FLR but must start from 0 */
550 if (unlikely(lle->lle_extent->e_start != 0)) {
551 CERROR("%s: DOM entry must be the first stripe in a mirror\n",
552 lov2obd(dev->ld_lov)->obd_name);
553 dump_lsm(D_ERROR, lov->lo_lsm);
557 /* find proper MDS device */
558 rc = lov_fld_lookup(dev, fid, &idx);
562 LASSERTF(dev->ld_md_tgts[idx].ldm_mdc != NULL,
563 "LOV md target[%u] is NULL\n", idx);
565 /* check lsm is DOM, more checks are needed */
566 LASSERT(lsme->lsme_stripe_count == 0);
569 * Create lower cl_objects.
571 mdcdev = dev->ld_md_tgts[idx].ldm_mdc;
573 LASSERTF(mdcdev != NULL, "non-initialized mdc subdev\n");
575 /* DoM object has no oinfo in LSM entry, create it exclusively */
576 OBD_SLAB_ALLOC_PTR_GFP(loi, lov_oinfo_slab, GFP_NOFS);
580 fid_to_ostid(lu_object_fid(lov2lu(lov)), &loi->loi_oi);
582 sconf->u.coc_oinfo = loi;
584 clo = lov_sub_find(env, mdcdev, fid, sconf);
586 GOTO(out, rc = PTR_ERR(clo));
588 rc = lov_init_sub(env, lov, clo, loi, lov_comp_index(index, 0));
589 if (rc == -EAGAIN) /* try again */
594 lle->lle_dom.lo_dom = cl2lovsub(clo);
595 spin_lock_init(&lle->lle_dom.lo_dom_r0.lo_sub_lock);
596 lle->lle_dom.lo_dom_r0.lo_nr = 1;
597 lle->lle_dom.lo_dom_r0.lo_sub = &lle->lle_dom.lo_dom;
598 lle->lle_dom.lo_loi = loi;
600 rc = lov_page_slice_fixup(lov, clo);
605 OBD_SLAB_FREE_PTR(loi, lov_oinfo_slab);
610 * Implementation of lov_layout_operations::llo_fini for DOM object.
612 * Finish the DOM object and free related memory.
614 * \param[in] env execution environment
615 * \param[in] lov LOV object
616 * \param[in] state LOV layout state
618 static void lov_fini_dom(const struct lu_env *env,
619 struct lov_layout_entry *lle)
621 if (lle->lle_dom.lo_dom != NULL)
622 lle->lle_dom.lo_dom = NULL;
623 if (lle->lle_dom.lo_loi != NULL)
624 OBD_SLAB_FREE_PTR(lle->lle_dom.lo_loi, lov_oinfo_slab);
627 static struct lov_comp_layout_entry_ops dom_ops = {
628 .lco_init = lov_init_dom,
629 .lco_fini = lov_fini_dom,
630 .lco_getattr = lov_attr_get_dom,
633 static int lov_init_composite(const struct lu_env *env, struct lov_device *dev,
634 struct lov_object *lov, struct lov_stripe_md *lsm,
635 const struct cl_object_conf *conf,
636 union lov_layout_state *state)
638 struct lov_layout_composite *comp = &state->composite;
639 struct lov_layout_entry *lle;
640 struct lov_mirror_entry *lre;
641 unsigned int entry_count;
642 unsigned int psz = 0;
643 unsigned int mirror_count;
644 int flr_state = lsm->lsm_flags & LCM_FL_FLR_MASK;
652 LASSERT(lsm->lsm_entry_count > 0);
653 LASSERT(lov->lo_lsm == NULL);
654 lov->lo_lsm = lsm_addref(lsm);
655 lov->lo_layout_invalid = true;
657 dump_lsm(D_INODE, lsm);
659 entry_count = lsm->lsm_entry_count;
661 spin_lock_init(&comp->lo_write_lock);
662 comp->lo_flags = lsm->lsm_flags;
663 comp->lo_mirror_count = lsm->lsm_mirror_count + 1;
664 comp->lo_entry_count = lsm->lsm_entry_count;
665 comp->lo_preferred_mirror = -1;
667 if (equi(flr_state == LCM_FL_NONE, comp->lo_mirror_count > 1))
670 OBD_ALLOC(comp->lo_mirrors,
671 comp->lo_mirror_count * sizeof(*comp->lo_mirrors));
672 if (comp->lo_mirrors == NULL)
675 OBD_ALLOC(comp->lo_entries, entry_count * sizeof(*comp->lo_entries));
676 if (comp->lo_entries == NULL)
679 /* Initiate all entry types and extents data at first */
680 for (i = 0, j = 0, mirror_count = 1; i < entry_count; i++) {
683 lle = &comp->lo_entries[i];
685 lle->lle_lsme = lsm->lsm_entries[i];
686 lle->lle_type = lov_entry_type(lle->lle_lsme);
687 switch (lle->lle_type) {
688 case LOV_PATTERN_RAID0:
689 lle->lle_comp_ops = &raid0_ops;
691 case LOV_PATTERN_MDT:
692 /* Allowed to have several DOM stripes in different
693 * mirrors with the same DoM size.
696 dom_size = lle->lle_lsme->lsme_extent.e_end;
697 } else if (dom_size !=
698 lle->lle_lsme->lsme_extent.e_end) {
699 CERROR("%s: DOM entries with different sizes\n",
700 lov2obd(dev->ld_lov)->obd_name);
701 dump_lsm(D_ERROR, lsm);
704 lle->lle_comp_ops = &dom_ops;
707 CERROR("%s: unknown composite layout entry type %i\n",
708 lov2obd(dev->ld_lov)->obd_name,
709 lsm->lsm_entries[i]->lsme_pattern);
710 dump_lsm(D_ERROR, lsm);
714 lle->lle_extent = &lle->lle_lsme->lsme_extent;
715 lle->lle_valid = !(lle->lle_lsme->lsme_flags & LCME_FL_STALE);
717 if (flr_state != LCM_FL_NONE)
718 mirror_id = mirror_id_of(lle->lle_lsme->lsme_id);
720 lre = &comp->lo_mirrors[j];
722 if (mirror_id == lre->lre_mirror_id) {
723 lre->lre_valid |= lle->lle_valid;
724 lre->lre_stale |= !lle->lle_valid;
729 /* new mirror detected, assume that the mirrors
730 * are shorted in layout */
733 if (j >= comp->lo_mirror_count)
736 lre = &comp->lo_mirrors[j];
739 /* entries must be sorted by mirrors */
740 lre->lre_mirror_id = mirror_id;
741 lre->lre_start = lre->lre_end = i;
742 lre->lre_preferred = !!(lle->lle_lsme->lsme_flags &
744 lre->lre_valid = lle->lle_valid;
745 lre->lre_stale = !lle->lle_valid;
748 /* sanity check for FLR */
749 if (mirror_count != comp->lo_mirror_count) {
751 " doesn't have the # of mirrors it claims, %u/%u\n",
752 PFID(lu_object_fid(lov2lu(lov))), mirror_count,
753 comp->lo_mirror_count + 1);
755 GOTO(out, result = -EINVAL);
758 lov_foreach_layout_entry(lov, lle) {
759 int index = lov_layout_entry_index(lov, lle);
762 * If the component has not been init-ed on MDS side, for
763 * PFL layout, we'd know that the components beyond this one
764 * will be dynamically init-ed later on file write/trunc ops.
766 if (!lsme_inited(lle->lle_lsme))
769 result = lle->lle_comp_ops->lco_init(env, dev, lov, index,
774 LASSERT(ergo(psz > 0, psz == result));
779 cl_object_header(&lov->lo_cl)->coh_page_bufsize += psz;
781 /* decide the preferred mirror. It uses the hash value of lov_object
782 * so that different clients would use different mirrors for read. */
784 seq = hash_long((unsigned long)lov, 8);
785 for (i = 0; i < comp->lo_mirror_count; i++) {
786 unsigned int idx = (i + seq) % comp->lo_mirror_count;
788 lre = lov_mirror_entry(lov, idx);
792 mirror_count++; /* valid mirror */
794 if (lre->lre_preferred || comp->lo_preferred_mirror < 0)
795 comp->lo_preferred_mirror = idx;
799 " doesn't have any valid mirrors\n",
800 PFID(lu_object_fid(lov2lu(lov))));
802 comp->lo_preferred_mirror = 0;
805 LASSERT(comp->lo_preferred_mirror >= 0);
809 return result > 0 ? 0 : result;
812 static int lov_init_empty(const struct lu_env *env, struct lov_device *dev,
813 struct lov_object *lov, struct lov_stripe_md *lsm,
814 const struct cl_object_conf *conf,
815 union lov_layout_state *state)
820 static int lov_init_released(const struct lu_env *env,
821 struct lov_device *dev, struct lov_object *lov,
822 struct lov_stripe_md *lsm,
823 const struct cl_object_conf *conf,
824 union lov_layout_state *state)
826 LASSERT(lsm != NULL);
827 LASSERT(lsm->lsm_is_released);
828 LASSERT(lov->lo_lsm == NULL);
830 lov->lo_lsm = lsm_addref(lsm);
834 static int lov_init_foreign(const struct lu_env *env,
835 struct lov_device *dev, struct lov_object *lov,
836 struct lov_stripe_md *lsm,
837 const struct cl_object_conf *conf,
838 union lov_layout_state *state)
840 LASSERT(lsm != NULL);
841 LASSERT(lov->lo_type == LLT_FOREIGN);
842 LASSERT(lov->lo_lsm == NULL);
844 lov->lo_lsm = lsm_addref(lsm);
848 static int lov_delete_empty(const struct lu_env *env, struct lov_object *lov,
849 union lov_layout_state *state)
851 LASSERT(lov->lo_type == LLT_EMPTY || lov->lo_type == LLT_RELEASED ||
852 lov->lo_type == LLT_FOREIGN);
854 lov_layout_wait(env, lov);
858 static int lov_delete_composite(const struct lu_env *env,
859 struct lov_object *lov,
860 union lov_layout_state *state)
862 struct lov_layout_entry *entry;
863 struct lov_layout_composite *comp = &state->composite;
867 dump_lsm(D_INODE, lov->lo_lsm);
869 lov_layout_wait(env, lov);
870 if (comp->lo_entries)
871 lov_foreach_layout_entry(lov, entry)
872 lov_delete_raid0(env, lov, entry);
877 static void lov_fini_empty(const struct lu_env *env, struct lov_object *lov,
878 union lov_layout_state *state)
880 LASSERT(lov->lo_type == LLT_EMPTY || lov->lo_type == LLT_RELEASED);
883 static void lov_fini_composite(const struct lu_env *env,
884 struct lov_object *lov,
885 union lov_layout_state *state)
887 struct lov_layout_composite *comp = &state->composite;
890 if (comp->lo_entries != NULL) {
891 struct lov_layout_entry *entry;
893 lov_foreach_layout_entry(lov, entry)
894 if (entry->lle_comp_ops)
895 entry->lle_comp_ops->lco_fini(env, entry);
897 OBD_FREE(comp->lo_entries,
898 comp->lo_entry_count * sizeof(*comp->lo_entries));
899 comp->lo_entries = NULL;
902 if (comp->lo_mirrors != NULL) {
903 OBD_FREE(comp->lo_mirrors,
904 comp->lo_mirror_count * sizeof(*comp->lo_mirrors));
905 comp->lo_mirrors = NULL;
908 memset(comp, 0, sizeof(*comp));
910 dump_lsm(D_INODE, lov->lo_lsm);
911 lov_free_memmd(&lov->lo_lsm);
916 static void lov_fini_released(const struct lu_env *env, struct lov_object *lov,
917 union lov_layout_state *state)
920 dump_lsm(D_INODE, lov->lo_lsm);
921 lov_free_memmd(&lov->lo_lsm);
925 static int lov_print_empty(const struct lu_env *env, void *cookie,
926 lu_printer_t p, const struct lu_object *o)
928 (*p)(env, cookie, "empty %d\n", lu2lov(o)->lo_layout_invalid);
932 static int lov_print_composite(const struct lu_env *env, void *cookie,
933 lu_printer_t p, const struct lu_object *o)
935 struct lov_object *lov = lu2lov(o);
936 struct lov_stripe_md *lsm = lov->lo_lsm;
939 (*p)(env, cookie, "entries: %d, %s, lsm{%p 0x%08X %d %u}:\n",
940 lsm->lsm_entry_count,
941 lov->lo_layout_invalid ? "invalid" : "valid", lsm,
942 lsm->lsm_magic, atomic_read(&lsm->lsm_refc),
943 lsm->lsm_layout_gen);
945 for (i = 0; i < lsm->lsm_entry_count; i++) {
946 struct lov_stripe_md_entry *lse = lsm->lsm_entries[i];
947 struct lov_layout_entry *lle = lov_entry(lov, i);
950 DEXT ": { 0x%08X, %u, %#x, %u, %#x, %u, %u }\n",
951 PEXT(&lse->lsme_extent), lse->lsme_magic,
952 lse->lsme_id, lse->lsme_pattern, lse->lsme_layout_gen,
953 lse->lsme_flags, lse->lsme_stripe_count,
954 lse->lsme_stripe_size);
955 lov_print_raid0(env, cookie, p, lle);
961 static int lov_print_released(const struct lu_env *env, void *cookie,
962 lu_printer_t p, const struct lu_object *o)
964 struct lov_object *lov = lu2lov(o);
965 struct lov_stripe_md *lsm = lov->lo_lsm;
968 "released: %s, lsm{%p 0x%08X %d %u}:\n",
969 lov->lo_layout_invalid ? "invalid" : "valid", lsm,
970 lsm->lsm_magic, atomic_read(&lsm->lsm_refc),
971 lsm->lsm_layout_gen);
975 static int lov_print_foreign(const struct lu_env *env, void *cookie,
976 lu_printer_t p, const struct lu_object *o)
978 struct lov_object *lov = lu2lov(o);
979 struct lov_stripe_md *lsm = lov->lo_lsm;
982 "foreign: %s, lsm{%p 0x%08X %d %u}:\n",
983 lov->lo_layout_invalid ? "invalid" : "valid", lsm,
984 lsm->lsm_magic, atomic_read(&lsm->lsm_refc),
985 lsm->lsm_layout_gen);
987 "raw_ea_content '%.*s'\n",
988 (int)lsm->lsm_foreign_size, (char *)lsm_foreign(lsm));
993 * Implements cl_object_operations::coo_attr_get() method for an object
994 * without stripes (LLT_EMPTY layout type).
996 * The only attributes this layer is authoritative in this case is
997 * cl_attr::cat_blocks---it's 0.
999 static int lov_attr_get_empty(const struct lu_env *env, struct cl_object *obj,
1000 struct cl_attr *attr)
1002 attr->cat_blocks = 0;
1006 static int lov_attr_get_composite(const struct lu_env *env,
1007 struct cl_object *obj,
1008 struct cl_attr *attr)
1010 struct lov_object *lov = cl2lov(obj);
1011 struct lov_layout_entry *entry;
1017 attr->cat_blocks = 0;
1018 lov_foreach_layout_entry(lov, entry) {
1019 struct cl_attr *lov_attr = NULL;
1020 int index = lov_layout_entry_index(lov, entry);
1022 if (!entry->lle_valid)
1025 /* PFL: This component has not been init-ed. */
1026 if (!lsm_entry_inited(lov->lo_lsm, index))
1029 result = entry->lle_comp_ops->lco_getattr(env, lov, index,
1034 if (lov_attr == NULL)
1037 CDEBUG(D_INODE, "COMP ID #%i: s=%llu m=%llu a=%llu c=%llu "
1038 "b=%llu\n", index - 1, lov_attr->cat_size,
1039 lov_attr->cat_mtime, lov_attr->cat_atime,
1040 lov_attr->cat_ctime, lov_attr->cat_blocks);
1043 attr->cat_blocks += lov_attr->cat_blocks;
1044 if (attr->cat_size < lov_attr->cat_size)
1045 attr->cat_size = lov_attr->cat_size;
1046 if (attr->cat_kms < lov_attr->cat_kms)
1047 attr->cat_kms = lov_attr->cat_kms;
1048 if (attr->cat_atime < lov_attr->cat_atime)
1049 attr->cat_atime = lov_attr->cat_atime;
1050 if (attr->cat_ctime < lov_attr->cat_ctime)
1051 attr->cat_ctime = lov_attr->cat_ctime;
1052 if (attr->cat_mtime < lov_attr->cat_mtime)
1053 attr->cat_mtime = lov_attr->cat_mtime;
1059 static int lov_flush_composite(const struct lu_env *env,
1060 struct cl_object *obj,
1061 struct ldlm_lock *lock)
1063 struct lov_object *lov = cl2lov(obj);
1064 struct lovsub_object *lovsub;
1068 if (!lsme_is_dom(lov->lo_lsm->lsm_entries[0]))
1071 lovsub = lov->u.composite.lo_entries[0].lle_dom.lo_dom;
1072 RETURN(cl_object_flush(env, lovsub2cl(lovsub), lock));
1075 const static struct lov_layout_operations lov_dispatch[] = {
1077 .llo_init = lov_init_empty,
1078 .llo_delete = lov_delete_empty,
1079 .llo_fini = lov_fini_empty,
1080 .llo_print = lov_print_empty,
1081 .llo_page_init = lov_page_init_empty,
1082 .llo_lock_init = lov_lock_init_empty,
1083 .llo_io_init = lov_io_init_empty,
1084 .llo_getattr = lov_attr_get_empty,
1087 .llo_init = lov_init_released,
1088 .llo_delete = lov_delete_empty,
1089 .llo_fini = lov_fini_released,
1090 .llo_print = lov_print_released,
1091 .llo_page_init = lov_page_init_empty,
1092 .llo_lock_init = lov_lock_init_empty,
1093 .llo_io_init = lov_io_init_released,
1094 .llo_getattr = lov_attr_get_empty,
1097 .llo_init = lov_init_composite,
1098 .llo_delete = lov_delete_composite,
1099 .llo_fini = lov_fini_composite,
1100 .llo_print = lov_print_composite,
1101 .llo_page_init = lov_page_init_composite,
1102 .llo_lock_init = lov_lock_init_composite,
1103 .llo_io_init = lov_io_init_composite,
1104 .llo_getattr = lov_attr_get_composite,
1105 .llo_flush = lov_flush_composite,
1108 .llo_init = lov_init_foreign,
1109 .llo_delete = lov_delete_empty,
1110 .llo_fini = lov_fini_released,
1111 .llo_print = lov_print_foreign,
1112 .llo_page_init = lov_page_init_foreign,
1113 .llo_lock_init = lov_lock_init_empty,
1114 .llo_io_init = lov_io_init_empty,
1115 .llo_getattr = lov_attr_get_empty,
1120 * Performs a double-dispatch based on the layout type of an object.
1122 #define LOV_2DISPATCH_NOLOCK(obj, op, ...) \
1124 struct lov_object *__obj = (obj); \
1125 enum lov_layout_type __llt; \
1127 __llt = __obj->lo_type; \
1128 LASSERT(__llt < ARRAY_SIZE(lov_dispatch)); \
1129 lov_dispatch[__llt].op(__VA_ARGS__); \
1133 * Return lov_layout_type associated with a given lsm
1135 static enum lov_layout_type lov_type(struct lov_stripe_md *lsm)
1140 if (lsm->lsm_is_released)
1141 return LLT_RELEASED;
1143 if (lsm->lsm_magic == LOV_MAGIC_V1 ||
1144 lsm->lsm_magic == LOV_MAGIC_V3 ||
1145 lsm->lsm_magic == LOV_MAGIC_COMP_V1)
1148 if (lsm->lsm_magic == LOV_MAGIC_FOREIGN)
1154 static inline void lov_conf_freeze(struct lov_object *lov)
1156 CDEBUG(D_INODE, "To take share lov(%p) owner %p/%p\n",
1157 lov, lov->lo_owner, current);
1158 if (lov->lo_owner != current)
1159 down_read(&lov->lo_type_guard);
1162 static inline void lov_conf_thaw(struct lov_object *lov)
1164 CDEBUG(D_INODE, "To release share lov(%p) owner %p/%p\n",
1165 lov, lov->lo_owner, current);
1166 if (lov->lo_owner != current)
1167 up_read(&lov->lo_type_guard);
1170 #define LOV_2DISPATCH_MAYLOCK(obj, op, lock, ...) \
1172 struct lov_object *__obj = (obj); \
1173 int __lock = !!(lock); \
1174 typeof(lov_dispatch[0].op(__VA_ARGS__)) __result; \
1177 lov_conf_freeze(__obj); \
1178 __result = LOV_2DISPATCH_NOLOCK(obj, op, __VA_ARGS__); \
1180 lov_conf_thaw(__obj); \
1185 * Performs a locked double-dispatch based on the layout type of an object.
1187 #define LOV_2DISPATCH(obj, op, ...) \
1188 LOV_2DISPATCH_MAYLOCK(obj, op, 1, __VA_ARGS__)
1190 #define LOV_2DISPATCH_VOID(obj, op, ...) \
1192 struct lov_object *__obj = (obj); \
1193 enum lov_layout_type __llt; \
1195 lov_conf_freeze(__obj); \
1196 __llt = __obj->lo_type; \
1197 LASSERT(__llt < ARRAY_SIZE(lov_dispatch)); \
1198 lov_dispatch[__llt].op(__VA_ARGS__); \
1199 lov_conf_thaw(__obj); \
1202 static void lov_conf_lock(struct lov_object *lov)
1204 LASSERT(lov->lo_owner != current);
1205 down_write(&lov->lo_type_guard);
1206 LASSERT(lov->lo_owner == NULL);
1207 lov->lo_owner = current;
1208 CDEBUG(D_INODE, "Took exclusive lov(%p) owner %p\n",
1209 lov, lov->lo_owner);
1212 static void lov_conf_unlock(struct lov_object *lov)
1214 CDEBUG(D_INODE, "To release exclusive lov(%p) owner %p\n",
1215 lov, lov->lo_owner);
1216 lov->lo_owner = NULL;
1217 up_write(&lov->lo_type_guard);
1220 static int lov_layout_wait(const struct lu_env *env, struct lov_object *lov)
1222 struct l_wait_info lwi = { 0 };
1225 while (atomic_read(&lov->lo_active_ios) > 0) {
1226 CDEBUG(D_INODE, "file:"DFID" wait for active IO, now: %d.\n",
1227 PFID(lu_object_fid(lov2lu(lov))),
1228 atomic_read(&lov->lo_active_ios));
1230 l_wait_event(lov->lo_waitq,
1231 atomic_read(&lov->lo_active_ios) == 0, &lwi);
1236 static int lov_layout_change(const struct lu_env *unused,
1237 struct lov_object *lov, struct lov_stripe_md *lsm,
1238 const struct cl_object_conf *conf)
1240 enum lov_layout_type llt = lov_type(lsm);
1241 union lov_layout_state *state = &lov->u;
1242 const struct lov_layout_operations *old_ops;
1243 const struct lov_layout_operations *new_ops;
1244 struct lov_device *lov_dev = lov_object_dev(lov);
1250 LASSERT(lov->lo_type < ARRAY_SIZE(lov_dispatch));
1252 env = cl_env_get(&refcheck);
1254 RETURN(PTR_ERR(env));
1256 LASSERT(llt < ARRAY_SIZE(lov_dispatch));
1258 CDEBUG(D_INODE, DFID" from %s to %s\n",
1259 PFID(lu_object_fid(lov2lu(lov))),
1260 llt2str(lov->lo_type), llt2str(llt));
1262 old_ops = &lov_dispatch[lov->lo_type];
1263 new_ops = &lov_dispatch[llt];
1265 rc = cl_object_prune(env, &lov->lo_cl);
1269 rc = old_ops->llo_delete(env, lov, &lov->u);
1273 old_ops->llo_fini(env, lov, &lov->u);
1275 LASSERT(atomic_read(&lov->lo_active_ios) == 0);
1277 CDEBUG(D_INODE, DFID "Apply new layout lov %p, type %d\n",
1278 PFID(lu_object_fid(lov2lu(lov))), lov, llt);
1280 /* page bufsize fixup */
1281 cl_object_header(&lov->lo_cl)->coh_page_bufsize -=
1282 lov_page_slice_fixup(lov, NULL);
1285 rc = new_ops->llo_init(env, lov_dev, lov, lsm, conf, state);
1287 struct obd_device *obd = lov2obd(lov_dev->ld_lov);
1289 CERROR("%s: cannot apply new layout on "DFID" : rc = %d\n",
1290 obd->obd_name, PFID(lu_object_fid(lov2lu(lov))), rc);
1291 new_ops->llo_delete(env, lov, state);
1292 new_ops->llo_fini(env, lov, state);
1293 /* this file becomes an EMPTY file. */
1294 lov->lo_type = LLT_EMPTY;
1299 cl_env_put(env, &refcheck);
1303 /*****************************************************************************
1305 * Lov object operations.
1308 int lov_object_init(const struct lu_env *env, struct lu_object *obj,
1309 const struct lu_object_conf *conf)
1311 struct lov_object *lov = lu2lov(obj);
1312 struct lov_device *dev = lov_object_dev(lov);
1313 const struct cl_object_conf *cconf = lu2cl_conf(conf);
1314 union lov_layout_state *set = &lov->u;
1315 const struct lov_layout_operations *ops;
1316 struct lov_stripe_md *lsm = NULL;
1320 init_rwsem(&lov->lo_type_guard);
1321 atomic_set(&lov->lo_active_ios, 0);
1322 init_waitqueue_head(&lov->lo_waitq);
1323 cl_object_page_init(lu2cl(obj), sizeof(struct lov_page));
1325 lov->lo_type = LLT_EMPTY;
1326 if (cconf->u.coc_layout.lb_buf != NULL) {
1327 lsm = lov_unpackmd(dev->ld_lov,
1328 cconf->u.coc_layout.lb_buf,
1329 cconf->u.coc_layout.lb_len);
1331 RETURN(PTR_ERR(lsm));
1333 dump_lsm(D_INODE, lsm);
1336 /* no locking is necessary, as object is being created */
1337 lov->lo_type = lov_type(lsm);
1338 ops = &lov_dispatch[lov->lo_type];
1339 rc = ops->llo_init(env, dev, lov, lsm, cconf, set);
1349 static int lov_conf_set(const struct lu_env *env, struct cl_object *obj,
1350 const struct cl_object_conf *conf)
1352 struct lov_stripe_md *lsm = NULL;
1353 struct lov_object *lov = cl2lov(obj);
1357 if (conf->coc_opc == OBJECT_CONF_SET &&
1358 conf->u.coc_layout.lb_buf != NULL) {
1359 lsm = lov_unpackmd(lov_object_dev(lov)->ld_lov,
1360 conf->u.coc_layout.lb_buf,
1361 conf->u.coc_layout.lb_len);
1363 RETURN(PTR_ERR(lsm));
1364 dump_lsm(D_INODE, lsm);
1368 if (conf->coc_opc == OBJECT_CONF_INVALIDATE) {
1369 lov->lo_layout_invalid = true;
1370 GOTO(out, result = 0);
1373 if (conf->coc_opc == OBJECT_CONF_WAIT) {
1374 if (lov->lo_layout_invalid &&
1375 atomic_read(&lov->lo_active_ios) > 0) {
1376 lov_conf_unlock(lov);
1377 result = lov_layout_wait(env, lov);
1383 LASSERT(conf->coc_opc == OBJECT_CONF_SET);
1385 if ((lsm == NULL && lov->lo_lsm == NULL) ||
1386 ((lsm != NULL && lov->lo_lsm != NULL) &&
1387 (lov->lo_lsm->lsm_layout_gen == lsm->lsm_layout_gen) &&
1388 (lov->lo_lsm->lsm_entries[0]->lsme_pattern ==
1389 lsm->lsm_entries[0]->lsme_pattern))) {
1390 /* same version of layout */
1391 lov->lo_layout_invalid = false;
1392 GOTO(out, result = 0);
1395 /* will change layout - check if there still exists active IO. */
1396 if (atomic_read(&lov->lo_active_ios) > 0) {
1397 lov->lo_layout_invalid = true;
1398 GOTO(out, result = -EBUSY);
1401 result = lov_layout_change(env, lov, lsm, conf);
1402 lov->lo_layout_invalid = result != 0;
1406 lov_conf_unlock(lov);
1408 CDEBUG(D_INODE, DFID" lo_layout_invalid=%d\n",
1409 PFID(lu_object_fid(lov2lu(lov))), lov->lo_layout_invalid);
1413 static void lov_object_delete(const struct lu_env *env, struct lu_object *obj)
1415 struct lov_object *lov = lu2lov(obj);
1418 LOV_2DISPATCH_VOID(lov, llo_delete, env, lov, &lov->u);
1422 static void lov_object_free(const struct lu_env *env, struct lu_object *obj)
1424 struct lov_object *lov = lu2lov(obj);
1427 LOV_2DISPATCH_VOID(lov, llo_fini, env, lov, &lov->u);
1428 lu_object_fini(obj);
1429 OBD_SLAB_FREE_PTR(lov, lov_object_kmem);
1433 static int lov_object_print(const struct lu_env *env, void *cookie,
1434 lu_printer_t p, const struct lu_object *o)
1436 return LOV_2DISPATCH_NOLOCK(lu2lov(o), llo_print, env, cookie, p, o);
1439 int lov_page_init(const struct lu_env *env, struct cl_object *obj,
1440 struct cl_page *page, pgoff_t index)
1442 return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_page_init, env, obj, page,
1447 * Implements cl_object_operations::clo_io_init() method for lov
1448 * layer. Dispatches to the appropriate layout io initialization method.
1450 int lov_io_init(const struct lu_env *env, struct cl_object *obj,
1453 CL_IO_SLICE_CLEAN(lov_env_io(env), lis_preserved);
1455 CDEBUG(D_INODE, DFID "io %p type %d ignore/verify layout %d/%d\n",
1456 PFID(lu_object_fid(&obj->co_lu)), io, io->ci_type,
1457 io->ci_ignore_layout, io->ci_verify_layout);
1459 /* IO type CIT_MISC with ci_ignore_layout set are usually invoked from
1460 * the OSC layer. It shouldn't take lov layout conf lock in that case,
1461 * because as long as the OSC object exists, the layout can't be
1463 return LOV_2DISPATCH_MAYLOCK(cl2lov(obj), llo_io_init,
1464 !(io->ci_ignore_layout && io->ci_type == CIT_MISC),
1469 * An implementation of cl_object_operations::clo_attr_get() method for lov
1470 * layer. For raid0 layout this collects and merges attributes of all
1473 static int lov_attr_get(const struct lu_env *env, struct cl_object *obj,
1474 struct cl_attr *attr)
1476 /* do not take lock, as this function is called under a
1477 * spin-lock. Layout is protected from changing by ongoing IO. */
1478 return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_getattr, env, obj, attr);
1481 static int lov_attr_update(const struct lu_env *env, struct cl_object *obj,
1482 const struct cl_attr *attr, unsigned valid)
1485 * No dispatch is required here, as no layout implements this.
1490 int lov_lock_init(const struct lu_env *env, struct cl_object *obj,
1491 struct cl_lock *lock, const struct cl_io *io)
1493 /* No need to lock because we've taken one refcount of layout. */
1494 return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_lock_init, env, obj, lock,
1499 * We calculate on which OST the mapping will end. If the length of mapping
1500 * is greater than (stripe_size * stripe_count) then the last_stripe will
1501 * will be one just before start_stripe. Else we check if the mapping
1502 * intersects each OST and find last_stripe.
1503 * This function returns the last_stripe and also sets the stripe_count
1504 * over which the mapping is spread
1506 * \param lsm [in] striping information for the file
1507 * \param index [in] stripe component index
1508 * \param ext [in] logical extent of mapping
1509 * \param start_stripe [in] starting stripe of the mapping
1510 * \param stripe_count [out] the number of stripes across which to map is
1513 * \retval last_stripe return the last stripe of the mapping
1515 static int fiemap_calc_last_stripe(struct lov_stripe_md *lsm, int index,
1516 struct lu_extent *ext,
1517 int start_stripe, int *stripe_count)
1519 struct lov_stripe_md_entry *lsme = lsm->lsm_entries[index];
1525 if (ext->e_end - ext->e_start >
1526 lsme->lsme_stripe_size * lsme->lsme_stripe_count) {
1527 last_stripe = (start_stripe < 1 ? lsme->lsme_stripe_count - 1 :
1529 *stripe_count = lsme->lsme_stripe_count;
1531 for (j = 0, i = start_stripe; j < lsme->lsme_stripe_count;
1532 i = (i + 1) % lsme->lsme_stripe_count, j++) {
1533 if ((lov_stripe_intersects(lsm, index, i, ext,
1534 &obd_start, &obd_end)) == 0)
1538 last_stripe = (start_stripe + j - 1) % lsme->lsme_stripe_count;
1545 * Set fe_device and copy extents from local buffer into main return buffer.
1547 * \param fiemap [out] fiemap to hold all extents
1548 * \param lcl_fm_ext [in] array of fiemap extents get from OSC layer
1549 * \param ost_index [in] OST index to be written into the fm_device
1550 * field for each extent
1551 * \param ext_count [in] number of extents to be copied
1552 * \param current_extent [in] where to start copying in the extent array
1554 static void fiemap_prepare_and_copy_exts(struct fiemap *fiemap,
1555 struct fiemap_extent *lcl_fm_ext,
1556 int ost_index, unsigned int ext_count,
1562 for (ext = 0; ext < ext_count; ext++) {
1563 lcl_fm_ext[ext].fe_device = ost_index;
1564 lcl_fm_ext[ext].fe_flags |= FIEMAP_EXTENT_NET;
1567 /* Copy fm_extent's from fm_local to return buffer */
1568 to = (char *)fiemap + fiemap_count_to_size(current_extent);
1569 memcpy(to, lcl_fm_ext, ext_count * sizeof(struct fiemap_extent));
1572 #define FIEMAP_BUFFER_SIZE 4096
1575 * Non-zero fe_logical indicates that this is a continuation FIEMAP
1576 * call. The local end offset and the device are sent in the first
1577 * fm_extent. This function calculates the stripe number from the index.
1578 * This function returns a stripe_no on which mapping is to be restarted.
1580 * This function returns fm_end_offset which is the in-OST offset at which
1581 * mapping should be restarted. If fm_end_offset=0 is returned then caller
1582 * will re-calculate proper offset in next stripe.
1583 * Note that the first extent is passed to lov_get_info via the value field.
1585 * \param fiemap [in] fiemap request header
1586 * \param lsm [in] striping information for the file
1587 * \param index [in] stripe component index
1588 * \param ext [in] logical extent of mapping
1589 * \param start_stripe [out] starting stripe will be returned in this
1591 static u64 fiemap_calc_fm_end_offset(struct fiemap *fiemap,
1592 struct lov_stripe_md *lsm,
1593 int index, struct lu_extent *ext,
1596 struct lov_stripe_md_entry *lsme = lsm->lsm_entries[index];
1597 u64 local_end = fiemap->fm_extents[0].fe_logical;
1604 if (fiemap->fm_extent_count == 0 ||
1605 fiemap->fm_extents[0].fe_logical == 0)
1608 /* Find out stripe_no from ost_index saved in the fe_device */
1609 for (i = 0; i < lsme->lsme_stripe_count; i++) {
1610 struct lov_oinfo *oinfo = lsme->lsme_oinfo[i];
1612 if (lov_oinfo_is_dummy(oinfo))
1615 if (oinfo->loi_ost_idx == fiemap->fm_extents[0].fe_device) {
1621 if (stripe_no == -1)
1624 /* If we have finished mapping on previous device, shift logical
1625 * offset to start of next device */
1626 if (lov_stripe_intersects(lsm, index, stripe_no, ext,
1627 &lun_start, &lun_end) != 0 &&
1628 local_end < lun_end) {
1629 fm_end_offset = local_end;
1630 *start_stripe = stripe_no;
1632 /* This is a special value to indicate that caller should
1633 * calculate offset in next stripe. */
1635 *start_stripe = (stripe_no + 1) % lsme->lsme_stripe_count;
1638 return fm_end_offset;
1641 struct fiemap_state {
1642 struct fiemap *fs_fm;
1643 struct lu_extent fs_ext;
1648 int fs_start_stripe;
1650 bool fs_device_done;
1651 bool fs_finish_stripe;
1655 static struct cl_object *lov_find_subobj(const struct lu_env *env,
1656 struct lov_object *lov,
1657 struct lov_stripe_md *lsm,
1660 struct lov_device *dev = lu2lov_dev(lov2lu(lov)->lo_dev);
1661 struct lov_thread_info *lti = lov_env_info(env);
1662 struct lu_fid *ofid = <i->lti_fid;
1663 struct lov_oinfo *oinfo;
1664 struct cl_device *subdev;
1665 int entry = lov_comp_entry(index);
1666 int stripe = lov_comp_stripe(index);
1669 struct cl_object *result;
1671 if (lov->lo_type != LLT_COMP)
1672 GOTO(out, result = NULL);
1674 if (entry >= lsm->lsm_entry_count ||
1675 stripe >= lsm->lsm_entries[entry]->lsme_stripe_count)
1676 GOTO(out, result = NULL);
1678 oinfo = lsm->lsm_entries[entry]->lsme_oinfo[stripe];
1679 ost_idx = oinfo->loi_ost_idx;
1680 rc = ostid_to_fid(ofid, &oinfo->loi_oi, ost_idx);
1682 GOTO(out, result = NULL);
1684 subdev = lovsub2cl_dev(dev->ld_target[ost_idx]);
1685 result = lov_sub_find(env, subdev, ofid, NULL);
1688 result = ERR_PTR(-EINVAL);
1692 int fiemap_for_stripe(const struct lu_env *env, struct cl_object *obj,
1693 struct lov_stripe_md *lsm, struct fiemap *fiemap,
1694 size_t *buflen, struct ll_fiemap_info_key *fmkey,
1695 int index, int stripeno, struct fiemap_state *fs)
1697 struct lov_stripe_md_entry *lsme = lsm->lsm_entries[index];
1698 struct cl_object *subobj;
1699 struct lov_obd *lov = lu2lov_dev(obj->co_lu.lo_dev)->ld_lov;
1700 struct fiemap_extent *fm_ext = &fs->fs_fm->fm_extents[0];
1701 u64 req_fm_len; /* Stores length of required mapping */
1702 u64 len_mapped_single_call;
1706 unsigned int ext_count;
1707 /* EOF for object */
1708 bool ost_eof = false;
1709 /* done with required mapping for this OST? */
1710 bool ost_done = false;
1714 fs->fs_device_done = false;
1715 /* Find out range of mapping on this stripe */
1716 if ((lov_stripe_intersects(lsm, index, stripeno, &fs->fs_ext,
1717 &lun_start, &obd_object_end)) == 0)
1720 if (lov_oinfo_is_dummy(lsme->lsme_oinfo[stripeno]))
1723 /* If this is a continuation FIEMAP call and we are on
1724 * starting stripe then lun_start needs to be set to
1726 if (fs->fs_end_offset != 0 && stripeno == fs->fs_start_stripe)
1727 lun_start = fs->fs_end_offset;
1728 lun_end = lov_size_to_stripe(lsm, index, fs->fs_ext.e_end, stripeno);
1729 if (lun_start == lun_end)
1732 req_fm_len = obd_object_end - lun_start + 1;
1733 fs->fs_fm->fm_length = 0;
1734 len_mapped_single_call = 0;
1736 /* find lobsub object */
1737 subobj = lov_find_subobj(env, cl2lov(obj), lsm,
1738 lov_comp_index(index, stripeno));
1740 return PTR_ERR(subobj);
1741 /* If the output buffer is very large and the objects have many
1742 * extents we may need to loop on a single OST repeatedly */
1744 if (fiemap->fm_extent_count > 0) {
1745 /* Don't get too many extents. */
1746 if (fs->fs_cur_extent + fs->fs_cnt_need >
1747 fiemap->fm_extent_count)
1748 fs->fs_cnt_need = fiemap->fm_extent_count -
1752 lun_start += len_mapped_single_call;
1753 fs->fs_fm->fm_length = req_fm_len - len_mapped_single_call;
1754 req_fm_len = fs->fs_fm->fm_length;
1756 * If we've collected enough extent map, we'd request 1 more,
1757 * to see whether we coincidentally finished all available
1758 * extent map, so that FIEMAP_EXTENT_LAST would be set.
1760 fs->fs_fm->fm_extent_count = fs->fs_enough ?
1761 1 : fs->fs_cnt_need;
1762 fs->fs_fm->fm_mapped_extents = 0;
1763 fs->fs_fm->fm_flags = fiemap->fm_flags;
1765 ost_index = lsme->lsme_oinfo[stripeno]->loi_ost_idx;
1767 if (ost_index < 0 || ost_index >= lov->desc.ld_tgt_count)
1768 GOTO(obj_put, rc = -EINVAL);
1769 /* If OST is inactive, return extent with UNKNOWN flag. */
1770 if (!lov->lov_tgts[ost_index]->ltd_active) {
1771 fs->fs_fm->fm_flags |= FIEMAP_EXTENT_LAST;
1772 fs->fs_fm->fm_mapped_extents = 1;
1774 fm_ext[0].fe_logical = lun_start;
1775 fm_ext[0].fe_length = obd_object_end - lun_start + 1;
1776 fm_ext[0].fe_flags |= FIEMAP_EXTENT_UNKNOWN;
1781 fs->fs_fm->fm_start = lun_start;
1782 fs->fs_fm->fm_flags &= ~FIEMAP_FLAG_DEVICE_ORDER;
1783 memcpy(&fmkey->lfik_fiemap, fs->fs_fm, sizeof(*fs->fs_fm));
1784 *buflen = fiemap_count_to_size(fs->fs_fm->fm_extent_count);
1786 rc = cl_object_fiemap(env, subobj, fmkey, fs->fs_fm, buflen);
1790 ext_count = fs->fs_fm->fm_mapped_extents;
1791 if (ext_count == 0) {
1793 fs->fs_device_done = true;
1794 /* If last stripe has hold at the end,
1795 * we need to return */
1796 if (stripeno == fs->fs_last_stripe) {
1797 fiemap->fm_mapped_extents = 0;
1798 fs->fs_finish_stripe = true;
1802 } else if (fs->fs_enough) {
1804 * We've collected enough extents and there are
1805 * more extents after it.
1810 /* If we just need num of extents, got to next device */
1811 if (fiemap->fm_extent_count == 0) {
1812 fs->fs_cur_extent += ext_count;
1816 /* prepare to copy retrived map extents */
1817 len_mapped_single_call = fm_ext[ext_count - 1].fe_logical +
1818 fm_ext[ext_count - 1].fe_length -
1821 /* Have we finished mapping on this device? */
1822 if (req_fm_len <= len_mapped_single_call) {
1824 fs->fs_device_done = true;
1827 /* Clear the EXTENT_LAST flag which can be present on
1828 * the last extent */
1829 if (fm_ext[ext_count - 1].fe_flags & FIEMAP_EXTENT_LAST)
1830 fm_ext[ext_count - 1].fe_flags &= ~FIEMAP_EXTENT_LAST;
1831 if (lov_stripe_size(lsm, index,
1832 fm_ext[ext_count - 1].fe_logical +
1833 fm_ext[ext_count - 1].fe_length,
1834 stripeno) >= fmkey->lfik_oa.o_size) {
1836 fs->fs_device_done = true;
1839 fiemap_prepare_and_copy_exts(fiemap, fm_ext, ost_index,
1840 ext_count, fs->fs_cur_extent);
1841 fs->fs_cur_extent += ext_count;
1843 /* Ran out of available extents? */
1844 if (fs->fs_cur_extent >= fiemap->fm_extent_count)
1845 fs->fs_enough = true;
1846 } while (!ost_done && !ost_eof);
1848 if (stripeno == fs->fs_last_stripe)
1849 fs->fs_finish_stripe = true;
1851 cl_object_put(env, subobj);
1857 * Break down the FIEMAP request and send appropriate calls to individual OSTs.
1858 * This also handles the restarting of FIEMAP calls in case mapping overflows
1859 * the available number of extents in single call.
1861 * \param env [in] lustre environment
1862 * \param obj [in] file object
1863 * \param fmkey [in] fiemap request header and other info
1864 * \param fiemap [out] fiemap buffer holding retrived map extents
1865 * \param buflen [in/out] max buffer length of @fiemap, when iterate
1866 * each OST, it is used to limit max map needed
1870 static int lov_object_fiemap(const struct lu_env *env, struct cl_object *obj,
1871 struct ll_fiemap_info_key *fmkey,
1872 struct fiemap *fiemap, size_t *buflen)
1874 struct lov_stripe_md_entry *lsme;
1875 struct lov_stripe_md *lsm;
1876 struct fiemap *fm_local = NULL;
1884 unsigned int buffer_size = FIEMAP_BUFFER_SIZE;
1886 struct fiemap_state fs = { 0 };
1889 lsm = lov_lsm_addref(cl2lov(obj));
1891 /* no extent: there is no object for mapping */
1892 fiemap->fm_mapped_extents = 0;
1896 if (!(fiemap->fm_flags & FIEMAP_FLAG_DEVICE_ORDER)) {
1898 * If the entry count > 1 or stripe_count > 1 and the
1899 * application does not understand DEVICE_ORDER flag,
1900 * it cannot interpret the extents correctly.
1902 if (lsm->lsm_entry_count > 1 ||
1903 (lsm->lsm_entry_count == 1 &&
1904 lsm->lsm_entries[0]->lsme_stripe_count > 1))
1905 GOTO(out_lsm, rc = -ENOTSUPP);
1908 /* No support for DOM layout yet. */
1909 if (lsme_is_dom(lsm->lsm_entries[0]))
1910 GOTO(out_lsm, rc = -ENOTSUPP);
1912 if (lsm->lsm_is_released) {
1913 if (fiemap->fm_start < fmkey->lfik_oa.o_size) {
1915 * released file, return a minimal FIEMAP if
1916 * request fits in file-size.
1918 fiemap->fm_mapped_extents = 1;
1919 fiemap->fm_extents[0].fe_logical = fiemap->fm_start;
1920 if (fiemap->fm_start + fiemap->fm_length <
1921 fmkey->lfik_oa.o_size)
1922 fiemap->fm_extents[0].fe_length =
1925 fiemap->fm_extents[0].fe_length =
1926 fmkey->lfik_oa.o_size -
1928 fiemap->fm_extents[0].fe_flags |=
1929 FIEMAP_EXTENT_UNKNOWN | FIEMAP_EXTENT_LAST;
1931 GOTO(out_lsm, rc = 0);
1934 /* buffer_size is small to hold fm_extent_count of extents. */
1935 if (fiemap_count_to_size(fiemap->fm_extent_count) < buffer_size)
1936 buffer_size = fiemap_count_to_size(fiemap->fm_extent_count);
1938 OBD_ALLOC_LARGE(fm_local, buffer_size);
1939 if (fm_local == NULL)
1940 GOTO(out_lsm, rc = -ENOMEM);
1943 * Requested extent count exceeds the fiemap buffer size, shrink our
1946 if (fiemap_count_to_size(fiemap->fm_extent_count) > *buflen)
1947 fiemap->fm_extent_count = fiemap_size_to_count(*buflen);
1948 if (fiemap->fm_extent_count == 0)
1951 fs.fs_enough = false;
1952 fs.fs_cur_extent = 0;
1953 fs.fs_fm = fm_local;
1954 fs.fs_cnt_need = fiemap_size_to_count(buffer_size);
1956 whole_start = fiemap->fm_start;
1957 /* whole_start is beyond the end of the file */
1958 if (whole_start > fmkey->lfik_oa.o_size)
1959 GOTO(out_fm_local, rc = -EINVAL);
1960 whole_end = (fiemap->fm_length == OBD_OBJECT_EOF) ?
1961 fmkey->lfik_oa.o_size :
1962 whole_start + fiemap->fm_length - 1;
1964 * If fiemap->fm_length != OBD_OBJECT_EOF but whole_end exceeds file
1967 if (whole_end > fmkey->lfik_oa.o_size)
1968 whole_end = fmkey->lfik_oa.o_size;
1970 start_entry = lov_lsm_entry(lsm, whole_start);
1971 end_entry = lov_lsm_entry(lsm, whole_end);
1972 if (end_entry == -1)
1973 end_entry = lsm->lsm_entry_count - 1;
1975 if (start_entry == -1 || end_entry == -1)
1976 GOTO(out_fm_local, rc = -EINVAL);
1978 /* TODO: rewrite it with lov_foreach_io_layout() */
1979 for (entry = start_entry; entry <= end_entry; entry++) {
1980 lsme = lsm->lsm_entries[entry];
1982 if (!lsme_inited(lsme))
1985 if (entry == start_entry)
1986 fs.fs_ext.e_start = whole_start;
1988 fs.fs_ext.e_start = lsme->lsme_extent.e_start;
1989 if (entry == end_entry)
1990 fs.fs_ext.e_end = whole_end;
1992 fs.fs_ext.e_end = lsme->lsme_extent.e_end - 1;
1993 fs.fs_length = fs.fs_ext.e_end - fs.fs_ext.e_start + 1;
1995 /* Calculate start stripe, last stripe and length of mapping */
1996 fs.fs_start_stripe = lov_stripe_number(lsm, entry,
1998 fs.fs_last_stripe = fiemap_calc_last_stripe(lsm, entry,
1999 &fs.fs_ext, fs.fs_start_stripe,
2001 fs.fs_end_offset = fiemap_calc_fm_end_offset(fiemap, lsm, entry,
2002 &fs.fs_ext, &fs.fs_start_stripe);
2003 /* Check each stripe */
2004 for (cur_stripe = fs.fs_start_stripe; stripe_count > 0;
2006 cur_stripe = (cur_stripe + 1) % lsme->lsme_stripe_count) {
2007 rc = fiemap_for_stripe(env, obj, lsm, fiemap, buflen,
2008 fmkey, entry, cur_stripe, &fs);
2010 GOTO(out_fm_local, rc);
2013 if (fs.fs_finish_stripe)
2015 } /* for each stripe */
2016 } /* for covering layout component */
2018 * We've traversed all components, set @entry to the last component
2019 * entry, it's for the last stripe check.
2023 /* Indicate that we are returning device offsets unless file just has
2025 if (lsm->lsm_entry_count > 1 ||
2026 (lsm->lsm_entry_count == 1 &&
2027 lsm->lsm_entries[0]->lsme_stripe_count > 1))
2028 fiemap->fm_flags |= FIEMAP_FLAG_DEVICE_ORDER;
2030 if (fiemap->fm_extent_count == 0)
2031 goto skip_last_device_calc;
2033 /* Check if we have reached the last stripe and whether mapping for that
2034 * stripe is done. */
2035 if ((cur_stripe == fs.fs_last_stripe) && fs.fs_device_done)
2036 fiemap->fm_extents[fs.fs_cur_extent - 1].fe_flags |=
2038 skip_last_device_calc:
2039 fiemap->fm_mapped_extents = fs.fs_cur_extent;
2041 OBD_FREE_LARGE(fm_local, buffer_size);
2048 static int lov_object_getstripe(const struct lu_env *env, struct cl_object *obj,
2049 struct lov_user_md __user *lum, size_t size)
2051 struct lov_object *lov = cl2lov(obj);
2052 struct lov_stripe_md *lsm;
2056 lsm = lov_lsm_addref(lov);
2060 rc = lov_getstripe(env, cl2lov(obj), lsm, lum, size);
2065 static int lov_object_layout_get(const struct lu_env *env,
2066 struct cl_object *obj,
2067 struct cl_layout *cl)
2069 struct lov_object *lov = cl2lov(obj);
2070 struct lov_stripe_md *lsm = lov_lsm_addref(lov);
2071 struct lu_buf *buf = &cl->cl_buf;
2077 cl->cl_layout_gen = CL_LAYOUT_GEN_EMPTY;
2082 cl->cl_size = lov_comp_md_size(lsm);
2083 cl->cl_layout_gen = lsm->lsm_layout_gen;
2084 cl->cl_dom_comp_size = 0;
2085 cl->cl_is_released = lsm->lsm_is_released;
2086 if (lsm_is_composite(lsm->lsm_magic)) {
2087 struct lov_stripe_md_entry *lsme = lsm->lsm_entries[0];
2089 cl->cl_is_composite = true;
2091 if (lsme_is_dom(lsme))
2092 cl->cl_dom_comp_size = lsme->lsme_extent.e_end;
2094 cl->cl_is_composite = false;
2097 rc = lov_lsm_pack(lsm, buf->lb_buf, buf->lb_len);
2100 RETURN(rc < 0 ? rc : 0);
2103 static loff_t lov_object_maxbytes(struct cl_object *obj)
2105 struct lov_object *lov = cl2lov(obj);
2106 struct lov_stripe_md *lsm = lov_lsm_addref(lov);
2112 maxbytes = lsm->lsm_maxbytes;
2119 static int lov_object_flush(const struct lu_env *env, struct cl_object *obj,
2120 struct ldlm_lock *lock)
2122 return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_flush, env, obj, lock);
2125 static const struct cl_object_operations lov_ops = {
2126 .coo_page_init = lov_page_init,
2127 .coo_lock_init = lov_lock_init,
2128 .coo_io_init = lov_io_init,
2129 .coo_attr_get = lov_attr_get,
2130 .coo_attr_update = lov_attr_update,
2131 .coo_conf_set = lov_conf_set,
2132 .coo_getstripe = lov_object_getstripe,
2133 .coo_layout_get = lov_object_layout_get,
2134 .coo_maxbytes = lov_object_maxbytes,
2135 .coo_fiemap = lov_object_fiemap,
2136 .coo_object_flush = lov_object_flush
2139 static const struct lu_object_operations lov_lu_obj_ops = {
2140 .loo_object_init = lov_object_init,
2141 .loo_object_delete = lov_object_delete,
2142 .loo_object_release = NULL,
2143 .loo_object_free = lov_object_free,
2144 .loo_object_print = lov_object_print,
2145 .loo_object_invariant = NULL
2148 struct lu_object *lov_object_alloc(const struct lu_env *env,
2149 const struct lu_object_header *unused,
2150 struct lu_device *dev)
2152 struct lov_object *lov;
2153 struct lu_object *obj;
2156 OBD_SLAB_ALLOC_PTR_GFP(lov, lov_object_kmem, GFP_NOFS);
2159 lu_object_init(obj, NULL, dev);
2160 lov->lo_cl.co_ops = &lov_ops;
2161 lov->lo_type = -1; /* invalid, to catch uninitialized type */
2163 * object io operation vector (cl_object::co_iop) is installed
2164 * later in lov_object_init(), as different vectors are used
2165 * for object with different layouts.
2167 obj->lo_ops = &lov_lu_obj_ops;
2173 struct lov_stripe_md *lov_lsm_addref(struct lov_object *lov)
2175 struct lov_stripe_md *lsm = NULL;
2177 lov_conf_freeze(lov);
2178 if (lov->lo_lsm != NULL) {
2179 lsm = lsm_addref(lov->lo_lsm);
2180 CDEBUG(D_INODE, "lsm %p addref %d/%d by %p.\n",
2181 lsm, atomic_read(&lsm->lsm_refc),
2182 lov->lo_layout_invalid, current);
2188 int lov_read_and_clear_async_rc(struct cl_object *clob)
2190 struct lu_object *luobj;
2194 luobj = lu_object_locate(&cl_object_header(clob)->coh_lu,
2196 if (luobj != NULL) {
2197 struct lov_object *lov = lu2lov(luobj);
2199 lov_conf_freeze(lov);
2200 switch (lov->lo_type) {
2202 struct lov_stripe_md *lsm;
2206 LASSERT(lsm != NULL);
2207 for (i = 0; i < lsm->lsm_entry_count; i++) {
2208 struct lov_stripe_md_entry *lse =
2209 lsm->lsm_entries[i];
2212 if (!lsme_inited(lse))
2215 for (j = 0; j < lse->lsme_stripe_count; j++) {
2216 struct lov_oinfo *loi =
2219 if (lov_oinfo_is_dummy(loi))
2222 if (loi->loi_ar.ar_rc && !rc)
2223 rc = loi->loi_ar.ar_rc;
2224 loi->loi_ar.ar_rc = 0;
2240 EXPORT_SYMBOL(lov_read_and_clear_async_rc);