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, PROJID, and size, and osp_attr_set() ignores all
1135 * but UID, GID and PROJID. Declaration of size attr setting
1136 * happens through lod_declare_init_size(), and not through
1137 * this function. Therefore we need not load striping unless
1138 * ownership is changing. This should save memory and (we hope)
1139 * speed up rename().
1141 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1142 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID)))
1145 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1148 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID | LA_MODE |
1149 LA_ATIME | LA_MTIME | LA_CTIME |
1154 * load striping information, notice we don't do this when object
1155 * is being initialized as we don't need this information till
1156 * few specific cases like destroy, chown
1158 rc = lod_load_striping(env, lo);
1162 if (!lod_obj_is_striped(dt))
1166 * if object is striped declare changes on the stripes
1168 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1169 LASSERT(lo->ldo_stripe);
1170 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
1171 if (lo->ldo_stripe[i] == NULL)
1173 rc = lod_sub_object_declare_attr_set(env,
1174 lo->ldo_stripe[i], attr,
1180 struct lod_obj_stripe_cb_data data;
1182 data.locd_attr = attr;
1183 data.locd_declare = true;
1184 rc = lod_obj_for_each_stripe(env, lo, th,
1185 lod_obj_stripe_attr_set_cb, &data);
1191 if (!dt_object_exists(next) || dt_object_remote(next) ||
1192 !S_ISREG(attr->la_mode))
1195 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1196 rc = lod_sub_object_declare_xattr_del(env, next,
1197 XATTR_NAME_LOV, th);
1201 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1202 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1203 struct lod_thread_info *info = lod_env_info(env);
1204 struct lu_buf *buf = &info->lti_buf;
1206 buf->lb_buf = info->lti_ea_store;
1207 buf->lb_len = info->lti_ea_store_size;
1208 rc = lod_sub_object_declare_xattr_set(env, next, buf,
1210 LU_XATTR_REPLACE, th);
1217 * Implementation of dt_object_operations::do_attr_set.
1219 * If the object is striped, then apply the changes to all or subset of
1220 * the stripes depending on the object type and specific attributes.
1222 * \see dt_object_operations::do_attr_set() in the API description for details.
1224 static int lod_attr_set(const struct lu_env *env,
1225 struct dt_object *dt,
1226 const struct lu_attr *attr,
1229 struct dt_object *next = dt_object_child(dt);
1230 struct lod_object *lo = lod_dt_obj(dt);
1235 * apply changes to the local object
1237 rc = lod_sub_object_attr_set(env, next, attr, th);
1241 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1242 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID)))
1245 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1248 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE | LA_PROJID |
1249 LA_ATIME | LA_MTIME | LA_CTIME |
1254 if (!lod_obj_is_striped(dt))
1258 * if object is striped, apply changes to all the stripes
1260 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1261 LASSERT(lo->ldo_stripe);
1262 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
1263 if (unlikely(lo->ldo_stripe[i] == NULL))
1266 if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1269 rc = lod_sub_object_attr_set(env, lo->ldo_stripe[i],
1275 struct lod_obj_stripe_cb_data data;
1277 data.locd_attr = attr;
1278 data.locd_declare = false;
1279 rc = lod_obj_for_each_stripe(env, lo, th,
1280 lod_obj_stripe_attr_set_cb, &data);
1286 if (!dt_object_exists(next) || dt_object_remote(next) ||
1287 !S_ISREG(attr->la_mode))
1290 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1291 rc = lod_sub_object_xattr_del(env, next, XATTR_NAME_LOV, th);
1295 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1296 struct lod_thread_info *info = lod_env_info(env);
1297 struct lu_buf *buf = &info->lti_buf;
1298 struct ost_id *oi = &info->lti_ostid;
1299 struct lu_fid *fid = &info->lti_fid;
1300 struct lov_mds_md_v1 *lmm;
1301 struct lov_ost_data_v1 *objs;
1304 rc = lod_get_lov_ea(env, lo);
1308 buf->lb_buf = info->lti_ea_store;
1309 buf->lb_len = info->lti_ea_store_size;
1310 lmm = info->lti_ea_store;
1311 magic = le32_to_cpu(lmm->lmm_magic);
1312 if (magic == LOV_MAGIC_COMP_V1) {
1313 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1314 struct lov_comp_md_entry_v1 *lcme =
1315 &lcm->lcm_entries[0];
1317 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1318 magic = le32_to_cpu(lmm->lmm_magic);
1321 if (magic == LOV_MAGIC_V1)
1322 objs = &(lmm->lmm_objects[0]);
1324 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1325 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1326 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1328 fid_to_ostid(fid, oi);
1329 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1331 rc = lod_sub_object_xattr_set(env, next, buf, XATTR_NAME_LOV,
1332 LU_XATTR_REPLACE, th);
1333 } else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1334 struct lod_thread_info *info = lod_env_info(env);
1335 struct lu_buf *buf = &info->lti_buf;
1336 struct lov_comp_md_v1 *lcm;
1337 struct lov_comp_md_entry_v1 *lcme;
1339 rc = lod_get_lov_ea(env, lo);
1343 buf->lb_buf = info->lti_ea_store;
1344 buf->lb_len = info->lti_ea_store_size;
1346 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
1349 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1350 lcme = &lcm->lcm_entries[0];
1351 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1352 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1354 rc = lod_sub_object_xattr_set(env, next, buf, XATTR_NAME_LOV,
1355 LU_XATTR_REPLACE, th);
1362 * Implementation of dt_object_operations::do_xattr_get.
1364 * If LOV EA is requested from the root object and it's not
1365 * found, then return default striping for the filesystem.
1367 * \see dt_object_operations::do_xattr_get() in the API description for details.
1369 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1370 struct lu_buf *buf, const char *name)
1372 struct lod_thread_info *info = lod_env_info(env);
1373 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1378 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1379 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1380 struct lmv_mds_md_v1 *lmv1;
1383 if (rc > (typeof(rc))sizeof(*lmv1))
1386 if (rc < (typeof(rc))sizeof(*lmv1))
1387 RETURN(rc = rc > 0 ? -EINVAL : rc);
1389 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1390 CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1392 info->lti_buf.lb_buf = info->lti_key;
1393 info->lti_buf.lb_len = sizeof(*lmv1);
1394 rc = dt_xattr_get(env, dt_object_child(dt),
1395 &info->lti_buf, name);
1396 if (unlikely(rc != sizeof(*lmv1)))
1397 RETURN(rc = rc > 0 ? -EINVAL : rc);
1399 lmv1 = info->lti_buf.lb_buf;
1400 /* The on-disk LMV EA only contains header, but the
1401 * returned LMV EA size should contain the space for
1402 * the FIDs of all shards of the striped directory. */
1403 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1404 rc = lmv_mds_md_size(
1405 le32_to_cpu(lmv1->lmv_stripe_count),
1408 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1412 RETURN(rc = rc1 != 0 ? rc1 : rc);
1415 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1419 * XXX: Only used by lfsck
1421 * lod returns default striping on the real root of the device
1422 * this is like the root stores default striping for the whole
1423 * filesystem. historically we've been using a different approach
1424 * and store it in the config.
1426 dt_root_get(env, dev->lod_child, &info->lti_fid);
1427 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1429 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1430 struct lov_user_md *lum = buf->lb_buf;
1431 struct lov_desc *desc = &dev->lod_desc;
1433 if (buf->lb_buf == NULL) {
1435 } else if (buf->lb_len >= sizeof(*lum)) {
1436 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1437 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1438 lmm_oi_set_id(&lum->lmm_oi, 0);
1439 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1440 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1441 lum->lmm_stripe_size = cpu_to_le32(
1442 desc->ld_default_stripe_size);
1443 lum->lmm_stripe_count = cpu_to_le16(
1444 desc->ld_default_stripe_count);
1445 lum->lmm_stripe_offset = cpu_to_le16(
1446 desc->ld_default_stripe_offset);
1459 * Checks that the magic of the stripe is sane.
1461 * \param[in] lod lod device
1462 * \param[in] lum a buffer storing LMV EA to verify
1464 * \retval 0 if the EA is sane
1465 * \retval negative otherwise
1467 static int lod_verify_md_striping(struct lod_device *lod,
1468 const struct lmv_user_md_v1 *lum)
1470 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1471 CERROR("%s: invalid lmv_user_md: magic = %x, "
1472 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1473 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1474 (int)le32_to_cpu(lum->lum_stripe_offset),
1475 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1483 * Initialize LMV EA for a slave.
1485 * Initialize slave's LMV EA from the master's LMV EA.
1487 * \param[in] master_lmv a buffer containing master's EA
1488 * \param[out] slave_lmv a buffer where slave's EA will be stored
1491 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1492 const struct lmv_mds_md_v1 *master_lmv)
1494 *slave_lmv = *master_lmv;
1495 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1501 * Generate LMV EA from the object passed as \a dt. The object must have
1502 * the stripes created and initialized.
1504 * \param[in] env execution environment
1505 * \param[in] dt object
1506 * \param[out] lmv_buf buffer storing generated LMV EA
1508 * \retval 0 on success
1509 * \retval negative if failed
1511 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1512 struct lu_buf *lmv_buf)
1514 struct lod_thread_info *info = lod_env_info(env);
1515 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1516 struct lod_object *lo = lod_dt_obj(dt);
1517 struct lmv_mds_md_v1 *lmm1;
1519 int type = LU_SEQ_RANGE_ANY;
1524 LASSERT(lo->ldo_dir_striped != 0);
1525 LASSERT(lo->ldo_dir_stripenr > 0);
1526 stripe_count = lo->ldo_dir_stripenr;
1527 /* Only store the LMV EA heahder on the disk. */
1528 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1529 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1533 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1536 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1537 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1538 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1539 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1540 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1545 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1546 lmv_buf->lb_buf = info->lti_ea_store;
1547 lmv_buf->lb_len = sizeof(*lmm1);
1553 * Create in-core represenation for a striped directory.
1555 * Parse the buffer containing LMV EA and instantiate LU objects
1556 * representing the stripe objects. The pointers to the objects are
1557 * stored in ldo_stripe field of \a lo. This function is used when
1558 * we need to access an already created object (i.e. load from a disk).
1560 * \param[in] env execution environment
1561 * \param[in] lo lod object
1562 * \param[in] buf buffer containing LMV EA
1564 * \retval 0 on success
1565 * \retval negative if failed
1567 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1568 const struct lu_buf *buf)
1570 struct lod_thread_info *info = lod_env_info(env);
1571 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1572 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1573 struct dt_object **stripe;
1574 union lmv_mds_md *lmm = buf->lb_buf;
1575 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1576 struct lu_fid *fid = &info->lti_fid;
1581 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1584 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1585 lo->ldo_dir_slave_stripe = 1;
1589 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1592 if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1595 LASSERT(lo->ldo_stripe == NULL);
1596 OBD_ALLOC(stripe, sizeof(stripe[0]) *
1597 (le32_to_cpu(lmv1->lmv_stripe_count)));
1601 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1602 struct dt_device *tgt_dt;
1603 struct dt_object *dto;
1604 int type = LU_SEQ_RANGE_ANY;
1607 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1608 if (!fid_is_sane(fid))
1609 GOTO(out, rc = -ESTALE);
1611 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1615 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1616 tgt_dt = lod->lod_child;
1618 struct lod_tgt_desc *tgt;
1620 tgt = LTD_TGT(ltd, idx);
1622 GOTO(out, rc = -ESTALE);
1623 tgt_dt = tgt->ltd_tgt;
1626 dto = dt_locate_at(env, tgt_dt, fid,
1627 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1630 GOTO(out, rc = PTR_ERR(dto));
1635 lo->ldo_stripe = stripe;
1636 lo->ldo_dir_stripenr = le32_to_cpu(lmv1->lmv_stripe_count);
1637 lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1639 lod_object_free_striping(env, lo);
1645 * Declare create a striped directory.
1647 * Declare creating a striped directory with a given stripe pattern on the
1648 * specified MDTs. A striped directory is represented as a regular directory
1649 * - an index listing all the stripes. The stripes point back to the master
1650 * object with ".." and LinkEA. The master object gets LMV EA which
1651 * identifies it as a striped directory. The function allocates FIDs
1654 * \param[in] env execution environment
1655 * \param[in] dt object
1656 * \param[in] attr attributes to initialize the objects with
1657 * \param[in] dof type of objects to be created
1658 * \param[in] th transaction handle
1660 * \retval 0 on success
1661 * \retval negative if failed
1663 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1664 struct dt_object *dt,
1665 struct lu_attr *attr,
1666 struct dt_object_format *dof,
1669 struct lod_thread_info *info = lod_env_info(env);
1670 struct lu_buf lmv_buf;
1671 struct lu_buf slave_lmv_buf;
1672 struct lmv_mds_md_v1 *lmm;
1673 struct lmv_mds_md_v1 *slave_lmm = NULL;
1674 struct dt_insert_rec *rec = &info->lti_dt_rec;
1675 struct lod_object *lo = lod_dt_obj(dt);
1680 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1683 lmm = lmv_buf.lb_buf;
1685 OBD_ALLOC_PTR(slave_lmm);
1686 if (slave_lmm == NULL)
1687 GOTO(out, rc = -ENOMEM);
1689 lod_prep_slave_lmv_md(slave_lmm, lmm);
1690 slave_lmv_buf.lb_buf = slave_lmm;
1691 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1693 if (!dt_try_as_dir(env, dt_object_child(dt)))
1694 GOTO(out, rc = -EINVAL);
1696 rec->rec_type = S_IFDIR;
1697 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
1698 struct dt_object *dto = lo->ldo_stripe[i];
1699 char *stripe_name = info->lti_key;
1700 struct lu_name *sname;
1701 struct linkea_data ldata = { NULL };
1702 struct lu_buf linkea_buf;
1704 rc = lod_sub_object_declare_create(env, dto, attr, NULL,
1709 if (!dt_try_as_dir(env, dto))
1710 GOTO(out, rc = -EINVAL);
1712 rc = lod_sub_object_declare_ref_add(env, dto, th);
1716 rec->rec_fid = lu_object_fid(&dto->do_lu);
1717 rc = lod_sub_object_declare_insert(env, dto,
1718 (const struct dt_rec *)rec,
1719 (const struct dt_key *)dot, th);
1723 /* master stripe FID will be put to .. */
1724 rec->rec_fid = lu_object_fid(&dt->do_lu);
1725 rc = lod_sub_object_declare_insert(env, dto,
1726 (const struct dt_rec *)rec,
1727 (const struct dt_key *)dotdot,
1732 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1733 cfs_fail_val != i) {
1734 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1736 slave_lmm->lmv_master_mdt_index =
1739 slave_lmm->lmv_master_mdt_index =
1741 rc = lod_sub_object_declare_xattr_set(env, dto,
1742 &slave_lmv_buf, XATTR_NAME_LMV, 0, th);
1747 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1749 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1750 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1752 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1753 PFID(lu_object_fid(&dto->do_lu)), i);
1755 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1756 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1757 sname, lu_object_fid(&dt->do_lu));
1761 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1762 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1763 rc = lod_sub_object_declare_xattr_set(env, dto, &linkea_buf,
1764 XATTR_NAME_LINK, 0, th);
1768 rec->rec_fid = lu_object_fid(&dto->do_lu);
1769 rc = lod_sub_object_declare_insert(env, dt_object_child(dt),
1770 (const struct dt_rec *)rec,
1771 (const struct dt_key *)stripe_name,
1776 rc = lod_sub_object_declare_ref_add(env, dt_object_child(dt),
1782 rc = lod_sub_object_declare_xattr_set(env, dt_object_child(dt),
1783 &lmv_buf, XATTR_NAME_LMV, 0, th);
1787 if (slave_lmm != NULL)
1788 OBD_FREE_PTR(slave_lmm);
1793 static int lod_prep_md_striped_create(const struct lu_env *env,
1794 struct dt_object *dt,
1795 struct lu_attr *attr,
1796 const struct lmv_user_md_v1 *lum,
1797 struct dt_object_format *dof,
1800 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1801 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1802 struct lod_object *lo = lod_dt_obj(dt);
1803 struct dt_object **stripe;
1812 /* The lum has been verifed in lod_verify_md_striping */
1813 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
1814 LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1816 stripe_count = le32_to_cpu(lum->lum_stripe_count);
1818 OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1819 if (idx_array == NULL)
1822 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1824 GOTO(out_free, rc = -ENOMEM);
1826 /* Start index must be the master MDT */
1827 master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1828 idx_array[0] = master_index;
1829 for (i = 0; i < stripe_count; i++) {
1830 struct lod_tgt_desc *tgt = NULL;
1831 struct dt_object *dto;
1832 struct lu_fid fid = { 0 };
1834 struct lu_object_conf conf = { 0 };
1835 struct dt_device *tgt_dt = NULL;
1837 /* Try to find next avaible target */
1839 for (j = 0; j < lod->lod_remote_mdt_count;
1840 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1841 bool already_allocated = false;
1844 CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
1845 idx, lod->lod_remote_mdt_count + 1, i);
1847 if (likely(!OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
1848 /* check whether the idx already exists
1849 * in current allocated array */
1850 for (k = 0; k < i; k++) {
1851 if (idx_array[k] == idx) {
1852 already_allocated = true;
1857 if (already_allocated)
1861 /* Sigh, this index is not in the bitmap, let's check
1862 * next available target */
1863 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) &&
1864 idx != master_index)
1867 if (idx == master_index) {
1868 /* Allocate the FID locally */
1869 rc = obd_fid_alloc(env, lod->lod_child_exp,
1873 tgt_dt = lod->lod_child;
1877 /* check the status of the OSP */
1878 tgt = LTD_TGT(ltd, idx);
1882 tgt_dt = tgt->ltd_tgt;
1883 rc = dt_statfs(env, tgt_dt, NULL);
1885 /* this OSP doesn't feel well */
1890 rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1899 /* Can not allocate more stripes */
1900 if (j == lod->lod_remote_mdt_count) {
1901 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1902 lod2obd(lod)->obd_name, stripe_count, i);
1906 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
1907 idx, i, PFID(&fid));
1909 /* Set the start index for next stripe allocation */
1910 if (i < stripe_count - 1)
1911 idx_array[i + 1] = (idx + 1) %
1912 (lod->lod_remote_mdt_count + 1);
1913 /* tgt_dt and fid must be ready after search avaible OSP
1914 * in the above loop */
1915 LASSERT(tgt_dt != NULL);
1916 LASSERT(fid_is_sane(&fid));
1917 conf.loc_flags = LOC_F_NEW;
1918 dto = dt_locate_at(env, tgt_dt, &fid,
1919 dt->do_lu.lo_dev->ld_site->ls_top_dev,
1922 GOTO(out_put, rc = PTR_ERR(dto));
1926 lo->ldo_dir_striped = 1;
1927 lo->ldo_stripe = stripe;
1928 lo->ldo_dir_stripenr = i;
1929 lo->ldo_dir_stripes_allocated = stripe_count;
1931 if (lo->ldo_dir_stripenr == 0)
1932 GOTO(out_put, rc = -ENOSPC);
1934 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
1940 for (i = 0; i < stripe_count; i++)
1941 if (stripe[i] != NULL)
1942 dt_object_put(env, stripe[i]);
1943 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
1944 lo->ldo_dir_stripenr = 0;
1945 lo->ldo_dir_stripes_allocated = 0;
1946 lo->ldo_stripe = NULL;
1950 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
1956 * Declare create striped md object.
1958 * The function declares intention to create a striped directory. This is a
1959 * wrapper for lod_prep_md_striped_create(). The only additional functionality
1960 * is to verify pattern \a lum_buf is good. Check that function for the details.
1962 * \param[in] env execution environment
1963 * \param[in] dt object
1964 * \param[in] attr attributes to initialize the objects with
1965 * \param[in] lum_buf a pattern specifying the number of stripes and
1967 * \param[in] dof type of objects to be created
1968 * \param[in] th transaction handle
1970 * \retval 0 on success
1971 * \retval negative if failed
1974 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
1975 struct dt_object *dt,
1976 struct lu_attr *attr,
1977 const struct lu_buf *lum_buf,
1978 struct dt_object_format *dof,
1981 struct lod_object *lo = lod_dt_obj(dt);
1982 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1983 struct lmv_user_md_v1 *lum;
1987 lum = lum_buf->lb_buf;
1988 LASSERT(lum != NULL);
1990 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
1991 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
1992 (int)le32_to_cpu(lum->lum_stripe_offset));
1994 if (le32_to_cpu(lum->lum_stripe_count) == 0)
1997 rc = lod_verify_md_striping(lod, lum);
2001 /* prepare dir striped objects */
2002 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2004 /* failed to create striping, let's reset
2005 * config so that others don't get confused */
2006 lod_object_free_striping(env, lo);
2014 * Implementation of dt_object_operations::do_declare_xattr_set.
2016 * Used with regular (non-striped) objects. Basically it
2017 * initializes the striping information and applies the
2018 * change to all the stripes.
2020 * \see dt_object_operations::do_declare_xattr_set() in the API description
2023 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2024 struct dt_object *dt,
2025 const struct lu_buf *buf,
2026 const char *name, int fl,
2029 struct dt_object *next = dt_object_child(dt);
2030 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2031 struct lod_object *lo = lod_dt_obj(dt);
2036 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2037 struct lmv_user_md_v1 *lum;
2039 LASSERT(buf != NULL && buf->lb_buf != NULL);
2041 rc = lod_verify_md_striping(d, lum);
2044 } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2045 rc = lod_verify_striping(d, buf, false, 0);
2050 rc = lod_sub_object_declare_xattr_set(env, next, buf, name, fl, th);
2054 /* Note: Do not set LinkEA on sub-stripes, otherwise
2055 * it will confuse the fid2path process(see mdt_path_current()).
2056 * The linkEA between master and sub-stripes is set in
2057 * lod_xattr_set_lmv(). */
2058 if (strcmp(name, XATTR_NAME_LINK) == 0)
2061 /* set xattr to each stripes, if needed */
2062 rc = lod_load_striping(env, lo);
2066 if (lo->ldo_dir_stripenr == 0)
2069 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2070 LASSERT(lo->ldo_stripe[i]);
2072 rc = lod_sub_object_declare_xattr_set(env, lo->ldo_stripe[i],
2082 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2083 struct lod_object *lo,
2084 struct dt_object *dt, struct thandle *th,
2086 struct lod_obj_stripe_cb_data *data)
2088 struct lod_thread_info *info = lod_env_info(env);
2089 struct filter_fid *ff = &info->lti_ff;
2090 struct lu_buf *buf = &info->lti_buf;
2094 buf->lb_len = sizeof(*ff);
2095 rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2102 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2103 ff->ff_parent.f_ver = stripe_idx;
2104 fid_cpu_to_le(&ff->ff_parent, &ff->ff_parent);
2105 if (data->locd_declare)
2106 rc = lod_sub_object_declare_xattr_set(env, dt, buf,
2108 LU_XATTR_REPLACE, th);
2110 rc = lod_sub_object_xattr_set(env, dt, buf, XATTR_NAME_FID,
2111 LU_XATTR_REPLACE, th);
2117 * Reset parent FID on OST object
2119 * Replace parent FID with @dt object FID, which is only called during migration
2120 * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2121 * the FID is changed.
2123 * \param[in] env execution environment
2124 * \param[in] dt dt_object whose stripes's parent FID will be reset
2125 * \parem[in] th thandle
2126 * \param[in] declare if it is declare
2128 * \retval 0 if reset succeeds
2129 * \retval negative errno if reset fails
2131 static int lod_object_replace_parent_fid(const struct lu_env *env,
2132 struct dt_object *dt,
2133 struct thandle *th, bool declare)
2135 struct lod_object *lo = lod_dt_obj(dt);
2136 struct lod_thread_info *info = lod_env_info(env);
2137 struct lu_buf *buf = &info->lti_buf;
2138 struct filter_fid *ff;
2139 struct lod_obj_stripe_cb_data data;
2143 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2145 /* set xattr to each stripes, if needed */
2146 rc = lod_load_striping(env, lo);
2150 if (!lod_obj_is_striped(dt))
2153 if (info->lti_ea_store_size < sizeof(*ff)) {
2154 rc = lod_ea_store_resize(info, sizeof(*ff));
2159 buf->lb_buf = info->lti_ea_store;
2160 buf->lb_len = info->lti_ea_store_size;
2162 data.locd_declare = declare;
2163 rc = lod_obj_for_each_stripe(env, lo, th,
2164 lod_obj_stripe_replace_parent_fid_cb, &data);
2169 inline __u16 lod_comp_entry_stripecnt(struct lod_object *lo,
2170 struct lod_layout_component *entry,
2173 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2177 else if (lod_comp_inited(entry))
2178 return entry->llc_stripenr;
2179 else if ((__u16)-1 == entry->llc_stripenr)
2180 return lod->lod_desc.ld_tgt_count;
2182 return lod_get_stripecnt(lod, lo, entry->llc_stripenr);
2185 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2187 int magic, size = 0, i;
2188 struct lod_layout_component *comp_entries;
2193 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2194 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2196 lo->ldo_def_striping->lds_def_striping_is_composite;
2198 comp_cnt = lo->ldo_comp_cnt;
2199 comp_entries = lo->ldo_comp_entries;
2200 is_composite = lo->ldo_is_composite;
2204 LASSERT(comp_cnt != 0 && comp_entries != NULL);
2206 size = sizeof(struct lov_comp_md_v1) +
2207 sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2208 LASSERT(size % sizeof(__u64) == 0);
2211 for (i = 0; i < comp_cnt; i++) {
2214 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2215 stripenr = lod_comp_entry_stripecnt(lo, &comp_entries[i],
2217 size += lov_user_md_size(stripenr, magic);
2218 LASSERT(size % sizeof(__u64) == 0);
2224 * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2225 * the xattr value is binary lov_comp_md_v1 which contains component(s)
2228 * \param[in] env execution environment
2229 * \param[in] dt dt_object to add components on
2230 * \param[in] buf buffer contains components to be added
2231 * \parem[in] th thandle
2233 * \retval 0 on success
2234 * \retval negative errno on failure
2236 static int lod_declare_layout_add(const struct lu_env *env,
2237 struct dt_object *dt,
2238 const struct lu_buf *buf,
2241 struct lod_thread_info *info = lod_env_info(env);
2242 struct lod_layout_component *comp_array, *lod_comp;
2243 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2244 struct dt_object *next = dt_object_child(dt);
2245 struct lov_desc *desc = &d->lod_desc;
2246 struct lod_object *lo = lod_dt_obj(dt);
2247 struct lov_user_md_v3 *v3;
2248 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2251 int i, rc, array_cnt;
2254 LASSERT(lo->ldo_is_composite);
2256 prev_end = lo->ldo_comp_entries[lo->ldo_comp_cnt - 1].llc_extent.e_end;
2257 rc = lod_verify_striping(d, buf, false, prev_end);
2261 magic = comp_v1->lcm_magic;
2262 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2263 lustre_swab_lov_comp_md_v1(comp_v1);
2264 magic = comp_v1->lcm_magic;
2267 if (magic != LOV_USER_MAGIC_COMP_V1)
2270 array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2271 OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2272 if (comp_array == NULL)
2275 memcpy(comp_array, lo->ldo_comp_entries,
2276 sizeof(*comp_array) * lo->ldo_comp_cnt);
2278 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2279 struct lov_user_md_v1 *v1;
2280 struct lu_extent *ext;
2282 v1 = (struct lov_user_md *)((char *)comp_v1 +
2283 comp_v1->lcm_entries[i].lcme_offset);
2284 ext = &comp_v1->lcm_entries[i].lcme_extent;
2286 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2287 lod_comp->llc_extent.e_start = ext->e_start;
2288 lod_comp->llc_extent.e_end = ext->e_end;
2289 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2291 lod_comp->llc_stripenr = v1->lmm_stripe_count;
2292 if (!lod_comp->llc_stripenr ||
2293 lod_comp->llc_stripenr == (__u16)-1)
2294 lod_comp->llc_stripenr = desc->ld_default_stripe_count;
2295 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2296 if (!lod_comp->llc_stripe_size)
2297 lod_comp->llc_stripe_size =
2298 desc->ld_default_stripe_size;
2300 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2301 v3 = (struct lov_user_md_v3 *) v1;
2302 if (v3->lmm_pool_name[0] != '\0') {
2303 rc = lod_set_pool(&lod_comp->llc_pool,
2311 OBD_FREE(lo->ldo_comp_entries, sizeof(*lod_comp) * lo->ldo_comp_cnt);
2312 lo->ldo_comp_entries = comp_array;
2313 lo->ldo_comp_cnt = array_cnt;
2314 /* No need to increase layout generation here, it will be increased
2315 * later when generating component ID for the new components */
2317 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2318 rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
2319 XATTR_NAME_LOV, 0, th);
2326 for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2327 lod_comp = &comp_array[i];
2328 if (lod_comp->llc_pool != NULL) {
2329 OBD_FREE(lod_comp->llc_pool,
2330 strlen(lod_comp->llc_pool) + 1);
2331 lod_comp->llc_pool = NULL;
2334 OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2339 * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2340 * the '$field' can only be 'flags' now. The xattr value is binary
2341 * lov_comp_md_v1 which contains the component ID(s) and the value of
2342 * the field to be modified.
2344 * \param[in] env execution environment
2345 * \param[in] dt dt_object to be modified
2346 * \param[in] op operation string, like "set.flags"
2347 * \param[in] buf buffer contains components to be set
2348 * \parem[in] th thandle
2350 * \retval 0 on success
2351 * \retval negative errno on failure
2353 static int lod_declare_layout_set(const struct lu_env *env,
2354 struct dt_object *dt,
2355 char *op, const struct lu_buf *buf,
2358 struct lod_layout_component *lod_comp;
2359 struct lod_thread_info *info = lod_env_info(env);
2360 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2361 struct lod_object *lo = lod_dt_obj(dt);
2362 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2365 bool changed = false;
2368 if (strcmp(op, "set.flags") != 0) {
2369 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2370 lod2obd(d)->obd_name, op);
2374 magic = comp_v1->lcm_magic;
2375 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2376 lustre_swab_lov_comp_md_v1(comp_v1);
2377 magic = comp_v1->lcm_magic;
2380 if (magic != LOV_USER_MAGIC_COMP_V1)
2383 if (comp_v1->lcm_entry_count == 0) {
2384 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2385 lod2obd(d)->obd_name);
2389 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2390 id = comp_v1->lcm_entries[i].lcme_id;
2392 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2393 lod_comp = &lo->ldo_comp_entries[j];
2394 if (id == lod_comp->llc_id || id == LCME_ID_ALL) {
2395 lod_comp->llc_flags =
2396 comp_v1->lcm_entries[i].lcme_flags;
2403 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2404 lod2obd(d)->obd_name);
2408 lod_obj_inc_layout_gen(lo);
2410 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2411 rc = lod_sub_object_declare_xattr_set(env, dt, &info->lti_buf,
2412 XATTR_NAME_LOV, 0, th);
2417 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2418 * and the xattr value is a unique component ID or a special lcme_id.
2420 * \param[in] env execution environment
2421 * \param[in] dt dt_object to be operated on
2422 * \param[in] buf buffer contains component ID or lcme_id
2423 * \parem[in] th thandle
2425 * \retval 0 on success
2426 * \retval negative errno on failure
2428 static int lod_declare_layout_del(const struct lu_env *env,
2429 struct dt_object *dt,
2430 const struct lu_buf *buf,
2433 struct lod_thread_info *info = lod_env_info(env);
2434 struct dt_object *next = dt_object_child(dt);
2435 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2436 struct lod_object *lo = lod_dt_obj(dt);
2437 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2438 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2439 __u32 magic, id, flags, neg_flags = 0;
2443 LASSERT(lo->ldo_is_composite);
2445 rc = lod_verify_striping(d, buf, false, 0);
2449 magic = comp_v1->lcm_magic;
2450 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2451 lustre_swab_lov_comp_md_v1(comp_v1);
2452 magic = comp_v1->lcm_magic;
2455 if (magic != LOV_USER_MAGIC_COMP_V1)
2458 id = comp_v1->lcm_entries[0].lcme_id;
2459 flags = comp_v1->lcm_entries[0].lcme_flags;
2461 if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
2462 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
2463 lod2obd(d)->obd_name, id, flags);
2467 if (id != LCME_ID_INVAL && flags != 0) {
2468 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
2469 lod2obd(d)->obd_name);
2473 if (flags & LCME_FL_NEG) {
2474 neg_flags = flags & ~LCME_FL_NEG;
2478 left = lo->ldo_comp_cnt;
2482 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
2483 struct lod_layout_component *lod_comp;
2485 lod_comp = &lo->ldo_comp_entries[i];
2487 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
2489 else if (flags && !(flags & lod_comp->llc_flags))
2491 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
2494 if (left != (i + 1)) {
2495 CDEBUG(D_LAYOUT, "%s: this deletion will create "
2496 "a hole.\n", lod2obd(d)->obd_name);
2501 /* Mark the component as deleted */
2502 lod_comp->llc_id = LCME_ID_INVAL;
2504 /* Not instantiated component */
2505 if (lod_comp->llc_stripe == NULL)
2508 LASSERT(lod_comp->llc_stripenr > 0);
2509 for (j = 0; j < lod_comp->llc_stripenr; j++) {
2510 struct dt_object *obj = lod_comp->llc_stripe[j];
2514 rc = lod_sub_object_declare_destroy(env, obj, th);
2520 LASSERTF(left >= 0, "left = %d\n", left);
2521 if (left == lo->ldo_comp_cnt) {
2522 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
2523 lod2obd(d)->obd_name, id);
2527 memset(attr, 0, sizeof(*attr));
2528 attr->la_valid = LA_SIZE;
2529 rc = lod_sub_object_declare_attr_set(env, next, attr, th);
2534 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2535 rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
2536 XATTR_NAME_LOV, 0, th);
2538 rc = lod_sub_object_declare_xattr_del(env, next, XATTR_NAME_LOV,
2546 * Declare layout add/set/del operations issued by special xattr names:
2548 * XATTR_LUSTRE_LOV.add add component(s) to existing file
2549 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
2550 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
2552 * \param[in] env execution environment
2553 * \param[in] dt object
2554 * \param[in] name name of xattr
2555 * \param[in] buf lu_buf contains xattr value
2556 * \param[in] th transaction handle
2558 * \retval 0 on success
2559 * \retval negative if failed
2561 static int lod_declare_modify_layout(const struct lu_env *env,
2562 struct dt_object *dt,
2564 const struct lu_buf *buf,
2567 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2568 struct lod_object *lo = lod_dt_obj(dt);
2569 struct dt_object *next = dt_object_child(&lo->ldo_obj);
2571 int rc, len = strlen(XATTR_LUSTRE_LOV);
2574 LASSERT(dt_object_exists(dt));
2576 if (strlen(name) <= len || name[len] != '.') {
2577 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
2578 lod2obd(d)->obd_name, name);
2583 dt_write_lock(env, next, 0);
2584 rc = lod_load_striping_locked(env, lo);
2588 /* the layout to be modified must be a composite layout */
2589 if (!lo->ldo_is_composite) {
2590 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
2591 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
2592 GOTO(unlock, rc = -EINVAL);
2595 op = (char *)name + len;
2596 if (strcmp(op, "add") == 0) {
2597 rc = lod_declare_layout_add(env, dt, buf, th);
2598 } else if (strcmp(op, "del") == 0) {
2599 rc = lod_declare_layout_del(env, dt, buf, th);
2600 } else if (strncmp(op, "set", strlen("set")) == 0) {
2601 rc = lod_declare_layout_set(env, dt, op, buf, th);
2603 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
2604 lod2obd(d)->obd_name, name);
2605 GOTO(unlock, rc = -ENOTSUPP);
2609 lod_object_free_striping(env, lo);
2610 dt_write_unlock(env, next);
2616 * Implementation of dt_object_operations::do_declare_xattr_set.
2618 * \see dt_object_operations::do_declare_xattr_set() in the API description
2621 * the extension to the API:
2622 * - declaring LOVEA requests striping creation
2623 * - LU_XATTR_REPLACE means layout swap
2625 static int lod_declare_xattr_set(const struct lu_env *env,
2626 struct dt_object *dt,
2627 const struct lu_buf *buf,
2628 const char *name, int fl,
2631 struct dt_object *next = dt_object_child(dt);
2632 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2637 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2638 if ((S_ISREG(mode) || mode == 0) && !(fl & LU_XATTR_REPLACE) &&
2639 (strcmp(name, XATTR_NAME_LOV) == 0 ||
2640 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
2642 * this is a request to create object's striping.
2644 * allow to declare predefined striping on a new (!mode) object
2645 * which is supposed to be replay of regular file creation
2646 * (when LOV setting is declared)
2648 * LU_XATTR_REPLACE is set to indicate a layout swap
2650 if (dt_object_exists(dt)) {
2651 rc = dt_attr_get(env, next, attr);
2655 memset(attr, 0, sizeof(*attr));
2656 attr->la_valid = LA_TYPE | LA_MODE;
2657 attr->la_mode = S_IFREG;
2659 rc = lod_declare_striped_object(env, dt, attr, buf, th);
2660 } else if (S_ISREG(mode) &&
2661 strlen(name) > strlen(XATTR_LUSTRE_LOV) + 1 &&
2662 strncmp(name, XATTR_LUSTRE_LOV,
2663 strlen(XATTR_LUSTRE_LOV)) == 0) {
2665 * this is a request to modify object's striping.
2666 * add/set/del component(s).
2668 if (!dt_object_exists(dt))
2671 rc = lod_declare_modify_layout(env, dt, name, buf, th);
2672 } else if (S_ISDIR(mode)) {
2673 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2674 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
2675 rc = lod_object_replace_parent_fid(env, dt, th, true);
2677 rc = lod_sub_object_declare_xattr_set(env, next, buf, name,
2685 * Apply xattr changes to the object.
2687 * Applies xattr changes to the object and the stripes if the latter exist.
2689 * \param[in] env execution environment
2690 * \param[in] dt object
2691 * \param[in] buf buffer pointing to the new value of xattr
2692 * \param[in] name name of xattr
2693 * \param[in] fl flags
2694 * \param[in] th transaction handle
2696 * \retval 0 on success
2697 * \retval negative if failed
2699 static int lod_xattr_set_internal(const struct lu_env *env,
2700 struct dt_object *dt,
2701 const struct lu_buf *buf,
2702 const char *name, int fl,
2705 struct dt_object *next = dt_object_child(dt);
2706 struct lod_object *lo = lod_dt_obj(dt);
2711 rc = lod_sub_object_xattr_set(env, next, buf, name, fl, th);
2712 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2715 /* Note: Do not set LinkEA on sub-stripes, otherwise
2716 * it will confuse the fid2path process(see mdt_path_current()).
2717 * The linkEA between master and sub-stripes is set in
2718 * lod_xattr_set_lmv(). */
2719 if (lo->ldo_dir_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2722 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2723 LASSERT(lo->ldo_stripe[i]);
2725 rc = lod_sub_object_xattr_set(env, lo->ldo_stripe[i], buf, name,
2735 * Delete an extended attribute.
2737 * Deletes specified xattr from the object and the stripes if the latter exist.
2739 * \param[in] env execution environment
2740 * \param[in] dt object
2741 * \param[in] name name of xattr
2742 * \param[in] th transaction handle
2744 * \retval 0 on success
2745 * \retval negative if failed
2747 static int lod_xattr_del_internal(const struct lu_env *env,
2748 struct dt_object *dt,
2749 const char *name, struct thandle *th)
2751 struct dt_object *next = dt_object_child(dt);
2752 struct lod_object *lo = lod_dt_obj(dt);
2757 rc = lod_sub_object_xattr_del(env, next, name, th);
2758 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2761 if (lo->ldo_dir_stripenr == 0)
2764 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2765 LASSERT(lo->ldo_stripe[i]);
2767 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name,
2777 * Set default striping on a directory.
2779 * Sets specified striping on a directory object unless it matches the default
2780 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2781 * EA. This striping will be used when regular file is being created in this
2784 * \param[in] env execution environment
2785 * \param[in] dt the striped object
2786 * \param[in] buf buffer with the striping
2787 * \param[in] name name of EA
2788 * \param[in] fl xattr flag (see OSD API description)
2789 * \param[in] th transaction handle
2791 * \retval 0 on success
2792 * \retval negative if failed
2794 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
2795 struct dt_object *dt,
2796 const struct lu_buf *buf,
2797 const char *name, int fl,
2800 struct lov_user_md_v1 *lum;
2801 struct lov_user_md_v3 *v3 = NULL;
2802 const char *pool_name = NULL;
2807 LASSERT(buf != NULL && buf->lb_buf != NULL);
2810 switch (lum->lmm_magic) {
2811 case LOV_USER_MAGIC_V3:
2813 if (v3->lmm_pool_name[0] != '\0')
2814 pool_name = v3->lmm_pool_name;
2816 case LOV_USER_MAGIC_V1:
2817 /* if { size, offset, count } = { 0, -1, 0 } and no pool
2818 * (i.e. all default values specified) then delete default
2819 * striping from dir. */
2821 "set default striping: sz %u # %u offset %d %s %s\n",
2822 (unsigned)lum->lmm_stripe_size,
2823 (unsigned)lum->lmm_stripe_count,
2824 (int)lum->lmm_stripe_offset,
2825 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
2827 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
2828 lum->lmm_stripe_count,
2829 lum->lmm_stripe_offset,
2832 case LOV_USER_MAGIC_COMP_V1:
2836 CERROR("Invalid magic %x\n", lum->lmm_magic);
2841 rc = lod_xattr_del_internal(env, dt, name, th);
2845 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2852 * Set default striping on a directory object.
2854 * Sets specified striping on a directory object unless it matches the default
2855 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2856 * EA. This striping will be used when a new directory is being created in the
2859 * \param[in] env execution environment
2860 * \param[in] dt the striped object
2861 * \param[in] buf buffer with the striping
2862 * \param[in] name name of EA
2863 * \param[in] fl xattr flag (see OSD API description)
2864 * \param[in] th transaction handle
2866 * \retval 0 on success
2867 * \retval negative if failed
2869 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
2870 struct dt_object *dt,
2871 const struct lu_buf *buf,
2872 const char *name, int fl,
2875 struct lmv_user_md_v1 *lum;
2879 LASSERT(buf != NULL && buf->lb_buf != NULL);
2882 CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
2883 le32_to_cpu(lum->lum_stripe_count),
2884 (int)le32_to_cpu(lum->lum_stripe_offset));
2886 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
2887 le32_to_cpu(lum->lum_stripe_offset)) &&
2888 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
2889 rc = lod_xattr_del_internal(env, dt, name, th);
2893 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2902 * Turn directory into a striped directory.
2904 * During replay the client sends the striping created before MDT
2905 * failure, then the layer above LOD sends this defined striping
2906 * using ->do_xattr_set(), so LOD uses this method to replay creation
2907 * of the stripes. Notice the original information for the striping
2908 * (#stripes, FIDs, etc) was transferred in declare path.
2910 * \param[in] env execution environment
2911 * \param[in] dt the striped object
2912 * \param[in] buf not used currently
2913 * \param[in] name not used currently
2914 * \param[in] fl xattr flag (see OSD API description)
2915 * \param[in] th transaction handle
2917 * \retval 0 on success
2918 * \retval negative if failed
2920 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
2921 const struct lu_buf *buf, const char *name,
2922 int fl, struct thandle *th)
2924 struct lod_object *lo = lod_dt_obj(dt);
2925 struct lod_thread_info *info = lod_env_info(env);
2926 struct lu_attr *attr = &info->lti_attr;
2927 struct dt_object_format *dof = &info->lti_format;
2928 struct lu_buf lmv_buf;
2929 struct lu_buf slave_lmv_buf;
2930 struct lmv_mds_md_v1 *lmm;
2931 struct lmv_mds_md_v1 *slave_lmm = NULL;
2932 struct dt_insert_rec *rec = &info->lti_dt_rec;
2937 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2940 /* The stripes are supposed to be allocated in declare phase,
2941 * if there are no stripes being allocated, it will skip */
2942 if (lo->ldo_dir_stripenr == 0)
2945 rc = dt_attr_get(env, dt_object_child(dt), attr);
2949 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
2950 LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
2951 dof->dof_type = DFT_DIR;
2953 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
2956 lmm = lmv_buf.lb_buf;
2958 OBD_ALLOC_PTR(slave_lmm);
2959 if (slave_lmm == NULL)
2962 lod_prep_slave_lmv_md(slave_lmm, lmm);
2963 slave_lmv_buf.lb_buf = slave_lmm;
2964 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
2966 rec->rec_type = S_IFDIR;
2967 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2968 struct dt_object *dto;
2969 char *stripe_name = info->lti_key;
2970 struct lu_name *sname;
2971 struct linkea_data ldata = { NULL };
2972 struct lu_buf linkea_buf;
2974 dto = lo->ldo_stripe[i];
2976 dt_write_lock(env, dto, MOR_TGT_CHILD);
2977 rc = lod_sub_object_create(env, dto, attr, NULL, dof,
2980 dt_write_unlock(env, dto);
2984 rc = lod_sub_object_ref_add(env, dto, th);
2985 dt_write_unlock(env, dto);
2989 rec->rec_fid = lu_object_fid(&dto->do_lu);
2990 rc = lod_sub_object_index_insert(env, dto,
2991 (const struct dt_rec *)rec,
2992 (const struct dt_key *)dot, th, 0);
2996 rec->rec_fid = lu_object_fid(&dt->do_lu);
2997 rc = lod_sub_object_index_insert(env, dto, (struct dt_rec *)rec,
2998 (const struct dt_key *)dotdot, th, 0);
3002 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
3003 cfs_fail_val != i) {
3004 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
3006 slave_lmm->lmv_master_mdt_index =
3009 slave_lmm->lmv_master_mdt_index =
3012 rc = lod_sub_object_xattr_set(env, dto, &slave_lmv_buf,
3013 XATTR_NAME_LMV, fl, th);
3018 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
3020 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3021 PFID(lu_object_fid(&dto->do_lu)), i + 1);
3023 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3024 PFID(lu_object_fid(&dto->do_lu)), i);
3026 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
3027 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
3028 sname, lu_object_fid(&dt->do_lu));
3032 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
3033 linkea_buf.lb_len = ldata.ld_leh->leh_len;
3034 rc = lod_sub_object_xattr_set(env, dto, &linkea_buf,
3035 XATTR_NAME_LINK, 0, th);
3039 rec->rec_fid = lu_object_fid(&dto->do_lu);
3040 rc = lod_sub_object_index_insert(env, dt_object_child(dt),
3041 (const struct dt_rec *)rec,
3042 (const struct dt_key *)stripe_name, th, 0);
3046 rc = lod_sub_object_ref_add(env, dt_object_child(dt), th);
3051 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
3052 rc = lod_sub_object_xattr_set(env, dt_object_child(dt),
3053 &lmv_buf, XATTR_NAME_LMV, fl, th);
3055 if (slave_lmm != NULL)
3056 OBD_FREE_PTR(slave_lmm);
3062 * Helper function to declare/execute creation of a striped directory
3064 * Called in declare/create object path, prepare striping for a directory
3065 * and prepare defaults data striping for the objects to be created in
3066 * that directory. Notice the function calls "declaration" or "execution"
3067 * methods depending on \a declare param. This is a consequence of the
3068 * current approach while we don't have natural distributed transactions:
3069 * we basically execute non-local updates in the declare phase. So, the
3070 * arguments for the both phases are the same and this is the reason for
3071 * this function to exist.
3073 * \param[in] env execution environment
3074 * \param[in] dt object
3075 * \param[in] attr attributes the stripes will be created with
3076 * \param[in] dof format of stripes (see OSD API description)
3077 * \param[in] th transaction handle
3078 * \param[in] declare where to call "declare" or "execute" methods
3080 * \retval 0 on success
3081 * \retval negative if failed
3083 static int lod_dir_striping_create_internal(const struct lu_env *env,
3084 struct dt_object *dt,
3085 struct lu_attr *attr,
3086 struct dt_object_format *dof,
3090 struct lod_thread_info *info = lod_env_info(env);
3091 struct lod_object *lo = lod_dt_obj(dt);
3092 const struct lod_default_striping *lds = lo->ldo_def_striping;
3096 LASSERT(ergo(lds != NULL,
3097 lds->lds_def_striping_set ||
3098 lds->lds_dir_def_striping_set));
3100 if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripenr,
3101 lo->ldo_dir_stripe_offset)) {
3102 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3103 int stripe_count = lo->ldo_dir_stripenr;
3105 if (info->lti_ea_store_size < sizeof(*v1)) {
3106 rc = lod_ea_store_resize(info, sizeof(*v1));
3109 v1 = info->lti_ea_store;
3112 memset(v1, 0, sizeof(*v1));
3113 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3114 v1->lum_stripe_count = cpu_to_le32(stripe_count);
3115 v1->lum_stripe_offset =
3116 cpu_to_le32(lo->ldo_dir_stripe_offset);
3118 info->lti_buf.lb_buf = v1;
3119 info->lti_buf.lb_len = sizeof(*v1);
3122 rc = lod_declare_xattr_set_lmv(env, dt, attr,
3123 &info->lti_buf, dof, th);
3125 rc = lod_xattr_set_lmv(env, dt, &info->lti_buf,
3126 XATTR_NAME_LMV, 0, th);
3131 /* Transfer default LMV striping from the parent */
3132 if (lds != NULL && lds->lds_dir_def_striping_set &&
3133 !LMVEA_DELETE_VALUES(lds->lds_dir_def_stripenr,
3134 lds->lds_dir_def_stripe_offset)) {
3135 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3137 if (info->lti_ea_store_size < sizeof(*v1)) {
3138 rc = lod_ea_store_resize(info, sizeof(*v1));
3141 v1 = info->lti_ea_store;
3144 memset(v1, 0, sizeof(*v1));
3145 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3146 v1->lum_stripe_count = cpu_to_le32(lds->lds_dir_def_stripenr);
3147 v1->lum_stripe_offset =
3148 cpu_to_le32(lds->lds_dir_def_stripe_offset);
3150 cpu_to_le32(lds->lds_dir_def_hash_type);
3152 info->lti_buf.lb_buf = v1;
3153 info->lti_buf.lb_len = sizeof(*v1);
3155 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3156 XATTR_NAME_DEFAULT_LMV,
3159 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
3161 XATTR_NAME_DEFAULT_LMV, 0,
3167 /* Transfer default LOV striping from the parent */
3168 if (lds != NULL && lds->lds_def_striping_set &&
3169 lds->lds_def_comp_cnt != 0) {
3170 struct lov_mds_md *lmm;
3171 int lmm_size = lod_comp_md_size(lo, true);
3173 if (info->lti_ea_store_size < lmm_size) {
3174 rc = lod_ea_store_resize(info, lmm_size);
3178 lmm = info->lti_ea_store;
3180 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
3184 info->lti_buf.lb_buf = lmm;
3185 info->lti_buf.lb_len = lmm_size;
3188 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3189 XATTR_NAME_LOV, 0, th);
3191 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
3192 XATTR_NAME_LOV, 0, th);
3200 static int lod_declare_dir_striping_create(const struct lu_env *env,
3201 struct dt_object *dt,
3202 struct lu_attr *attr,
3203 struct dt_object_format *dof,
3206 return lod_dir_striping_create_internal(env, dt, attr, dof, th, true);
3209 static int lod_dir_striping_create(const struct lu_env *env,
3210 struct dt_object *dt,
3211 struct lu_attr *attr,
3212 struct dt_object_format *dof,
3215 return lod_dir_striping_create_internal(env, dt, attr, dof, th, false);
3219 * Make LOV EA for striped object.
3221 * Generate striping information and store it in the LOV EA of the given
3222 * object. The caller must ensure nobody else is calling the function
3223 * against the object concurrently. The transaction must be started.
3224 * FLDB service must be running as well; it's used to map FID to the target,
3225 * which is stored in LOV EA.
3227 * \param[in] env execution environment for this thread
3228 * \param[in] lo LOD object
3229 * \param[in] th transaction handle
3231 * \retval 0 if LOV EA is stored successfully
3232 * \retval negative error number on failure
3234 static int lod_generate_and_set_lovea(const struct lu_env *env,
3235 struct lod_object *lo,
3238 struct lod_thread_info *info = lod_env_info(env);
3239 struct dt_object *next = dt_object_child(&lo->ldo_obj);
3240 struct lov_mds_md_v1 *lmm;
3246 if (lo->ldo_comp_cnt == 0) {
3247 lod_object_free_striping(env, lo);
3248 rc = lod_sub_object_xattr_del(env, next, XATTR_NAME_LOV, th);
3252 lmm_size = lod_comp_md_size(lo, false);
3253 if (info->lti_ea_store_size < lmm_size) {
3254 rc = lod_ea_store_resize(info, lmm_size);
3258 lmm = info->lti_ea_store;
3260 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
3264 info->lti_buf.lb_buf = lmm;
3265 info->lti_buf.lb_len = lmm_size;
3266 rc = lod_sub_object_xattr_set(env, next, &info->lti_buf,
3267 XATTR_NAME_LOV, 0, th);
3272 * Delete layout component(s)
3274 * \param[in] env execution environment for this thread
3275 * \param[in] dt object
3276 * \param[in] th transaction handle
3278 * \retval 0 on success
3279 * \retval negative error number on failure
3281 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
3284 struct lod_layout_component *lod_comp;
3285 struct lod_object *lo = lod_dt_obj(dt);
3286 struct dt_object *next = dt_object_child(dt);
3287 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3290 LASSERT(lo->ldo_is_composite);
3291 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
3293 left = lo->ldo_comp_cnt;
3294 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3295 lod_comp = &lo->ldo_comp_entries[i];
3297 if (lod_comp->llc_id != LCME_ID_INVAL)
3301 /* Not instantiated component */
3302 if (lod_comp->llc_stripe == NULL)
3305 LASSERT(lod_comp->llc_stripenr > 0);
3306 for (j = 0; j < lod_comp->llc_stripenr; j++) {
3307 struct dt_object *obj = lod_comp->llc_stripe[j];
3311 rc = lod_sub_object_destroy(env, obj, th);
3315 lu_object_put(env, &obj->do_lu);
3316 lod_comp->llc_stripe[j] = NULL;
3318 OBD_FREE(lod_comp->llc_stripe, sizeof(struct dt_object *) *
3319 lod_comp->llc_stripes_allocated);
3320 lod_comp->llc_stripe = NULL;
3321 lod_comp->llc_stripes_allocated = 0;
3322 lod_obj_set_pool(lo, i, NULL);
3323 if (lod_comp->llc_ostlist.op_array) {
3324 OBD_FREE(lod_comp->llc_ostlist.op_array,
3325 lod_comp->llc_ostlist.op_size);
3326 lod_comp->llc_ostlist.op_array = NULL;
3327 lod_comp->llc_ostlist.op_size = 0;
3331 LASSERTF(left >= 0 && left < lo->ldo_comp_cnt, "left = %d\n", left);
3333 struct lod_layout_component *comp_array;
3335 OBD_ALLOC(comp_array, sizeof(*comp_array) * left);
3336 if (comp_array == NULL)
3337 GOTO(out, rc = -ENOMEM);
3339 memcpy(&comp_array[0], &lo->ldo_comp_entries[0],
3340 sizeof(*comp_array) * left);
3342 OBD_FREE(lo->ldo_comp_entries,
3343 sizeof(*comp_array) * lo->ldo_comp_cnt);
3344 lo->ldo_comp_entries = comp_array;
3345 lo->ldo_comp_cnt = left;
3346 lod_obj_inc_layout_gen(lo);
3348 lod_free_comp_entries(lo);
3351 LASSERT(dt_object_exists(dt));
3352 rc = dt_attr_get(env, next, attr);
3356 if (attr->la_size > 0) {
3358 attr->la_valid = LA_SIZE;
3359 rc = lod_sub_object_attr_set(env, next, attr, th);
3364 rc = lod_generate_and_set_lovea(env, lo, th);
3368 lod_object_free_striping(env, lo);
3373 * Implementation of dt_object_operations::do_xattr_set.
3375 * Sets specified extended attribute on the object. Three types of EAs are
3377 * LOV EA - stores striping for a regular file or default striping (when set
3379 * LMV EA - stores a marker for the striped directories
3380 * DMV EA - stores default directory striping
3382 * When striping is applied to a non-striped existing object (this is called
3383 * late striping), then LOD notices the caller wants to turn the object into a
3384 * striped one. The stripe objects are created and appropriate EA is set:
3385 * LOV EA storing all the stripes directly or LMV EA storing just a small header
3386 * with striping configuration.
3388 * \see dt_object_operations::do_xattr_set() in the API description for details.
3390 static int lod_xattr_set(const struct lu_env *env,
3391 struct dt_object *dt, const struct lu_buf *buf,
3392 const char *name, int fl, struct thandle *th)
3394 struct dt_object *next = dt_object_child(dt);
3398 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3399 strcmp(name, XATTR_NAME_LMV) == 0) {
3400 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
3402 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
3403 LMV_HASH_FLAG_MIGRATION)
3404 rc = lod_sub_object_xattr_set(env, next, buf, name, fl,
3407 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
3412 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3413 strcmp(name, XATTR_NAME_LOV) == 0) {
3415 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
3417 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3418 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
3420 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
3423 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3424 (!strcmp(name, XATTR_NAME_LOV) ||
3425 !strncmp(name, XATTR_LUSTRE_LOV,
3426 strlen(XATTR_LUSTRE_LOV)))) {
3427 /* in case of lov EA swap, just set it
3428 * if not, it is a replay so check striping match what we
3429 * already have during req replay, declare_xattr_set()
3430 * defines striping, then create() does the work */
3431 if (fl & LU_XATTR_REPLACE) {
3432 /* free stripes, then update disk */
3433 lod_object_free_striping(env, lod_dt_obj(dt));
3435 rc = lod_sub_object_xattr_set(env, next, buf, name,
3437 } else if (dt_object_remote(dt)) {
3438 /* This only happens during migration, see
3439 * mdd_migrate_create(), in which Master MDT will
3440 * create a remote target object, and only set
3441 * (migrating) stripe EA on the remote object,
3442 * and does not need creating each stripes. */
3443 rc = lod_sub_object_xattr_set(env, next, buf, name,
3445 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
3446 /* delete component(s) */
3447 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
3448 rc = lod_layout_del(env, dt, th);
3451 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
3452 * it's going to create create file with specified
3453 * component(s), the striping must have not being
3454 * cached in this case;
3456 * Otherwise, it's going to add/change component(s) to
3457 * an existing file, the striping must have been cached
3460 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
3461 !strcmp(name, XATTR_NAME_LOV),
3462 !lod_dt_obj(dt)->ldo_comp_cached));
3464 rc = lod_striping_create(env, dt, NULL, NULL, th);
3467 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3468 rc = lod_object_replace_parent_fid(env, dt, th, false);
3473 /* then all other xattr */
3474 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3480 * Implementation of dt_object_operations::do_declare_xattr_del.
3482 * \see dt_object_operations::do_declare_xattr_del() in the API description
3485 static int lod_declare_xattr_del(const struct lu_env *env,
3486 struct dt_object *dt, const char *name,
3489 struct lod_object *lo = lod_dt_obj(dt);
3494 rc = lod_sub_object_declare_xattr_del(env, dt_object_child(dt),
3499 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3502 /* set xattr to each stripes, if needed */
3503 rc = lod_load_striping(env, lo);
3507 if (lo->ldo_dir_stripenr == 0)
3510 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
3511 LASSERT(lo->ldo_stripe[i]);
3512 rc = lod_sub_object_declare_xattr_del(env, lo->ldo_stripe[i],
3522 * Implementation of dt_object_operations::do_xattr_del.
3524 * If EA storing a regular striping is being deleted, then release
3525 * all the references to the stripe objects in core.
3527 * \see dt_object_operations::do_xattr_del() in the API description for details.
3529 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
3530 const char *name, struct thandle *th)
3532 struct dt_object *next = dt_object_child(dt);
3533 struct lod_object *lo = lod_dt_obj(dt);
3538 if (!strcmp(name, XATTR_NAME_LOV))
3539 lod_object_free_striping(env, lod_dt_obj(dt));
3541 rc = lod_sub_object_xattr_del(env, next, name, th);
3542 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3545 if (lo->ldo_dir_stripenr == 0)
3548 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
3549 LASSERT(lo->ldo_stripe[i]);
3551 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name, th);
3560 * Implementation of dt_object_operations::do_xattr_list.
3562 * \see dt_object_operations::do_xattr_list() in the API description
3565 static int lod_xattr_list(const struct lu_env *env,
3566 struct dt_object *dt, const struct lu_buf *buf)
3568 return dt_xattr_list(env, dt_object_child(dt), buf);
3571 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
3573 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
3578 * Get default striping.
3580 * \param[in] env execution environment
3581 * \param[in] lo object
3582 * \param[out] lds default striping
3584 * \retval 0 on success
3585 * \retval negative if failed
3587 static int lod_get_default_lov_striping(const struct lu_env *env,
3588 struct lod_object *lo,
3589 struct lod_default_striping *lds)
3591 struct lod_thread_info *info = lod_env_info(env);
3592 struct lov_user_md_v1 *v1 = NULL;
3593 struct lov_user_md_v3 *v3 = NULL;
3594 struct lov_comp_md_v1 *comp_v1 = NULL;
3600 lds->lds_def_striping_set = 0;
3602 rc = lod_get_lov_ea(env, lo);
3606 if (rc < (typeof(rc))sizeof(struct lov_user_md))
3609 v1 = info->lti_ea_store;
3610 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
3611 lustre_swab_lov_user_md_v1(v1);
3612 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
3613 v3 = (struct lov_user_md_v3 *)v1;
3614 lustre_swab_lov_user_md_v3(v3);
3615 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3616 comp_v1 = (struct lov_comp_md_v1 *)v1;
3617 lustre_swab_lov_comp_md_v1(comp_v1);
3620 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
3621 v1->lmm_magic != LOV_MAGIC_COMP_V1)
3624 if (v1->lmm_magic == LOV_MAGIC_COMP_V1) {
3625 comp_v1 = (struct lov_comp_md_v1 *)v1;
3626 comp_cnt = comp_v1->lcm_entry_count;
3635 /* realloc default comp entries if necessary */
3636 rc = lod_def_striping_comp_resize(lds, comp_cnt);
3640 lds->lds_def_comp_cnt = comp_cnt;
3641 lds->lds_def_striping_is_composite = composite ? 1 : 0;
3643 for (i = 0; i < comp_cnt; i++) {
3644 struct lod_layout_component *lod_comp;
3645 struct lu_extent *ext;
3648 lod_comp = &lds->lds_def_comp_entries[i];
3650 * reset lod_comp values, llc_stripes is always NULL in
3651 * the default striping template, llc_pool will be reset
3654 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
3657 v1 = (struct lov_user_md *)((char *)comp_v1 +
3658 comp_v1->lcm_entries[i].lcme_offset);
3659 ext = &comp_v1->lcm_entries[i].lcme_extent;
3660 lod_comp->llc_extent = *ext;
3663 if (v1->lmm_pattern != LOV_PATTERN_RAID0 &&
3664 v1->lmm_pattern != 0) {
3665 lod_free_def_comp_entries(lds);
3669 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d "
3670 "stripe_offset=%d\n",
3671 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
3672 (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
3673 (int)v1->lmm_stripe_offset);
3675 lod_comp->llc_stripenr = v1->lmm_stripe_count;
3676 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
3677 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
3680 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
3681 /* XXX: sanity check here */
3682 v3 = (struct lov_user_md_v3 *) v1;
3683 if (v3->lmm_pool_name[0] != '\0')
3684 pool = v3->lmm_pool_name;
3686 lod_set_def_pool(lds, i, pool);
3689 lds->lds_def_striping_set = 1;
3694 * Get default directory striping.
3696 * \param[in] env execution environment
3697 * \param[in] lo object
3698 * \param[out] lds default striping
3700 * \retval 0 on success
3701 * \retval negative if failed
3703 static int lod_get_default_lmv_striping(const struct lu_env *env,
3704 struct lod_object *lo,
3705 struct lod_default_striping *lds)
3707 struct lod_thread_info *info = lod_env_info(env);
3708 struct lmv_user_md_v1 *v1 = NULL;
3712 lds->lds_dir_def_striping_set = 0;
3713 rc = lod_get_default_lmv_ea(env, lo);
3717 if (rc < (typeof(rc))sizeof(struct lmv_user_md))
3720 v1 = info->lti_ea_store;
3722 lds->lds_dir_def_stripenr = le32_to_cpu(v1->lum_stripe_count);
3723 lds->lds_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
3724 lds->lds_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
3725 lds->lds_dir_def_striping_set = 1;
3731 * Get default striping in the object.
3733 * Get object default striping and default directory striping.
3735 * \param[in] env execution environment
3736 * \param[in] lo object
3737 * \param[out] lds default striping
3739 * \retval 0 on success
3740 * \retval negative if failed
3742 static int lod_get_default_striping(const struct lu_env *env,
3743 struct lod_object *lo,
3744 struct lod_default_striping *lds)
3748 rc = lod_get_default_lov_striping(env, lo, lds);
3749 rc1 = lod_get_default_lmv_striping(env, lo, lds);
3750 if (rc == 0 && rc1 < 0)
3757 * Apply default striping on object.
3759 * If object striping pattern is not set, set to the one in default striping.
3760 * The default striping is from parent or fs.
3762 * \param[in] lo new object
3763 * \param[in] lds default striping
3764 * \param[in] mode new object's mode
3766 static void lod_striping_from_default(struct lod_object *lo,
3767 const struct lod_default_striping *lds,
3770 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
3771 struct lov_desc *desc = &d->lod_desc;
3774 if (lds->lds_def_striping_set && S_ISREG(mode)) {
3775 rc = lod_alloc_comp_entries(lo, lds->lds_def_comp_cnt);
3779 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
3781 for (i = 0; i < lo->ldo_comp_cnt; i++) {
3782 struct lod_layout_component *obj_comp =
3783 &lo->ldo_comp_entries[i];
3784 struct lod_layout_component *def_comp =
3785 &lds->lds_def_comp_entries[i];
3787 CDEBUG(D_LAYOUT, "Inherite from default: size:%hu "
3788 "nr:%u offset:%u %s\n",
3789 def_comp->llc_stripe_size,
3790 def_comp->llc_stripenr,
3791 def_comp->llc_stripe_offset,
3792 def_comp->llc_pool ?: "");
3794 *obj_comp = *def_comp;
3795 if (def_comp->llc_pool != NULL) {
3796 /* pointer was copied from def_comp */
3797 obj_comp->llc_pool = NULL;
3798 lod_obj_set_pool(lo, i, def_comp->llc_pool);
3802 * Don't initialize these fields for plain layout
3803 * (v1/v3) here, they are inherited in the order of
3804 * 'parent' -> 'fs default (root)' -> 'global default
3805 * values for stripe_count & stripe_size'.
3807 * see lod_ah_init().
3809 if (!lo->ldo_is_composite)
3812 if (obj_comp->llc_stripenr <= 0)
3813 obj_comp->llc_stripenr =
3814 desc->ld_default_stripe_count;
3815 if (obj_comp->llc_stripe_size <= 0)
3816 obj_comp->llc_stripe_size =
3817 desc->ld_default_stripe_size;
3819 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
3820 if (lo->ldo_dir_stripenr == 0)
3821 lo->ldo_dir_stripenr = lds->lds_dir_def_stripenr;
3822 if (lo->ldo_dir_stripe_offset == -1)
3823 lo->ldo_dir_stripe_offset =
3824 lds->lds_dir_def_stripe_offset;
3825 if (lo->ldo_dir_hash_type == 0)
3826 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
3828 CDEBUG(D_LAYOUT, "striping from default dir: nr:%hu, "
3829 "offset:%u, hash_type:%u\n",
3830 lo->ldo_dir_stripenr, lo->ldo_dir_stripe_offset,
3831 lo->ldo_dir_hash_type);
3835 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root)
3837 struct lod_layout_component *lod_comp;
3839 if (lo->ldo_comp_cnt == 0)
3842 if (lo->ldo_is_composite)
3845 lod_comp = &lo->ldo_comp_entries[0];
3847 if (lod_comp->llc_stripenr <= 0 ||
3848 lod_comp->llc_stripe_size <= 0)
3851 if (from_root && (lod_comp->llc_pool == NULL ||
3852 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
3859 * Implementation of dt_object_operations::do_ah_init.
3861 * This method is used to make a decision on the striping configuration for the
3862 * object being created. It can be taken from the \a parent object if it exists,
3863 * or filesystem's default. The resulting configuration (number of stripes,
3864 * stripe size/offset, pool name, etc) is stored in the object itself and will
3865 * be used by the methods like ->doo_declare_create().
3867 * \see dt_object_operations::do_ah_init() in the API description for details.
3869 static void lod_ah_init(const struct lu_env *env,
3870 struct dt_allocation_hint *ah,
3871 struct dt_object *parent,
3872 struct dt_object *child,
3875 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
3876 struct lod_thread_info *info = lod_env_info(env);
3877 struct lod_default_striping *lds = &info->lti_def_striping;
3878 struct dt_object *nextp = NULL;
3879 struct dt_object *nextc;
3880 struct lod_object *lp = NULL;
3881 struct lod_object *lc;
3882 struct lov_desc *desc;
3883 struct lod_layout_component *lod_comp;
3889 if (likely(parent)) {
3890 nextp = dt_object_child(parent);
3891 lp = lod_dt_obj(parent);
3894 nextc = dt_object_child(child);
3895 lc = lod_dt_obj(child);
3897 LASSERT(!lod_obj_is_striped(child));
3898 /* default layout template may have been set on the regular file
3899 * when this is called from mdd_create_data() */
3900 if (S_ISREG(child_mode))
3901 lod_free_comp_entries(lc);
3903 if (!dt_object_exists(nextc))
3904 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
3906 if (S_ISDIR(child_mode)) {
3907 /* other default values are 0 */
3908 lc->ldo_dir_stripe_offset = -1;
3910 /* get default striping from parent object */
3911 if (likely(lp != NULL))
3912 lod_get_default_striping(env, lp, lds);
3914 /* set child default striping info, default value is NULL */
3915 if (lds->lds_def_striping_set || lds->lds_dir_def_striping_set)
3916 lc->ldo_def_striping = lds;
3918 /* It should always honour the specified stripes */
3919 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
3920 lod_verify_md_striping(d, ah->dah_eadata) == 0) {
3921 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
3923 lc->ldo_dir_stripenr =
3924 le32_to_cpu(lum1->lum_stripe_count);
3925 lc->ldo_dir_stripe_offset =
3926 le32_to_cpu(lum1->lum_stripe_offset);
3927 lc->ldo_dir_hash_type =
3928 le32_to_cpu(lum1->lum_hash_type);
3929 CDEBUG(D_INFO, "set dir stripe: count %hu, offset %d, "
3931 lc->ldo_dir_stripenr,
3932 (int)lc->ldo_dir_stripe_offset,
3933 lc->ldo_dir_hash_type);
3935 /* transfer defaults LMV to new directory */
3936 lod_striping_from_default(lc, lds, child_mode);
3939 /* shrink the stripe_count to the avaible MDT count */
3940 if (lc->ldo_dir_stripenr > d->lod_remote_mdt_count + 1 &&
3941 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))
3942 lc->ldo_dir_stripenr = d->lod_remote_mdt_count + 1;
3944 /* Directory will be striped only if stripe_count > 1, if
3945 * stripe_count == 1, let's reset stripenr = 0 to avoid
3946 * create single master stripe and also help to unify the
3947 * stripe handling of directories and files */
3948 if (lc->ldo_dir_stripenr == 1)
3949 lc->ldo_dir_stripenr = 0;
3951 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
3952 lc->ldo_dir_stripenr, (int)lc->ldo_dir_stripe_offset,
3953 lc->ldo_dir_hash_type);
3958 /* child object regular file*/
3960 if (!lod_object_will_be_striped(S_ISREG(child_mode),
3961 lu_object_fid(&child->do_lu)))
3964 /* If object is going to be striped over OSTs, transfer default
3965 * striping information to the child, so that we can use it
3966 * during declaration and creation.
3968 * Try from the parent first.
3970 if (likely(lp != NULL)) {
3971 rc = lod_get_default_lov_striping(env, lp, lds);
3973 lod_striping_from_default(lc, lds, child_mode);
3976 /* Initialize lod_device::lod_md_root object reference */
3977 if (d->lod_md_root == NULL) {
3978 struct dt_object *root;
3979 struct lod_object *lroot;
3981 lu_root_fid(&info->lti_fid);
3982 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
3983 if (!IS_ERR(root)) {
3984 lroot = lod_dt_obj(root);
3986 spin_lock(&d->lod_lock);
3987 if (d->lod_md_root != NULL)
3988 dt_object_put(env, &d->lod_md_root->ldo_obj);
3989 d->lod_md_root = lroot;
3990 spin_unlock(&d->lod_lock);
3994 /* try inherit layout from the root object (fs default) when:
3995 * - parent does not have default layout; or
3996 * - parent has plain(v1/v3) default layout, and some attributes
3997 * are not specified in the default layout;
3999 if (d->lod_md_root != NULL && lod_need_inherit_more(lc, true)) {
4000 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds);
4003 if (lc->ldo_comp_cnt == 0) {
4004 lod_striping_from_default(lc, lds, child_mode);
4005 } else if (!lds->lds_def_striping_is_composite) {
4006 struct lod_layout_component *def_comp;
4008 LASSERT(!lc->ldo_is_composite);
4009 lod_comp = &lc->ldo_comp_entries[0];
4010 def_comp = &lds->lds_def_comp_entries[0];
4012 if (lod_comp->llc_stripenr <= 0)
4013 lod_comp->llc_stripenr = def_comp->llc_stripenr;
4014 if (lod_comp->llc_stripe_size <= 0)
4015 lod_comp->llc_stripe_size =
4016 def_comp->llc_stripe_size;
4017 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
4018 lod_comp->llc_stripe_offset =
4019 def_comp->llc_stripe_offset;
4020 if (lod_comp->llc_pool == NULL)
4021 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
4026 * fs default striping may not be explicitly set, or historically set
4027 * in config log, use them.
4029 if (lod_need_inherit_more(lc, false)) {
4031 if (lc->ldo_comp_cnt == 0) {
4032 rc = lod_alloc_comp_entries(lc, 1);
4034 /* fail to allocate memory, will create a
4035 * non-striped file. */
4037 lc->ldo_is_composite = 0;
4038 lod_comp = &lc->ldo_comp_entries[0];
4039 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4041 LASSERT(!lc->ldo_is_composite);
4042 lod_comp = &lc->ldo_comp_entries[0];
4043 desc = &d->lod_desc;
4044 if (lod_comp->llc_stripenr <= 0)
4045 lod_comp->llc_stripenr = desc->ld_default_stripe_count;
4046 if (lod_comp->llc_stripe_size <= 0)
4047 lod_comp->llc_stripe_size =
4048 desc->ld_default_stripe_size;
4054 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
4056 * Size initialization on late striping.
4058 * Propagate the size of a truncated object to a deferred striping.
4059 * This function handles a special case when truncate was done on a
4060 * non-striped object and now while the striping is being created
4061 * we can't lose that size, so we have to propagate it to the stripes
4064 * \param[in] env execution environment
4065 * \param[in] dt object
4066 * \param[in] th transaction handle
4068 * \retval 0 on success
4069 * \retval negative if failed
4071 static int lod_declare_init_size(const struct lu_env *env,
4072 struct dt_object *dt, struct thandle *th)
4074 struct dt_object *next = dt_object_child(dt);
4075 struct lod_object *lo = lod_dt_obj(dt);
4076 struct dt_object **objects = NULL;
4077 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4078 uint64_t size, offs;
4079 int i, rc, stripe, stripenr = 0, stripe_size = 0;
4082 if (!lod_obj_is_striped(dt))
4085 rc = dt_attr_get(env, next, attr);
4086 LASSERT(attr->la_valid & LA_SIZE);
4090 size = attr->la_size;
4094 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4095 struct lod_layout_component *lod_comp;
4096 struct lu_extent *extent;
4098 lod_comp = &lo->ldo_comp_entries[i];
4100 if (lod_comp->llc_stripe == NULL)
4103 extent = &lod_comp->llc_extent;
4104 CDEBUG(D_INFO, "%lld [%lld, %lld)\n",
4105 size, extent->e_start, extent->e_end);
4106 if (!lo->ldo_is_composite ||
4107 (size >= extent->e_start && size < extent->e_end)) {
4108 objects = lod_comp->llc_stripe;
4109 stripenr = lod_comp->llc_stripenr;
4110 stripe_size = lod_comp->llc_stripe_size;
4118 LASSERT(objects != NULL && stripe_size != 0);
4120 /* ll_do_div64(a, b) returns a % b, and a = a / b */
4121 ll_do_div64(size, (__u64)stripe_size);
4122 stripe = ll_do_div64(size, (__u64)stripenr);
4123 LASSERT(objects[stripe] != NULL);
4125 size = size * stripe_size;
4126 offs = attr->la_size;
4127 size += ll_do_div64(offs, stripe_size);
4129 attr->la_valid = LA_SIZE;
4130 attr->la_size = size;
4132 rc = lod_sub_object_declare_attr_set(env, objects[stripe], attr, th);
4138 * Declare creation of striped object.
4140 * The function declares creation stripes for a regular object. The function
4141 * also declares whether the stripes will be created with non-zero size if
4142 * previously size was set non-zero on the master object. If object \a dt is
4143 * not local, then only fully defined striping can be applied in \a lovea.
4144 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
4147 * \param[in] env execution environment
4148 * \param[in] dt object
4149 * \param[in] attr attributes the stripes will be created with
4150 * \param[in] lovea a buffer containing striping description
4151 * \param[in] th transaction handle
4153 * \retval 0 on success
4154 * \retval negative if failed
4156 int lod_declare_striped_object(const struct lu_env *env, struct dt_object *dt,
4157 struct lu_attr *attr,
4158 const struct lu_buf *lovea, struct thandle *th)
4160 struct lod_thread_info *info = lod_env_info(env);
4161 struct dt_object *next = dt_object_child(dt);
4162 struct lod_object *lo = lod_dt_obj(dt);
4166 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
4167 GOTO(out, rc = -ENOMEM);
4169 if (!dt_object_remote(next)) {
4170 /* choose OST and generate appropriate objects */
4171 rc = lod_prepare_create(env, lo, attr, lovea, th);
4176 * declare storage for striping data
4178 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
4180 /* LOD can not choose OST objects for remote objects, i.e.
4181 * stripes must be ready before that. Right now, it can only
4182 * happen during migrate, i.e. migrate process needs to create
4183 * remote regular file (mdd_migrate_create), then the migrate
4184 * process will provide stripeEA. */
4185 LASSERT(lovea != NULL);
4186 info->lti_buf = *lovea;
4189 rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
4190 XATTR_NAME_LOV, 0, th);
4195 * if striping is created with local object's size > 0,
4196 * we have to propagate this size to specific object
4197 * the case is possible only when local object was created previously
4199 if (dt_object_exists(next))
4200 rc = lod_declare_init_size(env, dt, th);
4203 /* failed to create striping or to set initial size, let's reset
4204 * config so that others don't get confused */
4206 lod_object_free_striping(env, lo);
4212 * Implementation of dt_object_operations::do_declare_create.
4214 * The method declares creation of a new object. If the object will be striped,
4215 * then helper functions are called to find FIDs for the stripes, declare
4216 * creation of the stripes and declare initialization of the striping
4217 * information to be stored in the master object.
4219 * \see dt_object_operations::do_declare_create() in the API description
4222 static int lod_declare_object_create(const struct lu_env *env,
4223 struct dt_object *dt,
4224 struct lu_attr *attr,
4225 struct dt_allocation_hint *hint,
4226 struct dt_object_format *dof,
4229 struct dt_object *next = dt_object_child(dt);
4230 struct lod_object *lo = lod_dt_obj(dt);
4239 * first of all, we declare creation of local object
4241 rc = lod_sub_object_declare_create(env, next, attr, hint, dof, th);
4245 if (dof->dof_type == DFT_SYM)
4246 dt->do_body_ops = &lod_body_lnk_ops;
4247 else if (dof->dof_type == DFT_REGULAR)
4248 dt->do_body_ops = &lod_body_ops;
4251 * it's lod_ah_init() that has decided the object will be striped
4253 if (dof->dof_type == DFT_REGULAR) {
4254 /* callers don't want stripes */
4255 /* XXX: all tricky interactions with ->ah_make_hint() decided
4256 * to use striping, then ->declare_create() behaving differently
4257 * should be cleaned */
4258 if (dof->u.dof_reg.striped != 0)
4259 rc = lod_declare_striped_object(env, dt, attr,
4261 } else if (dof->dof_type == DFT_DIR) {
4262 struct seq_server_site *ss;
4264 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
4266 /* If the parent has default stripeEA, and client
4267 * did not find it before sending create request,
4268 * then MDT will return -EREMOTE, and client will
4269 * retrieve the default stripeEA and re-create the
4272 * Note: if dah_eadata != NULL, it means creating the
4273 * striped directory with specified stripeEA, then it
4274 * should ignore the default stripeEA */
4275 if (hint != NULL && hint->dah_eadata == NULL) {
4276 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
4277 GOTO(out, rc = -EREMOTE);
4279 if (lo->ldo_dir_stripe_offset == -1) {
4280 /* child and parent should be in the same MDT */
4281 if (hint->dah_parent != NULL &&
4282 dt_object_remote(hint->dah_parent))
4283 GOTO(out, rc = -EREMOTE);
4284 } else if (lo->ldo_dir_stripe_offset !=
4286 struct lod_device *lod;
4287 struct lod_tgt_descs *ltd;
4288 struct lod_tgt_desc *tgt = NULL;
4289 bool found_mdt = false;
4292 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4293 ltd = &lod->lod_mdt_descs;
4294 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
4295 tgt = LTD_TGT(ltd, i);
4296 if (tgt->ltd_index ==
4297 lo->ldo_dir_stripe_offset) {
4303 /* If the MDT indicated by stripe_offset can be
4304 * found, then tell client to resend the create
4305 * request to the correct MDT, otherwise return
4306 * error to client */
4308 GOTO(out, rc = -EREMOTE);
4310 GOTO(out, rc = -EINVAL);
4314 rc = lod_declare_dir_striping_create(env, dt, attr, dof, th);
4317 /* failed to create striping or to set initial size, let's reset
4318 * config so that others don't get confused */
4320 lod_object_free_striping(env, lo);
4325 * Creation of a striped regular object.
4327 * The function is called to create the stripe objects for a regular
4328 * striped file. This can happen at the initial object creation or
4329 * when the caller asks LOD to do so using ->do_xattr_set() method
4330 * (so called late striping). Notice all the information are already
4331 * prepared in the form of the list of objects (ldo_stripe field).
4332 * This is done during declare phase.
4334 * \param[in] env execution environment
4335 * \param[in] dt object
4336 * \param[in] attr attributes the stripes will be created with
4337 * \param[in] dof format of stripes (see OSD API description)
4338 * \param[in] th transaction handle
4340 * \retval 0 on success
4341 * \retval negative if failed
4343 int lod_striping_create(const struct lu_env *env, struct dt_object *dt,
4344 struct lu_attr *attr, struct dt_object_format *dof,
4347 struct lod_layout_component *lod_comp;
4348 struct lod_object *lo = lod_dt_obj(dt);
4352 LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
4354 /* create all underlying objects */
4355 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4356 lod_comp = &lo->ldo_comp_entries[i];
4358 if (lod_comp_inited(lod_comp))
4361 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
4362 lod_comp_set_init(lod_comp);
4364 if (lod_comp->llc_stripe == NULL)
4367 LASSERT(lod_comp->llc_stripenr);
4368 for (j = 0; j < lod_comp->llc_stripenr; j++) {
4369 struct dt_object *object = lod_comp->llc_stripe[j];
4370 LASSERT(object != NULL);
4371 rc = lod_sub_object_create(env, object, attr, NULL,
4376 lod_comp_set_init(lod_comp);
4380 rc = lod_generate_and_set_lovea(env, lo, th);
4383 lo->ldo_comp_cached = 1;
4385 lod_object_free_striping(env, lo);
4391 * Implementation of dt_object_operations::do_create.
4393 * If any of preceeding methods (like ->do_declare_create(),
4394 * ->do_ah_init(), etc) chose to create a striped object,
4395 * then this method will create the master and the stripes.
4397 * \see dt_object_operations::do_create() in the API description for details.
4399 static int lod_object_create(const struct lu_env *env, struct dt_object *dt,
4400 struct lu_attr *attr,
4401 struct dt_allocation_hint *hint,
4402 struct dt_object_format *dof, struct thandle *th)
4407 /* create local object */
4408 rc = lod_sub_object_create(env, dt_object_child(dt), attr, hint, dof,
4413 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4414 lod_obj_is_striped(dt) && dof->u.dof_reg.striped != 0) {
4415 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
4416 rc = lod_striping_create(env, dt, attr, dof, th);
4423 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
4424 struct dt_object *dt, struct thandle *th,
4425 int stripe_idx, struct lod_obj_stripe_cb_data *data)
4427 if (data->locd_declare)
4428 return lod_sub_object_declare_destroy(env, dt, th);
4429 else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4430 stripe_idx == cfs_fail_val)
4431 return lod_sub_object_destroy(env, dt, th);
4437 * Implementation of dt_object_operations::do_declare_destroy.
4439 * If the object is a striped directory, then the function declares reference
4440 * removal from the master object (this is an index) to the stripes and declares
4441 * destroy of all the stripes. In all the cases, it declares an intention to
4442 * destroy the object itself.
4444 * \see dt_object_operations::do_declare_destroy() in the API description
4447 static int lod_declare_object_destroy(const struct lu_env *env,
4448 struct dt_object *dt,
4451 struct dt_object *next = dt_object_child(dt);
4452 struct lod_object *lo = lod_dt_obj(dt);
4453 struct lod_thread_info *info = lod_env_info(env);
4454 char *stripe_name = info->lti_key;
4459 * load striping information, notice we don't do this when object
4460 * is being initialized as we don't need this information till
4461 * few specific cases like destroy, chown
4463 rc = lod_load_striping(env, lo);
4467 /* declare destroy for all underlying objects */
4468 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4469 rc = next->do_ops->do_index_try(env, next,
4470 &dt_directory_features);
4474 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4475 rc = lod_sub_object_declare_ref_del(env, next, th);
4479 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4480 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4482 rc = lod_sub_object_declare_delete(env, next,
4483 (const struct dt_key *)stripe_name, th);
4490 * we declare destroy for the local object
4492 rc = lod_sub_object_declare_destroy(env, next, th);
4496 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4497 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4500 if (!lod_obj_is_striped(dt))
4503 /* declare destroy all striped objects */
4504 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4505 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4506 if (lo->ldo_stripe[i] == NULL)
4509 rc = lod_sub_object_declare_ref_del(env,
4510 lo->ldo_stripe[i], th);
4512 rc = lod_sub_object_declare_destroy(env,
4513 lo->ldo_stripe[i], th);
4518 struct lod_obj_stripe_cb_data data;
4520 data.locd_declare = true;
4521 rc = lod_obj_for_each_stripe(env, lo, th,
4522 lod_obj_stripe_destroy_cb, &data);
4529 * Implementation of dt_object_operations::do_destroy.
4531 * If the object is a striped directory, then the function removes references
4532 * from the master object (this is an index) to the stripes and destroys all
4533 * the stripes. In all the cases, the function destroys the object itself.
4535 * \see dt_object_operations::do_destroy() in the API description for details.
4537 static int lod_object_destroy(const struct lu_env *env,
4538 struct dt_object *dt, struct thandle *th)
4540 struct dt_object *next = dt_object_child(dt);
4541 struct lod_object *lo = lod_dt_obj(dt);
4542 struct lod_thread_info *info = lod_env_info(env);
4543 char *stripe_name = info->lti_key;
4548 /* destroy sub-stripe of master object */
4549 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4550 rc = next->do_ops->do_index_try(env, next,
4551 &dt_directory_features);
4555 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4556 rc = lod_sub_object_ref_del(env, next, th);
4560 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4561 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4564 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
4565 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
4566 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
4568 rc = lod_sub_object_delete(env, next,
4569 (const struct dt_key *)stripe_name, th);
4575 rc = lod_sub_object_destroy(env, next, th);
4579 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4580 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4583 if (!lod_obj_is_striped(dt))
4586 /* destroy all striped objects */
4587 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4588 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4589 if (lo->ldo_stripe[i] == NULL)
4591 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4592 i == cfs_fail_val) {
4593 dt_write_lock(env, lo->ldo_stripe[i],
4595 rc = lod_sub_object_ref_del(env,
4596 lo->ldo_stripe[i], th);
4597 dt_write_unlock(env, lo->ldo_stripe[i]);
4601 rc = lod_sub_object_destroy(env,
4602 lo->ldo_stripe[i], th);
4608 struct lod_obj_stripe_cb_data data;
4610 data.locd_declare = false;
4611 rc = lod_obj_for_each_stripe(env, lo, th,
4612 lod_obj_stripe_destroy_cb, &data);
4619 * Implementation of dt_object_operations::do_declare_ref_add.
4621 * \see dt_object_operations::do_declare_ref_add() in the API description
4624 static int lod_declare_ref_add(const struct lu_env *env,
4625 struct dt_object *dt, struct thandle *th)
4627 return lod_sub_object_declare_ref_add(env, dt_object_child(dt), th);
4631 * Implementation of dt_object_operations::do_ref_add.
4633 * \see dt_object_operations::do_ref_add() in the API description for details.
4635 static int lod_ref_add(const struct lu_env *env,
4636 struct dt_object *dt, struct thandle *th)
4638 return lod_sub_object_ref_add(env, dt_object_child(dt), th);
4642 * Implementation of dt_object_operations::do_declare_ref_del.
4644 * \see dt_object_operations::do_declare_ref_del() in the API description
4647 static int lod_declare_ref_del(const struct lu_env *env,
4648 struct dt_object *dt, struct thandle *th)
4650 return lod_sub_object_declare_ref_del(env, dt_object_child(dt), th);
4654 * Implementation of dt_object_operations::do_ref_del
4656 * \see dt_object_operations::do_ref_del() in the API description for details.
4658 static int lod_ref_del(const struct lu_env *env,
4659 struct dt_object *dt, struct thandle *th)
4661 return lod_sub_object_ref_del(env, dt_object_child(dt), th);
4665 * Implementation of dt_object_operations::do_object_sync.
4667 * \see dt_object_operations::do_object_sync() in the API description
4670 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
4671 __u64 start, __u64 end)
4673 return dt_object_sync(env, dt_object_child(dt), start, end);
4677 * Release LDLM locks on the stripes of a striped directory.
4679 * Iterates over all the locks taken on the stripe objects and
4682 * \param[in] env execution environment
4683 * \param[in] dt striped object
4684 * \param[in] einfo lock description
4685 * \param[in] policy data describing requested lock
4687 * \retval 0 on success
4688 * \retval negative if failed
4690 static int lod_object_unlock_internal(const struct lu_env *env,
4691 struct dt_object *dt,
4692 struct ldlm_enqueue_info *einfo,
4693 union ldlm_policy_data *policy)
4695 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
4700 if (slave_locks == NULL)
4703 for (i = 1; i < slave_locks->count; i++) {
4704 if (lustre_handle_is_used(&slave_locks->handles[i]))
4705 ldlm_lock_decref_and_cancel(&slave_locks->handles[i],
4713 * Implementation of dt_object_operations::do_object_unlock.
4715 * Used to release LDLM lock(s).
4717 * \see dt_object_operations::do_object_unlock() in the API description
4720 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
4721 struct ldlm_enqueue_info *einfo,
4722 union ldlm_policy_data *policy)
4724 struct lod_object *lo = lod_dt_obj(dt);
4725 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
4726 int slave_locks_size;
4730 if (slave_locks == NULL)
4733 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
4734 LASSERT(lo->ldo_dir_stripenr > 1);
4735 /* Note: for remote lock for single stripe dir, MDT will cancel
4736 * the lock by lockh directly */
4737 LASSERT(!dt_object_remote(dt_object_child(dt)));
4739 /* locks were unlocked in MDT layer */
4740 for (i = 1; i < slave_locks->count; i++) {
4741 LASSERT(!lustre_handle_is_used(&slave_locks->handles[i]));
4742 dt_invalidate(env, lo->ldo_stripe[i]);
4745 slave_locks_size = sizeof(*slave_locks) + slave_locks->count *
4746 sizeof(slave_locks->handles[0]);
4747 OBD_FREE(slave_locks, slave_locks_size);
4748 einfo->ei_cbdata = NULL;
4754 * Implementation of dt_object_operations::do_object_lock.
4756 * Used to get LDLM lock on the non-striped and striped objects.
4758 * \see dt_object_operations::do_object_lock() in the API description
4761 static int lod_object_lock(const struct lu_env *env,
4762 struct dt_object *dt,
4763 struct lustre_handle *lh,
4764 struct ldlm_enqueue_info *einfo,
4765 union ldlm_policy_data *policy)
4767 struct lod_object *lo = lod_dt_obj(dt);
4770 int slave_locks_size;
4771 struct lustre_handle_array *slave_locks = NULL;
4774 /* remote object lock */
4775 if (!einfo->ei_enq_slave) {
4776 LASSERT(dt_object_remote(dt));
4777 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
4781 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4782 GOTO(out, rc = -ENOTDIR);
4784 rc = lod_load_striping(env, lo);
4789 if (lo->ldo_dir_stripenr <= 1) {
4791 * NB, ei_cbdata stores pointer to slave locks, if no locks
4792 * taken, make sure it's set to NULL, otherwise MDT will try to
4795 einfo->ei_cbdata = NULL;
4799 slave_locks_size = sizeof(*slave_locks) + lo->ldo_dir_stripenr *
4800 sizeof(slave_locks->handles[0]);
4801 /* Freed in lod_object_unlock */
4802 OBD_ALLOC(slave_locks, slave_locks_size);
4803 if (slave_locks == NULL)
4804 GOTO(out, rc = -ENOMEM);
4805 slave_locks->count = lo->ldo_dir_stripenr;
4807 /* striped directory lock */
4808 for (i = 1; i < lo->ldo_dir_stripenr; i++) {
4809 struct lustre_handle lockh;
4810 struct ldlm_res_id *res_id;
4812 res_id = &lod_env_info(env)->lti_res_id;
4813 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
4815 einfo->ei_res_id = res_id;
4817 LASSERT(lo->ldo_stripe[i] != NULL);
4818 if (likely(dt_object_remote(lo->ldo_stripe[i]))) {
4819 rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh,
4822 struct ldlm_namespace *ns = einfo->ei_namespace;
4823 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
4824 ldlm_completion_callback completion = einfo->ei_cb_cp;
4825 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
4827 if (einfo->ei_mode == LCK_PW ||
4828 einfo->ei_mode == LCK_EX)
4829 dlmflags |= LDLM_FL_COS_INCOMPAT;
4831 /* This only happens if there are mulitple stripes
4832 * on the master MDT, i.e. except stripe0, there are
4833 * other stripes on the Master MDT as well, Only
4834 * happens in the test case right now. */
4835 LASSERT(ns != NULL);
4836 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS,
4837 policy, einfo->ei_mode,
4838 &dlmflags, blocking,
4840 NULL, 0, LVB_T_NONE,
4845 slave_locks->handles[i] = lockh;
4847 einfo->ei_cbdata = slave_locks;
4849 if (rc != 0 && slave_locks != NULL) {
4850 lod_object_unlock_internal(env, dt, einfo, policy);
4851 OBD_FREE(slave_locks, slave_locks_size);
4856 einfo->ei_cbdata = NULL;
4861 * Implementation of dt_object_operations::do_invalidate.
4863 * \see dt_object_operations::do_invalidate() in the API description for details
4865 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
4867 return dt_invalidate(env, dt_object_child(dt));
4870 static int lod_declare_layout_change(const struct lu_env *env,
4871 struct dt_object *dt,
4872 struct layout_intent *layout,
4873 const struct lu_buf *buf,
4876 struct lod_thread_info *info = lod_env_info(env);
4877 struct lod_object *lo = lod_dt_obj(dt);
4878 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
4879 struct dt_object *next = dt_object_child(dt);
4880 struct ost_pool *inuse = &info->lti_inuse_osts;
4881 struct lod_layout_component *lod_comp;
4882 struct lov_comp_md_v1 *comp_v1 = NULL;
4883 bool replay = false;
4884 bool need_create = false;
4888 if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
4889 dt_object_remote(next))
4892 dt_write_lock(env, next, 0);
4894 * In case the client is passing lovea, which only happens during
4895 * the replay of layout intent write RPC for now, we may need to
4896 * parse the lovea and apply new layout configuration.
4898 if (buf && buf->lb_len) {
4899 struct lov_user_md_v1 *v1 = buf->lb_buf;
4901 if (v1->lmm_magic != (LOV_MAGIC_DEF | LOV_MAGIC_COMP_V1) &&
4903 __swab32(LOV_MAGIC_DEF | LOV_MAGIC_COMP_V1)) {
4904 CERROR("%s: the replay buffer of layout extend "
4905 "(magic %#x) does not contain expected "
4906 "composite layout.\n",
4907 lod2obd(d)->obd_name, v1->lmm_magic);
4908 GOTO(out, rc = -EINVAL);
4911 lod_object_free_striping(env, lo);
4912 rc = lod_use_defined_striping(env, lo, buf);
4916 rc = lod_get_lov_ea(env, lo);
4919 /* old on-disk EA is stored in info->lti_buf */
4920 comp_v1 = (struct lov_comp_md_v1 *)&info->lti_buf.lb_buf;
4923 /* non replay path */
4924 rc = lod_load_striping_locked(env, lo);
4928 /* Prepare inuse array for composite file */
4929 rc = lod_prepare_inuse(env, lo);
4934 /* Make sure defined layout covers the requested write range. */
4935 lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
4936 if (lo->ldo_comp_cnt > 1 &&
4937 lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
4938 lod_comp->llc_extent.e_end < layout->li_end) {
4939 CDEBUG(replay ? D_ERROR : D_LAYOUT,
4940 "%s: the defined layout [0, %#llx) does not covers "
4941 "the write range [%#llx, %#llx).\n",
4942 lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
4943 layout->li_start, layout->li_end);
4944 GOTO(out, rc = -EINVAL);
4948 * Iterate ld->ldo_comp_entries, find the component whose extent under
4949 * the write range and not instantianted.
4951 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4952 lod_comp = &lo->ldo_comp_entries[i];
4954 if (lod_comp->llc_extent.e_start >= layout->li_end)
4958 if (lod_comp_inited(lod_comp))
4962 * In replay path, lod_comp is the EA passed by
4963 * client replay buffer, comp_v1 is the pre-recovery
4964 * on-disk EA, we'd sift out those components which
4965 * were init-ed in the on-disk EA.
4967 if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
4972 * this component hasn't instantiated in normal path, or during
4973 * replay it needs replay the instantiation.
4976 /* A released component is being extended */
4977 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
4978 GOTO(out, rc = -EINVAL);
4982 rc = lod_qos_prep_create(env, lo, NULL, th, i, inuse);
4988 lod_obj_inc_layout_gen(lo);
4990 GOTO(unlock, rc = -EALREADY);
4993 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
4994 rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
4995 XATTR_NAME_LOV, 0, th);
4999 lod_object_free_striping(env, lo);
5002 dt_write_unlock(env, next);
5008 * Instantiate layout component objects which covers the intent write offset.
5010 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
5011 struct layout_intent *layout,
5012 const struct lu_buf *buf, struct thandle *th)
5014 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
5016 RETURN(lod_striping_create(env, dt, attr, NULL, th));
5019 struct dt_object_operations lod_obj_ops = {
5020 .do_read_lock = lod_object_read_lock,
5021 .do_write_lock = lod_object_write_lock,
5022 .do_read_unlock = lod_object_read_unlock,
5023 .do_write_unlock = lod_object_write_unlock,
5024 .do_write_locked = lod_object_write_locked,
5025 .do_attr_get = lod_attr_get,
5026 .do_declare_attr_set = lod_declare_attr_set,
5027 .do_attr_set = lod_attr_set,
5028 .do_xattr_get = lod_xattr_get,
5029 .do_declare_xattr_set = lod_declare_xattr_set,
5030 .do_xattr_set = lod_xattr_set,
5031 .do_declare_xattr_del = lod_declare_xattr_del,
5032 .do_xattr_del = lod_xattr_del,
5033 .do_xattr_list = lod_xattr_list,
5034 .do_ah_init = lod_ah_init,
5035 .do_declare_create = lod_declare_object_create,
5036 .do_create = lod_object_create,
5037 .do_declare_destroy = lod_declare_object_destroy,
5038 .do_destroy = lod_object_destroy,
5039 .do_index_try = lod_index_try,
5040 .do_declare_ref_add = lod_declare_ref_add,
5041 .do_ref_add = lod_ref_add,
5042 .do_declare_ref_del = lod_declare_ref_del,
5043 .do_ref_del = lod_ref_del,
5044 .do_object_sync = lod_object_sync,
5045 .do_object_lock = lod_object_lock,
5046 .do_object_unlock = lod_object_unlock,
5047 .do_invalidate = lod_invalidate,
5048 .do_declare_layout_change = lod_declare_layout_change,
5049 .do_layout_change = lod_layout_change,
5053 * Implementation of dt_body_operations::dbo_read.
5055 * \see dt_body_operations::dbo_read() in the API description for details.
5057 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
5058 struct lu_buf *buf, loff_t *pos)
5060 struct dt_object *next = dt_object_child(dt);
5061 return next->do_body_ops->dbo_read(env, next, buf, pos);
5065 * Implementation of dt_body_operations::dbo_declare_write.
5067 * \see dt_body_operations::dbo_declare_write() in the API description
5070 static ssize_t lod_declare_write(const struct lu_env *env,
5071 struct dt_object *dt,
5072 const struct lu_buf *buf, loff_t pos,
5075 return lod_sub_object_declare_write(env, dt_object_child(dt), buf, pos,
5080 * Implementation of dt_body_operations::dbo_write.
5082 * \see dt_body_operations::dbo_write() in the API description for details.
5084 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
5085 const struct lu_buf *buf, loff_t *pos,
5086 struct thandle *th, int iq)
5088 return lod_sub_object_write(env, dt_object_child(dt), buf, pos, th, iq);
5091 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
5092 __u64 start, __u64 end, struct thandle *th)
5094 if (dt_object_remote(dt))
5097 return lod_sub_object_declare_punch(env, dt_object_child(dt), start,
5101 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
5102 __u64 start, __u64 end, struct thandle *th)
5104 if (dt_object_remote(dt))
5107 return lod_sub_object_punch(env, dt_object_child(dt), start, end, th);
5110 static const struct dt_body_operations lod_body_lnk_ops = {
5111 .dbo_read = lod_read,
5112 .dbo_declare_write = lod_declare_write,
5113 .dbo_write = lod_write
5116 static const struct dt_body_operations lod_body_ops = {
5117 .dbo_read = lod_read,
5118 .dbo_declare_write = lod_declare_write,
5119 .dbo_write = lod_write,
5120 .dbo_declare_punch = lod_declare_punch,
5121 .dbo_punch = lod_punch,
5125 * Implementation of lu_object_operations::loo_object_init.
5127 * The function determines the type and the index of the target device using
5128 * sequence of the object's FID. Then passes control down to the
5129 * corresponding device:
5130 * OSD for the local objects, OSP for remote
5132 * \see lu_object_operations::loo_object_init() in the API description
5135 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
5136 const struct lu_object_conf *conf)
5138 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
5139 struct lu_device *cdev = NULL;
5140 struct lu_object *cobj;
5141 struct lod_tgt_descs *ltd = NULL;
5142 struct lod_tgt_desc *tgt;
5144 int type = LU_SEQ_RANGE_ANY;
5148 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
5150 /* Note: Sometimes, it will Return EAGAIN here, see
5151 * ptrlpc_import_delay_req(), which might confuse
5152 * lu_object_find_at() and make it wait there incorrectly.
5153 * so we convert it to EIO here.*/
5160 if (type == LU_SEQ_RANGE_MDT &&
5161 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
5162 cdev = &lod->lod_child->dd_lu_dev;
5163 } else if (type == LU_SEQ_RANGE_MDT) {
5164 ltd = &lod->lod_mdt_descs;
5166 } else if (type == LU_SEQ_RANGE_OST) {
5167 ltd = &lod->lod_ost_descs;
5174 if (ltd->ltd_tgts_size > idx &&
5175 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
5176 tgt = LTD_TGT(ltd, idx);
5178 LASSERT(tgt != NULL);
5179 LASSERT(tgt->ltd_tgt != NULL);
5181 cdev = &(tgt->ltd_tgt->dd_lu_dev);
5183 lod_putref(lod, ltd);
5186 if (unlikely(cdev == NULL))
5189 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
5190 if (unlikely(cobj == NULL))
5193 lu_object_add(lo, cobj);
5200 * Release resources associated with striping.
5202 * If the object is striped (regular or directory), then release
5203 * the stripe objects references and free the ldo_stripe array.
5205 * \param[in] env execution environment
5206 * \param[in] lo object
5208 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
5210 struct lod_layout_component *lod_comp;
5213 if (lo->ldo_stripe != NULL) {
5214 LASSERT(lo->ldo_comp_entries == NULL);
5215 LASSERT(lo->ldo_dir_stripes_allocated > 0);
5217 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
5218 if (lo->ldo_stripe[i])
5219 dt_object_put(env, lo->ldo_stripe[i]);
5222 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
5223 OBD_FREE(lo->ldo_stripe, j);
5224 lo->ldo_stripe = NULL;
5225 lo->ldo_dir_stripes_allocated = 0;
5226 lo->ldo_dir_stripenr = 0;
5227 } else if (lo->ldo_comp_entries != NULL) {
5228 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5229 /* free lod_layout_component::llc_stripe array */
5230 lod_comp = &lo->ldo_comp_entries[i];
5232 if (lod_comp->llc_stripe == NULL)
5234 LASSERT(lod_comp->llc_stripes_allocated != 0);
5235 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
5236 if (lod_comp->llc_stripe[j] != NULL)
5238 &lod_comp->llc_stripe[j]->do_lu);
5240 OBD_FREE(lod_comp->llc_stripe,
5241 sizeof(struct dt_object *) *
5242 lod_comp->llc_stripes_allocated);
5243 lod_comp->llc_stripe = NULL;
5244 lod_comp->llc_stripes_allocated = 0;
5246 lod_free_comp_entries(lo);
5247 lo->ldo_comp_cached = 0;
5252 * Implementation of lu_object_operations::loo_object_start.
5254 * \see lu_object_operations::loo_object_start() in the API description
5257 static int lod_object_start(const struct lu_env *env, struct lu_object *o)
5259 if (S_ISLNK(o->lo_header->loh_attr & S_IFMT)) {
5260 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_lnk_ops;
5261 } else if (S_ISREG(o->lo_header->loh_attr & S_IFMT) ||
5262 fid_is_local_file(lu_object_fid(o))) {
5263 /* Note: some local file (like last rcvd) is created
5264 * through bottom layer (OSD), so the object initialization
5265 * comes to lod, it does not set loh_attr yet, so
5266 * set do_body_ops for local file anyway */
5267 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_ops;
5273 * Implementation of lu_object_operations::loo_object_free.
5275 * \see lu_object_operations::loo_object_free() in the API description
5278 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
5280 struct lod_object *lo = lu2lod_obj(o);
5282 /* release all underlying object pinned */
5283 lod_object_free_striping(env, lo);
5285 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
5289 * Implementation of lu_object_operations::loo_object_release.
5291 * \see lu_object_operations::loo_object_release() in the API description
5294 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
5296 /* XXX: shouldn't we release everything here in case if object
5297 * creation failed before? */
5301 * Implementation of lu_object_operations::loo_object_print.
5303 * \see lu_object_operations::loo_object_print() in the API description
5306 static int lod_object_print(const struct lu_env *env, void *cookie,
5307 lu_printer_t p, const struct lu_object *l)
5309 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
5311 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
5314 struct lu_object_operations lod_lu_obj_ops = {
5315 .loo_object_init = lod_object_init,
5316 .loo_object_start = lod_object_start,
5317 .loo_object_free = lod_object_free,
5318 .loo_object_release = lod_object_release,
5319 .loo_object_print = lod_object_print,