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, 2015, 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"
46 /*****************************************************************************
52 struct lov_layout_operations {
53 int (*llo_init)(const struct lu_env *env, struct lov_device *dev,
54 struct lov_object *lov,
55 const struct cl_object_conf *conf,
56 union lov_layout_state *state);
57 int (*llo_delete)(const struct lu_env *env, struct lov_object *lov,
58 union lov_layout_state *state);
59 void (*llo_fini)(const struct lu_env *env, struct lov_object *lov,
60 union lov_layout_state *state);
61 void (*llo_install)(const struct lu_env *env, struct lov_object *lov,
62 union lov_layout_state *state);
63 int (*llo_print)(const struct lu_env *env, void *cookie,
64 lu_printer_t p, const struct lu_object *o);
65 int (*llo_page_init)(const struct lu_env *env, struct cl_object *obj,
66 struct cl_page *page, pgoff_t index);
67 int (*llo_lock_init)(const struct lu_env *env,
68 struct cl_object *obj, struct cl_lock *lock,
69 const struct cl_io *io);
70 int (*llo_io_init)(const struct lu_env *env,
71 struct cl_object *obj, struct cl_io *io);
72 int (*llo_getattr)(const struct lu_env *env, struct cl_object *obj,
73 struct cl_attr *attr);
76 static int lov_layout_wait(const struct lu_env *env, struct lov_object *lov);
78 void lov_lsm_put(struct cl_object *unused, struct lov_stripe_md *lsm)
83 EXPORT_SYMBOL(lov_lsm_put);
85 /*****************************************************************************
87 * Lov object layout operations.
91 static void lov_install_empty(const struct lu_env *env,
92 struct lov_object *lov,
93 union lov_layout_state *state)
96 * File without objects.
100 static int lov_init_empty(const struct lu_env *env,
101 struct lov_device *dev, struct lov_object *lov,
102 const struct cl_object_conf *conf,
103 union lov_layout_state *state)
108 static void lov_install_raid0(const struct lu_env *env,
109 struct lov_object *lov,
110 union lov_layout_state *state)
114 static struct cl_object *lov_sub_find(const struct lu_env *env,
115 struct cl_device *dev,
116 const struct lu_fid *fid,
117 const struct cl_object_conf *conf)
121 o = lu_object_find_at(env, cl2lu_dev(dev), fid, &conf->coc_lu);
122 LASSERT(ergo(!IS_ERR(o), o->lo_dev->ld_type == &lovsub_device_type));
126 static int lov_init_sub(const struct lu_env *env, struct lov_object *lov,
127 struct cl_object *stripe, struct lov_layout_raid0 *r0,
130 struct cl_object_header *hdr;
131 struct cl_object_header *subhdr;
132 struct cl_object_header *parent;
133 struct lov_oinfo *oinfo;
136 if (OBD_FAIL_CHECK(OBD_FAIL_LOV_INIT)) {
137 /* For sanity:test_206.
138 * Do not leave the object in cache to avoid accessing
139 * freed memory. This is because osc_object is referring to
140 * lov_oinfo of lsm_stripe_data which will be freed due to
143 cl_object_kill(env, stripe);
144 cl_object_put(env, stripe);
148 hdr = cl_object_header(lov2cl(lov));
149 subhdr = cl_object_header(stripe);
151 oinfo = lov->lo_lsm->lsm_oinfo[idx];
152 CDEBUG(D_INODE, DFID"@%p[%d] -> "DFID"@%p: ostid: "DOSTID
153 " idx: %d gen: %d\n",
154 PFID(&subhdr->coh_lu.loh_fid), subhdr, idx,
155 PFID(&hdr->coh_lu.loh_fid), 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;
162 subhdr->coh_parent = hdr;
163 spin_unlock(&subhdr->coh_attr_guard);
164 subhdr->coh_nesting = hdr->coh_nesting + 1;
165 lu_object_ref_add(&stripe->co_lu, "lov-parent", lov);
166 r0->lo_sub[idx] = cl2lovsub(stripe);
167 r0->lo_sub[idx]->lso_super = lov;
168 r0->lo_sub[idx]->lso_index = idx;
171 struct lu_object *old_obj;
172 struct lov_object *old_lov;
173 unsigned int mask = D_INODE;
175 spin_unlock(&subhdr->coh_attr_guard);
176 old_obj = lu_object_locate(&parent->coh_lu, &lov_device_type);
178 old_lov = cl2lov(lu2cl(old_obj));
179 if (old_lov->lo_layout_invalid) {
180 /* the object's layout has already changed but isn't
183 lu_object_unhash(env, &stripe->co_lu);
190 LU_OBJECT_DEBUG(mask, env, &stripe->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, stripe);
199 static int lov_page_slice_fixup(struct lov_object *lov,
200 struct cl_object *stripe)
202 struct cl_object_header *hdr = cl_object_header(&lov->lo_cl);
206 return hdr->coh_page_bufsize - lov->lo_cl.co_slice_off -
207 cfs_size_round(sizeof(struct lov_page));
209 cl_object_for_each(o, stripe)
210 o->co_slice_off += hdr->coh_page_bufsize;
212 return cl_object_header(stripe)->coh_page_bufsize;
215 static int lov_init_raid0(const struct lu_env *env,
216 struct lov_device *dev, struct lov_object *lov,
217 const struct cl_object_conf *conf,
218 union lov_layout_state *state)
223 struct cl_object *stripe;
224 struct lov_thread_info *lti = lov_env_info(env);
225 struct cl_object_conf *subconf = <i->lti_stripe_conf;
226 struct lov_stripe_md *lsm = conf->u.coc_md->lsm;
227 struct lu_fid *ofid = <i->lti_fid;
228 struct lov_layout_raid0 *r0 = &state->raid0;
230 if (lsm->lsm_magic != LOV_MAGIC_V1 && lsm->lsm_magic != LOV_MAGIC_V3) {
231 dump_lsm(D_ERROR, lsm);
232 LASSERTF(0, "magic mismatch, expected %d/%d, actual %d.\n",
233 LOV_MAGIC_V1, LOV_MAGIC_V3, lsm->lsm_magic);
236 LASSERT(!lov->lo_lsm);
237 lov->lo_lsm = lsm_addref(lsm);
238 lov->lo_layout_invalid = true;
239 r0->lo_nr = lsm->lsm_stripe_count;
240 LASSERT(r0->lo_nr <= lov_targets_nr(dev));
242 r0->lo_sub = libcfs_kvzalloc(r0->lo_nr * sizeof(r0->lo_sub[0]),
248 subconf->coc_inode = conf->coc_inode;
249 spin_lock_init(&r0->lo_sub_lock);
251 * Create stripe cl_objects.
253 for (i = 0; i < r0->lo_nr && result == 0; ++i) {
254 struct cl_device *subdev;
255 struct lov_oinfo *oinfo = lsm->lsm_oinfo[i];
256 int ost_idx = oinfo->loi_ost_idx;
258 if (lov_oinfo_is_dummy(oinfo))
261 result = ostid_to_fid(ofid, &oinfo->loi_oi,
266 subdev = lovsub2cl_dev(dev->ld_target[ost_idx]);
267 subconf->u.coc_oinfo = oinfo;
268 LASSERTF(subdev, "not init ost %d\n", ost_idx);
269 /* In the function below, .hs_keycmp resolves to
270 * lu_obj_hop_keycmp()
272 /* coverity[overrun-buffer-val] */
273 stripe = lov_sub_find(env, subdev, ofid, subconf);
274 if (!IS_ERR(stripe)) {
275 result = lov_init_sub(env, lov, stripe, r0, i);
276 if (result == -EAGAIN) { /* try again */
282 result = PTR_ERR(stripe);
286 int sz = lov_page_slice_fixup(lov, stripe);
288 LASSERT(ergo(psz > 0, psz == sz));
293 cl_object_header(&lov->lo_cl)->coh_page_bufsize += psz;
301 static int lov_init_released(const struct lu_env *env,
302 struct lov_device *dev, struct lov_object *lov,
303 const struct cl_object_conf *conf,
304 union lov_layout_state *state)
306 struct lov_stripe_md *lsm = conf->u.coc_md->lsm;
309 LASSERT(lsm_is_released(lsm));
310 LASSERT(!lov->lo_lsm);
312 lov->lo_lsm = lsm_addref(lsm);
316 static int lov_delete_empty(const struct lu_env *env, struct lov_object *lov,
317 union lov_layout_state *state)
319 LASSERT(lov->lo_type == LLT_EMPTY || lov->lo_type == LLT_RELEASED);
321 lov_layout_wait(env, lov);
325 static void lov_subobject_kill(const struct lu_env *env, struct lov_object *lov,
326 struct lovsub_object *los, int idx)
328 struct cl_object *sub;
329 struct lov_layout_raid0 *r0;
330 struct lu_site *site;
331 struct lu_site_bkt_data *bkt;
332 wait_queue_t *waiter;
335 LASSERT(r0->lo_sub[idx] == los);
337 sub = lovsub2cl(los);
338 site = sub->co_lu.lo_dev->ld_site;
339 bkt = lu_site_bkt_from_fid(site, &sub->co_lu.lo_header->loh_fid);
341 cl_object_kill(env, sub);
342 /* release a reference to the sub-object and ... */
343 lu_object_ref_del(&sub->co_lu, "lov-parent", lov);
344 cl_object_put(env, sub);
346 /* ... wait until it is actually destroyed---sub-object clears its
347 * ->lo_sub[] slot in lovsub_object_fini()
349 if (r0->lo_sub[idx] == los) {
350 waiter = &lov_env_info(env)->lti_waiter;
351 init_waitqueue_entry(waiter, current);
352 add_wait_queue(&bkt->lsb_marche_funebre, waiter);
353 set_current_state(TASK_UNINTERRUPTIBLE);
355 /* this wait-queue is signaled at the end of
358 set_current_state(TASK_UNINTERRUPTIBLE);
359 spin_lock(&r0->lo_sub_lock);
360 if (r0->lo_sub[idx] == los) {
361 spin_unlock(&r0->lo_sub_lock);
364 spin_unlock(&r0->lo_sub_lock);
365 set_current_state(TASK_RUNNING);
369 remove_wait_queue(&bkt->lsb_marche_funebre, waiter);
371 LASSERT(!r0->lo_sub[idx]);
374 static int lov_delete_raid0(const struct lu_env *env, struct lov_object *lov,
375 union lov_layout_state *state)
377 struct lov_layout_raid0 *r0 = &state->raid0;
378 struct lov_stripe_md *lsm = lov->lo_lsm;
381 dump_lsm(D_INODE, lsm);
383 lov_layout_wait(env, lov);
385 for (i = 0; i < r0->lo_nr; ++i) {
386 struct lovsub_object *los = r0->lo_sub[i];
389 cl_object_prune(env, &los->lso_cl);
391 * If top-level object is to be evicted from
392 * the cache, so are its sub-objects.
394 lov_subobject_kill(env, lov, los, i);
401 static void lov_fini_empty(const struct lu_env *env, struct lov_object *lov,
402 union lov_layout_state *state)
404 LASSERT(lov->lo_type == LLT_EMPTY || lov->lo_type == LLT_RELEASED);
407 static void lov_fini_raid0(const struct lu_env *env, struct lov_object *lov,
408 union lov_layout_state *state)
410 struct lov_layout_raid0 *r0 = &state->raid0;
417 dump_lsm(D_INODE, lov->lo_lsm);
418 lov_free_memmd(&lov->lo_lsm);
421 static void lov_fini_released(const struct lu_env *env, struct lov_object *lov,
422 union lov_layout_state *state)
424 dump_lsm(D_INODE, lov->lo_lsm);
425 lov_free_memmd(&lov->lo_lsm);
428 static int lov_print_empty(const struct lu_env *env, void *cookie,
429 lu_printer_t p, const struct lu_object *o)
431 (*p)(env, cookie, "empty %d\n", lu2lov(o)->lo_layout_invalid);
435 static int lov_print_raid0(const struct lu_env *env, void *cookie,
436 lu_printer_t p, const struct lu_object *o)
438 struct lov_object *lov = lu2lov(o);
439 struct lov_layout_raid0 *r0 = lov_r0(lov);
440 struct lov_stripe_md *lsm = lov->lo_lsm;
443 (*p)(env, cookie, "stripes: %d, %s, lsm{%p 0x%08X %d %u %u}:\n",
444 r0->lo_nr, lov->lo_layout_invalid ? "invalid" : "valid", lsm,
445 lsm->lsm_magic, atomic_read(&lsm->lsm_refc),
446 lsm->lsm_stripe_count, lsm->lsm_layout_gen);
447 for (i = 0; i < r0->lo_nr; ++i) {
448 struct lu_object *sub;
451 sub = lovsub2lu(r0->lo_sub[i]);
452 lu_object_print(env, cookie, p, sub);
454 (*p)(env, cookie, "sub %d absent\n", i);
460 static int lov_print_released(const struct lu_env *env, void *cookie,
461 lu_printer_t p, const struct lu_object *o)
463 struct lov_object *lov = lu2lov(o);
464 struct lov_stripe_md *lsm = lov->lo_lsm;
467 "released: %s, lsm{%p 0x%08X %d %u %u}:\n",
468 lov->lo_layout_invalid ? "invalid" : "valid", lsm,
469 lsm->lsm_magic, atomic_read(&lsm->lsm_refc),
470 lsm->lsm_stripe_count, lsm->lsm_layout_gen);
475 * Implements cl_object_operations::coo_attr_get() method for an object
476 * without stripes (LLT_EMPTY layout type).
478 * The only attributes this layer is authoritative in this case is
479 * cl_attr::cat_blocks---it's 0.
481 static int lov_attr_get_empty(const struct lu_env *env, struct cl_object *obj,
482 struct cl_attr *attr)
484 attr->cat_blocks = 0;
488 static int lov_attr_get_raid0(const struct lu_env *env, struct cl_object *obj,
489 struct cl_attr *attr)
491 struct lov_object *lov = cl2lov(obj);
492 struct lov_layout_raid0 *r0 = lov_r0(lov);
493 struct cl_attr *lov_attr = &r0->lo_attr;
496 /* this is called w/o holding type guard mutex, so it must be inside
497 * an on going IO otherwise lsm may be replaced.
498 * LU-2117: it turns out there exists one exception. For mmaped files,
499 * the lock of those files may be requested in the other file's IO
500 * context, and this function is called in ccc_lock_state(), it will
501 * hit this assertion.
502 * Anyway, it's still okay to call attr_get w/o type guard as layout
503 * can't go if locks exist.
505 /* LASSERT(atomic_read(&lsm->lsm_refc) > 1); */
507 if (!r0->lo_attr_valid) {
508 struct lov_stripe_md *lsm = lov->lo_lsm;
509 struct ost_lvb *lvb = &lov_env_info(env)->lti_lvb;
512 memset(lvb, 0, sizeof(*lvb));
513 /* XXX: timestamps can be negative by sanity:test_39m,
516 lvb->lvb_atime = LLONG_MIN;
517 lvb->lvb_ctime = LLONG_MIN;
518 lvb->lvb_mtime = LLONG_MIN;
521 * XXX that should be replaced with a loop over sub-objects,
522 * doing cl_object_attr_get() on them. But for now, let's
523 * reuse old lov code.
527 * XXX take lsm spin-lock to keep lov_merge_lvb_kms()
528 * happy. It's not needed, because new code uses
529 * ->coh_attr_guard spin-lock to protect consistency of
530 * sub-object attributes.
532 lov_stripe_lock(lsm);
533 result = lov_merge_lvb_kms(lsm, lvb, &kms);
534 lov_stripe_unlock(lsm);
536 cl_lvb2attr(lov_attr, lvb);
537 lov_attr->cat_kms = kms;
538 r0->lo_attr_valid = 1;
541 if (result == 0) { /* merge results */
542 attr->cat_blocks = lov_attr->cat_blocks;
543 attr->cat_size = lov_attr->cat_size;
544 attr->cat_kms = lov_attr->cat_kms;
545 if (attr->cat_atime < lov_attr->cat_atime)
546 attr->cat_atime = lov_attr->cat_atime;
547 if (attr->cat_ctime < lov_attr->cat_ctime)
548 attr->cat_ctime = lov_attr->cat_ctime;
549 if (attr->cat_mtime < lov_attr->cat_mtime)
550 attr->cat_mtime = lov_attr->cat_mtime;
555 static const struct lov_layout_operations lov_dispatch[] = {
557 .llo_init = lov_init_empty,
558 .llo_delete = lov_delete_empty,
559 .llo_fini = lov_fini_empty,
560 .llo_install = lov_install_empty,
561 .llo_print = lov_print_empty,
562 .llo_page_init = lov_page_init_empty,
563 .llo_lock_init = lov_lock_init_empty,
564 .llo_io_init = lov_io_init_empty,
565 .llo_getattr = lov_attr_get_empty
568 .llo_init = lov_init_raid0,
569 .llo_delete = lov_delete_raid0,
570 .llo_fini = lov_fini_raid0,
571 .llo_install = lov_install_raid0,
572 .llo_print = lov_print_raid0,
573 .llo_page_init = lov_page_init_raid0,
574 .llo_lock_init = lov_lock_init_raid0,
575 .llo_io_init = lov_io_init_raid0,
576 .llo_getattr = lov_attr_get_raid0
579 .llo_init = lov_init_released,
580 .llo_delete = lov_delete_empty,
581 .llo_fini = lov_fini_released,
582 .llo_install = lov_install_empty,
583 .llo_print = lov_print_released,
584 .llo_page_init = lov_page_init_empty,
585 .llo_lock_init = lov_lock_init_empty,
586 .llo_io_init = lov_io_init_released,
587 .llo_getattr = lov_attr_get_empty
592 * Performs a double-dispatch based on the layout type of an object.
594 #define LOV_2DISPATCH_NOLOCK(obj, op, ...) \
596 struct lov_object *__obj = (obj); \
597 enum lov_layout_type __llt; \
599 __llt = __obj->lo_type; \
600 LASSERT(0 <= __llt && __llt < ARRAY_SIZE(lov_dispatch)); \
601 lov_dispatch[__llt].op(__VA_ARGS__); \
605 * Return lov_layout_type associated with a given lsm
607 static enum lov_layout_type lov_type(struct lov_stripe_md *lsm)
611 if (lsm_is_released(lsm))
616 static inline void lov_conf_freeze(struct lov_object *lov)
618 if (lov->lo_owner != current)
619 down_read(&lov->lo_type_guard);
622 static inline void lov_conf_thaw(struct lov_object *lov)
624 if (lov->lo_owner != current)
625 up_read(&lov->lo_type_guard);
628 #define LOV_2DISPATCH_MAYLOCK(obj, op, lock, ...) \
630 struct lov_object *__obj = (obj); \
631 int __lock = !!(lock); \
632 typeof(lov_dispatch[0].op(__VA_ARGS__)) __result; \
635 lov_conf_freeze(__obj); \
636 __result = LOV_2DISPATCH_NOLOCK(obj, op, __VA_ARGS__); \
638 lov_conf_thaw(__obj); \
643 * Performs a locked double-dispatch based on the layout type of an object.
645 #define LOV_2DISPATCH(obj, op, ...) \
646 LOV_2DISPATCH_MAYLOCK(obj, op, 1, __VA_ARGS__)
648 #define LOV_2DISPATCH_VOID(obj, op, ...) \
650 struct lov_object *__obj = (obj); \
651 enum lov_layout_type __llt; \
653 lov_conf_freeze(__obj); \
654 __llt = __obj->lo_type; \
655 LASSERT(0 <= __llt && __llt < ARRAY_SIZE(lov_dispatch)); \
656 lov_dispatch[__llt].op(__VA_ARGS__); \
657 lov_conf_thaw(__obj); \
660 static void lov_conf_lock(struct lov_object *lov)
662 LASSERT(lov->lo_owner != current);
663 down_write(&lov->lo_type_guard);
664 LASSERT(!lov->lo_owner);
665 lov->lo_owner = current;
668 static void lov_conf_unlock(struct lov_object *lov)
670 lov->lo_owner = NULL;
671 up_write(&lov->lo_type_guard);
674 static int lov_layout_wait(const struct lu_env *env, struct lov_object *lov)
676 struct l_wait_info lwi = { 0 };
678 while (atomic_read(&lov->lo_active_ios) > 0) {
679 CDEBUG(D_INODE, "file:" DFID " wait for active IO, now: %d.\n",
680 PFID(lu_object_fid(lov2lu(lov))),
681 atomic_read(&lov->lo_active_ios));
683 l_wait_event(lov->lo_waitq,
684 atomic_read(&lov->lo_active_ios) == 0, &lwi);
689 static int lov_layout_change(const struct lu_env *unused,
690 struct lov_object *lov,
691 const struct cl_object_conf *conf)
694 enum lov_layout_type llt = LLT_EMPTY;
695 union lov_layout_state *state = &lov->u;
696 const struct lov_layout_operations *old_ops;
697 const struct lov_layout_operations *new_ops;
703 LASSERT(0 <= lov->lo_type && lov->lo_type < ARRAY_SIZE(lov_dispatch));
706 llt = lov_type(conf->u.coc_md->lsm);
707 LASSERT(0 <= llt && llt < ARRAY_SIZE(lov_dispatch));
709 cookie = cl_env_reenter();
710 env = cl_env_get(&refcheck);
712 cl_env_reexit(cookie);
716 CDEBUG(D_INODE, DFID" from %s to %s\n",
717 PFID(lu_object_fid(lov2lu(lov))),
718 llt2str(lov->lo_type), llt2str(llt));
720 old_ops = &lov_dispatch[lov->lo_type];
721 new_ops = &lov_dispatch[llt];
723 result = cl_object_prune(env, &lov->lo_cl);
727 result = old_ops->llo_delete(env, lov, &lov->u);
729 old_ops->llo_fini(env, lov, &lov->u);
731 LASSERT(atomic_read(&lov->lo_active_ios) == 0);
733 lov->lo_type = LLT_EMPTY;
734 /* page bufsize fixup */
735 cl_object_header(&lov->lo_cl)->coh_page_bufsize -=
736 lov_page_slice_fixup(lov, NULL);
738 result = new_ops->llo_init(env,
739 lu2lov_dev(lov->lo_cl.co_lu.lo_dev),
742 new_ops->llo_install(env, lov, state);
745 new_ops->llo_delete(env, lov, state);
746 new_ops->llo_fini(env, lov, state);
747 /* this file becomes an EMPTY file. */
752 cl_env_put(env, &refcheck);
753 cl_env_reexit(cookie);
757 /*****************************************************************************
759 * Lov object operations.
762 int lov_object_init(const struct lu_env *env, struct lu_object *obj,
763 const struct lu_object_conf *conf)
765 struct lov_device *dev = lu2lov_dev(obj->lo_dev);
766 struct lov_object *lov = lu2lov(obj);
767 const struct cl_object_conf *cconf = lu2cl_conf(conf);
768 union lov_layout_state *set = &lov->u;
769 const struct lov_layout_operations *ops;
772 init_rwsem(&lov->lo_type_guard);
773 atomic_set(&lov->lo_active_ios, 0);
774 init_waitqueue_head(&lov->lo_waitq);
776 cl_object_page_init(lu2cl(obj), sizeof(struct lov_page));
778 /* no locking is necessary, as object is being created */
779 lov->lo_type = lov_type(cconf->u.coc_md->lsm);
780 ops = &lov_dispatch[lov->lo_type];
781 result = ops->llo_init(env, dev, lov, cconf, set);
783 ops->llo_install(env, lov, set);
787 static int lov_conf_set(const struct lu_env *env, struct cl_object *obj,
788 const struct cl_object_conf *conf)
790 struct lov_stripe_md *lsm = NULL;
791 struct lov_object *lov = cl2lov(obj);
795 if (conf->coc_opc == OBJECT_CONF_INVALIDATE) {
796 lov->lo_layout_invalid = true;
801 if (conf->coc_opc == OBJECT_CONF_WAIT) {
802 if (lov->lo_layout_invalid &&
803 atomic_read(&lov->lo_active_ios) > 0) {
804 lov_conf_unlock(lov);
805 result = lov_layout_wait(env, lov);
811 LASSERT(conf->coc_opc == OBJECT_CONF_SET);
814 lsm = conf->u.coc_md->lsm;
815 if ((!lsm && !lov->lo_lsm) ||
816 ((lsm && lov->lo_lsm) &&
817 (lov->lo_lsm->lsm_layout_gen == lsm->lsm_layout_gen) &&
818 (lov->lo_lsm->lsm_pattern == lsm->lsm_pattern))) {
819 /* same version of layout */
820 lov->lo_layout_invalid = false;
825 /* will change layout - check if there still exists active IO. */
826 if (atomic_read(&lov->lo_active_ios) > 0) {
827 lov->lo_layout_invalid = true;
832 result = lov_layout_change(env, lov, conf);
833 lov->lo_layout_invalid = result != 0;
836 lov_conf_unlock(lov);
837 CDEBUG(D_INODE, DFID" lo_layout_invalid=%d\n",
838 PFID(lu_object_fid(lov2lu(lov))), lov->lo_layout_invalid);
842 static void lov_object_delete(const struct lu_env *env, struct lu_object *obj)
844 struct lov_object *lov = lu2lov(obj);
846 LOV_2DISPATCH_VOID(lov, llo_delete, env, lov, &lov->u);
849 static void lov_object_free(const struct lu_env *env, struct lu_object *obj)
851 struct lov_object *lov = lu2lov(obj);
853 LOV_2DISPATCH_VOID(lov, llo_fini, env, lov, &lov->u);
855 kmem_cache_free(lov_object_kmem, lov);
858 static int lov_object_print(const struct lu_env *env, void *cookie,
859 lu_printer_t p, const struct lu_object *o)
861 return LOV_2DISPATCH_NOLOCK(lu2lov(o), llo_print, env, cookie, p, o);
864 int lov_page_init(const struct lu_env *env, struct cl_object *obj,
865 struct cl_page *page, pgoff_t index)
867 return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_page_init, env, obj, page,
872 * Implements cl_object_operations::clo_io_init() method for lov
873 * layer. Dispatches to the appropriate layout io initialization method.
875 int lov_io_init(const struct lu_env *env, struct cl_object *obj,
878 CL_IO_SLICE_CLEAN(lov_env_io(env), lis_cl);
879 return LOV_2DISPATCH_MAYLOCK(cl2lov(obj), llo_io_init,
880 !io->ci_ignore_layout, env, obj, io);
884 * An implementation of cl_object_operations::clo_attr_get() method for lov
885 * layer. For raid0 layout this collects and merges attributes of all
888 static int lov_attr_get(const struct lu_env *env, struct cl_object *obj,
889 struct cl_attr *attr)
891 /* do not take lock, as this function is called under a
892 * spin-lock. Layout is protected from changing by ongoing IO.
894 return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_getattr, env, obj, attr);
897 static int lov_attr_update(const struct lu_env *env, struct cl_object *obj,
898 const struct cl_attr *attr, unsigned int valid)
901 * No dispatch is required here, as no layout implements this.
906 int lov_lock_init(const struct lu_env *env, struct cl_object *obj,
907 struct cl_lock *lock, const struct cl_io *io)
909 /* No need to lock because we've taken one refcount of layout. */
910 return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_lock_init, env, obj, lock,
914 static int lov_object_getstripe(const struct lu_env *env, struct cl_object *obj,
915 struct lov_user_md __user *lum)
917 struct lov_object *lov = cl2lov(obj);
918 struct lov_stripe_md *lsm;
921 lsm = lov_lsm_addref(lov);
925 rc = lov_getstripe(cl2lov(obj), lsm, lum);
926 lov_lsm_put(obj, lsm);
930 static const struct cl_object_operations lov_ops = {
931 .coo_page_init = lov_page_init,
932 .coo_lock_init = lov_lock_init,
933 .coo_io_init = lov_io_init,
934 .coo_attr_get = lov_attr_get,
935 .coo_attr_update = lov_attr_update,
936 .coo_conf_set = lov_conf_set,
937 .coo_getstripe = lov_object_getstripe
940 static const struct lu_object_operations lov_lu_obj_ops = {
941 .loo_object_init = lov_object_init,
942 .loo_object_delete = lov_object_delete,
943 .loo_object_release = NULL,
944 .loo_object_free = lov_object_free,
945 .loo_object_print = lov_object_print,
946 .loo_object_invariant = NULL
949 struct lu_object *lov_object_alloc(const struct lu_env *env,
950 const struct lu_object_header *unused,
951 struct lu_device *dev)
953 struct lov_object *lov;
954 struct lu_object *obj;
956 lov = kmem_cache_zalloc(lov_object_kmem, GFP_NOFS);
959 lu_object_init(obj, NULL, dev);
960 lov->lo_cl.co_ops = &lov_ops;
961 lov->lo_type = -1; /* invalid, to catch uninitialized type */
963 * object io operation vector (cl_object::co_iop) is installed
964 * later in lov_object_init(), as different vectors are used
965 * for object with different layouts.
967 obj->lo_ops = &lov_lu_obj_ops;
974 struct lov_stripe_md *lov_lsm_addref(struct lov_object *lov)
976 struct lov_stripe_md *lsm = NULL;
978 lov_conf_freeze(lov);
980 lsm = lsm_addref(lov->lo_lsm);
981 CDEBUG(D_INODE, "lsm %p addref %d/%d by %p.\n",
982 lsm, atomic_read(&lsm->lsm_refc),
983 lov->lo_layout_invalid, current);
989 struct lov_stripe_md *lov_lsm_get(struct cl_object *clobj)
991 struct lu_object *luobj;
992 struct lov_stripe_md *lsm = NULL;
997 luobj = lu_object_locate(&cl_object_header(clobj)->coh_lu,
1000 lsm = lov_lsm_addref(lu2lov(luobj));
1003 EXPORT_SYMBOL(lov_lsm_get);
1005 int lov_read_and_clear_async_rc(struct cl_object *clob)
1007 struct lu_object *luobj;
1010 luobj = lu_object_locate(&cl_object_header(clob)->coh_lu,
1013 struct lov_object *lov = lu2lov(luobj);
1015 lov_conf_freeze(lov);
1016 switch (lov->lo_type) {
1018 struct lov_stripe_md *lsm;
1022 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1023 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
1025 if (lov_oinfo_is_dummy(loi))
1028 if (loi->loi_ar.ar_rc && !rc)
1029 rc = loi->loi_ar.ar_rc;
1030 loi->loi_ar.ar_rc = 0;
1043 EXPORT_SYMBOL(lov_read_and_clear_async_rc);