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);
1190 if (!dt_object_exists(next) || dt_object_remote(next) ||
1191 !S_ISREG(attr->la_mode))
1194 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1195 rc = lod_sub_object_declare_xattr_del(env, next,
1196 XATTR_NAME_LOV, th);
1200 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1201 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1202 struct lod_thread_info *info = lod_env_info(env);
1203 struct lu_buf *buf = &info->lti_buf;
1205 buf->lb_buf = info->lti_ea_store;
1206 buf->lb_len = info->lti_ea_store_size;
1207 rc = lod_sub_object_declare_xattr_set(env, next, buf,
1209 LU_XATTR_REPLACE, th);
1216 * Implementation of dt_object_operations::do_attr_set.
1218 * If the object is striped, then apply the changes to all or subset of
1219 * the stripes depending on the object type and specific attributes.
1221 * \see dt_object_operations::do_attr_set() in the API description for details.
1223 static int lod_attr_set(const struct lu_env *env,
1224 struct dt_object *dt,
1225 const struct lu_attr *attr,
1228 struct dt_object *next = dt_object_child(dt);
1229 struct lod_object *lo = lod_dt_obj(dt);
1234 * apply changes to the local object
1236 rc = lod_sub_object_attr_set(env, next, attr, th);
1240 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1241 if (!(attr->la_valid & (LA_UID | LA_GID)))
1244 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1247 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE |
1248 LA_ATIME | LA_MTIME | LA_CTIME |
1253 if (!lod_obj_is_striped(dt))
1257 * if object is striped, apply changes to all the stripes
1259 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1260 LASSERT(lo->ldo_stripe);
1261 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
1262 if (unlikely(lo->ldo_stripe[i] == NULL))
1265 if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1268 rc = lod_sub_object_attr_set(env, lo->ldo_stripe[i],
1274 struct lod_obj_stripe_cb_data data;
1276 data.locd_attr = attr;
1277 data.locd_declare = false;
1278 rc = lod_obj_for_each_stripe(env, lo, th,
1279 lod_obj_stripe_attr_set_cb, &data);
1285 if (!dt_object_exists(next) || dt_object_remote(next) ||
1286 !S_ISREG(attr->la_mode))
1289 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1290 rc = lod_sub_object_xattr_del(env, next, XATTR_NAME_LOV, th);
1294 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1295 struct lod_thread_info *info = lod_env_info(env);
1296 struct lu_buf *buf = &info->lti_buf;
1297 struct ost_id *oi = &info->lti_ostid;
1298 struct lu_fid *fid = &info->lti_fid;
1299 struct lov_mds_md_v1 *lmm;
1300 struct lov_ost_data_v1 *objs;
1303 rc = lod_get_lov_ea(env, lo);
1307 buf->lb_buf = info->lti_ea_store;
1308 buf->lb_len = info->lti_ea_store_size;
1309 lmm = info->lti_ea_store;
1310 magic = le32_to_cpu(lmm->lmm_magic);
1311 if (magic == LOV_MAGIC_COMP_V1) {
1312 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1313 struct lov_comp_md_entry_v1 *lcme =
1314 &lcm->lcm_entries[0];
1316 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1317 magic = le32_to_cpu(lmm->lmm_magic);
1320 if (magic == LOV_MAGIC_V1)
1321 objs = &(lmm->lmm_objects[0]);
1323 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1324 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1325 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1327 fid_to_ostid(fid, oi);
1328 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1330 rc = lod_sub_object_xattr_set(env, next, buf, XATTR_NAME_LOV,
1331 LU_XATTR_REPLACE, th);
1332 } else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1333 struct lod_thread_info *info = lod_env_info(env);
1334 struct lu_buf *buf = &info->lti_buf;
1335 struct lov_comp_md_v1 *lcm;
1336 struct lov_comp_md_entry_v1 *lcme;
1338 rc = lod_get_lov_ea(env, lo);
1342 buf->lb_buf = info->lti_ea_store;
1343 buf->lb_len = info->lti_ea_store_size;
1345 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
1348 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1349 lcme = &lcm->lcm_entries[0];
1350 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1351 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1353 rc = lod_sub_object_xattr_set(env, next, buf, XATTR_NAME_LOV,
1354 LU_XATTR_REPLACE, th);
1361 * Implementation of dt_object_operations::do_xattr_get.
1363 * If LOV EA is requested from the root object and it's not
1364 * found, then return default striping for the filesystem.
1366 * \see dt_object_operations::do_xattr_get() in the API description for details.
1368 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1369 struct lu_buf *buf, const char *name)
1371 struct lod_thread_info *info = lod_env_info(env);
1372 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1377 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1378 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1379 struct lmv_mds_md_v1 *lmv1;
1382 if (rc > (typeof(rc))sizeof(*lmv1))
1385 if (rc < (typeof(rc))sizeof(*lmv1))
1386 RETURN(rc = rc > 0 ? -EINVAL : rc);
1388 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1389 CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1391 info->lti_buf.lb_buf = info->lti_key;
1392 info->lti_buf.lb_len = sizeof(*lmv1);
1393 rc = dt_xattr_get(env, dt_object_child(dt),
1394 &info->lti_buf, name);
1395 if (unlikely(rc != sizeof(*lmv1)))
1396 RETURN(rc = rc > 0 ? -EINVAL : rc);
1398 lmv1 = info->lti_buf.lb_buf;
1399 /* The on-disk LMV EA only contains header, but the
1400 * returned LMV EA size should contain the space for
1401 * the FIDs of all shards of the striped directory. */
1402 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1403 rc = lmv_mds_md_size(
1404 le32_to_cpu(lmv1->lmv_stripe_count),
1407 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1411 RETURN(rc = rc1 != 0 ? rc1 : rc);
1414 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1418 * XXX: Only used by lfsck
1420 * lod returns default striping on the real root of the device
1421 * this is like the root stores default striping for the whole
1422 * filesystem. historically we've been using a different approach
1423 * and store it in the config.
1425 dt_root_get(env, dev->lod_child, &info->lti_fid);
1426 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1428 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1429 struct lov_user_md *lum = buf->lb_buf;
1430 struct lov_desc *desc = &dev->lod_desc;
1432 if (buf->lb_buf == NULL) {
1434 } else if (buf->lb_len >= sizeof(*lum)) {
1435 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1436 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1437 lmm_oi_set_id(&lum->lmm_oi, 0);
1438 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1439 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1440 lum->lmm_stripe_size = cpu_to_le32(
1441 desc->ld_default_stripe_size);
1442 lum->lmm_stripe_count = cpu_to_le16(
1443 desc->ld_default_stripe_count);
1444 lum->lmm_stripe_offset = cpu_to_le16(
1445 desc->ld_default_stripe_offset);
1458 * Checks that the magic of the stripe is sane.
1460 * \param[in] lod lod device
1461 * \param[in] lum a buffer storing LMV EA to verify
1463 * \retval 0 if the EA is sane
1464 * \retval negative otherwise
1466 static int lod_verify_md_striping(struct lod_device *lod,
1467 const struct lmv_user_md_v1 *lum)
1469 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1470 CERROR("%s: invalid lmv_user_md: magic = %x, "
1471 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1472 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1473 (int)le32_to_cpu(lum->lum_stripe_offset),
1474 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1482 * Initialize LMV EA for a slave.
1484 * Initialize slave's LMV EA from the master's LMV EA.
1486 * \param[in] master_lmv a buffer containing master's EA
1487 * \param[out] slave_lmv a buffer where slave's EA will be stored
1490 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1491 const struct lmv_mds_md_v1 *master_lmv)
1493 *slave_lmv = *master_lmv;
1494 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1500 * Generate LMV EA from the object passed as \a dt. The object must have
1501 * the stripes created and initialized.
1503 * \param[in] env execution environment
1504 * \param[in] dt object
1505 * \param[out] lmv_buf buffer storing generated LMV EA
1507 * \retval 0 on success
1508 * \retval negative if failed
1510 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1511 struct lu_buf *lmv_buf)
1513 struct lod_thread_info *info = lod_env_info(env);
1514 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1515 struct lod_object *lo = lod_dt_obj(dt);
1516 struct lmv_mds_md_v1 *lmm1;
1518 int type = LU_SEQ_RANGE_ANY;
1523 LASSERT(lo->ldo_dir_striped != 0);
1524 LASSERT(lo->ldo_dir_stripenr > 0);
1525 stripe_count = lo->ldo_dir_stripenr;
1526 /* Only store the LMV EA heahder on the disk. */
1527 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1528 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1532 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1535 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1536 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1537 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1538 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1539 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1544 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1545 lmv_buf->lb_buf = info->lti_ea_store;
1546 lmv_buf->lb_len = sizeof(*lmm1);
1552 * Create in-core represenation for a striped directory.
1554 * Parse the buffer containing LMV EA and instantiate LU objects
1555 * representing the stripe objects. The pointers to the objects are
1556 * stored in ldo_stripe field of \a lo. This function is used when
1557 * we need to access an already created object (i.e. load from a disk).
1559 * \param[in] env execution environment
1560 * \param[in] lo lod object
1561 * \param[in] buf buffer containing LMV EA
1563 * \retval 0 on success
1564 * \retval negative if failed
1566 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1567 const struct lu_buf *buf)
1569 struct lod_thread_info *info = lod_env_info(env);
1570 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1571 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1572 struct dt_object **stripe;
1573 union lmv_mds_md *lmm = buf->lb_buf;
1574 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1575 struct lu_fid *fid = &info->lti_fid;
1580 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1583 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1584 lo->ldo_dir_slave_stripe = 1;
1588 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1591 if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1594 LASSERT(lo->ldo_stripe == NULL);
1595 OBD_ALLOC(stripe, sizeof(stripe[0]) *
1596 (le32_to_cpu(lmv1->lmv_stripe_count)));
1600 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1601 struct dt_device *tgt_dt;
1602 struct dt_object *dto;
1603 int type = LU_SEQ_RANGE_ANY;
1606 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1607 if (!fid_is_sane(fid))
1608 GOTO(out, rc = -ESTALE);
1610 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1614 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1615 tgt_dt = lod->lod_child;
1617 struct lod_tgt_desc *tgt;
1619 tgt = LTD_TGT(ltd, idx);
1621 GOTO(out, rc = -ESTALE);
1622 tgt_dt = tgt->ltd_tgt;
1625 dto = dt_locate_at(env, tgt_dt, fid,
1626 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1629 GOTO(out, rc = PTR_ERR(dto));
1634 lo->ldo_stripe = stripe;
1635 lo->ldo_dir_stripenr = le32_to_cpu(lmv1->lmv_stripe_count);
1636 lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1638 lod_object_free_striping(env, lo);
1644 * Declare create a striped directory.
1646 * Declare creating a striped directory with a given stripe pattern on the
1647 * specified MDTs. A striped directory is represented as a regular directory
1648 * - an index listing all the stripes. The stripes point back to the master
1649 * object with ".." and LinkEA. The master object gets LMV EA which
1650 * identifies it as a striped directory. The function allocates FIDs
1653 * \param[in] env execution environment
1654 * \param[in] dt object
1655 * \param[in] attr attributes to initialize the objects with
1656 * \param[in] dof type of objects to be created
1657 * \param[in] th transaction handle
1659 * \retval 0 on success
1660 * \retval negative if failed
1662 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1663 struct dt_object *dt,
1664 struct lu_attr *attr,
1665 struct dt_object_format *dof,
1668 struct lod_thread_info *info = lod_env_info(env);
1669 struct lu_buf lmv_buf;
1670 struct lu_buf slave_lmv_buf;
1671 struct lmv_mds_md_v1 *lmm;
1672 struct lmv_mds_md_v1 *slave_lmm = NULL;
1673 struct dt_insert_rec *rec = &info->lti_dt_rec;
1674 struct lod_object *lo = lod_dt_obj(dt);
1679 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1682 lmm = lmv_buf.lb_buf;
1684 OBD_ALLOC_PTR(slave_lmm);
1685 if (slave_lmm == NULL)
1686 GOTO(out, rc = -ENOMEM);
1688 lod_prep_slave_lmv_md(slave_lmm, lmm);
1689 slave_lmv_buf.lb_buf = slave_lmm;
1690 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1692 if (!dt_try_as_dir(env, dt_object_child(dt)))
1693 GOTO(out, rc = -EINVAL);
1695 rec->rec_type = S_IFDIR;
1696 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
1697 struct dt_object *dto = lo->ldo_stripe[i];
1698 char *stripe_name = info->lti_key;
1699 struct lu_name *sname;
1700 struct linkea_data ldata = { NULL };
1701 struct lu_buf linkea_buf;
1703 rc = lod_sub_object_declare_create(env, dto, attr, NULL,
1708 if (!dt_try_as_dir(env, dto))
1709 GOTO(out, rc = -EINVAL);
1711 rc = lod_sub_object_declare_ref_add(env, dto, th);
1715 rec->rec_fid = lu_object_fid(&dto->do_lu);
1716 rc = lod_sub_object_declare_insert(env, dto,
1717 (const struct dt_rec *)rec,
1718 (const struct dt_key *)dot, th);
1722 /* master stripe FID will be put to .. */
1723 rec->rec_fid = lu_object_fid(&dt->do_lu);
1724 rc = lod_sub_object_declare_insert(env, dto,
1725 (const struct dt_rec *)rec,
1726 (const struct dt_key *)dotdot,
1731 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1732 cfs_fail_val != i) {
1733 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1735 slave_lmm->lmv_master_mdt_index =
1738 slave_lmm->lmv_master_mdt_index =
1740 rc = lod_sub_object_declare_xattr_set(env, dto,
1741 &slave_lmv_buf, XATTR_NAME_LMV, 0, th);
1746 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1748 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1749 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1751 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1752 PFID(lu_object_fid(&dto->do_lu)), i);
1754 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1755 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1756 sname, lu_object_fid(&dt->do_lu));
1760 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1761 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1762 rc = lod_sub_object_declare_xattr_set(env, dto, &linkea_buf,
1763 XATTR_NAME_LINK, 0, th);
1767 rec->rec_fid = lu_object_fid(&dto->do_lu);
1768 rc = lod_sub_object_declare_insert(env, dt_object_child(dt),
1769 (const struct dt_rec *)rec,
1770 (const struct dt_key *)stripe_name,
1775 rc = lod_sub_object_declare_ref_add(env, dt_object_child(dt),
1781 rc = lod_sub_object_declare_xattr_set(env, dt_object_child(dt),
1782 &lmv_buf, XATTR_NAME_LMV, 0, th);
1786 if (slave_lmm != NULL)
1787 OBD_FREE_PTR(slave_lmm);
1792 static int lod_prep_md_striped_create(const struct lu_env *env,
1793 struct dt_object *dt,
1794 struct lu_attr *attr,
1795 const struct lmv_user_md_v1 *lum,
1796 struct dt_object_format *dof,
1799 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1800 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1801 struct lod_object *lo = lod_dt_obj(dt);
1802 struct dt_object **stripe;
1811 /* The lum has been verifed in lod_verify_md_striping */
1812 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
1813 LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1815 stripe_count = le32_to_cpu(lum->lum_stripe_count);
1817 OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1818 if (idx_array == NULL)
1821 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1823 GOTO(out_free, rc = -ENOMEM);
1825 /* Start index will be the master MDT */
1826 master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1827 idx_array[0] = master_index;
1828 for (i = 0; i < stripe_count; i++) {
1829 struct lod_tgt_desc *tgt = NULL;
1830 struct dt_object *dto;
1831 struct lu_fid fid = { 0 };
1833 struct lu_object_conf conf = { 0 };
1834 struct dt_device *tgt_dt = NULL;
1836 /* Try to find next avaible target */
1838 for (j = 0; j < lod->lod_remote_mdt_count;
1839 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1840 bool already_allocated = false;
1843 CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
1844 idx, lod->lod_remote_mdt_count + 1, i);
1845 if (idx == master_index) {
1846 /* Allocate the FID locally */
1847 rc = obd_fid_alloc(env, lod->lod_child_exp,
1851 tgt_dt = lod->lod_child;
1855 /* Find next available target */
1856 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx))
1859 if (likely(!OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
1860 /* check whether the idx already exists
1861 * in current allocated array */
1862 for (k = 0; k < i; k++) {
1863 if (idx_array[k] == idx) {
1864 already_allocated = true;
1869 if (already_allocated)
1873 /* check the status of the OSP */
1874 tgt = LTD_TGT(ltd, idx);
1878 tgt_dt = tgt->ltd_tgt;
1879 rc = dt_statfs(env, tgt_dt, NULL);
1881 /* this OSP doesn't feel well */
1886 rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1895 /* Can not allocate more stripes */
1896 if (j == lod->lod_remote_mdt_count) {
1897 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1898 lod2obd(lod)->obd_name, stripe_count, i - 1);
1902 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
1903 idx, i, PFID(&fid));
1905 /* Set the start index for next stripe allocation */
1906 if (i < stripe_count - 1)
1907 idx_array[i + 1] = (idx + 1) %
1908 (lod->lod_remote_mdt_count + 1);
1909 /* tgt_dt and fid must be ready after search avaible OSP
1910 * in the above loop */
1911 LASSERT(tgt_dt != NULL);
1912 LASSERT(fid_is_sane(&fid));
1913 conf.loc_flags = LOC_F_NEW;
1914 dto = dt_locate_at(env, tgt_dt, &fid,
1915 dt->do_lu.lo_dev->ld_site->ls_top_dev,
1918 GOTO(out_put, rc = PTR_ERR(dto));
1922 lo->ldo_dir_striped = 1;
1923 lo->ldo_stripe = stripe;
1924 lo->ldo_dir_stripenr = i;
1925 lo->ldo_dir_stripes_allocated = stripe_count;
1927 if (lo->ldo_dir_stripenr == 0)
1928 GOTO(out_put, rc = -ENOSPC);
1930 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
1936 for (i = 0; i < stripe_count; i++)
1937 if (stripe[i] != NULL)
1938 dt_object_put(env, stripe[i]);
1939 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
1940 lo->ldo_dir_stripenr = 0;
1941 lo->ldo_dir_stripes_allocated = 0;
1942 lo->ldo_stripe = NULL;
1946 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
1952 * Declare create striped md object.
1954 * The function declares intention to create a striped directory. This is a
1955 * wrapper for lod_prep_md_striped_create(). The only additional functionality
1956 * is to verify pattern \a lum_buf is good. Check that function for the details.
1958 * \param[in] env execution environment
1959 * \param[in] dt object
1960 * \param[in] attr attributes to initialize the objects with
1961 * \param[in] lum_buf a pattern specifying the number of stripes and
1963 * \param[in] dof type of objects to be created
1964 * \param[in] th transaction handle
1966 * \retval 0 on success
1967 * \retval negative if failed
1970 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
1971 struct dt_object *dt,
1972 struct lu_attr *attr,
1973 const struct lu_buf *lum_buf,
1974 struct dt_object_format *dof,
1977 struct lod_object *lo = lod_dt_obj(dt);
1978 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1979 struct lmv_user_md_v1 *lum;
1983 lum = lum_buf->lb_buf;
1984 LASSERT(lum != NULL);
1986 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
1987 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
1988 (int)le32_to_cpu(lum->lum_stripe_offset));
1990 if (le32_to_cpu(lum->lum_stripe_count) == 0)
1993 rc = lod_verify_md_striping(lod, lum);
1997 /* prepare dir striped objects */
1998 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2000 /* failed to create striping, let's reset
2001 * config so that others don't get confused */
2002 lod_object_free_striping(env, lo);
2010 * Implementation of dt_object_operations::do_declare_xattr_set.
2012 * Used with regular (non-striped) objects. Basically it
2013 * initializes the striping information and applies the
2014 * change to all the stripes.
2016 * \see dt_object_operations::do_declare_xattr_set() in the API description
2019 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2020 struct dt_object *dt,
2021 const struct lu_buf *buf,
2022 const char *name, int fl,
2025 struct dt_object *next = dt_object_child(dt);
2026 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2027 struct lod_object *lo = lod_dt_obj(dt);
2032 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2033 struct lmv_user_md_v1 *lum;
2035 LASSERT(buf != NULL && buf->lb_buf != NULL);
2037 rc = lod_verify_md_striping(d, lum);
2040 } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2041 rc = lod_verify_striping(d, buf, false, 0);
2046 rc = lod_sub_object_declare_xattr_set(env, next, buf, name, fl, th);
2050 /* Note: Do not set LinkEA on sub-stripes, otherwise
2051 * it will confuse the fid2path process(see mdt_path_current()).
2052 * The linkEA between master and sub-stripes is set in
2053 * lod_xattr_set_lmv(). */
2054 if (strcmp(name, XATTR_NAME_LINK) == 0)
2057 /* set xattr to each stripes, if needed */
2058 rc = lod_load_striping(env, lo);
2062 if (lo->ldo_dir_stripenr == 0)
2065 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2066 LASSERT(lo->ldo_stripe[i]);
2068 rc = lod_sub_object_declare_xattr_set(env, lo->ldo_stripe[i],
2078 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2079 struct lod_object *lo,
2080 struct dt_object *dt, struct thandle *th,
2082 struct lod_obj_stripe_cb_data *data)
2084 struct lod_thread_info *info = lod_env_info(env);
2085 struct filter_fid *ff = &info->lti_ff;
2086 struct lu_buf *buf = &info->lti_buf;
2090 buf->lb_len = sizeof(*ff);
2091 rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2098 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2099 ff->ff_parent.f_ver = stripe_idx;
2100 fid_cpu_to_le(&ff->ff_parent, &ff->ff_parent);
2101 if (data->locd_declare)
2102 rc = lod_sub_object_declare_xattr_set(env, dt, buf,
2104 LU_XATTR_REPLACE, th);
2106 rc = lod_sub_object_xattr_set(env, dt, buf, XATTR_NAME_FID,
2107 LU_XATTR_REPLACE, th);
2113 * Reset parent FID on OST object
2115 * Replace parent FID with @dt object FID, which is only called during migration
2116 * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2117 * the FID is changed.
2119 * \param[in] env execution environment
2120 * \param[in] dt dt_object whose stripes's parent FID will be reset
2121 * \parem[in] th thandle
2122 * \param[in] declare if it is declare
2124 * \retval 0 if reset succeeds
2125 * \retval negative errno if reset fails
2127 static int lod_object_replace_parent_fid(const struct lu_env *env,
2128 struct dt_object *dt,
2129 struct thandle *th, bool declare)
2131 struct lod_object *lo = lod_dt_obj(dt);
2132 struct lod_thread_info *info = lod_env_info(env);
2133 struct lu_buf *buf = &info->lti_buf;
2134 struct filter_fid *ff;
2135 struct lod_obj_stripe_cb_data data;
2139 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2141 /* set xattr to each stripes, if needed */
2142 rc = lod_load_striping(env, lo);
2146 if (!lod_obj_is_striped(dt))
2149 if (info->lti_ea_store_size < sizeof(*ff)) {
2150 rc = lod_ea_store_resize(info, sizeof(*ff));
2155 buf->lb_buf = info->lti_ea_store;
2156 buf->lb_len = info->lti_ea_store_size;
2158 data.locd_declare = declare;
2159 rc = lod_obj_for_each_stripe(env, lo, th,
2160 lod_obj_stripe_replace_parent_fid_cb, &data);
2166 * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2167 * the xattr value is binary lov_comp_md_v1 which contains component(s)
2170 * \param[in] env execution environment
2171 * \param[in] dt dt_object to add components on
2172 * \param[in] buf buffer contains components to be added
2173 * \parem[in] th thandle
2175 * \retval 0 on success
2176 * \retval negative errno on failure
2178 static int lod_declare_layout_add(const struct lu_env *env,
2179 struct dt_object *dt,
2180 const struct lu_buf *buf,
2183 struct lod_layout_component *comp_array, *lod_comp;
2184 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2185 struct lov_desc *desc = &d->lod_desc;
2186 struct lod_object *lo = lod_dt_obj(dt);
2187 struct lov_user_md_v1 *v1;
2188 struct lov_user_md_v3 *v3;
2189 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2190 struct lu_extent *ext;
2193 int i, rc, array_cnt;
2196 LASSERT(lo->ldo_is_composite);
2198 magic = comp_v1->lcm_magic;
2199 /* Replay request, see comment for LOV_MAGIC_DEF */
2200 if (unlikely(le32_to_cpu(magic) == LOV_MAGIC_COMP_V1_DEF)) {
2201 struct dt_object *next = dt_object_child(dt);
2203 lod_object_free_striping(env, lo);
2204 rc = lod_use_defined_striping(env, lo, buf);
2206 lo->ldo_comp_cached = 1;
2207 rc = lod_sub_object_declare_xattr_set(env, next, buf,
2214 prev_end = lo->ldo_comp_entries[lo->ldo_comp_cnt - 1].llc_extent.e_end;
2215 rc = lod_verify_striping(d, buf, false, prev_end);
2219 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2220 lustre_swab_lov_comp_md_v1(comp_v1);
2221 magic = comp_v1->lcm_magic;
2224 if (magic != LOV_USER_MAGIC_COMP_V1)
2227 array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2228 OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2229 if (comp_array == NULL)
2232 memcpy(comp_array, lo->ldo_comp_entries,
2233 sizeof(*comp_array) * lo->ldo_comp_cnt);
2235 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2236 v1 = (struct lov_user_md *)((char *)comp_v1 +
2237 comp_v1->lcm_entries[i].lcme_offset);
2238 ext = &comp_v1->lcm_entries[i].lcme_extent;
2240 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2241 lod_comp->llc_extent.e_start = ext->e_start;
2242 lod_comp->llc_extent.e_end = ext->e_end;
2243 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2245 lod_comp->llc_stripenr = v1->lmm_stripe_count;
2246 if (lod_comp->llc_stripenr <= 0)
2247 lod_comp->llc_stripenr = desc->ld_default_stripe_count;
2248 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2249 if (lod_comp->llc_stripe_size <= 0)
2250 lod_comp->llc_stripe_size =
2251 desc->ld_default_stripe_size;
2253 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2255 v3 = (struct lov_user_md_v3 *) v1;
2256 if (v3->lmm_pool_name[0] != '\0') {
2257 len = strlen(v3->lmm_pool_name);
2258 OBD_ALLOC(lod_comp->llc_pool, len + 1);
2259 if (lod_comp->llc_pool == NULL)
2260 GOTO(error, rc = -ENOMEM);
2261 strncpy(lod_comp->llc_pool, v3->lmm_pool_name,
2267 OBD_FREE(lo->ldo_comp_entries, sizeof(*lod_comp) * lo->ldo_comp_cnt);
2268 lo->ldo_comp_entries = comp_array;
2269 lo->ldo_comp_cnt = array_cnt;
2270 /* No need to increase layout generation here, it will be increased
2271 * later when generating component ID for the new components */
2273 rc = lod_declare_striped_object(env, dt, NULL, NULL, th);
2277 for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2278 lod_comp = &comp_array[i];
2279 if (lod_comp->llc_pool != NULL) {
2280 OBD_FREE(lod_comp->llc_pool,
2281 strlen(lod_comp->llc_pool) + 1);
2282 lod_comp->llc_pool = NULL;
2285 OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2289 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2291 int magic, size = 0, i;
2292 struct lod_layout_component *comp_entries;
2297 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2298 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2300 lo->ldo_def_striping->lds_def_striping_is_composite;
2302 comp_cnt = lo->ldo_comp_cnt;
2303 comp_entries = lo->ldo_comp_entries;
2304 is_composite = lo->ldo_is_composite;
2308 LASSERT(comp_cnt != 0 && comp_entries != NULL);
2310 size = sizeof(struct lov_comp_md_v1) +
2311 sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2312 LASSERT(size % sizeof(__u64) == 0);
2315 for (i = 0; i < comp_cnt; i++) {
2316 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2318 size += lov_user_md_size(
2319 is_dir ? 0 : comp_entries[i].llc_stripenr,
2321 LASSERT(size % sizeof(__u64) == 0);
2327 * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2328 * the '$field' can only be 'flags' now. The xattr value is binary
2329 * lov_comp_md_v1 which contains the component ID(s) and the value of
2330 * the field to be modified.
2332 * \param[in] env execution environment
2333 * \param[in] dt dt_object to be modified
2334 * \param[in] op operation string, like "set.flags"
2335 * \param[in] buf buffer contains components to be set
2336 * \parem[in] th thandle
2338 * \retval 0 on success
2339 * \retval negative errno on failure
2341 static int lod_declare_layout_set(const struct lu_env *env,
2342 struct dt_object *dt,
2343 char *op, const struct lu_buf *buf,
2346 struct lod_layout_component *lod_comp;
2347 struct lod_thread_info *info = lod_env_info(env);
2348 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2349 struct lod_object *lo = lod_dt_obj(dt);
2350 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2353 bool changed = false;
2356 if (strcmp(op, "set.flags") != 0) {
2357 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2358 lod2obd(d)->obd_name, op);
2362 magic = comp_v1->lcm_magic;
2363 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2364 lustre_swab_lov_comp_md_v1(comp_v1);
2365 magic = comp_v1->lcm_magic;
2368 if (magic != LOV_USER_MAGIC_COMP_V1)
2371 if (comp_v1->lcm_entry_count == 0) {
2372 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2373 lod2obd(d)->obd_name);
2377 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2378 id = comp_v1->lcm_entries[i].lcme_id;
2380 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2381 lod_comp = &lo->ldo_comp_entries[j];
2382 if (id == lod_comp->llc_id || id == LCME_ID_ALL) {
2383 lod_comp->llc_flags =
2384 comp_v1->lcm_entries[i].lcme_flags;
2391 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2392 lod2obd(d)->obd_name);
2396 lod_obj_inc_layout_gen(lo);
2398 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2399 rc = lod_sub_object_declare_xattr_set(env, dt, &info->lti_buf,
2400 XATTR_NAME_LOV, 0, th);
2405 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2406 * and the xattr value is a unique component ID or a special lcme_id.
2408 * \param[in] env execution environment
2409 * \param[in] dt dt_object to be operated on
2410 * \param[in] buf buffer contains component ID or lcme_id
2411 * \parem[in] th thandle
2413 * \retval 0 on success
2414 * \retval negative errno on failure
2416 static int lod_declare_layout_del(const struct lu_env *env,
2417 struct dt_object *dt,
2418 const struct lu_buf *buf,
2421 struct lod_thread_info *info = lod_env_info(env);
2422 struct dt_object *next = dt_object_child(dt);
2423 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2424 struct lod_object *lo = lod_dt_obj(dt);
2425 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2430 LASSERT(lo->ldo_is_composite);
2432 id = *(__u32 *)buf->lb_buf;
2433 if (id == 0 || id == LCME_ID_NONE) {
2434 CDEBUG(D_LAYOUT, "%s: invalid component id %#x\n",
2435 lod2obd(d)->obd_name, id);
2439 left = lo->ldo_comp_cnt;
2443 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
2444 struct lod_layout_component *lod_comp;
2446 lod_comp = &lo->ldo_comp_entries[i];
2448 if (id <= LCME_ID_MAX && id != lod_comp->llc_id)
2450 else if (id > LCME_ID_MAX && id < LCME_ID_ALL &&
2451 !(id & lod_comp->llc_flags))
2454 if (left != (i + 1)) {
2455 CDEBUG(D_LAYOUT, "%s: this deletion will create "
2456 "a hole.\n", lod2obd(d)->obd_name);
2461 /* Mark the component as deleted */
2462 lod_comp->llc_id = LCME_ID_INVAL;
2464 /* Not instantiated component */
2465 if (lod_comp->llc_stripe == NULL)
2468 LASSERT(lod_comp->llc_stripenr > 0);
2469 for (j = 0; j < lod_comp->llc_stripenr; j++) {
2470 struct dt_object *obj = lod_comp->llc_stripe[j];
2474 rc = lod_sub_object_declare_destroy(env, obj, th);
2480 LASSERTF(left >= 0, "left = %d\n", left);
2481 if (left == lo->ldo_comp_cnt) {
2482 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
2483 lod2obd(d)->obd_name, id);
2487 memset(attr, 0, sizeof(*attr));
2488 attr->la_valid = LA_SIZE;
2489 rc = lod_sub_object_declare_attr_set(env, next, attr, th);
2494 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2495 rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
2496 XATTR_NAME_LOV, 0, th);
2498 rc = lod_sub_object_declare_xattr_del(env, next, XATTR_NAME_LOV,
2506 * Declare layout add/set/del operations issued by special xattr names:
2508 * XATTR_LUSTRE_LOV.add add component(s) to existing file
2509 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
2510 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
2512 * \param[in] env execution environment
2513 * \param[in] dt object
2514 * \param[in] name name of xattr
2515 * \param[in] buf lu_buf contains xattr value
2516 * \param[in] th transaction handle
2518 * \retval 0 on success
2519 * \retval negative if failed
2521 static int lod_declare_modify_layout(const struct lu_env *env,
2522 struct dt_object *dt,
2524 const struct lu_buf *buf,
2527 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2528 struct lod_object *lo = lod_dt_obj(dt);
2529 struct dt_object *next = dt_object_child(&lo->ldo_obj);
2531 int rc, len = strlen(XATTR_LUSTRE_LOV);
2534 LASSERT(dt_object_exists(dt));
2536 if (strlen(name) <= len || name[len] != '.') {
2537 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
2538 lod2obd(d)->obd_name, name);
2543 dt_write_lock(env, next, 0);
2544 rc = lod_load_striping_locked(env, lo);
2548 /* the layout to be modified must be a composite layout */
2549 if (!lo->ldo_is_composite) {
2550 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
2551 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
2552 GOTO(unlock, rc = -EINVAL);
2555 op = (char *)name + len;
2556 if (strcmp(op, "add") == 0) {
2557 rc = lod_declare_layout_add(env, dt, buf, th);
2558 } else if (strcmp(op, "del") == 0) {
2559 rc = lod_declare_layout_del(env, dt, buf, th);
2560 } else if (strncmp(op, "set", strlen("set")) == 0) {
2561 rc = lod_declare_layout_set(env, dt, op, buf, th);
2563 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
2564 lod2obd(d)->obd_name, name);
2565 GOTO(unlock, rc = -ENOTSUPP);
2569 lod_object_free_striping(env, lo);
2570 dt_write_unlock(env, next);
2576 * Implementation of dt_object_operations::do_declare_xattr_set.
2578 * \see dt_object_operations::do_declare_xattr_set() in the API description
2581 * the extension to the API:
2582 * - declaring LOVEA requests striping creation
2583 * - LU_XATTR_REPLACE means layout swap
2585 static int lod_declare_xattr_set(const struct lu_env *env,
2586 struct dt_object *dt,
2587 const struct lu_buf *buf,
2588 const char *name, int fl,
2591 struct dt_object *next = dt_object_child(dt);
2592 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2597 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2598 if ((S_ISREG(mode) || mode == 0) && !(fl & LU_XATTR_REPLACE) &&
2599 (strcmp(name, XATTR_NAME_LOV) == 0 ||
2600 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
2602 * this is a request to create object's striping.
2604 * allow to declare predefined striping on a new (!mode) object
2605 * which is supposed to be replay of regular file creation
2606 * (when LOV setting is declared)
2608 * LU_XATTR_REPLACE is set to indicate a layout swap
2610 if (dt_object_exists(dt)) {
2611 rc = dt_attr_get(env, next, attr);
2615 memset(attr, 0, sizeof(*attr));
2616 attr->la_valid = LA_TYPE | LA_MODE;
2617 attr->la_mode = S_IFREG;
2619 rc = lod_declare_striped_object(env, dt, attr, buf, th);
2620 } else if (S_ISREG(mode) &&
2621 strlen(name) > strlen(XATTR_LUSTRE_LOV) + 1 &&
2622 strncmp(name, XATTR_LUSTRE_LOV,
2623 strlen(XATTR_LUSTRE_LOV)) == 0) {
2625 * this is a request to modify object's striping.
2626 * add/set/del component(s).
2628 if (!dt_object_exists(dt))
2631 rc = lod_declare_modify_layout(env, dt, name, buf, th);
2632 } else if (S_ISDIR(mode)) {
2633 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2634 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
2635 rc = lod_object_replace_parent_fid(env, dt, th, true);
2637 rc = lod_sub_object_declare_xattr_set(env, next, buf, name,
2645 * Apply xattr changes to the object.
2647 * Applies xattr changes to the object and the stripes if the latter exist.
2649 * \param[in] env execution environment
2650 * \param[in] dt object
2651 * \param[in] buf buffer pointing to the new value of xattr
2652 * \param[in] name name of xattr
2653 * \param[in] fl flags
2654 * \param[in] th transaction handle
2656 * \retval 0 on success
2657 * \retval negative if failed
2659 static int lod_xattr_set_internal(const struct lu_env *env,
2660 struct dt_object *dt,
2661 const struct lu_buf *buf,
2662 const char *name, int fl,
2665 struct dt_object *next = dt_object_child(dt);
2666 struct lod_object *lo = lod_dt_obj(dt);
2671 rc = lod_sub_object_xattr_set(env, next, buf, name, fl, th);
2672 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2675 /* Note: Do not set LinkEA on sub-stripes, otherwise
2676 * it will confuse the fid2path process(see mdt_path_current()).
2677 * The linkEA between master and sub-stripes is set in
2678 * lod_xattr_set_lmv(). */
2679 if (lo->ldo_dir_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2682 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2683 LASSERT(lo->ldo_stripe[i]);
2685 rc = lod_sub_object_xattr_set(env, lo->ldo_stripe[i], buf, name,
2695 * Delete an extended attribute.
2697 * Deletes specified xattr from the object and the stripes if the latter exist.
2699 * \param[in] env execution environment
2700 * \param[in] dt object
2701 * \param[in] name name of xattr
2702 * \param[in] th transaction handle
2704 * \retval 0 on success
2705 * \retval negative if failed
2707 static int lod_xattr_del_internal(const struct lu_env *env,
2708 struct dt_object *dt,
2709 const char *name, struct thandle *th)
2711 struct dt_object *next = dt_object_child(dt);
2712 struct lod_object *lo = lod_dt_obj(dt);
2717 rc = lod_sub_object_xattr_del(env, next, name, th);
2718 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2721 if (lo->ldo_dir_stripenr == 0)
2724 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2725 LASSERT(lo->ldo_stripe[i]);
2727 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name,
2737 * Set default striping on a directory.
2739 * Sets specified striping on a directory object unless it matches the default
2740 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2741 * EA. This striping will be used when regular file is being created in this
2744 * \param[in] env execution environment
2745 * \param[in] dt the striped object
2746 * \param[in] buf buffer with the striping
2747 * \param[in] name name of EA
2748 * \param[in] fl xattr flag (see OSD API description)
2749 * \param[in] th transaction handle
2751 * \retval 0 on success
2752 * \retval negative if failed
2754 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
2755 struct dt_object *dt,
2756 const struct lu_buf *buf,
2757 const char *name, int fl,
2760 struct lov_user_md_v1 *lum;
2761 struct lov_user_md_v3 *v3 = NULL;
2762 const char *pool_name = NULL;
2767 LASSERT(buf != NULL && buf->lb_buf != NULL);
2770 switch (lum->lmm_magic) {
2771 case LOV_USER_MAGIC_V3:
2773 if (v3->lmm_pool_name[0] != '\0')
2774 pool_name = v3->lmm_pool_name;
2776 case LOV_USER_MAGIC_V1:
2777 /* if { size, offset, count } = { 0, -1, 0 } and no pool
2778 * (i.e. all default values specified) then delete default
2779 * striping from dir. */
2781 "set default striping: sz %u # %u offset %d %s %s\n",
2782 (unsigned)lum->lmm_stripe_size,
2783 (unsigned)lum->lmm_stripe_count,
2784 (int)lum->lmm_stripe_offset,
2785 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
2787 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
2788 lum->lmm_stripe_count,
2789 lum->lmm_stripe_offset,
2792 case LOV_USER_MAGIC_COMP_V1:
2796 CERROR("Invalid magic %x\n", lum->lmm_magic);
2801 rc = lod_xattr_del_internal(env, dt, name, th);
2805 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2812 * Set default striping on a directory object.
2814 * Sets specified striping on a directory object unless it matches the default
2815 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2816 * EA. This striping will be used when a new directory is being created in the
2819 * \param[in] env execution environment
2820 * \param[in] dt the striped object
2821 * \param[in] buf buffer with the striping
2822 * \param[in] name name of EA
2823 * \param[in] fl xattr flag (see OSD API description)
2824 * \param[in] th transaction handle
2826 * \retval 0 on success
2827 * \retval negative if failed
2829 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
2830 struct dt_object *dt,
2831 const struct lu_buf *buf,
2832 const char *name, int fl,
2835 struct lmv_user_md_v1 *lum;
2839 LASSERT(buf != NULL && buf->lb_buf != NULL);
2842 CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
2843 le32_to_cpu(lum->lum_stripe_count),
2844 (int)le32_to_cpu(lum->lum_stripe_offset));
2846 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
2847 le32_to_cpu(lum->lum_stripe_offset)) &&
2848 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
2849 rc = lod_xattr_del_internal(env, dt, name, th);
2853 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2862 * Turn directory into a striped directory.
2864 * During replay the client sends the striping created before MDT
2865 * failure, then the layer above LOD sends this defined striping
2866 * using ->do_xattr_set(), so LOD uses this method to replay creation
2867 * of the stripes. Notice the original information for the striping
2868 * (#stripes, FIDs, etc) was transferred in declare path.
2870 * \param[in] env execution environment
2871 * \param[in] dt the striped object
2872 * \param[in] buf not used currently
2873 * \param[in] name not used currently
2874 * \param[in] fl xattr flag (see OSD API description)
2875 * \param[in] th transaction handle
2877 * \retval 0 on success
2878 * \retval negative if failed
2880 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
2881 const struct lu_buf *buf, const char *name,
2882 int fl, struct thandle *th)
2884 struct lod_object *lo = lod_dt_obj(dt);
2885 struct lod_thread_info *info = lod_env_info(env);
2886 struct lu_attr *attr = &info->lti_attr;
2887 struct dt_object_format *dof = &info->lti_format;
2888 struct lu_buf lmv_buf;
2889 struct lu_buf slave_lmv_buf;
2890 struct lmv_mds_md_v1 *lmm;
2891 struct lmv_mds_md_v1 *slave_lmm = NULL;
2892 struct dt_insert_rec *rec = &info->lti_dt_rec;
2897 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2900 /* The stripes are supposed to be allocated in declare phase,
2901 * if there are no stripes being allocated, it will skip */
2902 if (lo->ldo_dir_stripenr == 0)
2905 rc = dt_attr_get(env, dt_object_child(dt), attr);
2909 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
2910 LA_MODE | LA_UID | LA_GID | LA_TYPE;
2911 dof->dof_type = DFT_DIR;
2913 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
2916 lmm = lmv_buf.lb_buf;
2918 OBD_ALLOC_PTR(slave_lmm);
2919 if (slave_lmm == NULL)
2922 lod_prep_slave_lmv_md(slave_lmm, lmm);
2923 slave_lmv_buf.lb_buf = slave_lmm;
2924 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
2926 rec->rec_type = S_IFDIR;
2927 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2928 struct dt_object *dto;
2929 char *stripe_name = info->lti_key;
2930 struct lu_name *sname;
2931 struct linkea_data ldata = { NULL };
2932 struct lu_buf linkea_buf;
2934 dto = lo->ldo_stripe[i];
2936 dt_write_lock(env, dto, MOR_TGT_CHILD);
2937 rc = lod_sub_object_create(env, dto, attr, NULL, dof,
2940 dt_write_unlock(env, dto);
2944 rc = lod_sub_object_ref_add(env, dto, th);
2945 dt_write_unlock(env, dto);
2949 rec->rec_fid = lu_object_fid(&dto->do_lu);
2950 rc = lod_sub_object_index_insert(env, dto,
2951 (const struct dt_rec *)rec,
2952 (const struct dt_key *)dot, th, 0);
2956 rec->rec_fid = lu_object_fid(&dt->do_lu);
2957 rc = lod_sub_object_index_insert(env, dto, (struct dt_rec *)rec,
2958 (const struct dt_key *)dotdot, th, 0);
2962 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
2963 cfs_fail_val != i) {
2964 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
2966 slave_lmm->lmv_master_mdt_index =
2969 slave_lmm->lmv_master_mdt_index =
2972 rc = lod_sub_object_xattr_set(env, dto, &slave_lmv_buf,
2973 XATTR_NAME_LMV, fl, th);
2978 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
2980 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2981 PFID(lu_object_fid(&dto->do_lu)), i + 1);
2983 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2984 PFID(lu_object_fid(&dto->do_lu)), i);
2986 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2987 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
2988 sname, lu_object_fid(&dt->do_lu));
2992 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
2993 linkea_buf.lb_len = ldata.ld_leh->leh_len;
2994 rc = lod_sub_object_xattr_set(env, dto, &linkea_buf,
2995 XATTR_NAME_LINK, 0, th);
2999 rec->rec_fid = lu_object_fid(&dto->do_lu);
3000 rc = lod_sub_object_index_insert(env, dt_object_child(dt),
3001 (const struct dt_rec *)rec,
3002 (const struct dt_key *)stripe_name, th, 0);
3006 rc = lod_sub_object_ref_add(env, dt_object_child(dt), th);
3011 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
3012 rc = lod_sub_object_xattr_set(env, dt_object_child(dt),
3013 &lmv_buf, XATTR_NAME_LMV, fl, th);
3015 if (slave_lmm != NULL)
3016 OBD_FREE_PTR(slave_lmm);
3022 * Helper function to declare/execute creation of a striped directory
3024 * Called in declare/create object path, prepare striping for a directory
3025 * and prepare defaults data striping for the objects to be created in
3026 * that directory. Notice the function calls "declaration" or "execution"
3027 * methods depending on \a declare param. This is a consequence of the
3028 * current approach while we don't have natural distributed transactions:
3029 * we basically execute non-local updates in the declare phase. So, the
3030 * arguments for the both phases are the same and this is the reason for
3031 * this function to exist.
3033 * \param[in] env execution environment
3034 * \param[in] dt object
3035 * \param[in] attr attributes the stripes will be created with
3036 * \param[in] dof format of stripes (see OSD API description)
3037 * \param[in] th transaction handle
3038 * \param[in] declare where to call "declare" or "execute" methods
3040 * \retval 0 on success
3041 * \retval negative if failed
3043 static int lod_dir_striping_create_internal(const struct lu_env *env,
3044 struct dt_object *dt,
3045 struct lu_attr *attr,
3046 struct dt_object_format *dof,
3050 struct lod_thread_info *info = lod_env_info(env);
3051 struct lod_object *lo = lod_dt_obj(dt);
3052 const struct lod_default_striping *lds = lo->ldo_def_striping;
3056 LASSERT(ergo(lds != NULL,
3057 lds->lds_def_striping_set ||
3058 lds->lds_dir_def_striping_set));
3060 if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripenr,
3061 lo->ldo_dir_stripe_offset)) {
3062 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3063 int stripe_count = lo->ldo_dir_stripenr;
3065 if (info->lti_ea_store_size < sizeof(*v1)) {
3066 rc = lod_ea_store_resize(info, sizeof(*v1));
3069 v1 = info->lti_ea_store;
3072 memset(v1, 0, sizeof(*v1));
3073 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3074 v1->lum_stripe_count = cpu_to_le32(stripe_count);
3075 v1->lum_stripe_offset =
3076 cpu_to_le32(lo->ldo_dir_stripe_offset);
3078 info->lti_buf.lb_buf = v1;
3079 info->lti_buf.lb_len = sizeof(*v1);
3082 rc = lod_declare_xattr_set_lmv(env, dt, attr,
3083 &info->lti_buf, dof, th);
3085 rc = lod_xattr_set_lmv(env, dt, &info->lti_buf,
3086 XATTR_NAME_LMV, 0, th);
3091 /* Transfer default LMV striping from the parent */
3092 if (lds != NULL && lds->lds_dir_def_striping_set &&
3093 !LMVEA_DELETE_VALUES(lds->lds_dir_def_stripenr,
3094 lds->lds_dir_def_stripe_offset)) {
3095 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3097 if (info->lti_ea_store_size < sizeof(*v1)) {
3098 rc = lod_ea_store_resize(info, sizeof(*v1));
3101 v1 = info->lti_ea_store;
3104 memset(v1, 0, sizeof(*v1));
3105 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3106 v1->lum_stripe_count = cpu_to_le32(lds->lds_dir_def_stripenr);
3107 v1->lum_stripe_offset =
3108 cpu_to_le32(lds->lds_dir_def_stripe_offset);
3110 cpu_to_le32(lds->lds_dir_def_hash_type);
3112 info->lti_buf.lb_buf = v1;
3113 info->lti_buf.lb_len = sizeof(*v1);
3115 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3116 XATTR_NAME_DEFAULT_LMV,
3119 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
3121 XATTR_NAME_DEFAULT_LMV, 0,
3127 /* Transfer default LOV striping from the parent */
3128 if (lds != NULL && lds->lds_def_striping_set &&
3129 lds->lds_def_comp_cnt != 0) {
3130 struct lov_mds_md *lmm;
3131 int lmm_size = lod_comp_md_size(lo, true);
3133 if (info->lti_ea_store_size < lmm_size) {
3134 rc = lod_ea_store_resize(info, lmm_size);
3138 lmm = info->lti_ea_store;
3140 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
3144 info->lti_buf.lb_buf = lmm;
3145 info->lti_buf.lb_len = lmm_size;
3148 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3149 XATTR_NAME_LOV, 0, th);
3151 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
3152 XATTR_NAME_LOV, 0, th);
3160 static int lod_declare_dir_striping_create(const struct lu_env *env,
3161 struct dt_object *dt,
3162 struct lu_attr *attr,
3163 struct dt_object_format *dof,
3166 return lod_dir_striping_create_internal(env, dt, attr, dof, th, true);
3169 static int lod_dir_striping_create(const struct lu_env *env,
3170 struct dt_object *dt,
3171 struct lu_attr *attr,
3172 struct dt_object_format *dof,
3175 return lod_dir_striping_create_internal(env, dt, attr, dof, th, false);
3179 * Make LOV EA for striped object.
3181 * Generate striping information and store it in the LOV EA of the given
3182 * object. The caller must ensure nobody else is calling the function
3183 * against the object concurrently. The transaction must be started.
3184 * FLDB service must be running as well; it's used to map FID to the target,
3185 * which is stored in LOV EA.
3187 * \param[in] env execution environment for this thread
3188 * \param[in] lo LOD object
3189 * \param[in] th transaction handle
3191 * \retval 0 if LOV EA is stored successfully
3192 * \retval negative error number on failure
3194 static int lod_generate_and_set_lovea(const struct lu_env *env,
3195 struct lod_object *lo,
3198 struct lod_thread_info *info = lod_env_info(env);
3199 struct dt_object *next = dt_object_child(&lo->ldo_obj);
3200 struct lov_mds_md_v1 *lmm;
3206 if (lo->ldo_comp_cnt == 0) {
3207 lod_object_free_striping(env, lo);
3208 rc = lod_sub_object_xattr_del(env, next, XATTR_NAME_LOV, th);
3212 lmm_size = lod_comp_md_size(lo, false);
3213 if (info->lti_ea_store_size < lmm_size) {
3214 rc = lod_ea_store_resize(info, lmm_size);
3218 lmm = info->lti_ea_store;
3220 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
3224 info->lti_buf.lb_buf = lmm;
3225 info->lti_buf.lb_len = lmm_size;
3226 rc = lod_sub_object_xattr_set(env, next, &info->lti_buf,
3227 XATTR_NAME_LOV, 0, th);
3232 * Delete layout component(s)
3234 * \param[in] env execution environment for this thread
3235 * \param[in] dt object
3236 * \param[in] th transaction handle
3238 * \retval 0 on success
3239 * \retval negative error number on failure
3241 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
3244 struct lod_layout_component *lod_comp;
3245 struct lod_object *lo = lod_dt_obj(dt);
3246 struct dt_object *next = dt_object_child(dt);
3247 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3250 LASSERT(lo->ldo_is_composite);
3251 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
3253 left = lo->ldo_comp_cnt;
3254 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3255 lod_comp = &lo->ldo_comp_entries[i];
3257 if (lod_comp->llc_id != LCME_ID_INVAL)
3261 /* Not instantiated component */
3262 if (lod_comp->llc_stripe == NULL)
3265 LASSERT(lod_comp->llc_stripenr > 0);
3266 for (j = 0; j < lod_comp->llc_stripenr; j++) {
3267 struct dt_object *obj = lod_comp->llc_stripe[j];
3271 rc = lod_sub_object_destroy(env, obj, th);
3275 lu_object_put(env, &obj->do_lu);
3276 lod_comp->llc_stripe[j] = NULL;
3278 OBD_FREE(lod_comp->llc_stripe, sizeof(struct dt_object *) *
3279 lod_comp->llc_stripes_allocated);
3280 lod_comp->llc_stripe = NULL;
3281 lod_comp->llc_stripes_allocated = 0;
3282 lod_obj_set_pool(lo, i, NULL);
3285 LASSERTF(left >= 0 && left < lo->ldo_comp_cnt, "left = %d\n", left);
3287 struct lod_layout_component *comp_array;
3289 OBD_ALLOC(comp_array, sizeof(*comp_array) * left);
3290 if (comp_array == NULL)
3291 GOTO(out, rc = -ENOMEM);
3293 memcpy(&comp_array[0], &lo->ldo_comp_entries[0],
3294 sizeof(*comp_array) * left);
3296 OBD_FREE(lo->ldo_comp_entries,
3297 sizeof(*comp_array) * lo->ldo_comp_cnt);
3298 lo->ldo_comp_entries = comp_array;
3299 lo->ldo_comp_cnt = left;
3300 lod_obj_inc_layout_gen(lo);
3302 lod_free_comp_entries(lo);
3305 LASSERT(dt_object_exists(dt));
3306 rc = dt_attr_get(env, next, attr);
3310 if (attr->la_size > 0) {
3312 attr->la_valid = LA_SIZE;
3313 rc = lod_sub_object_attr_set(env, next, attr, th);
3318 rc = lod_generate_and_set_lovea(env, lo, th);
3322 lod_object_free_striping(env, lo);
3327 * Implementation of dt_object_operations::do_xattr_set.
3329 * Sets specified extended attribute on the object. Three types of EAs are
3331 * LOV EA - stores striping for a regular file or default striping (when set
3333 * LMV EA - stores a marker for the striped directories
3334 * DMV EA - stores default directory striping
3336 * When striping is applied to a non-striped existing object (this is called
3337 * late striping), then LOD notices the caller wants to turn the object into a
3338 * striped one. The stripe objects are created and appropriate EA is set:
3339 * LOV EA storing all the stripes directly or LMV EA storing just a small header
3340 * with striping configuration.
3342 * \see dt_object_operations::do_xattr_set() in the API description for details.
3344 static int lod_xattr_set(const struct lu_env *env,
3345 struct dt_object *dt, const struct lu_buf *buf,
3346 const char *name, int fl, struct thandle *th)
3348 struct dt_object *next = dt_object_child(dt);
3352 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3353 strcmp(name, XATTR_NAME_LMV) == 0) {
3354 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
3356 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
3357 LMV_HASH_FLAG_MIGRATION)
3358 rc = lod_sub_object_xattr_set(env, next, buf, name, fl,
3361 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
3366 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3367 strcmp(name, XATTR_NAME_LOV) == 0) {
3369 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
3371 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3372 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
3374 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
3377 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3378 (!strcmp(name, XATTR_NAME_LOV) ||
3379 !strncmp(name, XATTR_LUSTRE_LOV,
3380 strlen(XATTR_LUSTRE_LOV)))) {
3381 /* in case of lov EA swap, just set it
3382 * if not, it is a replay so check striping match what we
3383 * already have during req replay, declare_xattr_set()
3384 * defines striping, then create() does the work */
3385 if (fl & LU_XATTR_REPLACE) {
3386 /* free stripes, then update disk */
3387 lod_object_free_striping(env, lod_dt_obj(dt));
3389 rc = lod_sub_object_xattr_set(env, next, buf, name,
3391 } else if (dt_object_remote(dt)) {
3392 /* This only happens during migration, see
3393 * mdd_migrate_create(), in which Master MDT will
3394 * create a remote target object, and only set
3395 * (migrating) stripe EA on the remote object,
3396 * and does not need creating each stripes. */
3397 rc = lod_sub_object_xattr_set(env, next, buf, name,
3399 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
3400 /* delete component(s) */
3401 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
3402 rc = lod_layout_del(env, dt, th);
3405 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
3406 * it's going to create create file with specified
3407 * component(s), the striping must have not being
3408 * cached in this case;
3410 * Otherwise, it's going to add/change component(s) to
3411 * an existing file, the striping must have been cached
3414 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
3415 !strcmp(name, XATTR_NAME_LOV),
3416 !lod_dt_obj(dt)->ldo_comp_cached));
3418 rc = lod_striping_create(env, dt, NULL, NULL, th);
3421 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3422 rc = lod_object_replace_parent_fid(env, dt, th, false);
3427 /* then all other xattr */
3428 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3434 * Implementation of dt_object_operations::do_declare_xattr_del.
3436 * \see dt_object_operations::do_declare_xattr_del() in the API description
3439 static int lod_declare_xattr_del(const struct lu_env *env,
3440 struct dt_object *dt, const char *name,
3443 struct lod_object *lo = lod_dt_obj(dt);
3448 rc = lod_sub_object_declare_xattr_del(env, dt_object_child(dt),
3453 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3456 /* set xattr to each stripes, if needed */
3457 rc = lod_load_striping(env, lo);
3461 if (lo->ldo_dir_stripenr == 0)
3464 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
3465 LASSERT(lo->ldo_stripe[i]);
3466 rc = lod_sub_object_declare_xattr_del(env, lo->ldo_stripe[i],
3476 * Implementation of dt_object_operations::do_xattr_del.
3478 * If EA storing a regular striping is being deleted, then release
3479 * all the references to the stripe objects in core.
3481 * \see dt_object_operations::do_xattr_del() in the API description for details.
3483 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
3484 const char *name, struct thandle *th)
3486 struct dt_object *next = dt_object_child(dt);
3487 struct lod_object *lo = lod_dt_obj(dt);
3492 if (!strcmp(name, XATTR_NAME_LOV))
3493 lod_object_free_striping(env, lod_dt_obj(dt));
3495 rc = lod_sub_object_xattr_del(env, next, name, th);
3496 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3499 if (lo->ldo_dir_stripenr == 0)
3502 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
3503 LASSERT(lo->ldo_stripe[i]);
3505 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name, th);
3514 * Implementation of dt_object_operations::do_xattr_list.
3516 * \see dt_object_operations::do_xattr_list() in the API description
3519 static int lod_xattr_list(const struct lu_env *env,
3520 struct dt_object *dt, const struct lu_buf *buf)
3522 return dt_xattr_list(env, dt_object_child(dt), buf);
3525 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
3527 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
3532 * Get default striping.
3534 * \param[in] env execution environment
3535 * \param[in] lo object
3536 * \param[out] lds default striping
3538 * \retval 0 on success
3539 * \retval negative if failed
3541 static int lod_get_default_lov_striping(const struct lu_env *env,
3542 struct lod_object *lo,
3543 struct lod_default_striping *lds)
3545 struct lod_thread_info *info = lod_env_info(env);
3546 struct lov_user_md_v1 *v1 = NULL;
3547 struct lov_user_md_v3 *v3 = NULL;
3548 struct lov_comp_md_v1 *comp_v1 = NULL;
3554 lds->lds_def_striping_set = 0;
3556 rc = lod_get_lov_ea(env, lo);
3560 if (rc < (typeof(rc))sizeof(struct lov_user_md))
3563 v1 = info->lti_ea_store;
3564 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
3565 lustre_swab_lov_user_md_v1(v1);
3566 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
3567 v3 = (struct lov_user_md_v3 *)v1;
3568 lustre_swab_lov_user_md_v3(v3);
3569 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3570 comp_v1 = (struct lov_comp_md_v1 *)v1;
3571 lustre_swab_lov_comp_md_v1(comp_v1);
3574 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
3575 v1->lmm_magic != LOV_MAGIC_COMP_V1)
3578 if (v1->lmm_magic == LOV_MAGIC_COMP_V1) {
3579 comp_v1 = (struct lov_comp_md_v1 *)v1;
3580 comp_cnt = comp_v1->lcm_entry_count;
3589 /* realloc default comp entries if necessary */
3590 rc = lod_def_striping_comp_resize(lds, comp_cnt);
3594 lds->lds_def_comp_cnt = comp_cnt;
3595 lds->lds_def_striping_is_composite = composite ? 1 : 0;
3597 for (i = 0; i < comp_cnt; i++) {
3598 struct lod_layout_component *lod_comp;
3599 struct lu_extent *ext;
3602 lod_comp = &lds->lds_def_comp_entries[i];
3604 * reset lod_comp values, llc_stripes is always NULL in
3605 * the default striping template, llc_pool will be reset
3608 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
3611 v1 = (struct lov_user_md *)((char *)comp_v1 +
3612 comp_v1->lcm_entries[i].lcme_offset);
3613 ext = &comp_v1->lcm_entries[i].lcme_extent;
3614 lod_comp->llc_extent = *ext;
3617 if (v1->lmm_pattern != LOV_PATTERN_RAID0 &&
3618 v1->lmm_pattern != 0) {
3619 lod_free_def_comp_entries(lds);
3623 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d "
3624 "stripe_offset=%d\n",
3625 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
3626 (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
3627 (int)v1->lmm_stripe_offset);
3629 lod_comp->llc_stripenr = v1->lmm_stripe_count;
3630 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
3631 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
3634 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
3635 /* XXX: sanity check here */
3636 v3 = (struct lov_user_md_v3 *) v1;
3637 if (v3->lmm_pool_name[0] != '\0')
3638 pool = v3->lmm_pool_name;
3640 lod_set_def_pool(lds, i, pool);
3643 lds->lds_def_striping_set = 1;
3648 * Get default directory striping.
3650 * \param[in] env execution environment
3651 * \param[in] lo object
3652 * \param[out] lds default striping
3654 * \retval 0 on success
3655 * \retval negative if failed
3657 static int lod_get_default_lmv_striping(const struct lu_env *env,
3658 struct lod_object *lo,
3659 struct lod_default_striping *lds)
3661 struct lod_thread_info *info = lod_env_info(env);
3662 struct lmv_user_md_v1 *v1 = NULL;
3666 lds->lds_dir_def_striping_set = 0;
3667 rc = lod_get_default_lmv_ea(env, lo);
3671 if (rc < (typeof(rc))sizeof(struct lmv_user_md))
3674 v1 = info->lti_ea_store;
3676 lds->lds_dir_def_stripenr = le32_to_cpu(v1->lum_stripe_count);
3677 lds->lds_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
3678 lds->lds_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
3679 lds->lds_dir_def_striping_set = 1;
3685 * Get default striping in the object.
3687 * Get object default striping and default directory striping.
3689 * \param[in] env execution environment
3690 * \param[in] lo object
3691 * \param[out] lds default striping
3693 * \retval 0 on success
3694 * \retval negative if failed
3696 static int lod_get_default_striping(const struct lu_env *env,
3697 struct lod_object *lo,
3698 struct lod_default_striping *lds)
3702 rc = lod_get_default_lov_striping(env, lo, lds);
3703 rc1 = lod_get_default_lmv_striping(env, lo, lds);
3704 if (rc == 0 && rc1 < 0)
3711 * Apply default striping on object.
3713 * If object striping pattern is not set, set to the one in default striping.
3714 * The default striping is from parent or fs.
3716 * \param[in] lo new object
3717 * \param[in] lds default striping
3718 * \param[in] mode new object's mode
3720 static void lod_striping_from_default(struct lod_object *lo,
3721 const struct lod_default_striping *lds,
3724 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
3725 struct lov_desc *desc = &d->lod_desc;
3728 if (lds->lds_def_striping_set && S_ISREG(mode)) {
3729 rc = lod_alloc_comp_entries(lo, lds->lds_def_comp_cnt);
3733 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
3735 for (i = 0; i < lo->ldo_comp_cnt; i++) {
3736 struct lod_layout_component *obj_comp =
3737 &lo->ldo_comp_entries[i];
3738 struct lod_layout_component *def_comp =
3739 &lds->lds_def_comp_entries[i];
3741 CDEBUG(D_LAYOUT, "Inherite from default: size:%hu "
3742 "nr:%u offset:%u %s\n",
3743 def_comp->llc_stripe_size,
3744 def_comp->llc_stripenr,
3745 def_comp->llc_stripe_offset,
3746 def_comp->llc_pool ?: "");
3748 *obj_comp = *def_comp;
3749 if (def_comp->llc_pool != NULL) {
3750 /* pointer was copied from def_comp */
3751 obj_comp->llc_pool = NULL;
3752 lod_obj_set_pool(lo, i, def_comp->llc_pool);
3756 * Don't initialize these fields for plain layout
3757 * (v1/v3) here, they are inherited in the order of
3758 * 'parent' -> 'fs default (root)' -> 'global default
3759 * values for stripe_count & stripe_size'.
3761 * see lod_ah_init().
3763 if (!lo->ldo_is_composite)
3766 if (obj_comp->llc_stripenr <= 0)
3767 obj_comp->llc_stripenr =
3768 desc->ld_default_stripe_count;
3769 if (obj_comp->llc_stripe_size <= 0)
3770 obj_comp->llc_stripe_size =
3771 desc->ld_default_stripe_size;
3773 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
3774 if (lo->ldo_dir_stripenr == 0)
3775 lo->ldo_dir_stripenr = lds->lds_dir_def_stripenr;
3776 if (lo->ldo_dir_stripe_offset == -1)
3777 lo->ldo_dir_stripe_offset =
3778 lds->lds_dir_def_stripe_offset;
3779 if (lo->ldo_dir_hash_type == 0)
3780 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
3782 CDEBUG(D_LAYOUT, "striping from default dir: nr:%hu, "
3783 "offset:%u, hash_type:%u\n",
3784 lo->ldo_dir_stripenr, lo->ldo_dir_stripe_offset,
3785 lo->ldo_dir_hash_type);
3789 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root)
3791 struct lod_layout_component *lod_comp;
3793 if (lo->ldo_comp_cnt == 0)
3796 if (lo->ldo_is_composite)
3799 lod_comp = &lo->ldo_comp_entries[0];
3801 if (lod_comp->llc_stripenr <= 0 ||
3802 lod_comp->llc_stripe_size <= 0)
3805 if (from_root && (lod_comp->llc_pool == NULL ||
3806 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
3813 * Implementation of dt_object_operations::do_ah_init.
3815 * This method is used to make a decision on the striping configuration for the
3816 * object being created. It can be taken from the \a parent object if it exists,
3817 * or filesystem's default. The resulting configuration (number of stripes,
3818 * stripe size/offset, pool name, etc) is stored in the object itself and will
3819 * be used by the methods like ->doo_declare_create().
3821 * \see dt_object_operations::do_ah_init() in the API description for details.
3823 static void lod_ah_init(const struct lu_env *env,
3824 struct dt_allocation_hint *ah,
3825 struct dt_object *parent,
3826 struct dt_object *child,
3829 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
3830 struct lod_thread_info *info = lod_env_info(env);
3831 struct lod_default_striping *lds = &info->lti_def_striping;
3832 struct dt_object *nextp = NULL;
3833 struct dt_object *nextc;
3834 struct lod_object *lp = NULL;
3835 struct lod_object *lc;
3836 struct lov_desc *desc;
3837 struct lod_layout_component *lod_comp;
3843 if (likely(parent)) {
3844 nextp = dt_object_child(parent);
3845 lp = lod_dt_obj(parent);
3848 nextc = dt_object_child(child);
3849 lc = lod_dt_obj(child);
3851 LASSERT(!lod_obj_is_striped(child));
3852 /* default layout template may have been set on the regular file
3853 * when this is called from mdd_create_data() */
3854 if (S_ISREG(child_mode))
3855 lod_free_comp_entries(lc);
3857 if (!dt_object_exists(nextc))
3858 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
3860 if (S_ISDIR(child_mode)) {
3861 /* other default values are 0 */
3862 lc->ldo_dir_stripe_offset = -1;
3864 /* get default striping from parent object */
3865 if (likely(lp != NULL))
3866 lod_get_default_striping(env, lp, lds);
3868 /* set child default striping info, default value is NULL */
3869 if (lds->lds_def_striping_set || lds->lds_dir_def_striping_set)
3870 lc->ldo_def_striping = lds;
3872 /* It should always honour the specified stripes */
3873 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
3874 lod_verify_md_striping(d, ah->dah_eadata) == 0) {
3875 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
3877 lc->ldo_dir_stripenr =
3878 le32_to_cpu(lum1->lum_stripe_count);
3879 lc->ldo_dir_stripe_offset =
3880 le32_to_cpu(lum1->lum_stripe_offset);
3881 lc->ldo_dir_hash_type =
3882 le32_to_cpu(lum1->lum_hash_type);
3883 CDEBUG(D_INFO, "set dir stripe: count %hu, offset %d, "
3885 lc->ldo_dir_stripenr,
3886 (int)lc->ldo_dir_stripe_offset,
3887 lc->ldo_dir_hash_type);
3889 /* transfer defaults LMV to new directory */
3890 lod_striping_from_default(lc, lds, child_mode);
3893 /* shrink the stripe_count to the avaible MDT count */
3894 if (lc->ldo_dir_stripenr > d->lod_remote_mdt_count + 1 &&
3895 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))
3896 lc->ldo_dir_stripenr = d->lod_remote_mdt_count + 1;
3898 /* Directory will be striped only if stripe_count > 1, if
3899 * stripe_count == 1, let's reset stripenr = 0 to avoid
3900 * create single master stripe and also help to unify the
3901 * stripe handling of directories and files */
3902 if (lc->ldo_dir_stripenr == 1)
3903 lc->ldo_dir_stripenr = 0;
3905 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
3906 lc->ldo_dir_stripenr, (int)lc->ldo_dir_stripe_offset,
3907 lc->ldo_dir_hash_type);
3912 /* child object regular file*/
3914 if (!lod_object_will_be_striped(S_ISREG(child_mode),
3915 lu_object_fid(&child->do_lu)))
3918 /* If object is going to be striped over OSTs, transfer default
3919 * striping information to the child, so that we can use it
3920 * during declaration and creation.
3922 * Try from the parent first.
3924 if (likely(lp != NULL)) {
3925 rc = lod_get_default_lov_striping(env, lp, lds);
3927 lod_striping_from_default(lc, lds, child_mode);
3930 /* Initialize lod_device::lod_md_root object reference */
3931 if (d->lod_md_root == NULL) {
3932 struct dt_object *root;
3933 struct lod_object *lroot;
3935 lu_root_fid(&info->lti_fid);
3936 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
3937 if (!IS_ERR(root)) {
3938 lroot = lod_dt_obj(root);
3940 spin_lock(&d->lod_lock);
3941 if (d->lod_md_root != NULL)
3942 dt_object_put(env, &d->lod_md_root->ldo_obj);
3943 d->lod_md_root = lroot;
3944 spin_unlock(&d->lod_lock);
3948 /* try inherit layout from the root object (fs default) when:
3949 * - parent does not have default layout; or
3950 * - parent has plain(v1/v3) default layout, and some attributes
3951 * are not specified in the default layout;
3953 if (d->lod_md_root != NULL && lod_need_inherit_more(lc, true)) {
3954 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds);
3957 if (lc->ldo_comp_cnt == 0) {
3958 lod_striping_from_default(lc, lds, child_mode);
3959 } else if (!lds->lds_def_striping_is_composite) {
3960 struct lod_layout_component *def_comp;
3962 LASSERT(!lc->ldo_is_composite);
3963 lod_comp = &lc->ldo_comp_entries[0];
3964 def_comp = &lds->lds_def_comp_entries[0];
3966 if (lod_comp->llc_stripenr <= 0)
3967 lod_comp->llc_stripenr = def_comp->llc_stripenr;
3968 if (lod_comp->llc_stripe_size <= 0)
3969 lod_comp->llc_stripe_size =
3970 def_comp->llc_stripe_size;
3971 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
3972 lod_comp->llc_stripe_offset =
3973 def_comp->llc_stripe_offset;
3974 if (lod_comp->llc_pool == NULL)
3975 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
3980 * fs default striping may not be explicitly set, or historically set
3981 * in config log, use them.
3983 if (lod_need_inherit_more(lc, false)) {
3985 if (lc->ldo_comp_cnt == 0) {
3986 rc = lod_alloc_comp_entries(lc, 1);
3988 /* fail to allocate memory, will create a
3989 * non-striped file. */
3991 lc->ldo_is_composite = 0;
3992 lod_comp = &lc->ldo_comp_entries[0];
3993 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
3995 LASSERT(!lc->ldo_is_composite);
3996 lod_comp = &lc->ldo_comp_entries[0];
3997 desc = &d->lod_desc;
3998 if (lod_comp->llc_stripenr <= 0)
3999 lod_comp->llc_stripenr = desc->ld_default_stripe_count;
4000 if (lod_comp->llc_stripe_size <= 0)
4001 lod_comp->llc_stripe_size =
4002 desc->ld_default_stripe_size;
4008 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
4010 * Size initialization on late striping.
4012 * Propagate the size of a truncated object to a deferred striping.
4013 * This function handles a special case when truncate was done on a
4014 * non-striped object and now while the striping is being created
4015 * we can't lose that size, so we have to propagate it to the stripes
4018 * \param[in] env execution environment
4019 * \param[in] dt object
4020 * \param[in] th transaction handle
4022 * \retval 0 on success
4023 * \retval negative if failed
4025 static int lod_declare_init_size(const struct lu_env *env,
4026 struct dt_object *dt, struct thandle *th)
4028 struct dt_object *next = dt_object_child(dt);
4029 struct lod_object *lo = lod_dt_obj(dt);
4030 struct dt_object **objects = NULL;
4031 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4032 uint64_t size, offs;
4033 int i, rc, stripe, stripenr = 0, stripe_size = 0;
4036 if (!lod_obj_is_striped(dt))
4039 rc = dt_attr_get(env, next, attr);
4040 LASSERT(attr->la_valid & LA_SIZE);
4044 size = attr->la_size;
4048 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4049 struct lod_layout_component *lod_comp;
4050 struct lu_extent *extent;
4052 lod_comp = &lo->ldo_comp_entries[i];
4054 if (lod_comp->llc_stripe == NULL)
4057 extent = &lod_comp->llc_extent;
4058 if (!lo->ldo_is_composite ||
4059 (size >= extent->e_start && size < extent->e_end)) {
4060 objects = lod_comp->llc_stripe;
4061 stripenr = lod_comp->llc_stripenr;
4062 stripe_size = lod_comp->llc_stripe_size;
4070 LASSERT(objects != NULL && stripe_size != 0);
4072 /* ll_do_div64(a, b) returns a % b, and a = a / b */
4073 ll_do_div64(size, (__u64)stripe_size);
4074 stripe = ll_do_div64(size, (__u64)stripenr);
4075 LASSERT(objects[stripe] != NULL);
4077 size = size * stripe_size;
4078 offs = attr->la_size;
4079 size += ll_do_div64(offs, stripe_size);
4081 attr->la_valid = LA_SIZE;
4082 attr->la_size = size;
4084 rc = lod_sub_object_declare_attr_set(env, objects[stripe], attr, th);
4090 * Declare creation of striped object.
4092 * The function declares creation stripes for a regular object. The function
4093 * also declares whether the stripes will be created with non-zero size if
4094 * previously size was set non-zero on the master object. If object \a dt is
4095 * not local, then only fully defined striping can be applied in \a lovea.
4096 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
4099 * \param[in] env execution environment
4100 * \param[in] dt object
4101 * \param[in] attr attributes the stripes will be created with
4102 * \param[in] lovea a buffer containing striping description
4103 * \param[in] th transaction handle
4105 * \retval 0 on success
4106 * \retval negative if failed
4108 int lod_declare_striped_object(const struct lu_env *env, struct dt_object *dt,
4109 struct lu_attr *attr,
4110 const struct lu_buf *lovea, struct thandle *th)
4112 struct lod_thread_info *info = lod_env_info(env);
4113 struct dt_object *next = dt_object_child(dt);
4114 struct lod_object *lo = lod_dt_obj(dt);
4118 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
4119 GOTO(out, rc = -ENOMEM);
4121 if (!dt_object_remote(next)) {
4122 /* choose OST and generate appropriate objects */
4123 rc = lod_prepare_create(env, lo, attr, lovea, th);
4128 * declare storage for striping data
4130 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
4132 /* LOD can not choose OST objects for remote objects, i.e.
4133 * stripes must be ready before that. Right now, it can only
4134 * happen during migrate, i.e. migrate process needs to create
4135 * remote regular file (mdd_migrate_create), then the migrate
4136 * process will provide stripeEA. */
4137 LASSERT(lovea != NULL);
4138 info->lti_buf = *lovea;
4141 rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
4142 XATTR_NAME_LOV, 0, th);
4147 * if striping is created with local object's size > 0,
4148 * we have to propagate this size to specific object
4149 * the case is possible only when local object was created previously
4151 if (dt_object_exists(next))
4152 rc = lod_declare_init_size(env, dt, th);
4155 /* failed to create striping or to set initial size, let's reset
4156 * config so that others don't get confused */
4158 lod_object_free_striping(env, lo);
4164 * Implementation of dt_object_operations::do_declare_create.
4166 * The method declares creation of a new object. If the object will be striped,
4167 * then helper functions are called to find FIDs for the stripes, declare
4168 * creation of the stripes and declare initialization of the striping
4169 * information to be stored in the master object.
4171 * \see dt_object_operations::do_declare_create() in the API description
4174 static int lod_declare_object_create(const struct lu_env *env,
4175 struct dt_object *dt,
4176 struct lu_attr *attr,
4177 struct dt_allocation_hint *hint,
4178 struct dt_object_format *dof,
4181 struct dt_object *next = dt_object_child(dt);
4182 struct lod_object *lo = lod_dt_obj(dt);
4191 * first of all, we declare creation of local object
4193 rc = lod_sub_object_declare_create(env, next, attr, hint, dof, th);
4197 if (dof->dof_type == DFT_SYM)
4198 dt->do_body_ops = &lod_body_lnk_ops;
4199 else if (dof->dof_type == DFT_REGULAR)
4200 dt->do_body_ops = &lod_body_ops;
4203 * it's lod_ah_init() that has decided the object will be striped
4205 if (dof->dof_type == DFT_REGULAR) {
4206 /* callers don't want stripes */
4207 /* XXX: all tricky interactions with ->ah_make_hint() decided
4208 * to use striping, then ->declare_create() behaving differently
4209 * should be cleaned */
4210 if (dof->u.dof_reg.striped != 0)
4211 rc = lod_declare_striped_object(env, dt, attr,
4213 } else if (dof->dof_type == DFT_DIR) {
4214 struct seq_server_site *ss;
4216 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
4218 /* If the parent has default stripeEA, and client
4219 * did not find it before sending create request,
4220 * then MDT will return -EREMOTE, and client will
4221 * retrieve the default stripeEA and re-create the
4224 * Note: if dah_eadata != NULL, it means creating the
4225 * striped directory with specified stripeEA, then it
4226 * should ignore the default stripeEA */
4227 if (hint != NULL && hint->dah_eadata == NULL) {
4228 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
4229 GOTO(out, rc = -EREMOTE);
4231 if (lo->ldo_dir_stripe_offset == -1) {
4232 /* child and parent should be in the same MDT */
4233 if (hint->dah_parent != NULL &&
4234 dt_object_remote(hint->dah_parent))
4235 GOTO(out, rc = -EREMOTE);
4236 } else if (lo->ldo_dir_stripe_offset !=
4238 struct lod_device *lod;
4239 struct lod_tgt_descs *ltd;
4240 struct lod_tgt_desc *tgt = NULL;
4241 bool found_mdt = false;
4244 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4245 ltd = &lod->lod_mdt_descs;
4246 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
4247 tgt = LTD_TGT(ltd, i);
4248 if (tgt->ltd_index ==
4249 lo->ldo_dir_stripe_offset) {
4255 /* If the MDT indicated by stripe_offset can be
4256 * found, then tell client to resend the create
4257 * request to the correct MDT, otherwise return
4258 * error to client */
4260 GOTO(out, rc = -EREMOTE);
4262 GOTO(out, rc = -EINVAL);
4266 rc = lod_declare_dir_striping_create(env, dt, attr, dof, th);
4269 /* failed to create striping or to set initial size, let's reset
4270 * config so that others don't get confused */
4272 lod_object_free_striping(env, lo);
4277 * Creation of a striped regular object.
4279 * The function is called to create the stripe objects for a regular
4280 * striped file. This can happen at the initial object creation or
4281 * when the caller asks LOD to do so using ->do_xattr_set() method
4282 * (so called late striping). Notice all the information are already
4283 * prepared in the form of the list of objects (ldo_stripe field).
4284 * This is done during declare phase.
4286 * \param[in] env execution environment
4287 * \param[in] dt object
4288 * \param[in] attr attributes the stripes will be created with
4289 * \param[in] dof format of stripes (see OSD API description)
4290 * \param[in] th transaction handle
4292 * \retval 0 on success
4293 * \retval negative if failed
4295 int lod_striping_create(const struct lu_env *env, struct dt_object *dt,
4296 struct lu_attr *attr, struct dt_object_format *dof,
4299 struct lod_layout_component *lod_comp;
4300 struct lod_object *lo = lod_dt_obj(dt);
4304 LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
4306 /* create all underlying objects */
4307 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4308 lod_comp = &lo->ldo_comp_entries[i];
4310 if (lod_comp->llc_flags & LCME_FL_INIT)
4313 lod_comp->llc_flags |= LCME_FL_INIT;
4315 if (lod_comp->llc_stripe == NULL)
4318 LASSERT(lod_comp->llc_stripenr > 0);
4319 for (j = 0; j < lod_comp->llc_stripenr; j++) {
4320 struct dt_object *object = lod_comp->llc_stripe[j];
4321 LASSERT(object != NULL);
4322 rc = lod_sub_object_create(env, object, attr, NULL,
4330 rc = lod_generate_and_set_lovea(env, lo, th);
4333 lo->ldo_comp_cached = 1;
4335 lod_object_free_striping(env, lo);
4341 * Implementation of dt_object_operations::do_create.
4343 * If any of preceeding methods (like ->do_declare_create(),
4344 * ->do_ah_init(), etc) chose to create a striped object,
4345 * then this method will create the master and the stripes.
4347 * \see dt_object_operations::do_create() in the API description for details.
4349 static int lod_object_create(const struct lu_env *env, struct dt_object *dt,
4350 struct lu_attr *attr,
4351 struct dt_allocation_hint *hint,
4352 struct dt_object_format *dof, struct thandle *th)
4357 /* create local object */
4358 rc = lod_sub_object_create(env, dt_object_child(dt), attr, hint, dof,
4363 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4364 lod_obj_is_striped(dt) && dof->u.dof_reg.striped != 0) {
4365 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
4366 rc = lod_striping_create(env, dt, attr, dof, th);
4373 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
4374 struct dt_object *dt, struct thandle *th,
4375 int stripe_idx, struct lod_obj_stripe_cb_data *data)
4377 if (data->locd_declare)
4378 return lod_sub_object_declare_destroy(env, dt, th);
4379 else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4380 stripe_idx == cfs_fail_val)
4381 return lod_sub_object_destroy(env, dt, th);
4387 * Implementation of dt_object_operations::do_declare_destroy.
4389 * If the object is a striped directory, then the function declares reference
4390 * removal from the master object (this is an index) to the stripes and declares
4391 * destroy of all the stripes. In all the cases, it declares an intention to
4392 * destroy the object itself.
4394 * \see dt_object_operations::do_declare_destroy() in the API description
4397 static int lod_declare_object_destroy(const struct lu_env *env,
4398 struct dt_object *dt,
4401 struct dt_object *next = dt_object_child(dt);
4402 struct lod_object *lo = lod_dt_obj(dt);
4403 struct lod_thread_info *info = lod_env_info(env);
4404 char *stripe_name = info->lti_key;
4409 * load striping information, notice we don't do this when object
4410 * is being initialized as we don't need this information till
4411 * few specific cases like destroy, chown
4413 rc = lod_load_striping(env, lo);
4417 /* declare destroy for all underlying objects */
4418 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4419 rc = next->do_ops->do_index_try(env, next,
4420 &dt_directory_features);
4424 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4425 rc = lod_sub_object_declare_ref_del(env, next, th);
4429 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4430 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4432 rc = lod_sub_object_declare_delete(env, next,
4433 (const struct dt_key *)stripe_name, th);
4440 * we declare destroy for the local object
4442 rc = lod_sub_object_declare_destroy(env, next, th);
4446 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4447 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4450 if (!lod_obj_is_striped(dt))
4453 /* declare destroy all striped objects */
4454 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4455 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4456 if (lo->ldo_stripe[i] == NULL)
4459 rc = lod_sub_object_declare_ref_del(env,
4460 lo->ldo_stripe[i], th);
4462 rc = lod_sub_object_declare_destroy(env,
4463 lo->ldo_stripe[i], th);
4468 struct lod_obj_stripe_cb_data data;
4470 data.locd_declare = true;
4471 rc = lod_obj_for_each_stripe(env, lo, th,
4472 lod_obj_stripe_destroy_cb, &data);
4479 * Implementation of dt_object_operations::do_destroy.
4481 * If the object is a striped directory, then the function removes references
4482 * from the master object (this is an index) to the stripes and destroys all
4483 * the stripes. In all the cases, the function destroys the object itself.
4485 * \see dt_object_operations::do_destroy() in the API description for details.
4487 static int lod_object_destroy(const struct lu_env *env,
4488 struct dt_object *dt, struct thandle *th)
4490 struct dt_object *next = dt_object_child(dt);
4491 struct lod_object *lo = lod_dt_obj(dt);
4492 struct lod_thread_info *info = lod_env_info(env);
4493 char *stripe_name = info->lti_key;
4498 /* destroy sub-stripe of master object */
4499 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4500 rc = next->do_ops->do_index_try(env, next,
4501 &dt_directory_features);
4505 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4506 rc = lod_sub_object_ref_del(env, next, th);
4510 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4511 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4514 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
4515 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
4516 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
4518 rc = lod_sub_object_delete(env, next,
4519 (const struct dt_key *)stripe_name, th);
4525 rc = lod_sub_object_destroy(env, next, th);
4529 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4530 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4533 if (!lod_obj_is_striped(dt))
4536 /* destroy all striped objects */
4537 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4538 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4539 if (lo->ldo_stripe[i] == NULL)
4541 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4542 i == cfs_fail_val) {
4543 dt_write_lock(env, lo->ldo_stripe[i],
4545 rc = lod_sub_object_ref_del(env,
4546 lo->ldo_stripe[i], th);
4547 dt_write_unlock(env, lo->ldo_stripe[i]);
4551 rc = lod_sub_object_destroy(env,
4552 lo->ldo_stripe[i], th);
4558 struct lod_obj_stripe_cb_data data;
4560 data.locd_declare = false;
4561 rc = lod_obj_for_each_stripe(env, lo, th,
4562 lod_obj_stripe_destroy_cb, &data);
4569 * Implementation of dt_object_operations::do_declare_ref_add.
4571 * \see dt_object_operations::do_declare_ref_add() in the API description
4574 static int lod_declare_ref_add(const struct lu_env *env,
4575 struct dt_object *dt, struct thandle *th)
4577 return lod_sub_object_declare_ref_add(env, dt_object_child(dt), th);
4581 * Implementation of dt_object_operations::do_ref_add.
4583 * \see dt_object_operations::do_ref_add() in the API description for details.
4585 static int lod_ref_add(const struct lu_env *env,
4586 struct dt_object *dt, struct thandle *th)
4588 return lod_sub_object_ref_add(env, dt_object_child(dt), th);
4592 * Implementation of dt_object_operations::do_declare_ref_del.
4594 * \see dt_object_operations::do_declare_ref_del() in the API description
4597 static int lod_declare_ref_del(const struct lu_env *env,
4598 struct dt_object *dt, struct thandle *th)
4600 return lod_sub_object_declare_ref_del(env, dt_object_child(dt), th);
4604 * Implementation of dt_object_operations::do_ref_del
4606 * \see dt_object_operations::do_ref_del() in the API description for details.
4608 static int lod_ref_del(const struct lu_env *env,
4609 struct dt_object *dt, struct thandle *th)
4611 return lod_sub_object_ref_del(env, dt_object_child(dt), th);
4615 * Implementation of dt_object_operations::do_object_sync.
4617 * \see dt_object_operations::do_object_sync() in the API description
4620 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
4621 __u64 start, __u64 end)
4623 return dt_object_sync(env, dt_object_child(dt), start, end);
4627 * Release LDLM locks on the stripes of a striped directory.
4629 * Iterates over all the locks taken on the stripe objects and
4632 * \param[in] env execution environment
4633 * \param[in] dt striped object
4634 * \param[in] einfo lock description
4635 * \param[in] policy data describing requested lock
4637 * \retval 0 on success
4638 * \retval negative if failed
4640 static int lod_object_unlock_internal(const struct lu_env *env,
4641 struct dt_object *dt,
4642 struct ldlm_enqueue_info *einfo,
4643 union ldlm_policy_data *policy)
4645 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
4650 if (slave_locks == NULL)
4653 for (i = 1; i < slave_locks->count; i++) {
4654 if (lustre_handle_is_used(&slave_locks->handles[i]))
4655 ldlm_lock_decref_and_cancel(&slave_locks->handles[i],
4663 * Implementation of dt_object_operations::do_object_unlock.
4665 * Used to release LDLM lock(s).
4667 * \see dt_object_operations::do_object_unlock() in the API description
4670 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
4671 struct ldlm_enqueue_info *einfo,
4672 union ldlm_policy_data *policy)
4674 struct lod_object *lo = lod_dt_obj(dt);
4675 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
4676 int slave_locks_size;
4680 if (slave_locks == NULL)
4683 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
4684 LASSERT(lo->ldo_dir_stripenr > 1);
4685 /* Note: for remote lock for single stripe dir, MDT will cancel
4686 * the lock by lockh directly */
4687 LASSERT(!dt_object_remote(dt_object_child(dt)));
4689 /* locks were unlocked in MDT layer */
4690 for (i = 1; i < slave_locks->count; i++) {
4691 LASSERT(!lustre_handle_is_used(&slave_locks->handles[i]));
4692 dt_invalidate(env, lo->ldo_stripe[i]);
4695 slave_locks_size = sizeof(*slave_locks) + slave_locks->count *
4696 sizeof(slave_locks->handles[0]);
4697 OBD_FREE(slave_locks, slave_locks_size);
4698 einfo->ei_cbdata = NULL;
4704 * Implementation of dt_object_operations::do_object_lock.
4706 * Used to get LDLM lock on the non-striped and striped objects.
4708 * \see dt_object_operations::do_object_lock() in the API description
4711 static int lod_object_lock(const struct lu_env *env,
4712 struct dt_object *dt,
4713 struct lustre_handle *lh,
4714 struct ldlm_enqueue_info *einfo,
4715 union ldlm_policy_data *policy)
4717 struct lod_object *lo = lod_dt_obj(dt);
4720 int slave_locks_size;
4721 struct lustre_handle_array *slave_locks = NULL;
4724 /* remote object lock */
4725 if (!einfo->ei_enq_slave) {
4726 LASSERT(dt_object_remote(dt));
4727 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
4731 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4732 GOTO(out, rc = -ENOTDIR);
4734 rc = lod_load_striping(env, lo);
4739 if (lo->ldo_dir_stripenr <= 1) {
4741 * NB, ei_cbdata stores pointer to slave locks, if no locks
4742 * taken, make sure it's set to NULL, otherwise MDT will try to
4745 einfo->ei_cbdata = NULL;
4749 slave_locks_size = sizeof(*slave_locks) + lo->ldo_dir_stripenr *
4750 sizeof(slave_locks->handles[0]);
4751 /* Freed in lod_object_unlock */
4752 OBD_ALLOC(slave_locks, slave_locks_size);
4753 if (slave_locks == NULL)
4754 GOTO(out, rc = -ENOMEM);
4755 slave_locks->count = lo->ldo_dir_stripenr;
4757 /* striped directory lock */
4758 for (i = 1; i < lo->ldo_dir_stripenr; i++) {
4759 struct lustre_handle lockh;
4760 struct ldlm_res_id *res_id;
4762 res_id = &lod_env_info(env)->lti_res_id;
4763 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
4765 einfo->ei_res_id = res_id;
4767 LASSERT(lo->ldo_stripe[i] != NULL);
4768 if (likely(dt_object_remote(lo->ldo_stripe[i]))) {
4769 rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh,
4772 struct ldlm_namespace *ns = einfo->ei_namespace;
4773 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
4774 ldlm_completion_callback completion = einfo->ei_cb_cp;
4775 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
4777 if (einfo->ei_mode == LCK_PW ||
4778 einfo->ei_mode == LCK_EX)
4779 dlmflags |= LDLM_FL_COS_INCOMPAT;
4781 /* This only happens if there are mulitple stripes
4782 * on the master MDT, i.e. except stripe0, there are
4783 * other stripes on the Master MDT as well, Only
4784 * happens in the test case right now. */
4785 LASSERT(ns != NULL);
4786 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS,
4787 policy, einfo->ei_mode,
4788 &dlmflags, blocking,
4790 NULL, 0, LVB_T_NONE,
4795 slave_locks->handles[i] = lockh;
4797 einfo->ei_cbdata = slave_locks;
4799 if (rc != 0 && slave_locks != NULL) {
4800 lod_object_unlock_internal(env, dt, einfo, policy);
4801 OBD_FREE(slave_locks, slave_locks_size);
4806 einfo->ei_cbdata = NULL;
4811 * Implementation of dt_object_operations::do_invalidate.
4813 * \see dt_object_operations::do_invalidate() in the API description for details
4815 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
4817 return dt_invalidate(env, dt_object_child(dt));
4820 struct dt_object_operations lod_obj_ops = {
4821 .do_read_lock = lod_object_read_lock,
4822 .do_write_lock = lod_object_write_lock,
4823 .do_read_unlock = lod_object_read_unlock,
4824 .do_write_unlock = lod_object_write_unlock,
4825 .do_write_locked = lod_object_write_locked,
4826 .do_attr_get = lod_attr_get,
4827 .do_declare_attr_set = lod_declare_attr_set,
4828 .do_attr_set = lod_attr_set,
4829 .do_xattr_get = lod_xattr_get,
4830 .do_declare_xattr_set = lod_declare_xattr_set,
4831 .do_xattr_set = lod_xattr_set,
4832 .do_declare_xattr_del = lod_declare_xattr_del,
4833 .do_xattr_del = lod_xattr_del,
4834 .do_xattr_list = lod_xattr_list,
4835 .do_ah_init = lod_ah_init,
4836 .do_declare_create = lod_declare_object_create,
4837 .do_create = lod_object_create,
4838 .do_declare_destroy = lod_declare_object_destroy,
4839 .do_destroy = lod_object_destroy,
4840 .do_index_try = lod_index_try,
4841 .do_declare_ref_add = lod_declare_ref_add,
4842 .do_ref_add = lod_ref_add,
4843 .do_declare_ref_del = lod_declare_ref_del,
4844 .do_ref_del = lod_ref_del,
4845 .do_object_sync = lod_object_sync,
4846 .do_object_lock = lod_object_lock,
4847 .do_object_unlock = lod_object_unlock,
4848 .do_invalidate = lod_invalidate,
4852 * Implementation of dt_body_operations::dbo_read.
4854 * \see dt_body_operations::dbo_read() in the API description for details.
4856 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
4857 struct lu_buf *buf, loff_t *pos)
4859 struct dt_object *next = dt_object_child(dt);
4860 return next->do_body_ops->dbo_read(env, next, buf, pos);
4864 * Implementation of dt_body_operations::dbo_declare_write.
4866 * \see dt_body_operations::dbo_declare_write() in the API description
4869 static ssize_t lod_declare_write(const struct lu_env *env,
4870 struct dt_object *dt,
4871 const struct lu_buf *buf, loff_t pos,
4874 return lod_sub_object_declare_write(env, dt_object_child(dt), buf, pos,
4879 * Implementation of dt_body_operations::dbo_write.
4881 * \see dt_body_operations::dbo_write() in the API description for details.
4883 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
4884 const struct lu_buf *buf, loff_t *pos,
4885 struct thandle *th, int iq)
4887 return lod_sub_object_write(env, dt_object_child(dt), buf, pos, th, iq);
4890 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
4891 __u64 start, __u64 end, struct thandle *th)
4893 if (dt_object_remote(dt))
4896 return lod_sub_object_declare_punch(env, dt_object_child(dt), start,
4900 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
4901 __u64 start, __u64 end, struct thandle *th)
4903 if (dt_object_remote(dt))
4906 return lod_sub_object_punch(env, dt_object_child(dt), start, end, th);
4909 static const struct dt_body_operations lod_body_lnk_ops = {
4910 .dbo_read = lod_read,
4911 .dbo_declare_write = lod_declare_write,
4912 .dbo_write = lod_write
4915 static const struct dt_body_operations lod_body_ops = {
4916 .dbo_read = lod_read,
4917 .dbo_declare_write = lod_declare_write,
4918 .dbo_write = lod_write,
4919 .dbo_declare_punch = lod_declare_punch,
4920 .dbo_punch = lod_punch,
4924 * Implementation of lu_object_operations::loo_object_init.
4926 * The function determines the type and the index of the target device using
4927 * sequence of the object's FID. Then passes control down to the
4928 * corresponding device:
4929 * OSD for the local objects, OSP for remote
4931 * \see lu_object_operations::loo_object_init() in the API description
4934 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
4935 const struct lu_object_conf *conf)
4937 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
4938 struct lu_device *cdev = NULL;
4939 struct lu_object *cobj;
4940 struct lod_tgt_descs *ltd = NULL;
4941 struct lod_tgt_desc *tgt;
4943 int type = LU_SEQ_RANGE_ANY;
4947 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
4949 /* Note: Sometimes, it will Return EAGAIN here, see
4950 * ptrlpc_import_delay_req(), which might confuse
4951 * lu_object_find_at() and make it wait there incorrectly.
4952 * so we convert it to EIO here.*/
4959 if (type == LU_SEQ_RANGE_MDT &&
4960 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
4961 cdev = &lod->lod_child->dd_lu_dev;
4962 } else if (type == LU_SEQ_RANGE_MDT) {
4963 ltd = &lod->lod_mdt_descs;
4965 } else if (type == LU_SEQ_RANGE_OST) {
4966 ltd = &lod->lod_ost_descs;
4973 if (ltd->ltd_tgts_size > idx &&
4974 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
4975 tgt = LTD_TGT(ltd, idx);
4977 LASSERT(tgt != NULL);
4978 LASSERT(tgt->ltd_tgt != NULL);
4980 cdev = &(tgt->ltd_tgt->dd_lu_dev);
4982 lod_putref(lod, ltd);
4985 if (unlikely(cdev == NULL))
4988 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
4989 if (unlikely(cobj == NULL))
4992 lu_object_add(lo, cobj);
4999 * Release resources associated with striping.
5001 * If the object is striped (regular or directory), then release
5002 * the stripe objects references and free the ldo_stripe array.
5004 * \param[in] env execution environment
5005 * \param[in] lo object
5007 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
5009 struct lod_layout_component *lod_comp;
5012 if (lo->ldo_stripe != NULL) {
5013 LASSERT(lo->ldo_comp_entries == NULL);
5014 LASSERT(lo->ldo_dir_stripes_allocated > 0);
5016 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
5017 if (lo->ldo_stripe[i])
5018 dt_object_put(env, lo->ldo_stripe[i]);
5021 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
5022 OBD_FREE(lo->ldo_stripe, j);
5023 lo->ldo_stripe = NULL;
5024 lo->ldo_dir_stripes_allocated = 0;
5025 lo->ldo_dir_stripenr = 0;
5026 } else if (lo->ldo_comp_entries != NULL) {
5027 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5028 /* free lod_layout_component::llc_stripe array */
5029 lod_comp = &lo->ldo_comp_entries[i];
5031 if (lod_comp->llc_stripe == NULL)
5033 LASSERT(lod_comp->llc_stripes_allocated != 0);
5034 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
5035 if (lod_comp->llc_stripe[j] != NULL)
5037 &lod_comp->llc_stripe[j]->do_lu);
5039 OBD_FREE(lod_comp->llc_stripe,
5040 sizeof(struct dt_object *) *
5041 lod_comp->llc_stripes_allocated);
5042 lod_comp->llc_stripe = NULL;
5043 lod_comp->llc_stripes_allocated = 0;
5045 lod_free_comp_entries(lo);
5046 lo->ldo_comp_cached = 0;
5051 * Implementation of lu_object_operations::loo_object_start.
5053 * \see lu_object_operations::loo_object_start() in the API description
5056 static int lod_object_start(const struct lu_env *env, struct lu_object *o)
5058 if (S_ISLNK(o->lo_header->loh_attr & S_IFMT)) {
5059 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_lnk_ops;
5060 } else if (S_ISREG(o->lo_header->loh_attr & S_IFMT) ||
5061 fid_is_local_file(lu_object_fid(o))) {
5062 /* Note: some local file (like last rcvd) is created
5063 * through bottom layer (OSD), so the object initialization
5064 * comes to lod, it does not set loh_attr yet, so
5065 * set do_body_ops for local file anyway */
5066 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_ops;
5072 * Implementation of lu_object_operations::loo_object_free.
5074 * \see lu_object_operations::loo_object_free() in the API description
5077 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
5079 struct lod_object *lo = lu2lod_obj(o);
5081 /* release all underlying object pinned */
5082 lod_object_free_striping(env, lo);
5084 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
5088 * Implementation of lu_object_operations::loo_object_release.
5090 * \see lu_object_operations::loo_object_release() in the API description
5093 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
5095 /* XXX: shouldn't we release everything here in case if object
5096 * creation failed before? */
5100 * Implementation of lu_object_operations::loo_object_print.
5102 * \see lu_object_operations::loo_object_print() in the API description
5105 static int lod_object_print(const struct lu_env *env, void *cookie,
5106 lu_printer_t p, const struct lu_object *l)
5108 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
5110 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
5113 struct lu_object_operations lod_lu_obj_ops = {
5114 .loo_object_init = lod_object_init,
5115 .loo_object_start = lod_object_start,
5116 .loo_object_free = lod_object_free,
5117 .loo_object_release = lod_object_release,
5118 .loo_object_print = lod_object_print,