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, 2016, Intel Corporation.
29 * lustre/lod/lod_object.c
31 * This file contains implementations of methods for the OSD API
32 * for the Logical Object Device (LOD) layer, which provides a virtual
33 * local OSD object interface to the MDD layer, and abstracts the
34 * addressing of local (OSD) and remote (OSP) objects. The API is
35 * described in the file lustre/include/dt_object.h and in
36 * Documentation/osd-api.txt.
38 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
41 #define DEBUG_SUBSYSTEM S_MDS
44 #include <obd_class.h>
45 #include <obd_support.h>
47 #include <lustre_fid.h>
48 #include <lustre_linkea.h>
49 #include <lustre_lmv.h>
50 #include <uapi/linux/lustre_param.h>
51 #include <lustre_swab.h>
52 #include <lustre_ver.h>
53 #include <lprocfs_status.h>
54 #include <md_object.h>
56 #include "lod_internal.h"
58 static const char dot[] = ".";
59 static const char dotdot[] = "..";
61 static const struct dt_body_operations lod_body_lnk_ops;
62 static const struct dt_body_operations lod_body_ops;
65 * Implementation of dt_index_operations::dio_lookup
67 * Used with regular (non-striped) objects.
69 * \see dt_index_operations::dio_lookup() in the API description for details.
71 static int lod_lookup(const struct lu_env *env, struct dt_object *dt,
72 struct dt_rec *rec, const struct dt_key *key)
74 struct dt_object *next = dt_object_child(dt);
75 return next->do_index_ops->dio_lookup(env, next, rec, key);
79 * Implementation of dt_index_operations::dio_declare_insert.
81 * Used with regular (non-striped) objects.
83 * \see dt_index_operations::dio_declare_insert() in the API description
86 static int lod_declare_insert(const struct lu_env *env, struct dt_object *dt,
87 const struct dt_rec *rec,
88 const struct dt_key *key, struct thandle *th)
90 return lod_sub_declare_insert(env, dt_object_child(dt), rec, key, th);
94 * Implementation of dt_index_operations::dio_insert.
96 * Used with regular (non-striped) objects
98 * \see dt_index_operations::dio_insert() in the API description for details.
100 static int lod_insert(const struct lu_env *env, struct dt_object *dt,
101 const struct dt_rec *rec, const struct dt_key *key,
102 struct thandle *th, int ign)
104 return lod_sub_insert(env, dt_object_child(dt), rec, key, th, ign);
108 * Implementation of dt_index_operations::dio_declare_delete.
110 * Used with regular (non-striped) objects.
112 * \see dt_index_operations::dio_declare_delete() in the API description
115 static int lod_declare_delete(const struct lu_env *env, struct dt_object *dt,
116 const struct dt_key *key, struct thandle *th)
118 return lod_sub_declare_delete(env, dt_object_child(dt), key, th);
122 * Implementation of dt_index_operations::dio_delete.
124 * Used with regular (non-striped) objects.
126 * \see dt_index_operations::dio_delete() in the API description for details.
128 static int lod_delete(const struct lu_env *env, struct dt_object *dt,
129 const struct dt_key *key, struct thandle *th)
131 return lod_sub_delete(env, dt_object_child(dt), key, th);
135 * Implementation of dt_it_ops::init.
137 * Used with regular (non-striped) objects.
139 * \see dt_it_ops::init() in the API description for details.
141 static struct dt_it *lod_it_init(const struct lu_env *env,
142 struct dt_object *dt, __u32 attr)
144 struct dt_object *next = dt_object_child(dt);
145 struct lod_it *it = &lod_env_info(env)->lti_it;
146 struct dt_it *it_next;
148 it_next = next->do_index_ops->dio_it.init(env, next, attr);
152 /* currently we do not use more than one iterator per thread
153 * so we store it in thread info. if at some point we need
154 * more active iterators in a single thread, we can allocate
156 LASSERT(it->lit_obj == NULL);
158 it->lit_it = it_next;
161 return (struct dt_it *)it;
164 #define LOD_CHECK_IT(env, it) \
166 LASSERT((it)->lit_obj != NULL); \
167 LASSERT((it)->lit_it != NULL); \
171 * Implementation of dt_index_operations::dio_it.fini.
173 * Used with regular (non-striped) objects.
175 * \see dt_index_operations::dio_it.fini() in the API description for details.
177 static void lod_it_fini(const struct lu_env *env, struct dt_it *di)
179 struct lod_it *it = (struct lod_it *)di;
181 LOD_CHECK_IT(env, it);
182 it->lit_obj->do_index_ops->dio_it.fini(env, it->lit_it);
184 /* the iterator not in use any more */
190 * Implementation of dt_it_ops::get.
192 * Used with regular (non-striped) objects.
194 * \see dt_it_ops::get() in the API description for details.
196 static int lod_it_get(const struct lu_env *env, struct dt_it *di,
197 const struct dt_key *key)
199 const struct lod_it *it = (const struct lod_it *)di;
201 LOD_CHECK_IT(env, it);
202 return it->lit_obj->do_index_ops->dio_it.get(env, it->lit_it, key);
206 * Implementation of dt_it_ops::put.
208 * Used with regular (non-striped) objects.
210 * \see dt_it_ops::put() in the API description for details.
212 static void lod_it_put(const struct lu_env *env, struct dt_it *di)
214 struct lod_it *it = (struct lod_it *)di;
216 LOD_CHECK_IT(env, it);
217 return it->lit_obj->do_index_ops->dio_it.put(env, it->lit_it);
221 * Implementation of dt_it_ops::next.
223 * Used with regular (non-striped) objects
225 * \see dt_it_ops::next() in the API description for details.
227 static int lod_it_next(const struct lu_env *env, struct dt_it *di)
229 struct lod_it *it = (struct lod_it *)di;
231 LOD_CHECK_IT(env, it);
232 return it->lit_obj->do_index_ops->dio_it.next(env, it->lit_it);
236 * Implementation of dt_it_ops::key.
238 * Used with regular (non-striped) objects.
240 * \see dt_it_ops::key() in the API description for details.
242 static struct dt_key *lod_it_key(const struct lu_env *env,
243 const struct dt_it *di)
245 const struct lod_it *it = (const struct lod_it *)di;
247 LOD_CHECK_IT(env, it);
248 return it->lit_obj->do_index_ops->dio_it.key(env, it->lit_it);
252 * Implementation of dt_it_ops::key_size.
254 * Used with regular (non-striped) objects.
256 * \see dt_it_ops::key_size() in the API description for details.
258 static int lod_it_key_size(const struct lu_env *env, const struct dt_it *di)
260 struct lod_it *it = (struct lod_it *)di;
262 LOD_CHECK_IT(env, it);
263 return it->lit_obj->do_index_ops->dio_it.key_size(env, it->lit_it);
267 * Implementation of dt_it_ops::rec.
269 * Used with regular (non-striped) objects.
271 * \see dt_it_ops::rec() in the API description for details.
273 static int lod_it_rec(const struct lu_env *env, const struct dt_it *di,
274 struct dt_rec *rec, __u32 attr)
276 const struct lod_it *it = (const struct lod_it *)di;
278 LOD_CHECK_IT(env, it);
279 return it->lit_obj->do_index_ops->dio_it.rec(env, it->lit_it, rec,
284 * Implementation of dt_it_ops::rec_size.
286 * Used with regular (non-striped) objects.
288 * \see dt_it_ops::rec_size() in the API description for details.
290 static int lod_it_rec_size(const struct lu_env *env, const struct dt_it *di,
293 const struct lod_it *it = (const struct lod_it *)di;
295 LOD_CHECK_IT(env, it);
296 return it->lit_obj->do_index_ops->dio_it.rec_size(env, it->lit_it,
301 * Implementation of dt_it_ops::store.
303 * Used with regular (non-striped) objects.
305 * \see dt_it_ops::store() in the API description for details.
307 static __u64 lod_it_store(const struct lu_env *env, const struct dt_it *di)
309 const struct lod_it *it = (const struct lod_it *)di;
311 LOD_CHECK_IT(env, it);
312 return it->lit_obj->do_index_ops->dio_it.store(env, it->lit_it);
316 * Implementation of dt_it_ops::load.
318 * Used with regular (non-striped) objects.
320 * \see dt_it_ops::load() in the API description for details.
322 static int lod_it_load(const struct lu_env *env, const struct dt_it *di,
325 const struct lod_it *it = (const struct lod_it *)di;
327 LOD_CHECK_IT(env, it);
328 return it->lit_obj->do_index_ops->dio_it.load(env, it->lit_it, hash);
332 * Implementation of dt_it_ops::key_rec.
334 * Used with regular (non-striped) objects.
336 * \see dt_it_ops::rec() in the API description for details.
338 static int lod_it_key_rec(const struct lu_env *env, const struct dt_it *di,
341 const struct lod_it *it = (const struct lod_it *)di;
343 LOD_CHECK_IT(env, it);
344 return it->lit_obj->do_index_ops->dio_it.key_rec(env, it->lit_it,
348 static struct dt_index_operations lod_index_ops = {
349 .dio_lookup = lod_lookup,
350 .dio_declare_insert = lod_declare_insert,
351 .dio_insert = lod_insert,
352 .dio_declare_delete = lod_declare_delete,
353 .dio_delete = lod_delete,
361 .key_size = lod_it_key_size,
363 .rec_size = lod_it_rec_size,
364 .store = lod_it_store,
366 .key_rec = lod_it_key_rec,
371 * Implementation of dt_it_ops::init.
373 * Used with striped objects. Internally just initializes the iterator
374 * on the first stripe.
376 * \see dt_it_ops::init() in the API description for details.
378 static struct dt_it *lod_striped_it_init(const struct lu_env *env,
379 struct dt_object *dt, __u32 attr)
381 struct lod_object *lo = lod_dt_obj(dt);
382 struct dt_object *next;
383 struct lod_it *it = &lod_env_info(env)->lti_it;
384 struct dt_it *it_next;
387 LASSERT(lo->ldo_dir_stripenr > 0);
388 next = lo->ldo_stripe[0];
389 LASSERT(next != NULL);
390 LASSERT(next->do_index_ops != NULL);
392 it_next = next->do_index_ops->dio_it.init(env, next, attr);
396 /* currently we do not use more than one iterator per thread
397 * so we store it in thread info. if at some point we need
398 * more active iterators in a single thread, we can allocate
400 LASSERT(it->lit_obj == NULL);
402 it->lit_stripe_index = 0;
404 it->lit_it = it_next;
407 return (struct dt_it *)it;
410 #define LOD_CHECK_STRIPED_IT(env, it, lo) \
412 LASSERT((it)->lit_obj != NULL); \
413 LASSERT((it)->lit_it != NULL); \
414 LASSERT((lo)->ldo_dir_stripenr > 0); \
415 LASSERT((it)->lit_stripe_index < (lo)->ldo_dir_stripenr); \
419 * Implementation of dt_it_ops::fini.
421 * Used with striped objects.
423 * \see dt_it_ops::fini() in the API description for details.
425 static void lod_striped_it_fini(const struct lu_env *env, struct dt_it *di)
427 struct lod_it *it = (struct lod_it *)di;
428 struct lod_object *lo = lod_dt_obj(it->lit_obj);
429 struct dt_object *next;
431 /* If lit_it == NULL, then it means the sub_it has been finished,
432 * which only happens in failure cases, see lod_striped_it_next() */
433 if (it->lit_it != NULL) {
434 LOD_CHECK_STRIPED_IT(env, it, lo);
436 next = lo->ldo_stripe[it->lit_stripe_index];
437 LASSERT(next != NULL);
438 LASSERT(next->do_index_ops != NULL);
440 next->do_index_ops->dio_it.fini(env, it->lit_it);
443 /* the iterator not in use any more */
446 it->lit_stripe_index = 0;
450 * Implementation of dt_it_ops::get.
452 * Right now it's not used widely, only to reset the iterator to the
453 * initial position. It should be possible to implement a full version
454 * which chooses a correct stripe to be able to position with any key.
456 * \see dt_it_ops::get() in the API description for details.
458 static int lod_striped_it_get(const struct lu_env *env, struct dt_it *di,
459 const struct dt_key *key)
461 const struct lod_it *it = (const struct lod_it *)di;
462 struct lod_object *lo = lod_dt_obj(it->lit_obj);
463 struct dt_object *next;
466 LOD_CHECK_STRIPED_IT(env, it, lo);
468 next = lo->ldo_stripe[it->lit_stripe_index];
469 LASSERT(next != NULL);
470 LASSERT(next->do_index_ops != NULL);
472 return next->do_index_ops->dio_it.get(env, it->lit_it, key);
476 * Implementation of dt_it_ops::put.
478 * Used with striped objects.
480 * \see dt_it_ops::put() in the API description for details.
482 static void lod_striped_it_put(const struct lu_env *env, struct dt_it *di)
484 struct lod_it *it = (struct lod_it *)di;
485 struct lod_object *lo = lod_dt_obj(it->lit_obj);
486 struct dt_object *next;
488 LOD_CHECK_STRIPED_IT(env, it, lo);
490 next = lo->ldo_stripe[it->lit_stripe_index];
491 LASSERT(next != NULL);
492 LASSERT(next->do_index_ops != NULL);
494 return next->do_index_ops->dio_it.put(env, it->lit_it);
498 * Implementation of dt_it_ops::next.
500 * Used with striped objects. When the end of the current stripe is
501 * reached, the method takes the next stripe's iterator.
503 * \see dt_it_ops::next() in the API description for details.
505 static int lod_striped_it_next(const struct lu_env *env, struct dt_it *di)
507 struct lod_it *it = (struct lod_it *)di;
508 struct lod_object *lo = lod_dt_obj(it->lit_obj);
509 struct dt_object *next;
510 struct dt_it *it_next;
514 LOD_CHECK_STRIPED_IT(env, it, lo);
516 next = lo->ldo_stripe[it->lit_stripe_index];
517 LASSERT(next != NULL);
518 LASSERT(next->do_index_ops != NULL);
520 rc = next->do_index_ops->dio_it.next(env, it->lit_it);
524 if (rc == 0 && it->lit_stripe_index == 0)
527 if (rc == 0 && it->lit_stripe_index > 0) {
528 struct lu_dirent *ent;
530 ent = (struct lu_dirent *)lod_env_info(env)->lti_key;
532 rc = next->do_index_ops->dio_it.rec(env, it->lit_it,
533 (struct dt_rec *)ent,
538 /* skip . and .. for slave stripe */
539 if ((strncmp(ent->lde_name, ".",
540 le16_to_cpu(ent->lde_namelen)) == 0 &&
541 le16_to_cpu(ent->lde_namelen) == 1) ||
542 (strncmp(ent->lde_name, "..",
543 le16_to_cpu(ent->lde_namelen)) == 0 &&
544 le16_to_cpu(ent->lde_namelen) == 2))
550 /* go to next stripe */
551 if (it->lit_stripe_index + 1 >= lo->ldo_dir_stripenr)
554 it->lit_stripe_index++;
556 next->do_index_ops->dio_it.put(env, it->lit_it);
557 next->do_index_ops->dio_it.fini(env, it->lit_it);
560 next = lo->ldo_stripe[it->lit_stripe_index];
561 LASSERT(next != NULL);
562 rc = next->do_ops->do_index_try(env, next, &dt_directory_features);
566 LASSERT(next->do_index_ops != NULL);
568 it_next = next->do_index_ops->dio_it.init(env, next, it->lit_attr);
569 if (!IS_ERR(it_next)) {
570 it->lit_it = it_next;
573 rc = PTR_ERR(it_next);
580 * Implementation of dt_it_ops::key.
582 * Used with striped objects.
584 * \see dt_it_ops::key() in the API description for details.
586 static struct dt_key *lod_striped_it_key(const struct lu_env *env,
587 const struct dt_it *di)
589 const struct lod_it *it = (const struct lod_it *)di;
590 struct lod_object *lo = lod_dt_obj(it->lit_obj);
591 struct dt_object *next;
593 LOD_CHECK_STRIPED_IT(env, it, lo);
595 next = lo->ldo_stripe[it->lit_stripe_index];
596 LASSERT(next != NULL);
597 LASSERT(next->do_index_ops != NULL);
599 return next->do_index_ops->dio_it.key(env, it->lit_it);
603 * Implementation of dt_it_ops::key_size.
605 * Used with striped objects.
607 * \see dt_it_ops::size() in the API description for details.
609 static int lod_striped_it_key_size(const struct lu_env *env,
610 const struct dt_it *di)
612 struct lod_it *it = (struct lod_it *)di;
613 struct lod_object *lo = lod_dt_obj(it->lit_obj);
614 struct dt_object *next;
616 LOD_CHECK_STRIPED_IT(env, it, lo);
618 next = lo->ldo_stripe[it->lit_stripe_index];
619 LASSERT(next != NULL);
620 LASSERT(next->do_index_ops != NULL);
622 return next->do_index_ops->dio_it.key_size(env, it->lit_it);
626 * Implementation of dt_it_ops::rec.
628 * Used with striped objects.
630 * \see dt_it_ops::rec() in the API description for details.
632 static int lod_striped_it_rec(const struct lu_env *env, const struct dt_it *di,
633 struct dt_rec *rec, __u32 attr)
635 const struct lod_it *it = (const struct lod_it *)di;
636 struct lod_object *lo = lod_dt_obj(it->lit_obj);
637 struct dt_object *next;
639 LOD_CHECK_STRIPED_IT(env, it, lo);
641 next = lo->ldo_stripe[it->lit_stripe_index];
642 LASSERT(next != NULL);
643 LASSERT(next->do_index_ops != NULL);
645 return next->do_index_ops->dio_it.rec(env, it->lit_it, rec, attr);
649 * Implementation of dt_it_ops::rec_size.
651 * Used with striped objects.
653 * \see dt_it_ops::rec_size() in the API description for details.
655 static int lod_striped_it_rec_size(const struct lu_env *env,
656 const struct dt_it *di, __u32 attr)
658 struct lod_it *it = (struct lod_it *)di;
659 struct lod_object *lo = lod_dt_obj(it->lit_obj);
660 struct dt_object *next;
662 LOD_CHECK_STRIPED_IT(env, it, lo);
664 next = lo->ldo_stripe[it->lit_stripe_index];
665 LASSERT(next != NULL);
666 LASSERT(next->do_index_ops != NULL);
668 return next->do_index_ops->dio_it.rec_size(env, it->lit_it, attr);
672 * Implementation of dt_it_ops::store.
674 * Used with striped objects.
676 * \see dt_it_ops::store() in the API description for details.
678 static __u64 lod_striped_it_store(const struct lu_env *env,
679 const struct dt_it *di)
681 const struct lod_it *it = (const struct lod_it *)di;
682 struct lod_object *lo = lod_dt_obj(it->lit_obj);
683 struct dt_object *next;
685 LOD_CHECK_STRIPED_IT(env, it, lo);
687 next = lo->ldo_stripe[it->lit_stripe_index];
688 LASSERT(next != NULL);
689 LASSERT(next->do_index_ops != NULL);
691 return next->do_index_ops->dio_it.store(env, it->lit_it);
695 * Implementation of dt_it_ops::load.
697 * Used with striped objects.
699 * \see dt_it_ops::load() in the API description for details.
701 static int lod_striped_it_load(const struct lu_env *env,
702 const struct dt_it *di, __u64 hash)
704 const struct lod_it *it = (const struct lod_it *)di;
705 struct lod_object *lo = lod_dt_obj(it->lit_obj);
706 struct dt_object *next;
708 LOD_CHECK_STRIPED_IT(env, it, lo);
710 next = lo->ldo_stripe[it->lit_stripe_index];
711 LASSERT(next != NULL);
712 LASSERT(next->do_index_ops != NULL);
714 return next->do_index_ops->dio_it.load(env, it->lit_it, hash);
717 static struct dt_index_operations lod_striped_index_ops = {
718 .dio_lookup = lod_lookup,
719 .dio_declare_insert = lod_declare_insert,
720 .dio_insert = lod_insert,
721 .dio_declare_delete = lod_declare_delete,
722 .dio_delete = lod_delete,
724 .init = lod_striped_it_init,
725 .fini = lod_striped_it_fini,
726 .get = lod_striped_it_get,
727 .put = lod_striped_it_put,
728 .next = lod_striped_it_next,
729 .key = lod_striped_it_key,
730 .key_size = lod_striped_it_key_size,
731 .rec = lod_striped_it_rec,
732 .rec_size = lod_striped_it_rec_size,
733 .store = lod_striped_it_store,
734 .load = lod_striped_it_load,
739 * Append the FID for each shard of the striped directory after the
740 * given LMV EA header.
742 * To simplify striped directory and the consistency verification,
743 * we only store the LMV EA header on disk, for both master object
744 * and slave objects. When someone wants to know the whole LMV EA,
745 * such as client readdir(), we can build the entrie LMV EA on the
746 * MDT side (in RAM) via iterating the sub-directory entries that
747 * are contained in the master object of the stripe directory.
749 * For the master object of the striped directroy, the valid name
750 * for each shard is composed of the ${shard_FID}:${shard_idx}.
752 * There may be holes in the LMV EA if some shards' name entries
753 * are corrupted or lost.
755 * \param[in] env pointer to the thread context
756 * \param[in] lo pointer to the master object of the striped directory
757 * \param[in] buf pointer to the lu_buf which will hold the LMV EA
758 * \param[in] resize whether re-allocate the buffer if it is not big enough
760 * \retval positive size of the LMV EA
761 * \retval 0 for nothing to be loaded
762 * \retval negative error number on failure
764 int lod_load_lmv_shards(const struct lu_env *env, struct lod_object *lo,
765 struct lu_buf *buf, bool resize)
767 struct lu_dirent *ent =
768 (struct lu_dirent *)lod_env_info(env)->lti_key;
769 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
770 struct dt_object *obj = dt_object_child(&lo->ldo_obj);
771 struct lmv_mds_md_v1 *lmv1 = buf->lb_buf;
773 const struct dt_it_ops *iops;
775 __u32 magic = le32_to_cpu(lmv1->lmv_magic);
780 /* If it is not a striped directory, then load nothing. */
781 if (magic != LMV_MAGIC_V1)
784 /* If it is in migration (or failure), then load nothing. */
785 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
788 stripes = le32_to_cpu(lmv1->lmv_stripe_count);
792 rc = lmv_mds_md_size(stripes, magic);
796 if (buf->lb_len < lmv1_size) {
805 lu_buf_alloc(buf, lmv1_size);
810 memcpy(buf->lb_buf, tbuf.lb_buf, tbuf.lb_len);
813 if (unlikely(!dt_try_as_dir(env, obj)))
816 memset(&lmv1->lmv_stripe_fids[0], 0, stripes * sizeof(struct lu_fid));
817 iops = &obj->do_index_ops->dio_it;
818 it = iops->init(env, obj, LUDA_64BITHASH);
822 rc = iops->load(env, it, 0);
824 rc = iops->next(env, it);
829 char name[FID_LEN + 2] = "";
834 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
840 fid_le_to_cpu(&fid, &ent->lde_fid);
841 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
842 if (ent->lde_name[0] == '.') {
843 if (ent->lde_namelen == 1)
846 if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
850 len = snprintf(name, sizeof(name),
851 DFID":", PFID(&ent->lde_fid));
852 /* The ent->lde_name is composed of ${FID}:${index} */
853 if (ent->lde_namelen < len + 1 ||
854 memcmp(ent->lde_name, name, len) != 0) {
855 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
856 "%s: invalid shard name %.*s with the FID "DFID
857 " for the striped directory "DFID", %s\n",
858 lod2obd(lod)->obd_name, ent->lde_namelen,
859 ent->lde_name, PFID(&fid),
860 PFID(lu_object_fid(&obj->do_lu)),
861 lod->lod_lmv_failout ? "failout" : "skip");
863 if (lod->lod_lmv_failout)
871 if (ent->lde_name[len] < '0' ||
872 ent->lde_name[len] > '9') {
873 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
874 "%s: invalid shard name %.*s with the "
875 "FID "DFID" for the striped directory "
877 lod2obd(lod)->obd_name, ent->lde_namelen,
878 ent->lde_name, PFID(&fid),
879 PFID(lu_object_fid(&obj->do_lu)),
880 lod->lod_lmv_failout ?
883 if (lod->lod_lmv_failout)
889 index = index * 10 + ent->lde_name[len++] - '0';
890 } while (len < ent->lde_namelen);
892 if (len == ent->lde_namelen) {
893 /* Out of LMV EA range. */
894 if (index >= stripes) {
895 CERROR("%s: the shard %.*s for the striped "
896 "directory "DFID" is out of the known "
897 "LMV EA range [0 - %u], failout\n",
898 lod2obd(lod)->obd_name, ent->lde_namelen,
900 PFID(lu_object_fid(&obj->do_lu)),
906 /* The slot has been occupied. */
907 if (!fid_is_zero(&lmv1->lmv_stripe_fids[index])) {
911 &lmv1->lmv_stripe_fids[index]);
912 CERROR("%s: both the shard "DFID" and "DFID
913 " for the striped directory "DFID
914 " claim the same LMV EA slot at the "
915 "index %d, failout\n",
916 lod2obd(lod)->obd_name,
917 PFID(&fid0), PFID(&fid),
918 PFID(lu_object_fid(&obj->do_lu)), index);
923 /* stored as LE mode */
924 lmv1->lmv_stripe_fids[index] = ent->lde_fid;
927 rc = iops->next(env, it);
934 RETURN(rc > 0 ? lmv_mds_md_size(stripes, magic) : rc);
938 * Implementation of dt_object_operations::do_index_try.
940 * \see dt_object_operations::do_index_try() in the API description for details.
942 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
943 const struct dt_index_features *feat)
945 struct lod_object *lo = lod_dt_obj(dt);
946 struct dt_object *next = dt_object_child(dt);
950 LASSERT(next->do_ops);
951 LASSERT(next->do_ops->do_index_try);
953 rc = lod_load_striping_locked(env, lo);
957 rc = next->do_ops->do_index_try(env, next, feat);
961 if (lo->ldo_dir_stripenr > 0) {
964 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
965 if (dt_object_exists(lo->ldo_stripe[i]) == 0)
967 rc = lo->ldo_stripe[i]->do_ops->do_index_try(env,
968 lo->ldo_stripe[i], feat);
972 dt->do_index_ops = &lod_striped_index_ops;
974 dt->do_index_ops = &lod_index_ops;
981 * Implementation of dt_object_operations::do_read_lock.
983 * \see dt_object_operations::do_read_lock() in the API description for details.
985 static void lod_read_lock(const struct lu_env *env, struct dt_object *dt,
988 dt_read_lock(env, dt_object_child(dt), role);
992 * Implementation of dt_object_operations::do_write_lock.
994 * \see dt_object_operations::do_write_lock() in the API description for
997 static void lod_write_lock(const struct lu_env *env, struct dt_object *dt,
1000 dt_write_lock(env, dt_object_child(dt), role);
1004 * Implementation of dt_object_operations::do_read_unlock.
1006 * \see dt_object_operations::do_read_unlock() in the API description for
1009 static void lod_read_unlock(const struct lu_env *env, struct dt_object *dt)
1011 dt_read_unlock(env, dt_object_child(dt));
1015 * Implementation of dt_object_operations::do_write_unlock.
1017 * \see dt_object_operations::do_write_unlock() in the API description for
1020 static void lod_write_unlock(const struct lu_env *env, struct dt_object *dt)
1022 dt_write_unlock(env, dt_object_child(dt));
1026 * Implementation of dt_object_operations::do_write_locked.
1028 * \see dt_object_operations::do_write_locked() in the API description for
1031 static int lod_write_locked(const struct lu_env *env, struct dt_object *dt)
1033 return dt_write_locked(env, dt_object_child(dt));
1037 * Implementation of dt_object_operations::do_attr_get.
1039 * \see dt_object_operations::do_attr_get() in the API description for details.
1041 static int lod_attr_get(const struct lu_env *env,
1042 struct dt_object *dt,
1043 struct lu_attr *attr)
1045 /* Note: for striped directory, client will merge attributes
1046 * from all of the sub-stripes see lmv_merge_attr(), and there
1047 * no MDD logic depend on directory nlink/size/time, so we can
1048 * always use master inode nlink and size for now. */
1049 return dt_attr_get(env, dt_object_child(dt), attr);
1052 int lod_obj_for_each_stripe(const struct lu_env *env, struct lod_object *lo,
1053 struct thandle *th, lod_obj_stripe_cb_t cb,
1054 struct lod_obj_stripe_cb_data *data)
1056 struct lod_layout_component *lod_comp;
1060 LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
1061 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1062 lod_comp = &lo->ldo_comp_entries[i];
1064 if (lod_comp->llc_stripe == NULL)
1067 LASSERT(lod_comp->llc_stripenr > 0);
1068 for (j = 0; j < lod_comp->llc_stripenr; j++) {
1069 struct dt_object *dt = lod_comp->llc_stripe[j];
1073 rc = cb(env, lo, dt, th, j, data);
1082 lod_obj_stripe_attr_set_cb(const struct lu_env *env, struct lod_object *lo,
1083 struct dt_object *dt, struct thandle *th,
1084 int stripe_idx, struct lod_obj_stripe_cb_data *data)
1086 if (data->locd_declare)
1087 return lod_sub_declare_attr_set(env, dt, data->locd_attr, th);
1089 return lod_sub_attr_set(env, dt, data->locd_attr, th);
1093 * Implementation of dt_object_operations::do_declare_attr_set.
1095 * If the object is striped, then apply the changes to all the stripes.
1097 * \see dt_object_operations::do_declare_attr_set() in the API description
1100 static int lod_declare_attr_set(const struct lu_env *env,
1101 struct dt_object *dt,
1102 const struct lu_attr *attr,
1105 struct dt_object *next = dt_object_child(dt);
1106 struct lod_object *lo = lod_dt_obj(dt);
1111 * declare setattr on the local object
1113 rc = lod_sub_declare_attr_set(env, next, attr, th);
1117 /* osp_declare_attr_set() ignores all attributes other than
1118 * UID, GID, PROJID, and size, and osp_attr_set() ignores all
1119 * but UID, GID and PROJID. Declaration of size attr setting
1120 * happens through lod_declare_init_size(), and not through
1121 * this function. Therefore we need not load striping unless
1122 * ownership is changing. This should save memory and (we hope)
1123 * speed up rename().
1125 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1126 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID)))
1129 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1132 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID | LA_MODE |
1133 LA_ATIME | LA_MTIME | LA_CTIME |
1138 * load striping information, notice we don't do this when object
1139 * is being initialized as we don't need this information till
1140 * few specific cases like destroy, chown
1142 rc = lod_load_striping(env, lo);
1146 if (!lod_obj_is_striped(dt))
1150 * if object is striped declare changes on the stripes
1152 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1153 LASSERT(lo->ldo_stripe);
1154 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
1155 if (lo->ldo_stripe[i] == NULL)
1157 rc = lod_sub_declare_attr_set(env, lo->ldo_stripe[i],
1163 struct lod_obj_stripe_cb_data data;
1165 data.locd_attr = attr;
1166 data.locd_declare = true;
1167 rc = lod_obj_for_each_stripe(env, lo, th,
1168 lod_obj_stripe_attr_set_cb, &data);
1174 if (!dt_object_exists(next) || dt_object_remote(next) ||
1175 !S_ISREG(attr->la_mode))
1178 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1179 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
1183 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1184 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1185 struct lod_thread_info *info = lod_env_info(env);
1186 struct lu_buf *buf = &info->lti_buf;
1188 buf->lb_buf = info->lti_ea_store;
1189 buf->lb_len = info->lti_ea_store_size;
1190 rc = lod_sub_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1191 LU_XATTR_REPLACE, th);
1198 * Implementation of dt_object_operations::do_attr_set.
1200 * If the object is striped, then apply the changes to all or subset of
1201 * the stripes depending on the object type and specific attributes.
1203 * \see dt_object_operations::do_attr_set() in the API description for details.
1205 static int lod_attr_set(const struct lu_env *env,
1206 struct dt_object *dt,
1207 const struct lu_attr *attr,
1210 struct dt_object *next = dt_object_child(dt);
1211 struct lod_object *lo = lod_dt_obj(dt);
1216 * apply changes to the local object
1218 rc = lod_sub_attr_set(env, next, attr, th);
1222 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1223 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID)))
1226 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1229 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE | LA_PROJID |
1230 LA_ATIME | LA_MTIME | LA_CTIME |
1235 if (!lod_obj_is_striped(dt))
1239 * if object is striped, apply changes to all the stripes
1241 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1242 LASSERT(lo->ldo_stripe);
1243 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
1244 if (unlikely(lo->ldo_stripe[i] == NULL))
1247 if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1250 rc = lod_sub_attr_set(env, lo->ldo_stripe[i], attr, th);
1255 struct lod_obj_stripe_cb_data data;
1257 data.locd_attr = attr;
1258 data.locd_declare = false;
1259 rc = lod_obj_for_each_stripe(env, lo, th,
1260 lod_obj_stripe_attr_set_cb, &data);
1266 if (!dt_object_exists(next) || dt_object_remote(next) ||
1267 !S_ISREG(attr->la_mode))
1270 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1271 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
1275 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1276 struct lod_thread_info *info = lod_env_info(env);
1277 struct lu_buf *buf = &info->lti_buf;
1278 struct ost_id *oi = &info->lti_ostid;
1279 struct lu_fid *fid = &info->lti_fid;
1280 struct lov_mds_md_v1 *lmm;
1281 struct lov_ost_data_v1 *objs;
1284 rc = lod_get_lov_ea(env, lo);
1288 buf->lb_buf = info->lti_ea_store;
1289 buf->lb_len = info->lti_ea_store_size;
1290 lmm = info->lti_ea_store;
1291 magic = le32_to_cpu(lmm->lmm_magic);
1292 if (magic == LOV_MAGIC_COMP_V1) {
1293 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1294 struct lov_comp_md_entry_v1 *lcme =
1295 &lcm->lcm_entries[0];
1297 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1298 magic = le32_to_cpu(lmm->lmm_magic);
1301 if (magic == LOV_MAGIC_V1)
1302 objs = &(lmm->lmm_objects[0]);
1304 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1305 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1306 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1308 fid_to_ostid(fid, oi);
1309 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1311 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1312 LU_XATTR_REPLACE, th);
1313 } else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1314 struct lod_thread_info *info = lod_env_info(env);
1315 struct lu_buf *buf = &info->lti_buf;
1316 struct lov_comp_md_v1 *lcm;
1317 struct lov_comp_md_entry_v1 *lcme;
1319 rc = lod_get_lov_ea(env, lo);
1323 buf->lb_buf = info->lti_ea_store;
1324 buf->lb_len = info->lti_ea_store_size;
1326 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
1329 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1330 lcme = &lcm->lcm_entries[0];
1331 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1332 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1334 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1335 LU_XATTR_REPLACE, th);
1342 * Implementation of dt_object_operations::do_xattr_get.
1344 * If LOV EA is requested from the root object and it's not
1345 * found, then return default striping for the filesystem.
1347 * \see dt_object_operations::do_xattr_get() in the API description for details.
1349 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1350 struct lu_buf *buf, const char *name)
1352 struct lod_thread_info *info = lod_env_info(env);
1353 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1358 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1359 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1360 struct lmv_mds_md_v1 *lmv1;
1363 if (rc > (typeof(rc))sizeof(*lmv1))
1366 if (rc < (typeof(rc))sizeof(*lmv1))
1367 RETURN(rc = rc > 0 ? -EINVAL : rc);
1369 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1370 CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1372 info->lti_buf.lb_buf = info->lti_key;
1373 info->lti_buf.lb_len = sizeof(*lmv1);
1374 rc = dt_xattr_get(env, dt_object_child(dt),
1375 &info->lti_buf, name);
1376 if (unlikely(rc != sizeof(*lmv1)))
1377 RETURN(rc = rc > 0 ? -EINVAL : rc);
1379 lmv1 = info->lti_buf.lb_buf;
1380 /* The on-disk LMV EA only contains header, but the
1381 * returned LMV EA size should contain the space for
1382 * the FIDs of all shards of the striped directory. */
1383 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1384 rc = lmv_mds_md_size(
1385 le32_to_cpu(lmv1->lmv_stripe_count),
1388 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1392 RETURN(rc = rc1 != 0 ? rc1 : rc);
1395 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1399 * XXX: Only used by lfsck
1401 * lod returns default striping on the real root of the device
1402 * this is like the root stores default striping for the whole
1403 * filesystem. historically we've been using a different approach
1404 * and store it in the config.
1406 dt_root_get(env, dev->lod_child, &info->lti_fid);
1407 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1409 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1410 struct lov_user_md *lum = buf->lb_buf;
1411 struct lov_desc *desc = &dev->lod_desc;
1413 if (buf->lb_buf == NULL) {
1415 } else if (buf->lb_len >= sizeof(*lum)) {
1416 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1417 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1418 lmm_oi_set_id(&lum->lmm_oi, 0);
1419 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1420 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1421 lum->lmm_stripe_size = cpu_to_le32(
1422 desc->ld_default_stripe_size);
1423 lum->lmm_stripe_count = cpu_to_le16(
1424 desc->ld_default_stripe_count);
1425 lum->lmm_stripe_offset = cpu_to_le16(
1426 desc->ld_default_stripe_offset);
1439 * Checks that the magic of the stripe is sane.
1441 * \param[in] lod lod device
1442 * \param[in] lum a buffer storing LMV EA to verify
1444 * \retval 0 if the EA is sane
1445 * \retval negative otherwise
1447 static int lod_verify_md_striping(struct lod_device *lod,
1448 const struct lmv_user_md_v1 *lum)
1450 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1451 CERROR("%s: invalid lmv_user_md: magic = %x, "
1452 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1453 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1454 (int)le32_to_cpu(lum->lum_stripe_offset),
1455 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1463 * Initialize LMV EA for a slave.
1465 * Initialize slave's LMV EA from the master's LMV EA.
1467 * \param[in] master_lmv a buffer containing master's EA
1468 * \param[out] slave_lmv a buffer where slave's EA will be stored
1471 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1472 const struct lmv_mds_md_v1 *master_lmv)
1474 *slave_lmv = *master_lmv;
1475 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1481 * Generate LMV EA from the object passed as \a dt. The object must have
1482 * the stripes created and initialized.
1484 * \param[in] env execution environment
1485 * \param[in] dt object
1486 * \param[out] lmv_buf buffer storing generated LMV EA
1488 * \retval 0 on success
1489 * \retval negative if failed
1491 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1492 struct lu_buf *lmv_buf)
1494 struct lod_thread_info *info = lod_env_info(env);
1495 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1496 struct lod_object *lo = lod_dt_obj(dt);
1497 struct lmv_mds_md_v1 *lmm1;
1499 int type = LU_SEQ_RANGE_ANY;
1504 LASSERT(lo->ldo_dir_striped != 0);
1505 LASSERT(lo->ldo_dir_stripenr > 0);
1506 stripe_count = lo->ldo_dir_stripenr;
1507 /* Only store the LMV EA heahder on the disk. */
1508 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1509 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1513 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1516 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1517 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1518 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1519 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1520 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1525 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1526 lmv_buf->lb_buf = info->lti_ea_store;
1527 lmv_buf->lb_len = sizeof(*lmm1);
1533 * Create in-core represenation for a striped directory.
1535 * Parse the buffer containing LMV EA and instantiate LU objects
1536 * representing the stripe objects. The pointers to the objects are
1537 * stored in ldo_stripe field of \a lo. This function is used when
1538 * we need to access an already created object (i.e. load from a disk).
1540 * \param[in] env execution environment
1541 * \param[in] lo lod object
1542 * \param[in] buf buffer containing LMV EA
1544 * \retval 0 on success
1545 * \retval negative if failed
1547 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1548 const struct lu_buf *buf)
1550 struct lod_thread_info *info = lod_env_info(env);
1551 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1552 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1553 struct dt_object **stripe;
1554 union lmv_mds_md *lmm = buf->lb_buf;
1555 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1556 struct lu_fid *fid = &info->lti_fid;
1561 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1564 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1565 lo->ldo_dir_slave_stripe = 1;
1569 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1572 if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1575 LASSERT(lo->ldo_stripe == NULL);
1576 OBD_ALLOC(stripe, sizeof(stripe[0]) *
1577 (le32_to_cpu(lmv1->lmv_stripe_count)));
1581 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1582 struct dt_device *tgt_dt;
1583 struct dt_object *dto;
1584 int type = LU_SEQ_RANGE_ANY;
1587 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1588 if (!fid_is_sane(fid))
1589 GOTO(out, rc = -ESTALE);
1591 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1595 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1596 tgt_dt = lod->lod_child;
1598 struct lod_tgt_desc *tgt;
1600 tgt = LTD_TGT(ltd, idx);
1602 GOTO(out, rc = -ESTALE);
1603 tgt_dt = tgt->ltd_tgt;
1606 dto = dt_locate_at(env, tgt_dt, fid,
1607 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1610 GOTO(out, rc = PTR_ERR(dto));
1615 lo->ldo_stripe = stripe;
1616 lo->ldo_dir_stripenr = le32_to_cpu(lmv1->lmv_stripe_count);
1617 lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1619 lod_object_free_striping(env, lo);
1625 * Declare create a striped directory.
1627 * Declare creating a striped directory with a given stripe pattern on the
1628 * specified MDTs. A striped directory is represented as a regular directory
1629 * - an index listing all the stripes. The stripes point back to the master
1630 * object with ".." and LinkEA. The master object gets LMV EA which
1631 * identifies it as a striped directory. The function allocates FIDs
1634 * \param[in] env execution environment
1635 * \param[in] dt object
1636 * \param[in] attr attributes to initialize the objects with
1637 * \param[in] dof type of objects to be created
1638 * \param[in] th transaction handle
1640 * \retval 0 on success
1641 * \retval negative if failed
1643 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1644 struct dt_object *dt,
1645 struct lu_attr *attr,
1646 struct dt_object_format *dof,
1649 struct lod_thread_info *info = lod_env_info(env);
1650 struct lu_buf lmv_buf;
1651 struct lu_buf slave_lmv_buf;
1652 struct lmv_mds_md_v1 *lmm;
1653 struct lmv_mds_md_v1 *slave_lmm = NULL;
1654 struct dt_insert_rec *rec = &info->lti_dt_rec;
1655 struct lod_object *lo = lod_dt_obj(dt);
1660 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1663 lmm = lmv_buf.lb_buf;
1665 OBD_ALLOC_PTR(slave_lmm);
1666 if (slave_lmm == NULL)
1667 GOTO(out, rc = -ENOMEM);
1669 lod_prep_slave_lmv_md(slave_lmm, lmm);
1670 slave_lmv_buf.lb_buf = slave_lmm;
1671 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1673 if (!dt_try_as_dir(env, dt_object_child(dt)))
1674 GOTO(out, rc = -EINVAL);
1676 rec->rec_type = S_IFDIR;
1677 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
1678 struct dt_object *dto = lo->ldo_stripe[i];
1679 char *stripe_name = info->lti_key;
1680 struct lu_name *sname;
1681 struct linkea_data ldata = { NULL };
1682 struct lu_buf linkea_buf;
1684 rc = lod_sub_declare_create(env, dto, attr, NULL, dof, th);
1688 if (!dt_try_as_dir(env, dto))
1689 GOTO(out, rc = -EINVAL);
1691 rc = lod_sub_declare_ref_add(env, dto, th);
1695 rec->rec_fid = lu_object_fid(&dto->do_lu);
1696 rc = lod_sub_declare_insert(env, dto,
1697 (const struct dt_rec *)rec,
1698 (const struct dt_key *)dot, th);
1702 /* master stripe FID will be put to .. */
1703 rec->rec_fid = lu_object_fid(&dt->do_lu);
1704 rc = lod_sub_declare_insert(env, dto,
1705 (const struct dt_rec *)rec,
1706 (const struct dt_key *)dotdot, th);
1710 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1711 cfs_fail_val != i) {
1712 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1714 slave_lmm->lmv_master_mdt_index =
1717 slave_lmm->lmv_master_mdt_index =
1719 rc = lod_sub_declare_xattr_set(env, dto, &slave_lmv_buf,
1720 XATTR_NAME_LMV, 0, th);
1725 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1727 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1728 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1730 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1731 PFID(lu_object_fid(&dto->do_lu)), i);
1733 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1734 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1735 sname, lu_object_fid(&dt->do_lu));
1739 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1740 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1741 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
1742 XATTR_NAME_LINK, 0, th);
1746 rec->rec_fid = lu_object_fid(&dto->do_lu);
1747 rc = lod_sub_declare_insert(env, dt_object_child(dt),
1748 (const struct dt_rec *)rec,
1749 (const struct dt_key *)stripe_name,
1754 rc = lod_sub_declare_ref_add(env, dt_object_child(dt), th);
1759 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt),
1760 &lmv_buf, XATTR_NAME_LMV, 0, th);
1764 if (slave_lmm != NULL)
1765 OBD_FREE_PTR(slave_lmm);
1770 static int lod_prep_md_striped_create(const struct lu_env *env,
1771 struct dt_object *dt,
1772 struct lu_attr *attr,
1773 const struct lmv_user_md_v1 *lum,
1774 struct dt_object_format *dof,
1777 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1778 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1779 struct lod_object *lo = lod_dt_obj(dt);
1780 struct dt_object **stripe;
1789 /* The lum has been verifed in lod_verify_md_striping */
1790 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
1791 LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1793 stripe_count = le32_to_cpu(lum->lum_stripe_count);
1795 OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1796 if (idx_array == NULL)
1799 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1801 GOTO(out_free, rc = -ENOMEM);
1803 /* Start index must be the master MDT */
1804 master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1805 idx_array[0] = master_index;
1806 for (i = 0; i < stripe_count; i++) {
1807 struct lod_tgt_desc *tgt = NULL;
1808 struct dt_object *dto;
1809 struct lu_fid fid = { 0 };
1811 struct lu_object_conf conf = { 0 };
1812 struct dt_device *tgt_dt = NULL;
1814 /* Try to find next avaible target */
1816 for (j = 0; j < lod->lod_remote_mdt_count;
1817 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1818 bool already_allocated = false;
1821 CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
1822 idx, lod->lod_remote_mdt_count + 1, i);
1824 if (likely(!OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
1825 /* check whether the idx already exists
1826 * in current allocated array */
1827 for (k = 0; k < i; k++) {
1828 if (idx_array[k] == idx) {
1829 already_allocated = true;
1834 if (already_allocated)
1838 /* Sigh, this index is not in the bitmap, let's check
1839 * next available target */
1840 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) &&
1841 idx != master_index)
1844 if (idx == master_index) {
1845 /* Allocate the FID locally */
1846 rc = obd_fid_alloc(env, lod->lod_child_exp,
1850 tgt_dt = lod->lod_child;
1854 /* check the status of the OSP */
1855 tgt = LTD_TGT(ltd, idx);
1859 tgt_dt = tgt->ltd_tgt;
1860 rc = dt_statfs(env, tgt_dt, NULL);
1862 /* this OSP doesn't feel well */
1867 rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1876 /* Can not allocate more stripes */
1877 if (j == lod->lod_remote_mdt_count) {
1878 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1879 lod2obd(lod)->obd_name, stripe_count, i);
1883 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
1884 idx, i, PFID(&fid));
1886 /* Set the start index for next stripe allocation */
1887 if (i < stripe_count - 1)
1888 idx_array[i + 1] = (idx + 1) %
1889 (lod->lod_remote_mdt_count + 1);
1890 /* tgt_dt and fid must be ready after search avaible OSP
1891 * in the above loop */
1892 LASSERT(tgt_dt != NULL);
1893 LASSERT(fid_is_sane(&fid));
1894 conf.loc_flags = LOC_F_NEW;
1895 dto = dt_locate_at(env, tgt_dt, &fid,
1896 dt->do_lu.lo_dev->ld_site->ls_top_dev,
1899 GOTO(out_put, rc = PTR_ERR(dto));
1903 lo->ldo_dir_striped = 1;
1904 lo->ldo_stripe = stripe;
1905 lo->ldo_dir_stripenr = i;
1906 lo->ldo_dir_stripes_allocated = stripe_count;
1908 if (lo->ldo_dir_stripenr == 0)
1909 GOTO(out_put, rc = -ENOSPC);
1911 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
1917 for (i = 0; i < stripe_count; i++)
1918 if (stripe[i] != NULL)
1919 dt_object_put(env, stripe[i]);
1920 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
1921 lo->ldo_dir_stripenr = 0;
1922 lo->ldo_dir_stripes_allocated = 0;
1923 lo->ldo_stripe = NULL;
1927 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
1933 * Declare create striped md object.
1935 * The function declares intention to create a striped directory. This is a
1936 * wrapper for lod_prep_md_striped_create(). The only additional functionality
1937 * is to verify pattern \a lum_buf is good. Check that function for the details.
1939 * \param[in] env execution environment
1940 * \param[in] dt object
1941 * \param[in] attr attributes to initialize the objects with
1942 * \param[in] lum_buf a pattern specifying the number of stripes and
1944 * \param[in] dof type of objects to be created
1945 * \param[in] th transaction handle
1947 * \retval 0 on success
1948 * \retval negative if failed
1951 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
1952 struct dt_object *dt,
1953 struct lu_attr *attr,
1954 const struct lu_buf *lum_buf,
1955 struct dt_object_format *dof,
1958 struct lod_object *lo = lod_dt_obj(dt);
1959 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1960 struct lmv_user_md_v1 *lum;
1964 lum = lum_buf->lb_buf;
1965 LASSERT(lum != NULL);
1967 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
1968 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
1969 (int)le32_to_cpu(lum->lum_stripe_offset));
1971 if (le32_to_cpu(lum->lum_stripe_count) == 0)
1974 rc = lod_verify_md_striping(lod, lum);
1978 /* prepare dir striped objects */
1979 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
1981 /* failed to create striping, let's reset
1982 * config so that others don't get confused */
1983 lod_object_free_striping(env, lo);
1991 * Implementation of dt_object_operations::do_declare_xattr_set.
1993 * Used with regular (non-striped) objects. Basically it
1994 * initializes the striping information and applies the
1995 * change to all the stripes.
1997 * \see dt_object_operations::do_declare_xattr_set() in the API description
2000 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2001 struct dt_object *dt,
2002 const struct lu_buf *buf,
2003 const char *name, int fl,
2006 struct dt_object *next = dt_object_child(dt);
2007 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2008 struct lod_object *lo = lod_dt_obj(dt);
2013 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2014 struct lmv_user_md_v1 *lum;
2016 LASSERT(buf != NULL && buf->lb_buf != NULL);
2018 rc = lod_verify_md_striping(d, lum);
2021 } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2022 rc = lod_verify_striping(d, buf, false, 0);
2027 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2031 /* Note: Do not set LinkEA on sub-stripes, otherwise
2032 * it will confuse the fid2path process(see mdt_path_current()).
2033 * The linkEA between master and sub-stripes is set in
2034 * lod_xattr_set_lmv(). */
2035 if (strcmp(name, XATTR_NAME_LINK) == 0)
2038 /* set xattr to each stripes, if needed */
2039 rc = lod_load_striping(env, lo);
2043 if (lo->ldo_dir_stripenr == 0)
2046 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2047 LASSERT(lo->ldo_stripe[i]);
2049 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2059 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2060 struct lod_object *lo,
2061 struct dt_object *dt, struct thandle *th,
2063 struct lod_obj_stripe_cb_data *data)
2065 struct lod_thread_info *info = lod_env_info(env);
2066 struct filter_fid *ff = &info->lti_ff;
2067 struct lu_buf *buf = &info->lti_buf;
2071 buf->lb_len = sizeof(*ff);
2072 rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2079 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2080 ff->ff_parent.f_ver = stripe_idx;
2081 fid_cpu_to_le(&ff->ff_parent, &ff->ff_parent);
2082 if (data->locd_declare)
2083 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2084 LU_XATTR_REPLACE, th);
2086 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2087 LU_XATTR_REPLACE, th);
2093 * Reset parent FID on OST object
2095 * Replace parent FID with @dt object FID, which is only called during migration
2096 * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2097 * the FID is changed.
2099 * \param[in] env execution environment
2100 * \param[in] dt dt_object whose stripes's parent FID will be reset
2101 * \parem[in] th thandle
2102 * \param[in] declare if it is declare
2104 * \retval 0 if reset succeeds
2105 * \retval negative errno if reset fails
2107 static int lod_replace_parent_fid(const struct lu_env *env,
2108 struct dt_object *dt,
2109 struct thandle *th, bool declare)
2111 struct lod_object *lo = lod_dt_obj(dt);
2112 struct lod_thread_info *info = lod_env_info(env);
2113 struct lu_buf *buf = &info->lti_buf;
2114 struct filter_fid *ff;
2115 struct lod_obj_stripe_cb_data data;
2119 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2121 /* set xattr to each stripes, if needed */
2122 rc = lod_load_striping(env, lo);
2126 if (!lod_obj_is_striped(dt))
2129 if (info->lti_ea_store_size < sizeof(*ff)) {
2130 rc = lod_ea_store_resize(info, sizeof(*ff));
2135 buf->lb_buf = info->lti_ea_store;
2136 buf->lb_len = info->lti_ea_store_size;
2138 data.locd_declare = declare;
2139 rc = lod_obj_for_each_stripe(env, lo, th,
2140 lod_obj_stripe_replace_parent_fid_cb, &data);
2145 inline __u16 lod_comp_entry_stripecnt(struct lod_object *lo,
2146 struct lod_layout_component *entry,
2149 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2153 else if (lod_comp_inited(entry))
2154 return entry->llc_stripenr;
2155 else if ((__u16)-1 == entry->llc_stripenr)
2156 return lod->lod_desc.ld_tgt_count;
2158 return lod_get_stripecnt(lod, lo, entry->llc_stripenr);
2161 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2163 int magic, size = 0, i;
2164 struct lod_layout_component *comp_entries;
2169 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2170 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2172 lo->ldo_def_striping->lds_def_striping_is_composite;
2174 comp_cnt = lo->ldo_comp_cnt;
2175 comp_entries = lo->ldo_comp_entries;
2176 is_composite = lo->ldo_is_composite;
2180 LASSERT(comp_cnt != 0 && comp_entries != NULL);
2182 size = sizeof(struct lov_comp_md_v1) +
2183 sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2184 LASSERT(size % sizeof(__u64) == 0);
2187 for (i = 0; i < comp_cnt; i++) {
2190 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2191 stripenr = lod_comp_entry_stripecnt(lo, &comp_entries[i],
2193 size += lov_user_md_size(stripenr, magic);
2194 LASSERT(size % sizeof(__u64) == 0);
2200 * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2201 * the xattr value is binary lov_comp_md_v1 which contains component(s)
2204 * \param[in] env execution environment
2205 * \param[in] dt dt_object to add components on
2206 * \param[in] buf buffer contains components to be added
2207 * \parem[in] th thandle
2209 * \retval 0 on success
2210 * \retval negative errno on failure
2212 static int lod_declare_layout_add(const struct lu_env *env,
2213 struct dt_object *dt,
2214 const struct lu_buf *buf,
2217 struct lod_thread_info *info = lod_env_info(env);
2218 struct lod_layout_component *comp_array, *lod_comp;
2219 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2220 struct dt_object *next = dt_object_child(dt);
2221 struct lov_desc *desc = &d->lod_desc;
2222 struct lod_object *lo = lod_dt_obj(dt);
2223 struct lov_user_md_v3 *v3;
2224 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2227 int i, rc, array_cnt;
2230 LASSERT(lo->ldo_is_composite);
2232 prev_end = lo->ldo_comp_entries[lo->ldo_comp_cnt - 1].llc_extent.e_end;
2233 rc = lod_verify_striping(d, buf, false, prev_end);
2237 magic = comp_v1->lcm_magic;
2238 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2239 lustre_swab_lov_comp_md_v1(comp_v1);
2240 magic = comp_v1->lcm_magic;
2243 if (magic != LOV_USER_MAGIC_COMP_V1)
2246 array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2247 OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2248 if (comp_array == NULL)
2251 memcpy(comp_array, lo->ldo_comp_entries,
2252 sizeof(*comp_array) * lo->ldo_comp_cnt);
2254 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2255 struct lov_user_md_v1 *v1;
2256 struct lu_extent *ext;
2258 v1 = (struct lov_user_md *)((char *)comp_v1 +
2259 comp_v1->lcm_entries[i].lcme_offset);
2260 ext = &comp_v1->lcm_entries[i].lcme_extent;
2262 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2263 lod_comp->llc_extent.e_start = ext->e_start;
2264 lod_comp->llc_extent.e_end = ext->e_end;
2265 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2267 lod_comp->llc_stripenr = v1->lmm_stripe_count;
2268 if (!lod_comp->llc_stripenr ||
2269 lod_comp->llc_stripenr == (__u16)-1)
2270 lod_comp->llc_stripenr = desc->ld_default_stripe_count;
2271 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2272 if (!lod_comp->llc_stripe_size)
2273 lod_comp->llc_stripe_size =
2274 desc->ld_default_stripe_size;
2276 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2277 v3 = (struct lov_user_md_v3 *) v1;
2278 if (v3->lmm_pool_name[0] != '\0') {
2279 rc = lod_set_pool(&lod_comp->llc_pool,
2287 OBD_FREE(lo->ldo_comp_entries, sizeof(*lod_comp) * lo->ldo_comp_cnt);
2288 lo->ldo_comp_entries = comp_array;
2289 lo->ldo_comp_cnt = array_cnt;
2290 /* No need to increase layout generation here, it will be increased
2291 * later when generating component ID for the new components */
2293 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2294 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2295 XATTR_NAME_LOV, 0, th);
2302 for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2303 lod_comp = &comp_array[i];
2304 if (lod_comp->llc_pool != NULL) {
2305 OBD_FREE(lod_comp->llc_pool,
2306 strlen(lod_comp->llc_pool) + 1);
2307 lod_comp->llc_pool = NULL;
2310 OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2315 * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2316 * the '$field' can only be 'flags' now. The xattr value is binary
2317 * lov_comp_md_v1 which contains the component ID(s) and the value of
2318 * the field to be modified.
2320 * \param[in] env execution environment
2321 * \param[in] dt dt_object to be modified
2322 * \param[in] op operation string, like "set.flags"
2323 * \param[in] buf buffer contains components to be set
2324 * \parem[in] th thandle
2326 * \retval 0 on success
2327 * \retval negative errno on failure
2329 static int lod_declare_layout_set(const struct lu_env *env,
2330 struct dt_object *dt,
2331 char *op, const struct lu_buf *buf,
2334 struct lod_layout_component *lod_comp;
2335 struct lod_thread_info *info = lod_env_info(env);
2336 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2337 struct lod_object *lo = lod_dt_obj(dt);
2338 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2341 bool changed = false;
2344 if (strcmp(op, "set.flags") != 0) {
2345 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2346 lod2obd(d)->obd_name, op);
2350 magic = comp_v1->lcm_magic;
2351 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2352 lustre_swab_lov_comp_md_v1(comp_v1);
2353 magic = comp_v1->lcm_magic;
2356 if (magic != LOV_USER_MAGIC_COMP_V1)
2359 if (comp_v1->lcm_entry_count == 0) {
2360 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2361 lod2obd(d)->obd_name);
2365 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2366 id = comp_v1->lcm_entries[i].lcme_id;
2368 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2369 lod_comp = &lo->ldo_comp_entries[j];
2370 if (id == lod_comp->llc_id || id == LCME_ID_ALL) {
2371 lod_comp->llc_flags =
2372 comp_v1->lcm_entries[i].lcme_flags;
2379 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2380 lod2obd(d)->obd_name);
2384 lod_obj_inc_layout_gen(lo);
2386 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2387 rc = lod_sub_declare_xattr_set(env, dt, &info->lti_buf,
2388 XATTR_NAME_LOV, 0, th);
2393 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2394 * and the xattr value is a unique component ID or a special lcme_id.
2396 * \param[in] env execution environment
2397 * \param[in] dt dt_object to be operated on
2398 * \param[in] buf buffer contains component ID or lcme_id
2399 * \parem[in] th thandle
2401 * \retval 0 on success
2402 * \retval negative errno on failure
2404 static int lod_declare_layout_del(const struct lu_env *env,
2405 struct dt_object *dt,
2406 const struct lu_buf *buf,
2409 struct lod_thread_info *info = lod_env_info(env);
2410 struct dt_object *next = dt_object_child(dt);
2411 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2412 struct lod_object *lo = lod_dt_obj(dt);
2413 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2414 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2415 __u32 magic, id, flags, neg_flags = 0;
2419 LASSERT(lo->ldo_is_composite);
2421 rc = lod_verify_striping(d, buf, false, 0);
2425 magic = comp_v1->lcm_magic;
2426 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2427 lustre_swab_lov_comp_md_v1(comp_v1);
2428 magic = comp_v1->lcm_magic;
2431 if (magic != LOV_USER_MAGIC_COMP_V1)
2434 id = comp_v1->lcm_entries[0].lcme_id;
2435 flags = comp_v1->lcm_entries[0].lcme_flags;
2437 if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
2438 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
2439 lod2obd(d)->obd_name, id, flags);
2443 if (id != LCME_ID_INVAL && flags != 0) {
2444 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
2445 lod2obd(d)->obd_name);
2449 if (flags & LCME_FL_NEG) {
2450 neg_flags = flags & ~LCME_FL_NEG;
2454 left = lo->ldo_comp_cnt;
2458 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
2459 struct lod_layout_component *lod_comp;
2461 lod_comp = &lo->ldo_comp_entries[i];
2463 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
2465 else if (flags && !(flags & lod_comp->llc_flags))
2467 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
2470 if (left != (i + 1)) {
2471 CDEBUG(D_LAYOUT, "%s: this deletion will create "
2472 "a hole.\n", lod2obd(d)->obd_name);
2477 /* Mark the component as deleted */
2478 lod_comp->llc_id = LCME_ID_INVAL;
2480 /* Not instantiated component */
2481 if (lod_comp->llc_stripe == NULL)
2484 LASSERT(lod_comp->llc_stripenr > 0);
2485 for (j = 0; j < lod_comp->llc_stripenr; j++) {
2486 struct dt_object *obj = lod_comp->llc_stripe[j];
2490 rc = lod_sub_declare_destroy(env, obj, th);
2496 LASSERTF(left >= 0, "left = %d\n", left);
2497 if (left == lo->ldo_comp_cnt) {
2498 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
2499 lod2obd(d)->obd_name, id);
2503 memset(attr, 0, sizeof(*attr));
2504 attr->la_valid = LA_SIZE;
2505 rc = lod_sub_declare_attr_set(env, next, attr, th);
2510 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2511 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2512 XATTR_NAME_LOV, 0, th);
2514 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
2521 * Declare layout add/set/del operations issued by special xattr names:
2523 * XATTR_LUSTRE_LOV.add add component(s) to existing file
2524 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
2525 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
2527 * \param[in] env execution environment
2528 * \param[in] dt object
2529 * \param[in] name name of xattr
2530 * \param[in] buf lu_buf contains xattr value
2531 * \param[in] th transaction handle
2533 * \retval 0 on success
2534 * \retval negative if failed
2536 static int lod_declare_modify_layout(const struct lu_env *env,
2537 struct dt_object *dt,
2539 const struct lu_buf *buf,
2542 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2543 struct lod_object *lo = lod_dt_obj(dt);
2544 struct dt_object *next = dt_object_child(&lo->ldo_obj);
2546 int rc, len = strlen(XATTR_LUSTRE_LOV);
2549 LASSERT(dt_object_exists(dt));
2551 if (strlen(name) <= len || name[len] != '.') {
2552 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
2553 lod2obd(d)->obd_name, name);
2558 dt_write_lock(env, next, 0);
2559 rc = lod_load_striping_locked(env, lo);
2563 /* the layout to be modified must be a composite layout */
2564 if (!lo->ldo_is_composite) {
2565 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
2566 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
2567 GOTO(unlock, rc = -EINVAL);
2570 op = (char *)name + len;
2571 if (strcmp(op, "add") == 0) {
2572 rc = lod_declare_layout_add(env, dt, buf, th);
2573 } else if (strcmp(op, "del") == 0) {
2574 rc = lod_declare_layout_del(env, dt, buf, th);
2575 } else if (strncmp(op, "set", strlen("set")) == 0) {
2576 rc = lod_declare_layout_set(env, dt, op, buf, th);
2578 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
2579 lod2obd(d)->obd_name, name);
2580 GOTO(unlock, rc = -ENOTSUPP);
2584 lod_object_free_striping(env, lo);
2585 dt_write_unlock(env, next);
2591 * Implementation of dt_object_operations::do_declare_xattr_set.
2593 * \see dt_object_operations::do_declare_xattr_set() in the API description
2596 * the extension to the API:
2597 * - declaring LOVEA requests striping creation
2598 * - LU_XATTR_REPLACE means layout swap
2600 static int lod_declare_xattr_set(const struct lu_env *env,
2601 struct dt_object *dt,
2602 const struct lu_buf *buf,
2603 const char *name, int fl,
2606 struct dt_object *next = dt_object_child(dt);
2607 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2612 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2613 if ((S_ISREG(mode) || mode == 0) && !(fl & LU_XATTR_REPLACE) &&
2614 (strcmp(name, XATTR_NAME_LOV) == 0 ||
2615 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
2617 * this is a request to create object's striping.
2619 * allow to declare predefined striping on a new (!mode) object
2620 * which is supposed to be replay of regular file creation
2621 * (when LOV setting is declared)
2623 * LU_XATTR_REPLACE is set to indicate a layout swap
2625 if (dt_object_exists(dt)) {
2626 rc = dt_attr_get(env, next, attr);
2630 memset(attr, 0, sizeof(*attr));
2631 attr->la_valid = LA_TYPE | LA_MODE;
2632 attr->la_mode = S_IFREG;
2634 rc = lod_declare_striped_create(env, dt, attr, buf, th);
2635 } else if (S_ISREG(mode) &&
2636 strlen(name) > strlen(XATTR_LUSTRE_LOV) + 1 &&
2637 strncmp(name, XATTR_LUSTRE_LOV,
2638 strlen(XATTR_LUSTRE_LOV)) == 0) {
2640 * this is a request to modify object's striping.
2641 * add/set/del component(s).
2643 if (!dt_object_exists(dt))
2646 rc = lod_declare_modify_layout(env, dt, name, buf, th);
2647 } else if (S_ISDIR(mode)) {
2648 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2649 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
2650 rc = lod_replace_parent_fid(env, dt, th, true);
2652 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2659 * Apply xattr changes to the object.
2661 * Applies xattr changes to the object and the stripes if the latter exist.
2663 * \param[in] env execution environment
2664 * \param[in] dt object
2665 * \param[in] buf buffer pointing to the new value of xattr
2666 * \param[in] name name of xattr
2667 * \param[in] fl flags
2668 * \param[in] th transaction handle
2670 * \retval 0 on success
2671 * \retval negative if failed
2673 static int lod_xattr_set_internal(const struct lu_env *env,
2674 struct dt_object *dt,
2675 const struct lu_buf *buf,
2676 const char *name, int fl,
2679 struct dt_object *next = dt_object_child(dt);
2680 struct lod_object *lo = lod_dt_obj(dt);
2685 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
2686 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2689 /* Note: Do not set LinkEA on sub-stripes, otherwise
2690 * it will confuse the fid2path process(see mdt_path_current()).
2691 * The linkEA between master and sub-stripes is set in
2692 * lod_xattr_set_lmv(). */
2693 if (lo->ldo_dir_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2696 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2697 LASSERT(lo->ldo_stripe[i]);
2699 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
2709 * Delete an extended attribute.
2711 * Deletes specified xattr from the object and the stripes if the latter exist.
2713 * \param[in] env execution environment
2714 * \param[in] dt object
2715 * \param[in] name name of xattr
2716 * \param[in] th transaction handle
2718 * \retval 0 on success
2719 * \retval negative if failed
2721 static int lod_xattr_del_internal(const struct lu_env *env,
2722 struct dt_object *dt,
2723 const char *name, struct thandle *th)
2725 struct dt_object *next = dt_object_child(dt);
2726 struct lod_object *lo = lod_dt_obj(dt);
2731 rc = lod_sub_xattr_del(env, next, name, th);
2732 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2735 if (lo->ldo_dir_stripenr == 0)
2738 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2739 LASSERT(lo->ldo_stripe[i]);
2741 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
2750 * Set default striping on a directory.
2752 * Sets specified striping on a directory object unless it matches the default
2753 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2754 * EA. This striping will be used when regular file is being created in this
2757 * \param[in] env execution environment
2758 * \param[in] dt the striped object
2759 * \param[in] buf buffer with the striping
2760 * \param[in] name name of EA
2761 * \param[in] fl xattr flag (see OSD API description)
2762 * \param[in] th transaction handle
2764 * \retval 0 on success
2765 * \retval negative if failed
2767 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
2768 struct dt_object *dt,
2769 const struct lu_buf *buf,
2770 const char *name, int fl,
2773 struct lov_user_md_v1 *lum;
2774 struct lov_user_md_v3 *v3 = NULL;
2775 const char *pool_name = NULL;
2780 LASSERT(buf != NULL && buf->lb_buf != NULL);
2783 switch (lum->lmm_magic) {
2784 case LOV_USER_MAGIC_V3:
2786 if (v3->lmm_pool_name[0] != '\0')
2787 pool_name = v3->lmm_pool_name;
2789 case LOV_USER_MAGIC_V1:
2790 /* if { size, offset, count } = { 0, -1, 0 } and no pool
2791 * (i.e. all default values specified) then delete default
2792 * striping from dir. */
2794 "set default striping: sz %u # %u offset %d %s %s\n",
2795 (unsigned)lum->lmm_stripe_size,
2796 (unsigned)lum->lmm_stripe_count,
2797 (int)lum->lmm_stripe_offset,
2798 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
2800 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
2801 lum->lmm_stripe_count,
2802 lum->lmm_stripe_offset,
2805 case LOV_USER_MAGIC_COMP_V1:
2809 CERROR("Invalid magic %x\n", lum->lmm_magic);
2814 rc = lod_xattr_del_internal(env, dt, name, th);
2818 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2825 * Set default striping on a directory object.
2827 * Sets specified striping on a directory object unless it matches the default
2828 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2829 * EA. This striping will be used when a new directory is being created in the
2832 * \param[in] env execution environment
2833 * \param[in] dt the striped object
2834 * \param[in] buf buffer with the striping
2835 * \param[in] name name of EA
2836 * \param[in] fl xattr flag (see OSD API description)
2837 * \param[in] th transaction handle
2839 * \retval 0 on success
2840 * \retval negative if failed
2842 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
2843 struct dt_object *dt,
2844 const struct lu_buf *buf,
2845 const char *name, int fl,
2848 struct lmv_user_md_v1 *lum;
2852 LASSERT(buf != NULL && buf->lb_buf != NULL);
2855 CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
2856 le32_to_cpu(lum->lum_stripe_count),
2857 (int)le32_to_cpu(lum->lum_stripe_offset));
2859 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
2860 le32_to_cpu(lum->lum_stripe_offset)) &&
2861 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
2862 rc = lod_xattr_del_internal(env, dt, name, th);
2866 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2875 * Turn directory into a striped directory.
2877 * During replay the client sends the striping created before MDT
2878 * failure, then the layer above LOD sends this defined striping
2879 * using ->do_xattr_set(), so LOD uses this method to replay creation
2880 * of the stripes. Notice the original information for the striping
2881 * (#stripes, FIDs, etc) was transferred in declare path.
2883 * \param[in] env execution environment
2884 * \param[in] dt the striped object
2885 * \param[in] buf not used currently
2886 * \param[in] name not used currently
2887 * \param[in] fl xattr flag (see OSD API description)
2888 * \param[in] th transaction handle
2890 * \retval 0 on success
2891 * \retval negative if failed
2893 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
2894 const struct lu_buf *buf, const char *name,
2895 int fl, struct thandle *th)
2897 struct lod_object *lo = lod_dt_obj(dt);
2898 struct lod_thread_info *info = lod_env_info(env);
2899 struct lu_attr *attr = &info->lti_attr;
2900 struct dt_object_format *dof = &info->lti_format;
2901 struct lu_buf lmv_buf;
2902 struct lu_buf slave_lmv_buf;
2903 struct lmv_mds_md_v1 *lmm;
2904 struct lmv_mds_md_v1 *slave_lmm = NULL;
2905 struct dt_insert_rec *rec = &info->lti_dt_rec;
2910 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2913 /* The stripes are supposed to be allocated in declare phase,
2914 * if there are no stripes being allocated, it will skip */
2915 if (lo->ldo_dir_stripenr == 0)
2918 rc = dt_attr_get(env, dt_object_child(dt), attr);
2922 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
2923 LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
2924 dof->dof_type = DFT_DIR;
2926 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
2929 lmm = lmv_buf.lb_buf;
2931 OBD_ALLOC_PTR(slave_lmm);
2932 if (slave_lmm == NULL)
2935 lod_prep_slave_lmv_md(slave_lmm, lmm);
2936 slave_lmv_buf.lb_buf = slave_lmm;
2937 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
2939 rec->rec_type = S_IFDIR;
2940 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2941 struct dt_object *dto;
2942 char *stripe_name = info->lti_key;
2943 struct lu_name *sname;
2944 struct linkea_data ldata = { NULL };
2945 struct lu_buf linkea_buf;
2947 dto = lo->ldo_stripe[i];
2949 dt_write_lock(env, dto, MOR_TGT_CHILD);
2950 rc = lod_sub_create(env, dto, attr, NULL, dof, th);
2952 dt_write_unlock(env, dto);
2956 rc = lod_sub_ref_add(env, dto, th);
2957 dt_write_unlock(env, dto);
2961 rec->rec_fid = lu_object_fid(&dto->do_lu);
2962 rc = lod_sub_insert(env, dto, (const struct dt_rec *)rec,
2963 (const struct dt_key *)dot, th, 0);
2967 rec->rec_fid = lu_object_fid(&dt->do_lu);
2968 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
2969 (const struct dt_key *)dotdot, th, 0);
2973 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
2974 cfs_fail_val != i) {
2975 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
2977 slave_lmm->lmv_master_mdt_index =
2980 slave_lmm->lmv_master_mdt_index =
2983 rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
2984 XATTR_NAME_LMV, fl, th);
2989 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
2991 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2992 PFID(lu_object_fid(&dto->do_lu)), i + 1);
2994 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2995 PFID(lu_object_fid(&dto->do_lu)), i);
2997 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2998 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
2999 sname, lu_object_fid(&dt->do_lu));
3003 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
3004 linkea_buf.lb_len = ldata.ld_leh->leh_len;
3005 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
3006 XATTR_NAME_LINK, 0, th);
3010 rec->rec_fid = lu_object_fid(&dto->do_lu);
3011 rc = lod_sub_insert(env, dt_object_child(dt),
3012 (const struct dt_rec *)rec,
3013 (const struct dt_key *)stripe_name, th, 0);
3017 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
3022 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
3023 rc = lod_sub_xattr_set(env, dt_object_child(dt),
3024 &lmv_buf, XATTR_NAME_LMV, fl, th);
3026 if (slave_lmm != NULL)
3027 OBD_FREE_PTR(slave_lmm);
3033 * Helper function to declare/execute creation of a striped directory
3035 * Called in declare/create object path, prepare striping for a directory
3036 * and prepare defaults data striping for the objects to be created in
3037 * that directory. Notice the function calls "declaration" or "execution"
3038 * methods depending on \a declare param. This is a consequence of the
3039 * current approach while we don't have natural distributed transactions:
3040 * we basically execute non-local updates in the declare phase. So, the
3041 * arguments for the both phases are the same and this is the reason for
3042 * this function to exist.
3044 * \param[in] env execution environment
3045 * \param[in] dt object
3046 * \param[in] attr attributes the stripes will be created with
3047 * \param[in] dof format of stripes (see OSD API description)
3048 * \param[in] th transaction handle
3049 * \param[in] declare where to call "declare" or "execute" methods
3051 * \retval 0 on success
3052 * \retval negative if failed
3054 static int lod_dir_striping_create_internal(const struct lu_env *env,
3055 struct dt_object *dt,
3056 struct lu_attr *attr,
3057 struct dt_object_format *dof,
3061 struct lod_thread_info *info = lod_env_info(env);
3062 struct lod_object *lo = lod_dt_obj(dt);
3063 const struct lod_default_striping *lds = lo->ldo_def_striping;
3067 LASSERT(ergo(lds != NULL,
3068 lds->lds_def_striping_set ||
3069 lds->lds_dir_def_striping_set));
3071 if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripenr,
3072 lo->ldo_dir_stripe_offset)) {
3073 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3074 int stripe_count = lo->ldo_dir_stripenr;
3076 if (info->lti_ea_store_size < sizeof(*v1)) {
3077 rc = lod_ea_store_resize(info, sizeof(*v1));
3080 v1 = info->lti_ea_store;
3083 memset(v1, 0, sizeof(*v1));
3084 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3085 v1->lum_stripe_count = cpu_to_le32(stripe_count);
3086 v1->lum_stripe_offset =
3087 cpu_to_le32(lo->ldo_dir_stripe_offset);
3089 info->lti_buf.lb_buf = v1;
3090 info->lti_buf.lb_len = sizeof(*v1);
3093 rc = lod_declare_xattr_set_lmv(env, dt, attr,
3094 &info->lti_buf, dof, th);
3096 rc = lod_xattr_set_lmv(env, dt, &info->lti_buf,
3097 XATTR_NAME_LMV, 0, th);
3102 /* Transfer default LMV striping from the parent */
3103 if (lds != NULL && lds->lds_dir_def_striping_set &&
3104 !LMVEA_DELETE_VALUES(lds->lds_dir_def_stripenr,
3105 lds->lds_dir_def_stripe_offset)) {
3106 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3108 if (info->lti_ea_store_size < sizeof(*v1)) {
3109 rc = lod_ea_store_resize(info, sizeof(*v1));
3112 v1 = info->lti_ea_store;
3115 memset(v1, 0, sizeof(*v1));
3116 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3117 v1->lum_stripe_count = cpu_to_le32(lds->lds_dir_def_stripenr);
3118 v1->lum_stripe_offset =
3119 cpu_to_le32(lds->lds_dir_def_stripe_offset);
3121 cpu_to_le32(lds->lds_dir_def_hash_type);
3123 info->lti_buf.lb_buf = v1;
3124 info->lti_buf.lb_len = sizeof(*v1);
3126 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3127 XATTR_NAME_DEFAULT_LMV,
3130 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
3132 XATTR_NAME_DEFAULT_LMV, 0,
3138 /* Transfer default LOV striping from the parent */
3139 if (lds != NULL && lds->lds_def_striping_set &&
3140 lds->lds_def_comp_cnt != 0) {
3141 struct lov_mds_md *lmm;
3142 int lmm_size = lod_comp_md_size(lo, true);
3144 if (info->lti_ea_store_size < lmm_size) {
3145 rc = lod_ea_store_resize(info, lmm_size);
3149 lmm = info->lti_ea_store;
3151 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
3155 info->lti_buf.lb_buf = lmm;
3156 info->lti_buf.lb_len = lmm_size;
3159 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3160 XATTR_NAME_LOV, 0, th);
3162 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
3163 XATTR_NAME_LOV, 0, th);
3171 static int lod_declare_dir_striping_create(const struct lu_env *env,
3172 struct dt_object *dt,
3173 struct lu_attr *attr,
3174 struct dt_object_format *dof,
3177 return lod_dir_striping_create_internal(env, dt, attr, dof, th, true);
3180 static int lod_dir_striping_create(const struct lu_env *env,
3181 struct dt_object *dt,
3182 struct lu_attr *attr,
3183 struct dt_object_format *dof,
3186 return lod_dir_striping_create_internal(env, dt, attr, dof, th, false);
3190 * Make LOV EA for striped object.
3192 * Generate striping information and store it in the LOV EA of the given
3193 * object. The caller must ensure nobody else is calling the function
3194 * against the object concurrently. The transaction must be started.
3195 * FLDB service must be running as well; it's used to map FID to the target,
3196 * which is stored in LOV EA.
3198 * \param[in] env execution environment for this thread
3199 * \param[in] lo LOD object
3200 * \param[in] th transaction handle
3202 * \retval 0 if LOV EA is stored successfully
3203 * \retval negative error number on failure
3205 static int lod_generate_and_set_lovea(const struct lu_env *env,
3206 struct lod_object *lo,
3209 struct lod_thread_info *info = lod_env_info(env);
3210 struct dt_object *next = dt_object_child(&lo->ldo_obj);
3211 struct lov_mds_md_v1 *lmm;
3217 if (lo->ldo_comp_cnt == 0) {
3218 lod_object_free_striping(env, lo);
3219 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
3223 lmm_size = lod_comp_md_size(lo, false);
3224 if (info->lti_ea_store_size < lmm_size) {
3225 rc = lod_ea_store_resize(info, lmm_size);
3229 lmm = info->lti_ea_store;
3231 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
3235 info->lti_buf.lb_buf = lmm;
3236 info->lti_buf.lb_len = lmm_size;
3237 rc = lod_sub_xattr_set(env, next, &info->lti_buf,
3238 XATTR_NAME_LOV, 0, th);
3243 * Delete layout component(s)
3245 * \param[in] env execution environment for this thread
3246 * \param[in] dt object
3247 * \param[in] th transaction handle
3249 * \retval 0 on success
3250 * \retval negative error number on failure
3252 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
3255 struct lod_layout_component *lod_comp;
3256 struct lod_object *lo = lod_dt_obj(dt);
3257 struct dt_object *next = dt_object_child(dt);
3258 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3261 LASSERT(lo->ldo_is_composite);
3262 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
3264 left = lo->ldo_comp_cnt;
3265 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3266 lod_comp = &lo->ldo_comp_entries[i];
3268 if (lod_comp->llc_id != LCME_ID_INVAL)
3272 /* Not instantiated component */
3273 if (lod_comp->llc_stripe == NULL)
3276 LASSERT(lod_comp->llc_stripenr > 0);
3277 for (j = 0; j < lod_comp->llc_stripenr; j++) {
3278 struct dt_object *obj = lod_comp->llc_stripe[j];
3282 rc = lod_sub_destroy(env, obj, th);
3286 lu_object_put(env, &obj->do_lu);
3287 lod_comp->llc_stripe[j] = NULL;
3289 OBD_FREE(lod_comp->llc_stripe, sizeof(struct dt_object *) *
3290 lod_comp->llc_stripes_allocated);
3291 lod_comp->llc_stripe = NULL;
3292 lod_comp->llc_stripes_allocated = 0;
3293 lod_obj_set_pool(lo, i, NULL);
3294 if (lod_comp->llc_ostlist.op_array) {
3295 OBD_FREE(lod_comp->llc_ostlist.op_array,
3296 lod_comp->llc_ostlist.op_size);
3297 lod_comp->llc_ostlist.op_array = NULL;
3298 lod_comp->llc_ostlist.op_size = 0;
3302 LASSERTF(left >= 0 && left < lo->ldo_comp_cnt, "left = %d\n", left);
3304 struct lod_layout_component *comp_array;
3306 OBD_ALLOC(comp_array, sizeof(*comp_array) * left);
3307 if (comp_array == NULL)
3308 GOTO(out, rc = -ENOMEM);
3310 memcpy(&comp_array[0], &lo->ldo_comp_entries[0],
3311 sizeof(*comp_array) * left);
3313 OBD_FREE(lo->ldo_comp_entries,
3314 sizeof(*comp_array) * lo->ldo_comp_cnt);
3315 lo->ldo_comp_entries = comp_array;
3316 lo->ldo_comp_cnt = left;
3317 lod_obj_inc_layout_gen(lo);
3319 lod_free_comp_entries(lo);
3322 LASSERT(dt_object_exists(dt));
3323 rc = dt_attr_get(env, next, attr);
3327 if (attr->la_size > 0) {
3329 attr->la_valid = LA_SIZE;
3330 rc = lod_sub_attr_set(env, next, attr, th);
3335 rc = lod_generate_and_set_lovea(env, lo, th);
3339 lod_object_free_striping(env, lo);
3344 * Implementation of dt_object_operations::do_xattr_set.
3346 * Sets specified extended attribute on the object. Three types of EAs are
3348 * LOV EA - stores striping for a regular file or default striping (when set
3350 * LMV EA - stores a marker for the striped directories
3351 * DMV EA - stores default directory striping
3353 * When striping is applied to a non-striped existing object (this is called
3354 * late striping), then LOD notices the caller wants to turn the object into a
3355 * striped one. The stripe objects are created and appropriate EA is set:
3356 * LOV EA storing all the stripes directly or LMV EA storing just a small header
3357 * with striping configuration.
3359 * \see dt_object_operations::do_xattr_set() in the API description for details.
3361 static int lod_xattr_set(const struct lu_env *env,
3362 struct dt_object *dt, const struct lu_buf *buf,
3363 const char *name, int fl, struct thandle *th)
3365 struct dt_object *next = dt_object_child(dt);
3369 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3370 strcmp(name, XATTR_NAME_LMV) == 0) {
3371 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
3373 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
3374 LMV_HASH_FLAG_MIGRATION)
3375 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3377 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
3382 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3383 strcmp(name, XATTR_NAME_LOV) == 0) {
3385 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
3387 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3388 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
3390 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
3393 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3394 (!strcmp(name, XATTR_NAME_LOV) ||
3395 !strncmp(name, XATTR_LUSTRE_LOV,
3396 strlen(XATTR_LUSTRE_LOV)))) {
3397 /* in case of lov EA swap, just set it
3398 * if not, it is a replay so check striping match what we
3399 * already have during req replay, declare_xattr_set()
3400 * defines striping, then create() does the work */
3401 if (fl & LU_XATTR_REPLACE) {
3402 /* free stripes, then update disk */
3403 lod_object_free_striping(env, lod_dt_obj(dt));
3405 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3406 } else if (dt_object_remote(dt)) {
3407 /* This only happens during migration, see
3408 * mdd_migrate_create(), in which Master MDT will
3409 * create a remote target object, and only set
3410 * (migrating) stripe EA on the remote object,
3411 * and does not need creating each stripes. */
3412 rc = lod_sub_xattr_set(env, next, buf, name,
3414 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
3415 /* delete component(s) */
3416 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
3417 rc = lod_layout_del(env, dt, th);
3420 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
3421 * it's going to create create file with specified
3422 * component(s), the striping must have not being
3423 * cached in this case;
3425 * Otherwise, it's going to add/change component(s) to
3426 * an existing file, the striping must have been cached
3429 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
3430 !strcmp(name, XATTR_NAME_LOV),
3431 !lod_dt_obj(dt)->ldo_comp_cached));
3433 rc = lod_striped_create(env, dt, NULL, NULL, th);
3436 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3437 rc = lod_replace_parent_fid(env, dt, th, false);
3442 /* then all other xattr */
3443 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3449 * Implementation of dt_object_operations::do_declare_xattr_del.
3451 * \see dt_object_operations::do_declare_xattr_del() in the API description
3454 static int lod_declare_xattr_del(const struct lu_env *env,
3455 struct dt_object *dt, const char *name,
3458 struct lod_object *lo = lod_dt_obj(dt);
3463 rc = lod_sub_declare_xattr_del(env, dt_object_child(dt), name, th);
3467 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3470 /* set xattr to each stripes, if needed */
3471 rc = lod_load_striping(env, lo);
3475 if (lo->ldo_dir_stripenr == 0)
3478 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
3479 LASSERT(lo->ldo_stripe[i]);
3480 rc = lod_sub_declare_xattr_del(env, lo->ldo_stripe[i],
3490 * Implementation of dt_object_operations::do_xattr_del.
3492 * If EA storing a regular striping is being deleted, then release
3493 * all the references to the stripe objects in core.
3495 * \see dt_object_operations::do_xattr_del() in the API description for details.
3497 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
3498 const char *name, struct thandle *th)
3500 struct dt_object *next = dt_object_child(dt);
3501 struct lod_object *lo = lod_dt_obj(dt);
3506 if (!strcmp(name, XATTR_NAME_LOV))
3507 lod_object_free_striping(env, lod_dt_obj(dt));
3509 rc = lod_sub_xattr_del(env, next, name, th);
3510 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3513 if (lo->ldo_dir_stripenr == 0)
3516 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
3517 LASSERT(lo->ldo_stripe[i]);
3519 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3528 * Implementation of dt_object_operations::do_xattr_list.
3530 * \see dt_object_operations::do_xattr_list() in the API description
3533 static int lod_xattr_list(const struct lu_env *env,
3534 struct dt_object *dt, const struct lu_buf *buf)
3536 return dt_xattr_list(env, dt_object_child(dt), buf);
3539 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
3541 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
3546 * Get default striping.
3548 * \param[in] env execution environment
3549 * \param[in] lo object
3550 * \param[out] lds default striping
3552 * \retval 0 on success
3553 * \retval negative if failed
3555 static int lod_get_default_lov_striping(const struct lu_env *env,
3556 struct lod_object *lo,
3557 struct lod_default_striping *lds)
3559 struct lod_thread_info *info = lod_env_info(env);
3560 struct lov_user_md_v1 *v1 = NULL;
3561 struct lov_user_md_v3 *v3 = NULL;
3562 struct lov_comp_md_v1 *comp_v1 = NULL;
3568 lds->lds_def_striping_set = 0;
3570 rc = lod_get_lov_ea(env, lo);
3574 if (rc < (typeof(rc))sizeof(struct lov_user_md))
3577 v1 = info->lti_ea_store;
3578 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
3579 lustre_swab_lov_user_md_v1(v1);
3580 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
3581 v3 = (struct lov_user_md_v3 *)v1;
3582 lustre_swab_lov_user_md_v3(v3);
3583 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3584 comp_v1 = (struct lov_comp_md_v1 *)v1;
3585 lustre_swab_lov_comp_md_v1(comp_v1);
3588 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
3589 v1->lmm_magic != LOV_MAGIC_COMP_V1)
3592 if (v1->lmm_magic == LOV_MAGIC_COMP_V1) {
3593 comp_v1 = (struct lov_comp_md_v1 *)v1;
3594 comp_cnt = comp_v1->lcm_entry_count;
3603 /* realloc default comp entries if necessary */
3604 rc = lod_def_striping_comp_resize(lds, comp_cnt);
3608 lds->lds_def_comp_cnt = comp_cnt;
3609 lds->lds_def_striping_is_composite = composite ? 1 : 0;
3611 for (i = 0; i < comp_cnt; i++) {
3612 struct lod_layout_component *lod_comp;
3613 struct lu_extent *ext;
3616 lod_comp = &lds->lds_def_comp_entries[i];
3618 * reset lod_comp values, llc_stripes is always NULL in
3619 * the default striping template, llc_pool will be reset
3622 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
3625 v1 = (struct lov_user_md *)((char *)comp_v1 +
3626 comp_v1->lcm_entries[i].lcme_offset);
3627 ext = &comp_v1->lcm_entries[i].lcme_extent;
3628 lod_comp->llc_extent = *ext;
3631 if (v1->lmm_pattern != LOV_PATTERN_RAID0 &&
3632 v1->lmm_pattern != 0) {
3633 lod_free_def_comp_entries(lds);
3637 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d "
3638 "stripe_offset=%d\n",
3639 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
3640 (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
3641 (int)v1->lmm_stripe_offset);
3643 lod_comp->llc_stripenr = v1->lmm_stripe_count;
3644 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
3645 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
3648 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
3649 /* XXX: sanity check here */
3650 v3 = (struct lov_user_md_v3 *) v1;
3651 if (v3->lmm_pool_name[0] != '\0')
3652 pool = v3->lmm_pool_name;
3654 lod_set_def_pool(lds, i, pool);
3657 lds->lds_def_striping_set = 1;
3662 * Get default directory striping.
3664 * \param[in] env execution environment
3665 * \param[in] lo object
3666 * \param[out] lds default striping
3668 * \retval 0 on success
3669 * \retval negative if failed
3671 static int lod_get_default_lmv_striping(const struct lu_env *env,
3672 struct lod_object *lo,
3673 struct lod_default_striping *lds)
3675 struct lod_thread_info *info = lod_env_info(env);
3676 struct lmv_user_md_v1 *v1 = NULL;
3680 lds->lds_dir_def_striping_set = 0;
3681 rc = lod_get_default_lmv_ea(env, lo);
3685 if (rc < (typeof(rc))sizeof(struct lmv_user_md))
3688 v1 = info->lti_ea_store;
3690 lds->lds_dir_def_stripenr = le32_to_cpu(v1->lum_stripe_count);
3691 lds->lds_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
3692 lds->lds_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
3693 lds->lds_dir_def_striping_set = 1;
3699 * Get default striping in the object.
3701 * Get object default striping and default directory striping.
3703 * \param[in] env execution environment
3704 * \param[in] lo object
3705 * \param[out] lds default striping
3707 * \retval 0 on success
3708 * \retval negative if failed
3710 static int lod_get_default_striping(const struct lu_env *env,
3711 struct lod_object *lo,
3712 struct lod_default_striping *lds)
3716 rc = lod_get_default_lov_striping(env, lo, lds);
3717 rc1 = lod_get_default_lmv_striping(env, lo, lds);
3718 if (rc == 0 && rc1 < 0)
3725 * Apply default striping on object.
3727 * If object striping pattern is not set, set to the one in default striping.
3728 * The default striping is from parent or fs.
3730 * \param[in] lo new object
3731 * \param[in] lds default striping
3732 * \param[in] mode new object's mode
3734 static void lod_striping_from_default(struct lod_object *lo,
3735 const struct lod_default_striping *lds,
3738 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
3739 struct lov_desc *desc = &d->lod_desc;
3742 if (lds->lds_def_striping_set && S_ISREG(mode)) {
3743 rc = lod_alloc_comp_entries(lo, lds->lds_def_comp_cnt);
3747 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
3749 for (i = 0; i < lo->ldo_comp_cnt; i++) {
3750 struct lod_layout_component *obj_comp =
3751 &lo->ldo_comp_entries[i];
3752 struct lod_layout_component *def_comp =
3753 &lds->lds_def_comp_entries[i];
3755 CDEBUG(D_LAYOUT, "Inherite from default: size:%hu "
3756 "nr:%u offset:%u %s\n",
3757 def_comp->llc_stripe_size,
3758 def_comp->llc_stripenr,
3759 def_comp->llc_stripe_offset,
3760 def_comp->llc_pool ?: "");
3762 *obj_comp = *def_comp;
3763 if (def_comp->llc_pool != NULL) {
3764 /* pointer was copied from def_comp */
3765 obj_comp->llc_pool = NULL;
3766 lod_obj_set_pool(lo, i, def_comp->llc_pool);
3770 * Don't initialize these fields for plain layout
3771 * (v1/v3) here, they are inherited in the order of
3772 * 'parent' -> 'fs default (root)' -> 'global default
3773 * values for stripe_count & stripe_size'.
3775 * see lod_ah_init().
3777 if (!lo->ldo_is_composite)
3780 if (obj_comp->llc_stripenr <= 0)
3781 obj_comp->llc_stripenr =
3782 desc->ld_default_stripe_count;
3783 if (obj_comp->llc_stripe_size <= 0)
3784 obj_comp->llc_stripe_size =
3785 desc->ld_default_stripe_size;
3787 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
3788 if (lo->ldo_dir_stripenr == 0)
3789 lo->ldo_dir_stripenr = lds->lds_dir_def_stripenr;
3790 if (lo->ldo_dir_stripe_offset == -1)
3791 lo->ldo_dir_stripe_offset =
3792 lds->lds_dir_def_stripe_offset;
3793 if (lo->ldo_dir_hash_type == 0)
3794 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
3796 CDEBUG(D_LAYOUT, "striping from default dir: nr:%hu, "
3797 "offset:%u, hash_type:%u\n",
3798 lo->ldo_dir_stripenr, lo->ldo_dir_stripe_offset,
3799 lo->ldo_dir_hash_type);
3803 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root)
3805 struct lod_layout_component *lod_comp;
3807 if (lo->ldo_comp_cnt == 0)
3810 if (lo->ldo_is_composite)
3813 lod_comp = &lo->ldo_comp_entries[0];
3815 if (lod_comp->llc_stripenr <= 0 ||
3816 lod_comp->llc_stripe_size <= 0)
3819 if (from_root && (lod_comp->llc_pool == NULL ||
3820 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
3827 * Implementation of dt_object_operations::do_ah_init.
3829 * This method is used to make a decision on the striping configuration for the
3830 * object being created. It can be taken from the \a parent object if it exists,
3831 * or filesystem's default. The resulting configuration (number of stripes,
3832 * stripe size/offset, pool name, etc) is stored in the object itself and will
3833 * be used by the methods like ->doo_declare_create().
3835 * \see dt_object_operations::do_ah_init() in the API description for details.
3837 static void lod_ah_init(const struct lu_env *env,
3838 struct dt_allocation_hint *ah,
3839 struct dt_object *parent,
3840 struct dt_object *child,
3843 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
3844 struct lod_thread_info *info = lod_env_info(env);
3845 struct lod_default_striping *lds = &info->lti_def_striping;
3846 struct dt_object *nextp = NULL;
3847 struct dt_object *nextc;
3848 struct lod_object *lp = NULL;
3849 struct lod_object *lc;
3850 struct lov_desc *desc;
3851 struct lod_layout_component *lod_comp;
3857 if (likely(parent)) {
3858 nextp = dt_object_child(parent);
3859 lp = lod_dt_obj(parent);
3862 nextc = dt_object_child(child);
3863 lc = lod_dt_obj(child);
3865 LASSERT(!lod_obj_is_striped(child));
3866 /* default layout template may have been set on the regular file
3867 * when this is called from mdd_create_data() */
3868 if (S_ISREG(child_mode))
3869 lod_free_comp_entries(lc);
3871 if (!dt_object_exists(nextc))
3872 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
3874 if (S_ISDIR(child_mode)) {
3875 /* other default values are 0 */
3876 lc->ldo_dir_stripe_offset = -1;
3878 /* get default striping from parent object */
3879 if (likely(lp != NULL))
3880 lod_get_default_striping(env, lp, lds);
3882 /* set child default striping info, default value is NULL */
3883 if (lds->lds_def_striping_set || lds->lds_dir_def_striping_set)
3884 lc->ldo_def_striping = lds;
3886 /* It should always honour the specified stripes */
3887 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
3888 lod_verify_md_striping(d, ah->dah_eadata) == 0) {
3889 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
3891 lc->ldo_dir_stripenr =
3892 le32_to_cpu(lum1->lum_stripe_count);
3893 lc->ldo_dir_stripe_offset =
3894 le32_to_cpu(lum1->lum_stripe_offset);
3895 lc->ldo_dir_hash_type =
3896 le32_to_cpu(lum1->lum_hash_type);
3897 CDEBUG(D_INFO, "set dir stripe: count %hu, offset %d, "
3899 lc->ldo_dir_stripenr,
3900 (int)lc->ldo_dir_stripe_offset,
3901 lc->ldo_dir_hash_type);
3903 /* transfer defaults LMV to new directory */
3904 lod_striping_from_default(lc, lds, child_mode);
3907 /* shrink the stripe_count to the avaible MDT count */
3908 if (lc->ldo_dir_stripenr > d->lod_remote_mdt_count + 1 &&
3909 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))
3910 lc->ldo_dir_stripenr = d->lod_remote_mdt_count + 1;
3912 /* Directory will be striped only if stripe_count > 1, if
3913 * stripe_count == 1, let's reset stripenr = 0 to avoid
3914 * create single master stripe and also help to unify the
3915 * stripe handling of directories and files */
3916 if (lc->ldo_dir_stripenr == 1)
3917 lc->ldo_dir_stripenr = 0;
3919 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
3920 lc->ldo_dir_stripenr, (int)lc->ldo_dir_stripe_offset,
3921 lc->ldo_dir_hash_type);
3926 /* child object regular file*/
3928 if (!lod_object_will_be_striped(S_ISREG(child_mode),
3929 lu_object_fid(&child->do_lu)))
3932 /* If object is going to be striped over OSTs, transfer default
3933 * striping information to the child, so that we can use it
3934 * during declaration and creation.
3936 * Try from the parent first.
3938 if (likely(lp != NULL)) {
3939 rc = lod_get_default_lov_striping(env, lp, lds);
3941 lod_striping_from_default(lc, lds, child_mode);
3944 /* Initialize lod_device::lod_md_root object reference */
3945 if (d->lod_md_root == NULL) {
3946 struct dt_object *root;
3947 struct lod_object *lroot;
3949 lu_root_fid(&info->lti_fid);
3950 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
3951 if (!IS_ERR(root)) {
3952 lroot = lod_dt_obj(root);
3954 spin_lock(&d->lod_lock);
3955 if (d->lod_md_root != NULL)
3956 dt_object_put(env, &d->lod_md_root->ldo_obj);
3957 d->lod_md_root = lroot;
3958 spin_unlock(&d->lod_lock);
3962 /* try inherit layout from the root object (fs default) when:
3963 * - parent does not have default layout; or
3964 * - parent has plain(v1/v3) default layout, and some attributes
3965 * are not specified in the default layout;
3967 if (d->lod_md_root != NULL && lod_need_inherit_more(lc, true)) {
3968 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds);
3971 if (lc->ldo_comp_cnt == 0) {
3972 lod_striping_from_default(lc, lds, child_mode);
3973 } else if (!lds->lds_def_striping_is_composite) {
3974 struct lod_layout_component *def_comp;
3976 LASSERT(!lc->ldo_is_composite);
3977 lod_comp = &lc->ldo_comp_entries[0];
3978 def_comp = &lds->lds_def_comp_entries[0];
3980 if (lod_comp->llc_stripenr <= 0)
3981 lod_comp->llc_stripenr = def_comp->llc_stripenr;
3982 if (lod_comp->llc_stripe_size <= 0)
3983 lod_comp->llc_stripe_size =
3984 def_comp->llc_stripe_size;
3985 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
3986 lod_comp->llc_stripe_offset =
3987 def_comp->llc_stripe_offset;
3988 if (lod_comp->llc_pool == NULL)
3989 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
3994 * fs default striping may not be explicitly set, or historically set
3995 * in config log, use them.
3997 if (lod_need_inherit_more(lc, false)) {
3999 if (lc->ldo_comp_cnt == 0) {
4000 rc = lod_alloc_comp_entries(lc, 1);
4002 /* fail to allocate memory, will create a
4003 * non-striped file. */
4005 lc->ldo_is_composite = 0;
4006 lod_comp = &lc->ldo_comp_entries[0];
4007 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4009 LASSERT(!lc->ldo_is_composite);
4010 lod_comp = &lc->ldo_comp_entries[0];
4011 desc = &d->lod_desc;
4012 if (lod_comp->llc_stripenr <= 0)
4013 lod_comp->llc_stripenr = desc->ld_default_stripe_count;
4014 if (lod_comp->llc_stripe_size <= 0)
4015 lod_comp->llc_stripe_size =
4016 desc->ld_default_stripe_size;
4022 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
4024 * Size initialization on late striping.
4026 * Propagate the size of a truncated object to a deferred striping.
4027 * This function handles a special case when truncate was done on a
4028 * non-striped object and now while the striping is being created
4029 * we can't lose that size, so we have to propagate it to the stripes
4032 * \param[in] env execution environment
4033 * \param[in] dt object
4034 * \param[in] th transaction handle
4036 * \retval 0 on success
4037 * \retval negative if failed
4039 static int lod_declare_init_size(const struct lu_env *env,
4040 struct dt_object *dt, struct thandle *th)
4042 struct dt_object *next = dt_object_child(dt);
4043 struct lod_object *lo = lod_dt_obj(dt);
4044 struct dt_object **objects = NULL;
4045 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4046 uint64_t size, offs;
4047 int i, rc, stripe, stripenr = 0, stripe_size = 0;
4050 if (!lod_obj_is_striped(dt))
4053 rc = dt_attr_get(env, next, attr);
4054 LASSERT(attr->la_valid & LA_SIZE);
4058 size = attr->la_size;
4062 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4063 struct lod_layout_component *lod_comp;
4064 struct lu_extent *extent;
4066 lod_comp = &lo->ldo_comp_entries[i];
4068 if (lod_comp->llc_stripe == NULL)
4071 extent = &lod_comp->llc_extent;
4072 CDEBUG(D_INFO, "%lld [%lld, %lld)\n",
4073 size, extent->e_start, extent->e_end);
4074 if (!lo->ldo_is_composite ||
4075 (size >= extent->e_start && size < extent->e_end)) {
4076 objects = lod_comp->llc_stripe;
4077 stripenr = lod_comp->llc_stripenr;
4078 stripe_size = lod_comp->llc_stripe_size;
4086 LASSERT(objects != NULL && stripe_size != 0);
4088 /* ll_do_div64(a, b) returns a % b, and a = a / b */
4089 ll_do_div64(size, (__u64)stripe_size);
4090 stripe = ll_do_div64(size, (__u64)stripenr);
4091 LASSERT(objects[stripe] != NULL);
4093 size = size * stripe_size;
4094 offs = attr->la_size;
4095 size += ll_do_div64(offs, stripe_size);
4097 attr->la_valid = LA_SIZE;
4098 attr->la_size = size;
4100 rc = lod_sub_declare_attr_set(env, objects[stripe], attr, th);
4106 * Declare creation of striped object.
4108 * The function declares creation stripes for a regular object. The function
4109 * also declares whether the stripes will be created with non-zero size if
4110 * previously size was set non-zero on the master object. If object \a dt is
4111 * not local, then only fully defined striping can be applied in \a lovea.
4112 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
4115 * \param[in] env execution environment
4116 * \param[in] dt object
4117 * \param[in] attr attributes the stripes will be created with
4118 * \param[in] lovea a buffer containing striping description
4119 * \param[in] th transaction handle
4121 * \retval 0 on success
4122 * \retval negative if failed
4124 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
4125 struct lu_attr *attr,
4126 const struct lu_buf *lovea, struct thandle *th)
4128 struct lod_thread_info *info = lod_env_info(env);
4129 struct dt_object *next = dt_object_child(dt);
4130 struct lod_object *lo = lod_dt_obj(dt);
4134 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
4135 GOTO(out, rc = -ENOMEM);
4137 if (!dt_object_remote(next)) {
4138 /* choose OST and generate appropriate objects */
4139 rc = lod_prepare_create(env, lo, attr, lovea, th);
4144 * declare storage for striping data
4146 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
4148 /* LOD can not choose OST objects for remote objects, i.e.
4149 * stripes must be ready before that. Right now, it can only
4150 * happen during migrate, i.e. migrate process needs to create
4151 * remote regular file (mdd_migrate_create), then the migrate
4152 * process will provide stripeEA. */
4153 LASSERT(lovea != NULL);
4154 info->lti_buf = *lovea;
4157 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
4158 XATTR_NAME_LOV, 0, th);
4163 * if striping is created with local object's size > 0,
4164 * we have to propagate this size to specific object
4165 * the case is possible only when local object was created previously
4167 if (dt_object_exists(next))
4168 rc = lod_declare_init_size(env, dt, th);
4171 /* failed to create striping or to set initial size, let's reset
4172 * config so that others don't get confused */
4174 lod_object_free_striping(env, lo);
4180 * Implementation of dt_object_operations::do_declare_create.
4182 * The method declares creation of a new object. If the object will be striped,
4183 * then helper functions are called to find FIDs for the stripes, declare
4184 * creation of the stripes and declare initialization of the striping
4185 * information to be stored in the master object.
4187 * \see dt_object_operations::do_declare_create() in the API description
4190 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
4191 struct lu_attr *attr,
4192 struct dt_allocation_hint *hint,
4193 struct dt_object_format *dof, struct thandle *th)
4195 struct dt_object *next = dt_object_child(dt);
4196 struct lod_object *lo = lod_dt_obj(dt);
4205 * first of all, we declare creation of local object
4207 rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
4211 if (dof->dof_type == DFT_SYM)
4212 dt->do_body_ops = &lod_body_lnk_ops;
4213 else if (dof->dof_type == DFT_REGULAR)
4214 dt->do_body_ops = &lod_body_ops;
4217 * it's lod_ah_init() that has decided the object will be striped
4219 if (dof->dof_type == DFT_REGULAR) {
4220 /* callers don't want stripes */
4221 /* XXX: all tricky interactions with ->ah_make_hint() decided
4222 * to use striping, then ->declare_create() behaving differently
4223 * should be cleaned */
4224 if (dof->u.dof_reg.striped != 0)
4225 rc = lod_declare_striped_create(env, dt, attr,
4227 } else if (dof->dof_type == DFT_DIR) {
4228 struct seq_server_site *ss;
4230 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
4232 /* If the parent has default stripeEA, and client
4233 * did not find it before sending create request,
4234 * then MDT will return -EREMOTE, and client will
4235 * retrieve the default stripeEA and re-create the
4238 * Note: if dah_eadata != NULL, it means creating the
4239 * striped directory with specified stripeEA, then it
4240 * should ignore the default stripeEA */
4241 if (hint != NULL && hint->dah_eadata == NULL) {
4242 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
4243 GOTO(out, rc = -EREMOTE);
4245 if (lo->ldo_dir_stripe_offset == -1) {
4246 /* child and parent should be in the same MDT */
4247 if (hint->dah_parent != NULL &&
4248 dt_object_remote(hint->dah_parent))
4249 GOTO(out, rc = -EREMOTE);
4250 } else if (lo->ldo_dir_stripe_offset !=
4252 struct lod_device *lod;
4253 struct lod_tgt_descs *ltd;
4254 struct lod_tgt_desc *tgt = NULL;
4255 bool found_mdt = false;
4258 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4259 ltd = &lod->lod_mdt_descs;
4260 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
4261 tgt = LTD_TGT(ltd, i);
4262 if (tgt->ltd_index ==
4263 lo->ldo_dir_stripe_offset) {
4269 /* If the MDT indicated by stripe_offset can be
4270 * found, then tell client to resend the create
4271 * request to the correct MDT, otherwise return
4272 * error to client */
4274 GOTO(out, rc = -EREMOTE);
4276 GOTO(out, rc = -EINVAL);
4280 rc = lod_declare_dir_striping_create(env, dt, attr, dof, th);
4283 /* failed to create striping or to set initial size, let's reset
4284 * config so that others don't get confused */
4286 lod_object_free_striping(env, lo);
4291 * Creation of a striped regular object.
4293 * The function is called to create the stripe objects for a regular
4294 * striped file. This can happen at the initial object creation or
4295 * when the caller asks LOD to do so using ->do_xattr_set() method
4296 * (so called late striping). Notice all the information are already
4297 * prepared in the form of the list of objects (ldo_stripe field).
4298 * This is done during declare phase.
4300 * \param[in] env execution environment
4301 * \param[in] dt object
4302 * \param[in] attr attributes the stripes will be created with
4303 * \param[in] dof format of stripes (see OSD API description)
4304 * \param[in] th transaction handle
4306 * \retval 0 on success
4307 * \retval negative if failed
4309 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
4310 struct lu_attr *attr, struct dt_object_format *dof,
4313 struct lod_layout_component *lod_comp;
4314 struct lod_object *lo = lod_dt_obj(dt);
4318 LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
4320 /* create all underlying objects */
4321 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4322 lod_comp = &lo->ldo_comp_entries[i];
4324 if (lod_comp_inited(lod_comp))
4327 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
4328 lod_comp_set_init(lod_comp);
4330 if (lod_comp->llc_stripe == NULL)
4333 LASSERT(lod_comp->llc_stripenr);
4334 for (j = 0; j < lod_comp->llc_stripenr; j++) {
4335 struct dt_object *object = lod_comp->llc_stripe[j];
4336 LASSERT(object != NULL);
4337 rc = lod_sub_create(env, object, attr, NULL, dof, th);
4341 lod_comp_set_init(lod_comp);
4345 rc = lod_generate_and_set_lovea(env, lo, th);
4348 lo->ldo_comp_cached = 1;
4350 lod_object_free_striping(env, lo);
4356 * Implementation of dt_object_operations::do_create.
4358 * If any of preceeding methods (like ->do_declare_create(),
4359 * ->do_ah_init(), etc) chose to create a striped object,
4360 * then this method will create the master and the stripes.
4362 * \see dt_object_operations::do_create() in the API description for details.
4364 static int lod_create(const struct lu_env *env, struct dt_object *dt,
4365 struct lu_attr *attr, struct dt_allocation_hint *hint,
4366 struct dt_object_format *dof, struct thandle *th)
4371 /* create local object */
4372 rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
4376 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4377 lod_obj_is_striped(dt) && dof->u.dof_reg.striped != 0) {
4378 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
4379 rc = lod_striped_create(env, dt, attr, dof, th);
4386 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
4387 struct dt_object *dt, struct thandle *th,
4388 int stripe_idx, struct lod_obj_stripe_cb_data *data)
4390 if (data->locd_declare)
4391 return lod_sub_declare_destroy(env, dt, th);
4392 else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4393 stripe_idx == cfs_fail_val)
4394 return lod_sub_destroy(env, dt, th);
4400 * Implementation of dt_object_operations::do_declare_destroy.
4402 * If the object is a striped directory, then the function declares reference
4403 * removal from the master object (this is an index) to the stripes and declares
4404 * destroy of all the stripes. In all the cases, it declares an intention to
4405 * destroy the object itself.
4407 * \see dt_object_operations::do_declare_destroy() in the API description
4410 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
4413 struct dt_object *next = dt_object_child(dt);
4414 struct lod_object *lo = lod_dt_obj(dt);
4415 struct lod_thread_info *info = lod_env_info(env);
4416 char *stripe_name = info->lti_key;
4421 * load striping information, notice we don't do this when object
4422 * is being initialized as we don't need this information till
4423 * few specific cases like destroy, chown
4425 rc = lod_load_striping(env, lo);
4429 /* declare destroy for all underlying objects */
4430 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4431 rc = next->do_ops->do_index_try(env, next,
4432 &dt_directory_features);
4436 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4437 rc = lod_sub_declare_ref_del(env, next, th);
4441 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4442 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4444 rc = lod_sub_declare_delete(env, next,
4445 (const struct dt_key *)stripe_name, th);
4452 * we declare destroy for the local object
4454 rc = lod_sub_declare_destroy(env, next, th);
4458 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4459 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4462 if (!lod_obj_is_striped(dt))
4465 /* declare destroy all striped objects */
4466 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4467 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4468 if (lo->ldo_stripe[i] == NULL)
4471 rc = lod_sub_declare_ref_del(env, lo->ldo_stripe[i],
4474 rc = lod_sub_declare_destroy(env, lo->ldo_stripe[i],
4480 struct lod_obj_stripe_cb_data data;
4482 data.locd_declare = true;
4483 rc = lod_obj_for_each_stripe(env, lo, th,
4484 lod_obj_stripe_destroy_cb, &data);
4491 * Implementation of dt_object_operations::do_destroy.
4493 * If the object is a striped directory, then the function removes references
4494 * from the master object (this is an index) to the stripes and destroys all
4495 * the stripes. In all the cases, the function destroys the object itself.
4497 * \see dt_object_operations::do_destroy() in the API description for details.
4499 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
4502 struct dt_object *next = dt_object_child(dt);
4503 struct lod_object *lo = lod_dt_obj(dt);
4504 struct lod_thread_info *info = lod_env_info(env);
4505 char *stripe_name = info->lti_key;
4510 /* destroy sub-stripe of master object */
4511 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4512 rc = next->do_ops->do_index_try(env, next,
4513 &dt_directory_features);
4517 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4518 rc = lod_sub_ref_del(env, next, th);
4522 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4523 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4526 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
4527 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
4528 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
4530 rc = lod_sub_delete(env, next,
4531 (const struct dt_key *)stripe_name, th);
4537 rc = lod_sub_destroy(env, next, th);
4541 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4542 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4545 if (!lod_obj_is_striped(dt))
4548 /* destroy all striped objects */
4549 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4550 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4551 if (lo->ldo_stripe[i] == NULL)
4553 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4554 i == cfs_fail_val) {
4555 dt_write_lock(env, lo->ldo_stripe[i],
4557 rc = lod_sub_ref_del(env, lo->ldo_stripe[i],
4559 dt_write_unlock(env, lo->ldo_stripe[i]);
4563 rc = lod_sub_destroy(env, lo->ldo_stripe[i],
4570 struct lod_obj_stripe_cb_data data;
4572 data.locd_declare = false;
4573 rc = lod_obj_for_each_stripe(env, lo, th,
4574 lod_obj_stripe_destroy_cb, &data);
4581 * Implementation of dt_object_operations::do_declare_ref_add.
4583 * \see dt_object_operations::do_declare_ref_add() in the API description
4586 static int lod_declare_ref_add(const struct lu_env *env,
4587 struct dt_object *dt, struct thandle *th)
4589 return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
4593 * Implementation of dt_object_operations::do_ref_add.
4595 * \see dt_object_operations::do_ref_add() in the API description for details.
4597 static int lod_ref_add(const struct lu_env *env,
4598 struct dt_object *dt, struct thandle *th)
4600 return lod_sub_ref_add(env, dt_object_child(dt), th);
4604 * Implementation of dt_object_operations::do_declare_ref_del.
4606 * \see dt_object_operations::do_declare_ref_del() in the API description
4609 static int lod_declare_ref_del(const struct lu_env *env,
4610 struct dt_object *dt, struct thandle *th)
4612 return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
4616 * Implementation of dt_object_operations::do_ref_del
4618 * \see dt_object_operations::do_ref_del() in the API description for details.
4620 static int lod_ref_del(const struct lu_env *env,
4621 struct dt_object *dt, struct thandle *th)
4623 return lod_sub_ref_del(env, dt_object_child(dt), th);
4627 * Implementation of dt_object_operations::do_object_sync.
4629 * \see dt_object_operations::do_object_sync() in the API description
4632 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
4633 __u64 start, __u64 end)
4635 return dt_object_sync(env, dt_object_child(dt), start, end);
4639 * Release LDLM locks on the stripes of a striped directory.
4641 * Iterates over all the locks taken on the stripe objects and
4644 * \param[in] env execution environment
4645 * \param[in] dt striped object
4646 * \param[in] einfo lock description
4647 * \param[in] policy data describing requested lock
4649 * \retval 0 on success
4650 * \retval negative if failed
4652 static int lod_object_unlock_internal(const struct lu_env *env,
4653 struct dt_object *dt,
4654 struct ldlm_enqueue_info *einfo,
4655 union ldlm_policy_data *policy)
4657 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
4662 if (slave_locks == NULL)
4665 for (i = 1; i < slave_locks->count; i++) {
4666 if (lustre_handle_is_used(&slave_locks->handles[i]))
4667 ldlm_lock_decref_and_cancel(&slave_locks->handles[i],
4675 * Implementation of dt_object_operations::do_object_unlock.
4677 * Used to release LDLM lock(s).
4679 * \see dt_object_operations::do_object_unlock() in the API description
4682 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
4683 struct ldlm_enqueue_info *einfo,
4684 union ldlm_policy_data *policy)
4686 struct lod_object *lo = lod_dt_obj(dt);
4687 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
4688 int slave_locks_size;
4692 if (slave_locks == NULL)
4695 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
4696 LASSERT(lo->ldo_dir_stripenr > 1);
4697 /* Note: for remote lock for single stripe dir, MDT will cancel
4698 * the lock by lockh directly */
4699 LASSERT(!dt_object_remote(dt_object_child(dt)));
4701 /* locks were unlocked in MDT layer */
4702 for (i = 1; i < slave_locks->count; i++) {
4703 LASSERT(!lustre_handle_is_used(&slave_locks->handles[i]));
4704 dt_invalidate(env, lo->ldo_stripe[i]);
4707 slave_locks_size = sizeof(*slave_locks) + slave_locks->count *
4708 sizeof(slave_locks->handles[0]);
4709 OBD_FREE(slave_locks, slave_locks_size);
4710 einfo->ei_cbdata = NULL;
4716 * Implementation of dt_object_operations::do_object_lock.
4718 * Used to get LDLM lock on the non-striped and striped objects.
4720 * \see dt_object_operations::do_object_lock() in the API description
4723 static int lod_object_lock(const struct lu_env *env,
4724 struct dt_object *dt,
4725 struct lustre_handle *lh,
4726 struct ldlm_enqueue_info *einfo,
4727 union ldlm_policy_data *policy)
4729 struct lod_object *lo = lod_dt_obj(dt);
4732 int slave_locks_size;
4733 struct lustre_handle_array *slave_locks = NULL;
4736 /* remote object lock */
4737 if (!einfo->ei_enq_slave) {
4738 LASSERT(dt_object_remote(dt));
4739 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
4743 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4744 GOTO(out, rc = -ENOTDIR);
4746 rc = lod_load_striping(env, lo);
4751 if (lo->ldo_dir_stripenr <= 1) {
4753 * NB, ei_cbdata stores pointer to slave locks, if no locks
4754 * taken, make sure it's set to NULL, otherwise MDT will try to
4757 einfo->ei_cbdata = NULL;
4761 slave_locks_size = sizeof(*slave_locks) + lo->ldo_dir_stripenr *
4762 sizeof(slave_locks->handles[0]);
4763 /* Freed in lod_object_unlock */
4764 OBD_ALLOC(slave_locks, slave_locks_size);
4765 if (slave_locks == NULL)
4766 GOTO(out, rc = -ENOMEM);
4767 slave_locks->count = lo->ldo_dir_stripenr;
4769 /* striped directory lock */
4770 for (i = 1; i < lo->ldo_dir_stripenr; i++) {
4771 struct lustre_handle lockh;
4772 struct ldlm_res_id *res_id;
4774 res_id = &lod_env_info(env)->lti_res_id;
4775 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
4777 einfo->ei_res_id = res_id;
4779 LASSERT(lo->ldo_stripe[i] != NULL);
4780 if (likely(dt_object_remote(lo->ldo_stripe[i]))) {
4781 rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh,
4784 struct ldlm_namespace *ns = einfo->ei_namespace;
4785 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
4786 ldlm_completion_callback completion = einfo->ei_cb_cp;
4787 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
4789 if (einfo->ei_mode == LCK_PW ||
4790 einfo->ei_mode == LCK_EX)
4791 dlmflags |= LDLM_FL_COS_INCOMPAT;
4793 /* This only happens if there are mulitple stripes
4794 * on the master MDT, i.e. except stripe0, there are
4795 * other stripes on the Master MDT as well, Only
4796 * happens in the test case right now. */
4797 LASSERT(ns != NULL);
4798 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS,
4799 policy, einfo->ei_mode,
4800 &dlmflags, blocking,
4802 NULL, 0, LVB_T_NONE,
4807 slave_locks->handles[i] = lockh;
4809 einfo->ei_cbdata = slave_locks;
4811 if (rc != 0 && slave_locks != NULL) {
4812 lod_object_unlock_internal(env, dt, einfo, policy);
4813 OBD_FREE(slave_locks, slave_locks_size);
4818 einfo->ei_cbdata = NULL;
4823 * Implementation of dt_object_operations::do_invalidate.
4825 * \see dt_object_operations::do_invalidate() in the API description for details
4827 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
4829 return dt_invalidate(env, dt_object_child(dt));
4832 static int lod_declare_layout_change(const struct lu_env *env,
4833 struct dt_object *dt,
4834 struct layout_intent *layout,
4835 const struct lu_buf *buf,
4838 struct lod_thread_info *info = lod_env_info(env);
4839 struct lod_object *lo = lod_dt_obj(dt);
4840 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
4841 struct dt_object *next = dt_object_child(dt);
4842 struct ost_pool *inuse = &info->lti_inuse_osts;
4843 struct lod_layout_component *lod_comp;
4844 struct lov_comp_md_v1 *comp_v1 = NULL;
4845 bool replay = false;
4846 bool need_create = false;
4850 if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
4851 dt_object_remote(next))
4854 dt_write_lock(env, next, 0);
4856 * In case the client is passing lovea, which only happens during
4857 * the replay of layout intent write RPC for now, we may need to
4858 * parse the lovea and apply new layout configuration.
4860 if (buf && buf->lb_len) {
4861 struct lov_user_md_v1 *v1 = buf->lb_buf;
4863 if (v1->lmm_magic != (LOV_MAGIC_DEF | LOV_MAGIC_COMP_V1) &&
4865 __swab32(LOV_MAGIC_DEF | LOV_MAGIC_COMP_V1)) {
4866 CERROR("%s: the replay buffer of layout extend "
4867 "(magic %#x) does not contain expected "
4868 "composite layout.\n",
4869 lod2obd(d)->obd_name, v1->lmm_magic);
4870 GOTO(out, rc = -EINVAL);
4873 lod_object_free_striping(env, lo);
4874 rc = lod_use_defined_striping(env, lo, buf);
4878 rc = lod_get_lov_ea(env, lo);
4881 /* old on-disk EA is stored in info->lti_buf */
4882 comp_v1 = (struct lov_comp_md_v1 *)&info->lti_buf.lb_buf;
4885 /* non replay path */
4886 rc = lod_load_striping_locked(env, lo);
4890 /* Prepare inuse array for composite file */
4891 rc = lod_prepare_inuse(env, lo);
4896 /* Make sure defined layout covers the requested write range. */
4897 lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
4898 if (lo->ldo_comp_cnt > 1 &&
4899 lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
4900 lod_comp->llc_extent.e_end < layout->li_end) {
4901 CDEBUG(replay ? D_ERROR : D_LAYOUT,
4902 "%s: the defined layout [0, %#llx) does not covers "
4903 "the write range [%#llx, %#llx).\n",
4904 lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
4905 layout->li_start, layout->li_end);
4906 GOTO(out, rc = -EINVAL);
4910 * Iterate ld->ldo_comp_entries, find the component whose extent under
4911 * the write range and not instantianted.
4913 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4914 lod_comp = &lo->ldo_comp_entries[i];
4916 if (lod_comp->llc_extent.e_start >= layout->li_end)
4920 if (lod_comp_inited(lod_comp))
4924 * In replay path, lod_comp is the EA passed by
4925 * client replay buffer, comp_v1 is the pre-recovery
4926 * on-disk EA, we'd sift out those components which
4927 * were init-ed in the on-disk EA.
4929 if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
4934 * this component hasn't instantiated in normal path, or during
4935 * replay it needs replay the instantiation.
4938 /* A released component is being extended */
4939 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
4940 GOTO(out, rc = -EINVAL);
4944 rc = lod_qos_prep_create(env, lo, NULL, th, i, inuse);
4950 lod_obj_inc_layout_gen(lo);
4952 GOTO(unlock, rc = -EALREADY);
4955 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
4956 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
4957 XATTR_NAME_LOV, 0, th);
4961 lod_object_free_striping(env, lo);
4964 dt_write_unlock(env, next);
4970 * Instantiate layout component objects which covers the intent write offset.
4972 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
4973 struct layout_intent *layout,
4974 const struct lu_buf *buf, struct thandle *th)
4976 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4978 RETURN(lod_striped_create(env, dt, attr, NULL, th));
4981 struct dt_object_operations lod_obj_ops = {
4982 .do_read_lock = lod_read_lock,
4983 .do_write_lock = lod_write_lock,
4984 .do_read_unlock = lod_read_unlock,
4985 .do_write_unlock = lod_write_unlock,
4986 .do_write_locked = lod_write_locked,
4987 .do_attr_get = lod_attr_get,
4988 .do_declare_attr_set = lod_declare_attr_set,
4989 .do_attr_set = lod_attr_set,
4990 .do_xattr_get = lod_xattr_get,
4991 .do_declare_xattr_set = lod_declare_xattr_set,
4992 .do_xattr_set = lod_xattr_set,
4993 .do_declare_xattr_del = lod_declare_xattr_del,
4994 .do_xattr_del = lod_xattr_del,
4995 .do_xattr_list = lod_xattr_list,
4996 .do_ah_init = lod_ah_init,
4997 .do_declare_create = lod_declare_create,
4998 .do_create = lod_create,
4999 .do_declare_destroy = lod_declare_destroy,
5000 .do_destroy = lod_destroy,
5001 .do_index_try = lod_index_try,
5002 .do_declare_ref_add = lod_declare_ref_add,
5003 .do_ref_add = lod_ref_add,
5004 .do_declare_ref_del = lod_declare_ref_del,
5005 .do_ref_del = lod_ref_del,
5006 .do_object_sync = lod_object_sync,
5007 .do_object_lock = lod_object_lock,
5008 .do_object_unlock = lod_object_unlock,
5009 .do_invalidate = lod_invalidate,
5010 .do_declare_layout_change = lod_declare_layout_change,
5011 .do_layout_change = lod_layout_change,
5015 * Implementation of dt_body_operations::dbo_read.
5017 * \see dt_body_operations::dbo_read() in the API description for details.
5019 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
5020 struct lu_buf *buf, loff_t *pos)
5022 struct dt_object *next = dt_object_child(dt);
5023 return next->do_body_ops->dbo_read(env, next, buf, pos);
5027 * Implementation of dt_body_operations::dbo_declare_write.
5029 * \see dt_body_operations::dbo_declare_write() in the API description
5032 static ssize_t lod_declare_write(const struct lu_env *env,
5033 struct dt_object *dt,
5034 const struct lu_buf *buf, loff_t pos,
5037 return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
5041 * Implementation of dt_body_operations::dbo_write.
5043 * \see dt_body_operations::dbo_write() in the API description for details.
5045 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
5046 const struct lu_buf *buf, loff_t *pos,
5047 struct thandle *th, int iq)
5049 return lod_sub_write(env, dt_object_child(dt), buf, pos, th, iq);
5052 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
5053 __u64 start, __u64 end, struct thandle *th)
5055 if (dt_object_remote(dt))
5058 return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
5061 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
5062 __u64 start, __u64 end, struct thandle *th)
5064 if (dt_object_remote(dt))
5067 return lod_sub_punch(env, dt_object_child(dt), start, end, th);
5070 static const struct dt_body_operations lod_body_lnk_ops = {
5071 .dbo_read = lod_read,
5072 .dbo_declare_write = lod_declare_write,
5073 .dbo_write = lod_write
5076 static const struct dt_body_operations lod_body_ops = {
5077 .dbo_read = lod_read,
5078 .dbo_declare_write = lod_declare_write,
5079 .dbo_write = lod_write,
5080 .dbo_declare_punch = lod_declare_punch,
5081 .dbo_punch = lod_punch,
5085 * Implementation of lu_object_operations::loo_object_init.
5087 * The function determines the type and the index of the target device using
5088 * sequence of the object's FID. Then passes control down to the
5089 * corresponding device:
5090 * OSD for the local objects, OSP for remote
5092 * \see lu_object_operations::loo_object_init() in the API description
5095 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
5096 const struct lu_object_conf *conf)
5098 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
5099 struct lu_device *cdev = NULL;
5100 struct lu_object *cobj;
5101 struct lod_tgt_descs *ltd = NULL;
5102 struct lod_tgt_desc *tgt;
5104 int type = LU_SEQ_RANGE_ANY;
5108 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
5110 /* Note: Sometimes, it will Return EAGAIN here, see
5111 * ptrlpc_import_delay_req(), which might confuse
5112 * lu_object_find_at() and make it wait there incorrectly.
5113 * so we convert it to EIO here.*/
5120 if (type == LU_SEQ_RANGE_MDT &&
5121 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
5122 cdev = &lod->lod_child->dd_lu_dev;
5123 } else if (type == LU_SEQ_RANGE_MDT) {
5124 ltd = &lod->lod_mdt_descs;
5126 } else if (type == LU_SEQ_RANGE_OST) {
5127 ltd = &lod->lod_ost_descs;
5134 if (ltd->ltd_tgts_size > idx &&
5135 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
5136 tgt = LTD_TGT(ltd, idx);
5138 LASSERT(tgt != NULL);
5139 LASSERT(tgt->ltd_tgt != NULL);
5141 cdev = &(tgt->ltd_tgt->dd_lu_dev);
5143 lod_putref(lod, ltd);
5146 if (unlikely(cdev == NULL))
5149 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
5150 if (unlikely(cobj == NULL))
5153 lu_object_add(lo, cobj);
5160 * Release resources associated with striping.
5162 * If the object is striped (regular or directory), then release
5163 * the stripe objects references and free the ldo_stripe array.
5165 * \param[in] env execution environment
5166 * \param[in] lo object
5168 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
5170 struct lod_layout_component *lod_comp;
5173 if (lo->ldo_stripe != NULL) {
5174 LASSERT(lo->ldo_comp_entries == NULL);
5175 LASSERT(lo->ldo_dir_stripes_allocated > 0);
5177 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
5178 if (lo->ldo_stripe[i])
5179 dt_object_put(env, lo->ldo_stripe[i]);
5182 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
5183 OBD_FREE(lo->ldo_stripe, j);
5184 lo->ldo_stripe = NULL;
5185 lo->ldo_dir_stripes_allocated = 0;
5186 lo->ldo_dir_stripenr = 0;
5187 } else if (lo->ldo_comp_entries != NULL) {
5188 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5189 /* free lod_layout_component::llc_stripe array */
5190 lod_comp = &lo->ldo_comp_entries[i];
5192 if (lod_comp->llc_stripe == NULL)
5194 LASSERT(lod_comp->llc_stripes_allocated != 0);
5195 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
5196 if (lod_comp->llc_stripe[j] != NULL)
5198 &lod_comp->llc_stripe[j]->do_lu);
5200 OBD_FREE(lod_comp->llc_stripe,
5201 sizeof(struct dt_object *) *
5202 lod_comp->llc_stripes_allocated);
5203 lod_comp->llc_stripe = NULL;
5204 lod_comp->llc_stripes_allocated = 0;
5206 lod_free_comp_entries(lo);
5207 lo->ldo_comp_cached = 0;
5212 * Implementation of lu_object_operations::loo_object_start.
5214 * \see lu_object_operations::loo_object_start() in the API description
5217 static int lod_object_start(const struct lu_env *env, struct lu_object *o)
5219 if (S_ISLNK(o->lo_header->loh_attr & S_IFMT)) {
5220 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_lnk_ops;
5221 } else if (S_ISREG(o->lo_header->loh_attr & S_IFMT) ||
5222 fid_is_local_file(lu_object_fid(o))) {
5223 /* Note: some local file (like last rcvd) is created
5224 * through bottom layer (OSD), so the object initialization
5225 * comes to lod, it does not set loh_attr yet, so
5226 * set do_body_ops for local file anyway */
5227 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_ops;
5233 * Implementation of lu_object_operations::loo_object_free.
5235 * \see lu_object_operations::loo_object_free() in the API description
5238 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
5240 struct lod_object *lo = lu2lod_obj(o);
5242 /* release all underlying object pinned */
5243 lod_object_free_striping(env, lo);
5245 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
5249 * Implementation of lu_object_operations::loo_object_release.
5251 * \see lu_object_operations::loo_object_release() in the API description
5254 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
5256 /* XXX: shouldn't we release everything here in case if object
5257 * creation failed before? */
5261 * Implementation of lu_object_operations::loo_object_print.
5263 * \see lu_object_operations::loo_object_print() in the API description
5266 static int lod_object_print(const struct lu_env *env, void *cookie,
5267 lu_printer_t p, const struct lu_object *l)
5269 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
5271 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
5274 struct lu_object_operations lod_lu_obj_ops = {
5275 .loo_object_init = lod_object_init,
5276 .loo_object_start = lod_object_start,
5277 .loo_object_free = lod_object_free,
5278 .loo_object_release = lod_object_release,
5279 .loo_object_print = lod_object_print,