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, 2017, 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
43 #include <linux/random.h>
46 #include <obd_class.h>
47 #include <obd_support.h>
49 #include <lustre_fid.h>
50 #include <lustre_linkea.h>
51 #include <lustre_lmv.h>
52 #include <uapi/linux/lustre/lustre_param.h>
53 #include <lustre_swab.h>
54 #include <uapi/linux/lustre/lustre_ver.h>
55 #include <lprocfs_status.h>
56 #include <md_object.h>
58 #include "lod_internal.h"
60 static const char dot[] = ".";
61 static const char dotdot[] = "..";
64 * Implementation of dt_index_operations::dio_lookup
66 * Used with regular (non-striped) objects.
68 * \see dt_index_operations::dio_lookup() in the API description for details.
70 static int lod_lookup(const struct lu_env *env, struct dt_object *dt,
71 struct dt_rec *rec, const struct dt_key *key)
73 struct dt_object *next = dt_object_child(dt);
74 return next->do_index_ops->dio_lookup(env, next, rec, key);
78 * Implementation of dt_index_operations::dio_declare_insert.
80 * Used with regular (non-striped) objects.
82 * \see dt_index_operations::dio_declare_insert() in the API description
85 static int lod_declare_insert(const struct lu_env *env, struct dt_object *dt,
86 const struct dt_rec *rec,
87 const struct dt_key *key, struct thandle *th)
89 return lod_sub_declare_insert(env, dt_object_child(dt), rec, key, th);
93 * Implementation of dt_index_operations::dio_insert.
95 * Used with regular (non-striped) objects
97 * \see dt_index_operations::dio_insert() in the API description for details.
99 static int lod_insert(const struct lu_env *env, struct dt_object *dt,
100 const struct dt_rec *rec, const struct dt_key *key,
103 return lod_sub_insert(env, dt_object_child(dt), rec, key, th);
107 * Implementation of dt_index_operations::dio_declare_delete.
109 * Used with regular (non-striped) objects.
111 * \see dt_index_operations::dio_declare_delete() in the API description
114 static int lod_declare_delete(const struct lu_env *env, struct dt_object *dt,
115 const struct dt_key *key, struct thandle *th)
117 return lod_sub_declare_delete(env, dt_object_child(dt), key, th);
121 * Implementation of dt_index_operations::dio_delete.
123 * Used with regular (non-striped) objects.
125 * \see dt_index_operations::dio_delete() in the API description for details.
127 static int lod_delete(const struct lu_env *env, struct dt_object *dt,
128 const struct dt_key *key, struct thandle *th)
130 return lod_sub_delete(env, dt_object_child(dt), key, th);
134 * Implementation of dt_it_ops::init.
136 * Used with regular (non-striped) objects.
138 * \see dt_it_ops::init() in the API description for details.
140 static struct dt_it *lod_it_init(const struct lu_env *env,
141 struct dt_object *dt, __u32 attr)
143 struct dt_object *next = dt_object_child(dt);
144 struct lod_it *it = &lod_env_info(env)->lti_it;
145 struct dt_it *it_next;
147 it_next = next->do_index_ops->dio_it.init(env, next, attr);
151 /* currently we do not use more than one iterator per thread
152 * so we store it in thread info. if at some point we need
153 * more active iterators in a single thread, we can allocate
155 LASSERT(it->lit_obj == NULL);
157 it->lit_it = it_next;
160 return (struct dt_it *)it;
163 #define LOD_CHECK_IT(env, it) \
165 LASSERT((it)->lit_obj != NULL); \
166 LASSERT((it)->lit_it != NULL); \
170 * Implementation of dt_index_operations::dio_it.fini.
172 * Used with regular (non-striped) objects.
174 * \see dt_index_operations::dio_it.fini() in the API description for details.
176 static void lod_it_fini(const struct lu_env *env, struct dt_it *di)
178 struct lod_it *it = (struct lod_it *)di;
180 LOD_CHECK_IT(env, it);
181 it->lit_obj->do_index_ops->dio_it.fini(env, it->lit_it);
183 /* the iterator not in use any more */
189 * Implementation of dt_it_ops::get.
191 * Used with regular (non-striped) objects.
193 * \see dt_it_ops::get() in the API description for details.
195 static int lod_it_get(const struct lu_env *env, struct dt_it *di,
196 const struct dt_key *key)
198 const struct lod_it *it = (const struct lod_it *)di;
200 LOD_CHECK_IT(env, it);
201 return it->lit_obj->do_index_ops->dio_it.get(env, it->lit_it, key);
205 * Implementation of dt_it_ops::put.
207 * Used with regular (non-striped) objects.
209 * \see dt_it_ops::put() in the API description for details.
211 static void lod_it_put(const struct lu_env *env, struct dt_it *di)
213 struct lod_it *it = (struct lod_it *)di;
215 LOD_CHECK_IT(env, it);
216 return it->lit_obj->do_index_ops->dio_it.put(env, it->lit_it);
220 * Implementation of dt_it_ops::next.
222 * Used with regular (non-striped) objects
224 * \see dt_it_ops::next() in the API description for details.
226 static int lod_it_next(const struct lu_env *env, struct dt_it *di)
228 struct lod_it *it = (struct lod_it *)di;
230 LOD_CHECK_IT(env, it);
231 return it->lit_obj->do_index_ops->dio_it.next(env, it->lit_it);
235 * Implementation of dt_it_ops::key.
237 * Used with regular (non-striped) objects.
239 * \see dt_it_ops::key() in the API description for details.
241 static struct dt_key *lod_it_key(const struct lu_env *env,
242 const struct dt_it *di)
244 const struct lod_it *it = (const struct lod_it *)di;
246 LOD_CHECK_IT(env, it);
247 return it->lit_obj->do_index_ops->dio_it.key(env, it->lit_it);
251 * Implementation of dt_it_ops::key_size.
253 * Used with regular (non-striped) objects.
255 * \see dt_it_ops::key_size() in the API description for details.
257 static int lod_it_key_size(const struct lu_env *env, const struct dt_it *di)
259 struct lod_it *it = (struct lod_it *)di;
261 LOD_CHECK_IT(env, it);
262 return it->lit_obj->do_index_ops->dio_it.key_size(env, it->lit_it);
266 * Implementation of dt_it_ops::rec.
268 * Used with regular (non-striped) objects.
270 * \see dt_it_ops::rec() in the API description for details.
272 static int lod_it_rec(const struct lu_env *env, const struct dt_it *di,
273 struct dt_rec *rec, __u32 attr)
275 const struct lod_it *it = (const struct lod_it *)di;
277 LOD_CHECK_IT(env, it);
278 return it->lit_obj->do_index_ops->dio_it.rec(env, it->lit_it, rec,
283 * Implementation of dt_it_ops::rec_size.
285 * Used with regular (non-striped) objects.
287 * \see dt_it_ops::rec_size() in the API description for details.
289 static int lod_it_rec_size(const struct lu_env *env, const struct dt_it *di,
292 const struct lod_it *it = (const struct lod_it *)di;
294 LOD_CHECK_IT(env, it);
295 return it->lit_obj->do_index_ops->dio_it.rec_size(env, it->lit_it,
300 * Implementation of dt_it_ops::store.
302 * Used with regular (non-striped) objects.
304 * \see dt_it_ops::store() in the API description for details.
306 static __u64 lod_it_store(const struct lu_env *env, const struct dt_it *di)
308 const struct lod_it *it = (const struct lod_it *)di;
310 LOD_CHECK_IT(env, it);
311 return it->lit_obj->do_index_ops->dio_it.store(env, it->lit_it);
315 * Implementation of dt_it_ops::load.
317 * Used with regular (non-striped) objects.
319 * \see dt_it_ops::load() in the API description for details.
321 static int lod_it_load(const struct lu_env *env, const struct dt_it *di,
324 const struct lod_it *it = (const struct lod_it *)di;
326 LOD_CHECK_IT(env, it);
327 return it->lit_obj->do_index_ops->dio_it.load(env, it->lit_it, hash);
331 * Implementation of dt_it_ops::key_rec.
333 * Used with regular (non-striped) objects.
335 * \see dt_it_ops::rec() in the API description for details.
337 static int lod_it_key_rec(const struct lu_env *env, const struct dt_it *di,
340 const struct lod_it *it = (const struct lod_it *)di;
342 LOD_CHECK_IT(env, it);
343 return it->lit_obj->do_index_ops->dio_it.key_rec(env, it->lit_it,
347 static const struct dt_index_operations lod_index_ops = {
348 .dio_lookup = lod_lookup,
349 .dio_declare_insert = lod_declare_insert,
350 .dio_insert = lod_insert,
351 .dio_declare_delete = lod_declare_delete,
352 .dio_delete = lod_delete,
360 .key_size = lod_it_key_size,
362 .rec_size = lod_it_rec_size,
363 .store = lod_it_store,
365 .key_rec = lod_it_key_rec,
370 * Implementation of dt_index_operations::dio_lookup
372 * Used with striped directories.
374 * \see dt_index_operations::dio_lookup() in the API description for details.
376 static int lod_striped_lookup(const struct lu_env *env, struct dt_object *dt,
377 struct dt_rec *rec, const struct dt_key *key)
379 struct lod_object *lo = lod_dt_obj(dt);
380 struct dt_object *next;
381 const char *name = (const char *)key;
383 LASSERT(lo->ldo_dir_stripe_count > 0);
385 if (strcmp(name, dot) == 0) {
386 struct lu_fid *fid = (struct lu_fid *)rec;
388 *fid = *lod_object_fid(lo);
392 if (strcmp(name, dotdot) == 0) {
393 next = dt_object_child(dt);
397 index = __lmv_name_to_stripe_index(lo->ldo_dir_hash_type,
398 lo->ldo_dir_stripe_count,
399 lo->ldo_dir_migrate_hash,
400 lo->ldo_dir_migrate_offset,
401 name, strlen(name), true);
405 next = lo->ldo_stripe[index];
406 if (!next || !dt_object_exists(next))
410 return next->do_index_ops->dio_lookup(env, next, rec, key);
414 * Implementation of dt_it_ops::init.
416 * Used with striped objects. Internally just initializes the iterator
417 * on the first stripe.
419 * \see dt_it_ops::init() in the API description for details.
421 static struct dt_it *lod_striped_it_init(const struct lu_env *env,
422 struct dt_object *dt, __u32 attr)
424 struct lod_object *lo = lod_dt_obj(dt);
425 struct dt_object *next;
426 struct lod_it *it = &lod_env_info(env)->lti_it;
427 struct dt_it *it_next;
430 LASSERT(lo->ldo_dir_stripe_count > 0);
433 next = lo->ldo_stripe[index];
434 if (next && dt_object_exists(next))
436 } while (++index < lo->ldo_dir_stripe_count);
438 /* no valid stripe */
439 if (!next || !dt_object_exists(next))
440 return ERR_PTR(-ENODEV);
442 LASSERT(next->do_index_ops != NULL);
444 it_next = next->do_index_ops->dio_it.init(env, next, attr);
448 /* currently we do not use more than one iterator per thread
449 * so we store it in thread info. if at some point we need
450 * more active iterators in a single thread, we can allocate
452 LASSERT(it->lit_obj == NULL);
454 it->lit_stripe_index = index;
456 it->lit_it = it_next;
459 return (struct dt_it *)it;
462 #define LOD_CHECK_STRIPED_IT(env, it, lo) \
464 LASSERT((it)->lit_obj != NULL); \
465 LASSERT((it)->lit_it != NULL); \
466 LASSERT((lo)->ldo_dir_stripe_count > 0); \
467 LASSERT((it)->lit_stripe_index < (lo)->ldo_dir_stripe_count); \
471 * Implementation of dt_it_ops::fini.
473 * Used with striped objects.
475 * \see dt_it_ops::fini() in the API description for details.
477 static void lod_striped_it_fini(const struct lu_env *env, struct dt_it *di)
479 struct lod_it *it = (struct lod_it *)di;
480 struct lod_object *lo = lod_dt_obj(it->lit_obj);
481 struct dt_object *next;
483 /* If lit_it == NULL, then it means the sub_it has been finished,
484 * which only happens in failure cases, see lod_striped_it_next() */
485 if (it->lit_it != NULL) {
486 LOD_CHECK_STRIPED_IT(env, it, lo);
488 next = lo->ldo_stripe[it->lit_stripe_index];
490 LASSERT(next->do_index_ops != NULL);
491 next->do_index_ops->dio_it.fini(env, it->lit_it);
495 /* the iterator not in use any more */
498 it->lit_stripe_index = 0;
502 * Implementation of dt_it_ops::get.
504 * Right now it's not used widely, only to reset the iterator to the
505 * initial position. It should be possible to implement a full version
506 * which chooses a correct stripe to be able to position with any key.
508 * \see dt_it_ops::get() in the API description for details.
510 static int lod_striped_it_get(const struct lu_env *env, struct dt_it *di,
511 const struct dt_key *key)
513 const struct lod_it *it = (const struct lod_it *)di;
514 struct lod_object *lo = lod_dt_obj(it->lit_obj);
515 struct dt_object *next;
517 LOD_CHECK_STRIPED_IT(env, it, lo);
519 next = lo->ldo_stripe[it->lit_stripe_index];
520 LASSERT(next != NULL);
521 LASSERT(dt_object_exists(next));
522 LASSERT(next->do_index_ops != NULL);
524 return next->do_index_ops->dio_it.get(env, it->lit_it, key);
528 * Implementation of dt_it_ops::put.
530 * Used with striped objects.
532 * \see dt_it_ops::put() in the API description for details.
534 static void lod_striped_it_put(const struct lu_env *env, struct dt_it *di)
536 struct lod_it *it = (struct lod_it *)di;
537 struct lod_object *lo = lod_dt_obj(it->lit_obj);
538 struct dt_object *next;
541 * If lit_it == NULL, then it means the sub_it has been finished,
542 * which only happens in failure cases, see lod_striped_it_next()
547 LOD_CHECK_STRIPED_IT(env, it, lo);
549 next = lo->ldo_stripe[it->lit_stripe_index];
550 LASSERT(next != NULL);
551 LASSERT(next->do_index_ops != NULL);
553 return next->do_index_ops->dio_it.put(env, it->lit_it);
557 * Implementation of dt_it_ops::next.
559 * Used with striped objects. When the end of the current stripe is
560 * reached, the method takes the next stripe's iterator.
562 * \see dt_it_ops::next() in the API description for details.
564 static int lod_striped_it_next(const struct lu_env *env, struct dt_it *di)
566 struct lod_it *it = (struct lod_it *)di;
567 struct lod_object *lo = lod_dt_obj(it->lit_obj);
568 struct dt_object *next;
569 struct dt_it *it_next;
575 LOD_CHECK_STRIPED_IT(env, it, lo);
577 next = lo->ldo_stripe[it->lit_stripe_index];
578 LASSERT(next != NULL);
579 LASSERT(dt_object_exists(next));
580 LASSERT(next->do_index_ops != NULL);
582 rc = next->do_index_ops->dio_it.next(env, it->lit_it);
586 if (rc == 0 && it->lit_stripe_index == 0)
589 if (rc == 0 && it->lit_stripe_index > 0) {
590 struct lu_dirent *ent;
592 ent = (struct lu_dirent *)lod_env_info(env)->lti_key;
594 rc = next->do_index_ops->dio_it.rec(env, it->lit_it,
595 (struct dt_rec *)ent,
600 /* skip . and .. for slave stripe */
601 if ((strncmp(ent->lde_name, ".",
602 le16_to_cpu(ent->lde_namelen)) == 0 &&
603 le16_to_cpu(ent->lde_namelen) == 1) ||
604 (strncmp(ent->lde_name, "..",
605 le16_to_cpu(ent->lde_namelen)) == 0 &&
606 le16_to_cpu(ent->lde_namelen) == 2))
612 next->do_index_ops->dio_it.put(env, it->lit_it);
613 next->do_index_ops->dio_it.fini(env, it->lit_it);
616 /* go to next stripe */
617 index = it->lit_stripe_index;
618 while (++index < lo->ldo_dir_stripe_count) {
619 next = lo->ldo_stripe[index];
623 if (!dt_object_exists(next))
626 rc = next->do_ops->do_index_try(env, next,
627 &dt_directory_features);
631 LASSERT(next->do_index_ops != NULL);
633 it_next = next->do_index_ops->dio_it.init(env, next,
636 RETURN(PTR_ERR(it_next));
638 rc = next->do_index_ops->dio_it.get(env, it_next,
639 (const struct dt_key *)"");
641 RETURN(rc == 0 ? -EIO : rc);
643 it->lit_it = it_next;
644 it->lit_stripe_index = index;
653 * Implementation of dt_it_ops::key.
655 * Used with striped objects.
657 * \see dt_it_ops::key() in the API description for details.
659 static struct dt_key *lod_striped_it_key(const struct lu_env *env,
660 const struct dt_it *di)
662 const struct lod_it *it = (const struct lod_it *)di;
663 struct lod_object *lo = lod_dt_obj(it->lit_obj);
664 struct dt_object *next;
666 LOD_CHECK_STRIPED_IT(env, it, lo);
668 next = lo->ldo_stripe[it->lit_stripe_index];
669 LASSERT(next != NULL);
670 LASSERT(next->do_index_ops != NULL);
672 return next->do_index_ops->dio_it.key(env, it->lit_it);
676 * Implementation of dt_it_ops::key_size.
678 * Used with striped objects.
680 * \see dt_it_ops::size() in the API description for details.
682 static int lod_striped_it_key_size(const struct lu_env *env,
683 const struct dt_it *di)
685 struct lod_it *it = (struct lod_it *)di;
686 struct lod_object *lo = lod_dt_obj(it->lit_obj);
687 struct dt_object *next;
689 LOD_CHECK_STRIPED_IT(env, it, lo);
691 next = lo->ldo_stripe[it->lit_stripe_index];
692 LASSERT(next != NULL);
693 LASSERT(next->do_index_ops != NULL);
695 return next->do_index_ops->dio_it.key_size(env, it->lit_it);
699 * Implementation of dt_it_ops::rec.
701 * Used with striped objects.
703 * \see dt_it_ops::rec() in the API description for details.
705 static int lod_striped_it_rec(const struct lu_env *env, const struct dt_it *di,
706 struct dt_rec *rec, __u32 attr)
708 const struct lod_it *it = (const struct lod_it *)di;
709 struct lod_object *lo = lod_dt_obj(it->lit_obj);
710 struct dt_object *next;
712 LOD_CHECK_STRIPED_IT(env, it, lo);
714 next = lo->ldo_stripe[it->lit_stripe_index];
715 LASSERT(next != NULL);
716 LASSERT(next->do_index_ops != NULL);
718 return next->do_index_ops->dio_it.rec(env, it->lit_it, rec, attr);
722 * Implementation of dt_it_ops::rec_size.
724 * Used with striped objects.
726 * \see dt_it_ops::rec_size() in the API description for details.
728 static int lod_striped_it_rec_size(const struct lu_env *env,
729 const struct dt_it *di, __u32 attr)
731 struct lod_it *it = (struct lod_it *)di;
732 struct lod_object *lo = lod_dt_obj(it->lit_obj);
733 struct dt_object *next;
735 LOD_CHECK_STRIPED_IT(env, it, lo);
737 next = lo->ldo_stripe[it->lit_stripe_index];
738 LASSERT(next != NULL);
739 LASSERT(next->do_index_ops != NULL);
741 return next->do_index_ops->dio_it.rec_size(env, it->lit_it, attr);
745 * Implementation of dt_it_ops::store.
747 * Used with striped objects.
749 * \see dt_it_ops::store() in the API description for details.
751 static __u64 lod_striped_it_store(const struct lu_env *env,
752 const struct dt_it *di)
754 const struct lod_it *it = (const struct lod_it *)di;
755 struct lod_object *lo = lod_dt_obj(it->lit_obj);
756 struct dt_object *next;
758 LOD_CHECK_STRIPED_IT(env, it, lo);
760 next = lo->ldo_stripe[it->lit_stripe_index];
761 LASSERT(next != NULL);
762 LASSERT(next->do_index_ops != NULL);
764 return next->do_index_ops->dio_it.store(env, it->lit_it);
768 * Implementation of dt_it_ops::load.
770 * Used with striped objects.
772 * \see dt_it_ops::load() in the API description for details.
774 static int lod_striped_it_load(const struct lu_env *env,
775 const struct dt_it *di, __u64 hash)
777 const struct lod_it *it = (const struct lod_it *)di;
778 struct lod_object *lo = lod_dt_obj(it->lit_obj);
779 struct dt_object *next;
781 LOD_CHECK_STRIPED_IT(env, it, lo);
783 next = lo->ldo_stripe[it->lit_stripe_index];
784 LASSERT(next != NULL);
785 LASSERT(next->do_index_ops != NULL);
787 return next->do_index_ops->dio_it.load(env, it->lit_it, hash);
790 static const struct dt_index_operations lod_striped_index_ops = {
791 .dio_lookup = lod_striped_lookup,
792 .dio_declare_insert = lod_declare_insert,
793 .dio_insert = lod_insert,
794 .dio_declare_delete = lod_declare_delete,
795 .dio_delete = lod_delete,
797 .init = lod_striped_it_init,
798 .fini = lod_striped_it_fini,
799 .get = lod_striped_it_get,
800 .put = lod_striped_it_put,
801 .next = lod_striped_it_next,
802 .key = lod_striped_it_key,
803 .key_size = lod_striped_it_key_size,
804 .rec = lod_striped_it_rec,
805 .rec_size = lod_striped_it_rec_size,
806 .store = lod_striped_it_store,
807 .load = lod_striped_it_load,
812 * Append the FID for each shard of the striped directory after the
813 * given LMV EA header.
815 * To simplify striped directory and the consistency verification,
816 * we only store the LMV EA header on disk, for both master object
817 * and slave objects. When someone wants to know the whole LMV EA,
818 * such as client readdir(), we can build the entrie LMV EA on the
819 * MDT side (in RAM) via iterating the sub-directory entries that
820 * are contained in the master object of the stripe directory.
822 * For the master object of the striped directroy, the valid name
823 * for each shard is composed of the ${shard_FID}:${shard_idx}.
825 * There may be holes in the LMV EA if some shards' name entries
826 * are corrupted or lost.
828 * \param[in] env pointer to the thread context
829 * \param[in] lo pointer to the master object of the striped directory
830 * \param[in] buf pointer to the lu_buf which will hold the LMV EA
831 * \param[in] resize whether re-allocate the buffer if it is not big enough
833 * \retval positive size of the LMV EA
834 * \retval 0 for nothing to be loaded
835 * \retval negative error number on failure
837 int lod_load_lmv_shards(const struct lu_env *env, struct lod_object *lo,
838 struct lu_buf *buf, bool resize)
840 struct lu_dirent *ent =
841 (struct lu_dirent *)lod_env_info(env)->lti_key;
842 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
843 struct dt_object *obj = dt_object_child(&lo->ldo_obj);
844 struct lmv_mds_md_v1 *lmv1 = buf->lb_buf;
846 const struct dt_it_ops *iops;
848 __u32 magic = le32_to_cpu(lmv1->lmv_magic);
853 if (magic != LMV_MAGIC_V1)
856 stripes = le32_to_cpu(lmv1->lmv_stripe_count);
860 rc = lmv_mds_md_size(stripes, magic);
864 if (buf->lb_len < lmv1_size) {
873 lu_buf_alloc(buf, lmv1_size);
878 memcpy(buf->lb_buf, tbuf.lb_buf, tbuf.lb_len);
881 if (unlikely(!dt_try_as_dir(env, obj)))
884 memset(&lmv1->lmv_stripe_fids[0], 0, stripes * sizeof(struct lu_fid));
885 iops = &obj->do_index_ops->dio_it;
886 it = iops->init(env, obj, LUDA_64BITHASH);
890 rc = iops->load(env, it, 0);
892 rc = iops->next(env, it);
897 char name[FID_LEN + 2] = "";
902 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
908 fid_le_to_cpu(&fid, &ent->lde_fid);
909 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
910 if (ent->lde_name[0] == '.') {
911 if (ent->lde_namelen == 1)
914 if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
918 len = scnprintf(name, sizeof(name),
919 DFID":", PFID(&ent->lde_fid));
920 /* The ent->lde_name is composed of ${FID}:${index} */
921 if (ent->lde_namelen < len + 1 ||
922 memcmp(ent->lde_name, name, len) != 0) {
923 CDEBUG_LIMIT(lod->lod_lmv_failout ? D_ERROR : D_INFO,
924 "%s: invalid shard name %.*s with the FID "DFID" for the striped directory "DFID", %s\n",
925 lod2obd(lod)->obd_name, ent->lde_namelen,
926 ent->lde_name, PFID(&fid),
927 PFID(lu_object_fid(&obj->do_lu)),
928 lod->lod_lmv_failout ? "failout" : "skip");
930 if (lod->lod_lmv_failout)
938 if (ent->lde_name[len] < '0' ||
939 ent->lde_name[len] > '9') {
940 CDEBUG_LIMIT(lod->lod_lmv_failout ?
942 "%s: invalid shard name %.*s with the FID "DFID" for the striped directory "DFID", %s\n",
943 lod2obd(lod)->obd_name,
945 ent->lde_name, PFID(&fid),
946 PFID(lu_object_fid(&obj->do_lu)),
947 lod->lod_lmv_failout ?
950 if (lod->lod_lmv_failout)
956 index = index * 10 + ent->lde_name[len++] - '0';
957 } while (len < ent->lde_namelen);
959 if (len == ent->lde_namelen) {
960 /* Out of LMV EA range. */
961 if (index >= stripes) {
962 CERROR("%s: the shard %.*s for the striped "
963 "directory "DFID" is out of the known "
964 "LMV EA range [0 - %u], failout\n",
965 lod2obd(lod)->obd_name, ent->lde_namelen,
967 PFID(lu_object_fid(&obj->do_lu)),
973 /* The slot has been occupied. */
974 if (!fid_is_zero(&lmv1->lmv_stripe_fids[index])) {
978 &lmv1->lmv_stripe_fids[index]);
979 CERROR("%s: both the shard "DFID" and "DFID
980 " for the striped directory "DFID
981 " claim the same LMV EA slot at the "
982 "index %d, failout\n",
983 lod2obd(lod)->obd_name,
984 PFID(&fid0), PFID(&fid),
985 PFID(lu_object_fid(&obj->do_lu)), index);
990 /* stored as LE mode */
991 lmv1->lmv_stripe_fids[index] = ent->lde_fid;
994 rc = iops->next(env, it);
1001 RETURN(rc > 0 ? lmv_mds_md_size(stripes, magic) : rc);
1005 * Implementation of dt_object_operations::do_index_try.
1007 * \see dt_object_operations::do_index_try() in the API description for details.
1009 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
1010 const struct dt_index_features *feat)
1012 struct lod_object *lo = lod_dt_obj(dt);
1013 struct dt_object *next = dt_object_child(dt);
1017 LASSERT(next->do_ops);
1018 LASSERT(next->do_ops->do_index_try);
1020 rc = lod_striping_load(env, lo);
1024 rc = next->do_ops->do_index_try(env, next, feat);
1028 if (lo->ldo_dir_stripe_count > 0) {
1031 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1032 if (!lo->ldo_stripe[i])
1034 if (!dt_object_exists(lo->ldo_stripe[i]))
1036 rc = lo->ldo_stripe[i]->do_ops->do_index_try(env,
1037 lo->ldo_stripe[i], feat);
1041 dt->do_index_ops = &lod_striped_index_ops;
1043 dt->do_index_ops = &lod_index_ops;
1050 * Implementation of dt_object_operations::do_read_lock.
1052 * \see dt_object_operations::do_read_lock() in the API description for details.
1054 static void lod_read_lock(const struct lu_env *env, struct dt_object *dt,
1057 dt_read_lock(env, dt_object_child(dt), role);
1061 * Implementation of dt_object_operations::do_write_lock.
1063 * \see dt_object_operations::do_write_lock() in the API description for
1066 static void lod_write_lock(const struct lu_env *env, struct dt_object *dt,
1069 dt_write_lock(env, dt_object_child(dt), role);
1073 * Implementation of dt_object_operations::do_read_unlock.
1075 * \see dt_object_operations::do_read_unlock() in the API description for
1078 static void lod_read_unlock(const struct lu_env *env, struct dt_object *dt)
1080 dt_read_unlock(env, dt_object_child(dt));
1084 * Implementation of dt_object_operations::do_write_unlock.
1086 * \see dt_object_operations::do_write_unlock() in the API description for
1089 static void lod_write_unlock(const struct lu_env *env, struct dt_object *dt)
1091 dt_write_unlock(env, dt_object_child(dt));
1095 * Implementation of dt_object_operations::do_write_locked.
1097 * \see dt_object_operations::do_write_locked() in the API description for
1100 static int lod_write_locked(const struct lu_env *env, struct dt_object *dt)
1102 return dt_write_locked(env, dt_object_child(dt));
1106 * Implementation of dt_object_operations::do_attr_get.
1108 * \see dt_object_operations::do_attr_get() in the API description for details.
1110 static int lod_attr_get(const struct lu_env *env,
1111 struct dt_object *dt,
1112 struct lu_attr *attr)
1114 /* Note: for striped directory, client will merge attributes
1115 * from all of the sub-stripes see lmv_merge_attr(), and there
1116 * no MDD logic depend on directory nlink/size/time, so we can
1117 * always use master inode nlink and size for now. */
1118 return dt_attr_get(env, dt_object_child(dt), attr);
1121 void lod_adjust_stripe_size(struct lod_layout_component *comp,
1122 __u32 def_stripe_size)
1124 __u64 comp_end = comp->llc_extent.e_end;
1126 /* Choose stripe size if not set. Note that default stripe size can't
1127 * be used as is, because it must be multiplier of given component end.
1128 * - first check if default stripe size can be used
1129 * - if not than select the lowest set bit from component end and use
1130 * that value as stripe size
1132 if (!comp->llc_stripe_size) {
1133 if (comp_end == LUSTRE_EOF || !(comp_end % def_stripe_size))
1134 comp->llc_stripe_size = def_stripe_size;
1136 comp->llc_stripe_size = comp_end & ~(comp_end - 1);
1138 if (comp_end != LUSTRE_EOF &&
1139 comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
1140 CWARN("Component end %llu is not a multiple of min size %u\n",
1141 comp_end, LOV_MIN_STRIPE_SIZE);
1142 comp_end = round_up(comp_end, LOV_MIN_STRIPE_SIZE);
1144 /* check stripe size is multiplier of comp_end */
1145 if (comp_end != LUSTRE_EOF &&
1146 comp_end != comp->llc_extent.e_start &&
1147 comp_end % comp->llc_stripe_size) {
1148 /* fix that even for defined stripe size but warn
1149 * about the problem, that must not happen
1151 CWARN("Component end %llu is not aligned by the stripe size %u\n",
1152 comp_end, comp->llc_stripe_size);
1153 comp->llc_stripe_size = comp_end & ~(comp_end - 1);
1158 static inline void lod_adjust_stripe_info(struct lod_layout_component *comp,
1159 struct lov_desc *desc,
1162 if (comp->llc_pattern != LOV_PATTERN_MDT) {
1163 if (append_stripes) {
1164 comp->llc_stripe_count = append_stripes;
1165 } else if (!comp->llc_stripe_count) {
1166 comp->llc_stripe_count =
1167 desc->ld_default_stripe_count;
1171 lod_adjust_stripe_size(comp, desc->ld_default_stripe_size);
1174 int lod_obj_for_each_stripe(const struct lu_env *env, struct lod_object *lo,
1176 struct lod_obj_stripe_cb_data *data)
1178 struct lod_layout_component *lod_comp;
1182 mutex_lock(&lo->ldo_layout_mutex);
1183 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1184 lod_comp = &lo->ldo_comp_entries[i];
1186 if (lod_comp->llc_stripe == NULL)
1189 /* has stripe but not inited yet, this component has been
1190 * declared to be created, but hasn't created yet.
1192 if (!lod_comp_inited(lod_comp))
1195 if (data->locd_comp_skip_cb &&
1196 data->locd_comp_skip_cb(env, lo, i, data))
1199 if (data->locd_comp_cb) {
1200 rc = data->locd_comp_cb(env, lo, i, data);
1205 /* could used just to do sth about component, not each
1208 if (!data->locd_stripe_cb)
1211 LASSERT(lod_comp->llc_stripe_count > 0);
1212 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
1213 struct dt_object *dt = lod_comp->llc_stripe[j];
1217 rc = data->locd_stripe_cb(env, lo, dt, th, i, j, data);
1223 mutex_unlock(&lo->ldo_layout_mutex);
1227 static bool lod_obj_attr_set_comp_skip_cb(const struct lu_env *env,
1228 struct lod_object *lo, int comp_idx,
1229 struct lod_obj_stripe_cb_data *data)
1231 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
1232 bool skipped = false;
1234 if (!(data->locd_attr->la_valid & LA_LAYOUT_VERSION))
1237 switch (lo->ldo_flr_state) {
1238 case LCM_FL_WRITE_PENDING: {
1241 /* skip stale components */
1242 if (lod_comp->llc_flags & LCME_FL_STALE) {
1247 /* skip valid and overlapping components, therefore any
1248 * attempts to write overlapped components will never succeed
1249 * because client will get EINPROGRESS. */
1250 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1254 if (lo->ldo_comp_entries[i].llc_flags & LCME_FL_STALE)
1257 if (lu_extent_is_overlapped(&lod_comp->llc_extent,
1258 &lo->ldo_comp_entries[i].llc_extent)) {
1266 case LCM_FL_SYNC_PENDING:
1269 LASSERTF(0, "impossible: %d\n", lo->ldo_flr_state);
1273 CDEBUG(D_LAYOUT, DFID": %s to set component %x to version: %u\n",
1274 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
1275 skipped ? "skipped" : "chose", lod_comp->llc_id,
1276 data->locd_attr->la_layout_version);
1282 lod_obj_stripe_attr_set_cb(const struct lu_env *env, struct lod_object *lo,
1283 struct dt_object *dt, struct thandle *th,
1284 int comp_idx, int stripe_idx,
1285 struct lod_obj_stripe_cb_data *data)
1287 if (data->locd_declare)
1288 return lod_sub_declare_attr_set(env, dt, data->locd_attr, th);
1290 if (data->locd_attr->la_valid & LA_LAYOUT_VERSION) {
1291 CDEBUG(D_LAYOUT, DFID": set layout version: %u, comp_idx: %d\n",
1292 PFID(lu_object_fid(&dt->do_lu)),
1293 data->locd_attr->la_layout_version, comp_idx);
1296 return lod_sub_attr_set(env, dt, data->locd_attr, th);
1300 * Implementation of dt_object_operations::do_declare_attr_set.
1302 * If the object is striped, then apply the changes to all the stripes.
1304 * \see dt_object_operations::do_declare_attr_set() in the API description
1307 static int lod_declare_attr_set(const struct lu_env *env,
1308 struct dt_object *dt,
1309 const struct lu_attr *attr,
1312 struct dt_object *next = dt_object_child(dt);
1313 struct lod_object *lo = lod_dt_obj(dt);
1318 * declare setattr on the local object
1320 rc = lod_sub_declare_attr_set(env, next, attr, th);
1324 /* osp_declare_attr_set() ignores all attributes other than
1325 * UID, GID, PROJID, and size, and osp_attr_set() ignores all
1326 * but UID, GID and PROJID. Declaration of size attr setting
1327 * happens through lod_declare_init_size(), and not through
1328 * this function. Therefore we need not load striping unless
1329 * ownership is changing. This should save memory and (we hope)
1330 * speed up rename().
1332 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1333 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1336 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1339 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID | LA_MODE |
1340 LA_ATIME | LA_MTIME | LA_CTIME |
1345 * load striping information, notice we don't do this when object
1346 * is being initialized as we don't need this information till
1347 * few specific cases like destroy, chown
1349 rc = lod_striping_load(env, lo);
1353 if (!lod_obj_is_striped(dt))
1357 * if object is striped declare changes on the stripes
1359 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1360 LASSERT(lo->ldo_stripe);
1361 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1362 if (lo->ldo_stripe[i] == NULL)
1364 if (!dt_object_exists(lo->ldo_stripe[i]))
1366 rc = lod_sub_declare_attr_set(env, lo->ldo_stripe[i],
1372 struct lod_obj_stripe_cb_data data = { { 0 } };
1374 data.locd_attr = attr;
1375 data.locd_declare = true;
1376 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1377 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1383 if (!dt_object_exists(next) || dt_object_remote(next) ||
1384 !S_ISREG(attr->la_mode))
1387 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1388 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
1392 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1393 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1394 struct lod_thread_info *info = lod_env_info(env);
1395 struct lu_buf *buf = &info->lti_buf;
1397 buf->lb_buf = info->lti_ea_store;
1398 buf->lb_len = info->lti_ea_store_size;
1399 rc = lod_sub_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1400 LU_XATTR_REPLACE, th);
1407 * Implementation of dt_object_operations::do_attr_set.
1409 * If the object is striped, then apply the changes to all or subset of
1410 * the stripes depending on the object type and specific attributes.
1412 * \see dt_object_operations::do_attr_set() in the API description for details.
1414 static int lod_attr_set(const struct lu_env *env,
1415 struct dt_object *dt,
1416 const struct lu_attr *attr,
1419 struct dt_object *next = dt_object_child(dt);
1420 struct lod_object *lo = lod_dt_obj(dt);
1425 * apply changes to the local object
1427 rc = lod_sub_attr_set(env, next, attr, th);
1431 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1432 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1435 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1438 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE | LA_PROJID |
1439 LA_ATIME | LA_MTIME | LA_CTIME |
1444 /* FIXME: a tricky case in the code path of mdd_layout_change():
1445 * the in-memory striping information has been freed in lod_xattr_set()
1446 * due to layout change. It has to load stripe here again. It only
1447 * changes flags of layout so declare_attr_set() is still accurate */
1448 rc = lod_striping_load(env, lo);
1452 if (!lod_obj_is_striped(dt))
1456 * if object is striped, apply changes to all the stripes
1458 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1459 LASSERT(lo->ldo_stripe);
1460 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1461 if (unlikely(lo->ldo_stripe[i] == NULL))
1464 if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1467 rc = lod_sub_attr_set(env, lo->ldo_stripe[i], attr, th);
1472 struct lod_obj_stripe_cb_data data = { { 0 } };
1474 data.locd_attr = attr;
1475 data.locd_declare = false;
1476 data.locd_comp_skip_cb = lod_obj_attr_set_comp_skip_cb;
1477 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1478 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1484 if (!dt_object_exists(next) || dt_object_remote(next) ||
1485 !S_ISREG(attr->la_mode))
1488 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1489 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
1493 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1494 struct lod_thread_info *info = lod_env_info(env);
1495 struct lu_buf *buf = &info->lti_buf;
1496 struct ost_id *oi = &info->lti_ostid;
1497 struct lu_fid *fid = &info->lti_fid;
1498 struct lov_mds_md_v1 *lmm;
1499 struct lov_ost_data_v1 *objs;
1502 rc = lod_get_lov_ea(env, lo);
1506 buf->lb_buf = info->lti_ea_store;
1507 buf->lb_len = info->lti_ea_store_size;
1508 lmm = info->lti_ea_store;
1509 magic = le32_to_cpu(lmm->lmm_magic);
1510 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
1511 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1512 struct lov_comp_md_entry_v1 *lcme =
1513 &lcm->lcm_entries[0];
1515 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1516 magic = le32_to_cpu(lmm->lmm_magic);
1519 if (magic == LOV_MAGIC_V1)
1520 objs = &(lmm->lmm_objects[0]);
1522 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1523 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1524 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1526 fid_to_ostid(fid, oi);
1527 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1529 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1530 LU_XATTR_REPLACE, th);
1531 } else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1532 struct lod_thread_info *info = lod_env_info(env);
1533 struct lu_buf *buf = &info->lti_buf;
1534 struct lov_comp_md_v1 *lcm;
1535 struct lov_comp_md_entry_v1 *lcme;
1537 rc = lod_get_lov_ea(env, lo);
1541 buf->lb_buf = info->lti_ea_store;
1542 buf->lb_len = info->lti_ea_store_size;
1544 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1 &&
1545 le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_SEL)
1548 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1549 lcme = &lcm->lcm_entries[0];
1550 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1551 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1553 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1554 LU_XATTR_REPLACE, th);
1561 * Implementation of dt_object_operations::do_xattr_get.
1563 * If LOV EA is requested from the root object and it's not
1564 * found, then return default striping for the filesystem.
1566 * \see dt_object_operations::do_xattr_get() in the API description for details.
1568 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1569 struct lu_buf *buf, const char *name)
1571 struct lod_thread_info *info = lod_env_info(env);
1572 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1577 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1578 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1579 struct lmv_mds_md_v1 *lmv1;
1580 struct lmv_foreign_md *lfm;
1583 if (rc > (typeof(rc))sizeof(*lmv1))
1586 /* short (<= sizeof(struct lmv_mds_md_v1)) foreign LMV case */
1587 /* XXX empty foreign LMV is not allowed */
1588 if (rc <= offsetof(typeof(*lfm), lfm_value))
1589 RETURN(rc = rc > 0 ? -EINVAL : rc);
1591 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1592 BUILD_BUG_ON(sizeof(*lmv1) > sizeof(info->lti_key));
1594 /* lti_buf is large enough for *lmv1 or a short
1595 * (<= sizeof(struct lmv_mds_md_v1)) foreign LMV
1597 info->lti_buf.lb_buf = info->lti_key;
1598 info->lti_buf.lb_len = sizeof(*lmv1);
1599 rc = dt_xattr_get(env, dt_object_child(dt),
1600 &info->lti_buf, name);
1601 if (unlikely(rc <= offsetof(typeof(*lfm),
1603 RETURN(rc = rc > 0 ? -EINVAL : rc);
1605 lfm = info->lti_buf.lb_buf;
1606 if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN)
1609 if (unlikely(rc != sizeof(*lmv1)))
1610 RETURN(rc = rc > 0 ? -EINVAL : rc);
1612 lmv1 = info->lti_buf.lb_buf;
1613 /* The on-disk LMV EA only contains header, but the
1614 * returned LMV EA size should contain the space for
1615 * the FIDs of all shards of the striped directory. */
1616 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1617 rc = lmv_mds_md_size(
1618 le32_to_cpu(lmv1->lmv_stripe_count),
1619 le32_to_cpu(lmv1->lmv_magic));
1622 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1625 if (rc != sizeof(*lmv1))
1626 RETURN(rc = rc > 0 ? -EINVAL : rc);
1628 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1632 RETURN(rc = rc1 != 0 ? rc1 : rc);
1635 if ((rc > 0) && buf->lb_buf && strcmp(name, XATTR_NAME_LOV) == 0) {
1636 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1638 if (lcm->lcm_magic == cpu_to_le32(LOV_MAGIC_SEL))
1639 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1642 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1646 * XXX: Only used by lfsck
1648 * lod returns default striping on the real root of the device
1649 * this is like the root stores default striping for the whole
1650 * filesystem. historically we've been using a different approach
1651 * and store it in the config.
1653 dt_root_get(env, dev->lod_child, &info->lti_fid);
1654 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1656 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1657 struct lov_user_md *lum = buf->lb_buf;
1658 struct lov_desc *desc = &dev->lod_ost_descs.ltd_lov_desc;
1660 if (buf->lb_buf == NULL) {
1662 } else if (buf->lb_len >= sizeof(*lum)) {
1663 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1664 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1665 lmm_oi_set_id(&lum->lmm_oi, 0);
1666 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1667 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1668 lum->lmm_stripe_size = cpu_to_le32(
1669 desc->ld_default_stripe_size);
1670 lum->lmm_stripe_count = cpu_to_le16(
1671 desc->ld_default_stripe_count);
1672 lum->lmm_stripe_offset = cpu_to_le16(
1673 desc->ld_default_stripe_offset);
1686 * Checks that the magic of the stripe is sane.
1688 * \param[in] lod lod device
1689 * \param[in] lum a buffer storing LMV EA to verify
1691 * \retval 0 if the EA is sane
1692 * \retval negative otherwise
1694 static int lod_verify_md_striping(struct lod_device *lod,
1695 const struct lmv_user_md_v1 *lum)
1697 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1698 CERROR("%s: invalid lmv_user_md: magic = %x, "
1699 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1700 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1701 (int)le32_to_cpu(lum->lum_stripe_offset),
1702 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1710 * Initialize LMV EA for a slave.
1712 * Initialize slave's LMV EA from the master's LMV EA.
1714 * \param[in] master_lmv a buffer containing master's EA
1715 * \param[out] slave_lmv a buffer where slave's EA will be stored
1718 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1719 const struct lmv_mds_md_v1 *master_lmv)
1721 *slave_lmv = *master_lmv;
1722 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1728 * Generate LMV EA from the object passed as \a dt. The object must have
1729 * the stripes created and initialized.
1731 * \param[in] env execution environment
1732 * \param[in] dt object
1733 * \param[out] lmv_buf buffer storing generated LMV EA
1735 * \retval 0 on success
1736 * \retval negative if failed
1738 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1739 struct lu_buf *lmv_buf)
1741 struct lod_thread_info *info = lod_env_info(env);
1742 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1743 struct lod_object *lo = lod_dt_obj(dt);
1744 struct lmv_mds_md_v1 *lmm1;
1746 int type = LU_SEQ_RANGE_ANY;
1751 LASSERT(lo->ldo_dir_striped != 0);
1752 LASSERT(lo->ldo_dir_stripe_count > 0);
1753 stripe_count = lo->ldo_dir_stripe_count;
1754 /* Only store the LMV EA heahder on the disk. */
1755 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1756 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1760 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1763 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1764 memset(lmm1, 0, sizeof(*lmm1));
1765 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1766 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1767 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1768 lmm1->lmv_layout_version = cpu_to_le32(lo->ldo_dir_layout_version);
1769 if (lod_is_layout_changing(lo)) {
1770 lmm1->lmv_migrate_hash = cpu_to_le32(lo->ldo_dir_migrate_hash);
1771 lmm1->lmv_migrate_offset =
1772 cpu_to_le32(lo->ldo_dir_migrate_offset);
1774 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1779 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1780 lmv_buf->lb_buf = info->lti_ea_store;
1781 lmv_buf->lb_len = sizeof(*lmm1);
1787 * Create in-core represenation for a striped directory.
1789 * Parse the buffer containing LMV EA and instantiate LU objects
1790 * representing the stripe objects. The pointers to the objects are
1791 * stored in ldo_stripe field of \a lo. This function is used when
1792 * we need to access an already created object (i.e. load from a disk).
1794 * \param[in] env execution environment
1795 * \param[in] lo lod object
1796 * \param[in] buf buffer containing LMV EA
1798 * \retval 0 on success
1799 * \retval negative if failed
1801 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1802 const struct lu_buf *buf)
1804 struct lod_thread_info *info = lod_env_info(env);
1805 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1806 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1807 struct dt_object **stripe;
1808 union lmv_mds_md *lmm = buf->lb_buf;
1809 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1810 struct lu_fid *fid = &info->lti_fid;
1815 LASSERT(mutex_is_locked(&lo->ldo_layout_mutex));
1817 /* XXX may be useless as not called for foreign LMV ?? */
1818 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_FOREIGN)
1821 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1822 lo->ldo_dir_slave_stripe = 1;
1826 if (!lmv_is_sane(lmv1))
1829 LASSERT(lo->ldo_stripe == NULL);
1830 OBD_ALLOC_PTR_ARRAY(stripe, le32_to_cpu(lmv1->lmv_stripe_count));
1834 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1835 struct dt_device *tgt_dt;
1836 struct dt_object *dto;
1837 int type = LU_SEQ_RANGE_ANY;
1840 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1841 if (!fid_is_sane(fid)) {
1846 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1850 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1851 tgt_dt = lod->lod_child;
1853 struct lod_tgt_desc *tgt;
1855 tgt = LTD_TGT(ltd, idx);
1857 GOTO(out, rc = -ESTALE);
1858 tgt_dt = tgt->ltd_tgt;
1861 dto = dt_locate_at(env, tgt_dt, fid,
1862 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1865 GOTO(out, rc = PTR_ERR(dto));
1870 lo->ldo_stripe = stripe;
1871 lo->ldo_dir_stripe_count = le32_to_cpu(lmv1->lmv_stripe_count);
1872 lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1873 lo->ldo_dir_layout_version = le32_to_cpu(lmv1->lmv_layout_version);
1874 lo->ldo_dir_migrate_offset = le32_to_cpu(lmv1->lmv_migrate_offset);
1875 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv1->lmv_migrate_hash);
1876 lo->ldo_dir_hash_type = le32_to_cpu(lmv1->lmv_hash_type);
1878 lod_striping_free_nolock(env, lo);
1884 * Declare create a striped directory.
1886 * Declare creating a striped directory with a given stripe pattern on the
1887 * specified MDTs. A striped directory is represented as a regular directory
1888 * - an index listing all the stripes. The stripes point back to the master
1889 * object with ".." and LinkEA. The master object gets LMV EA which
1890 * identifies it as a striped directory. The function allocates FIDs
1893 * \param[in] env execution environment
1894 * \param[in] dt object
1895 * \param[in] attr attributes to initialize the objects with
1896 * \param[in] dof type of objects to be created
1897 * \param[in] th transaction handle
1899 * \retval 0 on success
1900 * \retval negative if failed
1902 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1903 struct dt_object *dt,
1904 struct lu_attr *attr,
1905 struct dt_object_format *dof,
1908 struct lod_thread_info *info = lod_env_info(env);
1909 struct lu_buf lmv_buf;
1910 struct lu_buf slave_lmv_buf;
1911 struct lmv_mds_md_v1 *lmm;
1912 struct lmv_mds_md_v1 *slave_lmm = NULL;
1913 struct dt_insert_rec *rec = &info->lti_dt_rec;
1914 struct lod_object *lo = lod_dt_obj(dt);
1919 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1922 lmm = lmv_buf.lb_buf;
1924 OBD_ALLOC_PTR(slave_lmm);
1925 if (slave_lmm == NULL)
1926 GOTO(out, rc = -ENOMEM);
1928 lod_prep_slave_lmv_md(slave_lmm, lmm);
1929 slave_lmv_buf.lb_buf = slave_lmm;
1930 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1932 if (!dt_try_as_dir(env, dt_object_child(dt)))
1933 GOTO(out, rc = -EINVAL);
1935 rec->rec_type = S_IFDIR;
1936 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1937 struct dt_object *dto = lo->ldo_stripe[i];
1938 char *stripe_name = info->lti_key;
1939 struct lu_name *sname;
1940 struct linkea_data ldata = { NULL };
1941 struct lu_buf linkea_buf;
1943 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
1947 /* directory split skip create for existing stripes */
1948 if (!(lod_is_splitting(lo) && i < lo->ldo_dir_split_offset)) {
1949 rc = lod_sub_declare_create(env, dto, attr, NULL, dof,
1954 if (!dt_try_as_dir(env, dto))
1955 GOTO(out, rc = -EINVAL);
1957 rc = lod_sub_declare_ref_add(env, dto, th);
1961 rec->rec_fid = lu_object_fid(&dto->do_lu);
1962 rc = lod_sub_declare_insert(env, dto,
1963 (const struct dt_rec *)rec,
1964 (const struct dt_key *)dot,
1969 /* master stripe FID will be put to .. */
1970 rec->rec_fid = lu_object_fid(&dt->do_lu);
1971 rc = lod_sub_declare_insert(env, dto,
1972 (const struct dt_rec *)rec,
1973 (const struct dt_key *)dotdot,
1978 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1980 snprintf(stripe_name, sizeof(info->lti_key),
1982 PFID(lu_object_fid(&dto->do_lu)),
1985 snprintf(stripe_name, sizeof(info->lti_key),
1987 PFID(lu_object_fid(&dto->do_lu)), i);
1989 sname = lod_name_get(env, stripe_name,
1990 strlen(stripe_name));
1991 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1992 sname, lu_object_fid(&dt->do_lu));
1996 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1997 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1998 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
1999 XATTR_NAME_LINK, 0, th);
2003 rec->rec_fid = lu_object_fid(&dto->do_lu);
2004 rc = lod_sub_declare_insert(env, dt_object_child(dt),
2005 (const struct dt_rec *)rec,
2006 (const struct dt_key *)stripe_name, th);
2010 rc = lod_sub_declare_ref_add(env, dt_object_child(dt),
2016 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
2017 cfs_fail_val != i) {
2018 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
2020 slave_lmm->lmv_master_mdt_index =
2023 slave_lmm->lmv_master_mdt_index =
2025 rc = lod_sub_declare_xattr_set(env, dto, &slave_lmv_buf,
2026 XATTR_NAME_LMV, 0, th);
2032 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt),
2033 &lmv_buf, XATTR_NAME_LMV, 0, th);
2037 if (slave_lmm != NULL)
2038 OBD_FREE_PTR(slave_lmm);
2044 * Allocate a striping on a predefined set of MDTs.
2046 * Allocates new striping using the MDT index range provided by the data from
2047 * the lum_obejcts contained in the lmv_user_md passed to this method if
2048 * \a is_specific is true; or allocates new layout starting from MDT index in
2049 * lo->ldo_dir_stripe_offset. The exact order of MDTs is not important and
2050 * varies depending on MDT status. The number of stripes needed and stripe
2051 * offset are taken from the object. If that number cannot be met, then the
2052 * function returns an error and then it's the caller's responsibility to
2053 * release the stripes allocated. All the internal structures are protected,
2054 * but no concurrent allocation is allowed on the same objects.
2056 * \param[in] env execution environment for this thread
2057 * \param[in] lo LOD object
2058 * \param[out] stripes striping created
2059 * \param[out] mdt_indices MDT indices of striping created
2060 * \param[in] is_specific true if the MDTs are provided by lum; false if
2061 * only the starting MDT index is provided
2063 * \retval positive stripes allocated, including the first stripe allocated
2065 * \retval negative errno on failure
2067 static int lod_mdt_alloc_specific(const struct lu_env *env,
2068 struct lod_object *lo,
2069 struct dt_object **stripes,
2070 __u32 *mdt_indices, bool is_specific)
2072 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
2073 struct lu_tgt_descs *ltd = &lod->lod_mdt_descs;
2074 struct lu_tgt_desc *tgt = NULL;
2075 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2076 struct dt_device *tgt_dt = NULL;
2077 struct lu_fid fid = { 0 };
2078 struct dt_object *dto;
2080 u32 stripe_count = lo->ldo_dir_stripe_count;
2086 master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
2087 if (!is_specific && stripe_count > 1)
2088 /* Set the start index for the 2nd stripe allocation */
2089 mdt_indices[1] = (mdt_indices[0] + 1) %
2090 (lod->lod_remote_mdt_count + 1);
2092 for (; stripe_idx < stripe_count; stripe_idx++) {
2093 /* Try to find next avaible target */
2094 idx = mdt_indices[stripe_idx];
2095 for (j = 0; j < lod->lod_remote_mdt_count;
2096 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
2097 bool already_allocated = false;
2100 CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
2101 idx, lod->lod_remote_mdt_count + 1, stripe_idx);
2103 if (likely(!is_specific &&
2104 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
2105 /* check whether the idx already exists
2106 * in current allocated array */
2107 for (k = 0; k < stripe_idx; k++) {
2108 if (mdt_indices[k] == idx) {
2109 already_allocated = true;
2114 if (already_allocated)
2118 /* Sigh, this index is not in the bitmap, let's check
2119 * next available target */
2120 if (!test_bit(idx, ltd->ltd_tgt_bitmap) &&
2121 idx != master_index)
2124 if (idx == master_index) {
2125 /* Allocate the FID locally */
2126 tgt_dt = lod->lod_child;
2127 rc = dt_fid_alloc(env, tgt_dt, &fid, NULL,
2134 /* check the status of the OSP */
2135 tgt = LTD_TGT(ltd, idx);
2139 tgt_dt = tgt->ltd_tgt;
2140 if (!tgt->ltd_active)
2141 /* this OSP doesn't feel well */
2144 rc = dt_fid_alloc(env, tgt_dt, &fid, NULL, NULL);
2151 /* Can not allocate more stripes */
2152 if (j == lod->lod_remote_mdt_count) {
2153 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
2154 lod2obd(lod)->obd_name, stripe_count,
2159 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
2160 idx, stripe_idx, PFID(&fid));
2161 mdt_indices[stripe_idx] = idx;
2162 /* Set the start index for next stripe allocation */
2163 if (!is_specific && stripe_idx < stripe_count - 1) {
2165 * for large dir test, put all other slaves on one
2166 * remote MDT, otherwise we may save too many local
2167 * slave locks which will exceed RS_MAX_LOCKS.
2169 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)))
2171 mdt_indices[stripe_idx + 1] = (idx + 1) %
2172 (lod->lod_remote_mdt_count + 1);
2174 /* tgt_dt and fid must be ready after search avaible OSP
2175 * in the above loop */
2176 LASSERT(tgt_dt != NULL);
2177 LASSERT(fid_is_sane(&fid));
2179 /* fail a remote stripe FID allocation */
2180 if (stripe_idx && OBD_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_FID))
2183 dto = dt_locate_at(env, tgt_dt, &fid,
2184 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
2191 stripes[stripe_idx] = dto;
2197 for (j = 1; j < stripe_idx; j++) {
2198 LASSERT(stripes[j] != NULL);
2199 dt_object_put(env, stripes[j]);
2205 static int lod_prep_md_striped_create(const struct lu_env *env,
2206 struct dt_object *dt,
2207 struct lu_attr *attr,
2208 const struct lmv_user_md_v1 *lum,
2209 struct dt_object_format *dof,
2212 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
2213 struct lod_object *lo = lod_dt_obj(dt);
2214 struct dt_object **stripes;
2215 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2216 struct lu_fid fid = { 0 };
2223 /* The lum has been verifed in lod_verify_md_striping */
2224 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC ||
2225 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC);
2227 stripe_count = lo->ldo_dir_stripe_count;
2229 OBD_ALLOC_PTR_ARRAY(stripes, stripe_count);
2233 /* Allocate the first stripe locally */
2234 rc = dt_fid_alloc(env, lod->lod_child, &fid, NULL, NULL);
2238 stripes[0] = dt_locate_at(env, lod->lod_child, &fid,
2239 dt->do_lu.lo_dev->ld_site->ls_top_dev, &conf);
2240 if (IS_ERR(stripes[0]))
2241 GOTO(out, rc = PTR_ERR(stripes[0]));
2243 if (lo->ldo_dir_stripe_offset == LMV_OFFSET_DEFAULT) {
2244 lod_qos_statfs_update(env, lod, &lod->lod_mdt_descs);
2245 rc = lod_mdt_alloc_qos(env, lo, stripes, 1, stripe_count);
2247 rc = lod_mdt_alloc_rr(env, lo, stripes, 1,
2251 bool is_specific = false;
2253 OBD_ALLOC_PTR_ARRAY(idx_array, stripe_count);
2255 GOTO(out, rc = -ENOMEM);
2257 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) {
2259 for (i = 0; i < stripe_count; i++)
2261 le32_to_cpu(lum->lum_objects[i].lum_mds);
2264 /* stripe 0 is local */
2266 lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
2267 rc = lod_mdt_alloc_specific(env, lo, stripes, idx_array,
2269 OBD_FREE_PTR_ARRAY(idx_array, stripe_count);
2277 lo->ldo_dir_striped = 1;
2278 lo->ldo_stripe = stripes;
2279 lo->ldo_dir_stripe_count = rc;
2280 lo->ldo_dir_stripes_allocated = stripe_count;
2282 lo->ldo_dir_stripe_loaded = 1;
2284 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
2286 lod_striping_free(env, lo);
2292 if (!IS_ERR_OR_NULL(stripes[0]))
2293 dt_object_put(env, stripes[0]);
2294 for (i = 1; i < stripe_count; i++)
2295 LASSERT(!stripes[i]);
2296 OBD_FREE_PTR_ARRAY(stripes, stripe_count);
2303 * Alloc cached foreign LMV
2305 * \param[in] lo object
2306 * \param[in] size size of foreign LMV
2308 * \retval 0 on success
2309 * \retval negative if failed
2311 int lod_alloc_foreign_lmv(struct lod_object *lo, size_t size)
2313 OBD_ALLOC_LARGE(lo->ldo_foreign_lmv, size);
2314 if (lo->ldo_foreign_lmv == NULL)
2316 lo->ldo_foreign_lmv_size = size;
2317 lo->ldo_dir_is_foreign = 1;
2323 * Declare create striped md object.
2325 * The function declares intention to create a striped directory. This is a
2326 * wrapper for lod_prep_md_striped_create(). The only additional functionality
2327 * is to verify pattern \a lum_buf is good. Check that function for the details.
2329 * \param[in] env execution environment
2330 * \param[in] dt object
2331 * \param[in] attr attributes to initialize the objects with
2332 * \param[in] lum_buf a pattern specifying the number of stripes and
2334 * \param[in] dof type of objects to be created
2335 * \param[in] th transaction handle
2337 * \retval 0 on success
2338 * \retval negative if failed
2341 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
2342 struct dt_object *dt,
2343 struct lu_attr *attr,
2344 const struct lu_buf *lum_buf,
2345 struct dt_object_format *dof,
2348 struct lod_object *lo = lod_dt_obj(dt);
2349 struct lmv_user_md_v1 *lum = lum_buf->lb_buf;
2353 LASSERT(lum != NULL);
2355 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
2356 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
2357 (int)le32_to_cpu(lum->lum_stripe_offset));
2359 if (lo->ldo_dir_stripe_count == 0) {
2360 if (lo->ldo_dir_is_foreign) {
2361 rc = lod_alloc_foreign_lmv(lo, lum_buf->lb_len);
2364 memcpy(lo->ldo_foreign_lmv, lum, lum_buf->lb_len);
2365 lo->ldo_dir_stripe_loaded = 1;
2370 /* prepare dir striped objects */
2371 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2373 /* failed to create striping, let's reset
2374 * config so that others don't get confused */
2375 lod_striping_free(env, lo);
2383 * Set or replace striped directory layout, and LFSCK may set layout on a plain
2384 * directory, so don't check stripe count.
2386 * \param[in] env execution environment
2387 * \param[in] dt target object
2388 * \param[in] buf LMV buf which contains source stripe fids
2389 * \param[in] fl set or replace
2390 * \param[in] th transaction handle
2392 * \retval 0 on success
2393 * \retval negative if failed
2395 static int lod_dir_layout_set(const struct lu_env *env,
2396 struct dt_object *dt,
2397 const struct lu_buf *buf,
2401 struct dt_object *next = dt_object_child(dt);
2402 struct lod_object *lo = lod_dt_obj(dt);
2403 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2404 struct lmv_mds_md_v1 *lmv = buf->lb_buf;
2405 struct lmv_mds_md_v1 *slave_lmv;
2406 struct lu_buf slave_buf;
2412 if (!lmv_is_sane2(lmv))
2415 /* adjust hash for dir merge, which may not be set in user command */
2416 if (lmv_is_merging(lmv) &&
2417 !(lmv->lmv_migrate_hash & LMV_HASH_TYPE_MASK))
2418 lmv->lmv_merge_hash |=
2419 lod->lod_mdt_descs.ltd_lmv_desc.ld_pattern &
2422 LMV_DEBUG(D_INFO, lmv, "set");
2424 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LMV, fl, th);
2428 /* directory restripe may update stripe LMV directly */
2429 if (!lo->ldo_dir_stripe_count)
2432 lo->ldo_dir_hash_type = le32_to_cpu(lmv->lmv_hash_type);
2433 lo->ldo_dir_migrate_offset = le32_to_cpu(lmv->lmv_migrate_offset);
2434 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_migrate_hash);
2435 lo->ldo_dir_layout_version = le32_to_cpu(lmv->lmv_layout_version);
2437 OBD_ALLOC_PTR(slave_lmv);
2441 lod_prep_slave_lmv_md(slave_lmv, lmv);
2442 slave_buf.lb_buf = slave_lmv;
2443 slave_buf.lb_len = sizeof(*slave_lmv);
2445 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2446 if (!lo->ldo_stripe[i])
2449 if (!dt_object_exists(lo->ldo_stripe[i]))
2452 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], &slave_buf,
2453 XATTR_NAME_LMV, fl, th);
2458 OBD_FREE_PTR(slave_lmv);
2464 * Implementation of dt_object_operations::do_declare_xattr_set.
2466 * Used with regular (non-striped) objects. Basically it
2467 * initializes the striping information and applies the
2468 * change to all the stripes.
2470 * \see dt_object_operations::do_declare_xattr_set() in the API description
2473 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2474 struct dt_object *dt,
2475 const struct lu_buf *buf,
2476 const char *name, int fl,
2479 struct dt_object *next = dt_object_child(dt);
2480 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2481 struct lod_object *lo = lod_dt_obj(dt);
2486 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2487 struct lmv_user_md_v1 *lum;
2489 LASSERT(buf != NULL && buf->lb_buf != NULL);
2491 rc = lod_verify_md_striping(d, lum);
2494 } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2495 rc = lod_verify_striping(env, d, lo, buf, false);
2500 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2504 /* Note: Do not set LinkEA on sub-stripes, otherwise
2505 * it will confuse the fid2path process(see mdt_path_current()).
2506 * The linkEA between master and sub-stripes is set in
2507 * lod_xattr_set_lmv(). */
2508 if (strcmp(name, XATTR_NAME_LINK) == 0)
2511 /* set xattr to each stripes, if needed */
2512 rc = lod_striping_load(env, lo);
2516 if (lo->ldo_dir_stripe_count == 0)
2519 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2520 if (!lo->ldo_stripe[i])
2523 if (!dt_object_exists(lo->ldo_stripe[i]))
2526 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2536 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2537 struct lod_object *lo,
2538 struct dt_object *dt, struct thandle *th,
2539 int comp_idx, int stripe_idx,
2540 struct lod_obj_stripe_cb_data *data)
2542 struct lod_thread_info *info = lod_env_info(env);
2543 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
2544 struct filter_fid *ff = &info->lti_ff;
2545 struct lu_buf *buf = &info->lti_buf;
2549 buf->lb_len = sizeof(*ff);
2550 rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2558 * locd_buf is set if it's called by dir migration, which doesn't check
2561 if (data->locd_buf) {
2562 memset(ff, 0, sizeof(*ff));
2563 ff->ff_parent = *(struct lu_fid *)data->locd_buf->lb_buf;
2565 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
2567 if (lu_fid_eq(lod_object_fid(lo), &ff->ff_parent) &&
2568 ff->ff_layout.ol_comp_id == comp->llc_id)
2571 memset(ff, 0, sizeof(*ff));
2572 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2575 /* rewrite filter_fid */
2576 ff->ff_parent.f_ver = stripe_idx;
2577 ff->ff_layout.ol_stripe_size = comp->llc_stripe_size;
2578 ff->ff_layout.ol_stripe_count = comp->llc_stripe_count;
2579 ff->ff_layout.ol_comp_id = comp->llc_id;
2580 ff->ff_layout.ol_comp_start = comp->llc_extent.e_start;
2581 ff->ff_layout.ol_comp_end = comp->llc_extent.e_end;
2582 filter_fid_cpu_to_le(ff, ff, sizeof(*ff));
2584 if (data->locd_declare)
2585 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2586 LU_XATTR_REPLACE, th);
2588 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2589 LU_XATTR_REPLACE, th);
2595 * Reset parent FID on OST object
2597 * Replace parent FID with @dt object FID, which is only called during migration
2598 * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2599 * the FID is changed.
2601 * \param[in] env execution environment
2602 * \param[in] dt dt_object whose stripes's parent FID will be reset
2603 * \parem[in] th thandle
2604 * \param[in] declare if it is declare
2606 * \retval 0 if reset succeeds
2607 * \retval negative errno if reset fails
2609 static int lod_replace_parent_fid(const struct lu_env *env,
2610 struct dt_object *dt,
2611 const struct lu_buf *buf,
2612 struct thandle *th, bool declare)
2614 struct lod_object *lo = lod_dt_obj(dt);
2615 struct lod_thread_info *info = lod_env_info(env);
2616 struct filter_fid *ff;
2617 struct lod_obj_stripe_cb_data data = { { 0 } };
2621 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2623 /* set xattr to each stripes, if needed */
2624 rc = lod_striping_load(env, lo);
2628 if (!lod_obj_is_striped(dt))
2631 if (info->lti_ea_store_size < sizeof(*ff)) {
2632 rc = lod_ea_store_resize(info, sizeof(*ff));
2637 data.locd_declare = declare;
2638 data.locd_stripe_cb = lod_obj_stripe_replace_parent_fid_cb;
2639 data.locd_buf = buf;
2640 rc = lod_obj_for_each_stripe(env, lo, th, &data);
2645 __u16 lod_comp_entry_stripe_count(struct lod_object *lo,
2646 int comp_idx, bool is_dir)
2648 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2649 struct lod_layout_component *entry;
2654 entry = &lo->ldo_comp_entries[comp_idx];
2655 if (lod_comp_inited(entry))
2656 return entry->llc_stripe_count;
2657 else if ((__u16)-1 == entry->llc_stripe_count)
2658 return lod->lod_ost_count;
2660 return lod_get_stripe_count(lod, lo, comp_idx,
2661 entry->llc_stripe_count,
2662 entry->llc_pattern &
2663 LOV_PATTERN_OVERSTRIPING);
2666 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2668 int magic, size = 0, i;
2669 struct lod_layout_component *comp_entries;
2671 bool is_composite, is_foreign = false;
2674 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2675 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2677 lo->ldo_def_striping->lds_def_striping_is_composite;
2679 comp_cnt = lo->ldo_comp_cnt;
2680 comp_entries = lo->ldo_comp_entries;
2681 is_composite = lo->ldo_is_composite;
2682 is_foreign = lo->ldo_is_foreign;
2686 return lo->ldo_foreign_lov_size;
2688 LASSERT(comp_cnt != 0 && comp_entries != NULL);
2690 size = sizeof(struct lov_comp_md_v1) +
2691 sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2692 LASSERT(size % sizeof(__u64) == 0);
2695 for (i = 0; i < comp_cnt; i++) {
2698 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2699 stripe_count = lod_comp_entry_stripe_count(lo, i, is_dir);
2700 if (!is_dir && is_composite)
2701 lod_comp_shrink_stripe_count(&comp_entries[i],
2704 size += lov_user_md_size(stripe_count, magic);
2705 LASSERT(size % sizeof(__u64) == 0);
2711 * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2712 * the xattr value is binary lov_comp_md_v1 which contains component(s)
2715 * \param[in] env execution environment
2716 * \param[in] dt dt_object to add components on
2717 * \param[in] buf buffer contains components to be added
2718 * \parem[in] th thandle
2720 * \retval 0 on success
2721 * \retval negative errno on failure
2723 static int lod_declare_layout_add(const struct lu_env *env,
2724 struct dt_object *dt,
2725 const struct lu_buf *buf,
2728 struct lod_thread_info *info = lod_env_info(env);
2729 struct lod_layout_component *comp_array, *lod_comp, *old_array;
2730 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2731 struct dt_object *next = dt_object_child(dt);
2732 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
2733 struct lod_object *lo = lod_dt_obj(dt);
2734 struct lov_user_md_v3 *v3;
2735 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2737 int i, rc, array_cnt, old_array_cnt;
2740 LASSERT(lo->ldo_is_composite);
2742 if (lo->ldo_flr_state != LCM_FL_NONE)
2745 rc = lod_verify_striping(env, d, lo, buf, false);
2749 magic = comp_v1->lcm_magic;
2750 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2751 lustre_swab_lov_comp_md_v1(comp_v1);
2752 magic = comp_v1->lcm_magic;
2755 if (magic != LOV_USER_MAGIC_COMP_V1)
2758 mutex_lock(&lo->ldo_layout_mutex);
2760 array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2761 OBD_ALLOC_PTR_ARRAY(comp_array, array_cnt);
2762 if (comp_array == NULL) {
2763 mutex_unlock(&lo->ldo_layout_mutex);
2768 memcpy(comp_array, lo->ldo_comp_entries,
2769 sizeof(*comp_array) * lo->ldo_comp_cnt);
2771 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2772 struct lov_user_md_v1 *v1;
2773 struct lu_extent *ext;
2775 v1 = (struct lov_user_md *)((char *)comp_v1 +
2776 comp_v1->lcm_entries[i].lcme_offset);
2777 ext = &comp_v1->lcm_entries[i].lcme_extent;
2779 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2780 lod_comp->llc_extent.e_start = ext->e_start;
2781 lod_comp->llc_extent.e_end = ext->e_end;
2782 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2783 lod_comp->llc_flags = comp_v1->lcm_entries[i].lcme_flags;
2785 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2786 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2787 lod_adjust_stripe_info(lod_comp, desc, 0);
2789 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2790 v3 = (struct lov_user_md_v3 *) v1;
2791 if (v3->lmm_pool_name[0] != '\0') {
2792 rc = lod_set_pool(&lod_comp->llc_pool,
2800 old_array = lo->ldo_comp_entries;
2801 old_array_cnt = lo->ldo_comp_cnt;
2803 lo->ldo_comp_entries = comp_array;
2804 lo->ldo_comp_cnt = array_cnt;
2806 /* No need to increase layout generation here, it will be increased
2807 * later when generating component ID for the new components */
2809 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2810 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2811 XATTR_NAME_LOV, 0, th);
2813 lo->ldo_comp_entries = old_array;
2814 lo->ldo_comp_cnt = old_array_cnt;
2818 OBD_FREE_PTR_ARRAY(old_array, old_array_cnt);
2820 LASSERT(lo->ldo_mirror_count == 1);
2821 lo->ldo_mirrors[0].lme_end = array_cnt - 1;
2823 mutex_unlock(&lo->ldo_layout_mutex);
2828 for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2829 lod_comp = &comp_array[i];
2830 if (lod_comp->llc_pool != NULL) {
2831 OBD_FREE(lod_comp->llc_pool,
2832 strlen(lod_comp->llc_pool) + 1);
2833 lod_comp->llc_pool = NULL;
2836 OBD_FREE_PTR_ARRAY(comp_array, array_cnt);
2837 mutex_unlock(&lo->ldo_layout_mutex);
2843 * lod_last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2844 * @mirror_id: Mirror id to be checked.
2847 * This function checks if a mirror with specified @mirror_id is the last
2848 * non-stale mirror of a LOD object @lo.
2850 * Return: true or false.
2853 bool lod_last_non_stale_mirror(__u16 mirror_id, struct lod_object *lo)
2855 struct lod_layout_component *lod_comp;
2856 bool has_stale_flag;
2859 for (i = 0; i < lo->ldo_mirror_count; i++) {
2860 if (lo->ldo_mirrors[i].lme_id == mirror_id ||
2861 lo->ldo_mirrors[i].lme_stale)
2864 has_stale_flag = false;
2865 lod_foreach_mirror_comp(lod_comp, lo, i) {
2866 if (lod_comp->llc_flags & LCME_FL_STALE) {
2867 has_stale_flag = true;
2871 if (!has_stale_flag)
2879 * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2880 * the '$field' can only be 'flags' now. The xattr value is binary
2881 * lov_comp_md_v1 which contains the component ID(s) and the value of
2882 * the field to be modified.
2883 * Please update allowed_lustre_lov macro if $field groks more values
2886 * \param[in] env execution environment
2887 * \param[in] dt dt_object to be modified
2888 * \param[in] op operation string, like "set.flags"
2889 * \param[in] buf buffer contains components to be set
2890 * \parem[in] th thandle
2892 * \retval 0 on success
2893 * \retval negative errno on failure
2895 static int lod_declare_layout_set(const struct lu_env *env,
2896 struct dt_object *dt,
2897 char *op, const struct lu_buf *buf,
2900 struct lod_layout_component *lod_comp;
2901 struct lod_thread_info *info = lod_env_info(env);
2902 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2903 struct lod_object *lo = lod_dt_obj(dt);
2904 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2907 bool changed = false;
2910 /* Please update allowed_lustre_lov macro if op
2911 * groks more values in the future
2913 if (strcmp(op, "set.flags") != 0) {
2914 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2915 lod2obd(d)->obd_name, op);
2919 magic = comp_v1->lcm_magic;
2920 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2921 lustre_swab_lov_comp_md_v1(comp_v1);
2922 magic = comp_v1->lcm_magic;
2925 if (magic != LOV_USER_MAGIC_COMP_V1)
2928 if (comp_v1->lcm_entry_count == 0) {
2929 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2930 lod2obd(d)->obd_name);
2934 mutex_lock(&lo->ldo_layout_mutex);
2935 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2936 __u32 id = comp_v1->lcm_entries[i].lcme_id;
2937 __u32 flags = comp_v1->lcm_entries[i].lcme_flags;
2938 __u32 mirror_flag = flags & LCME_MIRROR_FLAGS;
2939 __u16 mirror_id = mirror_id_of(id);
2940 bool neg = flags & LCME_FL_NEG;
2942 if (flags & LCME_FL_INIT) {
2944 lod_striping_free_nolock(env, lo);
2945 mutex_unlock(&lo->ldo_layout_mutex);
2949 flags &= ~(LCME_MIRROR_FLAGS | LCME_FL_NEG);
2950 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2951 lod_comp = &lo->ldo_comp_entries[j];
2953 /* lfs only put one flag in each entry */
2954 if ((flags && id != lod_comp->llc_id) ||
2955 (mirror_flag && mirror_id !=
2956 mirror_id_of(lod_comp->llc_id)))
2961 lod_comp->llc_flags &= ~flags;
2963 lod_comp->llc_flags &= ~mirror_flag;
2966 if ((flags & LCME_FL_STALE) &&
2967 lod_last_non_stale_mirror(mirror_id,
2970 &lo->ldo_layout_mutex);
2973 lod_comp->llc_flags |= flags;
2976 lod_comp->llc_flags |= mirror_flag;
2977 if (mirror_flag & LCME_FL_NOSYNC)
2978 lod_comp->llc_timestamp =
2979 ktime_get_real_seconds();
2985 mutex_unlock(&lo->ldo_layout_mutex);
2988 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2989 lod2obd(d)->obd_name);
2993 lod_obj_inc_layout_gen(lo);
2995 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2996 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), &info->lti_buf,
2997 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3002 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
3003 * and the xattr value is a unique component ID or a special lcme_id.
3005 * \param[in] env execution environment
3006 * \param[in] dt dt_object to be operated on
3007 * \param[in] buf buffer contains component ID or lcme_id
3008 * \parem[in] th thandle
3010 * \retval 0 on success
3011 * \retval negative errno on failure
3013 static int lod_declare_layout_del(const struct lu_env *env,
3014 struct dt_object *dt,
3015 const struct lu_buf *buf,
3018 struct lod_thread_info *info = lod_env_info(env);
3019 struct dt_object *next = dt_object_child(dt);
3020 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3021 struct lod_object *lo = lod_dt_obj(dt);
3022 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3023 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
3024 __u32 magic, id, flags, neg_flags = 0;
3028 LASSERT(lo->ldo_is_composite);
3030 if (lo->ldo_flr_state != LCM_FL_NONE)
3033 magic = comp_v1->lcm_magic;
3034 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3035 lustre_swab_lov_comp_md_v1(comp_v1);
3036 magic = comp_v1->lcm_magic;
3039 if (magic != LOV_USER_MAGIC_COMP_V1)
3042 id = comp_v1->lcm_entries[0].lcme_id;
3043 flags = comp_v1->lcm_entries[0].lcme_flags;
3045 if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
3046 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
3047 lod2obd(d)->obd_name, id, flags);
3051 if (id != LCME_ID_INVAL && flags != 0) {
3052 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
3053 lod2obd(d)->obd_name);
3057 if (id == LCME_ID_INVAL && !flags) {
3058 CDEBUG(D_LAYOUT, "%s: no id or flags specified.\n",
3059 lod2obd(d)->obd_name);
3063 if (flags & LCME_FL_NEG) {
3064 neg_flags = flags & ~LCME_FL_NEG;
3068 mutex_lock(&lo->ldo_layout_mutex);
3070 left = lo->ldo_comp_cnt;
3072 mutex_unlock(&lo->ldo_layout_mutex);
3076 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3077 struct lod_layout_component *lod_comp;
3079 lod_comp = &lo->ldo_comp_entries[i];
3081 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
3083 else if (flags && !(flags & lod_comp->llc_flags))
3085 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
3088 if (left != (i + 1)) {
3089 CDEBUG(D_LAYOUT, "%s: this deletion will create "
3090 "a hole.\n", lod2obd(d)->obd_name);
3091 mutex_unlock(&lo->ldo_layout_mutex);
3096 /* Mark the component as deleted */
3097 lod_comp->llc_id = LCME_ID_INVAL;
3099 /* Not instantiated component */
3100 if (lod_comp->llc_stripe == NULL)
3103 LASSERT(lod_comp->llc_stripe_count > 0);
3104 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
3105 struct dt_object *obj = lod_comp->llc_stripe[j];
3109 rc = lod_sub_declare_destroy(env, obj, th);
3111 mutex_unlock(&lo->ldo_layout_mutex);
3117 LASSERTF(left >= 0, "left = %d\n", left);
3118 if (left == lo->ldo_comp_cnt) {
3119 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
3120 lod2obd(d)->obd_name, id);
3121 mutex_unlock(&lo->ldo_layout_mutex);
3125 mutex_unlock(&lo->ldo_layout_mutex);
3127 memset(attr, 0, sizeof(*attr));
3128 attr->la_valid = LA_SIZE;
3129 rc = lod_sub_declare_attr_set(env, next, attr, th);
3134 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
3135 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
3136 XATTR_NAME_LOV, 0, th);
3138 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
3145 * Declare layout add/set/del operations issued by special xattr names:
3147 * XATTR_LUSTRE_LOV.add add component(s) to existing file
3148 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
3149 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
3151 * \param[in] env execution environment
3152 * \param[in] dt object
3153 * \param[in] name name of xattr
3154 * \param[in] buf lu_buf contains xattr value
3155 * \param[in] th transaction handle
3157 * \retval 0 on success
3158 * \retval negative if failed
3160 static int lod_declare_modify_layout(const struct lu_env *env,
3161 struct dt_object *dt,
3163 const struct lu_buf *buf,
3166 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3167 struct lod_object *lo = lod_dt_obj(dt);
3169 int rc, len = strlen(XATTR_LUSTRE_LOV);
3172 LASSERT(dt_object_exists(dt));
3174 if (strlen(name) <= len || name[len] != '.') {
3175 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
3176 lod2obd(d)->obd_name, name);
3181 rc = lod_striping_load(env, lo);
3185 /* the layout to be modified must be a composite layout */
3186 if (!lo->ldo_is_composite) {
3187 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
3188 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3189 GOTO(unlock, rc = -EINVAL);
3192 op = (char *)name + len;
3193 if (strcmp(op, "add") == 0) {
3194 rc = lod_declare_layout_add(env, dt, buf, th);
3195 } else if (strcmp(op, "del") == 0) {
3196 rc = lod_declare_layout_del(env, dt, buf, th);
3197 } else if (strncmp(op, "set", strlen("set")) == 0) {
3198 rc = lod_declare_layout_set(env, dt, op, buf, th);
3200 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
3201 lod2obd(d)->obd_name, name);
3202 GOTO(unlock, rc = -ENOTSUPP);
3206 lod_striping_free(env, lo);
3212 * Convert a plain file lov_mds_md to a composite layout.
3214 * \param[in,out] info the thread info::lti_ea_store buffer contains little
3215 * endian plain file layout
3217 * \retval 0 on success, <0 on failure
3219 static int lod_layout_convert(struct lod_thread_info *info)
3221 struct lov_mds_md *lmm = info->lti_ea_store;
3222 struct lov_mds_md *lmm_save;
3223 struct lov_comp_md_v1 *lcm;
3224 struct lov_comp_md_entry_v1 *lcme;
3230 /* realloc buffer to a composite layout which contains one component */
3231 blob_size = lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
3232 le32_to_cpu(lmm->lmm_magic));
3233 size = sizeof(*lcm) + sizeof(*lcme) + blob_size;
3235 OBD_ALLOC_LARGE(lmm_save, blob_size);
3237 GOTO(out, rc = -ENOMEM);
3239 memcpy(lmm_save, lmm, blob_size);
3241 if (info->lti_ea_store_size < size) {
3242 rc = lod_ea_store_resize(info, size);
3247 lcm = info->lti_ea_store;
3248 memset(lcm, 0, sizeof(*lcm) + sizeof(*lcme));
3249 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
3250 lcm->lcm_size = cpu_to_le32(size);
3251 lcm->lcm_layout_gen = cpu_to_le32(le16_to_cpu(
3252 lmm_save->lmm_layout_gen));
3253 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
3254 lcm->lcm_entry_count = cpu_to_le16(1);
3256 lcme = &lcm->lcm_entries[0];
3257 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
3258 lcme->lcme_extent.e_start = 0;
3259 lcme->lcme_extent.e_end = cpu_to_le64(OBD_OBJECT_EOF);
3260 lcme->lcme_offset = cpu_to_le32(sizeof(*lcm) + sizeof(*lcme));
3261 lcme->lcme_size = cpu_to_le32(blob_size);
3263 memcpy((char *)lcm + lcme->lcme_offset, (char *)lmm_save, blob_size);
3268 OBD_FREE_LARGE(lmm_save, blob_size);
3273 * Merge layouts to form a mirrored file.
3275 static int lod_declare_layout_merge(const struct lu_env *env,
3276 struct dt_object *dt, const struct lu_buf *mbuf,
3279 struct lod_thread_info *info = lod_env_info(env);
3280 struct lu_attr *layout_attr = &info->lti_layout_attr;
3281 struct lu_buf *buf = &info->lti_buf;
3282 struct lod_object *lo = lod_dt_obj(dt);
3283 struct lov_comp_md_v1 *lcm;
3284 struct lov_comp_md_v1 *cur_lcm;
3285 struct lov_comp_md_v1 *merge_lcm;
3286 struct lov_comp_md_entry_v1 *lcme;
3287 struct lov_mds_md_v1 *lmm;
3290 __u16 cur_entry_count;
3291 __u16 merge_entry_count;
3293 __u16 mirror_id = 0;
3300 merge_lcm = mbuf->lb_buf;
3301 if (mbuf->lb_len < sizeof(*merge_lcm))
3304 /* must be an existing layout from disk */
3305 if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
3308 merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
3310 /* do not allow to merge two mirrored files */
3311 if (le16_to_cpu(merge_lcm->lcm_mirror_count))
3314 /* verify the target buffer */
3315 rc = lod_get_lov_ea(env, lo);
3317 RETURN(rc ? : -ENODATA);
3319 cur_lcm = info->lti_ea_store;
3320 switch (le32_to_cpu(cur_lcm->lcm_magic)) {
3323 rc = lod_layout_convert(info);
3325 case LOV_MAGIC_COMP_V1:
3335 /* info->lti_ea_store could be reallocated in lod_layout_convert() */
3336 cur_lcm = info->lti_ea_store;
3337 cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
3339 /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
3340 mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
3341 if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
3344 /* size of new layout */
3345 size = le32_to_cpu(cur_lcm->lcm_size) +
3346 le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
3348 memset(buf, 0, sizeof(*buf));
3349 lu_buf_alloc(buf, size);
3350 if (buf->lb_buf == NULL)
3354 memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
3356 offset = sizeof(*lcm) +
3357 sizeof(*lcme) * (cur_entry_count + merge_entry_count);
3358 for (i = 0; i < cur_entry_count; i++) {
3359 struct lov_comp_md_entry_v1 *cur_lcme;
3361 lcme = &lcm->lcm_entries[i];
3362 cur_lcme = &cur_lcm->lcm_entries[i];
3364 lcme->lcme_offset = cpu_to_le32(offset);
3365 memcpy((char *)lcm + offset,
3366 (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
3367 le32_to_cpu(lcme->lcme_size));
3369 offset += le32_to_cpu(lcme->lcme_size);
3371 if (mirror_count == 1 &&
3372 mirror_id_of(le32_to_cpu(lcme->lcme_id)) == 0) {
3373 /* Add mirror from a non-flr file, create new mirror ID.
3374 * Otherwise, keep existing mirror's component ID, used
3375 * for mirror extension.
3377 id = pflr_id(1, i + 1);
3378 lcme->lcme_id = cpu_to_le32(id);
3381 id = max(le32_to_cpu(lcme->lcme_id), id);
3384 mirror_id = mirror_id_of(id) + 1;
3386 /* check if first entry in new layout is DOM */
3387 lmm = (struct lov_mds_md_v1 *)((char *)merge_lcm +
3388 merge_lcm->lcm_entries[0].lcme_offset);
3389 merge_has_dom = lov_pattern(le32_to_cpu(lmm->lmm_pattern)) ==
3392 for (i = 0; i < merge_entry_count; i++) {
3393 struct lov_comp_md_entry_v1 *merge_lcme;
3395 merge_lcme = &merge_lcm->lcm_entries[i];
3396 lcme = &lcm->lcm_entries[cur_entry_count + i];
3398 *lcme = *merge_lcme;
3399 lcme->lcme_offset = cpu_to_le32(offset);
3400 if (merge_has_dom && i == 0)
3401 lcme->lcme_flags |= cpu_to_le32(LCME_FL_STALE);
3403 id = pflr_id(mirror_id, i + 1);
3404 lcme->lcme_id = cpu_to_le32(id);
3406 memcpy((char *)lcm + offset,
3407 (char *)merge_lcm + le32_to_cpu(merge_lcme->lcme_offset),
3408 le32_to_cpu(lcme->lcme_size));
3410 offset += le32_to_cpu(lcme->lcme_size);
3413 /* fixup layout information */
3414 lcm->lcm_size = cpu_to_le32(size);
3415 lcm->lcm_entry_count = cpu_to_le16(cur_entry_count + merge_entry_count);
3416 lcm->lcm_mirror_count = cpu_to_le16(mirror_count);
3417 if ((le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK) == LCM_FL_NONE)
3418 lcm->lcm_flags = cpu_to_le32(LCM_FL_RDONLY);
3420 rc = lod_striping_reload(env, lo, buf, 0);
3424 lod_obj_inc_layout_gen(lo);
3425 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3427 /* transfer layout version to OST objects. */
3428 if (lo->ldo_mirror_count > 1) {
3429 struct lod_obj_stripe_cb_data data = { {0} };
3431 layout_attr->la_valid = LA_LAYOUT_VERSION;
3432 layout_attr->la_layout_version = 0;
3433 data.locd_attr = layout_attr;
3434 data.locd_declare = true;
3435 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
3436 rc = lod_obj_for_each_stripe(env, lo, th, &data);
3441 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), buf,
3442 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3450 * Split layouts, just set the LOVEA with the layout from mbuf.
3452 static int lod_declare_layout_split(const struct lu_env *env,
3453 struct dt_object *dt, const struct lu_buf *mbuf,
3456 struct lod_thread_info *info = lod_env_info(env);
3457 struct lu_attr *layout_attr = &info->lti_layout_attr;
3458 struct lod_object *lo = lod_dt_obj(dt);
3459 struct lov_comp_md_v1 *lcm = mbuf->lb_buf;
3463 rc = lod_striping_reload(env, lo, mbuf, LVF_ALL_STALE);
3467 lod_obj_inc_layout_gen(lo);
3468 /* fix on-disk layout gen */
3469 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3472 /* transfer layout version to OST objects. */
3473 if (lo->ldo_mirror_count > 1) {
3474 struct lod_obj_stripe_cb_data data = { {0} };
3476 layout_attr->la_valid = LA_LAYOUT_VERSION;
3477 layout_attr->la_layout_version = 0;
3478 data.locd_attr = layout_attr;
3479 data.locd_declare = true;
3480 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
3481 rc = lod_obj_for_each_stripe(env, lo, th, &data);
3486 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), mbuf,
3487 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3491 static int lod_layout_declare_or_purge_mirror(const struct lu_env *env,
3492 struct dt_object *dt, const struct lu_buf *buf,
3493 struct thandle *th, bool declare)
3495 struct lod_thread_info *info = lod_env_info(env);
3496 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3497 struct lod_object *lo = lod_dt_obj(dt);
3498 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
3499 struct lov_comp_md_entry_v1 *entry;
3500 struct lov_mds_md_v1 *lmm;
3501 struct dt_object **sub_objs = NULL;
3502 int rc = 0, i, k, array_count = 0;
3507 * other ops (like lod_declare_destroy) could destroying sub objects
3510 mutex_lock(&lo->ldo_layout_mutex);
3513 /* prepare sub-objects array */
3514 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3515 entry = &comp_v1->lcm_entries[i];
3517 if (!(entry->lcme_flags & LCME_FL_INIT))
3520 lmm = (struct lov_mds_md_v1 *)
3521 ((char *)comp_v1 + entry->lcme_offset);
3522 array_count += lmm->lmm_stripe_count;
3524 OBD_ALLOC_PTR_ARRAY(sub_objs, array_count);
3525 if (sub_objs == NULL) {
3526 mutex_unlock(&lo->ldo_layout_mutex);
3531 k = 0; /* sub_objs index */
3532 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3533 struct lov_ost_data_v1 *objs;
3534 struct lu_object *o, *n;
3535 struct dt_object *dto;
3536 struct lu_device *nd;
3537 struct lov_mds_md_v3 *v3;
3541 entry = &comp_v1->lcm_entries[i];
3543 if (!(entry->lcme_flags & LCME_FL_INIT))
3546 lmm = (struct lov_mds_md_v1 *)
3547 ((char *)comp_v1 + entry->lcme_offset);
3548 v3 = (struct lov_mds_md_v3 *)lmm;
3549 if (lmm->lmm_magic == LOV_MAGIC_V3)
3550 objs = &v3->lmm_objects[0];
3552 objs = &lmm->lmm_objects[0];
3554 for (j = 0; j < lmm->lmm_stripe_count; j++) {
3555 idx = objs[j].l_ost_idx;
3556 rc = ostid_to_fid(&info->lti_fid, &objs[j].l_ost_oi,
3561 if (!fid_is_sane(&info->lti_fid)) {
3562 CERROR("%s: sub-object insane fid "DFID"\n",
3563 lod2obd(d)->obd_name,
3564 PFID(&info->lti_fid));
3565 GOTO(out, rc = -EINVAL);
3568 lod_getref(&d->lod_ost_descs);
3570 rc = validate_lod_and_idx(d, idx);
3572 lod_putref(d, &d->lod_ost_descs);
3576 nd = &OST_TGT(d, idx)->ltd_tgt->dd_lu_dev;
3577 lod_putref(d, &d->lod_ost_descs);
3579 o = lu_object_find_at(env, nd, &info->lti_fid, NULL);
3581 GOTO(out, rc = PTR_ERR(o));
3583 n = lu_object_locate(o->lo_header, nd->ld_type);
3585 lu_object_put(env, n);
3586 GOTO(out, rc = -ENOENT);
3589 dto = container_of(n, struct dt_object, do_lu);
3592 rc = lod_sub_declare_destroy(env, dto, th);
3593 dt_object_put(env, dto);
3598 * collect to-be-destroyed sub objects, the
3599 * reference would be released after actual
3605 } /* for each stripe */
3606 } /* for each component in the mirror */
3611 /* destroy the sub objects */
3612 for (; i < k; i++) {
3613 rc = lod_sub_destroy(env, sub_objs[i], th);
3616 dt_object_put(env, sub_objs[i]);
3620 * if a sub object destroy failed, we'd release sub objects
3621 * reference get from above sub_objs collection.
3624 dt_object_put(env, sub_objs[i]);
3626 OBD_FREE_PTR_ARRAY(sub_objs, array_count);
3628 mutex_unlock(&lo->ldo_layout_mutex);
3634 * Purge layouts, delete sub objects in the mirror stored in the vic_buf,
3635 * and set the LOVEA with the layout from mbuf.
3637 static int lod_declare_layout_purge(const struct lu_env *env,
3638 struct dt_object *dt, const struct lu_buf *buf,
3641 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3642 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
3647 if (le32_to_cpu(comp_v1->lcm_magic) != LOV_MAGIC_COMP_V1) {
3648 CERROR("%s: invalid layout magic %#x != %#x\n",
3649 lod2obd(d)->obd_name, le32_to_cpu(comp_v1->lcm_magic),
3654 if (cpu_to_le32(LOV_MAGIC_COMP_V1) != LOV_MAGIC_COMP_V1)
3655 lustre_swab_lov_comp_md_v1(comp_v1);
3657 /* from now on, @buf contains cpu endian data */
3659 if (comp_v1->lcm_mirror_count != 0) {
3660 CERROR("%s: can only purge one mirror from "DFID"\n",
3661 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3665 /* delcare sub objects deletion in the mirror stored in @buf */
3666 rc = lod_layout_declare_or_purge_mirror(env, dt, buf, th, true);
3670 /* delete sub objects from the mirror stored in @buf */
3671 static int lod_layout_purge(const struct lu_env *env, struct dt_object *dt,
3672 const struct lu_buf *buf, struct thandle *th)
3677 rc = lod_layout_declare_or_purge_mirror(env, dt, buf, th, false);
3682 * Implementation of dt_object_operations::do_declare_xattr_set.
3684 * \see dt_object_operations::do_declare_xattr_set() in the API description
3687 * the extension to the API:
3688 * - declaring LOVEA requests striping creation
3689 * - LU_XATTR_REPLACE means layout swap
3691 static int lod_declare_xattr_set(const struct lu_env *env,
3692 struct dt_object *dt,
3693 const struct lu_buf *buf,
3694 const char *name, int fl,
3697 struct dt_object *next = dt_object_child(dt);
3698 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3703 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
3704 if ((S_ISREG(mode) || mode == 0) &&
3705 !(fl & (LU_XATTR_REPLACE | LU_XATTR_MERGE | LU_XATTR_SPLIT |
3707 (strcmp(name, XATTR_NAME_LOV) == 0 ||
3708 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
3710 * this is a request to create object's striping.
3712 * allow to declare predefined striping on a new (!mode) object
3713 * which is supposed to be replay of regular file creation
3714 * (when LOV setting is declared)
3716 * LU_XATTR_REPLACE is set to indicate a layout swap
3718 if (dt_object_exists(dt)) {
3719 rc = dt_attr_get(env, next, attr);
3723 memset(attr, 0, sizeof(*attr));
3724 attr->la_valid = LA_TYPE | LA_MODE;
3725 attr->la_mode = S_IFREG;
3727 rc = lod_declare_striped_create(env, dt, attr, buf, th);
3728 } else if (fl & LU_XATTR_MERGE) {
3729 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3730 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3731 rc = lod_declare_layout_merge(env, dt, buf, th);
3732 } else if (fl & LU_XATTR_SPLIT) {
3733 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3734 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3735 rc = lod_declare_layout_split(env, dt, buf, th);
3736 } else if (fl & LU_XATTR_PURGE) {
3737 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3738 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3739 rc = lod_declare_layout_purge(env, dt, buf, th);
3740 } else if (S_ISREG(mode) &&
3741 strlen(name) >= sizeof(XATTR_LUSTRE_LOV) + 3 &&
3742 allowed_lustre_lov(name)) {
3744 * this is a request to modify object's striping.
3745 * add/set/del component(s).
3747 if (!dt_object_exists(dt))
3750 rc = lod_declare_modify_layout(env, dt, name, buf, th);
3751 } else if (S_ISDIR(mode)) {
3752 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
3753 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3754 rc = lod_replace_parent_fid(env, dt, buf, th, true);
3756 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
3763 * Apply xattr changes to the object.
3765 * Applies xattr changes to the object and the stripes if the latter exist.
3767 * \param[in] env execution environment
3768 * \param[in] dt object
3769 * \param[in] buf buffer pointing to the new value of xattr
3770 * \param[in] name name of xattr
3771 * \param[in] fl flags
3772 * \param[in] th transaction handle
3774 * \retval 0 on success
3775 * \retval negative if failed
3777 static int lod_xattr_set_internal(const struct lu_env *env,
3778 struct dt_object *dt,
3779 const struct lu_buf *buf,
3780 const char *name, int fl,
3783 struct dt_object *next = dt_object_child(dt);
3784 struct lod_object *lo = lod_dt_obj(dt);
3789 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3790 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3793 /* Note: Do not set LinkEA on sub-stripes, otherwise
3794 * it will confuse the fid2path process(see mdt_path_current()).
3795 * The linkEA between master and sub-stripes is set in
3796 * lod_xattr_set_lmv(). */
3797 if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
3800 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3801 if (!lo->ldo_stripe[i])
3804 if (!dt_object_exists(lo->ldo_stripe[i]))
3807 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
3817 * Delete an extended attribute.
3819 * Deletes specified xattr from the object and the stripes if the latter exist.
3821 * \param[in] env execution environment
3822 * \param[in] dt object
3823 * \param[in] name name of xattr
3824 * \param[in] th transaction handle
3826 * \retval 0 on success
3827 * \retval negative if failed
3829 static int lod_xattr_del_internal(const struct lu_env *env,
3830 struct dt_object *dt,
3831 const char *name, struct thandle *th)
3833 struct dt_object *next = dt_object_child(dt);
3834 struct lod_object *lo = lod_dt_obj(dt);
3840 rc = lod_sub_xattr_del(env, next, name, th);
3841 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3844 if (lo->ldo_dir_stripe_count == 0)
3847 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3848 if (!lo->ldo_stripe[i])
3851 if (!dt_object_exists(lo->ldo_stripe[i]))
3854 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3863 * Set default striping on a directory.
3865 * Sets specified striping on a directory object unless it matches the default
3866 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3867 * EA. This striping will be used when regular file is being created in this
3870 * \param[in] env execution environment
3871 * \param[in] dt the striped object
3872 * \param[in] buf buffer with the striping
3873 * \param[in] name name of EA
3874 * \param[in] fl xattr flag (see OSD API description)
3875 * \param[in] th transaction handle
3877 * \retval 0 on success
3878 * \retval negative if failed
3880 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
3881 struct dt_object *dt,
3882 const struct lu_buf *buf,
3883 const char *name, int fl,
3886 struct lov_user_md_v1 *lum;
3887 struct lov_user_md_v3 *v3 = NULL;
3888 const char *pool_name = NULL;
3893 LASSERT(buf != NULL && buf->lb_buf != NULL);
3896 switch (lum->lmm_magic) {
3897 case LOV_USER_MAGIC_SPECIFIC:
3898 case LOV_USER_MAGIC_V3:
3900 if (v3->lmm_pool_name[0] != '\0')
3901 pool_name = v3->lmm_pool_name;
3903 case LOV_USER_MAGIC_V1:
3904 /* if { size, offset, count } = { 0, -1, 0 } and no pool
3905 * (i.e. all default values specified) then delete default
3906 * striping from dir. */
3908 "set default striping: sz %u # %u offset %d %s %s\n",
3909 (unsigned)lum->lmm_stripe_size,
3910 (unsigned)lum->lmm_stripe_count,
3911 (int)lum->lmm_stripe_offset,
3912 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
3914 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
3915 lum->lmm_stripe_count,
3916 lum->lmm_stripe_offset,
3919 case LOV_USER_MAGIC_COMP_V1:
3921 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lum;
3922 struct lov_comp_md_entry_v1 *lcme;
3925 comp_cnt = le16_to_cpu(lcm->lcm_entry_count);
3926 for (i = 0; i < comp_cnt; i++) {
3927 lcme = &lcm->lcm_entries[i];
3928 if (lcme->lcme_flags & cpu_to_le32(LCME_FL_EXTENSION)) {
3929 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_SEL);
3938 CERROR("Invalid magic %x\n", lum->lmm_magic);
3943 rc = lod_xattr_del_internal(env, dt, name, th);
3947 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3954 * Set default striping on a directory object.
3956 * Sets specified striping on a directory object unless it matches the default
3957 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3958 * EA. This striping will be used when a new directory is being created in the
3961 * \param[in] env execution environment
3962 * \param[in] dt the striped object
3963 * \param[in] buf buffer with the striping
3964 * \param[in] name name of EA
3965 * \param[in] fl xattr flag (see OSD API description)
3966 * \param[in] th transaction handle
3968 * \retval 0 on success
3969 * \retval negative if failed
3971 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
3972 struct dt_object *dt,
3973 const struct lu_buf *buf,
3974 const char *name, int fl,
3977 struct lmv_user_md_v1 *lum;
3982 LASSERT(buf != NULL && buf->lb_buf != NULL);
3986 "set default stripe_count # %u stripe_offset %d hash %u\n",
3987 le32_to_cpu(lum->lum_stripe_count),
3988 (int)le32_to_cpu(lum->lum_stripe_offset),
3989 le32_to_cpu(lum->lum_hash_type));
3991 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
3992 le32_to_cpu(lum->lum_stripe_offset)) &&
3993 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
3994 rc = lod_xattr_del_internal(env, dt, name, th);
3998 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
4007 * Turn directory into a striped directory.
4009 * During replay the client sends the striping created before MDT
4010 * failure, then the layer above LOD sends this defined striping
4011 * using ->do_xattr_set(), so LOD uses this method to replay creation
4012 * of the stripes. Notice the original information for the striping
4013 * (#stripes, FIDs, etc) was transferred in declare path.
4015 * \param[in] env execution environment
4016 * \param[in] dt the striped object
4017 * \param[in] buf not used currently
4018 * \param[in] name not used currently
4019 * \param[in] fl xattr flag (see OSD API description)
4020 * \param[in] th transaction handle
4022 * \retval 0 on success
4023 * \retval negative if failed
4025 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
4026 const struct lu_buf *buf, const char *name,
4027 int fl, struct thandle *th)
4029 struct lod_object *lo = lod_dt_obj(dt);
4030 struct lod_thread_info *info = lod_env_info(env);
4031 struct lu_attr *attr = &info->lti_attr;
4032 struct dt_object_format *dof = &info->lti_format;
4033 struct lu_buf lmv_buf;
4034 struct lu_buf slave_lmv_buf;
4035 struct lmv_mds_md_v1 *lmm;
4036 struct lmv_mds_md_v1 *slave_lmm = NULL;
4037 struct dt_insert_rec *rec = &info->lti_dt_rec;
4042 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4045 /* The stripes are supposed to be allocated in declare phase,
4046 * if there are no stripes being allocated, it will skip */
4047 if (lo->ldo_dir_stripe_count == 0) {
4048 if (lo->ldo_dir_is_foreign) {
4049 rc = lod_sub_xattr_set(env, dt_object_child(dt), buf,
4050 XATTR_NAME_LMV, fl, th);
4057 rc = dt_attr_get(env, dt_object_child(dt), attr);
4061 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_FLAGS |
4062 LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
4063 dof->dof_type = DFT_DIR;
4065 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
4068 lmm = lmv_buf.lb_buf;
4070 OBD_ALLOC_PTR(slave_lmm);
4071 if (slave_lmm == NULL)
4074 lod_prep_slave_lmv_md(slave_lmm, lmm);
4075 slave_lmv_buf.lb_buf = slave_lmm;
4076 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
4078 rec->rec_type = S_IFDIR;
4079 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4080 struct dt_object *dto = lo->ldo_stripe[i];
4081 char *stripe_name = info->lti_key;
4082 struct lu_name *sname;
4083 struct linkea_data ldata = { NULL };
4084 struct lu_buf linkea_buf;
4086 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
4090 /* fail a remote stripe creation */
4091 if (i && OBD_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_CREATE))
4094 /* don't create stripe if:
4095 * 1. it's source stripe of migrating directory
4096 * 2. it's existed stripe of splitting directory
4098 if ((lod_is_migrating(lo) && i >= lo->ldo_dir_migrate_offset) ||
4099 (lod_is_splitting(lo) && i < lo->ldo_dir_split_offset)) {
4100 if (!dt_object_exists(dto))
4101 GOTO(out, rc = -EINVAL);
4103 dt_write_lock(env, dto, DT_TGT_CHILD);
4104 rc = lod_sub_create(env, dto, attr, NULL, dof, th);
4106 dt_write_unlock(env, dto);
4110 rc = lod_sub_ref_add(env, dto, th);
4111 dt_write_unlock(env, dto);
4115 rec->rec_fid = lu_object_fid(&dto->do_lu);
4116 rc = lod_sub_insert(env, dto,
4117 (const struct dt_rec *)rec,
4118 (const struct dt_key *)dot, th);
4123 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
4124 cfs_fail_val != i) {
4125 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
4127 slave_lmm->lmv_master_mdt_index =
4130 slave_lmm->lmv_master_mdt_index =
4133 rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
4134 XATTR_NAME_LMV, 0, th);
4139 /* don't insert stripe if it's existed stripe of splitting
4140 * directory (this directory is striped).
4141 * NB, plain directory will insert itself as the first
4144 if (lod_is_splitting(lo) && lo->ldo_dir_split_offset > 1 &&
4145 lo->ldo_dir_split_offset > i)
4148 rec->rec_fid = lu_object_fid(&dt->do_lu);
4149 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
4150 (const struct dt_key *)dotdot, th);
4154 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
4156 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4157 PFID(lu_object_fid(&dto->do_lu)), i + 1);
4159 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4160 PFID(lu_object_fid(&dto->do_lu)), i);
4162 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
4163 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
4164 sname, lu_object_fid(&dt->do_lu));
4168 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
4169 linkea_buf.lb_len = ldata.ld_leh->leh_len;
4170 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
4171 XATTR_NAME_LINK, 0, th);
4175 rec->rec_fid = lu_object_fid(&dto->do_lu);
4176 rc = lod_sub_insert(env, dt_object_child(dt),
4177 (const struct dt_rec *)rec,
4178 (const struct dt_key *)stripe_name, th);
4182 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
4187 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
4188 rc = lod_sub_xattr_set(env, dt_object_child(dt),
4189 &lmv_buf, XATTR_NAME_LMV, fl, th);
4191 if (slave_lmm != NULL)
4192 OBD_FREE_PTR(slave_lmm);
4198 * Helper function to declare/execute creation of a striped directory
4200 * Called in declare/create object path, prepare striping for a directory
4201 * and prepare defaults data striping for the objects to be created in
4202 * that directory. Notice the function calls "declaration" or "execution"
4203 * methods depending on \a declare param. This is a consequence of the
4204 * current approach while we don't have natural distributed transactions:
4205 * we basically execute non-local updates in the declare phase. So, the
4206 * arguments for the both phases are the same and this is the reason for
4207 * this function to exist.
4209 * \param[in] env execution environment
4210 * \param[in] dt object
4211 * \param[in] attr attributes the stripes will be created with
4212 * \param[in] lmu lmv_user_md if MDT indices are specified
4213 * \param[in] dof format of stripes (see OSD API description)
4214 * \param[in] th transaction handle
4215 * \param[in] declare where to call "declare" or "execute" methods
4217 * \retval 0 on success
4218 * \retval negative if failed
4220 static int lod_dir_striping_create_internal(const struct lu_env *env,
4221 struct dt_object *dt,
4222 struct lu_attr *attr,
4223 const struct lu_buf *lmu,
4224 struct dt_object_format *dof,
4228 struct lod_thread_info *info = lod_env_info(env);
4229 struct lod_object *lo = lod_dt_obj(dt);
4230 const struct lod_default_striping *lds = lo->ldo_def_striping;
4234 LASSERT(ergo(lds != NULL,
4235 lds->lds_def_striping_set ||
4236 lds->lds_dir_def_striping_set));
4238 if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
4239 lo->ldo_dir_stripe_offset)) {
4241 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
4242 int stripe_count = lo->ldo_dir_stripe_count;
4244 if (info->lti_ea_store_size < sizeof(*v1)) {
4245 rc = lod_ea_store_resize(info, sizeof(*v1));
4248 v1 = info->lti_ea_store;
4251 memset(v1, 0, sizeof(*v1));
4252 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
4253 v1->lum_stripe_count = cpu_to_le32(stripe_count);
4254 v1->lum_stripe_offset =
4255 cpu_to_le32(lo->ldo_dir_stripe_offset);
4257 info->lti_buf.lb_buf = v1;
4258 info->lti_buf.lb_len = sizeof(*v1);
4259 lmu = &info->lti_buf;
4263 rc = lod_declare_xattr_set_lmv(env, dt, attr, lmu, dof,
4266 rc = lod_xattr_set_lmv(env, dt, lmu, XATTR_NAME_LMV, 0,
4271 /* foreign LMV EA case */
4273 struct lmv_foreign_md *lfm = lmu->lb_buf;
4275 if (lfm->lfm_magic == LMV_MAGIC_FOREIGN) {
4276 rc = lod_declare_xattr_set_lmv(env, dt, attr,
4280 if (lo->ldo_dir_is_foreign) {
4281 LASSERT(lo->ldo_foreign_lmv != NULL &&
4282 lo->ldo_foreign_lmv_size > 0);
4283 info->lti_buf.lb_buf = lo->ldo_foreign_lmv;
4284 info->lti_buf.lb_len = lo->ldo_foreign_lmv_size;
4285 lmu = &info->lti_buf;
4286 rc = lod_xattr_set_lmv(env, dt, lmu,
4287 XATTR_NAME_LMV, 0, th);
4292 /* Transfer default LMV striping from the parent */
4293 if (lds != NULL && lds->lds_dir_def_striping_set &&
4294 lds->lds_dir_def_max_inherit != LMV_INHERIT_END &&
4295 lds->lds_dir_def_max_inherit != LMV_INHERIT_NONE &&
4296 !(LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
4297 lds->lds_dir_def_stripe_offset) &&
4298 le32_to_cpu(lds->lds_dir_def_hash_type) !=
4299 LMV_HASH_TYPE_UNKNOWN)) {
4300 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
4302 if (info->lti_ea_store_size < sizeof(*v1)) {
4303 rc = lod_ea_store_resize(info, sizeof(*v1));
4306 v1 = info->lti_ea_store;
4309 memset(v1, 0, sizeof(*v1));
4310 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
4311 v1->lum_stripe_count =
4312 cpu_to_le32(lds->lds_dir_def_stripe_count);
4313 v1->lum_stripe_offset =
4314 cpu_to_le32(lds->lds_dir_def_stripe_offset);
4316 cpu_to_le32(lds->lds_dir_def_hash_type);
4317 v1->lum_max_inherit =
4318 lmv_inherit_next(lds->lds_dir_def_max_inherit);
4319 v1->lum_max_inherit_rr =
4320 lmv_inherit_rr_next(lds->lds_dir_def_max_inherit_rr);
4322 info->lti_buf.lb_buf = v1;
4323 info->lti_buf.lb_len = sizeof(*v1);
4325 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4326 XATTR_NAME_DEFAULT_LMV,
4329 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
4331 XATTR_NAME_DEFAULT_LMV, 0,
4337 /* Transfer default LOV striping from the parent */
4338 if (lds != NULL && lds->lds_def_striping_set &&
4339 lds->lds_def_comp_cnt != 0) {
4340 struct lov_mds_md *lmm;
4341 int lmm_size = lod_comp_md_size(lo, true);
4343 if (info->lti_ea_store_size < lmm_size) {
4344 rc = lod_ea_store_resize(info, lmm_size);
4348 lmm = info->lti_ea_store;
4350 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
4354 info->lti_buf.lb_buf = lmm;
4355 info->lti_buf.lb_len = lmm_size;
4358 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4359 XATTR_NAME_LOV, 0, th);
4361 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4362 XATTR_NAME_LOV, 0, th);
4367 /* ldo_def_striping is not allocated, clear after use, in case directory
4368 * layout is changed later.
4371 lo->ldo_def_striping = NULL;
4376 static int lod_declare_dir_striping_create(const struct lu_env *env,
4377 struct dt_object *dt,
4378 struct lu_attr *attr,
4380 struct dt_object_format *dof,
4383 return lod_dir_striping_create_internal(env, dt, attr, lmu, dof, th,
4387 static int lod_dir_striping_create(const struct lu_env *env,
4388 struct dt_object *dt,
4389 struct lu_attr *attr,
4390 struct dt_object_format *dof,
4393 return lod_dir_striping_create_internal(env, dt, attr, NULL, dof, th,
4398 * Make LOV EA for striped object.
4400 * Generate striping information and store it in the LOV EA of the given
4401 * object. The caller must ensure nobody else is calling the function
4402 * against the object concurrently. The transaction must be started.
4403 * FLDB service must be running as well; it's used to map FID to the target,
4404 * which is stored in LOV EA.
4406 * \param[in] env execution environment for this thread
4407 * \param[in] lo LOD object
4408 * \param[in] th transaction handle
4410 * \retval 0 if LOV EA is stored successfully
4411 * \retval negative error number on failure
4413 static int lod_generate_and_set_lovea(const struct lu_env *env,
4414 struct lod_object *lo,
4417 struct lod_thread_info *info = lod_env_info(env);
4418 struct dt_object *next = dt_object_child(&lo->ldo_obj);
4419 struct lov_mds_md_v1 *lmm;
4425 if (lo->ldo_comp_cnt == 0 && !lo->ldo_is_foreign) {
4426 lod_striping_free_nolock(env, lo);
4427 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
4431 lmm_size = lod_comp_md_size(lo, false);
4432 if (info->lti_ea_store_size < lmm_size) {
4433 rc = lod_ea_store_resize(info, lmm_size);
4437 lmm = info->lti_ea_store;
4439 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
4443 info->lti_buf.lb_buf = lmm;
4444 info->lti_buf.lb_len = lmm_size;
4445 rc = lod_sub_xattr_set(env, next, &info->lti_buf,
4446 XATTR_NAME_LOV, 0, th);
4450 static __u32 lod_gen_component_id(struct lod_object *lo,
4451 int mirror_id, int comp_idx);
4454 * Repeat an existing component
4456 * Creates a new layout by replicating an existing component. Uses striping
4457 * policy from previous component as a template for the striping for the new
4460 * New component starts with zero length, will be extended (or removed) before
4461 * returning layout to client.
4463 * NB: Reallocates layout components array (lo->ldo_comp_entries), invalidating
4464 * any pre-existing pointers to components. Handle with care.
4466 * \param[in] env execution environment for this thread
4467 * \param[in,out] lo object to update the layout of
4468 * \param[in] index index of component to copy
4470 * \retval 0 on success
4471 * \retval negative errno on error
4473 static int lod_layout_repeat_comp(const struct lu_env *env,
4474 struct lod_object *lo, int index)
4476 struct lod_layout_component *lod_comp;
4477 struct lod_layout_component *new_comp = NULL;
4478 struct lod_layout_component *comp_array;
4479 int rc = 0, i, new_cnt = lo->ldo_comp_cnt + 1;
4484 lod_comp = &lo->ldo_comp_entries[index];
4485 LASSERT(lod_comp_inited(lod_comp) && lod_comp->llc_id != LCME_ID_INVAL);
4487 CDEBUG(D_LAYOUT, "repeating component %d\n", index);
4489 OBD_ALLOC_PTR_ARRAY(comp_array, new_cnt);
4490 if (comp_array == NULL)
4491 GOTO(out, rc = -ENOMEM);
4493 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4494 memcpy(&comp_array[i + offset], &lo->ldo_comp_entries[i],
4495 sizeof(*comp_array));
4497 /* Duplicate this component in to the next slot */
4499 new_comp = &comp_array[i + 1];
4500 memcpy(&comp_array[i + 1], &lo->ldo_comp_entries[i],
4501 sizeof(*comp_array));
4502 /* We must now skip this new component when copying */
4507 /* Set up copied component */
4508 new_comp->llc_flags &= ~LCME_FL_INIT;
4509 new_comp->llc_stripe = NULL;
4510 new_comp->llc_stripes_allocated = 0;
4511 new_comp->llc_ost_indices = NULL;
4512 new_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4513 /* for uninstantiated components, layout gen stores default stripe
4515 new_comp->llc_layout_gen = lod_comp->llc_stripe_offset;
4516 /* This makes the repeated component zero-length, placed at the end of
4517 * the preceding component */
4518 new_comp->llc_extent.e_start = new_comp->llc_extent.e_end;
4519 new_comp->llc_timestamp = lod_comp->llc_timestamp;
4520 new_comp->llc_pool = NULL;
4522 rc = lod_set_pool(&new_comp->llc_pool, lod_comp->llc_pool);
4526 if (new_comp->llc_ostlist.op_array) {
4527 __u32 *op_array = NULL;
4529 OBD_ALLOC(op_array, new_comp->llc_ostlist.op_size);
4531 GOTO(out, rc = -ENOMEM);
4532 memcpy(op_array, &new_comp->llc_ostlist.op_array,
4533 new_comp->llc_ostlist.op_size);
4534 new_comp->llc_ostlist.op_array = op_array;
4537 OBD_FREE_PTR_ARRAY(lo->ldo_comp_entries, lo->ldo_comp_cnt);
4538 lo->ldo_comp_entries = comp_array;
4539 lo->ldo_comp_cnt = new_cnt;
4541 /* Generate an id for the new component */
4542 mirror_id = mirror_id_of(new_comp->llc_id);
4543 new_comp->llc_id = LCME_ID_INVAL;
4544 new_comp->llc_id = lod_gen_component_id(lo, mirror_id, index + 1);
4545 if (new_comp->llc_id == LCME_ID_INVAL)
4546 GOTO(out, rc = -ERANGE);
4551 OBD_FREE_PTR_ARRAY(comp_array, new_cnt);
4556 static int lod_layout_data_init(struct lod_thread_info *info, __u32 comp_cnt)
4560 /* clear memory region that will be used for layout change */
4561 memset(&info->lti_layout_attr, 0, sizeof(struct lu_attr));
4562 info->lti_count = 0;
4564 if (info->lti_comp_size >= comp_cnt)
4567 if (info->lti_comp_size > 0) {
4568 OBD_FREE_PTR_ARRAY(info->lti_comp_idx, info->lti_comp_size);
4569 info->lti_comp_size = 0;
4572 OBD_ALLOC_PTR_ARRAY(info->lti_comp_idx, comp_cnt);
4573 if (!info->lti_comp_idx)
4576 info->lti_comp_size = comp_cnt;
4581 * Prepare new layout minus deleted components
4583 * Removes components marked for deletion (LCME_ID_INVAL) by copying to a new
4584 * layout and skipping those components. Removes stripe objects if any exist.
4587 * Reallocates layout components array (lo->ldo_comp_entries), invalidating
4588 * any pre-existing pointers to components.
4590 * Caller is responsible for updating mirror end (ldo_mirror[].lme_end).
4592 * \param[in] env execution environment for this thread
4593 * \param[in,out] lo object to update the layout of
4594 * \param[in] th transaction handle for this operation
4596 * \retval # of components deleted
4597 * \retval negative errno on error
4599 static int lod_layout_del_prep_layout(const struct lu_env *env,
4600 struct lod_object *lo,
4603 struct lod_layout_component *lod_comp;
4604 struct lod_thread_info *info = lod_env_info(env);
4605 int rc = 0, i, j, deleted = 0;
4609 LASSERT(lo->ldo_is_composite);
4610 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
4612 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
4616 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4617 lod_comp = &lo->ldo_comp_entries[i];
4619 if (lod_comp->llc_id != LCME_ID_INVAL) {
4620 /* Build array of things to keep */
4621 info->lti_comp_idx[info->lti_count++] = i;
4625 lod_obj_set_pool(lo, i, NULL);
4626 if (lod_comp->llc_ostlist.op_array) {
4627 OBD_FREE(lod_comp->llc_ostlist.op_array,
4628 lod_comp->llc_ostlist.op_size);
4629 lod_comp->llc_ostlist.op_array = NULL;
4630 lod_comp->llc_ostlist.op_size = 0;
4634 CDEBUG(D_LAYOUT, "deleting comp %d, left %d\n", i,
4635 lo->ldo_comp_cnt - deleted);
4637 /* No striping info for this component */
4638 if (lod_comp->llc_stripe == NULL)
4641 LASSERT(lod_comp->llc_stripe_count > 0);
4642 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4643 struct dt_object *obj = lod_comp->llc_stripe[j];
4648 /* components which are not init have no sub objects
4650 if (lod_comp_inited(lod_comp)) {
4651 rc = lod_sub_destroy(env, obj, th);
4656 lu_object_put(env, &obj->do_lu);
4657 lod_comp->llc_stripe[j] = NULL;
4659 OBD_FREE_PTR_ARRAY(lod_comp->llc_stripe,
4660 lod_comp->llc_stripes_allocated);
4661 lod_comp->llc_stripe = NULL;
4662 OBD_FREE_PTR_ARRAY(lod_comp->llc_ost_indices,
4663 lod_comp->llc_stripes_allocated);
4664 lod_comp->llc_ost_indices = NULL;
4665 lod_comp->llc_stripes_allocated = 0;
4668 /* info->lti_count has the amount of left components */
4669 LASSERTF(info->lti_count >= 0 && info->lti_count < lo->ldo_comp_cnt,
4670 "left = %d, lo->ldo_comp_cnt %d\n", (int)info->lti_count,
4671 (int)lo->ldo_comp_cnt);
4673 if (info->lti_count > 0) {
4674 struct lod_layout_component *comp_array;
4676 OBD_ALLOC_PTR_ARRAY(comp_array, info->lti_count);
4677 if (comp_array == NULL)
4678 GOTO(out, rc = -ENOMEM);
4680 for (i = 0; i < info->lti_count; i++) {
4681 memcpy(&comp_array[i],
4682 &lo->ldo_comp_entries[info->lti_comp_idx[i]],
4683 sizeof(*comp_array));
4686 OBD_FREE_PTR_ARRAY(lo->ldo_comp_entries, lo->ldo_comp_cnt);
4687 lo->ldo_comp_entries = comp_array;
4688 lo->ldo_comp_cnt = info->lti_count;
4690 lod_free_comp_entries(lo);
4695 return rc ? rc : deleted;
4699 * Delete layout component(s)
4701 * This function sets up the layout data in the env and does the setattrs
4702 * required to write out the new layout. The layout itself is modified in
4703 * lod_layout_del_prep_layout.
4705 * \param[in] env execution environment for this thread
4706 * \param[in] dt object
4707 * \param[in] th transaction handle
4709 * \retval 0 on success
4710 * \retval negative error number on failure
4712 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
4715 struct lod_object *lo = lod_dt_obj(dt);
4716 struct dt_object *next = dt_object_child(dt);
4717 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4720 LASSERT(lo->ldo_mirror_count == 1);
4722 mutex_lock(&lo->ldo_layout_mutex);
4724 rc = lod_layout_del_prep_layout(env, lo, th);
4728 /* Only do this if we didn't delete all components */
4729 if (lo->ldo_comp_cnt > 0) {
4730 lo->ldo_mirrors[0].lme_end = lo->ldo_comp_cnt - 1;
4731 lod_obj_inc_layout_gen(lo);
4734 LASSERT(dt_object_exists(dt));
4735 rc = dt_attr_get(env, next, attr);
4739 if (attr->la_size > 0) {
4741 attr->la_valid = LA_SIZE;
4742 rc = lod_sub_attr_set(env, next, attr, th);
4747 rc = lod_generate_and_set_lovea(env, lo, th);
4751 lod_striping_free_nolock(env, lo);
4753 mutex_unlock(&lo->ldo_layout_mutex);
4759 static int lod_get_default_lov_striping(const struct lu_env *env,
4760 struct lod_object *lo,
4761 struct lod_default_striping *lds,
4762 struct dt_allocation_hint *ah);
4764 * Implementation of dt_object_operations::do_xattr_set.
4766 * Sets specified extended attribute on the object. Three types of EAs are
4768 * LOV EA - stores striping for a regular file or default striping (when set
4770 * LMV EA - stores a marker for the striped directories
4771 * DMV EA - stores default directory striping
4773 * When striping is applied to a non-striped existing object (this is called
4774 * late striping), then LOD notices the caller wants to turn the object into a
4775 * striped one. The stripe objects are created and appropriate EA is set:
4776 * LOV EA storing all the stripes directly or LMV EA storing just a small header
4777 * with striping configuration.
4779 * \see dt_object_operations::do_xattr_set() in the API description for details.
4781 static int lod_xattr_set(const struct lu_env *env,
4782 struct dt_object *dt, const struct lu_buf *buf,
4783 const char *name, int fl, struct thandle *th)
4785 struct dt_object *next = dt_object_child(dt);
4786 struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
4787 struct lod_object *lo = lod_dt_obj(dt);
4788 struct lod_obj_stripe_cb_data data = { {0} };
4793 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4794 !strcmp(name, XATTR_NAME_LMV)) {
4796 case LU_XATTR_CREATE:
4797 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
4800 case LU_XATTR_REPLACE:
4801 rc = lod_dir_layout_set(env, dt, buf, fl, th);
4808 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4809 strcmp(name, XATTR_NAME_LOV) == 0) {
4810 struct lod_default_striping *lds = lod_lds_buf_get(env);
4811 struct lov_user_md_v1 *v1 = buf->lb_buf;
4812 char pool[LOV_MAXPOOLNAME + 1];
4815 /* get existing striping config */
4816 rc = lod_get_default_lov_striping(env, lod_dt_obj(dt), lds,
4821 memset(pool, 0, sizeof(pool));
4822 if (lds->lds_def_striping_set == 1)
4823 lod_layout_get_pool(lds->lds_def_comp_entries,
4824 lds->lds_def_comp_cnt, pool,
4827 is_del = LOVEA_DELETE_VALUES(v1->lmm_stripe_size,
4828 v1->lmm_stripe_count,
4829 v1->lmm_stripe_offset,
4832 /* Retain the pool name if it is not given */
4833 if (v1->lmm_magic == LOV_USER_MAGIC_V1 && pool[0] != '\0' &&
4835 struct lod_thread_info *info = lod_env_info(env);
4836 struct lov_user_md_v3 *v3 = info->lti_ea_store;
4838 memset(v3, 0, sizeof(*v3));
4839 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
4840 v3->lmm_pattern = cpu_to_le32(v1->lmm_pattern);
4841 v3->lmm_stripe_count =
4842 cpu_to_le32(v1->lmm_stripe_count);
4843 v3->lmm_stripe_offset =
4844 cpu_to_le32(v1->lmm_stripe_offset);
4845 v3->lmm_stripe_size = cpu_to_le32(v1->lmm_stripe_size);
4847 strlcpy(v3->lmm_pool_name, pool,
4848 sizeof(v3->lmm_pool_name));
4850 info->lti_buf.lb_buf = v3;
4851 info->lti_buf.lb_len = sizeof(*v3);
4852 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4855 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name,
4859 if (lds->lds_def_striping_set == 1 &&
4860 lds->lds_def_comp_entries != NULL)
4861 lod_free_def_comp_entries(lds);
4864 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4865 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
4867 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
4870 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4871 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4872 strcmp(name, XATTR_LUSTRE_LOV) == 0 ||
4873 allowed_lustre_lov(name))) {
4874 /* in case of lov EA swap, just set it
4875 * if not, it is a replay so check striping match what we
4876 * already have during req replay, declare_xattr_set()
4877 * defines striping, then create() does the work */
4878 if (fl & LU_XATTR_REPLACE) {
4879 /* free stripes, then update disk */
4880 lod_striping_free(env, lod_dt_obj(dt));
4882 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
4883 } else if (fl & LU_XATTR_SPLIT) {
4884 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
4888 rc = lod_striping_reload(env, lo, buf, LVF_ALL_STALE);
4892 if (lo->ldo_mirror_count > 1 &&
4893 layout_attr->la_valid & LA_LAYOUT_VERSION) {
4895 layout_attr->la_layout_version =
4897 data.locd_attr = layout_attr;
4898 data.locd_declare = false;
4899 data.locd_stripe_cb =
4900 lod_obj_stripe_attr_set_cb;
4901 rc = lod_obj_for_each_stripe(env, lo, th,
4906 } else if (fl & LU_XATTR_PURGE) {
4907 rc = lod_layout_purge(env, dt, buf, th);
4908 } else if (dt_object_remote(dt)) {
4909 /* This only happens during migration, see
4910 * mdd_migrate_create(), in which Master MDT will
4911 * create a remote target object, and only set
4912 * (migrating) stripe EA on the remote object,
4913 * and does not need creating each stripes. */
4914 rc = lod_sub_xattr_set(env, next, buf, name,
4916 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
4917 /* delete component(s) */
4918 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
4919 rc = lod_layout_del(env, dt, th);
4922 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
4923 * it's going to create create file with specified
4924 * component(s), the striping must have not being
4925 * cached in this case;
4927 * Otherwise, it's going to add/change component(s) to
4928 * an existing file, the striping must have been cached
4931 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
4932 !strcmp(name, XATTR_NAME_LOV),
4933 !lod_dt_obj(dt)->ldo_comp_cached));
4935 rc = lod_striped_create(env, dt, NULL, NULL, th);
4939 if (fl & LU_XATTR_MERGE && lo->ldo_mirror_count > 1 &&
4940 layout_attr->la_valid & LA_LAYOUT_VERSION) {
4941 /* mirror merge exec phase */
4942 layout_attr->la_layout_version =
4944 data.locd_attr = layout_attr;
4945 data.locd_declare = false;
4946 data.locd_stripe_cb =
4947 lod_obj_stripe_attr_set_cb;
4948 rc = lod_obj_for_each_stripe(env, lo, th,
4955 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
4956 rc = lod_replace_parent_fid(env, dt, buf, th, false);
4961 /* then all other xattr */
4962 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
4968 * Implementation of dt_object_operations::do_declare_xattr_del.
4970 * \see dt_object_operations::do_declare_xattr_del() in the API description
4973 static int lod_declare_xattr_del(const struct lu_env *env,
4974 struct dt_object *dt, const char *name,
4977 struct lod_object *lo = lod_dt_obj(dt);
4978 struct dt_object *next = dt_object_child(dt);
4983 rc = lod_sub_declare_xattr_del(env, next, name, th);
4987 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4990 /* NB: don't delete stripe LMV, because when we do this, normally we
4991 * will remove stripes, besides, if directory LMV is corrupt, this will
4992 * prevent deleting its LMV and fixing it (via LFSCK).
4994 if (!strcmp(name, XATTR_NAME_LMV))
4997 rc = lod_striping_load(env, lo);
5001 if (lo->ldo_dir_stripe_count == 0)
5004 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5005 struct dt_object *dto = lo->ldo_stripe[i];
5010 if (!dt_object_exists(dto))
5013 rc = lod_sub_declare_xattr_del(env, dto, name, th);
5022 * Implementation of dt_object_operations::do_xattr_del.
5024 * If EA storing a regular striping is being deleted, then release
5025 * all the references to the stripe objects in core.
5027 * \see dt_object_operations::do_xattr_del() in the API description for details.
5029 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
5030 const char *name, struct thandle *th)
5036 if (!strcmp(name, XATTR_NAME_LOV) || !strcmp(name, XATTR_NAME_LMV))
5037 lod_striping_free(env, lod_dt_obj(dt));
5039 rc = lod_xattr_del_internal(env, dt, name, th);
5045 * Implementation of dt_object_operations::do_xattr_list.
5047 * \see dt_object_operations::do_xattr_list() in the API description
5050 static int lod_xattr_list(const struct lu_env *env,
5051 struct dt_object *dt, const struct lu_buf *buf)
5053 return dt_xattr_list(env, dt_object_child(dt), buf);
5056 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
5058 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
5062 * Copy OST list from layout provided by user.
5064 * \param[in] lod_comp layout_component to be filled
5065 * \param[in] v3 LOV EA V3 user data
5067 * \retval 0 on success
5068 * \retval negative if failed
5070 int lod_comp_copy_ost_lists(struct lod_layout_component *lod_comp,
5071 struct lov_user_md_v3 *v3)
5077 if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
5078 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
5080 if (lod_comp->llc_ostlist.op_array) {
5081 if (lod_comp->llc_ostlist.op_size >=
5082 v3->lmm_stripe_count * sizeof(__u32)) {
5083 lod_comp->llc_ostlist.op_count =
5084 v3->lmm_stripe_count;
5087 OBD_FREE(lod_comp->llc_ostlist.op_array,
5088 lod_comp->llc_ostlist.op_size);
5091 /* copy ost list from lmm */
5092 lod_comp->llc_ostlist.op_count = v3->lmm_stripe_count;
5093 lod_comp->llc_ostlist.op_size = v3->lmm_stripe_count * sizeof(__u32);
5094 OBD_ALLOC(lod_comp->llc_ostlist.op_array,
5095 lod_comp->llc_ostlist.op_size);
5096 if (!lod_comp->llc_ostlist.op_array)
5099 for (j = 0; j < v3->lmm_stripe_count; j++) {
5100 lod_comp->llc_ostlist.op_array[j] =
5101 v3->lmm_objects[j].l_ost_idx;
5109 * Get default striping.
5111 * \param[in] env execution environment
5112 * \param[in] lo object
5113 * \param[out] lds default striping
5115 * \retval 0 on success
5116 * \retval negative if failed
5118 static int lod_get_default_lov_striping(const struct lu_env *env,
5119 struct lod_object *lo,
5120 struct lod_default_striping *lds,
5121 struct dt_allocation_hint *ah)
5123 struct lod_thread_info *info = lod_env_info(env);
5124 struct lov_user_md_v1 *v1 = NULL;
5125 struct lov_user_md_v3 *v3 = NULL;
5126 struct lov_comp_md_v1 *comp_v1 = NULL;
5134 lds->lds_def_striping_set = 0;
5136 rc = lod_get_lov_ea(env, lo);
5140 if (rc < (typeof(rc))sizeof(struct lov_user_md))
5143 v1 = info->lti_ea_store;
5144 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
5145 lustre_swab_lov_user_md_v1(v1);
5146 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
5147 v3 = (struct lov_user_md_v3 *)v1;
5148 lustre_swab_lov_user_md_v3(v3);
5149 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_SPECIFIC)) {
5150 v3 = (struct lov_user_md_v3 *)v1;
5151 lustre_swab_lov_user_md_v3(v3);
5152 lustre_swab_lov_user_md_objects(v3->lmm_objects,
5153 v3->lmm_stripe_count);
5154 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1) ||
5155 v1->lmm_magic == __swab32(LOV_USER_MAGIC_SEL)) {
5156 comp_v1 = (struct lov_comp_md_v1 *)v1;
5157 lustre_swab_lov_comp_md_v1(comp_v1);
5160 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
5161 v1->lmm_magic != LOV_MAGIC_COMP_V1 &&
5162 v1->lmm_magic != LOV_MAGIC_SEL &&
5163 v1->lmm_magic != LOV_USER_MAGIC_SPECIFIC)
5166 if ((v1->lmm_magic == LOV_MAGIC_COMP_V1 ||
5167 v1->lmm_magic == LOV_MAGIC_SEL) &&
5168 !(ah && ah->dah_append_stripes)) {
5169 comp_v1 = (struct lov_comp_md_v1 *)v1;
5170 comp_cnt = comp_v1->lcm_entry_count;
5173 mirror_cnt = comp_v1->lcm_mirror_count + 1;
5181 /* realloc default comp entries if necessary */
5182 rc = lod_def_striping_comp_resize(lds, comp_cnt);
5186 lds->lds_def_comp_cnt = comp_cnt;
5187 lds->lds_def_striping_is_composite = composite;
5188 lds->lds_def_mirror_cnt = mirror_cnt;
5190 for (i = 0; i < comp_cnt; i++) {
5191 struct lod_layout_component *lod_comp;
5194 lod_comp = &lds->lds_def_comp_entries[i];
5196 * reset lod_comp values, llc_stripes is always NULL in
5197 * the default striping template, llc_pool will be reset
5200 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
5203 v1 = (struct lov_user_md *)((char *)comp_v1 +
5204 comp_v1->lcm_entries[i].lcme_offset);
5205 lod_comp->llc_extent =
5206 comp_v1->lcm_entries[i].lcme_extent;
5207 /* We only inherit certain flags from the layout */
5208 lod_comp->llc_flags =
5209 comp_v1->lcm_entries[i].lcme_flags &
5210 LCME_TEMPLATE_FLAGS;
5213 if (!lov_pattern_supported(v1->lmm_pattern) &&
5214 !(v1->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
5215 lod_free_def_comp_entries(lds);
5219 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d stripe_offset=%d append_stripes=%d\n",
5220 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
5221 (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
5222 (int)v1->lmm_stripe_offset,
5223 ah ? ah->dah_append_stripes : 0);
5225 if (ah && ah->dah_append_stripes)
5226 lod_comp->llc_stripe_count = ah->dah_append_stripes;
5228 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
5229 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
5230 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
5231 lod_comp->llc_pattern = v1->lmm_pattern;
5234 if (ah && ah->dah_append_pool && ah->dah_append_pool[0]) {
5235 pool = ah->dah_append_pool;
5236 } else if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
5237 /* XXX: sanity check here */
5238 v3 = (struct lov_user_md_v3 *) v1;
5239 if (v3->lmm_pool_name[0] != '\0')
5240 pool = v3->lmm_pool_name;
5242 lod_set_def_pool(lds, i, pool);
5243 if (v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
5244 v3 = (struct lov_user_md_v3 *)v1;
5245 rc = lod_comp_copy_ost_lists(lod_comp, v3);
5248 } else if (lod_comp->llc_ostlist.op_array &&
5249 lod_comp->llc_ostlist.op_count) {
5250 for (j = 0; j < lod_comp->llc_ostlist.op_count; j++)
5251 lod_comp->llc_ostlist.op_array[j] = -1;
5252 lod_comp->llc_ostlist.op_count = 0;
5256 lds->lds_def_striping_set = 1;
5261 * Get default directory striping.
5263 * \param[in] env execution environment
5264 * \param[in] lo object
5265 * \param[out] lds default striping
5267 * \retval 0 on success
5268 * \retval negative if failed
5270 static int lod_get_default_lmv_striping(const struct lu_env *env,
5271 struct lod_object *lo,
5272 struct lod_default_striping *lds)
5274 struct lmv_user_md *lmu;
5277 lds->lds_dir_def_striping_set = 0;
5279 rc = lod_get_default_lmv_ea(env, lo);
5283 if (rc >= (int)sizeof(*lmu)) {
5284 struct lod_thread_info *info = lod_env_info(env);
5286 lmu = info->lti_ea_store;
5288 lds->lds_dir_def_stripe_count =
5289 le32_to_cpu(lmu->lum_stripe_count);
5290 lds->lds_dir_def_stripe_offset =
5291 le32_to_cpu(lmu->lum_stripe_offset);
5292 lds->lds_dir_def_hash_type =
5293 le32_to_cpu(lmu->lum_hash_type);
5294 lds->lds_dir_def_max_inherit = lmu->lum_max_inherit;
5295 lds->lds_dir_def_max_inherit_rr = lmu->lum_max_inherit_rr;
5296 lds->lds_dir_def_striping_set = 1;
5303 * Get default striping in the object.
5305 * Get object default striping and default directory striping.
5307 * \param[in] env execution environment
5308 * \param[in] lo object
5309 * \param[out] lds default striping
5311 * \retval 0 on success
5312 * \retval negative if failed
5314 static int lod_get_default_striping(const struct lu_env *env,
5315 struct lod_object *lo,
5316 struct lod_default_striping *lds)
5320 rc = lod_get_default_lov_striping(env, lo, lds, NULL);
5321 if (lds->lds_def_striping_set) {
5322 struct lod_thread_info *info = lod_env_info(env);
5323 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5325 rc = lod_verify_striping(env, d, lo, &info->lti_buf, false);
5327 lds->lds_def_striping_set = 0;
5330 rc1 = lod_get_default_lmv_striping(env, lo, lds);
5331 if (rc == 0 && rc1 < 0)
5338 * Apply default striping on object.
5340 * If object striping pattern is not set, set to the one in default striping.
5341 * The default striping is from parent or fs.
5343 * \param[in] lo new object
5344 * \param[in] lds default striping
5345 * \param[in] mode new object's mode
5347 static void lod_striping_from_default(struct lod_object *lo,
5348 const struct lod_default_striping *lds,
5351 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5354 if (lds->lds_def_striping_set && S_ISREG(mode)) {
5355 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
5357 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
5358 lds->lds_def_comp_cnt);
5362 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
5363 if (lds->lds_def_mirror_cnt > 1)
5364 lo->ldo_flr_state = LCM_FL_RDONLY;
5366 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5367 struct lod_layout_component *obj_comp =
5368 &lo->ldo_comp_entries[i];
5369 struct lod_layout_component *def_comp =
5370 &lds->lds_def_comp_entries[i];
5372 CDEBUG(D_LAYOUT, "Inherit from default: flags=%#x "
5373 "size=%hu nr=%u offset=%u pattern=%#x pool=%s\n",
5374 def_comp->llc_flags,
5375 def_comp->llc_stripe_size,
5376 def_comp->llc_stripe_count,
5377 def_comp->llc_stripe_offset,
5378 def_comp->llc_pattern,
5379 def_comp->llc_pool ?: "");
5381 *obj_comp = *def_comp;
5382 if (def_comp->llc_pool != NULL) {
5383 /* pointer was copied from def_comp */
5384 obj_comp->llc_pool = NULL;
5385 lod_obj_set_pool(lo, i, def_comp->llc_pool);
5389 if (def_comp->llc_ostlist.op_array &&
5390 def_comp->llc_ostlist.op_count) {
5391 OBD_ALLOC(obj_comp->llc_ostlist.op_array,
5392 obj_comp->llc_ostlist.op_size);
5393 if (!obj_comp->llc_ostlist.op_array)
5395 memcpy(obj_comp->llc_ostlist.op_array,
5396 def_comp->llc_ostlist.op_array,
5397 obj_comp->llc_ostlist.op_size);
5398 } else if (def_comp->llc_ostlist.op_array) {
5399 obj_comp->llc_ostlist.op_array = NULL;
5403 * Don't initialize these fields for plain layout
5404 * (v1/v3) here, they are inherited in the order of
5405 * 'parent' -> 'fs default (root)' -> 'global default
5406 * values for stripe_count & stripe_size'.
5408 * see lod_ah_init().
5410 if (!lo->ldo_is_composite)
5413 lod_adjust_stripe_info(obj_comp, desc, 0);
5415 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
5416 if (lo->ldo_dir_stripe_count == 0)
5417 lo->ldo_dir_stripe_count =
5418 lds->lds_dir_def_stripe_count;
5419 if (lo->ldo_dir_stripe_offset == -1)
5420 lo->ldo_dir_stripe_offset =
5421 lds->lds_dir_def_stripe_offset;
5422 if (lo->ldo_dir_hash_type == 0)
5423 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
5425 CDEBUG(D_LAYOUT, "striping from default dir: count:%hu, "
5426 "offset:%u, hash_type:%u\n",
5427 lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
5428 lo->ldo_dir_hash_type);
5432 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root,
5435 struct lod_layout_component *lod_comp;
5437 if (lo->ldo_comp_cnt == 0)
5440 if (lo->ldo_is_composite)
5443 lod_comp = &lo->ldo_comp_entries[0];
5445 if (lod_comp->llc_stripe_count <= 0 ||
5446 lod_comp->llc_stripe_size <= 0)
5449 if (from_root && (lod_comp->llc_pool == NULL ||
5450 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
5453 if (append_pool && append_pool[0])
5460 * Implementation of dt_object_operations::do_ah_init.
5462 * This method is used to make a decision on the striping configuration for the
5463 * object being created. It can be taken from the \a parent object if it exists,
5464 * or filesystem's default. The resulting configuration (number of stripes,
5465 * stripe size/offset, pool name, etc) is stored in the object itself and will
5466 * be used by the methods like ->doo_declare_create().
5468 * \see dt_object_operations::do_ah_init() in the API description for details.
5470 static void lod_ah_init(const struct lu_env *env,
5471 struct dt_allocation_hint *ah,
5472 struct dt_object *parent,
5473 struct dt_object *child,
5476 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
5477 struct lod_thread_info *info = lod_env_info(env);
5478 struct lod_default_striping *lds = lod_lds_buf_get(env);
5479 struct dt_object *nextp = NULL;
5480 struct dt_object *nextc;
5481 struct lod_object *lp = NULL;
5482 struct lod_object *lc;
5483 struct lov_desc *desc;
5484 struct lod_layout_component *lod_comp;
5490 if (ah->dah_append_stripes == -1)
5491 ah->dah_append_stripes =
5492 d->lod_ost_descs.ltd_lov_desc.ld_tgt_count;
5494 if (likely(parent)) {
5495 nextp = dt_object_child(parent);
5496 lp = lod_dt_obj(parent);
5499 nextc = dt_object_child(child);
5500 lc = lod_dt_obj(child);
5502 LASSERT(!lod_obj_is_striped(child));
5503 /* default layout template may have been set on the regular file
5504 * when this is called from mdd_create_data() */
5505 if (S_ISREG(child_mode))
5506 lod_free_comp_entries(lc);
5508 if (!dt_object_exists(nextc))
5509 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
5511 if (S_ISDIR(child_mode)) {
5512 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
5514 /* other default values are 0 */
5515 lc->ldo_dir_stripe_offset = -1;
5517 /* no default striping configuration is needed for
5520 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5521 le32_to_cpu(lum1->lum_magic) == LMV_MAGIC_FOREIGN) {
5522 lc->ldo_dir_is_foreign = true;
5523 /* keep stripe_count 0 and stripe_offset -1 */
5524 CDEBUG(D_INFO, "no default striping for foreign dir\n");
5528 if (likely(lp != NULL))
5529 lod_get_default_striping(env, lp, lds);
5531 /* It should always honour the specified stripes */
5532 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
5533 * will have old magic. In this case, we should ignore the
5534 * stripe count and try to create dir by default stripe.
5536 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5537 (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
5538 le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
5539 lc->ldo_dir_stripe_count =
5540 le32_to_cpu(lum1->lum_stripe_count);
5541 lc->ldo_dir_stripe_offset =
5542 le32_to_cpu(lum1->lum_stripe_offset);
5543 lc->ldo_dir_hash_type =
5544 le32_to_cpu(lum1->lum_hash_type);
5546 "set dirstripe: count %hu, offset %d, hash %u\n",
5547 lc->ldo_dir_stripe_count,
5548 (int)lc->ldo_dir_stripe_offset,
5549 lc->ldo_dir_hash_type);
5551 if (d->lod_mdt_descs.ltd_lmv_desc.ld_active_tgt_count &&
5552 lc->ldo_dir_stripe_count < 2 &&
5553 lum1->lum_max_inherit != LMV_INHERIT_NONE) {
5554 /* when filesystem-wide default LMV is set, dirs
5555 * will be created on MDT by space usage, but if
5556 * dir is created with "lfs mkdir -c 1 ...", its
5557 * subdirs should be kept on the same MDT. To
5558 * guarantee this, set default LMV for such dir.
5560 lds->lds_dir_def_stripe_count =
5561 le32_to_cpu(lum1->lum_stripe_count);
5562 /* if "-1" stripe offset is set, save current
5563 * MDT index in default LMV.
5565 if (le32_to_cpu(lum1->lum_stripe_offset) ==
5567 lds->lds_dir_def_stripe_offset =
5568 lod2lu_dev(d)->ld_site->ld_seq_site->ss_node_id;
5570 lds->lds_dir_def_stripe_offset =
5571 le32_to_cpu(lum1->lum_stripe_offset);
5572 lds->lds_dir_def_hash_type =
5573 le32_to_cpu(lum1->lum_hash_type);
5574 lds->lds_dir_def_max_inherit =
5575 lum1->lum_max_inherit;
5576 /* it will be decreased by 1 later in setting */
5577 if (lum1->lum_max_inherit >= LMV_INHERIT_END &&
5578 lum1->lum_max_inherit < LMV_INHERIT_MAX)
5579 lds->lds_dir_def_max_inherit++;
5580 lds->lds_dir_def_max_inherit_rr =
5581 lum1->lum_max_inherit_rr;
5582 lds->lds_dir_def_striping_set = 1;
5583 /* don't inherit LOV from ROOT */
5584 if (lds->lds_def_striping_set &&
5585 fid_is_root(lod_object_fid(lp)))
5586 lds->lds_def_striping_set = 0;
5587 lc->ldo_def_striping = lds;
5588 } else if (lds->lds_def_striping_set &&
5589 !fid_is_root(lod_object_fid(lp))) {
5590 /* don't inherit default LMV for "lfs mkdir" */
5591 lds->lds_dir_def_striping_set = 0;
5592 lc->ldo_def_striping = lds;
5595 /* inherit default striping except ROOT */
5596 if ((lds->lds_def_striping_set ||
5597 lds->lds_dir_def_striping_set) &&
5598 !fid_is_root(lod_object_fid(lp)))
5599 lc->ldo_def_striping = lds;
5601 /* transfer defaults LMV to new directory */
5602 lod_striping_from_default(lc, lds, child_mode);
5604 /* set count 0 to create normal directory */
5605 if (lc->ldo_dir_stripe_count == 1)
5606 lc->ldo_dir_stripe_count = 0;
5609 /* shrink the stripe count to max_mdt_stripecount if it is -1
5610 * and max_mdt_stripecount is not 0
5612 if (lc->ldo_dir_stripe_count == (__u16)(-1) &&
5613 d->lod_max_mdt_stripecount)
5614 lc->ldo_dir_stripe_count = d->lod_max_mdt_stripecount;
5616 /* shrink the stripe_count to the avaible MDT count */
5617 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
5618 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)) {
5619 lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
5620 if (lc->ldo_dir_stripe_count == 1)
5621 lc->ldo_dir_stripe_count = 0;
5624 if (!(lc->ldo_dir_hash_type & LMV_HASH_TYPE_MASK))
5625 lc->ldo_dir_hash_type |=
5626 d->lod_mdt_descs.ltd_lmv_desc.ld_pattern;
5628 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
5629 lc->ldo_dir_stripe_count,
5630 (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
5635 /* child object regular file*/
5637 if (!lod_object_will_be_striped(S_ISREG(child_mode),
5638 lu_object_fid(&child->do_lu)))
5641 /* If object is going to be striped over OSTs, transfer default
5642 * striping information to the child, so that we can use it
5643 * during declaration and creation.
5645 * Try from the parent first.
5647 if (likely(lp != NULL)) {
5648 rc = lod_get_default_lov_striping(env, lp, lds, ah);
5649 if (rc == 0 && lds->lds_def_striping_set) {
5650 rc = lod_verify_striping(env, d, lp, &info->lti_buf,
5653 lod_striping_from_default(lc, lds, child_mode);
5657 /* Initialize lod_device::lod_md_root object reference */
5658 if (d->lod_md_root == NULL) {
5659 struct dt_object *root;
5660 struct lod_object *lroot;
5662 lu_root_fid(&info->lti_fid);
5663 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
5664 if (!IS_ERR(root)) {
5665 lroot = lod_dt_obj(root);
5667 spin_lock(&d->lod_lock);
5668 if (d->lod_md_root != NULL)
5669 dt_object_put(env, &d->lod_md_root->ldo_obj);
5670 d->lod_md_root = lroot;
5671 spin_unlock(&d->lod_lock);
5675 /* try inherit layout from the root object (fs default) when:
5676 * - parent does not have default layout; or
5677 * - parent has plain(v1/v3) default layout, and some attributes
5678 * are not specified in the default layout;
5680 if (d->lod_md_root != NULL &&
5681 lod_need_inherit_more(lc, true, ah->dah_append_pool)) {
5682 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds,
5684 if (rc || !lds->lds_def_striping_set)
5687 rc = lod_verify_striping(env, d, d->lod_md_root, &info->lti_buf,
5692 if (lc->ldo_comp_cnt == 0) {
5693 lod_striping_from_default(lc, lds, child_mode);
5694 } else if (!lds->lds_def_striping_is_composite) {
5695 struct lod_layout_component *def_comp;
5697 LASSERT(!lc->ldo_is_composite);
5698 lod_comp = &lc->ldo_comp_entries[0];
5699 def_comp = &lds->lds_def_comp_entries[0];
5701 if (lod_comp->llc_stripe_count <= 0)
5702 lod_comp->llc_stripe_count =
5703 def_comp->llc_stripe_count;
5704 if (lod_comp->llc_stripe_size <= 0)
5705 lod_comp->llc_stripe_size =
5706 def_comp->llc_stripe_size;
5707 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT &&
5708 (!lod_comp->llc_pool || !lod_comp->llc_pool[0]))
5709 lod_comp->llc_stripe_offset =
5710 def_comp->llc_stripe_offset;
5711 if (lod_comp->llc_pool == NULL)
5712 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
5717 * fs default striping may not be explicitly set, or historically set
5718 * in config log, use them.
5720 if (lod_need_inherit_more(lc, false, ah->dah_append_pool)) {
5721 if (lc->ldo_comp_cnt == 0) {
5722 rc = lod_alloc_comp_entries(lc, 0, 1);
5724 /* fail to allocate memory, will create a
5725 * non-striped file. */
5727 lc->ldo_is_composite = 0;
5728 lod_comp = &lc->ldo_comp_entries[0];
5729 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
5731 LASSERT(!lc->ldo_is_composite);
5732 lod_comp = &lc->ldo_comp_entries[0];
5733 desc = &d->lod_ost_descs.ltd_lov_desc;
5734 lod_adjust_stripe_info(lod_comp, desc, ah->dah_append_stripes);
5735 if (ah->dah_append_pool && ah->dah_append_pool[0])
5736 lod_obj_set_pool(lc, 0, ah->dah_append_pool);
5743 * Size initialization on late striping.
5745 * Propagate the size of a truncated object to a deferred striping.
5746 * This function handles a special case when truncate was done on a
5747 * non-striped object and now while the striping is being created
5748 * we can't lose that size, so we have to propagate it to the stripes
5751 * \param[in] env execution environment
5752 * \param[in] dt object
5753 * \param[in] th transaction handle
5755 * \retval 0 on success
5756 * \retval negative if failed
5758 static int lod_declare_init_size(const struct lu_env *env,
5759 struct dt_object *dt, struct thandle *th)
5761 struct dt_object *next = dt_object_child(dt);
5762 struct lod_object *lo = lod_dt_obj(dt);
5763 struct dt_object **objects = NULL;
5764 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
5765 uint64_t size, offs;
5766 int i, rc, stripe, stripe_count = 0, stripe_size = 0;
5767 struct lu_extent size_ext;
5770 if (!lod_obj_is_striped(dt))
5773 rc = dt_attr_get(env, next, attr);
5774 LASSERT(attr->la_valid & LA_SIZE);
5778 size = attr->la_size;
5782 size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
5783 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5784 struct lod_layout_component *lod_comp;
5785 struct lu_extent *extent;
5787 lod_comp = &lo->ldo_comp_entries[i];
5789 if (lod_comp->llc_stripe == NULL)
5792 extent = &lod_comp->llc_extent;
5793 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
5794 if (!lo->ldo_is_composite ||
5795 lu_extent_is_overlapped(extent, &size_ext)) {
5796 objects = lod_comp->llc_stripe;
5797 stripe_count = lod_comp->llc_stripe_count;
5798 stripe_size = lod_comp->llc_stripe_size;
5801 if (stripe_count == 0)
5804 LASSERT(objects != NULL && stripe_size != 0);
5805 do_div(size, stripe_size);
5806 stripe = do_div(size, stripe_count);
5807 LASSERT(objects[stripe] != NULL);
5809 size = size * stripe_size;
5810 offs = attr->la_size;
5811 size += do_div(offs, stripe_size);
5813 attr->la_valid = LA_SIZE;
5814 attr->la_size = size;
5816 rc = lod_sub_declare_attr_set(env, objects[stripe],
5825 * Declare creation of striped object.
5827 * The function declares creation stripes for a regular object. The function
5828 * also declares whether the stripes will be created with non-zero size if
5829 * previously size was set non-zero on the master object. If object \a dt is
5830 * not local, then only fully defined striping can be applied in \a lovea.
5831 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
5834 * \param[in] env execution environment
5835 * \param[in] dt object
5836 * \param[in] attr attributes the stripes will be created with
5837 * \param[in] lovea a buffer containing striping description
5838 * \param[in] th transaction handle
5840 * \retval 0 on success
5841 * \retval negative if failed
5843 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
5844 struct lu_attr *attr,
5845 const struct lu_buf *lovea, struct thandle *th)
5847 struct lod_thread_info *info = lod_env_info(env);
5848 struct dt_object *next = dt_object_child(dt);
5849 struct lod_object *lo = lod_dt_obj(dt);
5853 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
5854 GOTO(out, rc = -ENOMEM);
5856 if (!dt_object_remote(next)) {
5857 /* choose OST and generate appropriate objects */
5858 rc = lod_prepare_create(env, lo, attr, lovea, th);
5863 * declare storage for striping data
5865 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5867 /* LOD can not choose OST objects for remote objects, i.e.
5868 * stripes must be ready before that. Right now, it can only
5869 * happen during migrate, i.e. migrate process needs to create
5870 * remote regular file (mdd_migrate_create), then the migrate
5871 * process will provide stripeEA. */
5872 LASSERT(lovea != NULL);
5873 info->lti_buf = *lovea;
5876 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
5877 XATTR_NAME_LOV, 0, th);
5882 * if striping is created with local object's size > 0,
5883 * we have to propagate this size to specific object
5884 * the case is possible only when local object was created previously
5886 if (dt_object_exists(next))
5887 rc = lod_declare_init_size(env, dt, th);
5890 /* failed to create striping or to set initial size, let's reset
5891 * config so that others don't get confused */
5893 lod_striping_free(env, lo);
5899 * Whether subdirectories under \a dt should be created on MDTs by space QoS
5901 * If LMV_HASH_FLAG_SPACE is set on directory default layout, its subdirectories
5902 * should be created on MDT by space QoS.
5904 * \param[in] env execution environment
5905 * \param[in] dev lu device
5906 * \param[in] dt object
5908 * \retval 1 if directory should create subdir by space usage
5910 * \retval -ev if failed
5912 static inline int dt_object_qos_mkdir(const struct lu_env *env,
5913 struct lu_device *dev,
5914 struct dt_object *dt)
5916 struct lod_thread_info *info = lod_env_info(env);
5917 struct lu_object *obj;
5918 struct lod_object *lo;
5919 struct lmv_user_md *lmu;
5922 obj = lu_object_find_slice(env, dev, lu_object_fid(&dt->do_lu), NULL);
5924 return PTR_ERR(obj);
5926 lo = lu2lod_obj(obj);
5928 rc = lod_get_default_lmv_ea(env, lo);
5929 dt_object_put(env, dt);
5933 if (rc < (int)sizeof(*lmu))
5936 lmu = info->lti_ea_store;
5937 return le32_to_cpu(lmu->lum_stripe_offset) == LMV_OFFSET_DEFAULT;
5941 * Implementation of dt_object_operations::do_declare_create.
5943 * The method declares creation of a new object. If the object will be striped,
5944 * then helper functions are called to find FIDs for the stripes, declare
5945 * creation of the stripes and declare initialization of the striping
5946 * information to be stored in the master object.
5948 * \see dt_object_operations::do_declare_create() in the API description
5951 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
5952 struct lu_attr *attr,
5953 struct dt_allocation_hint *hint,
5954 struct dt_object_format *dof, struct thandle *th)
5956 struct dt_object *next = dt_object_child(dt);
5957 struct lod_object *lo = lod_dt_obj(dt);
5966 * first of all, we declare creation of local object
5968 rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
5973 * it's lod_ah_init() that has decided the object will be striped
5975 if (dof->dof_type == DFT_REGULAR) {
5976 /* callers don't want stripes */
5977 /* XXX: all tricky interactions with ->ah_make_hint() decided
5978 * to use striping, then ->declare_create() behaving differently
5979 * should be cleaned */
5980 if (dof->u.dof_reg.striped != 0)
5981 rc = lod_declare_striped_create(env, dt, attr,
5983 } else if (dof->dof_type == DFT_DIR) {
5984 struct seq_server_site *ss;
5985 struct lu_buf buf = { NULL };
5986 struct lu_buf *lmu = NULL;
5988 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
5990 /* If the parent has default stripeEA, and client
5991 * did not find it before sending create request,
5992 * then MDT will return -EREMOTE, and client will
5993 * retrieve the default stripeEA and re-create the
5996 * Note: if dah_eadata != NULL, it means creating the
5997 * striped directory with specified stripeEA, then it
5998 * should ignore the default stripeEA */
5999 if (hint != NULL && hint->dah_eadata == NULL) {
6000 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
6001 GOTO(out, rc = -EREMOTE);
6003 if (lo->ldo_dir_stripe_offset != LMV_OFFSET_DEFAULT &&
6004 lo->ldo_dir_stripe_offset != ss->ss_node_id) {
6005 struct lod_device *lod;
6006 struct lu_tgt_desc *mdt = NULL;
6007 bool found_mdt = false;
6009 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6010 lod_foreach_mdt(lod, mdt) {
6011 if (mdt->ltd_index ==
6012 lo->ldo_dir_stripe_offset) {
6018 /* If the MDT indicated by stripe_offset can be
6019 * found, then tell client to resend the create
6020 * request to the correct MDT, otherwise return
6021 * error to client */
6023 GOTO(out, rc = -EREMOTE);
6025 GOTO(out, rc = -EINVAL);
6027 } else if (hint && hint->dah_eadata) {
6029 lmu->lb_buf = (void *)hint->dah_eadata;
6030 lmu->lb_len = hint->dah_eadata_len;
6033 rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
6037 /* failed to create striping or to set initial size, let's reset
6038 * config so that others don't get confused */
6040 lod_striping_free(env, lo);
6045 * Generate component ID for new created component.
6047 * \param[in] lo LOD object
6048 * \param[in] comp_idx index of ldo_comp_entries
6050 * \retval component ID on success
6051 * \retval LCME_ID_INVAL on failure
6053 static __u32 lod_gen_component_id(struct lod_object *lo,
6054 int mirror_id, int comp_idx)
6056 struct lod_layout_component *lod_comp;
6057 __u32 id, start, end;
6060 LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
6062 lod_obj_inc_layout_gen(lo);
6063 id = lo->ldo_layout_gen;
6064 if (likely(id <= SEQ_ID_MAX))
6065 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
6067 /* Layout generation wraps, need to check collisions. */
6068 start = id & SEQ_ID_MASK;
6071 for (id = start; id <= end; id++) {
6072 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6073 lod_comp = &lo->ldo_comp_entries[i];
6074 if (pflr_id(mirror_id, id) == lod_comp->llc_id)
6077 /* Found the ununsed ID */
6078 if (i == lo->ldo_comp_cnt)
6079 RETURN(pflr_id(mirror_id, id));
6081 if (end == LCME_ID_MAX) {
6083 end = min(lo->ldo_layout_gen & LCME_ID_MASK,
6084 (__u32)(LCME_ID_MAX - 1));
6088 RETURN(LCME_ID_INVAL);
6092 * Creation of a striped regular object.
6094 * The function is called to create the stripe objects for a regular
6095 * striped file. This can happen at the initial object creation or
6096 * when the caller asks LOD to do so using ->do_xattr_set() method
6097 * (so called late striping). Notice all the information are already
6098 * prepared in the form of the list of objects (ldo_stripe field).
6099 * This is done during declare phase.
6101 * \param[in] env execution environment
6102 * \param[in] dt object
6103 * \param[in] attr attributes the stripes will be created with
6104 * \param[in] dof format of stripes (see OSD API description)
6105 * \param[in] th transaction handle
6107 * \retval 0 on success
6108 * \retval negative if failed
6110 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
6111 struct lu_attr *attr, struct dt_object_format *dof,
6114 struct lod_layout_component *lod_comp;
6115 struct lod_object *lo = lod_dt_obj(dt);
6120 mutex_lock(&lo->ldo_layout_mutex);
6122 LASSERT((lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL) ||
6123 lo->ldo_is_foreign);
6125 mirror_id = 0; /* non-flr file's mirror_id is 0 */
6126 if (lo->ldo_mirror_count > 1) {
6127 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6128 lod_comp = &lo->ldo_comp_entries[i];
6129 if (lod_comp->llc_id != LCME_ID_INVAL &&
6130 mirror_id_of(lod_comp->llc_id) > mirror_id)
6131 mirror_id = mirror_id_of(lod_comp->llc_id);
6135 /* create all underlying objects */
6136 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6137 lod_comp = &lo->ldo_comp_entries[i];
6139 if (lod_comp->llc_id == LCME_ID_INVAL) {
6140 /* only the component of FLR layout with more than 1
6141 * mirror has mirror ID in its component ID.
6143 if (lod_comp->llc_extent.e_start == 0 &&
6144 lo->ldo_mirror_count > 1)
6147 lod_comp->llc_id = lod_gen_component_id(lo,
6149 if (lod_comp->llc_id == LCME_ID_INVAL)
6150 GOTO(out, rc = -ERANGE);
6153 if (lod_comp_inited(lod_comp))
6156 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
6157 lod_comp_set_init(lod_comp);
6159 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
6160 lod_comp_set_init(lod_comp);
6162 if (lod_comp->llc_stripe == NULL)
6165 LASSERT(lod_comp->llc_stripe_count);
6166 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6167 struct dt_object *object = lod_comp->llc_stripe[j];
6168 LASSERT(object != NULL);
6169 rc = lod_sub_create(env, object, attr, NULL, dof, th);
6173 lod_comp_set_init(lod_comp);
6176 rc = lod_fill_mirrors(lo);
6180 lo->ldo_comp_cached = 1;
6182 rc = lod_generate_and_set_lovea(env, lo, th);
6186 mutex_unlock(&lo->ldo_layout_mutex);
6191 lod_striping_free_nolock(env, lo);
6192 mutex_unlock(&lo->ldo_layout_mutex);
6197 static inline bool lod_obj_is_dom(struct dt_object *dt)
6199 struct lod_object *lo = lod_dt_obj(dt);
6201 if (!dt_object_exists(dt_object_child(dt)))
6204 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
6207 if (!lo->ldo_comp_cnt)
6210 return (lov_pattern(lo->ldo_comp_entries[0].llc_pattern) ==
6215 * Implementation of dt_object_operations::do_create.
6217 * If any of preceeding methods (like ->do_declare_create(),
6218 * ->do_ah_init(), etc) chose to create a striped object,
6219 * then this method will create the master and the stripes.
6221 * \see dt_object_operations::do_create() in the API description for details.
6223 static int lod_create(const struct lu_env *env, struct dt_object *dt,
6224 struct lu_attr *attr, struct dt_allocation_hint *hint,
6225 struct dt_object_format *dof, struct thandle *th)
6230 /* create local object */
6231 rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
6235 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
6236 (lod_obj_is_striped(dt) || lod_obj_is_dom(dt)) &&
6237 dof->u.dof_reg.striped != 0) {
6238 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
6239 rc = lod_striped_create(env, dt, attr, dof, th);
6246 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
6247 struct dt_object *dt, struct thandle *th,
6248 int comp_idx, int stripe_idx,
6249 struct lod_obj_stripe_cb_data *data)
6251 if (data->locd_declare)
6252 return lod_sub_declare_destroy(env, dt, th);
6254 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
6255 stripe_idx == cfs_fail_val)
6256 return lod_sub_destroy(env, dt, th);
6262 * Implementation of dt_object_operations::do_declare_destroy.
6264 * If the object is a striped directory, then the function declares reference
6265 * removal from the master object (this is an index) to the stripes and declares
6266 * destroy of all the stripes. In all the cases, it declares an intention to
6267 * destroy the object itself.
6269 * \see dt_object_operations::do_declare_destroy() in the API description
6272 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
6275 struct dt_object *next = dt_object_child(dt);
6276 struct lod_object *lo = lod_dt_obj(dt);
6277 struct lod_thread_info *info = lod_env_info(env);
6278 struct dt_object *stripe;
6279 char *stripe_name = info->lti_key;
6285 * load striping information, notice we don't do this when object
6286 * is being initialized as we don't need this information till
6287 * few specific cases like destroy, chown
6289 rc = lod_striping_load(env, lo);
6293 /* declare destroy for all underlying objects */
6294 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6295 rc = next->do_ops->do_index_try(env, next,
6296 &dt_directory_features);
6300 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6301 stripe = lo->ldo_stripe[i];
6305 rc = lod_sub_declare_ref_del(env, next, th);
6309 snprintf(stripe_name, sizeof(info->lti_key),
6311 PFID(lu_object_fid(&stripe->do_lu)), i);
6312 rc = lod_sub_declare_delete(env, next,
6313 (const struct dt_key *)stripe_name, th);
6320 * we declare destroy for the local object
6322 rc = lod_sub_declare_destroy(env, next, th);
6326 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
6327 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
6330 if (!lod_obj_is_striped(dt))
6333 /* declare destroy all striped objects */
6334 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6335 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6336 stripe = lo->ldo_stripe[i];
6340 if (!dt_object_exists(stripe))
6343 rc = lod_sub_declare_ref_del(env, stripe, th);
6347 rc = lod_sub_declare_destroy(env, stripe, th);
6352 struct lod_obj_stripe_cb_data data = { { 0 } };
6354 data.locd_declare = true;
6355 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6356 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6363 * Implementation of dt_object_operations::do_destroy.
6365 * If the object is a striped directory, then the function removes references
6366 * from the master object (this is an index) to the stripes and destroys all
6367 * the stripes. In all the cases, the function destroys the object itself.
6369 * \see dt_object_operations::do_destroy() in the API description for details.
6371 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
6374 struct dt_object *next = dt_object_child(dt);
6375 struct lod_object *lo = lod_dt_obj(dt);
6376 struct lod_thread_info *info = lod_env_info(env);
6377 char *stripe_name = info->lti_key;
6378 struct dt_object *stripe;
6384 /* destroy sub-stripe of master object */
6385 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6386 rc = next->do_ops->do_index_try(env, next,
6387 &dt_directory_features);
6391 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6392 stripe = lo->ldo_stripe[i];
6396 rc = lod_sub_ref_del(env, next, th);
6400 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
6401 PFID(lu_object_fid(&stripe->do_lu)), i);
6403 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
6404 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
6405 PFID(lu_object_fid(&stripe->do_lu)));
6407 rc = lod_sub_delete(env, next,
6408 (const struct dt_key *)stripe_name, th);
6414 rc = lod_sub_destroy(env, next, th);
6418 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
6419 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
6422 if (!lod_obj_is_striped(dt))
6425 /* destroy all striped objects */
6426 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6427 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6428 stripe = lo->ldo_stripe[i];
6432 if (!dt_object_exists(stripe))
6435 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
6436 i == cfs_fail_val) {
6437 dt_write_lock(env, stripe, DT_TGT_CHILD);
6438 rc = lod_sub_ref_del(env, stripe, th);
6439 dt_write_unlock(env, stripe);
6443 rc = lod_sub_destroy(env, stripe, th);
6449 struct lod_obj_stripe_cb_data data = { { 0 } };
6451 data.locd_declare = false;
6452 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6453 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6460 * Implementation of dt_object_operations::do_declare_ref_add.
6462 * \see dt_object_operations::do_declare_ref_add() in the API description
6465 static int lod_declare_ref_add(const struct lu_env *env,
6466 struct dt_object *dt, struct thandle *th)
6468 return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
6472 * Implementation of dt_object_operations::do_ref_add.
6474 * \see dt_object_operations::do_ref_add() in the API description for details.
6476 static int lod_ref_add(const struct lu_env *env,
6477 struct dt_object *dt, struct thandle *th)
6479 return lod_sub_ref_add(env, dt_object_child(dt), th);
6483 * Implementation of dt_object_operations::do_declare_ref_del.
6485 * \see dt_object_operations::do_declare_ref_del() in the API description
6488 static int lod_declare_ref_del(const struct lu_env *env,
6489 struct dt_object *dt, struct thandle *th)
6491 return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
6495 * Implementation of dt_object_operations::do_ref_del
6497 * \see dt_object_operations::do_ref_del() in the API description for details.
6499 static int lod_ref_del(const struct lu_env *env,
6500 struct dt_object *dt, struct thandle *th)
6502 return lod_sub_ref_del(env, dt_object_child(dt), th);
6506 * Implementation of dt_object_operations::do_object_sync.
6508 * \see dt_object_operations::do_object_sync() in the API description
6511 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
6512 __u64 start, __u64 end)
6514 return dt_object_sync(env, dt_object_child(dt), start, end);
6518 * Implementation of dt_object_operations::do_object_unlock.
6520 * Used to release LDLM lock(s).
6522 * \see dt_object_operations::do_object_unlock() in the API description
6525 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
6526 struct ldlm_enqueue_info *einfo,
6527 union ldlm_policy_data *policy)
6529 struct lod_object *lo = lod_dt_obj(dt);
6530 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
6531 int slave_locks_size;
6535 if (slave_locks == NULL)
6538 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
6539 /* Note: for remote lock for single stripe dir, MDT will cancel
6540 * the lock by lockh directly */
6541 LASSERT(!dt_object_remote(dt_object_child(dt)));
6543 /* locks were unlocked in MDT layer */
6544 for (i = 0; i < slave_locks->ha_count; i++)
6545 LASSERT(!lustre_handle_is_used(&slave_locks->ha_handles[i]));
6548 * NB, ha_count may not equal to ldo_dir_stripe_count, because dir
6549 * layout may change, e.g., shrink dir layout after migration.
6551 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6552 if (lo->ldo_stripe[i])
6553 dt_invalidate(env, lo->ldo_stripe[i]);
6556 slave_locks_size = offsetof(typeof(*slave_locks),
6557 ha_handles[slave_locks->ha_count]);
6558 OBD_FREE(slave_locks, slave_locks_size);
6559 einfo->ei_cbdata = NULL;
6565 * Implementation of dt_object_operations::do_object_lock.
6567 * Used to get LDLM lock on the non-striped and striped objects.
6569 * \see dt_object_operations::do_object_lock() in the API description
6572 static int lod_object_lock(const struct lu_env *env,
6573 struct dt_object *dt,
6574 struct lustre_handle *lh,
6575 struct ldlm_enqueue_info *einfo,
6576 union ldlm_policy_data *policy)
6578 struct lod_object *lo = lod_dt_obj(dt);
6579 int slave_locks_size;
6580 struct lustre_handle_array *slave_locks = NULL;
6585 /* remote object lock */
6586 if (!einfo->ei_enq_slave) {
6587 LASSERT(dt_object_remote(dt));
6588 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
6592 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
6595 rc = lod_striping_load(env, lo);
6600 if (lo->ldo_dir_stripe_count <= 1)
6603 slave_locks_size = offsetof(typeof(*slave_locks),
6604 ha_handles[lo->ldo_dir_stripe_count]);
6605 /* Freed in lod_object_unlock */
6606 OBD_ALLOC(slave_locks, slave_locks_size);
6609 slave_locks->ha_count = lo->ldo_dir_stripe_count;
6611 /* striped directory lock */
6612 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6613 struct lustre_handle lockh;
6614 struct ldlm_res_id *res_id;
6615 struct dt_object *stripe;
6617 stripe = lo->ldo_stripe[i];
6621 res_id = &lod_env_info(env)->lti_res_id;
6622 fid_build_reg_res_name(lu_object_fid(&stripe->do_lu), res_id);
6623 einfo->ei_res_id = res_id;
6625 if (dt_object_remote(stripe)) {
6626 set_bit(i, (void *)slave_locks->ha_map);
6627 rc = dt_object_lock(env, stripe, &lockh, einfo, policy);
6629 struct ldlm_namespace *ns = einfo->ei_namespace;
6630 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
6631 ldlm_completion_callback completion = einfo->ei_cb_cp;
6632 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
6634 if (einfo->ei_mode == LCK_PW ||
6635 einfo->ei_mode == LCK_EX)
6636 dlmflags |= LDLM_FL_COS_INCOMPAT;
6638 LASSERT(ns != NULL);
6639 rc = ldlm_cli_enqueue_local(env, ns, res_id, LDLM_IBITS,
6640 policy, einfo->ei_mode,
6641 &dlmflags, blocking,
6643 NULL, 0, LVB_T_NONE,
6648 ldlm_lock_decref_and_cancel(
6649 &slave_locks->ha_handles[i],
6651 OBD_FREE(slave_locks, slave_locks_size);
6654 slave_locks->ha_handles[i] = lockh;
6656 einfo->ei_cbdata = slave_locks;
6662 * Implementation of dt_object_operations::do_invalidate.
6664 * \see dt_object_operations::do_invalidate() in the API description for details
6666 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
6668 return dt_invalidate(env, dt_object_child(dt));
6671 static int lod_declare_instantiate_components(const struct lu_env *env,
6672 struct lod_object *lo,
6676 struct lod_thread_info *info = lod_env_info(env);
6681 LASSERT(info->lti_count < lo->ldo_comp_cnt);
6683 for (i = 0; i < info->lti_count; i++) {
6684 rc = lod_qos_prep_create(env, lo, NULL, th,
6685 info->lti_comp_idx[i], reserve);
6691 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6692 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
6693 &info->lti_buf, XATTR_NAME_LOV, 0, th);
6700 * Check OSTs for an existing component for further extension
6702 * Checks if OSTs are still healthy and not out of space. Gets free space
6703 * on OSTs (relative to allocation watermark rmb_low) and compares to
6704 * the proposed new_end for this component.
6706 * Decides whether or not to extend a component on its current OSTs.
6708 * \param[in] env execution environment for this thread
6709 * \param[in] lo object we're checking
6710 * \param[in] index index of this component
6711 * \param[in] extension_size extension size for this component
6712 * \param[in] extent layout extent for requested operation
6713 * \param[in] comp_extent extension component extent
6714 * \param[in] write if this is write operation
6716 * \retval true - OK to extend on current OSTs
6717 * \retval false - do not extend on current OSTs
6719 static bool lod_sel_osts_allowed(const struct lu_env *env,
6720 struct lod_object *lo,
6721 int index, __u64 reserve,
6722 struct lu_extent *extent,
6723 struct lu_extent *comp_extent, int write)
6725 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[index];
6726 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6727 struct lod_thread_info *tinfo = lod_env_info(env);
6728 struct obd_statfs *sfs = &tinfo->lti_osfs;
6729 __u64 available = 0;
6735 LASSERT(lod_comp->llc_stripe_count != 0);
6737 lod_getref(&lod->lod_ost_descs);
6738 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
6739 int index = lod_comp->llc_ost_indices[i];
6740 struct lod_tgt_desc *ost = OST_TGT(lod, index);
6741 struct obd_statfs_info info = { 0 };
6742 int j, repeated = 0;
6746 /* Get the number of times this OST repeats in this component.
6747 * Note: inter-component repeats are not counted as this is
6748 * considered as a rare case: we try to not repeat OST in other
6749 * components if possible. */
6750 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6751 if (index != lod_comp->llc_ost_indices[j])
6754 /* already handled */
6760 if (j < lod_comp->llc_stripe_count)
6763 if (!test_bit(index, lod->lod_ost_bitmap)) {
6764 CDEBUG(D_LAYOUT, "ost %d no longer present\n", index);
6769 rc = dt_statfs_info(env, ost->ltd_tgt, sfs, &info);
6771 CDEBUG(D_LAYOUT, "statfs failed for ost %d, error %d\n",
6777 if (sfs->os_state & OS_STATFS_ENOSPC ||
6778 sfs->os_state & OS_STATFS_READONLY ||
6779 sfs->os_state & OS_STATFS_DEGRADED) {
6780 CDEBUG(D_LAYOUT, "ost %d is not availble for SEL "
6781 "extension, state %u\n", index, sfs->os_state);
6787 available = sfs->os_bavail * sfs->os_bsize;
6788 /* 'available' is relative to the allocation threshold */
6789 available -= (__u64) info.os_reserved_mb_low << 20;
6791 CDEBUG(D_LAYOUT, "ost %d lowwm: %d highwm: %d, "
6792 "%llu %% blocks available, %llu %% blocks free\n",
6793 index, info.os_reserved_mb_low, info.os_reserved_mb_high,
6794 (100ull * sfs->os_bavail) / sfs->os_blocks,
6795 (100ull * sfs->os_bfree) / sfs->os_blocks);
6797 if (reserve * repeated > available) {
6799 CDEBUG(D_LAYOUT, "low space on ost %d, available %llu "
6800 "< extension size %llu repeated %d\n", index,
6801 available, reserve, repeated);
6805 lod_putref(lod, &lod->lod_ost_descs);
6811 * Adjust extents after component removal
6813 * When we remove an extension component, we move the start of the next
6814 * component to match the start of the extension component, so no space is left
6817 * \param[in] env execution environment for this thread
6818 * \param[in] lo object
6819 * \param[in] max_comp layout component
6820 * \param[in] index index of this component
6822 * \retval 0 on success
6823 * \retval negative errno on error
6825 static void lod_sel_adjust_extents(const struct lu_env *env,
6826 struct lod_object *lo,
6827 int max_comp, int index)
6829 struct lod_layout_component *lod_comp = NULL;
6830 struct lod_layout_component *next = NULL;
6831 struct lod_layout_component *prev = NULL;
6832 __u64 new_start = 0;
6836 /* Extension space component */
6837 lod_comp = &lo->ldo_comp_entries[index];
6838 next = &lo->ldo_comp_entries[index + 1];
6839 prev = &lo->ldo_comp_entries[index - 1];
6841 LASSERT(lod_comp != NULL && prev != NULL && next != NULL);
6842 LASSERT(lod_comp->llc_flags & LCME_FL_EXTENSION);
6844 /* Previous is being removed */
6845 if (prev && prev->llc_id == LCME_ID_INVAL)
6846 new_start = prev->llc_extent.e_start;
6848 new_start = lod_comp->llc_extent.e_start;
6850 for (i = index + 1; i < max_comp; i++) {
6851 lod_comp = &lo->ldo_comp_entries[i];
6853 start = lod_comp->llc_extent.e_start;
6854 lod_comp->llc_extent.e_start = new_start;
6856 /* We only move zero length extendable components */
6857 if (!(start == lod_comp->llc_extent.e_end))
6860 LASSERT(!(lod_comp->llc_flags & LCME_FL_INIT));
6862 lod_comp->llc_extent.e_end = new_start;
6866 /* Calculate the proposed 'new end' for a component we're extending */
6867 static __u64 lod_extension_new_end(__u64 extension_size, __u64 extent_end,
6868 __u32 stripe_size, __u64 component_end,
6869 __u64 extension_end)
6873 LASSERT(extension_size != 0 && stripe_size != 0);
6875 /* Round up to extension size */
6876 if (extent_end == OBD_OBJECT_EOF) {
6877 new_end = OBD_OBJECT_EOF;
6879 /* Add at least extension_size to the previous component_end,
6880 * covering the req layout extent */
6881 new_end = max(extent_end - component_end, extension_size);
6882 new_end = roundup(new_end, extension_size);
6883 new_end += component_end;
6885 /* Component end must be min stripe size aligned */
6886 if (new_end % stripe_size) {
6887 CDEBUG(D_LAYOUT, "new component end is not aligned "
6888 "by the stripe size %u: [%llu, %llu) ext size "
6889 "%llu new end %llu, aligning\n",
6890 stripe_size, component_end, extent_end,
6891 extension_size, new_end);
6892 new_end = roundup(new_end, stripe_size);
6896 if (new_end < extent_end)
6897 new_end = OBD_OBJECT_EOF;
6900 /* Don't extend past the end of the extension component */
6901 if (new_end > extension_end)
6902 new_end = extension_end;
6908 * Calculate the exact reservation (per-OST extension_size) on the OSTs being
6909 * instantiated. It needs to be calculated in advance and taken into account at
6910 * the instantiation time, because otherwise lod_statfs_and_check() may consider
6911 * an OST as OK, but SEL needs its extension_size to fit the free space and the
6912 * OST may turn out to be low-on-space, thus inappropriate OST may be used and
6915 * \param[in] lod_comp lod component we are checking
6917 * \retval size to reserved on each OST of lod_comp's stripe.
6919 static __u64 lod_sel_stripe_reserved(struct lod_layout_component *lod_comp)
6921 /* extension_size is file level, so we must divide by stripe count to
6922 * compare it to available space on a single OST */
6923 return lod_comp->llc_stripe_size * SEL_UNIT_SIZE /
6924 lod_comp->llc_stripe_count;
6927 /* As lod_sel_handler() could be re-entered for the same component several
6928 * times, this is the data for the next call. Fields could be changed to
6929 * component indexes when needed, (e.g. if there is no need to instantiate
6930 * all the previous components up to the current position) to tell the caller
6931 * where to start over from. */
6938 * Process extent updates for a particular layout component
6940 * Handle layout updates for a particular extension space component touched by
6941 * a layout update operation. Core function of self-extending PFL feature.
6943 * In general, this function processes exactly *one* stage of an extension
6944 * operation, modifying the layout accordingly, then returns to the caller.
6945 * The caller is responsible for restarting processing with the new layout,
6946 * which may repeatedly return to this function until the extension updates
6949 * This function does one of a few things to the layout:
6950 * 1. Extends the component before the current extension space component to
6951 * allow it to accomodate the requested operation (if space/policy permit that
6952 * component to continue on its current OSTs)
6954 * 2. If extension of the existing component fails, we do one of two things:
6955 * a. If there is a component after the extension space, we remove the
6956 * extension space component, move the start of the next component down
6957 * accordingly, then notify the caller to restart processing w/the new
6959 * b. If there is no following component, we try repeating the current
6960 * component, creating a new component using the current one as a
6961 * template (keeping its stripe properties but not specific striping),
6962 * and try assigning striping for this component. If there is sufficient
6963 * free space on the OSTs chosen for this component, it is instantiated
6964 * and i/o continues there.
6966 * If there is not sufficient space on the new OSTs, we remove this new
6967 * component & extend the current component.
6969 * Note further that uninited components followed by extension space can be zero
6970 * length meaning that we will try to extend them before initializing them, and
6971 * if that fails, they will be removed without initialization.
6973 * 3. If we extend to/beyond the end of an extension space component, that
6974 * component is exhausted (all of its range has been given to real components),
6975 * so we remove it and restart processing.
6977 * \param[in] env execution environment for this thread
6978 * \param[in,out] lo object to update the layout of
6979 * \param[in] extent layout extent for requested operation, update
6980 * layout to fit this operation
6981 * \param[in] th transaction handle for this operation
6982 * \param[in,out] max_comp the highest comp for the portion of the layout
6983 * we are operating on (For FLR, the chosen
6984 * replica). Updated because we may remove
6986 * \param[in] index index of the extension space component we're
6988 * \param[in] write if this is write op
6989 * \param[in,out] force if the extension is to be forced; set here
6990 to force it on the 2nd call for the same
6993 * \retval 0 on success
6994 * \retval negative errno on error
6996 static int lod_sel_handler(const struct lu_env *env,
6997 struct lod_object *lo,
6998 struct lu_extent *extent,
6999 struct thandle *th, int *max_comp,
7000 int index, int write,
7001 struct sel_data *sd)
7003 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7004 struct lod_thread_info *info = lod_env_info(env);
7005 struct lod_layout_component *lod_comp;
7006 struct lod_layout_component *prev;
7007 struct lod_layout_component *next = NULL;
7008 __u64 extension_size, reserve;
7015 /* First component cannot be extension space */
7017 CERROR("%s: "DFID" first component cannot be extension space\n",
7018 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
7022 lod_comp = &lo->ldo_comp_entries[index];
7023 prev = &lo->ldo_comp_entries[index - 1];
7024 if ((index + 1) < *max_comp)
7025 next = &lo->ldo_comp_entries[index + 1];
7027 /* extension size uses the stripe size field as KiB */
7028 extension_size = lod_comp->llc_stripe_size * SEL_UNIT_SIZE;
7030 CDEBUG(D_LAYOUT, "prev start %llu, extension start %llu, extension end"
7031 " %llu, extension size %llu\n", prev->llc_extent.e_start,
7032 lod_comp->llc_extent.e_start, lod_comp->llc_extent.e_end,
7035 /* Two extension space components cannot be adjacent & extension space
7036 * components cannot be init */
7037 if ((prev->llc_flags & LCME_FL_EXTENSION) ||
7038 !(ergo(next, !(next->llc_flags & LCME_FL_EXTENSION))) ||
7039 lod_comp_inited(lod_comp)) {
7040 CERROR("%s: "DFID" invalid extension space components\n",
7041 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
7045 reserve = lod_sel_stripe_reserved(lod_comp);
7047 if (!prev->llc_stripe) {
7048 CDEBUG(D_LAYOUT, "Previous component not inited\n");
7049 info->lti_count = 1;
7050 info->lti_comp_idx[0] = index - 1;
7051 rc = lod_declare_instantiate_components(env, lo, th, reserve);
7052 /* ENOSPC tells us we can't use this component. If there is
7053 * a next or we are repeating, we either spill over (next) or
7054 * extend the original comp (repeat). Otherwise, return the
7055 * error to the user. */
7056 if (rc == -ENOSPC && (next || sd->sd_repeat))
7062 if (sd->sd_force == 0 && rc == 0)
7063 rc = !lod_sel_osts_allowed(env, lo, index - 1, reserve, extent,
7064 &lod_comp->llc_extent, write);
7066 repeated = !!(sd->sd_repeat);
7070 /* Extend previous component */
7072 new_end = lod_extension_new_end(extension_size, extent->e_end,
7073 prev->llc_stripe_size,
7074 prev->llc_extent.e_end,
7075 lod_comp->llc_extent.e_end);
7077 CDEBUG(D_LAYOUT, "new end %llu\n", new_end);
7078 lod_comp->llc_extent.e_start = new_end;
7079 prev->llc_extent.e_end = new_end;
7081 if (prev->llc_extent.e_end == lod_comp->llc_extent.e_end) {
7082 CDEBUG(D_LAYOUT, "Extension component exhausted\n");
7083 lod_comp->llc_id = LCME_ID_INVAL;
7087 /* rc == 1, failed to extend current component */
7090 /* Normal 'spillover' case - Remove the extension
7091 * space component & bring down the start of the next
7093 lod_comp->llc_id = LCME_ID_INVAL;
7095 if (!(prev->llc_flags & LCME_FL_INIT)) {
7096 prev->llc_id = LCME_ID_INVAL;
7099 lod_sel_adjust_extents(env, lo, *max_comp, index);
7100 } else if (lod_comp_inited(prev)) {
7101 /* If there is no next, and the previous component is
7102 * INIT'ed, try repeating the previous component. */
7103 LASSERT(repeated == 0);
7104 rc = lod_layout_repeat_comp(env, lo, index - 1);
7108 /* The previous component is a repeated component.
7109 * Record this so we don't keep trying to repeat it. */
7112 /* If the previous component is not INIT'ed, this may
7113 * be a component we have just instantiated but failed
7114 * to extend. Or even a repeated component we failed
7115 * to prepare a striping for. Do not repeat but instead
7116 * remove the repeated component & force the extention
7117 * of the original one */
7120 prev->llc_id = LCME_ID_INVAL;
7127 rc = lod_layout_del_prep_layout(env, lo, NULL);
7130 LASSERTF(-rc == change,
7131 "number deleted %d != requested %d\n", -rc,
7134 *max_comp = *max_comp + change;
7136 /* lod_del_prep_layout reallocates ldo_comp_entries, so we must
7137 * refresh these pointers before using them */
7138 lod_comp = &lo->ldo_comp_entries[index];
7139 prev = &lo->ldo_comp_entries[index - 1];
7140 CDEBUG(D_LAYOUT, "After extent updates: prev start %llu, current start "
7141 "%llu, current end %llu max_comp %d ldo_comp_cnt %d\n",
7142 prev->llc_extent.e_start, lod_comp->llc_extent.e_start,
7143 lod_comp->llc_extent.e_end, *max_comp, lo->ldo_comp_cnt);
7145 /* Layout changed successfully */
7150 * Declare layout extent updates
7152 * Handles extensions. Identifies extension components touched by current
7153 * operation and passes them to processing function.
7155 * Restarts with updated layouts from the processing function until the current
7156 * operation no longer touches an extension space component.
7158 * \param[in] env execution environment for this thread
7159 * \param[in,out] lo object to update the layout of
7160 * \param[in] extent layout extent for requested operation, update layout to
7161 * fit this operation
7162 * \param[in] th transaction handle for this operation
7163 * \param[in] pick identifies chosen mirror for FLR layouts
7164 * \param[in] write if this is write op
7166 * \retval 1 on layout changed, 0 on no change
7167 * \retval negative errno on error
7169 static int lod_declare_update_extents(const struct lu_env *env,
7170 struct lod_object *lo, struct lu_extent *extent,
7171 struct thandle *th, int pick, int write)
7173 struct lod_thread_info *info = lod_env_info(env);
7174 struct lod_layout_component *lod_comp;
7175 bool layout_changed = false;
7176 struct sel_data sd = { 0 };
7184 /* This makes us work on the components of the chosen mirror */
7185 start_index = lo->ldo_mirrors[pick].lme_start;
7186 max_comp = lo->ldo_mirrors[pick].lme_end + 1;
7187 if (lo->ldo_flr_state == LCM_FL_NONE)
7188 LASSERT(start_index == 0 && max_comp == lo->ldo_comp_cnt);
7190 CDEBUG(D_LAYOUT, "extent->e_start %llu, extent->e_end %llu\n",
7191 extent->e_start, extent->e_end);
7192 for (i = start_index; i < max_comp; i++) {
7193 lod_comp = &lo->ldo_comp_entries[i];
7195 /* We've passed all components of interest */
7196 if (lod_comp->llc_extent.e_start >= extent->e_end)
7199 if (lod_comp->llc_flags & LCME_FL_EXTENSION) {
7200 layout_changed = true;
7201 rc = lod_sel_handler(env, lo, extent, th, &max_comp,
7206 /* Nothing has changed behind the prev one */
7212 /* We may have added or removed components. If so, we must update the
7213 * start & ends of all the mirrors after the current one, and the end
7214 * of the current mirror. */
7215 change = max_comp - 1 - lo->ldo_mirrors[pick].lme_end;
7217 lo->ldo_mirrors[pick].lme_end += change;
7218 for (i = pick + 1; i < lo->ldo_mirror_count; i++) {
7219 lo->ldo_mirrors[i].lme_start += change;
7220 lo->ldo_mirrors[i].lme_end += change;
7226 /* The amount of components has changed, adjust the lti_comp_idx */
7227 rc2 = lod_layout_data_init(info, lo->ldo_comp_cnt);
7229 return rc < 0 ? rc : rc2 < 0 ? rc2 : layout_changed;
7232 /* If striping is already instantiated or INIT'ed DOM? */
7233 static bool lod_is_instantiation_needed(struct lod_layout_component *comp)
7235 return !(((lov_pattern(comp->llc_pattern) == LOV_PATTERN_MDT) &&
7236 lod_comp_inited(comp)) || comp->llc_stripe);
7240 * Declare layout update for a non-FLR layout.
7242 * \param[in] env execution environment for this thread
7243 * \param[in,out] lo object to update the layout of
7244 * \param[in] layout layout intent for requested operation, "update" is
7245 * a process of reacting to this
7246 * \param[in] buf buffer containing lov ea (see comment on usage inline)
7247 * \param[in] th transaction handle for this operation
7249 * \retval 0 on success
7250 * \retval negative errno on error
7252 static int lod_declare_update_plain(const struct lu_env *env,
7253 struct lod_object *lo, struct layout_intent *layout,
7254 const struct lu_buf *buf, struct thandle *th)
7256 struct lod_thread_info *info = lod_env_info(env);
7257 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7258 struct lod_layout_component *lod_comp;
7259 struct lov_comp_md_v1 *comp_v1 = NULL;
7260 bool layout_changed = false;
7261 bool replay = false;
7265 LASSERT(lo->ldo_flr_state == LCM_FL_NONE);
7268 * In case the client is passing lovea, which only happens during
7269 * the replay of layout intent write RPC for now, we may need to
7270 * parse the lovea and apply new layout configuration.
7272 if (buf && buf->lb_len) {
7273 struct lov_user_md_v1 *v1 = buf->lb_buf;
7275 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
7276 v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
7277 LOV_MAGIC_COMP_V1)) {
7278 CERROR("%s: the replay buffer of layout extend "
7279 "(magic %#x) does not contain expected "
7280 "composite layout.\n",
7281 lod2obd(d)->obd_name, v1->lmm_magic);
7282 GOTO(out, rc = -EINVAL);
7285 rc = lod_use_defined_striping(env, lo, buf);
7288 lo->ldo_comp_cached = 1;
7290 rc = lod_get_lov_ea(env, lo);
7293 /* old on-disk EA is stored in info->lti_buf */
7294 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
7296 layout_changed = true;
7298 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
7302 /* non replay path */
7303 rc = lod_striping_load(env, lo);
7308 /* Make sure defined layout covers the requested write range. */
7309 lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
7310 if (lo->ldo_comp_cnt > 1 &&
7311 lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
7312 lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
7313 CDEBUG_LIMIT(replay ? D_ERROR : D_LAYOUT,
7314 "%s: the defined layout [0, %#llx) does not "
7315 "covers the write range "DEXT"\n",
7316 lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
7317 PEXT(&layout->li_extent));
7318 GOTO(out, rc = -EINVAL);
7321 CDEBUG(D_LAYOUT, "%s: "DFID": update components "DEXT"\n",
7322 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
7323 PEXT(&layout->li_extent));
7326 rc = lod_declare_update_extents(env, lo, &layout->li_extent,
7327 th, 0, layout->li_opc == LAYOUT_INTENT_WRITE);
7331 layout_changed = true;
7335 * Iterate ld->ldo_comp_entries, find the component whose extent under
7336 * the write range and not instantianted.
7338 for (i = 0; i < lo->ldo_comp_cnt; i++) {
7339 lod_comp = &lo->ldo_comp_entries[i];
7341 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
7345 /* If striping is instantiated or INIT'ed DOM skip */
7346 if (!lod_is_instantiation_needed(lod_comp))
7350 * In replay path, lod_comp is the EA passed by
7351 * client replay buffer, comp_v1 is the pre-recovery
7352 * on-disk EA, we'd sift out those components which
7353 * were init-ed in the on-disk EA.
7355 if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
7360 * this component hasn't instantiated in normal path, or during
7361 * replay it needs replay the instantiation.
7364 /* A released component is being extended */
7365 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
7366 GOTO(out, rc = -EINVAL);
7368 LASSERT(info->lti_comp_idx != NULL);
7369 info->lti_comp_idx[info->lti_count++] = i;
7370 layout_changed = true;
7373 if (!layout_changed)
7376 lod_obj_inc_layout_gen(lo);
7377 rc = lod_declare_instantiate_components(env, lo, th, 0);
7381 lod_striping_free(env, lo);
7385 static inline int lod_comp_index(struct lod_object *lo,
7386 struct lod_layout_component *lod_comp)
7388 LASSERT(lod_comp >= lo->ldo_comp_entries &&
7389 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
7391 return lod_comp - lo->ldo_comp_entries;
7395 * Stale other mirrors by writing extent.
7397 static int lod_stale_components(const struct lu_env *env, struct lod_object *lo,
7398 int primary, struct lu_extent *extent,
7401 struct lod_layout_component *pri_comp, *lod_comp;
7402 struct lu_extent pri_extent;
7407 /* The writing extent decides which components in the primary
7408 * are affected... */
7409 CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
7412 lod_foreach_mirror_comp(pri_comp, lo, primary) {
7413 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
7416 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
7417 lod_comp_index(lo, pri_comp),
7418 PEXT(&pri_comp->llc_extent));
7420 pri_extent.e_start = pri_comp->llc_extent.e_start;
7421 pri_extent.e_end = pri_comp->llc_extent.e_end;
7423 for (i = 0; i < lo->ldo_mirror_count; i++) {
7426 rc = lod_declare_update_extents(env, lo, &pri_extent,
7428 /* if update_extents changed the layout, it may have
7429 * reallocated the component array, so start over to
7430 * avoid using stale pointers */
7436 /* ... and then stale other components that are
7437 * overlapping with primary components */
7438 lod_foreach_mirror_comp(lod_comp, lo, i) {
7439 if (!lu_extent_is_overlapped(
7441 &lod_comp->llc_extent))
7444 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
7445 i, lod_comp_index(lo, lod_comp));
7447 lod_comp->llc_flags |= LCME_FL_STALE;
7448 lo->ldo_mirrors[i].lme_stale = 1;
7457 * check an OST's availability
7458 * \param[in] env execution environment
7459 * \param[in] lo lod object
7460 * \param[in] dt dt object
7461 * \param[in] index mirror index
7463 * \retval negative if failed
7464 * \retval 1 if \a dt is available
7465 * \retval 0 if \a dt is not available
7467 static inline int lod_check_ost_avail(const struct lu_env *env,
7468 struct lod_object *lo,
7469 struct dt_object *dt, int index)
7471 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7472 struct lod_tgt_desc *ost;
7474 int type = LU_SEQ_RANGE_OST;
7477 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &idx, &type);
7479 CERROR("%s: can't locate "DFID":rc = %d\n",
7480 lod2obd(lod)->obd_name, PFID(lu_object_fid(&dt->do_lu)),
7485 ost = OST_TGT(lod, idx);
7486 if (ost->ltd_statfs.os_state &
7487 (OS_STATFS_READONLY | OS_STATFS_ENOSPC | OS_STATFS_ENOINO |
7488 OS_STATFS_NOPRECREATE) ||
7489 ost->ltd_active == 0) {
7490 CDEBUG(D_LAYOUT, DFID ": mirror %d OST%d unavail, rc = %d\n",
7491 PFID(lod_object_fid(lo)), index, idx, rc);
7499 * Pick primary mirror for write
7500 * \param[in] env execution environment
7501 * \param[in] lo object
7502 * \param[in] extent write range
7504 static int lod_primary_pick(const struct lu_env *env, struct lod_object *lo,
7505 struct lu_extent *extent)
7507 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7508 unsigned int seq = 0;
7509 struct lod_layout_component *lod_comp;
7511 int picked = -1, second_pick = -1, third_pick = -1;
7514 if (OBD_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
7515 get_random_bytes(&seq, sizeof(seq));
7516 seq %= lo->ldo_mirror_count;
7520 * Pick a mirror as the primary, and check the availability of OSTs.
7522 * This algo can be revised later after knowing the topology of
7525 lod_qos_statfs_update(env, lod, &lod->lod_ost_descs);
7527 rc = lod_fill_mirrors(lo);
7531 for (i = 0; i < lo->ldo_mirror_count; i++) {
7532 bool ost_avail = true;
7533 int index = (i + seq) % lo->ldo_mirror_count;
7535 if (lo->ldo_mirrors[index].lme_stale) {
7536 CDEBUG(D_LAYOUT, DFID": mirror %d stale\n",
7537 PFID(lod_object_fid(lo)), index);
7541 /* 2nd pick is for the primary mirror containing unavail OST */
7542 if (lo->ldo_mirrors[index].lme_prefer && second_pick < 0)
7543 second_pick = index;
7545 /* 3rd pick is for non-primary mirror containing unavail OST */
7546 if (second_pick < 0 && third_pick < 0)
7550 * we found a non-primary 1st pick, we'd like to find a
7551 * potential pirmary mirror.
7553 if (picked >= 0 && !lo->ldo_mirrors[index].lme_prefer)
7556 /* check the availability of OSTs */
7557 lod_foreach_mirror_comp(lod_comp, lo, index) {
7558 if (!lod_comp_inited(lod_comp) || !lod_comp->llc_stripe)
7561 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
7562 struct dt_object *dt = lod_comp->llc_stripe[j];
7564 rc = lod_check_ost_avail(env, lo, dt, index);
7571 } /* for all dt object in one component */
7574 } /* for all components in a mirror */
7577 * the OSTs where allocated objects locates in the components
7578 * of the mirror are available.
7583 /* this mirror has all OSTs available */
7587 * primary with all OSTs are available, this is the perfect
7590 if (lo->ldo_mirrors[index].lme_prefer)
7592 } /* for all mirrors */
7594 /* failed to pick a sound mirror, lower our expectation */
7596 picked = second_pick;
7598 picked = third_pick;
7605 static int lod_prepare_resync_mirror(const struct lu_env *env,
7606 struct lod_object *lo,
7609 struct lod_thread_info *info = lod_env_info(env);
7610 struct lod_layout_component *lod_comp;
7611 bool neg = !!(MIRROR_ID_NEG & mirror_id);
7614 mirror_id &= ~MIRROR_ID_NEG;
7616 for (i = 0; i < lo->ldo_mirror_count; i++) {
7617 if ((!neg && lo->ldo_mirrors[i].lme_id != mirror_id) ||
7618 (neg && lo->ldo_mirrors[i].lme_id == mirror_id))
7621 lod_foreach_mirror_comp(lod_comp, lo, i) {
7622 if (lod_comp_inited(lod_comp))
7625 info->lti_comp_idx[info->lti_count++] =
7626 lod_comp_index(lo, lod_comp);
7634 * figure out the components should be instantiated for resync.
7636 static int lod_prepare_resync(const struct lu_env *env, struct lod_object *lo,
7637 struct lu_extent *extent)
7639 struct lod_thread_info *info = lod_env_info(env);
7640 struct lod_layout_component *lod_comp;
7641 unsigned int need_sync = 0;
7645 DFID": instantiate all stale components in "DEXT"\n",
7646 PFID(lod_object_fid(lo)), PEXT(extent));
7649 * instantiate all components within this extent, even non-stale
7652 for (i = 0; i < lo->ldo_mirror_count; i++) {
7653 if (!lo->ldo_mirrors[i].lme_stale)
7656 lod_foreach_mirror_comp(lod_comp, lo, i) {
7657 if (!lu_extent_is_overlapped(extent,
7658 &lod_comp->llc_extent))
7663 if (lod_comp_inited(lod_comp))
7666 CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
7667 i, lod_comp_index(lo, lod_comp));
7668 info->lti_comp_idx[info->lti_count++] =
7669 lod_comp_index(lo, lod_comp);
7673 return need_sync ? 0 : -EALREADY;
7676 static int lod_declare_update_rdonly(const struct lu_env *env,
7677 struct lod_object *lo, struct md_layout_change *mlc,
7680 struct lod_thread_info *info = lod_env_info(env);
7681 struct lu_attr *layout_attr = &info->lti_layout_attr;
7682 struct lod_layout_component *lod_comp;
7683 struct lu_extent extent = { 0 };
7687 LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
7688 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7689 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7690 LASSERT(lo->ldo_mirror_count > 0);
7692 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7693 struct layout_intent *layout = mlc->mlc_intent;
7694 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7697 extent = layout->li_extent;
7698 CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
7699 PFID(lod_object_fid(lo)), PEXT(&extent));
7701 picked = lod_primary_pick(env, lo, &extent);
7705 CDEBUG(D_LAYOUT, DFID": picked mirror id %u as primary\n",
7706 PFID(lod_object_fid(lo)),
7707 lo->ldo_mirrors[picked].lme_id);
7709 /* Update extents of primary before staling */
7710 rc = lod_declare_update_extents(env, lo, &extent, th, picked,
7715 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
7717 * trunc transfers [0, size) in the intent extent, we'd
7718 * stale components overlapping [size, eof).
7720 extent.e_start = extent.e_end;
7721 extent.e_end = OBD_OBJECT_EOF;
7724 /* stale overlapping components from other mirrors */
7725 rc = lod_stale_components(env, lo, picked, &extent, th);
7729 /* restore truncate intent extent */
7730 if (layout->li_opc == LAYOUT_INTENT_TRUNC)
7731 extent.e_end = extent.e_start;
7733 /* instantiate components for the picked mirror, start from 0 */
7736 lod_foreach_mirror_comp(lod_comp, lo, picked) {
7737 if (!lu_extent_is_overlapped(&extent,
7738 &lod_comp->llc_extent))
7741 if (!lod_is_instantiation_needed(lod_comp))
7744 info->lti_comp_idx[info->lti_count++] =
7745 lod_comp_index(lo, lod_comp);
7748 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7749 } else { /* MD_LAYOUT_RESYNC */
7753 * could contain multiple non-stale mirrors, so we need to
7754 * prep uninited all components assuming any non-stale mirror
7755 * could be picked as the primary mirror.
7757 if (mlc->mlc_mirror_id == 0) {
7759 for (i = 0; i < lo->ldo_mirror_count; i++) {
7760 if (lo->ldo_mirrors[i].lme_stale)
7763 lod_foreach_mirror_comp(lod_comp, lo, i) {
7764 if (!lod_comp_inited(lod_comp))
7768 lod_comp->llc_extent.e_end)
7770 lod_comp->llc_extent.e_end;
7773 rc = lod_prepare_resync(env, lo, &extent);
7777 /* mirror write, try to init its all components */
7778 rc = lod_prepare_resync_mirror(env, lo,
7779 mlc->mlc_mirror_id);
7784 /* change the file state to SYNC_PENDING */
7785 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7788 /* Reset the layout version once it's becoming too large.
7789 * This way it can make sure that the layout version is
7790 * monotonously increased in this writing era. */
7791 lod_obj_inc_layout_gen(lo);
7792 if (lo->ldo_layout_gen > (LCME_ID_MAX >> 1)) {
7793 __u32 layout_version;
7795 get_random_bytes(&layout_version, sizeof(layout_version));
7796 lo->ldo_layout_gen = layout_version & 0xffff;
7799 rc = lod_declare_instantiate_components(env, lo, th, 0);
7803 layout_attr->la_valid = LA_LAYOUT_VERSION;
7804 layout_attr->la_layout_version = 0; /* set current version */
7805 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7806 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7807 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7813 lod_striping_free(env, lo);
7817 static int lod_declare_update_write_pending(const struct lu_env *env,
7818 struct lod_object *lo, struct md_layout_change *mlc,
7821 struct lod_thread_info *info = lod_env_info(env);
7822 struct lu_attr *layout_attr = &info->lti_layout_attr;
7823 struct lod_layout_component *lod_comp;
7824 struct lu_extent extent = { 0 };
7830 LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
7831 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7832 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7834 /* look for the first preferred mirror */
7835 for (i = 0; i < lo->ldo_mirror_count; i++) {
7836 if (lo->ldo_mirrors[i].lme_stale)
7838 if (lo->ldo_mirrors[i].lme_prefer == 0)
7845 /* no primary, use any in-sync */
7846 for (i = 0; i < lo->ldo_mirror_count; i++) {
7847 if (lo->ldo_mirrors[i].lme_stale)
7853 CERROR(DFID ": doesn't have a primary mirror\n",
7854 PFID(lod_object_fid(lo)));
7855 GOTO(out, rc = -ENODATA);
7859 CDEBUG(D_LAYOUT, DFID": found primary %u\n",
7860 PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
7862 LASSERT(!lo->ldo_mirrors[primary].lme_stale);
7864 /* for LAYOUT_WRITE opc, it has to do the following operations:
7865 * 1. stale overlapping componets from stale mirrors;
7866 * 2. instantiate components of the primary mirror;
7867 * 3. transfter layout version to all objects of the primary;
7869 * for LAYOUT_RESYNC opc, it will do:
7870 * 1. instantiate components of all stale mirrors;
7871 * 2. transfer layout version to all objects to close write era. */
7873 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7874 struct layout_intent *layout = mlc->mlc_intent;
7875 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7877 LASSERT(mlc->mlc_intent != NULL);
7879 extent = mlc->mlc_intent->li_extent;
7881 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
7882 PFID(lod_object_fid(lo)), PEXT(&extent));
7884 /* 1. Update extents of primary before staling */
7885 rc = lod_declare_update_extents(env, lo, &extent, th, primary,
7890 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
7892 * trunc transfers [0, size) in the intent extent, we'd
7893 * stale components overlapping [size, eof).
7895 extent.e_start = extent.e_end;
7896 extent.e_end = OBD_OBJECT_EOF;
7899 /* 2. stale overlapping components */
7900 rc = lod_stale_components(env, lo, primary, &extent, th);
7904 /* 3. find the components which need instantiating.
7905 * instantiate [0, mlc->mlc_intent->e_end) */
7907 /* restore truncate intent extent */
7908 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC)
7909 extent.e_end = extent.e_start;
7912 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7913 if (!lu_extent_is_overlapped(&extent,
7914 &lod_comp->llc_extent))
7917 if (!lod_is_instantiation_needed(lod_comp))
7920 CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
7921 primary, lod_comp_index(lo, lod_comp));
7922 info->lti_comp_idx[info->lti_count++] =
7923 lod_comp_index(lo, lod_comp);
7925 } else { /* MD_LAYOUT_RESYNC */
7926 if (mlc->mlc_mirror_id == 0) {
7928 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7929 if (!lod_comp_inited(lod_comp))
7932 extent.e_end = lod_comp->llc_extent.e_end;
7935 rc = lod_prepare_resync(env, lo, &extent);
7939 /* mirror write, try to init its all components */
7940 rc = lod_prepare_resync_mirror(env, lo,
7941 mlc->mlc_mirror_id);
7946 /* change the file state to SYNC_PENDING */
7947 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7950 rc = lod_declare_instantiate_components(env, lo, th, 0);
7954 /* 3. transfer layout version to OST objects.
7955 * transfer new layout version to OST objects so that stale writes
7956 * can be denied. It also ends an era of writing by setting
7957 * LU_LAYOUT_RESYNC. Normal client can never use this bit to
7958 * send write RPC; only resync RPCs could do it. */
7959 layout_attr->la_valid = LA_LAYOUT_VERSION;
7960 layout_attr->la_layout_version = 0; /* set current version */
7961 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7962 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7963 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7967 lod_obj_inc_layout_gen(lo);
7970 lod_striping_free(env, lo);
7974 static int lod_declare_update_sync_pending(const struct lu_env *env,
7975 struct lod_object *lo, struct md_layout_change *mlc,
7978 struct lod_thread_info *info = lod_env_info(env);
7979 struct lu_attr *layout_attr = &info->lti_layout_attr;
7980 unsigned sync_components = 0;
7981 unsigned resync_components = 0;
7986 LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
7987 LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
7988 mlc->mlc_opc == MD_LAYOUT_WRITE);
7990 CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
7991 PFID(lod_object_fid(lo)), mlc->mlc_opc);
7993 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7994 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
7995 PFID(lod_object_fid(lo)));
7997 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7998 return lod_declare_update_write_pending(env, lo, mlc, th);
8001 /* MD_LAYOUT_RESYNC_DONE */
8003 for (i = 0; i < lo->ldo_comp_cnt; i++) {
8004 struct lod_layout_component *lod_comp;
8007 lod_comp = &lo->ldo_comp_entries[i];
8009 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
8014 for (j = 0; j < mlc->mlc_resync_count; j++) {
8015 if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
8018 mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
8019 lod_comp->llc_flags &= ~LCME_FL_STALE;
8020 resync_components++;
8026 for (i = 0; i < mlc->mlc_resync_count; i++) {
8027 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
8030 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
8031 "or already synced\n", PFID(lod_object_fid(lo)),
8032 mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
8033 GOTO(out, rc = -EINVAL);
8036 if (!sync_components || (mlc->mlc_resync_count && !resync_components)) {
8037 CDEBUG(D_LAYOUT, DFID": no mirror in sync\n",
8038 PFID(lod_object_fid(lo)));
8040 /* tend to return an error code here to prevent
8041 * the MDT from setting SoM attribute */
8042 GOTO(out, rc = -EINVAL);
8045 CDEBUG(D_LAYOUT, DFID": synced %u resynced %u/%zu components\n",
8046 PFID(lod_object_fid(lo)),
8047 sync_components, resync_components, mlc->mlc_resync_count);
8049 lo->ldo_flr_state = LCM_FL_RDONLY;
8050 lod_obj_inc_layout_gen(lo);
8052 layout_attr->la_valid = LA_LAYOUT_VERSION;
8053 layout_attr->la_layout_version = 0; /* set current version */
8054 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
8058 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
8059 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
8060 &info->lti_buf, XATTR_NAME_LOV, 0, th);
8065 lod_striping_free(env, lo);
8069 typedef int (*mlc_handler)(const struct lu_env *env, struct dt_object *dt,
8070 const struct md_layout_change *mlc,
8071 struct thandle *th);
8074 * Attach stripes after target's for migrating directory. NB, we
8075 * only need to declare this, the actual work is done inside
8076 * lod_xattr_set_lmv().
8078 * \param[in] env execution environment
8079 * \param[in] dt target object
8080 * \param[in] mlc layout change data
8081 * \param[in] th transaction handle
8083 * \retval 0 on success
8084 * \retval negative if failed
8086 static int lod_dir_declare_layout_attach(const struct lu_env *env,
8087 struct dt_object *dt,
8088 const struct md_layout_change *mlc,
8091 struct lod_thread_info *info = lod_env_info(env);
8092 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
8093 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
8094 struct lod_object *lo = lod_dt_obj(dt);
8095 struct dt_object *next = dt_object_child(dt);
8096 struct dt_object_format *dof = &info->lti_format;
8097 struct lmv_mds_md_v1 *lmv = mlc->mlc_buf.lb_buf;
8098 struct dt_object **stripes;
8099 __u32 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
8100 struct lu_fid *fid = &info->lti_fid;
8101 struct lod_tgt_desc *tgt;
8102 struct dt_object *dto;
8103 struct dt_device *tgt_dt;
8104 int type = LU_SEQ_RANGE_ANY;
8105 struct dt_insert_rec *rec = &info->lti_dt_rec;
8106 char *stripe_name = info->lti_key;
8107 struct lu_name *sname;
8108 struct linkea_data ldata = { NULL };
8109 struct lu_buf linkea_buf;
8116 if (!lmv_is_sane(lmv))
8119 if (!dt_try_as_dir(env, dt))
8122 dof->dof_type = DFT_DIR;
8124 OBD_ALLOC_PTR_ARRAY(stripes, (lo->ldo_dir_stripe_count + stripe_count));
8128 for (i = 0; i < lo->ldo_dir_stripe_count; i++)
8129 stripes[i] = lo->ldo_stripe[i];
8131 rec->rec_type = S_IFDIR;
8133 for (i = 0; i < stripe_count; i++) {
8135 &lmv->lmv_stripe_fids[i]);
8136 if (!fid_is_sane(fid))
8139 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
8143 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
8144 tgt_dt = lod->lod_child;
8146 tgt = LTD_TGT(ltd, idx);
8148 GOTO(out, rc = -ESTALE);
8149 tgt_dt = tgt->ltd_tgt;
8152 dto = dt_locate_at(env, tgt_dt, fid,
8153 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
8156 GOTO(out, rc = PTR_ERR(dto));
8158 stripes[i + lo->ldo_dir_stripe_count] = dto;
8160 if (!dt_try_as_dir(env, dto))
8161 GOTO(out, rc = -ENOTDIR);
8163 rc = lod_sub_declare_ref_add(env, dto, th);
8167 rec->rec_fid = lu_object_fid(&dto->do_lu);
8168 rc = lod_sub_declare_insert(env, dto,
8169 (const struct dt_rec *)rec,
8170 (const struct dt_key *)dot, th);
8174 rc = lod_sub_declare_insert(env, dto,
8175 (const struct dt_rec *)rec,
8176 (const struct dt_key *)dotdot, th);
8180 rc = lod_sub_declare_xattr_set(env, dto, &mlc->mlc_buf,
8181 XATTR_NAME_LMV, 0, th);
8185 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
8186 PFID(lu_object_fid(&dto->do_lu)),
8187 i + lo->ldo_dir_stripe_count);
8189 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
8190 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
8191 sname, lu_object_fid(&dt->do_lu));
8195 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
8196 linkea_buf.lb_len = ldata.ld_leh->leh_len;
8197 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
8198 XATTR_NAME_LINK, 0, th);
8202 rc = lod_sub_declare_insert(env, next,
8203 (const struct dt_rec *)rec,
8204 (const struct dt_key *)stripe_name,
8209 rc = lod_sub_declare_ref_add(env, next, th);
8215 OBD_FREE_PTR_ARRAY(lo->ldo_stripe,
8216 lo->ldo_dir_stripes_allocated);
8217 lo->ldo_stripe = stripes;
8218 lo->ldo_dir_migrate_offset = lo->ldo_dir_stripe_count;
8219 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_hash_type);
8220 lo->ldo_dir_stripe_count += stripe_count;
8221 lo->ldo_dir_stripes_allocated += stripe_count;
8223 /* plain directory split creates target as a plain directory, while
8224 * after source attached as the first stripe, it becomes a striped
8225 * directory, set correct do_index_ops, otherwise it can't be unlinked.
8227 dt->do_index_ops = &lod_striped_index_ops;
8231 i = lo->ldo_dir_stripe_count;
8232 while (i < lo->ldo_dir_stripe_count + stripe_count && stripes[i])
8233 dt_object_put(env, stripes[i++]);
8235 OBD_FREE_PTR_ARRAY(stripes, stripe_count + lo->ldo_dir_stripe_count);
8239 static int lod_dir_declare_layout_detach(const struct lu_env *env,
8240 struct dt_object *dt,
8241 const struct md_layout_change *unused,
8244 struct lod_thread_info *info = lod_env_info(env);
8245 struct lod_object *lo = lod_dt_obj(dt);
8246 struct dt_object *next = dt_object_child(dt);
8247 char *stripe_name = info->lti_key;
8248 struct dt_object *dto;
8252 if (!dt_try_as_dir(env, dt))
8255 if (!lo->ldo_dir_stripe_count)
8256 return lod_sub_declare_delete(env, next,
8257 (const struct dt_key *)dotdot, th);
8259 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8260 dto = lo->ldo_stripe[i];
8264 if (!dt_try_as_dir(env, dto))
8267 rc = lod_sub_declare_delete(env, dto,
8268 (const struct dt_key *)dotdot, th);
8272 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8273 PFID(lu_object_fid(&dto->do_lu)), i);
8275 rc = lod_sub_declare_delete(env, next,
8276 (const struct dt_key *)stripe_name, th);
8280 rc = lod_sub_declare_ref_del(env, next, th);
8288 static int dt_dir_is_empty(const struct lu_env *env,
8289 struct dt_object *obj)
8292 const struct dt_it_ops *iops;
8297 if (!dt_try_as_dir(env, obj))
8300 iops = &obj->do_index_ops->dio_it;
8301 it = iops->init(env, obj, LUDA_64BITHASH);
8303 RETURN(PTR_ERR(it));
8305 rc = iops->get(env, it, (const struct dt_key *)"");
8309 for (rc = 0, i = 0; rc == 0 && i < 3; ++i)
8310 rc = iops->next(env, it);
8316 /* Huh? Index contains no zero key? */
8321 iops->fini(env, it);
8326 static int lod_dir_declare_layout_shrink(const struct lu_env *env,
8327 struct dt_object *dt,
8328 const struct md_layout_change *mlc,
8331 struct lod_thread_info *info = lod_env_info(env);
8332 struct lod_object *lo = lod_dt_obj(dt);
8333 struct dt_object *next = dt_object_child(dt);
8334 struct lmv_user_md *lmu = mlc->mlc_buf.lb_buf;
8335 char *stripe_name = info->lti_key;
8336 struct lu_buf *lmv_buf = &info->lti_buf;
8337 __u32 final_stripe_count;
8338 struct dt_object *dto;
8344 if (!dt_try_as_dir(env, dt))
8347 /* shouldn't be called on plain directory */
8348 LASSERT(lo->ldo_dir_stripe_count);
8350 lmv_buf->lb_buf = &info->lti_lmv.lmv_md_v1;
8351 lmv_buf->lb_len = sizeof(info->lti_lmv.lmv_md_v1);
8353 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
8354 LASSERT(final_stripe_count &&
8355 final_stripe_count < lo->ldo_dir_stripe_count);
8357 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8358 dto = lo->ldo_stripe[i];
8362 if (i < final_stripe_count) {
8363 rc = lod_sub_declare_xattr_set(env, dto, lmv_buf,
8365 LU_XATTR_REPLACE, th);
8372 rc = dt_dir_is_empty(env, dto);
8376 rc = lod_sub_declare_ref_del(env, dto, th);
8380 rc = lod_sub_declare_destroy(env, dto, th);
8384 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8385 PFID(lu_object_fid(&dto->do_lu)), i);
8387 rc = lod_sub_declare_delete(env, next,
8388 (const struct dt_key *)stripe_name, th);
8392 rc = lod_sub_declare_ref_del(env, next, th);
8397 rc = lod_sub_declare_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV,
8398 LU_XATTR_REPLACE, th);
8403 * Allocate stripes for split directory.
8405 * \param[in] env execution environment
8406 * \param[in] dt target object
8407 * \param[in] mlc layout change data
8408 * \param[in] th transaction handle
8410 * \retval 0 on success
8411 * \retval negative if failed
8413 static int lod_dir_declare_layout_split(const struct lu_env *env,
8414 struct dt_object *dt,
8415 const struct md_layout_change *mlc,
8418 struct lod_thread_info *info = lod_env_info(env);
8419 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
8420 struct lod_object *lo = lod_dt_obj(dt);
8421 struct dt_object_format *dof = &info->lti_format;
8422 struct lmv_user_md_v1 *lum = mlc->mlc_spec->u.sp_ea.eadata;
8423 struct dt_object **stripes;
8431 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
8432 LASSERT(le32_to_cpu(lum->lum_stripe_offset) == LMV_OFFSET_DEFAULT);
8434 saved_count = lo->ldo_dir_stripes_allocated;
8435 stripe_count = le32_to_cpu(lum->lum_stripe_count);
8436 if (stripe_count <= saved_count)
8439 dof->dof_type = DFT_DIR;
8441 OBD_ALLOC(stripes, sizeof(*stripes) * stripe_count);
8445 for (i = 0; i < lo->ldo_dir_stripes_allocated; i++)
8446 stripes[i] = lo->ldo_stripe[i];
8448 lod_qos_statfs_update(env, lod, &lod->lod_mdt_descs);
8449 rc = lod_mdt_alloc_qos(env, lo, stripes, saved_count, stripe_count);
8451 rc = lod_mdt_alloc_rr(env, lo, stripes, saved_count,
8454 OBD_FREE(stripes, sizeof(*stripes) * stripe_count);
8458 LASSERT(rc > saved_count);
8459 OBD_FREE(lo->ldo_stripe,
8460 sizeof(*stripes) * lo->ldo_dir_stripes_allocated);
8461 lo->ldo_stripe = stripes;
8462 lo->ldo_dir_striped = 1;
8463 lo->ldo_dir_stripe_count = rc;
8464 lo->ldo_dir_stripes_allocated = stripe_count;
8465 lo->ldo_dir_split_hash = lo->ldo_dir_hash_type;
8466 lo->ldo_dir_hash_type = le32_to_cpu(lum->lum_hash_type);
8467 if (!lmv_is_known_hash_type(lo->ldo_dir_hash_type))
8468 lo->ldo_dir_hash_type =
8469 lod->lod_mdt_descs.ltd_lmv_desc.ld_pattern;
8470 lo->ldo_dir_hash_type |= LMV_HASH_FLAG_SPLIT | LMV_HASH_FLAG_MIGRATION;
8471 lo->ldo_dir_split_offset = saved_count;
8472 lo->ldo_dir_layout_version++;
8473 lo->ldo_dir_stripe_loaded = 1;
8475 rc = lod_dir_declare_create_stripes(env, dt, mlc->mlc_attr, dof, th);
8477 lod_striping_free(env, lo);
8483 * detach all stripes from dir master object, NB, stripes are not destroyed, but
8484 * deleted from it's parent namespace, this function is called in two places:
8485 * 1. mdd_migrate_mdt() detach stripes from source, and attach them to
8487 * 2. mdd_dir_layout_update() detach stripe before turning 1-stripe directory to
8488 * a plain directory.
8490 * \param[in] env execution environment
8491 * \param[in] dt target object
8492 * \param[in] mlc layout change data
8493 * \param[in] th transaction handle
8495 * \retval 0 on success
8496 * \retval negative if failed
8498 static int lod_dir_layout_detach(const struct lu_env *env,
8499 struct dt_object *dt,
8500 const struct md_layout_change *mlc,
8503 struct lod_thread_info *info = lod_env_info(env);
8504 struct lod_object *lo = lod_dt_obj(dt);
8505 struct dt_object *next = dt_object_child(dt);
8506 char *stripe_name = info->lti_key;
8507 struct dt_object *dto;
8513 if (!lo->ldo_dir_stripe_count) {
8514 /* plain directory delete .. */
8515 rc = lod_sub_delete(env, next,
8516 (const struct dt_key *)dotdot, th);
8520 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8521 dto = lo->ldo_stripe[i];
8525 rc = lod_sub_delete(env, dto,
8526 (const struct dt_key *)dotdot, th);
8530 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8531 PFID(lu_object_fid(&dto->do_lu)), i);
8533 rc = lod_sub_delete(env, next,
8534 (const struct dt_key *)stripe_name, th);
8538 rc = lod_sub_ref_del(env, next, th);
8543 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8544 dto = lo->ldo_stripe[i];
8546 dt_object_put(env, dto);
8548 OBD_FREE_PTR_ARRAY(lo->ldo_stripe, lo->ldo_dir_stripes_allocated);
8549 lo->ldo_stripe = NULL;
8550 lo->ldo_dir_stripes_allocated = 0;
8551 lo->ldo_dir_stripe_count = 0;
8552 dt->do_index_ops = &lod_index_ops;
8557 static int lod_dir_layout_shrink(const struct lu_env *env,
8558 struct dt_object *dt,
8559 const struct md_layout_change *mlc,
8562 struct lod_thread_info *info = lod_env_info(env);
8563 struct lod_object *lo = lod_dt_obj(dt);
8564 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
8565 struct dt_object *next = dt_object_child(dt);
8566 struct lmv_user_md *lmu = mlc->mlc_buf.lb_buf;
8567 __u32 final_stripe_count;
8568 char *stripe_name = info->lti_key;
8569 struct dt_object *dto;
8570 struct lu_buf *lmv_buf = &info->lti_buf;
8571 struct lmv_mds_md_v1 *lmv = &info->lti_lmv.lmv_md_v1;
8573 int type = LU_SEQ_RANGE_ANY;
8579 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
8581 lmv_buf->lb_buf = lmv;
8582 lmv_buf->lb_len = sizeof(*lmv);
8583 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
8584 lmv->lmv_stripe_count = cpu_to_le32(final_stripe_count);
8585 lmv->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type) &
8586 cpu_to_le32(LMV_HASH_TYPE_MASK |
8587 LMV_HASH_FLAG_FIXED);
8588 lmv->lmv_layout_version =
8589 cpu_to_le32(lo->ldo_dir_layout_version + 1);
8590 lmv->lmv_migrate_offset = 0;
8591 lmv->lmv_migrate_hash = 0;
8593 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8594 dto = lo->ldo_stripe[i];
8598 if (i < final_stripe_count) {
8599 rc = lod_fld_lookup(env, lod,
8600 lu_object_fid(&dto->do_lu),
8605 lmv->lmv_master_mdt_index = cpu_to_le32(mdtidx);
8606 rc = lod_sub_xattr_set(env, dto, lmv_buf,
8608 LU_XATTR_REPLACE, th);
8615 dt_write_lock(env, dto, DT_TGT_CHILD);
8616 rc = lod_sub_ref_del(env, dto, th);
8617 dt_write_unlock(env, dto);
8621 rc = lod_sub_destroy(env, dto, th);
8625 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8626 PFID(lu_object_fid(&dto->do_lu)), i);
8628 rc = lod_sub_delete(env, next,
8629 (const struct dt_key *)stripe_name, th);
8633 rc = lod_sub_ref_del(env, next, th);
8638 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &mdtidx,
8643 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
8644 lmv->lmv_master_mdt_index = cpu_to_le32(mdtidx);
8645 rc = lod_sub_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV,
8646 LU_XATTR_REPLACE, th);
8650 for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
8651 dto = lo->ldo_stripe[i];
8653 dt_object_put(env, dto);
8655 lo->ldo_dir_stripe_count = final_stripe_count;
8660 static mlc_handler dir_mlc_declare_ops[MD_LAYOUT_MAX] = {
8661 [MD_LAYOUT_ATTACH] = lod_dir_declare_layout_attach,
8662 [MD_LAYOUT_DETACH] = lod_dir_declare_layout_detach,
8663 [MD_LAYOUT_SHRINK] = lod_dir_declare_layout_shrink,
8664 [MD_LAYOUT_SPLIT] = lod_dir_declare_layout_split,
8667 static mlc_handler dir_mlc_ops[MD_LAYOUT_MAX] = {
8668 [MD_LAYOUT_DETACH] = lod_dir_layout_detach,
8669 [MD_LAYOUT_SHRINK] = lod_dir_layout_shrink,
8672 static int lod_declare_layout_change(const struct lu_env *env,
8673 struct dt_object *dt, struct md_layout_change *mlc,
8676 struct lod_thread_info *info = lod_env_info(env);
8677 struct lod_object *lo = lod_dt_obj(dt);
8682 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
8683 LASSERT(dir_mlc_declare_ops[mlc->mlc_opc]);
8684 rc = dir_mlc_declare_ops[mlc->mlc_opc](env, dt, mlc, th);
8688 if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
8689 dt_object_remote(dt_object_child(dt)))
8692 rc = lod_striping_load(env, lo);
8696 LASSERT(lo->ldo_comp_cnt > 0);
8698 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
8702 switch (lo->ldo_flr_state) {
8704 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
8708 rc = lod_declare_update_rdonly(env, lo, mlc, th);
8710 case LCM_FL_WRITE_PENDING:
8711 rc = lod_declare_update_write_pending(env, lo, mlc, th);
8713 case LCM_FL_SYNC_PENDING:
8714 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
8725 * Instantiate layout component objects which covers the intent write offset.
8727 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
8728 struct md_layout_change *mlc, struct thandle *th)
8730 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
8731 struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
8732 struct lod_object *lo = lod_dt_obj(dt);
8737 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
8738 LASSERT(dir_mlc_ops[mlc->mlc_opc]);
8739 rc = dir_mlc_ops[mlc->mlc_opc](env, dt, mlc, th);
8743 rc = lod_striped_create(env, dt, attr, NULL, th);
8744 if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
8745 layout_attr->la_layout_version |= lo->ldo_layout_gen;
8746 rc = lod_attr_set(env, dt, layout_attr, th);
8752 const struct dt_object_operations lod_obj_ops = {
8753 .do_read_lock = lod_read_lock,
8754 .do_write_lock = lod_write_lock,
8755 .do_read_unlock = lod_read_unlock,
8756 .do_write_unlock = lod_write_unlock,
8757 .do_write_locked = lod_write_locked,
8758 .do_attr_get = lod_attr_get,
8759 .do_declare_attr_set = lod_declare_attr_set,
8760 .do_attr_set = lod_attr_set,
8761 .do_xattr_get = lod_xattr_get,
8762 .do_declare_xattr_set = lod_declare_xattr_set,
8763 .do_xattr_set = lod_xattr_set,
8764 .do_declare_xattr_del = lod_declare_xattr_del,
8765 .do_xattr_del = lod_xattr_del,
8766 .do_xattr_list = lod_xattr_list,
8767 .do_ah_init = lod_ah_init,
8768 .do_declare_create = lod_declare_create,
8769 .do_create = lod_create,
8770 .do_declare_destroy = lod_declare_destroy,
8771 .do_destroy = lod_destroy,
8772 .do_index_try = lod_index_try,
8773 .do_declare_ref_add = lod_declare_ref_add,
8774 .do_ref_add = lod_ref_add,
8775 .do_declare_ref_del = lod_declare_ref_del,
8776 .do_ref_del = lod_ref_del,
8777 .do_object_sync = lod_object_sync,
8778 .do_object_lock = lod_object_lock,
8779 .do_object_unlock = lod_object_unlock,
8780 .do_invalidate = lod_invalidate,
8781 .do_declare_layout_change = lod_declare_layout_change,
8782 .do_layout_change = lod_layout_change,
8786 * Implementation of dt_body_operations::dbo_read.
8788 * \see dt_body_operations::dbo_read() in the API description for details.
8790 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
8791 struct lu_buf *buf, loff_t *pos)
8793 struct dt_object *next = dt_object_child(dt);
8795 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
8796 S_ISLNK(dt->do_lu.lo_header->loh_attr));
8797 return next->do_body_ops->dbo_read(env, next, buf, pos);
8801 * Implementation of dt_body_operations::dbo_declare_write.
8803 * \see dt_body_operations::dbo_declare_write() in the API description
8806 static ssize_t lod_declare_write(const struct lu_env *env,
8807 struct dt_object *dt,
8808 const struct lu_buf *buf, loff_t pos,
8811 return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
8815 * Implementation of dt_body_operations::dbo_write.
8817 * \see dt_body_operations::dbo_write() in the API description for details.
8819 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
8820 const struct lu_buf *buf, loff_t *pos,
8823 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
8824 S_ISLNK(dt->do_lu.lo_header->loh_attr));
8825 return lod_sub_write(env, dt_object_child(dt), buf, pos, th);
8828 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
8829 __u64 start, __u64 end, struct thandle *th)
8831 if (dt_object_remote(dt))
8834 return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
8837 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
8838 __u64 start, __u64 end, struct thandle *th)
8840 if (dt_object_remote(dt))
8843 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
8844 return lod_sub_punch(env, dt_object_child(dt), start, end, th);
8848 * different type of files use the same body_ops because object may be created
8849 * in OUT, where there is no chance to set correct body_ops for each type, so
8850 * body_ops themselves will check file type inside, see lod_read/write/punch for
8853 static const struct dt_body_operations lod_body_ops = {
8854 .dbo_read = lod_read,
8855 .dbo_declare_write = lod_declare_write,
8856 .dbo_write = lod_write,
8857 .dbo_declare_punch = lod_declare_punch,
8858 .dbo_punch = lod_punch,
8862 * Implementation of lu_object_operations::loo_object_init.
8864 * The function determines the type and the index of the target device using
8865 * sequence of the object's FID. Then passes control down to the
8866 * corresponding device:
8867 * OSD for the local objects, OSP for remote
8869 * \see lu_object_operations::loo_object_init() in the API description
8872 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
8873 const struct lu_object_conf *conf)
8875 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
8876 struct lu_device *cdev = NULL;
8877 struct lu_object *cobj;
8878 struct lod_tgt_descs *ltd = NULL;
8879 struct lod_tgt_desc *tgt;
8881 int type = LU_SEQ_RANGE_ANY;
8885 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
8889 if (type == LU_SEQ_RANGE_MDT &&
8890 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
8891 cdev = &lod->lod_child->dd_lu_dev;
8892 } else if (type == LU_SEQ_RANGE_MDT) {
8893 ltd = &lod->lod_mdt_descs;
8895 } else if (type == LU_SEQ_RANGE_OST) {
8896 ltd = &lod->lod_ost_descs;
8903 if (ltd->ltd_tgts_size > idx &&
8904 test_bit(idx, ltd->ltd_tgt_bitmap)) {
8905 tgt = LTD_TGT(ltd, idx);
8907 LASSERT(tgt != NULL);
8908 LASSERT(tgt->ltd_tgt != NULL);
8910 cdev = &(tgt->ltd_tgt->dd_lu_dev);
8912 lod_putref(lod, ltd);
8915 if (unlikely(cdev == NULL))
8918 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
8919 if (unlikely(cobj == NULL))
8922 lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
8924 lu_object_add(lo, cobj);
8931 * Alloc cached foreign LOV
8933 * \param[in] lo object
8934 * \param[in] size size of foreign LOV
8936 * \retval 0 on success
8937 * \retval negative if failed
8939 int lod_alloc_foreign_lov(struct lod_object *lo, size_t size)
8941 OBD_ALLOC_LARGE(lo->ldo_foreign_lov, size);
8942 if (lo->ldo_foreign_lov == NULL)
8944 lo->ldo_foreign_lov_size = size;
8945 lo->ldo_is_foreign = 1;
8951 * Free cached foreign LOV
8953 * \param[in] lo object
8955 void lod_free_foreign_lov(struct lod_object *lo)
8957 if (lo->ldo_foreign_lov != NULL)
8958 OBD_FREE_LARGE(lo->ldo_foreign_lov, lo->ldo_foreign_lov_size);
8959 lo->ldo_foreign_lov = NULL;
8960 lo->ldo_foreign_lov_size = 0;
8961 lo->ldo_is_foreign = 0;
8966 * Free cached foreign LMV
8968 * \param[in] lo object
8970 void lod_free_foreign_lmv(struct lod_object *lo)
8972 if (lo->ldo_foreign_lmv != NULL)
8973 OBD_FREE_LARGE(lo->ldo_foreign_lmv, lo->ldo_foreign_lmv_size);
8974 lo->ldo_foreign_lmv = NULL;
8975 lo->ldo_foreign_lmv_size = 0;
8976 lo->ldo_dir_is_foreign = 0;
8981 * Release resources associated with striping.
8983 * If the object is striped (regular or directory), then release
8984 * the stripe objects references and free the ldo_stripe array.
8986 * \param[in] env execution environment
8987 * \param[in] lo object
8989 void lod_striping_free_nolock(const struct lu_env *env, struct lod_object *lo)
8991 struct lod_layout_component *lod_comp;
8994 if (unlikely(lo->ldo_is_foreign)) {
8995 lod_free_foreign_lov(lo);
8996 lo->ldo_comp_cached = 0;
8997 } else if (unlikely(lo->ldo_dir_is_foreign)) {
8998 lod_free_foreign_lmv(lo);
8999 lo->ldo_dir_stripe_loaded = 0;
9000 } else if (lo->ldo_stripe != NULL) {
9001 LASSERT(lo->ldo_comp_entries == NULL);
9002 LASSERT(lo->ldo_dir_stripes_allocated > 0);
9004 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
9005 if (lo->ldo_stripe[i])
9006 dt_object_put(env, lo->ldo_stripe[i]);
9009 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
9010 OBD_FREE(lo->ldo_stripe, j);
9011 lo->ldo_stripe = NULL;
9012 lo->ldo_dir_stripes_allocated = 0;
9013 lo->ldo_dir_stripe_loaded = 0;
9014 lo->ldo_dir_stripe_count = 0;
9015 } else if (lo->ldo_comp_entries != NULL) {
9016 for (i = 0; i < lo->ldo_comp_cnt; i++) {
9017 /* free lod_layout_component::llc_stripe array */
9018 lod_comp = &lo->ldo_comp_entries[i];
9020 if (lod_comp->llc_stripe == NULL)
9022 LASSERT(lod_comp->llc_stripes_allocated != 0);
9023 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
9024 if (lod_comp->llc_stripe[j] != NULL)
9026 &lod_comp->llc_stripe[j]->do_lu);
9028 OBD_FREE_PTR_ARRAY(lod_comp->llc_stripe,
9029 lod_comp->llc_stripes_allocated);
9030 lod_comp->llc_stripe = NULL;
9031 OBD_FREE_PTR_ARRAY(lod_comp->llc_ost_indices,
9032 lod_comp->llc_stripes_allocated);
9033 lod_comp->llc_ost_indices = NULL;
9034 lod_comp->llc_stripes_allocated = 0;
9036 lod_free_comp_entries(lo);
9037 lo->ldo_comp_cached = 0;
9041 void lod_striping_free(const struct lu_env *env, struct lod_object *lo)
9043 mutex_lock(&lo->ldo_layout_mutex);
9044 lod_striping_free_nolock(env, lo);
9045 mutex_unlock(&lo->ldo_layout_mutex);
9049 * Implementation of lu_object_operations::loo_object_free.
9051 * \see lu_object_operations::loo_object_free() in the API description
9054 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
9056 struct lod_object *lo = lu2lod_obj(o);
9058 /* release all underlying object pinned */
9059 lod_striping_free(env, lo);
9061 /* lo doesn't contain a lu_object_header, so we don't need call_rcu */
9062 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
9066 * Implementation of lu_object_operations::loo_object_release.
9068 * \see lu_object_operations::loo_object_release() in the API description
9071 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
9073 /* XXX: shouldn't we release everything here in case if object
9074 * creation failed before? */
9078 * Implementation of lu_object_operations::loo_object_print.
9080 * \see lu_object_operations::loo_object_print() in the API description
9083 static int lod_object_print(const struct lu_env *env, void *cookie,
9084 lu_printer_t p, const struct lu_object *l)
9086 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
9088 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
9091 const struct lu_object_operations lod_lu_obj_ops = {
9092 .loo_object_init = lod_object_init,
9093 .loo_object_free = lod_object_free,
9094 .loo_object_release = lod_object_release,
9095 .loo_object_print = lod_object_print,