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,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved
24 * Use is subject to license terms.
26 * Copyright (c) 2012, Intel Corporation.
29 * lustre/lod/lod_object.c
31 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
35 # define EXPORT_SYMTAB
37 #define DEBUG_SUBSYSTEM S_MDS
40 #include <obd_class.h>
41 #include <lustre_ver.h>
42 #include <obd_support.h>
43 #include <lprocfs_status.h>
45 #include <lustre_fid.h>
46 #include <lustre_param.h>
47 #include <lustre_fid.h>
50 #include "lod_internal.h"
52 extern cfs_mem_cache_t *lod_object_kmem;
53 static const struct dt_body_operations lod_body_lnk_ops;
55 static int lod_index_lookup(const struct lu_env *env, struct dt_object *dt,
56 struct dt_rec *rec, const struct dt_key *key,
57 struct lustre_capa *capa)
59 struct dt_object *next = dt_object_child(dt);
60 return next->do_index_ops->dio_lookup(env, next, rec, key, capa);
63 static int lod_declare_index_insert(const struct lu_env *env,
65 const struct dt_rec *rec,
66 const struct dt_key *key,
67 struct thandle *handle)
69 return dt_declare_insert(env, dt_object_child(dt), rec, key, handle);
72 static int lod_index_insert(const struct lu_env *env,
74 const struct dt_rec *rec,
75 const struct dt_key *key,
77 struct lustre_capa *capa,
80 return dt_insert(env, dt_object_child(dt), rec, key, th, capa, ign);
83 static int lod_declare_index_delete(const struct lu_env *env,
85 const struct dt_key *key,
88 return dt_declare_delete(env, dt_object_child(dt), key, th);
91 static int lod_index_delete(const struct lu_env *env,
93 const struct dt_key *key,
95 struct lustre_capa *capa)
97 return dt_delete(env, dt_object_child(dt), key, th, capa);
100 static struct dt_it *lod_it_init(const struct lu_env *env,
101 struct dt_object *dt, __u32 attr,
102 struct lustre_capa *capa)
104 struct dt_object *next = dt_object_child(dt);
105 struct lod_it *it = &lod_env_info(env)->lti_it;
106 struct dt_it *it_next;
109 it_next = next->do_index_ops->dio_it.init(env, next, attr, capa);
113 /* currently we do not use more than one iterator per thread
114 * so we store it in thread info. if at some point we need
115 * more active iterators in a single thread, we can allocate
117 LASSERT(it->lit_obj == NULL);
119 it->lit_it = it_next;
122 return (struct dt_it *)it;
125 #define LOD_CHECK_IT(env, it) \
127 LASSERT((it)->lit_obj != NULL); \
128 LASSERT((it)->lit_it != NULL); \
131 void lod_it_fini(const struct lu_env *env, struct dt_it *di)
133 struct lod_it *it = (struct lod_it *)di;
135 LOD_CHECK_IT(env, it);
136 it->lit_obj->do_index_ops->dio_it.fini(env, it->lit_it);
138 /* the iterator not in use any more */
143 int lod_it_get(const struct lu_env *env, struct dt_it *di,
144 const struct dt_key *key)
146 const struct lod_it *it = (const struct lod_it *)di;
148 LOD_CHECK_IT(env, it);
149 return it->lit_obj->do_index_ops->dio_it.get(env, it->lit_it, key);
152 void lod_it_put(const struct lu_env *env, struct dt_it *di)
154 struct lod_it *it = (struct lod_it *)di;
156 LOD_CHECK_IT(env, it);
157 return it->lit_obj->do_index_ops->dio_it.put(env, it->lit_it);
160 int lod_it_next(const struct lu_env *env, struct dt_it *di)
162 struct lod_it *it = (struct lod_it *)di;
164 LOD_CHECK_IT(env, it);
165 return it->lit_obj->do_index_ops->dio_it.next(env, it->lit_it);
168 struct dt_key *lod_it_key(const struct lu_env *env, const struct dt_it *di)
170 const struct lod_it *it = (const struct lod_it *)di;
172 LOD_CHECK_IT(env, it);
173 return it->lit_obj->do_index_ops->dio_it.key(env, it->lit_it);
176 int lod_it_key_size(const struct lu_env *env, const struct dt_it *di)
178 struct lod_it *it = (struct lod_it *)di;
180 LOD_CHECK_IT(env, it);
181 return it->lit_obj->do_index_ops->dio_it.key_size(env, it->lit_it);
184 int lod_it_rec(const struct lu_env *env, const struct dt_it *di,
185 struct dt_rec *rec, __u32 attr)
187 const struct lod_it *it = (const struct lod_it *)di;
189 LOD_CHECK_IT(env, it);
190 return it->lit_obj->do_index_ops->dio_it.rec(env, it->lit_it, rec, attr);
193 __u64 lod_it_store(const struct lu_env *env, const struct dt_it *di)
195 const struct lod_it *it = (const struct lod_it *)di;
197 LOD_CHECK_IT(env, it);
198 return it->lit_obj->do_index_ops->dio_it.store(env, it->lit_it);
201 int lod_it_load(const struct lu_env *env, const struct dt_it *di, __u64 hash)
203 const struct lod_it *it = (const struct lod_it *)di;
205 LOD_CHECK_IT(env, it);
206 return it->lit_obj->do_index_ops->dio_it.load(env, it->lit_it, hash);
209 int lod_it_key_rec(const struct lu_env *env, const struct dt_it *di,
212 const struct lod_it *it = (const struct lod_it *)di;
214 LOD_CHECK_IT(env, it);
215 return it->lit_obj->do_index_ops->dio_it.key_rec(env, it->lit_it, key_rec);
218 static struct dt_index_operations lod_index_ops = {
219 .dio_lookup = lod_index_lookup,
220 .dio_declare_insert = lod_declare_index_insert,
221 .dio_insert = lod_index_insert,
222 .dio_declare_delete = lod_declare_index_delete,
223 .dio_delete = lod_index_delete,
231 .key_size = lod_it_key_size,
233 .store = lod_it_store,
235 .key_rec = lod_it_key_rec,
239 static void lod_object_read_lock(const struct lu_env *env,
240 struct dt_object *dt, unsigned role)
242 dt_read_lock(env, dt_object_child(dt), role);
245 static void lod_object_write_lock(const struct lu_env *env,
246 struct dt_object *dt, unsigned role)
248 dt_write_lock(env, dt_object_child(dt), role);
251 static void lod_object_read_unlock(const struct lu_env *env,
252 struct dt_object *dt)
254 dt_read_unlock(env, dt_object_child(dt));
257 static void lod_object_write_unlock(const struct lu_env *env,
258 struct dt_object *dt)
260 dt_write_unlock(env, dt_object_child(dt));
263 static int lod_object_write_locked(const struct lu_env *env,
264 struct dt_object *dt)
266 return dt_write_locked(env, dt_object_child(dt));
269 static int lod_attr_get(const struct lu_env *env,
270 struct dt_object *dt,
271 struct lu_attr *attr,
272 struct lustre_capa *capa)
274 return dt_attr_get(env, dt_object_child(dt), attr, capa);
277 static int lod_declare_attr_set(const struct lu_env *env,
278 struct dt_object *dt,
279 const struct lu_attr *attr,
280 struct thandle *handle)
282 struct dt_object *next = dt_object_child(dt);
283 struct lod_object *lo = lod_dt_obj(dt);
288 * declare setattr on the local object
290 rc = dt_declare_attr_set(env, next, attr, handle);
295 * load striping information, notice we don't do this when object
296 * is being initialized as we don't need this information till
297 * few specific cases like destroy, chown
299 rc = lod_load_striping(env, lo);
304 * if object is striped declare changes on the stripes
306 LASSERT(lo->ldo_stripe || lo->ldo_stripenr == 0);
307 for (i = 0; i < lo->ldo_stripenr; i++) {
308 LASSERT(lo->ldo_stripe[i]);
309 rc = dt_declare_attr_set(env, lo->ldo_stripe[i], attr, handle);
311 CERROR("failed declaration: %d\n", rc);
319 static int lod_attr_set(const struct lu_env *env,
320 struct dt_object *dt,
321 const struct lu_attr *attr,
322 struct thandle *handle,
323 struct lustre_capa *capa)
325 struct dt_object *next = dt_object_child(dt);
326 struct lod_object *lo = lod_dt_obj(dt);
331 * apply changes to the local object
333 rc = dt_attr_set(env, next, attr, handle, capa);
338 * if object is striped, apply changes to all the stripes
340 LASSERT(lo->ldo_stripe || lo->ldo_stripenr == 0);
341 for (i = 0; i < lo->ldo_stripenr; i++) {
342 LASSERT(lo->ldo_stripe[i]);
343 rc = dt_attr_set(env, lo->ldo_stripe[i], attr, handle, capa);
345 CERROR("failed declaration: %d\n", rc);
353 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
354 struct lu_buf *buf, const char *name,
355 struct lustre_capa *capa)
357 struct lod_thread_info *info = lod_env_info(env);
358 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
362 rc = dt_xattr_get(env, dt_object_child(dt), buf, name, capa);
363 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
367 * lod returns default striping on the real root of the device
368 * this is like the root stores default striping for the whole
369 * filesystem. historically we've been using a different approach
370 * and store it in the config.
372 dt_root_get(env, dev->lod_child, &info->lti_fid);
373 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
375 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
376 struct lov_user_md *lum = buf->lb_buf;
377 struct lov_desc *desc = &dev->lod_desc;
379 if (buf->lb_buf == NULL) {
380 rc = sizeof(struct lov_user_md_v1);
381 } else if (buf->lb_len >= sizeof(struct lov_user_md_v1)) {
382 lum->lmm_magic = LOV_USER_MAGIC_V1;
383 lum->lmm_oi.oi_seq = FID_SEQ_LOV_DEFAULT;
384 lum->lmm_pattern = desc->ld_pattern;
385 lum->lmm_stripe_size = desc->ld_default_stripe_size;
386 lum->lmm_stripe_count = desc->ld_default_stripe_count;
387 lum->lmm_stripe_offset = desc->ld_default_stripe_offset;
388 rc = sizeof(struct lov_user_md_v1);
398 * LOV xattr is a storage for striping, and LOD owns this xattr.
399 * but LOD allows others to control striping to some extent
401 * - to set new defined striping
402 * - to set new semi-defined striping
403 * - number of stripes is defined
404 * - number of stripes + osts are defined
407 static int lod_declare_xattr_set(const struct lu_env *env,
408 struct dt_object *dt,
409 const struct lu_buf *buf,
410 const char *name, int fl,
413 struct dt_object *next = dt_object_child(dt);
414 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
420 * allow to declare predefined striping on a new (!mode) object
421 * which is supposed to be replay of regular file creation
422 * (when LOV setting is declared)
423 * LU_XATTR_REPLACE is set to indicate a layout swap
425 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
426 if ((S_ISREG(mode) || !mode) && !strcmp(name, XATTR_NAME_LOV) &&
427 !(fl & LU_XATTR_REPLACE)) {
429 * this is a request to manipulate object's striping
431 if (dt_object_exists(dt)) {
432 rc = dt_attr_get(env, next, attr, BYPASS_CAPA);
436 memset(attr, 0, sizeof(attr));
437 attr->la_valid = LA_TYPE | LA_MODE;
438 attr->la_mode = S_IFREG;
440 rc = lod_declare_striped_object(env, dt, attr, buf, th);
445 rc = dt_declare_xattr_set(env, next, buf, name, fl, th);
450 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
451 struct dt_object *dt,
452 const struct lu_buf *buf,
453 const char *name, int fl,
455 struct lustre_capa *capa)
457 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
458 struct dt_object *next = dt_object_child(dt);
459 struct lod_object *l = lod_dt_obj(dt);
460 struct lov_user_md_v1 *lum;
461 struct lov_user_md_v3 *v3 = NULL;
465 LASSERT(l->ldo_stripe == NULL);
466 l->ldo_striping_cached = 0;
467 l->ldo_def_striping_set = 0;
468 lod_object_set_pool(l, NULL);
469 l->ldo_def_stripe_size = 0;
470 l->ldo_def_stripenr = 0;
473 LASSERT(buf->lb_buf);
476 rc = lod_verify_striping(d, buf, 0);
480 if (lum->lmm_magic == LOV_USER_MAGIC_V3)
483 /* if { size, offset, count } = { 0, -1, 0 } and no pool
484 * (i.e. all default values specified) then delete default
485 * striping from dir. */
487 "set default striping: sz %u # %u offset %d %s %s\n",
488 (unsigned)lum->lmm_stripe_size,
489 (unsigned)lum->lmm_stripe_count,
490 (int)lum->lmm_stripe_offset,
491 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
493 if (LOVEA_DELETE_VALUES((lum->lmm_stripe_size),
494 (lum->lmm_stripe_count),
495 (lum->lmm_stripe_offset)) &&
496 lum->lmm_magic == LOV_USER_MAGIC_V1) {
497 rc = dt_xattr_del(env, next, name, th, capa);
501 rc = dt_xattr_set(env, next, buf, name, fl, th, capa);
507 static int lod_xattr_set(const struct lu_env *env,
508 struct dt_object *dt, const struct lu_buf *buf,
509 const char *name, int fl, struct thandle *th,
510 struct lustre_capa *capa)
512 struct dt_object *next = dt_object_child(dt);
517 attr = dt->do_lu.lo_header->loh_attr & S_IFMT;
519 if (strncmp(name, XATTR_NAME_LOV, strlen(XATTR_NAME_LOV)) == 0)
520 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name,
523 rc = dt_xattr_set(env, next, buf, name, fl, th, capa);
525 } else if (S_ISREG(attr) && !strcmp(name, XATTR_NAME_LOV)) {
526 /* in case of lov EA swap, just set it
527 * if not, it is a replay so check striping match what we
528 * already have during req replay, declare_xattr_set()
529 * defines striping, then create() does the work
531 if (fl & LU_XATTR_REPLACE)
532 rc = dt_xattr_set(env, next, buf, name, fl, th, capa);
534 rc = lod_striping_create(env, dt, NULL, NULL, th);
538 * behave transparantly for all other EAs
540 rc = dt_xattr_set(env, next, buf, name, fl, th, capa);
546 static int lod_declare_xattr_del(const struct lu_env *env,
547 struct dt_object *dt, const char *name,
550 return dt_declare_xattr_del(env, dt_object_child(dt), name, th);
553 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
554 const char *name, struct thandle *th,
555 struct lustre_capa *capa)
557 return dt_xattr_del(env, dt_object_child(dt), name, th, capa);
560 static int lod_xattr_list(const struct lu_env *env,
561 struct dt_object *dt, struct lu_buf *buf,
562 struct lustre_capa *capa)
564 return dt_xattr_list(env, dt_object_child(dt), buf, capa);
567 int lod_object_set_pool(struct lod_object *o, char *pool)
572 len = strlen(o->ldo_pool);
573 OBD_FREE(o->ldo_pool, len + 1);
578 OBD_ALLOC(o->ldo_pool, len + 1);
579 if (o->ldo_pool == NULL)
581 strcpy(o->ldo_pool, pool);
586 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
588 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
591 static int lod_cache_parent_striping(const struct lu_env *env,
592 struct lod_object *lp)
594 struct lov_user_md_v1 *v1 = NULL;
595 struct lov_user_md_v3 *v3 = NULL;
599 /* dt_ah_init() is called from MDD without parent being write locked
601 dt_write_lock(env, dt_object_child(&lp->ldo_obj), 0);
602 if (lp->ldo_striping_cached)
603 GOTO(unlock, rc = 0);
605 rc = lod_get_lov_ea(env, lp);
609 if (rc < sizeof(struct lov_user_md)) {
610 /* don't lookup for non-existing or invalid striping */
611 lp->ldo_def_striping_set = 0;
612 lp->ldo_striping_cached = 1;
613 lp->ldo_def_stripe_size = 0;
614 lp->ldo_def_stripenr = 0;
615 lp->ldo_def_stripe_offset = (typeof(v1->lmm_stripe_offset))(-1);
616 GOTO(unlock, rc = 0);
619 v1 = (struct lov_user_md_v1 *)lod_env_info(env)->lti_ea_store;
620 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1))
621 lustre_swab_lov_user_md_v1(v1);
622 else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3))
623 lustre_swab_lov_user_md_v3(v3);
625 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1)
626 GOTO(unlock, rc = 0);
628 if (v1->lmm_pattern != LOV_PATTERN_RAID0 && v1->lmm_pattern != 0)
629 GOTO(unlock, rc = 0);
631 lp->ldo_def_stripenr = v1->lmm_stripe_count;
632 lp->ldo_def_stripe_size = v1->lmm_stripe_size;
633 lp->ldo_def_stripe_offset = v1->lmm_stripe_offset;
634 lp->ldo_striping_cached = 1;
635 lp->ldo_def_striping_set = 1;
637 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
638 /* XXX: sanity check here */
639 v3 = (struct lov_user_md_v3 *) v1;
640 if (v3->lmm_pool_name[0])
641 lod_object_set_pool(lp, v3->lmm_pool_name);
644 CDEBUG(D_OTHER, "def. striping: # %d, sz %d, off %d %s%s on "DFID"\n",
645 lp->ldo_def_stripenr, lp->ldo_def_stripe_size,
646 lp->ldo_def_stripe_offset, v3 ? "from " : "",
647 v3 ? lp->ldo_pool : "", PFID(lu_object_fid(&lp->ldo_obj.do_lu)));
651 dt_write_unlock(env, dt_object_child(&lp->ldo_obj));
656 * used to transfer default striping data to the object being created
658 static void lod_ah_init(const struct lu_env *env,
659 struct dt_allocation_hint *ah,
660 struct dt_object *parent,
661 struct dt_object *child,
662 cfs_umode_t child_mode)
664 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
665 struct dt_object *nextp = NULL;
666 struct dt_object *nextc;
667 struct lod_object *lp = NULL;
668 struct lod_object *lc;
669 struct lov_desc *desc;
674 if (likely(parent)) {
675 nextp = dt_object_child(parent);
676 lp = lod_dt_obj(parent);
679 nextc = dt_object_child(child);
680 lc = lod_dt_obj(child);
682 LASSERT(lc->ldo_stripenr == 0);
683 LASSERT(lc->ldo_stripe == NULL);
686 * local object may want some hints
687 * in case of late striping creation, ->ah_init()
688 * can be called with local object existing
690 if (!dt_object_exists(nextc) || dt_object_remote(nextc))
691 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
693 if (S_ISDIR(child_mode)) {
694 if (lp->ldo_striping_cached == 0) {
695 /* we haven't tried to get default striping for
696 * the directory yet, let's cache it in the object */
697 lod_cache_parent_striping(env, lp);
699 /* transfer defaults to new directory */
700 if (lp->ldo_striping_cached) {
702 lod_object_set_pool(lc, lp->ldo_pool);
703 lc->ldo_def_stripenr = lp->ldo_def_stripenr;
704 lc->ldo_def_stripe_size = lp->ldo_def_stripe_size;
705 lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
706 lc->ldo_striping_cached = 1;
707 lc->ldo_def_striping_set = 1;
708 CDEBUG(D_OTHER, "inherite EA sz:%d off:%d nr:%d\n",
709 (int)lc->ldo_def_stripenr,
710 (int)lc->ldo_def_stripe_size,
711 (int)lc->ldo_def_stripe_offset);
717 * if object is going to be striped over OSTs, transfer default
718 * striping information to the child, so that we can use it
719 * during declaration and creation
721 if (!lod_object_will_be_striped(S_ISREG(child_mode),
722 lu_object_fid(&child->do_lu)))
726 * try from the parent
728 if (likely(parent)) {
729 if (lp->ldo_striping_cached == 0) {
730 /* we haven't tried to get default striping for
731 * the directory yet, let's cache it in the object */
732 lod_cache_parent_striping(env, lp);
735 lc->ldo_def_stripe_offset = (__u16) -1;
737 if (lp->ldo_def_striping_set) {
739 lod_object_set_pool(lc, lp->ldo_pool);
740 lc->ldo_stripenr = lp->ldo_def_stripenr;
741 lc->ldo_stripe_size = lp->ldo_def_stripe_size;
742 lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
743 CDEBUG(D_OTHER, "striping from parent: #%d, sz %d %s\n",
744 lc->ldo_stripenr, lc->ldo_stripe_size,
745 lp->ldo_pool ? lp->ldo_pool : "");
750 * if the parent doesn't provide with specific pattern, grab fs-wide one
753 if (lc->ldo_stripenr == 0)
754 lc->ldo_stripenr = desc->ld_default_stripe_count;
755 if (lc->ldo_stripe_size == 0)
756 lc->ldo_stripe_size = desc->ld_default_stripe_size;
757 CDEBUG(D_OTHER, "final striping: # %d stripes, sz %d from %s\n",
758 lc->ldo_stripenr, lc->ldo_stripe_size,
759 lc->ldo_pool ? lc->ldo_pool : "");
764 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
766 * this function handles a special case when truncate was done
767 * on a stripeless object and now striping is being created
768 * we can't lose that size, so we have to propagate it to newly
771 static int lod_declare_init_size(const struct lu_env *env,
772 struct dt_object *dt, struct thandle *th)
774 struct dt_object *next = dt_object_child(dt);
775 struct lod_object *lo = lod_dt_obj(dt);
776 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
781 /* XXX: we support the simplest (RAID0) striping so far */
782 LASSERT(lo->ldo_stripe || lo->ldo_stripenr == 0);
783 LASSERT(lo->ldo_stripe_size > 0);
785 rc = dt_attr_get(env, next, attr, BYPASS_CAPA);
786 LASSERT(attr->la_valid & LA_SIZE);
790 size = attr->la_size;
794 /* ll_do_div64(a, b) returns a % b, and a = a / b */
795 ll_do_div64(size, (__u64) lo->ldo_stripe_size);
796 stripe = ll_do_div64(size, (__u64) lo->ldo_stripenr);
798 size = size * lo->ldo_stripe_size;
799 offs = attr->la_size;
800 size += ll_do_div64(offs, lo->ldo_stripe_size);
802 attr->la_valid = LA_SIZE;
803 attr->la_size = size;
805 rc = dt_declare_attr_set(env, lo->ldo_stripe[stripe], attr, th);
812 * Create declaration of striped object
814 int lod_declare_striped_object(const struct lu_env *env, struct dt_object *dt,
815 struct lu_attr *attr,
816 const struct lu_buf *lovea, struct thandle *th)
818 struct lod_thread_info *info = lod_env_info(env);
819 struct dt_object *next = dt_object_child(dt);
820 struct lod_object *lo = lod_dt_obj(dt);
824 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO)) {
825 /* failed to create striping, let's reset
826 * config so that others don't get confused */
827 lod_object_free_striping(env, lo);
828 GOTO(out, rc = -ENOMEM);
831 /* choose OST and generate appropriate objects */
832 rc = lod_qos_prep_create(env, lo, attr, lovea, th);
834 /* failed to create striping, let's reset
835 * config so that others don't get confused */
836 lod_object_free_striping(env, lo);
841 * declare storage for striping data
843 info->lti_buf.lb_len = lov_mds_md_size(lo->ldo_stripenr,
844 lo->ldo_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1);
845 rc = dt_declare_xattr_set(env, next, &info->lti_buf, XATTR_NAME_LOV,
851 * if striping is created with local object's size > 0,
852 * we have to propagate this size to specific object
853 * the case is possible only when local object was created previously
855 if (dt_object_exists(next))
856 rc = lod_declare_init_size(env, dt, th);
862 static int lod_declare_object_create(const struct lu_env *env,
863 struct dt_object *dt,
864 struct lu_attr *attr,
865 struct dt_allocation_hint *hint,
866 struct dt_object_format *dof,
869 struct dt_object *next = dt_object_child(dt);
870 struct lod_object *lo = lod_dt_obj(dt);
879 * first of all, we declare creation of local object
881 rc = dt_declare_create(env, next, attr, hint, dof, th);
885 if (dof->dof_type == DFT_SYM)
886 dt->do_body_ops = &lod_body_lnk_ops;
889 * it's lod_ah_init() who has decided the object will striped
891 if (dof->dof_type == DFT_REGULAR) {
892 /* callers don't want stripes */
893 /* XXX: all tricky interactions with ->ah_make_hint() decided
894 * to use striping, then ->declare_create() behaving differently
895 * should be cleaned */
896 if (dof->u.dof_reg.striped == 0)
897 lo->ldo_stripenr = 0;
898 if (lo->ldo_stripenr > 0)
899 rc = lod_declare_striped_object(env, dt, attr,
901 } else if (dof->dof_type == DFT_DIR && lo->ldo_striping_cached) {
902 struct lod_thread_info *info = lod_env_info(env);
904 struct lov_user_md_v3 *v3;
906 if (LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size,
907 lo->ldo_def_stripenr,
908 lo->ldo_def_stripe_offset))
915 v3->lmm_magic = cpu_to_le32(LOV_MAGIC_V3);
916 v3->lmm_pattern = cpu_to_le32(LOV_PATTERN_RAID0);
917 fid_ostid_pack(lu_object_fid(&dt->do_lu), &v3->lmm_oi);
918 ostid_cpu_to_le(&v3->lmm_oi, &v3->lmm_oi);
919 v3->lmm_stripe_size = cpu_to_le32(lo->ldo_def_stripe_size);
920 v3->lmm_stripe_count = cpu_to_le32(lo->ldo_def_stripenr);
921 v3->lmm_stripe_offset = cpu_to_le16(lo->ldo_def_stripe_offset);
923 strncpy(v3->lmm_pool_name, lo->ldo_pool,
926 info->lti_buf.lb_buf = v3;
927 info->lti_buf.lb_len = sizeof(*v3);
929 /* to transfer default striping from the parent */
930 rc = dt_declare_xattr_set(env, next, &info->lti_buf,
931 XATTR_NAME_LOV, 0, th);
939 int lod_striping_create(const struct lu_env *env, struct dt_object *dt,
940 struct lu_attr *attr, struct dt_object_format *dof,
943 struct lod_object *lo = lod_dt_obj(dt);
947 LASSERT(lo->ldo_stripe);
948 LASSERT(lo->ldo_stripenr > 0);
949 LASSERT(lo->ldo_striping_cached == 0);
951 /* create all underlying objects */
952 for (i = 0; i < lo->ldo_stripenr; i++) {
953 LASSERT(lo->ldo_stripe[i]);
954 rc = dt_create(env, lo->ldo_stripe[i], attr, NULL, dof, th);
960 rc = lod_generate_and_set_lovea(env, lo, th);
965 static int lod_object_create(const struct lu_env *env, struct dt_object *dt,
966 struct lu_attr *attr,
967 struct dt_allocation_hint *hint,
968 struct dt_object_format *dof, struct thandle *th)
970 struct dt_object *next = dt_object_child(dt);
971 struct lod_object *lo = lod_dt_obj(dt);
975 /* create local object */
976 rc = dt_create(env, next, attr, hint, dof, th);
979 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
980 rc = lod_store_def_striping(env, dt, th);
981 else if (lo->ldo_stripe)
982 rc = lod_striping_create(env, dt, attr, dof, th);
988 static int lod_declare_object_destroy(const struct lu_env *env,
989 struct dt_object *dt,
992 struct dt_object *next = dt_object_child(dt);
993 struct lod_object *lo = lod_dt_obj(dt);
998 * we declare destroy for the local object
1000 rc = dt_declare_destroy(env, next, th);
1005 * load striping information, notice we don't do this when object
1006 * is being initialized as we don't need this information till
1007 * few specific cases like destroy, chown
1009 rc = lod_load_striping(env, lo);
1013 /* declare destroy for all underlying objects */
1014 for (i = 0; i < lo->ldo_stripenr; i++) {
1015 LASSERT(lo->ldo_stripe[i]);
1016 rc = dt_declare_destroy(env, lo->ldo_stripe[i], th);
1025 static int lod_object_destroy(const struct lu_env *env,
1026 struct dt_object *dt, struct thandle *th)
1028 struct dt_object *next = dt_object_child(dt);
1029 struct lod_object *lo = lod_dt_obj(dt);
1033 /* destroy local object */
1034 rc = dt_destroy(env, next, th);
1038 /* destroy all underlying objects */
1039 for (i = 0; i < lo->ldo_stripenr; i++) {
1040 LASSERT(lo->ldo_stripe[i]);
1041 rc = dt_destroy(env, lo->ldo_stripe[i], th);
1049 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
1050 const struct dt_index_features *feat)
1052 struct dt_object *next = dt_object_child(dt);
1056 LASSERT(next->do_ops);
1057 LASSERT(next->do_ops->do_index_try);
1059 rc = next->do_ops->do_index_try(env, next, feat);
1060 if (next->do_index_ops && dt->do_index_ops == NULL)
1061 dt->do_index_ops = &lod_index_ops;
1066 static int lod_declare_ref_add(const struct lu_env *env,
1067 struct dt_object *dt, struct thandle *th)
1069 return dt_declare_ref_add(env, dt_object_child(dt), th);
1072 static int lod_ref_add(const struct lu_env *env,
1073 struct dt_object *dt, struct thandle *th)
1075 return dt_ref_add(env, dt_object_child(dt), th);
1078 static int lod_declare_ref_del(const struct lu_env *env,
1079 struct dt_object *dt, struct thandle *th)
1081 return dt_declare_ref_del(env, dt_object_child(dt), th);
1084 static int lod_ref_del(const struct lu_env *env,
1085 struct dt_object *dt, struct thandle *th)
1087 return dt_ref_del(env, dt_object_child(dt), th);
1090 static struct obd_capa *lod_capa_get(const struct lu_env *env,
1091 struct dt_object *dt,
1092 struct lustre_capa *old, __u64 opc)
1094 return dt_capa_get(env, dt_object_child(dt), old, opc);
1097 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt)
1099 return dt_object_sync(env, dt_object_child(dt));
1102 struct dt_object_operations lod_obj_ops = {
1103 .do_read_lock = lod_object_read_lock,
1104 .do_write_lock = lod_object_write_lock,
1105 .do_read_unlock = lod_object_read_unlock,
1106 .do_write_unlock = lod_object_write_unlock,
1107 .do_write_locked = lod_object_write_locked,
1108 .do_attr_get = lod_attr_get,
1109 .do_declare_attr_set = lod_declare_attr_set,
1110 .do_attr_set = lod_attr_set,
1111 .do_xattr_get = lod_xattr_get,
1112 .do_declare_xattr_set = lod_declare_xattr_set,
1113 .do_xattr_set = lod_xattr_set,
1114 .do_declare_xattr_del = lod_declare_xattr_del,
1115 .do_xattr_del = lod_xattr_del,
1116 .do_xattr_list = lod_xattr_list,
1117 .do_ah_init = lod_ah_init,
1118 .do_declare_create = lod_declare_object_create,
1119 .do_create = lod_object_create,
1120 .do_declare_destroy = lod_declare_object_destroy,
1121 .do_destroy = lod_object_destroy,
1122 .do_index_try = lod_index_try,
1123 .do_declare_ref_add = lod_declare_ref_add,
1124 .do_ref_add = lod_ref_add,
1125 .do_declare_ref_del = lod_declare_ref_del,
1126 .do_ref_del = lod_ref_del,
1127 .do_capa_get = lod_capa_get,
1128 .do_object_sync = lod_object_sync,
1131 static int lod_object_lock(const struct lu_env *env,
1132 struct dt_object *dt, struct lustre_handle *lh,
1133 struct ldlm_enqueue_info *einfo,
1136 struct dt_object *next = dt_object_child(dt);
1141 * declare setattr on the local object
1143 rc = dt_object_lock(env, next, lh, einfo, policy);
1148 struct dt_lock_operations lod_lock_ops = {
1149 .do_object_lock = lod_object_lock,
1151 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
1152 struct lu_buf *buf, loff_t *pos,
1153 struct lustre_capa *capa)
1155 struct dt_object *next = dt_object_child(dt);
1156 return next->do_body_ops->dbo_read(env, next, buf, pos, capa);
1159 static ssize_t lod_declare_write(const struct lu_env *env,
1160 struct dt_object *dt,
1161 const loff_t size, loff_t pos,
1164 return dt_declare_record_write(env, dt_object_child(dt),
1168 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
1169 const struct lu_buf *buf, loff_t *pos,
1170 struct thandle *th, struct lustre_capa *capa, int iq)
1172 struct dt_object *next = dt_object_child(dt);
1174 return next->do_body_ops->dbo_write(env, next, buf, pos, th, capa, iq);
1177 static const struct dt_body_operations lod_body_lnk_ops = {
1178 .dbo_read = lod_read,
1179 .dbo_declare_write = lod_declare_write,
1180 .dbo_write = lod_write
1183 static int lod_object_init(const struct lu_env *env, struct lu_object *o,
1184 const struct lu_object_conf *conf)
1186 struct lod_device *d = lu2lod_dev(o->lo_dev);
1187 struct lu_object *below;
1188 struct lu_device *under;
1192 * create local object
1194 under = &d->lod_child->dd_lu_dev;
1195 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
1199 lu_object_add(o, below);
1204 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
1208 if (lo->ldo_stripe) {
1209 LASSERT(lo->ldo_stripes_allocated > 0);
1211 for (i = 0; i < lo->ldo_stripenr; i++) {
1212 if (lo->ldo_stripe[i])
1213 lu_object_put(env, &lo->ldo_stripe[i]->do_lu);
1216 i = sizeof(struct dt_object *) * lo->ldo_stripes_allocated;
1217 OBD_FREE(lo->ldo_stripe, i);
1218 lo->ldo_stripe = NULL;
1219 lo->ldo_stripes_allocated = 0;
1221 lo->ldo_stripenr = 0;
1225 * ->start is called once all slices are initialized, including header's
1226 * cache for mode (object type). using the type we can initialize ops
1228 static int lod_object_start(const struct lu_env *env, struct lu_object *o)
1230 if (S_ISLNK(o->lo_header->loh_attr & S_IFMT))
1231 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_lnk_ops;
1235 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
1237 struct lod_object *mo = lu2lod_obj(o);
1240 * release all underlying object pinned
1243 lod_object_free_striping(env, mo);
1245 lod_object_set_pool(mo, NULL);
1248 OBD_SLAB_FREE_PTR(mo, lod_object_kmem);
1251 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
1253 /* XXX: shouldn't we release everything here in case if object
1254 * creation failed before? */
1257 static int lod_object_print(const struct lu_env *env, void *cookie,
1258 lu_printer_t p, const struct lu_object *l)
1260 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
1262 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
1265 struct lu_object_operations lod_lu_obj_ops = {
1266 .loo_object_init = lod_object_init,
1267 .loo_object_start = lod_object_start,
1268 .loo_object_free = lod_object_free,
1269 .loo_object_release = lod_object_release,
1270 .loo_object_print = lod_object_print,
1274 * Init remote lod object
1276 static int lod_robject_init(const struct lu_env *env, struct lu_object *lo,
1277 const struct lu_object_conf *conf)
1279 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
1280 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1281 struct lu_device *c_dev = NULL;
1282 struct lu_object *c_obj;
1287 if (ltd->ltd_tgts_size > 0) {
1288 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
1289 struct lod_tgt_desc *tgt;
1290 tgt = LTD_TGT(ltd, i);
1291 LASSERT(tgt && tgt->ltd_tgt);
1292 if (tgt->ltd_index ==
1293 lu2lod_obj(lo)->ldo_mds_num) {
1294 c_dev = &(tgt->ltd_tgt->dd_lu_dev);
1299 lod_putref(lod, ltd);
1301 if (unlikely(c_dev == NULL))
1304 c_obj = c_dev->ld_ops->ldo_object_alloc(env, lo->lo_header, c_dev);
1305 if (unlikely(c_obj == NULL))
1308 lu_object_add(lo, c_obj);
1313 struct lu_object_operations lod_lu_robj_ops = {
1314 .loo_object_init = lod_robject_init,
1315 .loo_object_start = lod_object_start,
1316 .loo_object_free = lod_object_free,
1317 .loo_object_release = lod_object_release,
1318 .loo_object_print = lod_object_print,