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 dt_object *obj = &lo->ldo_obj;
2044 struct lu_buf *buf = &info->lti_buf;
2045 struct filter_fid *ff = buf->lb_buf;
2048 rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2052 fid_le_to_cpu(&ff->ff_parent, &ff->ff_parent);
2053 ff->ff_parent.f_seq = lu_object_fid(&obj->do_lu)->f_seq;
2054 ff->ff_parent.f_oid = lu_object_fid(&obj->do_lu)->f_oid;
2055 fid_cpu_to_le(&ff->ff_parent, &ff->ff_parent);
2057 if (data->locd_declare)
2058 rc = lod_sub_object_declare_xattr_set(env, dt, buf,
2060 LU_XATTR_REPLACE, th);
2062 rc = lod_sub_object_xattr_set(env, dt, buf, XATTR_NAME_FID,
2063 LU_XATTR_REPLACE, th);
2069 * Reset parent FID on OST object
2071 * Replace parent FID with @dt object FID, which is only called during migration
2072 * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2073 * the FID is changed.
2075 * \param[in] env execution environment
2076 * \param[in] dt dt_object whose stripes's parent FID will be reset
2077 * \parem[in] th thandle
2078 * \param[in] declare if it is declare
2080 * \retval 0 if reset succeeds
2081 * \retval negative errno if reset fails
2083 static int lod_object_replace_parent_fid(const struct lu_env *env,
2084 struct dt_object *dt,
2085 struct thandle *th, bool declare)
2087 struct lod_object *lo = lod_dt_obj(dt);
2088 struct lod_thread_info *info = lod_env_info(env);
2089 struct lu_buf *buf = &info->lti_buf;
2090 struct filter_fid *ff;
2091 struct lod_obj_stripe_cb_data data;
2095 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2097 /* set xattr to each stripes, if needed */
2098 rc = lod_load_striping(env, lo);
2102 if (!lod_obj_is_striped(dt))
2105 if (info->lti_ea_store_size < sizeof(*ff)) {
2106 rc = lod_ea_store_resize(info, sizeof(*ff));
2111 buf->lb_buf = info->lti_ea_store;
2112 buf->lb_len = info->lti_ea_store_size;
2114 data.locd_declare = declare;
2115 rc = lod_obj_for_each_stripe(env, lo, th,
2116 lod_obj_stripe_replace_parent_fid_cb, &data);
2122 * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2123 * the xattr value is binary lov_comp_md_v1 which contains component(s)
2126 * \param[in] env execution environment
2127 * \param[in] dt dt_object to add components on
2128 * \param[in] buf buffer contains components to be added
2129 * \parem[in] th thandle
2131 * \retval 0 on success
2132 * \retval negative errno on failure
2134 static int lod_declare_layout_add(const struct lu_env *env,
2135 struct dt_object *dt,
2136 const struct lu_buf *buf,
2139 struct lod_layout_component *comp_array, *lod_comp;
2140 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2141 struct lov_desc *desc = &d->lod_desc;
2142 struct lod_object *lo = lod_dt_obj(dt);
2143 struct lov_user_md_v1 *v1;
2144 struct lov_user_md_v3 *v3;
2145 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2146 struct lu_extent *ext;
2149 int i, rc, array_cnt;
2152 LASSERT(lo->ldo_is_composite);
2154 magic = comp_v1->lcm_magic;
2155 /* Replay request, see comment for LOV_MAGIC_DEF */
2156 if (unlikely(le32_to_cpu(magic) == LOV_MAGIC_COMP_V1_DEF)) {
2157 struct dt_object *next = dt_object_child(dt);
2159 lod_object_free_striping(env, lo);
2160 rc = lod_use_defined_striping(env, lo, buf);
2162 lo->ldo_comp_cached = 1;
2163 rc = lod_sub_object_declare_xattr_set(env, next, buf,
2170 prev_end = lo->ldo_comp_entries[lo->ldo_comp_cnt - 1].llc_extent.e_end;
2171 rc = lod_verify_striping(d, buf, false, prev_end);
2175 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2176 lustre_swab_lov_comp_md_v1(comp_v1);
2177 magic = comp_v1->lcm_magic;
2180 if (magic != LOV_USER_MAGIC_COMP_V1)
2183 array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2184 OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2185 if (comp_array == NULL)
2188 memcpy(comp_array, lo->ldo_comp_entries,
2189 sizeof(*comp_array) * lo->ldo_comp_cnt);
2191 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2192 v1 = (struct lov_user_md *)((char *)comp_v1 +
2193 comp_v1->lcm_entries[i].lcme_offset);
2194 ext = &comp_v1->lcm_entries[i].lcme_extent;
2196 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2197 lod_comp->llc_extent.e_start = ext->e_start;
2198 lod_comp->llc_extent.e_end = ext->e_end;
2199 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2201 lod_comp->llc_stripenr = v1->lmm_stripe_count;
2202 if (lod_comp->llc_stripenr <= 0)
2203 lod_comp->llc_stripenr = desc->ld_default_stripe_count;
2204 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2205 if (lod_comp->llc_stripe_size <= 0)
2206 lod_comp->llc_stripe_size =
2207 desc->ld_default_stripe_size;
2209 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2211 v3 = (struct lov_user_md_v3 *) v1;
2212 if (v3->lmm_pool_name[0] != '\0') {
2213 len = strlen(v3->lmm_pool_name);
2214 OBD_ALLOC(lod_comp->llc_pool, len + 1);
2215 if (lod_comp->llc_pool == NULL)
2216 GOTO(error, rc = -ENOMEM);
2217 strncpy(lod_comp->llc_pool, v3->lmm_pool_name,
2223 OBD_FREE(lo->ldo_comp_entries, sizeof(*lod_comp) * lo->ldo_comp_cnt);
2224 lo->ldo_comp_entries = comp_array;
2225 lo->ldo_comp_cnt = array_cnt;
2226 /* No need to increase layout generation here, it will be increased
2227 * later when generating component ID for the new components */
2229 rc = lod_declare_striped_object(env, dt, NULL, NULL, th);
2233 for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2234 lod_comp = &comp_array[i];
2235 if (lod_comp->llc_pool != NULL) {
2236 OBD_FREE(lod_comp->llc_pool,
2237 strlen(lod_comp->llc_pool) + 1);
2238 lod_comp->llc_pool = NULL;
2241 OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2245 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2247 int magic, size = 0, i;
2248 struct lod_layout_component *comp_entries;
2253 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2254 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2256 lo->ldo_def_striping->lds_def_striping_is_composite;
2258 comp_cnt = lo->ldo_comp_cnt;
2259 comp_entries = lo->ldo_comp_entries;
2260 is_composite = lo->ldo_is_composite;
2264 LASSERT(comp_cnt != 0 && comp_entries != NULL);
2266 size = sizeof(struct lov_comp_md_v1) +
2267 sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2268 LASSERT(size % sizeof(__u64) == 0);
2271 for (i = 0; i < comp_cnt; i++) {
2272 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2274 size += lov_user_md_size(
2275 is_dir ? 0 : comp_entries[i].llc_stripenr,
2277 LASSERT(size % sizeof(__u64) == 0);
2283 * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2284 * the '$field' can only be 'flags' now. The xattr value is binary
2285 * lov_comp_md_v1 which contains the component ID(s) and the value of
2286 * the field to be modified.
2288 * \param[in] env execution environment
2289 * \param[in] dt dt_object to be modified
2290 * \param[in] op operation string, like "set.flags"
2291 * \param[in] buf buffer contains components to be set
2292 * \parem[in] th thandle
2294 * \retval 0 on success
2295 * \retval negative errno on failure
2297 static int lod_declare_layout_set(const struct lu_env *env,
2298 struct dt_object *dt,
2299 char *op, const struct lu_buf *buf,
2302 struct lod_layout_component *lod_comp;
2303 struct lod_thread_info *info = lod_env_info(env);
2304 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2305 struct lod_object *lo = lod_dt_obj(dt);
2306 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2309 bool changed = false;
2312 if (strcmp(op, "set.flags") != 0) {
2313 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2314 lod2obd(d)->obd_name, op);
2318 magic = comp_v1->lcm_magic;
2319 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2320 lustre_swab_lov_comp_md_v1(comp_v1);
2321 magic = comp_v1->lcm_magic;
2324 if (magic != LOV_USER_MAGIC_COMP_V1)
2327 if (comp_v1->lcm_entry_count == 0) {
2328 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2329 lod2obd(d)->obd_name);
2333 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2334 id = comp_v1->lcm_entries[i].lcme_id;
2336 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2337 lod_comp = &lo->ldo_comp_entries[j];
2338 if (id == lod_comp->llc_id || id == LCME_ID_ALL) {
2339 lod_comp->llc_flags =
2340 comp_v1->lcm_entries[i].lcme_flags;
2347 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2348 lod2obd(d)->obd_name);
2352 lod_obj_inc_layout_gen(lo);
2354 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2355 rc = lod_sub_object_declare_xattr_set(env, dt, &info->lti_buf,
2356 XATTR_NAME_LOV, 0, th);
2361 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2362 * and the xattr value is a unique component ID or a special lcme_id.
2364 * \param[in] env execution environment
2365 * \param[in] dt dt_object to be operated on
2366 * \param[in] buf buffer contains component ID or lcme_id
2367 * \parem[in] th thandle
2369 * \retval 0 on success
2370 * \retval negative errno on failure
2372 static int lod_declare_layout_del(const struct lu_env *env,
2373 struct dt_object *dt,
2374 const struct lu_buf *buf,
2377 struct lod_thread_info *info = lod_env_info(env);
2378 struct dt_object *next = dt_object_child(dt);
2379 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2380 struct lod_object *lo = lod_dt_obj(dt);
2381 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2386 LASSERT(lo->ldo_is_composite);
2388 id = *(__u32 *)buf->lb_buf;
2389 if (id == 0 || id == LCME_ID_NONE) {
2390 CDEBUG(D_LAYOUT, "%s: invalid component id %#x\n",
2391 lod2obd(d)->obd_name, id);
2395 left = lo->ldo_comp_cnt;
2399 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
2400 struct lod_layout_component *lod_comp;
2402 lod_comp = &lo->ldo_comp_entries[i];
2404 if (id <= LCME_ID_MAX && id != lod_comp->llc_id)
2406 else if (id > LCME_ID_MAX && id < LCME_ID_ALL &&
2407 !(id & lod_comp->llc_flags))
2410 if (left != (i + 1)) {
2411 CDEBUG(D_LAYOUT, "%s: this deletion will create "
2412 "a hole.\n", lod2obd(d)->obd_name);
2417 /* Mark the component as deleted */
2418 lod_comp->llc_id = LCME_ID_INVAL;
2420 /* Not instantiated component */
2421 if (lod_comp->llc_stripe == NULL)
2424 LASSERT(lod_comp->llc_stripenr > 0);
2425 for (j = 0; j < lod_comp->llc_stripenr; j++) {
2426 struct dt_object *obj = lod_comp->llc_stripe[j];
2430 rc = lod_sub_object_declare_destroy(env, obj, th);
2436 LASSERTF(left >= 0, "left = %d\n", left);
2437 if (left == lo->ldo_comp_cnt) {
2438 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
2439 lod2obd(d)->obd_name, id);
2443 memset(attr, 0, sizeof(*attr));
2444 attr->la_valid = LA_SIZE;
2445 rc = lod_sub_object_declare_attr_set(env, next, attr, th);
2450 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2451 rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
2452 XATTR_NAME_LOV, 0, th);
2454 rc = lod_sub_object_declare_xattr_del(env, next, XATTR_NAME_LOV,
2462 * Declare layout add/set/del operations issued by special xattr names:
2464 * XATTR_LUSTRE_LOV.add add component(s) to existing file
2465 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
2466 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
2468 * \param[in] env execution environment
2469 * \param[in] dt object
2470 * \param[in] name name of xattr
2471 * \param[in] buf lu_buf contains xattr value
2472 * \param[in] th transaction handle
2474 * \retval 0 on success
2475 * \retval negative if failed
2477 static int lod_declare_modify_layout(const struct lu_env *env,
2478 struct dt_object *dt,
2480 const struct lu_buf *buf,
2483 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2484 struct lod_object *lo = lod_dt_obj(dt);
2485 struct dt_object *next = dt_object_child(&lo->ldo_obj);
2487 int rc, len = strlen(XATTR_LUSTRE_LOV);
2490 LASSERT(dt_object_exists(dt));
2492 if (strlen(name) <= len || name[len] != '.') {
2493 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
2494 lod2obd(d)->obd_name, name);
2499 dt_write_lock(env, next, 0);
2500 rc = lod_load_striping_locked(env, lo);
2504 /* the layout to be modified must be a composite layout */
2505 if (!lo->ldo_is_composite) {
2506 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
2507 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
2508 GOTO(unlock, rc = -EINVAL);
2511 op = (char *)name + len;
2512 if (strcmp(op, "add") == 0) {
2513 rc = lod_declare_layout_add(env, dt, buf, th);
2514 } else if (strcmp(op, "del") == 0) {
2515 rc = lod_declare_layout_del(env, dt, buf, th);
2516 } else if (strncmp(op, "set", strlen("set")) == 0) {
2517 rc = lod_declare_layout_set(env, dt, op, buf, th);
2519 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
2520 lod2obd(d)->obd_name, name);
2521 GOTO(unlock, rc = -ENOTSUPP);
2525 lod_object_free_striping(env, lo);
2526 dt_write_unlock(env, next);
2532 * Implementation of dt_object_operations::do_declare_xattr_set.
2534 * \see dt_object_operations::do_declare_xattr_set() in the API description
2537 * the extension to the API:
2538 * - declaring LOVEA requests striping creation
2539 * - LU_XATTR_REPLACE means layout swap
2541 static int lod_declare_xattr_set(const struct lu_env *env,
2542 struct dt_object *dt,
2543 const struct lu_buf *buf,
2544 const char *name, int fl,
2547 struct dt_object *next = dt_object_child(dt);
2548 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2553 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2554 if ((S_ISREG(mode) || mode == 0) && !(fl & LU_XATTR_REPLACE) &&
2555 (strcmp(name, XATTR_NAME_LOV) == 0 ||
2556 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
2558 * this is a request to create object's striping.
2560 * allow to declare predefined striping on a new (!mode) object
2561 * which is supposed to be replay of regular file creation
2562 * (when LOV setting is declared)
2564 * LU_XATTR_REPLACE is set to indicate a layout swap
2566 if (dt_object_exists(dt)) {
2567 rc = dt_attr_get(env, next, attr);
2571 memset(attr, 0, sizeof(*attr));
2572 attr->la_valid = LA_TYPE | LA_MODE;
2573 attr->la_mode = S_IFREG;
2575 rc = lod_declare_striped_object(env, dt, attr, buf, th);
2576 } else if (S_ISREG(mode) &&
2577 strlen(name) > strlen(XATTR_LUSTRE_LOV) + 1 &&
2578 strncmp(name, XATTR_LUSTRE_LOV,
2579 strlen(XATTR_LUSTRE_LOV)) == 0) {
2581 * this is a request to modify object's striping.
2582 * add/set/del component(s).
2584 if (!dt_object_exists(dt))
2587 rc = lod_declare_modify_layout(env, dt, name, buf, th);
2588 } else if (S_ISDIR(mode)) {
2589 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2590 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
2591 rc = lod_object_replace_parent_fid(env, dt, th, true);
2593 rc = lod_sub_object_declare_xattr_set(env, next, buf, name,
2601 * Apply xattr changes to the object.
2603 * Applies xattr changes to the object and the stripes if the latter exist.
2605 * \param[in] env execution environment
2606 * \param[in] dt object
2607 * \param[in] buf buffer pointing to the new value of xattr
2608 * \param[in] name name of xattr
2609 * \param[in] fl flags
2610 * \param[in] th transaction handle
2612 * \retval 0 on success
2613 * \retval negative if failed
2615 static int lod_xattr_set_internal(const struct lu_env *env,
2616 struct dt_object *dt,
2617 const struct lu_buf *buf,
2618 const char *name, int fl,
2621 struct dt_object *next = dt_object_child(dt);
2622 struct lod_object *lo = lod_dt_obj(dt);
2627 rc = lod_sub_object_xattr_set(env, next, buf, name, fl, th);
2628 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2631 /* Note: Do not set LinkEA on sub-stripes, otherwise
2632 * it will confuse the fid2path process(see mdt_path_current()).
2633 * The linkEA between master and sub-stripes is set in
2634 * lod_xattr_set_lmv(). */
2635 if (lo->ldo_dir_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2638 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2639 LASSERT(lo->ldo_stripe[i]);
2641 rc = lod_sub_object_xattr_set(env, lo->ldo_stripe[i], buf, name,
2651 * Delete an extended attribute.
2653 * Deletes specified xattr from the object and the stripes if the latter exist.
2655 * \param[in] env execution environment
2656 * \param[in] dt object
2657 * \param[in] name name of xattr
2658 * \param[in] th transaction handle
2660 * \retval 0 on success
2661 * \retval negative if failed
2663 static int lod_xattr_del_internal(const struct lu_env *env,
2664 struct dt_object *dt,
2665 const char *name, struct thandle *th)
2667 struct dt_object *next = dt_object_child(dt);
2668 struct lod_object *lo = lod_dt_obj(dt);
2673 rc = lod_sub_object_xattr_del(env, next, name, th);
2674 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2677 if (lo->ldo_dir_stripenr == 0)
2680 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2681 LASSERT(lo->ldo_stripe[i]);
2683 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name,
2693 * Set default striping on a directory.
2695 * Sets specified striping on a directory object unless it matches the default
2696 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2697 * EA. This striping will be used when regular file is being created in this
2700 * \param[in] env execution environment
2701 * \param[in] dt the striped object
2702 * \param[in] buf buffer with the striping
2703 * \param[in] name name of EA
2704 * \param[in] fl xattr flag (see OSD API description)
2705 * \param[in] th transaction handle
2707 * \retval 0 on success
2708 * \retval negative if failed
2710 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
2711 struct dt_object *dt,
2712 const struct lu_buf *buf,
2713 const char *name, int fl,
2716 struct lov_user_md_v1 *lum;
2717 struct lov_user_md_v3 *v3 = NULL;
2718 const char *pool_name = NULL;
2723 LASSERT(buf != NULL && buf->lb_buf != NULL);
2726 switch (lum->lmm_magic) {
2727 case LOV_USER_MAGIC_V3:
2729 if (v3->lmm_pool_name[0] != '\0')
2730 pool_name = v3->lmm_pool_name;
2732 case LOV_USER_MAGIC_V1:
2733 /* if { size, offset, count } = { 0, -1, 0 } and no pool
2734 * (i.e. all default values specified) then delete default
2735 * striping from dir. */
2737 "set default striping: sz %u # %u offset %d %s %s\n",
2738 (unsigned)lum->lmm_stripe_size,
2739 (unsigned)lum->lmm_stripe_count,
2740 (int)lum->lmm_stripe_offset,
2741 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
2743 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
2744 lum->lmm_stripe_count,
2745 lum->lmm_stripe_offset,
2748 case LOV_USER_MAGIC_COMP_V1:
2752 CERROR("Invalid magic %x\n", lum->lmm_magic);
2757 rc = lod_xattr_del_internal(env, dt, name, th);
2761 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2768 * Set default striping on a directory object.
2770 * Sets specified striping on a directory object unless it matches the default
2771 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2772 * EA. This striping will be used when a new directory is being created in the
2775 * \param[in] env execution environment
2776 * \param[in] dt the striped object
2777 * \param[in] buf buffer with the striping
2778 * \param[in] name name of EA
2779 * \param[in] fl xattr flag (see OSD API description)
2780 * \param[in] th transaction handle
2782 * \retval 0 on success
2783 * \retval negative if failed
2785 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
2786 struct dt_object *dt,
2787 const struct lu_buf *buf,
2788 const char *name, int fl,
2791 struct lmv_user_md_v1 *lum;
2795 LASSERT(buf != NULL && buf->lb_buf != NULL);
2798 CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
2799 le32_to_cpu(lum->lum_stripe_count),
2800 (int)le32_to_cpu(lum->lum_stripe_offset));
2802 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
2803 le32_to_cpu(lum->lum_stripe_offset)) &&
2804 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
2805 rc = lod_xattr_del_internal(env, dt, name, th);
2809 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2818 * Turn directory into a striped directory.
2820 * During replay the client sends the striping created before MDT
2821 * failure, then the layer above LOD sends this defined striping
2822 * using ->do_xattr_set(), so LOD uses this method to replay creation
2823 * of the stripes. Notice the original information for the striping
2824 * (#stripes, FIDs, etc) was transferred in declare path.
2826 * \param[in] env execution environment
2827 * \param[in] dt the striped object
2828 * \param[in] buf not used currently
2829 * \param[in] name not used currently
2830 * \param[in] fl xattr flag (see OSD API description)
2831 * \param[in] th transaction handle
2833 * \retval 0 on success
2834 * \retval negative if failed
2836 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
2837 const struct lu_buf *buf, const char *name,
2838 int fl, struct thandle *th)
2840 struct lod_object *lo = lod_dt_obj(dt);
2841 struct lod_thread_info *info = lod_env_info(env);
2842 struct lu_attr *attr = &info->lti_attr;
2843 struct dt_object_format *dof = &info->lti_format;
2844 struct lu_buf lmv_buf;
2845 struct lu_buf slave_lmv_buf;
2846 struct lmv_mds_md_v1 *lmm;
2847 struct lmv_mds_md_v1 *slave_lmm = NULL;
2848 struct dt_insert_rec *rec = &info->lti_dt_rec;
2853 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2856 /* The stripes are supposed to be allocated in declare phase,
2857 * if there are no stripes being allocated, it will skip */
2858 if (lo->ldo_dir_stripenr == 0)
2861 rc = dt_attr_get(env, dt_object_child(dt), attr);
2865 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
2866 LA_MODE | LA_UID | LA_GID | LA_TYPE;
2867 dof->dof_type = DFT_DIR;
2869 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
2872 lmm = lmv_buf.lb_buf;
2874 OBD_ALLOC_PTR(slave_lmm);
2875 if (slave_lmm == NULL)
2878 lod_prep_slave_lmv_md(slave_lmm, lmm);
2879 slave_lmv_buf.lb_buf = slave_lmm;
2880 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
2882 rec->rec_type = S_IFDIR;
2883 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2884 struct dt_object *dto;
2885 char *stripe_name = info->lti_key;
2886 struct lu_name *sname;
2887 struct linkea_data ldata = { NULL };
2888 struct lu_buf linkea_buf;
2890 dto = lo->ldo_stripe[i];
2892 dt_write_lock(env, dto, MOR_TGT_CHILD);
2893 rc = lod_sub_object_create(env, dto, attr, NULL, dof,
2896 dt_write_unlock(env, dto);
2900 rc = lod_sub_object_ref_add(env, dto, th);
2901 dt_write_unlock(env, dto);
2905 rec->rec_fid = lu_object_fid(&dto->do_lu);
2906 rc = lod_sub_object_index_insert(env, dto,
2907 (const struct dt_rec *)rec,
2908 (const struct dt_key *)dot, th, 0);
2912 rec->rec_fid = lu_object_fid(&dt->do_lu);
2913 rc = lod_sub_object_index_insert(env, dto, (struct dt_rec *)rec,
2914 (const struct dt_key *)dotdot, th, 0);
2918 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
2919 cfs_fail_val != i) {
2920 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
2922 slave_lmm->lmv_master_mdt_index =
2925 slave_lmm->lmv_master_mdt_index =
2928 rc = lod_sub_object_xattr_set(env, dto, &slave_lmv_buf,
2929 XATTR_NAME_LMV, fl, th);
2934 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
2936 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2937 PFID(lu_object_fid(&dto->do_lu)), i + 1);
2939 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2940 PFID(lu_object_fid(&dto->do_lu)), i);
2942 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2943 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
2944 sname, lu_object_fid(&dt->do_lu));
2948 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
2949 linkea_buf.lb_len = ldata.ld_leh->leh_len;
2950 rc = lod_sub_object_xattr_set(env, dto, &linkea_buf,
2951 XATTR_NAME_LINK, 0, th);
2955 rec->rec_fid = lu_object_fid(&dto->do_lu);
2956 rc = lod_sub_object_index_insert(env, dt_object_child(dt),
2957 (const struct dt_rec *)rec,
2958 (const struct dt_key *)stripe_name, th, 0);
2962 rc = lod_sub_object_ref_add(env, dt_object_child(dt), th);
2967 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
2968 rc = lod_sub_object_xattr_set(env, dt_object_child(dt),
2969 &lmv_buf, XATTR_NAME_LMV, fl, th);
2971 if (slave_lmm != NULL)
2972 OBD_FREE_PTR(slave_lmm);
2978 * Helper function to declare/execute creation of a striped directory
2980 * Called in declare/create object path, prepare striping for a directory
2981 * and prepare defaults data striping for the objects to be created in
2982 * that directory. Notice the function calls "declaration" or "execution"
2983 * methods depending on \a declare param. This is a consequence of the
2984 * current approach while we don't have natural distributed transactions:
2985 * we basically execute non-local updates in the declare phase. So, the
2986 * arguments for the both phases are the same and this is the reason for
2987 * this function to exist.
2989 * \param[in] env execution environment
2990 * \param[in] dt object
2991 * \param[in] attr attributes the stripes will be created with
2992 * \param[in] dof format of stripes (see OSD API description)
2993 * \param[in] th transaction handle
2994 * \param[in] declare where to call "declare" or "execute" methods
2996 * \retval 0 on success
2997 * \retval negative if failed
2999 static int lod_dir_striping_create_internal(const struct lu_env *env,
3000 struct dt_object *dt,
3001 struct lu_attr *attr,
3002 struct dt_object_format *dof,
3006 struct lod_thread_info *info = lod_env_info(env);
3007 struct lod_object *lo = lod_dt_obj(dt);
3008 const struct lod_default_striping *lds = lo->ldo_def_striping;
3012 LASSERT(ergo(lds != NULL,
3013 lds->lds_def_striping_set ||
3014 lds->lds_dir_def_striping_set));
3016 if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripenr,
3017 lo->ldo_dir_stripe_offset)) {
3018 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3019 int stripe_count = lo->ldo_dir_stripenr;
3021 if (info->lti_ea_store_size < sizeof(*v1)) {
3022 rc = lod_ea_store_resize(info, sizeof(*v1));
3025 v1 = info->lti_ea_store;
3028 memset(v1, 0, sizeof(*v1));
3029 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3030 v1->lum_stripe_count = cpu_to_le32(stripe_count);
3031 v1->lum_stripe_offset =
3032 cpu_to_le32(lo->ldo_dir_stripe_offset);
3034 info->lti_buf.lb_buf = v1;
3035 info->lti_buf.lb_len = sizeof(*v1);
3038 rc = lod_declare_xattr_set_lmv(env, dt, attr,
3039 &info->lti_buf, dof, th);
3041 rc = lod_xattr_set_lmv(env, dt, &info->lti_buf,
3042 XATTR_NAME_LMV, 0, th);
3047 /* Transfer default LMV striping from the parent */
3048 if (lds != NULL && lds->lds_dir_def_striping_set &&
3049 !LMVEA_DELETE_VALUES(lds->lds_dir_def_stripenr,
3050 lds->lds_dir_def_stripe_offset)) {
3051 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3053 if (info->lti_ea_store_size < sizeof(*v1)) {
3054 rc = lod_ea_store_resize(info, sizeof(*v1));
3057 v1 = info->lti_ea_store;
3060 memset(v1, 0, sizeof(*v1));
3061 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3062 v1->lum_stripe_count = cpu_to_le32(lds->lds_dir_def_stripenr);
3063 v1->lum_stripe_offset =
3064 cpu_to_le32(lds->lds_dir_def_stripe_offset);
3066 cpu_to_le32(lds->lds_dir_def_hash_type);
3068 info->lti_buf.lb_buf = v1;
3069 info->lti_buf.lb_len = sizeof(*v1);
3071 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3072 XATTR_NAME_DEFAULT_LMV,
3075 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
3077 XATTR_NAME_DEFAULT_LMV, 0,
3083 /* Transfer default LOV striping from the parent */
3084 if (lds != NULL && lds->lds_def_striping_set &&
3085 lds->lds_def_comp_cnt != 0) {
3086 struct lov_mds_md *lmm;
3087 int lmm_size = lod_comp_md_size(lo, true);
3089 if (info->lti_ea_store_size < lmm_size) {
3090 rc = lod_ea_store_resize(info, lmm_size);
3094 lmm = info->lti_ea_store;
3096 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
3100 info->lti_buf.lb_buf = lmm;
3101 info->lti_buf.lb_len = lmm_size;
3104 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3105 XATTR_NAME_LOV, 0, th);
3107 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
3108 XATTR_NAME_LOV, 0, th);
3116 static int lod_declare_dir_striping_create(const struct lu_env *env,
3117 struct dt_object *dt,
3118 struct lu_attr *attr,
3119 struct dt_object_format *dof,
3122 return lod_dir_striping_create_internal(env, dt, attr, dof, th, true);
3125 static int lod_dir_striping_create(const struct lu_env *env,
3126 struct dt_object *dt,
3127 struct lu_attr *attr,
3128 struct dt_object_format *dof,
3131 return lod_dir_striping_create_internal(env, dt, attr, dof, th, false);
3135 * Make LOV EA for striped object.
3137 * Generate striping information and store it in the LOV EA of the given
3138 * object. The caller must ensure nobody else is calling the function
3139 * against the object concurrently. The transaction must be started.
3140 * FLDB service must be running as well; it's used to map FID to the target,
3141 * which is stored in LOV EA.
3143 * \param[in] env execution environment for this thread
3144 * \param[in] lo LOD object
3145 * \param[in] th transaction handle
3147 * \retval 0 if LOV EA is stored successfully
3148 * \retval negative error number on failure
3150 static int lod_generate_and_set_lovea(const struct lu_env *env,
3151 struct lod_object *lo,
3154 struct lod_thread_info *info = lod_env_info(env);
3155 struct dt_object *next = dt_object_child(&lo->ldo_obj);
3156 struct lov_mds_md_v1 *lmm;
3162 if (lo->ldo_comp_cnt == 0) {
3163 lod_object_free_striping(env, lo);
3164 rc = lod_sub_object_xattr_del(env, next, XATTR_NAME_LOV, th);
3168 lmm_size = lod_comp_md_size(lo, false);
3169 if (info->lti_ea_store_size < lmm_size) {
3170 rc = lod_ea_store_resize(info, lmm_size);
3174 lmm = info->lti_ea_store;
3176 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
3180 info->lti_buf.lb_buf = lmm;
3181 info->lti_buf.lb_len = lmm_size;
3182 rc = lod_sub_object_xattr_set(env, next, &info->lti_buf,
3183 XATTR_NAME_LOV, 0, th);
3188 * Delete layout component(s)
3190 * \param[in] env execution environment for this thread
3191 * \param[in] dt object
3192 * \param[in] th transaction handle
3194 * \retval 0 on success
3195 * \retval negative error number on failure
3197 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
3200 struct lod_layout_component *lod_comp;
3201 struct lod_object *lo = lod_dt_obj(dt);
3202 struct dt_object *next = dt_object_child(dt);
3203 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3206 LASSERT(lo->ldo_is_composite);
3207 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
3209 left = lo->ldo_comp_cnt;
3210 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3211 lod_comp = &lo->ldo_comp_entries[i];
3213 if (lod_comp->llc_id != LCME_ID_INVAL)
3217 /* Not instantiated component */
3218 if (lod_comp->llc_stripe == NULL)
3221 LASSERT(lod_comp->llc_stripenr > 0);
3222 for (j = 0; j < lod_comp->llc_stripenr; j++) {
3223 struct dt_object *obj = lod_comp->llc_stripe[j];
3227 rc = lod_sub_object_destroy(env, obj, th);
3231 lu_object_put(env, &obj->do_lu);
3232 lod_comp->llc_stripe[j] = NULL;
3234 OBD_FREE(lod_comp->llc_stripe, sizeof(struct dt_object *) *
3235 lod_comp->llc_stripes_allocated);
3236 lod_comp->llc_stripe = NULL;
3237 lod_comp->llc_stripes_allocated = 0;
3238 lod_obj_set_pool(lo, i, NULL);
3241 LASSERTF(left >= 0 && left < lo->ldo_comp_cnt, "left = %d\n", left);
3243 struct lod_layout_component *comp_array;
3245 OBD_ALLOC(comp_array, sizeof(*comp_array) * left);
3246 if (comp_array == NULL)
3247 GOTO(out, rc = -ENOMEM);
3249 memcpy(&comp_array[0], &lo->ldo_comp_entries[0],
3250 sizeof(*comp_array) * left);
3252 OBD_FREE(lo->ldo_comp_entries,
3253 sizeof(*comp_array) * lo->ldo_comp_cnt);
3254 lo->ldo_comp_entries = comp_array;
3255 lo->ldo_comp_cnt = left;
3256 lod_obj_inc_layout_gen(lo);
3258 lod_free_comp_entries(lo);
3261 LASSERT(dt_object_exists(dt));
3262 rc = dt_attr_get(env, next, attr);
3266 if (attr->la_size > 0) {
3268 attr->la_valid = LA_SIZE;
3269 rc = lod_sub_object_attr_set(env, next, attr, th);
3274 rc = lod_generate_and_set_lovea(env, lo, th);
3278 lod_object_free_striping(env, lo);
3283 * Implementation of dt_object_operations::do_xattr_set.
3285 * Sets specified extended attribute on the object. Three types of EAs are
3287 * LOV EA - stores striping for a regular file or default striping (when set
3289 * LMV EA - stores a marker for the striped directories
3290 * DMV EA - stores default directory striping
3292 * When striping is applied to a non-striped existing object (this is called
3293 * late striping), then LOD notices the caller wants to turn the object into a
3294 * striped one. The stripe objects are created and appropriate EA is set:
3295 * LOV EA storing all the stripes directly or LMV EA storing just a small header
3296 * with striping configuration.
3298 * \see dt_object_operations::do_xattr_set() in the API description for details.
3300 static int lod_xattr_set(const struct lu_env *env,
3301 struct dt_object *dt, const struct lu_buf *buf,
3302 const char *name, int fl, struct thandle *th)
3304 struct dt_object *next = dt_object_child(dt);
3308 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3309 strcmp(name, XATTR_NAME_LMV) == 0) {
3310 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
3312 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
3313 LMV_HASH_FLAG_MIGRATION)
3314 rc = lod_sub_object_xattr_set(env, next, buf, name, fl,
3317 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
3322 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3323 strcmp(name, XATTR_NAME_LOV) == 0) {
3325 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
3327 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3328 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
3330 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
3333 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3334 (!strcmp(name, XATTR_NAME_LOV) ||
3335 !strncmp(name, XATTR_LUSTRE_LOV,
3336 strlen(XATTR_LUSTRE_LOV)))) {
3337 /* in case of lov EA swap, just set it
3338 * if not, it is a replay so check striping match what we
3339 * already have during req replay, declare_xattr_set()
3340 * defines striping, then create() does the work */
3341 if (fl & LU_XATTR_REPLACE) {
3342 /* free stripes, then update disk */
3343 lod_object_free_striping(env, lod_dt_obj(dt));
3345 rc = lod_sub_object_xattr_set(env, next, buf, name,
3347 } else if (dt_object_remote(dt)) {
3348 /* This only happens during migration, see
3349 * mdd_migrate_create(), in which Master MDT will
3350 * create a remote target object, and only set
3351 * (migrating) stripe EA on the remote object,
3352 * and does not need creating each stripes. */
3353 rc = lod_sub_object_xattr_set(env, next, buf, name,
3355 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
3356 /* delete component(s) */
3357 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
3358 rc = lod_layout_del(env, dt, th);
3361 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
3362 * it's going to create create file with specified
3363 * component(s), the striping must have not being
3364 * cached in this case;
3366 * Otherwise, it's going to add/change component(s) to
3367 * an existing file, the striping must have been cached
3370 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
3371 !strcmp(name, XATTR_NAME_LOV),
3372 !lod_dt_obj(dt)->ldo_comp_cached));
3374 rc = lod_striping_create(env, dt, NULL, NULL, th);
3377 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3378 rc = lod_object_replace_parent_fid(env, dt, th, false);
3383 /* then all other xattr */
3384 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3390 * Implementation of dt_object_operations::do_declare_xattr_del.
3392 * \see dt_object_operations::do_declare_xattr_del() in the API description
3395 static int lod_declare_xattr_del(const struct lu_env *env,
3396 struct dt_object *dt, const char *name,
3399 struct lod_object *lo = lod_dt_obj(dt);
3404 rc = lod_sub_object_declare_xattr_del(env, dt_object_child(dt),
3409 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3412 /* set xattr to each stripes, if needed */
3413 rc = lod_load_striping(env, lo);
3417 if (lo->ldo_dir_stripenr == 0)
3420 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
3421 LASSERT(lo->ldo_stripe[i]);
3422 rc = lod_sub_object_declare_xattr_del(env, lo->ldo_stripe[i],
3432 * Implementation of dt_object_operations::do_xattr_del.
3434 * If EA storing a regular striping is being deleted, then release
3435 * all the references to the stripe objects in core.
3437 * \see dt_object_operations::do_xattr_del() in the API description for details.
3439 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
3440 const char *name, struct thandle *th)
3442 struct dt_object *next = dt_object_child(dt);
3443 struct lod_object *lo = lod_dt_obj(dt);
3448 if (!strcmp(name, XATTR_NAME_LOV))
3449 lod_object_free_striping(env, lod_dt_obj(dt));
3451 rc = lod_sub_object_xattr_del(env, next, name, th);
3452 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3455 if (lo->ldo_dir_stripenr == 0)
3458 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
3459 LASSERT(lo->ldo_stripe[i]);
3461 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name, th);
3470 * Implementation of dt_object_operations::do_xattr_list.
3472 * \see dt_object_operations::do_xattr_list() in the API description
3475 static int lod_xattr_list(const struct lu_env *env,
3476 struct dt_object *dt, const struct lu_buf *buf)
3478 return dt_xattr_list(env, dt_object_child(dt), buf);
3481 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
3483 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
3488 * Get default striping.
3490 * \param[in] env execution environment
3491 * \param[in] lo object
3492 * \param[out] lds default striping
3494 * \retval 0 on success
3495 * \retval negative if failed
3497 static int lod_get_default_lov_striping(const struct lu_env *env,
3498 struct lod_object *lo,
3499 struct lod_default_striping *lds)
3501 struct lod_thread_info *info = lod_env_info(env);
3502 struct lov_user_md_v1 *v1 = NULL;
3503 struct lov_user_md_v3 *v3 = NULL;
3504 struct lov_comp_md_v1 *comp_v1 = NULL;
3510 lds->lds_def_striping_set = 0;
3512 rc = lod_get_lov_ea(env, lo);
3516 if (rc < (typeof(rc))sizeof(struct lov_user_md))
3519 v1 = info->lti_ea_store;
3520 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
3521 lustre_swab_lov_user_md_v1(v1);
3522 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
3523 v3 = (struct lov_user_md_v3 *)v1;
3524 lustre_swab_lov_user_md_v3(v3);
3525 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3526 comp_v1 = (struct lov_comp_md_v1 *)v1;
3527 lustre_swab_lov_comp_md_v1(comp_v1);
3530 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
3531 v1->lmm_magic != LOV_MAGIC_COMP_V1)
3534 if (v1->lmm_magic == LOV_MAGIC_COMP_V1) {
3535 comp_v1 = (struct lov_comp_md_v1 *)v1;
3536 comp_cnt = comp_v1->lcm_entry_count;
3545 /* realloc default comp entries if necessary */
3546 rc = lod_def_striping_comp_resize(lds, comp_cnt);
3550 lds->lds_def_comp_cnt = comp_cnt;
3551 lds->lds_def_striping_is_composite = composite ? 1 : 0;
3553 for (i = 0; i < comp_cnt; i++) {
3554 struct lod_layout_component *lod_comp;
3555 struct lu_extent *ext;
3558 lod_comp = &lds->lds_def_comp_entries[i];
3560 * reset lod_comp values, llc_stripes is always NULL in
3561 * the default striping template, llc_pool will be reset
3564 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
3567 v1 = (struct lov_user_md *)((char *)comp_v1 +
3568 comp_v1->lcm_entries[i].lcme_offset);
3569 ext = &comp_v1->lcm_entries[i].lcme_extent;
3570 lod_comp->llc_extent = *ext;
3573 if (v1->lmm_pattern != LOV_PATTERN_RAID0 &&
3574 v1->lmm_pattern != 0) {
3575 lod_free_def_comp_entries(lds);
3579 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d "
3580 "stripe_offset=%d\n",
3581 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
3582 (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
3583 (int)v1->lmm_stripe_offset);
3585 lod_comp->llc_stripenr = v1->lmm_stripe_count;
3586 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
3587 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
3590 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
3591 /* XXX: sanity check here */
3592 v3 = (struct lov_user_md_v3 *) v1;
3593 if (v3->lmm_pool_name[0] != '\0')
3594 pool = v3->lmm_pool_name;
3596 lod_set_def_pool(lds, i, pool);
3599 lds->lds_def_striping_set = 1;
3604 * Get default directory striping.
3606 * \param[in] env execution environment
3607 * \param[in] lo object
3608 * \param[out] lds default striping
3610 * \retval 0 on success
3611 * \retval negative if failed
3613 static int lod_get_default_lmv_striping(const struct lu_env *env,
3614 struct lod_object *lo,
3615 struct lod_default_striping *lds)
3617 struct lod_thread_info *info = lod_env_info(env);
3618 struct lmv_user_md_v1 *v1 = NULL;
3622 lds->lds_dir_def_striping_set = 0;
3623 rc = lod_get_default_lmv_ea(env, lo);
3627 if (rc < (typeof(rc))sizeof(struct lmv_user_md))
3630 v1 = info->lti_ea_store;
3632 lds->lds_dir_def_stripenr = le32_to_cpu(v1->lum_stripe_count);
3633 lds->lds_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
3634 lds->lds_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
3635 lds->lds_dir_def_striping_set = 1;
3641 * Get default striping in the object.
3643 * Get object default striping and default directory striping.
3645 * \param[in] env execution environment
3646 * \param[in] lo object
3647 * \param[out] lds default striping
3649 * \retval 0 on success
3650 * \retval negative if failed
3652 static int lod_get_default_striping(const struct lu_env *env,
3653 struct lod_object *lo,
3654 struct lod_default_striping *lds)
3658 rc = lod_get_default_lov_striping(env, lo, lds);
3659 rc1 = lod_get_default_lmv_striping(env, lo, lds);
3660 if (rc == 0 && rc1 < 0)
3667 * Apply default striping on object.
3669 * If object striping pattern is not set, set to the one in default striping.
3670 * The default striping is from parent or fs.
3672 * \param[in] lo new object
3673 * \param[in] lds default striping
3674 * \param[in] mode new object's mode
3676 static void lod_striping_from_default(struct lod_object *lo,
3677 const struct lod_default_striping *lds,
3680 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
3681 struct lov_desc *desc = &d->lod_desc;
3684 if (lds->lds_def_striping_set && S_ISREG(mode)) {
3685 rc = lod_alloc_comp_entries(lo, lds->lds_def_comp_cnt);
3689 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
3691 for (i = 0; i < lo->ldo_comp_cnt; i++) {
3692 struct lod_layout_component *obj_comp =
3693 &lo->ldo_comp_entries[i];
3694 struct lod_layout_component *def_comp =
3695 &lds->lds_def_comp_entries[i];
3697 CDEBUG(D_LAYOUT, "Inherite from default: size:%hu "
3698 "nr:%u offset:%u %s\n",
3699 def_comp->llc_stripe_size,
3700 def_comp->llc_stripenr,
3701 def_comp->llc_stripe_offset,
3702 def_comp->llc_pool ?: "");
3704 *obj_comp = *def_comp;
3705 if (def_comp->llc_pool != NULL) {
3706 /* pointer was copied from def_comp */
3707 obj_comp->llc_pool = NULL;
3708 lod_obj_set_pool(lo, i, def_comp->llc_pool);
3712 * Don't initialize these fields for plain layout
3713 * (v1/v3) here, they are inherited in the order of
3714 * 'parent' -> 'fs default (root)' -> 'global default
3715 * values for stripe_count & stripe_size'.
3717 * see lod_ah_init().
3719 if (!lo->ldo_is_composite)
3722 if (obj_comp->llc_stripenr <= 0)
3723 obj_comp->llc_stripenr =
3724 desc->ld_default_stripe_count;
3725 if (obj_comp->llc_stripe_size <= 0)
3726 obj_comp->llc_stripe_size =
3727 desc->ld_default_stripe_size;
3729 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
3730 if (lo->ldo_dir_stripenr == 0)
3731 lo->ldo_dir_stripenr = lds->lds_dir_def_stripenr;
3732 if (lo->ldo_dir_stripe_offset == -1)
3733 lo->ldo_dir_stripe_offset =
3734 lds->lds_dir_def_stripe_offset;
3735 if (lo->ldo_dir_hash_type == 0)
3736 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
3738 CDEBUG(D_LAYOUT, "striping from default dir: nr:%hu, "
3739 "offset:%u, hash_type:%u\n",
3740 lo->ldo_dir_stripenr, lo->ldo_dir_stripe_offset,
3741 lo->ldo_dir_hash_type);
3745 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root)
3747 struct lod_layout_component *lod_comp;
3749 if (lo->ldo_comp_cnt == 0)
3752 if (lo->ldo_is_composite)
3755 lod_comp = &lo->ldo_comp_entries[0];
3757 if (lod_comp->llc_stripenr <= 0 ||
3758 lod_comp->llc_stripe_size <= 0)
3761 if (from_root && (lod_comp->llc_pool == NULL ||
3762 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
3769 * Implementation of dt_object_operations::do_ah_init.
3771 * This method is used to make a decision on the striping configuration for the
3772 * object being created. It can be taken from the \a parent object if it exists,
3773 * or filesystem's default. The resulting configuration (number of stripes,
3774 * stripe size/offset, pool name, etc) is stored in the object itself and will
3775 * be used by the methods like ->doo_declare_create().
3777 * \see dt_object_operations::do_ah_init() in the API description for details.
3779 static void lod_ah_init(const struct lu_env *env,
3780 struct dt_allocation_hint *ah,
3781 struct dt_object *parent,
3782 struct dt_object *child,
3785 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
3786 struct lod_thread_info *info = lod_env_info(env);
3787 struct lod_default_striping *lds = &info->lti_def_striping;
3788 struct dt_object *nextp = NULL;
3789 struct dt_object *nextc;
3790 struct lod_object *lp = NULL;
3791 struct lod_object *lc;
3792 struct lov_desc *desc;
3793 struct lod_layout_component *lod_comp;
3799 if (likely(parent)) {
3800 nextp = dt_object_child(parent);
3801 lp = lod_dt_obj(parent);
3804 nextc = dt_object_child(child);
3805 lc = lod_dt_obj(child);
3807 LASSERT(!lod_obj_is_striped(child));
3808 /* default layout template may have been set on the regular file
3809 * when this is called from mdd_create_data() */
3810 if (S_ISREG(child_mode))
3811 lod_free_comp_entries(lc);
3813 if (!dt_object_exists(nextc))
3814 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
3816 if (S_ISDIR(child_mode)) {
3817 /* other default values are 0 */
3818 lc->ldo_dir_stripe_offset = -1;
3820 /* get default striping from parent object */
3821 if (likely(lp != NULL))
3822 lod_get_default_striping(env, lp, lds);
3824 /* set child default striping info, default value is NULL */
3825 if (lds->lds_def_striping_set || lds->lds_dir_def_striping_set)
3826 lc->ldo_def_striping = lds;
3828 /* It should always honour the specified stripes */
3829 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
3830 lod_verify_md_striping(d, ah->dah_eadata) == 0) {
3831 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
3833 lc->ldo_dir_stripenr =
3834 le32_to_cpu(lum1->lum_stripe_count);
3835 lc->ldo_dir_stripe_offset =
3836 le32_to_cpu(lum1->lum_stripe_offset);
3837 lc->ldo_dir_hash_type =
3838 le32_to_cpu(lum1->lum_hash_type);
3839 CDEBUG(D_INFO, "set dir stripe: count %hu, offset %d, "
3841 lc->ldo_dir_stripenr,
3842 (int)lc->ldo_dir_stripe_offset,
3843 lc->ldo_dir_hash_type);
3845 /* transfer defaults LMV to new directory */
3846 lod_striping_from_default(lc, lds, child_mode);
3849 /* shrink the stripe_count to the avaible MDT count */
3850 if (lc->ldo_dir_stripenr > d->lod_remote_mdt_count + 1 &&
3851 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))
3852 lc->ldo_dir_stripenr = d->lod_remote_mdt_count + 1;
3854 /* Directory will be striped only if stripe_count > 1, if
3855 * stripe_count == 1, let's reset stripenr = 0 to avoid
3856 * create single master stripe and also help to unify the
3857 * stripe handling of directories and files */
3858 if (lc->ldo_dir_stripenr == 1)
3859 lc->ldo_dir_stripenr = 0;
3861 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
3862 lc->ldo_dir_stripenr, (int)lc->ldo_dir_stripe_offset,
3863 lc->ldo_dir_hash_type);
3868 /* child object regular file*/
3870 if (!lod_object_will_be_striped(S_ISREG(child_mode),
3871 lu_object_fid(&child->do_lu)))
3874 /* If object is going to be striped over OSTs, transfer default
3875 * striping information to the child, so that we can use it
3876 * during declaration and creation.
3878 * Try from the parent first.
3880 if (likely(lp != NULL)) {
3881 rc = lod_get_default_lov_striping(env, lp, lds);
3883 lod_striping_from_default(lc, lds, child_mode);
3886 /* Initialize lod_device::lod_md_root object reference */
3887 if (d->lod_md_root == NULL) {
3888 struct dt_object *root;
3889 struct lod_object *lroot;
3891 lu_root_fid(&info->lti_fid);
3892 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
3893 if (!IS_ERR(root)) {
3894 lroot = lod_dt_obj(root);
3896 spin_lock(&d->lod_lock);
3897 if (d->lod_md_root != NULL)
3898 dt_object_put(env, &d->lod_md_root->ldo_obj);
3899 d->lod_md_root = lroot;
3900 spin_unlock(&d->lod_lock);
3904 /* try inherit layout from the root object (fs default) when:
3905 * - parent does not have default layout; or
3906 * - parent has plain(v1/v3) default layout, and some attributes
3907 * are not specified in the default layout;
3909 if (d->lod_md_root != NULL && lod_need_inherit_more(lc, true)) {
3910 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds);
3913 if (lc->ldo_comp_cnt == 0) {
3914 lod_striping_from_default(lc, lds, child_mode);
3915 } else if (!lds->lds_def_striping_is_composite) {
3916 struct lod_layout_component *def_comp;
3918 LASSERT(!lc->ldo_is_composite);
3919 lod_comp = &lc->ldo_comp_entries[0];
3920 def_comp = &lds->lds_def_comp_entries[0];
3922 if (lod_comp->llc_stripenr <= 0)
3923 lod_comp->llc_stripenr = def_comp->llc_stripenr;
3924 if (lod_comp->llc_stripe_size <= 0)
3925 lod_comp->llc_stripe_size =
3926 def_comp->llc_stripe_size;
3927 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
3928 lod_comp->llc_stripe_offset =
3929 def_comp->llc_stripe_offset;
3930 if (lod_comp->llc_pool == NULL)
3931 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
3936 * fs default striping may not be explicitly set, or historically set
3937 * in config log, use them.
3939 if (lod_need_inherit_more(lc, false)) {
3941 if (lc->ldo_comp_cnt == 0) {
3942 rc = lod_alloc_comp_entries(lc, 1);
3944 /* fail to allocate memory, will create a
3945 * non-striped file. */
3947 lc->ldo_is_composite = 0;
3948 lod_comp = &lc->ldo_comp_entries[0];
3949 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
3951 LASSERT(!lc->ldo_is_composite);
3952 lod_comp = &lc->ldo_comp_entries[0];
3953 desc = &d->lod_desc;
3954 if (lod_comp->llc_stripenr <= 0)
3955 lod_comp->llc_stripenr = desc->ld_default_stripe_count;
3956 if (lod_comp->llc_stripe_size <= 0)
3957 lod_comp->llc_stripe_size =
3958 desc->ld_default_stripe_size;
3964 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
3966 * Size initialization on late striping.
3968 * Propagate the size of a truncated object to a deferred striping.
3969 * This function handles a special case when truncate was done on a
3970 * non-striped object and now while the striping is being created
3971 * we can't lose that size, so we have to propagate it to the stripes
3974 * \param[in] env execution environment
3975 * \param[in] dt object
3976 * \param[in] th transaction handle
3978 * \retval 0 on success
3979 * \retval negative if failed
3981 static int lod_declare_init_size(const struct lu_env *env,
3982 struct dt_object *dt, struct thandle *th)
3984 struct dt_object *next = dt_object_child(dt);
3985 struct lod_object *lo = lod_dt_obj(dt);
3986 struct dt_object **objects = NULL;
3987 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3988 uint64_t size, offs;
3989 int i, rc, stripe, stripenr = 0, stripe_size = 0;
3992 if (!lod_obj_is_striped(dt))
3995 rc = dt_attr_get(env, next, attr);
3996 LASSERT(attr->la_valid & LA_SIZE);
4000 size = attr->la_size;
4004 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4005 struct lod_layout_component *lod_comp;
4006 struct lu_extent *extent;
4008 lod_comp = &lo->ldo_comp_entries[i];
4010 if (lod_comp->llc_stripe == NULL)
4013 extent = &lod_comp->llc_extent;
4014 if (!lo->ldo_is_composite ||
4015 (size >= extent->e_start && size < extent->e_end)) {
4016 objects = lod_comp->llc_stripe;
4017 stripenr = lod_comp->llc_stripenr;
4018 stripe_size = lod_comp->llc_stripe_size;
4026 LASSERT(objects != NULL && stripe_size != 0);
4028 /* ll_do_div64(a, b) returns a % b, and a = a / b */
4029 ll_do_div64(size, (__u64)stripe_size);
4030 stripe = ll_do_div64(size, (__u64)stripenr);
4031 LASSERT(objects[stripe] != NULL);
4033 size = size * stripe_size;
4034 offs = attr->la_size;
4035 size += ll_do_div64(offs, stripe_size);
4037 attr->la_valid = LA_SIZE;
4038 attr->la_size = size;
4040 rc = lod_sub_object_declare_attr_set(env, objects[stripe], attr, th);
4046 * Declare creation of striped object.
4048 * The function declares creation stripes for a regular object. The function
4049 * also declares whether the stripes will be created with non-zero size if
4050 * previously size was set non-zero on the master object. If object \a dt is
4051 * not local, then only fully defined striping can be applied in \a lovea.
4052 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
4055 * \param[in] env execution environment
4056 * \param[in] dt object
4057 * \param[in] attr attributes the stripes will be created with
4058 * \param[in] lovea a buffer containing striping description
4059 * \param[in] th transaction handle
4061 * \retval 0 on success
4062 * \retval negative if failed
4064 int lod_declare_striped_object(const struct lu_env *env, struct dt_object *dt,
4065 struct lu_attr *attr,
4066 const struct lu_buf *lovea, struct thandle *th)
4068 struct lod_thread_info *info = lod_env_info(env);
4069 struct dt_object *next = dt_object_child(dt);
4070 struct lod_object *lo = lod_dt_obj(dt);
4074 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
4075 GOTO(out, rc = -ENOMEM);
4077 if (!dt_object_remote(next)) {
4078 /* choose OST and generate appropriate objects */
4079 rc = lod_prepare_create(env, lo, attr, lovea, th);
4084 * declare storage for striping data
4086 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
4088 /* LOD can not choose OST objects for remote objects, i.e.
4089 * stripes must be ready before that. Right now, it can only
4090 * happen during migrate, i.e. migrate process needs to create
4091 * remote regular file (mdd_migrate_create), then the migrate
4092 * process will provide stripeEA. */
4093 LASSERT(lovea != NULL);
4094 info->lti_buf = *lovea;
4097 rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
4098 XATTR_NAME_LOV, 0, th);
4103 * if striping is created with local object's size > 0,
4104 * we have to propagate this size to specific object
4105 * the case is possible only when local object was created previously
4107 if (dt_object_exists(next))
4108 rc = lod_declare_init_size(env, dt, th);
4111 /* failed to create striping or to set initial size, let's reset
4112 * config so that others don't get confused */
4114 lod_object_free_striping(env, lo);
4120 * Implementation of dt_object_operations::do_declare_create.
4122 * The method declares creation of a new object. If the object will be striped,
4123 * then helper functions are called to find FIDs for the stripes, declare
4124 * creation of the stripes and declare initialization of the striping
4125 * information to be stored in the master object.
4127 * \see dt_object_operations::do_declare_create() in the API description
4130 static int lod_declare_object_create(const struct lu_env *env,
4131 struct dt_object *dt,
4132 struct lu_attr *attr,
4133 struct dt_allocation_hint *hint,
4134 struct dt_object_format *dof,
4137 struct dt_object *next = dt_object_child(dt);
4138 struct lod_object *lo = lod_dt_obj(dt);
4147 * first of all, we declare creation of local object
4149 rc = lod_sub_object_declare_create(env, next, attr, hint, dof, th);
4153 if (dof->dof_type == DFT_SYM)
4154 dt->do_body_ops = &lod_body_lnk_ops;
4155 else if (dof->dof_type == DFT_REGULAR)
4156 dt->do_body_ops = &lod_body_ops;
4159 * it's lod_ah_init() that has decided the object will be striped
4161 if (dof->dof_type == DFT_REGULAR) {
4162 /* callers don't want stripes */
4163 /* XXX: all tricky interactions with ->ah_make_hint() decided
4164 * to use striping, then ->declare_create() behaving differently
4165 * should be cleaned */
4166 if (dof->u.dof_reg.striped != 0)
4167 rc = lod_declare_striped_object(env, dt, attr,
4169 } else if (dof->dof_type == DFT_DIR) {
4170 struct seq_server_site *ss;
4172 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
4174 /* If the parent has default stripeEA, and client
4175 * did not find it before sending create request,
4176 * then MDT will return -EREMOTE, and client will
4177 * retrieve the default stripeEA and re-create the
4180 * Note: if dah_eadata != NULL, it means creating the
4181 * striped directory with specified stripeEA, then it
4182 * should ignore the default stripeEA */
4183 if (hint != NULL && hint->dah_eadata == NULL) {
4184 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
4185 GOTO(out, rc = -EREMOTE);
4187 if (lo->ldo_dir_stripe_offset == -1) {
4188 /* child and parent should be in the same MDT */
4189 if (hint->dah_parent != NULL &&
4190 dt_object_remote(hint->dah_parent))
4191 GOTO(out, rc = -EREMOTE);
4192 } else if (lo->ldo_dir_stripe_offset !=
4194 struct lod_device *lod;
4195 struct lod_tgt_descs *ltd;
4196 struct lod_tgt_desc *tgt = NULL;
4197 bool found_mdt = false;
4200 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4201 ltd = &lod->lod_mdt_descs;
4202 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
4203 tgt = LTD_TGT(ltd, i);
4204 if (tgt->ltd_index ==
4205 lo->ldo_dir_stripe_offset) {
4211 /* If the MDT indicated by stripe_offset can be
4212 * found, then tell client to resend the create
4213 * request to the correct MDT, otherwise return
4214 * error to client */
4216 GOTO(out, rc = -EREMOTE);
4218 GOTO(out, rc = -EINVAL);
4222 rc = lod_declare_dir_striping_create(env, dt, attr, dof, th);
4225 /* failed to create striping or to set initial size, let's reset
4226 * config so that others don't get confused */
4228 lod_object_free_striping(env, lo);
4233 * Creation of a striped regular object.
4235 * The function is called to create the stripe objects for a regular
4236 * striped file. This can happen at the initial object creation or
4237 * when the caller asks LOD to do so using ->do_xattr_set() method
4238 * (so called late striping). Notice all the information are already
4239 * prepared in the form of the list of objects (ldo_stripe field).
4240 * This is done during declare phase.
4242 * \param[in] env execution environment
4243 * \param[in] dt object
4244 * \param[in] attr attributes the stripes will be created with
4245 * \param[in] dof format of stripes (see OSD API description)
4246 * \param[in] th transaction handle
4248 * \retval 0 on success
4249 * \retval negative if failed
4251 int lod_striping_create(const struct lu_env *env, struct dt_object *dt,
4252 struct lu_attr *attr, struct dt_object_format *dof,
4255 struct lod_layout_component *lod_comp;
4256 struct lod_object *lo = lod_dt_obj(dt);
4260 LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
4262 /* create all underlying objects */
4263 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4264 lod_comp = &lo->ldo_comp_entries[i];
4266 if (lod_comp->llc_flags & LCME_FL_INIT)
4269 lod_comp->llc_flags |= LCME_FL_INIT;
4271 if (lod_comp->llc_stripe == NULL)
4274 LASSERT(lod_comp->llc_stripenr > 0);
4275 for (j = 0; j < lod_comp->llc_stripenr; j++) {
4276 struct dt_object *object = lod_comp->llc_stripe[j];
4277 LASSERT(object != NULL);
4278 rc = lod_sub_object_create(env, object, attr, NULL,
4286 rc = lod_generate_and_set_lovea(env, lo, th);
4289 lo->ldo_comp_cached = 1;
4291 lod_object_free_striping(env, lo);
4297 * Implementation of dt_object_operations::do_create.
4299 * If any of preceeding methods (like ->do_declare_create(),
4300 * ->do_ah_init(), etc) chose to create a striped object,
4301 * then this method will create the master and the stripes.
4303 * \see dt_object_operations::do_create() in the API description for details.
4305 static int lod_object_create(const struct lu_env *env, struct dt_object *dt,
4306 struct lu_attr *attr,
4307 struct dt_allocation_hint *hint,
4308 struct dt_object_format *dof, struct thandle *th)
4313 /* create local object */
4314 rc = lod_sub_object_create(env, dt_object_child(dt), attr, hint, dof,
4319 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4320 lod_obj_is_striped(dt) && dof->u.dof_reg.striped != 0) {
4321 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
4322 rc = lod_striping_create(env, dt, attr, dof, th);
4329 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
4330 struct dt_object *dt, struct thandle *th,
4331 int stripe_idx, struct lod_obj_stripe_cb_data *data)
4333 if (data->locd_declare)
4334 return lod_sub_object_declare_destroy(env, dt, th);
4335 else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4336 stripe_idx == cfs_fail_val)
4337 return lod_sub_object_destroy(env, dt, th);
4343 * Implementation of dt_object_operations::do_declare_destroy.
4345 * If the object is a striped directory, then the function declares reference
4346 * removal from the master object (this is an index) to the stripes and declares
4347 * destroy of all the stripes. In all the cases, it declares an intention to
4348 * destroy the object itself.
4350 * \see dt_object_operations::do_declare_destroy() in the API description
4353 static int lod_declare_object_destroy(const struct lu_env *env,
4354 struct dt_object *dt,
4357 struct dt_object *next = dt_object_child(dt);
4358 struct lod_object *lo = lod_dt_obj(dt);
4359 struct lod_thread_info *info = lod_env_info(env);
4360 char *stripe_name = info->lti_key;
4365 * load striping information, notice we don't do this when object
4366 * is being initialized as we don't need this information till
4367 * few specific cases like destroy, chown
4369 rc = lod_load_striping(env, lo);
4373 /* declare destroy for all underlying objects */
4374 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4375 rc = next->do_ops->do_index_try(env, next,
4376 &dt_directory_features);
4380 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4381 rc = lod_sub_object_declare_ref_del(env, next, th);
4385 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4386 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4388 rc = lod_sub_object_declare_delete(env, next,
4389 (const struct dt_key *)stripe_name, th);
4396 * we declare destroy for the local object
4398 rc = lod_sub_object_declare_destroy(env, next, th);
4402 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4403 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4406 if (!lod_obj_is_striped(dt))
4409 /* declare destroy all striped objects */
4410 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4411 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4412 if (lo->ldo_stripe[i] == NULL)
4415 rc = lod_sub_object_declare_ref_del(env,
4416 lo->ldo_stripe[i], th);
4418 rc = lod_sub_object_declare_destroy(env,
4419 lo->ldo_stripe[i], th);
4424 struct lod_obj_stripe_cb_data data;
4426 data.locd_declare = true;
4427 rc = lod_obj_for_each_stripe(env, lo, th,
4428 lod_obj_stripe_destroy_cb, &data);
4435 * Implementation of dt_object_operations::do_destroy.
4437 * If the object is a striped directory, then the function removes references
4438 * from the master object (this is an index) to the stripes and destroys all
4439 * the stripes. In all the cases, the function destroys the object itself.
4441 * \see dt_object_operations::do_destroy() in the API description for details.
4443 static int lod_object_destroy(const struct lu_env *env,
4444 struct dt_object *dt, struct thandle *th)
4446 struct dt_object *next = dt_object_child(dt);
4447 struct lod_object *lo = lod_dt_obj(dt);
4448 struct lod_thread_info *info = lod_env_info(env);
4449 char *stripe_name = info->lti_key;
4454 /* destroy sub-stripe of master object */
4455 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4456 rc = next->do_ops->do_index_try(env, next,
4457 &dt_directory_features);
4461 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4462 rc = lod_sub_object_ref_del(env, next, th);
4466 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4467 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4470 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
4471 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
4472 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
4474 rc = lod_sub_object_delete(env, next,
4475 (const struct dt_key *)stripe_name, th);
4481 rc = lod_sub_object_destroy(env, next, th);
4485 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4486 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4489 if (!lod_obj_is_striped(dt))
4492 /* destroy all striped objects */
4493 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4494 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4495 if (lo->ldo_stripe[i] == NULL)
4497 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4498 i == cfs_fail_val) {
4499 dt_write_lock(env, lo->ldo_stripe[i],
4501 rc = lod_sub_object_ref_del(env,
4502 lo->ldo_stripe[i], th);
4503 dt_write_unlock(env, lo->ldo_stripe[i]);
4507 rc = lod_sub_object_destroy(env,
4508 lo->ldo_stripe[i], th);
4514 struct lod_obj_stripe_cb_data data;
4516 data.locd_declare = false;
4517 rc = lod_obj_for_each_stripe(env, lo, th,
4518 lod_obj_stripe_destroy_cb, &data);
4525 * Implementation of dt_object_operations::do_declare_ref_add.
4527 * \see dt_object_operations::do_declare_ref_add() in the API description
4530 static int lod_declare_ref_add(const struct lu_env *env,
4531 struct dt_object *dt, struct thandle *th)
4533 return lod_sub_object_declare_ref_add(env, dt_object_child(dt), th);
4537 * Implementation of dt_object_operations::do_ref_add.
4539 * \see dt_object_operations::do_ref_add() in the API description for details.
4541 static int lod_ref_add(const struct lu_env *env,
4542 struct dt_object *dt, struct thandle *th)
4544 return lod_sub_object_ref_add(env, dt_object_child(dt), th);
4548 * Implementation of dt_object_operations::do_declare_ref_del.
4550 * \see dt_object_operations::do_declare_ref_del() in the API description
4553 static int lod_declare_ref_del(const struct lu_env *env,
4554 struct dt_object *dt, struct thandle *th)
4556 return lod_sub_object_declare_ref_del(env, dt_object_child(dt), th);
4560 * Implementation of dt_object_operations::do_ref_del
4562 * \see dt_object_operations::do_ref_del() in the API description for details.
4564 static int lod_ref_del(const struct lu_env *env,
4565 struct dt_object *dt, struct thandle *th)
4567 return lod_sub_object_ref_del(env, dt_object_child(dt), th);
4571 * Implementation of dt_object_operations::do_object_sync.
4573 * \see dt_object_operations::do_object_sync() in the API description
4576 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
4577 __u64 start, __u64 end)
4579 return dt_object_sync(env, dt_object_child(dt), start, end);
4583 * Release LDLM locks on the stripes of a striped directory.
4585 * Iterates over all the locks taken on the stripe objects and
4588 * \param[in] env execution environment
4589 * \param[in] dt striped object
4590 * \param[in] einfo lock description
4591 * \param[in] policy data describing requested lock
4593 * \retval 0 on success
4594 * \retval negative if failed
4596 static int lod_object_unlock_internal(const struct lu_env *env,
4597 struct dt_object *dt,
4598 struct ldlm_enqueue_info *einfo,
4599 union ldlm_policy_data *policy)
4601 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
4606 if (slave_locks == NULL)
4609 for (i = 1; i < slave_locks->count; i++) {
4610 if (lustre_handle_is_used(&slave_locks->handles[i]))
4611 ldlm_lock_decref_and_cancel(&slave_locks->handles[i],
4619 * Implementation of dt_object_operations::do_object_unlock.
4621 * Used to release LDLM lock(s).
4623 * \see dt_object_operations::do_object_unlock() in the API description
4626 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
4627 struct ldlm_enqueue_info *einfo,
4628 union ldlm_policy_data *policy)
4630 struct lod_object *lo = lod_dt_obj(dt);
4631 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
4632 int slave_locks_size;
4636 if (slave_locks == NULL)
4639 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
4640 LASSERT(lo->ldo_dir_stripenr > 1);
4641 /* Note: for remote lock for single stripe dir, MDT will cancel
4642 * the lock by lockh directly */
4643 LASSERT(!dt_object_remote(dt_object_child(dt)));
4645 /* locks were unlocked in MDT layer */
4646 for (i = 1; i < slave_locks->count; i++) {
4647 LASSERT(!lustre_handle_is_used(&slave_locks->handles[i]));
4648 dt_invalidate(env, lo->ldo_stripe[i]);
4651 slave_locks_size = sizeof(*slave_locks) + slave_locks->count *
4652 sizeof(slave_locks->handles[0]);
4653 OBD_FREE(slave_locks, slave_locks_size);
4654 einfo->ei_cbdata = NULL;
4660 * Implementation of dt_object_operations::do_object_lock.
4662 * Used to get LDLM lock on the non-striped and striped objects.
4664 * \see dt_object_operations::do_object_lock() in the API description
4667 static int lod_object_lock(const struct lu_env *env,
4668 struct dt_object *dt,
4669 struct lustre_handle *lh,
4670 struct ldlm_enqueue_info *einfo,
4671 union ldlm_policy_data *policy)
4673 struct lod_object *lo = lod_dt_obj(dt);
4676 int slave_locks_size;
4677 struct lustre_handle_array *slave_locks = NULL;
4680 /* remote object lock */
4681 if (!einfo->ei_enq_slave) {
4682 LASSERT(dt_object_remote(dt));
4683 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
4687 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4688 GOTO(out, rc = -ENOTDIR);
4690 rc = lod_load_striping(env, lo);
4695 if (lo->ldo_dir_stripenr <= 1) {
4697 * NB, ei_cbdata stores pointer to slave locks, if no locks
4698 * taken, make sure it's set to NULL, otherwise MDT will try to
4701 einfo->ei_cbdata = NULL;
4705 slave_locks_size = sizeof(*slave_locks) + lo->ldo_dir_stripenr *
4706 sizeof(slave_locks->handles[0]);
4707 /* Freed in lod_object_unlock */
4708 OBD_ALLOC(slave_locks, slave_locks_size);
4709 if (slave_locks == NULL)
4710 GOTO(out, rc = -ENOMEM);
4711 slave_locks->count = lo->ldo_dir_stripenr;
4713 /* striped directory lock */
4714 for (i = 1; i < lo->ldo_dir_stripenr; i++) {
4715 struct lustre_handle lockh;
4716 struct ldlm_res_id *res_id;
4718 res_id = &lod_env_info(env)->lti_res_id;
4719 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
4721 einfo->ei_res_id = res_id;
4723 LASSERT(lo->ldo_stripe[i] != NULL);
4724 if (likely(dt_object_remote(lo->ldo_stripe[i]))) {
4725 rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh,
4728 struct ldlm_namespace *ns = einfo->ei_namespace;
4729 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
4730 ldlm_completion_callback completion = einfo->ei_cb_cp;
4731 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
4733 if (einfo->ei_mode == LCK_PW ||
4734 einfo->ei_mode == LCK_EX)
4735 dlmflags |= LDLM_FL_COS_INCOMPAT;
4737 /* This only happens if there are mulitple stripes
4738 * on the master MDT, i.e. except stripe0, there are
4739 * other stripes on the Master MDT as well, Only
4740 * happens in the test case right now. */
4741 LASSERT(ns != NULL);
4742 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS,
4743 policy, einfo->ei_mode,
4744 &dlmflags, blocking,
4746 NULL, 0, LVB_T_NONE,
4751 slave_locks->handles[i] = lockh;
4753 einfo->ei_cbdata = slave_locks;
4755 if (rc != 0 && slave_locks != NULL) {
4756 lod_object_unlock_internal(env, dt, einfo, policy);
4757 OBD_FREE(slave_locks, slave_locks_size);
4762 einfo->ei_cbdata = NULL;
4767 * Implementation of dt_object_operations::do_invalidate.
4769 * \see dt_object_operations::do_invalidate() in the API description for details
4771 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
4773 return dt_invalidate(env, dt_object_child(dt));
4776 struct dt_object_operations lod_obj_ops = {
4777 .do_read_lock = lod_object_read_lock,
4778 .do_write_lock = lod_object_write_lock,
4779 .do_read_unlock = lod_object_read_unlock,
4780 .do_write_unlock = lod_object_write_unlock,
4781 .do_write_locked = lod_object_write_locked,
4782 .do_attr_get = lod_attr_get,
4783 .do_declare_attr_set = lod_declare_attr_set,
4784 .do_attr_set = lod_attr_set,
4785 .do_xattr_get = lod_xattr_get,
4786 .do_declare_xattr_set = lod_declare_xattr_set,
4787 .do_xattr_set = lod_xattr_set,
4788 .do_declare_xattr_del = lod_declare_xattr_del,
4789 .do_xattr_del = lod_xattr_del,
4790 .do_xattr_list = lod_xattr_list,
4791 .do_ah_init = lod_ah_init,
4792 .do_declare_create = lod_declare_object_create,
4793 .do_create = lod_object_create,
4794 .do_declare_destroy = lod_declare_object_destroy,
4795 .do_destroy = lod_object_destroy,
4796 .do_index_try = lod_index_try,
4797 .do_declare_ref_add = lod_declare_ref_add,
4798 .do_ref_add = lod_ref_add,
4799 .do_declare_ref_del = lod_declare_ref_del,
4800 .do_ref_del = lod_ref_del,
4801 .do_object_sync = lod_object_sync,
4802 .do_object_lock = lod_object_lock,
4803 .do_object_unlock = lod_object_unlock,
4804 .do_invalidate = lod_invalidate,
4808 * Implementation of dt_body_operations::dbo_read.
4810 * \see dt_body_operations::dbo_read() in the API description for details.
4812 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
4813 struct lu_buf *buf, loff_t *pos)
4815 struct dt_object *next = dt_object_child(dt);
4816 return next->do_body_ops->dbo_read(env, next, buf, pos);
4820 * Implementation of dt_body_operations::dbo_declare_write.
4822 * \see dt_body_operations::dbo_declare_write() in the API description
4825 static ssize_t lod_declare_write(const struct lu_env *env,
4826 struct dt_object *dt,
4827 const struct lu_buf *buf, loff_t pos,
4830 return lod_sub_object_declare_write(env, dt_object_child(dt), buf, pos,
4835 * Implementation of dt_body_operations::dbo_write.
4837 * \see dt_body_operations::dbo_write() in the API description for details.
4839 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
4840 const struct lu_buf *buf, loff_t *pos,
4841 struct thandle *th, int iq)
4843 return lod_sub_object_write(env, dt_object_child(dt), buf, pos, th, iq);
4846 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
4847 __u64 start, __u64 end, struct thandle *th)
4849 if (dt_object_remote(dt))
4852 return lod_sub_object_declare_punch(env, dt_object_child(dt), start,
4856 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
4857 __u64 start, __u64 end, struct thandle *th)
4859 if (dt_object_remote(dt))
4862 return lod_sub_object_punch(env, dt_object_child(dt), start, end, th);
4865 static const struct dt_body_operations lod_body_lnk_ops = {
4866 .dbo_read = lod_read,
4867 .dbo_declare_write = lod_declare_write,
4868 .dbo_write = lod_write
4871 static const struct dt_body_operations lod_body_ops = {
4872 .dbo_read = lod_read,
4873 .dbo_declare_write = lod_declare_write,
4874 .dbo_write = lod_write,
4875 .dbo_declare_punch = lod_declare_punch,
4876 .dbo_punch = lod_punch,
4880 * Implementation of lu_object_operations::loo_object_init.
4882 * The function determines the type and the index of the target device using
4883 * sequence of the object's FID. Then passes control down to the
4884 * corresponding device:
4885 * OSD for the local objects, OSP for remote
4887 * \see lu_object_operations::loo_object_init() in the API description
4890 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
4891 const struct lu_object_conf *conf)
4893 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
4894 struct lu_device *cdev = NULL;
4895 struct lu_object *cobj;
4896 struct lod_tgt_descs *ltd = NULL;
4897 struct lod_tgt_desc *tgt;
4899 int type = LU_SEQ_RANGE_ANY;
4903 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
4905 /* Note: Sometimes, it will Return EAGAIN here, see
4906 * ptrlpc_import_delay_req(), which might confuse
4907 * lu_object_find_at() and make it wait there incorrectly.
4908 * so we convert it to EIO here.*/
4915 if (type == LU_SEQ_RANGE_MDT &&
4916 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
4917 cdev = &lod->lod_child->dd_lu_dev;
4918 } else if (type == LU_SEQ_RANGE_MDT) {
4919 ltd = &lod->lod_mdt_descs;
4921 } else if (type == LU_SEQ_RANGE_OST) {
4922 ltd = &lod->lod_ost_descs;
4929 if (ltd->ltd_tgts_size > idx &&
4930 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
4931 tgt = LTD_TGT(ltd, idx);
4933 LASSERT(tgt != NULL);
4934 LASSERT(tgt->ltd_tgt != NULL);
4936 cdev = &(tgt->ltd_tgt->dd_lu_dev);
4938 lod_putref(lod, ltd);
4941 if (unlikely(cdev == NULL))
4944 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
4945 if (unlikely(cobj == NULL))
4948 lu_object_add(lo, cobj);
4955 * Release resources associated with striping.
4957 * If the object is striped (regular or directory), then release
4958 * the stripe objects references and free the ldo_stripe array.
4960 * \param[in] env execution environment
4961 * \param[in] lo object
4963 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
4965 struct lod_layout_component *lod_comp;
4968 if (lo->ldo_stripe != NULL) {
4969 LASSERT(lo->ldo_comp_entries == NULL);
4970 LASSERT(lo->ldo_dir_stripes_allocated > 0);
4972 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4973 if (lo->ldo_stripe[i])
4974 dt_object_put(env, lo->ldo_stripe[i]);
4977 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
4978 OBD_FREE(lo->ldo_stripe, j);
4979 lo->ldo_stripe = NULL;
4980 lo->ldo_dir_stripes_allocated = 0;
4981 lo->ldo_dir_stripenr = 0;
4982 } else if (lo->ldo_comp_entries != NULL) {
4983 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4984 /* free lod_layout_component::llc_stripe array */
4985 lod_comp = &lo->ldo_comp_entries[i];
4987 if (lod_comp->llc_stripe == NULL)
4989 LASSERT(lod_comp->llc_stripes_allocated != 0);
4990 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
4991 if (lod_comp->llc_stripe[j] != NULL)
4993 &lod_comp->llc_stripe[j]->do_lu);
4995 OBD_FREE(lod_comp->llc_stripe,
4996 sizeof(struct dt_object *) *
4997 lod_comp->llc_stripes_allocated);
4998 lod_comp->llc_stripe = NULL;
4999 lod_comp->llc_stripes_allocated = 0;
5001 lod_free_comp_entries(lo);
5002 lo->ldo_comp_cached = 0;
5007 * Implementation of lu_object_operations::loo_object_start.
5009 * \see lu_object_operations::loo_object_start() in the API description
5012 static int lod_object_start(const struct lu_env *env, struct lu_object *o)
5014 if (S_ISLNK(o->lo_header->loh_attr & S_IFMT)) {
5015 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_lnk_ops;
5016 } else if (S_ISREG(o->lo_header->loh_attr & S_IFMT) ||
5017 fid_is_local_file(lu_object_fid(o))) {
5018 /* Note: some local file (like last rcvd) is created
5019 * through bottom layer (OSD), so the object initialization
5020 * comes to lod, it does not set loh_attr yet, so
5021 * set do_body_ops for local file anyway */
5022 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_ops;
5028 * Implementation of lu_object_operations::loo_object_free.
5030 * \see lu_object_operations::loo_object_free() in the API description
5033 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
5035 struct lod_object *lo = lu2lod_obj(o);
5037 /* release all underlying object pinned */
5038 lod_object_free_striping(env, lo);
5040 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
5044 * Implementation of lu_object_operations::loo_object_release.
5046 * \see lu_object_operations::loo_object_release() in the API description
5049 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
5051 /* XXX: shouldn't we release everything here in case if object
5052 * creation failed before? */
5056 * Implementation of lu_object_operations::loo_object_print.
5058 * \see lu_object_operations::loo_object_print() in the API description
5061 static int lod_object_print(const struct lu_env *env, void *cookie,
5062 lu_printer_t p, const struct lu_object *l)
5064 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
5066 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
5069 struct lu_object_operations lod_lu_obj_ops = {
5070 .loo_object_init = lod_object_init,
5071 .loo_object_start = lod_object_start,
5072 .loo_object_free = lod_object_free,
5073 .loo_object_release = lod_object_release,
5074 .loo_object_print = lod_object_print,