1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
30 * Use is subject to license terms.
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>
41 #define DEBUG_SUBSYSTEM S_LOV
43 #include "lov_cl_internal.h"
49 /*****************************************************************************
55 struct lov_layout_operations {
56 int (*llo_init)(const struct lu_env *env, struct lov_device *dev,
57 struct lov_object *lov,
58 const struct cl_object_conf *conf,
59 union lov_layout_state *state);
60 void (*llo_delete)(const struct lu_env *env, struct lov_object *lov,
61 union lov_layout_state *state);
62 void (*llo_fini)(const struct lu_env *env, struct lov_object *lov,
63 union lov_layout_state *state);
64 void (*llo_install)(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 struct cl_page *(*llo_page_init)(const struct lu_env *env,
69 struct cl_object *obj,
72 int (*llo_lock_init)(const struct lu_env *env,
73 struct cl_object *obj, struct cl_lock *lock,
74 const struct cl_io *io);
75 int (*llo_io_init)(const struct lu_env *env,
76 struct cl_object *obj, struct cl_io *io);
77 int (*llo_getattr)(const struct lu_env *env, struct cl_object *obj,
78 struct cl_attr *attr);
81 /*****************************************************************************
83 * Lov object layout operations.
87 static void lov_install_empty(const struct lu_env *env,
88 struct lov_object *lov,
89 union lov_layout_state *state)
92 * File without objects.
96 static int lov_init_empty(const struct lu_env *env,
97 struct lov_device *dev, struct lov_object *lov,
98 const struct cl_object_conf *conf,
99 union lov_layout_state *state)
104 static void lov_install_raid0(const struct lu_env *env,
105 struct lov_object *lov,
106 union lov_layout_state *state)
111 static struct cl_object *lov_sub_find(const struct lu_env *env,
112 struct cl_device *dev,
113 const struct lu_fid *fid,
114 const struct cl_object_conf *conf)
119 o = lu_object_find_at(env, cl2lu_dev(dev), fid, &conf->coc_lu);
120 LASSERT(ergo(!IS_ERR(o), o->lo_dev->ld_type == &lovsub_device_type));
124 static int lov_init_sub(const struct lu_env *env, struct lov_object *lov,
125 struct cl_object *stripe,
126 struct lov_layout_raid0 *r0, int idx)
128 struct cl_object_header *hdr;
129 struct cl_object_header *subhdr;
130 struct cl_object_header *parent;
131 struct lov_oinfo *oinfo;
134 hdr = cl_object_header(lov2cl(lov));
135 subhdr = cl_object_header(stripe);
136 parent = subhdr->coh_parent;
138 oinfo = r0->lo_lsm->lsm_oinfo[idx];
139 CDEBUG(D_INODE, DFID"@%p[%d] -> "DFID"@%p: id: "LPU64" seq: "LPU64
140 " idx: %d gen: %d\n",
141 PFID(&subhdr->coh_lu.loh_fid), subhdr, idx,
142 PFID(&hdr->coh_lu.loh_fid), hdr,
143 oinfo->loi_id, oinfo->loi_seq,
144 oinfo->loi_ost_idx, oinfo->loi_ost_gen);
146 if (parent == NULL) {
147 subhdr->coh_parent = hdr;
148 subhdr->coh_nesting = hdr->coh_nesting + 1;
149 lu_object_ref_add(&stripe->co_lu, "lov-parent", lov);
150 r0->lo_sub[idx] = cl2lovsub(stripe);
151 r0->lo_sub[idx]->lso_super = lov;
152 r0->lo_sub[idx]->lso_index = idx;
155 CERROR("Stripe is already owned by other file (%i).\n", idx);
156 LU_OBJECT_DEBUG(D_ERROR, env, &stripe->co_lu, "\n");
157 LU_OBJECT_DEBUG(D_ERROR, env, lu_object_top(&parent->coh_lu),
159 LU_OBJECT_HEADER(D_ERROR, env, lov2lu(lov), "new\n");
160 cl_object_put(env, stripe);
166 static int lov_init_raid0(const struct lu_env *env,
167 struct lov_device *dev, struct lov_object *lov,
168 const struct cl_object_conf *conf,
169 union lov_layout_state *state)
174 struct cl_object *stripe;
175 struct lov_thread_info *lti = lov_env_info(env);
176 struct cl_object_conf *subconf = <i->lti_stripe_conf;
177 struct lov_stripe_md *lsm = conf->u.coc_md->lsm;
178 struct lu_fid *ofid = <i->lti_fid;
179 struct lov_layout_raid0 *r0 = &state->raid0;
182 r0->lo_nr = conf->u.coc_md->lsm->lsm_stripe_count;
183 r0->lo_lsm = conf->u.coc_md->lsm;
184 LASSERT(r0->lo_nr <= lov_targets_nr(dev));
186 OBD_ALLOC(r0->lo_sub, r0->lo_nr * sizeof r0->lo_sub[0]);
187 if (r0->lo_sub != NULL) {
189 subconf->coc_inode = conf->coc_inode;
190 cfs_spin_lock_init(&r0->lo_sub_lock);
192 * Create stripe cl_objects.
194 for (i = 0; i < r0->lo_nr && result == 0; ++i) {
195 struct cl_device *subdev;
196 struct lov_oinfo *oinfo = lsm->lsm_oinfo[i];
197 int ost_idx = oinfo->loi_ost_idx;
199 fid_ostid_unpack(ofid, &oinfo->loi_oi,
201 subdev = lovsub2cl_dev(dev->ld_target[ost_idx]);
202 subconf->u.coc_oinfo = oinfo;
203 LASSERTF(subdev != NULL, "not init ost %d\n", ost_idx);
204 stripe = lov_sub_find(env, subdev, ofid, subconf);
206 result = lov_init_sub(env, lov, stripe, r0, i);
208 result = PTR_ERR(stripe);
215 static void lov_delete_empty(const struct lu_env *env, struct lov_object *lov,
216 union lov_layout_state *state)
218 LASSERT(lov->lo_type == LLT_EMPTY);
221 static void lov_subobject_kill(const struct lu_env *env, struct lov_object *lov,
222 struct lovsub_object *los, int idx)
224 struct cl_object *sub;
225 struct lov_layout_raid0 *r0;
226 struct lu_site *site;
227 cfs_waitlink_t *waiter;
230 LASSERT(r0->lo_sub[idx] == los);
232 sub = lovsub2cl(los);
233 site = sub->co_lu.lo_dev->ld_site;
235 cl_object_kill(env, sub);
236 /* release a reference to the sub-object and ... */
237 lu_object_ref_del(&sub->co_lu, "lov-parent", lov);
238 cl_object_put(env, sub);
240 /* ... wait until it is actually destroyed---sub-object clears its
241 * ->lo_sub[] slot in lovsub_object_fini() */
242 if (r0->lo_sub[idx] == los) {
243 waiter = &lov_env_info(env)->lti_waiter;
244 cfs_waitlink_init(waiter);
245 cfs_waitq_add(&site->ls_marche_funebre, waiter);
246 cfs_set_current_state(CFS_TASK_UNINT);
248 /* this wait-queue is signaled at the end of
249 * lu_object_free(). */
250 cfs_set_current_state(CFS_TASK_UNINT);
251 cfs_spin_lock(&r0->lo_sub_lock);
252 if (r0->lo_sub[idx] == los) {
253 cfs_spin_unlock(&r0->lo_sub_lock);
254 cfs_waitq_wait(waiter, CFS_TASK_UNINT);
256 cfs_spin_unlock(&r0->lo_sub_lock);
257 cfs_set_current_state(CFS_TASK_RUNNING);
261 cfs_waitq_del(&site->ls_marche_funebre, waiter);
263 LASSERT(r0->lo_sub[idx] == NULL);
266 static void lov_delete_raid0(const struct lu_env *env, struct lov_object *lov,
267 union lov_layout_state *state)
269 struct lov_layout_raid0 *r0 = &state->raid0;
273 if (r0->lo_sub != NULL) {
274 for (i = 0; i < r0->lo_nr; ++i) {
275 struct lovsub_object *los = r0->lo_sub[i];
279 * If top-level object is to be evicted from
280 * the cache, so are its sub-objects.
282 lov_subobject_kill(env, lov, los, i);
288 static void lov_fini_empty(const struct lu_env *env, struct lov_object *lov,
289 union lov_layout_state *state)
291 LASSERT(lov->lo_type == LLT_EMPTY);
294 static void lov_fini_raid0(const struct lu_env *env, struct lov_object *lov,
295 union lov_layout_state *state)
297 struct lov_layout_raid0 *r0 = &state->raid0;
300 if (r0->lo_sub != NULL) {
301 OBD_FREE(r0->lo_sub, r0->lo_nr * sizeof r0->lo_sub[0]);
307 static int lov_print_empty(const struct lu_env *env, void *cookie,
308 lu_printer_t p, const struct lu_object *o)
310 (*p)(env, cookie, "empty\n");
314 static int lov_print_raid0(const struct lu_env *env, void *cookie,
315 lu_printer_t p, const struct lu_object *o)
317 struct lov_object *lov = lu2lov(o);
318 struct lov_layout_raid0 *r0 = lov_r0(lov);
321 (*p)(env, cookie, "stripes: %d:\n", r0->lo_nr);
322 for (i = 0; i < r0->lo_nr; ++i) {
323 struct lu_object *sub;
325 if (r0->lo_sub[i] != NULL) {
326 sub = lovsub2lu(r0->lo_sub[i]);
327 lu_object_print(env, cookie, p, sub);
329 (*p)(env, cookie, "sub %d absent\n", i);
335 * Implements cl_object_operations::coo_attr_get() method for an object
336 * without stripes (LLT_EMPTY layout type).
338 * The only attributes this layer is authoritative in this case is
339 * cl_attr::cat_blocks---it's 0.
341 static int lov_attr_get_empty(const struct lu_env *env, struct cl_object *obj,
342 struct cl_attr *attr)
344 attr->cat_blocks = 0;
348 static int lov_attr_get_raid0(const struct lu_env *env, struct cl_object *obj,
349 struct cl_attr *attr)
351 struct lov_object *lov = cl2lov(obj);
352 struct lov_layout_raid0 *r0 = lov_r0(lov);
353 struct lov_stripe_md *lsm = lov->u.raid0.lo_lsm;
354 struct ost_lvb *lvb = &lov_env_info(env)->lti_lvb;
359 if (!r0->lo_attr_valid) {
361 * Fill LVB with attributes already initialized by the upper
364 cl_attr2lvb(lvb, attr);
368 * XXX that should be replaced with a loop over sub-objects,
369 * doing cl_object_attr_get() on them. But for now, let's
370 * reuse old lov code.
374 * XXX take lsm spin-lock to keep lov_merge_lvb_kms()
375 * happy. It's not needed, because new code uses
376 * ->coh_attr_guard spin-lock to protect consistency of
377 * sub-object attributes.
379 lov_stripe_lock(lsm);
380 result = lov_merge_lvb_kms(lsm, lvb, &kms);
381 lov_stripe_unlock(lsm);
383 cl_lvb2attr(attr, lvb);
385 r0->lo_attr_valid = 1;
393 const static struct lov_layout_operations lov_dispatch[] = {
395 .llo_init = lov_init_empty,
396 .llo_delete = lov_delete_empty,
397 .llo_fini = lov_fini_empty,
398 .llo_install = lov_install_empty,
399 .llo_print = lov_print_empty,
400 .llo_page_init = lov_page_init_empty,
401 .llo_lock_init = NULL,
402 .llo_io_init = lov_io_init_empty,
403 .llo_getattr = lov_attr_get_empty
406 .llo_init = lov_init_raid0,
407 .llo_delete = lov_delete_raid0,
408 .llo_fini = lov_fini_raid0,
409 .llo_install = lov_install_raid0,
410 .llo_print = lov_print_raid0,
411 .llo_page_init = lov_page_init_raid0,
412 .llo_lock_init = lov_lock_init_raid0,
413 .llo_io_init = lov_io_init_raid0,
414 .llo_getattr = lov_attr_get_raid0
420 * Performs a double-dispatch based on the layout type of an object.
422 #define LOV_2DISPATCH_NOLOCK(obj, op, ...) \
424 struct lov_object *__obj = (obj); \
425 enum lov_layout_type __llt; \
427 __llt = __obj->lo_type; \
428 LASSERT(0 <= __llt && __llt < ARRAY_SIZE(lov_dispatch)); \
429 lov_dispatch[__llt].op(__VA_ARGS__); \
432 #define LOV_2DISPATCH_MAYLOCK(obj, op, lock, ...) \
434 struct lov_object *__obj = (obj); \
435 int __lock = !!(lock); \
436 typeof(lov_dispatch[0].op(__VA_ARGS__)) __result; \
438 __lock &= __obj->lo_owner != cfs_current(); \
440 cfs_down_read(&__obj->lo_type_guard); \
441 __result = LOV_2DISPATCH_NOLOCK(obj, op, __VA_ARGS__); \
443 cfs_up_read(&__obj->lo_type_guard); \
448 * Performs a locked double-dispatch based on the layout type of an object.
450 #define LOV_2DISPATCH(obj, op, ...) \
451 LOV_2DISPATCH_MAYLOCK(obj, op, 1, __VA_ARGS__)
453 #define LOV_2DISPATCH_VOID(obj, op, ...) \
455 struct lov_object *__obj = (obj); \
456 enum lov_layout_type __llt; \
458 if (__obj->lo_owner != cfs_current()) \
459 cfs_down_read(&__obj->lo_type_guard); \
460 __llt = __obj->lo_type; \
461 LASSERT(0 <= __llt && __llt < ARRAY_SIZE(lov_dispatch)); \
462 lov_dispatch[__llt].op(__VA_ARGS__); \
463 if (__obj->lo_owner != cfs_current()) \
464 cfs_up_read(&__obj->lo_type_guard); \
467 static int lov_layout_change(const struct lu_env *env,
468 struct lov_object *obj, enum lov_layout_type llt,
469 const struct cl_object_conf *conf)
472 union lov_layout_state *state = &lov_env_info(env)->lti_state;
473 const struct lov_layout_operations *old_ops;
474 const struct lov_layout_operations *new_ops;
476 LASSERT(0 <= obj->lo_type && obj->lo_type < ARRAY_SIZE(lov_dispatch));
477 LASSERT(0 <= llt && llt < ARRAY_SIZE(lov_dispatch));
480 old_ops = &lov_dispatch[obj->lo_type];
481 new_ops = &lov_dispatch[llt];
483 result = new_ops->llo_init(env, lu2lov_dev(obj->lo_cl.co_lu.lo_dev),
486 struct cl_object_header *hdr = cl_object_header(&obj->lo_cl);
488 struct lu_env *nested;
491 cookie = cl_env_reenter();
492 nested = cl_env_get(&refcheck);
494 cl_object_prune(nested, &obj->lo_cl);
496 result = PTR_ERR(nested);
497 cl_env_put(nested, &refcheck);
498 cl_env_reexit(cookie);
500 old_ops->llo_fini(env, obj, &obj->u);
501 LASSERT(cfs_list_empty(&hdr->coh_locks));
502 LASSERT(hdr->coh_tree.rnode == NULL);
503 LASSERT(hdr->coh_pages == 0);
505 new_ops->llo_install(env, obj, state);
508 new_ops->llo_fini(env, obj, state);
512 /*****************************************************************************
514 * Lov object operations.
518 int lov_object_init(const struct lu_env *env, struct lu_object *obj,
519 const struct lu_object_conf *conf)
521 struct lov_device *dev = lu2lov_dev(obj->lo_dev);
522 struct lov_object *lov = lu2lov(obj);
523 const struct cl_object_conf *cconf = lu2cl_conf(conf);
524 union lov_layout_state *set = &lov_env_info(env)->lti_state;
525 const struct lov_layout_operations *ops;
529 cfs_init_rwsem(&lov->lo_type_guard);
531 /* no locking is necessary, as object is being created */
532 lov->lo_type = cconf->u.coc_md->lsm != NULL ? LLT_RAID0 : LLT_EMPTY;
533 ops = &lov_dispatch[lov->lo_type];
534 result = ops->llo_init(env, dev, lov, cconf, set);
536 ops->llo_install(env, lov, set);
538 ops->llo_fini(env, lov, set);
542 static int lov_conf_set(const struct lu_env *env, struct cl_object *obj,
543 const struct cl_object_conf *conf)
545 struct lov_object *lov = cl2lov(obj);
550 * Currently only LLT_EMPTY -> LLT_RAID0 transition is supported.
552 LASSERT(lov->lo_owner != cfs_current());
553 cfs_down_write(&lov->lo_type_guard);
554 LASSERT(lov->lo_owner == NULL);
555 lov->lo_owner = cfs_current();
556 if (lov->lo_type == LLT_EMPTY && conf->u.coc_md->lsm != NULL)
557 result = lov_layout_change(env, lov, LLT_RAID0, conf);
559 result = -EOPNOTSUPP;
560 lov->lo_owner = NULL;
561 cfs_up_write(&lov->lo_type_guard);
565 static void lov_object_delete(const struct lu_env *env, struct lu_object *obj)
567 struct lov_object *lov = lu2lov(obj);
570 LOV_2DISPATCH_VOID(lov, llo_delete, env, lov, &lov->u);
574 static void lov_object_free(const struct lu_env *env, struct lu_object *obj)
576 struct lov_object *lov = lu2lov(obj);
579 LOV_2DISPATCH_VOID(lov, llo_fini, env, lov, &lov->u);
581 OBD_SLAB_FREE_PTR(lov, lov_object_kmem);
585 static int lov_object_print(const struct lu_env *env, void *cookie,
586 lu_printer_t p, const struct lu_object *o)
588 return LOV_2DISPATCH(lu2lov(o), llo_print, env, cookie, p, o);
591 struct cl_page *lov_page_init(const struct lu_env *env, struct cl_object *obj,
592 struct cl_page *page, cfs_page_t *vmpage)
594 return LOV_2DISPATCH(cl2lov(obj),
595 llo_page_init, env, obj, page, vmpage);
599 * Implements cl_object_operations::clo_io_init() method for lov
600 * layer. Dispatches to the appropriate layout io initialization method.
602 int lov_io_init(const struct lu_env *env, struct cl_object *obj,
605 CL_IO_SLICE_CLEAN(lov_env_io(env), lis_cl);
607 * Do not take lock in case of CIT_MISC io, because
609 * - if this is an io for a glimpse, then we don't care;
611 * - if this not a glimpse (writepage or lock cancellation), then
612 * layout change cannot happen because a page or a lock
615 * - lock ordering (lock mutex nests within layout rw-semaphore)
616 * is obeyed in case of lock cancellation.
618 return LOV_2DISPATCH_MAYLOCK(cl2lov(obj), llo_io_init,
619 io->ci_type != CIT_MISC, env, obj, io);
623 * An implementation of cl_object_operations::clo_attr_get() method for lov
624 * layer. For raid0 layout this collects and merges attributes of all
627 static int lov_attr_get(const struct lu_env *env, struct cl_object *obj,
628 struct cl_attr *attr)
630 /* do not take lock, as this function is called under a
631 * spin-lock. Layout is protected from changing by ongoing IO. */
632 return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_getattr, env, obj, attr);
635 static int lov_attr_set(const struct lu_env *env, struct cl_object *obj,
636 const struct cl_attr *attr, unsigned valid)
639 * No dispatch is required here, as no layout implements this.
644 int lov_lock_init(const struct lu_env *env, struct cl_object *obj,
645 struct cl_lock *lock, const struct cl_io *io)
647 return LOV_2DISPATCH(cl2lov(obj), llo_lock_init, env, obj, lock, io);
650 static const struct cl_object_operations lov_ops = {
651 .coo_page_init = lov_page_init,
652 .coo_lock_init = lov_lock_init,
653 .coo_io_init = lov_io_init,
654 .coo_attr_get = lov_attr_get,
655 .coo_attr_set = lov_attr_set,
656 .coo_conf_set = lov_conf_set
659 static const struct lu_object_operations lov_lu_obj_ops = {
660 .loo_object_init = lov_object_init,
661 .loo_object_delete = lov_object_delete,
662 .loo_object_release = NULL,
663 .loo_object_free = lov_object_free,
664 .loo_object_print = lov_object_print,
665 .loo_object_invariant = NULL
668 struct lu_object *lov_object_alloc(const struct lu_env *env,
669 const struct lu_object_header *unused,
670 struct lu_device *dev)
672 struct lov_object *lov;
673 struct lu_object *obj;
676 OBD_SLAB_ALLOC_PTR_GFP(lov, lov_object_kmem, CFS_ALLOC_IO);
679 lu_object_init(obj, NULL, dev);
680 lov->lo_cl.co_ops = &lov_ops;
681 lov->lo_type = -1; /* invalid, to catch uninitialized type */
683 * object io operation vector (cl_object::co_iop) is installed
684 * later in lov_object_init(), as different vectors are used
685 * for object with different layouts.
687 obj->lo_ops = &lov_lu_obj_ops;