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);
294 /* osp_declare_attr_set() ignores all attributes other than
295 * UID, GID, and size, and osp_attr_set() ignores all but UID
296 * and GID. Declaration of size attr setting happens through
297 * lod_declare_init_size(), and not through this function.
298 * Therefore we need not load striping unless ownership is
299 * changing. This should save memory and (we hope) speed up
301 if (!(attr->la_valid & (LA_UID | LA_GID)))
305 * load striping information, notice we don't do this when object
306 * is being initialized as we don't need this information till
307 * few specific cases like destroy, chown
309 rc = lod_load_striping(env, lo);
314 * if object is striped declare changes on the stripes
316 LASSERT(lo->ldo_stripe || lo->ldo_stripenr == 0);
317 for (i = 0; i < lo->ldo_stripenr; i++) {
318 LASSERT(lo->ldo_stripe[i]);
319 rc = dt_declare_attr_set(env, lo->ldo_stripe[i], attr, handle);
321 CERROR("failed declaration: %d\n", rc);
329 static int lod_attr_set(const struct lu_env *env,
330 struct dt_object *dt,
331 const struct lu_attr *attr,
332 struct thandle *handle,
333 struct lustre_capa *capa)
335 struct dt_object *next = dt_object_child(dt);
336 struct lod_object *lo = lod_dt_obj(dt);
341 * apply changes to the local object
343 rc = dt_attr_set(env, next, attr, handle, capa);
347 if (!(attr->la_valid & (LA_UID | LA_GID)))
351 * if object is striped, apply changes to all the stripes
353 LASSERT(lo->ldo_stripe || lo->ldo_stripenr == 0);
354 for (i = 0; i < lo->ldo_stripenr; i++) {
355 LASSERT(lo->ldo_stripe[i]);
356 rc = dt_attr_set(env, lo->ldo_stripe[i], attr, handle, capa);
358 CERROR("failed declaration: %d\n", rc);
366 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
367 struct lu_buf *buf, const char *name,
368 struct lustre_capa *capa)
370 struct lod_thread_info *info = lod_env_info(env);
371 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
375 rc = dt_xattr_get(env, dt_object_child(dt), buf, name, capa);
376 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
380 * lod returns default striping on the real root of the device
381 * this is like the root stores default striping for the whole
382 * filesystem. historically we've been using a different approach
383 * and store it in the config.
385 dt_root_get(env, dev->lod_child, &info->lti_fid);
386 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
388 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
389 struct lov_user_md *lum = buf->lb_buf;
390 struct lov_desc *desc = &dev->lod_desc;
392 if (buf->lb_buf == NULL) {
393 rc = sizeof(struct lov_user_md_v1);
394 } else if (buf->lb_len >= sizeof(struct lov_user_md_v1)) {
395 lum->lmm_magic = LOV_USER_MAGIC_V1;
396 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
397 lum->lmm_pattern = desc->ld_pattern;
398 lum->lmm_stripe_size = desc->ld_default_stripe_size;
399 lum->lmm_stripe_count = desc->ld_default_stripe_count;
400 lum->lmm_stripe_offset = desc->ld_default_stripe_offset;
401 rc = sizeof(struct lov_user_md_v1);
411 * LOV xattr is a storage for striping, and LOD owns this xattr.
412 * but LOD allows others to control striping to some extent
414 * - to set new defined striping
415 * - to set new semi-defined striping
416 * - number of stripes is defined
417 * - number of stripes + osts are defined
420 static int lod_declare_xattr_set(const struct lu_env *env,
421 struct dt_object *dt,
422 const struct lu_buf *buf,
423 const char *name, int fl,
426 struct dt_object *next = dt_object_child(dt);
427 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
433 * allow to declare predefined striping on a new (!mode) object
434 * which is supposed to be replay of regular file creation
435 * (when LOV setting is declared)
436 * LU_XATTR_REPLACE is set to indicate a layout swap
438 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
439 if ((S_ISREG(mode) || !mode) && !strcmp(name, XATTR_NAME_LOV) &&
440 !(fl & LU_XATTR_REPLACE)) {
442 * this is a request to manipulate object's striping
444 if (dt_object_exists(dt)) {
445 rc = dt_attr_get(env, next, attr, BYPASS_CAPA);
449 memset(attr, 0, sizeof(*attr));
450 attr->la_valid = LA_TYPE | LA_MODE;
451 attr->la_mode = S_IFREG;
453 rc = lod_declare_striped_object(env, dt, attr, buf, th);
458 rc = dt_declare_xattr_set(env, next, buf, name, fl, th);
463 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
464 struct dt_object *dt,
465 const struct lu_buf *buf,
466 const char *name, int fl,
468 struct lustre_capa *capa)
470 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
471 struct dt_object *next = dt_object_child(dt);
472 struct lod_object *l = lod_dt_obj(dt);
473 struct lov_user_md_v1 *lum;
474 struct lov_user_md_v3 *v3 = NULL;
478 LASSERT(l->ldo_stripe == NULL);
479 l->ldo_striping_cached = 0;
480 l->ldo_def_striping_set = 0;
481 lod_object_set_pool(l, NULL);
482 l->ldo_def_stripe_size = 0;
483 l->ldo_def_stripenr = 0;
486 LASSERT(buf->lb_buf);
489 rc = lod_verify_striping(d, buf, 0);
493 if (lum->lmm_magic == LOV_USER_MAGIC_V3)
496 /* if { size, offset, count } = { 0, -1, 0 } and no pool
497 * (i.e. all default values specified) then delete default
498 * striping from dir. */
500 "set default striping: sz %u # %u offset %d %s %s\n",
501 (unsigned)lum->lmm_stripe_size,
502 (unsigned)lum->lmm_stripe_count,
503 (int)lum->lmm_stripe_offset,
504 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
506 if (LOVEA_DELETE_VALUES((lum->lmm_stripe_size),
507 (lum->lmm_stripe_count),
508 (lum->lmm_stripe_offset)) &&
509 lum->lmm_magic == LOV_USER_MAGIC_V1) {
510 rc = dt_xattr_del(env, next, name, th, capa);
514 rc = dt_xattr_set(env, next, buf, name, fl, th, capa);
520 static int lod_xattr_set(const struct lu_env *env,
521 struct dt_object *dt, const struct lu_buf *buf,
522 const char *name, int fl, struct thandle *th,
523 struct lustre_capa *capa)
525 struct dt_object *next = dt_object_child(dt);
530 attr = dt->do_lu.lo_header->loh_attr & S_IFMT;
532 if (strncmp(name, XATTR_NAME_LOV, strlen(XATTR_NAME_LOV)) == 0)
533 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name,
536 rc = dt_xattr_set(env, next, buf, name, fl, th, capa);
538 } else if (S_ISREG(attr) && !strcmp(name, XATTR_NAME_LOV)) {
539 /* in case of lov EA swap, just set it
540 * if not, it is a replay so check striping match what we
541 * already have during req replay, declare_xattr_set()
542 * defines striping, then create() does the work
544 if (fl & LU_XATTR_REPLACE) {
545 /* free stripes, then update disk */
546 lod_object_free_striping(env, lod_dt_obj(dt));
547 rc = dt_xattr_set(env, next, buf, name, fl, th, capa);
549 rc = lod_striping_create(env, dt, NULL, NULL, th);
554 * behave transparantly for all other EAs
556 rc = dt_xattr_set(env, next, buf, name, fl, th, capa);
562 static int lod_declare_xattr_del(const struct lu_env *env,
563 struct dt_object *dt, const char *name,
566 return dt_declare_xattr_del(env, dt_object_child(dt), name, th);
569 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
570 const char *name, struct thandle *th,
571 struct lustre_capa *capa)
573 if (!strcmp(name, XATTR_NAME_LOV))
574 lod_object_free_striping(env, lod_dt_obj(dt));
575 return dt_xattr_del(env, dt_object_child(dt), name, th, capa);
578 static int lod_xattr_list(const struct lu_env *env,
579 struct dt_object *dt, struct lu_buf *buf,
580 struct lustre_capa *capa)
582 return dt_xattr_list(env, dt_object_child(dt), buf, capa);
585 int lod_object_set_pool(struct lod_object *o, char *pool)
590 len = strlen(o->ldo_pool);
591 OBD_FREE(o->ldo_pool, len + 1);
596 OBD_ALLOC(o->ldo_pool, len + 1);
597 if (o->ldo_pool == NULL)
599 strcpy(o->ldo_pool, pool);
604 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
606 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
609 static int lod_cache_parent_striping(const struct lu_env *env,
610 struct lod_object *lp)
612 struct lov_user_md_v1 *v1 = NULL;
613 struct lov_user_md_v3 *v3 = NULL;
617 /* dt_ah_init() is called from MDD without parent being write locked
619 dt_write_lock(env, dt_object_child(&lp->ldo_obj), 0);
620 if (lp->ldo_striping_cached)
621 GOTO(unlock, rc = 0);
623 rc = lod_get_lov_ea(env, lp);
627 if (rc < sizeof(struct lov_user_md)) {
628 /* don't lookup for non-existing or invalid striping */
629 lp->ldo_def_striping_set = 0;
630 lp->ldo_striping_cached = 1;
631 lp->ldo_def_stripe_size = 0;
632 lp->ldo_def_stripenr = 0;
633 lp->ldo_def_stripe_offset = (typeof(v1->lmm_stripe_offset))(-1);
634 GOTO(unlock, rc = 0);
637 v1 = (struct lov_user_md_v1 *)lod_env_info(env)->lti_ea_store;
638 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1))
639 lustre_swab_lov_user_md_v1(v1);
640 else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3))
641 lustre_swab_lov_user_md_v3(v3);
643 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1)
644 GOTO(unlock, rc = 0);
646 if (v1->lmm_pattern != LOV_PATTERN_RAID0 && v1->lmm_pattern != 0)
647 GOTO(unlock, rc = 0);
649 lp->ldo_def_stripenr = v1->lmm_stripe_count;
650 lp->ldo_def_stripe_size = v1->lmm_stripe_size;
651 lp->ldo_def_stripe_offset = v1->lmm_stripe_offset;
652 lp->ldo_striping_cached = 1;
653 lp->ldo_def_striping_set = 1;
655 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
656 /* XXX: sanity check here */
657 v3 = (struct lov_user_md_v3 *) v1;
658 if (v3->lmm_pool_name[0])
659 lod_object_set_pool(lp, v3->lmm_pool_name);
662 CDEBUG(D_OTHER, "def. striping: # %d, sz %d, off %d %s%s on "DFID"\n",
663 lp->ldo_def_stripenr, lp->ldo_def_stripe_size,
664 lp->ldo_def_stripe_offset, v3 ? "from " : "",
665 v3 ? lp->ldo_pool : "", PFID(lu_object_fid(&lp->ldo_obj.do_lu)));
669 dt_write_unlock(env, dt_object_child(&lp->ldo_obj));
674 * used to transfer default striping data to the object being created
676 static void lod_ah_init(const struct lu_env *env,
677 struct dt_allocation_hint *ah,
678 struct dt_object *parent,
679 struct dt_object *child,
680 cfs_umode_t child_mode)
682 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
683 struct dt_object *nextp = NULL;
684 struct dt_object *nextc;
685 struct lod_object *lp = NULL;
686 struct lod_object *lc;
687 struct lov_desc *desc;
692 if (likely(parent)) {
693 nextp = dt_object_child(parent);
694 lp = lod_dt_obj(parent);
697 nextc = dt_object_child(child);
698 lc = lod_dt_obj(child);
700 LASSERT(lc->ldo_stripenr == 0);
701 LASSERT(lc->ldo_stripe == NULL);
704 * local object may want some hints
705 * in case of late striping creation, ->ah_init()
706 * can be called with local object existing
708 if (!dt_object_exists(nextc) || dt_object_remote(nextc))
709 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
711 if (S_ISDIR(child_mode)) {
712 if (lp->ldo_striping_cached == 0) {
713 /* we haven't tried to get default striping for
714 * the directory yet, let's cache it in the object */
715 lod_cache_parent_striping(env, lp);
717 /* transfer defaults to new directory */
718 if (lp->ldo_striping_cached) {
720 lod_object_set_pool(lc, lp->ldo_pool);
721 lc->ldo_def_stripenr = lp->ldo_def_stripenr;
722 lc->ldo_def_stripe_size = lp->ldo_def_stripe_size;
723 lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
724 lc->ldo_striping_cached = 1;
725 lc->ldo_def_striping_set = 1;
726 CDEBUG(D_OTHER, "inherite EA sz:%d off:%d nr:%d\n",
727 (int)lc->ldo_def_stripenr,
728 (int)lc->ldo_def_stripe_size,
729 (int)lc->ldo_def_stripe_offset);
735 * if object is going to be striped over OSTs, transfer default
736 * striping information to the child, so that we can use it
737 * during declaration and creation
739 if (!lod_object_will_be_striped(S_ISREG(child_mode),
740 lu_object_fid(&child->do_lu)))
744 * try from the parent
746 if (likely(parent)) {
747 if (lp->ldo_striping_cached == 0) {
748 /* we haven't tried to get default striping for
749 * the directory yet, let's cache it in the object */
750 lod_cache_parent_striping(env, lp);
753 lc->ldo_def_stripe_offset = (__u16) -1;
755 if (lp->ldo_def_striping_set) {
757 lod_object_set_pool(lc, lp->ldo_pool);
758 lc->ldo_stripenr = lp->ldo_def_stripenr;
759 lc->ldo_stripe_size = lp->ldo_def_stripe_size;
760 lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
761 CDEBUG(D_OTHER, "striping from parent: #%d, sz %d %s\n",
762 lc->ldo_stripenr, lc->ldo_stripe_size,
763 lp->ldo_pool ? lp->ldo_pool : "");
768 * if the parent doesn't provide with specific pattern, grab fs-wide one
771 if (lc->ldo_stripenr == 0)
772 lc->ldo_stripenr = desc->ld_default_stripe_count;
773 if (lc->ldo_stripe_size == 0)
774 lc->ldo_stripe_size = desc->ld_default_stripe_size;
775 CDEBUG(D_OTHER, "final striping: # %d stripes, sz %d from %s\n",
776 lc->ldo_stripenr, lc->ldo_stripe_size,
777 lc->ldo_pool ? lc->ldo_pool : "");
782 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
784 * this function handles a special case when truncate was done
785 * on a stripeless object and now striping is being created
786 * we can't lose that size, so we have to propagate it to newly
789 static int lod_declare_init_size(const struct lu_env *env,
790 struct dt_object *dt, struct thandle *th)
792 struct dt_object *next = dt_object_child(dt);
793 struct lod_object *lo = lod_dt_obj(dt);
794 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
799 /* XXX: we support the simplest (RAID0) striping so far */
800 LASSERT(lo->ldo_stripe || lo->ldo_stripenr == 0);
801 LASSERT(lo->ldo_stripe_size > 0);
803 rc = dt_attr_get(env, next, attr, BYPASS_CAPA);
804 LASSERT(attr->la_valid & LA_SIZE);
808 size = attr->la_size;
812 /* ll_do_div64(a, b) returns a % b, and a = a / b */
813 ll_do_div64(size, (__u64) lo->ldo_stripe_size);
814 stripe = ll_do_div64(size, (__u64) lo->ldo_stripenr);
816 size = size * lo->ldo_stripe_size;
817 offs = attr->la_size;
818 size += ll_do_div64(offs, lo->ldo_stripe_size);
820 attr->la_valid = LA_SIZE;
821 attr->la_size = size;
823 rc = dt_declare_attr_set(env, lo->ldo_stripe[stripe], attr, th);
830 * Create declaration of striped object
832 int lod_declare_striped_object(const struct lu_env *env, struct dt_object *dt,
833 struct lu_attr *attr,
834 const struct lu_buf *lovea, struct thandle *th)
836 struct lod_thread_info *info = lod_env_info(env);
837 struct dt_object *next = dt_object_child(dt);
838 struct lod_object *lo = lod_dt_obj(dt);
842 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO)) {
843 /* failed to create striping, let's reset
844 * config so that others don't get confused */
845 lod_object_free_striping(env, lo);
846 GOTO(out, rc = -ENOMEM);
849 /* choose OST and generate appropriate objects */
850 rc = lod_qos_prep_create(env, lo, attr, lovea, th);
852 /* failed to create striping, let's reset
853 * config so that others don't get confused */
854 lod_object_free_striping(env, lo);
859 * declare storage for striping data
861 info->lti_buf.lb_len = lov_mds_md_size(lo->ldo_stripenr,
862 lo->ldo_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1);
863 rc = dt_declare_xattr_set(env, next, &info->lti_buf, XATTR_NAME_LOV,
869 * if striping is created with local object's size > 0,
870 * we have to propagate this size to specific object
871 * the case is possible only when local object was created previously
873 if (dt_object_exists(next))
874 rc = lod_declare_init_size(env, dt, th);
880 static int lod_declare_object_create(const struct lu_env *env,
881 struct dt_object *dt,
882 struct lu_attr *attr,
883 struct dt_allocation_hint *hint,
884 struct dt_object_format *dof,
887 struct dt_object *next = dt_object_child(dt);
888 struct lod_object *lo = lod_dt_obj(dt);
897 * first of all, we declare creation of local object
899 rc = dt_declare_create(env, next, attr, hint, dof, th);
903 if (dof->dof_type == DFT_SYM)
904 dt->do_body_ops = &lod_body_lnk_ops;
907 * it's lod_ah_init() who has decided the object will striped
909 if (dof->dof_type == DFT_REGULAR) {
910 /* callers don't want stripes */
911 /* XXX: all tricky interactions with ->ah_make_hint() decided
912 * to use striping, then ->declare_create() behaving differently
913 * should be cleaned */
914 if (dof->u.dof_reg.striped == 0)
915 lo->ldo_stripenr = 0;
916 if (lo->ldo_stripenr > 0)
917 rc = lod_declare_striped_object(env, dt, attr,
919 } else if (dof->dof_type == DFT_DIR && lo->ldo_striping_cached) {
920 struct lod_thread_info *info = lod_env_info(env);
922 struct lov_user_md_v3 *v3;
924 if (LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size,
925 lo->ldo_def_stripenr,
926 lo->ldo_def_stripe_offset))
933 v3->lmm_magic = cpu_to_le32(LOV_MAGIC_V3);
934 v3->lmm_pattern = cpu_to_le32(LOV_PATTERN_RAID0);
935 fid_to_lmm_oi(lu_object_fid(&dt->do_lu), &v3->lmm_oi);
936 lmm_oi_cpu_to_le(&v3->lmm_oi, &v3->lmm_oi);
937 v3->lmm_stripe_size = cpu_to_le32(lo->ldo_def_stripe_size);
938 v3->lmm_stripe_count = cpu_to_le32(lo->ldo_def_stripenr);
939 v3->lmm_stripe_offset = cpu_to_le16(lo->ldo_def_stripe_offset);
941 strncpy(v3->lmm_pool_name, lo->ldo_pool,
944 info->lti_buf.lb_buf = v3;
945 info->lti_buf.lb_len = sizeof(*v3);
947 /* to transfer default striping from the parent */
948 rc = dt_declare_xattr_set(env, next, &info->lti_buf,
949 XATTR_NAME_LOV, 0, th);
957 int lod_striping_create(const struct lu_env *env, struct dt_object *dt,
958 struct lu_attr *attr, struct dt_object_format *dof,
961 struct lod_object *lo = lod_dt_obj(dt);
965 LASSERT(lo->ldo_stripe);
966 LASSERT(lo->ldo_stripenr > 0);
967 LASSERT(lo->ldo_striping_cached == 0);
969 /* create all underlying objects */
970 for (i = 0; i < lo->ldo_stripenr; i++) {
971 LASSERT(lo->ldo_stripe[i]);
972 rc = dt_create(env, lo->ldo_stripe[i], attr, NULL, dof, th);
978 rc = lod_generate_and_set_lovea(env, lo, th);
983 static int lod_object_create(const struct lu_env *env, struct dt_object *dt,
984 struct lu_attr *attr,
985 struct dt_allocation_hint *hint,
986 struct dt_object_format *dof, struct thandle *th)
988 struct dt_object *next = dt_object_child(dt);
989 struct lod_object *lo = lod_dt_obj(dt);
993 /* create local object */
994 rc = dt_create(env, next, attr, hint, dof, th);
997 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
998 rc = lod_store_def_striping(env, dt, th);
999 else if (lo->ldo_stripe)
1000 rc = lod_striping_create(env, dt, attr, dof, th);
1006 static int lod_declare_object_destroy(const struct lu_env *env,
1007 struct dt_object *dt,
1010 struct dt_object *next = dt_object_child(dt);
1011 struct lod_object *lo = lod_dt_obj(dt);
1016 * we declare destroy for the local object
1018 rc = dt_declare_destroy(env, next, th);
1023 * load striping information, notice we don't do this when object
1024 * is being initialized as we don't need this information till
1025 * few specific cases like destroy, chown
1027 rc = lod_load_striping(env, lo);
1031 /* declare destroy for all underlying objects */
1032 for (i = 0; i < lo->ldo_stripenr; i++) {
1033 LASSERT(lo->ldo_stripe[i]);
1034 rc = dt_declare_destroy(env, lo->ldo_stripe[i], th);
1043 static int lod_object_destroy(const struct lu_env *env,
1044 struct dt_object *dt, struct thandle *th)
1046 struct dt_object *next = dt_object_child(dt);
1047 struct lod_object *lo = lod_dt_obj(dt);
1051 /* destroy local object */
1052 rc = dt_destroy(env, next, th);
1056 /* destroy all underlying objects */
1057 for (i = 0; i < lo->ldo_stripenr; i++) {
1058 LASSERT(lo->ldo_stripe[i]);
1059 rc = dt_destroy(env, lo->ldo_stripe[i], th);
1067 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
1068 const struct dt_index_features *feat)
1070 struct dt_object *next = dt_object_child(dt);
1074 LASSERT(next->do_ops);
1075 LASSERT(next->do_ops->do_index_try);
1077 rc = next->do_ops->do_index_try(env, next, feat);
1078 if (next->do_index_ops && dt->do_index_ops == NULL)
1079 dt->do_index_ops = &lod_index_ops;
1084 static int lod_declare_ref_add(const struct lu_env *env,
1085 struct dt_object *dt, struct thandle *th)
1087 return dt_declare_ref_add(env, dt_object_child(dt), th);
1090 static int lod_ref_add(const struct lu_env *env,
1091 struct dt_object *dt, struct thandle *th)
1093 return dt_ref_add(env, dt_object_child(dt), th);
1096 static int lod_declare_ref_del(const struct lu_env *env,
1097 struct dt_object *dt, struct thandle *th)
1099 return dt_declare_ref_del(env, dt_object_child(dt), th);
1102 static int lod_ref_del(const struct lu_env *env,
1103 struct dt_object *dt, struct thandle *th)
1105 return dt_ref_del(env, dt_object_child(dt), th);
1108 static struct obd_capa *lod_capa_get(const struct lu_env *env,
1109 struct dt_object *dt,
1110 struct lustre_capa *old, __u64 opc)
1112 return dt_capa_get(env, dt_object_child(dt), old, opc);
1115 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt)
1117 return dt_object_sync(env, dt_object_child(dt));
1120 static int lod_object_lock(const struct lu_env *env,
1121 struct dt_object *dt, struct lustre_handle *lh,
1122 struct ldlm_enqueue_info *einfo,
1125 struct dt_object *next = dt_object_child(dt);
1130 * declare setattr on the local object
1132 rc = dt_object_lock(env, next, lh, einfo, policy);
1137 struct dt_object_operations lod_obj_ops = {
1138 .do_read_lock = lod_object_read_lock,
1139 .do_write_lock = lod_object_write_lock,
1140 .do_read_unlock = lod_object_read_unlock,
1141 .do_write_unlock = lod_object_write_unlock,
1142 .do_write_locked = lod_object_write_locked,
1143 .do_attr_get = lod_attr_get,
1144 .do_declare_attr_set = lod_declare_attr_set,
1145 .do_attr_set = lod_attr_set,
1146 .do_xattr_get = lod_xattr_get,
1147 .do_declare_xattr_set = lod_declare_xattr_set,
1148 .do_xattr_set = lod_xattr_set,
1149 .do_declare_xattr_del = lod_declare_xattr_del,
1150 .do_xattr_del = lod_xattr_del,
1151 .do_xattr_list = lod_xattr_list,
1152 .do_ah_init = lod_ah_init,
1153 .do_declare_create = lod_declare_object_create,
1154 .do_create = lod_object_create,
1155 .do_declare_destroy = lod_declare_object_destroy,
1156 .do_destroy = lod_object_destroy,
1157 .do_index_try = lod_index_try,
1158 .do_declare_ref_add = lod_declare_ref_add,
1159 .do_ref_add = lod_ref_add,
1160 .do_declare_ref_del = lod_declare_ref_del,
1161 .do_ref_del = lod_ref_del,
1162 .do_capa_get = lod_capa_get,
1163 .do_object_sync = lod_object_sync,
1164 .do_object_lock = lod_object_lock,
1167 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
1168 struct lu_buf *buf, loff_t *pos,
1169 struct lustre_capa *capa)
1171 struct dt_object *next = dt_object_child(dt);
1172 return next->do_body_ops->dbo_read(env, next, buf, pos, capa);
1175 static ssize_t lod_declare_write(const struct lu_env *env,
1176 struct dt_object *dt,
1177 const loff_t size, loff_t pos,
1180 return dt_declare_record_write(env, dt_object_child(dt),
1184 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
1185 const struct lu_buf *buf, loff_t *pos,
1186 struct thandle *th, struct lustre_capa *capa, int iq)
1188 struct dt_object *next = dt_object_child(dt);
1190 return next->do_body_ops->dbo_write(env, next, buf, pos, th, capa, iq);
1193 static const struct dt_body_operations lod_body_lnk_ops = {
1194 .dbo_read = lod_read,
1195 .dbo_declare_write = lod_declare_write,
1196 .dbo_write = lod_write
1199 static int lod_object_init(const struct lu_env *env, struct lu_object *o,
1200 const struct lu_object_conf *conf)
1202 struct lod_device *d = lu2lod_dev(o->lo_dev);
1203 struct lu_object *below;
1204 struct lu_device *under;
1208 * create local object
1210 under = &d->lod_child->dd_lu_dev;
1211 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
1215 lu_object_add(o, below);
1220 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
1224 if (lo->ldo_stripe) {
1225 LASSERT(lo->ldo_stripes_allocated > 0);
1227 for (i = 0; i < lo->ldo_stripenr; i++) {
1228 if (lo->ldo_stripe[i])
1229 lu_object_put(env, &lo->ldo_stripe[i]->do_lu);
1232 i = sizeof(struct dt_object *) * lo->ldo_stripes_allocated;
1233 OBD_FREE(lo->ldo_stripe, i);
1234 lo->ldo_stripe = NULL;
1235 lo->ldo_stripes_allocated = 0;
1237 lo->ldo_stripenr = 0;
1241 * ->start is called once all slices are initialized, including header's
1242 * cache for mode (object type). using the type we can initialize ops
1244 static int lod_object_start(const struct lu_env *env, struct lu_object *o)
1246 if (S_ISLNK(o->lo_header->loh_attr & S_IFMT))
1247 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_lnk_ops;
1251 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
1253 struct lod_object *mo = lu2lod_obj(o);
1256 * release all underlying object pinned
1259 lod_object_free_striping(env, mo);
1261 lod_object_set_pool(mo, NULL);
1264 OBD_SLAB_FREE_PTR(mo, lod_object_kmem);
1267 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
1269 /* XXX: shouldn't we release everything here in case if object
1270 * creation failed before? */
1273 static int lod_object_print(const struct lu_env *env, void *cookie,
1274 lu_printer_t p, const struct lu_object *l)
1276 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
1278 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
1281 struct lu_object_operations lod_lu_obj_ops = {
1282 .loo_object_init = lod_object_init,
1283 .loo_object_start = lod_object_start,
1284 .loo_object_free = lod_object_free,
1285 .loo_object_release = lod_object_release,
1286 .loo_object_print = lod_object_print,
1290 * Init remote lod object
1292 static int lod_robject_init(const struct lu_env *env, struct lu_object *lo,
1293 const struct lu_object_conf *conf)
1295 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
1296 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1297 struct lu_device *c_dev = NULL;
1298 struct lu_object *c_obj;
1303 if (ltd->ltd_tgts_size > 0) {
1304 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
1305 struct lod_tgt_desc *tgt;
1306 tgt = LTD_TGT(ltd, i);
1307 LASSERT(tgt && tgt->ltd_tgt);
1308 if (tgt->ltd_index ==
1309 lu2lod_obj(lo)->ldo_mds_num) {
1310 c_dev = &(tgt->ltd_tgt->dd_lu_dev);
1315 lod_putref(lod, ltd);
1317 if (unlikely(c_dev == NULL))
1320 c_obj = c_dev->ld_ops->ldo_object_alloc(env, lo->lo_header, c_dev);
1321 if (unlikely(c_obj == NULL))
1324 lu_object_add(lo, c_obj);
1329 struct lu_object_operations lod_lu_robj_ops = {
1330 .loo_object_init = lod_robject_init,
1331 .loo_object_start = lod_object_start,
1332 .loo_object_free = lod_object_free,
1333 .loo_object_release = lod_object_release,
1334 .loo_object_print = lod_object_print,