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,
1050 struct thandle *th, lod_obj_stripe_cb_t cb,
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 LASSERT(lod_comp->llc_stripe_count > 0);
1065 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
1066 struct dt_object *dt = lod_comp->llc_stripe[j];
1070 rc = cb(env, lo, dt, th, j, data);
1079 lod_obj_stripe_attr_set_cb(const struct lu_env *env, struct lod_object *lo,
1080 struct dt_object *dt, struct thandle *th,
1081 int stripe_idx, struct lod_obj_stripe_cb_data *data)
1083 if (data->locd_declare)
1084 return lod_sub_declare_attr_set(env, dt, data->locd_attr, th);
1086 return lod_sub_attr_set(env, dt, data->locd_attr, th);
1090 * Implementation of dt_object_operations::do_declare_attr_set.
1092 * If the object is striped, then apply the changes to all the stripes.
1094 * \see dt_object_operations::do_declare_attr_set() in the API description
1097 static int lod_declare_attr_set(const struct lu_env *env,
1098 struct dt_object *dt,
1099 const struct lu_attr *attr,
1102 struct dt_object *next = dt_object_child(dt);
1103 struct lod_object *lo = lod_dt_obj(dt);
1108 * declare setattr on the local object
1110 rc = lod_sub_declare_attr_set(env, next, attr, th);
1114 /* osp_declare_attr_set() ignores all attributes other than
1115 * UID, GID, PROJID, and size, and osp_attr_set() ignores all
1116 * but UID, GID and PROJID. Declaration of size attr setting
1117 * happens through lod_declare_init_size(), and not through
1118 * this function. Therefore we need not load striping unless
1119 * ownership is changing. This should save memory and (we hope)
1120 * speed up rename().
1122 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1123 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID)))
1126 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1129 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID | LA_MODE |
1130 LA_ATIME | LA_MTIME | LA_CTIME |
1135 * load striping information, notice we don't do this when object
1136 * is being initialized as we don't need this information till
1137 * few specific cases like destroy, chown
1139 rc = lod_load_striping(env, lo);
1143 if (!lod_obj_is_striped(dt))
1147 * if object is striped declare changes on the stripes
1149 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1150 LASSERT(lo->ldo_stripe);
1151 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1152 if (lo->ldo_stripe[i] == NULL)
1154 rc = lod_sub_declare_attr_set(env, lo->ldo_stripe[i],
1160 struct lod_obj_stripe_cb_data data;
1162 data.locd_attr = attr;
1163 data.locd_declare = true;
1164 rc = lod_obj_for_each_stripe(env, lo, th,
1165 lod_obj_stripe_attr_set_cb, &data);
1171 if (!dt_object_exists(next) || dt_object_remote(next) ||
1172 !S_ISREG(attr->la_mode))
1175 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1176 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
1180 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1181 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1182 struct lod_thread_info *info = lod_env_info(env);
1183 struct lu_buf *buf = &info->lti_buf;
1185 buf->lb_buf = info->lti_ea_store;
1186 buf->lb_len = info->lti_ea_store_size;
1187 rc = lod_sub_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1188 LU_XATTR_REPLACE, th);
1195 * Implementation of dt_object_operations::do_attr_set.
1197 * If the object is striped, then apply the changes to all or subset of
1198 * the stripes depending on the object type and specific attributes.
1200 * \see dt_object_operations::do_attr_set() in the API description for details.
1202 static int lod_attr_set(const struct lu_env *env,
1203 struct dt_object *dt,
1204 const struct lu_attr *attr,
1207 struct dt_object *next = dt_object_child(dt);
1208 struct lod_object *lo = lod_dt_obj(dt);
1213 * apply changes to the local object
1215 rc = lod_sub_attr_set(env, next, attr, th);
1219 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1220 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID)))
1223 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1226 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE | LA_PROJID |
1227 LA_ATIME | LA_MTIME | LA_CTIME |
1232 if (!lod_obj_is_striped(dt))
1236 * if object is striped, apply changes to all the stripes
1238 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1239 LASSERT(lo->ldo_stripe);
1240 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1241 if (unlikely(lo->ldo_stripe[i] == NULL))
1244 if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1247 rc = lod_sub_attr_set(env, lo->ldo_stripe[i], attr, th);
1252 struct lod_obj_stripe_cb_data data;
1254 data.locd_attr = attr;
1255 data.locd_declare = false;
1256 rc = lod_obj_for_each_stripe(env, lo, th,
1257 lod_obj_stripe_attr_set_cb, &data);
1263 if (!dt_object_exists(next) || dt_object_remote(next) ||
1264 !S_ISREG(attr->la_mode))
1267 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1268 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
1272 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1273 struct lod_thread_info *info = lod_env_info(env);
1274 struct lu_buf *buf = &info->lti_buf;
1275 struct ost_id *oi = &info->lti_ostid;
1276 struct lu_fid *fid = &info->lti_fid;
1277 struct lov_mds_md_v1 *lmm;
1278 struct lov_ost_data_v1 *objs;
1281 rc = lod_get_lov_ea(env, lo);
1285 buf->lb_buf = info->lti_ea_store;
1286 buf->lb_len = info->lti_ea_store_size;
1287 lmm = info->lti_ea_store;
1288 magic = le32_to_cpu(lmm->lmm_magic);
1289 if (magic == LOV_MAGIC_COMP_V1) {
1290 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1291 struct lov_comp_md_entry_v1 *lcme =
1292 &lcm->lcm_entries[0];
1294 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1295 magic = le32_to_cpu(lmm->lmm_magic);
1298 if (magic == LOV_MAGIC_V1)
1299 objs = &(lmm->lmm_objects[0]);
1301 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1302 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1303 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1305 fid_to_ostid(fid, oi);
1306 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1308 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1309 LU_XATTR_REPLACE, th);
1310 } else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1311 struct lod_thread_info *info = lod_env_info(env);
1312 struct lu_buf *buf = &info->lti_buf;
1313 struct lov_comp_md_v1 *lcm;
1314 struct lov_comp_md_entry_v1 *lcme;
1316 rc = lod_get_lov_ea(env, lo);
1320 buf->lb_buf = info->lti_ea_store;
1321 buf->lb_len = info->lti_ea_store_size;
1323 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
1326 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1327 lcme = &lcm->lcm_entries[0];
1328 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1329 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1331 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1332 LU_XATTR_REPLACE, th);
1339 * Implementation of dt_object_operations::do_xattr_get.
1341 * If LOV EA is requested from the root object and it's not
1342 * found, then return default striping for the filesystem.
1344 * \see dt_object_operations::do_xattr_get() in the API description for details.
1346 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1347 struct lu_buf *buf, const char *name)
1349 struct lod_thread_info *info = lod_env_info(env);
1350 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1355 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1356 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1357 struct lmv_mds_md_v1 *lmv1;
1360 if (rc > (typeof(rc))sizeof(*lmv1))
1363 if (rc < (typeof(rc))sizeof(*lmv1))
1364 RETURN(rc = rc > 0 ? -EINVAL : rc);
1366 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1367 CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1369 info->lti_buf.lb_buf = info->lti_key;
1370 info->lti_buf.lb_len = sizeof(*lmv1);
1371 rc = dt_xattr_get(env, dt_object_child(dt),
1372 &info->lti_buf, name);
1373 if (unlikely(rc != sizeof(*lmv1)))
1374 RETURN(rc = rc > 0 ? -EINVAL : rc);
1376 lmv1 = info->lti_buf.lb_buf;
1377 /* The on-disk LMV EA only contains header, but the
1378 * returned LMV EA size should contain the space for
1379 * the FIDs of all shards of the striped directory. */
1380 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1381 rc = lmv_mds_md_size(
1382 le32_to_cpu(lmv1->lmv_stripe_count),
1385 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1389 RETURN(rc = rc1 != 0 ? rc1 : rc);
1392 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1396 * XXX: Only used by lfsck
1398 * lod returns default striping on the real root of the device
1399 * this is like the root stores default striping for the whole
1400 * filesystem. historically we've been using a different approach
1401 * and store it in the config.
1403 dt_root_get(env, dev->lod_child, &info->lti_fid);
1404 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1406 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1407 struct lov_user_md *lum = buf->lb_buf;
1408 struct lov_desc *desc = &dev->lod_desc;
1410 if (buf->lb_buf == NULL) {
1412 } else if (buf->lb_len >= sizeof(*lum)) {
1413 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1414 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1415 lmm_oi_set_id(&lum->lmm_oi, 0);
1416 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1417 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1418 lum->lmm_stripe_size = cpu_to_le32(
1419 desc->ld_default_stripe_size);
1420 lum->lmm_stripe_count = cpu_to_le16(
1421 desc->ld_default_stripe_count);
1422 lum->lmm_stripe_offset = cpu_to_le16(
1423 desc->ld_default_stripe_offset);
1436 * Checks that the magic of the stripe is sane.
1438 * \param[in] lod lod device
1439 * \param[in] lum a buffer storing LMV EA to verify
1441 * \retval 0 if the EA is sane
1442 * \retval negative otherwise
1444 static int lod_verify_md_striping(struct lod_device *lod,
1445 const struct lmv_user_md_v1 *lum)
1447 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1448 CERROR("%s: invalid lmv_user_md: magic = %x, "
1449 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1450 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1451 (int)le32_to_cpu(lum->lum_stripe_offset),
1452 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1460 * Initialize LMV EA for a slave.
1462 * Initialize slave's LMV EA from the master's LMV EA.
1464 * \param[in] master_lmv a buffer containing master's EA
1465 * \param[out] slave_lmv a buffer where slave's EA will be stored
1468 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1469 const struct lmv_mds_md_v1 *master_lmv)
1471 *slave_lmv = *master_lmv;
1472 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1478 * Generate LMV EA from the object passed as \a dt. The object must have
1479 * the stripes created and initialized.
1481 * \param[in] env execution environment
1482 * \param[in] dt object
1483 * \param[out] lmv_buf buffer storing generated LMV EA
1485 * \retval 0 on success
1486 * \retval negative if failed
1488 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1489 struct lu_buf *lmv_buf)
1491 struct lod_thread_info *info = lod_env_info(env);
1492 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1493 struct lod_object *lo = lod_dt_obj(dt);
1494 struct lmv_mds_md_v1 *lmm1;
1496 int type = LU_SEQ_RANGE_ANY;
1501 LASSERT(lo->ldo_dir_striped != 0);
1502 LASSERT(lo->ldo_dir_stripe_count > 0);
1503 stripe_count = lo->ldo_dir_stripe_count;
1504 /* Only store the LMV EA heahder on the disk. */
1505 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1506 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1510 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1513 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1514 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1515 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1516 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1517 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1522 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1523 lmv_buf->lb_buf = info->lti_ea_store;
1524 lmv_buf->lb_len = sizeof(*lmm1);
1530 * Create in-core represenation for a striped directory.
1532 * Parse the buffer containing LMV EA and instantiate LU objects
1533 * representing the stripe objects. The pointers to the objects are
1534 * stored in ldo_stripe field of \a lo. This function is used when
1535 * we need to access an already created object (i.e. load from a disk).
1537 * \param[in] env execution environment
1538 * \param[in] lo lod object
1539 * \param[in] buf buffer containing LMV EA
1541 * \retval 0 on success
1542 * \retval negative if failed
1544 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1545 const struct lu_buf *buf)
1547 struct lod_thread_info *info = lod_env_info(env);
1548 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1549 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1550 struct dt_object **stripe;
1551 union lmv_mds_md *lmm = buf->lb_buf;
1552 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1553 struct lu_fid *fid = &info->lti_fid;
1558 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1561 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1562 lo->ldo_dir_slave_stripe = 1;
1566 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1569 if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1572 LASSERT(lo->ldo_stripe == NULL);
1573 OBD_ALLOC(stripe, sizeof(stripe[0]) *
1574 (le32_to_cpu(lmv1->lmv_stripe_count)));
1578 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1579 struct dt_device *tgt_dt;
1580 struct dt_object *dto;
1581 int type = LU_SEQ_RANGE_ANY;
1584 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1585 if (!fid_is_sane(fid))
1586 GOTO(out, rc = -ESTALE);
1588 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1592 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1593 tgt_dt = lod->lod_child;
1595 struct lod_tgt_desc *tgt;
1597 tgt = LTD_TGT(ltd, idx);
1599 GOTO(out, rc = -ESTALE);
1600 tgt_dt = tgt->ltd_tgt;
1603 dto = dt_locate_at(env, tgt_dt, fid,
1604 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1607 GOTO(out, rc = PTR_ERR(dto));
1612 lo->ldo_stripe = stripe;
1613 lo->ldo_dir_stripe_count = le32_to_cpu(lmv1->lmv_stripe_count);
1614 lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1616 lod_object_free_striping(env, lo);
1622 * Declare create a striped directory.
1624 * Declare creating a striped directory with a given stripe pattern on the
1625 * specified MDTs. A striped directory is represented as a regular directory
1626 * - an index listing all the stripes. The stripes point back to the master
1627 * object with ".." and LinkEA. The master object gets LMV EA which
1628 * identifies it as a striped directory. The function allocates FIDs
1631 * \param[in] env execution environment
1632 * \param[in] dt object
1633 * \param[in] attr attributes to initialize the objects with
1634 * \param[in] dof type of objects to be created
1635 * \param[in] th transaction handle
1637 * \retval 0 on success
1638 * \retval negative if failed
1640 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1641 struct dt_object *dt,
1642 struct lu_attr *attr,
1643 struct dt_object_format *dof,
1646 struct lod_thread_info *info = lod_env_info(env);
1647 struct lu_buf lmv_buf;
1648 struct lu_buf slave_lmv_buf;
1649 struct lmv_mds_md_v1 *lmm;
1650 struct lmv_mds_md_v1 *slave_lmm = NULL;
1651 struct dt_insert_rec *rec = &info->lti_dt_rec;
1652 struct lod_object *lo = lod_dt_obj(dt);
1657 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1660 lmm = lmv_buf.lb_buf;
1662 OBD_ALLOC_PTR(slave_lmm);
1663 if (slave_lmm == NULL)
1664 GOTO(out, rc = -ENOMEM);
1666 lod_prep_slave_lmv_md(slave_lmm, lmm);
1667 slave_lmv_buf.lb_buf = slave_lmm;
1668 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1670 if (!dt_try_as_dir(env, dt_object_child(dt)))
1671 GOTO(out, rc = -EINVAL);
1673 rec->rec_type = S_IFDIR;
1674 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1675 struct dt_object *dto = lo->ldo_stripe[i];
1676 char *stripe_name = info->lti_key;
1677 struct lu_name *sname;
1678 struct linkea_data ldata = { NULL };
1679 struct lu_buf linkea_buf;
1681 rc = lod_sub_declare_create(env, dto, attr, NULL, dof, th);
1685 if (!dt_try_as_dir(env, dto))
1686 GOTO(out, rc = -EINVAL);
1688 rc = lod_sub_declare_ref_add(env, dto, th);
1692 rec->rec_fid = lu_object_fid(&dto->do_lu);
1693 rc = lod_sub_declare_insert(env, dto,
1694 (const struct dt_rec *)rec,
1695 (const struct dt_key *)dot, th);
1699 /* master stripe FID will be put to .. */
1700 rec->rec_fid = lu_object_fid(&dt->do_lu);
1701 rc = lod_sub_declare_insert(env, dto,
1702 (const struct dt_rec *)rec,
1703 (const struct dt_key *)dotdot, th);
1707 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1708 cfs_fail_val != i) {
1709 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1711 slave_lmm->lmv_master_mdt_index =
1714 slave_lmm->lmv_master_mdt_index =
1716 rc = lod_sub_declare_xattr_set(env, dto, &slave_lmv_buf,
1717 XATTR_NAME_LMV, 0, th);
1722 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1724 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1725 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1727 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1728 PFID(lu_object_fid(&dto->do_lu)), i);
1730 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1731 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1732 sname, lu_object_fid(&dt->do_lu));
1736 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1737 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1738 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
1739 XATTR_NAME_LINK, 0, th);
1743 rec->rec_fid = lu_object_fid(&dto->do_lu);
1744 rc = lod_sub_declare_insert(env, dt_object_child(dt),
1745 (const struct dt_rec *)rec,
1746 (const struct dt_key *)stripe_name,
1751 rc = lod_sub_declare_ref_add(env, dt_object_child(dt), th);
1756 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt),
1757 &lmv_buf, XATTR_NAME_LMV, 0, th);
1761 if (slave_lmm != NULL)
1762 OBD_FREE_PTR(slave_lmm);
1767 static int lod_prep_md_striped_create(const struct lu_env *env,
1768 struct dt_object *dt,
1769 struct lu_attr *attr,
1770 const struct lmv_user_md_v1 *lum,
1771 struct dt_object_format *dof,
1774 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1775 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1776 struct lod_object *lo = lod_dt_obj(dt);
1777 struct dt_object **stripe;
1786 /* The lum has been verifed in lod_verify_md_striping */
1787 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
1788 LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1790 stripe_count = le32_to_cpu(lum->lum_stripe_count);
1792 OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1793 if (idx_array == NULL)
1796 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1798 GOTO(out_free, rc = -ENOMEM);
1800 /* Start index must be the master MDT */
1801 master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1802 idx_array[0] = master_index;
1803 for (i = 0; i < stripe_count; i++) {
1804 struct lod_tgt_desc *tgt = NULL;
1805 struct dt_object *dto;
1806 struct lu_fid fid = { 0 };
1808 struct lu_object_conf conf = { 0 };
1809 struct dt_device *tgt_dt = NULL;
1811 /* Try to find next avaible target */
1813 for (j = 0; j < lod->lod_remote_mdt_count;
1814 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1815 bool already_allocated = false;
1818 CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
1819 idx, lod->lod_remote_mdt_count + 1, i);
1821 if (likely(!OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
1822 /* check whether the idx already exists
1823 * in current allocated array */
1824 for (k = 0; k < i; k++) {
1825 if (idx_array[k] == idx) {
1826 already_allocated = true;
1831 if (already_allocated)
1835 /* Sigh, this index is not in the bitmap, let's check
1836 * next available target */
1837 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) &&
1838 idx != master_index)
1841 if (idx == master_index) {
1842 /* Allocate the FID locally */
1843 rc = obd_fid_alloc(env, lod->lod_child_exp,
1847 tgt_dt = lod->lod_child;
1851 /* check the status of the OSP */
1852 tgt = LTD_TGT(ltd, idx);
1856 tgt_dt = tgt->ltd_tgt;
1857 rc = dt_statfs(env, tgt_dt, NULL);
1859 /* this OSP doesn't feel well */
1864 rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1873 /* Can not allocate more stripes */
1874 if (j == lod->lod_remote_mdt_count) {
1875 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1876 lod2obd(lod)->obd_name, stripe_count, i);
1880 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
1881 idx, i, PFID(&fid));
1883 /* Set the start index for next stripe allocation */
1884 if (i < stripe_count - 1)
1885 idx_array[i + 1] = (idx + 1) %
1886 (lod->lod_remote_mdt_count + 1);
1887 /* tgt_dt and fid must be ready after search avaible OSP
1888 * in the above loop */
1889 LASSERT(tgt_dt != NULL);
1890 LASSERT(fid_is_sane(&fid));
1891 conf.loc_flags = LOC_F_NEW;
1892 dto = dt_locate_at(env, tgt_dt, &fid,
1893 dt->do_lu.lo_dev->ld_site->ls_top_dev,
1896 GOTO(out_put, rc = PTR_ERR(dto));
1900 lo->ldo_dir_stripe_loaded = 1;
1901 lo->ldo_dir_striped = 1;
1902 lo->ldo_stripe = stripe;
1903 lo->ldo_dir_stripe_count = i;
1904 lo->ldo_dir_stripes_allocated = stripe_count;
1906 if (lo->ldo_dir_stripe_count == 0)
1907 GOTO(out_put, rc = -ENOSPC);
1909 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
1915 for (i = 0; i < stripe_count; i++)
1916 if (stripe[i] != NULL)
1917 dt_object_put(env, stripe[i]);
1918 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
1919 lo->ldo_dir_stripe_count = 0;
1920 lo->ldo_dir_stripes_allocated = 0;
1921 lo->ldo_stripe = NULL;
1925 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
1931 * Declare create striped md object.
1933 * The function declares intention to create a striped directory. This is a
1934 * wrapper for lod_prep_md_striped_create(). The only additional functionality
1935 * is to verify pattern \a lum_buf is good. Check that function for the details.
1937 * \param[in] env execution environment
1938 * \param[in] dt object
1939 * \param[in] attr attributes to initialize the objects with
1940 * \param[in] lum_buf a pattern specifying the number of stripes and
1942 * \param[in] dof type of objects to be created
1943 * \param[in] th transaction handle
1945 * \retval 0 on success
1946 * \retval negative if failed
1949 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
1950 struct dt_object *dt,
1951 struct lu_attr *attr,
1952 const struct lu_buf *lum_buf,
1953 struct dt_object_format *dof,
1956 struct lod_object *lo = lod_dt_obj(dt);
1957 struct lmv_user_md_v1 *lum = lum_buf->lb_buf;
1961 LASSERT(lum != NULL);
1963 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
1964 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
1965 (int)le32_to_cpu(lum->lum_stripe_offset));
1967 if (le32_to_cpu(lum->lum_stripe_count) == 0)
1970 /* prepare dir striped objects */
1971 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
1973 /* failed to create striping, let's reset
1974 * config so that others don't get confused */
1975 lod_object_free_striping(env, lo);
1983 * Implementation of dt_object_operations::do_declare_xattr_set.
1985 * Used with regular (non-striped) objects. Basically it
1986 * initializes the striping information and applies the
1987 * change to all the stripes.
1989 * \see dt_object_operations::do_declare_xattr_set() in the API description
1992 static int lod_dir_declare_xattr_set(const struct lu_env *env,
1993 struct dt_object *dt,
1994 const struct lu_buf *buf,
1995 const char *name, int fl,
1998 struct dt_object *next = dt_object_child(dt);
1999 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2000 struct lod_object *lo = lod_dt_obj(dt);
2005 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2006 struct lmv_user_md_v1 *lum;
2008 LASSERT(buf != NULL && buf->lb_buf != NULL);
2010 rc = lod_verify_md_striping(d, lum);
2013 } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2014 rc = lod_verify_striping(d, buf, false, 0);
2019 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2023 /* Note: Do not set LinkEA on sub-stripes, otherwise
2024 * it will confuse the fid2path process(see mdt_path_current()).
2025 * The linkEA between master and sub-stripes is set in
2026 * lod_xattr_set_lmv(). */
2027 if (strcmp(name, XATTR_NAME_LINK) == 0)
2030 /* set xattr to each stripes, if needed */
2031 rc = lod_load_striping(env, lo);
2035 if (lo->ldo_dir_stripe_count == 0)
2038 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2039 LASSERT(lo->ldo_stripe[i]);
2041 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2051 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2052 struct lod_object *lo,
2053 struct dt_object *dt, struct thandle *th,
2055 struct lod_obj_stripe_cb_data *data)
2057 struct lod_thread_info *info = lod_env_info(env);
2058 struct filter_fid *ff = &info->lti_ff;
2059 struct lu_buf *buf = &info->lti_buf;
2063 buf->lb_len = sizeof(*ff);
2064 rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2071 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2072 ff->ff_parent.f_ver = stripe_idx;
2073 fid_cpu_to_le(&ff->ff_parent, &ff->ff_parent);
2074 if (data->locd_declare)
2075 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2076 LU_XATTR_REPLACE, th);
2078 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2079 LU_XATTR_REPLACE, th);
2085 * Reset parent FID on OST object
2087 * Replace parent FID with @dt object FID, which is only called during migration
2088 * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2089 * the FID is changed.
2091 * \param[in] env execution environment
2092 * \param[in] dt dt_object whose stripes's parent FID will be reset
2093 * \parem[in] th thandle
2094 * \param[in] declare if it is declare
2096 * \retval 0 if reset succeeds
2097 * \retval negative errno if reset fails
2099 static int lod_replace_parent_fid(const struct lu_env *env,
2100 struct dt_object *dt,
2101 struct thandle *th, bool declare)
2103 struct lod_object *lo = lod_dt_obj(dt);
2104 struct lod_thread_info *info = lod_env_info(env);
2105 struct lu_buf *buf = &info->lti_buf;
2106 struct filter_fid *ff;
2107 struct lod_obj_stripe_cb_data data;
2111 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2113 /* set xattr to each stripes, if needed */
2114 rc = lod_load_striping(env, lo);
2118 if (!lod_obj_is_striped(dt))
2121 if (info->lti_ea_store_size < sizeof(*ff)) {
2122 rc = lod_ea_store_resize(info, sizeof(*ff));
2127 buf->lb_buf = info->lti_ea_store;
2128 buf->lb_len = info->lti_ea_store_size;
2130 data.locd_declare = declare;
2131 rc = lod_obj_for_each_stripe(env, lo, th,
2132 lod_obj_stripe_replace_parent_fid_cb,
2138 inline __u16 lod_comp_entry_stripe_count(struct lod_object *lo,
2139 struct lod_layout_component *entry,
2142 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2146 else if (lod_comp_inited(entry))
2147 return entry->llc_stripe_count;
2148 else if ((__u16)-1 == entry->llc_stripe_count)
2149 return lod->lod_desc.ld_tgt_count;
2151 return lod_get_stripe_count(lod, lo, entry->llc_stripe_count);
2154 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2156 int magic, size = 0, i;
2157 struct lod_layout_component *comp_entries;
2162 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2163 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2165 lo->ldo_def_striping->lds_def_striping_is_composite;
2167 comp_cnt = lo->ldo_comp_cnt;
2168 comp_entries = lo->ldo_comp_entries;
2169 is_composite = lo->ldo_is_composite;
2173 LASSERT(comp_cnt != 0 && comp_entries != NULL);
2175 size = sizeof(struct lov_comp_md_v1) +
2176 sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2177 LASSERT(size % sizeof(__u64) == 0);
2180 for (i = 0; i < comp_cnt; i++) {
2183 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2184 stripe_count = lod_comp_entry_stripe_count(lo, &comp_entries[i],
2186 if (!is_dir && is_composite)
2187 lod_comp_shrink_stripe_count(&comp_entries[i],
2190 size += lov_user_md_size(stripe_count, magic);
2191 LASSERT(size % sizeof(__u64) == 0);
2197 * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2198 * the xattr value is binary lov_comp_md_v1 which contains component(s)
2201 * \param[in] env execution environment
2202 * \param[in] dt dt_object to add components on
2203 * \param[in] buf buffer contains components to be added
2204 * \parem[in] th thandle
2206 * \retval 0 on success
2207 * \retval negative errno on failure
2209 static int lod_declare_layout_add(const struct lu_env *env,
2210 struct dt_object *dt,
2211 const struct lu_buf *buf,
2214 struct lod_thread_info *info = lod_env_info(env);
2215 struct lod_layout_component *comp_array, *lod_comp;
2216 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2217 struct dt_object *next = dt_object_child(dt);
2218 struct lov_desc *desc = &d->lod_desc;
2219 struct lod_object *lo = lod_dt_obj(dt);
2220 struct lov_user_md_v3 *v3;
2221 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2224 int i, rc, array_cnt;
2227 LASSERT(lo->ldo_is_composite);
2229 prev_end = lo->ldo_comp_entries[lo->ldo_comp_cnt - 1].llc_extent.e_end;
2230 rc = lod_verify_striping(d, buf, false, prev_end);
2234 magic = comp_v1->lcm_magic;
2235 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2236 lustre_swab_lov_comp_md_v1(comp_v1);
2237 magic = comp_v1->lcm_magic;
2240 if (magic != LOV_USER_MAGIC_COMP_V1)
2243 array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2244 OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2245 if (comp_array == NULL)
2248 memcpy(comp_array, lo->ldo_comp_entries,
2249 sizeof(*comp_array) * lo->ldo_comp_cnt);
2251 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2252 struct lov_user_md_v1 *v1;
2253 struct lu_extent *ext;
2255 v1 = (struct lov_user_md *)((char *)comp_v1 +
2256 comp_v1->lcm_entries[i].lcme_offset);
2257 ext = &comp_v1->lcm_entries[i].lcme_extent;
2259 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2260 lod_comp->llc_extent.e_start = ext->e_start;
2261 lod_comp->llc_extent.e_end = ext->e_end;
2262 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2264 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2265 if (!lod_comp->llc_stripe_count ||
2266 lod_comp->llc_stripe_count == (__u16)-1)
2267 lod_comp->llc_stripe_count =
2268 desc->ld_default_stripe_count;
2269 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2270 if (!lod_comp->llc_stripe_size)
2271 lod_comp->llc_stripe_size =
2272 desc->ld_default_stripe_size;
2274 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2275 v3 = (struct lov_user_md_v3 *) v1;
2276 if (v3->lmm_pool_name[0] != '\0') {
2277 rc = lod_set_pool(&lod_comp->llc_pool,
2285 OBD_FREE(lo->ldo_comp_entries, sizeof(*lod_comp) * lo->ldo_comp_cnt);
2286 lo->ldo_comp_entries = comp_array;
2287 lo->ldo_comp_cnt = array_cnt;
2288 /* No need to increase layout generation here, it will be increased
2289 * later when generating component ID for the new components */
2291 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2292 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2293 XATTR_NAME_LOV, 0, th);
2300 for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2301 lod_comp = &comp_array[i];
2302 if (lod_comp->llc_pool != NULL) {
2303 OBD_FREE(lod_comp->llc_pool,
2304 strlen(lod_comp->llc_pool) + 1);
2305 lod_comp->llc_pool = NULL;
2308 OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2313 * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2314 * the '$field' can only be 'flags' now. The xattr value is binary
2315 * lov_comp_md_v1 which contains the component ID(s) and the value of
2316 * the field to be modified.
2318 * \param[in] env execution environment
2319 * \param[in] dt dt_object to be modified
2320 * \param[in] op operation string, like "set.flags"
2321 * \param[in] buf buffer contains components to be set
2322 * \parem[in] th thandle
2324 * \retval 0 on success
2325 * \retval negative errno on failure
2327 static int lod_declare_layout_set(const struct lu_env *env,
2328 struct dt_object *dt,
2329 char *op, const struct lu_buf *buf,
2332 struct lod_layout_component *lod_comp;
2333 struct lod_thread_info *info = lod_env_info(env);
2334 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2335 struct lod_object *lo = lod_dt_obj(dt);
2336 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2339 bool changed = false;
2342 if (strcmp(op, "set.flags") != 0) {
2343 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2344 lod2obd(d)->obd_name, op);
2348 magic = comp_v1->lcm_magic;
2349 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2350 lustre_swab_lov_comp_md_v1(comp_v1);
2351 magic = comp_v1->lcm_magic;
2354 if (magic != LOV_USER_MAGIC_COMP_V1)
2357 if (comp_v1->lcm_entry_count == 0) {
2358 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2359 lod2obd(d)->obd_name);
2363 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2364 id = comp_v1->lcm_entries[i].lcme_id;
2366 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2367 lod_comp = &lo->ldo_comp_entries[j];
2368 if (id == lod_comp->llc_id || id == LCME_ID_ALL) {
2369 lod_comp->llc_flags =
2370 comp_v1->lcm_entries[i].lcme_flags;
2377 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2378 lod2obd(d)->obd_name);
2382 lod_obj_inc_layout_gen(lo);
2384 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2385 rc = lod_sub_declare_xattr_set(env, dt, &info->lti_buf,
2386 XATTR_NAME_LOV, 0, th);
2391 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2392 * and the xattr value is a unique component ID or a special lcme_id.
2394 * \param[in] env execution environment
2395 * \param[in] dt dt_object to be operated on
2396 * \param[in] buf buffer contains component ID or lcme_id
2397 * \parem[in] th thandle
2399 * \retval 0 on success
2400 * \retval negative errno on failure
2402 static int lod_declare_layout_del(const struct lu_env *env,
2403 struct dt_object *dt,
2404 const struct lu_buf *buf,
2407 struct lod_thread_info *info = lod_env_info(env);
2408 struct dt_object *next = dt_object_child(dt);
2409 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2410 struct lod_object *lo = lod_dt_obj(dt);
2411 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2412 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2413 __u32 magic, id, flags, neg_flags = 0;
2417 LASSERT(lo->ldo_is_composite);
2419 rc = lod_verify_striping(d, buf, false, 0);
2423 magic = comp_v1->lcm_magic;
2424 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2425 lustre_swab_lov_comp_md_v1(comp_v1);
2426 magic = comp_v1->lcm_magic;
2429 if (magic != LOV_USER_MAGIC_COMP_V1)
2432 id = comp_v1->lcm_entries[0].lcme_id;
2433 flags = comp_v1->lcm_entries[0].lcme_flags;
2435 if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
2436 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
2437 lod2obd(d)->obd_name, id, flags);
2441 if (id != LCME_ID_INVAL && flags != 0) {
2442 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
2443 lod2obd(d)->obd_name);
2447 if (flags & LCME_FL_NEG) {
2448 neg_flags = flags & ~LCME_FL_NEG;
2452 left = lo->ldo_comp_cnt;
2456 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
2457 struct lod_layout_component *lod_comp;
2459 lod_comp = &lo->ldo_comp_entries[i];
2461 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
2463 else if (flags && !(flags & lod_comp->llc_flags))
2465 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
2468 if (left != (i + 1)) {
2469 CDEBUG(D_LAYOUT, "%s: this deletion will create "
2470 "a hole.\n", lod2obd(d)->obd_name);
2475 /* Mark the component as deleted */
2476 lod_comp->llc_id = LCME_ID_INVAL;
2478 /* Not instantiated component */
2479 if (lod_comp->llc_stripe == NULL)
2482 LASSERT(lod_comp->llc_stripe_count > 0);
2483 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
2484 struct dt_object *obj = lod_comp->llc_stripe[j];
2488 rc = lod_sub_declare_destroy(env, obj, th);
2494 LASSERTF(left >= 0, "left = %d\n", left);
2495 if (left == lo->ldo_comp_cnt) {
2496 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
2497 lod2obd(d)->obd_name, id);
2501 memset(attr, 0, sizeof(*attr));
2502 attr->la_valid = LA_SIZE;
2503 rc = lod_sub_declare_attr_set(env, next, attr, th);
2508 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2509 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2510 XATTR_NAME_LOV, 0, th);
2512 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
2519 * Declare layout add/set/del operations issued by special xattr names:
2521 * XATTR_LUSTRE_LOV.add add component(s) to existing file
2522 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
2523 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
2525 * \param[in] env execution environment
2526 * \param[in] dt object
2527 * \param[in] name name of xattr
2528 * \param[in] buf lu_buf contains xattr value
2529 * \param[in] th transaction handle
2531 * \retval 0 on success
2532 * \retval negative if failed
2534 static int lod_declare_modify_layout(const struct lu_env *env,
2535 struct dt_object *dt,
2537 const struct lu_buf *buf,
2540 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2541 struct lod_object *lo = lod_dt_obj(dt);
2542 struct dt_object *next = dt_object_child(&lo->ldo_obj);
2544 int rc, len = strlen(XATTR_LUSTRE_LOV);
2547 LASSERT(dt_object_exists(dt));
2549 if (strlen(name) <= len || name[len] != '.') {
2550 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
2551 lod2obd(d)->obd_name, name);
2556 dt_write_lock(env, next, 0);
2557 rc = lod_load_striping_locked(env, lo);
2561 /* the layout to be modified must be a composite layout */
2562 if (!lo->ldo_is_composite) {
2563 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
2564 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
2565 GOTO(unlock, rc = -EINVAL);
2568 op = (char *)name + len;
2569 if (strcmp(op, "add") == 0) {
2570 rc = lod_declare_layout_add(env, dt, buf, th);
2571 } else if (strcmp(op, "del") == 0) {
2572 rc = lod_declare_layout_del(env, dt, buf, th);
2573 } else if (strncmp(op, "set", strlen("set")) == 0) {
2574 rc = lod_declare_layout_set(env, dt, op, buf, th);
2576 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
2577 lod2obd(d)->obd_name, name);
2578 GOTO(unlock, rc = -ENOTSUPP);
2582 lod_object_free_striping(env, lo);
2583 dt_write_unlock(env, next);
2589 * Implementation of dt_object_operations::do_declare_xattr_set.
2591 * \see dt_object_operations::do_declare_xattr_set() in the API description
2594 * the extension to the API:
2595 * - declaring LOVEA requests striping creation
2596 * - LU_XATTR_REPLACE means layout swap
2598 static int lod_declare_xattr_set(const struct lu_env *env,
2599 struct dt_object *dt,
2600 const struct lu_buf *buf,
2601 const char *name, int fl,
2604 struct dt_object *next = dt_object_child(dt);
2605 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2610 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2611 if ((S_ISREG(mode) || mode == 0) && !(fl & LU_XATTR_REPLACE) &&
2612 (strcmp(name, XATTR_NAME_LOV) == 0 ||
2613 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
2615 * this is a request to create object's striping.
2617 * allow to declare predefined striping on a new (!mode) object
2618 * which is supposed to be replay of regular file creation
2619 * (when LOV setting is declared)
2621 * LU_XATTR_REPLACE is set to indicate a layout swap
2623 if (dt_object_exists(dt)) {
2624 rc = dt_attr_get(env, next, attr);
2628 memset(attr, 0, sizeof(*attr));
2629 attr->la_valid = LA_TYPE | LA_MODE;
2630 attr->la_mode = S_IFREG;
2632 rc = lod_declare_striped_create(env, dt, attr, buf, th);
2633 } else if (S_ISREG(mode) &&
2634 strlen(name) > strlen(XATTR_LUSTRE_LOV) + 1 &&
2635 strncmp(name, XATTR_LUSTRE_LOV,
2636 strlen(XATTR_LUSTRE_LOV)) == 0) {
2638 * this is a request to modify object's striping.
2639 * add/set/del component(s).
2641 if (!dt_object_exists(dt))
2644 rc = lod_declare_modify_layout(env, dt, name, buf, th);
2645 } else if (S_ISDIR(mode)) {
2646 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2647 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
2648 rc = lod_replace_parent_fid(env, dt, th, true);
2650 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2657 * Apply xattr changes to the object.
2659 * Applies xattr changes to the object and the stripes if the latter exist.
2661 * \param[in] env execution environment
2662 * \param[in] dt object
2663 * \param[in] buf buffer pointing to the new value of xattr
2664 * \param[in] name name of xattr
2665 * \param[in] fl flags
2666 * \param[in] th transaction handle
2668 * \retval 0 on success
2669 * \retval negative if failed
2671 static int lod_xattr_set_internal(const struct lu_env *env,
2672 struct dt_object *dt,
2673 const struct lu_buf *buf,
2674 const char *name, int fl,
2677 struct dt_object *next = dt_object_child(dt);
2678 struct lod_object *lo = lod_dt_obj(dt);
2683 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
2684 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2687 /* Note: Do not set LinkEA on sub-stripes, otherwise
2688 * it will confuse the fid2path process(see mdt_path_current()).
2689 * The linkEA between master and sub-stripes is set in
2690 * lod_xattr_set_lmv(). */
2691 if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2694 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2695 LASSERT(lo->ldo_stripe[i]);
2697 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
2707 * Delete an extended attribute.
2709 * Deletes specified xattr from the object and the stripes if the latter exist.
2711 * \param[in] env execution environment
2712 * \param[in] dt object
2713 * \param[in] name name of xattr
2714 * \param[in] th transaction handle
2716 * \retval 0 on success
2717 * \retval negative if failed
2719 static int lod_xattr_del_internal(const struct lu_env *env,
2720 struct dt_object *dt,
2721 const char *name, struct thandle *th)
2723 struct dt_object *next = dt_object_child(dt);
2724 struct lod_object *lo = lod_dt_obj(dt);
2729 rc = lod_sub_xattr_del(env, next, name, th);
2730 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2733 if (lo->ldo_dir_stripe_count == 0)
2736 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2737 LASSERT(lo->ldo_stripe[i]);
2739 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
2748 * Set default striping on a directory.
2750 * Sets specified striping on a directory object unless it matches the default
2751 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2752 * EA. This striping will be used when regular file is being created in this
2755 * \param[in] env execution environment
2756 * \param[in] dt the striped object
2757 * \param[in] buf buffer with the striping
2758 * \param[in] name name of EA
2759 * \param[in] fl xattr flag (see OSD API description)
2760 * \param[in] th transaction handle
2762 * \retval 0 on success
2763 * \retval negative if failed
2765 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
2766 struct dt_object *dt,
2767 const struct lu_buf *buf,
2768 const char *name, int fl,
2771 struct lov_user_md_v1 *lum;
2772 struct lov_user_md_v3 *v3 = NULL;
2773 const char *pool_name = NULL;
2778 LASSERT(buf != NULL && buf->lb_buf != NULL);
2781 switch (lum->lmm_magic) {
2782 case LOV_USER_MAGIC_V3:
2784 if (v3->lmm_pool_name[0] != '\0')
2785 pool_name = v3->lmm_pool_name;
2787 case LOV_USER_MAGIC_V1:
2788 /* if { size, offset, count } = { 0, -1, 0 } and no pool
2789 * (i.e. all default values specified) then delete default
2790 * striping from dir. */
2792 "set default striping: sz %u # %u offset %d %s %s\n",
2793 (unsigned)lum->lmm_stripe_size,
2794 (unsigned)lum->lmm_stripe_count,
2795 (int)lum->lmm_stripe_offset,
2796 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
2798 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
2799 lum->lmm_stripe_count,
2800 lum->lmm_stripe_offset,
2803 case LOV_USER_MAGIC_COMP_V1:
2807 CERROR("Invalid magic %x\n", lum->lmm_magic);
2812 rc = lod_xattr_del_internal(env, dt, name, th);
2816 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2823 * Set default striping on a directory object.
2825 * Sets specified striping on a directory object unless it matches the default
2826 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2827 * EA. This striping will be used when a new directory is being created in the
2830 * \param[in] env execution environment
2831 * \param[in] dt the striped object
2832 * \param[in] buf buffer with the striping
2833 * \param[in] name name of EA
2834 * \param[in] fl xattr flag (see OSD API description)
2835 * \param[in] th transaction handle
2837 * \retval 0 on success
2838 * \retval negative if failed
2840 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
2841 struct dt_object *dt,
2842 const struct lu_buf *buf,
2843 const char *name, int fl,
2846 struct lmv_user_md_v1 *lum;
2850 LASSERT(buf != NULL && buf->lb_buf != NULL);
2853 CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
2854 le32_to_cpu(lum->lum_stripe_count),
2855 (int)le32_to_cpu(lum->lum_stripe_offset));
2857 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
2858 le32_to_cpu(lum->lum_stripe_offset)) &&
2859 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
2860 rc = lod_xattr_del_internal(env, dt, name, th);
2864 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2873 * Turn directory into a striped directory.
2875 * During replay the client sends the striping created before MDT
2876 * failure, then the layer above LOD sends this defined striping
2877 * using ->do_xattr_set(), so LOD uses this method to replay creation
2878 * of the stripes. Notice the original information for the striping
2879 * (#stripes, FIDs, etc) was transferred in declare path.
2881 * \param[in] env execution environment
2882 * \param[in] dt the striped object
2883 * \param[in] buf not used currently
2884 * \param[in] name not used currently
2885 * \param[in] fl xattr flag (see OSD API description)
2886 * \param[in] th transaction handle
2888 * \retval 0 on success
2889 * \retval negative if failed
2891 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
2892 const struct lu_buf *buf, const char *name,
2893 int fl, struct thandle *th)
2895 struct lod_object *lo = lod_dt_obj(dt);
2896 struct lod_thread_info *info = lod_env_info(env);
2897 struct lu_attr *attr = &info->lti_attr;
2898 struct dt_object_format *dof = &info->lti_format;
2899 struct lu_buf lmv_buf;
2900 struct lu_buf slave_lmv_buf;
2901 struct lmv_mds_md_v1 *lmm;
2902 struct lmv_mds_md_v1 *slave_lmm = NULL;
2903 struct dt_insert_rec *rec = &info->lti_dt_rec;
2908 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2911 /* The stripes are supposed to be allocated in declare phase,
2912 * if there are no stripes being allocated, it will skip */
2913 if (lo->ldo_dir_stripe_count == 0)
2916 rc = dt_attr_get(env, dt_object_child(dt), attr);
2920 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
2921 LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
2922 dof->dof_type = DFT_DIR;
2924 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
2927 lmm = lmv_buf.lb_buf;
2929 OBD_ALLOC_PTR(slave_lmm);
2930 if (slave_lmm == NULL)
2933 lod_prep_slave_lmv_md(slave_lmm, lmm);
2934 slave_lmv_buf.lb_buf = slave_lmm;
2935 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
2937 rec->rec_type = S_IFDIR;
2938 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2939 struct dt_object *dto;
2940 char *stripe_name = info->lti_key;
2941 struct lu_name *sname;
2942 struct linkea_data ldata = { NULL };
2943 struct lu_buf linkea_buf;
2945 dto = lo->ldo_stripe[i];
2947 dt_write_lock(env, dto, MOR_TGT_CHILD);
2948 rc = lod_sub_create(env, dto, attr, NULL, dof, th);
2950 dt_write_unlock(env, dto);
2954 rc = lod_sub_ref_add(env, dto, th);
2955 dt_write_unlock(env, dto);
2959 rec->rec_fid = lu_object_fid(&dto->do_lu);
2960 rc = lod_sub_insert(env, dto, (const struct dt_rec *)rec,
2961 (const struct dt_key *)dot, th, 0);
2965 rec->rec_fid = lu_object_fid(&dt->do_lu);
2966 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
2967 (const struct dt_key *)dotdot, th, 0);
2971 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
2972 cfs_fail_val != i) {
2973 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
2975 slave_lmm->lmv_master_mdt_index =
2978 slave_lmm->lmv_master_mdt_index =
2981 rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
2982 XATTR_NAME_LMV, fl, th);
2987 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
2989 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2990 PFID(lu_object_fid(&dto->do_lu)), i + 1);
2992 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2993 PFID(lu_object_fid(&dto->do_lu)), i);
2995 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2996 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
2997 sname, lu_object_fid(&dt->do_lu));
3001 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
3002 linkea_buf.lb_len = ldata.ld_leh->leh_len;
3003 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
3004 XATTR_NAME_LINK, 0, th);
3008 rec->rec_fid = lu_object_fid(&dto->do_lu);
3009 rc = lod_sub_insert(env, dt_object_child(dt),
3010 (const struct dt_rec *)rec,
3011 (const struct dt_key *)stripe_name, th, 0);
3015 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
3020 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
3021 rc = lod_sub_xattr_set(env, dt_object_child(dt),
3022 &lmv_buf, XATTR_NAME_LMV, fl, th);
3024 if (slave_lmm != NULL)
3025 OBD_FREE_PTR(slave_lmm);
3031 * Helper function to declare/execute creation of a striped directory
3033 * Called in declare/create object path, prepare striping for a directory
3034 * and prepare defaults data striping for the objects to be created in
3035 * that directory. Notice the function calls "declaration" or "execution"
3036 * methods depending on \a declare param. This is a consequence of the
3037 * current approach while we don't have natural distributed transactions:
3038 * we basically execute non-local updates in the declare phase. So, the
3039 * arguments for the both phases are the same and this is the reason for
3040 * this function to exist.
3042 * \param[in] env execution environment
3043 * \param[in] dt object
3044 * \param[in] attr attributes the stripes will be created with
3045 * \param[in] dof format of stripes (see OSD API description)
3046 * \param[in] th transaction handle
3047 * \param[in] declare where to call "declare" or "execute" methods
3049 * \retval 0 on success
3050 * \retval negative if failed
3052 static int lod_dir_striping_create_internal(const struct lu_env *env,
3053 struct dt_object *dt,
3054 struct lu_attr *attr,
3055 struct dt_object_format *dof,
3059 struct lod_thread_info *info = lod_env_info(env);
3060 struct lod_object *lo = lod_dt_obj(dt);
3061 const struct lod_default_striping *lds = lo->ldo_def_striping;
3065 LASSERT(ergo(lds != NULL,
3066 lds->lds_def_striping_set ||
3067 lds->lds_dir_def_striping_set));
3069 if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
3070 lo->ldo_dir_stripe_offset)) {
3071 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3072 int stripe_count = lo->ldo_dir_stripe_count;
3074 if (info->lti_ea_store_size < sizeof(*v1)) {
3075 rc = lod_ea_store_resize(info, sizeof(*v1));
3078 v1 = info->lti_ea_store;
3081 memset(v1, 0, sizeof(*v1));
3082 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3083 v1->lum_stripe_count = cpu_to_le32(stripe_count);
3084 v1->lum_stripe_offset =
3085 cpu_to_le32(lo->ldo_dir_stripe_offset);
3087 info->lti_buf.lb_buf = v1;
3088 info->lti_buf.lb_len = sizeof(*v1);
3091 rc = lod_declare_xattr_set_lmv(env, dt, attr,
3092 &info->lti_buf, dof, th);
3094 rc = lod_xattr_set_lmv(env, dt, &info->lti_buf,
3095 XATTR_NAME_LMV, 0, th);
3100 /* Transfer default LMV striping from the parent */
3101 if (lds != NULL && lds->lds_dir_def_striping_set &&
3102 !LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
3103 lds->lds_dir_def_stripe_offset)) {
3104 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3106 if (info->lti_ea_store_size < sizeof(*v1)) {
3107 rc = lod_ea_store_resize(info, sizeof(*v1));
3110 v1 = info->lti_ea_store;
3113 memset(v1, 0, sizeof(*v1));
3114 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3115 v1->lum_stripe_count =
3116 cpu_to_le32(lds->lds_dir_def_stripe_count);
3117 v1->lum_stripe_offset =
3118 cpu_to_le32(lds->lds_dir_def_stripe_offset);
3120 cpu_to_le32(lds->lds_dir_def_hash_type);
3122 info->lti_buf.lb_buf = v1;
3123 info->lti_buf.lb_len = sizeof(*v1);
3125 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3126 XATTR_NAME_DEFAULT_LMV,
3129 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
3131 XATTR_NAME_DEFAULT_LMV, 0,
3137 /* Transfer default LOV striping from the parent */
3138 if (lds != NULL && lds->lds_def_striping_set &&
3139 lds->lds_def_comp_cnt != 0) {
3140 struct lov_mds_md *lmm;
3141 int lmm_size = lod_comp_md_size(lo, true);
3143 if (info->lti_ea_store_size < lmm_size) {
3144 rc = lod_ea_store_resize(info, lmm_size);
3148 lmm = info->lti_ea_store;
3150 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
3154 info->lti_buf.lb_buf = lmm;
3155 info->lti_buf.lb_len = lmm_size;
3158 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3159 XATTR_NAME_LOV, 0, th);
3161 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
3162 XATTR_NAME_LOV, 0, th);
3170 static int lod_declare_dir_striping_create(const struct lu_env *env,
3171 struct dt_object *dt,
3172 struct lu_attr *attr,
3173 struct dt_object_format *dof,
3176 return lod_dir_striping_create_internal(env, dt, attr, dof, th, true);
3179 static int lod_dir_striping_create(const struct lu_env *env,
3180 struct dt_object *dt,
3181 struct lu_attr *attr,
3182 struct dt_object_format *dof,
3185 return lod_dir_striping_create_internal(env, dt, attr, dof, th, false);
3189 * Make LOV EA for striped object.
3191 * Generate striping information and store it in the LOV EA of the given
3192 * object. The caller must ensure nobody else is calling the function
3193 * against the object concurrently. The transaction must be started.
3194 * FLDB service must be running as well; it's used to map FID to the target,
3195 * which is stored in LOV EA.
3197 * \param[in] env execution environment for this thread
3198 * \param[in] lo LOD object
3199 * \param[in] th transaction handle
3201 * \retval 0 if LOV EA is stored successfully
3202 * \retval negative error number on failure
3204 static int lod_generate_and_set_lovea(const struct lu_env *env,
3205 struct lod_object *lo,
3208 struct lod_thread_info *info = lod_env_info(env);
3209 struct dt_object *next = dt_object_child(&lo->ldo_obj);
3210 struct lov_mds_md_v1 *lmm;
3216 if (lo->ldo_comp_cnt == 0) {
3217 lod_object_free_striping(env, lo);
3218 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
3222 lmm_size = lod_comp_md_size(lo, false);
3223 if (info->lti_ea_store_size < lmm_size) {
3224 rc = lod_ea_store_resize(info, lmm_size);
3228 lmm = info->lti_ea_store;
3230 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
3234 info->lti_buf.lb_buf = lmm;
3235 info->lti_buf.lb_len = lmm_size;
3236 rc = lod_sub_xattr_set(env, next, &info->lti_buf,
3237 XATTR_NAME_LOV, 0, th);
3242 * Delete layout component(s)
3244 * \param[in] env execution environment for this thread
3245 * \param[in] dt object
3246 * \param[in] th transaction handle
3248 * \retval 0 on success
3249 * \retval negative error number on failure
3251 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
3254 struct lod_layout_component *lod_comp;
3255 struct lod_object *lo = lod_dt_obj(dt);
3256 struct dt_object *next = dt_object_child(dt);
3257 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3260 LASSERT(lo->ldo_is_composite);
3261 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
3263 left = lo->ldo_comp_cnt;
3264 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3265 lod_comp = &lo->ldo_comp_entries[i];
3267 if (lod_comp->llc_id != LCME_ID_INVAL)
3271 /* Not instantiated component */
3272 if (lod_comp->llc_stripe == NULL)
3275 LASSERT(lod_comp->llc_stripe_count > 0);
3276 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
3277 struct dt_object *obj = lod_comp->llc_stripe[j];
3281 rc = lod_sub_destroy(env, obj, th);
3285 lu_object_put(env, &obj->do_lu);
3286 lod_comp->llc_stripe[j] = NULL;
3288 OBD_FREE(lod_comp->llc_stripe, sizeof(struct dt_object *) *
3289 lod_comp->llc_stripes_allocated);
3290 lod_comp->llc_stripe = NULL;
3291 lod_comp->llc_stripes_allocated = 0;
3292 lod_obj_set_pool(lo, i, NULL);
3293 if (lod_comp->llc_ostlist.op_array) {
3294 OBD_FREE(lod_comp->llc_ostlist.op_array,
3295 lod_comp->llc_ostlist.op_size);
3296 lod_comp->llc_ostlist.op_array = NULL;
3297 lod_comp->llc_ostlist.op_size = 0;
3301 LASSERTF(left >= 0 && left < lo->ldo_comp_cnt, "left = %d\n", left);
3303 struct lod_layout_component *comp_array;
3305 OBD_ALLOC(comp_array, sizeof(*comp_array) * left);
3306 if (comp_array == NULL)
3307 GOTO(out, rc = -ENOMEM);
3309 memcpy(&comp_array[0], &lo->ldo_comp_entries[0],
3310 sizeof(*comp_array) * left);
3312 OBD_FREE(lo->ldo_comp_entries,
3313 sizeof(*comp_array) * lo->ldo_comp_cnt);
3314 lo->ldo_comp_entries = comp_array;
3315 lo->ldo_comp_cnt = left;
3316 lod_obj_inc_layout_gen(lo);
3318 lod_free_comp_entries(lo);
3321 LASSERT(dt_object_exists(dt));
3322 rc = dt_attr_get(env, next, attr);
3326 if (attr->la_size > 0) {
3328 attr->la_valid = LA_SIZE;
3329 rc = lod_sub_attr_set(env, next, attr, th);
3334 rc = lod_generate_and_set_lovea(env, lo, th);
3338 lod_object_free_striping(env, lo);
3343 * Implementation of dt_object_operations::do_xattr_set.
3345 * Sets specified extended attribute on the object. Three types of EAs are
3347 * LOV EA - stores striping for a regular file or default striping (when set
3349 * LMV EA - stores a marker for the striped directories
3350 * DMV EA - stores default directory striping
3352 * When striping is applied to a non-striped existing object (this is called
3353 * late striping), then LOD notices the caller wants to turn the object into a
3354 * striped one. The stripe objects are created and appropriate EA is set:
3355 * LOV EA storing all the stripes directly or LMV EA storing just a small header
3356 * with striping configuration.
3358 * \see dt_object_operations::do_xattr_set() in the API description for details.
3360 static int lod_xattr_set(const struct lu_env *env,
3361 struct dt_object *dt, const struct lu_buf *buf,
3362 const char *name, int fl, struct thandle *th)
3364 struct dt_object *next = dt_object_child(dt);
3368 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3369 strcmp(name, XATTR_NAME_LMV) == 0) {
3370 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
3372 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
3373 LMV_HASH_FLAG_MIGRATION)
3374 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3376 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
3381 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3382 strcmp(name, XATTR_NAME_LOV) == 0) {
3384 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
3386 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3387 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
3389 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
3392 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3393 (!strcmp(name, XATTR_NAME_LOV) ||
3394 !strncmp(name, XATTR_LUSTRE_LOV,
3395 strlen(XATTR_LUSTRE_LOV)))) {
3396 /* in case of lov EA swap, just set it
3397 * if not, it is a replay so check striping match what we
3398 * already have during req replay, declare_xattr_set()
3399 * defines striping, then create() does the work */
3400 if (fl & LU_XATTR_REPLACE) {
3401 /* free stripes, then update disk */
3402 lod_object_free_striping(env, lod_dt_obj(dt));
3404 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3405 } else if (dt_object_remote(dt)) {
3406 /* This only happens during migration, see
3407 * mdd_migrate_create(), in which Master MDT will
3408 * create a remote target object, and only set
3409 * (migrating) stripe EA on the remote object,
3410 * and does not need creating each stripes. */
3411 rc = lod_sub_xattr_set(env, next, buf, name,
3413 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
3414 /* delete component(s) */
3415 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
3416 rc = lod_layout_del(env, dt, th);
3419 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
3420 * it's going to create create file with specified
3421 * component(s), the striping must have not being
3422 * cached in this case;
3424 * Otherwise, it's going to add/change component(s) to
3425 * an existing file, the striping must have been cached
3428 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
3429 !strcmp(name, XATTR_NAME_LOV),
3430 !lod_dt_obj(dt)->ldo_comp_cached));
3432 rc = lod_striped_create(env, dt, NULL, NULL, th);
3435 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3436 rc = lod_replace_parent_fid(env, dt, th, false);
3441 /* then all other xattr */
3442 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3448 * Implementation of dt_object_operations::do_declare_xattr_del.
3450 * \see dt_object_operations::do_declare_xattr_del() in the API description
3453 static int lod_declare_xattr_del(const struct lu_env *env,
3454 struct dt_object *dt, const char *name,
3457 struct lod_object *lo = lod_dt_obj(dt);
3462 rc = lod_sub_declare_xattr_del(env, dt_object_child(dt), name, th);
3466 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3469 /* set xattr to each stripes, if needed */
3470 rc = lod_load_striping(env, lo);
3474 if (lo->ldo_dir_stripe_count == 0)
3477 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3478 LASSERT(lo->ldo_stripe[i]);
3479 rc = lod_sub_declare_xattr_del(env, lo->ldo_stripe[i],
3489 * Implementation of dt_object_operations::do_xattr_del.
3491 * If EA storing a regular striping is being deleted, then release
3492 * all the references to the stripe objects in core.
3494 * \see dt_object_operations::do_xattr_del() in the API description for details.
3496 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
3497 const char *name, struct thandle *th)
3499 struct dt_object *next = dt_object_child(dt);
3500 struct lod_object *lo = lod_dt_obj(dt);
3505 if (!strcmp(name, XATTR_NAME_LOV))
3506 lod_object_free_striping(env, lod_dt_obj(dt));
3508 rc = lod_sub_xattr_del(env, next, name, th);
3509 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3512 if (lo->ldo_dir_stripe_count == 0)
3515 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3516 LASSERT(lo->ldo_stripe[i]);
3518 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3527 * Implementation of dt_object_operations::do_xattr_list.
3529 * \see dt_object_operations::do_xattr_list() in the API description
3532 static int lod_xattr_list(const struct lu_env *env,
3533 struct dt_object *dt, const struct lu_buf *buf)
3535 return dt_xattr_list(env, dt_object_child(dt), buf);
3538 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
3540 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
3545 * Get default striping.
3547 * \param[in] env execution environment
3548 * \param[in] lo object
3549 * \param[out] lds default striping
3551 * \retval 0 on success
3552 * \retval negative if failed
3554 static int lod_get_default_lov_striping(const struct lu_env *env,
3555 struct lod_object *lo,
3556 struct lod_default_striping *lds)
3558 struct lod_thread_info *info = lod_env_info(env);
3559 struct lov_user_md_v1 *v1 = NULL;
3560 struct lov_user_md_v3 *v3 = NULL;
3561 struct lov_comp_md_v1 *comp_v1 = NULL;
3567 lds->lds_def_striping_set = 0;
3569 rc = lod_get_lov_ea(env, lo);
3573 if (rc < (typeof(rc))sizeof(struct lov_user_md))
3576 v1 = info->lti_ea_store;
3577 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
3578 lustre_swab_lov_user_md_v1(v1);
3579 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
3580 v3 = (struct lov_user_md_v3 *)v1;
3581 lustre_swab_lov_user_md_v3(v3);
3582 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3583 comp_v1 = (struct lov_comp_md_v1 *)v1;
3584 lustre_swab_lov_comp_md_v1(comp_v1);
3587 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
3588 v1->lmm_magic != LOV_MAGIC_COMP_V1)
3591 if (v1->lmm_magic == LOV_MAGIC_COMP_V1) {
3592 comp_v1 = (struct lov_comp_md_v1 *)v1;
3593 comp_cnt = comp_v1->lcm_entry_count;
3602 /* realloc default comp entries if necessary */
3603 rc = lod_def_striping_comp_resize(lds, comp_cnt);
3607 lds->lds_def_comp_cnt = comp_cnt;
3608 lds->lds_def_striping_is_composite = composite ? 1 : 0;
3610 for (i = 0; i < comp_cnt; i++) {
3611 struct lod_layout_component *lod_comp;
3612 struct lu_extent *ext;
3615 lod_comp = &lds->lds_def_comp_entries[i];
3617 * reset lod_comp values, llc_stripes is always NULL in
3618 * the default striping template, llc_pool will be reset
3621 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
3624 v1 = (struct lov_user_md *)((char *)comp_v1 +
3625 comp_v1->lcm_entries[i].lcme_offset);
3626 ext = &comp_v1->lcm_entries[i].lcme_extent;
3627 lod_comp->llc_extent = *ext;
3630 if (v1->lmm_pattern != LOV_PATTERN_RAID0 &&
3631 v1->lmm_pattern != LOV_PATTERN_MDT &&
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_stripe_count = v1->lmm_stripe_count;
3644 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
3645 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
3646 lod_comp->llc_pattern = v1->lmm_pattern;
3649 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
3650 /* XXX: sanity check here */
3651 v3 = (struct lov_user_md_v3 *) v1;
3652 if (v3->lmm_pool_name[0] != '\0')
3653 pool = v3->lmm_pool_name;
3655 lod_set_def_pool(lds, i, pool);
3658 lds->lds_def_striping_set = 1;
3663 * Get default directory striping.
3665 * \param[in] env execution environment
3666 * \param[in] lo object
3667 * \param[out] lds default striping
3669 * \retval 0 on success
3670 * \retval negative if failed
3672 static int lod_get_default_lmv_striping(const struct lu_env *env,
3673 struct lod_object *lo,
3674 struct lod_default_striping *lds)
3676 struct lod_thread_info *info = lod_env_info(env);
3677 struct lmv_user_md_v1 *v1 = NULL;
3681 lds->lds_dir_def_striping_set = 0;
3682 rc = lod_get_default_lmv_ea(env, lo);
3686 if (rc < (typeof(rc))sizeof(struct lmv_user_md))
3689 v1 = info->lti_ea_store;
3691 lds->lds_dir_def_stripe_count = le32_to_cpu(v1->lum_stripe_count);
3692 lds->lds_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
3693 lds->lds_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
3694 lds->lds_dir_def_striping_set = 1;
3700 * Get default striping in the object.
3702 * Get object default striping and default directory striping.
3704 * \param[in] env execution environment
3705 * \param[in] lo object
3706 * \param[out] lds default striping
3708 * \retval 0 on success
3709 * \retval negative if failed
3711 static int lod_get_default_striping(const struct lu_env *env,
3712 struct lod_object *lo,
3713 struct lod_default_striping *lds)
3717 rc = lod_get_default_lov_striping(env, lo, lds);
3718 rc1 = lod_get_default_lmv_striping(env, lo, lds);
3719 if (rc == 0 && rc1 < 0)
3726 * Apply default striping on object.
3728 * If object striping pattern is not set, set to the one in default striping.
3729 * The default striping is from parent or fs.
3731 * \param[in] lo new object
3732 * \param[in] lds default striping
3733 * \param[in] mode new object's mode
3735 static void lod_striping_from_default(struct lod_object *lo,
3736 const struct lod_default_striping *lds,
3739 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
3740 struct lov_desc *desc = &d->lod_desc;
3743 if (lds->lds_def_striping_set && S_ISREG(mode)) {
3744 rc = lod_alloc_comp_entries(lo, lds->lds_def_comp_cnt);
3748 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
3750 for (i = 0; i < lo->ldo_comp_cnt; i++) {
3751 struct lod_layout_component *obj_comp =
3752 &lo->ldo_comp_entries[i];
3753 struct lod_layout_component *def_comp =
3754 &lds->lds_def_comp_entries[i];
3756 CDEBUG(D_LAYOUT, "Inherite from default: size:%hu "
3757 "nr:%u offset:%u pattern %#x %s\n",
3758 def_comp->llc_stripe_size,
3759 def_comp->llc_stripe_count,
3760 def_comp->llc_stripe_offset,
3761 def_comp->llc_pattern,
3762 def_comp->llc_pool ?: "");
3764 *obj_comp = *def_comp;
3765 if (def_comp->llc_pool != NULL) {
3766 /* pointer was copied from def_comp */
3767 obj_comp->llc_pool = NULL;
3768 lod_obj_set_pool(lo, i, def_comp->llc_pool);
3772 * Don't initialize these fields for plain layout
3773 * (v1/v3) here, they are inherited in the order of
3774 * 'parent' -> 'fs default (root)' -> 'global default
3775 * values for stripe_count & stripe_size'.
3777 * see lod_ah_init().
3779 if (!lo->ldo_is_composite)
3782 if (obj_comp->llc_stripe_count <= 0 &&
3783 obj_comp->llc_pattern != LOV_PATTERN_MDT)
3784 obj_comp->llc_stripe_count =
3785 desc->ld_default_stripe_count;
3786 if (obj_comp->llc_stripe_size <= 0)
3787 obj_comp->llc_stripe_size =
3788 desc->ld_default_stripe_size;
3790 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
3791 if (lo->ldo_dir_stripe_count == 0)
3792 lo->ldo_dir_stripe_count =
3793 lds->lds_dir_def_stripe_count;
3794 if (lo->ldo_dir_stripe_offset == -1)
3795 lo->ldo_dir_stripe_offset =
3796 lds->lds_dir_def_stripe_offset;
3797 if (lo->ldo_dir_hash_type == 0)
3798 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
3800 CDEBUG(D_LAYOUT, "striping from default dir: count:%hu, "
3801 "offset:%u, hash_type:%u\n",
3802 lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
3803 lo->ldo_dir_hash_type);
3807 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root)
3809 struct lod_layout_component *lod_comp;
3811 if (lo->ldo_comp_cnt == 0)
3814 if (lo->ldo_is_composite)
3817 lod_comp = &lo->ldo_comp_entries[0];
3819 if (lod_comp->llc_stripe_count <= 0 ||
3820 lod_comp->llc_stripe_size <= 0)
3823 if (from_root && (lod_comp->llc_pool == NULL ||
3824 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
3831 * Implementation of dt_object_operations::do_ah_init.
3833 * This method is used to make a decision on the striping configuration for the
3834 * object being created. It can be taken from the \a parent object if it exists,
3835 * or filesystem's default. The resulting configuration (number of stripes,
3836 * stripe size/offset, pool name, etc) is stored in the object itself and will
3837 * be used by the methods like ->doo_declare_create().
3839 * \see dt_object_operations::do_ah_init() in the API description for details.
3841 static void lod_ah_init(const struct lu_env *env,
3842 struct dt_allocation_hint *ah,
3843 struct dt_object *parent,
3844 struct dt_object *child,
3847 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
3848 struct lod_thread_info *info = lod_env_info(env);
3849 struct lod_default_striping *lds = &info->lti_def_striping;
3850 struct dt_object *nextp = NULL;
3851 struct dt_object *nextc;
3852 struct lod_object *lp = NULL;
3853 struct lod_object *lc;
3854 struct lov_desc *desc;
3855 struct lod_layout_component *lod_comp;
3861 if (likely(parent)) {
3862 nextp = dt_object_child(parent);
3863 lp = lod_dt_obj(parent);
3866 nextc = dt_object_child(child);
3867 lc = lod_dt_obj(child);
3869 LASSERT(!lod_obj_is_striped(child));
3870 /* default layout template may have been set on the regular file
3871 * when this is called from mdd_create_data() */
3872 if (S_ISREG(child_mode))
3873 lod_free_comp_entries(lc);
3875 if (!dt_object_exists(nextc))
3876 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
3878 if (S_ISDIR(child_mode)) {
3879 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
3881 /* other default values are 0 */
3882 lc->ldo_dir_stripe_offset = -1;
3884 /* get default striping from parent object */
3885 if (likely(lp != NULL))
3886 lod_get_default_striping(env, lp, lds);
3888 /* set child default striping info, default value is NULL */
3889 if (lds->lds_def_striping_set || lds->lds_dir_def_striping_set)
3890 lc->ldo_def_striping = lds;
3892 /* It should always honour the specified stripes */
3893 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
3894 * will have old magic. In this case, we should ignore the
3895 * stripe count and try to create dir by default stripe.
3897 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
3898 le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC) {
3899 lc->ldo_dir_stripe_count =
3900 le32_to_cpu(lum1->lum_stripe_count);
3901 lc->ldo_dir_stripe_offset =
3902 le32_to_cpu(lum1->lum_stripe_offset);
3903 lc->ldo_dir_hash_type =
3904 le32_to_cpu(lum1->lum_hash_type);
3906 "set dirstripe: count %hu, offset %d, hash %u\n",
3907 lc->ldo_dir_stripe_count,
3908 (int)lc->ldo_dir_stripe_offset,
3909 lc->ldo_dir_hash_type);
3911 /* transfer defaults LMV to new directory */
3912 lod_striping_from_default(lc, lds, child_mode);
3915 /* shrink the stripe_count to the avaible MDT count */
3916 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
3917 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))
3918 lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
3920 /* Directory will be striped only if stripe_count > 1, if
3921 * stripe_count == 1, let's reset stripe_count = 0 to avoid
3922 * create single master stripe and also help to unify the
3923 * stripe handling of directories and files */
3924 if (lc->ldo_dir_stripe_count == 1)
3925 lc->ldo_dir_stripe_count = 0;
3927 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
3928 lc->ldo_dir_stripe_count,
3929 (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
3934 /* child object regular file*/
3936 if (!lod_object_will_be_striped(S_ISREG(child_mode),
3937 lu_object_fid(&child->do_lu)))
3940 /* If object is going to be striped over OSTs, transfer default
3941 * striping information to the child, so that we can use it
3942 * during declaration and creation.
3944 * Try from the parent first.
3946 if (likely(lp != NULL)) {
3947 rc = lod_get_default_lov_striping(env, lp, lds);
3949 lod_striping_from_default(lc, lds, child_mode);
3952 /* Initialize lod_device::lod_md_root object reference */
3953 if (d->lod_md_root == NULL) {
3954 struct dt_object *root;
3955 struct lod_object *lroot;
3957 lu_root_fid(&info->lti_fid);
3958 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
3959 if (!IS_ERR(root)) {
3960 lroot = lod_dt_obj(root);
3962 spin_lock(&d->lod_lock);
3963 if (d->lod_md_root != NULL)
3964 dt_object_put(env, &d->lod_md_root->ldo_obj);
3965 d->lod_md_root = lroot;
3966 spin_unlock(&d->lod_lock);
3970 /* try inherit layout from the root object (fs default) when:
3971 * - parent does not have default layout; or
3972 * - parent has plain(v1/v3) default layout, and some attributes
3973 * are not specified in the default layout;
3975 if (d->lod_md_root != NULL && lod_need_inherit_more(lc, true)) {
3976 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds);
3979 if (lc->ldo_comp_cnt == 0) {
3980 lod_striping_from_default(lc, lds, child_mode);
3981 } else if (!lds->lds_def_striping_is_composite) {
3982 struct lod_layout_component *def_comp;
3984 LASSERT(!lc->ldo_is_composite);
3985 lod_comp = &lc->ldo_comp_entries[0];
3986 def_comp = &lds->lds_def_comp_entries[0];
3988 if (lod_comp->llc_stripe_count <= 0)
3989 lod_comp->llc_stripe_count =
3990 def_comp->llc_stripe_count;
3991 if (lod_comp->llc_stripe_size <= 0)
3992 lod_comp->llc_stripe_size =
3993 def_comp->llc_stripe_size;
3994 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
3995 lod_comp->llc_stripe_offset =
3996 def_comp->llc_stripe_offset;
3997 if (lod_comp->llc_pool == NULL)
3998 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
4003 * fs default striping may not be explicitly set, or historically set
4004 * in config log, use them.
4006 if (lod_need_inherit_more(lc, false)) {
4008 if (lc->ldo_comp_cnt == 0) {
4009 rc = lod_alloc_comp_entries(lc, 1);
4011 /* fail to allocate memory, will create a
4012 * non-striped file. */
4014 lc->ldo_is_composite = 0;
4015 lod_comp = &lc->ldo_comp_entries[0];
4016 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4018 LASSERT(!lc->ldo_is_composite);
4019 lod_comp = &lc->ldo_comp_entries[0];
4020 desc = &d->lod_desc;
4021 if (lod_comp->llc_stripe_count <= 0)
4022 lod_comp->llc_stripe_count =
4023 desc->ld_default_stripe_count;
4024 if (lod_comp->llc_stripe_size <= 0)
4025 lod_comp->llc_stripe_size =
4026 desc->ld_default_stripe_size;
4032 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
4034 * Size initialization on late striping.
4036 * Propagate the size of a truncated object to a deferred striping.
4037 * This function handles a special case when truncate was done on a
4038 * non-striped object and now while the striping is being created
4039 * we can't lose that size, so we have to propagate it to the stripes
4042 * \param[in] env execution environment
4043 * \param[in] dt object
4044 * \param[in] th transaction handle
4046 * \retval 0 on success
4047 * \retval negative if failed
4049 static int lod_declare_init_size(const struct lu_env *env,
4050 struct dt_object *dt, struct thandle *th)
4052 struct dt_object *next = dt_object_child(dt);
4053 struct lod_object *lo = lod_dt_obj(dt);
4054 struct dt_object **objects = NULL;
4055 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4056 uint64_t size, offs;
4057 int i, rc, stripe, stripe_count = 0, stripe_size = 0;
4060 if (!lod_obj_is_striped(dt))
4063 rc = dt_attr_get(env, next, attr);
4064 LASSERT(attr->la_valid & LA_SIZE);
4068 size = attr->la_size;
4072 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4073 struct lod_layout_component *lod_comp;
4074 struct lu_extent *extent;
4076 lod_comp = &lo->ldo_comp_entries[i];
4078 if (lod_comp->llc_stripe == NULL)
4081 extent = &lod_comp->llc_extent;
4082 CDEBUG(D_INFO, "%lld [%lld, %lld)\n",
4083 size, extent->e_start, extent->e_end);
4084 if (!lo->ldo_is_composite ||
4085 (size >= extent->e_start && size < extent->e_end)) {
4086 objects = lod_comp->llc_stripe;
4087 stripe_count = lod_comp->llc_stripe_count;
4088 stripe_size = lod_comp->llc_stripe_size;
4093 if (stripe_count == 0)
4096 LASSERT(objects != NULL && stripe_size != 0);
4098 /* ll_do_div64(a, b) returns a % b, and a = a / b */
4099 ll_do_div64(size, (__u64)stripe_size);
4100 stripe = ll_do_div64(size, (__u64)stripe_count);
4101 LASSERT(objects[stripe] != NULL);
4103 size = size * stripe_size;
4104 offs = attr->la_size;
4105 size += ll_do_div64(offs, stripe_size);
4107 attr->la_valid = LA_SIZE;
4108 attr->la_size = size;
4110 rc = lod_sub_declare_attr_set(env, objects[stripe], attr, th);
4116 * Declare creation of striped object.
4118 * The function declares creation stripes for a regular object. The function
4119 * also declares whether the stripes will be created with non-zero size if
4120 * previously size was set non-zero on the master object. If object \a dt is
4121 * not local, then only fully defined striping can be applied in \a lovea.
4122 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
4125 * \param[in] env execution environment
4126 * \param[in] dt object
4127 * \param[in] attr attributes the stripes will be created with
4128 * \param[in] lovea a buffer containing striping description
4129 * \param[in] th transaction handle
4131 * \retval 0 on success
4132 * \retval negative if failed
4134 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
4135 struct lu_attr *attr,
4136 const struct lu_buf *lovea, struct thandle *th)
4138 struct lod_thread_info *info = lod_env_info(env);
4139 struct dt_object *next = dt_object_child(dt);
4140 struct lod_object *lo = lod_dt_obj(dt);
4144 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
4145 GOTO(out, rc = -ENOMEM);
4147 if (!dt_object_remote(next)) {
4148 /* choose OST and generate appropriate objects */
4149 rc = lod_prepare_create(env, lo, attr, lovea, th);
4154 * declare storage for striping data
4156 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
4158 /* LOD can not choose OST objects for remote objects, i.e.
4159 * stripes must be ready before that. Right now, it can only
4160 * happen during migrate, i.e. migrate process needs to create
4161 * remote regular file (mdd_migrate_create), then the migrate
4162 * process will provide stripeEA. */
4163 LASSERT(lovea != NULL);
4164 info->lti_buf = *lovea;
4167 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
4168 XATTR_NAME_LOV, 0, th);
4173 * if striping is created with local object's size > 0,
4174 * we have to propagate this size to specific object
4175 * the case is possible only when local object was created previously
4177 if (dt_object_exists(next))
4178 rc = lod_declare_init_size(env, dt, th);
4181 /* failed to create striping or to set initial size, let's reset
4182 * config so that others don't get confused */
4184 lod_object_free_striping(env, lo);
4190 * Implementation of dt_object_operations::do_declare_create.
4192 * The method declares creation of a new object. If the object will be striped,
4193 * then helper functions are called to find FIDs for the stripes, declare
4194 * creation of the stripes and declare initialization of the striping
4195 * information to be stored in the master object.
4197 * \see dt_object_operations::do_declare_create() in the API description
4200 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
4201 struct lu_attr *attr,
4202 struct dt_allocation_hint *hint,
4203 struct dt_object_format *dof, struct thandle *th)
4205 struct dt_object *next = dt_object_child(dt);
4206 struct lod_object *lo = lod_dt_obj(dt);
4215 * first of all, we declare creation of local object
4217 rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
4222 * it's lod_ah_init() that has decided the object will be striped
4224 if (dof->dof_type == DFT_REGULAR) {
4225 /* callers don't want stripes */
4226 /* XXX: all tricky interactions with ->ah_make_hint() decided
4227 * to use striping, then ->declare_create() behaving differently
4228 * should be cleaned */
4229 if (dof->u.dof_reg.striped != 0)
4230 rc = lod_declare_striped_create(env, dt, attr,
4232 } else if (dof->dof_type == DFT_DIR) {
4233 struct seq_server_site *ss;
4235 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
4237 /* If the parent has default stripeEA, and client
4238 * did not find it before sending create request,
4239 * then MDT will return -EREMOTE, and client will
4240 * retrieve the default stripeEA and re-create the
4243 * Note: if dah_eadata != NULL, it means creating the
4244 * striped directory with specified stripeEA, then it
4245 * should ignore the default stripeEA */
4246 if (hint != NULL && hint->dah_eadata == NULL) {
4247 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
4248 GOTO(out, rc = -EREMOTE);
4250 if (lo->ldo_dir_stripe_offset == -1) {
4251 /* child and parent should be in the same MDT */
4252 if (hint->dah_parent != NULL &&
4253 dt_object_remote(hint->dah_parent))
4254 GOTO(out, rc = -EREMOTE);
4255 } else if (lo->ldo_dir_stripe_offset !=
4257 struct lod_device *lod;
4258 struct lod_tgt_descs *ltd;
4259 struct lod_tgt_desc *tgt = NULL;
4260 bool found_mdt = false;
4263 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4264 ltd = &lod->lod_mdt_descs;
4265 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
4266 tgt = LTD_TGT(ltd, i);
4267 if (tgt->ltd_index ==
4268 lo->ldo_dir_stripe_offset) {
4274 /* If the MDT indicated by stripe_offset can be
4275 * found, then tell client to resend the create
4276 * request to the correct MDT, otherwise return
4277 * error to client */
4279 GOTO(out, rc = -EREMOTE);
4281 GOTO(out, rc = -EINVAL);
4285 rc = lod_declare_dir_striping_create(env, dt, attr, dof, th);
4288 /* failed to create striping or to set initial size, let's reset
4289 * config so that others don't get confused */
4291 lod_object_free_striping(env, lo);
4296 * Creation of a striped regular object.
4298 * The function is called to create the stripe objects for a regular
4299 * striped file. This can happen at the initial object creation or
4300 * when the caller asks LOD to do so using ->do_xattr_set() method
4301 * (so called late striping). Notice all the information are already
4302 * prepared in the form of the list of objects (ldo_stripe field).
4303 * This is done during declare phase.
4305 * \param[in] env execution environment
4306 * \param[in] dt object
4307 * \param[in] attr attributes the stripes will be created with
4308 * \param[in] dof format of stripes (see OSD API description)
4309 * \param[in] th transaction handle
4311 * \retval 0 on success
4312 * \retval negative if failed
4314 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
4315 struct lu_attr *attr, struct dt_object_format *dof,
4318 struct lod_layout_component *lod_comp;
4319 struct lod_object *lo = lod_dt_obj(dt);
4323 LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
4325 /* create all underlying objects */
4326 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4327 lod_comp = &lo->ldo_comp_entries[i];
4329 if (lod_comp_inited(lod_comp))
4332 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
4333 lod_comp_set_init(lod_comp);
4335 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
4336 lod_comp_set_init(lod_comp);
4338 if (lod_comp->llc_stripe == NULL)
4341 LASSERT(lod_comp->llc_stripe_count);
4342 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4343 struct dt_object *object = lod_comp->llc_stripe[j];
4344 LASSERT(object != NULL);
4345 rc = lod_sub_create(env, object, attr, NULL, dof, th);
4349 lod_comp_set_init(lod_comp);
4353 rc = lod_generate_and_set_lovea(env, lo, th);
4356 lo->ldo_comp_cached = 1;
4358 lod_object_free_striping(env, lo);
4364 * Implementation of dt_object_operations::do_create.
4366 * If any of preceeding methods (like ->do_declare_create(),
4367 * ->do_ah_init(), etc) chose to create a striped object,
4368 * then this method will create the master and the stripes.
4370 * \see dt_object_operations::do_create() in the API description for details.
4372 static int lod_create(const struct lu_env *env, struct dt_object *dt,
4373 struct lu_attr *attr, struct dt_allocation_hint *hint,
4374 struct dt_object_format *dof, struct thandle *th)
4379 /* create local object */
4380 rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
4384 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4385 lod_obj_is_striped(dt) && dof->u.dof_reg.striped != 0) {
4386 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
4387 rc = lod_striped_create(env, dt, attr, dof, th);
4394 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
4395 struct dt_object *dt, struct thandle *th,
4396 int stripe_idx, struct lod_obj_stripe_cb_data *data)
4398 if (data->locd_declare)
4399 return lod_sub_declare_destroy(env, dt, th);
4400 else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4401 stripe_idx == cfs_fail_val)
4402 return lod_sub_destroy(env, dt, th);
4408 * Implementation of dt_object_operations::do_declare_destroy.
4410 * If the object is a striped directory, then the function declares reference
4411 * removal from the master object (this is an index) to the stripes and declares
4412 * destroy of all the stripes. In all the cases, it declares an intention to
4413 * destroy the object itself.
4415 * \see dt_object_operations::do_declare_destroy() in the API description
4418 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
4421 struct dt_object *next = dt_object_child(dt);
4422 struct lod_object *lo = lod_dt_obj(dt);
4423 struct lod_thread_info *info = lod_env_info(env);
4424 char *stripe_name = info->lti_key;
4429 * load striping information, notice we don't do this when object
4430 * is being initialized as we don't need this information till
4431 * few specific cases like destroy, chown
4433 rc = lod_load_striping(env, lo);
4437 /* declare destroy for all underlying objects */
4438 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4439 rc = next->do_ops->do_index_try(env, next,
4440 &dt_directory_features);
4444 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4445 rc = lod_sub_declare_ref_del(env, next, th);
4449 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4450 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4452 rc = lod_sub_declare_delete(env, next,
4453 (const struct dt_key *)stripe_name, th);
4460 * we declare destroy for the local object
4462 rc = lod_sub_declare_destroy(env, next, th);
4466 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4467 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4470 if (!lod_obj_is_striped(dt))
4473 /* declare destroy all striped objects */
4474 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4475 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4476 if (lo->ldo_stripe[i] == NULL)
4479 rc = lod_sub_declare_ref_del(env, lo->ldo_stripe[i],
4482 rc = lod_sub_declare_destroy(env, lo->ldo_stripe[i],
4488 struct lod_obj_stripe_cb_data data;
4490 data.locd_declare = true;
4491 rc = lod_obj_for_each_stripe(env, lo, th,
4492 lod_obj_stripe_destroy_cb, &data);
4499 * Implementation of dt_object_operations::do_destroy.
4501 * If the object is a striped directory, then the function removes references
4502 * from the master object (this is an index) to the stripes and destroys all
4503 * the stripes. In all the cases, the function destroys the object itself.
4505 * \see dt_object_operations::do_destroy() in the API description for details.
4507 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
4510 struct dt_object *next = dt_object_child(dt);
4511 struct lod_object *lo = lod_dt_obj(dt);
4512 struct lod_thread_info *info = lod_env_info(env);
4513 char *stripe_name = info->lti_key;
4518 /* destroy sub-stripe of master object */
4519 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4520 rc = next->do_ops->do_index_try(env, next,
4521 &dt_directory_features);
4525 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4526 rc = lod_sub_ref_del(env, next, th);
4530 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4531 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4534 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
4535 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
4536 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
4538 rc = lod_sub_delete(env, next,
4539 (const struct dt_key *)stripe_name, th);
4545 rc = lod_sub_destroy(env, next, th);
4549 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4550 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4553 if (!lod_obj_is_striped(dt))
4556 /* destroy all striped objects */
4557 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4558 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4559 if (lo->ldo_stripe[i] == NULL)
4561 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4562 i == cfs_fail_val) {
4563 dt_write_lock(env, lo->ldo_stripe[i],
4565 rc = lod_sub_ref_del(env, lo->ldo_stripe[i],
4567 dt_write_unlock(env, lo->ldo_stripe[i]);
4571 rc = lod_sub_destroy(env, lo->ldo_stripe[i],
4578 struct lod_obj_stripe_cb_data data;
4580 data.locd_declare = false;
4581 rc = lod_obj_for_each_stripe(env, lo, th,
4582 lod_obj_stripe_destroy_cb, &data);
4589 * Implementation of dt_object_operations::do_declare_ref_add.
4591 * \see dt_object_operations::do_declare_ref_add() in the API description
4594 static int lod_declare_ref_add(const struct lu_env *env,
4595 struct dt_object *dt, struct thandle *th)
4597 return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
4601 * Implementation of dt_object_operations::do_ref_add.
4603 * \see dt_object_operations::do_ref_add() in the API description for details.
4605 static int lod_ref_add(const struct lu_env *env,
4606 struct dt_object *dt, struct thandle *th)
4608 return lod_sub_ref_add(env, dt_object_child(dt), th);
4612 * Implementation of dt_object_operations::do_declare_ref_del.
4614 * \see dt_object_operations::do_declare_ref_del() in the API description
4617 static int lod_declare_ref_del(const struct lu_env *env,
4618 struct dt_object *dt, struct thandle *th)
4620 return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
4624 * Implementation of dt_object_operations::do_ref_del
4626 * \see dt_object_operations::do_ref_del() in the API description for details.
4628 static int lod_ref_del(const struct lu_env *env,
4629 struct dt_object *dt, struct thandle *th)
4631 return lod_sub_ref_del(env, dt_object_child(dt), th);
4635 * Implementation of dt_object_operations::do_object_sync.
4637 * \see dt_object_operations::do_object_sync() in the API description
4640 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
4641 __u64 start, __u64 end)
4643 return dt_object_sync(env, dt_object_child(dt), start, end);
4647 * Release LDLM locks on the stripes of a striped directory.
4649 * Iterates over all the locks taken on the stripe objects and
4652 * \param[in] env execution environment
4653 * \param[in] dt striped object
4654 * \param[in] einfo lock description
4655 * \param[in] policy data describing requested lock
4657 * \retval 0 on success
4658 * \retval negative if failed
4660 static int lod_object_unlock_internal(const struct lu_env *env,
4661 struct dt_object *dt,
4662 struct ldlm_enqueue_info *einfo,
4663 union ldlm_policy_data *policy)
4665 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
4670 if (slave_locks == NULL)
4673 for (i = 1; i < slave_locks->count; i++) {
4674 if (lustre_handle_is_used(&slave_locks->handles[i]))
4675 ldlm_lock_decref_and_cancel(&slave_locks->handles[i],
4683 * Implementation of dt_object_operations::do_object_unlock.
4685 * Used to release LDLM lock(s).
4687 * \see dt_object_operations::do_object_unlock() in the API description
4690 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
4691 struct ldlm_enqueue_info *einfo,
4692 union ldlm_policy_data *policy)
4694 struct lod_object *lo = lod_dt_obj(dt);
4695 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
4696 int slave_locks_size;
4700 if (slave_locks == NULL)
4703 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
4704 LASSERT(lo->ldo_dir_stripe_count > 1);
4705 /* Note: for remote lock for single stripe dir, MDT will cancel
4706 * the lock by lockh directly */
4707 LASSERT(!dt_object_remote(dt_object_child(dt)));
4709 /* locks were unlocked in MDT layer */
4710 for (i = 1; i < slave_locks->count; i++) {
4711 LASSERT(!lustre_handle_is_used(&slave_locks->handles[i]));
4712 dt_invalidate(env, lo->ldo_stripe[i]);
4715 slave_locks_size = sizeof(*slave_locks) + slave_locks->count *
4716 sizeof(slave_locks->handles[0]);
4717 OBD_FREE(slave_locks, slave_locks_size);
4718 einfo->ei_cbdata = NULL;
4724 * Implementation of dt_object_operations::do_object_lock.
4726 * Used to get LDLM lock on the non-striped and striped objects.
4728 * \see dt_object_operations::do_object_lock() in the API description
4731 static int lod_object_lock(const struct lu_env *env,
4732 struct dt_object *dt,
4733 struct lustre_handle *lh,
4734 struct ldlm_enqueue_info *einfo,
4735 union ldlm_policy_data *policy)
4737 struct lod_object *lo = lod_dt_obj(dt);
4740 int slave_locks_size;
4741 struct lustre_handle_array *slave_locks = NULL;
4744 /* remote object lock */
4745 if (!einfo->ei_enq_slave) {
4746 LASSERT(dt_object_remote(dt));
4747 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
4751 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4752 GOTO(out, rc = -ENOTDIR);
4754 rc = lod_load_striping(env, lo);
4759 if (lo->ldo_dir_stripe_count <= 1) {
4761 * NB, ei_cbdata stores pointer to slave locks, if no locks
4762 * taken, make sure it's set to NULL, otherwise MDT will try to
4765 einfo->ei_cbdata = NULL;
4769 slave_locks_size = sizeof(*slave_locks) + lo->ldo_dir_stripe_count *
4770 sizeof(slave_locks->handles[0]);
4771 /* Freed in lod_object_unlock */
4772 OBD_ALLOC(slave_locks, slave_locks_size);
4773 if (slave_locks == NULL)
4774 GOTO(out, rc = -ENOMEM);
4775 slave_locks->count = lo->ldo_dir_stripe_count;
4777 /* striped directory lock */
4778 for (i = 1; i < lo->ldo_dir_stripe_count; i++) {
4779 struct lustre_handle lockh;
4780 struct ldlm_res_id *res_id;
4782 res_id = &lod_env_info(env)->lti_res_id;
4783 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
4785 einfo->ei_res_id = res_id;
4787 LASSERT(lo->ldo_stripe[i] != NULL);
4788 if (likely(dt_object_remote(lo->ldo_stripe[i]))) {
4789 rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh,
4792 struct ldlm_namespace *ns = einfo->ei_namespace;
4793 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
4794 ldlm_completion_callback completion = einfo->ei_cb_cp;
4795 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
4797 if (einfo->ei_mode == LCK_PW ||
4798 einfo->ei_mode == LCK_EX)
4799 dlmflags |= LDLM_FL_COS_INCOMPAT;
4801 /* This only happens if there are mulitple stripes
4802 * on the master MDT, i.e. except stripe0, there are
4803 * other stripes on the Master MDT as well, Only
4804 * happens in the test case right now. */
4805 LASSERT(ns != NULL);
4806 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS,
4807 policy, einfo->ei_mode,
4808 &dlmflags, blocking,
4810 NULL, 0, LVB_T_NONE,
4815 slave_locks->handles[i] = lockh;
4817 einfo->ei_cbdata = slave_locks;
4819 if (rc != 0 && slave_locks != NULL) {
4820 lod_object_unlock_internal(env, dt, einfo, policy);
4821 OBD_FREE(slave_locks, slave_locks_size);
4826 einfo->ei_cbdata = NULL;
4831 * Implementation of dt_object_operations::do_invalidate.
4833 * \see dt_object_operations::do_invalidate() in the API description for details
4835 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
4837 return dt_invalidate(env, dt_object_child(dt));
4840 static int lod_declare_layout_change(const struct lu_env *env,
4841 struct dt_object *dt,
4842 struct layout_intent *layout,
4843 const struct lu_buf *buf,
4846 struct lod_thread_info *info = lod_env_info(env);
4847 struct lod_object *lo = lod_dt_obj(dt);
4848 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
4849 struct dt_object *next = dt_object_child(dt);
4850 struct ost_pool *inuse = &info->lti_inuse_osts;
4851 struct lod_layout_component *lod_comp;
4852 struct lov_comp_md_v1 *comp_v1 = NULL;
4853 bool replay = false;
4854 bool need_create = false;
4858 if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
4859 dt_object_remote(next))
4862 dt_write_lock(env, next, 0);
4864 * In case the client is passing lovea, which only happens during
4865 * the replay of layout intent write RPC for now, we may need to
4866 * parse the lovea and apply new layout configuration.
4868 if (buf && buf->lb_len) {
4869 struct lov_user_md_v1 *v1 = buf->lb_buf;
4871 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
4872 v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
4873 LOV_MAGIC_COMP_V1)) {
4874 CERROR("%s: the replay buffer of layout extend "
4875 "(magic %#x) does not contain expected "
4876 "composite layout.\n",
4877 lod2obd(d)->obd_name, v1->lmm_magic);
4878 GOTO(out, rc = -EINVAL);
4881 lod_object_free_striping(env, lo);
4882 rc = lod_use_defined_striping(env, lo, buf);
4886 rc = lod_get_lov_ea(env, lo);
4889 /* old on-disk EA is stored in info->lti_buf */
4890 comp_v1 = (struct lov_comp_md_v1 *)&info->lti_buf.lb_buf;
4893 /* non replay path */
4894 rc = lod_load_striping_locked(env, lo);
4898 /* Prepare inuse array for composite file */
4899 rc = lod_prepare_inuse(env, lo);
4904 /* Make sure defined layout covers the requested write range. */
4905 lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
4906 if (lo->ldo_comp_cnt > 1 &&
4907 lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
4908 lod_comp->llc_extent.e_end < layout->li_end) {
4909 CDEBUG(replay ? D_ERROR : D_LAYOUT,
4910 "%s: the defined layout [0, %#llx) does not covers "
4911 "the write range [%#llx, %#llx).\n",
4912 lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
4913 layout->li_start, layout->li_end);
4914 GOTO(out, rc = -EINVAL);
4918 * Iterate ld->ldo_comp_entries, find the component whose extent under
4919 * the write range and not instantianted.
4921 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4922 lod_comp = &lo->ldo_comp_entries[i];
4924 if (lod_comp->llc_extent.e_start >= layout->li_end)
4928 if (lod_comp_inited(lod_comp))
4932 * In replay path, lod_comp is the EA passed by
4933 * client replay buffer, comp_v1 is the pre-recovery
4934 * on-disk EA, we'd sift out those components which
4935 * were init-ed in the on-disk EA.
4937 if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
4942 * this component hasn't instantiated in normal path, or during
4943 * replay it needs replay the instantiation.
4946 /* A released component is being extended */
4947 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
4948 GOTO(out, rc = -EINVAL);
4952 rc = lod_qos_prep_create(env, lo, NULL, th, i, inuse);
4958 lod_obj_inc_layout_gen(lo);
4960 GOTO(unlock, rc = -EALREADY);
4963 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
4964 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
4965 XATTR_NAME_LOV, 0, th);
4969 lod_object_free_striping(env, lo);
4972 dt_write_unlock(env, next);
4978 * Instantiate layout component objects which covers the intent write offset.
4980 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
4981 struct layout_intent *layout,
4982 const struct lu_buf *buf, struct thandle *th)
4984 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4986 RETURN(lod_striped_create(env, dt, attr, NULL, th));
4989 struct dt_object_operations lod_obj_ops = {
4990 .do_read_lock = lod_read_lock,
4991 .do_write_lock = lod_write_lock,
4992 .do_read_unlock = lod_read_unlock,
4993 .do_write_unlock = lod_write_unlock,
4994 .do_write_locked = lod_write_locked,
4995 .do_attr_get = lod_attr_get,
4996 .do_declare_attr_set = lod_declare_attr_set,
4997 .do_attr_set = lod_attr_set,
4998 .do_xattr_get = lod_xattr_get,
4999 .do_declare_xattr_set = lod_declare_xattr_set,
5000 .do_xattr_set = lod_xattr_set,
5001 .do_declare_xattr_del = lod_declare_xattr_del,
5002 .do_xattr_del = lod_xattr_del,
5003 .do_xattr_list = lod_xattr_list,
5004 .do_ah_init = lod_ah_init,
5005 .do_declare_create = lod_declare_create,
5006 .do_create = lod_create,
5007 .do_declare_destroy = lod_declare_destroy,
5008 .do_destroy = lod_destroy,
5009 .do_index_try = lod_index_try,
5010 .do_declare_ref_add = lod_declare_ref_add,
5011 .do_ref_add = lod_ref_add,
5012 .do_declare_ref_del = lod_declare_ref_del,
5013 .do_ref_del = lod_ref_del,
5014 .do_object_sync = lod_object_sync,
5015 .do_object_lock = lod_object_lock,
5016 .do_object_unlock = lod_object_unlock,
5017 .do_invalidate = lod_invalidate,
5018 .do_declare_layout_change = lod_declare_layout_change,
5019 .do_layout_change = lod_layout_change,
5023 * Implementation of dt_body_operations::dbo_read.
5025 * \see dt_body_operations::dbo_read() in the API description for details.
5027 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
5028 struct lu_buf *buf, loff_t *pos)
5030 struct dt_object *next = dt_object_child(dt);
5032 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
5033 S_ISLNK(dt->do_lu.lo_header->loh_attr));
5034 return next->do_body_ops->dbo_read(env, next, buf, pos);
5038 * Implementation of dt_body_operations::dbo_declare_write.
5040 * \see dt_body_operations::dbo_declare_write() in the API description
5043 static ssize_t lod_declare_write(const struct lu_env *env,
5044 struct dt_object *dt,
5045 const struct lu_buf *buf, loff_t pos,
5048 return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
5052 * Implementation of dt_body_operations::dbo_write.
5054 * \see dt_body_operations::dbo_write() in the API description for details.
5056 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
5057 const struct lu_buf *buf, loff_t *pos,
5058 struct thandle *th, int iq)
5060 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
5061 S_ISLNK(dt->do_lu.lo_header->loh_attr));
5062 return lod_sub_write(env, dt_object_child(dt), buf, pos, th, iq);
5065 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
5066 __u64 start, __u64 end, struct thandle *th)
5068 if (dt_object_remote(dt))
5071 return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
5074 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
5075 __u64 start, __u64 end, struct thandle *th)
5077 if (dt_object_remote(dt))
5080 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
5081 return lod_sub_punch(env, dt_object_child(dt), start, end, th);
5085 * different type of files use the same body_ops because object may be created
5086 * in OUT, where there is no chance to set correct body_ops for each type, so
5087 * body_ops themselves will check file type inside, see lod_read/write/punch for
5090 const struct dt_body_operations lod_body_ops = {
5091 .dbo_read = lod_read,
5092 .dbo_declare_write = lod_declare_write,
5093 .dbo_write = lod_write,
5094 .dbo_declare_punch = lod_declare_punch,
5095 .dbo_punch = lod_punch,
5099 * Implementation of lu_object_operations::loo_object_init.
5101 * The function determines the type and the index of the target device using
5102 * sequence of the object's FID. Then passes control down to the
5103 * corresponding device:
5104 * OSD for the local objects, OSP for remote
5106 * \see lu_object_operations::loo_object_init() in the API description
5109 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
5110 const struct lu_object_conf *conf)
5112 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
5113 struct lu_device *cdev = NULL;
5114 struct lu_object *cobj;
5115 struct lod_tgt_descs *ltd = NULL;
5116 struct lod_tgt_desc *tgt;
5118 int type = LU_SEQ_RANGE_ANY;
5122 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
5124 /* Note: Sometimes, it will Return EAGAIN here, see
5125 * ptrlpc_import_delay_req(), which might confuse
5126 * lu_object_find_at() and make it wait there incorrectly.
5127 * so we convert it to EIO here.*/
5134 if (type == LU_SEQ_RANGE_MDT &&
5135 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
5136 cdev = &lod->lod_child->dd_lu_dev;
5137 } else if (type == LU_SEQ_RANGE_MDT) {
5138 ltd = &lod->lod_mdt_descs;
5140 } else if (type == LU_SEQ_RANGE_OST) {
5141 ltd = &lod->lod_ost_descs;
5148 if (ltd->ltd_tgts_size > idx &&
5149 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
5150 tgt = LTD_TGT(ltd, idx);
5152 LASSERT(tgt != NULL);
5153 LASSERT(tgt->ltd_tgt != NULL);
5155 cdev = &(tgt->ltd_tgt->dd_lu_dev);
5157 lod_putref(lod, ltd);
5160 if (unlikely(cdev == NULL))
5163 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
5164 if (unlikely(cobj == NULL))
5167 lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
5169 lu_object_add(lo, cobj);
5176 * Release resources associated with striping.
5178 * If the object is striped (regular or directory), then release
5179 * the stripe objects references and free the ldo_stripe array.
5181 * \param[in] env execution environment
5182 * \param[in] lo object
5184 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
5186 struct lod_layout_component *lod_comp;
5189 if (lo->ldo_stripe != NULL) {
5190 LASSERT(lo->ldo_comp_entries == NULL);
5191 LASSERT(lo->ldo_dir_stripes_allocated > 0);
5193 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5194 if (lo->ldo_stripe[i])
5195 dt_object_put(env, lo->ldo_stripe[i]);
5198 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
5199 OBD_FREE(lo->ldo_stripe, j);
5200 lo->ldo_stripe = NULL;
5201 lo->ldo_dir_stripes_allocated = 0;
5202 lo->ldo_dir_stripe_loaded = 0;
5203 lo->ldo_dir_stripe_count = 0;
5204 } else if (lo->ldo_comp_entries != NULL) {
5205 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5206 /* free lod_layout_component::llc_stripe array */
5207 lod_comp = &lo->ldo_comp_entries[i];
5209 if (lod_comp->llc_stripe == NULL)
5211 LASSERT(lod_comp->llc_stripes_allocated != 0);
5212 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
5213 if (lod_comp->llc_stripe[j] != NULL)
5215 &lod_comp->llc_stripe[j]->do_lu);
5217 OBD_FREE(lod_comp->llc_stripe,
5218 sizeof(struct dt_object *) *
5219 lod_comp->llc_stripes_allocated);
5220 lod_comp->llc_stripe = NULL;
5221 lod_comp->llc_stripes_allocated = 0;
5223 lod_free_comp_entries(lo);
5224 lo->ldo_comp_cached = 0;
5229 * Implementation of lu_object_operations::loo_object_free.
5231 * \see lu_object_operations::loo_object_free() in the API description
5234 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
5236 struct lod_object *lo = lu2lod_obj(o);
5238 /* release all underlying object pinned */
5239 lod_object_free_striping(env, lo);
5241 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
5245 * Implementation of lu_object_operations::loo_object_release.
5247 * \see lu_object_operations::loo_object_release() in the API description
5250 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
5252 /* XXX: shouldn't we release everything here in case if object
5253 * creation failed before? */
5257 * Implementation of lu_object_operations::loo_object_print.
5259 * \see lu_object_operations::loo_object_print() in the API description
5262 static int lod_object_print(const struct lu_env *env, void *cookie,
5263 lu_printer_t p, const struct lu_object *l)
5265 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
5267 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
5270 struct lu_object_operations lod_lu_obj_ops = {
5271 .loo_object_init = lod_object_init,
5272 .loo_object_free = lod_object_free,
5273 .loo_object_release = lod_object_release,
5274 .loo_object_print = lod_object_print,