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/lustre_param.h>
51 #include <lustre_swab.h>
52 #include <uapi/linux/lustre/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[] = "..";
62 * Implementation of dt_index_operations::dio_lookup
64 * Used with regular (non-striped) objects.
66 * \see dt_index_operations::dio_lookup() in the API description for details.
68 static int lod_lookup(const struct lu_env *env, struct dt_object *dt,
69 struct dt_rec *rec, const struct dt_key *key)
71 struct dt_object *next = dt_object_child(dt);
72 return next->do_index_ops->dio_lookup(env, next, rec, key);
76 * Implementation of dt_index_operations::dio_declare_insert.
78 * Used with regular (non-striped) objects.
80 * \see dt_index_operations::dio_declare_insert() in the API description
83 static int lod_declare_insert(const struct lu_env *env, struct dt_object *dt,
84 const struct dt_rec *rec,
85 const struct dt_key *key, struct thandle *th)
87 return lod_sub_declare_insert(env, dt_object_child(dt), rec, key, th);
91 * Implementation of dt_index_operations::dio_insert.
93 * Used with regular (non-striped) objects
95 * \see dt_index_operations::dio_insert() in the API description for details.
97 static int lod_insert(const struct lu_env *env, struct dt_object *dt,
98 const struct dt_rec *rec, const struct dt_key *key,
99 struct thandle *th, int ign)
101 return lod_sub_insert(env, dt_object_child(dt), rec, key, th, ign);
105 * Implementation of dt_index_operations::dio_declare_delete.
107 * Used with regular (non-striped) objects.
109 * \see dt_index_operations::dio_declare_delete() in the API description
112 static int lod_declare_delete(const struct lu_env *env, struct dt_object *dt,
113 const struct dt_key *key, struct thandle *th)
115 return lod_sub_declare_delete(env, dt_object_child(dt), key, th);
119 * Implementation of dt_index_operations::dio_delete.
121 * Used with regular (non-striped) objects.
123 * \see dt_index_operations::dio_delete() in the API description for details.
125 static int lod_delete(const struct lu_env *env, struct dt_object *dt,
126 const struct dt_key *key, struct thandle *th)
128 return lod_sub_delete(env, dt_object_child(dt), key, th);
132 * Implementation of dt_it_ops::init.
134 * Used with regular (non-striped) objects.
136 * \see dt_it_ops::init() in the API description for details.
138 static struct dt_it *lod_it_init(const struct lu_env *env,
139 struct dt_object *dt, __u32 attr)
141 struct dt_object *next = dt_object_child(dt);
142 struct lod_it *it = &lod_env_info(env)->lti_it;
143 struct dt_it *it_next;
145 it_next = next->do_index_ops->dio_it.init(env, next, attr);
149 /* currently we do not use more than one iterator per thread
150 * so we store it in thread info. if at some point we need
151 * more active iterators in a single thread, we can allocate
153 LASSERT(it->lit_obj == NULL);
155 it->lit_it = it_next;
158 return (struct dt_it *)it;
161 #define LOD_CHECK_IT(env, it) \
163 LASSERT((it)->lit_obj != NULL); \
164 LASSERT((it)->lit_it != NULL); \
168 * Implementation of dt_index_operations::dio_it.fini.
170 * Used with regular (non-striped) objects.
172 * \see dt_index_operations::dio_it.fini() in the API description for details.
174 static void lod_it_fini(const struct lu_env *env, struct dt_it *di)
176 struct lod_it *it = (struct lod_it *)di;
178 LOD_CHECK_IT(env, it);
179 it->lit_obj->do_index_ops->dio_it.fini(env, it->lit_it);
181 /* the iterator not in use any more */
187 * Implementation of dt_it_ops::get.
189 * Used with regular (non-striped) objects.
191 * \see dt_it_ops::get() in the API description for details.
193 static int lod_it_get(const struct lu_env *env, struct dt_it *di,
194 const struct dt_key *key)
196 const struct lod_it *it = (const struct lod_it *)di;
198 LOD_CHECK_IT(env, it);
199 return it->lit_obj->do_index_ops->dio_it.get(env, it->lit_it, key);
203 * Implementation of dt_it_ops::put.
205 * Used with regular (non-striped) objects.
207 * \see dt_it_ops::put() in the API description for details.
209 static void lod_it_put(const struct lu_env *env, struct dt_it *di)
211 struct lod_it *it = (struct lod_it *)di;
213 LOD_CHECK_IT(env, it);
214 return it->lit_obj->do_index_ops->dio_it.put(env, it->lit_it);
218 * Implementation of dt_it_ops::next.
220 * Used with regular (non-striped) objects
222 * \see dt_it_ops::next() in the API description for details.
224 static int lod_it_next(const struct lu_env *env, struct dt_it *di)
226 struct lod_it *it = (struct lod_it *)di;
228 LOD_CHECK_IT(env, it);
229 return it->lit_obj->do_index_ops->dio_it.next(env, it->lit_it);
233 * Implementation of dt_it_ops::key.
235 * Used with regular (non-striped) objects.
237 * \see dt_it_ops::key() in the API description for details.
239 static struct dt_key *lod_it_key(const struct lu_env *env,
240 const struct dt_it *di)
242 const struct lod_it *it = (const struct lod_it *)di;
244 LOD_CHECK_IT(env, it);
245 return it->lit_obj->do_index_ops->dio_it.key(env, it->lit_it);
249 * Implementation of dt_it_ops::key_size.
251 * Used with regular (non-striped) objects.
253 * \see dt_it_ops::key_size() in the API description for details.
255 static int lod_it_key_size(const struct lu_env *env, const struct dt_it *di)
257 struct lod_it *it = (struct lod_it *)di;
259 LOD_CHECK_IT(env, it);
260 return it->lit_obj->do_index_ops->dio_it.key_size(env, it->lit_it);
264 * Implementation of dt_it_ops::rec.
266 * Used with regular (non-striped) objects.
268 * \see dt_it_ops::rec() in the API description for details.
270 static int lod_it_rec(const struct lu_env *env, const struct dt_it *di,
271 struct dt_rec *rec, __u32 attr)
273 const struct lod_it *it = (const struct lod_it *)di;
275 LOD_CHECK_IT(env, it);
276 return it->lit_obj->do_index_ops->dio_it.rec(env, it->lit_it, rec,
281 * Implementation of dt_it_ops::rec_size.
283 * Used with regular (non-striped) objects.
285 * \see dt_it_ops::rec_size() in the API description for details.
287 static int lod_it_rec_size(const struct lu_env *env, const struct dt_it *di,
290 const struct lod_it *it = (const struct lod_it *)di;
292 LOD_CHECK_IT(env, it);
293 return it->lit_obj->do_index_ops->dio_it.rec_size(env, it->lit_it,
298 * Implementation of dt_it_ops::store.
300 * Used with regular (non-striped) objects.
302 * \see dt_it_ops::store() in the API description for details.
304 static __u64 lod_it_store(const struct lu_env *env, const struct dt_it *di)
306 const struct lod_it *it = (const struct lod_it *)di;
308 LOD_CHECK_IT(env, it);
309 return it->lit_obj->do_index_ops->dio_it.store(env, it->lit_it);
313 * Implementation of dt_it_ops::load.
315 * Used with regular (non-striped) objects.
317 * \see dt_it_ops::load() in the API description for details.
319 static int lod_it_load(const struct lu_env *env, const struct dt_it *di,
322 const struct lod_it *it = (const struct lod_it *)di;
324 LOD_CHECK_IT(env, it);
325 return it->lit_obj->do_index_ops->dio_it.load(env, it->lit_it, hash);
329 * Implementation of dt_it_ops::key_rec.
331 * Used with regular (non-striped) objects.
333 * \see dt_it_ops::rec() in the API description for details.
335 static int lod_it_key_rec(const struct lu_env *env, const struct dt_it *di,
338 const struct lod_it *it = (const struct lod_it *)di;
340 LOD_CHECK_IT(env, it);
341 return it->lit_obj->do_index_ops->dio_it.key_rec(env, it->lit_it,
345 static struct dt_index_operations lod_index_ops = {
346 .dio_lookup = lod_lookup,
347 .dio_declare_insert = lod_declare_insert,
348 .dio_insert = lod_insert,
349 .dio_declare_delete = lod_declare_delete,
350 .dio_delete = lod_delete,
358 .key_size = lod_it_key_size,
360 .rec_size = lod_it_rec_size,
361 .store = lod_it_store,
363 .key_rec = lod_it_key_rec,
368 * Implementation of dt_it_ops::init.
370 * Used with striped objects. Internally just initializes the iterator
371 * on the first stripe.
373 * \see dt_it_ops::init() in the API description for details.
375 static struct dt_it *lod_striped_it_init(const struct lu_env *env,
376 struct dt_object *dt, __u32 attr)
378 struct lod_object *lo = lod_dt_obj(dt);
379 struct dt_object *next;
380 struct lod_it *it = &lod_env_info(env)->lti_it;
381 struct dt_it *it_next;
384 LASSERT(lo->ldo_dir_stripe_count > 0);
385 next = lo->ldo_stripe[0];
386 LASSERT(next != NULL);
387 LASSERT(next->do_index_ops != NULL);
389 it_next = next->do_index_ops->dio_it.init(env, next, attr);
393 /* currently we do not use more than one iterator per thread
394 * so we store it in thread info. if at some point we need
395 * more active iterators in a single thread, we can allocate
397 LASSERT(it->lit_obj == NULL);
399 it->lit_stripe_index = 0;
401 it->lit_it = it_next;
404 return (struct dt_it *)it;
407 #define LOD_CHECK_STRIPED_IT(env, it, lo) \
409 LASSERT((it)->lit_obj != NULL); \
410 LASSERT((it)->lit_it != NULL); \
411 LASSERT((lo)->ldo_dir_stripe_count > 0); \
412 LASSERT((it)->lit_stripe_index < (lo)->ldo_dir_stripe_count); \
416 * Implementation of dt_it_ops::fini.
418 * Used with striped objects.
420 * \see dt_it_ops::fini() in the API description for details.
422 static void lod_striped_it_fini(const struct lu_env *env, struct dt_it *di)
424 struct lod_it *it = (struct lod_it *)di;
425 struct lod_object *lo = lod_dt_obj(it->lit_obj);
426 struct dt_object *next;
428 /* If lit_it == NULL, then it means the sub_it has been finished,
429 * which only happens in failure cases, see lod_striped_it_next() */
430 if (it->lit_it != NULL) {
431 LOD_CHECK_STRIPED_IT(env, it, lo);
433 next = lo->ldo_stripe[it->lit_stripe_index];
434 LASSERT(next != NULL);
435 LASSERT(next->do_index_ops != NULL);
437 next->do_index_ops->dio_it.fini(env, it->lit_it);
440 /* the iterator not in use any more */
443 it->lit_stripe_index = 0;
447 * Implementation of dt_it_ops::get.
449 * Right now it's not used widely, only to reset the iterator to the
450 * initial position. It should be possible to implement a full version
451 * which chooses a correct stripe to be able to position with any key.
453 * \see dt_it_ops::get() in the API description for details.
455 static int lod_striped_it_get(const struct lu_env *env, struct dt_it *di,
456 const struct dt_key *key)
458 const struct lod_it *it = (const struct lod_it *)di;
459 struct lod_object *lo = lod_dt_obj(it->lit_obj);
460 struct dt_object *next;
463 LOD_CHECK_STRIPED_IT(env, it, lo);
465 next = lo->ldo_stripe[it->lit_stripe_index];
466 LASSERT(next != NULL);
467 LASSERT(next->do_index_ops != NULL);
469 return next->do_index_ops->dio_it.get(env, it->lit_it, key);
473 * Implementation of dt_it_ops::put.
475 * Used with striped objects.
477 * \see dt_it_ops::put() in the API description for details.
479 static void lod_striped_it_put(const struct lu_env *env, struct dt_it *di)
481 struct lod_it *it = (struct lod_it *)di;
482 struct lod_object *lo = lod_dt_obj(it->lit_obj);
483 struct dt_object *next;
485 LOD_CHECK_STRIPED_IT(env, it, lo);
487 next = lo->ldo_stripe[it->lit_stripe_index];
488 LASSERT(next != NULL);
489 LASSERT(next->do_index_ops != NULL);
491 return next->do_index_ops->dio_it.put(env, it->lit_it);
495 * Implementation of dt_it_ops::next.
497 * Used with striped objects. When the end of the current stripe is
498 * reached, the method takes the next stripe's iterator.
500 * \see dt_it_ops::next() in the API description for details.
502 static int lod_striped_it_next(const struct lu_env *env, struct dt_it *di)
504 struct lod_it *it = (struct lod_it *)di;
505 struct lod_object *lo = lod_dt_obj(it->lit_obj);
506 struct dt_object *next;
507 struct dt_it *it_next;
511 LOD_CHECK_STRIPED_IT(env, it, lo);
513 next = lo->ldo_stripe[it->lit_stripe_index];
514 LASSERT(next != NULL);
515 LASSERT(next->do_index_ops != NULL);
517 rc = next->do_index_ops->dio_it.next(env, it->lit_it);
521 if (rc == 0 && it->lit_stripe_index == 0)
524 if (rc == 0 && it->lit_stripe_index > 0) {
525 struct lu_dirent *ent;
527 ent = (struct lu_dirent *)lod_env_info(env)->lti_key;
529 rc = next->do_index_ops->dio_it.rec(env, it->lit_it,
530 (struct dt_rec *)ent,
535 /* skip . and .. for slave stripe */
536 if ((strncmp(ent->lde_name, ".",
537 le16_to_cpu(ent->lde_namelen)) == 0 &&
538 le16_to_cpu(ent->lde_namelen) == 1) ||
539 (strncmp(ent->lde_name, "..",
540 le16_to_cpu(ent->lde_namelen)) == 0 &&
541 le16_to_cpu(ent->lde_namelen) == 2))
547 /* go to next stripe */
548 if (it->lit_stripe_index + 1 >= lo->ldo_dir_stripe_count)
551 it->lit_stripe_index++;
553 next->do_index_ops->dio_it.put(env, it->lit_it);
554 next->do_index_ops->dio_it.fini(env, it->lit_it);
557 next = lo->ldo_stripe[it->lit_stripe_index];
558 LASSERT(next != NULL);
559 rc = next->do_ops->do_index_try(env, next, &dt_directory_features);
563 LASSERT(next->do_index_ops != NULL);
565 it_next = next->do_index_ops->dio_it.init(env, next, it->lit_attr);
566 if (!IS_ERR(it_next)) {
567 it->lit_it = it_next;
570 rc = PTR_ERR(it_next);
577 * Implementation of dt_it_ops::key.
579 * Used with striped objects.
581 * \see dt_it_ops::key() in the API description for details.
583 static struct dt_key *lod_striped_it_key(const struct lu_env *env,
584 const struct dt_it *di)
586 const struct lod_it *it = (const struct lod_it *)di;
587 struct lod_object *lo = lod_dt_obj(it->lit_obj);
588 struct dt_object *next;
590 LOD_CHECK_STRIPED_IT(env, it, lo);
592 next = lo->ldo_stripe[it->lit_stripe_index];
593 LASSERT(next != NULL);
594 LASSERT(next->do_index_ops != NULL);
596 return next->do_index_ops->dio_it.key(env, it->lit_it);
600 * Implementation of dt_it_ops::key_size.
602 * Used with striped objects.
604 * \see dt_it_ops::size() in the API description for details.
606 static int lod_striped_it_key_size(const struct lu_env *env,
607 const struct dt_it *di)
609 struct lod_it *it = (struct lod_it *)di;
610 struct lod_object *lo = lod_dt_obj(it->lit_obj);
611 struct dt_object *next;
613 LOD_CHECK_STRIPED_IT(env, it, lo);
615 next = lo->ldo_stripe[it->lit_stripe_index];
616 LASSERT(next != NULL);
617 LASSERT(next->do_index_ops != NULL);
619 return next->do_index_ops->dio_it.key_size(env, it->lit_it);
623 * Implementation of dt_it_ops::rec.
625 * Used with striped objects.
627 * \see dt_it_ops::rec() in the API description for details.
629 static int lod_striped_it_rec(const struct lu_env *env, const struct dt_it *di,
630 struct dt_rec *rec, __u32 attr)
632 const struct lod_it *it = (const struct lod_it *)di;
633 struct lod_object *lo = lod_dt_obj(it->lit_obj);
634 struct dt_object *next;
636 LOD_CHECK_STRIPED_IT(env, it, lo);
638 next = lo->ldo_stripe[it->lit_stripe_index];
639 LASSERT(next != NULL);
640 LASSERT(next->do_index_ops != NULL);
642 return next->do_index_ops->dio_it.rec(env, it->lit_it, rec, attr);
646 * Implementation of dt_it_ops::rec_size.
648 * Used with striped objects.
650 * \see dt_it_ops::rec_size() in the API description for details.
652 static int lod_striped_it_rec_size(const struct lu_env *env,
653 const struct dt_it *di, __u32 attr)
655 struct lod_it *it = (struct lod_it *)di;
656 struct lod_object *lo = lod_dt_obj(it->lit_obj);
657 struct dt_object *next;
659 LOD_CHECK_STRIPED_IT(env, it, lo);
661 next = lo->ldo_stripe[it->lit_stripe_index];
662 LASSERT(next != NULL);
663 LASSERT(next->do_index_ops != NULL);
665 return next->do_index_ops->dio_it.rec_size(env, it->lit_it, attr);
669 * Implementation of dt_it_ops::store.
671 * Used with striped objects.
673 * \see dt_it_ops::store() in the API description for details.
675 static __u64 lod_striped_it_store(const struct lu_env *env,
676 const struct dt_it *di)
678 const struct lod_it *it = (const struct lod_it *)di;
679 struct lod_object *lo = lod_dt_obj(it->lit_obj);
680 struct dt_object *next;
682 LOD_CHECK_STRIPED_IT(env, it, lo);
684 next = lo->ldo_stripe[it->lit_stripe_index];
685 LASSERT(next != NULL);
686 LASSERT(next->do_index_ops != NULL);
688 return next->do_index_ops->dio_it.store(env, it->lit_it);
692 * Implementation of dt_it_ops::load.
694 * Used with striped objects.
696 * \see dt_it_ops::load() in the API description for details.
698 static int lod_striped_it_load(const struct lu_env *env,
699 const struct dt_it *di, __u64 hash)
701 const struct lod_it *it = (const struct lod_it *)di;
702 struct lod_object *lo = lod_dt_obj(it->lit_obj);
703 struct dt_object *next;
705 LOD_CHECK_STRIPED_IT(env, it, lo);
707 next = lo->ldo_stripe[it->lit_stripe_index];
708 LASSERT(next != NULL);
709 LASSERT(next->do_index_ops != NULL);
711 return next->do_index_ops->dio_it.load(env, it->lit_it, hash);
714 static struct dt_index_operations lod_striped_index_ops = {
715 .dio_lookup = lod_lookup,
716 .dio_declare_insert = lod_declare_insert,
717 .dio_insert = lod_insert,
718 .dio_declare_delete = lod_declare_delete,
719 .dio_delete = lod_delete,
721 .init = lod_striped_it_init,
722 .fini = lod_striped_it_fini,
723 .get = lod_striped_it_get,
724 .put = lod_striped_it_put,
725 .next = lod_striped_it_next,
726 .key = lod_striped_it_key,
727 .key_size = lod_striped_it_key_size,
728 .rec = lod_striped_it_rec,
729 .rec_size = lod_striped_it_rec_size,
730 .store = lod_striped_it_store,
731 .load = lod_striped_it_load,
736 * Append the FID for each shard of the striped directory after the
737 * given LMV EA header.
739 * To simplify striped directory and the consistency verification,
740 * we only store the LMV EA header on disk, for both master object
741 * and slave objects. When someone wants to know the whole LMV EA,
742 * such as client readdir(), we can build the entrie LMV EA on the
743 * MDT side (in RAM) via iterating the sub-directory entries that
744 * are contained in the master object of the stripe directory.
746 * For the master object of the striped directroy, the valid name
747 * for each shard is composed of the ${shard_FID}:${shard_idx}.
749 * There may be holes in the LMV EA if some shards' name entries
750 * are corrupted or lost.
752 * \param[in] env pointer to the thread context
753 * \param[in] lo pointer to the master object of the striped directory
754 * \param[in] buf pointer to the lu_buf which will hold the LMV EA
755 * \param[in] resize whether re-allocate the buffer if it is not big enough
757 * \retval positive size of the LMV EA
758 * \retval 0 for nothing to be loaded
759 * \retval negative error number on failure
761 int lod_load_lmv_shards(const struct lu_env *env, struct lod_object *lo,
762 struct lu_buf *buf, bool resize)
764 struct lu_dirent *ent =
765 (struct lu_dirent *)lod_env_info(env)->lti_key;
766 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
767 struct dt_object *obj = dt_object_child(&lo->ldo_obj);
768 struct lmv_mds_md_v1 *lmv1 = buf->lb_buf;
770 const struct dt_it_ops *iops;
772 __u32 magic = le32_to_cpu(lmv1->lmv_magic);
777 /* If it is not a striped directory, then load nothing. */
778 if (magic != LMV_MAGIC_V1)
781 /* If it is in migration (or failure), then load nothing. */
782 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
785 stripes = le32_to_cpu(lmv1->lmv_stripe_count);
789 rc = lmv_mds_md_size(stripes, magic);
793 if (buf->lb_len < lmv1_size) {
802 lu_buf_alloc(buf, lmv1_size);
807 memcpy(buf->lb_buf, tbuf.lb_buf, tbuf.lb_len);
810 if (unlikely(!dt_try_as_dir(env, obj)))
813 memset(&lmv1->lmv_stripe_fids[0], 0, stripes * sizeof(struct lu_fid));
814 iops = &obj->do_index_ops->dio_it;
815 it = iops->init(env, obj, LUDA_64BITHASH);
819 rc = iops->load(env, it, 0);
821 rc = iops->next(env, it);
826 char name[FID_LEN + 2] = "";
831 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
837 fid_le_to_cpu(&fid, &ent->lde_fid);
838 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
839 if (ent->lde_name[0] == '.') {
840 if (ent->lde_namelen == 1)
843 if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
847 len = snprintf(name, sizeof(name),
848 DFID":", PFID(&ent->lde_fid));
849 /* The ent->lde_name is composed of ${FID}:${index} */
850 if (ent->lde_namelen < len + 1 ||
851 memcmp(ent->lde_name, name, len) != 0) {
852 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
853 "%s: invalid shard name %.*s with the FID "DFID
854 " for the striped directory "DFID", %s\n",
855 lod2obd(lod)->obd_name, ent->lde_namelen,
856 ent->lde_name, PFID(&fid),
857 PFID(lu_object_fid(&obj->do_lu)),
858 lod->lod_lmv_failout ? "failout" : "skip");
860 if (lod->lod_lmv_failout)
868 if (ent->lde_name[len] < '0' ||
869 ent->lde_name[len] > '9') {
870 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
871 "%s: invalid shard name %.*s with the "
872 "FID "DFID" for the striped directory "
874 lod2obd(lod)->obd_name, ent->lde_namelen,
875 ent->lde_name, PFID(&fid),
876 PFID(lu_object_fid(&obj->do_lu)),
877 lod->lod_lmv_failout ?
880 if (lod->lod_lmv_failout)
886 index = index * 10 + ent->lde_name[len++] - '0';
887 } while (len < ent->lde_namelen);
889 if (len == ent->lde_namelen) {
890 /* Out of LMV EA range. */
891 if (index >= stripes) {
892 CERROR("%s: the shard %.*s for the striped "
893 "directory "DFID" is out of the known "
894 "LMV EA range [0 - %u], failout\n",
895 lod2obd(lod)->obd_name, ent->lde_namelen,
897 PFID(lu_object_fid(&obj->do_lu)),
903 /* The slot has been occupied. */
904 if (!fid_is_zero(&lmv1->lmv_stripe_fids[index])) {
908 &lmv1->lmv_stripe_fids[index]);
909 CERROR("%s: both the shard "DFID" and "DFID
910 " for the striped directory "DFID
911 " claim the same LMV EA slot at the "
912 "index %d, failout\n",
913 lod2obd(lod)->obd_name,
914 PFID(&fid0), PFID(&fid),
915 PFID(lu_object_fid(&obj->do_lu)), index);
920 /* stored as LE mode */
921 lmv1->lmv_stripe_fids[index] = ent->lde_fid;
924 rc = iops->next(env, it);
931 RETURN(rc > 0 ? lmv_mds_md_size(stripes, magic) : rc);
935 * Implementation of dt_object_operations::do_index_try.
937 * \see dt_object_operations::do_index_try() in the API description for details.
939 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
940 const struct dt_index_features *feat)
942 struct lod_object *lo = lod_dt_obj(dt);
943 struct dt_object *next = dt_object_child(dt);
947 LASSERT(next->do_ops);
948 LASSERT(next->do_ops->do_index_try);
950 rc = lod_load_striping_locked(env, lo);
954 rc = next->do_ops->do_index_try(env, next, feat);
958 if (lo->ldo_dir_stripe_count > 0) {
961 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
962 if (dt_object_exists(lo->ldo_stripe[i]) == 0)
964 rc = lo->ldo_stripe[i]->do_ops->do_index_try(env,
965 lo->ldo_stripe[i], feat);
969 dt->do_index_ops = &lod_striped_index_ops;
971 dt->do_index_ops = &lod_index_ops;
978 * Implementation of dt_object_operations::do_read_lock.
980 * \see dt_object_operations::do_read_lock() in the API description for details.
982 static void lod_read_lock(const struct lu_env *env, struct dt_object *dt,
985 dt_read_lock(env, dt_object_child(dt), role);
989 * Implementation of dt_object_operations::do_write_lock.
991 * \see dt_object_operations::do_write_lock() in the API description for
994 static void lod_write_lock(const struct lu_env *env, struct dt_object *dt,
997 dt_write_lock(env, dt_object_child(dt), role);
1001 * Implementation of dt_object_operations::do_read_unlock.
1003 * \see dt_object_operations::do_read_unlock() in the API description for
1006 static void lod_read_unlock(const struct lu_env *env, struct dt_object *dt)
1008 dt_read_unlock(env, dt_object_child(dt));
1012 * Implementation of dt_object_operations::do_write_unlock.
1014 * \see dt_object_operations::do_write_unlock() in the API description for
1017 static void lod_write_unlock(const struct lu_env *env, struct dt_object *dt)
1019 dt_write_unlock(env, dt_object_child(dt));
1023 * Implementation of dt_object_operations::do_write_locked.
1025 * \see dt_object_operations::do_write_locked() in the API description for
1028 static int lod_write_locked(const struct lu_env *env, struct dt_object *dt)
1030 return dt_write_locked(env, dt_object_child(dt));
1034 * Implementation of dt_object_operations::do_attr_get.
1036 * \see dt_object_operations::do_attr_get() in the API description for details.
1038 static int lod_attr_get(const struct lu_env *env,
1039 struct dt_object *dt,
1040 struct lu_attr *attr)
1042 /* Note: for striped directory, client will merge attributes
1043 * from all of the sub-stripes see lmv_merge_attr(), and there
1044 * no MDD logic depend on directory nlink/size/time, so we can
1045 * always use master inode nlink and size for now. */
1046 return dt_attr_get(env, dt_object_child(dt), attr);
1049 int lod_obj_for_each_stripe(const struct lu_env *env, struct lod_object *lo,
1051 struct lod_obj_stripe_cb_data *data)
1053 struct lod_layout_component *lod_comp;
1057 LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
1058 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1059 lod_comp = &lo->ldo_comp_entries[i];
1061 if (lod_comp->llc_stripe == NULL)
1064 /* has stripe but not inited yet, this component has been
1065 * declared to be created, but hasn't created yet.
1067 if (!lod_comp_inited(lod_comp))
1070 if (data->locd_comp_skip_cb &&
1071 data->locd_comp_skip_cb(env, lo, i, data))
1074 LASSERT(lod_comp->llc_stripe_count > 0);
1075 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
1076 struct dt_object *dt = lod_comp->llc_stripe[j];
1080 rc = data->locd_stripe_cb(env, lo, dt, th, i, j, data);
1088 static bool lod_obj_attr_set_comp_skip_cb(const struct lu_env *env,
1089 struct lod_object *lo, int comp_idx,
1090 struct lod_obj_stripe_cb_data *data)
1092 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
1093 bool skipped = false;
1095 if (!(data->locd_attr->la_valid & LA_LAYOUT_VERSION))
1098 switch (lo->ldo_flr_state) {
1099 case LCM_FL_WRITE_PENDING: {
1102 /* skip stale components */
1103 if (lod_comp->llc_flags & LCME_FL_STALE) {
1108 /* skip valid and overlapping components, therefore any
1109 * attempts to write overlapped components will never succeed
1110 * because client will get EINPROGRESS. */
1111 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1115 if (lo->ldo_comp_entries[i].llc_flags & LCME_FL_STALE)
1118 if (lu_extent_is_overlapped(&lod_comp->llc_extent,
1119 &lo->ldo_comp_entries[i].llc_extent)) {
1127 LASSERTF(0, "impossible: %d\n", lo->ldo_flr_state);
1128 case LCM_FL_SYNC_PENDING:
1132 CDEBUG(D_LAYOUT, DFID": %s to set component %x to version: %u\n",
1133 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
1134 skipped ? "skipped" : "chose", lod_comp->llc_id,
1135 data->locd_attr->la_layout_version);
1141 lod_obj_stripe_attr_set_cb(const struct lu_env *env, struct lod_object *lo,
1142 struct dt_object *dt, struct thandle *th,
1143 int comp_idx, int stripe_idx,
1144 struct lod_obj_stripe_cb_data *data)
1146 if (data->locd_declare)
1147 return lod_sub_declare_attr_set(env, dt, data->locd_attr, th);
1149 if (data->locd_attr->la_valid & LA_LAYOUT_VERSION) {
1150 CDEBUG(D_LAYOUT, DFID": set layout version: %u, comp_idx: %d\n",
1151 PFID(lu_object_fid(&dt->do_lu)),
1152 data->locd_attr->la_layout_version, comp_idx);
1155 return lod_sub_attr_set(env, dt, data->locd_attr, th);
1159 * Implementation of dt_object_operations::do_declare_attr_set.
1161 * If the object is striped, then apply the changes to all the stripes.
1163 * \see dt_object_operations::do_declare_attr_set() in the API description
1166 static int lod_declare_attr_set(const struct lu_env *env,
1167 struct dt_object *dt,
1168 const struct lu_attr *attr,
1171 struct dt_object *next = dt_object_child(dt);
1172 struct lod_object *lo = lod_dt_obj(dt);
1177 * declare setattr on the local object
1179 rc = lod_sub_declare_attr_set(env, next, attr, th);
1183 /* osp_declare_attr_set() ignores all attributes other than
1184 * UID, GID, PROJID, and size, and osp_attr_set() ignores all
1185 * but UID, GID and PROJID. Declaration of size attr setting
1186 * happens through lod_declare_init_size(), and not through
1187 * this function. Therefore we need not load striping unless
1188 * ownership is changing. This should save memory and (we hope)
1189 * speed up rename().
1191 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1192 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1195 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1198 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID | LA_MODE |
1199 LA_ATIME | LA_MTIME | LA_CTIME |
1204 * load striping information, notice we don't do this when object
1205 * is being initialized as we don't need this information till
1206 * few specific cases like destroy, chown
1208 rc = lod_load_striping(env, lo);
1212 if (!lod_obj_is_striped(dt))
1216 * if object is striped declare changes on the stripes
1218 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1219 LASSERT(lo->ldo_stripe);
1220 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1221 if (lo->ldo_stripe[i] == NULL)
1223 rc = lod_sub_declare_attr_set(env, lo->ldo_stripe[i],
1229 struct lod_obj_stripe_cb_data data = { { 0 } };
1231 data.locd_attr = attr;
1232 data.locd_declare = true;
1233 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1234 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1240 if (!dt_object_exists(next) || dt_object_remote(next) ||
1241 !S_ISREG(attr->la_mode))
1244 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1245 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
1249 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1250 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1251 struct lod_thread_info *info = lod_env_info(env);
1252 struct lu_buf *buf = &info->lti_buf;
1254 buf->lb_buf = info->lti_ea_store;
1255 buf->lb_len = info->lti_ea_store_size;
1256 rc = lod_sub_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1257 LU_XATTR_REPLACE, th);
1264 * Implementation of dt_object_operations::do_attr_set.
1266 * If the object is striped, then apply the changes to all or subset of
1267 * the stripes depending on the object type and specific attributes.
1269 * \see dt_object_operations::do_attr_set() in the API description for details.
1271 static int lod_attr_set(const struct lu_env *env,
1272 struct dt_object *dt,
1273 const struct lu_attr *attr,
1276 struct dt_object *next = dt_object_child(dt);
1277 struct lod_object *lo = lod_dt_obj(dt);
1282 * apply changes to the local object
1284 rc = lod_sub_attr_set(env, next, attr, th);
1288 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1289 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1292 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1295 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE | LA_PROJID |
1296 LA_ATIME | LA_MTIME | LA_CTIME |
1301 /* FIXME: a tricky case in the code path of mdd_layout_change():
1302 * the in-memory striping information has been freed in lod_xattr_set()
1303 * due to layout change. It has to load stripe here again. It only
1304 * changes flags of layout so declare_attr_set() is still accurate */
1305 rc = lod_load_striping_locked(env, lo);
1309 if (!lod_obj_is_striped(dt))
1313 * if object is striped, apply changes to all the stripes
1315 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1316 LASSERT(lo->ldo_stripe);
1317 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1318 if (unlikely(lo->ldo_stripe[i] == NULL))
1321 if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1324 rc = lod_sub_attr_set(env, lo->ldo_stripe[i], attr, th);
1329 struct lod_obj_stripe_cb_data data = { { 0 } };
1331 data.locd_attr = attr;
1332 data.locd_declare = false;
1333 data.locd_comp_skip_cb = lod_obj_attr_set_comp_skip_cb;
1334 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1335 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1341 if (!dt_object_exists(next) || dt_object_remote(next) ||
1342 !S_ISREG(attr->la_mode))
1345 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1346 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
1350 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1351 struct lod_thread_info *info = lod_env_info(env);
1352 struct lu_buf *buf = &info->lti_buf;
1353 struct ost_id *oi = &info->lti_ostid;
1354 struct lu_fid *fid = &info->lti_fid;
1355 struct lov_mds_md_v1 *lmm;
1356 struct lov_ost_data_v1 *objs;
1359 rc = lod_get_lov_ea(env, lo);
1363 buf->lb_buf = info->lti_ea_store;
1364 buf->lb_len = info->lti_ea_store_size;
1365 lmm = info->lti_ea_store;
1366 magic = le32_to_cpu(lmm->lmm_magic);
1367 if (magic == LOV_MAGIC_COMP_V1) {
1368 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1369 struct lov_comp_md_entry_v1 *lcme =
1370 &lcm->lcm_entries[0];
1372 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1373 magic = le32_to_cpu(lmm->lmm_magic);
1376 if (magic == LOV_MAGIC_V1)
1377 objs = &(lmm->lmm_objects[0]);
1379 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1380 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1381 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1383 fid_to_ostid(fid, oi);
1384 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1386 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1387 LU_XATTR_REPLACE, th);
1388 } else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1389 struct lod_thread_info *info = lod_env_info(env);
1390 struct lu_buf *buf = &info->lti_buf;
1391 struct lov_comp_md_v1 *lcm;
1392 struct lov_comp_md_entry_v1 *lcme;
1394 rc = lod_get_lov_ea(env, lo);
1398 buf->lb_buf = info->lti_ea_store;
1399 buf->lb_len = info->lti_ea_store_size;
1401 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
1404 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1405 lcme = &lcm->lcm_entries[0];
1406 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1407 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1409 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1410 LU_XATTR_REPLACE, th);
1417 * Implementation of dt_object_operations::do_xattr_get.
1419 * If LOV EA is requested from the root object and it's not
1420 * found, then return default striping for the filesystem.
1422 * \see dt_object_operations::do_xattr_get() in the API description for details.
1424 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1425 struct lu_buf *buf, const char *name)
1427 struct lod_thread_info *info = lod_env_info(env);
1428 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1433 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1434 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1435 struct lmv_mds_md_v1 *lmv1;
1438 if (rc > (typeof(rc))sizeof(*lmv1))
1441 if (rc < (typeof(rc))sizeof(*lmv1))
1442 RETURN(rc = rc > 0 ? -EINVAL : rc);
1444 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1445 CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1447 info->lti_buf.lb_buf = info->lti_key;
1448 info->lti_buf.lb_len = sizeof(*lmv1);
1449 rc = dt_xattr_get(env, dt_object_child(dt),
1450 &info->lti_buf, name);
1451 if (unlikely(rc != sizeof(*lmv1)))
1452 RETURN(rc = rc > 0 ? -EINVAL : rc);
1454 lmv1 = info->lti_buf.lb_buf;
1455 /* The on-disk LMV EA only contains header, but the
1456 * returned LMV EA size should contain the space for
1457 * the FIDs of all shards of the striped directory. */
1458 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1459 rc = lmv_mds_md_size(
1460 le32_to_cpu(lmv1->lmv_stripe_count),
1463 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1467 RETURN(rc = rc1 != 0 ? rc1 : rc);
1470 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1474 * XXX: Only used by lfsck
1476 * lod returns default striping on the real root of the device
1477 * this is like the root stores default striping for the whole
1478 * filesystem. historically we've been using a different approach
1479 * and store it in the config.
1481 dt_root_get(env, dev->lod_child, &info->lti_fid);
1482 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1484 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1485 struct lov_user_md *lum = buf->lb_buf;
1486 struct lov_desc *desc = &dev->lod_desc;
1488 if (buf->lb_buf == NULL) {
1490 } else if (buf->lb_len >= sizeof(*lum)) {
1491 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1492 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1493 lmm_oi_set_id(&lum->lmm_oi, 0);
1494 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1495 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1496 lum->lmm_stripe_size = cpu_to_le32(
1497 desc->ld_default_stripe_size);
1498 lum->lmm_stripe_count = cpu_to_le16(
1499 desc->ld_default_stripe_count);
1500 lum->lmm_stripe_offset = cpu_to_le16(
1501 desc->ld_default_stripe_offset);
1514 * Checks that the magic of the stripe is sane.
1516 * \param[in] lod lod device
1517 * \param[in] lum a buffer storing LMV EA to verify
1519 * \retval 0 if the EA is sane
1520 * \retval negative otherwise
1522 static int lod_verify_md_striping(struct lod_device *lod,
1523 const struct lmv_user_md_v1 *lum)
1525 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1526 CERROR("%s: invalid lmv_user_md: magic = %x, "
1527 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1528 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1529 (int)le32_to_cpu(lum->lum_stripe_offset),
1530 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1538 * Initialize LMV EA for a slave.
1540 * Initialize slave's LMV EA from the master's LMV EA.
1542 * \param[in] master_lmv a buffer containing master's EA
1543 * \param[out] slave_lmv a buffer where slave's EA will be stored
1546 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1547 const struct lmv_mds_md_v1 *master_lmv)
1549 *slave_lmv = *master_lmv;
1550 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1556 * Generate LMV EA from the object passed as \a dt. The object must have
1557 * the stripes created and initialized.
1559 * \param[in] env execution environment
1560 * \param[in] dt object
1561 * \param[out] lmv_buf buffer storing generated LMV EA
1563 * \retval 0 on success
1564 * \retval negative if failed
1566 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1567 struct lu_buf *lmv_buf)
1569 struct lod_thread_info *info = lod_env_info(env);
1570 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1571 struct lod_object *lo = lod_dt_obj(dt);
1572 struct lmv_mds_md_v1 *lmm1;
1574 int type = LU_SEQ_RANGE_ANY;
1579 LASSERT(lo->ldo_dir_striped != 0);
1580 LASSERT(lo->ldo_dir_stripe_count > 0);
1581 stripe_count = lo->ldo_dir_stripe_count;
1582 /* Only store the LMV EA heahder on the disk. */
1583 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1584 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1588 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1591 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1592 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1593 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1594 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1595 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1600 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1601 lmv_buf->lb_buf = info->lti_ea_store;
1602 lmv_buf->lb_len = sizeof(*lmm1);
1608 * Create in-core represenation for a striped directory.
1610 * Parse the buffer containing LMV EA and instantiate LU objects
1611 * representing the stripe objects. The pointers to the objects are
1612 * stored in ldo_stripe field of \a lo. This function is used when
1613 * we need to access an already created object (i.e. load from a disk).
1615 * \param[in] env execution environment
1616 * \param[in] lo lod object
1617 * \param[in] buf buffer containing LMV EA
1619 * \retval 0 on success
1620 * \retval negative if failed
1622 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1623 const struct lu_buf *buf)
1625 struct lod_thread_info *info = lod_env_info(env);
1626 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1627 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1628 struct dt_object **stripe;
1629 union lmv_mds_md *lmm = buf->lb_buf;
1630 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1631 struct lu_fid *fid = &info->lti_fid;
1636 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1639 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1640 lo->ldo_dir_slave_stripe = 1;
1644 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1647 if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1650 LASSERT(lo->ldo_stripe == NULL);
1651 OBD_ALLOC(stripe, sizeof(stripe[0]) *
1652 (le32_to_cpu(lmv1->lmv_stripe_count)));
1656 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1657 struct dt_device *tgt_dt;
1658 struct dt_object *dto;
1659 int type = LU_SEQ_RANGE_ANY;
1662 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1663 if (!fid_is_sane(fid))
1664 GOTO(out, rc = -ESTALE);
1666 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1670 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1671 tgt_dt = lod->lod_child;
1673 struct lod_tgt_desc *tgt;
1675 tgt = LTD_TGT(ltd, idx);
1677 GOTO(out, rc = -ESTALE);
1678 tgt_dt = tgt->ltd_tgt;
1681 dto = dt_locate_at(env, tgt_dt, fid,
1682 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1685 GOTO(out, rc = PTR_ERR(dto));
1690 lo->ldo_stripe = stripe;
1691 lo->ldo_dir_stripe_count = le32_to_cpu(lmv1->lmv_stripe_count);
1692 lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1694 lod_object_free_striping(env, lo);
1700 * Declare create a striped directory.
1702 * Declare creating a striped directory with a given stripe pattern on the
1703 * specified MDTs. A striped directory is represented as a regular directory
1704 * - an index listing all the stripes. The stripes point back to the master
1705 * object with ".." and LinkEA. The master object gets LMV EA which
1706 * identifies it as a striped directory. The function allocates FIDs
1709 * \param[in] env execution environment
1710 * \param[in] dt object
1711 * \param[in] attr attributes to initialize the objects with
1712 * \param[in] dof type of objects to be created
1713 * \param[in] th transaction handle
1715 * \retval 0 on success
1716 * \retval negative if failed
1718 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1719 struct dt_object *dt,
1720 struct lu_attr *attr,
1721 struct dt_object_format *dof,
1724 struct lod_thread_info *info = lod_env_info(env);
1725 struct lu_buf lmv_buf;
1726 struct lu_buf slave_lmv_buf;
1727 struct lmv_mds_md_v1 *lmm;
1728 struct lmv_mds_md_v1 *slave_lmm = NULL;
1729 struct dt_insert_rec *rec = &info->lti_dt_rec;
1730 struct lod_object *lo = lod_dt_obj(dt);
1735 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1738 lmm = lmv_buf.lb_buf;
1740 OBD_ALLOC_PTR(slave_lmm);
1741 if (slave_lmm == NULL)
1742 GOTO(out, rc = -ENOMEM);
1744 lod_prep_slave_lmv_md(slave_lmm, lmm);
1745 slave_lmv_buf.lb_buf = slave_lmm;
1746 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1748 if (!dt_try_as_dir(env, dt_object_child(dt)))
1749 GOTO(out, rc = -EINVAL);
1751 rec->rec_type = S_IFDIR;
1752 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1753 struct dt_object *dto = lo->ldo_stripe[i];
1754 char *stripe_name = info->lti_key;
1755 struct lu_name *sname;
1756 struct linkea_data ldata = { NULL };
1757 struct lu_buf linkea_buf;
1759 rc = lod_sub_declare_create(env, dto, attr, NULL, dof, th);
1763 if (!dt_try_as_dir(env, dto))
1764 GOTO(out, rc = -EINVAL);
1766 rc = lod_sub_declare_ref_add(env, dto, th);
1770 rec->rec_fid = lu_object_fid(&dto->do_lu);
1771 rc = lod_sub_declare_insert(env, dto,
1772 (const struct dt_rec *)rec,
1773 (const struct dt_key *)dot, th);
1777 /* master stripe FID will be put to .. */
1778 rec->rec_fid = lu_object_fid(&dt->do_lu);
1779 rc = lod_sub_declare_insert(env, dto,
1780 (const struct dt_rec *)rec,
1781 (const struct dt_key *)dotdot, th);
1785 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1786 cfs_fail_val != i) {
1787 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1789 slave_lmm->lmv_master_mdt_index =
1792 slave_lmm->lmv_master_mdt_index =
1794 rc = lod_sub_declare_xattr_set(env, dto, &slave_lmv_buf,
1795 XATTR_NAME_LMV, 0, th);
1800 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1802 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1803 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1805 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1806 PFID(lu_object_fid(&dto->do_lu)), i);
1808 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1809 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1810 sname, lu_object_fid(&dt->do_lu));
1814 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1815 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1816 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
1817 XATTR_NAME_LINK, 0, th);
1821 rec->rec_fid = lu_object_fid(&dto->do_lu);
1822 rc = lod_sub_declare_insert(env, dt_object_child(dt),
1823 (const struct dt_rec *)rec,
1824 (const struct dt_key *)stripe_name,
1829 rc = lod_sub_declare_ref_add(env, dt_object_child(dt), th);
1834 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt),
1835 &lmv_buf, XATTR_NAME_LMV, 0, th);
1839 if (slave_lmm != NULL)
1840 OBD_FREE_PTR(slave_lmm);
1845 static int lod_prep_md_striped_create(const struct lu_env *env,
1846 struct dt_object *dt,
1847 struct lu_attr *attr,
1848 const struct lmv_user_md_v1 *lum,
1849 struct dt_object_format *dof,
1852 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1853 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1854 struct lod_object *lo = lod_dt_obj(dt);
1855 struct dt_object **stripe;
1864 /* The lum has been verifed in lod_verify_md_striping */
1865 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
1866 LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1868 stripe_count = le32_to_cpu(lum->lum_stripe_count);
1870 OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1871 if (idx_array == NULL)
1874 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1876 GOTO(out_free, rc = -ENOMEM);
1878 /* Start index must be the master MDT */
1879 master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1880 idx_array[0] = master_index;
1881 for (i = 0; i < stripe_count; i++) {
1882 struct lod_tgt_desc *tgt = NULL;
1883 struct dt_object *dto;
1884 struct lu_fid fid = { 0 };
1886 struct lu_object_conf conf = { 0 };
1887 struct dt_device *tgt_dt = NULL;
1889 /* Try to find next avaible target */
1891 for (j = 0; j < lod->lod_remote_mdt_count;
1892 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1893 bool already_allocated = false;
1896 CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
1897 idx, lod->lod_remote_mdt_count + 1, i);
1899 if (likely(!OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
1900 /* check whether the idx already exists
1901 * in current allocated array */
1902 for (k = 0; k < i; k++) {
1903 if (idx_array[k] == idx) {
1904 already_allocated = true;
1909 if (already_allocated)
1913 /* Sigh, this index is not in the bitmap, let's check
1914 * next available target */
1915 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) &&
1916 idx != master_index)
1919 if (idx == master_index) {
1920 /* Allocate the FID locally */
1921 rc = obd_fid_alloc(env, lod->lod_child_exp,
1925 tgt_dt = lod->lod_child;
1929 /* check the status of the OSP */
1930 tgt = LTD_TGT(ltd, idx);
1934 tgt_dt = tgt->ltd_tgt;
1935 rc = dt_statfs(env, tgt_dt, NULL);
1937 /* this OSP doesn't feel well */
1942 rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1951 /* Can not allocate more stripes */
1952 if (j == lod->lod_remote_mdt_count) {
1953 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1954 lod2obd(lod)->obd_name, stripe_count, i);
1958 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
1959 idx, i, PFID(&fid));
1961 /* Set the start index for next stripe allocation */
1962 if (i < stripe_count - 1)
1963 idx_array[i + 1] = (idx + 1) %
1964 (lod->lod_remote_mdt_count + 1);
1965 /* tgt_dt and fid must be ready after search avaible OSP
1966 * in the above loop */
1967 LASSERT(tgt_dt != NULL);
1968 LASSERT(fid_is_sane(&fid));
1969 conf.loc_flags = LOC_F_NEW;
1970 dto = dt_locate_at(env, tgt_dt, &fid,
1971 dt->do_lu.lo_dev->ld_site->ls_top_dev,
1974 GOTO(out_put, rc = PTR_ERR(dto));
1978 lo->ldo_dir_stripe_loaded = 1;
1979 lo->ldo_dir_striped = 1;
1980 lo->ldo_stripe = stripe;
1981 lo->ldo_dir_stripe_count = i;
1982 lo->ldo_dir_stripes_allocated = stripe_count;
1984 if (lo->ldo_dir_stripe_count == 0)
1985 GOTO(out_put, rc = -ENOSPC);
1987 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
1993 for (i = 0; i < stripe_count; i++)
1994 if (stripe[i] != NULL)
1995 dt_object_put(env, stripe[i]);
1996 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
1997 lo->ldo_dir_stripe_count = 0;
1998 lo->ldo_dir_stripes_allocated = 0;
1999 lo->ldo_stripe = NULL;
2003 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
2009 * Declare create striped md object.
2011 * The function declares intention to create a striped directory. This is a
2012 * wrapper for lod_prep_md_striped_create(). The only additional functionality
2013 * is to verify pattern \a lum_buf is good. Check that function for the details.
2015 * \param[in] env execution environment
2016 * \param[in] dt object
2017 * \param[in] attr attributes to initialize the objects with
2018 * \param[in] lum_buf a pattern specifying the number of stripes and
2020 * \param[in] dof type of objects to be created
2021 * \param[in] th transaction handle
2023 * \retval 0 on success
2024 * \retval negative if failed
2027 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
2028 struct dt_object *dt,
2029 struct lu_attr *attr,
2030 const struct lu_buf *lum_buf,
2031 struct dt_object_format *dof,
2034 struct lod_object *lo = lod_dt_obj(dt);
2035 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
2036 struct lmv_user_md_v1 *lum;
2040 lum = lum_buf->lb_buf;
2041 LASSERT(lum != NULL);
2043 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
2044 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
2045 (int)le32_to_cpu(lum->lum_stripe_offset));
2047 if (le32_to_cpu(lum->lum_stripe_count) == 0)
2050 rc = lod_verify_md_striping(lod, lum);
2054 /* prepare dir striped objects */
2055 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2057 /* failed to create striping, let's reset
2058 * config so that others don't get confused */
2059 lod_object_free_striping(env, lo);
2067 * Implementation of dt_object_operations::do_declare_xattr_set.
2069 * Used with regular (non-striped) objects. Basically it
2070 * initializes the striping information and applies the
2071 * change to all the stripes.
2073 * \see dt_object_operations::do_declare_xattr_set() in the API description
2076 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2077 struct dt_object *dt,
2078 const struct lu_buf *buf,
2079 const char *name, int fl,
2082 struct dt_object *next = dt_object_child(dt);
2083 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2084 struct lod_object *lo = lod_dt_obj(dt);
2089 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2090 struct lmv_user_md_v1 *lum;
2092 LASSERT(buf != NULL && buf->lb_buf != NULL);
2094 rc = lod_verify_md_striping(d, lum);
2097 } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2098 rc = lod_verify_striping(d, lo, buf, false);
2103 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2107 /* Note: Do not set LinkEA on sub-stripes, otherwise
2108 * it will confuse the fid2path process(see mdt_path_current()).
2109 * The linkEA between master and sub-stripes is set in
2110 * lod_xattr_set_lmv(). */
2111 if (strcmp(name, XATTR_NAME_LINK) == 0)
2114 /* set xattr to each stripes, if needed */
2115 rc = lod_load_striping(env, lo);
2119 if (lo->ldo_dir_stripe_count == 0)
2122 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2123 LASSERT(lo->ldo_stripe[i]);
2125 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2135 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2136 struct lod_object *lo,
2137 struct dt_object *dt, struct thandle *th,
2138 int comp_idx, int stripe_idx,
2139 struct lod_obj_stripe_cb_data *data)
2141 struct lod_thread_info *info = lod_env_info(env);
2142 struct filter_fid *ff = &info->lti_ff;
2143 struct lu_buf *buf = &info->lti_buf;
2147 buf->lb_len = sizeof(*ff);
2148 rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2155 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2156 ff->ff_parent.f_ver = stripe_idx;
2157 fid_cpu_to_le(&ff->ff_parent, &ff->ff_parent);
2158 if (data->locd_declare)
2159 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2160 LU_XATTR_REPLACE, th);
2162 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2163 LU_XATTR_REPLACE, th);
2169 * Reset parent FID on OST object
2171 * Replace parent FID with @dt object FID, which is only called during migration
2172 * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2173 * the FID is changed.
2175 * \param[in] env execution environment
2176 * \param[in] dt dt_object whose stripes's parent FID will be reset
2177 * \parem[in] th thandle
2178 * \param[in] declare if it is declare
2180 * \retval 0 if reset succeeds
2181 * \retval negative errno if reset fails
2183 static int lod_replace_parent_fid(const struct lu_env *env,
2184 struct dt_object *dt,
2185 struct thandle *th, bool declare)
2187 struct lod_object *lo = lod_dt_obj(dt);
2188 struct lod_thread_info *info = lod_env_info(env);
2189 struct lu_buf *buf = &info->lti_buf;
2190 struct filter_fid *ff;
2191 struct lod_obj_stripe_cb_data data = { { 0 } };
2195 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2197 /* set xattr to each stripes, if needed */
2198 rc = lod_load_striping(env, lo);
2202 if (!lod_obj_is_striped(dt))
2205 if (info->lti_ea_store_size < sizeof(*ff)) {
2206 rc = lod_ea_store_resize(info, sizeof(*ff));
2211 buf->lb_buf = info->lti_ea_store;
2212 buf->lb_len = info->lti_ea_store_size;
2214 data.locd_declare = declare;
2215 data.locd_stripe_cb = lod_obj_stripe_replace_parent_fid_cb;
2216 rc = lod_obj_for_each_stripe(env, lo, th, &data);
2221 inline __u16 lod_comp_entry_stripe_count(struct lod_object *lo,
2222 struct lod_layout_component *entry,
2225 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2229 else if (lod_comp_inited(entry))
2230 return entry->llc_stripe_count;
2231 else if ((__u16)-1 == entry->llc_stripe_count)
2232 return lod->lod_desc.ld_tgt_count;
2234 return lod_get_stripe_count(lod, lo, entry->llc_stripe_count);
2237 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2239 int magic, size = 0, i;
2240 struct lod_layout_component *comp_entries;
2245 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2246 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2248 lo->ldo_def_striping->lds_def_striping_is_composite;
2250 comp_cnt = lo->ldo_comp_cnt;
2251 comp_entries = lo->ldo_comp_entries;
2252 is_composite = lo->ldo_is_composite;
2256 LASSERT(comp_cnt != 0 && comp_entries != NULL);
2258 size = sizeof(struct lov_comp_md_v1) +
2259 sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2260 LASSERT(size % sizeof(__u64) == 0);
2263 for (i = 0; i < comp_cnt; i++) {
2266 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2267 stripe_count = lod_comp_entry_stripe_count(lo, &comp_entries[i],
2269 if (!is_dir && is_composite)
2270 lod_comp_shrink_stripe_count(&comp_entries[i],
2273 size += lov_user_md_size(stripe_count, magic);
2274 LASSERT(size % sizeof(__u64) == 0);
2280 * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2281 * the xattr value is binary lov_comp_md_v1 which contains component(s)
2284 * \param[in] env execution environment
2285 * \param[in] dt dt_object to add components on
2286 * \param[in] buf buffer contains components to be added
2287 * \parem[in] th thandle
2289 * \retval 0 on success
2290 * \retval negative errno on failure
2292 static int lod_declare_layout_add(const struct lu_env *env,
2293 struct dt_object *dt,
2294 const struct lu_buf *buf,
2297 struct lod_thread_info *info = lod_env_info(env);
2298 struct lod_layout_component *comp_array, *lod_comp, *old_array;
2299 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2300 struct dt_object *next = dt_object_child(dt);
2301 struct lov_desc *desc = &d->lod_desc;
2302 struct lod_object *lo = lod_dt_obj(dt);
2303 struct lov_user_md_v3 *v3;
2304 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2306 int i, rc, array_cnt, old_array_cnt;
2309 LASSERT(lo->ldo_is_composite);
2311 if (lo->ldo_flr_state != LCM_FL_NOT_FLR)
2314 rc = lod_verify_striping(d, lo, buf, false);
2318 magic = comp_v1->lcm_magic;
2319 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2320 lustre_swab_lov_comp_md_v1(comp_v1);
2321 magic = comp_v1->lcm_magic;
2324 if (magic != LOV_USER_MAGIC_COMP_V1)
2327 array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2328 OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2329 if (comp_array == NULL)
2332 memcpy(comp_array, lo->ldo_comp_entries,
2333 sizeof(*comp_array) * lo->ldo_comp_cnt);
2335 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2336 struct lov_user_md_v1 *v1;
2337 struct lu_extent *ext;
2339 v1 = (struct lov_user_md *)((char *)comp_v1 +
2340 comp_v1->lcm_entries[i].lcme_offset);
2341 ext = &comp_v1->lcm_entries[i].lcme_extent;
2343 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2344 lod_comp->llc_extent.e_start = ext->e_start;
2345 lod_comp->llc_extent.e_end = ext->e_end;
2346 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2347 lod_comp->llc_flags = comp_v1->lcm_entries[i].lcme_flags;
2349 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2350 if (!lod_comp->llc_stripe_count ||
2351 lod_comp->llc_stripe_count == (__u16)-1)
2352 lod_comp->llc_stripe_count =
2353 desc->ld_default_stripe_count;
2354 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2355 if (!lod_comp->llc_stripe_size)
2356 lod_comp->llc_stripe_size =
2357 desc->ld_default_stripe_size;
2359 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2360 v3 = (struct lov_user_md_v3 *) v1;
2361 if (v3->lmm_pool_name[0] != '\0') {
2362 rc = lod_set_pool(&lod_comp->llc_pool,
2370 old_array = lo->ldo_comp_entries;
2371 old_array_cnt = lo->ldo_comp_cnt;
2373 lo->ldo_comp_entries = comp_array;
2374 lo->ldo_comp_cnt = array_cnt;
2376 /* No need to increase layout generation here, it will be increased
2377 * later when generating component ID for the new components */
2379 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2380 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2381 XATTR_NAME_LOV, 0, th);
2383 lo->ldo_comp_entries = old_array;
2384 lo->ldo_comp_cnt = old_array_cnt;
2388 OBD_FREE(old_array, sizeof(*lod_comp) * old_array_cnt);
2390 LASSERT(lo->ldo_mirror_count == 1);
2391 lo->ldo_mirrors[0].lme_end = array_cnt - 1;
2396 for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2397 lod_comp = &comp_array[i];
2398 if (lod_comp->llc_pool != NULL) {
2399 OBD_FREE(lod_comp->llc_pool,
2400 strlen(lod_comp->llc_pool) + 1);
2401 lod_comp->llc_pool = NULL;
2404 OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2409 * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2410 * the '$field' can only be 'flags' now. The xattr value is binary
2411 * lov_comp_md_v1 which contains the component ID(s) and the value of
2412 * the field to be modified.
2414 * \param[in] env execution environment
2415 * \param[in] dt dt_object to be modified
2416 * \param[in] op operation string, like "set.flags"
2417 * \param[in] buf buffer contains components to be set
2418 * \parem[in] th thandle
2420 * \retval 0 on success
2421 * \retval negative errno on failure
2423 static int lod_declare_layout_set(const struct lu_env *env,
2424 struct dt_object *dt,
2425 char *op, const struct lu_buf *buf,
2428 struct lod_layout_component *lod_comp;
2429 struct lod_thread_info *info = lod_env_info(env);
2430 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2431 struct lod_object *lo = lod_dt_obj(dt);
2432 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2435 bool changed = false;
2438 if (strcmp(op, "set.flags") != 0) {
2439 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2440 lod2obd(d)->obd_name, op);
2444 magic = comp_v1->lcm_magic;
2445 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2446 lustre_swab_lov_comp_md_v1(comp_v1);
2447 magic = comp_v1->lcm_magic;
2450 if (magic != LOV_USER_MAGIC_COMP_V1)
2453 if (comp_v1->lcm_entry_count == 0) {
2454 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2455 lod2obd(d)->obd_name);
2459 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2460 id = comp_v1->lcm_entries[i].lcme_id;
2462 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2463 lod_comp = &lo->ldo_comp_entries[j];
2464 if (id == lod_comp->llc_id || id == LCME_ID_ALL) {
2465 lod_comp->llc_flags =
2466 comp_v1->lcm_entries[i].lcme_flags;
2473 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2474 lod2obd(d)->obd_name);
2478 lod_obj_inc_layout_gen(lo);
2480 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2481 rc = lod_sub_declare_xattr_set(env, dt, &info->lti_buf,
2482 XATTR_NAME_LOV, 0, th);
2487 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2488 * and the xattr value is a unique component ID or a special lcme_id.
2490 * \param[in] env execution environment
2491 * \param[in] dt dt_object to be operated on
2492 * \param[in] buf buffer contains component ID or lcme_id
2493 * \parem[in] th thandle
2495 * \retval 0 on success
2496 * \retval negative errno on failure
2498 static int lod_declare_layout_del(const struct lu_env *env,
2499 struct dt_object *dt,
2500 const struct lu_buf *buf,
2503 struct lod_thread_info *info = lod_env_info(env);
2504 struct dt_object *next = dt_object_child(dt);
2505 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2506 struct lod_object *lo = lod_dt_obj(dt);
2507 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2508 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2509 __u32 magic, id, flags, neg_flags = 0;
2513 LASSERT(lo->ldo_is_composite);
2515 if (lo->ldo_flr_state != LCM_FL_NOT_FLR)
2518 magic = comp_v1->lcm_magic;
2519 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2520 lustre_swab_lov_comp_md_v1(comp_v1);
2521 magic = comp_v1->lcm_magic;
2524 if (magic != LOV_USER_MAGIC_COMP_V1)
2527 id = comp_v1->lcm_entries[0].lcme_id;
2528 flags = comp_v1->lcm_entries[0].lcme_flags;
2530 if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
2531 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
2532 lod2obd(d)->obd_name, id, flags);
2536 if (id != LCME_ID_INVAL && flags != 0) {
2537 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
2538 lod2obd(d)->obd_name);
2542 if (flags & LCME_FL_NEG) {
2543 neg_flags = flags & ~LCME_FL_NEG;
2547 left = lo->ldo_comp_cnt;
2551 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
2552 struct lod_layout_component *lod_comp;
2554 lod_comp = &lo->ldo_comp_entries[i];
2556 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
2558 else if (flags && !(flags & lod_comp->llc_flags))
2560 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
2563 if (left != (i + 1)) {
2564 CDEBUG(D_LAYOUT, "%s: this deletion will create "
2565 "a hole.\n", lod2obd(d)->obd_name);
2570 /* Mark the component as deleted */
2571 lod_comp->llc_id = LCME_ID_INVAL;
2573 /* Not instantiated component */
2574 if (lod_comp->llc_stripe == NULL)
2577 LASSERT(lod_comp->llc_stripe_count > 0);
2578 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
2579 struct dt_object *obj = lod_comp->llc_stripe[j];
2583 rc = lod_sub_declare_destroy(env, obj, th);
2589 LASSERTF(left >= 0, "left = %d\n", left);
2590 if (left == lo->ldo_comp_cnt) {
2591 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
2592 lod2obd(d)->obd_name, id);
2596 memset(attr, 0, sizeof(*attr));
2597 attr->la_valid = LA_SIZE;
2598 rc = lod_sub_declare_attr_set(env, next, attr, th);
2603 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2604 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2605 XATTR_NAME_LOV, 0, th);
2607 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
2614 * Declare layout add/set/del operations issued by special xattr names:
2616 * XATTR_LUSTRE_LOV.add add component(s) to existing file
2617 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
2618 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
2620 * \param[in] env execution environment
2621 * \param[in] dt object
2622 * \param[in] name name of xattr
2623 * \param[in] buf lu_buf contains xattr value
2624 * \param[in] th transaction handle
2626 * \retval 0 on success
2627 * \retval negative if failed
2629 static int lod_declare_modify_layout(const struct lu_env *env,
2630 struct dt_object *dt,
2632 const struct lu_buf *buf,
2635 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2636 struct lod_object *lo = lod_dt_obj(dt);
2637 struct dt_object *next = dt_object_child(&lo->ldo_obj);
2639 int rc, len = strlen(XATTR_LUSTRE_LOV);
2642 LASSERT(dt_object_exists(dt));
2644 if (strlen(name) <= len || name[len] != '.') {
2645 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
2646 lod2obd(d)->obd_name, name);
2651 dt_write_lock(env, next, 0);
2652 rc = lod_load_striping_locked(env, lo);
2656 /* the layout to be modified must be a composite layout */
2657 if (!lo->ldo_is_composite) {
2658 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
2659 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
2660 GOTO(unlock, rc = -EINVAL);
2663 op = (char *)name + len;
2664 if (strcmp(op, "add") == 0) {
2665 rc = lod_declare_layout_add(env, dt, buf, th);
2666 } else if (strcmp(op, "del") == 0) {
2667 rc = lod_declare_layout_del(env, dt, buf, th);
2668 } else if (strncmp(op, "set", strlen("set")) == 0) {
2669 rc = lod_declare_layout_set(env, dt, op, buf, th);
2671 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
2672 lod2obd(d)->obd_name, name);
2673 GOTO(unlock, rc = -ENOTSUPP);
2677 lod_object_free_striping(env, lo);
2678 dt_write_unlock(env, next);
2684 * Merge layouts to form a mirrored file.
2686 static int lod_declare_layout_merge(const struct lu_env *env,
2687 struct dt_object *dt, const struct lu_buf *mbuf,
2690 struct lod_thread_info *info = lod_env_info(env);
2691 struct lu_buf *buf = &info->lti_buf;
2692 struct lod_object *lo = lod_dt_obj(dt);
2693 struct lov_comp_md_v1 *lcm;
2694 struct lov_comp_md_v1 *cur_lcm;
2695 struct lov_comp_md_v1 *merge_lcm;
2696 struct lov_comp_md_entry_v1 *lcme;
2699 __u16 cur_entry_count;
2700 __u16 merge_entry_count;
2702 __u16 mirror_id = 0;
2707 merge_lcm = mbuf->lb_buf;
2708 if (mbuf->lb_len < sizeof(*merge_lcm))
2711 /* must be an existing layout from disk */
2712 if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
2715 merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
2717 /* do not allow to merge two mirrored files */
2718 if (le16_to_cpu(merge_lcm->lcm_mirror_count))
2721 /* verify the target buffer */
2722 rc = lod_get_lov_ea(env, lo);
2724 RETURN(rc ? : -ENODATA);
2726 cur_lcm = info->lti_ea_store;
2727 if (le32_to_cpu(cur_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
2730 cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
2732 /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
2733 mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
2734 if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
2737 /* size of new layout */
2738 size = le32_to_cpu(cur_lcm->lcm_size) +
2739 le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
2741 memset(buf, 0, sizeof(*buf));
2742 lu_buf_alloc(buf, size);
2743 if (buf->lb_buf == NULL)
2747 memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
2749 offset = sizeof(*lcm) +
2750 sizeof(*lcme) * (cur_entry_count + merge_entry_count);
2751 for (i = 0; i < cur_entry_count; i++) {
2752 struct lov_comp_md_entry_v1 *cur_lcme;
2754 lcme = &lcm->lcm_entries[i];
2755 cur_lcme = &cur_lcm->lcm_entries[i];
2757 lcme->lcme_offset = cpu_to_le32(offset);
2758 memcpy((char *)lcm + offset,
2759 (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
2760 le32_to_cpu(lcme->lcme_size));
2762 offset += le32_to_cpu(lcme->lcme_size);
2764 if (mirror_count == 1) {
2765 /* new mirrored file, create new mirror ID */
2766 id = pflr_id(1, i + 1);
2767 lcme->lcme_id = cpu_to_le32(id);
2770 id = MAX(le32_to_cpu(lcme->lcme_id), id);
2773 mirror_id = mirror_id_of(id) + 1;
2774 for (i = 0; i < merge_entry_count; i++) {
2775 struct lov_comp_md_entry_v1 *merge_lcme;
2777 merge_lcme = &merge_lcm->lcm_entries[i];
2778 lcme = &lcm->lcm_entries[cur_entry_count + i];
2780 *lcme = *merge_lcme;
2781 lcme->lcme_offset = cpu_to_le32(offset);
2783 id = pflr_id(mirror_id, i + 1);
2784 lcme->lcme_id = cpu_to_le32(id);
2786 memcpy((char *)lcm + offset,
2787 (char *)merge_lcm + le32_to_cpu(merge_lcme->lcme_offset),
2788 le32_to_cpu(lcme->lcme_size));
2790 offset += le32_to_cpu(lcme->lcme_size);
2793 /* fixup layout information */
2794 lod_obj_inc_layout_gen(lo);
2795 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
2796 lcm->lcm_size = cpu_to_le32(size);
2797 lcm->lcm_entry_count = cpu_to_le16(cur_entry_count + merge_entry_count);
2798 lcm->lcm_mirror_count = cpu_to_le16(mirror_count);
2799 if ((le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK) == LCM_FL_NOT_FLR)
2800 lcm->lcm_flags = cpu_to_le32(LCM_FL_RDONLY);
2802 LASSERT(dt_write_locked(env, dt_object_child(dt)));
2803 lod_object_free_striping(env, lo);
2804 rc = lod_parse_striping(env, lo, buf);
2808 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), buf,
2809 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
2817 * Implementation of dt_object_operations::do_declare_xattr_set.
2819 * \see dt_object_operations::do_declare_xattr_set() in the API description
2822 * the extension to the API:
2823 * - declaring LOVEA requests striping creation
2824 * - LU_XATTR_REPLACE means layout swap
2826 static int lod_declare_xattr_set(const struct lu_env *env,
2827 struct dt_object *dt,
2828 const struct lu_buf *buf,
2829 const char *name, int fl,
2832 struct dt_object *next = dt_object_child(dt);
2833 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2838 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2839 if ((S_ISREG(mode) || mode == 0) &&
2840 !(fl & (LU_XATTR_REPLACE | LU_XATTR_MERGE)) &&
2841 (strcmp(name, XATTR_NAME_LOV) == 0 ||
2842 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
2844 * this is a request to create object's striping.
2846 * allow to declare predefined striping on a new (!mode) object
2847 * which is supposed to be replay of regular file creation
2848 * (when LOV setting is declared)
2850 * LU_XATTR_REPLACE is set to indicate a layout swap
2852 if (dt_object_exists(dt)) {
2853 rc = dt_attr_get(env, next, attr);
2857 memset(attr, 0, sizeof(*attr));
2858 attr->la_valid = LA_TYPE | LA_MODE;
2859 attr->la_mode = S_IFREG;
2861 rc = lod_declare_striped_create(env, dt, attr, buf, th);
2862 } else if (fl & LU_XATTR_MERGE) {
2863 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
2864 strcmp(name, XATTR_LUSTRE_LOV) == 0);
2865 rc = lod_declare_layout_merge(env, dt, buf, th);
2866 } else if (S_ISREG(mode) &&
2867 strlen(name) > strlen(XATTR_LUSTRE_LOV) + 1 &&
2868 strncmp(name, XATTR_LUSTRE_LOV,
2869 strlen(XATTR_LUSTRE_LOV)) == 0) {
2871 * this is a request to modify object's striping.
2872 * add/set/del component(s).
2874 if (!dt_object_exists(dt))
2877 rc = lod_declare_modify_layout(env, dt, name, buf, th);
2878 } else if (S_ISDIR(mode)) {
2879 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2880 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
2881 rc = lod_replace_parent_fid(env, dt, th, true);
2883 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2890 * Apply xattr changes to the object.
2892 * Applies xattr changes to the object and the stripes if the latter exist.
2894 * \param[in] env execution environment
2895 * \param[in] dt object
2896 * \param[in] buf buffer pointing to the new value of xattr
2897 * \param[in] name name of xattr
2898 * \param[in] fl flags
2899 * \param[in] th transaction handle
2901 * \retval 0 on success
2902 * \retval negative if failed
2904 static int lod_xattr_set_internal(const struct lu_env *env,
2905 struct dt_object *dt,
2906 const struct lu_buf *buf,
2907 const char *name, int fl,
2910 struct dt_object *next = dt_object_child(dt);
2911 struct lod_object *lo = lod_dt_obj(dt);
2916 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
2917 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2920 /* Note: Do not set LinkEA on sub-stripes, otherwise
2921 * it will confuse the fid2path process(see mdt_path_current()).
2922 * The linkEA between master and sub-stripes is set in
2923 * lod_xattr_set_lmv(). */
2924 if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2927 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2928 LASSERT(lo->ldo_stripe[i]);
2930 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
2940 * Delete an extended attribute.
2942 * Deletes specified xattr from the object and the stripes if the latter exist.
2944 * \param[in] env execution environment
2945 * \param[in] dt object
2946 * \param[in] name name of xattr
2947 * \param[in] th transaction handle
2949 * \retval 0 on success
2950 * \retval negative if failed
2952 static int lod_xattr_del_internal(const struct lu_env *env,
2953 struct dt_object *dt,
2954 const char *name, struct thandle *th)
2956 struct dt_object *next = dt_object_child(dt);
2957 struct lod_object *lo = lod_dt_obj(dt);
2962 rc = lod_sub_xattr_del(env, next, name, th);
2963 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2966 if (lo->ldo_dir_stripe_count == 0)
2969 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2970 LASSERT(lo->ldo_stripe[i]);
2972 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
2981 * Set default striping on a directory.
2983 * Sets specified striping on a directory object unless it matches the default
2984 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2985 * EA. This striping will be used when regular file is being created in this
2988 * \param[in] env execution environment
2989 * \param[in] dt the striped object
2990 * \param[in] buf buffer with the striping
2991 * \param[in] name name of EA
2992 * \param[in] fl xattr flag (see OSD API description)
2993 * \param[in] th transaction handle
2995 * \retval 0 on success
2996 * \retval negative if failed
2998 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
2999 struct dt_object *dt,
3000 const struct lu_buf *buf,
3001 const char *name, int fl,
3004 struct lov_user_md_v1 *lum;
3005 struct lov_user_md_v3 *v3 = NULL;
3006 const char *pool_name = NULL;
3011 LASSERT(buf != NULL && buf->lb_buf != NULL);
3014 switch (lum->lmm_magic) {
3015 case LOV_USER_MAGIC_V3:
3017 if (v3->lmm_pool_name[0] != '\0')
3018 pool_name = v3->lmm_pool_name;
3020 case LOV_USER_MAGIC_V1:
3021 /* if { size, offset, count } = { 0, -1, 0 } and no pool
3022 * (i.e. all default values specified) then delete default
3023 * striping from dir. */
3025 "set default striping: sz %u # %u offset %d %s %s\n",
3026 (unsigned)lum->lmm_stripe_size,
3027 (unsigned)lum->lmm_stripe_count,
3028 (int)lum->lmm_stripe_offset,
3029 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
3031 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
3032 lum->lmm_stripe_count,
3033 lum->lmm_stripe_offset,
3036 case LOV_USER_MAGIC_COMP_V1:
3040 CERROR("Invalid magic %x\n", lum->lmm_magic);
3045 rc = lod_xattr_del_internal(env, dt, name, th);
3049 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3056 * Set default striping on a directory object.
3058 * Sets specified striping on a directory object unless it matches the default
3059 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3060 * EA. This striping will be used when a new directory is being created in the
3063 * \param[in] env execution environment
3064 * \param[in] dt the striped object
3065 * \param[in] buf buffer with the striping
3066 * \param[in] name name of EA
3067 * \param[in] fl xattr flag (see OSD API description)
3068 * \param[in] th transaction handle
3070 * \retval 0 on success
3071 * \retval negative if failed
3073 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
3074 struct dt_object *dt,
3075 const struct lu_buf *buf,
3076 const char *name, int fl,
3079 struct lmv_user_md_v1 *lum;
3083 LASSERT(buf != NULL && buf->lb_buf != NULL);
3086 CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
3087 le32_to_cpu(lum->lum_stripe_count),
3088 (int)le32_to_cpu(lum->lum_stripe_offset));
3090 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
3091 le32_to_cpu(lum->lum_stripe_offset)) &&
3092 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
3093 rc = lod_xattr_del_internal(env, dt, name, th);
3097 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3106 * Turn directory into a striped directory.
3108 * During replay the client sends the striping created before MDT
3109 * failure, then the layer above LOD sends this defined striping
3110 * using ->do_xattr_set(), so LOD uses this method to replay creation
3111 * of the stripes. Notice the original information for the striping
3112 * (#stripes, FIDs, etc) was transferred in declare path.
3114 * \param[in] env execution environment
3115 * \param[in] dt the striped object
3116 * \param[in] buf not used currently
3117 * \param[in] name not used currently
3118 * \param[in] fl xattr flag (see OSD API description)
3119 * \param[in] th transaction handle
3121 * \retval 0 on success
3122 * \retval negative if failed
3124 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
3125 const struct lu_buf *buf, const char *name,
3126 int fl, struct thandle *th)
3128 struct lod_object *lo = lod_dt_obj(dt);
3129 struct lod_thread_info *info = lod_env_info(env);
3130 struct lu_attr *attr = &info->lti_attr;
3131 struct dt_object_format *dof = &info->lti_format;
3132 struct lu_buf lmv_buf;
3133 struct lu_buf slave_lmv_buf;
3134 struct lmv_mds_md_v1 *lmm;
3135 struct lmv_mds_md_v1 *slave_lmm = NULL;
3136 struct dt_insert_rec *rec = &info->lti_dt_rec;
3141 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3144 /* The stripes are supposed to be allocated in declare phase,
3145 * if there are no stripes being allocated, it will skip */
3146 if (lo->ldo_dir_stripe_count == 0)
3149 rc = dt_attr_get(env, dt_object_child(dt), attr);
3153 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
3154 LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
3155 dof->dof_type = DFT_DIR;
3157 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
3160 lmm = lmv_buf.lb_buf;
3162 OBD_ALLOC_PTR(slave_lmm);
3163 if (slave_lmm == NULL)
3166 lod_prep_slave_lmv_md(slave_lmm, lmm);
3167 slave_lmv_buf.lb_buf = slave_lmm;
3168 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
3170 rec->rec_type = S_IFDIR;
3171 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3172 struct dt_object *dto;
3173 char *stripe_name = info->lti_key;
3174 struct lu_name *sname;
3175 struct linkea_data ldata = { NULL };
3176 struct lu_buf linkea_buf;
3178 dto = lo->ldo_stripe[i];
3180 dt_write_lock(env, dto, MOR_TGT_CHILD);
3181 rc = lod_sub_create(env, dto, attr, NULL, dof, th);
3183 dt_write_unlock(env, dto);
3187 rc = lod_sub_ref_add(env, dto, th);
3188 dt_write_unlock(env, dto);
3192 rec->rec_fid = lu_object_fid(&dto->do_lu);
3193 rc = lod_sub_insert(env, dto, (const struct dt_rec *)rec,
3194 (const struct dt_key *)dot, th, 0);
3198 rec->rec_fid = lu_object_fid(&dt->do_lu);
3199 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
3200 (const struct dt_key *)dotdot, th, 0);
3204 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
3205 cfs_fail_val != i) {
3206 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
3208 slave_lmm->lmv_master_mdt_index =
3211 slave_lmm->lmv_master_mdt_index =
3214 rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
3215 XATTR_NAME_LMV, fl, th);
3220 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
3222 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3223 PFID(lu_object_fid(&dto->do_lu)), i + 1);
3225 snprintf(stripe_name, sizeof(info->lti_ke