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 LASSERTF(0, "impossible: %d\n", lo->ldo_flr_state);
1267 case LCM_FL_SYNC_PENDING:
1271 CDEBUG(D_LAYOUT, DFID": %s to set component %x to version: %u\n",
1272 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
1273 skipped ? "skipped" : "chose", lod_comp->llc_id,
1274 data->locd_attr->la_layout_version);
1280 lod_obj_stripe_attr_set_cb(const struct lu_env *env, struct lod_object *lo,
1281 struct dt_object *dt, struct thandle *th,
1282 int comp_idx, int stripe_idx,
1283 struct lod_obj_stripe_cb_data *data)
1285 if (data->locd_declare)
1286 return lod_sub_declare_attr_set(env, dt, data->locd_attr, th);
1288 if (data->locd_attr->la_valid & LA_LAYOUT_VERSION) {
1289 CDEBUG(D_LAYOUT, DFID": set layout version: %u, comp_idx: %d\n",
1290 PFID(lu_object_fid(&dt->do_lu)),
1291 data->locd_attr->la_layout_version, comp_idx);
1294 return lod_sub_attr_set(env, dt, data->locd_attr, th);
1298 * Implementation of dt_object_operations::do_declare_attr_set.
1300 * If the object is striped, then apply the changes to all the stripes.
1302 * \see dt_object_operations::do_declare_attr_set() in the API description
1305 static int lod_declare_attr_set(const struct lu_env *env,
1306 struct dt_object *dt,
1307 const struct lu_attr *attr,
1310 struct dt_object *next = dt_object_child(dt);
1311 struct lod_object *lo = lod_dt_obj(dt);
1316 * declare setattr on the local object
1318 rc = lod_sub_declare_attr_set(env, next, attr, th);
1322 /* osp_declare_attr_set() ignores all attributes other than
1323 * UID, GID, PROJID, and size, and osp_attr_set() ignores all
1324 * but UID, GID and PROJID. Declaration of size attr setting
1325 * happens through lod_declare_init_size(), and not through
1326 * this function. Therefore we need not load striping unless
1327 * ownership is changing. This should save memory and (we hope)
1328 * speed up rename().
1330 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1331 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1334 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1337 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID | LA_MODE |
1338 LA_ATIME | LA_MTIME | LA_CTIME |
1343 * load striping information, notice we don't do this when object
1344 * is being initialized as we don't need this information till
1345 * few specific cases like destroy, chown
1347 rc = lod_striping_load(env, lo);
1351 if (!lod_obj_is_striped(dt))
1355 * if object is striped declare changes on the stripes
1357 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1358 LASSERT(lo->ldo_stripe);
1359 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1360 if (lo->ldo_stripe[i] == NULL)
1362 if (!dt_object_exists(lo->ldo_stripe[i]))
1364 rc = lod_sub_declare_attr_set(env, lo->ldo_stripe[i],
1370 struct lod_obj_stripe_cb_data data = { { 0 } };
1372 data.locd_attr = attr;
1373 data.locd_declare = true;
1374 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1375 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1381 if (!dt_object_exists(next) || dt_object_remote(next) ||
1382 !S_ISREG(attr->la_mode))
1385 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1386 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
1390 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1391 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1392 struct lod_thread_info *info = lod_env_info(env);
1393 struct lu_buf *buf = &info->lti_buf;
1395 buf->lb_buf = info->lti_ea_store;
1396 buf->lb_len = info->lti_ea_store_size;
1397 rc = lod_sub_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1398 LU_XATTR_REPLACE, th);
1405 * Implementation of dt_object_operations::do_attr_set.
1407 * If the object is striped, then apply the changes to all or subset of
1408 * the stripes depending on the object type and specific attributes.
1410 * \see dt_object_operations::do_attr_set() in the API description for details.
1412 static int lod_attr_set(const struct lu_env *env,
1413 struct dt_object *dt,
1414 const struct lu_attr *attr,
1417 struct dt_object *next = dt_object_child(dt);
1418 struct lod_object *lo = lod_dt_obj(dt);
1423 * apply changes to the local object
1425 rc = lod_sub_attr_set(env, next, attr, th);
1429 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1430 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1433 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1436 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE | LA_PROJID |
1437 LA_ATIME | LA_MTIME | LA_CTIME |
1442 /* FIXME: a tricky case in the code path of mdd_layout_change():
1443 * the in-memory striping information has been freed in lod_xattr_set()
1444 * due to layout change. It has to load stripe here again. It only
1445 * changes flags of layout so declare_attr_set() is still accurate */
1446 rc = lod_striping_load(env, lo);
1450 if (!lod_obj_is_striped(dt))
1454 * if object is striped, apply changes to all the stripes
1456 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1457 LASSERT(lo->ldo_stripe);
1458 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1459 if (unlikely(lo->ldo_stripe[i] == NULL))
1462 if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1465 rc = lod_sub_attr_set(env, lo->ldo_stripe[i], attr, th);
1470 struct lod_obj_stripe_cb_data data = { { 0 } };
1472 data.locd_attr = attr;
1473 data.locd_declare = false;
1474 data.locd_comp_skip_cb = lod_obj_attr_set_comp_skip_cb;
1475 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1476 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1482 if (!dt_object_exists(next) || dt_object_remote(next) ||
1483 !S_ISREG(attr->la_mode))
1486 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1487 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
1491 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1492 struct lod_thread_info *info = lod_env_info(env);
1493 struct lu_buf *buf = &info->lti_buf;
1494 struct ost_id *oi = &info->lti_ostid;
1495 struct lu_fid *fid = &info->lti_fid;
1496 struct lov_mds_md_v1 *lmm;
1497 struct lov_ost_data_v1 *objs;
1500 rc = lod_get_lov_ea(env, lo);
1504 buf->lb_buf = info->lti_ea_store;
1505 buf->lb_len = info->lti_ea_store_size;
1506 lmm = info->lti_ea_store;
1507 magic = le32_to_cpu(lmm->lmm_magic);
1508 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
1509 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1510 struct lov_comp_md_entry_v1 *lcme =
1511 &lcm->lcm_entries[0];
1513 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1514 magic = le32_to_cpu(lmm->lmm_magic);
1517 if (magic == LOV_MAGIC_V1)
1518 objs = &(lmm->lmm_objects[0]);
1520 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1521 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1522 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1524 fid_to_ostid(fid, oi);
1525 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1527 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1528 LU_XATTR_REPLACE, th);
1529 } else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1530 struct lod_thread_info *info = lod_env_info(env);
1531 struct lu_buf *buf = &info->lti_buf;
1532 struct lov_comp_md_v1 *lcm;
1533 struct lov_comp_md_entry_v1 *lcme;
1535 rc = lod_get_lov_ea(env, lo);
1539 buf->lb_buf = info->lti_ea_store;
1540 buf->lb_len = info->lti_ea_store_size;
1542 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1 &&
1543 le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_SEL)
1546 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1547 lcme = &lcm->lcm_entries[0];
1548 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1549 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1551 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1552 LU_XATTR_REPLACE, th);
1559 * Implementation of dt_object_operations::do_xattr_get.
1561 * If LOV EA is requested from the root object and it's not
1562 * found, then return default striping for the filesystem.
1564 * \see dt_object_operations::do_xattr_get() in the API description for details.
1566 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1567 struct lu_buf *buf, const char *name)
1569 struct lod_thread_info *info = lod_env_info(env);
1570 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1575 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1576 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1577 struct lmv_mds_md_v1 *lmv1;
1578 struct lmv_foreign_md *lfm;
1581 if (rc > (typeof(rc))sizeof(*lmv1))
1584 /* short (<= sizeof(struct lmv_mds_md_v1)) foreign LMV case */
1585 /* XXX empty foreign LMV is not allowed */
1586 if (rc <= offsetof(typeof(*lfm), lfm_value))
1587 RETURN(rc = rc > 0 ? -EINVAL : rc);
1589 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1590 BUILD_BUG_ON(sizeof(*lmv1) > sizeof(info->lti_key));
1592 /* lti_buf is large enough for *lmv1 or a short
1593 * (<= sizeof(struct lmv_mds_md_v1)) foreign LMV
1595 info->lti_buf.lb_buf = info->lti_key;
1596 info->lti_buf.lb_len = sizeof(*lmv1);
1597 rc = dt_xattr_get(env, dt_object_child(dt),
1598 &info->lti_buf, name);
1599 if (unlikely(rc <= offsetof(typeof(*lfm),
1601 RETURN(rc = rc > 0 ? -EINVAL : rc);
1603 lfm = info->lti_buf.lb_buf;
1604 if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN)
1607 if (unlikely(rc != sizeof(*lmv1)))
1608 RETURN(rc = rc > 0 ? -EINVAL : rc);
1610 lmv1 = info->lti_buf.lb_buf;
1611 /* The on-disk LMV EA only contains header, but the
1612 * returned LMV EA size should contain the space for
1613 * the FIDs of all shards of the striped directory. */
1614 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1615 rc = lmv_mds_md_size(
1616 le32_to_cpu(lmv1->lmv_stripe_count),
1617 le32_to_cpu(lmv1->lmv_magic));
1620 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1623 if (rc != sizeof(*lmv1))
1624 RETURN(rc = rc > 0 ? -EINVAL : rc);
1626 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1630 RETURN(rc = rc1 != 0 ? rc1 : rc);
1633 if ((rc > 0) && buf->lb_buf && strcmp(name, XATTR_NAME_LOV) == 0) {
1634 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1636 if (lcm->lcm_magic == cpu_to_le32(LOV_MAGIC_SEL))
1637 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1640 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1644 * XXX: Only used by lfsck
1646 * lod returns default striping on the real root of the device
1647 * this is like the root stores default striping for the whole
1648 * filesystem. historically we've been using a different approach
1649 * and store it in the config.
1651 dt_root_get(env, dev->lod_child, &info->lti_fid);
1652 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1654 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1655 struct lov_user_md *lum = buf->lb_buf;
1656 struct lov_desc *desc = &dev->lod_ost_descs.ltd_lov_desc;
1658 if (buf->lb_buf == NULL) {
1660 } else if (buf->lb_len >= sizeof(*lum)) {
1661 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1662 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1663 lmm_oi_set_id(&lum->lmm_oi, 0);
1664 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1665 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1666 lum->lmm_stripe_size = cpu_to_le32(
1667 desc->ld_default_stripe_size);
1668 lum->lmm_stripe_count = cpu_to_le16(
1669 desc->ld_default_stripe_count);
1670 lum->lmm_stripe_offset = cpu_to_le16(
1671 desc->ld_default_stripe_offset);
1684 * Checks that the magic of the stripe is sane.
1686 * \param[in] lod lod device
1687 * \param[in] lum a buffer storing LMV EA to verify
1689 * \retval 0 if the EA is sane
1690 * \retval negative otherwise
1692 static int lod_verify_md_striping(struct lod_device *lod,
1693 const struct lmv_user_md_v1 *lum)
1695 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1696 CERROR("%s: invalid lmv_user_md: magic = %x, "
1697 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1698 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1699 (int)le32_to_cpu(lum->lum_stripe_offset),
1700 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1708 * Initialize LMV EA for a slave.
1710 * Initialize slave's LMV EA from the master's LMV EA.
1712 * \param[in] master_lmv a buffer containing master's EA
1713 * \param[out] slave_lmv a buffer where slave's EA will be stored
1716 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1717 const struct lmv_mds_md_v1 *master_lmv)
1719 *slave_lmv = *master_lmv;
1720 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1726 * Generate LMV EA from the object passed as \a dt. The object must have
1727 * the stripes created and initialized.
1729 * \param[in] env execution environment
1730 * \param[in] dt object
1731 * \param[out] lmv_buf buffer storing generated LMV EA
1733 * \retval 0 on success
1734 * \retval negative if failed
1736 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1737 struct lu_buf *lmv_buf)
1739 struct lod_thread_info *info = lod_env_info(env);
1740 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1741 struct lod_object *lo = lod_dt_obj(dt);
1742 struct lmv_mds_md_v1 *lmm1;
1744 int type = LU_SEQ_RANGE_ANY;
1749 LASSERT(lo->ldo_dir_striped != 0);
1750 LASSERT(lo->ldo_dir_stripe_count > 0);
1751 stripe_count = lo->ldo_dir_stripe_count;
1752 /* Only store the LMV EA heahder on the disk. */
1753 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1754 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1758 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1761 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1762 memset(lmm1, 0, sizeof(*lmm1));
1763 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1764 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1765 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1766 lmm1->lmv_layout_version = cpu_to_le32(lo->ldo_dir_layout_version);
1767 if (lod_is_layout_changing(lo)) {
1768 lmm1->lmv_migrate_hash = cpu_to_le32(lo->ldo_dir_migrate_hash);
1769 lmm1->lmv_migrate_offset =
1770 cpu_to_le32(lo->ldo_dir_migrate_offset);
1772 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1777 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1778 lmv_buf->lb_buf = info->lti_ea_store;
1779 lmv_buf->lb_len = sizeof(*lmm1);
1785 * Create in-core represenation for a striped directory.
1787 * Parse the buffer containing LMV EA and instantiate LU objects
1788 * representing the stripe objects. The pointers to the objects are
1789 * stored in ldo_stripe field of \a lo. This function is used when
1790 * we need to access an already created object (i.e. load from a disk).
1792 * \param[in] env execution environment
1793 * \param[in] lo lod object
1794 * \param[in] buf buffer containing LMV EA
1796 * \retval 0 on success
1797 * \retval negative if failed
1799 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1800 const struct lu_buf *buf)
1802 struct lod_thread_info *info = lod_env_info(env);
1803 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1804 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1805 struct dt_object **stripe;
1806 union lmv_mds_md *lmm = buf->lb_buf;
1807 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1808 struct lu_fid *fid = &info->lti_fid;
1813 LASSERT(mutex_is_locked(&lo->ldo_layout_mutex));
1815 /* XXX may be useless as not called for foreign LMV ?? */
1816 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_FOREIGN)
1819 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1820 lo->ldo_dir_slave_stripe = 1;
1824 if (!lmv_is_sane(lmv1))
1827 LASSERT(lo->ldo_stripe == NULL);
1828 OBD_ALLOC_PTR_ARRAY(stripe, le32_to_cpu(lmv1->lmv_stripe_count));
1832 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1833 struct dt_device *tgt_dt;
1834 struct dt_object *dto;
1835 int type = LU_SEQ_RANGE_ANY;
1838 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1839 if (!fid_is_sane(fid)) {
1844 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1848 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1849 tgt_dt = lod->lod_child;
1851 struct lod_tgt_desc *tgt;
1853 tgt = LTD_TGT(ltd, idx);
1855 GOTO(out, rc = -ESTALE);
1856 tgt_dt = tgt->ltd_tgt;
1859 dto = dt_locate_at(env, tgt_dt, fid,
1860 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1863 GOTO(out, rc = PTR_ERR(dto));
1868 lo->ldo_stripe = stripe;
1869 lo->ldo_dir_stripe_count = le32_to_cpu(lmv1->lmv_stripe_count);
1870 lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1871 lo->ldo_dir_layout_version = le32_to_cpu(lmv1->lmv_layout_version);
1872 lo->ldo_dir_migrate_offset = le32_to_cpu(lmv1->lmv_migrate_offset);
1873 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv1->lmv_migrate_hash);
1874 lo->ldo_dir_hash_type = le32_to_cpu(lmv1->lmv_hash_type);
1876 lod_striping_free_nolock(env, lo);
1882 * Declare create a striped directory.
1884 * Declare creating a striped directory with a given stripe pattern on the
1885 * specified MDTs. A striped directory is represented as a regular directory
1886 * - an index listing all the stripes. The stripes point back to the master
1887 * object with ".." and LinkEA. The master object gets LMV EA which
1888 * identifies it as a striped directory. The function allocates FIDs
1891 * \param[in] env execution environment
1892 * \param[in] dt object
1893 * \param[in] attr attributes to initialize the objects with
1894 * \param[in] dof type of objects to be created
1895 * \param[in] th transaction handle
1897 * \retval 0 on success
1898 * \retval negative if failed
1900 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1901 struct dt_object *dt,
1902 struct lu_attr *attr,
1903 struct dt_object_format *dof,
1906 struct lod_thread_info *info = lod_env_info(env);
1907 struct lu_buf lmv_buf;
1908 struct lu_buf slave_lmv_buf;
1909 struct lmv_mds_md_v1 *lmm;
1910 struct lmv_mds_md_v1 *slave_lmm = NULL;
1911 struct dt_insert_rec *rec = &info->lti_dt_rec;
1912 struct lod_object *lo = lod_dt_obj(dt);
1917 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1920 lmm = lmv_buf.lb_buf;
1922 OBD_ALLOC_PTR(slave_lmm);
1923 if (slave_lmm == NULL)
1924 GOTO(out, rc = -ENOMEM);
1926 lod_prep_slave_lmv_md(slave_lmm, lmm);
1927 slave_lmv_buf.lb_buf = slave_lmm;
1928 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1930 if (!dt_try_as_dir(env, dt_object_child(dt)))
1931 GOTO(out, rc = -EINVAL);
1933 rec->rec_type = S_IFDIR;
1934 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1935 struct dt_object *dto = lo->ldo_stripe[i];
1936 char *stripe_name = info->lti_key;
1937 struct lu_name *sname;
1938 struct linkea_data ldata = { NULL };
1939 struct lu_buf linkea_buf;
1941 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
1945 /* directory split skip create for existing stripes */
1946 if (!(lod_is_splitting(lo) && i < lo->ldo_dir_split_offset)) {
1947 rc = lod_sub_declare_create(env, dto, attr, NULL, dof,
1952 if (!dt_try_as_dir(env, dto))
1953 GOTO(out, rc = -EINVAL);
1955 rc = lod_sub_declare_ref_add(env, dto, th);
1959 rec->rec_fid = lu_object_fid(&dto->do_lu);
1960 rc = lod_sub_declare_insert(env, dto,
1961 (const struct dt_rec *)rec,
1962 (const struct dt_key *)dot,
1967 /* master stripe FID will be put to .. */
1968 rec->rec_fid = lu_object_fid(&dt->do_lu);
1969 rc = lod_sub_declare_insert(env, dto,
1970 (const struct dt_rec *)rec,
1971 (const struct dt_key *)dotdot,
1976 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1978 snprintf(stripe_name, sizeof(info->lti_key),
1980 PFID(lu_object_fid(&dto->do_lu)),
1983 snprintf(stripe_name, sizeof(info->lti_key),
1985 PFID(lu_object_fid(&dto->do_lu)), i);
1987 sname = lod_name_get(env, stripe_name,
1988 strlen(stripe_name));
1989 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1990 sname, lu_object_fid(&dt->do_lu));
1994 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1995 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1996 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
1997 XATTR_NAME_LINK, 0, th);
2001 rec->rec_fid = lu_object_fid(&dto->do_lu);
2002 rc = lod_sub_declare_insert(env, dt_object_child(dt),
2003 (const struct dt_rec *)rec,
2004 (const struct dt_key *)stripe_name, th);
2008 rc = lod_sub_declare_ref_add(env, dt_object_child(dt),
2014 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
2015 cfs_fail_val != i) {
2016 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
2018 slave_lmm->lmv_master_mdt_index =
2021 slave_lmm->lmv_master_mdt_index =
2023 rc = lod_sub_declare_xattr_set(env, dto, &slave_lmv_buf,
2024 XATTR_NAME_LMV, 0, th);
2030 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt),
2031 &lmv_buf, XATTR_NAME_LMV, 0, th);
2035 if (slave_lmm != NULL)
2036 OBD_FREE_PTR(slave_lmm);
2042 * Allocate a striping on a predefined set of MDTs.
2044 * Allocates new striping using the MDT index range provided by the data from
2045 * the lum_obejcts contained in the lmv_user_md passed to this method if
2046 * \a is_specific is true; or allocates new layout starting from MDT index in
2047 * lo->ldo_dir_stripe_offset. The exact order of MDTs is not important and
2048 * varies depending on MDT status. The number of stripes needed and stripe
2049 * offset are taken from the object. If that number cannot be met, then the
2050 * function returns an error and then it's the caller's responsibility to
2051 * release the stripes allocated. All the internal structures are protected,
2052 * but no concurrent allocation is allowed on the same objects.
2054 * \param[in] env execution environment for this thread
2055 * \param[in] lo LOD object
2056 * \param[out] stripes striping created
2057 * \param[out] mdt_indices MDT indices of striping created
2058 * \param[in] is_specific true if the MDTs are provided by lum; false if
2059 * only the starting MDT index is provided
2061 * \retval positive stripes allocated, including the first stripe allocated
2063 * \retval negative errno on failure
2065 static int lod_mdt_alloc_specific(const struct lu_env *env,
2066 struct lod_object *lo,
2067 struct dt_object **stripes,
2068 __u32 *mdt_indices, bool is_specific)
2070 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
2071 struct lu_tgt_descs *ltd = &lod->lod_mdt_descs;
2072 struct lu_tgt_desc *tgt = NULL;
2073 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2074 struct dt_device *tgt_dt = NULL;
2075 struct lu_fid fid = { 0 };
2076 struct dt_object *dto;
2078 u32 stripe_count = lo->ldo_dir_stripe_count;
2084 master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
2085 if (!is_specific && stripe_count > 1)
2086 /* Set the start index for the 2nd stripe allocation */
2087 mdt_indices[1] = (mdt_indices[0] + 1) %
2088 (lod->lod_remote_mdt_count + 1);
2090 for (; stripe_idx < stripe_count; stripe_idx++) {
2091 /* Try to find next avaible target */
2092 idx = mdt_indices[stripe_idx];
2093 for (j = 0; j < lod->lod_remote_mdt_count;
2094 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
2095 bool already_allocated = false;
2098 CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
2099 idx, lod->lod_remote_mdt_count + 1, stripe_idx);
2101 if (likely(!is_specific &&
2102 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
2103 /* check whether the idx already exists
2104 * in current allocated array */
2105 for (k = 0; k < stripe_idx; k++) {
2106 if (mdt_indices[k] == idx) {
2107 already_allocated = true;
2112 if (already_allocated)
2116 /* Sigh, this index is not in the bitmap, let's check
2117 * next available target */
2118 if (!test_bit(idx, ltd->ltd_tgt_bitmap) &&
2119 idx != master_index)
2122 if (idx == master_index) {
2123 /* Allocate the FID locally */
2124 tgt_dt = lod->lod_child;
2125 rc = dt_fid_alloc(env, tgt_dt, &fid, NULL,
2132 /* check the status of the OSP */
2133 tgt = LTD_TGT(ltd, idx);
2137 tgt_dt = tgt->ltd_tgt;
2138 if (!tgt->ltd_active)
2139 /* this OSP doesn't feel well */
2142 rc = dt_fid_alloc(env, tgt_dt, &fid, NULL, NULL);
2149 /* Can not allocate more stripes */
2150 if (j == lod->lod_remote_mdt_count) {
2151 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
2152 lod2obd(lod)->obd_name, stripe_count,
2157 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
2158 idx, stripe_idx, PFID(&fid));
2159 mdt_indices[stripe_idx] = idx;
2160 /* Set the start index for next stripe allocation */
2161 if (!is_specific && stripe_idx < stripe_count - 1) {
2163 * for large dir test, put all other slaves on one
2164 * remote MDT, otherwise we may save too many local
2165 * slave locks which will exceed RS_MAX_LOCKS.
2167 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)))
2169 mdt_indices[stripe_idx + 1] = (idx + 1) %
2170 (lod->lod_remote_mdt_count + 1);
2172 /* tgt_dt and fid must be ready after search avaible OSP
2173 * in the above loop */
2174 LASSERT(tgt_dt != NULL);
2175 LASSERT(fid_is_sane(&fid));
2177 /* fail a remote stripe FID allocation */
2178 if (stripe_idx && OBD_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_FID))
2181 dto = dt_locate_at(env, tgt_dt, &fid,
2182 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
2189 stripes[stripe_idx] = dto;
2195 for (j = 1; j < stripe_idx; j++) {
2196 LASSERT(stripes[j] != NULL);
2197 dt_object_put(env, stripes[j]);
2203 static int lod_prep_md_striped_create(const struct lu_env *env,
2204 struct dt_object *dt,
2205 struct lu_attr *attr,
2206 const struct lmv_user_md_v1 *lum,
2207 struct dt_object_format *dof,
2210 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
2211 struct lod_object *lo = lod_dt_obj(dt);
2212 struct dt_object **stripes;
2213 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2214 struct lu_fid fid = { 0 };
2221 /* The lum has been verifed in lod_verify_md_striping */
2222 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC ||
2223 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC);
2225 stripe_count = lo->ldo_dir_stripe_count;
2227 OBD_ALLOC_PTR_ARRAY(stripes, stripe_count);
2231 /* Allocate the first stripe locally */
2232 rc = dt_fid_alloc(env, lod->lod_child, &fid, NULL, NULL);
2236 stripes[0] = dt_locate_at(env, lod->lod_child, &fid,
2237 dt->do_lu.lo_dev->ld_site->ls_top_dev, &conf);
2238 if (IS_ERR(stripes[0]))
2239 GOTO(out, rc = PTR_ERR(stripes[0]));
2241 if (lo->ldo_dir_stripe_offset == LMV_OFFSET_DEFAULT) {
2242 lod_qos_statfs_update(env, lod, &lod->lod_mdt_descs);
2243 rc = lod_mdt_alloc_qos(env, lo, stripes, 1, stripe_count);
2245 rc = lod_mdt_alloc_rr(env, lo, stripes, 1,
2249 bool is_specific = false;
2251 OBD_ALLOC_PTR_ARRAY(idx_array, stripe_count);
2253 GOTO(out, rc = -ENOMEM);
2255 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) {
2257 for (i = 0; i < stripe_count; i++)
2259 le32_to_cpu(lum->lum_objects[i].lum_mds);
2262 /* stripe 0 is local */
2264 lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
2265 rc = lod_mdt_alloc_specific(env, lo, stripes, idx_array,
2267 OBD_FREE_PTR_ARRAY(idx_array, stripe_count);
2275 lo->ldo_dir_striped = 1;
2276 lo->ldo_stripe = stripes;
2277 lo->ldo_dir_stripe_count = rc;
2278 lo->ldo_dir_stripes_allocated = stripe_count;
2280 lo->ldo_dir_stripe_loaded = 1;
2282 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
2284 lod_striping_free(env, lo);
2290 if (!IS_ERR_OR_NULL(stripes[0]))
2291 dt_object_put(env, stripes[0]);
2292 for (i = 1; i < stripe_count; i++)
2293 LASSERT(!stripes[i]);
2294 OBD_FREE_PTR_ARRAY(stripes, stripe_count);
2301 * Alloc cached foreign LMV
2303 * \param[in] lo object
2304 * \param[in] size size of foreign LMV
2306 * \retval 0 on success
2307 * \retval negative if failed
2309 int lod_alloc_foreign_lmv(struct lod_object *lo, size_t size)
2311 OBD_ALLOC_LARGE(lo->ldo_foreign_lmv, size);
2312 if (lo->ldo_foreign_lmv == NULL)
2314 lo->ldo_foreign_lmv_size = size;
2315 lo->ldo_dir_is_foreign = 1;
2321 * Declare create striped md object.
2323 * The function declares intention to create a striped directory. This is a
2324 * wrapper for lod_prep_md_striped_create(). The only additional functionality
2325 * is to verify pattern \a lum_buf is good. Check that function for the details.
2327 * \param[in] env execution environment
2328 * \param[in] dt object
2329 * \param[in] attr attributes to initialize the objects with
2330 * \param[in] lum_buf a pattern specifying the number of stripes and
2332 * \param[in] dof type of objects to be created
2333 * \param[in] th transaction handle
2335 * \retval 0 on success
2336 * \retval negative if failed
2339 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
2340 struct dt_object *dt,
2341 struct lu_attr *attr,
2342 const struct lu_buf *lum_buf,
2343 struct dt_object_format *dof,
2346 struct lod_object *lo = lod_dt_obj(dt);
2347 struct lmv_user_md_v1 *lum = lum_buf->lb_buf;
2351 LASSERT(lum != NULL);
2353 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
2354 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
2355 (int)le32_to_cpu(lum->lum_stripe_offset));
2357 if (lo->ldo_dir_stripe_count == 0) {
2358 if (lo->ldo_dir_is_foreign) {
2359 rc = lod_alloc_foreign_lmv(lo, lum_buf->lb_len);
2362 memcpy(lo->ldo_foreign_lmv, lum, lum_buf->lb_len);
2363 lo->ldo_dir_stripe_loaded = 1;
2368 /* prepare dir striped objects */
2369 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2371 /* failed to create striping, let's reset
2372 * config so that others don't get confused */
2373 lod_striping_free(env, lo);
2381 * Set or replace striped directory layout, and LFSCK may set layout on a plain
2382 * directory, so don't check stripe count.
2384 * \param[in] env execution environment
2385 * \param[in] dt target object
2386 * \param[in] buf LMV buf which contains source stripe fids
2387 * \param[in] fl set or replace
2388 * \param[in] th transaction handle
2390 * \retval 0 on success
2391 * \retval negative if failed
2393 static int lod_dir_layout_set(const struct lu_env *env,
2394 struct dt_object *dt,
2395 const struct lu_buf *buf,
2399 struct dt_object *next = dt_object_child(dt);
2400 struct lod_object *lo = lod_dt_obj(dt);
2401 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2402 struct lmv_mds_md_v1 *lmv = buf->lb_buf;
2403 struct lmv_mds_md_v1 *slave_lmv;
2404 struct lu_buf slave_buf;
2410 if (!lmv_is_sane2(lmv))
2413 /* adjust hash for dir merge, which may not be set in user command */
2414 if (lmv_is_merging(lmv) && !lmv->lmv_migrate_hash)
2415 lmv->lmv_merge_hash =
2416 lod->lod_mdt_descs.ltd_lmv_desc.ld_pattern;
2418 LMV_DEBUG(D_INFO, lmv, "set");
2420 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LMV, fl, th);
2424 /* directory restripe may update stripe LMV directly */
2425 if (!lo->ldo_dir_stripe_count)
2428 lo->ldo_dir_hash_type = le32_to_cpu(lmv->lmv_hash_type);
2429 lo->ldo_dir_migrate_offset = le32_to_cpu(lmv->lmv_migrate_offset);
2430 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_migrate_hash);
2431 lo->ldo_dir_layout_version = le32_to_cpu(lmv->lmv_layout_version);
2433 OBD_ALLOC_PTR(slave_lmv);
2437 lod_prep_slave_lmv_md(slave_lmv, lmv);
2438 slave_buf.lb_buf = slave_lmv;
2439 slave_buf.lb_len = sizeof(*slave_lmv);
2441 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2442 if (!lo->ldo_stripe[i])
2445 if (!dt_object_exists(lo->ldo_stripe[i]))
2448 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], &slave_buf,
2449 XATTR_NAME_LMV, fl, th);
2454 OBD_FREE_PTR(slave_lmv);
2460 * Implementation of dt_object_operations::do_declare_xattr_set.
2462 * Used with regular (non-striped) objects. Basically it
2463 * initializes the striping information and applies the
2464 * change to all the stripes.
2466 * \see dt_object_operations::do_declare_xattr_set() in the API description
2469 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2470 struct dt_object *dt,
2471 const struct lu_buf *buf,
2472 const char *name, int fl,
2475 struct dt_object *next = dt_object_child(dt);
2476 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2477 struct lod_object *lo = lod_dt_obj(dt);
2482 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2483 struct lmv_user_md_v1 *lum;
2485 LASSERT(buf != NULL && buf->lb_buf != NULL);
2487 rc = lod_verify_md_striping(d, lum);
2490 } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2491 rc = lod_verify_striping(env, d, lo, buf, false);
2496 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2500 /* Note: Do not set LinkEA on sub-stripes, otherwise
2501 * it will confuse the fid2path process(see mdt_path_current()).
2502 * The linkEA between master and sub-stripes is set in
2503 * lod_xattr_set_lmv(). */
2504 if (strcmp(name, XATTR_NAME_LINK) == 0)
2507 /* set xattr to each stripes, if needed */
2508 rc = lod_striping_load(env, lo);
2512 if (lo->ldo_dir_stripe_count == 0)
2515 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2516 if (!lo->ldo_stripe[i])
2519 if (!dt_object_exists(lo->ldo_stripe[i]))
2522 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2532 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2533 struct lod_object *lo,
2534 struct dt_object *dt, struct thandle *th,
2535 int comp_idx, int stripe_idx,
2536 struct lod_obj_stripe_cb_data *data)
2538 struct lod_thread_info *info = lod_env_info(env);
2539 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
2540 struct filter_fid *ff = &info->lti_ff;
2541 struct lu_buf *buf = &info->lti_buf;
2545 buf->lb_len = sizeof(*ff);
2546 rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2554 * locd_buf is set if it's called by dir migration, which doesn't check
2557 if (data->locd_buf) {
2558 memset(ff, 0, sizeof(*ff));
2559 ff->ff_parent = *(struct lu_fid *)data->locd_buf->lb_buf;
2561 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
2563 if (lu_fid_eq(lod_object_fid(lo), &ff->ff_parent) &&
2564 ff->ff_layout.ol_comp_id == comp->llc_id)
2567 memset(ff, 0, sizeof(*ff));
2568 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2571 /* rewrite filter_fid */
2572 ff->ff_parent.f_ver = stripe_idx;
2573 ff->ff_layout.ol_stripe_size = comp->llc_stripe_size;
2574 ff->ff_layout.ol_stripe_count = comp->llc_stripe_count;
2575 ff->ff_layout.ol_comp_id = comp->llc_id;
2576 ff->ff_layout.ol_comp_start = comp->llc_extent.e_start;
2577 ff->ff_layout.ol_comp_end = comp->llc_extent.e_end;
2578 filter_fid_cpu_to_le(ff, ff, sizeof(*ff));
2580 if (data->locd_declare)
2581 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2582 LU_XATTR_REPLACE, th);
2584 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2585 LU_XATTR_REPLACE, th);
2591 * Reset parent FID on OST object
2593 * Replace parent FID with @dt object FID, which is only called during migration
2594 * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2595 * the FID is changed.
2597 * \param[in] env execution environment
2598 * \param[in] dt dt_object whose stripes's parent FID will be reset
2599 * \parem[in] th thandle
2600 * \param[in] declare if it is declare
2602 * \retval 0 if reset succeeds
2603 * \retval negative errno if reset fails
2605 static int lod_replace_parent_fid(const struct lu_env *env,
2606 struct dt_object *dt,
2607 const struct lu_buf *buf,
2608 struct thandle *th, bool declare)
2610 struct lod_object *lo = lod_dt_obj(dt);
2611 struct lod_thread_info *info = lod_env_info(env);
2612 struct filter_fid *ff;
2613 struct lod_obj_stripe_cb_data data = { { 0 } };
2617 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2619 /* set xattr to each stripes, if needed */
2620 rc = lod_striping_load(env, lo);
2624 if (!lod_obj_is_striped(dt))
2627 if (info->lti_ea_store_size < sizeof(*ff)) {
2628 rc = lod_ea_store_resize(info, sizeof(*ff));
2633 data.locd_declare = declare;
2634 data.locd_stripe_cb = lod_obj_stripe_replace_parent_fid_cb;
2635 data.locd_buf = buf;
2636 rc = lod_obj_for_each_stripe(env, lo, th, &data);
2641 __u16 lod_comp_entry_stripe_count(struct lod_object *lo,
2642 int comp_idx, bool is_dir)
2644 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2645 struct lod_layout_component *entry;
2650 entry = &lo->ldo_comp_entries[comp_idx];
2651 if (lod_comp_inited(entry))
2652 return entry->llc_stripe_count;
2653 else if ((__u16)-1 == entry->llc_stripe_count)
2654 return lod->lod_ost_count;
2656 return lod_get_stripe_count(lod, lo, comp_idx,
2657 entry->llc_stripe_count, false);
2660 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2662 int magic, size = 0, i;
2663 struct lod_layout_component *comp_entries;
2665 bool is_composite, is_foreign = false;
2668 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2669 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2671 lo->ldo_def_striping->lds_def_striping_is_composite;
2673 comp_cnt = lo->ldo_comp_cnt;
2674 comp_entries = lo->ldo_comp_entries;
2675 is_composite = lo->ldo_is_composite;
2676 is_foreign = lo->ldo_is_foreign;
2680 return lo->ldo_foreign_lov_size;
2682 LASSERT(comp_cnt != 0 && comp_entries != NULL);
2684 size = sizeof(struct lov_comp_md_v1) +
2685 sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2686 LASSERT(size % sizeof(__u64) == 0);
2689 for (i = 0; i < comp_cnt; i++) {
2692 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2693 stripe_count = lod_comp_entry_stripe_count(lo, i, is_dir);
2694 if (!is_dir && is_composite)
2695 lod_comp_shrink_stripe_count(&comp_entries[i],
2698 size += lov_user_md_size(stripe_count, magic);
2699 LASSERT(size % sizeof(__u64) == 0);
2705 * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2706 * the xattr value is binary lov_comp_md_v1 which contains component(s)
2709 * \param[in] env execution environment
2710 * \param[in] dt dt_object to add components on
2711 * \param[in] buf buffer contains components to be added
2712 * \parem[in] th thandle
2714 * \retval 0 on success
2715 * \retval negative errno on failure
2717 static int lod_declare_layout_add(const struct lu_env *env,
2718 struct dt_object *dt,
2719 const struct lu_buf *buf,
2722 struct lod_thread_info *info = lod_env_info(env);
2723 struct lod_layout_component *comp_array, *lod_comp, *old_array;
2724 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2725 struct dt_object *next = dt_object_child(dt);
2726 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
2727 struct lod_object *lo = lod_dt_obj(dt);
2728 struct lov_user_md_v3 *v3;
2729 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2731 int i, rc, array_cnt, old_array_cnt;
2734 LASSERT(lo->ldo_is_composite);
2736 if (lo->ldo_flr_state != LCM_FL_NONE)
2739 rc = lod_verify_striping(env, d, lo, buf, false);
2743 magic = comp_v1->lcm_magic;
2744 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2745 lustre_swab_lov_comp_md_v1(comp_v1);
2746 magic = comp_v1->lcm_magic;
2749 if (magic != LOV_USER_MAGIC_COMP_V1)
2752 mutex_lock(&lo->ldo_layout_mutex);
2754 array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2755 OBD_ALLOC_PTR_ARRAY(comp_array, array_cnt);
2756 if (comp_array == NULL) {
2757 mutex_unlock(&lo->ldo_layout_mutex);
2762 memcpy(comp_array, lo->ldo_comp_entries,
2763 sizeof(*comp_array) * lo->ldo_comp_cnt);
2765 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2766 struct lov_user_md_v1 *v1;
2767 struct lu_extent *ext;
2769 v1 = (struct lov_user_md *)((char *)comp_v1 +
2770 comp_v1->lcm_entries[i].lcme_offset);
2771 ext = &comp_v1->lcm_entries[i].lcme_extent;
2773 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2774 lod_comp->llc_extent.e_start = ext->e_start;
2775 lod_comp->llc_extent.e_end = ext->e_end;
2776 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2777 lod_comp->llc_flags = comp_v1->lcm_entries[i].lcme_flags;
2779 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2780 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2781 lod_adjust_stripe_info(lod_comp, desc, 0);
2783 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2784 v3 = (struct lov_user_md_v3 *) v1;
2785 if (v3->lmm_pool_name[0] != '\0') {
2786 rc = lod_set_pool(&lod_comp->llc_pool,
2794 old_array = lo->ldo_comp_entries;
2795 old_array_cnt = lo->ldo_comp_cnt;
2797 lo->ldo_comp_entries = comp_array;
2798 lo->ldo_comp_cnt = array_cnt;
2800 /* No need to increase layout generation here, it will be increased
2801 * later when generating component ID for the new components */
2803 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2804 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2805 XATTR_NAME_LOV, 0, th);
2807 lo->ldo_comp_entries = old_array;
2808 lo->ldo_comp_cnt = old_array_cnt;
2812 OBD_FREE_PTR_ARRAY(old_array, old_array_cnt);
2814 LASSERT(lo->ldo_mirror_count == 1);
2815 lo->ldo_mirrors[0].lme_end = array_cnt - 1;
2817 mutex_unlock(&lo->ldo_layout_mutex);
2822 for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2823 lod_comp = &comp_array[i];
2824 if (lod_comp->llc_pool != NULL) {
2825 OBD_FREE(lod_comp->llc_pool,
2826 strlen(lod_comp->llc_pool) + 1);
2827 lod_comp->llc_pool = NULL;
2830 OBD_FREE_PTR_ARRAY(comp_array, array_cnt);
2831 mutex_unlock(&lo->ldo_layout_mutex);
2837 * lod_last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2838 * @mirror_id: Mirror id to be checked.
2841 * This function checks if a mirror with specified @mirror_id is the last
2842 * non-stale mirror of a LOD object @lo.
2844 * Return: true or false.
2847 bool lod_last_non_stale_mirror(__u16 mirror_id, struct lod_object *lo)
2849 struct lod_layout_component *lod_comp;
2850 bool has_stale_flag;
2853 for (i = 0; i < lo->ldo_mirror_count; i++) {
2854 if (lo->ldo_mirrors[i].lme_id == mirror_id ||
2855 lo->ldo_mirrors[i].lme_stale)
2858 has_stale_flag = false;
2859 lod_foreach_mirror_comp(lod_comp, lo, i) {
2860 if (lod_comp->llc_flags & LCME_FL_STALE) {
2861 has_stale_flag = true;
2865 if (!has_stale_flag)
2873 * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2874 * the '$field' can only be 'flags' now. The xattr value is binary
2875 * lov_comp_md_v1 which contains the component ID(s) and the value of
2876 * the field to be modified.
2877 * Please update allowed_lustre_lov macro if $field groks more values
2880 * \param[in] env execution environment
2881 * \param[in] dt dt_object to be modified
2882 * \param[in] op operation string, like "set.flags"
2883 * \param[in] buf buffer contains components to be set
2884 * \parem[in] th thandle
2886 * \retval 0 on success
2887 * \retval negative errno on failure
2889 static int lod_declare_layout_set(const struct lu_env *env,
2890 struct dt_object *dt,
2891 char *op, const struct lu_buf *buf,
2894 struct lod_layout_component *lod_comp;
2895 struct lod_thread_info *info = lod_env_info(env);
2896 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2897 struct lod_object *lo = lod_dt_obj(dt);
2898 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2901 bool changed = false;
2904 /* Please update allowed_lustre_lov macro if op
2905 * groks more values in the future
2907 if (strcmp(op, "set.flags") != 0) {
2908 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2909 lod2obd(d)->obd_name, op);
2913 magic = comp_v1->lcm_magic;
2914 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2915 lustre_swab_lov_comp_md_v1(comp_v1);
2916 magic = comp_v1->lcm_magic;
2919 if (magic != LOV_USER_MAGIC_COMP_V1)
2922 if (comp_v1->lcm_entry_count == 0) {
2923 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2924 lod2obd(d)->obd_name);
2928 mutex_lock(&lo->ldo_layout_mutex);
2929 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2930 __u32 id = comp_v1->lcm_entries[i].lcme_id;
2931 __u32 flags = comp_v1->lcm_entries[i].lcme_flags;
2932 __u32 mirror_flag = flags & LCME_MIRROR_FLAGS;
2933 __u16 mirror_id = mirror_id_of(id);
2934 bool neg = flags & LCME_FL_NEG;
2936 if (flags & LCME_FL_INIT) {
2938 lod_striping_free_nolock(env, lo);
2939 mutex_unlock(&lo->ldo_layout_mutex);
2943 flags &= ~(LCME_MIRROR_FLAGS | LCME_FL_NEG);
2944 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2945 lod_comp = &lo->ldo_comp_entries[j];
2947 /* lfs only put one flag in each entry */
2948 if ((flags && id != lod_comp->llc_id) ||
2949 (mirror_flag && mirror_id !=
2950 mirror_id_of(lod_comp->llc_id)))
2955 lod_comp->llc_flags &= ~flags;
2957 lod_comp->llc_flags &= ~mirror_flag;
2960 if ((flags & LCME_FL_STALE) &&
2961 lod_last_non_stale_mirror(mirror_id,
2964 &lo->ldo_layout_mutex);
2967 lod_comp->llc_flags |= flags;
2970 lod_comp->llc_flags |= mirror_flag;
2971 if (mirror_flag & LCME_FL_NOSYNC)
2972 lod_comp->llc_timestamp =
2973 ktime_get_real_seconds();
2979 mutex_unlock(&lo->ldo_layout_mutex);
2982 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2983 lod2obd(d)->obd_name);
2987 lod_obj_inc_layout_gen(lo);
2989 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2990 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), &info->lti_buf,
2991 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
2996 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2997 * and the xattr value is a unique component ID or a special lcme_id.
2999 * \param[in] env execution environment
3000 * \param[in] dt dt_object to be operated on
3001 * \param[in] buf buffer contains component ID or lcme_id
3002 * \parem[in] th thandle
3004 * \retval 0 on success
3005 * \retval negative errno on failure
3007 static int lod_declare_layout_del(const struct lu_env *env,
3008 struct dt_object *dt,
3009 const struct lu_buf *buf,
3012 struct lod_thread_info *info = lod_env_info(env);
3013 struct dt_object *next = dt_object_child(dt);
3014 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3015 struct lod_object *lo = lod_dt_obj(dt);
3016 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3017 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
3018 __u32 magic, id, flags, neg_flags = 0;
3022 LASSERT(lo->ldo_is_composite);
3024 if (lo->ldo_flr_state != LCM_FL_NONE)
3027 magic = comp_v1->lcm_magic;
3028 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3029 lustre_swab_lov_comp_md_v1(comp_v1);
3030 magic = comp_v1->lcm_magic;
3033 if (magic != LOV_USER_MAGIC_COMP_V1)
3036 id = comp_v1->lcm_entries[0].lcme_id;
3037 flags = comp_v1->lcm_entries[0].lcme_flags;
3039 if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
3040 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
3041 lod2obd(d)->obd_name, id, flags);
3045 if (id != LCME_ID_INVAL && flags != 0) {
3046 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
3047 lod2obd(d)->obd_name);
3051 if (id == LCME_ID_INVAL && !flags) {
3052 CDEBUG(D_LAYOUT, "%s: no id or flags specified.\n",
3053 lod2obd(d)->obd_name);
3057 if (flags & LCME_FL_NEG) {
3058 neg_flags = flags & ~LCME_FL_NEG;
3062 mutex_lock(&lo->ldo_layout_mutex);
3064 left = lo->ldo_comp_cnt;
3066 mutex_unlock(&lo->ldo_layout_mutex);
3070 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3071 struct lod_layout_component *lod_comp;
3073 lod_comp = &lo->ldo_comp_entries[i];
3075 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
3077 else if (flags && !(flags & lod_comp->llc_flags))
3079 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
3082 if (left != (i + 1)) {
3083 CDEBUG(D_LAYOUT, "%s: this deletion will create "
3084 "a hole.\n", lod2obd(d)->obd_name);
3085 mutex_unlock(&lo->ldo_layout_mutex);
3090 /* Mark the component as deleted */
3091 lod_comp->llc_id = LCME_ID_INVAL;
3093 /* Not instantiated component */
3094 if (lod_comp->llc_stripe == NULL)
3097 LASSERT(lod_comp->llc_stripe_count > 0);
3098 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
3099 struct dt_object *obj = lod_comp->llc_stripe[j];
3103 rc = lod_sub_declare_destroy(env, obj, th);
3105 mutex_unlock(&lo->ldo_layout_mutex);
3111 LASSERTF(left >= 0, "left = %d\n", left);
3112 if (left == lo->ldo_comp_cnt) {
3113 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
3114 lod2obd(d)->obd_name, id);
3115 mutex_unlock(&lo->ldo_layout_mutex);
3119 mutex_unlock(&lo->ldo_layout_mutex);
3121 memset(attr, 0, sizeof(*attr));
3122 attr->la_valid = LA_SIZE;
3123 rc = lod_sub_declare_attr_set(env, next, attr, th);
3128 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
3129 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
3130 XATTR_NAME_LOV, 0, th);
3132 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
3139 * Declare layout add/set/del operations issued by special xattr names:
3141 * XATTR_LUSTRE_LOV.add add component(s) to existing file
3142 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
3143 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
3145 * \param[in] env execution environment
3146 * \param[in] dt object
3147 * \param[in] name name of xattr
3148 * \param[in] buf lu_buf contains xattr value
3149 * \param[in] th transaction handle
3151 * \retval 0 on success
3152 * \retval negative if failed
3154 static int lod_declare_modify_layout(const struct lu_env *env,
3155 struct dt_object *dt,
3157 const struct lu_buf *buf,
3160 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3161 struct lod_object *lo = lod_dt_obj(dt);
3163 int rc, len = strlen(XATTR_LUSTRE_LOV);
3166 LASSERT(dt_object_exists(dt));
3168 if (strlen(name) <= len || name[len] != '.') {
3169 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
3170 lod2obd(d)->obd_name, name);
3175 rc = lod_striping_load(env, lo);
3179 /* the layout to be modified must be a composite layout */
3180 if (!lo->ldo_is_composite) {
3181 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
3182 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3183 GOTO(unlock, rc = -EINVAL);
3186 op = (char *)name + len;
3187 if (strcmp(op, "add") == 0) {
3188 rc = lod_declare_layout_add(env, dt, buf, th);
3189 } else if (strcmp(op, "del") == 0) {
3190 rc = lod_declare_layout_del(env, dt, buf, th);
3191 } else if (strncmp(op, "set", strlen("set")) == 0) {
3192 rc = lod_declare_layout_set(env, dt, op, buf, th);
3194 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
3195 lod2obd(d)->obd_name, name);
3196 GOTO(unlock, rc = -ENOTSUPP);
3200 lod_striping_free(env, lo);
3206 * Convert a plain file lov_mds_md to a composite layout.
3208 * \param[in,out] info the thread info::lti_ea_store buffer contains little
3209 * endian plain file layout
3211 * \retval 0 on success, <0 on failure
3213 static int lod_layout_convert(struct lod_thread_info *info)
3215 struct lov_mds_md *lmm = info->lti_ea_store;
3216 struct lov_mds_md *lmm_save;
3217 struct lov_comp_md_v1 *lcm;
3218 struct lov_comp_md_entry_v1 *lcme;
3224 /* realloc buffer to a composite layout which contains one component */
3225 blob_size = lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
3226 le32_to_cpu(lmm->lmm_magic));
3227 size = sizeof(*lcm) + sizeof(*lcme) + blob_size;
3229 OBD_ALLOC_LARGE(lmm_save, blob_size);
3231 GOTO(out, rc = -ENOMEM);
3233 memcpy(lmm_save, lmm, blob_size);
3235 if (info->lti_ea_store_size < size) {
3236 rc = lod_ea_store_resize(info, size);
3241 lcm = info->lti_ea_store;
3242 memset(lcm, 0, sizeof(*lcm) + sizeof(*lcme));
3243 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
3244 lcm->lcm_size = cpu_to_le32(size);
3245 lcm->lcm_layout_gen = cpu_to_le32(le16_to_cpu(
3246 lmm_save->lmm_layout_gen));
3247 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
3248 lcm->lcm_entry_count = cpu_to_le16(1);
3250 lcme = &lcm->lcm_entries[0];
3251 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
3252 lcme->lcme_extent.e_start = 0;
3253 lcme->lcme_extent.e_end = cpu_to_le64(OBD_OBJECT_EOF);
3254 lcme->lcme_offset = cpu_to_le32(sizeof(*lcm) + sizeof(*lcme));
3255 lcme->lcme_size = cpu_to_le32(blob_size);
3257 memcpy((char *)lcm + lcme->lcme_offset, (char *)lmm_save, blob_size);
3262 OBD_FREE_LARGE(lmm_save, blob_size);
3267 * Merge layouts to form a mirrored file.
3269 static int lod_declare_layout_merge(const struct lu_env *env,
3270 struct dt_object *dt, const struct lu_buf *mbuf,
3273 struct lod_thread_info *info = lod_env_info(env);
3274 struct lu_buf *buf = &info->lti_buf;
3275 struct lod_object *lo = lod_dt_obj(dt);
3276 struct lov_comp_md_v1 *lcm;
3277 struct lov_comp_md_v1 *cur_lcm;
3278 struct lov_comp_md_v1 *merge_lcm;
3279 struct lov_comp_md_entry_v1 *lcme;
3280 struct lov_mds_md_v1 *lmm;
3283 __u16 cur_entry_count;
3284 __u16 merge_entry_count;
3286 __u16 mirror_id = 0;
3293 merge_lcm = mbuf->lb_buf;
3294 if (mbuf->lb_len < sizeof(*merge_lcm))
3297 /* must be an existing layout from disk */
3298 if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
3301 merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
3303 /* do not allow to merge two mirrored files */
3304 if (le16_to_cpu(merge_lcm->lcm_mirror_count))
3307 /* verify the target buffer */
3308 rc = lod_get_lov_ea(env, lo);
3310 RETURN(rc ? : -ENODATA);
3312 cur_lcm = info->lti_ea_store;
3313 switch (le32_to_cpu(cur_lcm->lcm_magic)) {
3316 rc = lod_layout_convert(info);
3318 case LOV_MAGIC_COMP_V1:
3328 /* info->lti_ea_store could be reallocated in lod_layout_convert() */
3329 cur_lcm = info->lti_ea_store;
3330 cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
3332 /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
3333 mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
3334 if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
3337 /* size of new layout */
3338 size = le32_to_cpu(cur_lcm->lcm_size) +
3339 le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
3341 memset(buf, 0, sizeof(*buf));
3342 lu_buf_alloc(buf, size);
3343 if (buf->lb_buf == NULL)
3347 memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
3349 offset = sizeof(*lcm) +
3350 sizeof(*lcme) * (cur_entry_count + merge_entry_count);
3351 for (i = 0; i < cur_entry_count; i++) {
3352 struct lov_comp_md_entry_v1 *cur_lcme;
3354 lcme = &lcm->lcm_entries[i];
3355 cur_lcme = &cur_lcm->lcm_entries[i];
3357 lcme->lcme_offset = cpu_to_le32(offset);
3358 memcpy((char *)lcm + offset,
3359 (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
3360 le32_to_cpu(lcme->lcme_size));
3362 offset += le32_to_cpu(lcme->lcme_size);
3364 if (mirror_count == 1 &&
3365 mirror_id_of(le32_to_cpu(lcme->lcme_id)) == 0) {
3366 /* Add mirror from a non-flr file, create new mirror ID.
3367 * Otherwise, keep existing mirror's component ID, used
3368 * for mirror extension.
3370 id = pflr_id(1, i + 1);
3371 lcme->lcme_id = cpu_to_le32(id);
3374 id = max(le32_to_cpu(lcme->lcme_id), id);
3377 mirror_id = mirror_id_of(id) + 1;
3379 /* check if first entry in new layout is DOM */
3380 lmm = (struct lov_mds_md_v1 *)((char *)merge_lcm +
3381 merge_lcm->lcm_entries[0].lcme_offset);
3382 merge_has_dom = lov_pattern(le32_to_cpu(lmm->lmm_pattern)) ==
3385 for (i = 0; i < merge_entry_count; i++) {
3386 struct lov_comp_md_entry_v1 *merge_lcme;
3388 merge_lcme = &merge_lcm->lcm_entries[i];
3389 lcme = &lcm->lcm_entries[cur_entry_count + i];
3391 *lcme = *merge_lcme;
3392 lcme->lcme_offset = cpu_to_le32(offset);
3393 if (merge_has_dom && i == 0)
3394 lcme->lcme_flags |= cpu_to_le32(LCME_FL_STALE);
3396 id = pflr_id(mirror_id, i + 1);
3397 lcme->lcme_id = cpu_to_le32(id);
3399 memcpy((char *)lcm + offset,
3400 (char *)merge_lcm + le32_to_cpu(merge_lcme->lcme_offset),
3401 le32_to_cpu(lcme->lcme_size));
3403 offset += le32_to_cpu(lcme->lcme_size);
3406 /* fixup layout information */
3407 lod_obj_inc_layout_gen(lo);
3408 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3409 lcm->lcm_size = cpu_to_le32(size);
3410 lcm->lcm_entry_count = cpu_to_le16(cur_entry_count + merge_entry_count);
3411 lcm->lcm_mirror_count = cpu_to_le16(mirror_count);
3412 if ((le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK) == LCM_FL_NONE)
3413 lcm->lcm_flags = cpu_to_le32(LCM_FL_RDONLY);
3415 rc = lod_striping_reload(env, lo, buf);
3419 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), buf,
3420 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3428 * Split layouts, just set the LOVEA with the layout from mbuf.
3430 static int lod_declare_layout_split(const struct lu_env *env,
3431 struct dt_object *dt, const struct lu_buf *mbuf,
3434 struct lod_object *lo = lod_dt_obj(dt);
3435 struct lov_comp_md_v1 *lcm = mbuf->lb_buf;
3439 rc = lod_striping_reload(env, lo, mbuf);
3443 lod_obj_inc_layout_gen(lo);
3444 /* fix on-disk layout gen */
3445 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3447 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), mbuf,
3448 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3452 static int lod_layout_declare_or_purge_mirror(const struct lu_env *env,
3453 struct dt_object *dt, const struct lu_buf *buf,
3454 struct thandle *th, bool declare)
3456 struct lod_thread_info *info = lod_env_info(env);
3457 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3458 struct lod_object *lo = lod_dt_obj(dt);
3459 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
3460 struct lov_comp_md_entry_v1 *entry;
3461 struct lov_mds_md_v1 *lmm;
3462 struct dt_object **sub_objs = NULL;
3463 int rc = 0, i, k, array_count = 0;
3468 * other ops (like lod_declare_destroy) could destroying sub objects
3471 mutex_lock(&lo->ldo_layout_mutex);
3474 /* prepare sub-objects array */
3475 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3476 entry = &comp_v1->lcm_entries[i];
3478 if (!(entry->lcme_flags & LCME_FL_INIT))
3481 lmm = (struct lov_mds_md_v1 *)
3482 ((char *)comp_v1 + entry->lcme_offset);
3483 array_count += lmm->lmm_stripe_count;
3485 OBD_ALLOC_PTR_ARRAY(sub_objs, array_count);
3486 if (sub_objs == NULL) {
3487 mutex_unlock(&lo->ldo_layout_mutex);
3492 k = 0; /* sub_objs index */
3493 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3494 struct lov_ost_data_v1 *objs;
3495 struct lu_object *o, *n;
3496 struct dt_object *dto;
3497 struct lu_device *nd;
3498 struct lov_mds_md_v3 *v3;
3502 entry = &comp_v1->lcm_entries[i];
3504 if (!(entry->lcme_flags & LCME_FL_INIT))
3507 lmm = (struct lov_mds_md_v1 *)
3508 ((char *)comp_v1 + entry->lcme_offset);
3509 v3 = (struct lov_mds_md_v3 *)lmm;
3510 if (lmm->lmm_magic == LOV_MAGIC_V3)
3511 objs = &v3->lmm_objects[0];
3513 objs = &lmm->lmm_objects[0];
3515 for (j = 0; j < lmm->lmm_stripe_count; j++) {
3516 idx = objs[j].l_ost_idx;
3517 rc = ostid_to_fid(&info->lti_fid, &objs[j].l_ost_oi,
3522 if (!fid_is_sane(&info->lti_fid)) {
3523 CERROR("%s: sub-object insane fid "DFID"\n",
3524 lod2obd(d)->obd_name,
3525 PFID(&info->lti_fid));
3526 GOTO(out, rc = -EINVAL);
3529 lod_getref(&d->lod_ost_descs);
3531 rc = validate_lod_and_idx(d, idx);
3533 lod_putref(d, &d->lod_ost_descs);
3537 nd = &OST_TGT(d, idx)->ltd_tgt->dd_lu_dev;
3538 lod_putref(d, &d->lod_ost_descs);
3540 o = lu_object_find_at(env, nd, &info->lti_fid, NULL);
3542 GOTO(out, rc = PTR_ERR(o));
3544 n = lu_object_locate(o->lo_header, nd->ld_type);
3546 lu_object_put(env, n);
3547 GOTO(out, rc = -ENOENT);
3550 dto = container_of(n, struct dt_object, do_lu);
3553 rc = lod_sub_declare_destroy(env, dto, th);
3554 dt_object_put(env, dto);
3559 * collect to-be-destroyed sub objects, the
3560 * reference would be released after actual
3566 } /* for each stripe */
3567 } /* for each component in the mirror */
3572 /* destroy the sub objects */
3573 for (; i < k; i++) {
3574 rc = lod_sub_destroy(env, sub_objs[i], th);
3577 dt_object_put(env, sub_objs[i]);
3581 * if a sub object destroy failed, we'd release sub objects
3582 * reference get from above sub_objs collection.
3585 dt_object_put(env, sub_objs[i]);
3587 OBD_FREE_PTR_ARRAY(sub_objs, array_count);
3589 mutex_unlock(&lo->ldo_layout_mutex);
3595 * Purge layouts, delete sub objects in the mirror stored in the vic_buf,
3596 * and set the LOVEA with the layout from mbuf.
3598 static int lod_declare_layout_purge(const struct lu_env *env,
3599 struct dt_object *dt, const struct lu_buf *buf,
3602 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3603 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
3608 if (le32_to_cpu(comp_v1->lcm_magic) != LOV_MAGIC_COMP_V1) {
3609 CERROR("%s: invalid layout magic %#x != %#x\n",
3610 lod2obd(d)->obd_name, le32_to_cpu(comp_v1->lcm_magic),
3615 if (cpu_to_le32(LOV_MAGIC_COMP_V1) != LOV_MAGIC_COMP_V1)
3616 lustre_swab_lov_comp_md_v1(comp_v1);
3618 /* from now on, @buf contains cpu endian data */
3620 if (comp_v1->lcm_mirror_count != 0) {
3621 CERROR("%s: can only purge one mirror from "DFID"\n",
3622 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3626 /* delcare sub objects deletion in the mirror stored in @buf */
3627 rc = lod_layout_declare_or_purge_mirror(env, dt, buf, th, true);
3631 /* delete sub objects from the mirror stored in @buf */
3632 static int lod_layout_purge(const struct lu_env *env, struct dt_object *dt,
3633 const struct lu_buf *buf, struct thandle *th)
3638 rc = lod_layout_declare_or_purge_mirror(env, dt, buf, th, false);
3643 * Implementation of dt_object_operations::do_declare_xattr_set.
3645 * \see dt_object_operations::do_declare_xattr_set() in the API description
3648 * the extension to the API:
3649 * - declaring LOVEA requests striping creation
3650 * - LU_XATTR_REPLACE means layout swap
3652 static int lod_declare_xattr_set(const struct lu_env *env,
3653 struct dt_object *dt,
3654 const struct lu_buf *buf,
3655 const char *name, int fl,
3658 struct dt_object *next = dt_object_child(dt);
3659 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3664 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
3665 if ((S_ISREG(mode) || mode == 0) &&
3666 !(fl & (LU_XATTR_REPLACE | LU_XATTR_MERGE | LU_XATTR_SPLIT |
3668 (strcmp(name, XATTR_NAME_LOV) == 0 ||
3669 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
3671 * this is a request to create object's striping.
3673 * allow to declare predefined striping on a new (!mode) object
3674 * which is supposed to be replay of regular file creation
3675 * (when LOV setting is declared)
3677 * LU_XATTR_REPLACE is set to indicate a layout swap
3679 if (dt_object_exists(dt)) {
3680 rc = dt_attr_get(env, next, attr);
3684 memset(attr, 0, sizeof(*attr));
3685 attr->la_valid = LA_TYPE | LA_MODE;
3686 attr->la_mode = S_IFREG;
3688 rc = lod_declare_striped_create(env, dt, attr, buf, th);
3689 } else if (fl & LU_XATTR_MERGE) {
3690 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3691 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3692 rc = lod_declare_layout_merge(env, dt, buf, th);
3693 } else if (fl & LU_XATTR_SPLIT) {
3694 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3695 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3696 rc = lod_declare_layout_split(env, dt, buf, th);
3697 } else if (fl & LU_XATTR_PURGE) {
3698 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3699 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3700 rc = lod_declare_layout_purge(env, dt, buf, th);
3701 } else if (S_ISREG(mode) &&
3702 strlen(name) >= sizeof(XATTR_LUSTRE_LOV) + 3 &&
3703 allowed_lustre_lov(name)) {
3705 * this is a request to modify object's striping.
3706 * add/set/del component(s).
3708 if (!dt_object_exists(dt))
3711 rc = lod_declare_modify_layout(env, dt, name, buf, th);
3712 } else if (S_ISDIR(mode)) {
3713 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
3714 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3715 rc = lod_replace_parent_fid(env, dt, buf, th, true);
3717 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
3724 * Apply xattr changes to the object.
3726 * Applies xattr changes to the object and the stripes if the latter exist.
3728 * \param[in] env execution environment
3729 * \param[in] dt object
3730 * \param[in] buf buffer pointing to the new value of xattr
3731 * \param[in] name name of xattr
3732 * \param[in] fl flags
3733 * \param[in] th transaction handle
3735 * \retval 0 on success
3736 * \retval negative if failed
3738 static int lod_xattr_set_internal(const struct lu_env *env,
3739 struct dt_object *dt,
3740 const struct lu_buf *buf,
3741 const char *name, int fl,
3744 struct dt_object *next = dt_object_child(dt);
3745 struct lod_object *lo = lod_dt_obj(dt);
3750 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3751 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3754 /* Note: Do not set LinkEA on sub-stripes, otherwise
3755 * it will confuse the fid2path process(see mdt_path_current()).
3756 * The linkEA between master and sub-stripes is set in
3757 * lod_xattr_set_lmv(). */
3758 if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
3761 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3762 if (!lo->ldo_stripe[i])
3765 if (!dt_object_exists(lo->ldo_stripe[i]))
3768 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
3778 * Delete an extended attribute.
3780 * Deletes specified xattr from the object and the stripes if the latter exist.
3782 * \param[in] env execution environment
3783 * \param[in] dt object
3784 * \param[in] name name of xattr
3785 * \param[in] th transaction handle
3787 * \retval 0 on success
3788 * \retval negative if failed
3790 static int lod_xattr_del_internal(const struct lu_env *env,
3791 struct dt_object *dt,
3792 const char *name, struct thandle *th)
3794 struct dt_object *next = dt_object_child(dt);
3795 struct lod_object *lo = lod_dt_obj(dt);
3801 rc = lod_sub_xattr_del(env, next, name, th);
3802 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3805 if (lo->ldo_dir_stripe_count == 0)
3808 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3809 if (!lo->ldo_stripe[i])
3812 if (!dt_object_exists(lo->ldo_stripe[i]))
3815 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3824 * Set default striping on a directory.
3826 * Sets specified striping on a directory object unless it matches the default
3827 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3828 * EA. This striping will be used when regular file is being created in this
3831 * \param[in] env execution environment
3832 * \param[in] dt the striped object
3833 * \param[in] buf buffer with the striping
3834 * \param[in] name name of EA
3835 * \param[in] fl xattr flag (see OSD API description)
3836 * \param[in] th transaction handle
3838 * \retval 0 on success
3839 * \retval negative if failed
3841 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
3842 struct dt_object *dt,
3843 const struct lu_buf *buf,
3844 const char *name, int fl,
3847 struct lov_user_md_v1 *lum;
3848 struct lov_user_md_v3 *v3 = NULL;
3849 const char *pool_name = NULL;
3854 LASSERT(buf != NULL && buf->lb_buf != NULL);
3857 switch (lum->lmm_magic) {
3858 case LOV_USER_MAGIC_SPECIFIC:
3859 case LOV_USER_MAGIC_V3:
3861 if (v3->lmm_pool_name[0] != '\0')
3862 pool_name = v3->lmm_pool_name;
3864 case LOV_USER_MAGIC_V1:
3865 /* if { size, offset, count } = { 0, -1, 0 } and no pool
3866 * (i.e. all default values specified) then delete default
3867 * striping from dir. */
3869 "set default striping: sz %u # %u offset %d %s %s\n",
3870 (unsigned)lum->lmm_stripe_size,
3871 (unsigned)lum->lmm_stripe_count,
3872 (int)lum->lmm_stripe_offset,
3873 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
3875 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
3876 lum->lmm_stripe_count,
3877 lum->lmm_stripe_offset,
3880 case LOV_USER_MAGIC_COMP_V1:
3882 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lum;
3883 struct lov_comp_md_entry_v1 *lcme;
3886 comp_cnt = le16_to_cpu(lcm->lcm_entry_count);
3887 for (i = 0; i < comp_cnt; i++) {
3888 lcme = &lcm->lcm_entries[i];
3889 if (lcme->lcme_flags & cpu_to_le32(LCME_FL_EXTENSION)) {
3890 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_SEL);
3899 CERROR("Invalid magic %x\n", lum->lmm_magic);
3904 rc = lod_xattr_del_internal(env, dt, name, th);
3908 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3915 * Set default striping on a directory object.
3917 * Sets specified striping on a directory object unless it matches the default
3918 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3919 * EA. This striping will be used when a new directory is being created in the
3922 * \param[in] env execution environment
3923 * \param[in] dt the striped object
3924 * \param[in] buf buffer with the striping
3925 * \param[in] name name of EA
3926 * \param[in] fl xattr flag (see OSD API description)
3927 * \param[in] th transaction handle
3929 * \retval 0 on success
3930 * \retval negative if failed
3932 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
3933 struct dt_object *dt,
3934 const struct lu_buf *buf,
3935 const char *name, int fl,
3938 struct lmv_user_md_v1 *lum;
3943 LASSERT(buf != NULL && buf->lb_buf != NULL);
3947 "set default stripe_count # %u stripe_offset %d hash %u\n",
3948 le32_to_cpu(lum->lum_stripe_count),
3949 (int)le32_to_cpu(lum->lum_stripe_offset),
3950 le32_to_cpu(lum->lum_hash_type));
3952 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
3953 le32_to_cpu(lum->lum_stripe_offset)) &&
3954 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
3955 rc = lod_xattr_del_internal(env, dt, name, th);
3959 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3968 * Turn directory into a striped directory.
3970 * During replay the client sends the striping created before MDT
3971 * failure, then the layer above LOD sends this defined striping
3972 * using ->do_xattr_set(), so LOD uses this method to replay creation
3973 * of the stripes. Notice the original information for the striping
3974 * (#stripes, FIDs, etc) was transferred in declare path.
3976 * \param[in] env execution environment
3977 * \param[in] dt the striped object
3978 * \param[in] buf not used currently
3979 * \param[in] name not used currently
3980 * \param[in] fl xattr flag (see OSD API description)
3981 * \param[in] th transaction handle
3983 * \retval 0 on success
3984 * \retval negative if failed
3986 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
3987 const struct lu_buf *buf, const char *name,
3988 int fl, struct thandle *th)
3990 struct lod_object *lo = lod_dt_obj(dt);
3991 struct lod_thread_info *info = lod_env_info(env);
3992 struct lu_attr *attr = &info->lti_attr;
3993 struct dt_object_format *dof = &info->lti_format;
3994 struct lu_buf lmv_buf;
3995 struct lu_buf slave_lmv_buf;
3996 struct lmv_mds_md_v1 *lmm;
3997 struct lmv_mds_md_v1 *slave_lmm = NULL;
3998 struct dt_insert_rec *rec = &info->lti_dt_rec;
4003 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4006 /* The stripes are supposed to be allocated in declare phase,
4007 * if there are no stripes being allocated, it will skip */
4008 if (lo->ldo_dir_stripe_count == 0) {
4009 if (lo->ldo_dir_is_foreign) {
4010 rc = lod_sub_xattr_set(env, dt_object_child(dt), buf,
4011 XATTR_NAME_LMV, fl, th);
4018 rc = dt_attr_get(env, dt_object_child(dt), attr);
4022 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_FLAGS |
4023 LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
4024 dof->dof_type = DFT_DIR;
4026 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
4029 lmm = lmv_buf.lb_buf;
4031 OBD_ALLOC_PTR(slave_lmm);
4032 if (slave_lmm == NULL)
4035 lod_prep_slave_lmv_md(slave_lmm, lmm);
4036 slave_lmv_buf.lb_buf = slave_lmm;
4037 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
4039 rec->rec_type = S_IFDIR;
4040 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4041 struct dt_object *dto = lo->ldo_stripe[i];
4042 char *stripe_name = info->lti_key;
4043 struct lu_name *sname;
4044 struct linkea_data ldata = { NULL };
4045 struct lu_buf linkea_buf;
4047 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
4051 /* fail a remote stripe creation */
4052 if (i && OBD_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_CREATE))
4055 /* don't create stripe if:
4056 * 1. it's source stripe of migrating directory
4057 * 2. it's existed stripe of splitting directory
4059 if ((lod_is_migrating(lo) && i >= lo->ldo_dir_migrate_offset) ||
4060 (lod_is_splitting(lo) && i < lo->ldo_dir_split_offset)) {
4061 if (!dt_object_exists(dto))
4062 GOTO(out, rc = -EINVAL);
4064 dt_write_lock(env, dto, DT_TGT_CHILD);
4065 rc = lod_sub_create(env, dto, attr, NULL, dof, th);
4067 dt_write_unlock(env, dto);
4071 rc = lod_sub_ref_add(env, dto, th);
4072 dt_write_unlock(env, dto);
4076 rec->rec_fid = lu_object_fid(&dto->do_lu);
4077 rc = lod_sub_insert(env, dto,
4078 (const struct dt_rec *)rec,
4079 (const struct dt_key *)dot, th);
4084 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
4085 cfs_fail_val != i) {
4086 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
4088 slave_lmm->lmv_master_mdt_index =
4091 slave_lmm->lmv_master_mdt_index =
4094 rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
4095 XATTR_NAME_LMV, 0, th);
4100 /* don't insert stripe if it's existed stripe of splitting
4101 * directory (this directory is striped).
4102 * NB, plain directory will insert itself as the first
4105 if (lod_is_splitting(lo) && lo->ldo_dir_split_offset > 1 &&
4106 lo->ldo_dir_split_offset > i)
4109 rec->rec_fid = lu_object_fid(&dt->do_lu);
4110 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
4111 (const struct dt_key *)dotdot, th);
4115 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
4117 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4118 PFID(lu_object_fid(&dto->do_lu)), i + 1);
4120 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4121 PFID(lu_object_fid(&dto->do_lu)), i);
4123 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
4124 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
4125 sname, lu_object_fid(&dt->do_lu));
4129 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
4130 linkea_buf.lb_len = ldata.ld_leh->leh_len;
4131 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
4132 XATTR_NAME_LINK, 0, th);
4136 rec->rec_fid = lu_object_fid(&dto->do_lu);
4137 rc = lod_sub_insert(env, dt_object_child(dt),
4138 (const struct dt_rec *)rec,
4139 (const struct dt_key *)stripe_name, th);
4143 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
4148 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
4149 rc = lod_sub_xattr_set(env, dt_object_child(dt),
4150 &lmv_buf, XATTR_NAME_LMV, fl, th);
4152 if (slave_lmm != NULL)
4153 OBD_FREE_PTR(slave_lmm);
4159 * Helper function to declare/execute creation of a striped directory
4161 * Called in declare/create object path, prepare striping for a directory
4162 * and prepare defaults data striping for the objects to be created in
4163 * that directory. Notice the function calls "declaration" or "execution"
4164 * methods depending on \a declare param. This is a consequence of the
4165 * current approach while we don't have natural distributed transactions:
4166 * we basically execute non-local updates in the declare phase. So, the
4167 * arguments for the both phases are the same and this is the reason for
4168 * this function to exist.
4170 * \param[in] env execution environment
4171 * \param[in] dt object
4172 * \param[in] attr attributes the stripes will be created with
4173 * \param[in] lmu lmv_user_md if MDT indices are specified
4174 * \param[in] dof format of stripes (see OSD API description)
4175 * \param[in] th transaction handle
4176 * \param[in] declare where to call "declare" or "execute" methods
4178 * \retval 0 on success
4179 * \retval negative if failed
4181 static int lod_dir_striping_create_internal(const struct lu_env *env,
4182 struct dt_object *dt,
4183 struct lu_attr *attr,
4184 const struct lu_buf *lmu,
4185 struct dt_object_format *dof,
4189 struct lod_thread_info *info = lod_env_info(env);
4190 struct lod_object *lo = lod_dt_obj(dt);
4191 const struct lod_default_striping *lds = lo->ldo_def_striping;
4195 LASSERT(ergo(lds != NULL,
4196 lds->lds_def_striping_set ||
4197 lds->lds_dir_def_striping_set));
4199 if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
4200 lo->ldo_dir_stripe_offset)) {
4202 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
4203 int stripe_count = lo->ldo_dir_stripe_count;
4205 if (info->lti_ea_store_size < sizeof(*v1)) {
4206 rc = lod_ea_store_resize(info, sizeof(*v1));
4209 v1 = info->lti_ea_store;
4212 memset(v1, 0, sizeof(*v1));
4213 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
4214 v1->lum_stripe_count = cpu_to_le32(stripe_count);
4215 v1->lum_stripe_offset =
4216 cpu_to_le32(lo->ldo_dir_stripe_offset);
4218 info->lti_buf.lb_buf = v1;
4219 info->lti_buf.lb_len = sizeof(*v1);
4220 lmu = &info->lti_buf;
4224 rc = lod_declare_xattr_set_lmv(env, dt, attr, lmu, dof,
4227 rc = lod_xattr_set_lmv(env, dt, lmu, XATTR_NAME_LMV, 0,
4232 /* foreign LMV EA case */
4234 struct lmv_foreign_md *lfm = lmu->lb_buf;
4236 if (lfm->lfm_magic == LMV_MAGIC_FOREIGN) {
4237 rc = lod_declare_xattr_set_lmv(env, dt, attr,
4241 if (lo->ldo_dir_is_foreign) {
4242 LASSERT(lo->ldo_foreign_lmv != NULL &&
4243 lo->ldo_foreign_lmv_size > 0);
4244 info->lti_buf.lb_buf = lo->ldo_foreign_lmv;
4245 info->lti_buf.lb_len = lo->ldo_foreign_lmv_size;
4246 lmu = &info->lti_buf;
4247 rc = lod_xattr_set_lmv(env, dt, lmu,
4248 XATTR_NAME_LMV, 0, th);
4253 /* Transfer default LMV striping from the parent */
4254 if (lds != NULL && lds->lds_dir_def_striping_set &&
4255 lds->lds_dir_def_max_inherit != LMV_INHERIT_END &&
4256 lds->lds_dir_def_max_inherit != LMV_INHERIT_NONE &&
4257 !(LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
4258 lds->lds_dir_def_stripe_offset) &&
4259 le32_to_cpu(lds->lds_dir_def_hash_type) !=
4260 LMV_HASH_TYPE_UNKNOWN)) {
4261 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
4263 if (info->lti_ea_store_size < sizeof(*v1)) {
4264 rc = lod_ea_store_resize(info, sizeof(*v1));
4267 v1 = info->lti_ea_store;
4270 memset(v1, 0, sizeof(*v1));
4271 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
4272 v1->lum_stripe_count =
4273 cpu_to_le32(lds->lds_dir_def_stripe_count);
4274 v1->lum_stripe_offset =
4275 cpu_to_le32(lds->lds_dir_def_stripe_offset);
4277 cpu_to_le32(lds->lds_dir_def_hash_type);
4278 v1->lum_max_inherit =
4279 lmv_inherit_next(lds->lds_dir_def_max_inherit);
4280 v1->lum_max_inherit_rr =
4281 lmv_inherit_rr_next(lds->lds_dir_def_max_inherit_rr);
4283 info->lti_buf.lb_buf = v1;
4284 info->lti_buf.lb_len = sizeof(*v1);
4286 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4287 XATTR_NAME_DEFAULT_LMV,
4290 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
4292 XATTR_NAME_DEFAULT_LMV, 0,
4298 /* Transfer default LOV striping from the parent */
4299 if (lds != NULL && lds->lds_def_striping_set &&
4300 lds->lds_def_comp_cnt != 0) {
4301 struct lov_mds_md *lmm;
4302 int lmm_size = lod_comp_md_size(lo, true);
4304 if (info->lti_ea_store_size < lmm_size) {
4305 rc = lod_ea_store_resize(info, lmm_size);
4309 lmm = info->lti_ea_store;
4311 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
4315 info->lti_buf.lb_buf = lmm;
4316 info->lti_buf.lb_len = lmm_size;
4319 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4320 XATTR_NAME_LOV, 0, th);
4322 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4323 XATTR_NAME_LOV, 0, th);
4331 static int lod_declare_dir_striping_create(const struct lu_env *env,
4332 struct dt_object *dt,
4333 struct lu_attr *attr,
4335 struct dt_object_format *dof,
4338 return lod_dir_striping_create_internal(env, dt, attr, lmu, dof, th,
4342 static int lod_dir_striping_create(const struct lu_env *env,
4343 struct dt_object *dt,
4344 struct lu_attr *attr,
4345 struct dt_object_format *dof,
4348 return lod_dir_striping_create_internal(env, dt, attr, NULL, dof, th,
4353 * Make LOV EA for striped object.
4355 * Generate striping information and store it in the LOV EA of the given
4356 * object. The caller must ensure nobody else is calling the function
4357 * against the object concurrently. The transaction must be started.
4358 * FLDB service must be running as well; it's used to map FID to the target,
4359 * which is stored in LOV EA.
4361 * \param[in] env execution environment for this thread
4362 * \param[in] lo LOD object
4363 * \param[in] th transaction handle
4365 * \retval 0 if LOV EA is stored successfully
4366 * \retval negative error number on failure
4368 static int lod_generate_and_set_lovea(const struct lu_env *env,
4369 struct lod_object *lo,
4372 struct lod_thread_info *info = lod_env_info(env);
4373 struct dt_object *next = dt_object_child(&lo->ldo_obj);
4374 struct lov_mds_md_v1 *lmm;
4380 if (lo->ldo_comp_cnt == 0 && !lo->ldo_is_foreign) {
4381 lod_striping_free_nolock(env, lo);
4382 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
4386 lmm_size = lod_comp_md_size(lo, false);
4387 if (info->lti_ea_store_size < lmm_size) {
4388 rc = lod_ea_store_resize(info, lmm_size);
4392 lmm = info->lti_ea_store;
4394 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
4398 info->lti_buf.lb_buf = lmm;
4399 info->lti_buf.lb_len = lmm_size;
4400 rc = lod_sub_xattr_set(env, next, &info->lti_buf,
4401 XATTR_NAME_LOV, 0, th);
4405 static __u32 lod_gen_component_id(struct lod_object *lo,
4406 int mirror_id, int comp_idx);
4409 * Repeat an existing component
4411 * Creates a new layout by replicating an existing component. Uses striping
4412 * policy from previous component as a template for the striping for the new
4415 * New component starts with zero length, will be extended (or removed) before
4416 * returning layout to client.
4418 * NB: Reallocates layout components array (lo->ldo_comp_entries), invalidating
4419 * any pre-existing pointers to components. Handle with care.
4421 * \param[in] env execution environment for this thread
4422 * \param[in,out] lo object to update the layout of
4423 * \param[in] index index of component to copy
4425 * \retval 0 on success
4426 * \retval negative errno on error
4428 static int lod_layout_repeat_comp(const struct lu_env *env,
4429 struct lod_object *lo, int index)
4431 struct lod_layout_component *lod_comp;
4432 struct lod_layout_component *new_comp = NULL;
4433 struct lod_layout_component *comp_array;
4434 int rc = 0, i, new_cnt = lo->ldo_comp_cnt + 1;
4439 lod_comp = &lo->ldo_comp_entries[index];
4440 LASSERT(lod_comp_inited(lod_comp) && lod_comp->llc_id != LCME_ID_INVAL);
4442 CDEBUG(D_LAYOUT, "repeating component %d\n", index);
4444 OBD_ALLOC_PTR_ARRAY(comp_array, new_cnt);
4445 if (comp_array == NULL)
4446 GOTO(out, rc = -ENOMEM);
4448 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4449 memcpy(&comp_array[i + offset], &lo->ldo_comp_entries[i],
4450 sizeof(*comp_array));
4452 /* Duplicate this component in to the next slot */
4454 new_comp = &comp_array[i + 1];
4455 memcpy(&comp_array[i + 1], &lo->ldo_comp_entries[i],
4456 sizeof(*comp_array));
4457 /* We must now skip this new component when copying */
4462 /* Set up copied component */
4463 new_comp->llc_flags &= ~LCME_FL_INIT;
4464 new_comp->llc_stripe = NULL;
4465 new_comp->llc_stripes_allocated = 0;
4466 new_comp->llc_ost_indices = NULL;
4467 new_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4468 /* for uninstantiated components, layout gen stores default stripe
4470 new_comp->llc_layout_gen = lod_comp->llc_stripe_offset;
4471 /* This makes the repeated component zero-length, placed at the end of
4472 * the preceding component */
4473 new_comp->llc_extent.e_start = new_comp->llc_extent.e_end;
4474 new_comp->llc_timestamp = lod_comp->llc_timestamp;
4475 new_comp->llc_pool = NULL;
4477 rc = lod_set_pool(&new_comp->llc_pool, lod_comp->llc_pool);
4481 if (new_comp->llc_ostlist.op_array) {
4482 __u32 *op_array = NULL;
4484 OBD_ALLOC(op_array, new_comp->llc_ostlist.op_size);
4486 GOTO(out, rc = -ENOMEM);
4487 memcpy(op_array, &new_comp->llc_ostlist.op_array,
4488 new_comp->llc_ostlist.op_size);
4489 new_comp->llc_ostlist.op_array = op_array;
4492 OBD_FREE_PTR_ARRAY(lo->ldo_comp_entries, lo->ldo_comp_cnt);
4493 lo->ldo_comp_entries = comp_array;
4494 lo->ldo_comp_cnt = new_cnt;
4496 /* Generate an id for the new component */
4497 mirror_id = mirror_id_of(new_comp->llc_id);
4498 new_comp->llc_id = LCME_ID_INVAL;
4499 new_comp->llc_id = lod_gen_component_id(lo, mirror_id, index + 1);
4500 if (new_comp->llc_id == LCME_ID_INVAL)
4501 GOTO(out, rc = -ERANGE);
4506 OBD_FREE_PTR_ARRAY(comp_array, new_cnt);
4511 static int lod_layout_data_init(struct lod_thread_info *info, __u32 comp_cnt)
4515 /* clear memory region that will be used for layout change */
4516 memset(&info->lti_layout_attr, 0, sizeof(struct lu_attr));
4517 info->lti_count = 0;
4519 if (info->lti_comp_size >= comp_cnt)
4522 if (info->lti_comp_size > 0) {
4523 OBD_FREE_PTR_ARRAY(info->lti_comp_idx, info->lti_comp_size);
4524 info->lti_comp_size = 0;
4527 OBD_ALLOC_PTR_ARRAY(info->lti_comp_idx, comp_cnt);
4528 if (!info->lti_comp_idx)
4531 info->lti_comp_size = comp_cnt;
4536 * Prepare new layout minus deleted components
4538 * Removes components marked for deletion (LCME_ID_INVAL) by copying to a new
4539 * layout and skipping those components. Removes stripe objects if any exist.
4542 * Reallocates layout components array (lo->ldo_comp_entries), invalidating
4543 * any pre-existing pointers to components.
4545 * Caller is responsible for updating mirror end (ldo_mirror[].lme_end).
4547 * \param[in] env execution environment for this thread
4548 * \param[in,out] lo object to update the layout of
4549 * \param[in] th transaction handle for this operation
4551 * \retval # of components deleted
4552 * \retval negative errno on error
4554 static int lod_layout_del_prep_layout(const struct lu_env *env,
4555 struct lod_object *lo,
4558 struct lod_layout_component *lod_comp;
4559 struct lod_thread_info *info = lod_env_info(env);
4560 int rc = 0, i, j, deleted = 0;
4564 LASSERT(lo->ldo_is_composite);
4565 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
4567 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
4571 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4572 lod_comp = &lo->ldo_comp_entries[i];
4574 if (lod_comp->llc_id != LCME_ID_INVAL) {
4575 /* Build array of things to keep */
4576 info->lti_comp_idx[info->lti_count++] = i;
4580 lod_obj_set_pool(lo, i, NULL);
4581 if (lod_comp->llc_ostlist.op_array) {
4582 OBD_FREE(lod_comp->llc_ostlist.op_array,
4583 lod_comp->llc_ostlist.op_size);
4584 lod_comp->llc_ostlist.op_array = NULL;
4585 lod_comp->llc_ostlist.op_size = 0;
4589 CDEBUG(D_LAYOUT, "deleting comp %d, left %d\n", i,
4590 lo->ldo_comp_cnt - deleted);
4592 /* No striping info for this component */
4593 if (lod_comp->llc_stripe == NULL)
4596 LASSERT(lod_comp->llc_stripe_count > 0);
4597 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4598 struct dt_object *obj = lod_comp->llc_stripe[j];
4603 /* components which are not init have no sub objects
4605 if (lod_comp_inited(lod_comp)) {
4606 rc = lod_sub_destroy(env, obj, th);
4611 lu_object_put(env, &obj->do_lu);
4612 lod_comp->llc_stripe[j] = NULL;
4614 OBD_FREE_PTR_ARRAY(lod_comp->llc_stripe,
4615 lod_comp->llc_stripes_allocated);
4616 lod_comp->llc_stripe = NULL;
4617 OBD_FREE_PTR_ARRAY(lod_comp->llc_ost_indices,
4618 lod_comp->llc_stripes_allocated);
4619 lod_comp->llc_ost_indices = NULL;
4620 lod_comp->llc_stripes_allocated = 0;
4623 /* info->lti_count has the amount of left components */
4624 LASSERTF(info->lti_count >= 0 && info->lti_count < lo->ldo_comp_cnt,
4625 "left = %d, lo->ldo_comp_cnt %d\n", (int)info->lti_count,
4626 (int)lo->ldo_comp_cnt);
4628 if (info->lti_count > 0) {
4629 struct lod_layout_component *comp_array;
4631 OBD_ALLOC_PTR_ARRAY(comp_array, info->lti_count);
4632 if (comp_array == NULL)
4633 GOTO(out, rc = -ENOMEM);
4635 for (i = 0; i < info->lti_count; i++) {
4636 memcpy(&comp_array[i],
4637 &lo->ldo_comp_entries[info->lti_comp_idx[i]],
4638 sizeof(*comp_array));
4641 OBD_FREE_PTR_ARRAY(lo->ldo_comp_entries, lo->ldo_comp_cnt);
4642 lo->ldo_comp_entries = comp_array;
4643 lo->ldo_comp_cnt = info->lti_count;
4645 lod_free_comp_entries(lo);
4650 return rc ? rc : deleted;
4654 * Delete layout component(s)
4656 * This function sets up the layout data in the env and does the setattrs
4657 * required to write out the new layout. The layout itself is modified in
4658 * lod_layout_del_prep_layout.
4660 * \param[in] env execution environment for this thread
4661 * \param[in] dt object
4662 * \param[in] th transaction handle
4664 * \retval 0 on success
4665 * \retval negative error number on failure
4667 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
4670 struct lod_object *lo = lod_dt_obj(dt);
4671 struct dt_object *next = dt_object_child(dt);
4672 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4675 LASSERT(lo->ldo_mirror_count == 1);
4677 mutex_lock(&lo->ldo_layout_mutex);
4679 rc = lod_layout_del_prep_layout(env, lo, th);
4683 /* Only do this if we didn't delete all components */
4684 if (lo->ldo_comp_cnt > 0) {
4685 lo->ldo_mirrors[0].lme_end = lo->ldo_comp_cnt - 1;
4686 lod_obj_inc_layout_gen(lo);
4689 LASSERT(dt_object_exists(dt));
4690 rc = dt_attr_get(env, next, attr);
4694 if (attr->la_size > 0) {
4696 attr->la_valid = LA_SIZE;
4697 rc = lod_sub_attr_set(env, next, attr, th);
4702 rc = lod_generate_and_set_lovea(env, lo, th);
4706 lod_striping_free_nolock(env, lo);
4708 mutex_unlock(&lo->ldo_layout_mutex);
4714 static int lod_get_default_lov_striping(const struct lu_env *env,
4715 struct lod_object *lo,
4716 struct lod_default_striping *lds,
4717 struct dt_allocation_hint *ah);
4719 * Implementation of dt_object_operations::do_xattr_set.
4721 * Sets specified extended attribute on the object. Three types of EAs are
4723 * LOV EA - stores striping for a regular file or default striping (when set
4725 * LMV EA - stores a marker for the striped directories
4726 * DMV EA - stores default directory striping
4728 * When striping is applied to a non-striped existing object (this is called
4729 * late striping), then LOD notices the caller wants to turn the object into a
4730 * striped one. The stripe objects are created and appropriate EA is set:
4731 * LOV EA storing all the stripes directly or LMV EA storing just a small header
4732 * with striping configuration.
4734 * \see dt_object_operations::do_xattr_set() in the API description for details.
4736 static int lod_xattr_set(const struct lu_env *env,
4737 struct dt_object *dt, const struct lu_buf *buf,
4738 const char *name, int fl, struct thandle *th)
4740 struct dt_object *next = dt_object_child(dt);
4745 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4746 !strcmp(name, XATTR_NAME_LMV)) {
4748 case LU_XATTR_CREATE:
4749 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
4752 case LU_XATTR_REPLACE:
4753 rc = lod_dir_layout_set(env, dt, buf, fl, th);
4760 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4761 strcmp(name, XATTR_NAME_LOV) == 0) {
4762 struct lod_default_striping *lds = lod_lds_buf_get(env);
4763 struct lov_user_md_v1 *v1 = buf->lb_buf;
4764 char pool[LOV_MAXPOOLNAME + 1];
4767 /* get existing striping config */
4768 rc = lod_get_default_lov_striping(env, lod_dt_obj(dt), lds,
4773 memset(pool, 0, sizeof(pool));
4774 if (lds->lds_def_striping_set == 1)
4775 lod_layout_get_pool(lds->lds_def_comp_entries,
4776 lds->lds_def_comp_cnt, pool,
4779 is_del = LOVEA_DELETE_VALUES(v1->lmm_stripe_size,
4780 v1->lmm_stripe_count,
4781 v1->lmm_stripe_offset,
4784 /* Retain the pool name if it is not given */
4785 if (v1->lmm_magic == LOV_USER_MAGIC_V1 && pool[0] != '\0' &&
4787 struct lod_thread_info *info = lod_env_info(env);
4788 struct lov_user_md_v3 *v3 = info->lti_ea_store;
4790 memset(v3, 0, sizeof(*v3));
4791 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
4792 v3->lmm_pattern = cpu_to_le32(v1->lmm_pattern);
4793 v3->lmm_stripe_count =
4794 cpu_to_le32(v1->lmm_stripe_count);
4795 v3->lmm_stripe_offset =
4796 cpu_to_le32(v1->lmm_stripe_offset);
4797 v3->lmm_stripe_size = cpu_to_le32(v1->lmm_stripe_size);
4799 strlcpy(v3->lmm_pool_name, pool,
4800 sizeof(v3->lmm_pool_name));
4802 info->lti_buf.lb_buf = v3;
4803 info->lti_buf.lb_len = sizeof(*v3);
4804 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4807 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name,
4811 if (lds->lds_def_striping_set == 1 &&
4812 lds->lds_def_comp_entries != NULL)
4813 lod_free_def_comp_entries(lds);
4816 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4817 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
4819 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
4822 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4823 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4824 strcmp(name, XATTR_LUSTRE_LOV) == 0 ||
4825 allowed_lustre_lov(name))) {
4826 /* in case of lov EA swap, just set it
4827 * if not, it is a replay so check striping match what we
4828 * already have during req replay, declare_xattr_set()
4829 * defines striping, then create() does the work */
4830 if (fl & LU_XATTR_REPLACE) {
4831 /* free stripes, then update disk */
4832 lod_striping_free(env, lod_dt_obj(dt));
4834 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
4835 } else if (fl & LU_XATTR_PURGE) {
4836 rc = lod_layout_purge(env, dt, buf, th);
4837 } else if (dt_object_remote(dt)) {
4838 /* This only happens during migration, see
4839 * mdd_migrate_create(), in which Master MDT will
4840 * create a remote target object, and only set
4841 * (migrating) stripe EA on the remote object,
4842 * and does not need creating each stripes. */
4843 rc = lod_sub_xattr_set(env, next, buf, name,
4845 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
4846 /* delete component(s) */
4847 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
4848 rc = lod_layout_del(env, dt, th);
4851 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
4852 * it's going to create create file with specified
4853 * component(s), the striping must have not being
4854 * cached in this case;
4856 * Otherwise, it's going to add/change component(s) to
4857 * an existing file, the striping must have been cached
4860 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
4861 !strcmp(name, XATTR_NAME_LOV),
4862 !lod_dt_obj(dt)->ldo_comp_cached));
4864 rc = lod_striped_create(env, dt, NULL, NULL, th);
4867 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
4868 rc = lod_replace_parent_fid(env, dt, buf, th, false);
4873 /* then all other xattr */
4874 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
4880 * Implementation of dt_object_operations::do_declare_xattr_del.
4882 * \see dt_object_operations::do_declare_xattr_del() in the API description
4885 static int lod_declare_xattr_del(const struct lu_env *env,
4886 struct dt_object *dt, const char *name,
4889 struct lod_object *lo = lod_dt_obj(dt);
4890 struct dt_object *next = dt_object_child(dt);
4895 rc = lod_sub_declare_xattr_del(env, next, name, th);
4899 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4902 /* NB: don't delete stripe LMV, because when we do this, normally we
4903 * will remove stripes, besides, if directory LMV is corrupt, this will
4904 * prevent deleting its LMV and fixing it (via LFSCK).
4906 if (!strcmp(name, XATTR_NAME_LMV))
4909 rc = lod_striping_load(env, lo);
4913 if (lo->ldo_dir_stripe_count == 0)
4916 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4917 struct dt_object *dto = lo->ldo_stripe[i];
4922 if (!dt_object_exists(dto))
4925 rc = lod_sub_declare_xattr_del(env, dto, name, th);
4934 * Implementation of dt_object_operations::do_xattr_del.
4936 * If EA storing a regular striping is being deleted, then release
4937 * all the references to the stripe objects in core.
4939 * \see dt_object_operations::do_xattr_del() in the API description for details.
4941 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
4942 const char *name, struct thandle *th)
4948 if (!strcmp(name, XATTR_NAME_LOV) || !strcmp(name, XATTR_NAME_LMV))
4949 lod_striping_free(env, lod_dt_obj(dt));
4951 rc = lod_xattr_del_internal(env, dt, name, th);
4957 * Implementation of dt_object_operations::do_xattr_list.
4959 * \see dt_object_operations::do_xattr_list() in the API description
4962 static int lod_xattr_list(const struct lu_env *env,
4963 struct dt_object *dt, const struct lu_buf *buf)
4965 return dt_xattr_list(env, dt_object_child(dt), buf);
4968 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
4970 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
4974 * Copy OST list from layout provided by user.
4976 * \param[in] lod_comp layout_component to be filled
4977 * \param[in] v3 LOV EA V3 user data
4979 * \retval 0 on success
4980 * \retval negative if failed
4982 int lod_comp_copy_ost_lists(struct lod_layout_component *lod_comp,
4983 struct lov_user_md_v3 *v3)
4989 if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
4990 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
4992 if (lod_comp->llc_ostlist.op_array) {
4993 if (lod_comp->llc_ostlist.op_size >=
4994 v3->lmm_stripe_count * sizeof(__u32)) {
4995 lod_comp->llc_ostlist.op_count =
4996 v3->lmm_stripe_count;
4999 OBD_FREE(lod_comp->llc_ostlist.op_array,
5000 lod_comp->llc_ostlist.op_size);
5003 /* copy ost list from lmm */
5004 lod_comp->llc_ostlist.op_count = v3->lmm_stripe_count;
5005 lod_comp->llc_ostlist.op_size = v3->lmm_stripe_count * sizeof(__u32);
5006 OBD_ALLOC(lod_comp->llc_ostlist.op_array,
5007 lod_comp->llc_ostlist.op_size);
5008 if (!lod_comp->llc_ostlist.op_array)
5011 for (j = 0; j < v3->lmm_stripe_count; j++) {
5012 lod_comp->llc_ostlist.op_array[j] =
5013 v3->lmm_objects[j].l_ost_idx;
5021 * Get default striping.
5023 * \param[in] env execution environment
5024 * \param[in] lo object
5025 * \param[out] lds default striping
5027 * \retval 0 on success
5028 * \retval negative if failed
5030 static int lod_get_default_lov_striping(const struct lu_env *env,
5031 struct lod_object *lo,
5032 struct lod_default_striping *lds,
5033 struct dt_allocation_hint *ah)
5035 struct lod_thread_info *info = lod_env_info(env);
5036 struct lov_user_md_v1 *v1 = NULL;
5037 struct lov_user_md_v3 *v3 = NULL;
5038 struct lov_comp_md_v1 *comp_v1 = NULL;
5046 rc = lod_get_lov_ea(env, lo);
5050 if (rc < (typeof(rc))sizeof(struct lov_user_md))
5053 v1 = info->lti_ea_store;
5054 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
5055 lustre_swab_lov_user_md_v1(v1);
5056 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
5057 v3 = (struct lov_user_md_v3 *)v1;
5058 lustre_swab_lov_user_md_v3(v3);
5059 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_SPECIFIC)) {
5060 v3 = (struct lov_user_md_v3 *)v1;
5061 lustre_swab_lov_user_md_v3(v3);
5062 lustre_swab_lov_user_md_objects(v3->lmm_objects,
5063 v3->lmm_stripe_count);
5064 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1) ||
5065 v1->lmm_magic == __swab32(LOV_USER_MAGIC_SEL)) {
5066 comp_v1 = (struct lov_comp_md_v1 *)v1;
5067 lustre_swab_lov_comp_md_v1(comp_v1);
5070 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
5071 v1->lmm_magic != LOV_MAGIC_COMP_V1 &&
5072 v1->lmm_magic != LOV_MAGIC_SEL &&
5073 v1->lmm_magic != LOV_USER_MAGIC_SPECIFIC)
5076 if ((v1->lmm_magic == LOV_MAGIC_COMP_V1 ||
5077 v1->lmm_magic == LOV_MAGIC_SEL) &&
5078 !(ah && ah->dah_append_stripes)) {
5079 comp_v1 = (struct lov_comp_md_v1 *)v1;
5080 comp_cnt = comp_v1->lcm_entry_count;
5083 mirror_cnt = comp_v1->lcm_mirror_count + 1;
5091 /* realloc default comp entries if necessary */
5092 rc = lod_def_striping_comp_resize(lds, comp_cnt);
5096 lds->lds_def_comp_cnt = comp_cnt;
5097 lds->lds_def_striping_is_composite = composite;
5098 lds->lds_def_mirror_cnt = mirror_cnt;
5100 for (i = 0; i < comp_cnt; i++) {
5101 struct lod_layout_component *lod_comp;
5104 lod_comp = &lds->lds_def_comp_entries[i];
5106 * reset lod_comp values, llc_stripes is always NULL in
5107 * the default striping template, llc_pool will be reset
5110 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
5113 v1 = (struct lov_user_md *)((char *)comp_v1 +
5114 comp_v1->lcm_entries[i].lcme_offset);
5115 lod_comp->llc_extent =
5116 comp_v1->lcm_entries[i].lcme_extent;
5117 /* We only inherit certain flags from the layout */
5118 lod_comp->llc_flags =
5119 comp_v1->lcm_entries[i].lcme_flags &
5120 LCME_TEMPLATE_FLAGS;
5123 if (!lov_pattern_supported(v1->lmm_pattern) &&
5124 !(v1->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
5125 lod_free_def_comp_entries(lds);
5129 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d stripe_offset=%d append_stripes=%d\n",
5130 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
5131 (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
5132 (int)v1->lmm_stripe_offset,
5133 ah ? ah->dah_append_stripes : 0);
5135 if (ah && ah->dah_append_stripes)
5136 lod_comp->llc_stripe_count = ah->dah_append_stripes;
5138 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
5139 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
5140 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
5141 lod_comp->llc_pattern = v1->lmm_pattern;
5144 if (ah && ah->dah_append_pool && ah->dah_append_pool[0]) {
5145 pool = ah->dah_append_pool;
5146 } else if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
5147 /* XXX: sanity check here */
5148 v3 = (struct lov_user_md_v3 *) v1;
5149 if (v3->lmm_pool_name[0] != '\0')
5150 pool = v3->lmm_pool_name;
5152 lod_set_def_pool(lds, i, pool);
5153 if (v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
5154 v3 = (struct lov_user_md_v3 *)v1;
5155 rc = lod_comp_copy_ost_lists(lod_comp, v3);
5158 } else if (lod_comp->llc_ostlist.op_array &&
5159 lod_comp->llc_ostlist.op_count) {
5160 for (j = 0; j < lod_comp->llc_ostlist.op_count; j++)
5161 lod_comp->llc_ostlist.op_array[j] = -1;
5162 lod_comp->llc_ostlist.op_count = 0;
5166 lds->lds_def_striping_set = 1;
5171 * Get default directory striping.
5173 * \param[in] env execution environment
5174 * \param[in] lo object
5175 * \param[out] lds default striping
5177 * \retval 0 on success
5178 * \retval negative if failed
5180 static int lod_get_default_lmv_striping(const struct lu_env *env,
5181 struct lod_object *lo,
5182 struct lod_default_striping *lds)
5184 struct lmv_user_md *lmu;
5187 lds->lds_dir_def_striping_set = 0;
5189 rc = lod_get_default_lmv_ea(env, lo);
5193 if (rc >= (int)sizeof(*lmu)) {
5194 struct lod_thread_info *info = lod_env_info(env);
5196 lmu = info->lti_ea_store;
5198 lds->lds_dir_def_stripe_count =
5199 le32_to_cpu(lmu->lum_stripe_count);
5200 lds->lds_dir_def_stripe_offset =
5201 le32_to_cpu(lmu->lum_stripe_offset);
5202 lds->lds_dir_def_hash_type =
5203 le32_to_cpu(lmu->lum_hash_type);
5204 lds->lds_dir_def_max_inherit = lmu->lum_max_inherit;
5205 lds->lds_dir_def_max_inherit_rr = lmu->lum_max_inherit_rr;
5206 lds->lds_dir_def_striping_set = 1;
5213 * Get default striping in the object.
5215 * Get object default striping and default directory striping.
5217 * \param[in] env execution environment
5218 * \param[in] lo object
5219 * \param[out] lds default striping
5221 * \retval 0 on success
5222 * \retval negative if failed
5224 static int lod_get_default_striping(const struct lu_env *env,
5225 struct lod_object *lo,
5226 struct lod_default_striping *lds)
5230 rc = lod_get_default_lov_striping(env, lo, lds, NULL);
5231 rc1 = lod_get_default_lmv_striping(env, lo, lds);
5232 if (rc == 0 && rc1 < 0)
5239 * Apply default striping on object.
5241 * If object striping pattern is not set, set to the one in default striping.
5242 * The default striping is from parent or fs.
5244 * \param[in] lo new object
5245 * \param[in] lds default striping
5246 * \param[in] mode new object's mode
5248 static void lod_striping_from_default(struct lod_object *lo,
5249 const struct lod_default_striping *lds,
5252 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5255 if (lds->lds_def_striping_set && S_ISREG(mode)) {
5256 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
5258 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
5259 lds->lds_def_comp_cnt);
5263 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
5264 if (lds->lds_def_mirror_cnt > 1)
5265 lo->ldo_flr_state = LCM_FL_RDONLY;
5267 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5268 struct lod_layout_component *obj_comp =
5269 &lo->ldo_comp_entries[i];
5270 struct lod_layout_component *def_comp =
5271 &lds->lds_def_comp_entries[i];
5273 CDEBUG(D_LAYOUT, "Inherit from default: flags=%#x "
5274 "size=%hu nr=%u offset=%u pattern=%#x pool=%s\n",
5275 def_comp->llc_flags,
5276 def_comp->llc_stripe_size,
5277 def_comp->llc_stripe_count,
5278 def_comp->llc_stripe_offset,
5279 def_comp->llc_pattern,
5280 def_comp->llc_pool ?: "");
5282 *obj_comp = *def_comp;
5283 if (def_comp->llc_pool != NULL) {
5284 /* pointer was copied from def_comp */
5285 obj_comp->llc_pool = NULL;
5286 lod_obj_set_pool(lo, i, def_comp->llc_pool);
5290 if (def_comp->llc_ostlist.op_array &&
5291 def_comp->llc_ostlist.op_count) {
5292 OBD_ALLOC(obj_comp->llc_ostlist.op_array,
5293 obj_comp->llc_ostlist.op_size);
5294 if (!obj_comp->llc_ostlist.op_array)
5296 memcpy(obj_comp->llc_ostlist.op_array,
5297 def_comp->llc_ostlist.op_array,
5298 obj_comp->llc_ostlist.op_size);
5299 } else if (def_comp->llc_ostlist.op_array) {
5300 obj_comp->llc_ostlist.op_array = NULL;
5304 * Don't initialize these fields for plain layout
5305 * (v1/v3) here, they are inherited in the order of
5306 * 'parent' -> 'fs default (root)' -> 'global default
5307 * values for stripe_count & stripe_size'.
5309 * see lod_ah_init().
5311 if (!lo->ldo_is_composite)
5314 lod_adjust_stripe_info(obj_comp, desc, 0);
5316 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
5317 if (lo->ldo_dir_stripe_count == 0)
5318 lo->ldo_dir_stripe_count =
5319 lds->lds_dir_def_stripe_count;
5320 if (lo->ldo_dir_stripe_offset == -1)
5321 lo->ldo_dir_stripe_offset =
5322 lds->lds_dir_def_stripe_offset;
5323 if (lo->ldo_dir_hash_type == 0)
5324 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
5326 CDEBUG(D_LAYOUT, "striping from default dir: count:%hu, "
5327 "offset:%u, hash_type:%u\n",
5328 lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
5329 lo->ldo_dir_hash_type);
5333 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root,
5336 struct lod_layout_component *lod_comp;
5338 if (lo->ldo_comp_cnt == 0)
5341 if (lo->ldo_is_composite)
5344 lod_comp = &lo->ldo_comp_entries[0];
5346 if (lod_comp->llc_stripe_count <= 0 ||
5347 lod_comp->llc_stripe_size <= 0)
5350 if (from_root && (lod_comp->llc_pool == NULL ||
5351 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
5354 if (append_pool && append_pool[0])
5361 * Implementation of dt_object_operations::do_ah_init.
5363 * This method is used to make a decision on the striping configuration for the
5364 * object being created. It can be taken from the \a parent object if it exists,
5365 * or filesystem's default. The resulting configuration (number of stripes,
5366 * stripe size/offset, pool name, etc) is stored in the object itself and will
5367 * be used by the methods like ->doo_declare_create().
5369 * \see dt_object_operations::do_ah_init() in the API description for details.
5371 static void lod_ah_init(const struct lu_env *env,
5372 struct dt_allocation_hint *ah,
5373 struct dt_object *parent,
5374 struct dt_object *child,
5377 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
5378 struct lod_thread_info *info = lod_env_info(env);
5379 struct lod_default_striping *lds = lod_lds_buf_get(env);
5380 struct dt_object *nextp = NULL;
5381 struct dt_object *nextc;
5382 struct lod_object *lp = NULL;
5383 struct lod_object *lc;
5384 struct lov_desc *desc;
5385 struct lod_layout_component *lod_comp;
5391 if (ah->dah_append_stripes == -1)
5392 ah->dah_append_stripes =
5393 d->lod_ost_descs.ltd_lov_desc.ld_tgt_count;
5395 if (likely(parent)) {
5396 nextp = dt_object_child(parent);
5397 lp = lod_dt_obj(parent);
5400 nextc = dt_object_child(child);
5401 lc = lod_dt_obj(child);
5403 LASSERT(!lod_obj_is_striped(child));
5404 /* default layout template may have been set on the regular file
5405 * when this is called from mdd_create_data() */
5406 if (S_ISREG(child_mode))
5407 lod_free_comp_entries(lc);
5409 if (!dt_object_exists(nextc))
5410 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
5412 if (S_ISDIR(child_mode)) {
5413 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
5415 /* other default values are 0 */
5416 lc->ldo_dir_stripe_offset = -1;
5418 /* no default striping configuration is needed for
5421 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5422 le32_to_cpu(lum1->lum_magic) == LMV_MAGIC_FOREIGN) {
5423 lc->ldo_dir_is_foreign = true;
5424 /* keep stripe_count 0 and stripe_offset -1 */
5425 CDEBUG(D_INFO, "no default striping for foreign dir\n");
5430 * If parent object is not root directory,
5431 * then get default striping from parent object.
5433 if (likely(lp != NULL)) {
5434 lod_get_default_striping(env, lp, lds);
5436 /* inherit default striping except ROOT */
5437 if ((lds->lds_def_striping_set ||
5438 lds->lds_dir_def_striping_set) &&
5439 !fid_is_root(lod_object_fid(lp)))
5440 lc->ldo_def_striping = lds;
5443 /* It should always honour the specified stripes */
5444 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
5445 * will have old magic. In this case, we should ignore the
5446 * stripe count and try to create dir by default stripe.
5448 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5449 (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
5450 le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
5451 lc->ldo_dir_stripe_count =
5452 le32_to_cpu(lum1->lum_stripe_count);
5453 lc->ldo_dir_stripe_offset =
5454 le32_to_cpu(lum1->lum_stripe_offset);
5455 lc->ldo_dir_hash_type =
5456 le32_to_cpu(lum1->lum_hash_type);
5458 "set dirstripe: count %hu, offset %d, hash %u\n",
5459 lc->ldo_dir_stripe_count,
5460 (int)lc->ldo_dir_stripe_offset,
5461 lc->ldo_dir_hash_type);
5463 /* transfer defaults LMV to new directory */
5464 lod_striping_from_default(lc, lds, child_mode);
5466 /* set count 0 to create normal directory */
5467 if (lc->ldo_dir_stripe_count == 1)
5468 lc->ldo_dir_stripe_count = 0;
5471 /* shrink the stripe_count to the avaible MDT count */
5472 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
5473 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)) {
5474 lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
5475 if (lc->ldo_dir_stripe_count == 1)
5476 lc->ldo_dir_stripe_count = 0;
5479 if (!(lc->ldo_dir_hash_type & LMV_HASH_TYPE_MASK))
5480 lc->ldo_dir_hash_type |=
5481 d->lod_mdt_descs.ltd_lmv_desc.ld_pattern;
5483 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
5484 lc->ldo_dir_stripe_count,
5485 (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
5490 /* child object regular file*/
5492 if (!lod_object_will_be_striped(S_ISREG(child_mode),
5493 lu_object_fid(&child->do_lu)))
5496 /* If object is going to be striped over OSTs, transfer default
5497 * striping information to the child, so that we can use it
5498 * during declaration and creation.
5500 * Try from the parent first.
5502 if (likely(lp != NULL)) {
5503 rc = lod_get_default_lov_striping(env, lp, lds, ah);
5505 lod_striping_from_default(lc, lds, child_mode);
5508 /* Initialize lod_device::lod_md_root object reference */
5509 if (d->lod_md_root == NULL) {
5510 struct dt_object *root;
5511 struct lod_object *lroot;
5513 lu_root_fid(&info->lti_fid);
5514 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
5515 if (!IS_ERR(root)) {
5516 lroot = lod_dt_obj(root);
5518 spin_lock(&d->lod_lock);
5519 if (d->lod_md_root != NULL)
5520 dt_object_put(env, &d->lod_md_root->ldo_obj);
5521 d->lod_md_root = lroot;
5522 spin_unlock(&d->lod_lock);
5526 /* try inherit layout from the root object (fs default) when:
5527 * - parent does not have default layout; or
5528 * - parent has plain(v1/v3) default layout, and some attributes
5529 * are not specified in the default layout;
5531 if (d->lod_md_root != NULL &&
5532 lod_need_inherit_more(lc, true, ah->dah_append_pool)) {
5533 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds,
5537 if (lc->ldo_comp_cnt == 0) {
5538 lod_striping_from_default(lc, lds, child_mode);
5539 } else if (!lds->lds_def_striping_is_composite) {
5540 struct lod_layout_component *def_comp;
5542 LASSERT(!lc->ldo_is_composite);
5543 lod_comp = &lc->ldo_comp_entries[0];
5544 def_comp = &lds->lds_def_comp_entries[0];
5546 if (lod_comp->llc_stripe_count <= 0)
5547 lod_comp->llc_stripe_count =
5548 def_comp->llc_stripe_count;
5549 if (lod_comp->llc_stripe_size <= 0)
5550 lod_comp->llc_stripe_size =
5551 def_comp->llc_stripe_size;
5552 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT &&
5553 (!lod_comp->llc_pool || !lod_comp->llc_pool[0]))
5554 lod_comp->llc_stripe_offset =
5555 def_comp->llc_stripe_offset;
5556 if (lod_comp->llc_pool == NULL)
5557 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
5562 * fs default striping may not be explicitly set, or historically set
5563 * in config log, use them.
5565 if (lod_need_inherit_more(lc, false, ah->dah_append_pool)) {
5566 if (lc->ldo_comp_cnt == 0) {
5567 rc = lod_alloc_comp_entries(lc, 0, 1);
5569 /* fail to allocate memory, will create a
5570 * non-striped file. */
5572 lc->ldo_is_composite = 0;
5573 lod_comp = &lc->ldo_comp_entries[0];
5574 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
5576 LASSERT(!lc->ldo_is_composite);
5577 lod_comp = &lc->ldo_comp_entries[0];
5578 desc = &d->lod_ost_descs.ltd_lov_desc;
5579 lod_adjust_stripe_info(lod_comp, desc, ah->dah_append_stripes);
5580 if (ah->dah_append_pool && ah->dah_append_pool[0])
5581 lod_obj_set_pool(lc, 0, ah->dah_append_pool);
5588 * Size initialization on late striping.
5590 * Propagate the size of a truncated object to a deferred striping.
5591 * This function handles a special case when truncate was done on a
5592 * non-striped object and now while the striping is being created
5593 * we can't lose that size, so we have to propagate it to the stripes
5596 * \param[in] env execution environment
5597 * \param[in] dt object
5598 * \param[in] th transaction handle
5600 * \retval 0 on success
5601 * \retval negative if failed
5603 static int lod_declare_init_size(const struct lu_env *env,
5604 struct dt_object *dt, struct thandle *th)
5606 struct dt_object *next = dt_object_child(dt);
5607 struct lod_object *lo = lod_dt_obj(dt);
5608 struct dt_object **objects = NULL;
5609 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
5610 uint64_t size, offs;
5611 int i, rc, stripe, stripe_count = 0, stripe_size = 0;
5612 struct lu_extent size_ext;
5615 if (!lod_obj_is_striped(dt))
5618 rc = dt_attr_get(env, next, attr);
5619 LASSERT(attr->la_valid & LA_SIZE);
5623 size = attr->la_size;
5627 size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
5628 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5629 struct lod_layout_component *lod_comp;
5630 struct lu_extent *extent;
5632 lod_comp = &lo->ldo_comp_entries[i];
5634 if (lod_comp->llc_stripe == NULL)
5637 extent = &lod_comp->llc_extent;
5638 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
5639 if (!lo->ldo_is_composite ||
5640 lu_extent_is_overlapped(extent, &size_ext)) {
5641 objects = lod_comp->llc_stripe;
5642 stripe_count = lod_comp->llc_stripe_count;
5643 stripe_size = lod_comp->llc_stripe_size;
5646 if (stripe_count == 0)
5649 LASSERT(objects != NULL && stripe_size != 0);
5650 do_div(size, stripe_size);
5651 stripe = do_div(size, stripe_count);
5652 LASSERT(objects[stripe] != NULL);
5654 size = size * stripe_size;
5655 offs = attr->la_size;
5656 size += do_div(offs, stripe_size);
5658 attr->la_valid = LA_SIZE;
5659 attr->la_size = size;
5661 rc = lod_sub_declare_attr_set(env, objects[stripe],
5670 * Declare creation of striped object.
5672 * The function declares creation stripes for a regular object. The function
5673 * also declares whether the stripes will be created with non-zero size if
5674 * previously size was set non-zero on the master object. If object \a dt is
5675 * not local, then only fully defined striping can be applied in \a lovea.
5676 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
5679 * \param[in] env execution environment
5680 * \param[in] dt object
5681 * \param[in] attr attributes the stripes will be created with
5682 * \param[in] lovea a buffer containing striping description
5683 * \param[in] th transaction handle
5685 * \retval 0 on success
5686 * \retval negative if failed
5688 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
5689 struct lu_attr *attr,
5690 const struct lu_buf *lovea, struct thandle *th)
5692 struct lod_thread_info *info = lod_env_info(env);
5693 struct dt_object *next = dt_object_child(dt);
5694 struct lod_object *lo = lod_dt_obj(dt);
5698 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
5699 GOTO(out, rc = -ENOMEM);
5701 if (!dt_object_remote(next)) {
5702 /* choose OST and generate appropriate objects */
5703 rc = lod_prepare_create(env, lo, attr, lovea, th);
5708 * declare storage for striping data
5710 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5712 /* LOD can not choose OST objects for remote objects, i.e.
5713 * stripes must be ready before that. Right now, it can only
5714 * happen during migrate, i.e. migrate process needs to create
5715 * remote regular file (mdd_migrate_create), then the migrate
5716 * process will provide stripeEA. */
5717 LASSERT(lovea != NULL);
5718 info->lti_buf = *lovea;
5721 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
5722 XATTR_NAME_LOV, 0, th);
5727 * if striping is created with local object's size > 0,
5728 * we have to propagate this size to specific object
5729 * the case is possible only when local object was created previously
5731 if (dt_object_exists(next))
5732 rc = lod_declare_init_size(env, dt, th);
5735 /* failed to create striping or to set initial size, let's reset
5736 * config so that others don't get confused */
5738 lod_striping_free(env, lo);
5744 * Whether subdirectories under \a dt should be created on MDTs by space QoS
5746 * If LMV_HASH_FLAG_SPACE is set on directory default layout, its subdirectories
5747 * should be created on MDT by space QoS.
5749 * \param[in] env execution environment
5750 * \param[in] dev lu device
5751 * \param[in] dt object
5753 * \retval 1 if directory should create subdir by space usage
5755 * \retval -ev if failed
5757 static inline int dt_object_qos_mkdir(const struct lu_env *env,
5758 struct lu_device *dev,
5759 struct dt_object *dt)
5761 struct lod_thread_info *info = lod_env_info(env);
5762 struct lu_object *obj;
5763 struct lod_object *lo;
5764 struct lmv_user_md *lmu;
5767 obj = lu_object_find_slice(env, dev, lu_object_fid(&dt->do_lu), NULL);
5769 return PTR_ERR(obj);
5771 lo = lu2lod_obj(obj);
5773 rc = lod_get_default_lmv_ea(env, lo);
5774 dt_object_put(env, dt);
5778 if (rc < (int)sizeof(*lmu))
5781 lmu = info->lti_ea_store;
5782 return le32_to_cpu(lmu->lum_stripe_offset) == LMV_OFFSET_DEFAULT;
5786 * Implementation of dt_object_operations::do_declare_create.
5788 * The method declares creation of a new object. If the object will be striped,
5789 * then helper functions are called to find FIDs for the stripes, declare
5790 * creation of the stripes and declare initialization of the striping
5791 * information to be stored in the master object.
5793 * \see dt_object_operations::do_declare_create() in the API description
5796 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
5797 struct lu_attr *attr,
5798 struct dt_allocation_hint *hint,
5799 struct dt_object_format *dof, struct thandle *th)
5801 struct dt_object *next = dt_object_child(dt);
5802 struct lod_object *lo = lod_dt_obj(dt);
5811 * first of all, we declare creation of local object
5813 rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
5818 * it's lod_ah_init() that has decided the object will be striped
5820 if (dof->dof_type == DFT_REGULAR) {
5821 /* callers don't want stripes */
5822 /* XXX: all tricky interactions with ->ah_make_hint() decided
5823 * to use striping, then ->declare_create() behaving differently
5824 * should be cleaned */
5825 if (dof->u.dof_reg.striped != 0)
5826 rc = lod_declare_striped_create(env, dt, attr,
5828 } else if (dof->dof_type == DFT_DIR) {
5829 struct seq_server_site *ss;
5830 struct lu_buf buf = { NULL };
5831 struct lu_buf *lmu = NULL;
5833 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
5835 /* If the parent has default stripeEA, and client
5836 * did not find it before sending create request,
5837 * then MDT will return -EREMOTE, and client will
5838 * retrieve the default stripeEA and re-create the
5841 * Note: if dah_eadata != NULL, it means creating the
5842 * striped directory with specified stripeEA, then it
5843 * should ignore the default stripeEA */
5844 if (hint != NULL && hint->dah_eadata == NULL) {
5845 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
5846 GOTO(out, rc = -EREMOTE);
5848 if (lo->ldo_dir_stripe_offset == LMV_OFFSET_DEFAULT) {
5849 struct lod_default_striping *lds;
5851 lds = lo->ldo_def_striping;
5853 * child and parent should be on the same MDT,
5854 * but if parent has default LMV, and the start
5855 * MDT offset is -1, it's allowed. This check
5856 * is not necessary after 2.12.22 because client
5857 * follows this already, but old client may not.
5859 if (hint->dah_parent &&
5860 dt_object_remote(hint->dah_parent) && lds &&
5861 lds->lds_dir_def_stripe_offset !=
5863 GOTO(out, rc = -EREMOTE);
5864 } else if (lo->ldo_dir_stripe_offset !=
5866 struct lod_device *lod;
5867 struct lu_tgt_desc *mdt = NULL;
5868 bool found_mdt = false;
5870 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5871 lod_foreach_mdt(lod, mdt) {
5872 if (mdt->ltd_index ==
5873 lo->ldo_dir_stripe_offset) {
5879 /* If the MDT indicated by stripe_offset can be
5880 * found, then tell client to resend the create
5881 * request to the correct MDT, otherwise return
5882 * error to client */
5884 GOTO(out, rc = -EREMOTE);
5886 GOTO(out, rc = -EINVAL);
5888 } else if (hint && hint->dah_eadata) {
5890 lmu->lb_buf = (void *)hint->dah_eadata;
5891 lmu->lb_len = hint->dah_eadata_len;
5894 rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
5898 /* failed to create striping or to set initial size, let's reset
5899 * config so that others don't get confused */
5901 lod_striping_free(env, lo);
5906 * Generate component ID for new created component.
5908 * \param[in] lo LOD object
5909 * \param[in] comp_idx index of ldo_comp_entries
5911 * \retval component ID on success
5912 * \retval LCME_ID_INVAL on failure
5914 static __u32 lod_gen_component_id(struct lod_object *lo,
5915 int mirror_id, int comp_idx)
5917 struct lod_layout_component *lod_comp;
5918 __u32 id, start, end;
5921 LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
5923 lod_obj_inc_layout_gen(lo);
5924 id = lo->ldo_layout_gen;
5925 if (likely(id <= SEQ_ID_MAX))
5926 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
5928 /* Layout generation wraps, need to check collisions. */
5929 start = id & SEQ_ID_MASK;
5932 for (id = start; id <= end; id++) {
5933 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5934 lod_comp = &lo->ldo_comp_entries[i];
5935 if (pflr_id(mirror_id, id) == lod_comp->llc_id)
5938 /* Found the ununsed ID */
5939 if (i == lo->ldo_comp_cnt)
5940 RETURN(pflr_id(mirror_id, id));
5942 if (end == LCME_ID_MAX) {
5944 end = min(lo->ldo_layout_gen & LCME_ID_MASK,
5945 (__u32)(LCME_ID_MAX - 1));
5949 RETURN(LCME_ID_INVAL);
5953 * Creation of a striped regular object.
5955 * The function is called to create the stripe objects for a regular
5956 * striped file. This can happen at the initial object creation or
5957 * when the caller asks LOD to do so using ->do_xattr_set() method
5958 * (so called late striping). Notice all the information are already
5959 * prepared in the form of the list of objects (ldo_stripe field).
5960 * This is done during declare phase.
5962 * \param[in] env execution environment
5963 * \param[in] dt object
5964 * \param[in] attr attributes the stripes will be created with
5965 * \param[in] dof format of stripes (see OSD API description)
5966 * \param[in] th transaction handle
5968 * \retval 0 on success
5969 * \retval negative if failed
5971 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
5972 struct lu_attr *attr, struct dt_object_format *dof,
5975 struct lod_layout_component *lod_comp;
5976 struct lod_object *lo = lod_dt_obj(dt);
5981 mutex_lock(&lo->ldo_layout_mutex);
5983 LASSERT((lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL) ||
5984 lo->ldo_is_foreign);
5986 mirror_id = 0; /* non-flr file's mirror_id is 0 */
5987 if (lo->ldo_mirror_count > 1) {
5988 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5989 lod_comp = &lo->ldo_comp_entries[i];
5990 if (lod_comp->llc_id != LCME_ID_INVAL &&
5991 mirror_id_of(lod_comp->llc_id) > mirror_id)
5992 mirror_id = mirror_id_of(lod_comp->llc_id);
5996 /* create all underlying objects */
5997 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5998 lod_comp = &lo->ldo_comp_entries[i];
6000 if (lod_comp->llc_id == LCME_ID_INVAL) {
6001 /* only the component of FLR layout with more than 1
6002 * mirror has mirror ID in its component ID.
6004 if (lod_comp->llc_extent.e_start == 0 &&
6005 lo->ldo_mirror_count > 1)
6008 lod_comp->llc_id = lod_gen_component_id(lo,
6010 if (lod_comp->llc_id == LCME_ID_INVAL)
6011 GOTO(out, rc = -ERANGE);
6014 if (lod_comp_inited(lod_comp))
6017 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
6018 lod_comp_set_init(lod_comp);
6020 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
6021 lod_comp_set_init(lod_comp);
6023 if (lod_comp->llc_stripe == NULL)
6026 LASSERT(lod_comp->llc_stripe_count);
6027 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6028 struct dt_object *object = lod_comp->llc_stripe[j];
6029 LASSERT(object != NULL);
6030 rc = lod_sub_create(env, object, attr, NULL, dof, th);
6034 lod_comp_set_init(lod_comp);
6037 rc = lod_fill_mirrors(lo);
6041 lo->ldo_comp_cached = 1;
6043 rc = lod_generate_and_set_lovea(env, lo, th);
6047 mutex_unlock(&lo->ldo_layout_mutex);
6052 lod_striping_free_nolock(env, lo);
6053 mutex_unlock(&lo->ldo_layout_mutex);
6058 static inline bool lod_obj_is_dom(struct dt_object *dt)
6060 struct lod_object *lo = lod_dt_obj(dt);
6062 if (!dt_object_exists(dt_object_child(dt)))
6065 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
6068 if (!lo->ldo_comp_cnt)
6071 return (lov_pattern(lo->ldo_comp_entries[0].llc_pattern) ==
6076 * Implementation of dt_object_operations::do_create.
6078 * If any of preceeding methods (like ->do_declare_create(),
6079 * ->do_ah_init(), etc) chose to create a striped object,
6080 * then this method will create the master and the stripes.
6082 * \see dt_object_operations::do_create() in the API description for details.
6084 static int lod_create(const struct lu_env *env, struct dt_object *dt,
6085 struct lu_attr *attr, struct dt_allocation_hint *hint,
6086 struct dt_object_format *dof, struct thandle *th)
6091 /* create local object */
6092 rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
6096 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
6097 (lod_obj_is_striped(dt) || lod_obj_is_dom(dt)) &&
6098 dof->u.dof_reg.striped != 0) {
6099 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
6100 rc = lod_striped_create(env, dt, attr, dof, th);
6107 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
6108 struct dt_object *dt, struct thandle *th,
6109 int comp_idx, int stripe_idx,
6110 struct lod_obj_stripe_cb_data *data)
6112 if (data->locd_declare)
6113 return lod_sub_declare_destroy(env, dt, th);
6115 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
6116 stripe_idx == cfs_fail_val)
6117 return lod_sub_destroy(env, dt, th);
6123 * Implementation of dt_object_operations::do_declare_destroy.
6125 * If the object is a striped directory, then the function declares reference
6126 * removal from the master object (this is an index) to the stripes and declares
6127 * destroy of all the stripes. In all the cases, it declares an intention to
6128 * destroy the object itself.
6130 * \see dt_object_operations::do_declare_destroy() in the API description
6133 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
6136 struct dt_object *next = dt_object_child(dt);
6137 struct lod_object *lo = lod_dt_obj(dt);
6138 struct lod_thread_info *info = lod_env_info(env);
6139 struct dt_object *stripe;
6140 char *stripe_name = info->lti_key;
6146 * load striping information, notice we don't do this when object
6147 * is being initialized as we don't need this information till
6148 * few specific cases like destroy, chown
6150 rc = lod_striping_load(env, lo);
6154 /* declare destroy for all underlying objects */
6155 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6156 rc = next->do_ops->do_index_try(env, next,
6157 &dt_directory_features);
6161 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6162 stripe = lo->ldo_stripe[i];
6166 rc = lod_sub_declare_ref_del(env, next, th);
6170 snprintf(stripe_name, sizeof(info->lti_key),
6172 PFID(lu_object_fid(&stripe->do_lu)), i);
6173 rc = lod_sub_declare_delete(env, next,
6174 (const struct dt_key *)stripe_name, th);
6181 * we declare destroy for the local object
6183 rc = lod_sub_declare_destroy(env, next, th);
6187 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
6188 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
6191 if (!lod_obj_is_striped(dt))
6194 /* declare destroy all striped objects */
6195 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6196 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6197 stripe = lo->ldo_stripe[i];
6201 if (!dt_object_exists(stripe))
6204 rc = lod_sub_declare_ref_del(env, stripe, th);
6208 rc = lod_sub_declare_destroy(env, stripe, th);
6213 struct lod_obj_stripe_cb_data data = { { 0 } };
6215 data.locd_declare = true;
6216 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6217 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6224 * Implementation of dt_object_operations::do_destroy.
6226 * If the object is a striped directory, then the function removes references
6227 * from the master object (this is an index) to the stripes and destroys all
6228 * the stripes. In all the cases, the function destroys the object itself.
6230 * \see dt_object_operations::do_destroy() in the API description for details.
6232 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
6235 struct dt_object *next = dt_object_child(dt);
6236 struct lod_object *lo = lod_dt_obj(dt);
6237 struct lod_thread_info *info = lod_env_info(env);
6238 char *stripe_name = info->lti_key;
6239 struct dt_object *stripe;
6245 /* destroy sub-stripe of master object */
6246 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6247 rc = next->do_ops->do_index_try(env, next,
6248 &dt_directory_features);
6252 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6253 stripe = lo->ldo_stripe[i];
6257 rc = lod_sub_ref_del(env, next, th);
6261 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
6262 PFID(lu_object_fid(&stripe->do_lu)), i);
6264 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
6265 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
6266 PFID(lu_object_fid(&stripe->do_lu)));
6268 rc = lod_sub_delete(env, next,
6269 (const struct dt_key *)stripe_name, th);
6275 rc = lod_sub_destroy(env, next, th);
6279 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
6280 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
6283 if (!lod_obj_is_striped(dt))
6286 /* destroy all striped objects */
6287 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6288 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6289 stripe = lo->ldo_stripe[i];
6293 if (!dt_object_exists(stripe))
6296 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
6297 i == cfs_fail_val) {
6298 dt_write_lock(env, stripe, DT_TGT_CHILD);
6299 rc = lod_sub_ref_del(env, stripe, th);
6300 dt_write_unlock(env, stripe);
6304 rc = lod_sub_destroy(env, stripe, th);
6310 struct lod_obj_stripe_cb_data data = { { 0 } };
6312 data.locd_declare = false;
6313 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6314 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6321 * Implementation of dt_object_operations::do_declare_ref_add.
6323 * \see dt_object_operations::do_declare_ref_add() in the API description
6326 static int lod_declare_ref_add(const struct lu_env *env,
6327 struct dt_object *dt, struct thandle *th)
6329 return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
6333 * Implementation of dt_object_operations::do_ref_add.
6335 * \see dt_object_operations::do_ref_add() in the API description for details.
6337 static int lod_ref_add(const struct lu_env *env,
6338 struct dt_object *dt, struct thandle *th)
6340 return lod_sub_ref_add(env, dt_object_child(dt), th);
6344 * Implementation of dt_object_operations::do_declare_ref_del.
6346 * \see dt_object_operations::do_declare_ref_del() in the API description
6349 static int lod_declare_ref_del(const struct lu_env *env,
6350 struct dt_object *dt, struct thandle *th)
6352 return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
6356 * Implementation of dt_object_operations::do_ref_del
6358 * \see dt_object_operations::do_ref_del() in the API description for details.
6360 static int lod_ref_del(const struct lu_env *env,
6361 struct dt_object *dt, struct thandle *th)
6363 return lod_sub_ref_del(env, dt_object_child(dt), th);
6367 * Implementation of dt_object_operations::do_object_sync.
6369 * \see dt_object_operations::do_object_sync() in the API description
6372 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
6373 __u64 start, __u64 end)
6375 return dt_object_sync(env, dt_object_child(dt), start, end);
6379 * Implementation of dt_object_operations::do_object_unlock.
6381 * Used to release LDLM lock(s).
6383 * \see dt_object_operations::do_object_unlock() in the API description
6386 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
6387 struct ldlm_enqueue_info *einfo,
6388 union ldlm_policy_data *policy)
6390 struct lod_object *lo = lod_dt_obj(dt);
6391 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
6392 int slave_locks_size;
6396 if (slave_locks == NULL)
6399 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
6400 /* Note: for remote lock for single stripe dir, MDT will cancel
6401 * the lock by lockh directly */
6402 LASSERT(!dt_object_remote(dt_object_child(dt)));
6404 /* locks were unlocked in MDT layer */
6405 for (i = 0; i < slave_locks->ha_count; i++)
6406 LASSERT(!lustre_handle_is_used(&slave_locks->ha_handles[i]));
6409 * NB, ha_count may not equal to ldo_dir_stripe_count, because dir
6410 * layout may change, e.g., shrink dir layout after migration.
6412 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6413 if (lo->ldo_stripe[i])
6414 dt_invalidate(env, lo->ldo_stripe[i]);
6417 slave_locks_size = offsetof(typeof(*slave_locks),
6418 ha_handles[slave_locks->ha_count]);
6419 OBD_FREE(slave_locks, slave_locks_size);
6420 einfo->ei_cbdata = NULL;
6426 * Implementation of dt_object_operations::do_object_lock.
6428 * Used to get LDLM lock on the non-striped and striped objects.
6430 * \see dt_object_operations::do_object_lock() in the API description
6433 static int lod_object_lock(const struct lu_env *env,
6434 struct dt_object *dt,
6435 struct lustre_handle *lh,
6436 struct ldlm_enqueue_info *einfo,
6437 union ldlm_policy_data *policy)
6439 struct lod_object *lo = lod_dt_obj(dt);
6440 int slave_locks_size;
6441 struct lustre_handle_array *slave_locks = NULL;
6446 /* remote object lock */
6447 if (!einfo->ei_enq_slave) {
6448 LASSERT(dt_object_remote(dt));
6449 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
6453 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
6456 rc = lod_striping_load(env, lo);
6461 if (lo->ldo_dir_stripe_count <= 1)
6464 slave_locks_size = offsetof(typeof(*slave_locks),
6465 ha_handles[lo->ldo_dir_stripe_count]);
6466 /* Freed in lod_object_unlock */
6467 OBD_ALLOC(slave_locks, slave_locks_size);
6470 slave_locks->ha_count = lo->ldo_dir_stripe_count;
6472 /* striped directory lock */
6473 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6474 struct lustre_handle lockh;
6475 struct ldlm_res_id *res_id;
6476 struct dt_object *stripe;
6478 stripe = lo->ldo_stripe[i];
6482 res_id = &lod_env_info(env)->lti_res_id;
6483 fid_build_reg_res_name(lu_object_fid(&stripe->do_lu), res_id);
6484 einfo->ei_res_id = res_id;
6486 if (dt_object_remote(stripe)) {
6487 set_bit(i, (void *)slave_locks->ha_map);
6488 rc = dt_object_lock(env, stripe, &lockh, einfo, policy);
6490 struct ldlm_namespace *ns = einfo->ei_namespace;
6491 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
6492 ldlm_completion_callback completion = einfo->ei_cb_cp;
6493 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
6495 if (einfo->ei_mode == LCK_PW ||
6496 einfo->ei_mode == LCK_EX)
6497 dlmflags |= LDLM_FL_COS_INCOMPAT;
6499 LASSERT(ns != NULL);
6500 rc = ldlm_cli_enqueue_local(env, ns, res_id, LDLM_IBITS,
6501 policy, einfo->ei_mode,
6502 &dlmflags, blocking,
6504 NULL, 0, LVB_T_NONE,
6509 ldlm_lock_decref_and_cancel(
6510 &slave_locks->ha_handles[i],
6512 OBD_FREE(slave_locks, slave_locks_size);
6515 slave_locks->ha_handles[i] = lockh;
6517 einfo->ei_cbdata = slave_locks;
6523 * Implementation of dt_object_operations::do_invalidate.
6525 * \see dt_object_operations::do_invalidate() in the API description for details
6527 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
6529 return dt_invalidate(env, dt_object_child(dt));
6532 static int lod_declare_instantiate_components(const struct lu_env *env,
6533 struct lod_object *lo,
6537 struct lod_thread_info *info = lod_env_info(env);
6542 LASSERT(info->lti_count < lo->ldo_comp_cnt);
6544 for (i = 0; i < info->lti_count; i++) {
6545 rc = lod_qos_prep_create(env, lo, NULL, th,
6546 info->lti_comp_idx[i], reserve);
6552 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6553 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
6554 &info->lti_buf, XATTR_NAME_LOV, 0, th);
6561 * Check OSTs for an existing component for further extension
6563 * Checks if OSTs are still healthy and not out of space. Gets free space
6564 * on OSTs (relative to allocation watermark rmb_low) and compares to
6565 * the proposed new_end for this component.
6567 * Decides whether or not to extend a component on its current OSTs.
6569 * \param[in] env execution environment for this thread
6570 * \param[in] lo object we're checking
6571 * \param[in] index index of this component
6572 * \param[in] extension_size extension size for this component
6573 * \param[in] extent layout extent for requested operation
6574 * \param[in] comp_extent extension component extent
6575 * \param[in] write if this is write operation
6577 * \retval true - OK to extend on current OSTs
6578 * \retval false - do not extend on current OSTs
6580 static bool lod_sel_osts_allowed(const struct lu_env *env,
6581 struct lod_object *lo,
6582 int index, __u64 reserve,
6583 struct lu_extent *extent,
6584 struct lu_extent *comp_extent, int write)
6586 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[index];
6587 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6588 struct lod_thread_info *tinfo = lod_env_info(env);
6589 struct obd_statfs *sfs = &tinfo->lti_osfs;
6590 __u64 available = 0;
6596 LASSERT(lod_comp->llc_stripe_count != 0);
6598 lod_getref(&lod->lod_ost_descs);
6599 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
6600 int index = lod_comp->llc_ost_indices[i];
6601 struct lod_tgt_desc *ost = OST_TGT(lod, index);
6602 struct obd_statfs_info info = { 0 };
6603 int j, repeated = 0;
6607 /* Get the number of times this OST repeats in this component.
6608 * Note: inter-component repeats are not counted as this is
6609 * considered as a rare case: we try to not repeat OST in other
6610 * components if possible. */
6611 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6612 if (index != lod_comp->llc_ost_indices[j])
6615 /* already handled */
6621 if (j < lod_comp->llc_stripe_count)
6624 if (!test_bit(index, lod->lod_ost_bitmap)) {
6625 CDEBUG(D_LAYOUT, "ost %d no longer present\n", index);
6630 rc = dt_statfs_info(env, ost->ltd_tgt, sfs, &info);
6632 CDEBUG(D_LAYOUT, "statfs failed for ost %d, error %d\n",
6638 if (sfs->os_state & OS_STATFS_ENOSPC ||
6639 sfs->os_state & OS_STATFS_READONLY ||
6640 sfs->os_state & OS_STATFS_DEGRADED) {
6641 CDEBUG(D_LAYOUT, "ost %d is not availble for SEL "
6642 "extension, state %u\n", index, sfs->os_state);
6648 available = sfs->os_bavail * sfs->os_bsize;
6649 /* 'available' is relative to the allocation threshold */
6650 available -= (__u64) info.os_reserved_mb_low << 20;
6652 CDEBUG(D_LAYOUT, "ost %d lowwm: %d highwm: %d, "
6653 "%llu %% blocks available, %llu %% blocks free\n",
6654 index, info.os_reserved_mb_low, info.os_reserved_mb_high,
6655 (100ull * sfs->os_bavail) / sfs->os_blocks,
6656 (100ull * sfs->os_bfree) / sfs->os_blocks);
6658 if (reserve * repeated > available) {
6660 CDEBUG(D_LAYOUT, "low space on ost %d, available %llu "
6661 "< extension size %llu repeated %d\n", index,
6662 available, reserve, repeated);
6666 lod_putref(lod, &lod->lod_ost_descs);
6672 * Adjust extents after component removal
6674 * When we remove an extension component, we move the start of the next
6675 * component to match the start of the extension component, so no space is left
6678 * \param[in] env execution environment for this thread
6679 * \param[in] lo object
6680 * \param[in] max_comp layout component
6681 * \param[in] index index of this component
6683 * \retval 0 on success
6684 * \retval negative errno on error
6686 static void lod_sel_adjust_extents(const struct lu_env *env,
6687 struct lod_object *lo,
6688 int max_comp, int index)
6690 struct lod_layout_component *lod_comp = NULL;
6691 struct lod_layout_component *next = NULL;
6692 struct lod_layout_component *prev = NULL;
6693 __u64 new_start = 0;
6697 /* Extension space component */
6698 lod_comp = &lo->ldo_comp_entries[index];
6699 next = &lo->ldo_comp_entries[index + 1];
6700 prev = &lo->ldo_comp_entries[index - 1];
6702 LASSERT(lod_comp != NULL && prev != NULL && next != NULL);
6703 LASSERT(lod_comp->llc_flags & LCME_FL_EXTENSION);
6705 /* Previous is being removed */
6706 if (prev && prev->llc_id == LCME_ID_INVAL)
6707 new_start = prev->llc_extent.e_start;
6709 new_start = lod_comp->llc_extent.e_start;
6711 for (i = index + 1; i < max_comp; i++) {
6712 lod_comp = &lo->ldo_comp_entries[i];
6714 start = lod_comp->llc_extent.e_start;
6715 lod_comp->llc_extent.e_start = new_start;
6717 /* We only move zero length extendable components */
6718 if (!(start == lod_comp->llc_extent.e_end))
6721 LASSERT(!(lod_comp->llc_flags & LCME_FL_INIT));
6723 lod_comp->llc_extent.e_end = new_start;
6727 /* Calculate the proposed 'new end' for a component we're extending */
6728 static __u64 lod_extension_new_end(__u64 extension_size, __u64 extent_end,
6729 __u32 stripe_size, __u64 component_end,
6730 __u64 extension_end)
6734 LASSERT(extension_size != 0 && stripe_size != 0);
6736 /* Round up to extension size */
6737 if (extent_end == OBD_OBJECT_EOF) {
6738 new_end = OBD_OBJECT_EOF;
6740 /* Add at least extension_size to the previous component_end,
6741 * covering the req layout extent */
6742 new_end = max(extent_end - component_end, extension_size);
6743 new_end = roundup(new_end, extension_size);
6744 new_end += component_end;
6746 /* Component end must be min stripe size aligned */
6747 if (new_end % stripe_size) {
6748 CDEBUG(D_LAYOUT, "new component end is not aligned "
6749 "by the stripe size %u: [%llu, %llu) ext size "
6750 "%llu new end %llu, aligning\n",
6751 stripe_size, component_end, extent_end,
6752 extension_size, new_end);
6753 new_end = roundup(new_end, stripe_size);
6757 if (new_end < extent_end)
6758 new_end = OBD_OBJECT_EOF;
6761 /* Don't extend past the end of the extension component */
6762 if (new_end > extension_end)
6763 new_end = extension_end;
6769 * Calculate the exact reservation (per-OST extension_size) on the OSTs being
6770 * instantiated. It needs to be calculated in advance and taken into account at
6771 * the instantiation time, because otherwise lod_statfs_and_check() may consider
6772 * an OST as OK, but SEL needs its extension_size to fit the free space and the
6773 * OST may turn out to be low-on-space, thus inappropriate OST may be used and
6776 * \param[in] lod_comp lod component we are checking
6778 * \retval size to reserved on each OST of lod_comp's stripe.
6780 static __u64 lod_sel_stripe_reserved(struct lod_layout_component *lod_comp)
6782 /* extension_size is file level, so we must divide by stripe count to
6783 * compare it to available space on a single OST */
6784 return lod_comp->llc_stripe_size * SEL_UNIT_SIZE /
6785 lod_comp->llc_stripe_count;
6788 /* As lod_sel_handler() could be re-entered for the same component several
6789 * times, this is the data for the next call. Fields could be changed to
6790 * component indexes when needed, (e.g. if there is no need to instantiate
6791 * all the previous components up to the current position) to tell the caller
6792 * where to start over from. */
6799 * Process extent updates for a particular layout component
6801 * Handle layout updates for a particular extension space component touched by
6802 * a layout update operation. Core function of self-extending PFL feature.
6804 * In general, this function processes exactly *one* stage of an extension
6805 * operation, modifying the layout accordingly, then returns to the caller.
6806 * The caller is responsible for restarting processing with the new layout,
6807 * which may repeatedly return to this function until the extension updates
6810 * This function does one of a few things to the layout:
6811 * 1. Extends the component before the current extension space component to
6812 * allow it to accomodate the requested operation (if space/policy permit that
6813 * component to continue on its current OSTs)
6815 * 2. If extension of the existing component fails, we do one of two things:
6816 * a. If there is a component after the extension space, we remove the
6817 * extension space component, move the start of the next component down
6818 * accordingly, then notify the caller to restart processing w/the new
6820 * b. If there is no following component, we try repeating the current
6821 * component, creating a new component using the current one as a
6822 * template (keeping its stripe properties but not specific striping),
6823 * and try assigning striping for this component. If there is sufficient
6824 * free space on the OSTs chosen for this component, it is instantiated
6825 * and i/o continues there.
6827 * If there is not sufficient space on the new OSTs, we remove this new
6828 * component & extend the current component.
6830 * Note further that uninited components followed by extension space can be zero
6831 * length meaning that we will try to extend them before initializing them, and
6832 * if that fails, they will be removed without initialization.
6834 * 3. If we extend to/beyond the end of an extension space component, that
6835 * component is exhausted (all of its range has been given to real components),
6836 * so we remove it and restart processing.
6838 * \param[in] env execution environment for this thread
6839 * \param[in,out] lo object to update the layout of
6840 * \param[in] extent layout extent for requested operation, update
6841 * layout to fit this operation
6842 * \param[in] th transaction handle for this operation
6843 * \param[in,out] max_comp the highest comp for the portion of the layout
6844 * we are operating on (For FLR, the chosen
6845 * replica). Updated because we may remove
6847 * \param[in] index index of the extension space component we're
6849 * \param[in] write if this is write op
6850 * \param[in,out] force if the extension is to be forced; set here
6851 to force it on the 2nd call for the same
6854 * \retval 0 on success
6855 * \retval negative errno on error
6857 static int lod_sel_handler(const struct lu_env *env,
6858 struct lod_object *lo,
6859 struct lu_extent *extent,
6860 struct thandle *th, int *max_comp,
6861 int index, int write,
6862 struct sel_data *sd)
6864 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6865 struct lod_thread_info *info = lod_env_info(env);
6866 struct lod_layout_component *lod_comp;
6867 struct lod_layout_component *prev;
6868 struct lod_layout_component *next = NULL;
6869 __u64 extension_size, reserve;
6876 /* First component cannot be extension space */
6878 CERROR("%s: "DFID" first component cannot be extension space\n",
6879 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6883 lod_comp = &lo->ldo_comp_entries[index];
6884 prev = &lo->ldo_comp_entries[index - 1];
6885 if ((index + 1) < *max_comp)
6886 next = &lo->ldo_comp_entries[index + 1];
6888 /* extension size uses the stripe size field as KiB */
6889 extension_size = lod_comp->llc_stripe_size * SEL_UNIT_SIZE;
6891 CDEBUG(D_LAYOUT, "prev start %llu, extension start %llu, extension end"
6892 " %llu, extension size %llu\n", prev->llc_extent.e_start,
6893 lod_comp->llc_extent.e_start, lod_comp->llc_extent.e_end,
6896 /* Two extension space components cannot be adjacent & extension space
6897 * components cannot be init */
6898 if ((prev->llc_flags & LCME_FL_EXTENSION) ||
6899 !(ergo(next, !(next->llc_flags & LCME_FL_EXTENSION))) ||
6900 lod_comp_inited(lod_comp)) {
6901 CERROR("%s: "DFID" invalid extension space components\n",
6902 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6906 reserve = lod_sel_stripe_reserved(lod_comp);
6908 if (!prev->llc_stripe) {
6909 CDEBUG(D_LAYOUT, "Previous component not inited\n");
6910 info->lti_count = 1;
6911 info->lti_comp_idx[0] = index - 1;
6912 rc = lod_declare_instantiate_components(env, lo, th, reserve);
6913 /* ENOSPC tells us we can't use this component. If there is
6914 * a next or we are repeating, we either spill over (next) or
6915 * extend the original comp (repeat). Otherwise, return the
6916 * error to the user. */
6917 if (rc == -ENOSPC && (next || sd->sd_repeat))
6923 if (sd->sd_force == 0 && rc == 0)
6924 rc = !lod_sel_osts_allowed(env, lo, index - 1, reserve, extent,
6925 &lod_comp->llc_extent, write);
6927 repeated = !!(sd->sd_repeat);
6931 /* Extend previous component */
6933 new_end = lod_extension_new_end(extension_size, extent->e_end,
6934 prev->llc_stripe_size,
6935 prev->llc_extent.e_end,
6936 lod_comp->llc_extent.e_end);
6938 CDEBUG(D_LAYOUT, "new end %llu\n", new_end);
6939 lod_comp->llc_extent.e_start = new_end;
6940 prev->llc_extent.e_end = new_end;
6942 if (prev->llc_extent.e_end == lod_comp->llc_extent.e_end) {
6943 CDEBUG(D_LAYOUT, "Extension component exhausted\n");
6944 lod_comp->llc_id = LCME_ID_INVAL;
6948 /* rc == 1, failed to extend current component */
6951 /* Normal 'spillover' case - Remove the extension
6952 * space component & bring down the start of the next
6954 lod_comp->llc_id = LCME_ID_INVAL;
6956 if (!(prev->llc_flags & LCME_FL_INIT)) {
6957 prev->llc_id = LCME_ID_INVAL;
6960 lod_sel_adjust_extents(env, lo, *max_comp, index);
6961 } else if (lod_comp_inited(prev)) {
6962 /* If there is no next, and the previous component is
6963 * INIT'ed, try repeating the previous component. */
6964 LASSERT(repeated == 0);
6965 rc = lod_layout_repeat_comp(env, lo, index - 1);
6969 /* The previous component is a repeated component.
6970 * Record this so we don't keep trying to repeat it. */
6973 /* If the previous component is not INIT'ed, this may
6974 * be a component we have just instantiated but failed
6975 * to extend. Or even a repeated component we failed
6976 * to prepare a striping for. Do not repeat but instead
6977 * remove the repeated component & force the extention
6978 * of the original one */
6981 prev->llc_id = LCME_ID_INVAL;
6988 rc = lod_layout_del_prep_layout(env, lo, NULL);
6991 LASSERTF(-rc == change,
6992 "number deleted %d != requested %d\n", -rc,
6995 *max_comp = *max_comp + change;
6997 /* lod_del_prep_layout reallocates ldo_comp_entries, so we must
6998 * refresh these pointers before using them */
6999 lod_comp = &lo->ldo_comp_entries[index];
7000 prev = &lo->ldo_comp_entries[index - 1];
7001 CDEBUG(D_LAYOUT, "After extent updates: prev start %llu, current start "
7002 "%llu, current end %llu max_comp %d ldo_comp_cnt %d\n",
7003 prev->llc_extent.e_start, lod_comp->llc_extent.e_start,
7004 lod_comp->llc_extent.e_end, *max_comp, lo->ldo_comp_cnt);
7006 /* Layout changed successfully */
7011 * Declare layout extent updates
7013 * Handles extensions. Identifies extension components touched by current
7014 * operation and passes them to processing function.
7016 * Restarts with updated layouts from the processing function until the current
7017 * operation no longer touches an extension space component.
7019 * \param[in] env execution environment for this thread
7020 * \param[in,out] lo object to update the layout of
7021 * \param[in] extent layout extent for requested operation, update layout to
7022 * fit this operation
7023 * \param[in] th transaction handle for this operation
7024 * \param[in] pick identifies chosen mirror for FLR layouts
7025 * \param[in] write if this is write op
7027 * \retval 1 on layout changed, 0 on no change
7028 * \retval negative errno on error
7030 static int lod_declare_update_extents(const struct lu_env *env,
7031 struct lod_object *lo, struct lu_extent *extent,
7032 struct thandle *th, int pick, int write)
7034 struct lod_thread_info *info = lod_env_info(env);
7035 struct lod_layout_component *lod_comp;
7036 bool layout_changed = false;
7037 struct sel_data sd = { 0 };
7045 /* This makes us work on the components of the chosen mirror */
7046 start_index = lo->ldo_mirrors[pick].lme_start;
7047 max_comp = lo->ldo_mirrors[pick].lme_end + 1;
7048 if (lo->ldo_flr_state == LCM_FL_NONE)
7049 LASSERT(start_index == 0 && max_comp == lo->ldo_comp_cnt);
7051 CDEBUG(D_LAYOUT, "extent->e_start %llu, extent->e_end %llu\n",
7052 extent->e_start, extent->e_end);
7053 for (i = start_index; i < max_comp; i++) {
7054 lod_comp = &lo->ldo_comp_entries[i];
7056 /* We've passed all components of interest */
7057 if (lod_comp->llc_extent.e_start >= extent->e_end)
7060 if (lod_comp->llc_flags & LCME_FL_EXTENSION) {
7061 layout_changed = true;
7062 rc = lod_sel_handler(env, lo, extent, th, &max_comp,
7067 /* Nothing has changed behind the prev one */
7073 /* We may have added or removed components. If so, we must update the
7074 * start & ends of all the mirrors after the current one, and the end
7075 * of the current mirror. */
7076 change = max_comp - 1 - lo->ldo_mirrors[pick].lme_end;
7078 lo->ldo_mirrors[pick].lme_end += change;
7079 for (i = pick + 1; i < lo->ldo_mirror_count; i++) {
7080 lo->ldo_mirrors[i].lme_start += change;
7081 lo->ldo_mirrors[i].lme_end += change;
7087 /* The amount of components has changed, adjust the lti_comp_idx */
7088 rc2 = lod_layout_data_init(info, lo->ldo_comp_cnt);
7090 return rc < 0 ? rc : rc2 < 0 ? rc2 : layout_changed;
7093 /* If striping is already instantiated or INIT'ed DOM? */
7094 static bool lod_is_instantiation_needed(struct lod_layout_component *comp)
7096 return !(((lov_pattern(comp->llc_pattern) == LOV_PATTERN_MDT) &&
7097 lod_comp_inited(comp)) || comp->llc_stripe);
7101 * Declare layout update for a non-FLR layout.
7103 * \param[in] env execution environment for this thread
7104 * \param[in,out] lo object to update the layout of
7105 * \param[in] layout layout intent for requested operation, "update" is
7106 * a process of reacting to this
7107 * \param[in] buf buffer containing lov ea (see comment on usage inline)
7108 * \param[in] th transaction handle for this operation
7110 * \retval 0 on success
7111 * \retval negative errno on error
7113 static int lod_declare_update_plain(const struct lu_env *env,
7114 struct lod_object *lo, struct layout_intent *layout,
7115 const struct lu_buf *buf, struct thandle *th)
7117 struct lod_thread_info *info = lod_env_info(env);
7118 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7119 struct lod_layout_component *lod_comp;
7120 struct lov_comp_md_v1 *comp_v1 = NULL;
7121 bool layout_changed = false;
7122 bool replay = false;
7126 LASSERT(lo->ldo_flr_state == LCM_FL_NONE);
7129 * In case the client is passing lovea, which only happens during
7130 * the replay of layout intent write RPC for now, we may need to
7131 * parse the lovea and apply new layout configuration.
7133 if (buf && buf->lb_len) {
7134 struct lov_user_md_v1 *v1 = buf->lb_buf;
7136 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
7137 v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
7138 LOV_MAGIC_COMP_V1)) {
7139 CERROR("%s: the replay buffer of layout extend "
7140 "(magic %#x) does not contain expected "
7141 "composite layout.\n",
7142 lod2obd(d)->obd_name, v1->lmm_magic);
7143 GOTO(out, rc = -EINVAL);
7146 rc = lod_use_defined_striping(env, lo, buf);
7149 lo->ldo_comp_cached = 1;
7151 rc = lod_get_lov_ea(env, lo);
7154 /* old on-disk EA is stored in info->lti_buf */
7155 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
7157 layout_changed = true;
7159 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
7163 /* non replay path */
7164 rc = lod_striping_load(env, lo);
7169 /* Make sure defined layout covers the requested write range. */
7170 lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
7171 if (lo->ldo_comp_cnt > 1 &&
7172 lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
7173 lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
7174 CDEBUG_LIMIT(replay ? D_ERROR : D_LAYOUT,
7175 "%s: the defined layout [0, %#llx) does not "
7176 "covers the write range "DEXT"\n",
7177 lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
7178 PEXT(&layout->li_extent));
7179 GOTO(out, rc = -EINVAL);
7182 CDEBUG(D_LAYOUT, "%s: "DFID": update components "DEXT"\n",
7183 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
7184 PEXT(&layout->li_extent));
7187 rc = lod_declare_update_extents(env, lo, &layout->li_extent,
7188 th, 0, layout->li_opc == LAYOUT_INTENT_WRITE);
7192 layout_changed = true;
7196 * Iterate ld->ldo_comp_entries, find the component whose extent under
7197 * the write range and not instantianted.
7199 for (i = 0; i < lo->ldo_comp_cnt; i++) {
7200 lod_comp = &lo->ldo_comp_entries[i];
7202 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
7206 /* If striping is instantiated or INIT'ed DOM skip */
7207 if (!lod_is_instantiation_needed(lod_comp))
7211 * In replay path, lod_comp is the EA passed by
7212 * client replay buffer, comp_v1 is the pre-recovery
7213 * on-disk EA, we'd sift out those components which
7214 * were init-ed in the on-disk EA.
7216 if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
7221 * this component hasn't instantiated in normal path, or during
7222 * replay it needs replay the instantiation.
7225 /* A released component is being extended */
7226 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
7227 GOTO(out, rc = -EINVAL);
7229 LASSERT(info->lti_comp_idx != NULL);
7230 info->lti_comp_idx[info->lti_count++] = i;
7231 layout_changed = true;
7234 if (!layout_changed)
7237 lod_obj_inc_layout_gen(lo);
7238 rc = lod_declare_instantiate_components(env, lo, th, 0);
7242 lod_striping_free(env, lo);
7246 static inline int lod_comp_index(struct lod_object *lo,
7247 struct lod_layout_component *lod_comp)
7249 LASSERT(lod_comp >= lo->ldo_comp_entries &&
7250 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
7252 return lod_comp - lo->ldo_comp_entries;
7256 * Stale other mirrors by writing extent.
7258 static int lod_stale_components(const struct lu_env *env, struct lod_object *lo,
7259 int primary, struct lu_extent *extent,
7262 struct lod_layout_component *pri_comp, *lod_comp;
7263 struct lu_extent pri_extent;
7268 /* The writing extent decides which components in the primary
7269 * are affected... */
7270 CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
7273 lod_foreach_mirror_comp(pri_comp, lo, primary) {
7274 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
7277 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
7278 lod_comp_index(lo, pri_comp),
7279 PEXT(&pri_comp->llc_extent));
7281 pri_extent.e_start = pri_comp->llc_extent.e_start;
7282 pri_extent.e_end = pri_comp->llc_extent.e_end;
7284 for (i = 0; i < lo->ldo_mirror_count; i++) {
7287 rc = lod_declare_update_extents(env, lo, &pri_extent,
7289 /* if update_extents changed the layout, it may have
7290 * reallocated the component array, so start over to
7291 * avoid using stale pointers */
7297 /* ... and then stale other components that are
7298 * overlapping with primary components */
7299 lod_foreach_mirror_comp(lod_comp, lo, i) {
7300 if (!lu_extent_is_overlapped(
7302 &lod_comp->llc_extent))
7305 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
7306 i, lod_comp_index(lo, lod_comp));
7308 lod_comp->llc_flags |= LCME_FL_STALE;
7309 lo->ldo_mirrors[i].lme_stale = 1;
7318 * check an OST's availability
7319 * \param[in] env execution environment
7320 * \param[in] lo lod object
7321 * \param[in] dt dt object
7322 * \param[in] index mirror index
7324 * \retval negative if failed
7325 * \retval 1 if \a dt is available
7326 * \retval 0 if \a dt is not available
7328 static inline int lod_check_ost_avail(const struct lu_env *env,
7329 struct lod_object *lo,
7330 struct dt_object *dt, int index)
7332 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7333 struct lod_tgt_desc *ost;
7335 int type = LU_SEQ_RANGE_OST;
7338 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &idx, &type);
7340 CERROR("%s: can't locate "DFID":rc = %d\n",
7341 lod2obd(lod)->obd_name, PFID(lu_object_fid(&dt->do_lu)),
7346 ost = OST_TGT(lod, idx);
7347 if (ost->ltd_statfs.os_state &
7348 (OS_STATFS_READONLY | OS_STATFS_ENOSPC | OS_STATFS_ENOINO |
7349 OS_STATFS_NOPRECREATE) ||
7350 ost->ltd_active == 0) {
7351 CDEBUG(D_LAYOUT, DFID ": mirror %d OST%d unavail, rc = %d\n",
7352 PFID(lod_object_fid(lo)), index, idx, rc);
7360 * Pick primary mirror for write
7361 * \param[in] env execution environment
7362 * \param[in] lo object
7363 * \param[in] extent write range
7365 static int lod_primary_pick(const struct lu_env *env, struct lod_object *lo,
7366 struct lu_extent *extent)
7368 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7369 unsigned int seq = 0;
7370 struct lod_layout_component *lod_comp;
7372 int picked = -1, second_pick = -1, third_pick = -1;
7375 if (OBD_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
7376 get_random_bytes(&seq, sizeof(seq));
7377 seq %= lo->ldo_mirror_count;
7381 * Pick a mirror as the primary, and check the availability of OSTs.
7383 * This algo can be revised later after knowing the topology of
7386 lod_qos_statfs_update(env, lod, &lod->lod_ost_descs);
7387 for (i = 0; i < lo->ldo_mirror_count; i++) {
7388 bool ost_avail = true;
7389 int index = (i + seq) % lo->ldo_mirror_count;
7391 if (lo->ldo_mirrors[index].lme_stale) {
7392 CDEBUG(D_LAYOUT, DFID": mirror %d stale\n",
7393 PFID(lod_object_fid(lo)), index);
7397 /* 2nd pick is for the primary mirror containing unavail OST */
7398 if (lo->ldo_mirrors[index].lme_prefer && second_pick < 0)
7399 second_pick = index;
7401 /* 3rd pick is for non-primary mirror containing unavail OST */
7402 if (second_pick < 0 && third_pick < 0)
7406 * we found a non-primary 1st pick, we'd like to find a
7407 * potential pirmary mirror.
7409 if (picked >= 0 && !lo->ldo_mirrors[index].lme_prefer)
7412 /* check the availability of OSTs */
7413 lod_foreach_mirror_comp(lod_comp, lo, index) {
7414 if (!lod_comp_inited(lod_comp) || !lod_comp->llc_stripe)
7417 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
7418 struct dt_object *dt = lod_comp->llc_stripe[j];
7420 rc = lod_check_ost_avail(env, lo, dt, index);
7427 } /* for all dt object in one component */
7430 } /* for all components in a mirror */
7433 * the OSTs where allocated objects locates in the components
7434 * of the mirror are available.
7439 /* this mirror has all OSTs available */
7443 * primary with all OSTs are available, this is the perfect
7446 if (lo->ldo_mirrors[index].lme_prefer)
7448 } /* for all mirrors */
7450 /* failed to pick a sound mirror, lower our expectation */
7452 picked = second_pick;
7454 picked = third_pick;
7461 static int lod_prepare_resync_mirror(const struct lu_env *env,
7462 struct lod_object *lo,
7465 struct lod_thread_info *info = lod_env_info(env);
7466 struct lod_layout_component *lod_comp;
7467 bool neg = !!(MIRROR_ID_NEG & mirror_id);
7470 mirror_id &= ~MIRROR_ID_NEG;
7472 for (i = 0; i < lo->ldo_mirror_count; i++) {
7473 if ((!neg && lo->ldo_mirrors[i].lme_id != mirror_id) ||
7474 (neg && lo->ldo_mirrors[i].lme_id == mirror_id))
7477 lod_foreach_mirror_comp(lod_comp, lo, i) {
7478 if (lod_comp_inited(lod_comp))
7481 info->lti_comp_idx[info->lti_count++] =
7482 lod_comp_index(lo, lod_comp);
7490 * figure out the components should be instantiated for resync.
7492 static int lod_prepare_resync(const struct lu_env *env, struct lod_object *lo,
7493 struct lu_extent *extent)
7495 struct lod_thread_info *info = lod_env_info(env);
7496 struct lod_layout_component *lod_comp;
7497 unsigned int need_sync = 0;
7501 DFID": instantiate all stale components in "DEXT"\n",
7502 PFID(lod_object_fid(lo)), PEXT(extent));
7505 * instantiate all components within this extent, even non-stale
7508 for (i = 0; i < lo->ldo_mirror_count; i++) {
7509 if (!lo->ldo_mirrors[i].lme_stale)
7512 lod_foreach_mirror_comp(lod_comp, lo, i) {
7513 if (!lu_extent_is_overlapped(extent,
7514 &lod_comp->llc_extent))
7519 if (lod_comp_inited(lod_comp))
7522 CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
7523 i, lod_comp_index(lo, lod_comp));
7524 info->lti_comp_idx[info->lti_count++] =
7525 lod_comp_index(lo, lod_comp);
7529 return need_sync ? 0 : -EALREADY;
7532 static int lod_declare_update_rdonly(const struct lu_env *env,
7533 struct lod_object *lo, struct md_layout_change *mlc,
7536 struct lod_thread_info *info = lod_env_info(env);
7537 struct lu_attr *layout_attr = &info->lti_layout_attr;
7538 struct lod_layout_component *lod_comp;
7539 struct lu_extent extent = { 0 };
7543 LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
7544 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7545 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7546 LASSERT(lo->ldo_mirror_count > 0);
7548 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7549 struct layout_intent *layout = mlc->mlc_intent;
7550 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7553 extent = layout->li_extent;
7554 CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
7555 PFID(lod_object_fid(lo)), PEXT(&extent));
7557 picked = lod_primary_pick(env, lo, &extent);
7561 CDEBUG(D_LAYOUT, DFID": picked mirror id %u as primary\n",
7562 PFID(lod_object_fid(lo)),
7563 lo->ldo_mirrors[picked].lme_id);
7565 /* Update extents of primary before staling */
7566 rc = lod_declare_update_extents(env, lo, &extent, th, picked,
7571 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
7573 * trunc transfers [0, size) in the intent extent, we'd
7574 * stale components overlapping [size, eof).
7576 extent.e_start = extent.e_end;
7577 extent.e_end = OBD_OBJECT_EOF;
7580 /* stale overlapping components from other mirrors */
7581 rc = lod_stale_components(env, lo, picked, &extent, th);
7585 /* restore truncate intent extent */
7586 if (layout->li_opc == LAYOUT_INTENT_TRUNC)
7587 extent.e_end = extent.e_start;
7589 /* instantiate components for the picked mirror, start from 0 */
7592 lod_foreach_mirror_comp(lod_comp, lo, picked) {
7593 if (!lu_extent_is_overlapped(&extent,
7594 &lod_comp->llc_extent))
7597 if (!lod_is_instantiation_needed(lod_comp))
7600 info->lti_comp_idx[info->lti_count++] =
7601 lod_comp_index(lo, lod_comp);
7604 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7605 } else { /* MD_LAYOUT_RESYNC */
7609 * could contain multiple non-stale mirrors, so we need to
7610 * prep uninited all components assuming any non-stale mirror
7611 * could be picked as the primary mirror.
7613 if (mlc->mlc_mirror_id == 0) {
7615 for (i = 0; i < lo->ldo_mirror_count; i++) {
7616 if (lo->ldo_mirrors[i].lme_stale)
7619 lod_foreach_mirror_comp(lod_comp, lo, i) {
7620 if (!lod_comp_inited(lod_comp))
7624 lod_comp->llc_extent.e_end)
7626 lod_comp->llc_extent.e_end;
7629 rc = lod_prepare_resync(env, lo, &extent);
7633 /* mirror write, try to init its all components */
7634 rc = lod_prepare_resync_mirror(env, lo,
7635 mlc->mlc_mirror_id);
7640 /* change the file state to SYNC_PENDING */
7641 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7644 /* Reset the layout version once it's becoming too large.
7645 * This way it can make sure that the layout version is
7646 * monotonously increased in this writing era. */
7647 lod_obj_inc_layout_gen(lo);
7648 if (lo->ldo_layout_gen > (LCME_ID_MAX >> 1)) {
7649 __u32 layout_version;
7651 get_random_bytes(&layout_version, sizeof(layout_version));
7652 lo->ldo_layout_gen = layout_version & 0xffff;
7655 rc = lod_declare_instantiate_components(env, lo, th, 0);
7659 layout_attr->la_valid = LA_LAYOUT_VERSION;
7660 layout_attr->la_layout_version = 0; /* set current version */
7661 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7662 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7663 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7669 lod_striping_free(env, lo);
7673 static int lod_declare_update_write_pending(const struct lu_env *env,
7674 struct lod_object *lo, struct md_layout_change *mlc,
7677 struct lod_thread_info *info = lod_env_info(env);
7678 struct lu_attr *layout_attr = &info->lti_layout_attr;
7679 struct lod_layout_component *lod_comp;
7680 struct lu_extent extent = { 0 };
7686 LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
7687 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7688 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7690 /* look for the first preferred mirror */
7691 for (i = 0; i < lo->ldo_mirror_count; i++) {
7692 if (lo->ldo_mirrors[i].lme_stale)
7694 if (lo->ldo_mirrors[i].lme_prefer == 0)
7701 /* no primary, use any in-sync */
7702 for (i = 0; i < lo->ldo_mirror_count; i++) {
7703 if (lo->ldo_mirrors[i].lme_stale)
7709 CERROR(DFID ": doesn't have a primary mirror\n",
7710 PFID(lod_object_fid(lo)));
7711 GOTO(out, rc = -ENODATA);
7715 CDEBUG(D_LAYOUT, DFID": found primary %u\n",
7716 PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
7718 LASSERT(!lo->ldo_mirrors[primary].lme_stale);
7720 /* for LAYOUT_WRITE opc, it has to do the following operations:
7721 * 1. stale overlapping componets from stale mirrors;
7722 * 2. instantiate components of the primary mirror;
7723 * 3. transfter layout version to all objects of the primary;
7725 * for LAYOUT_RESYNC opc, it will do:
7726 * 1. instantiate components of all stale mirrors;
7727 * 2. transfer layout version to all objects to close write era. */
7729 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7730 struct layout_intent *layout = mlc->mlc_intent;
7731 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7733 LASSERT(mlc->mlc_intent != NULL);
7735 extent = mlc->mlc_intent->li_extent;
7737 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
7738 PFID(lod_object_fid(lo)), PEXT(&extent));
7740 /* 1. Update extents of primary before staling */
7741 rc = lod_declare_update_extents(env, lo, &extent, th, primary,
7746 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
7748 * trunc transfers [0, size) in the intent extent, we'd
7749 * stale components overlapping [size, eof).
7751 extent.e_start = extent.e_end;
7752 extent.e_end = OBD_OBJECT_EOF;
7755 /* 2. stale overlapping components */
7756 rc = lod_stale_components(env, lo, primary, &extent, th);
7760 /* 3. find the components which need instantiating.
7761 * instantiate [0, mlc->mlc_intent->e_end) */
7763 /* restore truncate intent extent */
7764 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC)
7765 extent.e_end = extent.e_start;
7768 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7769 if (!lu_extent_is_overlapped(&extent,
7770 &lod_comp->llc_extent))
7773 if (!lod_is_instantiation_needed(lod_comp))
7776 CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
7777 primary, lod_comp_index(lo, lod_comp));
7778 info->lti_comp_idx[info->lti_count++] =
7779 lod_comp_index(lo, lod_comp);
7781 } else { /* MD_LAYOUT_RESYNC */
7782 if (mlc->mlc_mirror_id == 0) {
7784 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7785 if (!lod_comp_inited(lod_comp))
7788 extent.e_end = lod_comp->llc_extent.e_end;
7791 rc = lod_prepare_resync(env, lo, &extent);
7795 /* mirror write, try to init its all components */
7796 rc = lod_prepare_resync_mirror(env, lo,
7797 mlc->mlc_mirror_id);
7802 /* change the file state to SYNC_PENDING */
7803 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7806 rc = lod_declare_instantiate_components(env, lo, th, 0);
7810 /* 3. transfer layout version to OST objects.
7811 * transfer new layout version to OST objects so that stale writes
7812 * can be denied. It also ends an era of writing by setting
7813 * LU_LAYOUT_RESYNC. Normal client can never use this bit to
7814 * send write RPC; only resync RPCs could do it. */
7815 layout_attr->la_valid = LA_LAYOUT_VERSION;
7816 layout_attr->la_layout_version = 0; /* set current version */
7817 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7818 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7819 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7823 lod_obj_inc_layout_gen(lo);
7826 lod_striping_free(env, lo);
7830 static int lod_declare_update_sync_pending(const struct lu_env *env,
7831 struct lod_object *lo, struct md_layout_change *mlc,
7834 struct lod_thread_info *info = lod_env_info(env);
7835 unsigned sync_components = 0;
7836 unsigned resync_components = 0;
7841 LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
7842 LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
7843 mlc->mlc_opc == MD_LAYOUT_WRITE);
7845 CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
7846 PFID(lod_object_fid(lo)), mlc->mlc_opc);
7848 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7849 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
7850 PFID(lod_object_fid(lo)));
7852 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7853 return lod_declare_update_write_pending(env, lo, mlc, th);
7856 /* MD_LAYOUT_RESYNC_DONE */
7858 for (i = 0; i < lo->ldo_comp_cnt; i++) {
7859 struct lod_layout_component *lod_comp;
7862 lod_comp = &lo->ldo_comp_entries[i];
7864 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
7869 for (j = 0; j < mlc->mlc_resync_count; j++) {
7870 if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
7873 mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
7874 lod_comp->llc_flags &= ~LCME_FL_STALE;
7875 resync_components++;
7881 for (i = 0; i < mlc->mlc_resync_count; i++) {
7882 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
7885 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
7886 "or already synced\n", PFID(lod_object_fid(lo)),
7887 mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
7888 GOTO(out, rc = -EINVAL);
7891 if (!sync_components || (mlc->mlc_resync_count && !resync_components)) {
7892 CDEBUG(D_LAYOUT, DFID": no mirror in sync\n",
7893 PFID(lod_object_fid(lo)));
7895 /* tend to return an error code here to prevent
7896 * the MDT from setting SoM attribute */
7897 GOTO(out, rc = -EINVAL);
7900 CDEBUG(D_LAYOUT, DFID": synced %u resynced %u/%zu components\n",
7901 PFID(lod_object_fid(lo)),
7902 sync_components, resync_components, mlc->mlc_resync_count);
7904 lo->ldo_flr_state = LCM_FL_RDONLY;
7905 lod_obj_inc_layout_gen(lo);
7907 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
7908 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
7909 &info->lti_buf, XATTR_NAME_LOV, 0, th);
7914 lod_striping_free(env, lo);
7918 typedef int (*mlc_handler)(const struct lu_env *env, struct dt_object *dt,
7919 const struct md_layout_change *mlc,
7920 struct thandle *th);
7923 * Attach stripes after target's for migrating directory. NB, we
7924 * only need to declare this, the actual work is done inside
7925 * lod_xattr_set_lmv().
7927 * \param[in] env execution environment
7928 * \param[in] dt target object
7929 * \param[in] mlc layout change data
7930 * \param[in] th transaction handle
7932 * \retval 0 on success
7933 * \retval negative if failed
7935 static int lod_dir_declare_layout_attach(const struct lu_env *env,
7936 struct dt_object *dt,
7937 const struct md_layout_change *mlc,
7940 struct lod_thread_info *info = lod_env_info(env);
7941 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
7942 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
7943 struct lod_object *lo = lod_dt_obj(dt);
7944 struct dt_object *next = dt_object_child(dt);
7945 struct dt_object_format *dof = &info->lti_format;
7946 struct lmv_mds_md_v1 *lmv = mlc->mlc_buf.lb_buf;
7947 struct dt_object **stripes;
7948 __u32 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
7949 struct lu_fid *fid = &info->lti_fid;
7950 struct lod_tgt_desc *tgt;
7951 struct dt_object *dto;
7952 struct dt_device *tgt_dt;
7953 int type = LU_SEQ_RANGE_ANY;
7954 struct dt_insert_rec *rec = &info->lti_dt_rec;
7955 char *stripe_name = info->lti_key;
7956 struct lu_name *sname;
7957 struct linkea_data ldata = { NULL };
7958 struct lu_buf linkea_buf;
7965 if (!lmv_is_sane(lmv))
7968 if (!dt_try_as_dir(env, dt))
7971 dof->dof_type = DFT_DIR;
7973 OBD_ALLOC_PTR_ARRAY(stripes, (lo->ldo_dir_stripe_count + stripe_count));
7977 for (i = 0; i < lo->ldo_dir_stripe_count; i++)
7978 stripes[i] = lo->ldo_stripe[i];
7980 rec->rec_type = S_IFDIR;
7982 for (i = 0; i < stripe_count; i++) {
7984 &lmv->lmv_stripe_fids[i]);
7985 if (!fid_is_sane(fid))
7988 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
7992 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
7993 tgt_dt = lod->lod_child;
7995 tgt = LTD_TGT(ltd, idx);
7997 GOTO(out, rc = -ESTALE);
7998 tgt_dt = tgt->ltd_tgt;
8001 dto = dt_locate_at(env, tgt_dt, fid,
8002 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
8005 GOTO(out, rc = PTR_ERR(dto));
8007 stripes[i + lo->ldo_dir_stripe_count] = dto;
8009 if (!dt_try_as_dir(env, dto))
8010 GOTO(out, rc = -ENOTDIR);
8012 rc = lod_sub_declare_ref_add(env, dto, th);
8016 rec->rec_fid = lu_object_fid(&dto->do_lu);
8017 rc = lod_sub_declare_insert(env, dto,
8018 (const struct dt_rec *)rec,
8019 (const struct dt_key *)dot, th);
8023 rc = lod_sub_declare_insert(env, dto,
8024 (const struct dt_rec *)rec,
8025 (const struct dt_key *)dotdot, th);
8029 rc = lod_sub_declare_xattr_set(env, dto, &mlc->mlc_buf,
8030 XATTR_NAME_LMV, 0, th);
8034 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
8035 PFID(lu_object_fid(&dto->do_lu)),
8036 i + lo->ldo_dir_stripe_count);
8038 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
8039 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
8040 sname, lu_object_fid(&dt->do_lu));
8044 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
8045 linkea_buf.lb_len = ldata.ld_leh->leh_len;
8046 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
8047 XATTR_NAME_LINK, 0, th);
8051 rc = lod_sub_declare_insert(env, next,
8052 (const struct dt_rec *)rec,
8053 (const struct dt_key *)stripe_name,
8058 rc = lod_sub_declare_ref_add(env, next, th);
8064 OBD_FREE_PTR_ARRAY(lo->ldo_stripe,
8065 lo->ldo_dir_stripes_allocated);
8066 lo->ldo_stripe = stripes;
8067 lo->ldo_dir_migrate_offset = lo->ldo_dir_stripe_count;
8068 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_hash_type);
8069 lo->ldo_dir_stripe_count += stripe_count;
8070 lo->ldo_dir_stripes_allocated += stripe_count;
8072 /* plain directory split creates target as a plain directory, while
8073 * after source attached as the first stripe, it becomes a striped
8074 * directory, set correct do_index_ops, otherwise it can't be unlinked.
8076 dt->do_index_ops = &lod_striped_index_ops;
8080 i = lo->ldo_dir_stripe_count;
8081 while (i < lo->ldo_dir_stripe_count + stripe_count && stripes[i])
8082 dt_object_put(env, stripes[i++]);
8084 OBD_FREE_PTR_ARRAY(stripes, stripe_count + lo->ldo_dir_stripe_count);
8088 static int lod_dir_declare_layout_detach(const struct lu_env *env,
8089 struct dt_object *dt,
8090 const struct md_layout_change *unused,
8093 struct lod_thread_info *info = lod_env_info(env);
8094 struct lod_object *lo = lod_dt_obj(dt);
8095 struct dt_object *next = dt_object_child(dt);
8096 char *stripe_name = info->lti_key;
8097 struct dt_object *dto;
8101 if (!dt_try_as_dir(env, dt))
8104 if (!lo->ldo_dir_stripe_count)
8105 return lod_sub_declare_delete(env, next,
8106 (const struct dt_key *)dotdot, th);
8108 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8109 dto = lo->ldo_stripe[i];
8113 if (!dt_try_as_dir(env, dto))
8116 rc = lod_sub_declare_delete(env, dto,
8117 (const struct dt_key *)dotdot, th);
8121 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8122 PFID(lu_object_fid(&dto->do_lu)), i);
8124 rc = lod_sub_declare_delete(env, next,
8125 (const struct dt_key *)stripe_name, th);
8129 rc = lod_sub_declare_ref_del(env, next, th);
8137 static int dt_dir_is_empty(const struct lu_env *env,
8138 struct dt_object *obj)
8141 const struct dt_it_ops *iops;
8146 if (!dt_try_as_dir(env, obj))
8149 iops = &obj->do_index_ops->dio_it;
8150 it = iops->init(env, obj, LUDA_64BITHASH);
8152 RETURN(PTR_ERR(it));
8154 rc = iops->get(env, it, (const struct dt_key *)"");
8158 for (rc = 0, i = 0; rc == 0 && i < 3; ++i)
8159 rc = iops->next(env, it);
8165 /* Huh? Index contains no zero key? */
8170 iops->fini(env, it);
8175 static int lod_dir_declare_layout_shrink(const struct lu_env *env,
8176 struct dt_object *dt,
8177 const struct md_layout_change *mlc,
8180 struct lod_thread_info *info = lod_env_info(env);
8181 struct lod_object *lo = lod_dt_obj(dt);
8182 struct dt_object *next = dt_object_child(dt);
8183 struct lmv_user_md *lmu = mlc->mlc_buf.lb_buf;
8184 __u32 final_stripe_count;
8185 char *stripe_name = info->lti_key;
8186 struct lu_buf *lmv_buf = &info->lti_buf;
8187 struct dt_object *dto;
8193 if (!dt_try_as_dir(env, dt))
8196 /* shouldn't be called on plain directory */
8197 LASSERT(lo->ldo_dir_stripe_count);
8199 lmv_buf->lb_buf = &info->lti_lmv.lmv_md_v1;
8200 lmv_buf->lb_len = sizeof(info->lti_lmv.lmv_md_v1);
8202 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
8203 LASSERT(final_stripe_count &&
8204 final_stripe_count < lo->ldo_dir_stripe_count);
8206 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8207 dto = lo->ldo_stripe[i];
8211 if (i < final_stripe_count) {
8212 if (final_stripe_count == 1)
8215 rc = lod_sub_declare_xattr_set(env, dto, lmv_buf,
8217 LU_XATTR_REPLACE, th);
8224 rc = dt_dir_is_empty(env, dto);
8228 rc = lod_sub_declare_ref_del(env, dto, th);
8232 rc = lod_sub_declare_destroy(env, dto, th);
8236 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8237 PFID(lu_object_fid(&dto->do_lu)), i);
8239 rc = lod_sub_declare_delete(env, next,
8240 (const struct dt_key *)stripe_name, th);
8244 rc = lod_sub_declare_ref_del(env, next, th);
8249 rc = lod_sub_declare_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV,
8250 LU_XATTR_REPLACE, th);
8255 * Allocate stripes for split directory.
8257 * \param[in] env execution environment
8258 * \param[in] dt target object
8259 * \param[in] mlc layout change data
8260 * \param[in] th transaction handle
8262 * \retval 0 on success
8263 * \retval negative if failed
8265 static int lod_dir_declare_layout_split(const struct lu_env *env,
8266 struct dt_object *dt,
8267 const struct md_layout_change *mlc,
8270 struct lod_thread_info *info = lod_env_info(env);
8271 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
8272 struct lod_object *lo = lod_dt_obj(dt);
8273 struct dt_object_format *dof = &info->lti_format;
8274 struct lmv_user_md_v1 *lum = mlc->mlc_spec->u.sp_ea.eadata;
8275 struct dt_object **stripes;
8283 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
8284 LASSERT(le32_to_cpu(lum->lum_stripe_offset) == LMV_OFFSET_DEFAULT);
8286 saved_count = lo->ldo_dir_stripes_allocated;
8287 stripe_count = le32_to_cpu(lum->lum_stripe_count);
8288 if (stripe_count <= saved_count)
8291 dof->dof_type = DFT_DIR;
8293 OBD_ALLOC(stripes, sizeof(*stripes) * stripe_count);
8297 for (i = 0; i < lo->ldo_dir_stripes_allocated; i++)
8298 stripes[i] = lo->ldo_stripe[i];
8300 lod_qos_statfs_update(env, lod, &lod->lod_mdt_descs);
8301 rc = lod_mdt_alloc_qos(env, lo, stripes, saved_count, stripe_count);
8303 rc = lod_mdt_alloc_rr(env, lo, stripes, saved_count,
8306 OBD_FREE(stripes, sizeof(*stripes) * stripe_count);
8310 LASSERT(rc > saved_count);
8311 OBD_FREE(lo->ldo_stripe,
8312 sizeof(*stripes) * lo->ldo_dir_stripes_allocated);
8313 lo->ldo_stripe = stripes;
8314 lo->ldo_dir_striped = 1;
8315 lo->ldo_dir_stripe_count = rc;
8316 lo->ldo_dir_stripes_allocated = stripe_count;
8317 lo->ldo_dir_split_hash = lo->ldo_dir_hash_type;
8318 lo->ldo_dir_hash_type = le32_to_cpu(lum->lum_hash_type);
8319 if (!lmv_is_known_hash_type(lo->ldo_dir_hash_type))
8320 lo->ldo_dir_hash_type =
8321 lod->lod_mdt_descs.ltd_lmv_desc.ld_pattern;
8322 lo->ldo_dir_hash_type |= LMV_HASH_FLAG_SPLIT | LMV_HASH_FLAG_MIGRATION;
8323 lo->ldo_dir_split_offset = saved_count;
8324 lo->ldo_dir_layout_version++;
8325 lo->ldo_dir_stripe_loaded = 1;
8327 rc = lod_dir_declare_create_stripes(env, dt, mlc->mlc_attr, dof, th);
8329 lod_striping_free(env, lo);
8335 * detach all stripes from dir master object, NB, stripes are not destroyed, but
8336 * deleted from it's parent namespace, this function is called in two places:
8337 * 1. mdd_migrate_mdt() detach stripes from source, and attach them to
8339 * 2. mdd_dir_layout_update() detach stripe before turning 1-stripe directory to
8340 * a plain directory.
8342 * \param[in] env execution environment
8343 * \param[in] dt target object
8344 * \param[in] mlc layout change data
8345 * \param[in] th transaction handle
8347 * \retval 0 on success
8348 * \retval negative if failed
8350 static int lod_dir_layout_detach(const struct lu_env *env,
8351 struct dt_object *dt,
8352 const struct md_layout_change *mlc,
8355 struct lod_thread_info *info = lod_env_info(env);
8356 struct lod_object *lo = lod_dt_obj(dt);
8357 struct dt_object *next = dt_object_child(dt);
8358 char *stripe_name = info->lti_key;
8359 struct dt_object *dto;
8365 if (!lo->ldo_dir_stripe_count) {
8366 /* plain directory delete .. */
8367 rc = lod_sub_delete(env, next,
8368 (const struct dt_key *)dotdot, th);
8372 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8373 dto = lo->ldo_stripe[i];
8377 rc = lod_sub_delete(env, dto,
8378 (const struct dt_key *)dotdot, th);
8382 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8383 PFID(lu_object_fid(&dto->do_lu)), i);
8385 rc = lod_sub_delete(env, next,
8386 (const struct dt_key *)stripe_name, th);
8390 rc = lod_sub_ref_del(env, next, th);
8395 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8396 dto = lo->ldo_stripe[i];
8398 dt_object_put(env, dto);
8400 OBD_FREE_PTR_ARRAY(lo->ldo_stripe, lo->ldo_dir_stripes_allocated);
8401 lo->ldo_stripe = NULL;
8402 lo->ldo_dir_stripes_allocated = 0;
8403 lo->ldo_dir_stripe_count = 0;
8404 dt->do_index_ops = &lod_index_ops;
8409 static int lod_dir_layout_shrink(const struct lu_env *env,
8410 struct dt_object *dt,
8411 const struct md_layout_change *mlc,
8414 struct lod_thread_info *info = lod_env_info(env);
8415 struct lod_object *lo = lod_dt_obj(dt);
8416 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
8417 struct dt_object *next = dt_object_child(dt);
8418 struct lmv_user_md *lmu = mlc->mlc_buf.lb_buf;
8419 __u32 final_stripe_count;
8420 char *stripe_name = info->lti_key;
8421 struct dt_object *dto;
8422 struct lu_buf *lmv_buf = &info->lti_buf;
8423 struct lmv_mds_md_v1 *lmv = &info->lti_lmv.lmv_md_v1;
8425 int type = LU_SEQ_RANGE_ANY;
8431 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
8433 lmv_buf->lb_buf = lmv;
8434 lmv_buf->lb_len = sizeof(*lmv);
8435 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
8436 lmv->lmv_stripe_count = cpu_to_le32(final_stripe_count);
8437 lmv->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type) &
8438 cpu_to_le32(LMV_HASH_TYPE_MASK);
8439 lmv->lmv_layout_version =
8440 cpu_to_le32(lo->ldo_dir_layout_version + 1);
8442 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8443 dto = lo->ldo_stripe[i];
8447 if (i < final_stripe_count) {
8448 /* if only one stripe left, no need to update
8449 * LMV because this stripe will replace master
8450 * object and act as a plain directory.
8452 if (final_stripe_count == 1)
8456 rc = lod_fld_lookup(env, lod,
8457 lu_object_fid(&dto->do_lu),
8462 lmv->lmv_master_mdt_index = cpu_to_le32(mdtidx);
8463 rc = lod_sub_xattr_set(env, dto, lmv_buf,
8465 LU_XATTR_REPLACE, th);
8472 dt_write_lock(env, dto, DT_TGT_CHILD);
8473 rc = lod_sub_ref_del(env, dto, th);
8474 dt_write_unlock(env, dto);
8478 rc = lod_sub_destroy(env, dto, th);
8482 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8483 PFID(lu_object_fid(&dto->do_lu)), i);
8485 rc = lod_sub_delete(env, next,
8486 (const struct dt_key *)stripe_name, th);
8490 rc = lod_sub_ref_del(env, next, th);
8495 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &mdtidx,
8500 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
8501 lmv->lmv_master_mdt_index = cpu_to_le32(mdtidx);
8502 rc = lod_sub_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV,
8503 LU_XATTR_REPLACE, th);
8507 for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
8508 dto = lo->ldo_stripe[i];
8510 dt_object_put(env, dto);
8512 lo->ldo_dir_stripe_count = final_stripe_count;
8517 static mlc_handler dir_mlc_declare_ops[MD_LAYOUT_MAX] = {
8518 [MD_LAYOUT_ATTACH] = lod_dir_declare_layout_attach,
8519 [MD_LAYOUT_DETACH] = lod_dir_declare_layout_detach,
8520 [MD_LAYOUT_SHRINK] = lod_dir_declare_layout_shrink,
8521 [MD_LAYOUT_SPLIT] = lod_dir_declare_layout_split,
8524 static mlc_handler dir_mlc_ops[MD_LAYOUT_MAX] = {
8525 [MD_LAYOUT_DETACH] = lod_dir_layout_detach,
8526 [MD_LAYOUT_SHRINK] = lod_dir_layout_shrink,
8529 static int lod_declare_layout_change(const struct lu_env *env,
8530 struct dt_object *dt, struct md_layout_change *mlc,
8533 struct lod_thread_info *info = lod_env_info(env);
8534 struct lod_object *lo = lod_dt_obj(dt);
8539 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
8540 LASSERT(dir_mlc_declare_ops[mlc->mlc_opc]);
8541 rc = dir_mlc_declare_ops[mlc->mlc_opc](env, dt, mlc, th);
8545 if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
8546 dt_object_remote(dt_object_child(dt)))
8549 rc = lod_striping_load(env, lo);
8553 LASSERT(lo->ldo_comp_cnt > 0);
8555 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
8559 switch (lo->ldo_flr_state) {
8561 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
8565 rc = lod_declare_update_rdonly(env, lo, mlc, th);
8567 case LCM_FL_WRITE_PENDING:
8568 rc = lod_declare_update_write_pending(env, lo, mlc, th);
8570 case LCM_FL_SYNC_PENDING:
8571 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
8582 * Instantiate layout component objects which covers the intent write offset.
8584 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
8585 struct md_layout_change *mlc, struct thandle *th)
8587 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
8588 struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
8589 struct lod_object *lo = lod_dt_obj(dt);
8594 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
8595 LASSERT(dir_mlc_ops[mlc->mlc_opc]);
8596 rc = dir_mlc_ops[mlc->mlc_opc](env, dt, mlc, th);
8600 rc = lod_striped_create(env, dt, attr, NULL, th);
8601 if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
8602 layout_attr->la_layout_version |= lo->ldo_layout_gen;
8603 rc = lod_attr_set(env, dt, layout_attr, th);
8609 const struct dt_object_operations lod_obj_ops = {
8610 .do_read_lock = lod_read_lock,
8611 .do_write_lock = lod_write_lock,
8612 .do_read_unlock = lod_read_unlock,
8613 .do_write_unlock = lod_write_unlock,
8614 .do_write_locked = lod_write_locked,
8615 .do_attr_get = lod_attr_get,
8616 .do_declare_attr_set = lod_declare_attr_set,
8617 .do_attr_set = lod_attr_set,
8618 .do_xattr_get = lod_xattr_get,
8619 .do_declare_xattr_set = lod_declare_xattr_set,
8620 .do_xattr_set = lod_xattr_set,
8621 .do_declare_xattr_del = lod_declare_xattr_del,
8622 .do_xattr_del = lod_xattr_del,
8623 .do_xattr_list = lod_xattr_list,
8624 .do_ah_init = lod_ah_init,
8625 .do_declare_create = lod_declare_create,
8626 .do_create = lod_create,
8627 .do_declare_destroy = lod_declare_destroy,
8628 .do_destroy = lod_destroy,
8629 .do_index_try = lod_index_try,
8630 .do_declare_ref_add = lod_declare_ref_add,
8631 .do_ref_add = lod_ref_add,
8632 .do_declare_ref_del = lod_declare_ref_del,
8633 .do_ref_del = lod_ref_del,
8634 .do_object_sync = lod_object_sync,
8635 .do_object_lock = lod_object_lock,
8636 .do_object_unlock = lod_object_unlock,
8637 .do_invalidate = lod_invalidate,
8638 .do_declare_layout_change = lod_declare_layout_change,
8639 .do_layout_change = lod_layout_change,
8643 * Implementation of dt_body_operations::dbo_read.
8645 * \see dt_body_operations::dbo_read() in the API description for details.
8647 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
8648 struct lu_buf *buf, loff_t *pos)
8650 struct dt_object *next = dt_object_child(dt);
8652 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
8653 S_ISLNK(dt->do_lu.lo_header->loh_attr));
8654 return next->do_body_ops->dbo_read(env, next, buf, pos);
8658 * Implementation of dt_body_operations::dbo_declare_write.
8660 * \see dt_body_operations::dbo_declare_write() in the API description
8663 static ssize_t lod_declare_write(const struct lu_env *env,
8664 struct dt_object *dt,
8665 const struct lu_buf *buf, loff_t pos,
8668 return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
8672 * Implementation of dt_body_operations::dbo_write.
8674 * \see dt_body_operations::dbo_write() in the API description for details.
8676 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
8677 const struct lu_buf *buf, loff_t *pos,
8680 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
8681 S_ISLNK(dt->do_lu.lo_header->loh_attr));
8682 return lod_sub_write(env, dt_object_child(dt), buf, pos, th);
8685 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
8686 __u64 start, __u64 end, struct thandle *th)
8688 if (dt_object_remote(dt))
8691 return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
8694 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
8695 __u64 start, __u64 end, struct thandle *th)
8697 if (dt_object_remote(dt))
8700 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
8701 return lod_sub_punch(env, dt_object_child(dt), start, end, th);
8705 * different type of files use the same body_ops because object may be created
8706 * in OUT, where there is no chance to set correct body_ops for each type, so
8707 * body_ops themselves will check file type inside, see lod_read/write/punch for
8710 static const struct dt_body_operations lod_body_ops = {
8711 .dbo_read = lod_read,
8712 .dbo_declare_write = lod_declare_write,
8713 .dbo_write = lod_write,
8714 .dbo_declare_punch = lod_declare_punch,
8715 .dbo_punch = lod_punch,
8719 * Implementation of lu_object_operations::loo_object_init.
8721 * The function determines the type and the index of the target device using
8722 * sequence of the object's FID. Then passes control down to the
8723 * corresponding device:
8724 * OSD for the local objects, OSP for remote
8726 * \see lu_object_operations::loo_object_init() in the API description
8729 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
8730 const struct lu_object_conf *conf)
8732 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
8733 struct lu_device *cdev = NULL;
8734 struct lu_object *cobj;
8735 struct lod_tgt_descs *ltd = NULL;
8736 struct lod_tgt_desc *tgt;
8738 int type = LU_SEQ_RANGE_ANY;
8742 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
8746 if (type == LU_SEQ_RANGE_MDT &&
8747 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
8748 cdev = &lod->lod_child->dd_lu_dev;
8749 } else if (type == LU_SEQ_RANGE_MDT) {
8750 ltd = &lod->lod_mdt_descs;
8752 } else if (type == LU_SEQ_RANGE_OST) {
8753 ltd = &lod->lod_ost_descs;
8760 if (ltd->ltd_tgts_size > idx &&
8761 test_bit(idx, ltd->ltd_tgt_bitmap)) {
8762 tgt = LTD_TGT(ltd, idx);
8764 LASSERT(tgt != NULL);
8765 LASSERT(tgt->ltd_tgt != NULL);
8767 cdev = &(tgt->ltd_tgt->dd_lu_dev);
8769 lod_putref(lod, ltd);
8772 if (unlikely(cdev == NULL))
8775 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
8776 if (unlikely(cobj == NULL))
8779 lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
8781 lu_object_add(lo, cobj);
8788 * Alloc cached foreign LOV
8790 * \param[in] lo object
8791 * \param[in] size size of foreign LOV
8793 * \retval 0 on success
8794 * \retval negative if failed
8796 int lod_alloc_foreign_lov(struct lod_object *lo, size_t size)
8798 OBD_ALLOC_LARGE(lo->ldo_foreign_lov, size);
8799 if (lo->ldo_foreign_lov == NULL)
8801 lo->ldo_foreign_lov_size = size;
8802 lo->ldo_is_foreign = 1;
8808 * Free cached foreign LOV
8810 * \param[in] lo object
8812 void lod_free_foreign_lov(struct lod_object *lo)
8814 if (lo->ldo_foreign_lov != NULL)
8815 OBD_FREE_LARGE(lo->ldo_foreign_lov, lo->ldo_foreign_lov_size);
8816 lo->ldo_foreign_lov = NULL;
8817 lo->ldo_foreign_lov_size = 0;
8818 lo->ldo_is_foreign = 0;
8823 * Free cached foreign LMV
8825 * \param[in] lo object
8827 void lod_free_foreign_lmv(struct lod_object *lo)
8829 if (lo->ldo_foreign_lmv != NULL)
8830 OBD_FREE_LARGE(lo->ldo_foreign_lmv, lo->ldo_foreign_lmv_size);
8831 lo->ldo_foreign_lmv = NULL;
8832 lo->ldo_foreign_lmv_size = 0;
8833 lo->ldo_dir_is_foreign = 0;
8838 * Release resources associated with striping.
8840 * If the object is striped (regular or directory), then release
8841 * the stripe objects references and free the ldo_stripe array.
8843 * \param[in] env execution environment
8844 * \param[in] lo object
8846 void lod_striping_free_nolock(const struct lu_env *env, struct lod_object *lo)
8848 struct lod_layout_component *lod_comp;
8851 if (unlikely(lo->ldo_is_foreign)) {
8852 lod_free_foreign_lov(lo);
8853 lo->ldo_comp_cached = 0;
8854 } else if (unlikely(lo->ldo_dir_is_foreign)) {
8855 lod_free_foreign_lmv(lo);
8856 lo->ldo_dir_stripe_loaded = 0;
8857 } else if (lo->ldo_stripe != NULL) {
8858 LASSERT(lo->ldo_comp_entries == NULL);
8859 LASSERT(lo->ldo_dir_stripes_allocated > 0);
8861 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8862 if (lo->ldo_stripe[i])
8863 dt_object_put(env, lo->ldo_stripe[i]);
8866 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
8867 OBD_FREE(lo->ldo_stripe, j);
8868 lo->ldo_stripe = NULL;
8869 lo->ldo_dir_stripes_allocated = 0;
8870 lo->ldo_dir_stripe_loaded = 0;
8871 lo->ldo_dir_stripe_count = 0;
8872 } else if (lo->ldo_comp_entries != NULL) {
8873 for (i = 0; i < lo->ldo_comp_cnt; i++) {
8874 /* free lod_layout_component::llc_stripe array */
8875 lod_comp = &lo->ldo_comp_entries[i];
8877 if (lod_comp->llc_stripe == NULL)
8879 LASSERT(lod_comp->llc_stripes_allocated != 0);
8880 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
8881 if (lod_comp->llc_stripe[j] != NULL)
8883 &lod_comp->llc_stripe[j]->do_lu);
8885 OBD_FREE_PTR_ARRAY(lod_comp->llc_stripe,
8886 lod_comp->llc_stripes_allocated);
8887 lod_comp->llc_stripe = NULL;
8888 OBD_FREE_PTR_ARRAY(lod_comp->llc_ost_indices,
8889 lod_comp->llc_stripes_allocated);
8890 lod_comp->llc_ost_indices = NULL;
8891 lod_comp->llc_stripes_allocated = 0;
8893 lod_free_comp_entries(lo);
8894 lo->ldo_comp_cached = 0;
8898 void lod_striping_free(const struct lu_env *env, struct lod_object *lo)
8900 mutex_lock(&lo->ldo_layout_mutex);
8901 lod_striping_free_nolock(env, lo);
8902 mutex_unlock(&lo->ldo_layout_mutex);
8906 * Implementation of lu_object_operations::loo_object_free.
8908 * \see lu_object_operations::loo_object_free() in the API description
8911 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
8913 struct lod_object *lo = lu2lod_obj(o);
8915 /* release all underlying object pinned */
8916 lod_striping_free(env, lo);
8918 /* lo doesn't contain a lu_object_header, so we don't need call_rcu */
8919 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
8923 * Implementation of lu_object_operations::loo_object_release.
8925 * \see lu_object_operations::loo_object_release() in the API description
8928 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
8930 /* XXX: shouldn't we release everything here in case if object
8931 * creation failed before? */
8935 * Implementation of lu_object_operations::loo_object_print.
8937 * \see lu_object_operations::loo_object_print() in the API description
8940 static int lod_object_print(const struct lu_env *env, void *cookie,
8941 lu_printer_t p, const struct lu_object *l)
8943 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
8945 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
8948 const struct lu_object_operations lod_lu_obj_ops = {
8949 .loo_object_init = lod_object_init,
8950 .loo_object_free = lod_object_free,
8951 .loo_object_release = lod_object_release,
8952 .loo_object_print = lod_object_print,