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, 2016, 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 "lov_cl_internal.h"
42 static inline struct lov_device *lov_object_dev(struct lov_object *obj)
44 return lu2lov_dev(obj->lo_cl.co_lu.lo_dev);
51 /*****************************************************************************
57 struct lov_layout_operations {
58 int (*llo_init)(const struct lu_env *env, struct lov_device *dev,
59 struct lov_object *lov, struct lov_stripe_md *lsm,
60 const struct cl_object_conf *conf,
61 union lov_layout_state *state);
62 int (*llo_delete)(const struct lu_env *env, struct lov_object *lov,
63 union lov_layout_state *state);
64 void (*llo_fini)(const struct lu_env *env, struct lov_object *lov,
65 union lov_layout_state *state);
66 int (*llo_print)(const struct lu_env *env, void *cookie,
67 lu_printer_t p, const struct lu_object *o);
68 int (*llo_page_init)(const struct lu_env *env, struct cl_object *obj,
69 struct cl_page *page, pgoff_t index);
70 int (*llo_lock_init)(const struct lu_env *env,
71 struct cl_object *obj, struct cl_lock *lock,
72 const struct cl_io *io);
73 int (*llo_io_init)(const struct lu_env *env,
74 struct cl_object *obj, struct cl_io *io);
75 int (*llo_getattr)(const struct lu_env *env, struct cl_object *obj,
76 struct cl_attr *attr);
79 static int lov_layout_wait(const struct lu_env *env, struct lov_object *lov);
81 static void lov_lsm_put(struct lov_stripe_md *lsm)
87 /*****************************************************************************
89 * Lov object layout operations.
92 static int lov_init_empty(const struct lu_env *env, struct lov_device *dev,
93 struct lov_object *lov, struct lov_stripe_md *lsm,
94 const struct cl_object_conf *conf,
95 union lov_layout_state *state)
100 static struct cl_object *lov_sub_find(const struct lu_env *env,
101 struct cl_device *dev,
102 const struct lu_fid *fid,
103 const struct cl_object_conf *conf)
108 o = lu_object_find_at(env, cl2lu_dev(dev), fid, &conf->coc_lu);
109 LASSERT(ergo(!IS_ERR(o), o->lo_dev->ld_type == &lovsub_device_type));
113 static int lov_init_sub(const struct lu_env *env, struct lov_object *lov,
114 struct cl_object *subobj, struct lov_layout_raid0 *r0,
115 struct lov_oinfo *oinfo, int idx)
117 struct cl_object_header *hdr;
118 struct cl_object_header *subhdr;
119 struct cl_object_header *parent;
120 int entry = lov_comp_entry(idx);
121 int stripe = lov_comp_stripe(idx);
124 if (OBD_FAIL_CHECK(OBD_FAIL_LOV_INIT)) {
125 /* For sanity:test_206.
126 * Do not leave the object in cache to avoid accessing
127 * freed memory. This is because osc_object is referring to
128 * lov_oinfo of lsm_stripe_data which will be freed due to
130 cl_object_kill(env, subobj);
131 cl_object_put(env, subobj);
135 hdr = cl_object_header(lov2cl(lov));
136 subhdr = cl_object_header(subobj);
138 CDEBUG(D_INODE, DFID"@%p[%d:%d] -> "DFID"@%p: ostid: "DOSTID
139 " ost idx: %d gen: %d\n",
140 PFID(lu_object_fid(&subobj->co_lu)), subhdr, entry, stripe,
141 PFID(lu_object_fid(lov2lu(lov))), hdr, POSTID(&oinfo->loi_oi),
142 oinfo->loi_ost_idx, oinfo->loi_ost_gen);
144 /* reuse ->coh_attr_guard to protect coh_parent change */
145 spin_lock(&subhdr->coh_attr_guard);
146 parent = subhdr->coh_parent;
147 if (parent == NULL) {
148 subhdr->coh_parent = hdr;
149 spin_unlock(&subhdr->coh_attr_guard);
150 subhdr->coh_nesting = hdr->coh_nesting + 1;
151 lu_object_ref_add(&subobj->co_lu, "lov-parent", lov);
152 r0->lo_sub[stripe] = cl2lovsub(subobj);
153 r0->lo_sub[stripe]->lso_super = lov;
154 r0->lo_sub[stripe]->lso_index = idx;
157 struct lu_object *old_obj;
158 struct lov_object *old_lov;
159 unsigned int mask = D_INODE;
161 spin_unlock(&subhdr->coh_attr_guard);
162 old_obj = lu_object_locate(&parent->coh_lu, &lov_device_type);
163 LASSERT(old_obj != NULL);
164 old_lov = cl2lov(lu2cl(old_obj));
165 if (old_lov->lo_layout_invalid) {
166 /* the object's layout has already changed but isn't
168 lu_object_unhash(env, &subobj->co_lu);
175 LU_OBJECT_DEBUG(mask, env, &subobj->co_lu,
176 "stripe %d is already owned.", idx);
177 LU_OBJECT_DEBUG(mask, env, old_obj, "owned.");
178 LU_OBJECT_HEADER(mask, env, lov2lu(lov), "try to own.\n");
179 cl_object_put(env, subobj);
184 static int lov_page_slice_fixup(struct lov_object *lov,
185 struct cl_object *stripe)
187 struct cl_object_header *hdr = cl_object_header(&lov->lo_cl);
191 return hdr->coh_page_bufsize - lov->lo_cl.co_slice_off -
192 cfs_size_round(sizeof(struct lov_page));
194 cl_object_for_each(o, stripe)
195 o->co_slice_off += hdr->coh_page_bufsize;
197 return cl_object_header(stripe)->coh_page_bufsize;
200 static int lov_init_raid0(const struct lu_env *env, struct lov_device *dev,
201 struct lov_object *lov, int index,
202 struct lov_layout_raid0 *r0)
204 struct lov_thread_info *lti = lov_env_info(env);
205 struct cl_object_conf *subconf = <i->lti_stripe_conf;
206 struct lu_fid *ofid = <i->lti_fid;
207 struct cl_object *stripe;
208 struct lov_stripe_md_entry *lse = lov_lse(lov, index);
215 spin_lock_init(&r0->lo_sub_lock);
216 r0->lo_nr = lse->lsme_stripe_count;
217 LASSERT(r0->lo_nr <= lov_targets_nr(dev));
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, r0, oinfo,
259 lov_comp_index(index, i));
260 if (result == -EAGAIN) { /* try again */
267 int sz = lov_page_slice_fixup(lov, stripe);
268 LASSERT(ergo(psz > 0, psz == sz));
278 static int lov_init_composite(const struct lu_env *env, struct lov_device *dev,
279 struct lov_object *lov, struct lov_stripe_md *lsm,
280 const struct cl_object_conf *conf,
281 union lov_layout_state *state)
283 struct lov_layout_composite *comp = &state->composite;
284 unsigned int entry_count;
285 unsigned int psz = 0;
291 LASSERT(lsm->lsm_entry_count > 0);
292 LASSERT(lov->lo_lsm == NULL);
293 lov->lo_lsm = lsm_addref(lsm);
294 lov->lo_layout_invalid = true;
296 entry_count = lsm->lsm_entry_count;
297 comp->lo_entry_count = entry_count;
299 OBD_ALLOC(comp->lo_entries, entry_count * sizeof(*comp->lo_entries));
300 if (comp->lo_entries == NULL)
303 for (i = 0; i < entry_count; i++) {
304 struct lov_layout_entry *le = &comp->lo_entries[i];
306 le->lle_extent = lsm->lsm_entries[i]->lsme_extent;
308 * If the component has not been init-ed on MDS side, for
309 * PFL layout, we'd know that the components beyond this one
310 * will be dynamically init-ed later on file write/trunc ops.
312 if (!lsm_entry_inited(lsm, i))
315 result = lov_init_raid0(env, dev, lov, i, &le->lle_raid0);
319 LASSERT(ergo(psz > 0, psz == result));
323 cl_object_header(&lov->lo_cl)->coh_page_bufsize += psz;
325 return result > 0 ? 0 : result;
328 static int lov_init_released(const struct lu_env *env,
329 struct lov_device *dev, struct lov_object *lov,
330 struct lov_stripe_md *lsm,
331 const struct cl_object_conf *conf,
332 union lov_layout_state *state)
334 LASSERT(lsm != NULL);
335 LASSERT(lsm->lsm_is_released);
336 LASSERT(lov->lo_lsm == NULL);
338 lov->lo_lsm = lsm_addref(lsm);
342 static struct cl_object *lov_find_subobj(const struct lu_env *env,
343 struct lov_object *lov,
344 struct lov_stripe_md *lsm,
347 struct lov_device *dev = lu2lov_dev(lov2lu(lov)->lo_dev);
348 struct lov_thread_info *lti = lov_env_info(env);
349 struct lu_fid *ofid = <i->lti_fid;
350 struct lov_oinfo *oinfo;
351 struct cl_device *subdev;
352 int entry = lov_comp_entry(index);
353 int stripe = lov_comp_stripe(index);
356 struct cl_object *result;
358 if (lov->lo_type != LLT_COMP)
359 GOTO(out, result = NULL);
361 if (entry >= lsm->lsm_entry_count ||
362 stripe >= lsm->lsm_entries[entry]->lsme_stripe_count)
363 GOTO(out, result = NULL);
365 oinfo = lsm->lsm_entries[entry]->lsme_oinfo[stripe];
366 ost_idx = oinfo->loi_ost_idx;
367 rc = ostid_to_fid(ofid, &oinfo->loi_oi, ost_idx);
369 GOTO(out, result = NULL);
371 subdev = lovsub2cl_dev(dev->ld_target[ost_idx]);
372 result = lov_sub_find(env, subdev, ofid, NULL);
375 result = ERR_PTR(-EINVAL);
379 static int lov_delete_empty(const struct lu_env *env, struct lov_object *lov,
380 union lov_layout_state *state)
382 LASSERT(lov->lo_type == LLT_EMPTY || lov->lo_type == LLT_RELEASED);
384 lov_layout_wait(env, lov);
388 static void lov_subobject_kill(const struct lu_env *env, struct lov_object *lov,
389 struct lov_layout_raid0 *r0,
390 struct lovsub_object *los, int idx)
392 struct cl_object *sub;
393 struct lu_site *site;
394 struct lu_site_bkt_data *bkt;
395 wait_queue_t *waiter;
397 LASSERT(r0->lo_sub[idx] == los);
399 sub = lovsub2cl(los);
400 site = sub->co_lu.lo_dev->ld_site;
401 bkt = lu_site_bkt_from_fid(site, &sub->co_lu.lo_header->loh_fid);
403 cl_object_kill(env, sub);
404 /* release a reference to the sub-object and ... */
405 lu_object_ref_del(&sub->co_lu, "lov-parent", lov);
406 cl_object_put(env, sub);
408 /* ... wait until it is actually destroyed---sub-object clears its
409 * ->lo_sub[] slot in lovsub_object_fini() */
410 if (r0->lo_sub[idx] == los) {
411 waiter = &lov_env_info(env)->lti_waiter;
412 init_waitqueue_entry(waiter, current);
413 add_wait_queue(&bkt->lsb_marche_funebre, waiter);
414 set_current_state(TASK_UNINTERRUPTIBLE);
416 /* this wait-queue is signaled at the end of
417 * lu_object_free(). */
418 set_current_state(TASK_UNINTERRUPTIBLE);
419 spin_lock(&r0->lo_sub_lock);
420 if (r0->lo_sub[idx] == los) {
421 spin_unlock(&r0->lo_sub_lock);
424 spin_unlock(&r0->lo_sub_lock);
425 set_current_state(TASK_RUNNING);
429 remove_wait_queue(&bkt->lsb_marche_funebre, waiter);
431 LASSERT(r0->lo_sub[idx] == NULL);
434 static void lov_delete_raid0(const struct lu_env *env, struct lov_object *lov,
435 struct lov_layout_raid0 *r0)
439 if (r0->lo_sub != NULL) {
442 for (i = 0; i < r0->lo_nr; ++i) {
443 struct lovsub_object *los = r0->lo_sub[i];
446 cl_object_prune(env, &los->lso_cl);
448 * If top-level object is to be evicted from
449 * the cache, so are its sub-objects.
451 lov_subobject_kill(env, lov, r0, los, i);
459 static int lov_delete_composite(const struct lu_env *env,
460 struct lov_object *lov,
461 union lov_layout_state *state)
463 struct lov_layout_entry *entry;
464 struct lov_layout_composite *comp = &state->composite;
468 dump_lsm(D_INODE, lov->lo_lsm);
470 lov_layout_wait(env, lov);
471 if (comp->lo_entries)
472 lov_foreach_layout_entry(lov, entry)
473 lov_delete_raid0(env, lov, &entry->lle_raid0);
478 static void lov_fini_empty(const struct lu_env *env, struct lov_object *lov,
479 union lov_layout_state *state)
481 LASSERT(lov->lo_type == LLT_EMPTY || lov->lo_type == LLT_RELEASED);
484 static void lov_fini_raid0(const struct lu_env *env,
485 struct lov_layout_raid0 *r0)
487 if (r0->lo_sub != NULL) {
488 OBD_FREE_LARGE(r0->lo_sub, r0->lo_nr * sizeof r0->lo_sub[0]);
493 static void lov_fini_composite(const struct lu_env *env,
494 struct lov_object *lov,
495 union lov_layout_state *state)
497 struct lov_layout_composite *comp = &state->composite;
500 if (comp->lo_entries != NULL) {
501 struct lov_layout_entry *entry;
503 lov_foreach_layout_entry(lov, entry)
504 lov_fini_raid0(env, &entry->lle_raid0);
506 OBD_FREE(comp->lo_entries,
507 comp->lo_entry_count * sizeof(*comp->lo_entries));
508 comp->lo_entries = NULL;
511 dump_lsm(D_INODE, lov->lo_lsm);
512 lov_free_memmd(&lov->lo_lsm);
517 static void lov_fini_released(const struct lu_env *env, struct lov_object *lov,
518 union lov_layout_state *state)
521 dump_lsm(D_INODE, lov->lo_lsm);
522 lov_free_memmd(&lov->lo_lsm);
526 static int lov_print_empty(const struct lu_env *env, void *cookie,
527 lu_printer_t p, const struct lu_object *o)
529 (*p)(env, cookie, "empty %d\n", lu2lov(o)->lo_layout_invalid);
533 static int lov_print_raid0(const struct lu_env *env, void *cookie,
534 lu_printer_t p, struct lov_layout_raid0 *r0)
538 for (i = 0; i < r0->lo_nr; ++i) {
539 struct lu_object *sub;
541 if (r0->lo_sub[i] != NULL) {
542 sub = lovsub2lu(r0->lo_sub[i]);
543 lu_object_print(env, cookie, p, sub);
545 (*p)(env, cookie, "sub %d absent\n", i);
551 static int lov_print_composite(const struct lu_env *env, void *cookie,
552 lu_printer_t p, const struct lu_object *o)
554 struct lov_object *lov = lu2lov(o);
555 struct lov_stripe_md *lsm = lov->lo_lsm;
558 (*p)(env, cookie, "entries: %d, %s, lsm{%p 0x%08X %d %u}:\n",
559 lsm->lsm_entry_count,
560 lov->lo_layout_invalid ? "invalid" : "valid", lsm,
561 lsm->lsm_magic, atomic_read(&lsm->lsm_refc),
562 lsm->lsm_layout_gen);
564 for (i = 0; i < lsm->lsm_entry_count; i++) {
565 struct lov_stripe_md_entry *lse = lsm->lsm_entries[i];
567 (*p)(env, cookie, DEXT ": { 0x%08X, %u, %u, %#x, %u, %u }\n",
568 PEXT(&lse->lsme_extent), lse->lsme_magic,
569 lse->lsme_id, lse->lsme_layout_gen, lse->lsme_flags,
570 lse->lsme_stripe_count, lse->lsme_stripe_size);
571 lov_print_raid0(env, cookie, p, lov_r0(lov, i));
577 static int lov_print_released(const struct lu_env *env, void *cookie,
578 lu_printer_t p, const struct lu_object *o)
580 struct lov_object *lov = lu2lov(o);
581 struct lov_stripe_md *lsm = lov->lo_lsm;
584 "released: %s, lsm{%p 0x%08X %d %u}:\n",
585 lov->lo_layout_invalid ? "invalid" : "valid", lsm,
586 lsm->lsm_magic, atomic_read(&lsm->lsm_refc),
587 lsm->lsm_layout_gen);
592 * Implements cl_object_operations::coo_attr_get() method for an object
593 * without stripes (LLT_EMPTY layout type).
595 * The only attributes this layer is authoritative in this case is
596 * cl_attr::cat_blocks---it's 0.
598 static int lov_attr_get_empty(const struct lu_env *env, struct cl_object *obj,
599 struct cl_attr *attr)
601 attr->cat_blocks = 0;
605 static int lov_attr_get_raid0(const struct lu_env *env, struct lov_object *lov,
606 unsigned int index, struct lov_layout_raid0 *r0)
609 struct lov_stripe_md *lsm = lov->lo_lsm;
610 struct ost_lvb *lvb = &lov_env_info(env)->lti_lvb;
611 struct cl_attr *attr = &r0->lo_attr;
615 if (r0->lo_attr_valid)
618 memset(lvb, 0, sizeof(*lvb));
620 /* XXX: timestamps can be negative by sanity:test_39m,
622 lvb->lvb_atime = LLONG_MIN;
623 lvb->lvb_ctime = LLONG_MIN;
624 lvb->lvb_mtime = LLONG_MIN;
627 * XXX that should be replaced with a loop over sub-objects,
628 * doing cl_object_attr_get() on them. But for now, let's
629 * reuse old lov code.
633 * XXX take lsm spin-lock to keep lov_merge_lvb_kms()
634 * happy. It's not needed, because new code uses
635 * ->coh_attr_guard spin-lock to protect consistency of
636 * sub-object attributes.
638 lov_stripe_lock(lsm);
639 result = lov_merge_lvb_kms(lsm, index, lvb, &kms);
640 lov_stripe_unlock(lsm);
642 cl_lvb2attr(attr, lvb);
644 r0->lo_attr_valid = 1;
650 static int lov_attr_get_composite(const struct lu_env *env,
651 struct cl_object *obj,
652 struct cl_attr *attr)
654 struct lov_object *lov = cl2lov(obj);
655 struct lov_layout_entry *entry;
662 attr->cat_blocks = 0;
663 lov_foreach_layout_entry(lov, entry) {
664 struct lov_layout_raid0 *r0 = &entry->lle_raid0;
665 struct cl_attr *lov_attr = &r0->lo_attr;
667 /* PFL: This component has not been init-ed. */
668 if (!lsm_entry_inited(lov->lo_lsm, index))
671 result = lov_attr_get_raid0(env, lov, index, r0);
678 attr->cat_blocks += lov_attr->cat_blocks;
679 if (attr->cat_size < lov_attr->cat_size)
680 attr->cat_size = lov_attr->cat_size;
681 if (attr->cat_kms < lov_attr->cat_kms)
682 attr->cat_kms = lov_attr->cat_kms;
683 if (attr->cat_atime < lov_attr->cat_atime)
684 attr->cat_atime = lov_attr->cat_atime;
685 if (attr->cat_ctime < lov_attr->cat_ctime)
686 attr->cat_ctime = lov_attr->cat_ctime;
687 if (attr->cat_mtime < lov_attr->cat_mtime)
688 attr->cat_mtime = lov_attr->cat_mtime;
693 const static struct lov_layout_operations lov_dispatch[] = {
695 .llo_init = lov_init_empty,
696 .llo_delete = lov_delete_empty,
697 .llo_fini = lov_fini_empty,
698 .llo_print = lov_print_empty,
699 .llo_page_init = lov_page_init_empty,
700 .llo_lock_init = lov_lock_init_empty,
701 .llo_io_init = lov_io_init_empty,
702 .llo_getattr = lov_attr_get_empty,
705 .llo_init = lov_init_released,
706 .llo_delete = lov_delete_empty,
707 .llo_fini = lov_fini_released,
708 .llo_print = lov_print_released,
709 .llo_page_init = lov_page_init_empty,
710 .llo_lock_init = lov_lock_init_empty,
711 .llo_io_init = lov_io_init_released,
712 .llo_getattr = lov_attr_get_empty,
715 .llo_init = lov_init_composite,
716 .llo_delete = lov_delete_composite,
717 .llo_fini = lov_fini_composite,
718 .llo_print = lov_print_composite,
719 .llo_page_init = lov_page_init_composite,
720 .llo_lock_init = lov_lock_init_composite,
721 .llo_io_init = lov_io_init_composite,
722 .llo_getattr = lov_attr_get_composite,
727 * Performs a double-dispatch based on the layout type of an object.
729 #define LOV_2DISPATCH_NOLOCK(obj, op, ...) \
731 struct lov_object *__obj = (obj); \
732 enum lov_layout_type __llt; \
734 __llt = __obj->lo_type; \
735 LASSERT(__llt < ARRAY_SIZE(lov_dispatch)); \
736 lov_dispatch[__llt].op(__VA_ARGS__); \
740 * Return lov_layout_type associated with a given lsm
742 static enum lov_layout_type lov_type(struct lov_stripe_md *lsm)
747 if (lsm->lsm_is_released)
750 if (lsm->lsm_magic == LOV_MAGIC_V1 ||
751 lsm->lsm_magic == LOV_MAGIC_V3 ||
752 lsm->lsm_magic == LOV_MAGIC_COMP_V1)
758 static inline void lov_conf_freeze(struct lov_object *lov)
760 CDEBUG(D_INODE, "To take share lov(%p) owner %p/%p\n",
761 lov, lov->lo_owner, current);
762 if (lov->lo_owner != current)
763 down_read(&lov->lo_type_guard);
766 static inline void lov_conf_thaw(struct lov_object *lov)
768 CDEBUG(D_INODE, "To release share lov(%p) owner %p/%p\n",
769 lov, lov->lo_owner, current);
770 if (lov->lo_owner != current)
771 up_read(&lov->lo_type_guard);
774 #define LOV_2DISPATCH_MAYLOCK(obj, op, lock, ...) \
776 struct lov_object *__obj = (obj); \
777 int __lock = !!(lock); \
778 typeof(lov_dispatch[0].op(__VA_ARGS__)) __result; \
781 lov_conf_freeze(__obj); \
782 __result = LOV_2DISPATCH_NOLOCK(obj, op, __VA_ARGS__); \
784 lov_conf_thaw(__obj); \
789 * Performs a locked double-dispatch based on the layout type of an object.
791 #define LOV_2DISPATCH(obj, op, ...) \
792 LOV_2DISPATCH_MAYLOCK(obj, op, 1, __VA_ARGS__)
794 #define LOV_2DISPATCH_VOID(obj, op, ...) \
796 struct lov_object *__obj = (obj); \
797 enum lov_layout_type __llt; \
799 lov_conf_freeze(__obj); \
800 __llt = __obj->lo_type; \
801 LASSERT(__llt < ARRAY_SIZE(lov_dispatch)); \
802 lov_dispatch[__llt].op(__VA_ARGS__); \
803 lov_conf_thaw(__obj); \
806 static void lov_conf_lock(struct lov_object *lov)
808 LASSERT(lov->lo_owner != current);
809 down_write(&lov->lo_type_guard);
810 LASSERT(lov->lo_owner == NULL);
811 lov->lo_owner = current;
812 CDEBUG(D_INODE, "Took exclusive lov(%p) owner %p\n",
816 static void lov_conf_unlock(struct lov_object *lov)
818 CDEBUG(D_INODE, "To release exclusive lov(%p) owner %p\n",
820 lov->lo_owner = NULL;
821 up_write(&lov->lo_type_guard);
824 static int lov_layout_wait(const struct lu_env *env, struct lov_object *lov)
826 struct l_wait_info lwi = { 0 };
829 while (atomic_read(&lov->lo_active_ios) > 0) {
830 CDEBUG(D_INODE, "file:"DFID" wait for active IO, now: %d.\n",
831 PFID(lu_object_fid(lov2lu(lov))),
832 atomic_read(&lov->lo_active_ios));
834 l_wait_event(lov->lo_waitq,
835 atomic_read(&lov->lo_active_ios) == 0, &lwi);
840 static int lov_layout_change(const struct lu_env *unused,
841 struct lov_object *lov, struct lov_stripe_md *lsm,
842 const struct cl_object_conf *conf)
844 enum lov_layout_type llt = lov_type(lsm);
845 union lov_layout_state *state = &lov->u;
846 const struct lov_layout_operations *old_ops;
847 const struct lov_layout_operations *new_ops;
848 struct lov_device *lov_dev = lov_object_dev(lov);
854 LASSERT(lov->lo_type < ARRAY_SIZE(lov_dispatch));
856 env = cl_env_get(&refcheck);
858 RETURN(PTR_ERR(env));
860 LASSERT(llt < ARRAY_SIZE(lov_dispatch));
862 CDEBUG(D_INODE, DFID" from %s to %s\n",
863 PFID(lu_object_fid(lov2lu(lov))),
864 llt2str(lov->lo_type), llt2str(llt));
866 old_ops = &lov_dispatch[lov->lo_type];
867 new_ops = &lov_dispatch[llt];
869 rc = cl_object_prune(env, &lov->lo_cl);
873 rc = old_ops->llo_delete(env, lov, &lov->u);
877 old_ops->llo_fini(env, lov, &lov->u);
879 LASSERT(atomic_read(&lov->lo_active_ios) == 0);
881 CDEBUG(D_INODE, DFID "Apply new layout lov %p, type %d\n",
882 PFID(lu_object_fid(lov2lu(lov))), lov, llt);
884 lov->lo_type = LLT_EMPTY;
886 /* page bufsize fixup */
887 cl_object_header(&lov->lo_cl)->coh_page_bufsize -=
888 lov_page_slice_fixup(lov, NULL);
890 rc = new_ops->llo_init(env, lov_dev, lov, lsm, conf, state);
892 struct obd_device *obd = lov2obd(lov_dev->ld_lov);
894 CERROR("%s: cannot apply new layout on "DFID" : rc = %d\n",
895 obd->obd_name, PFID(lu_object_fid(lov2lu(lov))), rc);
896 new_ops->llo_delete(env, lov, state);
897 new_ops->llo_fini(env, lov, state);
898 /* this file becomes an EMPTY file. */
905 cl_env_put(env, &refcheck);
909 /*****************************************************************************
911 * Lov object operations.
914 int lov_object_init(const struct lu_env *env, struct lu_object *obj,
915 const struct lu_object_conf *conf)
917 struct lov_object *lov = lu2lov(obj);
918 struct lov_device *dev = lov_object_dev(lov);
919 const struct cl_object_conf *cconf = lu2cl_conf(conf);
920 union lov_layout_state *set = &lov->u;
921 const struct lov_layout_operations *ops;
922 struct lov_stripe_md *lsm = NULL;
926 init_rwsem(&lov->lo_type_guard);
927 atomic_set(&lov->lo_active_ios, 0);
928 init_waitqueue_head(&lov->lo_waitq);
929 cl_object_page_init(lu2cl(obj), sizeof(struct lov_page));
931 lov->lo_type = LLT_EMPTY;
932 if (cconf->u.coc_layout.lb_buf != NULL) {
933 lsm = lov_unpackmd(dev->ld_lov,
934 cconf->u.coc_layout.lb_buf,
935 cconf->u.coc_layout.lb_len);
937 RETURN(PTR_ERR(lsm));
939 dump_lsm(D_INODE, lsm);
942 /* no locking is necessary, as object is being created */
943 lov->lo_type = lov_type(lsm);
944 ops = &lov_dispatch[lov->lo_type];
945 rc = ops->llo_init(env, dev, lov, lsm, cconf, set);
955 static int lov_conf_set(const struct lu_env *env, struct cl_object *obj,
956 const struct cl_object_conf *conf)
958 struct lov_stripe_md *lsm = NULL;
959 struct lov_object *lov = cl2lov(obj);
963 if (conf->coc_opc == OBJECT_CONF_SET &&
964 conf->u.coc_layout.lb_buf != NULL) {
965 lsm = lov_unpackmd(lov_object_dev(lov)->ld_lov,
966 conf->u.coc_layout.lb_buf,
967 conf->u.coc_layout.lb_len);
969 RETURN(PTR_ERR(lsm));
970 dump_lsm(D_INODE, lsm);
974 if (conf->coc_opc == OBJECT_CONF_INVALIDATE) {
975 lov->lo_layout_invalid = true;
976 GOTO(out, result = 0);
979 if (conf->coc_opc == OBJECT_CONF_WAIT) {
980 if (lov->lo_layout_invalid &&
981 atomic_read(&lov->lo_active_ios) > 0) {
982 lov_conf_unlock(lov);
983 result = lov_layout_wait(env, lov);
989 LASSERT(conf->coc_opc == OBJECT_CONF_SET);
991 if ((lsm == NULL && lov->lo_lsm == NULL) ||
992 ((lsm != NULL && lov->lo_lsm != NULL) &&
993 (lov->lo_lsm->lsm_layout_gen == lsm->lsm_layout_gen) &&
994 (lov->lo_lsm->lsm_entries[0]->lsme_pattern ==
995 lsm->lsm_entries[0]->lsme_pattern))) {
996 /* same version of layout */
997 lov->lo_layout_invalid = false;
998 GOTO(out, result = 0);
1001 /* will change layout - check if there still exists active IO. */
1002 if (atomic_read(&lov->lo_active_ios) > 0) {
1003 lov->lo_layout_invalid = true;
1004 GOTO(out, result = -EBUSY);
1007 result = lov_layout_change(env, lov, lsm, conf);
1008 lov->lo_layout_invalid = result != 0;
1012 lov_conf_unlock(lov);
1014 CDEBUG(D_INODE, DFID" lo_layout_invalid=%d\n",
1015 PFID(lu_object_fid(lov2lu(lov))), lov->lo_layout_invalid);
1019 static void lov_object_delete(const struct lu_env *env, struct lu_object *obj)
1021 struct lov_object *lov = lu2lov(obj);
1024 LOV_2DISPATCH_VOID(lov, llo_delete, env, lov, &lov->u);
1028 static void lov_object_free(const struct lu_env *env, struct lu_object *obj)
1030 struct lov_object *lov = lu2lov(obj);
1033 LOV_2DISPATCH_VOID(lov, llo_fini, env, lov, &lov->u);
1034 lu_object_fini(obj);
1035 OBD_SLAB_FREE_PTR(lov, lov_object_kmem);
1039 static int lov_object_print(const struct lu_env *env, void *cookie,
1040 lu_printer_t p, const struct lu_object *o)
1042 return LOV_2DISPATCH_NOLOCK(lu2lov(o), llo_print, env, cookie, p, o);
1045 int lov_page_init(const struct lu_env *env, struct cl_object *obj,
1046 struct cl_page *page, pgoff_t index)
1048 return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_page_init, env, obj, page,
1053 * Implements cl_object_operations::clo_io_init() method for lov
1054 * layer. Dispatches to the appropriate layout io initialization method.
1056 int lov_io_init(const struct lu_env *env, struct cl_object *obj,
1059 CL_IO_SLICE_CLEAN(lov_env_io(env), lis_cl);
1061 CDEBUG(D_INODE, DFID "io %p type %d ignore/verify layout %d/%d\n",
1062 PFID(lu_object_fid(&obj->co_lu)), io, io->ci_type,
1063 io->ci_ignore_layout, io->ci_verify_layout);
1065 /* IO type CIT_MISC with ci_ignore_layout set are usually invoked from
1066 * the OSC layer. It shouldn't take lov layout conf lock in that case,
1067 * because as long as the OSC object exists, the layout can't be
1069 return LOV_2DISPATCH_MAYLOCK(cl2lov(obj), llo_io_init,
1070 !(io->ci_ignore_layout && io->ci_type == CIT_MISC),
1075 * An implementation of cl_object_operations::clo_attr_get() method for lov
1076 * layer. For raid0 layout this collects and merges attributes of all
1079 static int lov_attr_get(const struct lu_env *env, struct cl_object *obj,
1080 struct cl_attr *attr)
1082 /* do not take lock, as this function is called under a
1083 * spin-lock. Layout is protected from changing by ongoing IO. */
1084 return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_getattr, env, obj, attr);
1087 static int lov_attr_update(const struct lu_env *env, struct cl_object *obj,
1088 const struct cl_attr *attr, unsigned valid)
1091 * No dispatch is required here, as no layout implements this.
1096 int lov_lock_init(const struct lu_env *env, struct cl_object *obj,
1097 struct cl_lock *lock, const struct cl_io *io)
1099 /* No need to lock because we've taken one refcount of layout. */
1100 return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_lock_init, env, obj, lock,
1105 * We calculate on which OST the mapping will end. If the length of mapping
1106 * is greater than (stripe_size * stripe_count) then the last_stripe will
1107 * will be one just before start_stripe. Else we check if the mapping
1108 * intersects each OST and find last_stripe.
1109 * This function returns the last_stripe and also sets the stripe_count
1110 * over which the mapping is spread
1112 * \param lsm [in] striping information for the file
1113 * \param index [in] stripe component index
1114 * \param ext [in] logical extent of mapping
1115 * \param start_stripe [in] starting stripe of the mapping
1116 * \param stripe_count [out] the number of stripes across which to map is
1119 * \retval last_stripe return the last stripe of the mapping
1121 static int fiemap_calc_last_stripe(struct lov_stripe_md *lsm, int index,
1122 struct lu_extent *ext,
1123 int start_stripe, int *stripe_count)
1125 struct lov_stripe_md_entry *lsme = lsm->lsm_entries[index];
1131 if (ext->e_end - ext->e_start >
1132 lsme->lsme_stripe_size * lsme->lsme_stripe_count) {
1133 last_stripe = (start_stripe < 1 ? lsme->lsme_stripe_count - 1 :
1135 *stripe_count = lsme->lsme_stripe_count;
1137 for (j = 0, i = start_stripe; j < lsme->lsme_stripe_count;
1138 i = (i + 1) % lsme->lsme_stripe_count, j++) {
1139 if ((lov_stripe_intersects(lsm, index, i, ext,
1140 &obd_start, &obd_end)) == 0)
1144 last_stripe = (start_stripe + j - 1) % lsme->lsme_stripe_count;
1151 * Set fe_device and copy extents from local buffer into main return buffer.
1153 * \param fiemap [out] fiemap to hold all extents
1154 * \param lcl_fm_ext [in] array of fiemap extents get from OSC layer
1155 * \param ost_index [in] OST index to be written into the fm_device
1156 * field for each extent
1157 * \param ext_count [in] number of extents to be copied
1158 * \param current_extent [in] where to start copying in the extent array
1160 static void fiemap_prepare_and_copy_exts(struct fiemap *fiemap,
1161 struct fiemap_extent *lcl_fm_ext,
1162 int ost_index, unsigned int ext_count,
1168 for (ext = 0; ext < ext_count; ext++) {
1169 lcl_fm_ext[ext].fe_device = ost_index;
1170 lcl_fm_ext[ext].fe_flags |= FIEMAP_EXTENT_NET;
1173 /* Copy fm_extent's from fm_local to return buffer */
1174 to = (char *)fiemap + fiemap_count_to_size(current_extent);
1175 memcpy(to, lcl_fm_ext, ext_count * sizeof(struct fiemap_extent));
1178 #define FIEMAP_BUFFER_SIZE 4096
1181 * Non-zero fe_logical indicates that this is a continuation FIEMAP
1182 * call. The local end offset and the device are sent in the first
1183 * fm_extent. This function calculates the stripe number from the index.
1184 * This function returns a stripe_no on which mapping is to be restarted.
1186 * This function returns fm_end_offset which is the in-OST offset at which
1187 * mapping should be restarted. If fm_end_offset=0 is returned then caller
1188 * will re-calculate proper offset in next stripe.
1189 * Note that the first extent is passed to lov_get_info via the value field.
1191 * \param fiemap [in] fiemap request header
1192 * \param lsm [in] striping information for the file
1193 * \param index [in] stripe component index
1194 * \param ext [in] logical extent of mapping
1195 * \param start_stripe [out] starting stripe will be returned in this
1197 static u64 fiemap_calc_fm_end_offset(struct fiemap *fiemap,
1198 struct lov_stripe_md *lsm,
1199 int index, struct lu_extent *ext,
1202 struct lov_stripe_md_entry *lsme = lsm->lsm_entries[index];
1203 u64 local_end = fiemap->fm_extents[0].fe_logical;
1210 if (fiemap->fm_extent_count == 0 ||
1211 fiemap->fm_extents[0].fe_logical == 0)
1214 /* Find out stripe_no from ost_index saved in the fe_device */
1215 for (i = 0; i < lsme->lsme_stripe_count; i++) {
1216 struct lov_oinfo *oinfo = lsme->lsme_oinfo[i];
1218 if (lov_oinfo_is_dummy(oinfo))
1221 if (oinfo->loi_ost_idx == fiemap->fm_extents[0].fe_device) {
1227 if (stripe_no == -1)
1230 /* If we have finished mapping on previous device, shift logical
1231 * offset to start of next device */
1232 if (lov_stripe_intersects(lsm, index, stripe_no, ext,
1233 &lun_start, &lun_end) != 0 &&
1234 local_end < lun_end) {
1235 fm_end_offset = local_end;
1236 *start_stripe = stripe_no;
1238 /* This is a special value to indicate that caller should
1239 * calculate offset in next stripe. */
1241 *start_stripe = (stripe_no + 1) % lsme->lsme_stripe_count;
1244 return fm_end_offset;
1247 struct fiemap_state {
1248 struct fiemap *fs_fm;
1249 struct lu_extent fs_ext;
1254 int fs_start_stripe;
1256 bool fs_device_done;
1257 bool fs_finish_stripe;
1261 int fiemap_for_stripe(const struct lu_env *env, struct cl_object *obj,
1262 struct lov_stripe_md *lsm, struct fiemap *fiemap,
1263 size_t *buflen, struct ll_fiemap_info_key *fmkey,
1264 int index, int stripeno, struct fiemap_state *fs)
1266 struct lov_stripe_md_entry *lsme = lsm->lsm_entries[index];
1267 struct cl_object *subobj;
1268 struct lov_obd *lov = lu2lov_dev(obj->co_lu.lo_dev)->ld_lov;
1269 struct fiemap_extent *fm_ext = &fs->fs_fm->fm_extents[0];
1270 u64 req_fm_len; /* Stores length of required mapping */
1271 u64 len_mapped_single_call;
1275 unsigned int ext_count;
1276 /* EOF for object */
1277 bool ost_eof = false;
1278 /* done with required mapping for this OST? */
1279 bool ost_done = false;
1283 fs->fs_device_done = false;
1284 /* Find out range of mapping on this stripe */
1285 if ((lov_stripe_intersects(lsm, index, stripeno, &fs->fs_ext,
1286 &lun_start, &obd_object_end)) == 0)
1289 if (lov_oinfo_is_dummy(lsme->lsme_oinfo[stripeno]))
1292 /* If this is a continuation FIEMAP call and we are on
1293 * starting stripe then lun_start needs to be set to
1295 if (fs->fs_end_offset != 0 && stripeno == fs->fs_start_stripe)
1296 lun_start = fs->fs_end_offset;
1297 lun_end = lov_size_to_stripe(lsm, index, fs->fs_ext.e_end, stripeno);
1298 if (lun_start == lun_end)
1301 req_fm_len = obd_object_end - lun_start;
1302 fs->fs_fm->fm_length = 0;
1303 len_mapped_single_call = 0;
1305 /* find lobsub object */
1306 subobj = lov_find_subobj(env, cl2lov(obj), lsm,
1307 lov_comp_index(index, stripeno));
1309 return PTR_ERR(subobj);
1310 /* If the output buffer is very large and the objects have many
1311 * extents we may need to loop on a single OST repeatedly */
1313 if (fiemap->fm_extent_count > 0) {
1314 /* Don't get too many extents. */
1315 if (fs->fs_cur_extent + fs->fs_cnt_need >
1316 fiemap->fm_extent_count)
1317 fs->fs_cnt_need = fiemap->fm_extent_count -
1321 lun_start += len_mapped_single_call;
1322 fs->fs_fm->fm_length = req_fm_len - len_mapped_single_call;
1323 req_fm_len = fs->fs_fm->fm_length;
1325 * If we've collected enough extent map, we'd request 1 more,
1326 * to see whether we coincidentally finished all available
1327 * extent map, so that FIEMAP_EXTENT_LAST would be set.
1329 fs->fs_fm->fm_extent_count = fs->fs_enough ?
1330 1 : fs->fs_cnt_need;
1331 fs->fs_fm->fm_mapped_extents = 0;
1332 fs->fs_fm->fm_flags = fiemap->fm_flags;
1334 ost_index = lsme->lsme_oinfo[stripeno]->loi_ost_idx;
1336 if (ost_index < 0 || ost_index >= lov->desc.ld_tgt_count)
1337 GOTO(obj_put, rc = -EINVAL);
1338 /* If OST is inactive, return extent with UNKNOWN flag. */
1339 if (!lov->lov_tgts[ost_index]->ltd_active) {
1340 fs->fs_fm->fm_flags |= FIEMAP_EXTENT_LAST;
1341 fs->fs_fm->fm_mapped_extents = 1;
1343 fm_ext[0].fe_logical = lun_start;
1344 fm_ext[0].fe_length = obd_object_end - lun_start;
1345 fm_ext[0].fe_flags |= FIEMAP_EXTENT_UNKNOWN;
1350 fs->fs_fm->fm_start = lun_start;
1351 fs->fs_fm->fm_flags &= ~FIEMAP_FLAG_DEVICE_ORDER;
1352 memcpy(&fmkey->lfik_fiemap, fs->fs_fm, sizeof(*fs->fs_fm));
1353 *buflen = fiemap_count_to_size(fs->fs_fm->fm_extent_count);
1355 rc = cl_object_fiemap(env, subobj, fmkey, fs->fs_fm, buflen);
1359 ext_count = fs->fs_fm->fm_mapped_extents;
1360 if (ext_count == 0) {
1362 fs->fs_device_done = true;
1363 /* If last stripe has hold at the end,
1364 * we need to return */
1365 if (stripeno == fs->fs_last_stripe) {
1366 fiemap->fm_mapped_extents = 0;
1367 fs->fs_finish_stripe = true;
1371 } else if (fs->fs_enough) {
1373 * We've collected enough extents and there are
1374 * more extents after it.
1379 /* If we just need num of extents, got to next device */
1380 if (fiemap->fm_extent_count == 0) {
1381 fs->fs_cur_extent += ext_count;
1385 /* prepare to copy retrived map extents */
1386 len_mapped_single_call = fm_ext[ext_count - 1].fe_logical +
1387 fm_ext[ext_count - 1].fe_length -
1390 /* Have we finished mapping on this device? */
1391 if (req_fm_len <= len_mapped_single_call) {
1393 fs->fs_device_done = true;
1396 /* Clear the EXTENT_LAST flag which can be present on
1397 * the last extent */
1398 if (fm_ext[ext_count - 1].fe_flags & FIEMAP_EXTENT_LAST)
1399 fm_ext[ext_count - 1].fe_flags &= ~FIEMAP_EXTENT_LAST;
1400 if (lov_stripe_size(lsm, index,
1401 fm_ext[ext_count - 1].fe_logical +
1402 fm_ext[ext_count - 1].fe_length,
1403 stripeno) >= fmkey->lfik_oa.o_size) {
1405 fs->fs_device_done = true;
1408 fiemap_prepare_and_copy_exts(fiemap, fm_ext, ost_index,
1409 ext_count, fs->fs_cur_extent);
1410 fs->fs_cur_extent += ext_count;
1412 /* Ran out of available extents? */
1413 if (fs->fs_cur_extent >= fiemap->fm_extent_count)
1414 fs->fs_enough = true;
1415 } while (!ost_done && !ost_eof);
1417 if (stripeno == fs->fs_last_stripe)
1418 fs->fs_finish_stripe = true;
1420 cl_object_put(env, subobj);
1426 * Break down the FIEMAP request and send appropriate calls to individual OSTs.
1427 * This also handles the restarting of FIEMAP calls in case mapping overflows
1428 * the available number of extents in single call.
1430 * \param env [in] lustre environment
1431 * \param obj [in] file object
1432 * \param fmkey [in] fiemap request header and other info
1433 * \param fiemap [out] fiemap buffer holding retrived map extents
1434 * \param buflen [in/out] max buffer length of @fiemap, when iterate
1435 * each OST, it is used to limit max map needed
1439 static int lov_object_fiemap(const struct lu_env *env, struct cl_object *obj,
1440 struct ll_fiemap_info_key *fmkey,
1441 struct fiemap *fiemap, size_t *buflen)
1443 struct lov_stripe_md_entry *lsme;
1444 struct lov_stripe_md *lsm;
1445 struct fiemap *fm_local = NULL;
1453 unsigned int buffer_size = FIEMAP_BUFFER_SIZE;
1455 struct fiemap_state fs = { 0 };
1458 lsm = lov_lsm_addref(cl2lov(obj));
1462 if (!(fiemap->fm_flags & FIEMAP_FLAG_DEVICE_ORDER)) {
1464 * If the entry count > 1 or stripe_count > 1 and the
1465 * application does not understand DEVICE_ORDER flag,
1466 * it cannot interpret the extents correctly.
1468 if (lsm->lsm_entry_count > 1 ||
1469 (lsm->lsm_entry_count == 1 &&
1470 lsm->lsm_entries[0]->lsme_stripe_count > 1))
1471 GOTO(out_lsm, rc = -ENOTSUPP);
1474 if (lsm->lsm_is_released) {
1475 if (fiemap->fm_start < fmkey->lfik_oa.o_size) {
1477 * released file, return a minimal FIEMAP if
1478 * request fits in file-size.
1480 fiemap->fm_mapped_extents = 1;
1481 fiemap->fm_extents[0].fe_logical = fiemap->fm_start;
1482 if (fiemap->fm_start + fiemap->fm_length <
1483 fmkey->lfik_oa.o_size)
1484 fiemap->fm_extents[0].fe_length =
1487 fiemap->fm_extents[0].fe_length =
1488 fmkey->lfik_oa.o_size -
1490 fiemap->fm_extents[0].fe_flags |=
1491 FIEMAP_EXTENT_UNKNOWN | FIEMAP_EXTENT_LAST;
1493 GOTO(out_lsm, rc = 0);
1496 /* buffer_size is small to hold fm_extent_count of extents. */
1497 if (fiemap_count_to_size(fiemap->fm_extent_count) < buffer_size)
1498 buffer_size = fiemap_count_to_size(fiemap->fm_extent_count);
1500 OBD_ALLOC_LARGE(fm_local, buffer_size);
1501 if (fm_local == NULL)
1502 GOTO(out_lsm, rc = -ENOMEM);
1505 * Requested extent count exceeds the fiemap buffer size, shrink our
1508 if (fiemap_count_to_size(fiemap->fm_extent_count) > *buflen)
1509 fiemap->fm_extent_count = fiemap_size_to_count(*buflen);
1510 if (fiemap->fm_extent_count == 0)
1513 fs.fs_enough = false;
1514 fs.fs_cur_extent = 0;
1515 fs.fs_fm = fm_local;
1516 fs.fs_cnt_need = fiemap_size_to_count(buffer_size);
1518 whole_start = fiemap->fm_start;
1519 /* whole_start is beyond the end of the file */
1520 if (whole_start > fmkey->lfik_oa.o_size)
1521 GOTO(out_fm_local, rc = -EINVAL);
1522 whole_end = (fiemap->fm_length == OBD_OBJECT_EOF) ?
1523 fmkey->lfik_oa.o_size :
1524 whole_start + fiemap->fm_length - 1;
1526 * If fiemap->fm_length != OBD_OBJECT_EOF but whole_end exceeds file
1529 if (whole_end > fmkey->lfik_oa.o_size)
1530 whole_end = fmkey->lfik_oa.o_size;
1532 start_entry = lov_lsm_entry(lsm, whole_start);
1533 end_entry = lov_lsm_entry(lsm, whole_end);
1534 if (end_entry == -1)
1535 end_entry = lsm->lsm_entry_count - 1;
1537 if (start_entry == -1 || end_entry == -1)
1538 GOTO(out_fm_local, rc = -EINVAL);
1540 for (entry = start_entry; entry <= end_entry; entry++) {
1541 lsme = lsm->lsm_entries[entry];
1543 if (!lsme_inited(lsme))
1546 if (entry == start_entry)
1547 fs.fs_ext.e_start = whole_start;
1549 fs.fs_ext.e_start = lsme->lsme_extent.e_start;
1550 if (entry == end_entry)
1551 fs.fs_ext.e_end = whole_end;
1553 fs.fs_ext.e_end = lsme->lsme_extent.e_end - 1;
1554 fs.fs_length = fs.fs_ext.e_end - fs.fs_ext.e_start + 1;
1556 /* Calculate start stripe, last stripe and length of mapping */
1557 fs.fs_start_stripe = lov_stripe_number(lsm, entry,
1559 fs.fs_last_stripe = fiemap_calc_last_stripe(lsm, entry,
1560 &fs.fs_ext, fs.fs_start_stripe,
1562 fs.fs_end_offset = fiemap_calc_fm_end_offset(fiemap, lsm, entry,
1563 &fs.fs_ext, &fs.fs_start_stripe);
1564 /* Check each stripe */
1565 for (cur_stripe = fs.fs_start_stripe; stripe_count > 0;
1567 cur_stripe = (cur_stripe + 1) % lsme->lsme_stripe_count) {
1568 rc = fiemap_for_stripe(env, obj, lsm, fiemap, buflen,
1569 fmkey, entry, cur_stripe, &fs);
1571 GOTO(out_fm_local, rc);
1574 if (fs.fs_finish_stripe)
1576 } /* for each stripe */
1577 } /* for covering layout component */
1579 * We've traversed all components, set @entry to the last component
1580 * entry, it's for the last stripe check.
1584 /* Indicate that we are returning device offsets unless file just has
1586 if (lsm->lsm_entry_count > 1 ||
1587 (lsm->lsm_entry_count == 1 &&
1588 lsm->lsm_entries[0]->lsme_stripe_count > 1))
1589 fiemap->fm_flags |= FIEMAP_FLAG_DEVICE_ORDER;
1591 if (fiemap->fm_extent_count == 0)
1592 goto skip_last_device_calc;
1594 /* Check if we have reached the last stripe and whether mapping for that
1595 * stripe is done. */
1596 if ((cur_stripe == fs.fs_last_stripe) && fs.fs_device_done)
1597 fiemap->fm_extents[fs.fs_cur_extent - 1].fe_flags |=
1599 skip_last_device_calc:
1600 fiemap->fm_mapped_extents = fs.fs_cur_extent;
1602 OBD_FREE_LARGE(fm_local, buffer_size);
1609 static int lov_object_getstripe(const struct lu_env *env, struct cl_object *obj,
1610 struct lov_user_md __user *lum, size_t size)
1612 struct lov_object *lov = cl2lov(obj);
1613 struct lov_stripe_md *lsm;
1617 lsm = lov_lsm_addref(lov);
1621 rc = lov_getstripe(env, cl2lov(obj), lsm, lum, size);
1626 static int lov_object_layout_get(const struct lu_env *env,
1627 struct cl_object *obj,
1628 struct cl_layout *cl)
1630 struct lov_object *lov = cl2lov(obj);
1631 struct lov_stripe_md *lsm = lov_lsm_addref(lov);
1632 struct lu_buf *buf = &cl->cl_buf;
1638 cl->cl_layout_gen = CL_LAYOUT_GEN_EMPTY;
1643 cl->cl_size = lov_comp_md_size(lsm);
1644 cl->cl_layout_gen = lsm->lsm_layout_gen;
1645 cl->cl_is_composite = lsm_is_composite(lsm->lsm_magic);
1647 rc = lov_lsm_pack(lsm, buf->lb_buf, buf->lb_len);
1650 RETURN(rc < 0 ? rc : 0);
1653 static loff_t lov_object_maxbytes(struct cl_object *obj)
1655 struct lov_object *lov = cl2lov(obj);
1656 struct lov_stripe_md *lsm = lov_lsm_addref(lov);
1662 maxbytes = lsm->lsm_maxbytes;
1669 static const struct cl_object_operations lov_ops = {
1670 .coo_page_init = lov_page_init,
1671 .coo_lock_init = lov_lock_init,
1672 .coo_io_init = lov_io_init,
1673 .coo_attr_get = lov_attr_get,
1674 .coo_attr_update = lov_attr_update,
1675 .coo_conf_set = lov_conf_set,
1676 .coo_getstripe = lov_object_getstripe,
1677 .coo_layout_get = lov_object_layout_get,
1678 .coo_maxbytes = lov_object_maxbytes,
1679 .coo_fiemap = lov_object_fiemap,
1682 static const struct lu_object_operations lov_lu_obj_ops = {
1683 .loo_object_init = lov_object_init,
1684 .loo_object_delete = lov_object_delete,
1685 .loo_object_release = NULL,
1686 .loo_object_free = lov_object_free,
1687 .loo_object_print = lov_object_print,
1688 .loo_object_invariant = NULL
1691 struct lu_object *lov_object_alloc(const struct lu_env *env,
1692 const struct lu_object_header *unused,
1693 struct lu_device *dev)
1695 struct lov_object *lov;
1696 struct lu_object *obj;
1699 OBD_SLAB_ALLOC_PTR_GFP(lov, lov_object_kmem, GFP_NOFS);
1702 lu_object_init(obj, NULL, dev);
1703 lov->lo_cl.co_ops = &lov_ops;
1704 lov->lo_type = -1; /* invalid, to catch uninitialized type */
1706 * object io operation vector (cl_object::co_iop) is installed
1707 * later in lov_object_init(), as different vectors are used
1708 * for object with different layouts.
1710 obj->lo_ops = &lov_lu_obj_ops;
1716 struct lov_stripe_md *lov_lsm_addref(struct lov_object *lov)
1718 struct lov_stripe_md *lsm = NULL;
1720 lov_conf_freeze(lov);
1721 if (lov->lo_lsm != NULL) {
1722 lsm = lsm_addref(lov->lo_lsm);
1723 CDEBUG(D_INODE, "lsm %p addref %d/%d by %p.\n",
1724 lsm, atomic_read(&lsm->lsm_refc),
1725 lov->lo_layout_invalid, current);
1731 int lov_read_and_clear_async_rc(struct cl_object *clob)
1733 struct lu_object *luobj;
1737 luobj = lu_object_locate(&cl_object_header(clob)->coh_lu,
1739 if (luobj != NULL) {
1740 struct lov_object *lov = lu2lov(luobj);
1742 lov_conf_freeze(lov);
1743 switch (lov->lo_type) {
1745 struct lov_stripe_md *lsm;
1749 LASSERT(lsm != NULL);
1750 for (i = 0; i < lsm->lsm_entry_count; i++) {
1751 struct lov_stripe_md_entry *lse =
1752 lsm->lsm_entries[i];
1755 if (!lsme_inited(lse))
1758 for (j = 0; j < lse->lsme_stripe_count; j++) {
1759 struct lov_oinfo *loi =
1762 if (lov_oinfo_is_dummy(loi))
1765 if (loi->loi_ar.ar_rc && !rc)
1766 rc = loi->loi_ar.ar_rc;
1767 loi->loi_ar.ar_rc = 0;
1781 EXPORT_SYMBOL(lov_read_and_clear_async_rc);