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, false);
2664 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2666 int magic, size = 0, i;
2667 struct lod_layout_component *comp_entries;
2669 bool is_composite, is_foreign = false;
2672 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2673 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2675 lo->ldo_def_striping->lds_def_striping_is_composite;
2677 comp_cnt = lo->ldo_comp_cnt;
2678 comp_entries = lo->ldo_comp_entries;
2679 is_composite = lo->ldo_is_composite;
2680 is_foreign = lo->ldo_is_foreign;
2684 return lo->ldo_foreign_lov_size;
2686 LASSERT(comp_cnt != 0 && comp_entries != NULL);
2688 size = sizeof(struct lov_comp_md_v1) +
2689 sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2690 LASSERT(size % sizeof(__u64) == 0);
2693 for (i = 0; i < comp_cnt; i++) {
2696 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2697 stripe_count = lod_comp_entry_stripe_count(lo, i, is_dir);
2698 if (!is_dir && is_composite)
2699 lod_comp_shrink_stripe_count(&comp_entries[i],
2702 size += lov_user_md_size(stripe_count, magic);
2703 LASSERT(size % sizeof(__u64) == 0);
2709 * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2710 * the xattr value is binary lov_comp_md_v1 which contains component(s)
2713 * \param[in] env execution environment
2714 * \param[in] dt dt_object to add components on
2715 * \param[in] buf buffer contains components to be added
2716 * \parem[in] th thandle
2718 * \retval 0 on success
2719 * \retval negative errno on failure
2721 static int lod_declare_layout_add(const struct lu_env *env,
2722 struct dt_object *dt,
2723 const struct lu_buf *buf,
2726 struct lod_thread_info *info = lod_env_info(env);
2727 struct lod_layout_component *comp_array, *lod_comp, *old_array;
2728 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2729 struct dt_object *next = dt_object_child(dt);
2730 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
2731 struct lod_object *lo = lod_dt_obj(dt);
2732 struct lov_user_md_v3 *v3;
2733 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2735 int i, rc, array_cnt, old_array_cnt;
2738 LASSERT(lo->ldo_is_composite);
2740 if (lo->ldo_flr_state != LCM_FL_NONE)
2743 rc = lod_verify_striping(env, d, lo, buf, false);
2747 magic = comp_v1->lcm_magic;
2748 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2749 lustre_swab_lov_comp_md_v1(comp_v1);
2750 magic = comp_v1->lcm_magic;
2753 if (magic != LOV_USER_MAGIC_COMP_V1)
2756 mutex_lock(&lo->ldo_layout_mutex);
2758 array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2759 OBD_ALLOC_PTR_ARRAY(comp_array, array_cnt);
2760 if (comp_array == NULL) {
2761 mutex_unlock(&lo->ldo_layout_mutex);
2766 memcpy(comp_array, lo->ldo_comp_entries,
2767 sizeof(*comp_array) * lo->ldo_comp_cnt);
2769 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2770 struct lov_user_md_v1 *v1;
2771 struct lu_extent *ext;
2773 v1 = (struct lov_user_md *)((char *)comp_v1 +
2774 comp_v1->lcm_entries[i].lcme_offset);
2775 ext = &comp_v1->lcm_entries[i].lcme_extent;
2777 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2778 lod_comp->llc_extent.e_start = ext->e_start;
2779 lod_comp->llc_extent.e_end = ext->e_end;
2780 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2781 lod_comp->llc_flags = comp_v1->lcm_entries[i].lcme_flags;
2783 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2784 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2785 lod_adjust_stripe_info(lod_comp, desc, 0);
2787 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2788 v3 = (struct lov_user_md_v3 *) v1;
2789 if (v3->lmm_pool_name[0] != '\0') {
2790 rc = lod_set_pool(&lod_comp->llc_pool,
2798 old_array = lo->ldo_comp_entries;
2799 old_array_cnt = lo->ldo_comp_cnt;
2801 lo->ldo_comp_entries = comp_array;
2802 lo->ldo_comp_cnt = array_cnt;
2804 /* No need to increase layout generation here, it will be increased
2805 * later when generating component ID for the new components */
2807 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2808 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2809 XATTR_NAME_LOV, 0, th);
2811 lo->ldo_comp_entries = old_array;
2812 lo->ldo_comp_cnt = old_array_cnt;
2816 OBD_FREE_PTR_ARRAY(old_array, old_array_cnt);
2818 LASSERT(lo->ldo_mirror_count == 1);
2819 lo->ldo_mirrors[0].lme_end = array_cnt - 1;
2821 mutex_unlock(&lo->ldo_layout_mutex);
2826 for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2827 lod_comp = &comp_array[i];
2828 if (lod_comp->llc_pool != NULL) {
2829 OBD_FREE(lod_comp->llc_pool,
2830 strlen(lod_comp->llc_pool) + 1);
2831 lod_comp->llc_pool = NULL;
2834 OBD_FREE_PTR_ARRAY(comp_array, array_cnt);
2835 mutex_unlock(&lo->ldo_layout_mutex);
2841 * lod_last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2842 * @mirror_id: Mirror id to be checked.
2845 * This function checks if a mirror with specified @mirror_id is the last
2846 * non-stale mirror of a LOD object @lo.
2848 * Return: true or false.
2851 bool lod_last_non_stale_mirror(__u16 mirror_id, struct lod_object *lo)
2853 struct lod_layout_component *lod_comp;
2854 bool has_stale_flag;
2857 for (i = 0; i < lo->ldo_mirror_count; i++) {
2858 if (lo->ldo_mirrors[i].lme_id == mirror_id ||
2859 lo->ldo_mirrors[i].lme_stale)
2862 has_stale_flag = false;
2863 lod_foreach_mirror_comp(lod_comp, lo, i) {
2864 if (lod_comp->llc_flags & LCME_FL_STALE) {
2865 has_stale_flag = true;
2869 if (!has_stale_flag)
2877 * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2878 * the '$field' can only be 'flags' now. The xattr value is binary
2879 * lov_comp_md_v1 which contains the component ID(s) and the value of
2880 * the field to be modified.
2881 * Please update allowed_lustre_lov macro if $field groks more values
2884 * \param[in] env execution environment
2885 * \param[in] dt dt_object to be modified
2886 * \param[in] op operation string, like "set.flags"
2887 * \param[in] buf buffer contains components to be set
2888 * \parem[in] th thandle
2890 * \retval 0 on success
2891 * \retval negative errno on failure
2893 static int lod_declare_layout_set(const struct lu_env *env,
2894 struct dt_object *dt,
2895 char *op, const struct lu_buf *buf,
2898 struct lod_layout_component *lod_comp;
2899 struct lod_thread_info *info = lod_env_info(env);
2900 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2901 struct lod_object *lo = lod_dt_obj(dt);
2902 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2905 bool changed = false;
2908 /* Please update allowed_lustre_lov macro if op
2909 * groks more values in the future
2911 if (strcmp(op, "set.flags") != 0) {
2912 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2913 lod2obd(d)->obd_name, op);
2917 magic = comp_v1->lcm_magic;
2918 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2919 lustre_swab_lov_comp_md_v1(comp_v1);
2920 magic = comp_v1->lcm_magic;
2923 if (magic != LOV_USER_MAGIC_COMP_V1)
2926 if (comp_v1->lcm_entry_count == 0) {
2927 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2928 lod2obd(d)->obd_name);
2932 mutex_lock(&lo->ldo_layout_mutex);
2933 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2934 __u32 id = comp_v1->lcm_entries[i].lcme_id;
2935 __u32 flags = comp_v1->lcm_entries[i].lcme_flags;
2936 __u32 mirror_flag = flags & LCME_MIRROR_FLAGS;
2937 __u16 mirror_id = mirror_id_of(id);
2938 bool neg = flags & LCME_FL_NEG;
2940 if (flags & LCME_FL_INIT) {
2942 lod_striping_free_nolock(env, lo);
2943 mutex_unlock(&lo->ldo_layout_mutex);
2947 flags &= ~(LCME_MIRROR_FLAGS | LCME_FL_NEG);
2948 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2949 lod_comp = &lo->ldo_comp_entries[j];
2951 /* lfs only put one flag in each entry */
2952 if ((flags && id != lod_comp->llc_id) ||
2953 (mirror_flag && mirror_id !=
2954 mirror_id_of(lod_comp->llc_id)))
2959 lod_comp->llc_flags &= ~flags;
2961 lod_comp->llc_flags &= ~mirror_flag;
2964 if ((flags & LCME_FL_STALE) &&
2965 lod_last_non_stale_mirror(mirror_id,
2968 &lo->ldo_layout_mutex);
2971 lod_comp->llc_flags |= flags;
2974 lod_comp->llc_flags |= mirror_flag;
2975 if (mirror_flag & LCME_FL_NOSYNC)
2976 lod_comp->llc_timestamp =
2977 ktime_get_real_seconds();
2983 mutex_unlock(&lo->ldo_layout_mutex);
2986 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2987 lod2obd(d)->obd_name);
2991 lod_obj_inc_layout_gen(lo);
2993 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2994 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), &info->lti_buf,
2995 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3000 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
3001 * and the xattr value is a unique component ID or a special lcme_id.
3003 * \param[in] env execution environment
3004 * \param[in] dt dt_object to be operated on
3005 * \param[in] buf buffer contains component ID or lcme_id
3006 * \parem[in] th thandle
3008 * \retval 0 on success
3009 * \retval negative errno on failure
3011 static int lod_declare_layout_del(const struct lu_env *env,
3012 struct dt_object *dt,
3013 const struct lu_buf *buf,
3016 struct lod_thread_info *info = lod_env_info(env);
3017 struct dt_object *next = dt_object_child(dt);
3018 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3019 struct lod_object *lo = lod_dt_obj(dt);
3020 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3021 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
3022 __u32 magic, id, flags, neg_flags = 0;
3026 LASSERT(lo->ldo_is_composite);
3028 if (lo->ldo_flr_state != LCM_FL_NONE)
3031 magic = comp_v1->lcm_magic;
3032 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3033 lustre_swab_lov_comp_md_v1(comp_v1);
3034 magic = comp_v1->lcm_magic;
3037 if (magic != LOV_USER_MAGIC_COMP_V1)
3040 id = comp_v1->lcm_entries[0].lcme_id;
3041 flags = comp_v1->lcm_entries[0].lcme_flags;
3043 if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
3044 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
3045 lod2obd(d)->obd_name, id, flags);
3049 if (id != LCME_ID_INVAL && flags != 0) {
3050 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
3051 lod2obd(d)->obd_name);
3055 if (id == LCME_ID_INVAL && !flags) {
3056 CDEBUG(D_LAYOUT, "%s: no id or flags specified.\n",
3057 lod2obd(d)->obd_name);
3061 if (flags & LCME_FL_NEG) {
3062 neg_flags = flags & ~LCME_FL_NEG;
3066 mutex_lock(&lo->ldo_layout_mutex);
3068 left = lo->ldo_comp_cnt;
3070 mutex_unlock(&lo->ldo_layout_mutex);
3074 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3075 struct lod_layout_component *lod_comp;
3077 lod_comp = &lo->ldo_comp_entries[i];
3079 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
3081 else if (flags && !(flags & lod_comp->llc_flags))
3083 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
3086 if (left != (i + 1)) {
3087 CDEBUG(D_LAYOUT, "%s: this deletion will create "
3088 "a hole.\n", lod2obd(d)->obd_name);
3089 mutex_unlock(&lo->ldo_layout_mutex);
3094 /* Mark the component as deleted */
3095 lod_comp->llc_id = LCME_ID_INVAL;
3097 /* Not instantiated component */
3098 if (lod_comp->llc_stripe == NULL)
3101 LASSERT(lod_comp->llc_stripe_count > 0);
3102 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
3103 struct dt_object *obj = lod_comp->llc_stripe[j];
3107 rc = lod_sub_declare_destroy(env, obj, th);
3109 mutex_unlock(&lo->ldo_layout_mutex);
3115 LASSERTF(left >= 0, "left = %d\n", left);
3116 if (left == lo->ldo_comp_cnt) {
3117 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
3118 lod2obd(d)->obd_name, id);
3119 mutex_unlock(&lo->ldo_layout_mutex);
3123 mutex_unlock(&lo->ldo_layout_mutex);
3125 memset(attr, 0, sizeof(*attr));
3126 attr->la_valid = LA_SIZE;
3127 rc = lod_sub_declare_attr_set(env, next, attr, th);
3132 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
3133 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
3134 XATTR_NAME_LOV, 0, th);
3136 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
3143 * Declare layout add/set/del operations issued by special xattr names:
3145 * XATTR_LUSTRE_LOV.add add component(s) to existing file
3146 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
3147 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
3149 * \param[in] env execution environment
3150 * \param[in] dt object
3151 * \param[in] name name of xattr
3152 * \param[in] buf lu_buf contains xattr value
3153 * \param[in] th transaction handle
3155 * \retval 0 on success
3156 * \retval negative if failed
3158 static int lod_declare_modify_layout(const struct lu_env *env,
3159 struct dt_object *dt,
3161 const struct lu_buf *buf,
3164 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3165 struct lod_object *lo = lod_dt_obj(dt);
3167 int rc, len = strlen(XATTR_LUSTRE_LOV);
3170 LASSERT(dt_object_exists(dt));
3172 if (strlen(name) <= len || name[len] != '.') {
3173 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
3174 lod2obd(d)->obd_name, name);
3179 rc = lod_striping_load(env, lo);
3183 /* the layout to be modified must be a composite layout */
3184 if (!lo->ldo_is_composite) {
3185 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
3186 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3187 GOTO(unlock, rc = -EINVAL);
3190 op = (char *)name + len;
3191 if (strcmp(op, "add") == 0) {
3192 rc = lod_declare_layout_add(env, dt, buf, th);
3193 } else if (strcmp(op, "del") == 0) {
3194 rc = lod_declare_layout_del(env, dt, buf, th);
3195 } else if (strncmp(op, "set", strlen("set")) == 0) {
3196 rc = lod_declare_layout_set(env, dt, op, buf, th);
3198 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
3199 lod2obd(d)->obd_name, name);
3200 GOTO(unlock, rc = -ENOTSUPP);
3204 lod_striping_free(env, lo);
3210 * Convert a plain file lov_mds_md to a composite layout.
3212 * \param[in,out] info the thread info::lti_ea_store buffer contains little
3213 * endian plain file layout
3215 * \retval 0 on success, <0 on failure
3217 static int lod_layout_convert(struct lod_thread_info *info)
3219 struct lov_mds_md *lmm = info->lti_ea_store;
3220 struct lov_mds_md *lmm_save;
3221 struct lov_comp_md_v1 *lcm;
3222 struct lov_comp_md_entry_v1 *lcme;
3228 /* realloc buffer to a composite layout which contains one component */
3229 blob_size = lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
3230 le32_to_cpu(lmm->lmm_magic));
3231 size = sizeof(*lcm) + sizeof(*lcme) + blob_size;
3233 OBD_ALLOC_LARGE(lmm_save, blob_size);
3235 GOTO(out, rc = -ENOMEM);
3237 memcpy(lmm_save, lmm, blob_size);
3239 if (info->lti_ea_store_size < size) {
3240 rc = lod_ea_store_resize(info, size);
3245 lcm = info->lti_ea_store;
3246 memset(lcm, 0, sizeof(*lcm) + sizeof(*lcme));
3247 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
3248 lcm->lcm_size = cpu_to_le32(size);
3249 lcm->lcm_layout_gen = cpu_to_le32(le16_to_cpu(
3250 lmm_save->lmm_layout_gen));
3251 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
3252 lcm->lcm_entry_count = cpu_to_le16(1);
3254 lcme = &lcm->lcm_entries[0];
3255 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
3256 lcme->lcme_extent.e_start = 0;
3257 lcme->lcme_extent.e_end = cpu_to_le64(OBD_OBJECT_EOF);
3258 lcme->lcme_offset = cpu_to_le32(sizeof(*lcm) + sizeof(*lcme));
3259 lcme->lcme_size = cpu_to_le32(blob_size);
3261 memcpy((char *)lcm + lcme->lcme_offset, (char *)lmm_save, blob_size);
3266 OBD_FREE_LARGE(lmm_save, blob_size);
3271 * Merge layouts to form a mirrored file.
3273 static int lod_declare_layout_merge(const struct lu_env *env,
3274 struct dt_object *dt, const struct lu_buf *mbuf,
3277 struct lod_thread_info *info = lod_env_info(env);
3278 struct lu_attr *layout_attr = &info->lti_layout_attr;
3279 struct lu_buf *buf = &info->lti_buf;
3280 struct lod_object *lo = lod_dt_obj(dt);
3281 struct lov_comp_md_v1 *lcm;
3282 struct lov_comp_md_v1 *cur_lcm;
3283 struct lov_comp_md_v1 *merge_lcm;
3284 struct lov_comp_md_entry_v1 *lcme;
3285 struct lov_mds_md_v1 *lmm;
3288 __u16 cur_entry_count;
3289 __u16 merge_entry_count;
3291 __u16 mirror_id = 0;
3298 merge_lcm = mbuf->lb_buf;
3299 if (mbuf->lb_len < sizeof(*merge_lcm))
3302 /* must be an existing layout from disk */
3303 if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
3306 merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
3308 /* do not allow to merge two mirrored files */
3309 if (le16_to_cpu(merge_lcm->lcm_mirror_count))
3312 /* verify the target buffer */
3313 rc = lod_get_lov_ea(env, lo);
3315 RETURN(rc ? : -ENODATA);
3317 cur_lcm = info->lti_ea_store;
3318 switch (le32_to_cpu(cur_lcm->lcm_magic)) {
3321 rc = lod_layout_convert(info);
3323 case LOV_MAGIC_COMP_V1:
3333 /* info->lti_ea_store could be reallocated in lod_layout_convert() */
3334 cur_lcm = info->lti_ea_store;
3335 cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
3337 /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
3338 mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
3339 if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
3342 /* size of new layout */
3343 size = le32_to_cpu(cur_lcm->lcm_size) +
3344 le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
3346 memset(buf, 0, sizeof(*buf));
3347 lu_buf_alloc(buf, size);
3348 if (buf->lb_buf == NULL)
3352 memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
3354 offset = sizeof(*lcm) +
3355 sizeof(*lcme) * (cur_entry_count + merge_entry_count);
3356 for (i = 0; i < cur_entry_count; i++) {
3357 struct lov_comp_md_entry_v1 *cur_lcme;
3359 lcme = &lcm->lcm_entries[i];
3360 cur_lcme = &cur_lcm->lcm_entries[i];
3362 lcme->lcme_offset = cpu_to_le32(offset);
3363 memcpy((char *)lcm + offset,
3364 (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
3365 le32_to_cpu(lcme->lcme_size));
3367 offset += le32_to_cpu(lcme->lcme_size);
3369 if (mirror_count == 1 &&
3370 mirror_id_of(le32_to_cpu(lcme->lcme_id)) == 0) {
3371 /* Add mirror from a non-flr file, create new mirror ID.
3372 * Otherwise, keep existing mirror's component ID, used
3373 * for mirror extension.
3375 id = pflr_id(1, i + 1);
3376 lcme->lcme_id = cpu_to_le32(id);
3379 id = max(le32_to_cpu(lcme->lcme_id), id);
3382 mirror_id = mirror_id_of(id) + 1;
3384 /* check if first entry in new layout is DOM */
3385 lmm = (struct lov_mds_md_v1 *)((char *)merge_lcm +
3386 merge_lcm->lcm_entries[0].lcme_offset);
3387 merge_has_dom = lov_pattern(le32_to_cpu(lmm->lmm_pattern)) ==
3390 for (i = 0; i < merge_entry_count; i++) {
3391 struct lov_comp_md_entry_v1 *merge_lcme;
3393 merge_lcme = &merge_lcm->lcm_entries[i];
3394 lcme = &lcm->lcm_entries[cur_entry_count + i];
3396 *lcme = *merge_lcme;
3397 lcme->lcme_offset = cpu_to_le32(offset);
3398 if (merge_has_dom && i == 0)
3399 lcme->lcme_flags |= cpu_to_le32(LCME_FL_STALE);
3401 id = pflr_id(mirror_id, i + 1);
3402 lcme->lcme_id = cpu_to_le32(id);
3404 memcpy((char *)lcm + offset,
3405 (char *)merge_lcm + le32_to_cpu(merge_lcme->lcme_offset),
3406 le32_to_cpu(lcme->lcme_size));
3408 offset += le32_to_cpu(lcme->lcme_size);
3411 /* fixup layout information */
3412 lcm->lcm_size = cpu_to_le32(size);
3413 lcm->lcm_entry_count = cpu_to_le16(cur_entry_count + merge_entry_count);
3414 lcm->lcm_mirror_count = cpu_to_le16(mirror_count);
3415 if ((le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK) == LCM_FL_NONE)
3416 lcm->lcm_flags = cpu_to_le32(LCM_FL_RDONLY);
3418 rc = lod_striping_reload(env, lo, buf);
3422 lod_obj_inc_layout_gen(lo);
3423 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3425 /* transfer layout version to OST objects. */
3426 if (lo->ldo_mirror_count > 1) {
3427 struct lod_obj_stripe_cb_data data = { {0} };
3429 layout_attr->la_valid = LA_LAYOUT_VERSION;
3430 layout_attr->la_layout_version = 0;
3431 data.locd_attr = layout_attr;
3432 data.locd_declare = true;
3433 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
3434 rc = lod_obj_for_each_stripe(env, lo, th, &data);
3439 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), buf,
3440 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3448 * Split layouts, just set the LOVEA with the layout from mbuf.
3450 static int lod_declare_layout_split(const struct lu_env *env,
3451 struct dt_object *dt, const struct lu_buf *mbuf,
3454 struct lod_thread_info *info = lod_env_info(env);
3455 struct lu_attr *layout_attr = &info->lti_layout_attr;
3456 struct lod_object *lo = lod_dt_obj(dt);
3457 struct lov_comp_md_v1 *lcm = mbuf->lb_buf;
3461 rc = lod_striping_reload(env, lo, mbuf);
3465 lod_obj_inc_layout_gen(lo);
3466 /* fix on-disk layout gen */
3467 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3470 /* transfer layout version to OST objects. */
3471 if (lo->ldo_mirror_count > 1) {
3472 struct lod_obj_stripe_cb_data data = { {0} };
3474 layout_attr->la_valid = LA_LAYOUT_VERSION;
3475 layout_attr->la_layout_version = 0;
3476 data.locd_attr = layout_attr;
3477 data.locd_declare = true;
3478 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
3479 rc = lod_obj_for_each_stripe(env, lo, th, &data);
3484 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), mbuf,
3485 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3489 static int lod_layout_declare_or_purge_mirror(const struct lu_env *env,
3490 struct dt_object *dt, const struct lu_buf *buf,
3491 struct thandle *th, bool declare)
3493 struct lod_thread_info *info = lod_env_info(env);
3494 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3495 struct lod_object *lo = lod_dt_obj(dt);
3496 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
3497 struct lov_comp_md_entry_v1 *entry;
3498 struct lov_mds_md_v1 *lmm;
3499 struct dt_object **sub_objs = NULL;
3500 int rc = 0, i, k, array_count = 0;
3505 * other ops (like lod_declare_destroy) could destroying sub objects
3508 mutex_lock(&lo->ldo_layout_mutex);
3511 /* prepare sub-objects array */
3512 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3513 entry = &comp_v1->lcm_entries[i];
3515 if (!(entry->lcme_flags & LCME_FL_INIT))
3518 lmm = (struct lov_mds_md_v1 *)
3519 ((char *)comp_v1 + entry->lcme_offset);
3520 array_count += lmm->lmm_stripe_count;
3522 OBD_ALLOC_PTR_ARRAY(sub_objs, array_count);
3523 if (sub_objs == NULL) {
3524 mutex_unlock(&lo->ldo_layout_mutex);
3529 k = 0; /* sub_objs index */
3530 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3531 struct lov_ost_data_v1 *objs;
3532 struct lu_object *o, *n;
3533 struct dt_object *dto;
3534 struct lu_device *nd;
3535 struct lov_mds_md_v3 *v3;
3539 entry = &comp_v1->lcm_entries[i];
3541 if (!(entry->lcme_flags & LCME_FL_INIT))
3544 lmm = (struct lov_mds_md_v1 *)
3545 ((char *)comp_v1 + entry->lcme_offset);
3546 v3 = (struct lov_mds_md_v3 *)lmm;
3547 if (lmm->lmm_magic == LOV_MAGIC_V3)
3548 objs = &v3->lmm_objects[0];
3550 objs = &lmm->lmm_objects[0];
3552 for (j = 0; j < lmm->lmm_stripe_count; j++) {
3553 idx = objs[j].l_ost_idx;
3554 rc = ostid_to_fid(&info->lti_fid, &objs[j].l_ost_oi,
3559 if (!fid_is_sane(&info->lti_fid)) {
3560 CERROR("%s: sub-object insane fid "DFID"\n",
3561 lod2obd(d)->obd_name,
3562 PFID(&info->lti_fid));
3563 GOTO(out, rc = -EINVAL);
3566 lod_getref(&d->lod_ost_descs);
3568 rc = validate_lod_and_idx(d, idx);
3570 lod_putref(d, &d->lod_ost_descs);
3574 nd = &OST_TGT(d, idx)->ltd_tgt->dd_lu_dev;
3575 lod_putref(d, &d->lod_ost_descs);
3577 o = lu_object_find_at(env, nd, &info->lti_fid, NULL);
3579 GOTO(out, rc = PTR_ERR(o));
3581 n = lu_object_locate(o->lo_header, nd->ld_type);
3583 lu_object_put(env, n);
3584 GOTO(out, rc = -ENOENT);
3587 dto = container_of(n, struct dt_object, do_lu);
3590 rc = lod_sub_declare_destroy(env, dto, th);
3591 dt_object_put(env, dto);
3596 * collect to-be-destroyed sub objects, the
3597 * reference would be released after actual
3603 } /* for each stripe */
3604 } /* for each component in the mirror */
3609 /* destroy the sub objects */
3610 for (; i < k; i++) {
3611 rc = lod_sub_destroy(env, sub_objs[i], th);
3614 dt_object_put(env, sub_objs[i]);
3618 * if a sub object destroy failed, we'd release sub objects
3619 * reference get from above sub_objs collection.
3622 dt_object_put(env, sub_objs[i]);
3624 OBD_FREE_PTR_ARRAY(sub_objs, array_count);
3626 mutex_unlock(&lo->ldo_layout_mutex);
3632 * Purge layouts, delete sub objects in the mirror stored in the vic_buf,
3633 * and set the LOVEA with the layout from mbuf.
3635 static int lod_declare_layout_purge(const struct lu_env *env,
3636 struct dt_object *dt, const struct lu_buf *buf,
3639 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3640 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
3645 if (le32_to_cpu(comp_v1->lcm_magic) != LOV_MAGIC_COMP_V1) {
3646 CERROR("%s: invalid layout magic %#x != %#x\n",
3647 lod2obd(d)->obd_name, le32_to_cpu(comp_v1->lcm_magic),
3652 if (cpu_to_le32(LOV_MAGIC_COMP_V1) != LOV_MAGIC_COMP_V1)
3653 lustre_swab_lov_comp_md_v1(comp_v1);
3655 /* from now on, @buf contains cpu endian data */
3657 if (comp_v1->lcm_mirror_count != 0) {
3658 CERROR("%s: can only purge one mirror from "DFID"\n",
3659 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3663 /* delcare sub objects deletion in the mirror stored in @buf */
3664 rc = lod_layout_declare_or_purge_mirror(env, dt, buf, th, true);
3668 /* delete sub objects from the mirror stored in @buf */
3669 static int lod_layout_purge(const struct lu_env *env, struct dt_object *dt,
3670 const struct lu_buf *buf, struct thandle *th)
3675 rc = lod_layout_declare_or_purge_mirror(env, dt, buf, th, false);
3680 * Implementation of dt_object_operations::do_declare_xattr_set.
3682 * \see dt_object_operations::do_declare_xattr_set() in the API description
3685 * the extension to the API:
3686 * - declaring LOVEA requests striping creation
3687 * - LU_XATTR_REPLACE means layout swap
3689 static int lod_declare_xattr_set(const struct lu_env *env,
3690 struct dt_object *dt,
3691 const struct lu_buf *buf,
3692 const char *name, int fl,
3695 struct dt_object *next = dt_object_child(dt);
3696 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3701 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
3702 if ((S_ISREG(mode) || mode == 0) &&
3703 !(fl & (LU_XATTR_REPLACE | LU_XATTR_MERGE | LU_XATTR_SPLIT |
3705 (strcmp(name, XATTR_NAME_LOV) == 0 ||
3706 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
3708 * this is a request to create object's striping.
3710 * allow to declare predefined striping on a new (!mode) object
3711 * which is supposed to be replay of regular file creation
3712 * (when LOV setting is declared)
3714 * LU_XATTR_REPLACE is set to indicate a layout swap
3716 if (dt_object_exists(dt)) {
3717 rc = dt_attr_get(env, next, attr);
3721 memset(attr, 0, sizeof(*attr));
3722 attr->la_valid = LA_TYPE | LA_MODE;
3723 attr->la_mode = S_IFREG;
3725 rc = lod_declare_striped_create(env, dt, attr, buf, th);
3726 } else if (fl & LU_XATTR_MERGE) {
3727 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3728 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3729 rc = lod_declare_layout_merge(env, dt, buf, th);
3730 } else if (fl & LU_XATTR_SPLIT) {
3731 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3732 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3733 rc = lod_declare_layout_split(env, dt, buf, th);
3734 } else if (fl & LU_XATTR_PURGE) {
3735 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3736 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3737 rc = lod_declare_layout_purge(env, dt, buf, th);
3738 } else if (S_ISREG(mode) &&
3739 strlen(name) >= sizeof(XATTR_LUSTRE_LOV) + 3 &&
3740 allowed_lustre_lov(name)) {
3742 * this is a request to modify object's striping.
3743 * add/set/del component(s).
3745 if (!dt_object_exists(dt))
3748 rc = lod_declare_modify_layout(env, dt, name, buf, th);
3749 } else if (S_ISDIR(mode)) {
3750 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
3751 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3752 rc = lod_replace_parent_fid(env, dt, buf, th, true);
3754 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
3761 * Apply xattr changes to the object.
3763 * Applies xattr changes to the object and the stripes if the latter exist.
3765 * \param[in] env execution environment
3766 * \param[in] dt object
3767 * \param[in] buf buffer pointing to the new value of xattr
3768 * \param[in] name name of xattr
3769 * \param[in] fl flags
3770 * \param[in] th transaction handle
3772 * \retval 0 on success
3773 * \retval negative if failed
3775 static int lod_xattr_set_internal(const struct lu_env *env,
3776 struct dt_object *dt,
3777 const struct lu_buf *buf,
3778 const char *name, int fl,
3781 struct dt_object *next = dt_object_child(dt);
3782 struct lod_object *lo = lod_dt_obj(dt);
3787 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3788 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3791 /* Note: Do not set LinkEA on sub-stripes, otherwise
3792 * it will confuse the fid2path process(see mdt_path_current()).
3793 * The linkEA between master and sub-stripes is set in
3794 * lod_xattr_set_lmv(). */
3795 if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
3798 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3799 if (!lo->ldo_stripe[i])
3802 if (!dt_object_exists(lo->ldo_stripe[i]))
3805 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
3815 * Delete an extended attribute.
3817 * Deletes specified xattr from the object and the stripes if the latter exist.
3819 * \param[in] env execution environment
3820 * \param[in] dt object
3821 * \param[in] name name of xattr
3822 * \param[in] th transaction handle
3824 * \retval 0 on success
3825 * \retval negative if failed
3827 static int lod_xattr_del_internal(const struct lu_env *env,
3828 struct dt_object *dt,
3829 const char *name, struct thandle *th)
3831 struct dt_object *next = dt_object_child(dt);
3832 struct lod_object *lo = lod_dt_obj(dt);
3838 rc = lod_sub_xattr_del(env, next, name, th);
3839 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3842 if (lo->ldo_dir_stripe_count == 0)
3845 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3846 if (!lo->ldo_stripe[i])
3849 if (!dt_object_exists(lo->ldo_stripe[i]))
3852 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3861 * Set default striping on a directory.
3863 * Sets specified striping on a directory object unless it matches the default
3864 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3865 * EA. This striping will be used when regular file is being created in this
3868 * \param[in] env execution environment
3869 * \param[in] dt the striped object
3870 * \param[in] buf buffer with the striping
3871 * \param[in] name name of EA
3872 * \param[in] fl xattr flag (see OSD API description)
3873 * \param[in] th transaction handle
3875 * \retval 0 on success
3876 * \retval negative if failed
3878 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
3879 struct dt_object *dt,
3880 const struct lu_buf *buf,
3881 const char *name, int fl,
3884 struct lov_user_md_v1 *lum;
3885 struct lov_user_md_v3 *v3 = NULL;
3886 const char *pool_name = NULL;
3891 LASSERT(buf != NULL && buf->lb_buf != NULL);
3894 switch (lum->lmm_magic) {
3895 case LOV_USER_MAGIC_SPECIFIC:
3896 case LOV_USER_MAGIC_V3:
3898 if (v3->lmm_pool_name[0] != '\0')
3899 pool_name = v3->lmm_pool_name;
3901 case LOV_USER_MAGIC_V1:
3902 /* if { size, offset, count } = { 0, -1, 0 } and no pool
3903 * (i.e. all default values specified) then delete default
3904 * striping from dir. */
3906 "set default striping: sz %u # %u offset %d %s %s\n",
3907 (unsigned)lum->lmm_stripe_size,
3908 (unsigned)lum->lmm_stripe_count,
3909 (int)lum->lmm_stripe_offset,
3910 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
3912 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
3913 lum->lmm_stripe_count,
3914 lum->lmm_stripe_offset,
3917 case LOV_USER_MAGIC_COMP_V1:
3919 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lum;
3920 struct lov_comp_md_entry_v1 *lcme;
3923 comp_cnt = le16_to_cpu(lcm->lcm_entry_count);
3924 for (i = 0; i < comp_cnt; i++) {
3925 lcme = &lcm->lcm_entries[i];
3926 if (lcme->lcme_flags & cpu_to_le32(LCME_FL_EXTENSION)) {
3927 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_SEL);
3936 CERROR("Invalid magic %x\n", lum->lmm_magic);
3941 rc = lod_xattr_del_internal(env, dt, name, th);
3945 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3952 * Set default striping on a directory object.
3954 * Sets specified striping on a directory object unless it matches the default
3955 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3956 * EA. This striping will be used when a new directory is being created in the
3959 * \param[in] env execution environment
3960 * \param[in] dt the striped object
3961 * \param[in] buf buffer with the striping
3962 * \param[in] name name of EA
3963 * \param[in] fl xattr flag (see OSD API description)
3964 * \param[in] th transaction handle
3966 * \retval 0 on success
3967 * \retval negative if failed
3969 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
3970 struct dt_object *dt,
3971 const struct lu_buf *buf,
3972 const char *name, int fl,
3975 struct lmv_user_md_v1 *lum;
3980 LASSERT(buf != NULL && buf->lb_buf != NULL);
3984 "set default stripe_count # %u stripe_offset %d hash %u\n",
3985 le32_to_cpu(lum->lum_stripe_count),
3986 (int)le32_to_cpu(lum->lum_stripe_offset),
3987 le32_to_cpu(lum->lum_hash_type));
3989 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
3990 le32_to_cpu(lum->lum_stripe_offset)) &&
3991 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
3992 rc = lod_xattr_del_internal(env, dt, name, th);
3996 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
4005 * Turn directory into a striped directory.
4007 * During replay the client sends the striping created before MDT
4008 * failure, then the layer above LOD sends this defined striping
4009 * using ->do_xattr_set(), so LOD uses this method to replay creation
4010 * of the stripes. Notice the original information for the striping
4011 * (#stripes, FIDs, etc) was transferred in declare path.
4013 * \param[in] env execution environment
4014 * \param[in] dt the striped object
4015 * \param[in] buf not used currently
4016 * \param[in] name not used currently
4017 * \param[in] fl xattr flag (see OSD API description)
4018 * \param[in] th transaction handle
4020 * \retval 0 on success
4021 * \retval negative if failed
4023 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
4024 const struct lu_buf *buf, const char *name,
4025 int fl, struct thandle *th)
4027 struct lod_object *lo = lod_dt_obj(dt);
4028 struct lod_thread_info *info = lod_env_info(env);
4029 struct lu_attr *attr = &info->lti_attr;
4030 struct dt_object_format *dof = &info->lti_format;
4031 struct lu_buf lmv_buf;
4032 struct lu_buf slave_lmv_buf;
4033 struct lmv_mds_md_v1 *lmm;
4034 struct lmv_mds_md_v1 *slave_lmm = NULL;
4035 struct dt_insert_rec *rec = &info->lti_dt_rec;
4040 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4043 /* The stripes are supposed to be allocated in declare phase,
4044 * if there are no stripes being allocated, it will skip */
4045 if (lo->ldo_dir_stripe_count == 0) {
4046 if (lo->ldo_dir_is_foreign) {
4047 rc = lod_sub_xattr_set(env, dt_object_child(dt), buf,
4048 XATTR_NAME_LMV, fl, th);
4055 rc = dt_attr_get(env, dt_object_child(dt), attr);
4059 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_FLAGS |
4060 LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
4061 dof->dof_type = DFT_DIR;
4063 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
4066 lmm = lmv_buf.lb_buf;
4068 OBD_ALLOC_PTR(slave_lmm);
4069 if (slave_lmm == NULL)
4072 lod_prep_slave_lmv_md(slave_lmm, lmm);
4073 slave_lmv_buf.lb_buf = slave_lmm;
4074 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
4076 rec->rec_type = S_IFDIR;
4077 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4078 struct dt_object *dto = lo->ldo_stripe[i];
4079 char *stripe_name = info->lti_key;
4080 struct lu_name *sname;
4081 struct linkea_data ldata = { NULL };
4082 struct lu_buf linkea_buf;
4084 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
4088 /* fail a remote stripe creation */
4089 if (i && OBD_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_CREATE))
4092 /* don't create stripe if:
4093 * 1. it's source stripe of migrating directory
4094 * 2. it's existed stripe of splitting directory
4096 if ((lod_is_migrating(lo) && i >= lo->ldo_dir_migrate_offset) ||
4097 (lod_is_splitting(lo) && i < lo->ldo_dir_split_offset)) {
4098 if (!dt_object_exists(dto))
4099 GOTO(out, rc = -EINVAL);
4101 dt_write_lock(env, dto, DT_TGT_CHILD);
4102 rc = lod_sub_create(env, dto, attr, NULL, dof, th);
4104 dt_write_unlock(env, dto);
4108 rc = lod_sub_ref_add(env, dto, th);
4109 dt_write_unlock(env, dto);
4113 rec->rec_fid = lu_object_fid(&dto->do_lu);
4114 rc = lod_sub_insert(env, dto,
4115 (const struct dt_rec *)rec,
4116 (const struct dt_key *)dot, th);
4121 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
4122 cfs_fail_val != i) {
4123 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
4125 slave_lmm->lmv_master_mdt_index =
4128 slave_lmm->lmv_master_mdt_index =
4131 rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
4132 XATTR_NAME_LMV, 0, th);
4137 /* don't insert stripe if it's existed stripe of splitting
4138 * directory (this directory is striped).
4139 * NB, plain directory will insert itself as the first
4142 if (lod_is_splitting(lo) && lo->ldo_dir_split_offset > 1 &&
4143 lo->ldo_dir_split_offset > i)
4146 rec->rec_fid = lu_object_fid(&dt->do_lu);
4147 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
4148 (const struct dt_key *)dotdot, th);
4152 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
4154 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4155 PFID(lu_object_fid(&dto->do_lu)), i + 1);
4157 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4158 PFID(lu_object_fid(&dto->do_lu)), i);
4160 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
4161 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
4162 sname, lu_object_fid(&dt->do_lu));
4166 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
4167 linkea_buf.lb_len = ldata.ld_leh->leh_len;
4168 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
4169 XATTR_NAME_LINK, 0, th);
4173 rec->rec_fid = lu_object_fid(&dto->do_lu);
4174 rc = lod_sub_insert(env, dt_object_child(dt),
4175 (const struct dt_rec *)rec,
4176 (const struct dt_key *)stripe_name, th);
4180 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
4185 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
4186 rc = lod_sub_xattr_set(env, dt_object_child(dt),
4187 &lmv_buf, XATTR_NAME_LMV, fl, th);
4189 if (slave_lmm != NULL)
4190 OBD_FREE_PTR(slave_lmm);
4196 * Helper function to declare/execute creation of a striped directory
4198 * Called in declare/create object path, prepare striping for a directory
4199 * and prepare defaults data striping for the objects to be created in
4200 * that directory. Notice the function calls "declaration" or "execution"
4201 * methods depending on \a declare param. This is a consequence of the
4202 * current approach while we don't have natural distributed transactions:
4203 * we basically execute non-local updates in the declare phase. So, the
4204 * arguments for the both phases are the same and this is the reason for
4205 * this function to exist.
4207 * \param[in] env execution environment
4208 * \param[in] dt object
4209 * \param[in] attr attributes the stripes will be created with
4210 * \param[in] lmu lmv_user_md if MDT indices are specified
4211 * \param[in] dof format of stripes (see OSD API description)
4212 * \param[in] th transaction handle
4213 * \param[in] declare where to call "declare" or "execute" methods
4215 * \retval 0 on success
4216 * \retval negative if failed
4218 static int lod_dir_striping_create_internal(const struct lu_env *env,
4219 struct dt_object *dt,
4220 struct lu_attr *attr,
4221 const struct lu_buf *lmu,
4222 struct dt_object_format *dof,
4226 struct lod_thread_info *info = lod_env_info(env);
4227 struct lod_object *lo = lod_dt_obj(dt);
4228 const struct lod_default_striping *lds = lo->ldo_def_striping;
4232 LASSERT(ergo(lds != NULL,
4233 lds->lds_def_striping_set ||
4234 lds->lds_dir_def_striping_set));
4236 if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
4237 lo->ldo_dir_stripe_offset)) {
4239 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
4240 int stripe_count = lo->ldo_dir_stripe_count;
4242 if (info->lti_ea_store_size < sizeof(*v1)) {
4243 rc = lod_ea_store_resize(info, sizeof(*v1));
4246 v1 = info->lti_ea_store;
4249 memset(v1, 0, sizeof(*v1));
4250 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
4251 v1->lum_stripe_count = cpu_to_le32(stripe_count);
4252 v1->lum_stripe_offset =
4253 cpu_to_le32(lo->ldo_dir_stripe_offset);
4255 info->lti_buf.lb_buf = v1;
4256 info->lti_buf.lb_len = sizeof(*v1);
4257 lmu = &info->lti_buf;
4261 rc = lod_declare_xattr_set_lmv(env, dt, attr, lmu, dof,
4264 rc = lod_xattr_set_lmv(env, dt, lmu, XATTR_NAME_LMV, 0,
4269 /* foreign LMV EA case */
4271 struct lmv_foreign_md *lfm = lmu->lb_buf;
4273 if (lfm->lfm_magic == LMV_MAGIC_FOREIGN) {
4274 rc = lod_declare_xattr_set_lmv(env, dt, attr,
4278 if (lo->ldo_dir_is_foreign) {
4279 LASSERT(lo->ldo_foreign_lmv != NULL &&
4280 lo->ldo_foreign_lmv_size > 0);
4281 info->lti_buf.lb_buf = lo->ldo_foreign_lmv;
4282 info->lti_buf.lb_len = lo->ldo_foreign_lmv_size;
4283 lmu = &info->lti_buf;
4284 rc = lod_xattr_set_lmv(env, dt, lmu,
4285 XATTR_NAME_LMV, 0, th);
4290 /* Transfer default LMV striping from the parent */
4291 if (lds != NULL && lds->lds_dir_def_striping_set &&
4292 lds->lds_dir_def_max_inherit != LMV_INHERIT_END &&
4293 lds->lds_dir_def_max_inherit != LMV_INHERIT_NONE &&
4294 !(LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
4295 lds->lds_dir_def_stripe_offset) &&
4296 le32_to_cpu(lds->lds_dir_def_hash_type) !=
4297 LMV_HASH_TYPE_UNKNOWN)) {
4298 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
4300 if (info->lti_ea_store_size < sizeof(*v1)) {
4301 rc = lod_ea_store_resize(info, sizeof(*v1));
4304 v1 = info->lti_ea_store;
4307 memset(v1, 0, sizeof(*v1));
4308 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
4309 v1->lum_stripe_count =
4310 cpu_to_le32(lds->lds_dir_def_stripe_count);
4311 v1->lum_stripe_offset =
4312 cpu_to_le32(lds->lds_dir_def_stripe_offset);
4314 cpu_to_le32(lds->lds_dir_def_hash_type);
4315 v1->lum_max_inherit =
4316 lmv_inherit_next(lds->lds_dir_def_max_inherit);
4317 v1->lum_max_inherit_rr =
4318 lmv_inherit_rr_next(lds->lds_dir_def_max_inherit_rr);
4320 info->lti_buf.lb_buf = v1;
4321 info->lti_buf.lb_len = sizeof(*v1);
4323 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4324 XATTR_NAME_DEFAULT_LMV,
4327 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
4329 XATTR_NAME_DEFAULT_LMV, 0,
4335 /* Transfer default LOV striping from the parent */
4336 if (lds != NULL && lds->lds_def_striping_set &&
4337 lds->lds_def_comp_cnt != 0) {
4338 struct lov_mds_md *lmm;
4339 int lmm_size = lod_comp_md_size(lo, true);
4341 if (info->lti_ea_store_size < lmm_size) {
4342 rc = lod_ea_store_resize(info, lmm_size);
4346 lmm = info->lti_ea_store;
4348 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
4352 info->lti_buf.lb_buf = lmm;
4353 info->lti_buf.lb_len = lmm_size;
4356 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4357 XATTR_NAME_LOV, 0, th);
4359 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4360 XATTR_NAME_LOV, 0, th);
4368 static int lod_declare_dir_striping_create(const struct lu_env *env,
4369 struct dt_object *dt,
4370 struct lu_attr *attr,
4372 struct dt_object_format *dof,
4375 return lod_dir_striping_create_internal(env, dt, attr, lmu, dof, th,
4379 static int lod_dir_striping_create(const struct lu_env *env,
4380 struct dt_object *dt,
4381 struct lu_attr *attr,
4382 struct dt_object_format *dof,
4385 return lod_dir_striping_create_internal(env, dt, attr, NULL, dof, th,
4390 * Make LOV EA for striped object.
4392 * Generate striping information and store it in the LOV EA of the given
4393 * object. The caller must ensure nobody else is calling the function
4394 * against the object concurrently. The transaction must be started.
4395 * FLDB service must be running as well; it's used to map FID to the target,
4396 * which is stored in LOV EA.
4398 * \param[in] env execution environment for this thread
4399 * \param[in] lo LOD object
4400 * \param[in] th transaction handle
4402 * \retval 0 if LOV EA is stored successfully
4403 * \retval negative error number on failure
4405 static int lod_generate_and_set_lovea(const struct lu_env *env,
4406 struct lod_object *lo,
4409 struct lod_thread_info *info = lod_env_info(env);
4410 struct dt_object *next = dt_object_child(&lo->ldo_obj);
4411 struct lov_mds_md_v1 *lmm;
4417 if (lo->ldo_comp_cnt == 0 && !lo->ldo_is_foreign) {
4418 lod_striping_free_nolock(env, lo);
4419 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
4423 lmm_size = lod_comp_md_size(lo, false);
4424 if (info->lti_ea_store_size < lmm_size) {
4425 rc = lod_ea_store_resize(info, lmm_size);
4429 lmm = info->lti_ea_store;
4431 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
4435 info->lti_buf.lb_buf = lmm;
4436 info->lti_buf.lb_len = lmm_size;
4437 rc = lod_sub_xattr_set(env, next, &info->lti_buf,
4438 XATTR_NAME_LOV, 0, th);
4442 static __u32 lod_gen_component_id(struct lod_object *lo,
4443 int mirror_id, int comp_idx);
4446 * Repeat an existing component
4448 * Creates a new layout by replicating an existing component. Uses striping
4449 * policy from previous component as a template for the striping for the new
4452 * New component starts with zero length, will be extended (or removed) before
4453 * returning layout to client.
4455 * NB: Reallocates layout components array (lo->ldo_comp_entries), invalidating
4456 * any pre-existing pointers to components. Handle with care.
4458 * \param[in] env execution environment for this thread
4459 * \param[in,out] lo object to update the layout of
4460 * \param[in] index index of component to copy
4462 * \retval 0 on success
4463 * \retval negative errno on error
4465 static int lod_layout_repeat_comp(const struct lu_env *env,
4466 struct lod_object *lo, int index)
4468 struct lod_layout_component *lod_comp;
4469 struct lod_layout_component *new_comp = NULL;
4470 struct lod_layout_component *comp_array;
4471 int rc = 0, i, new_cnt = lo->ldo_comp_cnt + 1;
4476 lod_comp = &lo->ldo_comp_entries[index];
4477 LASSERT(lod_comp_inited(lod_comp) && lod_comp->llc_id != LCME_ID_INVAL);
4479 CDEBUG(D_LAYOUT, "repeating component %d\n", index);
4481 OBD_ALLOC_PTR_ARRAY(comp_array, new_cnt);
4482 if (comp_array == NULL)
4483 GOTO(out, rc = -ENOMEM);
4485 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4486 memcpy(&comp_array[i + offset], &lo->ldo_comp_entries[i],
4487 sizeof(*comp_array));
4489 /* Duplicate this component in to the next slot */
4491 new_comp = &comp_array[i + 1];
4492 memcpy(&comp_array[i + 1], &lo->ldo_comp_entries[i],
4493 sizeof(*comp_array));
4494 /* We must now skip this new component when copying */
4499 /* Set up copied component */
4500 new_comp->llc_flags &= ~LCME_FL_INIT;
4501 new_comp->llc_stripe = NULL;
4502 new_comp->llc_stripes_allocated = 0;
4503 new_comp->llc_ost_indices = NULL;
4504 new_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4505 /* for uninstantiated components, layout gen stores default stripe
4507 new_comp->llc_layout_gen = lod_comp->llc_stripe_offset;
4508 /* This makes the repeated component zero-length, placed at the end of
4509 * the preceding component */
4510 new_comp->llc_extent.e_start = new_comp->llc_extent.e_end;
4511 new_comp->llc_timestamp = lod_comp->llc_timestamp;
4512 new_comp->llc_pool = NULL;
4514 rc = lod_set_pool(&new_comp->llc_pool, lod_comp->llc_pool);
4518 if (new_comp->llc_ostlist.op_array) {
4519 __u32 *op_array = NULL;
4521 OBD_ALLOC(op_array, new_comp->llc_ostlist.op_size);
4523 GOTO(out, rc = -ENOMEM);
4524 memcpy(op_array, &new_comp->llc_ostlist.op_array,
4525 new_comp->llc_ostlist.op_size);
4526 new_comp->llc_ostlist.op_array = op_array;
4529 OBD_FREE_PTR_ARRAY(lo->ldo_comp_entries, lo->ldo_comp_cnt);
4530 lo->ldo_comp_entries = comp_array;
4531 lo->ldo_comp_cnt = new_cnt;
4533 /* Generate an id for the new component */
4534 mirror_id = mirror_id_of(new_comp->llc_id);
4535 new_comp->llc_id = LCME_ID_INVAL;
4536 new_comp->llc_id = lod_gen_component_id(lo, mirror_id, index + 1);
4537 if (new_comp->llc_id == LCME_ID_INVAL)
4538 GOTO(out, rc = -ERANGE);
4543 OBD_FREE_PTR_ARRAY(comp_array, new_cnt);
4548 static int lod_layout_data_init(struct lod_thread_info *info, __u32 comp_cnt)
4552 /* clear memory region that will be used for layout change */
4553 memset(&info->lti_layout_attr, 0, sizeof(struct lu_attr));
4554 info->lti_count = 0;
4556 if (info->lti_comp_size >= comp_cnt)
4559 if (info->lti_comp_size > 0) {
4560 OBD_FREE_PTR_ARRAY(info->lti_comp_idx, info->lti_comp_size);
4561 info->lti_comp_size = 0;
4564 OBD_ALLOC_PTR_ARRAY(info->lti_comp_idx, comp_cnt);
4565 if (!info->lti_comp_idx)
4568 info->lti_comp_size = comp_cnt;
4573 * Prepare new layout minus deleted components
4575 * Removes components marked for deletion (LCME_ID_INVAL) by copying to a new
4576 * layout and skipping those components. Removes stripe objects if any exist.
4579 * Reallocates layout components array (lo->ldo_comp_entries), invalidating
4580 * any pre-existing pointers to components.
4582 * Caller is responsible for updating mirror end (ldo_mirror[].lme_end).
4584 * \param[in] env execution environment for this thread
4585 * \param[in,out] lo object to update the layout of
4586 * \param[in] th transaction handle for this operation
4588 * \retval # of components deleted
4589 * \retval negative errno on error
4591 static int lod_layout_del_prep_layout(const struct lu_env *env,
4592 struct lod_object *lo,
4595 struct lod_layout_component *lod_comp;
4596 struct lod_thread_info *info = lod_env_info(env);
4597 int rc = 0, i, j, deleted = 0;
4601 LASSERT(lo->ldo_is_composite);
4602 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
4604 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
4608 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4609 lod_comp = &lo->ldo_comp_entries[i];
4611 if (lod_comp->llc_id != LCME_ID_INVAL) {
4612 /* Build array of things to keep */
4613 info->lti_comp_idx[info->lti_count++] = i;
4617 lod_obj_set_pool(lo, i, NULL);
4618 if (lod_comp->llc_ostlist.op_array) {
4619 OBD_FREE(lod_comp->llc_ostlist.op_array,
4620 lod_comp->llc_ostlist.op_size);
4621 lod_comp->llc_ostlist.op_array = NULL;
4622 lod_comp->llc_ostlist.op_size = 0;
4626 CDEBUG(D_LAYOUT, "deleting comp %d, left %d\n", i,
4627 lo->ldo_comp_cnt - deleted);
4629 /* No striping info for this component */
4630 if (lod_comp->llc_stripe == NULL)
4633 LASSERT(lod_comp->llc_stripe_count > 0);
4634 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4635 struct dt_object *obj = lod_comp->llc_stripe[j];
4640 /* components which are not init have no sub objects
4642 if (lod_comp_inited(lod_comp)) {
4643 rc = lod_sub_destroy(env, obj, th);
4648 lu_object_put(env, &obj->do_lu);
4649 lod_comp->llc_stripe[j] = NULL;
4651 OBD_FREE_PTR_ARRAY(lod_comp->llc_stripe,
4652 lod_comp->llc_stripes_allocated);
4653 lod_comp->llc_stripe = NULL;
4654 OBD_FREE_PTR_ARRAY(lod_comp->llc_ost_indices,
4655 lod_comp->llc_stripes_allocated);
4656 lod_comp->llc_ost_indices = NULL;
4657 lod_comp->llc_stripes_allocated = 0;
4660 /* info->lti_count has the amount of left components */
4661 LASSERTF(info->lti_count >= 0 && info->lti_count < lo->ldo_comp_cnt,
4662 "left = %d, lo->ldo_comp_cnt %d\n", (int)info->lti_count,
4663 (int)lo->ldo_comp_cnt);
4665 if (info->lti_count > 0) {
4666 struct lod_layout_component *comp_array;
4668 OBD_ALLOC_PTR_ARRAY(comp_array, info->lti_count);
4669 if (comp_array == NULL)
4670 GOTO(out, rc = -ENOMEM);
4672 for (i = 0; i < info->lti_count; i++) {
4673 memcpy(&comp_array[i],
4674 &lo->ldo_comp_entries[info->lti_comp_idx[i]],
4675 sizeof(*comp_array));
4678 OBD_FREE_PTR_ARRAY(lo->ldo_comp_entries, lo->ldo_comp_cnt);
4679 lo->ldo_comp_entries = comp_array;
4680 lo->ldo_comp_cnt = info->lti_count;
4682 lod_free_comp_entries(lo);
4687 return rc ? rc : deleted;
4691 * Delete layout component(s)
4693 * This function sets up the layout data in the env and does the setattrs
4694 * required to write out the new layout. The layout itself is modified in
4695 * lod_layout_del_prep_layout.
4697 * \param[in] env execution environment for this thread
4698 * \param[in] dt object
4699 * \param[in] th transaction handle
4701 * \retval 0 on success
4702 * \retval negative error number on failure
4704 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
4707 struct lod_object *lo = lod_dt_obj(dt);
4708 struct dt_object *next = dt_object_child(dt);
4709 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4712 LASSERT(lo->ldo_mirror_count == 1);
4714 mutex_lock(&lo->ldo_layout_mutex);
4716 rc = lod_layout_del_prep_layout(env, lo, th);
4720 /* Only do this if we didn't delete all components */
4721 if (lo->ldo_comp_cnt > 0) {
4722 lo->ldo_mirrors[0].lme_end = lo->ldo_comp_cnt - 1;
4723 lod_obj_inc_layout_gen(lo);
4726 LASSERT(dt_object_exists(dt));
4727 rc = dt_attr_get(env, next, attr);
4731 if (attr->la_size > 0) {
4733 attr->la_valid = LA_SIZE;
4734 rc = lod_sub_attr_set(env, next, attr, th);
4739 rc = lod_generate_and_set_lovea(env, lo, th);
4743 lod_striping_free_nolock(env, lo);
4745 mutex_unlock(&lo->ldo_layout_mutex);
4751 static int lod_get_default_lov_striping(const struct lu_env *env,
4752 struct lod_object *lo,
4753 struct lod_default_striping *lds,
4754 struct dt_allocation_hint *ah);
4756 * Implementation of dt_object_operations::do_xattr_set.
4758 * Sets specified extended attribute on the object. Three types of EAs are
4760 * LOV EA - stores striping for a regular file or default striping (when set
4762 * LMV EA - stores a marker for the striped directories
4763 * DMV EA - stores default directory striping
4765 * When striping is applied to a non-striped existing object (this is called
4766 * late striping), then LOD notices the caller wants to turn the object into a
4767 * striped one. The stripe objects are created and appropriate EA is set:
4768 * LOV EA storing all the stripes directly or LMV EA storing just a small header
4769 * with striping configuration.
4771 * \see dt_object_operations::do_xattr_set() in the API description for details.
4773 static int lod_xattr_set(const struct lu_env *env,
4774 struct dt_object *dt, const struct lu_buf *buf,
4775 const char *name, int fl, struct thandle *th)
4777 struct dt_object *next = dt_object_child(dt);
4778 struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
4779 struct lod_object *lo = lod_dt_obj(dt);
4780 struct lod_obj_stripe_cb_data data = { {0} };
4785 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4786 !strcmp(name, XATTR_NAME_LMV)) {
4788 case LU_XATTR_CREATE:
4789 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
4792 case LU_XATTR_REPLACE:
4793 rc = lod_dir_layout_set(env, dt, buf, fl, th);
4800 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4801 strcmp(name, XATTR_NAME_LOV) == 0) {
4802 struct lod_default_striping *lds = lod_lds_buf_get(env);
4803 struct lov_user_md_v1 *v1 = buf->lb_buf;
4804 char pool[LOV_MAXPOOLNAME + 1];
4807 /* get existing striping config */
4808 rc = lod_get_default_lov_striping(env, lod_dt_obj(dt), lds,
4813 memset(pool, 0, sizeof(pool));
4814 if (lds->lds_def_striping_set == 1)
4815 lod_layout_get_pool(lds->lds_def_comp_entries,
4816 lds->lds_def_comp_cnt, pool,
4819 is_del = LOVEA_DELETE_VALUES(v1->lmm_stripe_size,
4820 v1->lmm_stripe_count,
4821 v1->lmm_stripe_offset,
4824 /* Retain the pool name if it is not given */
4825 if (v1->lmm_magic == LOV_USER_MAGIC_V1 && pool[0] != '\0' &&
4827 struct lod_thread_info *info = lod_env_info(env);
4828 struct lov_user_md_v3 *v3 = info->lti_ea_store;
4830 memset(v3, 0, sizeof(*v3));
4831 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
4832 v3->lmm_pattern = cpu_to_le32(v1->lmm_pattern);
4833 v3->lmm_stripe_count =
4834 cpu_to_le32(v1->lmm_stripe_count);
4835 v3->lmm_stripe_offset =
4836 cpu_to_le32(v1->lmm_stripe_offset);
4837 v3->lmm_stripe_size = cpu_to_le32(v1->lmm_stripe_size);
4839 strlcpy(v3->lmm_pool_name, pool,
4840 sizeof(v3->lmm_pool_name));
4842 info->lti_buf.lb_buf = v3;
4843 info->lti_buf.lb_len = sizeof(*v3);
4844 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4847 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name,
4851 if (lds->lds_def_striping_set == 1 &&
4852 lds->lds_def_comp_entries != NULL)
4853 lod_free_def_comp_entries(lds);
4856 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4857 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
4859 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
4862 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4863 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4864 strcmp(name, XATTR_LUSTRE_LOV) == 0 ||
4865 allowed_lustre_lov(name))) {
4866 /* in case of lov EA swap, just set it
4867 * if not, it is a replay so check striping match what we
4868 * already have during req replay, declare_xattr_set()
4869 * defines striping, then create() does the work */
4870 if (fl & LU_XATTR_REPLACE) {
4871 /* free stripes, then update disk */
4872 lod_striping_free(env, lod_dt_obj(dt));
4874 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
4875 } else if (fl & LU_XATTR_SPLIT) {
4876 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
4880 rc = lod_striping_reload(env, lo, buf);
4884 if (lo->ldo_mirror_count > 1 &&
4885 layout_attr->la_valid & LA_LAYOUT_VERSION) {
4887 layout_attr->la_layout_version =
4889 data.locd_attr = layout_attr;
4890 data.locd_declare = false;
4891 data.locd_stripe_cb =
4892 lod_obj_stripe_attr_set_cb;
4893 rc = lod_obj_for_each_stripe(env, lo, th,
4898 } else if (fl & LU_XATTR_PURGE) {
4899 rc = lod_layout_purge(env, dt, buf, th);
4900 } else if (dt_object_remote(dt)) {
4901 /* This only happens during migration, see
4902 * mdd_migrate_create(), in which Master MDT will
4903 * create a remote target object, and only set
4904 * (migrating) stripe EA on the remote object,
4905 * and does not need creating each stripes. */
4906 rc = lod_sub_xattr_set(env, next, buf, name,
4908 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
4909 /* delete component(s) */
4910 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
4911 rc = lod_layout_del(env, dt, th);
4914 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
4915 * it's going to create create file with specified
4916 * component(s), the striping must have not being
4917 * cached in this case;
4919 * Otherwise, it's going to add/change component(s) to
4920 * an existing file, the striping must have been cached
4923 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
4924 !strcmp(name, XATTR_NAME_LOV),
4925 !lod_dt_obj(dt)->ldo_comp_cached));
4927 rc = lod_striped_create(env, dt, NULL, NULL, th);
4931 if (fl & LU_XATTR_MERGE && lo->ldo_mirror_count > 1 &&
4932 layout_attr->la_valid & LA_LAYOUT_VERSION) {
4933 /* mirror merge exec phase */
4934 layout_attr->la_layout_version =
4936 data.locd_attr = layout_attr;
4937 data.locd_declare = false;
4938 data.locd_stripe_cb =
4939 lod_obj_stripe_attr_set_cb;
4940 rc = lod_obj_for_each_stripe(env, lo, th,
4947 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
4948 rc = lod_replace_parent_fid(env, dt, buf, th, false);
4953 /* then all other xattr */
4954 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
4960 * Implementation of dt_object_operations::do_declare_xattr_del.
4962 * \see dt_object_operations::do_declare_xattr_del() in the API description
4965 static int lod_declare_xattr_del(const struct lu_env *env,
4966 struct dt_object *dt, const char *name,
4969 struct lod_object *lo = lod_dt_obj(dt);
4970 struct dt_object *next = dt_object_child(dt);
4975 rc = lod_sub_declare_xattr_del(env, next, name, th);
4979 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4982 /* NB: don't delete stripe LMV, because when we do this, normally we
4983 * will remove stripes, besides, if directory LMV is corrupt, this will
4984 * prevent deleting its LMV and fixing it (via LFSCK).
4986 if (!strcmp(name, XATTR_NAME_LMV))
4989 rc = lod_striping_load(env, lo);
4993 if (lo->ldo_dir_stripe_count == 0)
4996 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4997 struct dt_object *dto = lo->ldo_stripe[i];
5002 if (!dt_object_exists(dto))
5005 rc = lod_sub_declare_xattr_del(env, dto, name, th);
5014 * Implementation of dt_object_operations::do_xattr_del.
5016 * If EA storing a regular striping is being deleted, then release
5017 * all the references to the stripe objects in core.
5019 * \see dt_object_operations::do_xattr_del() in the API description for details.
5021 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
5022 const char *name, struct thandle *th)
5028 if (!strcmp(name, XATTR_NAME_LOV) || !strcmp(name, XATTR_NAME_LMV))
5029 lod_striping_free(env, lod_dt_obj(dt));
5031 rc = lod_xattr_del_internal(env, dt, name, th);
5037 * Implementation of dt_object_operations::do_xattr_list.
5039 * \see dt_object_operations::do_xattr_list() in the API description
5042 static int lod_xattr_list(const struct lu_env *env,
5043 struct dt_object *dt, const struct lu_buf *buf)
5045 return dt_xattr_list(env, dt_object_child(dt), buf);
5048 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
5050 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
5054 * Copy OST list from layout provided by user.
5056 * \param[in] lod_comp layout_component to be filled
5057 * \param[in] v3 LOV EA V3 user data
5059 * \retval 0 on success
5060 * \retval negative if failed
5062 int lod_comp_copy_ost_lists(struct lod_layout_component *lod_comp,
5063 struct lov_user_md_v3 *v3)
5069 if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
5070 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
5072 if (lod_comp->llc_ostlist.op_array) {
5073 if (lod_comp->llc_ostlist.op_size >=
5074 v3->lmm_stripe_count * sizeof(__u32)) {
5075 lod_comp->llc_ostlist.op_count =
5076 v3->lmm_stripe_count;
5079 OBD_FREE(lod_comp->llc_ostlist.op_array,
5080 lod_comp->llc_ostlist.op_size);
5083 /* copy ost list from lmm */
5084 lod_comp->llc_ostlist.op_count = v3->lmm_stripe_count;
5085 lod_comp->llc_ostlist.op_size = v3->lmm_stripe_count * sizeof(__u32);
5086 OBD_ALLOC(lod_comp->llc_ostlist.op_array,
5087 lod_comp->llc_ostlist.op_size);
5088 if (!lod_comp->llc_ostlist.op_array)
5091 for (j = 0; j < v3->lmm_stripe_count; j++) {
5092 lod_comp->llc_ostlist.op_array[j] =
5093 v3->lmm_objects[j].l_ost_idx;
5101 * Get default striping.
5103 * \param[in] env execution environment
5104 * \param[in] lo object
5105 * \param[out] lds default striping
5107 * \retval 0 on success
5108 * \retval negative if failed
5110 static int lod_get_default_lov_striping(const struct lu_env *env,
5111 struct lod_object *lo,
5112 struct lod_default_striping *lds,
5113 struct dt_allocation_hint *ah)
5115 struct lod_thread_info *info = lod_env_info(env);
5116 struct lov_user_md_v1 *v1 = NULL;
5117 struct lov_user_md_v3 *v3 = NULL;
5118 struct lov_comp_md_v1 *comp_v1 = NULL;
5126 rc = lod_get_lov_ea(env, lo);
5130 if (rc < (typeof(rc))sizeof(struct lov_user_md))
5133 v1 = info->lti_ea_store;
5134 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
5135 lustre_swab_lov_user_md_v1(v1);
5136 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
5137 v3 = (struct lov_user_md_v3 *)v1;
5138 lustre_swab_lov_user_md_v3(v3);
5139 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_SPECIFIC)) {
5140 v3 = (struct lov_user_md_v3 *)v1;
5141 lustre_swab_lov_user_md_v3(v3);
5142 lustre_swab_lov_user_md_objects(v3->lmm_objects,
5143 v3->lmm_stripe_count);
5144 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1) ||
5145 v1->lmm_magic == __swab32(LOV_USER_MAGIC_SEL)) {
5146 comp_v1 = (struct lov_comp_md_v1 *)v1;
5147 lustre_swab_lov_comp_md_v1(comp_v1);
5150 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
5151 v1->lmm_magic != LOV_MAGIC_COMP_V1 &&
5152 v1->lmm_magic != LOV_MAGIC_SEL &&
5153 v1->lmm_magic != LOV_USER_MAGIC_SPECIFIC)
5156 if ((v1->lmm_magic == LOV_MAGIC_COMP_V1 ||
5157 v1->lmm_magic == LOV_MAGIC_SEL) &&
5158 !(ah && ah->dah_append_stripes)) {
5159 comp_v1 = (struct lov_comp_md_v1 *)v1;
5160 comp_cnt = comp_v1->lcm_entry_count;
5163 mirror_cnt = comp_v1->lcm_mirror_count + 1;
5171 /* realloc default comp entries if necessary */
5172 rc = lod_def_striping_comp_resize(lds, comp_cnt);
5176 lds->lds_def_comp_cnt = comp_cnt;
5177 lds->lds_def_striping_is_composite = composite;
5178 lds->lds_def_mirror_cnt = mirror_cnt;
5180 for (i = 0; i < comp_cnt; i++) {
5181 struct lod_layout_component *lod_comp;
5184 lod_comp = &lds->lds_def_comp_entries[i];
5186 * reset lod_comp values, llc_stripes is always NULL in
5187 * the default striping template, llc_pool will be reset
5190 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
5193 v1 = (struct lov_user_md *)((char *)comp_v1 +
5194 comp_v1->lcm_entries[i].lcme_offset);
5195 lod_comp->llc_extent =
5196 comp_v1->lcm_entries[i].lcme_extent;
5197 /* We only inherit certain flags from the layout */
5198 lod_comp->llc_flags =
5199 comp_v1->lcm_entries[i].lcme_flags &
5200 LCME_TEMPLATE_FLAGS;
5203 if (!lov_pattern_supported(v1->lmm_pattern) &&
5204 !(v1->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
5205 lod_free_def_comp_entries(lds);
5209 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d stripe_offset=%d append_stripes=%d\n",
5210 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
5211 (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
5212 (int)v1->lmm_stripe_offset,
5213 ah ? ah->dah_append_stripes : 0);
5215 if (ah && ah->dah_append_stripes)
5216 lod_comp->llc_stripe_count = ah->dah_append_stripes;
5218 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
5219 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
5220 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
5221 lod_comp->llc_pattern = v1->lmm_pattern;
5224 if (ah && ah->dah_append_pool && ah->dah_append_pool[0]) {
5225 pool = ah->dah_append_pool;
5226 } else if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
5227 /* XXX: sanity check here */
5228 v3 = (struct lov_user_md_v3 *) v1;
5229 if (v3->lmm_pool_name[0] != '\0')
5230 pool = v3->lmm_pool_name;
5232 lod_set_def_pool(lds, i, pool);
5233 if (v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
5234 v3 = (struct lov_user_md_v3 *)v1;
5235 rc = lod_comp_copy_ost_lists(lod_comp, v3);
5238 } else if (lod_comp->llc_ostlist.op_array &&
5239 lod_comp->llc_ostlist.op_count) {
5240 for (j = 0; j < lod_comp->llc_ostlist.op_count; j++)
5241 lod_comp->llc_ostlist.op_array[j] = -1;
5242 lod_comp->llc_ostlist.op_count = 0;
5246 lds->lds_def_striping_set = 1;
5251 * Get default directory striping.
5253 * \param[in] env execution environment
5254 * \param[in] lo object
5255 * \param[out] lds default striping
5257 * \retval 0 on success
5258 * \retval negative if failed
5260 static int lod_get_default_lmv_striping(const struct lu_env *env,
5261 struct lod_object *lo,
5262 struct lod_default_striping *lds)
5264 struct lmv_user_md *lmu;
5267 lds->lds_dir_def_striping_set = 0;
5269 rc = lod_get_default_lmv_ea(env, lo);
5273 if (rc >= (int)sizeof(*lmu)) {
5274 struct lod_thread_info *info = lod_env_info(env);
5276 lmu = info->lti_ea_store;
5278 lds->lds_dir_def_stripe_count =
5279 le32_to_cpu(lmu->lum_stripe_count);
5280 lds->lds_dir_def_stripe_offset =
5281 le32_to_cpu(lmu->lum_stripe_offset);
5282 lds->lds_dir_def_hash_type =
5283 le32_to_cpu(lmu->lum_hash_type);
5284 lds->lds_dir_def_max_inherit = lmu->lum_max_inherit;
5285 lds->lds_dir_def_max_inherit_rr = lmu->lum_max_inherit_rr;
5286 lds->lds_dir_def_striping_set = 1;
5293 * Get default striping in the object.
5295 * Get object default striping and default directory striping.
5297 * \param[in] env execution environment
5298 * \param[in] lo object
5299 * \param[out] lds default striping
5301 * \retval 0 on success
5302 * \retval negative if failed
5304 static int lod_get_default_striping(const struct lu_env *env,
5305 struct lod_object *lo,
5306 struct lod_default_striping *lds)
5310 rc = lod_get_default_lov_striping(env, lo, lds, NULL);
5311 rc1 = lod_get_default_lmv_striping(env, lo, lds);
5312 if (rc == 0 && rc1 < 0)
5319 * Apply default striping on object.
5321 * If object striping pattern is not set, set to the one in default striping.
5322 * The default striping is from parent or fs.
5324 * \param[in] lo new object
5325 * \param[in] lds default striping
5326 * \param[in] mode new object's mode
5328 static void lod_striping_from_default(struct lod_object *lo,
5329 const struct lod_default_striping *lds,
5332 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5335 if (lds->lds_def_striping_set && S_ISREG(mode)) {
5336 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
5338 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
5339 lds->lds_def_comp_cnt);
5343 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
5344 if (lds->lds_def_mirror_cnt > 1)
5345 lo->ldo_flr_state = LCM_FL_RDONLY;
5347 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5348 struct lod_layout_component *obj_comp =
5349 &lo->ldo_comp_entries[i];
5350 struct lod_layout_component *def_comp =
5351 &lds->lds_def_comp_entries[i];
5353 CDEBUG(D_LAYOUT, "Inherit from default: flags=%#x "
5354 "size=%hu nr=%u offset=%u pattern=%#x pool=%s\n",
5355 def_comp->llc_flags,
5356 def_comp->llc_stripe_size,
5357 def_comp->llc_stripe_count,
5358 def_comp->llc_stripe_offset,
5359 def_comp->llc_pattern,
5360 def_comp->llc_pool ?: "");
5362 *obj_comp = *def_comp;
5363 if (def_comp->llc_pool != NULL) {
5364 /* pointer was copied from def_comp */
5365 obj_comp->llc_pool = NULL;
5366 lod_obj_set_pool(lo, i, def_comp->llc_pool);
5370 if (def_comp->llc_ostlist.op_array &&
5371 def_comp->llc_ostlist.op_count) {
5372 OBD_ALLOC(obj_comp->llc_ostlist.op_array,
5373 obj_comp->llc_ostlist.op_size);
5374 if (!obj_comp->llc_ostlist.op_array)
5376 memcpy(obj_comp->llc_ostlist.op_array,
5377 def_comp->llc_ostlist.op_array,
5378 obj_comp->llc_ostlist.op_size);
5379 } else if (def_comp->llc_ostlist.op_array) {
5380 obj_comp->llc_ostlist.op_array = NULL;
5384 * Don't initialize these fields for plain layout
5385 * (v1/v3) here, they are inherited in the order of
5386 * 'parent' -> 'fs default (root)' -> 'global default
5387 * values for stripe_count & stripe_size'.
5389 * see lod_ah_init().
5391 if (!lo->ldo_is_composite)
5394 lod_adjust_stripe_info(obj_comp, desc, 0);
5396 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
5397 if (lo->ldo_dir_stripe_count == 0)
5398 lo->ldo_dir_stripe_count =
5399 lds->lds_dir_def_stripe_count;
5400 if (lo->ldo_dir_stripe_offset == -1)
5401 lo->ldo_dir_stripe_offset =
5402 lds->lds_dir_def_stripe_offset;
5403 if (lo->ldo_dir_hash_type == 0)
5404 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
5406 CDEBUG(D_LAYOUT, "striping from default dir: count:%hu, "
5407 "offset:%u, hash_type:%u\n",
5408 lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
5409 lo->ldo_dir_hash_type);
5413 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root,
5416 struct lod_layout_component *lod_comp;
5418 if (lo->ldo_comp_cnt == 0)
5421 if (lo->ldo_is_composite)
5424 lod_comp = &lo->ldo_comp_entries[0];
5426 if (lod_comp->llc_stripe_count <= 0 ||
5427 lod_comp->llc_stripe_size <= 0)
5430 if (from_root && (lod_comp->llc_pool == NULL ||
5431 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
5434 if (append_pool && append_pool[0])
5441 * Implementation of dt_object_operations::do_ah_init.
5443 * This method is used to make a decision on the striping configuration for the
5444 * object being created. It can be taken from the \a parent object if it exists,
5445 * or filesystem's default. The resulting configuration (number of stripes,
5446 * stripe size/offset, pool name, etc) is stored in the object itself and will
5447 * be used by the methods like ->doo_declare_create().
5449 * \see dt_object_operations::do_ah_init() in the API description for details.
5451 static void lod_ah_init(const struct lu_env *env,
5452 struct dt_allocation_hint *ah,
5453 struct dt_object *parent,
5454 struct dt_object *child,
5457 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
5458 struct lod_thread_info *info = lod_env_info(env);
5459 struct lod_default_striping *lds = lod_lds_buf_get(env);
5460 struct dt_object *nextp = NULL;
5461 struct dt_object *nextc;
5462 struct lod_object *lp = NULL;
5463 struct lod_object *lc;
5464 struct lov_desc *desc;
5465 struct lod_layout_component *lod_comp;
5471 if (ah->dah_append_stripes == -1)
5472 ah->dah_append_stripes =
5473 d->lod_ost_descs.ltd_lov_desc.ld_tgt_count;
5475 if (likely(parent)) {
5476 nextp = dt_object_child(parent);
5477 lp = lod_dt_obj(parent);
5480 nextc = dt_object_child(child);
5481 lc = lod_dt_obj(child);
5483 LASSERT(!lod_obj_is_striped(child));
5484 /* default layout template may have been set on the regular file
5485 * when this is called from mdd_create_data() */
5486 if (S_ISREG(child_mode))
5487 lod_free_comp_entries(lc);
5489 if (!dt_object_exists(nextc))
5490 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
5492 if (S_ISDIR(child_mode)) {
5493 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
5495 /* other default values are 0 */
5496 lc->ldo_dir_stripe_offset = -1;
5498 /* no default striping configuration is needed for
5501 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5502 le32_to_cpu(lum1->lum_magic) == LMV_MAGIC_FOREIGN) {
5503 lc->ldo_dir_is_foreign = true;
5504 /* keep stripe_count 0 and stripe_offset -1 */
5505 CDEBUG(D_INFO, "no default striping for foreign dir\n");
5510 * If parent object is not root directory,
5511 * then get default striping from parent object.
5513 if (likely(lp != NULL)) {
5514 lod_get_default_striping(env, lp, lds);
5516 /* inherit default striping except ROOT */
5517 if ((lds->lds_def_striping_set ||
5518 lds->lds_dir_def_striping_set) &&
5519 !fid_is_root(lod_object_fid(lp)))
5520 lc->ldo_def_striping = lds;
5523 /* It should always honour the specified stripes */
5524 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
5525 * will have old magic. In this case, we should ignore the
5526 * stripe count and try to create dir by default stripe.
5528 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5529 (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
5530 le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
5531 lc->ldo_dir_stripe_count =
5532 le32_to_cpu(lum1->lum_stripe_count);
5533 lc->ldo_dir_stripe_offset =
5534 le32_to_cpu(lum1->lum_stripe_offset);
5535 lc->ldo_dir_hash_type =
5536 le32_to_cpu(lum1->lum_hash_type);
5538 "set dirstripe: count %hu, offset %d, hash %u\n",
5539 lc->ldo_dir_stripe_count,
5540 (int)lc->ldo_dir_stripe_offset,
5541 lc->ldo_dir_hash_type);
5543 /* transfer defaults LMV to new directory */
5544 lod_striping_from_default(lc, lds, child_mode);
5546 /* set count 0 to create normal directory */
5547 if (lc->ldo_dir_stripe_count == 1)
5548 lc->ldo_dir_stripe_count = 0;
5551 /* shrink the stripe_count to the avaible MDT count */
5552 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
5553 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)) {
5554 lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
5555 if (lc->ldo_dir_stripe_count == 1)
5556 lc->ldo_dir_stripe_count = 0;
5559 if (!(lc->ldo_dir_hash_type & LMV_HASH_TYPE_MASK))
5560 lc->ldo_dir_hash_type |=
5561 d->lod_mdt_descs.ltd_lmv_desc.ld_pattern;
5563 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
5564 lc->ldo_dir_stripe_count,
5565 (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
5570 /* child object regular file*/
5572 if (!lod_object_will_be_striped(S_ISREG(child_mode),
5573 lu_object_fid(&child->do_lu)))
5576 /* If object is going to be striped over OSTs, transfer default
5577 * striping information to the child, so that we can use it
5578 * during declaration and creation.
5580 * Try from the parent first.
5582 if (likely(lp != NULL)) {
5583 rc = lod_get_default_lov_striping(env, lp, lds, ah);
5585 lod_striping_from_default(lc, lds, child_mode);
5588 /* Initialize lod_device::lod_md_root object reference */
5589 if (d->lod_md_root == NULL) {
5590 struct dt_object *root;
5591 struct lod_object *lroot;
5593 lu_root_fid(&info->lti_fid);
5594 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
5595 if (!IS_ERR(root)) {
5596 lroot = lod_dt_obj(root);
5598 spin_lock(&d->lod_lock);
5599 if (d->lod_md_root != NULL)
5600 dt_object_put(env, &d->lod_md_root->ldo_obj);
5601 d->lod_md_root = lroot;
5602 spin_unlock(&d->lod_lock);
5606 /* try inherit layout from the root object (fs default) when:
5607 * - parent does not have default layout; or
5608 * - parent has plain(v1/v3) default layout, and some attributes
5609 * are not specified in the default layout;
5611 if (d->lod_md_root != NULL &&
5612 lod_need_inherit_more(lc, true, ah->dah_append_pool)) {
5613 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds,
5617 if (lc->ldo_comp_cnt == 0) {
5618 lod_striping_from_default(lc, lds, child_mode);
5619 } else if (!lds->lds_def_striping_is_composite) {
5620 struct lod_layout_component *def_comp;
5622 LASSERT(!lc->ldo_is_composite);
5623 lod_comp = &lc->ldo_comp_entries[0];
5624 def_comp = &lds->lds_def_comp_entries[0];
5626 if (lod_comp->llc_stripe_count <= 0)
5627 lod_comp->llc_stripe_count =
5628 def_comp->llc_stripe_count;
5629 if (lod_comp->llc_stripe_size <= 0)
5630 lod_comp->llc_stripe_size =
5631 def_comp->llc_stripe_size;
5632 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT &&
5633 (!lod_comp->llc_pool || !lod_comp->llc_pool[0]))
5634 lod_comp->llc_stripe_offset =
5635 def_comp->llc_stripe_offset;
5636 if (lod_comp->llc_pool == NULL)
5637 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
5642 * fs default striping may not be explicitly set, or historically set
5643 * in config log, use them.
5645 if (lod_need_inherit_more(lc, false, ah->dah_append_pool)) {
5646 if (lc->ldo_comp_cnt == 0) {
5647 rc = lod_alloc_comp_entries(lc, 0, 1);
5649 /* fail to allocate memory, will create a
5650 * non-striped file. */
5652 lc->ldo_is_composite = 0;
5653 lod_comp = &lc->ldo_comp_entries[0];
5654 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
5656 LASSERT(!lc->ldo_is_composite);
5657 lod_comp = &lc->ldo_comp_entries[0];
5658 desc = &d->lod_ost_descs.ltd_lov_desc;
5659 lod_adjust_stripe_info(lod_comp, desc, ah->dah_append_stripes);
5660 if (ah->dah_append_pool && ah->dah_append_pool[0])
5661 lod_obj_set_pool(lc, 0, ah->dah_append_pool);
5668 * Size initialization on late striping.
5670 * Propagate the size of a truncated object to a deferred striping.
5671 * This function handles a special case when truncate was done on a
5672 * non-striped object and now while the striping is being created
5673 * we can't lose that size, so we have to propagate it to the stripes
5676 * \param[in] env execution environment
5677 * \param[in] dt object
5678 * \param[in] th transaction handle
5680 * \retval 0 on success
5681 * \retval negative if failed
5683 static int lod_declare_init_size(const struct lu_env *env,
5684 struct dt_object *dt, struct thandle *th)
5686 struct dt_object *next = dt_object_child(dt);
5687 struct lod_object *lo = lod_dt_obj(dt);
5688 struct dt_object **objects = NULL;
5689 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
5690 uint64_t size, offs;
5691 int i, rc, stripe, stripe_count = 0, stripe_size = 0;
5692 struct lu_extent size_ext;
5695 if (!lod_obj_is_striped(dt))
5698 rc = dt_attr_get(env, next, attr);
5699 LASSERT(attr->la_valid & LA_SIZE);
5703 size = attr->la_size;
5707 size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
5708 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5709 struct lod_layout_component *lod_comp;
5710 struct lu_extent *extent;
5712 lod_comp = &lo->ldo_comp_entries[i];
5714 if (lod_comp->llc_stripe == NULL)
5717 extent = &lod_comp->llc_extent;
5718 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
5719 if (!lo->ldo_is_composite ||
5720 lu_extent_is_overlapped(extent, &size_ext)) {
5721 objects = lod_comp->llc_stripe;
5722 stripe_count = lod_comp->llc_stripe_count;
5723 stripe_size = lod_comp->llc_stripe_size;
5726 if (stripe_count == 0)
5729 LASSERT(objects != NULL && stripe_size != 0);
5730 do_div(size, stripe_size);
5731 stripe = do_div(size, stripe_count);
5732 LASSERT(objects[stripe] != NULL);
5734 size = size * stripe_size;
5735 offs = attr->la_size;
5736 size += do_div(offs, stripe_size);
5738 attr->la_valid = LA_SIZE;
5739 attr->la_size = size;
5741 rc = lod_sub_declare_attr_set(env, objects[stripe],
5750 * Declare creation of striped object.
5752 * The function declares creation stripes for a regular object. The function
5753 * also declares whether the stripes will be created with non-zero size if
5754 * previously size was set non-zero on the master object. If object \a dt is
5755 * not local, then only fully defined striping can be applied in \a lovea.
5756 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
5759 * \param[in] env execution environment
5760 * \param[in] dt object
5761 * \param[in] attr attributes the stripes will be created with
5762 * \param[in] lovea a buffer containing striping description
5763 * \param[in] th transaction handle
5765 * \retval 0 on success
5766 * \retval negative if failed
5768 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
5769 struct lu_attr *attr,
5770 const struct lu_buf *lovea, struct thandle *th)
5772 struct lod_thread_info *info = lod_env_info(env);
5773 struct dt_object *next = dt_object_child(dt);
5774 struct lod_object *lo = lod_dt_obj(dt);
5778 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
5779 GOTO(out, rc = -ENOMEM);
5781 if (!dt_object_remote(next)) {
5782 /* choose OST and generate appropriate objects */
5783 rc = lod_prepare_create(env, lo, attr, lovea, th);
5788 * declare storage for striping data
5790 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5792 /* LOD can not choose OST objects for remote objects, i.e.
5793 * stripes must be ready before that. Right now, it can only
5794 * happen during migrate, i.e. migrate process needs to create
5795 * remote regular file (mdd_migrate_create), then the migrate
5796 * process will provide stripeEA. */
5797 LASSERT(lovea != NULL);
5798 info->lti_buf = *lovea;
5801 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
5802 XATTR_NAME_LOV, 0, th);
5807 * if striping is created with local object's size > 0,
5808 * we have to propagate this size to specific object
5809 * the case is possible only when local object was created previously
5811 if (dt_object_exists(next))
5812 rc = lod_declare_init_size(env, dt, th);
5815 /* failed to create striping or to set initial size, let's reset
5816 * config so that others don't get confused */
5818 lod_striping_free(env, lo);
5824 * Whether subdirectories under \a dt should be created on MDTs by space QoS
5826 * If LMV_HASH_FLAG_SPACE is set on directory default layout, its subdirectories
5827 * should be created on MDT by space QoS.
5829 * \param[in] env execution environment
5830 * \param[in] dev lu device
5831 * \param[in] dt object
5833 * \retval 1 if directory should create subdir by space usage
5835 * \retval -ev if failed
5837 static inline int dt_object_qos_mkdir(const struct lu_env *env,
5838 struct lu_device *dev,
5839 struct dt_object *dt)
5841 struct lod_thread_info *info = lod_env_info(env);
5842 struct lu_object *obj;
5843 struct lod_object *lo;
5844 struct lmv_user_md *lmu;
5847 obj = lu_object_find_slice(env, dev, lu_object_fid(&dt->do_lu), NULL);
5849 return PTR_ERR(obj);
5851 lo = lu2lod_obj(obj);
5853 rc = lod_get_default_lmv_ea(env, lo);
5854 dt_object_put(env, dt);
5858 if (rc < (int)sizeof(*lmu))
5861 lmu = info->lti_ea_store;
5862 return le32_to_cpu(lmu->lum_stripe_offset) == LMV_OFFSET_DEFAULT;
5866 * Implementation of dt_object_operations::do_declare_create.
5868 * The method declares creation of a new object. If the object will be striped,
5869 * then helper functions are called to find FIDs for the stripes, declare
5870 * creation of the stripes and declare initialization of the striping
5871 * information to be stored in the master object.
5873 * \see dt_object_operations::do_declare_create() in the API description
5876 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
5877 struct lu_attr *attr,
5878 struct dt_allocation_hint *hint,
5879 struct dt_object_format *dof, struct thandle *th)
5881 struct dt_object *next = dt_object_child(dt);
5882 struct lod_object *lo = lod_dt_obj(dt);
5891 * first of all, we declare creation of local object
5893 rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
5898 * it's lod_ah_init() that has decided the object will be striped
5900 if (dof->dof_type == DFT_REGULAR) {
5901 /* callers don't want stripes */
5902 /* XXX: all tricky interactions with ->ah_make_hint() decided
5903 * to use striping, then ->declare_create() behaving differently
5904 * should be cleaned */
5905 if (dof->u.dof_reg.striped != 0)
5906 rc = lod_declare_striped_create(env, dt, attr,
5908 } else if (dof->dof_type == DFT_DIR) {
5909 struct seq_server_site *ss;
5910 struct lu_buf buf = { NULL };
5911 struct lu_buf *lmu = NULL;
5913 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
5915 /* If the parent has default stripeEA, and client
5916 * did not find it before sending create request,
5917 * then MDT will return -EREMOTE, and client will
5918 * retrieve the default stripeEA and re-create the
5921 * Note: if dah_eadata != NULL, it means creating the
5922 * striped directory with specified stripeEA, then it
5923 * should ignore the default stripeEA */
5924 if (hint != NULL && hint->dah_eadata == NULL) {
5925 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
5926 GOTO(out, rc = -EREMOTE);
5928 if (lo->ldo_dir_stripe_offset != LMV_OFFSET_DEFAULT &&
5929 lo->ldo_dir_stripe_offset != ss->ss_node_id) {
5930 struct lod_device *lod;
5931 struct lu_tgt_desc *mdt = NULL;
5932 bool found_mdt = false;
5934 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5935 lod_foreach_mdt(lod, mdt) {
5936 if (mdt->ltd_index ==
5937 lo->ldo_dir_stripe_offset) {
5943 /* If the MDT indicated by stripe_offset can be
5944 * found, then tell client to resend the create
5945 * request to the correct MDT, otherwise return
5946 * error to client */
5948 GOTO(out, rc = -EREMOTE);
5950 GOTO(out, rc = -EINVAL);
5952 } else if (hint && hint->dah_eadata) {
5954 lmu->lb_buf = (void *)hint->dah_eadata;
5955 lmu->lb_len = hint->dah_eadata_len;
5958 rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
5962 /* failed to create striping or to set initial size, let's reset
5963 * config so that others don't get confused */
5965 lod_striping_free(env, lo);
5970 * Generate component ID for new created component.
5972 * \param[in] lo LOD object
5973 * \param[in] comp_idx index of ldo_comp_entries
5975 * \retval component ID on success
5976 * \retval LCME_ID_INVAL on failure
5978 static __u32 lod_gen_component_id(struct lod_object *lo,
5979 int mirror_id, int comp_idx)
5981 struct lod_layout_component *lod_comp;
5982 __u32 id, start, end;
5985 LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
5987 lod_obj_inc_layout_gen(lo);
5988 id = lo->ldo_layout_gen;
5989 if (likely(id <= SEQ_ID_MAX))
5990 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
5992 /* Layout generation wraps, need to check collisions. */
5993 start = id & SEQ_ID_MASK;
5996 for (id = start; id <= end; id++) {
5997 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5998 lod_comp = &lo->ldo_comp_entries[i];
5999 if (pflr_id(mirror_id, id) == lod_comp->llc_id)
6002 /* Found the ununsed ID */
6003 if (i == lo->ldo_comp_cnt)
6004 RETURN(pflr_id(mirror_id, id));
6006 if (end == LCME_ID_MAX) {
6008 end = min(lo->ldo_layout_gen & LCME_ID_MASK,
6009 (__u32)(LCME_ID_MAX - 1));
6013 RETURN(LCME_ID_INVAL);
6017 * Creation of a striped regular object.
6019 * The function is called to create the stripe objects for a regular
6020 * striped file. This can happen at the initial object creation or
6021 * when the caller asks LOD to do so using ->do_xattr_set() method
6022 * (so called late striping). Notice all the information are already
6023 * prepared in the form of the list of objects (ldo_stripe field).
6024 * This is done during declare phase.
6026 * \param[in] env execution environment
6027 * \param[in] dt object
6028 * \param[in] attr attributes the stripes will be created with
6029 * \param[in] dof format of stripes (see OSD API description)
6030 * \param[in] th transaction handle
6032 * \retval 0 on success
6033 * \retval negative if failed
6035 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
6036 struct lu_attr *attr, struct dt_object_format *dof,
6039 struct lod_layout_component *lod_comp;
6040 struct lod_object *lo = lod_dt_obj(dt);
6045 mutex_lock(&lo->ldo_layout_mutex);
6047 LASSERT((lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL) ||
6048 lo->ldo_is_foreign);
6050 mirror_id = 0; /* non-flr file's mirror_id is 0 */
6051 if (lo->ldo_mirror_count > 1) {
6052 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6053 lod_comp = &lo->ldo_comp_entries[i];
6054 if (lod_comp->llc_id != LCME_ID_INVAL &&
6055 mirror_id_of(lod_comp->llc_id) > mirror_id)
6056 mirror_id = mirror_id_of(lod_comp->llc_id);
6060 /* create all underlying objects */
6061 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6062 lod_comp = &lo->ldo_comp_entries[i];
6064 if (lod_comp->llc_id == LCME_ID_INVAL) {
6065 /* only the component of FLR layout with more than 1
6066 * mirror has mirror ID in its component ID.
6068 if (lod_comp->llc_extent.e_start == 0 &&
6069 lo->ldo_mirror_count > 1)
6072 lod_comp->llc_id = lod_gen_component_id(lo,
6074 if (lod_comp->llc_id == LCME_ID_INVAL)
6075 GOTO(out, rc = -ERANGE);
6078 if (lod_comp_inited(lod_comp))
6081 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
6082 lod_comp_set_init(lod_comp);
6084 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
6085 lod_comp_set_init(lod_comp);
6087 if (lod_comp->llc_stripe == NULL)
6090 LASSERT(lod_comp->llc_stripe_count);
6091 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6092 struct dt_object *object = lod_comp->llc_stripe[j];
6093 LASSERT(object != NULL);
6094 rc = lod_sub_create(env, object, attr, NULL, dof, th);
6098 lod_comp_set_init(lod_comp);
6101 rc = lod_fill_mirrors(lo);
6105 lo->ldo_comp_cached = 1;
6107 rc = lod_generate_and_set_lovea(env, lo, th);
6111 mutex_unlock(&lo->ldo_layout_mutex);
6116 lod_striping_free_nolock(env, lo);
6117 mutex_unlock(&lo->ldo_layout_mutex);
6122 static inline bool lod_obj_is_dom(struct dt_object *dt)
6124 struct lod_object *lo = lod_dt_obj(dt);
6126 if (!dt_object_exists(dt_object_child(dt)))
6129 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
6132 if (!lo->ldo_comp_cnt)
6135 return (lov_pattern(lo->ldo_comp_entries[0].llc_pattern) ==
6140 * Implementation of dt_object_operations::do_create.
6142 * If any of preceeding methods (like ->do_declare_create(),
6143 * ->do_ah_init(), etc) chose to create a striped object,
6144 * then this method will create the master and the stripes.
6146 * \see dt_object_operations::do_create() in the API description for details.
6148 static int lod_create(const struct lu_env *env, struct dt_object *dt,
6149 struct lu_attr *attr, struct dt_allocation_hint *hint,
6150 struct dt_object_format *dof, struct thandle *th)
6155 /* create local object */
6156 rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
6160 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
6161 (lod_obj_is_striped(dt) || lod_obj_is_dom(dt)) &&
6162 dof->u.dof_reg.striped != 0) {
6163 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
6164 rc = lod_striped_create(env, dt, attr, dof, th);
6171 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
6172 struct dt_object *dt, struct thandle *th,
6173 int comp_idx, int stripe_idx,
6174 struct lod_obj_stripe_cb_data *data)
6176 if (data->locd_declare)
6177 return lod_sub_declare_destroy(env, dt, th);
6179 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
6180 stripe_idx == cfs_fail_val)
6181 return lod_sub_destroy(env, dt, th);
6187 * Implementation of dt_object_operations::do_declare_destroy.
6189 * If the object is a striped directory, then the function declares reference
6190 * removal from the master object (this is an index) to the stripes and declares
6191 * destroy of all the stripes. In all the cases, it declares an intention to
6192 * destroy the object itself.
6194 * \see dt_object_operations::do_declare_destroy() in the API description
6197 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
6200 struct dt_object *next = dt_object_child(dt);
6201 struct lod_object *lo = lod_dt_obj(dt);
6202 struct lod_thread_info *info = lod_env_info(env);
6203 struct dt_object *stripe;
6204 char *stripe_name = info->lti_key;
6210 * load striping information, notice we don't do this when object
6211 * is being initialized as we don't need this information till
6212 * few specific cases like destroy, chown
6214 rc = lod_striping_load(env, lo);
6218 /* declare destroy for all underlying objects */
6219 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6220 rc = next->do_ops->do_index_try(env, next,
6221 &dt_directory_features);
6225 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6226 stripe = lo->ldo_stripe[i];
6230 rc = lod_sub_declare_ref_del(env, next, th);
6234 snprintf(stripe_name, sizeof(info->lti_key),
6236 PFID(lu_object_fid(&stripe->do_lu)), i);
6237 rc = lod_sub_declare_delete(env, next,
6238 (const struct dt_key *)stripe_name, th);
6245 * we declare destroy for the local object
6247 rc = lod_sub_declare_destroy(env, next, th);
6251 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
6252 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
6255 if (!lod_obj_is_striped(dt))
6258 /* declare destroy all striped objects */
6259 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6260 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6261 stripe = lo->ldo_stripe[i];
6265 if (!dt_object_exists(stripe))
6268 rc = lod_sub_declare_ref_del(env, stripe, th);
6272 rc = lod_sub_declare_destroy(env, stripe, th);
6277 struct lod_obj_stripe_cb_data data = { { 0 } };
6279 data.locd_declare = true;
6280 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6281 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6288 * Implementation of dt_object_operations::do_destroy.
6290 * If the object is a striped directory, then the function removes references
6291 * from the master object (this is an index) to the stripes and destroys all
6292 * the stripes. In all the cases, the function destroys the object itself.
6294 * \see dt_object_operations::do_destroy() in the API description for details.
6296 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
6299 struct dt_object *next = dt_object_child(dt);
6300 struct lod_object *lo = lod_dt_obj(dt);
6301 struct lod_thread_info *info = lod_env_info(env);
6302 char *stripe_name = info->lti_key;
6303 struct dt_object *stripe;
6309 /* destroy sub-stripe of master object */
6310 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6311 rc = next->do_ops->do_index_try(env, next,
6312 &dt_directory_features);
6316 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6317 stripe = lo->ldo_stripe[i];
6321 rc = lod_sub_ref_del(env, next, th);
6325 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
6326 PFID(lu_object_fid(&stripe->do_lu)), i);
6328 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
6329 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
6330 PFID(lu_object_fid(&stripe->do_lu)));
6332 rc = lod_sub_delete(env, next,
6333 (const struct dt_key *)stripe_name, th);
6339 rc = lod_sub_destroy(env, next, th);
6343 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
6344 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
6347 if (!lod_obj_is_striped(dt))
6350 /* destroy all striped objects */
6351 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6352 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6353 stripe = lo->ldo_stripe[i];
6357 if (!dt_object_exists(stripe))
6360 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
6361 i == cfs_fail_val) {
6362 dt_write_lock(env, stripe, DT_TGT_CHILD);
6363 rc = lod_sub_ref_del(env, stripe, th);
6364 dt_write_unlock(env, stripe);
6368 rc = lod_sub_destroy(env, stripe, th);
6374 struct lod_obj_stripe_cb_data data = { { 0 } };
6376 data.locd_declare = false;
6377 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6378 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6385 * Implementation of dt_object_operations::do_declare_ref_add.
6387 * \see dt_object_operations::do_declare_ref_add() in the API description
6390 static int lod_declare_ref_add(const struct lu_env *env,
6391 struct dt_object *dt, struct thandle *th)
6393 return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
6397 * Implementation of dt_object_operations::do_ref_add.
6399 * \see dt_object_operations::do_ref_add() in the API description for details.
6401 static int lod_ref_add(const struct lu_env *env,
6402 struct dt_object *dt, struct thandle *th)
6404 return lod_sub_ref_add(env, dt_object_child(dt), th);
6408 * Implementation of dt_object_operations::do_declare_ref_del.
6410 * \see dt_object_operations::do_declare_ref_del() in the API description
6413 static int lod_declare_ref_del(const struct lu_env *env,
6414 struct dt_object *dt, struct thandle *th)
6416 return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
6420 * Implementation of dt_object_operations::do_ref_del
6422 * \see dt_object_operations::do_ref_del() in the API description for details.
6424 static int lod_ref_del(const struct lu_env *env,
6425 struct dt_object *dt, struct thandle *th)
6427 return lod_sub_ref_del(env, dt_object_child(dt), th);
6431 * Implementation of dt_object_operations::do_object_sync.
6433 * \see dt_object_operations::do_object_sync() in the API description
6436 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
6437 __u64 start, __u64 end)
6439 return dt_object_sync(env, dt_object_child(dt), start, end);
6443 * Implementation of dt_object_operations::do_object_unlock.
6445 * Used to release LDLM lock(s).
6447 * \see dt_object_operations::do_object_unlock() in the API description
6450 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
6451 struct ldlm_enqueue_info *einfo,
6452 union ldlm_policy_data *policy)
6454 struct lod_object *lo = lod_dt_obj(dt);
6455 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
6456 int slave_locks_size;
6460 if (slave_locks == NULL)
6463 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
6464 /* Note: for remote lock for single stripe dir, MDT will cancel
6465 * the lock by lockh directly */
6466 LASSERT(!dt_object_remote(dt_object_child(dt)));
6468 /* locks were unlocked in MDT layer */
6469 for (i = 0; i < slave_locks->ha_count; i++)
6470 LASSERT(!lustre_handle_is_used(&slave_locks->ha_handles[i]));
6473 * NB, ha_count may not equal to ldo_dir_stripe_count, because dir
6474 * layout may change, e.g., shrink dir layout after migration.
6476 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6477 if (lo->ldo_stripe[i])
6478 dt_invalidate(env, lo->ldo_stripe[i]);
6481 slave_locks_size = offsetof(typeof(*slave_locks),
6482 ha_handles[slave_locks->ha_count]);
6483 OBD_FREE(slave_locks, slave_locks_size);
6484 einfo->ei_cbdata = NULL;
6490 * Implementation of dt_object_operations::do_object_lock.
6492 * Used to get LDLM lock on the non-striped and striped objects.
6494 * \see dt_object_operations::do_object_lock() in the API description
6497 static int lod_object_lock(const struct lu_env *env,
6498 struct dt_object *dt,
6499 struct lustre_handle *lh,
6500 struct ldlm_enqueue_info *einfo,
6501 union ldlm_policy_data *policy)
6503 struct lod_object *lo = lod_dt_obj(dt);
6504 int slave_locks_size;
6505 struct lustre_handle_array *slave_locks = NULL;
6510 /* remote object lock */
6511 if (!einfo->ei_enq_slave) {
6512 LASSERT(dt_object_remote(dt));
6513 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
6517 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
6520 rc = lod_striping_load(env, lo);
6525 if (lo->ldo_dir_stripe_count <= 1)
6528 slave_locks_size = offsetof(typeof(*slave_locks),
6529 ha_handles[lo->ldo_dir_stripe_count]);
6530 /* Freed in lod_object_unlock */
6531 OBD_ALLOC(slave_locks, slave_locks_size);
6534 slave_locks->ha_count = lo->ldo_dir_stripe_count;
6536 /* striped directory lock */
6537 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6538 struct lustre_handle lockh;
6539 struct ldlm_res_id *res_id;
6540 struct dt_object *stripe;
6542 stripe = lo->ldo_stripe[i];
6546 res_id = &lod_env_info(env)->lti_res_id;
6547 fid_build_reg_res_name(lu_object_fid(&stripe->do_lu), res_id);
6548 einfo->ei_res_id = res_id;
6550 if (dt_object_remote(stripe)) {
6551 set_bit(i, (void *)slave_locks->ha_map);
6552 rc = dt_object_lock(env, stripe, &lockh, einfo, policy);
6554 struct ldlm_namespace *ns = einfo->ei_namespace;
6555 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
6556 ldlm_completion_callback completion = einfo->ei_cb_cp;
6557 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
6559 if (einfo->ei_mode == LCK_PW ||
6560 einfo->ei_mode == LCK_EX)
6561 dlmflags |= LDLM_FL_COS_INCOMPAT;
6563 LASSERT(ns != NULL);
6564 rc = ldlm_cli_enqueue_local(env, ns, res_id, LDLM_IBITS,
6565 policy, einfo->ei_mode,
6566 &dlmflags, blocking,
6568 NULL, 0, LVB_T_NONE,
6573 ldlm_lock_decref_and_cancel(
6574 &slave_locks->ha_handles[i],
6576 OBD_FREE(slave_locks, slave_locks_size);
6579 slave_locks->ha_handles[i] = lockh;
6581 einfo->ei_cbdata = slave_locks;
6587 * Implementation of dt_object_operations::do_invalidate.
6589 * \see dt_object_operations::do_invalidate() in the API description for details
6591 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
6593 return dt_invalidate(env, dt_object_child(dt));
6596 static int lod_declare_instantiate_components(const struct lu_env *env,
6597 struct lod_object *lo,
6601 struct lod_thread_info *info = lod_env_info(env);
6606 LASSERT(info->lti_count < lo->ldo_comp_cnt);
6608 for (i = 0; i < info->lti_count; i++) {
6609 rc = lod_qos_prep_create(env, lo, NULL, th,
6610 info->lti_comp_idx[i], reserve);
6616 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6617 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
6618 &info->lti_buf, XATTR_NAME_LOV, 0, th);
6625 * Check OSTs for an existing component for further extension
6627 * Checks if OSTs are still healthy and not out of space. Gets free space
6628 * on OSTs (relative to allocation watermark rmb_low) and compares to
6629 * the proposed new_end for this component.
6631 * Decides whether or not to extend a component on its current OSTs.
6633 * \param[in] env execution environment for this thread
6634 * \param[in] lo object we're checking
6635 * \param[in] index index of this component
6636 * \param[in] extension_size extension size for this component
6637 * \param[in] extent layout extent for requested operation
6638 * \param[in] comp_extent extension component extent
6639 * \param[in] write if this is write operation
6641 * \retval true - OK to extend on current OSTs
6642 * \retval false - do not extend on current OSTs
6644 static bool lod_sel_osts_allowed(const struct lu_env *env,
6645 struct lod_object *lo,
6646 int index, __u64 reserve,
6647 struct lu_extent *extent,
6648 struct lu_extent *comp_extent, int write)
6650 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[index];
6651 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6652 struct lod_thread_info *tinfo = lod_env_info(env);
6653 struct obd_statfs *sfs = &tinfo->lti_osfs;
6654 __u64 available = 0;
6660 LASSERT(lod_comp->llc_stripe_count != 0);
6662 lod_getref(&lod->lod_ost_descs);
6663 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
6664 int index = lod_comp->llc_ost_indices[i];
6665 struct lod_tgt_desc *ost = OST_TGT(lod, index);
6666 struct obd_statfs_info info = { 0 };
6667 int j, repeated = 0;
6671 /* Get the number of times this OST repeats in this component.
6672 * Note: inter-component repeats are not counted as this is
6673 * considered as a rare case: we try to not repeat OST in other
6674 * components if possible. */
6675 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6676 if (index != lod_comp->llc_ost_indices[j])
6679 /* already handled */
6685 if (j < lod_comp->llc_stripe_count)
6688 if (!test_bit(index, lod->lod_ost_bitmap)) {
6689 CDEBUG(D_LAYOUT, "ost %d no longer present\n", index);
6694 rc = dt_statfs_info(env, ost->ltd_tgt, sfs, &info);
6696 CDEBUG(D_LAYOUT, "statfs failed for ost %d, error %d\n",
6702 if (sfs->os_state & OS_STATFS_ENOSPC ||
6703 sfs->os_state & OS_STATFS_READONLY ||
6704 sfs->os_state & OS_STATFS_DEGRADED) {
6705 CDEBUG(D_LAYOUT, "ost %d is not availble for SEL "
6706 "extension, state %u\n", index, sfs->os_state);
6712 available = sfs->os_bavail * sfs->os_bsize;
6713 /* 'available' is relative to the allocation threshold */
6714 available -= (__u64) info.os_reserved_mb_low << 20;
6716 CDEBUG(D_LAYOUT, "ost %d lowwm: %d highwm: %d, "
6717 "%llu %% blocks available, %llu %% blocks free\n",
6718 index, info.os_reserved_mb_low, info.os_reserved_mb_high,
6719 (100ull * sfs->os_bavail) / sfs->os_blocks,
6720 (100ull * sfs->os_bfree) / sfs->os_blocks);
6722 if (reserve * repeated > available) {
6724 CDEBUG(D_LAYOUT, "low space on ost %d, available %llu "
6725 "< extension size %llu repeated %d\n", index,
6726 available, reserve, repeated);
6730 lod_putref(lod, &lod->lod_ost_descs);
6736 * Adjust extents after component removal
6738 * When we remove an extension component, we move the start of the next
6739 * component to match the start of the extension component, so no space is left
6742 * \param[in] env execution environment for this thread
6743 * \param[in] lo object
6744 * \param[in] max_comp layout component
6745 * \param[in] index index of this component
6747 * \retval 0 on success
6748 * \retval negative errno on error
6750 static void lod_sel_adjust_extents(const struct lu_env *env,
6751 struct lod_object *lo,
6752 int max_comp, int index)
6754 struct lod_layout_component *lod_comp = NULL;
6755 struct lod_layout_component *next = NULL;
6756 struct lod_layout_component *prev = NULL;
6757 __u64 new_start = 0;
6761 /* Extension space component */
6762 lod_comp = &lo->ldo_comp_entries[index];
6763 next = &lo->ldo_comp_entries[index + 1];
6764 prev = &lo->ldo_comp_entries[index - 1];
6766 LASSERT(lod_comp != NULL && prev != NULL && next != NULL);
6767 LASSERT(lod_comp->llc_flags & LCME_FL_EXTENSION);
6769 /* Previous is being removed */
6770 if (prev && prev->llc_id == LCME_ID_INVAL)
6771 new_start = prev->llc_extent.e_start;
6773 new_start = lod_comp->llc_extent.e_start;
6775 for (i = index + 1; i < max_comp; i++) {
6776 lod_comp = &lo->ldo_comp_entries[i];
6778 start = lod_comp->llc_extent.e_start;
6779 lod_comp->llc_extent.e_start = new_start;
6781 /* We only move zero length extendable components */
6782 if (!(start == lod_comp->llc_extent.e_end))
6785 LASSERT(!(lod_comp->llc_flags & LCME_FL_INIT));
6787 lod_comp->llc_extent.e_end = new_start;
6791 /* Calculate the proposed 'new end' for a component we're extending */
6792 static __u64 lod_extension_new_end(__u64 extension_size, __u64 extent_end,
6793 __u32 stripe_size, __u64 component_end,
6794 __u64 extension_end)
6798 LASSERT(extension_size != 0 && stripe_size != 0);
6800 /* Round up to extension size */
6801 if (extent_end == OBD_OBJECT_EOF) {
6802 new_end = OBD_OBJECT_EOF;
6804 /* Add at least extension_size to the previous component_end,
6805 * covering the req layout extent */
6806 new_end = max(extent_end - component_end, extension_size);
6807 new_end = roundup(new_end, extension_size);
6808 new_end += component_end;
6810 /* Component end must be min stripe size aligned */
6811 if (new_end % stripe_size) {
6812 CDEBUG(D_LAYOUT, "new component end is not aligned "
6813 "by the stripe size %u: [%llu, %llu) ext size "
6814 "%llu new end %llu, aligning\n",
6815 stripe_size, component_end, extent_end,
6816 extension_size, new_end);
6817 new_end = roundup(new_end, stripe_size);
6821 if (new_end < extent_end)
6822 new_end = OBD_OBJECT_EOF;
6825 /* Don't extend past the end of the extension component */
6826 if (new_end > extension_end)
6827 new_end = extension_end;
6833 * Calculate the exact reservation (per-OST extension_size) on the OSTs being
6834 * instantiated. It needs to be calculated in advance and taken into account at
6835 * the instantiation time, because otherwise lod_statfs_and_check() may consider
6836 * an OST as OK, but SEL needs its extension_size to fit the free space and the
6837 * OST may turn out to be low-on-space, thus inappropriate OST may be used and
6840 * \param[in] lod_comp lod component we are checking
6842 * \retval size to reserved on each OST of lod_comp's stripe.
6844 static __u64 lod_sel_stripe_reserved(struct lod_layout_component *lod_comp)
6846 /* extension_size is file level, so we must divide by stripe count to
6847 * compare it to available space on a single OST */
6848 return lod_comp->llc_stripe_size * SEL_UNIT_SIZE /
6849 lod_comp->llc_stripe_count;
6852 /* As lod_sel_handler() could be re-entered for the same component several
6853 * times, this is the data for the next call. Fields could be changed to
6854 * component indexes when needed, (e.g. if there is no need to instantiate
6855 * all the previous components up to the current position) to tell the caller
6856 * where to start over from. */
6863 * Process extent updates for a particular layout component
6865 * Handle layout updates for a particular extension space component touched by
6866 * a layout update operation. Core function of self-extending PFL feature.
6868 * In general, this function processes exactly *one* stage of an extension
6869 * operation, modifying the layout accordingly, then returns to the caller.
6870 * The caller is responsible for restarting processing with the new layout,
6871 * which may repeatedly return to this function until the extension updates
6874 * This function does one of a few things to the layout:
6875 * 1. Extends the component before the current extension space component to
6876 * allow it to accomodate the requested operation (if space/policy permit that
6877 * component to continue on its current OSTs)
6879 * 2. If extension of the existing component fails, we do one of two things:
6880 * a. If there is a component after the extension space, we remove the
6881 * extension space component, move the start of the next component down
6882 * accordingly, then notify the caller to restart processing w/the new
6884 * b. If there is no following component, we try repeating the current
6885 * component, creating a new component using the current one as a
6886 * template (keeping its stripe properties but not specific striping),
6887 * and try assigning striping for this component. If there is sufficient
6888 * free space on the OSTs chosen for this component, it is instantiated
6889 * and i/o continues there.
6891 * If there is not sufficient space on the new OSTs, we remove this new
6892 * component & extend the current component.
6894 * Note further that uninited components followed by extension space can be zero
6895 * length meaning that we will try to extend them before initializing them, and
6896 * if that fails, they will be removed without initialization.
6898 * 3. If we extend to/beyond the end of an extension space component, that
6899 * component is exhausted (all of its range has been given to real components),
6900 * so we remove it and restart processing.
6902 * \param[in] env execution environment for this thread
6903 * \param[in,out] lo object to update the layout of
6904 * \param[in] extent layout extent for requested operation, update
6905 * layout to fit this operation
6906 * \param[in] th transaction handle for this operation
6907 * \param[in,out] max_comp the highest comp for the portion of the layout
6908 * we are operating on (For FLR, the chosen
6909 * replica). Updated because we may remove
6911 * \param[in] index index of the extension space component we're
6913 * \param[in] write if this is write op
6914 * \param[in,out] force if the extension is to be forced; set here
6915 to force it on the 2nd call for the same
6918 * \retval 0 on success
6919 * \retval negative errno on error
6921 static int lod_sel_handler(const struct lu_env *env,
6922 struct lod_object *lo,
6923 struct lu_extent *extent,
6924 struct thandle *th, int *max_comp,
6925 int index, int write,
6926 struct sel_data *sd)
6928 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6929 struct lod_thread_info *info = lod_env_info(env);
6930 struct lod_layout_component *lod_comp;
6931 struct lod_layout_component *prev;
6932 struct lod_layout_component *next = NULL;
6933 __u64 extension_size, reserve;
6940 /* First component cannot be extension space */
6942 CERROR("%s: "DFID" first component cannot be extension space\n",
6943 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6947 lod_comp = &lo->ldo_comp_entries[index];
6948 prev = &lo->ldo_comp_entries[index - 1];
6949 if ((index + 1) < *max_comp)
6950 next = &lo->ldo_comp_entries[index + 1];
6952 /* extension size uses the stripe size field as KiB */
6953 extension_size = lod_comp->llc_stripe_size * SEL_UNIT_SIZE;
6955 CDEBUG(D_LAYOUT, "prev start %llu, extension start %llu, extension end"
6956 " %llu, extension size %llu\n", prev->llc_extent.e_start,
6957 lod_comp->llc_extent.e_start, lod_comp->llc_extent.e_end,
6960 /* Two extension space components cannot be adjacent & extension space
6961 * components cannot be init */
6962 if ((prev->llc_flags & LCME_FL_EXTENSION) ||
6963 !(ergo(next, !(next->llc_flags & LCME_FL_EXTENSION))) ||
6964 lod_comp_inited(lod_comp)) {
6965 CERROR("%s: "DFID" invalid extension space components\n",
6966 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6970 reserve = lod_sel_stripe_reserved(lod_comp);
6972 if (!prev->llc_stripe) {
6973 CDEBUG(D_LAYOUT, "Previous component not inited\n");
6974 info->lti_count = 1;
6975 info->lti_comp_idx[0] = index - 1;
6976 rc = lod_declare_instantiate_components(env, lo, th, reserve);
6977 /* ENOSPC tells us we can't use this component. If there is
6978 * a next or we are repeating, we either spill over (next) or
6979 * extend the original comp (repeat). Otherwise, return the
6980 * error to the user. */
6981 if (rc == -ENOSPC && (next || sd->sd_repeat))
6987 if (sd->sd_force == 0 && rc == 0)
6988 rc = !lod_sel_osts_allowed(env, lo, index - 1, reserve, extent,
6989 &lod_comp->llc_extent, write);
6991 repeated = !!(sd->sd_repeat);
6995 /* Extend previous component */
6997 new_end = lod_extension_new_end(extension_size, extent->e_end,
6998 prev->llc_stripe_size,
6999 prev->llc_extent.e_end,
7000 lod_comp->llc_extent.e_end);
7002 CDEBUG(D_LAYOUT, "new end %llu\n", new_end);
7003 lod_comp->llc_extent.e_start = new_end;
7004 prev->llc_extent.e_end = new_end;
7006 if (prev->llc_extent.e_end == lod_comp->llc_extent.e_end) {
7007 CDEBUG(D_LAYOUT, "Extension component exhausted\n");
7008 lod_comp->llc_id = LCME_ID_INVAL;
7012 /* rc == 1, failed to extend current component */
7015 /* Normal 'spillover' case - Remove the extension
7016 * space component & bring down the start of the next
7018 lod_comp->llc_id = LCME_ID_INVAL;
7020 if (!(prev->llc_flags & LCME_FL_INIT)) {
7021 prev->llc_id = LCME_ID_INVAL;
7024 lod_sel_adjust_extents(env, lo, *max_comp, index);
7025 } else if (lod_comp_inited(prev)) {
7026 /* If there is no next, and the previous component is
7027 * INIT'ed, try repeating the previous component. */
7028 LASSERT(repeated == 0);
7029 rc = lod_layout_repeat_comp(env, lo, index - 1);
7033 /* The previous component is a repeated component.
7034 * Record this so we don't keep trying to repeat it. */
7037 /* If the previous component is not INIT'ed, this may
7038 * be a component we have just instantiated but failed
7039 * to extend. Or even a repeated component we failed
7040 * to prepare a striping for. Do not repeat but instead
7041 * remove the repeated component & force the extention
7042 * of the original one */
7045 prev->llc_id = LCME_ID_INVAL;
7052 rc = lod_layout_del_prep_layout(env, lo, NULL);
7055 LASSERTF(-rc == change,
7056 "number deleted %d != requested %d\n", -rc,
7059 *max_comp = *max_comp + change;
7061 /* lod_del_prep_layout reallocates ldo_comp_entries, so we must
7062 * refresh these pointers before using them */
7063 lod_comp = &lo->ldo_comp_entries[index];
7064 prev = &lo->ldo_comp_entries[index - 1];
7065 CDEBUG(D_LAYOUT, "After extent updates: prev start %llu, current start "
7066 "%llu, current end %llu max_comp %d ldo_comp_cnt %d\n",
7067 prev->llc_extent.e_start, lod_comp->llc_extent.e_start,
7068 lod_comp->llc_extent.e_end, *max_comp, lo->ldo_comp_cnt);
7070 /* Layout changed successfully */
7075 * Declare layout extent updates
7077 * Handles extensions. Identifies extension components touched by current
7078 * operation and passes them to processing function.
7080 * Restarts with updated layouts from the processing function until the current
7081 * operation no longer touches an extension space component.
7083 * \param[in] env execution environment for this thread
7084 * \param[in,out] lo object to update the layout of
7085 * \param[in] extent layout extent for requested operation, update layout to
7086 * fit this operation
7087 * \param[in] th transaction handle for this operation
7088 * \param[in] pick identifies chosen mirror for FLR layouts
7089 * \param[in] write if this is write op
7091 * \retval 1 on layout changed, 0 on no change
7092 * \retval negative errno on error
7094 static int lod_declare_update_extents(const struct lu_env *env,
7095 struct lod_object *lo, struct lu_extent *extent,
7096 struct thandle *th, int pick, int write)
7098 struct lod_thread_info *info = lod_env_info(env);
7099 struct lod_layout_component *lod_comp;
7100 bool layout_changed = false;
7101 struct sel_data sd = { 0 };
7109 /* This makes us work on the components of the chosen mirror */
7110 start_index = lo->ldo_mirrors[pick].lme_start;
7111 max_comp = lo->ldo_mirrors[pick].lme_end + 1;
7112 if (lo->ldo_flr_state == LCM_FL_NONE)
7113 LASSERT(start_index == 0 && max_comp == lo->ldo_comp_cnt);
7115 CDEBUG(D_LAYOUT, "extent->e_start %llu, extent->e_end %llu\n",
7116 extent->e_start, extent->e_end);
7117 for (i = start_index; i < max_comp; i++) {
7118 lod_comp = &lo->ldo_comp_entries[i];
7120 /* We've passed all components of interest */
7121 if (lod_comp->llc_extent.e_start >= extent->e_end)
7124 if (lod_comp->llc_flags & LCME_FL_EXTENSION) {
7125 layout_changed = true;
7126 rc = lod_sel_handler(env, lo, extent, th, &max_comp,
7131 /* Nothing has changed behind the prev one */
7137 /* We may have added or removed components. If so, we must update the
7138 * start & ends of all the mirrors after the current one, and the end
7139 * of the current mirror. */
7140 change = max_comp - 1 - lo->ldo_mirrors[pick].lme_end;
7142 lo->ldo_mirrors[pick].lme_end += change;
7143 for (i = pick + 1; i < lo->ldo_mirror_count; i++) {
7144 lo->ldo_mirrors[i].lme_start += change;
7145 lo->ldo_mirrors[i].lme_end += change;
7151 /* The amount of components has changed, adjust the lti_comp_idx */
7152 rc2 = lod_layout_data_init(info, lo->ldo_comp_cnt);
7154 return rc < 0 ? rc : rc2 < 0 ? rc2 : layout_changed;
7157 /* If striping is already instantiated or INIT'ed DOM? */
7158 static bool lod_is_instantiation_needed(struct lod_layout_component *comp)
7160 return !(((lov_pattern(comp->llc_pattern) == LOV_PATTERN_MDT) &&
7161 lod_comp_inited(comp)) || comp->llc_stripe);
7165 * Declare layout update for a non-FLR layout.
7167 * \param[in] env execution environment for this thread
7168 * \param[in,out] lo object to update the layout of
7169 * \param[in] layout layout intent for requested operation, "update" is
7170 * a process of reacting to this
7171 * \param[in] buf buffer containing lov ea (see comment on usage inline)
7172 * \param[in] th transaction handle for this operation
7174 * \retval 0 on success
7175 * \retval negative errno on error
7177 static int lod_declare_update_plain(const struct lu_env *env,
7178 struct lod_object *lo, struct layout_intent *layout,
7179 const struct lu_buf *buf, struct thandle *th)
7181 struct lod_thread_info *info = lod_env_info(env);
7182 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7183 struct lod_layout_component *lod_comp;
7184 struct lov_comp_md_v1 *comp_v1 = NULL;
7185 bool layout_changed = false;
7186 bool replay = false;
7190 LASSERT(lo->ldo_flr_state == LCM_FL_NONE);
7193 * In case the client is passing lovea, which only happens during
7194 * the replay of layout intent write RPC for now, we may need to
7195 * parse the lovea and apply new layout configuration.
7197 if (buf && buf->lb_len) {
7198 struct lov_user_md_v1 *v1 = buf->lb_buf;
7200 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
7201 v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
7202 LOV_MAGIC_COMP_V1)) {
7203 CERROR("%s: the replay buffer of layout extend "
7204 "(magic %#x) does not contain expected "
7205 "composite layout.\n",
7206 lod2obd(d)->obd_name, v1->lmm_magic);
7207 GOTO(out, rc = -EINVAL);
7210 rc = lod_use_defined_striping(env, lo, buf);
7213 lo->ldo_comp_cached = 1;
7215 rc = lod_get_lov_ea(env, lo);
7218 /* old on-disk EA is stored in info->lti_buf */
7219 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
7221 layout_changed = true;
7223 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
7227 /* non replay path */
7228 rc = lod_striping_load(env, lo);
7233 /* Make sure defined layout covers the requested write range. */
7234 lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
7235 if (lo->ldo_comp_cnt > 1 &&
7236 lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
7237 lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
7238 CDEBUG_LIMIT(replay ? D_ERROR : D_LAYOUT,
7239 "%s: the defined layout [0, %#llx) does not "
7240 "covers the write range "DEXT"\n",
7241 lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
7242 PEXT(&layout->li_extent));
7243 GOTO(out, rc = -EINVAL);
7246 CDEBUG(D_LAYOUT, "%s: "DFID": update components "DEXT"\n",
7247 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
7248 PEXT(&layout->li_extent));
7251 rc = lod_declare_update_extents(env, lo, &layout->li_extent,
7252 th, 0, layout->li_opc == LAYOUT_INTENT_WRITE);
7256 layout_changed = true;
7260 * Iterate ld->ldo_comp_entries, find the component whose extent under
7261 * the write range and not instantianted.
7263 for (i = 0; i < lo->ldo_comp_cnt; i++) {
7264 lod_comp = &lo->ldo_comp_entries[i];
7266 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
7270 /* If striping is instantiated or INIT'ed DOM skip */
7271 if (!lod_is_instantiation_needed(lod_comp))
7275 * In replay path, lod_comp is the EA passed by
7276 * client replay buffer, comp_v1 is the pre-recovery
7277 * on-disk EA, we'd sift out those components which
7278 * were init-ed in the on-disk EA.
7280 if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
7285 * this component hasn't instantiated in normal path, or during
7286 * replay it needs replay the instantiation.
7289 /* A released component is being extended */
7290 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
7291 GOTO(out, rc = -EINVAL);
7293 LASSERT(info->lti_comp_idx != NULL);
7294 info->lti_comp_idx[info->lti_count++] = i;
7295 layout_changed = true;
7298 if (!layout_changed)
7301 lod_obj_inc_layout_gen(lo);
7302 rc = lod_declare_instantiate_components(env, lo, th, 0);
7306 lod_striping_free(env, lo);
7310 static inline int lod_comp_index(struct lod_object *lo,
7311 struct lod_layout_component *lod_comp)
7313 LASSERT(lod_comp >= lo->ldo_comp_entries &&
7314 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
7316 return lod_comp - lo->ldo_comp_entries;
7320 * Stale other mirrors by writing extent.
7322 static int lod_stale_components(const struct lu_env *env, struct lod_object *lo,
7323 int primary, struct lu_extent *extent,
7326 struct lod_layout_component *pri_comp, *lod_comp;
7327 struct lu_extent pri_extent;
7332 /* The writing extent decides which components in the primary
7333 * are affected... */
7334 CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
7337 lod_foreach_mirror_comp(pri_comp, lo, primary) {
7338 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
7341 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
7342 lod_comp_index(lo, pri_comp),
7343 PEXT(&pri_comp->llc_extent));
7345 pri_extent.e_start = pri_comp->llc_extent.e_start;
7346 pri_extent.e_end = pri_comp->llc_extent.e_end;
7348 for (i = 0; i < lo->ldo_mirror_count; i++) {
7351 rc = lod_declare_update_extents(env, lo, &pri_extent,
7353 /* if update_extents changed the layout, it may have
7354 * reallocated the component array, so start over to
7355 * avoid using stale pointers */
7361 /* ... and then stale other components that are
7362 * overlapping with primary components */
7363 lod_foreach_mirror_comp(lod_comp, lo, i) {
7364 if (!lu_extent_is_overlapped(
7366 &lod_comp->llc_extent))
7369 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
7370 i, lod_comp_index(lo, lod_comp));
7372 lod_comp->llc_flags |= LCME_FL_STALE;
7373 lo->ldo_mirrors[i].lme_stale = 1;
7382 * check an OST's availability
7383 * \param[in] env execution environment
7384 * \param[in] lo lod object
7385 * \param[in] dt dt object
7386 * \param[in] index mirror index
7388 * \retval negative if failed
7389 * \retval 1 if \a dt is available
7390 * \retval 0 if \a dt is not available
7392 static inline int lod_check_ost_avail(const struct lu_env *env,
7393 struct lod_object *lo,
7394 struct dt_object *dt, int index)
7396 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7397 struct lod_tgt_desc *ost;
7399 int type = LU_SEQ_RANGE_OST;
7402 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &idx, &type);
7404 CERROR("%s: can't locate "DFID":rc = %d\n",
7405 lod2obd(lod)->obd_name, PFID(lu_object_fid(&dt->do_lu)),
7410 ost = OST_TGT(lod, idx);
7411 if (ost->ltd_statfs.os_state &
7412 (OS_STATFS_READONLY | OS_STATFS_ENOSPC | OS_STATFS_ENOINO |
7413 OS_STATFS_NOPRECREATE) ||
7414 ost->ltd_active == 0) {
7415 CDEBUG(D_LAYOUT, DFID ": mirror %d OST%d unavail, rc = %d\n",
7416 PFID(lod_object_fid(lo)), index, idx, rc);
7424 * Pick primary mirror for write
7425 * \param[in] env execution environment
7426 * \param[in] lo object
7427 * \param[in] extent write range
7429 static int lod_primary_pick(const struct lu_env *env, struct lod_object *lo,
7430 struct lu_extent *extent)
7432 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7433 unsigned int seq = 0;
7434 struct lod_layout_component *lod_comp;
7436 int picked = -1, second_pick = -1, third_pick = -1;
7439 if (OBD_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
7440 get_random_bytes(&seq, sizeof(seq));
7441 seq %= lo->ldo_mirror_count;
7445 * Pick a mirror as the primary, and check the availability of OSTs.
7447 * This algo can be revised later after knowing the topology of
7450 lod_qos_statfs_update(env, lod, &lod->lod_ost_descs);
7452 rc = lod_fill_mirrors(lo);
7456 for (i = 0; i < lo->ldo_mirror_count; i++) {
7457 bool ost_avail = true;
7458 int index = (i + seq) % lo->ldo_mirror_count;
7460 if (lo->ldo_mirrors[index].lme_stale) {
7461 CDEBUG(D_LAYOUT, DFID": mirror %d stale\n",
7462 PFID(lod_object_fid(lo)), index);
7466 /* 2nd pick is for the primary mirror containing unavail OST */
7467 if (lo->ldo_mirrors[index].lme_prefer && second_pick < 0)
7468 second_pick = index;
7470 /* 3rd pick is for non-primary mirror containing unavail OST */
7471 if (second_pick < 0 && third_pick < 0)
7475 * we found a non-primary 1st pick, we'd like to find a
7476 * potential pirmary mirror.
7478 if (picked >= 0 && !lo->ldo_mirrors[index].lme_prefer)
7481 /* check the availability of OSTs */
7482 lod_foreach_mirror_comp(lod_comp, lo, index) {
7483 if (!lod_comp_inited(lod_comp) || !lod_comp->llc_stripe)
7486 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
7487 struct dt_object *dt = lod_comp->llc_stripe[j];
7489 rc = lod_check_ost_avail(env, lo, dt, index);
7496 } /* for all dt object in one component */
7499 } /* for all components in a mirror */
7502 * the OSTs where allocated objects locates in the components
7503 * of the mirror are available.
7508 /* this mirror has all OSTs available */
7512 * primary with all OSTs are available, this is the perfect
7515 if (lo->ldo_mirrors[index].lme_prefer)
7517 } /* for all mirrors */
7519 /* failed to pick a sound mirror, lower our expectation */
7521 picked = second_pick;
7523 picked = third_pick;
7530 static int lod_prepare_resync_mirror(const struct lu_env *env,
7531 struct lod_object *lo,
7534 struct lod_thread_info *info = lod_env_info(env);
7535 struct lod_layout_component *lod_comp;
7536 bool neg = !!(MIRROR_ID_NEG & mirror_id);
7539 mirror_id &= ~MIRROR_ID_NEG;
7541 for (i = 0; i < lo->ldo_mirror_count; i++) {
7542 if ((!neg && lo->ldo_mirrors[i].lme_id != mirror_id) ||
7543 (neg && lo->ldo_mirrors[i].lme_id == mirror_id))
7546 lod_foreach_mirror_comp(lod_comp, lo, i) {
7547 if (lod_comp_inited(lod_comp))
7550 info->lti_comp_idx[info->lti_count++] =
7551 lod_comp_index(lo, lod_comp);
7559 * figure out the components should be instantiated for resync.
7561 static int lod_prepare_resync(const struct lu_env *env, struct lod_object *lo,
7562 struct lu_extent *extent)
7564 struct lod_thread_info *info = lod_env_info(env);
7565 struct lod_layout_component *lod_comp;
7566 unsigned int need_sync = 0;
7570 DFID": instantiate all stale components in "DEXT"\n",
7571 PFID(lod_object_fid(lo)), PEXT(extent));
7574 * instantiate all components within this extent, even non-stale
7577 for (i = 0; i < lo->ldo_mirror_count; i++) {
7578 if (!lo->ldo_mirrors[i].lme_stale)
7581 lod_foreach_mirror_comp(lod_comp, lo, i) {
7582 if (!lu_extent_is_overlapped(extent,
7583 &lod_comp->llc_extent))
7588 if (lod_comp_inited(lod_comp))
7591 CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
7592 i, lod_comp_index(lo, lod_comp));
7593 info->lti_comp_idx[info->lti_count++] =
7594 lod_comp_index(lo, lod_comp);
7598 return need_sync ? 0 : -EALREADY;
7601 static int lod_declare_update_rdonly(const struct lu_env *env,
7602 struct lod_object *lo, struct md_layout_change *mlc,
7605 struct lod_thread_info *info = lod_env_info(env);
7606 struct lu_attr *layout_attr = &info->lti_layout_attr;
7607 struct lod_layout_component *lod_comp;
7608 struct lu_extent extent = { 0 };
7612 LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
7613 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7614 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7615 LASSERT(lo->ldo_mirror_count > 0);
7617 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7618 struct layout_intent *layout = mlc->mlc_intent;
7619 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7622 extent = layout->li_extent;
7623 CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
7624 PFID(lod_object_fid(lo)), PEXT(&extent));
7626 picked = lod_primary_pick(env, lo, &extent);
7630 CDEBUG(D_LAYOUT, DFID": picked mirror id %u as primary\n",
7631 PFID(lod_object_fid(lo)),
7632 lo->ldo_mirrors[picked].lme_id);
7634 /* Update extents of primary before staling */
7635 rc = lod_declare_update_extents(env, lo, &extent, th, picked,
7640 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
7642 * trunc transfers [0, size) in the intent extent, we'd
7643 * stale components overlapping [size, eof).
7645 extent.e_start = extent.e_end;
7646 extent.e_end = OBD_OBJECT_EOF;
7649 /* stale overlapping components from other mirrors */
7650 rc = lod_stale_components(env, lo, picked, &extent, th);
7654 /* restore truncate intent extent */
7655 if (layout->li_opc == LAYOUT_INTENT_TRUNC)
7656 extent.e_end = extent.e_start;
7658 /* instantiate components for the picked mirror, start from 0 */
7661 lod_foreach_mirror_comp(lod_comp, lo, picked) {
7662 if (!lu_extent_is_overlapped(&extent,
7663 &lod_comp->llc_extent))
7666 if (!lod_is_instantiation_needed(lod_comp))
7669 info->lti_comp_idx[info->lti_count++] =
7670 lod_comp_index(lo, lod_comp);
7673 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7674 } else { /* MD_LAYOUT_RESYNC */
7678 * could contain multiple non-stale mirrors, so we need to
7679 * prep uninited all components assuming any non-stale mirror
7680 * could be picked as the primary mirror.
7682 if (mlc->mlc_mirror_id == 0) {
7684 for (i = 0; i < lo->ldo_mirror_count; i++) {
7685 if (lo->ldo_mirrors[i].lme_stale)
7688 lod_foreach_mirror_comp(lod_comp, lo, i) {
7689 if (!lod_comp_inited(lod_comp))
7693 lod_comp->llc_extent.e_end)
7695 lod_comp->llc_extent.e_end;
7698 rc = lod_prepare_resync(env, lo, &extent);
7702 /* mirror write, try to init its all components */
7703 rc = lod_prepare_resync_mirror(env, lo,
7704 mlc->mlc_mirror_id);
7709 /* change the file state to SYNC_PENDING */
7710 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7713 /* Reset the layout version once it's becoming too large.
7714 * This way it can make sure that the layout version is
7715 * monotonously increased in this writing era. */
7716 lod_obj_inc_layout_gen(lo);
7717 if (lo->ldo_layout_gen > (LCME_ID_MAX >> 1)) {
7718 __u32 layout_version;
7720 get_random_bytes(&layout_version, sizeof(layout_version));
7721 lo->ldo_layout_gen = layout_version & 0xffff;
7724 rc = lod_declare_instantiate_components(env, lo, th, 0);
7728 layout_attr->la_valid = LA_LAYOUT_VERSION;
7729 layout_attr->la_layout_version = 0; /* set current version */
7730 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7731 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7732 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7738 lod_striping_free(env, lo);
7742 static int lod_declare_update_write_pending(const struct lu_env *env,
7743 struct lod_object *lo, struct md_layout_change *mlc,
7746 struct lod_thread_info *info = lod_env_info(env);
7747 struct lu_attr *layout_attr = &info->lti_layout_attr;
7748 struct lod_layout_component *lod_comp;
7749 struct lu_extent extent = { 0 };
7755 LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
7756 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7757 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7759 /* look for the first preferred mirror */
7760 for (i = 0; i < lo->ldo_mirror_count; i++) {
7761 if (lo->ldo_mirrors[i].lme_stale)
7763 if (lo->ldo_mirrors[i].lme_prefer == 0)
7770 /* no primary, use any in-sync */
7771 for (i = 0; i < lo->ldo_mirror_count; i++) {
7772 if (lo->ldo_mirrors[i].lme_stale)
7778 CERROR(DFID ": doesn't have a primary mirror\n",
7779 PFID(lod_object_fid(lo)));
7780 GOTO(out, rc = -ENODATA);
7784 CDEBUG(D_LAYOUT, DFID": found primary %u\n",
7785 PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
7787 LASSERT(!lo->ldo_mirrors[primary].lme_stale);
7789 /* for LAYOUT_WRITE opc, it has to do the following operations:
7790 * 1. stale overlapping componets from stale mirrors;
7791 * 2. instantiate components of the primary mirror;
7792 * 3. transfter layout version to all objects of the primary;
7794 * for LAYOUT_RESYNC opc, it will do:
7795 * 1. instantiate components of all stale mirrors;
7796 * 2. transfer layout version to all objects to close write era. */
7798 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7799 struct layout_intent *layout = mlc->mlc_intent;
7800 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7802 LASSERT(mlc->mlc_intent != NULL);
7804 extent = mlc->mlc_intent->li_extent;
7806 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
7807 PFID(lod_object_fid(lo)), PEXT(&extent));
7809 /* 1. Update extents of primary before staling */
7810 rc = lod_declare_update_extents(env, lo, &extent, th, primary,
7815 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
7817 * trunc transfers [0, size) in the intent extent, we'd
7818 * stale components overlapping [size, eof).
7820 extent.e_start = extent.e_end;
7821 extent.e_end = OBD_OBJECT_EOF;
7824 /* 2. stale overlapping components */
7825 rc = lod_stale_components(env, lo, primary, &extent, th);
7829 /* 3. find the components which need instantiating.
7830 * instantiate [0, mlc->mlc_intent->e_end) */
7832 /* restore truncate intent extent */
7833 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC)
7834 extent.e_end = extent.e_start;
7837 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7838 if (!lu_extent_is_overlapped(&extent,
7839 &lod_comp->llc_extent))
7842 if (!lod_is_instantiation_needed(lod_comp))
7845 CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
7846 primary, lod_comp_index(lo, lod_comp));
7847 info->lti_comp_idx[info->lti_count++] =
7848 lod_comp_index(lo, lod_comp);
7850 } else { /* MD_LAYOUT_RESYNC */
7851 if (mlc->mlc_mirror_id == 0) {
7853 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7854 if (!lod_comp_inited(lod_comp))
7857 extent.e_end = lod_comp->llc_extent.e_end;
7860 rc = lod_prepare_resync(env, lo, &extent);
7864 /* mirror write, try to init its all components */
7865 rc = lod_prepare_resync_mirror(env, lo,
7866 mlc->mlc_mirror_id);
7871 /* change the file state to SYNC_PENDING */
7872 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7875 rc = lod_declare_instantiate_components(env, lo, th, 0);
7879 /* 3. transfer layout version to OST objects.
7880 * transfer new layout version to OST objects so that stale writes
7881 * can be denied. It also ends an era of writing by setting
7882 * LU_LAYOUT_RESYNC. Normal client can never use this bit to
7883 * send write RPC; only resync RPCs could do it. */
7884 layout_attr->la_valid = LA_LAYOUT_VERSION;
7885 layout_attr->la_layout_version = 0; /* set current version */
7886 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7887 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7888 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7892 lod_obj_inc_layout_gen(lo);
7895 lod_striping_free(env, lo);
7899 static int lod_declare_update_sync_pending(const struct lu_env *env,
7900 struct lod_object *lo, struct md_layout_change *mlc,
7903 struct lod_thread_info *info = lod_env_info(env);
7904 struct lu_attr *layout_attr = &info->lti_layout_attr;
7905 unsigned sync_components = 0;
7906 unsigned resync_components = 0;
7911 LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
7912 LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
7913 mlc->mlc_opc == MD_LAYOUT_WRITE);
7915 CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
7916 PFID(lod_object_fid(lo)), mlc->mlc_opc);
7918 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7919 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
7920 PFID(lod_object_fid(lo)));
7922 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7923 return lod_declare_update_write_pending(env, lo, mlc, th);
7926 /* MD_LAYOUT_RESYNC_DONE */
7928 for (i = 0; i < lo->ldo_comp_cnt; i++) {
7929 struct lod_layout_component *lod_comp;
7932 lod_comp = &lo->ldo_comp_entries[i];
7934 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
7939 for (j = 0; j < mlc->mlc_resync_count; j++) {
7940 if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
7943 mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
7944 lod_comp->llc_flags &= ~LCME_FL_STALE;
7945 resync_components++;
7951 for (i = 0; i < mlc->mlc_resync_count; i++) {
7952 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
7955 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
7956 "or already synced\n", PFID(lod_object_fid(lo)),
7957 mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
7958 GOTO(out, rc = -EINVAL);
7961 if (!sync_components || (mlc->mlc_resync_count && !resync_components)) {
7962 CDEBUG(D_LAYOUT, DFID": no mirror in sync\n",
7963 PFID(lod_object_fid(lo)));
7965 /* tend to return an error code here to prevent
7966 * the MDT from setting SoM attribute */
7967 GOTO(out, rc = -EINVAL);
7970 CDEBUG(D_LAYOUT, DFID": synced %u resynced %u/%zu components\n",
7971 PFID(lod_object_fid(lo)),
7972 sync_components, resync_components, mlc->mlc_resync_count);
7974 lo->ldo_flr_state = LCM_FL_RDONLY;
7975 lod_obj_inc_layout_gen(lo);
7977 layout_attr->la_valid = LA_LAYOUT_VERSION;
7978 layout_attr->la_layout_version = 0; /* set current version */
7979 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7983 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
7984 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
7985 &info->lti_buf, XATTR_NAME_LOV, 0, th);
7990 lod_striping_free(env, lo);
7994 typedef int (*mlc_handler)(const struct lu_env *env, struct dt_object *dt,
7995 const struct md_layout_change *mlc,
7996 struct thandle *th);
7999 * Attach stripes after target's for migrating directory. NB, we
8000 * only need to declare this, the actual work is done inside
8001 * lod_xattr_set_lmv().
8003 * \param[in] env execution environment
8004 * \param[in] dt target object
8005 * \param[in] mlc layout change data
8006 * \param[in] th transaction handle
8008 * \retval 0 on success
8009 * \retval negative if failed
8011 static int lod_dir_declare_layout_attach(const struct lu_env *env,
8012 struct dt_object *dt,
8013 const struct md_layout_change *mlc,
8016 struct lod_thread_info *info = lod_env_info(env);
8017 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
8018 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
8019 struct lod_object *lo = lod_dt_obj(dt);
8020 struct dt_object *next = dt_object_child(dt);
8021 struct dt_object_format *dof = &info->lti_format;
8022 struct lmv_mds_md_v1 *lmv = mlc->mlc_buf.lb_buf;
8023 struct dt_object **stripes;
8024 __u32 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
8025 struct lu_fid *fid = &info->lti_fid;
8026 struct lod_tgt_desc *tgt;
8027 struct dt_object *dto;
8028 struct dt_device *tgt_dt;
8029 int type = LU_SEQ_RANGE_ANY;
8030 struct dt_insert_rec *rec = &info->lti_dt_rec;
8031 char *stripe_name = info->lti_key;
8032 struct lu_name *sname;
8033 struct linkea_data ldata = { NULL };
8034 struct lu_buf linkea_buf;
8041 if (!lmv_is_sane(lmv))
8044 if (!dt_try_as_dir(env, dt))
8047 dof->dof_type = DFT_DIR;
8049 OBD_ALLOC_PTR_ARRAY(stripes, (lo->ldo_dir_stripe_count + stripe_count));
8053 for (i = 0; i < lo->ldo_dir_stripe_count; i++)
8054 stripes[i] = lo->ldo_stripe[i];
8056 rec->rec_type = S_IFDIR;
8058 for (i = 0; i < stripe_count; i++) {
8060 &lmv->lmv_stripe_fids[i]);
8061 if (!fid_is_sane(fid))
8064 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
8068 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
8069 tgt_dt = lod->lod_child;
8071 tgt = LTD_TGT(ltd, idx);
8073 GOTO(out, rc = -ESTALE);
8074 tgt_dt = tgt->ltd_tgt;
8077 dto = dt_locate_at(env, tgt_dt, fid,
8078 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
8081 GOTO(out, rc = PTR_ERR(dto));
8083 stripes[i + lo->ldo_dir_stripe_count] = dto;
8085 if (!dt_try_as_dir(env, dto))
8086 GOTO(out, rc = -ENOTDIR);
8088 rc = lod_sub_declare_ref_add(env, dto, th);
8092 rec->rec_fid = lu_object_fid(&dto->do_lu);
8093 rc = lod_sub_declare_insert(env, dto,
8094 (const struct dt_rec *)rec,
8095 (const struct dt_key *)dot, th);
8099 rc = lod_sub_declare_insert(env, dto,
8100 (const struct dt_rec *)rec,
8101 (const struct dt_key *)dotdot, th);
8105 rc = lod_sub_declare_xattr_set(env, dto, &mlc->mlc_buf,
8106 XATTR_NAME_LMV, 0, th);
8110 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
8111 PFID(lu_object_fid(&dto->do_lu)),
8112 i + lo->ldo_dir_stripe_count);
8114 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
8115 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
8116 sname, lu_object_fid(&dt->do_lu));
8120 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
8121 linkea_buf.lb_len = ldata.ld_leh->leh_len;
8122 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
8123 XATTR_NAME_LINK, 0, th);
8127 rc = lod_sub_declare_insert(env, next,
8128 (const struct dt_rec *)rec,
8129 (const struct dt_key *)stripe_name,
8134 rc = lod_sub_declare_ref_add(env, next, th);
8140 OBD_FREE_PTR_ARRAY(lo->ldo_stripe,
8141 lo->ldo_dir_stripes_allocated);
8142 lo->ldo_stripe = stripes;
8143 lo->ldo_dir_migrate_offset = lo->ldo_dir_stripe_count;
8144 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_hash_type);
8145 lo->ldo_dir_stripe_count += stripe_count;
8146 lo->ldo_dir_stripes_allocated += stripe_count;
8148 /* plain directory split creates target as a plain directory, while
8149 * after source attached as the first stripe, it becomes a striped
8150 * directory, set correct do_index_ops, otherwise it can't be unlinked.
8152 dt->do_index_ops = &lod_striped_index_ops;
8156 i = lo->ldo_dir_stripe_count;
8157 while (i < lo->ldo_dir_stripe_count + stripe_count && stripes[i])
8158 dt_object_put(env, stripes[i++]);
8160 OBD_FREE_PTR_ARRAY(stripes, stripe_count + lo->ldo_dir_stripe_count);
8164 static int lod_dir_declare_layout_detach(const struct lu_env *env,
8165 struct dt_object *dt,
8166 const struct md_layout_change *unused,
8169 struct lod_thread_info *info = lod_env_info(env);
8170 struct lod_object *lo = lod_dt_obj(dt);
8171 struct dt_object *next = dt_object_child(dt);
8172 char *stripe_name = info->lti_key;
8173 struct dt_object *dto;
8177 if (!dt_try_as_dir(env, dt))
8180 if (!lo->ldo_dir_stripe_count)
8181 return lod_sub_declare_delete(env, next,
8182 (const struct dt_key *)dotdot, th);
8184 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8185 dto = lo->ldo_stripe[i];
8189 if (!dt_try_as_dir(env, dto))
8192 rc = lod_sub_declare_delete(env, dto,
8193 (const struct dt_key *)dotdot, th);
8197 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8198 PFID(lu_object_fid(&dto->do_lu)), i);
8200 rc = lod_sub_declare_delete(env, next,
8201 (const struct dt_key *)stripe_name, th);
8205 rc = lod_sub_declare_ref_del(env, next, th);
8213 static int dt_dir_is_empty(const struct lu_env *env,
8214 struct dt_object *obj)
8217 const struct dt_it_ops *iops;
8222 if (!dt_try_as_dir(env, obj))
8225 iops = &obj->do_index_ops->dio_it;
8226 it = iops->init(env, obj, LUDA_64BITHASH);
8228 RETURN(PTR_ERR(it));
8230 rc = iops->get(env, it, (const struct dt_key *)"");
8234 for (rc = 0, i = 0; rc == 0 && i < 3; ++i)
8235 rc = iops->next(env, it);
8241 /* Huh? Index contains no zero key? */
8246 iops->fini(env, it);
8251 static int lod_dir_declare_layout_shrink(const struct lu_env *env,
8252 struct dt_object *dt,
8253 const struct md_layout_change *mlc,
8256 struct lod_thread_info *info = lod_env_info(env);
8257 struct lod_object *lo = lod_dt_obj(dt);
8258 struct dt_object *next = dt_object_child(dt);
8259 struct lmv_user_md *lmu = mlc->mlc_buf.lb_buf;
8260 char *stripe_name = info->lti_key;
8261 struct lu_buf *lmv_buf = &info->lti_buf;
8262 __u32 final_stripe_count;
8263 struct dt_object *dto;
8269 if (!dt_try_as_dir(env, dt))
8272 /* shouldn't be called on plain directory */
8273 LASSERT(lo->ldo_dir_stripe_count);
8275 lmv_buf->lb_buf = &info->lti_lmv.lmv_md_v1;
8276 lmv_buf->lb_len = sizeof(info->lti_lmv.lmv_md_v1);
8278 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
8279 LASSERT(final_stripe_count &&
8280 final_stripe_count < lo->ldo_dir_stripe_count);
8282 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8283 dto = lo->ldo_stripe[i];
8287 if (i < final_stripe_count) {
8288 rc = lod_sub_declare_xattr_set(env, dto, lmv_buf,
8290 LU_XATTR_REPLACE, th);
8297 rc = dt_dir_is_empty(env, dto);
8301 rc = lod_sub_declare_ref_del(env, dto, th);
8305 rc = lod_sub_declare_destroy(env, dto, th);
8309 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8310 PFID(lu_object_fid(&dto->do_lu)), i);
8312 rc = lod_sub_declare_delete(env, next,
8313 (const struct dt_key *)stripe_name, th);
8317 rc = lod_sub_declare_ref_del(env, next, th);
8322 rc = lod_sub_declare_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV,
8323 LU_XATTR_REPLACE, th);
8328 * Allocate stripes for split directory.
8330 * \param[in] env execution environment
8331 * \param[in] dt target object
8332 * \param[in] mlc layout change data
8333 * \param[in] th transaction handle
8335 * \retval 0 on success
8336 * \retval negative if failed
8338 static int lod_dir_declare_layout_split(const struct lu_env *env,
8339 struct dt_object *dt,
8340 const struct md_layout_change *mlc,
8343 struct lod_thread_info *info = lod_env_info(env);
8344 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
8345 struct lod_object *lo = lod_dt_obj(dt);
8346 struct dt_object_format *dof = &info->lti_format;
8347 struct lmv_user_md_v1 *lum = mlc->mlc_spec->u.sp_ea.eadata;
8348 struct dt_object **stripes;
8356 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
8357 LASSERT(le32_to_cpu(lum->lum_stripe_offset) == LMV_OFFSET_DEFAULT);
8359 saved_count = lo->ldo_dir_stripes_allocated;
8360 stripe_count = le32_to_cpu(lum->lum_stripe_count);
8361 if (stripe_count <= saved_count)
8364 dof->dof_type = DFT_DIR;
8366 OBD_ALLOC(stripes, sizeof(*stripes) * stripe_count);
8370 for (i = 0; i < lo->ldo_dir_stripes_allocated; i++)
8371 stripes[i] = lo->ldo_stripe[i];
8373 lod_qos_statfs_update(env, lod, &lod->lod_mdt_descs);
8374 rc = lod_mdt_alloc_qos(env, lo, stripes, saved_count, stripe_count);
8376 rc = lod_mdt_alloc_rr(env, lo, stripes, saved_count,
8379 OBD_FREE(stripes, sizeof(*stripes) * stripe_count);
8383 LASSERT(rc > saved_count);
8384 OBD_FREE(lo->ldo_stripe,
8385 sizeof(*stripes) * lo->ldo_dir_stripes_allocated);
8386 lo->ldo_stripe = stripes;
8387 lo->ldo_dir_striped = 1;
8388 lo->ldo_dir_stripe_count = rc;
8389 lo->ldo_dir_stripes_allocated = stripe_count;
8390 lo->ldo_dir_split_hash = lo->ldo_dir_hash_type;
8391 lo->ldo_dir_hash_type = le32_to_cpu(lum->lum_hash_type);
8392 if (!lmv_is_known_hash_type(lo->ldo_dir_hash_type))
8393 lo->ldo_dir_hash_type =
8394 lod->lod_mdt_descs.ltd_lmv_desc.ld_pattern;
8395 lo->ldo_dir_hash_type |= LMV_HASH_FLAG_SPLIT | LMV_HASH_FLAG_MIGRATION;
8396 lo->ldo_dir_split_offset = saved_count;
8397 lo->ldo_dir_layout_version++;
8398 lo->ldo_dir_stripe_loaded = 1;
8400 rc = lod_dir_declare_create_stripes(env, dt, mlc->mlc_attr, dof, th);
8402 lod_striping_free(env, lo);
8408 * detach all stripes from dir master object, NB, stripes are not destroyed, but
8409 * deleted from it's parent namespace, this function is called in two places:
8410 * 1. mdd_migrate_mdt() detach stripes from source, and attach them to
8412 * 2. mdd_dir_layout_update() detach stripe before turning 1-stripe directory to
8413 * a plain directory.
8415 * \param[in] env execution environment
8416 * \param[in] dt target object
8417 * \param[in] mlc layout change data
8418 * \param[in] th transaction handle
8420 * \retval 0 on success
8421 * \retval negative if failed
8423 static int lod_dir_layout_detach(const struct lu_env *env,
8424 struct dt_object *dt,
8425 const struct md_layout_change *mlc,
8428 struct lod_thread_info *info = lod_env_info(env);
8429 struct lod_object *lo = lod_dt_obj(dt);
8430 struct dt_object *next = dt_object_child(dt);
8431 char *stripe_name = info->lti_key;
8432 struct dt_object *dto;
8438 if (!lo->ldo_dir_stripe_count) {
8439 /* plain directory delete .. */
8440 rc = lod_sub_delete(env, next,
8441 (const struct dt_key *)dotdot, th);
8445 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8446 dto = lo->ldo_stripe[i];
8450 rc = lod_sub_delete(env, dto,
8451 (const struct dt_key *)dotdot, th);
8455 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8456 PFID(lu_object_fid(&dto->do_lu)), i);
8458 rc = lod_sub_delete(env, next,
8459 (const struct dt_key *)stripe_name, th);
8463 rc = lod_sub_ref_del(env, next, th);
8468 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8469 dto = lo->ldo_stripe[i];
8471 dt_object_put(env, dto);
8473 OBD_FREE_PTR_ARRAY(lo->ldo_stripe, lo->ldo_dir_stripes_allocated);
8474 lo->ldo_stripe = NULL;
8475 lo->ldo_dir_stripes_allocated = 0;
8476 lo->ldo_dir_stripe_count = 0;
8477 dt->do_index_ops = &lod_index_ops;
8482 static int lod_dir_layout_shrink(const struct lu_env *env,
8483 struct dt_object *dt,
8484 const struct md_layout_change *mlc,
8487 struct lod_thread_info *info = lod_env_info(env);
8488 struct lod_object *lo = lod_dt_obj(dt);
8489 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
8490 struct dt_object *next = dt_object_child(dt);
8491 struct lmv_user_md *lmu = mlc->mlc_buf.lb_buf;
8492 __u32 final_stripe_count;
8493 char *stripe_name = info->lti_key;
8494 struct dt_object *dto;
8495 struct lu_buf *lmv_buf = &info->lti_buf;
8496 struct lmv_mds_md_v1 *lmv = &info->lti_lmv.lmv_md_v1;
8498 int type = LU_SEQ_RANGE_ANY;
8504 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
8506 lmv_buf->lb_buf = lmv;
8507 lmv_buf->lb_len = sizeof(*lmv);
8508 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
8509 lmv->lmv_stripe_count = cpu_to_le32(final_stripe_count);
8510 lmv->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type) &
8511 cpu_to_le32(LMV_HASH_TYPE_MASK |
8512 LMV_HASH_FLAG_FIXED);
8513 lmv->lmv_layout_version =
8514 cpu_to_le32(lo->ldo_dir_layout_version + 1);
8515 lmv->lmv_migrate_offset = 0;
8516 lmv->lmv_migrate_hash = 0;
8518 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8519 dto = lo->ldo_stripe[i];
8523 if (i < final_stripe_count) {
8524 rc = lod_fld_lookup(env, lod,
8525 lu_object_fid(&dto->do_lu),
8530 lmv->lmv_master_mdt_index = cpu_to_le32(mdtidx);
8531 rc = lod_sub_xattr_set(env, dto, lmv_buf,
8533 LU_XATTR_REPLACE, th);
8540 dt_write_lock(env, dto, DT_TGT_CHILD);
8541 rc = lod_sub_ref_del(env, dto, th);
8542 dt_write_unlock(env, dto);
8546 rc = lod_sub_destroy(env, dto, th);
8550 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8551 PFID(lu_object_fid(&dto->do_lu)), i);
8553 rc = lod_sub_delete(env, next,
8554 (const struct dt_key *)stripe_name, th);
8558 rc = lod_sub_ref_del(env, next, th);
8563 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &mdtidx,
8568 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
8569 lmv->lmv_master_mdt_index = cpu_to_le32(mdtidx);
8570 rc = lod_sub_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV,
8571 LU_XATTR_REPLACE, th);
8575 for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
8576 dto = lo->ldo_stripe[i];
8578 dt_object_put(env, dto);
8580 lo->ldo_dir_stripe_count = final_stripe_count;
8585 static mlc_handler dir_mlc_declare_ops[MD_LAYOUT_MAX] = {
8586 [MD_LAYOUT_ATTACH] = lod_dir_declare_layout_attach,
8587 [MD_LAYOUT_DETACH] = lod_dir_declare_layout_detach,
8588 [MD_LAYOUT_SHRINK] = lod_dir_declare_layout_shrink,
8589 [MD_LAYOUT_SPLIT] = lod_dir_declare_layout_split,
8592 static mlc_handler dir_mlc_ops[MD_LAYOUT_MAX] = {
8593 [MD_LAYOUT_DETACH] = lod_dir_layout_detach,
8594 [MD_LAYOUT_SHRINK] = lod_dir_layout_shrink,
8597 static int lod_declare_layout_change(const struct lu_env *env,
8598 struct dt_object *dt, struct md_layout_change *mlc,
8601 struct lod_thread_info *info = lod_env_info(env);
8602 struct lod_object *lo = lod_dt_obj(dt);
8607 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
8608 LASSERT(dir_mlc_declare_ops[mlc->mlc_opc]);
8609 rc = dir_mlc_declare_ops[mlc->mlc_opc](env, dt, mlc, th);
8613 if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
8614 dt_object_remote(dt_object_child(dt)))
8617 rc = lod_striping_load(env, lo);
8621 LASSERT(lo->ldo_comp_cnt > 0);
8623 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
8627 switch (lo->ldo_flr_state) {
8629 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
8633 rc = lod_declare_update_rdonly(env, lo, mlc, th);
8635 case LCM_FL_WRITE_PENDING:
8636 rc = lod_declare_update_write_pending(env, lo, mlc, th);
8638 case LCM_FL_SYNC_PENDING:
8639 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
8650 * Instantiate layout component objects which covers the intent write offset.
8652 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
8653 struct md_layout_change *mlc, struct thandle *th)
8655 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
8656 struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
8657 struct lod_object *lo = lod_dt_obj(dt);
8662 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
8663 LASSERT(dir_mlc_ops[mlc->mlc_opc]);
8664 rc = dir_mlc_ops[mlc->mlc_opc](env, dt, mlc, th);
8668 rc = lod_striped_create(env, dt, attr, NULL, th);
8669 if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
8670 layout_attr->la_layout_version |= lo->ldo_layout_gen;
8671 rc = lod_attr_set(env, dt, layout_attr, th);
8677 const struct dt_object_operations lod_obj_ops = {
8678 .do_read_lock = lod_read_lock,
8679 .do_write_lock = lod_write_lock,
8680 .do_read_unlock = lod_read_unlock,
8681 .do_write_unlock = lod_write_unlock,
8682 .do_write_locked = lod_write_locked,
8683 .do_attr_get = lod_attr_get,
8684 .do_declare_attr_set = lod_declare_attr_set,
8685 .do_attr_set = lod_attr_set,
8686 .do_xattr_get = lod_xattr_get,
8687 .do_declare_xattr_set = lod_declare_xattr_set,
8688 .do_xattr_set = lod_xattr_set,
8689 .do_declare_xattr_del = lod_declare_xattr_del,
8690 .do_xattr_del = lod_xattr_del,
8691 .do_xattr_list = lod_xattr_list,
8692 .do_ah_init = lod_ah_init,
8693 .do_declare_create = lod_declare_create,
8694 .do_create = lod_create,
8695 .do_declare_destroy = lod_declare_destroy,
8696 .do_destroy = lod_destroy,
8697 .do_index_try = lod_index_try,
8698 .do_declare_ref_add = lod_declare_ref_add,
8699 .do_ref_add = lod_ref_add,
8700 .do_declare_ref_del = lod_declare_ref_del,
8701 .do_ref_del = lod_ref_del,
8702 .do_object_sync = lod_object_sync,
8703 .do_object_lock = lod_object_lock,
8704 .do_object_unlock = lod_object_unlock,
8705 .do_invalidate = lod_invalidate,
8706 .do_declare_layout_change = lod_declare_layout_change,
8707 .do_layout_change = lod_layout_change,
8711 * Implementation of dt_body_operations::dbo_read.
8713 * \see dt_body_operations::dbo_read() in the API description for details.
8715 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
8716 struct lu_buf *buf, loff_t *pos)
8718 struct dt_object *next = dt_object_child(dt);
8720 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
8721 S_ISLNK(dt->do_lu.lo_header->loh_attr));
8722 return next->do_body_ops->dbo_read(env, next, buf, pos);
8726 * Implementation of dt_body_operations::dbo_declare_write.
8728 * \see dt_body_operations::dbo_declare_write() in the API description
8731 static ssize_t lod_declare_write(const struct lu_env *env,
8732 struct dt_object *dt,
8733 const struct lu_buf *buf, loff_t pos,
8736 return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
8740 * Implementation of dt_body_operations::dbo_write.
8742 * \see dt_body_operations::dbo_write() in the API description for details.
8744 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
8745 const struct lu_buf *buf, loff_t *pos,
8748 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
8749 S_ISLNK(dt->do_lu.lo_header->loh_attr));
8750 return lod_sub_write(env, dt_object_child(dt), buf, pos, th);
8753 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
8754 __u64 start, __u64 end, struct thandle *th)
8756 if (dt_object_remote(dt))
8759 return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
8762 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
8763 __u64 start, __u64 end, struct thandle *th)
8765 if (dt_object_remote(dt))
8768 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
8769 return lod_sub_punch(env, dt_object_child(dt), start, end, th);
8773 * different type of files use the same body_ops because object may be created
8774 * in OUT, where there is no chance to set correct body_ops for each type, so
8775 * body_ops themselves will check file type inside, see lod_read/write/punch for
8778 static const struct dt_body_operations lod_body_ops = {
8779 .dbo_read = lod_read,
8780 .dbo_declare_write = lod_declare_write,
8781 .dbo_write = lod_write,
8782 .dbo_declare_punch = lod_declare_punch,
8783 .dbo_punch = lod_punch,
8787 * Implementation of lu_object_operations::loo_object_init.
8789 * The function determines the type and the index of the target device using
8790 * sequence of the object's FID. Then passes control down to the
8791 * corresponding device:
8792 * OSD for the local objects, OSP for remote
8794 * \see lu_object_operations::loo_object_init() in the API description
8797 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
8798 const struct lu_object_conf *conf)
8800 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
8801 struct lu_device *cdev = NULL;
8802 struct lu_object *cobj;
8803 struct lod_tgt_descs *ltd = NULL;
8804 struct lod_tgt_desc *tgt;
8806 int type = LU_SEQ_RANGE_ANY;
8810 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
8814 if (type == LU_SEQ_RANGE_MDT &&
8815 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
8816 cdev = &lod->lod_child->dd_lu_dev;
8817 } else if (type == LU_SEQ_RANGE_MDT) {
8818 ltd = &lod->lod_mdt_descs;
8820 } else if (type == LU_SEQ_RANGE_OST) {
8821 ltd = &lod->lod_ost_descs;
8828 if (ltd->ltd_tgts_size > idx &&
8829 test_bit(idx, ltd->ltd_tgt_bitmap)) {
8830 tgt = LTD_TGT(ltd, idx);
8832 LASSERT(tgt != NULL);
8833 LASSERT(tgt->ltd_tgt != NULL);
8835 cdev = &(tgt->ltd_tgt->dd_lu_dev);
8837 lod_putref(lod, ltd);
8840 if (unlikely(cdev == NULL))
8843 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
8844 if (unlikely(cobj == NULL))
8847 lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
8849 lu_object_add(lo, cobj);
8856 * Alloc cached foreign LOV
8858 * \param[in] lo object
8859 * \param[in] size size of foreign LOV
8861 * \retval 0 on success
8862 * \retval negative if failed
8864 int lod_alloc_foreign_lov(struct lod_object *lo, size_t size)
8866 OBD_ALLOC_LARGE(lo->ldo_foreign_lov, size);
8867 if (lo->ldo_foreign_lov == NULL)
8869 lo->ldo_foreign_lov_size = size;
8870 lo->ldo_is_foreign = 1;
8876 * Free cached foreign LOV
8878 * \param[in] lo object
8880 void lod_free_foreign_lov(struct lod_object *lo)
8882 if (lo->ldo_foreign_lov != NULL)
8883 OBD_FREE_LARGE(lo->ldo_foreign_lov, lo->ldo_foreign_lov_size);
8884 lo->ldo_foreign_lov = NULL;
8885 lo->ldo_foreign_lov_size = 0;
8886 lo->ldo_is_foreign = 0;
8891 * Free cached foreign LMV
8893 * \param[in] lo object
8895 void lod_free_foreign_lmv(struct lod_object *lo)
8897 if (lo->ldo_foreign_lmv != NULL)
8898 OBD_FREE_LARGE(lo->ldo_foreign_lmv, lo->ldo_foreign_lmv_size);
8899 lo->ldo_foreign_lmv = NULL;
8900 lo->ldo_foreign_lmv_size = 0;
8901 lo->ldo_dir_is_foreign = 0;
8906 * Release resources associated with striping.
8908 * If the object is striped (regular or directory), then release
8909 * the stripe objects references and free the ldo_stripe array.
8911 * \param[in] env execution environment
8912 * \param[in] lo object
8914 void lod_striping_free_nolock(const struct lu_env *env, struct lod_object *lo)
8916 struct lod_layout_component *lod_comp;
8919 if (unlikely(lo->ldo_is_foreign)) {
8920 lod_free_foreign_lov(lo);
8921 lo->ldo_comp_cached = 0;
8922 } else if (unlikely(lo->ldo_dir_is_foreign)) {
8923 lod_free_foreign_lmv(lo);
8924 lo->ldo_dir_stripe_loaded = 0;
8925 } else if (lo->ldo_stripe != NULL) {
8926 LASSERT(lo->ldo_comp_entries == NULL);
8927 LASSERT(lo->ldo_dir_stripes_allocated > 0);
8929 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8930 if (lo->ldo_stripe[i])
8931 dt_object_put(env, lo->ldo_stripe[i]);
8934 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
8935 OBD_FREE(lo->ldo_stripe, j);
8936 lo->ldo_stripe = NULL;
8937 lo->ldo_dir_stripes_allocated = 0;
8938 lo->ldo_dir_stripe_loaded = 0;
8939 lo->ldo_dir_stripe_count = 0;
8940 } else if (lo->ldo_comp_entries != NULL) {
8941 for (i = 0; i < lo->ldo_comp_cnt; i++) {
8942 /* free lod_layout_component::llc_stripe array */
8943 lod_comp = &lo->ldo_comp_entries[i];
8945 if (lod_comp->llc_stripe == NULL)
8947 LASSERT(lod_comp->llc_stripes_allocated != 0);
8948 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
8949 if (lod_comp->llc_stripe[j] != NULL)
8951 &lod_comp->llc_stripe[j]->do_lu);
8953 OBD_FREE_PTR_ARRAY(lod_comp->llc_stripe,
8954 lod_comp->llc_stripes_allocated);
8955 lod_comp->llc_stripe = NULL;
8956 OBD_FREE_PTR_ARRAY(lod_comp->llc_ost_indices,
8957 lod_comp->llc_stripes_allocated);
8958 lod_comp->llc_ost_indices = NULL;
8959 lod_comp->llc_stripes_allocated = 0;
8961 lod_free_comp_entries(lo);
8962 lo->ldo_comp_cached = 0;
8966 void lod_striping_free(const struct lu_env *env, struct lod_object *lo)
8968 mutex_lock(&lo->ldo_layout_mutex);
8969 lod_striping_free_nolock(env, lo);
8970 mutex_unlock(&lo->ldo_layout_mutex);
8974 * Implementation of lu_object_operations::loo_object_free.
8976 * \see lu_object_operations::loo_object_free() in the API description
8979 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
8981 struct lod_object *lo = lu2lod_obj(o);
8983 /* release all underlying object pinned */
8984 lod_striping_free(env, lo);
8986 /* lo doesn't contain a lu_object_header, so we don't need call_rcu */
8987 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
8991 * Implementation of lu_object_operations::loo_object_release.
8993 * \see lu_object_operations::loo_object_release() in the API description
8996 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
8998 /* XXX: shouldn't we release everything here in case if object
8999 * creation failed before? */
9003 * Implementation of lu_object_operations::loo_object_print.
9005 * \see lu_object_operations::loo_object_print() in the API description
9008 static int lod_object_print(const struct lu_env *env, void *cookie,
9009 lu_printer_t p, const struct lu_object *l)
9011 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
9013 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
9016 const struct lu_object_operations lod_lu_obj_ops = {
9017 .loo_object_init = lod_object_init,
9018 .loo_object_free = lod_object_free,
9019 .loo_object_release = lod_object_release,
9020 .loo_object_print = lod_object_print,