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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * Implementation of cl_object for LOV layer.
38 * Author: Nikita Danilov <nikita.danilov@sun.com>
39 * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
42 #define DEBUG_SUBSYSTEM S_LOV
44 #include "lov_cl_internal.h"
50 /*****************************************************************************
56 struct lov_layout_operations {
57 int (*llo_init)(const struct lu_env *env, struct lov_device *dev,
58 struct lov_object *lov,
59 const struct cl_object_conf *conf,
60 union lov_layout_state *state);
61 int (*llo_delete)(const struct lu_env *env, struct lov_object *lov,
62 union lov_layout_state *state);
63 void (*llo_fini)(const struct lu_env *env, struct lov_object *lov,
64 union lov_layout_state *state);
65 void (*llo_install)(const struct lu_env *env, struct lov_object *lov,
66 union lov_layout_state *state);
67 int (*llo_print)(const struct lu_env *env, void *cookie,
68 lu_printer_t p, const struct lu_object *o);
69 int (*llo_page_init)(const struct lu_env *env, struct cl_object *obj,
70 struct cl_page *page, pgoff_t index);
71 int (*llo_lock_init)(const struct lu_env *env,
72 struct cl_object *obj, struct cl_lock *lock,
73 const struct cl_io *io);
74 int (*llo_io_init)(const struct lu_env *env,
75 struct cl_object *obj, struct cl_io *io);
76 int (*llo_getattr)(const struct lu_env *env, struct cl_object *obj,
77 struct cl_attr *attr);
78 int (*llo_find_cbdata)(const struct lu_env *env, struct cl_object *obj,
79 ldlm_iterator_t iter, void *data);
82 static int lov_layout_wait(const struct lu_env *env, struct lov_object *lov);
84 struct lov_stripe_md *lov_lsm_get(struct cl_object *clobj)
86 struct lu_object *luobj;
87 struct lov_stripe_md *lsm = NULL;
92 luobj = lu_object_locate(&cl_object_header(clobj)->coh_lu,
95 lsm = lov_lsm_addref(lu2lov(luobj));
98 EXPORT_SYMBOL(lov_lsm_get);
100 void lov_lsm_put(struct cl_object *unused, struct lov_stripe_md *lsm)
103 lov_free_memmd(&lsm);
105 EXPORT_SYMBOL(lov_lsm_put);
107 /*****************************************************************************
109 * Lov object layout operations.
113 static void lov_install_empty(const struct lu_env *env,
114 struct lov_object *lov,
115 union lov_layout_state *state)
118 * File without objects.
122 static int lov_init_empty(const struct lu_env *env,
123 struct lov_device *dev, struct lov_object *lov,
124 const struct cl_object_conf *conf,
125 union lov_layout_state *state)
130 static void lov_install_raid0(const struct lu_env *env,
131 struct lov_object *lov,
132 union lov_layout_state *state)
136 static struct cl_object *lov_sub_find(const struct lu_env *env,
137 struct cl_device *dev,
138 const struct lu_fid *fid,
139 const struct cl_object_conf *conf)
144 o = lu_object_find_at(env, cl2lu_dev(dev), fid, &conf->coc_lu);
145 LASSERT(ergo(!IS_ERR(o), o->lo_dev->ld_type == &lovsub_device_type));
149 static int lov_init_sub(const struct lu_env *env, struct lov_object *lov,
150 struct cl_object *stripe, struct lov_layout_raid0 *r0,
153 struct cl_object_header *hdr;
154 struct cl_object_header *subhdr;
155 struct cl_object_header *parent;
156 struct lov_oinfo *oinfo;
159 if (OBD_FAIL_CHECK(OBD_FAIL_LOV_INIT)) {
160 /* For sanity:test_206.
161 * Do not leave the object in cache to avoid accessing
162 * freed memory. This is because osc_object is referring to
163 * lov_oinfo of lsm_stripe_data which will be freed due to
165 cl_object_kill(env, stripe);
166 cl_object_put(env, stripe);
170 hdr = cl_object_header(lov2cl(lov));
171 subhdr = cl_object_header(stripe);
173 oinfo = lov->lo_lsm->lsm_oinfo[idx];
174 CDEBUG(D_INODE, DFID"@%p[%d] -> "DFID"@%p: ostid: "DOSTID
175 " idx: %d gen: %d\n",
176 PFID(&subhdr->coh_lu.loh_fid), subhdr, idx,
177 PFID(&hdr->coh_lu.loh_fid), hdr, POSTID(&oinfo->loi_oi),
178 oinfo->loi_ost_idx, oinfo->loi_ost_gen);
180 /* reuse ->coh_attr_guard to protect coh_parent change */
181 spin_lock(&subhdr->coh_attr_guard);
182 parent = subhdr->coh_parent;
183 if (parent == NULL) {
184 subhdr->coh_parent = hdr;
185 spin_unlock(&subhdr->coh_attr_guard);
186 subhdr->coh_nesting = hdr->coh_nesting + 1;
187 lu_object_ref_add(&stripe->co_lu, "lov-parent", lov);
188 r0->lo_sub[idx] = cl2lovsub(stripe);
189 r0->lo_sub[idx]->lso_super = lov;
190 r0->lo_sub[idx]->lso_index = idx;
193 struct lu_object *old_obj;
194 struct lov_object *old_lov;
195 unsigned int mask = D_INODE;
197 spin_unlock(&subhdr->coh_attr_guard);
198 old_obj = lu_object_locate(&parent->coh_lu, &lov_device_type);
199 LASSERT(old_obj != NULL);
200 old_lov = cl2lov(lu2cl(old_obj));
201 if (old_lov->lo_layout_invalid) {
202 /* the object's layout has already changed but isn't
204 lu_object_unhash(env, &stripe->co_lu);
211 LU_OBJECT_DEBUG(mask, env, &stripe->co_lu,
212 "stripe %d is already owned.\n", idx);
213 LU_OBJECT_DEBUG(mask, env, old_obj, "owned.\n");
214 LU_OBJECT_HEADER(mask, env, lov2lu(lov), "try to own.\n");
215 cl_object_put(env, stripe);
220 static int lov_page_slice_fixup(struct lov_object *lov,
221 struct cl_object *stripe)
223 struct cl_object_header *hdr = cl_object_header(&lov->lo_cl);
227 return hdr->coh_page_bufsize - lov->lo_cl.co_slice_off -
228 cfs_size_round(sizeof(struct lov_page));
230 cl_object_for_each(o, stripe)
231 o->co_slice_off += hdr->coh_page_bufsize;
233 return cl_object_header(stripe)->coh_page_bufsize;
236 static int lov_init_raid0(const struct lu_env *env,
237 struct lov_device *dev, struct lov_object *lov,
238 const struct cl_object_conf *conf,
239 union lov_layout_state *state)
244 struct cl_object *stripe;
245 struct lov_thread_info *lti = lov_env_info(env);
246 struct cl_object_conf *subconf = <i->lti_stripe_conf;
247 struct lov_stripe_md *lsm = conf->u.coc_md->lsm;
248 struct lu_fid *ofid = <i->lti_fid;
249 struct lov_layout_raid0 *r0 = &state->raid0;
253 if (lsm->lsm_magic != LOV_MAGIC_V1 && lsm->lsm_magic != LOV_MAGIC_V3) {
254 dump_lsm(D_ERROR, lsm);
255 LASSERTF(0, "magic mismatch, expected %d/%d, actual %d.\n",
256 LOV_MAGIC_V1, LOV_MAGIC_V3, lsm->lsm_magic);
259 LASSERT(lov->lo_lsm == NULL);
260 lov->lo_lsm = lsm_addref(lsm);
261 r0->lo_nr = lsm->lsm_stripe_count;
262 LASSERT(r0->lo_nr <= lov_targets_nr(dev));
264 lov->lo_layout_invalid = true;
266 OBD_ALLOC_LARGE(r0->lo_sub, r0->lo_nr * sizeof r0->lo_sub[0]);
267 if (r0->lo_sub != NULL) {
271 subconf->coc_inode = conf->coc_inode;
272 spin_lock_init(&r0->lo_sub_lock);
274 * Create stripe cl_objects.
276 for (i = 0; i < r0->lo_nr && result == 0; ++i) {
277 struct cl_device *subdev;
278 struct lov_oinfo *oinfo = lsm->lsm_oinfo[i];
279 int ost_idx = oinfo->loi_ost_idx;
281 if (lov_oinfo_is_dummy(oinfo))
284 result = ostid_to_fid(ofid, &oinfo->loi_oi,
289 subdev = lovsub2cl_dev(dev->ld_target[ost_idx]);
290 subconf->u.coc_oinfo = oinfo;
291 LASSERTF(subdev != NULL, "not init ost %d\n", ost_idx);
292 /* In the function below, .hs_keycmp resolves to
293 * lu_obj_hop_keycmp() */
294 /* coverity[overrun-buffer-val] */
295 stripe = lov_sub_find(env, subdev, ofid, subconf);
296 if (!IS_ERR(stripe)) {
297 result = lov_init_sub(env, lov, stripe, r0, i);
298 if (result == -EAGAIN) { /* try again */
304 result = PTR_ERR(stripe);
308 int sz = lov_page_slice_fixup(lov, stripe);
309 LASSERT(ergo(psz > 0, psz == sz));
314 cl_object_header(&lov->lo_cl)->coh_page_bufsize += psz;
321 static int lov_init_released(const struct lu_env *env,
322 struct lov_device *dev, struct lov_object *lov,
323 const struct cl_object_conf *conf,
324 union lov_layout_state *state)
326 struct lov_stripe_md *lsm = conf->u.coc_md->lsm;
328 LASSERT(lsm != NULL);
329 LASSERT(lsm_is_released(lsm));
330 LASSERT(lov->lo_lsm == NULL);
332 lov->lo_lsm = lsm_addref(lsm);
336 static int lov_delete_empty(const struct lu_env *env, struct lov_object *lov,
337 union lov_layout_state *state)
339 LASSERT(lov->lo_type == LLT_EMPTY || lov->lo_type == LLT_RELEASED);
341 lov_layout_wait(env, lov);
345 static void lov_subobject_kill(const struct lu_env *env, struct lov_object *lov,
346 struct lovsub_object *los, int idx)
348 struct cl_object *sub;
349 struct lov_layout_raid0 *r0;
350 struct lu_site *site;
351 struct lu_site_bkt_data *bkt;
352 wait_queue_t *waiter;
355 LASSERT(r0->lo_sub[idx] == los);
357 sub = lovsub2cl(los);
358 site = sub->co_lu.lo_dev->ld_site;
359 bkt = lu_site_bkt_from_fid(site, &sub->co_lu.lo_header->loh_fid);
361 cl_object_kill(env, sub);
362 /* release a reference to the sub-object and ... */
363 lu_object_ref_del(&sub->co_lu, "lov-parent", lov);
364 cl_object_put(env, sub);
366 /* ... wait until it is actually destroyed---sub-object clears its
367 * ->lo_sub[] slot in lovsub_object_fini() */
368 if (r0->lo_sub[idx] == los) {
369 waiter = &lov_env_info(env)->lti_waiter;
370 init_waitqueue_entry_current(waiter);
371 add_wait_queue(&bkt->lsb_marche_funebre, waiter);
372 set_current_state(TASK_UNINTERRUPTIBLE);
374 /* this wait-queue is signaled at the end of
375 * lu_object_free(). */
376 set_current_state(TASK_UNINTERRUPTIBLE);
377 spin_lock(&r0->lo_sub_lock);
378 if (r0->lo_sub[idx] == los) {
379 spin_unlock(&r0->lo_sub_lock);
380 waitq_wait(waiter, TASK_UNINTERRUPTIBLE);
382 spin_unlock(&r0->lo_sub_lock);
383 set_current_state(TASK_RUNNING);
387 remove_wait_queue(&bkt->lsb_marche_funebre, waiter);
389 LASSERT(r0->lo_sub[idx] == NULL);
392 static int lov_delete_raid0(const struct lu_env *env, struct lov_object *lov,
393 union lov_layout_state *state)
395 struct lov_layout_raid0 *r0 = &state->raid0;
396 struct lov_stripe_md *lsm = lov->lo_lsm;
401 dump_lsm(D_INODE, lsm);
403 lov_layout_wait(env, lov);
404 if (r0->lo_sub != NULL) {
405 for (i = 0; i < r0->lo_nr; ++i) {
406 struct lovsub_object *los = r0->lo_sub[i];
409 cl_object_prune(env, &los->lso_cl);
411 * If top-level object is to be evicted from
412 * the cache, so are its sub-objects.
414 lov_subobject_kill(env, lov, los, i);
421 static void lov_fini_empty(const struct lu_env *env, struct lov_object *lov,
422 union lov_layout_state *state)
424 LASSERT(lov->lo_type == LLT_EMPTY || lov->lo_type == LLT_RELEASED);
427 static void lov_fini_raid0(const struct lu_env *env, struct lov_object *lov,
428 union lov_layout_state *state)
430 struct lov_layout_raid0 *r0 = &state->raid0;
433 if (r0->lo_sub != NULL) {
434 OBD_FREE_LARGE(r0->lo_sub, r0->lo_nr * sizeof r0->lo_sub[0]);
438 dump_lsm(D_INODE, lov->lo_lsm);
439 lov_free_memmd(&lov->lo_lsm);
444 static void lov_fini_released(const struct lu_env *env, struct lov_object *lov,
445 union lov_layout_state *state)
448 dump_lsm(D_INODE, lov->lo_lsm);
449 lov_free_memmd(&lov->lo_lsm);
453 static int lov_print_empty(const struct lu_env *env, void *cookie,
454 lu_printer_t p, const struct lu_object *o)
456 (*p)(env, cookie, "empty %d\n", lu2lov(o)->lo_layout_invalid);
460 static int lov_print_raid0(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_layout_raid0 *r0 = lov_r0(lov);
465 struct lov_stripe_md *lsm = lov->lo_lsm;
468 (*p)(env, cookie, "stripes: %d, %s, lsm{%p 0x%08X %d %u %u}:\n",
469 r0->lo_nr, lov->lo_layout_invalid ? "invalid" : "valid", lsm,
470 lsm->lsm_magic, atomic_read(&lsm->lsm_refc),
471 lsm->lsm_stripe_count, lsm->lsm_layout_gen);
472 for (i = 0; i < r0->lo_nr; ++i) {
473 struct lu_object *sub;
475 if (r0->lo_sub[i] != NULL) {
476 sub = lovsub2lu(r0->lo_sub[i]);
477 lu_object_print(env, cookie, p, sub);
479 (*p)(env, cookie, "sub %d absent\n", i);
485 static int lov_print_released(const struct lu_env *env, void *cookie,
486 lu_printer_t p, const struct lu_object *o)
488 struct lov_object *lov = lu2lov(o);
489 struct lov_stripe_md *lsm = lov->lo_lsm;
492 "released: %s, lsm{%p 0x%08X %d %u %u}:\n",
493 lov->lo_layout_invalid ? "invalid" : "valid", lsm,
494 lsm->lsm_magic, atomic_read(&lsm->lsm_refc),
495 lsm->lsm_stripe_count, lsm->lsm_layout_gen);
500 * Implements cl_object_operations::coo_attr_get() method for an object
501 * without stripes (LLT_EMPTY layout type).
503 * The only attributes this layer is authoritative in this case is
504 * cl_attr::cat_blocks---it's 0.
506 static int lov_attr_get_empty(const struct lu_env *env, struct cl_object *obj,
507 struct cl_attr *attr)
509 attr->cat_blocks = 0;
513 static int lov_attr_get_raid0(const struct lu_env *env, struct cl_object *obj,
514 struct cl_attr *attr)
516 struct lov_object *lov = cl2lov(obj);
517 struct lov_layout_raid0 *r0 = lov_r0(lov);
518 struct cl_attr *lov_attr = &r0->lo_attr;
523 /* this is called w/o holding type guard mutex, so it must be inside
524 * an on going IO otherwise lsm may be replaced.
525 * LU-2117: it turns out there exists one exception. For mmaped files,
526 * the lock of those files may be requested in the other file's IO
527 * context, and this function is called in ccc_lock_state(), it will
528 * hit this assertion.
529 * Anyway, it's still okay to call attr_get w/o type guard as layout
530 * can't go if locks exist. */
531 /* LASSERT(atomic_read(&lsm->lsm_refc) > 1); */
533 if (!r0->lo_attr_valid) {
534 struct lov_stripe_md *lsm = lov->lo_lsm;
535 struct ost_lvb *lvb = &lov_env_info(env)->lti_lvb;
538 memset(lvb, 0, sizeof(*lvb));
539 /* XXX: timestamps can be negative by sanity:test_39m,
541 lvb->lvb_atime = LLONG_MIN;
542 lvb->lvb_ctime = LLONG_MIN;
543 lvb->lvb_mtime = LLONG_MIN;
546 * XXX that should be replaced with a loop over sub-objects,
547 * doing cl_object_attr_get() on them. But for now, let's
548 * reuse old lov code.
552 * XXX take lsm spin-lock to keep lov_merge_lvb_kms()
553 * happy. It's not needed, because new code uses
554 * ->coh_attr_guard spin-lock to protect consistency of
555 * sub-object attributes.
557 lov_stripe_lock(lsm);
558 result = lov_merge_lvb_kms(lsm, lvb, &kms);
559 lov_stripe_unlock(lsm);
561 cl_lvb2attr(lov_attr, lvb);
562 lov_attr->cat_kms = kms;
563 r0->lo_attr_valid = 1;
566 if (result == 0) { /* merge results */
567 attr->cat_blocks = lov_attr->cat_blocks;
568 attr->cat_size = lov_attr->cat_size;
569 attr->cat_kms = lov_attr->cat_kms;
570 if (attr->cat_atime < lov_attr->cat_atime)
571 attr->cat_atime = lov_attr->cat_atime;
572 if (attr->cat_ctime < lov_attr->cat_ctime)
573 attr->cat_ctime = lov_attr->cat_ctime;
574 if (attr->cat_mtime < lov_attr->cat_mtime)
575 attr->cat_mtime = lov_attr->cat_mtime;
580 static int lov_find_cbdata_empty(const struct lu_env *env,
581 struct cl_object *obj, ldlm_iterator_t iter,
587 static int lov_find_cbdata_raid0(const struct lu_env *env,
588 struct cl_object *obj, ldlm_iterator_t iter,
591 struct lov_object *lov = cl2lov(obj);
592 struct lov_layout_raid0 *r0 = lov_r0(lov);
593 struct cl_object *subobj;
597 for (i = 0; i < r0->lo_nr; ++i) {
598 if (r0->lo_sub[i] == NULL)
601 subobj = lovsub2cl(r0->lo_sub[i]);
603 rc = cl_object_find_cbdata(env, subobj, iter, data);
611 const static struct lov_layout_operations lov_dispatch[] = {
613 .llo_init = lov_init_empty,
614 .llo_delete = lov_delete_empty,
615 .llo_fini = lov_fini_empty,
616 .llo_install = lov_install_empty,
617 .llo_print = lov_print_empty,
618 .llo_page_init = lov_page_init_empty,
619 .llo_lock_init = lov_lock_init_empty,
620 .llo_io_init = lov_io_init_empty,
621 .llo_getattr = lov_attr_get_empty,
622 .llo_find_cbdata = lov_find_cbdata_empty
625 .llo_init = lov_init_raid0,
626 .llo_delete = lov_delete_raid0,
627 .llo_fini = lov_fini_raid0,
628 .llo_install = lov_install_raid0,
629 .llo_print = lov_print_raid0,
630 .llo_page_init = lov_page_init_raid0,
631 .llo_lock_init = lov_lock_init_raid0,
632 .llo_io_init = lov_io_init_raid0,
633 .llo_getattr = lov_attr_get_raid0,
634 .llo_find_cbdata = lov_find_cbdata_raid0
637 .llo_init = lov_init_released,
638 .llo_delete = lov_delete_empty,
639 .llo_fini = lov_fini_released,
640 .llo_install = lov_install_empty,
641 .llo_print = lov_print_released,
642 .llo_page_init = lov_page_init_empty,
643 .llo_lock_init = lov_lock_init_empty,
644 .llo_io_init = lov_io_init_released,
645 .llo_getattr = lov_attr_get_empty,
646 .llo_find_cbdata = lov_find_cbdata_empty
651 * Performs a double-dispatch based on the layout type of an object.
653 #define LOV_2DISPATCH_NOLOCK(obj, op, ...) \
655 struct lov_object *__obj = (obj); \
656 enum lov_layout_type __llt; \
658 __llt = __obj->lo_type; \
659 LASSERT(0 <= __llt && __llt < ARRAY_SIZE(lov_dispatch)); \
660 lov_dispatch[__llt].op(__VA_ARGS__); \
664 * Return lov_layout_type associated with a given lsm
666 static enum lov_layout_type lov_type(struct lov_stripe_md *lsm)
670 if (lsm_is_released(lsm))
675 static inline void lov_conf_freeze(struct lov_object *lov)
677 if (lov->lo_owner != current)
678 down_read(&lov->lo_type_guard);
681 static inline void lov_conf_thaw(struct lov_object *lov)
683 if (lov->lo_owner != current)
684 up_read(&lov->lo_type_guard);
687 #define LOV_2DISPATCH_MAYLOCK(obj, op, lock, ...) \
689 struct lov_object *__obj = (obj); \
690 int __lock = !!(lock); \
691 typeof(lov_dispatch[0].op(__VA_ARGS__)) __result; \
694 lov_conf_freeze(__obj); \
695 __result = LOV_2DISPATCH_NOLOCK(obj, op, __VA_ARGS__); \
697 lov_conf_thaw(__obj); \
702 * Performs a locked double-dispatch based on the layout type of an object.
704 #define LOV_2DISPATCH(obj, op, ...) \
705 LOV_2DISPATCH_MAYLOCK(obj, op, 1, __VA_ARGS__)
707 #define LOV_2DISPATCH_VOID(obj, op, ...) \
709 struct lov_object *__obj = (obj); \
710 enum lov_layout_type __llt; \
712 lov_conf_freeze(__obj); \
713 __llt = __obj->lo_type; \
714 LASSERT(0 <= __llt && __llt < ARRAY_SIZE(lov_dispatch)); \
715 lov_dispatch[__llt].op(__VA_ARGS__); \
716 lov_conf_thaw(__obj); \
719 static void lov_conf_lock(struct lov_object *lov)
721 LASSERT(lov->lo_owner != current);
722 down_write(&lov->lo_type_guard);
723 LASSERT(lov->lo_owner == NULL);
724 lov->lo_owner = current;
727 static void lov_conf_unlock(struct lov_object *lov)
729 lov->lo_owner = NULL;
730 up_write(&lov->lo_type_guard);
733 static int lov_layout_wait(const struct lu_env *env, struct lov_object *lov)
735 struct l_wait_info lwi = { 0 };
738 while (atomic_read(&lov->lo_active_ios) > 0) {
739 CDEBUG(D_INODE, "file:"DFID" wait for active IO, now: %d.\n",
740 PFID(lu_object_fid(lov2lu(lov))),
741 atomic_read(&lov->lo_active_ios));
743 l_wait_event(lov->lo_waitq,
744 atomic_read(&lov->lo_active_ios) == 0, &lwi);
749 static int lov_layout_change(const struct lu_env *unused,
750 struct lov_object *lov,
751 const struct cl_object_conf *conf)
754 enum lov_layout_type llt = LLT_EMPTY;
755 union lov_layout_state *state = &lov->u;
756 const struct lov_layout_operations *old_ops;
757 const struct lov_layout_operations *new_ops;
764 LASSERT(0 <= lov->lo_type && lov->lo_type < ARRAY_SIZE(lov_dispatch));
766 if (conf->u.coc_md != NULL)
767 llt = lov_type(conf->u.coc_md->lsm);
768 LASSERT(0 <= llt && llt < ARRAY_SIZE(lov_dispatch));
770 cookie = cl_env_reenter();
771 env = cl_env_get(&refcheck);
773 cl_env_reexit(cookie);
774 RETURN(PTR_ERR(env));
777 CDEBUG(D_INODE, DFID" from %s to %s\n",
778 PFID(lu_object_fid(lov2lu(lov))),
779 llt2str(lov->lo_type), llt2str(llt));
781 old_ops = &lov_dispatch[lov->lo_type];
782 new_ops = &lov_dispatch[llt];
784 result = cl_object_prune(env, &lov->lo_cl);
788 result = old_ops->llo_delete(env, lov, &lov->u);
790 old_ops->llo_fini(env, lov, &lov->u);
792 LASSERT(atomic_read(&lov->lo_active_ios) == 0);
794 lov->lo_type = LLT_EMPTY;
795 /* page bufsize fixup */
796 cl_object_header(&lov->lo_cl)->coh_page_bufsize -=
797 lov_page_slice_fixup(lov, NULL);
799 result = new_ops->llo_init(env,
800 lu2lov_dev(lov->lo_cl.co_lu.lo_dev),
803 new_ops->llo_install(env, lov, state);
806 new_ops->llo_delete(env, lov, state);
807 new_ops->llo_fini(env, lov, state);
808 /* this file becomes an EMPTY file. */
813 cl_env_put(env, &refcheck);
814 cl_env_reexit(cookie);
818 /*****************************************************************************
820 * Lov object operations.
823 int lov_object_init(const struct lu_env *env, struct lu_object *obj,
824 const struct lu_object_conf *conf)
826 struct lov_device *dev = lu2lov_dev(obj->lo_dev);
827 struct lov_object *lov = lu2lov(obj);
828 const struct cl_object_conf *cconf = lu2cl_conf(conf);
829 union lov_layout_state *set = &lov->u;
830 const struct lov_layout_operations *ops;
834 init_rwsem(&lov->lo_type_guard);
835 atomic_set(&lov->lo_active_ios, 0);
836 init_waitqueue_head(&lov->lo_waitq);
838 cl_object_page_init(lu2cl(obj), sizeof(struct lov_page));
840 /* no locking is necessary, as object is being created */
841 lov->lo_type = lov_type(cconf->u.coc_md->lsm);
842 ops = &lov_dispatch[lov->lo_type];
843 result = ops->llo_init(env, dev, lov, cconf, set);
845 ops->llo_install(env, lov, set);
849 static int lov_conf_set(const struct lu_env *env, struct cl_object *obj,
850 const struct cl_object_conf *conf)
852 struct lov_stripe_md *lsm = NULL;
853 struct lov_object *lov = cl2lov(obj);
858 if (conf->coc_opc == OBJECT_CONF_INVALIDATE) {
859 lov->lo_layout_invalid = true;
860 GOTO(out, result = 0);
863 if (conf->coc_opc == OBJECT_CONF_WAIT) {
864 if (lov->lo_layout_invalid &&
865 atomic_read(&lov->lo_active_ios) > 0) {
866 lov_conf_unlock(lov);
867 result = lov_layout_wait(env, lov);
873 LASSERT(conf->coc_opc == OBJECT_CONF_SET);
875 if (conf->u.coc_md != NULL)
876 lsm = conf->u.coc_md->lsm;
877 if ((lsm == NULL && lov->lo_lsm == NULL) ||
878 ((lsm != NULL && lov->lo_lsm != NULL) &&
879 (lov->lo_lsm->lsm_layout_gen == lsm->lsm_layout_gen) &&
880 (lov->lo_lsm->lsm_pattern == lsm->lsm_pattern))) {
881 /* same version of layout */
882 lov->lo_layout_invalid = false;
883 GOTO(out, result = 0);
886 /* will change layout - check if there still exists active IO. */
887 if (atomic_read(&lov->lo_active_ios) > 0) {
888 lov->lo_layout_invalid = true;
889 GOTO(out, result = -EBUSY);
892 result = lov_layout_change(env, lov, conf);
893 lov->lo_layout_invalid = result != 0;
897 lov_conf_unlock(lov);
898 CDEBUG(D_INODE, DFID" lo_layout_invalid=%d\n",
899 PFID(lu_object_fid(lov2lu(lov))), lov->lo_layout_invalid);
903 static void lov_object_delete(const struct lu_env *env, struct lu_object *obj)
905 struct lov_object *lov = lu2lov(obj);
908 LOV_2DISPATCH_VOID(lov, llo_delete, env, lov, &lov->u);
912 static void lov_object_free(const struct lu_env *env, struct lu_object *obj)
914 struct lov_object *lov = lu2lov(obj);
917 LOV_2DISPATCH_VOID(lov, llo_fini, env, lov, &lov->u);
919 OBD_SLAB_FREE_PTR(lov, lov_object_kmem);
923 static int lov_object_print(const struct lu_env *env, void *cookie,
924 lu_printer_t p, const struct lu_object *o)
926 return LOV_2DISPATCH_NOLOCK(lu2lov(o), llo_print, env, cookie, p, o);
929 int lov_page_init(const struct lu_env *env, struct cl_object *obj,
930 struct cl_page *page, pgoff_t index)
932 return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_page_init, env, obj, page,
937 * Implements cl_object_operations::clo_io_init() method for lov
938 * layer. Dispatches to the appropriate layout io initialization method.
940 int lov_io_init(const struct lu_env *env, struct cl_object *obj,
943 CL_IO_SLICE_CLEAN(lov_env_io(env), lis_cl);
944 return LOV_2DISPATCH_MAYLOCK(cl2lov(obj), llo_io_init,
945 !io->ci_ignore_layout, env, obj, io);
949 * An implementation of cl_object_operations::clo_attr_get() method for lov
950 * layer. For raid0 layout this collects and merges attributes of all
953 static int lov_attr_get(const struct lu_env *env, struct cl_object *obj,
954 struct cl_attr *attr)
956 /* do not take lock, as this function is called under a
957 * spin-lock. Layout is protected from changing by ongoing IO. */
958 return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_getattr, env, obj, attr);
961 static int lov_attr_update(const struct lu_env *env, struct cl_object *obj,
962 const struct cl_attr *attr, unsigned valid)
965 * No dispatch is required here, as no layout implements this.
970 int lov_lock_init(const struct lu_env *env, struct cl_object *obj,
971 struct cl_lock *lock, const struct cl_io *io)
973 /* No need to lock because we've taken one refcount of layout. */
974 return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_lock_init, env, obj, lock,
978 static int lov_object_getstripe(const struct lu_env *env, struct cl_object *obj,
979 struct lov_user_md __user *lum)
981 struct lov_object *lov = cl2lov(obj);
982 struct lov_stripe_md *lsm;
986 lsm = lov_lsm_addref(lov);
990 rc = lov_getstripe(cl2lov(obj), lsm, lum);
991 lov_lsm_put(obj, lsm);
995 static int lov_object_find_cbdata(const struct lu_env *env,
996 struct cl_object *obj, ldlm_iterator_t iter,
1002 /* call cl_object_find_cbdata for sub obj */
1003 rc = LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_find_cbdata,
1004 env, obj, iter, data);
1008 static const struct cl_object_operations lov_ops = {
1009 .coo_page_init = lov_page_init,
1010 .coo_lock_init = lov_lock_init,
1011 .coo_io_init = lov_io_init,
1012 .coo_attr_get = lov_attr_get,
1013 .coo_attr_update = lov_attr_update,
1014 .coo_conf_set = lov_conf_set,
1015 .coo_getstripe = lov_object_getstripe,
1016 .coo_find_cbdata = lov_object_find_cbdata
1019 static const struct lu_object_operations lov_lu_obj_ops = {
1020 .loo_object_init = lov_object_init,
1021 .loo_object_delete = lov_object_delete,
1022 .loo_object_release = NULL,
1023 .loo_object_free = lov_object_free,
1024 .loo_object_print = lov_object_print,
1025 .loo_object_invariant = NULL
1028 struct lu_object *lov_object_alloc(const struct lu_env *env,
1029 const struct lu_object_header *unused,
1030 struct lu_device *dev)
1032 struct lov_object *lov;
1033 struct lu_object *obj;
1036 OBD_SLAB_ALLOC_PTR_GFP(lov, lov_object_kmem, GFP_NOFS);
1039 lu_object_init(obj, NULL, dev);
1040 lov->lo_cl.co_ops = &lov_ops;
1041 lov->lo_type = -1; /* invalid, to catch uninitialized type */
1043 * object io operation vector (cl_object::co_iop) is installed
1044 * later in lov_object_init(), as different vectors are used
1045 * for object with different layouts.
1047 obj->lo_ops = &lov_lu_obj_ops;
1053 struct lov_stripe_md *lov_lsm_addref(struct lov_object *lov)
1055 struct lov_stripe_md *lsm = NULL;
1057 lov_conf_freeze(lov);
1058 if (lov->lo_lsm != NULL) {
1059 lsm = lsm_addref(lov->lo_lsm);
1060 CDEBUG(D_INODE, "lsm %p addref %d/%d by %p.\n",
1061 lsm, atomic_read(&lsm->lsm_refc),
1062 lov->lo_layout_invalid, current);
1068 int lov_read_and_clear_async_rc(struct cl_object *clob)
1070 struct lu_object *luobj;
1074 luobj = lu_object_locate(&cl_object_header(clob)->coh_lu,
1076 if (luobj != NULL) {
1077 struct lov_object *lov = lu2lov(luobj);
1079 lov_conf_freeze(lov);
1080 switch (lov->lo_type) {
1082 struct lov_stripe_md *lsm;
1086 LASSERT(lsm != NULL);
1087 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1088 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
1090 if (lov_oinfo_is_dummy(loi))
1093 if (loi->loi_ar.ar_rc && !rc)
1094 rc = loi->loi_ar.ar_rc;
1095 loi->loi_ar.ar_rc = 0;
1108 EXPORT_SYMBOL(lov_read_and_clear_async_rc);