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 <lustre_param.h>
51 #include <lustre_swab.h>
52 #include <lustre_ver.h>
53 #include <lprocfs_status.h>
54 #include <md_object.h>
56 #include "lod_internal.h"
58 static const char dot[] = ".";
59 static const char dotdot[] = "..";
61 static const struct dt_body_operations lod_body_lnk_ops;
62 static const struct dt_body_operations lod_body_ops;
65 * Implementation of dt_index_operations::dio_lookup
67 * Used with regular (non-striped) objects.
69 * \see dt_index_operations::dio_lookup() in the API description for details.
71 static int lod_index_lookup(const struct lu_env *env, struct dt_object *dt,
72 struct dt_rec *rec, const struct dt_key *key)
74 struct dt_object *next = dt_object_child(dt);
75 return next->do_index_ops->dio_lookup(env, next, rec, key);
79 * Implementation of dt_index_operations::dio_declare_insert.
81 * Used with regular (non-striped) objects.
83 * \see dt_index_operations::dio_declare_insert() in the API description
86 static int lod_declare_index_insert(const struct lu_env *env,
88 const struct dt_rec *rec,
89 const struct dt_key *key,
92 return lod_sub_object_declare_insert(env, dt_object_child(dt),
97 * Implementation of dt_index_operations::dio_insert.
99 * Used with regular (non-striped) objects
101 * \see dt_index_operations::dio_insert() in the API description for details.
103 static int lod_index_insert(const struct lu_env *env,
104 struct dt_object *dt,
105 const struct dt_rec *rec,
106 const struct dt_key *key,
110 return lod_sub_object_index_insert(env, dt_object_child(dt), rec, key,
115 * Implementation of dt_index_operations::dio_declare_delete.
117 * Used with regular (non-striped) objects.
119 * \see dt_index_operations::dio_declare_delete() in the API description
122 static int lod_declare_index_delete(const struct lu_env *env,
123 struct dt_object *dt,
124 const struct dt_key *key,
127 return lod_sub_object_declare_delete(env, dt_object_child(dt), key,
132 * Implementation of dt_index_operations::dio_delete.
134 * Used with regular (non-striped) objects.
136 * \see dt_index_operations::dio_delete() in the API description for details.
138 static int lod_index_delete(const struct lu_env *env,
139 struct dt_object *dt,
140 const struct dt_key *key,
143 return lod_sub_object_delete(env, dt_object_child(dt), key, th);
147 * Implementation of dt_it_ops::init.
149 * Used with regular (non-striped) objects.
151 * \see dt_it_ops::init() in the API description for details.
153 static struct dt_it *lod_it_init(const struct lu_env *env,
154 struct dt_object *dt, __u32 attr)
156 struct dt_object *next = dt_object_child(dt);
157 struct lod_it *it = &lod_env_info(env)->lti_it;
158 struct dt_it *it_next;
160 it_next = next->do_index_ops->dio_it.init(env, next, attr);
164 /* currently we do not use more than one iterator per thread
165 * so we store it in thread info. if at some point we need
166 * more active iterators in a single thread, we can allocate
168 LASSERT(it->lit_obj == NULL);
170 it->lit_it = it_next;
173 return (struct dt_it *)it;
176 #define LOD_CHECK_IT(env, it) \
178 LASSERT((it)->lit_obj != NULL); \
179 LASSERT((it)->lit_it != NULL); \
183 * Implementation of dt_index_operations::dio_it.fini.
185 * Used with regular (non-striped) objects.
187 * \see dt_index_operations::dio_it.fini() in the API description for details.
189 static void lod_it_fini(const struct lu_env *env, struct dt_it *di)
191 struct lod_it *it = (struct lod_it *)di;
193 LOD_CHECK_IT(env, it);
194 it->lit_obj->do_index_ops->dio_it.fini(env, it->lit_it);
196 /* the iterator not in use any more */
202 * Implementation of dt_it_ops::get.
204 * Used with regular (non-striped) objects.
206 * \see dt_it_ops::get() in the API description for details.
208 static int lod_it_get(const struct lu_env *env, struct dt_it *di,
209 const struct dt_key *key)
211 const struct lod_it *it = (const struct lod_it *)di;
213 LOD_CHECK_IT(env, it);
214 return it->lit_obj->do_index_ops->dio_it.get(env, it->lit_it, key);
218 * Implementation of dt_it_ops::put.
220 * Used with regular (non-striped) objects.
222 * \see dt_it_ops::put() in the API description for details.
224 static void lod_it_put(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.put(env, it->lit_it);
233 * Implementation of dt_it_ops::next.
235 * Used with regular (non-striped) objects
237 * \see dt_it_ops::next() in the API description for details.
239 static int lod_it_next(const struct lu_env *env, struct dt_it *di)
241 struct lod_it *it = (struct lod_it *)di;
243 LOD_CHECK_IT(env, it);
244 return it->lit_obj->do_index_ops->dio_it.next(env, it->lit_it);
248 * Implementation of dt_it_ops::key.
250 * Used with regular (non-striped) objects.
252 * \see dt_it_ops::key() in the API description for details.
254 static struct dt_key *lod_it_key(const struct lu_env *env,
255 const struct dt_it *di)
257 const struct lod_it *it = (const struct lod_it *)di;
259 LOD_CHECK_IT(env, it);
260 return it->lit_obj->do_index_ops->dio_it.key(env, it->lit_it);
264 * Implementation of dt_it_ops::key_size.
266 * Used with regular (non-striped) objects.
268 * \see dt_it_ops::key_size() in the API description for details.
270 static int lod_it_key_size(const struct lu_env *env, const struct dt_it *di)
272 struct lod_it *it = (struct lod_it *)di;
274 LOD_CHECK_IT(env, it);
275 return it->lit_obj->do_index_ops->dio_it.key_size(env, it->lit_it);
279 * Implementation of dt_it_ops::rec.
281 * Used with regular (non-striped) objects.
283 * \see dt_it_ops::rec() in the API description for details.
285 static int lod_it_rec(const struct lu_env *env, const struct dt_it *di,
286 struct dt_rec *rec, __u32 attr)
288 const struct lod_it *it = (const struct lod_it *)di;
290 LOD_CHECK_IT(env, it);
291 return it->lit_obj->do_index_ops->dio_it.rec(env, it->lit_it, rec,
296 * Implementation of dt_it_ops::rec_size.
298 * Used with regular (non-striped) objects.
300 * \see dt_it_ops::rec_size() in the API description for details.
302 static int lod_it_rec_size(const struct lu_env *env, const struct dt_it *di,
305 const struct lod_it *it = (const struct lod_it *)di;
307 LOD_CHECK_IT(env, it);
308 return it->lit_obj->do_index_ops->dio_it.rec_size(env, it->lit_it,
313 * Implementation of dt_it_ops::store.
315 * Used with regular (non-striped) objects.
317 * \see dt_it_ops::store() in the API description for details.
319 static __u64 lod_it_store(const struct lu_env *env, const struct dt_it *di)
321 const struct lod_it *it = (const struct lod_it *)di;
323 LOD_CHECK_IT(env, it);
324 return it->lit_obj->do_index_ops->dio_it.store(env, it->lit_it);
328 * Implementation of dt_it_ops::load.
330 * Used with regular (non-striped) objects.
332 * \see dt_it_ops::load() in the API description for details.
334 static int lod_it_load(const struct lu_env *env, const struct dt_it *di,
337 const struct lod_it *it = (const struct lod_it *)di;
339 LOD_CHECK_IT(env, it);
340 return it->lit_obj->do_index_ops->dio_it.load(env, it->lit_it, hash);
344 * Implementation of dt_it_ops::key_rec.
346 * Used with regular (non-striped) objects.
348 * \see dt_it_ops::rec() in the API description for details.
350 static int lod_it_key_rec(const struct lu_env *env, const struct dt_it *di,
353 const struct lod_it *it = (const struct lod_it *)di;
355 LOD_CHECK_IT(env, it);
356 return it->lit_obj->do_index_ops->dio_it.key_rec(env, it->lit_it,
360 static struct dt_index_operations lod_index_ops = {
361 .dio_lookup = lod_index_lookup,
362 .dio_declare_insert = lod_declare_index_insert,
363 .dio_insert = lod_index_insert,
364 .dio_declare_delete = lod_declare_index_delete,
365 .dio_delete = lod_index_delete,
373 .key_size = lod_it_key_size,
375 .rec_size = lod_it_rec_size,
376 .store = lod_it_store,
378 .key_rec = lod_it_key_rec,
383 * Implementation of dt_it_ops::init.
385 * Used with striped objects. Internally just initializes the iterator
386 * on the first stripe.
388 * \see dt_it_ops::init() in the API description for details.
390 static struct dt_it *lod_striped_it_init(const struct lu_env *env,
391 struct dt_object *dt, __u32 attr)
393 struct lod_object *lo = lod_dt_obj(dt);
394 struct dt_object *next;
395 struct lod_it *it = &lod_env_info(env)->lti_it;
396 struct dt_it *it_next;
399 LASSERT(lo->ldo_dir_stripenr > 0);
400 next = lo->ldo_stripe[0];
401 LASSERT(next != NULL);
402 LASSERT(next->do_index_ops != NULL);
404 it_next = next->do_index_ops->dio_it.init(env, next, attr);
408 /* currently we do not use more than one iterator per thread
409 * so we store it in thread info. if at some point we need
410 * more active iterators in a single thread, we can allocate
412 LASSERT(it->lit_obj == NULL);
414 it->lit_stripe_index = 0;
416 it->lit_it = it_next;
419 return (struct dt_it *)it;
422 #define LOD_CHECK_STRIPED_IT(env, it, lo) \
424 LASSERT((it)->lit_obj != NULL); \
425 LASSERT((it)->lit_it != NULL); \
426 LASSERT((lo)->ldo_dir_stripenr > 0); \
427 LASSERT((it)->lit_stripe_index < (lo)->ldo_dir_stripenr); \
431 * Implementation of dt_it_ops::fini.
433 * Used with striped objects.
435 * \see dt_it_ops::fini() in the API description for details.
437 static void lod_striped_it_fini(const struct lu_env *env, struct dt_it *di)
439 struct lod_it *it = (struct lod_it *)di;
440 struct lod_object *lo = lod_dt_obj(it->lit_obj);
441 struct dt_object *next;
443 /* If lit_it == NULL, then it means the sub_it has been finished,
444 * which only happens in failure cases, see lod_striped_it_next() */
445 if (it->lit_it != NULL) {
446 LOD_CHECK_STRIPED_IT(env, it, lo);
448 next = lo->ldo_stripe[it->lit_stripe_index];
449 LASSERT(next != NULL);
450 LASSERT(next->do_index_ops != NULL);
452 next->do_index_ops->dio_it.fini(env, it->lit_it);
455 /* the iterator not in use any more */
458 it->lit_stripe_index = 0;
462 * Implementation of dt_it_ops::get.
464 * Right now it's not used widely, only to reset the iterator to the
465 * initial position. It should be possible to implement a full version
466 * which chooses a correct stripe to be able to position with any key.
468 * \see dt_it_ops::get() in the API description for details.
470 static int lod_striped_it_get(const struct lu_env *env, struct dt_it *di,
471 const struct dt_key *key)
473 const struct lod_it *it = (const struct lod_it *)di;
474 struct lod_object *lo = lod_dt_obj(it->lit_obj);
475 struct dt_object *next;
478 LOD_CHECK_STRIPED_IT(env, it, lo);
480 next = lo->ldo_stripe[it->lit_stripe_index];
481 LASSERT(next != NULL);
482 LASSERT(next->do_index_ops != NULL);
484 return next->do_index_ops->dio_it.get(env, it->lit_it, key);
488 * Implementation of dt_it_ops::put.
490 * Used with striped objects.
492 * \see dt_it_ops::put() in the API description for details.
494 static void lod_striped_it_put(const struct lu_env *env, struct dt_it *di)
496 struct lod_it *it = (struct lod_it *)di;
497 struct lod_object *lo = lod_dt_obj(it->lit_obj);
498 struct dt_object *next;
500 LOD_CHECK_STRIPED_IT(env, it, lo);
502 next = lo->ldo_stripe[it->lit_stripe_index];
503 LASSERT(next != NULL);
504 LASSERT(next->do_index_ops != NULL);
506 return next->do_index_ops->dio_it.put(env, it->lit_it);
510 * Implementation of dt_it_ops::next.
512 * Used with striped objects. When the end of the current stripe is
513 * reached, the method takes the next stripe's iterator.
515 * \see dt_it_ops::next() in the API description for details.
517 static int lod_striped_it_next(const struct lu_env *env, struct dt_it *di)
519 struct lod_it *it = (struct lod_it *)di;
520 struct lod_object *lo = lod_dt_obj(it->lit_obj);
521 struct dt_object *next;
522 struct dt_it *it_next;
526 LOD_CHECK_STRIPED_IT(env, it, lo);
528 next = lo->ldo_stripe[it->lit_stripe_index];
529 LASSERT(next != NULL);
530 LASSERT(next->do_index_ops != NULL);
532 rc = next->do_index_ops->dio_it.next(env, it->lit_it);
536 if (rc == 0 && it->lit_stripe_index == 0)
539 if (rc == 0 && it->lit_stripe_index > 0) {
540 struct lu_dirent *ent;
542 ent = (struct lu_dirent *)lod_env_info(env)->lti_key;
544 rc = next->do_index_ops->dio_it.rec(env, it->lit_it,
545 (struct dt_rec *)ent,
550 /* skip . and .. for slave stripe */
551 if ((strncmp(ent->lde_name, ".",
552 le16_to_cpu(ent->lde_namelen)) == 0 &&
553 le16_to_cpu(ent->lde_namelen) == 1) ||
554 (strncmp(ent->lde_name, "..",
555 le16_to_cpu(ent->lde_namelen)) == 0 &&
556 le16_to_cpu(ent->lde_namelen) == 2))
562 /* go to next stripe */
563 if (it->lit_stripe_index + 1 >= lo->ldo_dir_stripenr)
566 it->lit_stripe_index++;
568 next->do_index_ops->dio_it.put(env, it->lit_it);
569 next->do_index_ops->dio_it.fini(env, it->lit_it);
572 next = lo->ldo_stripe[it->lit_stripe_index];
573 LASSERT(next != NULL);
574 rc = next->do_ops->do_index_try(env, next, &dt_directory_features);
578 LASSERT(next->do_index_ops != NULL);
580 it_next = next->do_index_ops->dio_it.init(env, next, it->lit_attr);
581 if (!IS_ERR(it_next)) {
582 it->lit_it = it_next;
585 rc = PTR_ERR(it_next);
592 * Implementation of dt_it_ops::key.
594 * Used with striped objects.
596 * \see dt_it_ops::key() in the API description for details.
598 static struct dt_key *lod_striped_it_key(const struct lu_env *env,
599 const struct dt_it *di)
601 const struct lod_it *it = (const struct lod_it *)di;
602 struct lod_object *lo = lod_dt_obj(it->lit_obj);
603 struct dt_object *next;
605 LOD_CHECK_STRIPED_IT(env, it, lo);
607 next = lo->ldo_stripe[it->lit_stripe_index];
608 LASSERT(next != NULL);
609 LASSERT(next->do_index_ops != NULL);
611 return next->do_index_ops->dio_it.key(env, it->lit_it);
615 * Implementation of dt_it_ops::key_size.
617 * Used with striped objects.
619 * \see dt_it_ops::size() in the API description for details.
621 static int lod_striped_it_key_size(const struct lu_env *env,
622 const struct dt_it *di)
624 struct lod_it *it = (struct lod_it *)di;
625 struct lod_object *lo = lod_dt_obj(it->lit_obj);
626 struct dt_object *next;
628 LOD_CHECK_STRIPED_IT(env, it, lo);
630 next = lo->ldo_stripe[it->lit_stripe_index];
631 LASSERT(next != NULL);
632 LASSERT(next->do_index_ops != NULL);
634 return next->do_index_ops->dio_it.key_size(env, it->lit_it);
638 * Implementation of dt_it_ops::rec.
640 * Used with striped objects.
642 * \see dt_it_ops::rec() in the API description for details.
644 static int lod_striped_it_rec(const struct lu_env *env, const struct dt_it *di,
645 struct dt_rec *rec, __u32 attr)
647 const struct lod_it *it = (const struct lod_it *)di;
648 struct lod_object *lo = lod_dt_obj(it->lit_obj);
649 struct dt_object *next;
651 LOD_CHECK_STRIPED_IT(env, it, lo);
653 next = lo->ldo_stripe[it->lit_stripe_index];
654 LASSERT(next != NULL);
655 LASSERT(next->do_index_ops != NULL);
657 return next->do_index_ops->dio_it.rec(env, it->lit_it, rec, attr);
661 * Implementation of dt_it_ops::rec_size.
663 * Used with striped objects.
665 * \see dt_it_ops::rec_size() in the API description for details.
667 static int lod_striped_it_rec_size(const struct lu_env *env,
668 const struct dt_it *di, __u32 attr)
670 struct lod_it *it = (struct lod_it *)di;
671 struct lod_object *lo = lod_dt_obj(it->lit_obj);
672 struct dt_object *next;
674 LOD_CHECK_STRIPED_IT(env, it, lo);
676 next = lo->ldo_stripe[it->lit_stripe_index];
677 LASSERT(next != NULL);
678 LASSERT(next->do_index_ops != NULL);
680 return next->do_index_ops->dio_it.rec_size(env, it->lit_it, attr);
684 * Implementation of dt_it_ops::store.
686 * Used with striped objects.
688 * \see dt_it_ops::store() in the API description for details.
690 static __u64 lod_striped_it_store(const struct lu_env *env,
691 const struct dt_it *di)
693 const struct lod_it *it = (const struct lod_it *)di;
694 struct lod_object *lo = lod_dt_obj(it->lit_obj);
695 struct dt_object *next;
697 LOD_CHECK_STRIPED_IT(env, it, lo);
699 next = lo->ldo_stripe[it->lit_stripe_index];
700 LASSERT(next != NULL);
701 LASSERT(next->do_index_ops != NULL);
703 return next->do_index_ops->dio_it.store(env, it->lit_it);
707 * Implementation of dt_it_ops::load.
709 * Used with striped objects.
711 * \see dt_it_ops::load() in the API description for details.
713 static int lod_striped_it_load(const struct lu_env *env,
714 const struct dt_it *di, __u64 hash)
716 const struct lod_it *it = (const struct lod_it *)di;
717 struct lod_object *lo = lod_dt_obj(it->lit_obj);
718 struct dt_object *next;
720 LOD_CHECK_STRIPED_IT(env, it, lo);
722 next = lo->ldo_stripe[it->lit_stripe_index];
723 LASSERT(next != NULL);
724 LASSERT(next->do_index_ops != NULL);
726 return next->do_index_ops->dio_it.load(env, it->lit_it, hash);
729 static struct dt_index_operations lod_striped_index_ops = {
730 .dio_lookup = lod_index_lookup,
731 .dio_declare_insert = lod_declare_index_insert,
732 .dio_insert = lod_index_insert,
733 .dio_declare_delete = lod_declare_index_delete,
734 .dio_delete = lod_index_delete,
736 .init = lod_striped_it_init,
737 .fini = lod_striped_it_fini,
738 .get = lod_striped_it_get,
739 .put = lod_striped_it_put,
740 .next = lod_striped_it_next,
741 .key = lod_striped_it_key,
742 .key_size = lod_striped_it_key_size,
743 .rec = lod_striped_it_rec,
744 .rec_size = lod_striped_it_rec_size,
745 .store = lod_striped_it_store,
746 .load = lod_striped_it_load,
751 * Append the FID for each shard of the striped directory after the
752 * given LMV EA header.
754 * To simplify striped directory and the consistency verification,
755 * we only store the LMV EA header on disk, for both master object
756 * and slave objects. When someone wants to know the whole LMV EA,
757 * such as client readdir(), we can build the entrie LMV EA on the
758 * MDT side (in RAM) via iterating the sub-directory entries that
759 * are contained in the master object of the stripe directory.
761 * For the master object of the striped directroy, the valid name
762 * for each shard is composed of the ${shard_FID}:${shard_idx}.
764 * There may be holes in the LMV EA if some shards' name entries
765 * are corrupted or lost.
767 * \param[in] env pointer to the thread context
768 * \param[in] lo pointer to the master object of the striped directory
769 * \param[in] buf pointer to the lu_buf which will hold the LMV EA
770 * \param[in] resize whether re-allocate the buffer if it is not big enough
772 * \retval positive size of the LMV EA
773 * \retval 0 for nothing to be loaded
774 * \retval negative error number on failure
776 int lod_load_lmv_shards(const struct lu_env *env, struct lod_object *lo,
777 struct lu_buf *buf, bool resize)
779 struct lu_dirent *ent =
780 (struct lu_dirent *)lod_env_info(env)->lti_key;
781 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
782 struct dt_object *obj = dt_object_child(&lo->ldo_obj);
783 struct lmv_mds_md_v1 *lmv1 = buf->lb_buf;
785 const struct dt_it_ops *iops;
787 __u32 magic = le32_to_cpu(lmv1->lmv_magic);
792 /* If it is not a striped directory, then load nothing. */
793 if (magic != LMV_MAGIC_V1)
796 /* If it is in migration (or failure), then load nothing. */
797 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
800 stripes = le32_to_cpu(lmv1->lmv_stripe_count);
804 rc = lmv_mds_md_size(stripes, magic);
808 if (buf->lb_len < lmv1_size) {
817 lu_buf_alloc(buf, lmv1_size);
822 memcpy(buf->lb_buf, tbuf.lb_buf, tbuf.lb_len);
825 if (unlikely(!dt_try_as_dir(env, obj)))
828 memset(&lmv1->lmv_stripe_fids[0], 0, stripes * sizeof(struct lu_fid));
829 iops = &obj->do_index_ops->dio_it;
830 it = iops->init(env, obj, LUDA_64BITHASH);
834 rc = iops->load(env, it, 0);
836 rc = iops->next(env, it);
841 char name[FID_LEN + 2] = "";
846 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
852 fid_le_to_cpu(&fid, &ent->lde_fid);
853 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
854 if (ent->lde_name[0] == '.') {
855 if (ent->lde_namelen == 1)
858 if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
862 len = snprintf(name, sizeof(name),
863 DFID":", PFID(&ent->lde_fid));
864 /* The ent->lde_name is composed of ${FID}:${index} */
865 if (ent->lde_namelen < len + 1 ||
866 memcmp(ent->lde_name, name, len) != 0) {
867 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
868 "%s: invalid shard name %.*s with the FID "DFID
869 " for the striped directory "DFID", %s\n",
870 lod2obd(lod)->obd_name, ent->lde_namelen,
871 ent->lde_name, PFID(&fid),
872 PFID(lu_object_fid(&obj->do_lu)),
873 lod->lod_lmv_failout ? "failout" : "skip");
875 if (lod->lod_lmv_failout)
883 if (ent->lde_name[len] < '0' ||
884 ent->lde_name[len] > '9') {
885 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
886 "%s: invalid shard name %.*s with the "
887 "FID "DFID" for the striped directory "
889 lod2obd(lod)->obd_name, ent->lde_namelen,
890 ent->lde_name, PFID(&fid),
891 PFID(lu_object_fid(&obj->do_lu)),
892 lod->lod_lmv_failout ?
895 if (lod->lod_lmv_failout)
901 index = index * 10 + ent->lde_name[len++] - '0';
902 } while (len < ent->lde_namelen);
904 if (len == ent->lde_namelen) {
905 /* Out of LMV EA range. */
906 if (index >= stripes) {
907 CERROR("%s: the shard %.*s for the striped "
908 "directory "DFID" is out of the known "
909 "LMV EA range [0 - %u], failout\n",
910 lod2obd(lod)->obd_name, ent->lde_namelen,
912 PFID(lu_object_fid(&obj->do_lu)),
918 /* The slot has been occupied. */
919 if (!fid_is_zero(&lmv1->lmv_stripe_fids[index])) {
923 &lmv1->lmv_stripe_fids[index]);
924 CERROR("%s: both the shard "DFID" and "DFID
925 " for the striped directory "DFID
926 " claim the same LMV EA slot at the "
927 "index %d, failout\n",
928 lod2obd(lod)->obd_name,
929 PFID(&fid0), PFID(&fid),
930 PFID(lu_object_fid(&obj->do_lu)), index);
935 /* stored as LE mode */
936 lmv1->lmv_stripe_fids[index] = ent->lde_fid;
939 rc = iops->next(env, it);
946 RETURN(rc > 0 ? lmv_mds_md_size(stripes, magic) : rc);
950 * Implementation of dt_object_operations::do_index_try.
952 * \see dt_object_operations::do_index_try() in the API description for details.
954 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
955 const struct dt_index_features *feat)
957 struct lod_object *lo = lod_dt_obj(dt);
958 struct dt_object *next = dt_object_child(dt);
962 LASSERT(next->do_ops);
963 LASSERT(next->do_ops->do_index_try);
965 rc = lod_load_striping_locked(env, lo);
969 rc = next->do_ops->do_index_try(env, next, feat);
973 if (lo->ldo_dir_stripenr > 0) {
976 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
977 if (dt_object_exists(lo->ldo_stripe[i]) == 0)
979 rc = lo->ldo_stripe[i]->do_ops->do_index_try(env,
980 lo->ldo_stripe[i], feat);
984 dt->do_index_ops = &lod_striped_index_ops;
986 dt->do_index_ops = &lod_index_ops;
993 * Implementation of dt_object_operations::do_read_lock.
995 * \see dt_object_operations::do_read_lock() in the API description for details.
997 static void lod_object_read_lock(const struct lu_env *env,
998 struct dt_object *dt, unsigned role)
1000 dt_read_lock(env, dt_object_child(dt), role);
1004 * Implementation of dt_object_operations::do_write_lock.
1006 * \see dt_object_operations::do_write_lock() in the API description for
1009 static void lod_object_write_lock(const struct lu_env *env,
1010 struct dt_object *dt, unsigned role)
1012 dt_write_lock(env, dt_object_child(dt), role);
1016 * Implementation of dt_object_operations::do_read_unlock.
1018 * \see dt_object_operations::do_read_unlock() in the API description for
1021 static void lod_object_read_unlock(const struct lu_env *env,
1022 struct dt_object *dt)
1024 dt_read_unlock(env, dt_object_child(dt));
1028 * Implementation of dt_object_operations::do_write_unlock.
1030 * \see dt_object_operations::do_write_unlock() in the API description for
1033 static void lod_object_write_unlock(const struct lu_env *env,
1034 struct dt_object *dt)
1036 dt_write_unlock(env, dt_object_child(dt));
1040 * Implementation of dt_object_operations::do_write_locked.
1042 * \see dt_object_operations::do_write_locked() in the API description for
1045 static int lod_object_write_locked(const struct lu_env *env,
1046 struct dt_object *dt)
1048 return dt_write_locked(env, dt_object_child(dt));
1052 * Implementation of dt_object_operations::do_attr_get.
1054 * \see dt_object_operations::do_attr_get() in the API description for details.
1056 static int lod_attr_get(const struct lu_env *env,
1057 struct dt_object *dt,
1058 struct lu_attr *attr)
1060 /* Note: for striped directory, client will merge attributes
1061 * from all of the sub-stripes see lmv_merge_attr(), and there
1062 * no MDD logic depend on directory nlink/size/time, so we can
1063 * always use master inode nlink and size for now. */
1064 return dt_attr_get(env, dt_object_child(dt), attr);
1067 int lod_obj_for_each_stripe(const struct lu_env *env, struct lod_object *lo,
1068 struct thandle *th, lod_obj_stripe_cb_t cb,
1069 struct lod_obj_stripe_cb_data *data)
1071 struct lod_layout_component *lod_comp;
1075 LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
1076 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1077 lod_comp = &lo->ldo_comp_entries[i];
1079 if (lod_comp->llc_stripe == NULL)
1082 LASSERT(lod_comp->llc_stripenr > 0);
1083 for (j = 0; j < lod_comp->llc_stripenr; j++) {
1084 struct dt_object *dt = lod_comp->llc_stripe[j];
1088 rc = cb(env, lo, dt, th, j, data);
1097 lod_obj_stripe_attr_set_cb(const struct lu_env *env, struct lod_object *lo,
1098 struct dt_object *dt, struct thandle *th,
1099 int stripe_idx, struct lod_obj_stripe_cb_data *data)
1101 if (data->locd_declare)
1102 return lod_sub_object_declare_attr_set(env, dt,
1103 data->locd_attr, th);
1105 return lod_sub_object_attr_set(env, dt, data->locd_attr, th);
1109 * Implementation of dt_object_operations::do_declare_attr_set.
1111 * If the object is striped, then apply the changes to all the stripes.
1113 * \see dt_object_operations::do_declare_attr_set() in the API description
1116 static int lod_declare_attr_set(const struct lu_env *env,
1117 struct dt_object *dt,
1118 const struct lu_attr *attr,
1121 struct dt_object *next = dt_object_child(dt);
1122 struct lod_object *lo = lod_dt_obj(dt);
1127 * declare setattr on the local object
1129 rc = lod_sub_object_declare_attr_set(env, next, attr, th);
1133 /* osp_declare_attr_set() ignores all attributes other than
1134 * UID, GID, and size, and osp_attr_set() ignores all but UID
1135 * and GID. Declaration of size attr setting happens through
1136 * lod_declare_init_size(), and not through this function.
1137 * Therefore we need not load striping unless ownership is
1138 * changing. This should save memory and (we hope) speed up
1140 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1141 if (!(attr->la_valid & (LA_UID | LA_GID)))
1144 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1147 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE |
1148 LA_ATIME | LA_MTIME | LA_CTIME |
1153 * load striping information, notice we don't do this when object
1154 * is being initialized as we don't need this information till
1155 * few specific cases like destroy, chown
1157 rc = lod_load_striping(env, lo);
1161 if (!lod_obj_is_striped(dt))
1165 * if object is striped declare changes on the stripes
1167 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1168 LASSERT(lo->ldo_stripe);
1169 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
1170 if (lo->ldo_stripe[i] == NULL)
1172 rc = lod_sub_object_declare_attr_set(env,
1173 lo->ldo_stripe[i], attr,
1179 struct lod_obj_stripe_cb_data data;
1181 data.locd_attr = attr;
1182 data.locd_declare = true;
1183 rc = lod_obj_for_each_stripe(env, lo, th,
1184 lod_obj_stripe_attr_set_cb, &data);
1187 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE) &&
1188 dt_object_exists(next) != 0 &&
1189 dt_object_remote(next) == 0)
1190 lod_sub_object_declare_xattr_del(env, next,
1191 XATTR_NAME_LOV, th);
1193 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) &&
1194 dt_object_exists(next) &&
1195 dt_object_remote(next) == 0 && S_ISREG(attr->la_mode)) {
1196 struct lod_thread_info *info = lod_env_info(env);
1197 struct lu_buf *buf = &info->lti_buf;
1199 buf->lb_buf = info->lti_ea_store;
1200 buf->lb_len = info->lti_ea_store_size;
1201 lod_sub_object_declare_xattr_set(env, next, buf,
1203 LU_XATTR_REPLACE, th);
1210 * Implementation of dt_object_operations::do_attr_set.
1212 * If the object is striped, then apply the changes to all or subset of
1213 * the stripes depending on the object type and specific attributes.
1215 * \see dt_object_operations::do_attr_set() in the API description for details.
1217 static int lod_attr_set(const struct lu_env *env,
1218 struct dt_object *dt,
1219 const struct lu_attr *attr,
1222 struct dt_object *next = dt_object_child(dt);
1223 struct lod_object *lo = lod_dt_obj(dt);
1228 * apply changes to the local object
1230 rc = lod_sub_object_attr_set(env, next, attr, th);
1234 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1235 if (!(attr->la_valid & (LA_UID | LA_GID)))
1238 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1241 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE |
1242 LA_ATIME | LA_MTIME | LA_CTIME |
1247 if (!lod_obj_is_striped(dt))
1251 * if object is striped, apply changes to all the stripes
1253 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1254 LASSERT(lo->ldo_stripe);
1255 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
1256 if (unlikely(lo->ldo_stripe[i] == NULL))
1259 if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1262 rc = lod_sub_object_attr_set(env, lo->ldo_stripe[i],
1268 struct lod_obj_stripe_cb_data data;
1270 data.locd_attr = attr;
1271 data.locd_declare = false;
1272 rc = lod_obj_for_each_stripe(env, lo, th,
1273 lod_obj_stripe_attr_set_cb, &data);
1276 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE) &&
1277 dt_object_exists(next) != 0 &&
1278 dt_object_remote(next) == 0)
1279 rc = lod_sub_object_xattr_del(env, next, XATTR_NAME_LOV, th);
1281 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) &&
1282 dt_object_exists(next) &&
1283 dt_object_remote(next) == 0 && S_ISREG(attr->la_mode)) {
1284 struct lod_thread_info *info = lod_env_info(env);
1285 struct lu_buf *buf = &info->lti_buf;
1286 struct ost_id *oi = &info->lti_ostid;
1287 struct lu_fid *fid = &info->lti_fid;
1288 struct lov_mds_md_v1 *lmm;
1289 struct lov_ost_data_v1 *objs;
1293 rc1 = lod_get_lov_ea(env, lo);
1297 buf->lb_buf = info->lti_ea_store;
1298 buf->lb_len = info->lti_ea_store_size;
1299 lmm = info->lti_ea_store;
1300 magic = le32_to_cpu(lmm->lmm_magic);
1301 if (magic == LOV_MAGIC_V1)
1302 objs = &(lmm->lmm_objects[0]);
1304 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1305 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1306 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1308 fid_to_ostid(fid, oi);
1309 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1311 rc = lod_sub_object_xattr_set(env, next, buf, XATTR_NAME_LOV,
1312 LU_XATTR_REPLACE, th);
1319 * Implementation of dt_object_operations::do_xattr_get.
1321 * If LOV EA is requested from the root object and it's not
1322 * found, then return default striping for the filesystem.
1324 * \see dt_object_operations::do_xattr_get() in the API description for details.
1326 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1327 struct lu_buf *buf, const char *name)
1329 struct lod_thread_info *info = lod_env_info(env);
1330 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1335 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1336 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1337 struct lmv_mds_md_v1 *lmv1;
1340 if (rc > (typeof(rc))sizeof(*lmv1))
1343 if (rc < (typeof(rc))sizeof(*lmv1))
1344 RETURN(rc = rc > 0 ? -EINVAL : rc);
1346 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1347 CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1349 info->lti_buf.lb_buf = info->lti_key;
1350 info->lti_buf.lb_len = sizeof(*lmv1);
1351 rc = dt_xattr_get(env, dt_object_child(dt),
1352 &info->lti_buf, name);
1353 if (unlikely(rc != sizeof(*lmv1)))
1354 RETURN(rc = rc > 0 ? -EINVAL : rc);
1356 lmv1 = info->lti_buf.lb_buf;
1357 /* The on-disk LMV EA only contains header, but the
1358 * returned LMV EA size should contain the space for
1359 * the FIDs of all shards of the striped directory. */
1360 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1361 rc = lmv_mds_md_size(
1362 le32_to_cpu(lmv1->lmv_stripe_count),
1365 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1369 RETURN(rc = rc1 != 0 ? rc1 : rc);
1372 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1376 * XXX: Only used by lfsck
1378 * lod returns default striping on the real root of the device
1379 * this is like the root stores default striping for the whole
1380 * filesystem. historically we've been using a different approach
1381 * and store it in the config.
1383 dt_root_get(env, dev->lod_child, &info->lti_fid);
1384 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1386 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1387 struct lov_user_md *lum = buf->lb_buf;
1388 struct lov_desc *desc = &dev->lod_desc;
1390 if (buf->lb_buf == NULL) {
1392 } else if (buf->lb_len >= sizeof(*lum)) {
1393 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1394 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1395 lmm_oi_set_id(&lum->lmm_oi, 0);
1396 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1397 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1398 lum->lmm_stripe_size = cpu_to_le32(
1399 desc->ld_default_stripe_size);
1400 lum->lmm_stripe_count = cpu_to_le16(
1401 desc->ld_default_stripe_count);
1402 lum->lmm_stripe_offset = cpu_to_le16(
1403 desc->ld_default_stripe_offset);
1416 * Checks that the magic of the stripe is sane.
1418 * \param[in] lod lod device
1419 * \param[in] lum a buffer storing LMV EA to verify
1421 * \retval 0 if the EA is sane
1422 * \retval negative otherwise
1424 static int lod_verify_md_striping(struct lod_device *lod,
1425 const struct lmv_user_md_v1 *lum)
1427 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1428 CERROR("%s: invalid lmv_user_md: magic = %x, "
1429 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1430 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1431 (int)le32_to_cpu(lum->lum_stripe_offset),
1432 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1440 * Initialize LMV EA for a slave.
1442 * Initialize slave's LMV EA from the master's LMV EA.
1444 * \param[in] master_lmv a buffer containing master's EA
1445 * \param[out] slave_lmv a buffer where slave's EA will be stored
1448 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1449 const struct lmv_mds_md_v1 *master_lmv)
1451 *slave_lmv = *master_lmv;
1452 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1458 * Generate LMV EA from the object passed as \a dt. The object must have
1459 * the stripes created and initialized.
1461 * \param[in] env execution environment
1462 * \param[in] dt object
1463 * \param[out] lmv_buf buffer storing generated LMV EA
1465 * \retval 0 on success
1466 * \retval negative if failed
1468 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1469 struct lu_buf *lmv_buf)
1471 struct lod_thread_info *info = lod_env_info(env);
1472 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1473 struct lod_object *lo = lod_dt_obj(dt);
1474 struct lmv_mds_md_v1 *lmm1;
1476 int type = LU_SEQ_RANGE_ANY;
1481 LASSERT(lo->ldo_dir_striped != 0);
1482 LASSERT(lo->ldo_dir_stripenr > 0);
1483 stripe_count = lo->ldo_dir_stripenr;
1484 /* Only store the LMV EA heahder on the disk. */
1485 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1486 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1490 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1493 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1494 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1495 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1496 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1497 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1502 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1503 lmv_buf->lb_buf = info->lti_ea_store;
1504 lmv_buf->lb_len = sizeof(*lmm1);
1510 * Create in-core represenation for a striped directory.
1512 * Parse the buffer containing LMV EA and instantiate LU objects
1513 * representing the stripe objects. The pointers to the objects are
1514 * stored in ldo_stripe field of \a lo. This function is used when
1515 * we need to access an already created object (i.e. load from a disk).
1517 * \param[in] env execution environment
1518 * \param[in] lo lod object
1519 * \param[in] buf buffer containing LMV EA
1521 * \retval 0 on success
1522 * \retval negative if failed
1524 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1525 const struct lu_buf *buf)
1527 struct lod_thread_info *info = lod_env_info(env);
1528 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1529 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1530 struct dt_object **stripe;
1531 union lmv_mds_md *lmm = buf->lb_buf;
1532 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1533 struct lu_fid *fid = &info->lti_fid;
1538 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1541 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1542 lo->ldo_dir_slave_stripe = 1;
1546 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1549 if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1552 LASSERT(lo->ldo_stripe == NULL);
1553 OBD_ALLOC(stripe, sizeof(stripe[0]) *
1554 (le32_to_cpu(lmv1->lmv_stripe_count)));
1558 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1559 struct dt_device *tgt_dt;
1560 struct dt_object *dto;
1561 int type = LU_SEQ_RANGE_ANY;
1564 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1565 if (!fid_is_sane(fid))
1566 GOTO(out, rc = -ESTALE);
1568 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1572 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1573 tgt_dt = lod->lod_child;
1575 struct lod_tgt_desc *tgt;
1577 tgt = LTD_TGT(ltd, idx);
1579 GOTO(out, rc = -ESTALE);
1580 tgt_dt = tgt->ltd_tgt;
1583 dto = dt_locate_at(env, tgt_dt, fid,
1584 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1587 GOTO(out, rc = PTR_ERR(dto));
1592 lo->ldo_stripe = stripe;
1593 lo->ldo_dir_stripenr = le32_to_cpu(lmv1->lmv_stripe_count);
1594 lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1596 lod_object_free_striping(env, lo);
1602 * Declare create a striped directory.
1604 * Declare creating a striped directory with a given stripe pattern on the
1605 * specified MDTs. A striped directory is represented as a regular directory
1606 * - an index listing all the stripes. The stripes point back to the master
1607 * object with ".." and LinkEA. The master object gets LMV EA which
1608 * identifies it as a striped directory. The function allocates FIDs
1611 * \param[in] env execution environment
1612 * \param[in] dt object
1613 * \param[in] attr attributes to initialize the objects with
1614 * \param[in] dof type of objects to be created
1615 * \param[in] th transaction handle
1617 * \retval 0 on success
1618 * \retval negative if failed
1620 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1621 struct dt_object *dt,
1622 struct lu_attr *attr,
1623 struct dt_object_format *dof,
1626 struct lod_thread_info *info = lod_env_info(env);
1627 struct lu_buf lmv_buf;
1628 struct lu_buf slave_lmv_buf;
1629 struct lmv_mds_md_v1 *lmm;
1630 struct lmv_mds_md_v1 *slave_lmm = NULL;
1631 struct dt_insert_rec *rec = &info->lti_dt_rec;
1632 struct lod_object *lo = lod_dt_obj(dt);
1637 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1640 lmm = lmv_buf.lb_buf;
1642 OBD_ALLOC_PTR(slave_lmm);
1643 if (slave_lmm == NULL)
1644 GOTO(out, rc = -ENOMEM);
1646 lod_prep_slave_lmv_md(slave_lmm, lmm);
1647 slave_lmv_buf.lb_buf = slave_lmm;
1648 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1650 if (!dt_try_as_dir(env, dt_object_child(dt)))
1651 GOTO(out, rc = -EINVAL);
1653 rec->rec_type = S_IFDIR;
1654 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
1655 struct dt_object *dto = lo->ldo_stripe[i];
1656 char *stripe_name = info->lti_key;
1657 struct lu_name *sname;
1658 struct linkea_data ldata = { NULL };
1659 struct lu_buf linkea_buf;
1661 rc = lod_sub_object_declare_create(env, dto, attr, NULL,
1666 if (!dt_try_as_dir(env, dto))
1667 GOTO(out, rc = -EINVAL);
1669 rc = lod_sub_object_declare_ref_add(env, dto, th);
1673 rec->rec_fid = lu_object_fid(&dto->do_lu);
1674 rc = lod_sub_object_declare_insert(env, dto,
1675 (const struct dt_rec *)rec,
1676 (const struct dt_key *)dot, th);
1680 /* master stripe FID will be put to .. */
1681 rec->rec_fid = lu_object_fid(&dt->do_lu);
1682 rc = lod_sub_object_declare_insert(env, dto,
1683 (const struct dt_rec *)rec,
1684 (const struct dt_key *)dotdot,
1689 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1690 cfs_fail_val != i) {
1691 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1693 slave_lmm->lmv_master_mdt_index =
1696 slave_lmm->lmv_master_mdt_index =
1698 rc = lod_sub_object_declare_xattr_set(env, dto,
1699 &slave_lmv_buf, XATTR_NAME_LMV, 0, th);
1704 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1706 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1707 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1709 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1710 PFID(lu_object_fid(&dto->do_lu)), i);
1712 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1713 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1714 sname, lu_object_fid(&dt->do_lu));
1718 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1719 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1720 rc = lod_sub_object_declare_xattr_set(env, dto, &linkea_buf,
1721 XATTR_NAME_LINK, 0, th);
1725 rec->rec_fid = lu_object_fid(&dto->do_lu);
1726 rc = lod_sub_object_declare_insert(env, dt_object_child(dt),
1727 (const struct dt_rec *)rec,
1728 (const struct dt_key *)stripe_name,
1733 rc = lod_sub_object_declare_ref_add(env, dt_object_child(dt),
1739 rc = lod_sub_object_declare_xattr_set(env, dt_object_child(dt),
1740 &lmv_buf, XATTR_NAME_LMV, 0, th);
1744 if (slave_lmm != NULL)
1745 OBD_FREE_PTR(slave_lmm);
1750 static int lod_prep_md_striped_create(const struct lu_env *env,
1751 struct dt_object *dt,
1752 struct lu_attr *attr,
1753 const struct lmv_user_md_v1 *lum,
1754 struct dt_object_format *dof,
1757 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1758 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1759 struct lod_object *lo = lod_dt_obj(dt);
1760 struct dt_object **stripe;
1769 /* The lum has been verifed in lod_verify_md_striping */
1770 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
1771 LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1773 stripe_count = le32_to_cpu(lum->lum_stripe_count);
1775 OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1776 if (idx_array == NULL)
1779 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1781 GOTO(out_free, rc = -ENOMEM);
1783 /* Start index will be the master MDT */
1784 master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1785 idx_array[0] = master_index;
1786 for (i = 0; i < stripe_count; i++) {
1787 struct lod_tgt_desc *tgt = NULL;
1788 struct dt_object *dto;
1789 struct lu_fid fid = { 0 };
1791 struct lu_object_conf conf = { 0 };
1792 struct dt_device *tgt_dt = NULL;
1794 /* Try to find next avaible target */
1796 for (j = 0; j < lod->lod_remote_mdt_count;
1797 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1798 bool already_allocated = false;
1801 CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
1802 idx, lod->lod_remote_mdt_count + 1, i);
1803 if (idx == master_index) {
1804 /* Allocate the FID locally */
1805 rc = obd_fid_alloc(env, lod->lod_child_exp,
1809 tgt_dt = lod->lod_child;
1813 /* Find next available target */
1814 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx))
1817 if (likely(!OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
1818 /* check whether the idx already exists
1819 * in current allocated array */
1820 for (k = 0; k < i; k++) {
1821 if (idx_array[k] == idx) {
1822 already_allocated = true;
1827 if (already_allocated)
1831 /* check the status of the OSP */
1832 tgt = LTD_TGT(ltd, idx);
1836 tgt_dt = tgt->ltd_tgt;
1837 rc = dt_statfs(env, tgt_dt, NULL);
1839 /* this OSP doesn't feel well */
1844 rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1853 /* Can not allocate more stripes */
1854 if (j == lod->lod_remote_mdt_count) {
1855 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1856 lod2obd(lod)->obd_name, stripe_count, i - 1);
1860 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
1861 idx, i, PFID(&fid));
1863 /* Set the start index for next stripe allocation */
1864 if (i < stripe_count - 1)
1865 idx_array[i + 1] = (idx + 1) %
1866 (lod->lod_remote_mdt_count + 1);
1867 /* tgt_dt and fid must be ready after search avaible OSP
1868 * in the above loop */
1869 LASSERT(tgt_dt != NULL);
1870 LASSERT(fid_is_sane(&fid));
1871 conf.loc_flags = LOC_F_NEW;
1872 dto = dt_locate_at(env, tgt_dt, &fid,
1873 dt->do_lu.lo_dev->ld_site->ls_top_dev,
1876 GOTO(out_put, rc = PTR_ERR(dto));
1880 lo->ldo_dir_striped = 1;
1881 lo->ldo_stripe = stripe;
1882 lo->ldo_dir_stripenr = i;
1883 lo->ldo_dir_stripes_allocated = stripe_count;
1885 if (lo->ldo_dir_stripenr == 0)
1886 GOTO(out_put, rc = -ENOSPC);
1888 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
1894 for (i = 0; i < stripe_count; i++)
1895 if (stripe[i] != NULL)
1896 dt_object_put(env, stripe[i]);
1897 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
1898 lo->ldo_dir_stripenr = 0;
1899 lo->ldo_dir_stripes_allocated = 0;
1900 lo->ldo_stripe = NULL;
1904 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
1910 * Declare create striped md object.
1912 * The function declares intention to create a striped directory. This is a
1913 * wrapper for lod_prep_md_striped_create(). The only additional functionality
1914 * is to verify pattern \a lum_buf is good. Check that function for the details.
1916 * \param[in] env execution environment
1917 * \param[in] dt object
1918 * \param[in] attr attributes to initialize the objects with
1919 * \param[in] lum_buf a pattern specifying the number of stripes and
1921 * \param[in] dof type of objects to be created
1922 * \param[in] th transaction handle
1924 * \retval 0 on success
1925 * \retval negative if failed
1928 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
1929 struct dt_object *dt,
1930 struct lu_attr *attr,
1931 const struct lu_buf *lum_buf,
1932 struct dt_object_format *dof,
1935 struct lod_object *lo = lod_dt_obj(dt);
1936 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1937 struct lmv_user_md_v1 *lum;
1941 lum = lum_buf->lb_buf;
1942 LASSERT(lum != NULL);
1944 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
1945 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
1946 (int)le32_to_cpu(lum->lum_stripe_offset));
1948 if (le32_to_cpu(lum->lum_stripe_count) == 0)
1951 rc = lod_verify_md_striping(lod, lum);
1955 /* prepare dir striped objects */
1956 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
1958 /* failed to create striping, let's reset
1959 * config so that others don't get confused */
1960 lod_object_free_striping(env, lo);
1968 * Implementation of dt_object_operations::do_declare_xattr_set.
1970 * Used with regular (non-striped) objects. Basically it
1971 * initializes the striping information and applies the
1972 * change to all the stripes.
1974 * \see dt_object_operations::do_declare_xattr_set() in the API description
1977 static int lod_dir_declare_xattr_set(const struct lu_env *env,
1978 struct dt_object *dt,
1979 const struct lu_buf *buf,
1980 const char *name, int fl,
1983 struct dt_object *next = dt_object_child(dt);
1984 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
1985 struct lod_object *lo = lod_dt_obj(dt);
1990 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1991 struct lmv_user_md_v1 *lum;
1993 LASSERT(buf != NULL && buf->lb_buf != NULL);
1995 rc = lod_verify_md_striping(d, lum);
1998 } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
1999 rc = lod_verify_striping(d, buf, false, 0);
2004 rc = lod_sub_object_declare_xattr_set(env, next, buf, name, fl, th);
2008 /* Note: Do not set LinkEA on sub-stripes, otherwise
2009 * it will confuse the fid2path process(see mdt_path_current()).
2010 * The linkEA between master and sub-stripes is set in
2011 * lod_xattr_set_lmv(). */
2012 if (strcmp(name, XATTR_NAME_LINK) == 0)
2015 /* set xattr to each stripes, if needed */
2016 rc = lod_load_striping(env, lo);
2020 if (lo->ldo_dir_stripenr == 0)
2023 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2024 LASSERT(lo->ldo_stripe[i]);
2026 rc = lod_sub_object_declare_xattr_set(env, lo->ldo_stripe[i],
2036 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2037 struct lod_object *lo,
2038 struct dt_object *dt, struct thandle *th,
2040 struct lod_obj_stripe_cb_data *data)
2042 struct lod_thread_info *info = lod_env_info(env);
2043 struct filter_fid *ff = &info->lti_ff;
2044 struct lu_buf *buf = &info->lti_buf;
2048 buf->lb_len = sizeof(*ff);
2049 rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2056 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2057 ff->ff_parent.f_ver = stripe_idx;
2058 fid_cpu_to_le(&ff->ff_parent, &ff->ff_parent);
2059 if (data->locd_declare)
2060 rc = lod_sub_object_declare_xattr_set(env, dt, buf,
2062 LU_XATTR_REPLACE, th);
2064 rc = lod_sub_object_xattr_set(env, dt, buf, XATTR_NAME_FID,
2065 LU_XATTR_REPLACE, th);
2071 * Reset parent FID on OST object
2073 * Replace parent FID with @dt object FID, which is only called during migration
2074 * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2075 * the FID is changed.
2077 * \param[in] env execution environment
2078 * \param[in] dt dt_object whose stripes's parent FID will be reset
2079 * \parem[in] th thandle
2080 * \param[in] declare if it is declare
2082 * \retval 0 if reset succeeds
2083 * \retval negative errno if reset fails
2085 static int lod_object_replace_parent_fid(const struct lu_env *env,
2086 struct dt_object *dt,
2087 struct thandle *th, bool declare)
2089 struct lod_object *lo = lod_dt_obj(dt);
2090 struct lod_thread_info *info = lod_env_info(env);
2091 struct lu_buf *buf = &info->lti_buf;
2092 struct filter_fid *ff;
2093 struct lod_obj_stripe_cb_data data;
2097 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2099 /* set xattr to each stripes, if needed */
2100 rc = lod_load_striping(env, lo);
2104 if (!lod_obj_is_striped(dt))
2107 if (info->lti_ea_store_size < sizeof(*ff)) {
2108 rc = lod_ea_store_resize(info, sizeof(*ff));
2113 buf->lb_buf = info->lti_ea_store;
2114 buf->lb_len = info->lti_ea_store_size;
2116 data.locd_declare = declare;
2117 rc = lod_obj_for_each_stripe(env, lo, th,
2118 lod_obj_stripe_replace_parent_fid_cb, &data);
2124 * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2125 * the xattr value is binary lov_comp_md_v1 which contains component(s)
2128 * \param[in] env execution environment
2129 * \param[in] dt dt_object to add components on
2130 * \param[in] buf buffer contains components to be added
2131 * \parem[in] th thandle
2133 * \retval 0 on success
2134 * \retval negative errno on failure
2136 static int lod_declare_layout_add(const struct lu_env *env,
2137 struct dt_object *dt,
2138 const struct lu_buf *buf,
2141 struct lod_layout_component *comp_array, *lod_comp;
2142 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2143 struct lov_desc *desc = &d->lod_desc;
2144 struct lod_object *lo = lod_dt_obj(dt);
2145 struct lov_user_md_v1 *v1;
2146 struct lov_user_md_v3 *v3;
2147 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2148 struct lu_extent *ext;
2151 int i, rc, array_cnt;
2154 LASSERT(lo->ldo_is_composite);
2156 magic = comp_v1->lcm_magic;
2157 /* Replay request, see comment for LOV_MAGIC_DEF */
2158 if (unlikely(le32_to_cpu(magic) == LOV_MAGIC_COMP_V1_DEF)) {
2159 struct dt_object *next = dt_object_child(dt);
2161 lod_object_free_striping(env, lo);
2162 rc = lod_use_defined_striping(env, lo, buf);
2164 lo->ldo_comp_cached = 1;
2165 rc = lod_sub_object_declare_xattr_set(env, next, buf,
2172 prev_end = lo->ldo_comp_entries[lo->ldo_comp_cnt - 1].llc_extent.e_end;
2173 rc = lod_verify_striping(d, buf, false, prev_end);
2177 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2178 lustre_swab_lov_comp_md_v1(comp_v1);
2179 magic = comp_v1->lcm_magic;
2182 if (magic != LOV_USER_MAGIC_COMP_V1)
2185 array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2186 OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2187 if (comp_array == NULL)
2190 memcpy(comp_array, lo->ldo_comp_entries,
2191 sizeof(*comp_array) * lo->ldo_comp_cnt);
2193 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2194 v1 = (struct lov_user_md *)((char *)comp_v1 +
2195 comp_v1->lcm_entries[i].lcme_offset);
2196 ext = &comp_v1->lcm_entries[i].lcme_extent;
2198 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2199 lod_comp->llc_extent.e_start = ext->e_start;
2200 lod_comp->llc_extent.e_end = ext->e_end;
2201 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2203 lod_comp->llc_stripenr = v1->lmm_stripe_count;
2204 if (lod_comp->llc_stripenr <= 0)
2205 lod_comp->llc_stripenr = desc->ld_default_stripe_count;
2206 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2207 if (lod_comp->llc_stripe_size <= 0)
2208 lod_comp->llc_stripe_size =
2209 desc->ld_default_stripe_size;
2211 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2213 v3 = (struct lov_user_md_v3 *) v1;
2214 if (v3->lmm_pool_name[0] != '\0') {
2215 len = strlen(v3->lmm_pool_name);
2216 OBD_ALLOC(lod_comp->llc_pool, len + 1);
2217 if (lod_comp->llc_pool == NULL)
2218 GOTO(error, rc = -ENOMEM);
2219 strncpy(lod_comp->llc_pool, v3->lmm_pool_name,
2225 OBD_FREE(lo->ldo_comp_entries, sizeof(*lod_comp) * lo->ldo_comp_cnt);
2226 lo->ldo_comp_entries = comp_array;
2227 lo->ldo_comp_cnt = array_cnt;
2228 /* No need to increase layout generation here, it will be increased
2229 * later when generating component ID for the new components */
2231 rc = lod_declare_striped_object(env, dt, NULL, NULL, th);
2235 for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2236 lod_comp = &comp_array[i];
2237 if (lod_comp->llc_pool != NULL) {
2238 OBD_FREE(lod_comp->llc_pool,
2239 strlen(lod_comp->llc_pool) + 1);
2240 lod_comp->llc_pool = NULL;
2243 OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2247 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2249 int magic, size = 0, i;
2250 struct lod_layout_component *comp_entries;
2255 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2256 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2258 lo->ldo_def_striping->lds_def_striping_is_composite;
2260 comp_cnt = lo->ldo_comp_cnt;
2261 comp_entries = lo->ldo_comp_entries;
2262 is_composite = lo->ldo_is_composite;
2266 LASSERT(comp_cnt != 0 && comp_entries != NULL);
2268 size = sizeof(struct lov_comp_md_v1) +
2269 sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2270 LASSERT(size % sizeof(__u64) == 0);
2273 for (i = 0; i < comp_cnt; i++) {
2274 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2276 size += lov_user_md_size(
2277 is_dir ? 0 : comp_entries[i].llc_stripenr,
2279 LASSERT(size % sizeof(__u64) == 0);
2285 * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2286 * the '$field' can only be 'flags' now. The xattr value is binary
2287 * lov_comp_md_v1 which contains the component ID(s) and the value of
2288 * the field to be modified.
2290 * \param[in] env execution environment
2291 * \param[in] dt dt_object to be modified
2292 * \param[in] op operation string, like "set.flags"
2293 * \param[in] buf buffer contains components to be set
2294 * \parem[in] th thandle
2296 * \retval 0 on success
2297 * \retval negative errno on failure
2299 static int lod_declare_layout_set(const struct lu_env *env,
2300 struct dt_object *dt,
2301 char *op, const struct lu_buf *buf,
2304 struct lod_layout_component *lod_comp;
2305 struct lod_thread_info *info = lod_env_info(env);
2306 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2307 struct lod_object *lo = lod_dt_obj(dt);
2308 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2311 bool changed = false;
2314 if (strcmp(op, "set.flags") != 0) {
2315 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2316 lod2obd(d)->obd_name, op);
2320 magic = comp_v1->lcm_magic;
2321 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2322 lustre_swab_lov_comp_md_v1(comp_v1);
2323 magic = comp_v1->lcm_magic;
2326 if (magic != LOV_USER_MAGIC_COMP_V1)
2329 if (comp_v1->lcm_entry_count == 0) {
2330 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2331 lod2obd(d)->obd_name);
2335 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2336 id = comp_v1->lcm_entries[i].lcme_id;
2338 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2339 lod_comp = &lo->ldo_comp_entries[j];
2340 if (id == lod_comp->llc_id || id == LCME_ID_ALL) {
2341 lod_comp->llc_flags =
2342 comp_v1->lcm_entries[i].lcme_flags;
2349 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2350 lod2obd(d)->obd_name);
2354 lod_obj_inc_layout_gen(lo);
2356 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2357 rc = lod_sub_object_declare_xattr_set(env, dt, &info->lti_buf,
2358 XATTR_NAME_LOV, 0, th);
2363 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2364 * and the xattr value is a unique component ID or a special lcme_id.
2366 * \param[in] env execution environment
2367 * \param[in] dt dt_object to be operated on
2368 * \param[in] buf buffer contains component ID or lcme_id
2369 * \parem[in] th thandle
2371 * \retval 0 on success
2372 * \retval negative errno on failure
2374 static int lod_declare_layout_del(const struct lu_env *env,
2375 struct dt_object *dt,
2376 const struct lu_buf *buf,
2379 struct lod_thread_info *info = lod_env_info(env);
2380 struct dt_object *next = dt_object_child(dt);
2381 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2382 struct lod_object *lo = lod_dt_obj(dt);
2383 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2388 LASSERT(lo->ldo_is_composite);
2390 id = *(__u32 *)buf->lb_buf;
2391 if (id == 0 || id == LCME_ID_NONE) {
2392 CDEBUG(D_LAYOUT, "%s: invalid component id %#x\n",
2393 lod2obd(d)->obd_name, id);
2397 left = lo->ldo_comp_cnt;
2401 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
2402 struct lod_layout_component *lod_comp;
2404 lod_comp = &lo->ldo_comp_entries[i];
2406 if (id <= LCME_ID_MAX && id != lod_comp->llc_id)
2408 else if (id > LCME_ID_MAX && id < LCME_ID_ALL &&
2409 !(id & lod_comp->llc_flags))
2412 if (left != (i + 1)) {
2413 CDEBUG(D_LAYOUT, "%s: this deletion will create "
2414 "a hole.\n", lod2obd(d)->obd_name);
2419 /* Mark the component as deleted */
2420 lod_comp->llc_id = LCME_ID_INVAL;
2422 /* Not instantiated component */
2423 if (lod_comp->llc_stripe == NULL)
2426 LASSERT(lod_comp->llc_stripenr > 0);
2427 for (j = 0; j < lod_comp->llc_stripenr; j++) {
2428 struct dt_object *obj = lod_comp->llc_stripe[j];
2432 rc = lod_sub_object_declare_destroy(env, obj, th);
2438 LASSERTF(left >= 0, "left = %d\n", left);
2439 if (left == lo->ldo_comp_cnt) {
2440 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
2441 lod2obd(d)->obd_name, id);
2445 memset(attr, 0, sizeof(*attr));
2446 attr->la_valid = LA_SIZE;
2447 rc = lod_sub_object_declare_attr_set(env, next, attr, th);
2452 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2453 rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
2454 XATTR_NAME_LOV, 0, th);
2456 rc = lod_sub_object_declare_xattr_del(env, next, XATTR_NAME_LOV,
2464 * Declare layout add/set/del operations issued by special xattr names:
2466 * XATTR_LUSTRE_LOV.add add component(s) to existing file
2467 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
2468 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
2470 * \param[in] env execution environment
2471 * \param[in] dt object
2472 * \param[in] name name of xattr
2473 * \param[in] buf lu_buf contains xattr value
2474 * \param[in] th transaction handle
2476 * \retval 0 on success
2477 * \retval negative if failed
2479 static int lod_declare_modify_layout(const struct lu_env *env,
2480 struct dt_object *dt,
2482 const struct lu_buf *buf,
2485 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2486 struct lod_object *lo = lod_dt_obj(dt);
2487 struct dt_object *next = dt_object_child(&lo->ldo_obj);
2489 int rc, len = strlen(XATTR_LUSTRE_LOV);
2492 LASSERT(dt_object_exists(dt));
2494 if (strlen(name) <= len || name[len] != '.') {
2495 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
2496 lod2obd(d)->obd_name, name);
2501 dt_write_lock(env, next, 0);
2502 rc = lod_load_striping_locked(env, lo);
2506 /* the layout to be modified must be a composite layout */
2507 if (!lo->ldo_is_composite) {
2508 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
2509 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
2510 GOTO(unlock, rc = -EINVAL);
2513 op = (char *)name + len;
2514 if (strcmp(op, "add") == 0) {
2515 rc = lod_declare_layout_add(env, dt, buf, th);
2516 } else if (strcmp(op, "del") == 0) {
2517 rc = lod_declare_layout_del(env, dt, buf, th);
2518 } else if (strncmp(op, "set", strlen("set")) == 0) {
2519 rc = lod_declare_layout_set(env, dt, op, buf, th);
2521 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
2522 lod2obd(d)->obd_name, name);
2523 GOTO(unlock, rc = -ENOTSUPP);
2527 lod_object_free_striping(env, lo);
2528 dt_write_unlock(env, next);
2534 * Implementation of dt_object_operations::do_declare_xattr_set.
2536 * \see dt_object_operations::do_declare_xattr_set() in the API description
2539 * the extension to the API:
2540 * - declaring LOVEA requests striping creation
2541 * - LU_XATTR_REPLACE means layout swap
2543 static int lod_declare_xattr_set(const struct lu_env *env,
2544 struct dt_object *dt,
2545 const struct lu_buf *buf,
2546 const char *name, int fl,
2549 struct dt_object *next = dt_object_child(dt);
2550 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2555 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2556 if ((S_ISREG(mode) || mode == 0) && !(fl & LU_XATTR_REPLACE) &&
2557 (strcmp(name, XATTR_NAME_LOV) == 0 ||
2558 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
2560 * this is a request to create object's striping.
2562 * allow to declare predefined striping on a new (!mode) object
2563 * which is supposed to be replay of regular file creation
2564 * (when LOV setting is declared)
2566 * LU_XATTR_REPLACE is set to indicate a layout swap
2568 if (dt_object_exists(dt)) {
2569 rc = dt_attr_get(env, next, attr);
2573 memset(attr, 0, sizeof(*attr));
2574 attr->la_valid = LA_TYPE | LA_MODE;
2575 attr->la_mode = S_IFREG;
2577 rc = lod_declare_striped_object(env, dt, attr, buf, th);
2578 } else if (S_ISREG(mode) &&
2579 strlen(name) > strlen(XATTR_LUSTRE_LOV) + 1 &&
2580 strncmp(name, XATTR_LUSTRE_LOV,
2581 strlen(XATTR_LUSTRE_LOV)) == 0) {
2583 * this is a request to modify object's striping.
2584 * add/set/del component(s).
2586 if (!dt_object_exists(dt))
2589 rc = lod_declare_modify_layout(env, dt, name, buf, th);
2590 } else if (S_ISDIR(mode)) {
2591 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2592 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
2593 rc = lod_object_replace_parent_fid(env, dt, th, true);
2595 rc = lod_sub_object_declare_xattr_set(env, next, buf, name,
2603 * Apply xattr changes to the object.
2605 * Applies xattr changes to the object and the stripes if the latter exist.
2607 * \param[in] env execution environment
2608 * \param[in] dt object
2609 * \param[in] buf buffer pointing to the new value of xattr
2610 * \param[in] name name of xattr
2611 * \param[in] fl flags
2612 * \param[in] th transaction handle
2614 * \retval 0 on success
2615 * \retval negative if failed
2617 static int lod_xattr_set_internal(const struct lu_env *env,
2618 struct dt_object *dt,
2619 const struct lu_buf *buf,
2620 const char *name, int fl,
2623 struct dt_object *next = dt_object_child(dt);
2624 struct lod_object *lo = lod_dt_obj(dt);
2629 rc = lod_sub_object_xattr_set(env, next, buf, name, fl, th);
2630 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2633 /* Note: Do not set LinkEA on sub-stripes, otherwise
2634 * it will confuse the fid2path process(see mdt_path_current()).
2635 * The linkEA between master and sub-stripes is set in
2636 * lod_xattr_set_lmv(). */
2637 if (lo->ldo_dir_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2640 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2641 LASSERT(lo->ldo_stripe[i]);
2643 rc = lod_sub_object_xattr_set(env, lo->ldo_stripe[i], buf, name,
2653 * Delete an extended attribute.
2655 * Deletes specified xattr from the object and the stripes if the latter exist.
2657 * \param[in] env execution environment
2658 * \param[in] dt object
2659 * \param[in] name name of xattr
2660 * \param[in] th transaction handle
2662 * \retval 0 on success
2663 * \retval negative if failed
2665 static int lod_xattr_del_internal(const struct lu_env *env,
2666 struct dt_object *dt,
2667 const char *name, struct thandle *th)
2669 struct dt_object *next = dt_object_child(dt);
2670 struct lod_object *lo = lod_dt_obj(dt);
2675 rc = lod_sub_object_xattr_del(env, next, name, th);
2676 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2679 if (lo->ldo_dir_stripenr == 0)
2682 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2683 LASSERT(lo->ldo_stripe[i]);
2685 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name,
2695 * Set default striping on a directory.
2697 * Sets specified striping on a directory object unless it matches the default
2698 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2699 * EA. This striping will be used when regular file is being created in this
2702 * \param[in] env execution environment
2703 * \param[in] dt the striped object
2704 * \param[in] buf buffer with the striping
2705 * \param[in] name name of EA
2706 * \param[in] fl xattr flag (see OSD API description)
2707 * \param[in] th transaction handle
2709 * \retval 0 on success
2710 * \retval negative if failed
2712 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
2713 struct dt_object *dt,
2714 const struct lu_buf *buf,
2715 const char *name, int fl,
2718 struct lov_user_md_v1 *lum;
2719 struct lov_user_md_v3 *v3 = NULL;
2720 const char *pool_name = NULL;
2725 LASSERT(buf != NULL && buf->lb_buf != NULL);
2728 switch (lum->lmm_magic) {
2729 case LOV_USER_MAGIC_V3:
2731 if (v3->lmm_pool_name[0] != '\0')
2732 pool_name = v3->lmm_pool_name;
2734 case LOV_USER_MAGIC_V1:
2735 /* if { size, offset, count } = { 0, -1, 0 } and no pool
2736 * (i.e. all default values specified) then delete default
2737 * striping from dir. */
2739 "set default striping: sz %u # %u offset %d %s %s\n",
2740 (unsigned)lum->lmm_stripe_size,
2741 (unsigned)lum->lmm_stripe_count,
2742 (int)lum->lmm_stripe_offset,
2743 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
2745 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
2746 lum->lmm_stripe_count,
2747 lum->lmm_stripe_offset,
2750 case LOV_USER_MAGIC_COMP_V1:
2754 CERROR("Invalid magic %x\n", lum->lmm_magic);
2759 rc = lod_xattr_del_internal(env, dt, name, th);
2763 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2770 * Set default striping on a directory object.
2772 * Sets specified striping on a directory object unless it matches the default
2773 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2774 * EA. This striping will be used when a new directory is being created in the
2777 * \param[in] env execution environment
2778 * \param[in] dt the striped object
2779 * \param[in] buf buffer with the striping
2780 * \param[in] name name of EA
2781 * \param[in] fl xattr flag (see OSD API description)
2782 * \param[in] th transaction handle
2784 * \retval 0 on success
2785 * \retval negative if failed
2787 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
2788 struct dt_object *dt,
2789 const struct lu_buf *buf,
2790 const char *name, int fl,
2793 struct lmv_user_md_v1 *lum;
2797 LASSERT(buf != NULL && buf->lb_buf != NULL);
2800 CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
2801 le32_to_cpu(lum->lum_stripe_count),
2802 (int)le32_to_cpu(lum->lum_stripe_offset));
2804 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
2805 le32_to_cpu(lum->lum_stripe_offset)) &&
2806 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
2807 rc = lod_xattr_del_internal(env, dt, name, th);
2811 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2820 * Turn directory into a striped directory.
2822 * During replay the client sends the striping created before MDT
2823 * failure, then the layer above LOD sends this defined striping
2824 * using ->do_xattr_set(), so LOD uses this method to replay creation
2825 * of the stripes. Notice the original information for the striping
2826 * (#stripes, FIDs, etc) was transferred in declare path.
2828 * \param[in] env execution environment
2829 * \param[in] dt the striped object
2830 * \param[in] buf not used currently
2831 * \param[in] name not used currently
2832 * \param[in] fl xattr flag (see OSD API description)
2833 * \param[in] th transaction handle
2835 * \retval 0 on success
2836 * \retval negative if failed
2838 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
2839 const struct lu_buf *buf, const char *name,
2840 int fl, struct thandle *th)
2842 struct lod_object *lo = lod_dt_obj(dt);
2843 struct lod_thread_info *info = lod_env_info(env);
2844 struct lu_attr *attr = &info->lti_attr;
2845 struct dt_object_format *dof = &info->lti_format;
2846 struct lu_buf lmv_buf;
2847 struct lu_buf slave_lmv_buf;
2848 struct lmv_mds_md_v1 *lmm;
2849 struct lmv_mds_md_v1 *slave_lmm = NULL;
2850 struct dt_insert_rec *rec = &info->lti_dt_rec;
2855 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2858 /* The stripes are supposed to be allocated in declare phase,
2859 * if there are no stripes being allocated, it will skip */
2860 if (lo->ldo_dir_stripenr == 0)
2863 rc = dt_attr_get(env, dt_object_child(dt), attr);
2867 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
2868 LA_MODE | LA_UID | LA_GID | LA_TYPE;
2869 dof->dof_type = DFT_DIR;
2871 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
2874 lmm = lmv_buf.lb_buf;
2876 OBD_ALLOC_PTR(slave_lmm);
2877 if (slave_lmm == NULL)
2880 lod_prep_slave_lmv_md(slave_lmm, lmm);
2881 slave_lmv_buf.lb_buf = slave_lmm;
2882 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
2884 rec->rec_type = S_IFDIR;
2885 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2886 struct dt_object *dto;
2887 char *stripe_name = info->lti_key;
2888 struct lu_name *sname;
2889 struct linkea_data ldata = { NULL };
2890 struct lu_buf linkea_buf;
2892 dto = lo->ldo_stripe[i];
2894 dt_write_lock(env, dto, MOR_TGT_CHILD);
2895 rc = lod_sub_object_create(env, dto, attr, NULL, dof,
2898 dt_write_unlock(env, dto);
2902 rc = lod_sub_object_ref_add(env, dto, th);
2903 dt_write_unlock(env, dto);
2907 rec->rec_fid = lu_object_fid(&dto->do_lu);
2908 rc = lod_sub_object_index_insert(env, dto,
2909 (const struct dt_rec *)rec,
2910 (const struct dt_key *)dot, th, 0);
2914 rec->rec_fid = lu_object_fid(&dt->do_lu);
2915 rc = lod_sub_object_index_insert(env, dto, (struct dt_rec *)rec,
2916 (const struct dt_key *)dotdot, th, 0);
2920 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
2921 cfs_fail_val != i) {
2922 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
2924 slave_lmm->lmv_master_mdt_index =
2927 slave_lmm->lmv_master_mdt_index =
2930 rc = lod_sub_object_xattr_set(env, dto, &slave_lmv_buf,
2931 XATTR_NAME_LMV, fl, th);
2936 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
2938 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2939 PFID(lu_object_fid(&dto->do_lu)), i + 1);
2941 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2942 PFID(lu_object_fid(&dto->do_lu)), i);
2944 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2945 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
2946 sname, lu_object_fid(&dt->do_lu));
2950 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
2951 linkea_buf.lb_len = ldata.ld_leh->leh_len;
2952 rc = lod_sub_object_xattr_set(env, dto, &linkea_buf,
2953 XATTR_NAME_LINK, 0, th);
2957 rec->rec_fid = lu_object_fid(&dto->do_lu);
2958 rc = lod_sub_object_index_insert(env, dt_object_child(dt),
2959 (const struct dt_rec *)rec,
2960 (const struct dt_key *)stripe_name, th, 0);
2964 rc = lod_sub_object_ref_add(env, dt_object_child(dt), th);
2969 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
2970 rc = lod_sub_object_xattr_set(env, dt_object_child(dt),
2971 &lmv_buf, XATTR_NAME_LMV, fl, th);
2973 if (slave_lmm != NULL)
2974 OBD_FREE_PTR(slave_lmm);
2980 * Helper function to declare/execute creation of a striped directory
2982 * Called in declare/create object path, prepare striping for a directory
2983 * and prepare defaults data striping for the objects to be created in
2984 * that directory. Notice the function calls "declaration" or "execution"
2985 * methods depending on \a declare param. This is a consequence of the
2986 * current approach while we don't have natural distributed transactions:
2987 * we basically execute non-local updates in the declare phase. So, the
2988 * arguments for the both phases are the same and this is the reason for
2989 * this function to exist.
2991 * \param[in] env execution environment
2992 * \param[in] dt object
2993 * \param[in] attr attributes the stripes will be created with
2994 * \param[in] dof format of stripes (see OSD API description)
2995 * \param[in] th transaction handle
2996 * \param[in] declare where to call "declare" or "execute" methods
2998 * \retval 0 on success
2999 * \retval negative if failed
3001 static int lod_dir_striping_create_internal(const struct lu_env *env,
3002 struct dt_object *dt,
3003 struct lu_attr *attr,
3004 struct dt_object_format *dof,
3008 struct lod_thread_info *info = lod_env_info(env);
3009 struct lod_object *lo = lod_dt_obj(dt);
3010 const struct lod_default_striping *lds = lo->ldo_def_striping;
3014 LASSERT(ergo(lds != NULL,
3015 lds->lds_def_striping_set ||
3016 lds->lds_dir_def_striping_set));
3018 if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripenr,
3019 lo->ldo_dir_stripe_offset)) {
3020 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3021 int stripe_count = lo->ldo_dir_stripenr;
3023 if (info->lti_ea_store_size < sizeof(*v1)) {
3024 rc = lod_ea_store_resize(info, sizeof(*v1));
3027 v1 = info->lti_ea_store;
3030 memset(v1, 0, sizeof(*v1));
3031 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3032 v1->lum_stripe_count = cpu_to_le32(stripe_count);
3033 v1->lum_stripe_offset =
3034 cpu_to_le32(lo->ldo_dir_stripe_offset);
3036 info->lti_buf.lb_buf = v1;
3037 info->lti_buf.lb_len = sizeof(*v1);
3040 rc = lod_declare_xattr_set_lmv(env, dt, attr,
3041 &info->lti_buf, dof, th);
3043 rc = lod_xattr_set_lmv(env, dt, &info->lti_buf,
3044 XATTR_NAME_LMV, 0, th);
3049 /* Transfer default LMV striping from the parent */
3050 if (lds != NULL && lds->lds_dir_def_striping_set &&
3051 !LMVEA_DELETE_VALUES(lds->lds_dir_def_stripenr,
3052 lds->lds_dir_def_stripe_offset)) {
3053 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3055 if (info->lti_ea_store_size < sizeof(*v1)) {
3056 rc = lod_ea_store_resize(info, sizeof(*v1));
3059 v1 = info->lti_ea_store;
3062 memset(v1, 0, sizeof(*v1));
3063 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3064 v1->lum_stripe_count = cpu_to_le32(lds->lds_dir_def_stripenr);
3065 v1->lum_stripe_offset =
3066 cpu_to_le32(lds->lds_dir_def_stripe_offset);
3068 cpu_to_le32(lds->lds_dir_def_hash_type);
3070 info->lti_buf.lb_buf = v1;
3071 info->lti_buf.lb_len = sizeof(*v1);
3073 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3074 XATTR_NAME_DEFAULT_LMV,
3077 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
3079 XATTR_NAME_DEFAULT_LMV, 0,
3085 /* Transfer default LOV striping from the parent */
3086 if (lds != NULL && lds->lds_def_striping_set &&
3087 lds->lds_def_comp_cnt != 0) {
3088 struct lov_mds_md *lmm;
3089 int lmm_size = lod_comp_md_size(lo, true);
3091 if (info->lti_ea_store_size < lmm_size) {
3092 rc = lod_ea_store_resize(info, lmm_size);
3096 lmm = info->lti_ea_store;
3098 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
3102 info->lti_buf.lb_buf = lmm;
3103 info->lti_buf.lb_len = lmm_size;
3106 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3107 XATTR_NAME_LOV, 0, th);
3109 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
3110 XATTR_NAME_LOV, 0, th);
3118 static int lod_declare_dir_striping_create(const struct lu_env *env,
3119 struct dt_object *dt,
3120 struct lu_attr *attr,
3121 struct dt_object_format *dof,
3124 return lod_dir_striping_create_internal(env, dt, attr, dof, th, true);
3127 static int lod_dir_striping_create(const struct lu_env *env,
3128 struct dt_object *dt,
3129 struct lu_attr *attr,
3130 struct dt_object_format *dof,
3133 return lod_dir_striping_create_internal(env, dt, attr, dof, th, false);
3137 * Make LOV EA for striped object.
3139 * Generate striping information and store it in the LOV EA of the given
3140 * object. The caller must ensure nobody else is calling the function
3141 * against the object concurrently. The transaction must be started.
3142 * FLDB service must be running as well; it's used to map FID to the target,
3143 * which is stored in LOV EA.
3145 * \param[in] env execution environment for this thread
3146 * \param[in] lo LOD object
3147 * \param[in] th transaction handle
3149 * \retval 0 if LOV EA is stored successfully
3150 * \retval negative error number on failure
3152 static int lod_generate_and_set_lovea(const struct lu_env *env,
3153 struct lod_object *lo,
3156 struct lod_thread_info *info = lod_env_info(env);
3157 struct dt_object *next = dt_object_child(&lo->ldo_obj);
3158 struct lov_mds_md_v1 *lmm;
3164 if (lo->ldo_comp_cnt == 0) {
3165 lod_object_free_striping(env, lo);
3166 rc = lod_sub_object_xattr_del(env, next, XATTR_NAME_LOV, th);
3170 lmm_size = lod_comp_md_size(lo, false);
3171 if (info->lti_ea_store_size < lmm_size) {
3172 rc = lod_ea_store_resize(info, lmm_size);
3176 lmm = info->lti_ea_store;
3178 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
3182 info->lti_buf.lb_buf = lmm;
3183 info->lti_buf.lb_len = lmm_size;
3184 rc = lod_sub_object_xattr_set(env, next, &info->lti_buf,
3185 XATTR_NAME_LOV, 0, th);
3190 * Delete layout component(s)
3192 * \param[in] env execution environment for this thread
3193 * \param[in] dt object
3194 * \param[in] th transaction handle
3196 * \retval 0 on success
3197 * \retval negative error number on failure
3199 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
3202 struct lod_layout_component *lod_comp;
3203 struct lod_object *lo = lod_dt_obj(dt);
3204 struct dt_object *next = dt_object_child(dt);
3205 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3208 LASSERT(lo->ldo_is_composite);
3209 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
3211 left = lo->ldo_comp_cnt;
3212 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3213 lod_comp = &lo->ldo_comp_entries[i];
3215 if (lod_comp->llc_id != LCME_ID_INVAL)
3219 /* Not instantiated component */
3220 if (lod_comp->llc_stripe == NULL)
3223 LASSERT(lod_comp->llc_stripenr > 0);
3224 for (j = 0; j < lod_comp->llc_stripenr; j++) {
3225 struct dt_object *obj = lod_comp->llc_stripe[j];
3229 rc = lod_sub_object_destroy(env, obj, th);
3233 lu_object_put(env, &obj->do_lu);
3234 lod_comp->llc_stripe[j] = NULL;
3236 OBD_FREE(lod_comp->llc_stripe, sizeof(struct dt_object *) *
3237 lod_comp->llc_stripes_allocated);
3238 lod_comp->llc_stripe = NULL;
3239 lod_comp->llc_stripes_allocated = 0;
3240 lod_obj_set_pool(lo, i, NULL);
3243 LASSERTF(left >= 0 && left < lo->ldo_comp_cnt, "left = %d\n", left);
3245 struct lod_layout_component *comp_array;
3247 OBD_ALLOC(comp_array, sizeof(*comp_array) * left);
3248 if (comp_array == NULL)
3249 GOTO(out, rc = -ENOMEM);
3251 memcpy(&comp_array[0], &lo->ldo_comp_entries[0],
3252 sizeof(*comp_array) * left);
3254 OBD_FREE(lo->ldo_comp_entries,
3255 sizeof(*comp_array) * lo->ldo_comp_cnt);
3256 lo->ldo_comp_entries = comp_array;
3257 lo->ldo_comp_cnt = left;
3258 lod_obj_inc_layout_gen(lo);
3260 lod_free_comp_entries(lo);
3263 LASSERT(dt_object_exists(dt));
3264 rc = dt_attr_get(env, next, attr);
3268 if (attr->la_size > 0) {
3270 attr->la_valid = LA_SIZE;
3271 rc = lod_sub_object_attr_set(env, next, attr, th);
3276 rc = lod_generate_and_set_lovea(env, lo, th);
3280 lod_object_free_striping(env, lo);
3285 * Implementation of dt_object_operations::do_xattr_set.
3287 * Sets specified extended attribute on the object. Three types of EAs are
3289 * LOV EA - stores striping for a regular file or default striping (when set
3291 * LMV EA - stores a marker for the striped directories
3292 * DMV EA - stores default directory striping
3294 * When striping is applied to a non-striped existing object (this is called
3295 * late striping), then LOD notices the caller wants to turn the object into a
3296 * striped one. The stripe objects are created and appropriate EA is set:
3297 * LOV EA storing all the stripes directly or LMV EA storing just a small header
3298 * with striping configuration.
3300 * \see dt_object_operations::do_xattr_set() in the API description for details.
3302 static int lod_xattr_set(const struct lu_env *env,
3303 struct dt_object *dt, const struct lu_buf *buf,
3304 const char *name, int fl, struct thandle *th)
3306 struct dt_object *next = dt_object_child(dt);
3310 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3311 strcmp(name, XATTR_NAME_LMV) == 0) {
3312 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
3314 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
3315 LMV_HASH_FLAG_MIGRATION)
3316 rc = lod_sub_object_xattr_set(env, next, buf, name, fl,
3319 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
3324 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3325 strcmp(name, XATTR_NAME_LOV) == 0) {
3327 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
3329 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3330 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
3332 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
3335 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3336 (!strcmp(name, XATTR_NAME_LOV) ||
3337 !strncmp(name, XATTR_LUSTRE_LOV,
3338 strlen(XATTR_LUSTRE_LOV)))) {
3339 /* in case of lov EA swap, just set it
3340 * if not, it is a replay so check striping match what we
3341 * already have during req replay, declare_xattr_set()
3342 * defines striping, then create() does the work */
3343 if (fl & LU_XATTR_REPLACE) {
3344 /* free stripes, then update disk */
3345 lod_object_free_striping(env, lod_dt_obj(dt));
3347 rc = lod_sub_object_xattr_set(env, next, buf, name,
3349 } else if (dt_object_remote(dt)) {
3350 /* This only happens during migration, see
3351 * mdd_migrate_create(), in which Master MDT will
3352 * create a remote target object, and only set
3353 * (migrating) stripe EA on the remote object,
3354 * and does not need creating each stripes. */
3355 rc = lod_sub_object_xattr_set(env, next, buf, name,
3357 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
3358 /* delete component(s) */
3359 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
3360 rc = lod_layout_del(env, dt, th);
3363 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
3364 * it's going to create create file with specified
3365 * component(s), the striping must have not being
3366 * cached in this case;
3368 * Otherwise, it's going to add/change component(s) to
3369 * an existing file, the striping must have been cached
3372 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
3373 !strcmp(name, XATTR_NAME_LOV),
3374 !lod_dt_obj(dt)->ldo_comp_cached));
3376 rc = lod_striping_create(env, dt, NULL, NULL, th);
3379 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3380 rc = lod_object_replace_parent_fid(env, dt, th, false);
3385 /* then all other xattr */
3386 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3392 * Implementation of dt_object_operations::do_declare_xattr_del.
3394 * \see dt_object_operations::do_declare_xattr_del() in the API description
3397 static int lod_declare_xattr_del(const struct lu_env *env,
3398 struct dt_object *dt, const char *name,
3401 struct lod_object *lo = lod_dt_obj(dt);
3406 rc = lod_sub_object_declare_xattr_del(env, dt_object_child(dt),
3411 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3414 /* set xattr to each stripes, if needed */
3415 rc = lod_load_striping(env, lo);
3419 if (lo->ldo_dir_stripenr == 0)
3422 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
3423 LASSERT(lo->ldo_stripe[i]);
3424 rc = lod_sub_object_declare_xattr_del(env, lo->ldo_stripe[i],
3434 * Implementation of dt_object_operations::do_xattr_del.
3436 * If EA storing a regular striping is being deleted, then release
3437 * all the references to the stripe objects in core.
3439 * \see dt_object_operations::do_xattr_del() in the API description for details.
3441 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
3442 const char *name, struct thandle *th)
3444 struct dt_object *next = dt_object_child(dt);
3445 struct lod_object *lo = lod_dt_obj(dt);
3450 if (!strcmp(name, XATTR_NAME_LOV))
3451 lod_object_free_striping(env, lod_dt_obj(dt));
3453 rc = lod_sub_object_xattr_del(env, next, name, th);
3454 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3457 if (lo->ldo_dir_stripenr == 0)
3460 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
3461 LASSERT(lo->ldo_stripe[i]);
3463 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name, th);
3472 * Implementation of dt_object_operations::do_xattr_list.
3474 * \see dt_object_operations::do_xattr_list() in the API description
3477 static int lod_xattr_list(const struct lu_env *env,
3478 struct dt_object *dt, const struct lu_buf *buf)
3480 return dt_xattr_list(env, dt_object_child(dt), buf);
3483 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
3485 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
3490 * Get default striping.
3492 * \param[in] env execution environment
3493 * \param[in] lo object
3494 * \param[out] lds default striping
3496 * \retval 0 on success
3497 * \retval negative if failed
3499 static int lod_get_default_lov_striping(const struct lu_env *env,
3500 struct lod_object *lo,
3501 struct lod_default_striping *lds)
3503 struct lod_thread_info *info = lod_env_info(env);
3504 struct lov_user_md_v1 *v1 = NULL;
3505 struct lov_user_md_v3 *v3 = NULL;
3506 struct lov_comp_md_v1 *comp_v1 = NULL;
3512 lds->lds_def_striping_set = 0;
3514 rc = lod_get_lov_ea(env, lo);
3518 if (rc < (typeof(rc))sizeof(struct lov_user_md))
3521 v1 = info->lti_ea_store;
3522 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
3523 lustre_swab_lov_user_md_v1(v1);
3524 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
3525 v3 = (struct lov_user_md_v3 *)v1;
3526 lustre_swab_lov_user_md_v3(v3);
3527 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3528 comp_v1 = (struct lov_comp_md_v1 *)v1;
3529 lustre_swab_lov_comp_md_v1(comp_v1);
3532 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
3533 v1->lmm_magic != LOV_MAGIC_COMP_V1)
3536 if (v1->lmm_magic == LOV_MAGIC_COMP_V1) {
3537 comp_v1 = (struct lov_comp_md_v1 *)v1;
3538 comp_cnt = comp_v1->lcm_entry_count;
3547 /* realloc default comp entries if necessary */
3548 rc = lod_def_striping_comp_resize(lds, comp_cnt);
3552 lds->lds_def_comp_cnt = comp_cnt;
3553 lds->lds_def_striping_is_composite = composite ? 1 : 0;
3555 for (i = 0; i < comp_cnt; i++) {
3556 struct lod_layout_component *lod_comp;
3557 struct lu_extent *ext;
3560 lod_comp = &lds->lds_def_comp_entries[i];
3562 * reset lod_comp values, llc_stripes is always NULL in
3563 * the default striping template, llc_pool will be reset
3566 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
3569 v1 = (struct lov_user_md *)((char *)comp_v1 +
3570 comp_v1->lcm_entries[i].lcme_offset);
3571 ext = &comp_v1->lcm_entries[i].lcme_extent;
3572 lod_comp->llc_extent = *ext;
3575 if (v1->lmm_pattern != LOV_PATTERN_RAID0 &&
3576 v1->lmm_pattern != 0) {
3577 lod_free_def_comp_entries(lds);
3581 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d "
3582 "stripe_offset=%d\n",
3583 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
3584 (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
3585 (int)v1->lmm_stripe_offset);
3587 lod_comp->llc_stripenr = v1->lmm_stripe_count;
3588 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
3589 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
3592 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
3593 /* XXX: sanity check here */
3594 v3 = (struct lov_user_md_v3 *) v1;
3595 if (v3->lmm_pool_name[0] != '\0')
3596 pool = v3->lmm_pool_name;
3598 lod_set_def_pool(lds, i, pool);
3601 lds->lds_def_striping_set = 1;
3606 * Get default directory striping.
3608 * \param[in] env execution environment
3609 * \param[in] lo object
3610 * \param[out] lds default striping
3612 * \retval 0 on success
3613 * \retval negative if failed
3615 static int lod_get_default_lmv_striping(const struct lu_env *env,
3616 struct lod_object *lo,
3617 struct lod_default_striping *lds)
3619 struct lod_thread_info *info = lod_env_info(env);
3620 struct lmv_user_md_v1 *v1 = NULL;
3624 lds->lds_dir_def_striping_set = 0;
3625 rc = lod_get_default_lmv_ea(env, lo);
3629 if (rc < (typeof(rc))sizeof(struct lmv_user_md))
3632 v1 = info->lti_ea_store;
3634 lds->lds_dir_def_stripenr = le32_to_cpu(v1->lum_stripe_count);
3635 lds->lds_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
3636 lds->lds_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
3637 lds->lds_dir_def_striping_set = 1;
3643 * Get default striping in the object.
3645 * Get object default striping and default directory striping.
3647 * \param[in] env execution environment
3648 * \param[in] lo object
3649 * \param[out] lds default striping
3651 * \retval 0 on success
3652 * \retval negative if failed
3654 static int lod_get_default_striping(const struct lu_env *env,
3655 struct lod_object *lo,
3656 struct lod_default_striping *lds)
3660 rc = lod_get_default_lov_striping(env, lo, lds);
3661 rc1 = lod_get_default_lmv_striping(env, lo, lds);
3662 if (rc == 0 && rc1 < 0)
3669 * Apply default striping on object.
3671 * If object striping pattern is not set, set to the one in default striping.
3672 * The default striping is from parent or fs.
3674 * \param[in] lo new object
3675 * \param[in] lds default striping
3676 * \param[in] mode new object's mode
3678 static void lod_striping_from_default(struct lod_object *lo,
3679 const struct lod_default_striping *lds,
3682 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
3683 struct lov_desc *desc = &d->lod_desc;
3686 if (lds->lds_def_striping_set && S_ISREG(mode)) {
3687 rc = lod_alloc_comp_entries(lo, lds->lds_def_comp_cnt);
3691 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
3693 for (i = 0; i < lo->ldo_comp_cnt; i++) {
3694 struct lod_layout_component *obj_comp =
3695 &lo->ldo_comp_entries[i];
3696 struct lod_layout_component *def_comp =
3697 &lds->lds_def_comp_entries[i];
3699 CDEBUG(D_LAYOUT, "Inherite from default: size:%hu "
3700 "nr:%u offset:%u %s\n",
3701 def_comp->llc_stripe_size,
3702 def_comp->llc_stripenr,
3703 def_comp->llc_stripe_offset,
3704 def_comp->llc_pool ?: "");
3706 *obj_comp = *def_comp;
3707 if (def_comp->llc_pool != NULL) {
3708 /* pointer was copied from def_comp */
3709 obj_comp->llc_pool = NULL;
3710 lod_obj_set_pool(lo, i, def_comp->llc_pool);
3714 * Don't initialize these fields for plain layout
3715 * (v1/v3) here, they are inherited in the order of
3716 * 'parent' -> 'fs default (root)' -> 'global default
3717 * values for stripe_count & stripe_size'.
3719 * see lod_ah_init().
3721 if (!lo->ldo_is_composite)
3724 if (obj_comp->llc_stripenr <= 0)
3725 obj_comp->llc_stripenr =
3726 desc->ld_default_stripe_count;
3727 if (obj_comp->llc_stripe_size <= 0)
3728 obj_comp->llc_stripe_size =
3729 desc->ld_default_stripe_size;
3731 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
3732 if (lo->ldo_dir_stripenr == 0)
3733 lo->ldo_dir_stripenr = lds->lds_dir_def_stripenr;
3734 if (lo->ldo_dir_stripe_offset == -1)
3735 lo->ldo_dir_stripe_offset =
3736 lds->lds_dir_def_stripe_offset;
3737 if (lo->ldo_dir_hash_type == 0)
3738 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
3740 CDEBUG(D_LAYOUT, "striping from default dir: nr:%hu, "
3741 "offset:%u, hash_type:%u\n",
3742 lo->ldo_dir_stripenr, lo->ldo_dir_stripe_offset,
3743 lo->ldo_dir_hash_type);
3747 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root)
3749 struct lod_layout_component *lod_comp;
3751 if (lo->ldo_comp_cnt == 0)
3754 if (lo->ldo_is_composite)
3757 lod_comp = &lo->ldo_comp_entries[0];
3759 if (lod_comp->llc_stripenr <= 0 ||
3760 lod_comp->llc_stripe_size <= 0)
3763 if (from_root && (lod_comp->llc_pool == NULL ||
3764 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
3771 * Implementation of dt_object_operations::do_ah_init.
3773 * This method is used to make a decision on the striping configuration for the
3774 * object being created. It can be taken from the \a parent object if it exists,
3775 * or filesystem's default. The resulting configuration (number of stripes,
3776 * stripe size/offset, pool name, etc) is stored in the object itself and will
3777 * be used by the methods like ->doo_declare_create().
3779 * \see dt_object_operations::do_ah_init() in the API description for details.
3781 static void lod_ah_init(const struct lu_env *env,
3782 struct dt_allocation_hint *ah,
3783 struct dt_object *parent,
3784 struct dt_object *child,
3787 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
3788 struct lod_thread_info *info = lod_env_info(env);
3789 struct lod_default_striping *lds = &info->lti_def_striping;
3790 struct dt_object *nextp = NULL;
3791 struct dt_object *nextc;
3792 struct lod_object *lp = NULL;
3793 struct lod_object *lc;
3794 struct lov_desc *desc;
3795 struct lod_layout_component *lod_comp;
3801 if (likely(parent)) {
3802 nextp = dt_object_child(parent);
3803 lp = lod_dt_obj(parent);
3806 nextc = dt_object_child(child);
3807 lc = lod_dt_obj(child);
3809 LASSERT(!lod_obj_is_striped(child));
3810 /* default layout template may have been set on the regular file
3811 * when this is called from mdd_create_data() */
3812 if (S_ISREG(child_mode))
3813 lod_free_comp_entries(lc);
3815 if (!dt_object_exists(nextc))
3816 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
3818 if (S_ISDIR(child_mode)) {
3819 /* other default values are 0 */
3820 lc->ldo_dir_stripe_offset = -1;
3822 /* get default striping from parent object */
3823 if (likely(lp != NULL))
3824 lod_get_default_striping(env, lp, lds);
3826 /* set child default striping info, default value is NULL */
3827 if (lds->lds_def_striping_set || lds->lds_dir_def_striping_set)
3828 lc->ldo_def_striping = lds;
3830 /* It should always honour the specified stripes */
3831 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
3832 lod_verify_md_striping(d, ah->dah_eadata) == 0) {
3833 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
3835 lc->ldo_dir_stripenr =
3836 le32_to_cpu(lum1->lum_stripe_count);
3837 lc->ldo_dir_stripe_offset =
3838 le32_to_cpu(lum1->lum_stripe_offset);
3839 lc->ldo_dir_hash_type =
3840 le32_to_cpu(lum1->lum_hash_type);
3841 CDEBUG(D_INFO, "set dir stripe: count %hu, offset %d, "
3843 lc->ldo_dir_stripenr,
3844 (int)lc->ldo_dir_stripe_offset,
3845 lc->ldo_dir_hash_type);
3847 /* transfer defaults LMV to new directory */
3848 lod_striping_from_default(lc, lds, child_mode);
3851 /* shrink the stripe_count to the avaible MDT count */
3852 if (lc->ldo_dir_stripenr > d->lod_remote_mdt_count + 1 &&
3853 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))
3854 lc->ldo_dir_stripenr = d->lod_remote_mdt_count + 1;
3856 /* Directory will be striped only if stripe_count > 1, if
3857 * stripe_count == 1, let's reset stripenr = 0 to avoid
3858 * create single master stripe and also help to unify the
3859 * stripe handling of directories and files */
3860 if (lc->ldo_dir_stripenr == 1)
3861 lc->ldo_dir_stripenr = 0;
3863 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
3864 lc->ldo_dir_stripenr, (int)lc->ldo_dir_stripe_offset,
3865 lc->ldo_dir_hash_type);
3870 /* child object regular file*/
3872 if (!lod_object_will_be_striped(S_ISREG(child_mode),
3873 lu_object_fid(&child->do_lu)))
3876 /* If object is going to be striped over OSTs, transfer default
3877 * striping information to the child, so that we can use it
3878 * during declaration and creation.
3880 * Try from the parent first.
3882 if (likely(lp != NULL)) {
3883 rc = lod_get_default_lov_striping(env, lp, lds);
3885 lod_striping_from_default(lc, lds, child_mode);
3888 /* Initialize lod_device::lod_md_root object reference */
3889 if (d->lod_md_root == NULL) {
3890 struct dt_object *root;
3891 struct lod_object *lroot;
3893 lu_root_fid(&info->lti_fid);
3894 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
3895 if (!IS_ERR(root)) {
3896 lroot = lod_dt_obj(root);
3898 spin_lock(&d->lod_lock);
3899 if (d->lod_md_root != NULL)
3900 dt_object_put(env, &d->lod_md_root->ldo_obj);
3901 d->lod_md_root = lroot;
3902 spin_unlock(&d->lod_lock);
3906 /* try inherit layout from the root object (fs default) when:
3907 * - parent does not have default layout; or
3908 * - parent has plain(v1/v3) default layout, and some attributes
3909 * are not specified in the default layout;
3911 if (d->lod_md_root != NULL && lod_need_inherit_more(lc, true)) {
3912 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds);
3915 if (lc->ldo_comp_cnt == 0) {
3916 lod_striping_from_default(lc, lds, child_mode);
3917 } else if (!lds->lds_def_striping_is_composite) {
3918 struct lod_layout_component *def_comp;
3920 LASSERT(!lc->ldo_is_composite);
3921 lod_comp = &lc->ldo_comp_entries[0];
3922 def_comp = &lds->lds_def_comp_entries[0];
3924 if (lod_comp->llc_stripenr <= 0)
3925 lod_comp->llc_stripenr = def_comp->llc_stripenr;
3926 if (lod_comp->llc_stripe_size <= 0)
3927 lod_comp->llc_stripe_size =
3928 def_comp->llc_stripe_size;
3929 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
3930 lod_comp->llc_stripe_offset =
3931 def_comp->llc_stripe_offset;
3932 if (lod_comp->llc_pool == NULL)
3933 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
3938 * fs default striping may not be explicitly set, or historically set
3939 * in config log, use them.
3941 if (lod_need_inherit_more(lc, false)) {
3943 if (lc->ldo_comp_cnt == 0) {
3944 rc = lod_alloc_comp_entries(lc, 1);
3946 /* fail to allocate memory, will create a
3947 * non-striped file. */
3949 lc->ldo_is_composite = 0;
3950 lod_comp = &lc->ldo_comp_entries[0];
3951 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
3953 LASSERT(!lc->ldo_is_composite);
3954 lod_comp = &lc->ldo_comp_entries[0];
3955 desc = &d->lod_desc;
3956 if (lod_comp->llc_stripenr <= 0)
3957 lod_comp->llc_stripenr = desc->ld_default_stripe_count;
3958 if (lod_comp->llc_stripe_size <= 0)
3959 lod_comp->llc_stripe_size =
3960 desc->ld_default_stripe_size;
3966 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
3968 * Size initialization on late striping.
3970 * Propagate the size of a truncated object to a deferred striping.
3971 * This function handles a special case when truncate was done on a
3972 * non-striped object and now while the striping is being created
3973 * we can't lose that size, so we have to propagate it to the stripes
3976 * \param[in] env execution environment
3977 * \param[in] dt object
3978 * \param[in] th transaction handle
3980 * \retval 0 on success
3981 * \retval negative if failed
3983 static int lod_declare_init_size(const struct lu_env *env,
3984 struct dt_object *dt, struct thandle *th)
3986 struct dt_object *next = dt_object_child(dt);
3987 struct lod_object *lo = lod_dt_obj(dt);
3988 struct dt_object **objects = NULL;
3989 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3990 uint64_t size, offs;
3991 int i, rc, stripe, stripenr = 0, stripe_size = 0;
3994 if (!lod_obj_is_striped(dt))
3997 rc = dt_attr_get(env, next, attr);
3998 LASSERT(attr->la_valid & LA_SIZE);
4002 size = attr->la_size;
4006 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4007 struct lod_layout_component *lod_comp;
4008 struct lu_extent *extent;
4010 lod_comp = &lo->ldo_comp_entries[i];
4012 if (lod_comp->llc_stripe == NULL)
4015 extent = &lod_comp->llc_extent;
4016 if (!lo->ldo_is_composite ||
4017 (size >= extent->e_start && size < extent->e_end)) {
4018 objects = lod_comp->llc_stripe;
4019 stripenr = lod_comp->llc_stripenr;
4020 stripe_size = lod_comp->llc_stripe_size;
4028 LASSERT(objects != NULL && stripe_size != 0);
4030 /* ll_do_div64(a, b) returns a % b, and a = a / b */
4031 ll_do_div64(size, (__u64)stripe_size);
4032 stripe = ll_do_div64(size, (__u64)stripenr);
4033 LASSERT(objects[stripe] != NULL);
4035 size = size * stripe_size;
4036 offs = attr->la_size;
4037 size += ll_do_div64(offs, stripe_size);
4039 attr->la_valid = LA_SIZE;
4040 attr->la_size = size;
4042 rc = lod_sub_object_declare_attr_set(env, objects[stripe], attr, th);
4048 * Declare creation of striped object.
4050 * The function declares creation stripes for a regular object. The function
4051 * also declares whether the stripes will be created with non-zero size if
4052 * previously size was set non-zero on the master object. If object \a dt is
4053 * not local, then only fully defined striping can be applied in \a lovea.
4054 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
4057 * \param[in] env execution environment
4058 * \param[in] dt object
4059 * \param[in] attr attributes the stripes will be created with
4060 * \param[in] lovea a buffer containing striping description
4061 * \param[in] th transaction handle
4063 * \retval 0 on success
4064 * \retval negative if failed
4066 int lod_declare_striped_object(const struct lu_env *env, struct dt_object *dt,
4067 struct lu_attr *attr,
4068 const struct lu_buf *lovea, struct thandle *th)
4070 struct lod_thread_info *info = lod_env_info(env);
4071 struct dt_object *next = dt_object_child(dt);
4072 struct lod_object *lo = lod_dt_obj(dt);
4076 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
4077 GOTO(out, rc = -ENOMEM);
4079 if (!dt_object_remote(next)) {
4080 /* choose OST and generate appropriate objects */
4081 rc = lod_prepare_create(env, lo, attr, lovea, th);
4086 * declare storage for striping data
4088 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
4090 /* LOD can not choose OST objects for remote objects, i.e.
4091 * stripes must be ready before that. Right now, it can only
4092 * happen during migrate, i.e. migrate process needs to create
4093 * remote regular file (mdd_migrate_create), then the migrate
4094 * process will provide stripeEA. */
4095 LASSERT(lovea != NULL);
4096 info->lti_buf = *lovea;
4099 rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
4100 XATTR_NAME_LOV, 0, th);
4105 * if striping is created with local object's size > 0,
4106 * we have to propagate this size to specific object
4107 * the case is possible only when local object was created previously
4109 if (dt_object_exists(next))
4110 rc = lod_declare_init_size(env, dt, th);
4113 /* failed to create striping or to set initial size, let's reset
4114 * config so that others don't get confused */
4116 lod_object_free_striping(env, lo);
4122 * Implementation of dt_object_operations::do_declare_create.
4124 * The method declares creation of a new object. If the object will be striped,
4125 * then helper functions are called to find FIDs for the stripes, declare
4126 * creation of the stripes and declare initialization of the striping
4127 * information to be stored in the master object.
4129 * \see dt_object_operations::do_declare_create() in the API description
4132 static int lod_declare_object_create(const struct lu_env *env,
4133 struct dt_object *dt,
4134 struct lu_attr *attr,
4135 struct dt_allocation_hint *hint,
4136 struct dt_object_format *dof,
4139 struct dt_object *next = dt_object_child(dt);
4140 struct lod_object *lo = lod_dt_obj(dt);
4149 * first of all, we declare creation of local object
4151 rc = lod_sub_object_declare_create(env, next, attr, hint, dof, th);
4155 if (dof->dof_type == DFT_SYM)
4156 dt->do_body_ops = &lod_body_lnk_ops;
4157 else if (dof->dof_type == DFT_REGULAR)
4158 dt->do_body_ops = &lod_body_ops;
4161 * it's lod_ah_init() that has decided the object will be striped
4163 if (dof->dof_type == DFT_REGULAR) {
4164 /* callers don't want stripes */
4165 /* XXX: all tricky interactions with ->ah_make_hint() decided
4166 * to use striping, then ->declare_create() behaving differently
4167 * should be cleaned */
4168 if (dof->u.dof_reg.striped != 0)
4169 rc = lod_declare_striped_object(env, dt, attr,
4171 } else if (dof->dof_type == DFT_DIR) {
4172 struct seq_server_site *ss;
4174 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
4176 /* If the parent has default stripeEA, and client
4177 * did not find it before sending create request,
4178 * then MDT will return -EREMOTE, and client will
4179 * retrieve the default stripeEA and re-create the
4182 * Note: if dah_eadata != NULL, it means creating the
4183 * striped directory with specified stripeEA, then it
4184 * should ignore the default stripeEA */
4185 if (hint != NULL && hint->dah_eadata == NULL) {
4186 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
4187 GOTO(out, rc = -EREMOTE);
4189 if (lo->ldo_dir_stripe_offset == -1) {
4190 /* child and parent should be in the same MDT */
4191 if (hint->dah_parent != NULL &&
4192 dt_object_remote(hint->dah_parent))
4193 GOTO(out, rc = -EREMOTE);
4194 } else if (lo->ldo_dir_stripe_offset !=
4196 struct lod_device *lod;
4197 struct lod_tgt_descs *ltd;
4198 struct lod_tgt_desc *tgt = NULL;
4199 bool found_mdt = false;
4202 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4203 ltd = &lod->lod_mdt_descs;
4204 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
4205 tgt = LTD_TGT(ltd, i);
4206 if (tgt->ltd_index ==
4207 lo->ldo_dir_stripe_offset) {
4213 /* If the MDT indicated by stripe_offset can be
4214 * found, then tell client to resend the create
4215 * request to the correct MDT, otherwise return
4216 * error to client */
4218 GOTO(out, rc = -EREMOTE);
4220 GOTO(out, rc = -EINVAL);
4224 rc = lod_declare_dir_striping_create(env, dt, attr, dof, th);
4227 /* failed to create striping or to set initial size, let's reset
4228 * config so that others don't get confused */
4230 lod_object_free_striping(env, lo);
4235 * Creation of a striped regular object.
4237 * The function is called to create the stripe objects for a regular
4238 * striped file. This can happen at the initial object creation or
4239 * when the caller asks LOD to do so using ->do_xattr_set() method
4240 * (so called late striping). Notice all the information are already
4241 * prepared in the form of the list of objects (ldo_stripe field).
4242 * This is done during declare phase.
4244 * \param[in] env execution environment
4245 * \param[in] dt object
4246 * \param[in] attr attributes the stripes will be created with
4247 * \param[in] dof format of stripes (see OSD API description)
4248 * \param[in] th transaction handle
4250 * \retval 0 on success
4251 * \retval negative if failed
4253 int lod_striping_create(const struct lu_env *env, struct dt_object *dt,
4254 struct lu_attr *attr, struct dt_object_format *dof,
4257 struct lod_layout_component *lod_comp;
4258 struct lod_object *lo = lod_dt_obj(dt);
4262 LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
4264 /* create all underlying objects */
4265 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4266 lod_comp = &lo->ldo_comp_entries[i];
4268 if (lod_comp->llc_flags & LCME_FL_INIT)
4271 lod_comp->llc_flags |= LCME_FL_INIT;
4273 if (lod_comp->llc_stripe == NULL)
4276 LASSERT(lod_comp->llc_stripenr > 0);
4277 for (j = 0; j < lod_comp->llc_stripenr; j++) {
4278 struct dt_object *object = lod_comp->llc_stripe[j];
4279 LASSERT(object != NULL);
4280 rc = lod_sub_object_create(env, object, attr, NULL,
4288 rc = lod_generate_and_set_lovea(env, lo, th);
4291 lo->ldo_comp_cached = 1;
4293 lod_object_free_striping(env, lo);
4299 * Implementation of dt_object_operations::do_create.
4301 * If any of preceeding methods (like ->do_declare_create(),
4302 * ->do_ah_init(), etc) chose to create a striped object,
4303 * then this method will create the master and the stripes.
4305 * \see dt_object_operations::do_create() in the API description for details.
4307 static int lod_object_create(const struct lu_env *env, struct dt_object *dt,
4308 struct lu_attr *attr,
4309 struct dt_allocation_hint *hint,
4310 struct dt_object_format *dof, struct thandle *th)
4315 /* create local object */
4316 rc = lod_sub_object_create(env, dt_object_child(dt), attr, hint, dof,
4321 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4322 lod_obj_is_striped(dt) && dof->u.dof_reg.striped != 0) {
4323 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
4324 rc = lod_striping_create(env, dt, attr, dof, th);
4331 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
4332 struct dt_object *dt, struct thandle *th,
4333 int stripe_idx, struct lod_obj_stripe_cb_data *data)
4335 if (data->locd_declare)
4336 return lod_sub_object_declare_destroy(env, dt, th);
4337 else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4338 stripe_idx == cfs_fail_val)
4339 return lod_sub_object_destroy(env, dt, th);
4345 * Implementation of dt_object_operations::do_declare_destroy.
4347 * If the object is a striped directory, then the function declares reference
4348 * removal from the master object (this is an index) to the stripes and declares
4349 * destroy of all the stripes. In all the cases, it declares an intention to
4350 * destroy the object itself.
4352 * \see dt_object_operations::do_declare_destroy() in the API description
4355 static int lod_declare_object_destroy(const struct lu_env *env,
4356 struct dt_object *dt,
4359 struct dt_object *next = dt_object_child(dt);
4360 struct lod_object *lo = lod_dt_obj(dt);
4361 struct lod_thread_info *info = lod_env_info(env);
4362 char *stripe_name = info->lti_key;
4367 * load striping information, notice we don't do this when object
4368 * is being initialized as we don't need this information till
4369 * few specific cases like destroy, chown
4371 rc = lod_load_striping(env, lo);
4375 /* declare destroy for all underlying objects */
4376 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4377 rc = next->do_ops->do_index_try(env, next,
4378 &dt_directory_features);
4382 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4383 rc = lod_sub_object_declare_ref_del(env, next, th);
4387 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4388 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4390 rc = lod_sub_object_declare_delete(env, next,
4391 (const struct dt_key *)stripe_name, th);
4398 * we declare destroy for the local object
4400 rc = lod_sub_object_declare_destroy(env, next, th);
4404 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4405 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4408 if (!lod_obj_is_striped(dt))
4411 /* declare destroy all striped objects */
4412 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4413 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4414 if (lo->ldo_stripe[i] == NULL)
4417 rc = lod_sub_object_declare_ref_del(env,
4418 lo->ldo_stripe[i], th);
4420 rc = lod_sub_object_declare_destroy(env,
4421 lo->ldo_stripe[i], th);
4426 struct lod_obj_stripe_cb_data data;
4428 data.locd_declare = true;
4429 rc = lod_obj_for_each_stripe(env, lo, th,
4430 lod_obj_stripe_destroy_cb, &data);
4437 * Implementation of dt_object_operations::do_destroy.
4439 * If the object is a striped directory, then the function removes references
4440 * from the master object (this is an index) to the stripes and destroys all
4441 * the stripes. In all the cases, the function destroys the object itself.
4443 * \see dt_object_operations::do_destroy() in the API description for details.
4445 static int lod_object_destroy(const struct lu_env *env,
4446 struct dt_object *dt, struct thandle *th)
4448 struct dt_object *next = dt_object_child(dt);
4449 struct lod_object *lo = lod_dt_obj(dt);
4450 struct lod_thread_info *info = lod_env_info(env);
4451 char *stripe_name = info->lti_key;
4456 /* destroy sub-stripe of master object */
4457 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4458 rc = next->do_ops->do_index_try(env, next,
4459 &dt_directory_features);
4463 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4464 rc = lod_sub_object_ref_del(env, next, th);
4468 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4469 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4472 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
4473 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
4474 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
4476 rc = lod_sub_object_delete(env, next,
4477 (const struct dt_key *)stripe_name, th);
4483 rc = lod_sub_object_destroy(env, next, th);
4487 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4488 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4491 if (!lod_obj_is_striped(dt))
4494 /* destroy all striped objects */
4495 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4496 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4497 if (lo->ldo_stripe[i] == NULL)
4499 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4500 i == cfs_fail_val) {
4501 dt_write_lock(env, lo->ldo_stripe[i],
4503 rc = lod_sub_object_ref_del(env,
4504 lo->ldo_stripe[i], th);
4505 dt_write_unlock(env, lo->ldo_stripe[i]);
4509 rc = lod_sub_object_destroy(env,
4510 lo->ldo_stripe[i], th);
4516 struct lod_obj_stripe_cb_data data;
4518 data.locd_declare = false;
4519 rc = lod_obj_for_each_stripe(env, lo, th,
4520 lod_obj_stripe_destroy_cb, &data);
4527 * Implementation of dt_object_operations::do_declare_ref_add.
4529 * \see dt_object_operations::do_declare_ref_add() in the API description
4532 static int lod_declare_ref_add(const struct lu_env *env,
4533 struct dt_object *dt, struct thandle *th)
4535 return lod_sub_object_declare_ref_add(env, dt_object_child(dt), th);
4539 * Implementation of dt_object_operations::do_ref_add.
4541 * \see dt_object_operations::do_ref_add() in the API description for details.
4543 static int lod_ref_add(const struct lu_env *env,
4544 struct dt_object *dt, struct thandle *th)
4546 return lod_sub_object_ref_add(env, dt_object_child(dt), th);
4550 * Implementation of dt_object_operations::do_declare_ref_del.
4552 * \see dt_object_operations::do_declare_ref_del() in the API description
4555 static int lod_declare_ref_del(const struct lu_env *env,
4556 struct dt_object *dt, struct thandle *th)
4558 return lod_sub_object_declare_ref_del(env, dt_object_child(dt), th);
4562 * Implementation of dt_object_operations::do_ref_del
4564 * \see dt_object_operations::do_ref_del() in the API description for details.
4566 static int lod_ref_del(const struct lu_env *env,
4567 struct dt_object *dt, struct thandle *th)
4569 return lod_sub_object_ref_del(env, dt_object_child(dt), th);
4573 * Implementation of dt_object_operations::do_object_sync.
4575 * \see dt_object_operations::do_object_sync() in the API description
4578 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
4579 __u64 start, __u64 end)
4581 return dt_object_sync(env, dt_object_child(dt), start, end);
4585 * Release LDLM locks on the stripes of a striped directory.
4587 * Iterates over all the locks taken on the stripe objects and
4590 * \param[in] env execution environment
4591 * \param[in] dt striped object
4592 * \param[in] einfo lock description
4593 * \param[in] policy data describing requested lock
4595 * \retval 0 on success
4596 * \retval negative if failed
4598 static int lod_object_unlock_internal(const struct lu_env *env,
4599 struct dt_object *dt,
4600 struct ldlm_enqueue_info *einfo,
4601 union ldlm_policy_data *policy)
4603 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
4608 if (slave_locks == NULL)
4611 for (i = 1; i < slave_locks->count; i++) {
4612 if (lustre_handle_is_used(&slave_locks->handles[i]))
4613 ldlm_lock_decref_and_cancel(&slave_locks->handles[i],
4621 * Implementation of dt_object_operations::do_object_unlock.
4623 * Used to release LDLM lock(s).
4625 * \see dt_object_operations::do_object_unlock() in the API description
4628 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
4629 struct ldlm_enqueue_info *einfo,
4630 union ldlm_policy_data *policy)
4632 struct lod_object *lo = lod_dt_obj(dt);
4633 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
4634 int slave_locks_size;
4638 if (slave_locks == NULL)
4641 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
4642 LASSERT(lo->ldo_dir_stripenr > 1);
4643 /* Note: for remote lock for single stripe dir, MDT will cancel
4644 * the lock by lockh directly */
4645 LASSERT(!dt_object_remote(dt_object_child(dt)));
4647 /* locks were unlocked in MDT layer */
4648 for (i = 1; i < slave_locks->count; i++) {
4649 LASSERT(!lustre_handle_is_used(&slave_locks->handles[i]));
4650 dt_invalidate(env, lo->ldo_stripe[i]);
4653 slave_locks_size = sizeof(*slave_locks) + slave_locks->count *
4654 sizeof(slave_locks->handles[0]);
4655 OBD_FREE(slave_locks, slave_locks_size);
4656 einfo->ei_cbdata = NULL;
4662 * Implementation of dt_object_operations::do_object_lock.
4664 * Used to get LDLM lock on the non-striped and striped objects.
4666 * \see dt_object_operations::do_object_lock() in the API description
4669 static int lod_object_lock(const struct lu_env *env,
4670 struct dt_object *dt,
4671 struct lustre_handle *lh,
4672 struct ldlm_enqueue_info *einfo,
4673 union ldlm_policy_data *policy)
4675 struct lod_object *lo = lod_dt_obj(dt);
4678 int slave_locks_size;
4679 struct lustre_handle_array *slave_locks = NULL;
4682 /* remote object lock */
4683 if (!einfo->ei_enq_slave) {
4684 LASSERT(dt_object_remote(dt));
4685 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
4689 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4690 GOTO(out, rc = -ENOTDIR);
4692 rc = lod_load_striping(env, lo);
4697 if (lo->ldo_dir_stripenr <= 1) {
4699 * NB, ei_cbdata stores pointer to slave locks, if no locks
4700 * taken, make sure it's set to NULL, otherwise MDT will try to
4703 einfo->ei_cbdata = NULL;
4707 slave_locks_size = sizeof(*slave_locks) + lo->ldo_dir_stripenr *
4708 sizeof(slave_locks->handles[0]);
4709 /* Freed in lod_object_unlock */
4710 OBD_ALLOC(slave_locks, slave_locks_size);
4711 if (slave_locks == NULL)
4712 GOTO(out, rc = -ENOMEM);
4713 slave_locks->count = lo->ldo_dir_stripenr;
4715 /* striped directory lock */
4716 for (i = 1; i < lo->ldo_dir_stripenr; i++) {
4717 struct lustre_handle lockh;
4718 struct ldlm_res_id *res_id;
4720 res_id = &lod_env_info(env)->lti_res_id;
4721 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
4723 einfo->ei_res_id = res_id;
4725 LASSERT(lo->ldo_stripe[i] != NULL);
4726 if (likely(dt_object_remote(lo->ldo_stripe[i]))) {
4727 rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh,
4730 struct ldlm_namespace *ns = einfo->ei_namespace;
4731 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
4732 ldlm_completion_callback completion = einfo->ei_cb_cp;
4733 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
4735 if (einfo->ei_mode == LCK_PW ||
4736 einfo->ei_mode == LCK_EX)
4737 dlmflags |= LDLM_FL_COS_INCOMPAT;
4739 /* This only happens if there are mulitple stripes
4740 * on the master MDT, i.e. except stripe0, there are
4741 * other stripes on the Master MDT as well, Only
4742 * happens in the test case right now. */
4743 LASSERT(ns != NULL);
4744 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS,
4745 policy, einfo->ei_mode,
4746 &dlmflags, blocking,
4748 NULL, 0, LVB_T_NONE,
4753 slave_locks->handles[i] = lockh;
4755 einfo->ei_cbdata = slave_locks;
4757 if (rc != 0 && slave_locks != NULL) {
4758 lod_object_unlock_internal(env, dt, einfo, policy);
4759 OBD_FREE(slave_locks, slave_locks_size);
4764 einfo->ei_cbdata = NULL;
4769 * Implementation of dt_object_operations::do_invalidate.
4771 * \see dt_object_operations::do_invalidate() in the API description for details
4773 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
4775 return dt_invalidate(env, dt_object_child(dt));
4778 struct dt_object_operations lod_obj_ops = {
4779 .do_read_lock = lod_object_read_lock,
4780 .do_write_lock = lod_object_write_lock,
4781 .do_read_unlock = lod_object_read_unlock,
4782 .do_write_unlock = lod_object_write_unlock,
4783 .do_write_locked = lod_object_write_locked,
4784 .do_attr_get = lod_attr_get,
4785 .do_declare_attr_set = lod_declare_attr_set,
4786 .do_attr_set = lod_attr_set,
4787 .do_xattr_get = lod_xattr_get,
4788 .do_declare_xattr_set = lod_declare_xattr_set,
4789 .do_xattr_set = lod_xattr_set,
4790 .do_declare_xattr_del = lod_declare_xattr_del,
4791 .do_xattr_del = lod_xattr_del,
4792 .do_xattr_list = lod_xattr_list,
4793 .do_ah_init = lod_ah_init,
4794 .do_declare_create = lod_declare_object_create,
4795 .do_create = lod_object_create,
4796 .do_declare_destroy = lod_declare_object_destroy,
4797 .do_destroy = lod_object_destroy,
4798 .do_index_try = lod_index_try,
4799 .do_declare_ref_add = lod_declare_ref_add,
4800 .do_ref_add = lod_ref_add,
4801 .do_declare_ref_del = lod_declare_ref_del,
4802 .do_ref_del = lod_ref_del,
4803 .do_object_sync = lod_object_sync,
4804 .do_object_lock = lod_object_lock,
4805 .do_object_unlock = lod_object_unlock,
4806 .do_invalidate = lod_invalidate,
4810 * Implementation of dt_body_operations::dbo_read.
4812 * \see dt_body_operations::dbo_read() in the API description for details.
4814 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
4815 struct lu_buf *buf, loff_t *pos)
4817 struct dt_object *next = dt_object_child(dt);
4818 return next->do_body_ops->dbo_read(env, next, buf, pos);
4822 * Implementation of dt_body_operations::dbo_declare_write.
4824 * \see dt_body_operations::dbo_declare_write() in the API description
4827 static ssize_t lod_declare_write(const struct lu_env *env,
4828 struct dt_object *dt,
4829 const struct lu_buf *buf, loff_t pos,
4832 return lod_sub_object_declare_write(env, dt_object_child(dt), buf, pos,
4837 * Implementation of dt_body_operations::dbo_write.
4839 * \see dt_body_operations::dbo_write() in the API description for details.
4841 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
4842 const struct lu_buf *buf, loff_t *pos,
4843 struct thandle *th, int iq)
4845 return lod_sub_object_write(env, dt_object_child(dt), buf, pos, th, iq);
4848 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
4849 __u64 start, __u64 end, struct thandle *th)
4851 if (dt_object_remote(dt))
4854 return lod_sub_object_declare_punch(env, dt_object_child(dt), start,
4858 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
4859 __u64 start, __u64 end, struct thandle *th)
4861 if (dt_object_remote(dt))
4864 return lod_sub_object_punch(env, dt_object_child(dt), start, end, th);
4867 static const struct dt_body_operations lod_body_lnk_ops = {
4868 .dbo_read = lod_read,
4869 .dbo_declare_write = lod_declare_write,
4870 .dbo_write = lod_write
4873 static const struct dt_body_operations lod_body_ops = {
4874 .dbo_read = lod_read,
4875 .dbo_declare_write = lod_declare_write,
4876 .dbo_write = lod_write,
4877 .dbo_declare_punch = lod_declare_punch,
4878 .dbo_punch = lod_punch,
4882 * Implementation of lu_object_operations::loo_object_init.
4884 * The function determines the type and the index of the target device using
4885 * sequence of the object's FID. Then passes control down to the
4886 * corresponding device:
4887 * OSD for the local objects, OSP for remote
4889 * \see lu_object_operations::loo_object_init() in the API description
4892 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
4893 const struct lu_object_conf *conf)
4895 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
4896 struct lu_device *cdev = NULL;
4897 struct lu_object *cobj;
4898 struct lod_tgt_descs *ltd = NULL;
4899 struct lod_tgt_desc *tgt;
4901 int type = LU_SEQ_RANGE_ANY;
4905 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
4907 /* Note: Sometimes, it will Return EAGAIN here, see
4908 * ptrlpc_import_delay_req(), which might confuse
4909 * lu_object_find_at() and make it wait there incorrectly.
4910 * so we convert it to EIO here.*/
4917 if (type == LU_SEQ_RANGE_MDT &&
4918 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
4919 cdev = &lod->lod_child->dd_lu_dev;
4920 } else if (type == LU_SEQ_RANGE_MDT) {
4921 ltd = &lod->lod_mdt_descs;
4923 } else if (type == LU_SEQ_RANGE_OST) {
4924 ltd = &lod->lod_ost_descs;
4931 if (ltd->ltd_tgts_size > idx &&
4932 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
4933 tgt = LTD_TGT(ltd, idx);
4935 LASSERT(tgt != NULL);
4936 LASSERT(tgt->ltd_tgt != NULL);
4938 cdev = &(tgt->ltd_tgt->dd_lu_dev);
4940 lod_putref(lod, ltd);
4943 if (unlikely(cdev == NULL))
4946 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
4947 if (unlikely(cobj == NULL))
4950 lu_object_add(lo, cobj);
4957 * Release resources associated with striping.
4959 * If the object is striped (regular or directory), then release
4960 * the stripe objects references and free the ldo_stripe array.
4962 * \param[in] env execution environment
4963 * \param[in] lo object
4965 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
4967 struct lod_layout_component *lod_comp;
4970 if (lo->ldo_stripe != NULL) {
4971 LASSERT(lo->ldo_comp_entries == NULL);
4972 LASSERT(lo->ldo_dir_stripes_allocated > 0);
4974 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4975 if (lo->ldo_stripe[i])
4976 dt_object_put(env, lo->ldo_stripe[i]);
4979 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
4980 OBD_FREE(lo->ldo_stripe, j);
4981 lo->ldo_stripe = NULL;
4982 lo->ldo_dir_stripes_allocated = 0;
4983 lo->ldo_dir_stripenr = 0;
4984 } else if (lo->ldo_comp_entries != NULL) {
4985 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4986 /* free lod_layout_component::llc_stripe array */
4987 lod_comp = &lo->ldo_comp_entries[i];
4989 if (lod_comp->llc_stripe == NULL)
4991 LASSERT(lod_comp->llc_stripes_allocated != 0);
4992 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
4993 if (lod_comp->llc_stripe[j] != NULL)
4995 &lod_comp->llc_stripe[j]->do_lu);
4997 OBD_FREE(lod_comp->llc_stripe,
4998 sizeof(struct dt_object *) *
4999 lod_comp->llc_stripes_allocated);
5000 lod_comp->llc_stripe = NULL;
5001 lod_comp->llc_stripes_allocated = 0;
5003 lod_free_comp_entries(lo);
5004 lo->ldo_comp_cached = 0;
5009 * Implementation of lu_object_operations::loo_object_start.
5011 * \see lu_object_operations::loo_object_start() in the API description
5014 static int lod_object_start(const struct lu_env *env, struct lu_object *o)
5016 if (S_ISLNK(o->lo_header->loh_attr & S_IFMT)) {
5017 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_lnk_ops;
5018 } else if (S_ISREG(o->lo_header->loh_attr & S_IFMT) ||
5019 fid_is_local_file(lu_object_fid(o))) {
5020 /* Note: some local file (like last rcvd) is created
5021 * through bottom layer (OSD), so the object initialization
5022 * comes to lod, it does not set loh_attr yet, so
5023 * set do_body_ops for local file anyway */
5024 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_ops;
5030 * Implementation of lu_object_operations::loo_object_free.
5032 * \see lu_object_operations::loo_object_free() in the API description
5035 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
5037 struct lod_object *lo = lu2lod_obj(o);
5039 /* release all underlying object pinned */
5040 lod_object_free_striping(env, lo);
5042 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
5046 * Implementation of lu_object_operations::loo_object_release.
5048 * \see lu_object_operations::loo_object_release() in the API description
5051 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
5053 /* XXX: shouldn't we release everything here in case if object
5054 * creation failed before? */
5058 * Implementation of lu_object_operations::loo_object_print.
5060 * \see lu_object_operations::loo_object_print() in the API description
5063 static int lod_object_print(const struct lu_env *env, void *cookie,
5064 lu_printer_t p, const struct lu_object *l)
5066 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
5068 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
5071 struct lu_object_operations lod_lu_obj_ops = {
5072 .loo_object_init = lod_object_init,
5073 .loo_object_start = lod_object_start,
5074 .loo_object_free = lod_object_free,
5075 .loo_object_release = lod_object_release,
5076 .loo_object_print = lod_object_print,