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 !(LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
4256 lds->lds_dir_def_stripe_offset) &&
4257 le32_to_cpu(lds->lds_dir_def_hash_type) !=
4258 LMV_HASH_TYPE_UNKNOWN)) {
4259 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
4261 if (info->lti_ea_store_size < sizeof(*v1)) {
4262 rc = lod_ea_store_resize(info, sizeof(*v1));
4265 v1 = info->lti_ea_store;
4268 memset(v1, 0, sizeof(*v1));
4269 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
4270 v1->lum_stripe_count =
4271 cpu_to_le32(lds->lds_dir_def_stripe_count);
4272 v1->lum_stripe_offset =
4273 cpu_to_le32(lds->lds_dir_def_stripe_offset);
4275 cpu_to_le32(lds->lds_dir_def_hash_type);
4277 info->lti_buf.lb_buf = v1;
4278 info->lti_buf.lb_len = sizeof(*v1);
4280 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4281 XATTR_NAME_DEFAULT_LMV,
4284 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
4286 XATTR_NAME_DEFAULT_LMV, 0,
4292 /* Transfer default LOV striping from the parent */
4293 if (lds != NULL && lds->lds_def_striping_set &&
4294 lds->lds_def_comp_cnt != 0) {
4295 struct lov_mds_md *lmm;
4296 int lmm_size = lod_comp_md_size(lo, true);
4298 if (info->lti_ea_store_size < lmm_size) {
4299 rc = lod_ea_store_resize(info, lmm_size);
4303 lmm = info->lti_ea_store;
4305 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
4309 info->lti_buf.lb_buf = lmm;
4310 info->lti_buf.lb_len = lmm_size;
4313 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4314 XATTR_NAME_LOV, 0, th);
4316 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4317 XATTR_NAME_LOV, 0, th);
4325 static int lod_declare_dir_striping_create(const struct lu_env *env,
4326 struct dt_object *dt,
4327 struct lu_attr *attr,
4329 struct dt_object_format *dof,
4332 return lod_dir_striping_create_internal(env, dt, attr, lmu, dof, th,
4336 static int lod_dir_striping_create(const struct lu_env *env,
4337 struct dt_object *dt,
4338 struct lu_attr *attr,
4339 struct dt_object_format *dof,
4342 return lod_dir_striping_create_internal(env, dt, attr, NULL, dof, th,
4347 * Make LOV EA for striped object.
4349 * Generate striping information and store it in the LOV EA of the given
4350 * object. The caller must ensure nobody else is calling the function
4351 * against the object concurrently. The transaction must be started.
4352 * FLDB service must be running as well; it's used to map FID to the target,
4353 * which is stored in LOV EA.
4355 * \param[in] env execution environment for this thread
4356 * \param[in] lo LOD object
4357 * \param[in] th transaction handle
4359 * \retval 0 if LOV EA is stored successfully
4360 * \retval negative error number on failure
4362 static int lod_generate_and_set_lovea(const struct lu_env *env,
4363 struct lod_object *lo,
4366 struct lod_thread_info *info = lod_env_info(env);
4367 struct dt_object *next = dt_object_child(&lo->ldo_obj);
4368 struct lov_mds_md_v1 *lmm;
4374 if (lo->ldo_comp_cnt == 0 && !lo->ldo_is_foreign) {
4375 lod_striping_free_nolock(env, lo);
4376 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
4380 lmm_size = lod_comp_md_size(lo, false);
4381 if (info->lti_ea_store_size < lmm_size) {
4382 rc = lod_ea_store_resize(info, lmm_size);
4386 lmm = info->lti_ea_store;
4388 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
4392 info->lti_buf.lb_buf = lmm;
4393 info->lti_buf.lb_len = lmm_size;
4394 rc = lod_sub_xattr_set(env, next, &info->lti_buf,
4395 XATTR_NAME_LOV, 0, th);
4399 static __u32 lod_gen_component_id(struct lod_object *lo,
4400 int mirror_id, int comp_idx);
4403 * Repeat an existing component
4405 * Creates a new layout by replicating an existing component. Uses striping
4406 * policy from previous component as a template for the striping for the new
4409 * New component starts with zero length, will be extended (or removed) before
4410 * returning layout to client.
4412 * NB: Reallocates layout components array (lo->ldo_comp_entries), invalidating
4413 * any pre-existing pointers to components. Handle with care.
4415 * \param[in] env execution environment for this thread
4416 * \param[in,out] lo object to update the layout of
4417 * \param[in] index index of component to copy
4419 * \retval 0 on success
4420 * \retval negative errno on error
4422 static int lod_layout_repeat_comp(const struct lu_env *env,
4423 struct lod_object *lo, int index)
4425 struct lod_layout_component *lod_comp;
4426 struct lod_layout_component *new_comp = NULL;
4427 struct lod_layout_component *comp_array;
4428 int rc = 0, i, new_cnt = lo->ldo_comp_cnt + 1;
4433 lod_comp = &lo->ldo_comp_entries[index];
4434 LASSERT(lod_comp_inited(lod_comp) && lod_comp->llc_id != LCME_ID_INVAL);
4436 CDEBUG(D_LAYOUT, "repeating component %d\n", index);
4438 OBD_ALLOC_PTR_ARRAY(comp_array, new_cnt);
4439 if (comp_array == NULL)
4440 GOTO(out, rc = -ENOMEM);
4442 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4443 memcpy(&comp_array[i + offset], &lo->ldo_comp_entries[i],
4444 sizeof(*comp_array));
4446 /* Duplicate this component in to the next slot */
4448 new_comp = &comp_array[i + 1];
4449 memcpy(&comp_array[i + 1], &lo->ldo_comp_entries[i],
4450 sizeof(*comp_array));
4451 /* We must now skip this new component when copying */
4456 /* Set up copied component */
4457 new_comp->llc_flags &= ~LCME_FL_INIT;
4458 new_comp->llc_stripe = NULL;
4459 new_comp->llc_stripes_allocated = 0;
4460 new_comp->llc_ost_indices = NULL;
4461 new_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4462 /* for uninstantiated components, layout gen stores default stripe
4464 new_comp->llc_layout_gen = lod_comp->llc_stripe_offset;
4465 /* This makes the repeated component zero-length, placed at the end of
4466 * the preceding component */
4467 new_comp->llc_extent.e_start = new_comp->llc_extent.e_end;
4468 new_comp->llc_timestamp = lod_comp->llc_timestamp;
4469 new_comp->llc_pool = NULL;
4471 rc = lod_set_pool(&new_comp->llc_pool, lod_comp->llc_pool);
4475 if (new_comp->llc_ostlist.op_array) {
4476 __u32 *op_array = NULL;
4478 OBD_ALLOC(op_array, new_comp->llc_ostlist.op_size);
4480 GOTO(out, rc = -ENOMEM);
4481 memcpy(op_array, &new_comp->llc_ostlist.op_array,
4482 new_comp->llc_ostlist.op_size);
4483 new_comp->llc_ostlist.op_array = op_array;
4486 OBD_FREE_PTR_ARRAY(lo->ldo_comp_entries, lo->ldo_comp_cnt);
4487 lo->ldo_comp_entries = comp_array;
4488 lo->ldo_comp_cnt = new_cnt;
4490 /* Generate an id for the new component */
4491 mirror_id = mirror_id_of(new_comp->llc_id);
4492 new_comp->llc_id = LCME_ID_INVAL;
4493 new_comp->llc_id = lod_gen_component_id(lo, mirror_id, index + 1);
4494 if (new_comp->llc_id == LCME_ID_INVAL)
4495 GOTO(out, rc = -ERANGE);
4500 OBD_FREE_PTR_ARRAY(comp_array, new_cnt);
4505 static int lod_layout_data_init(struct lod_thread_info *info, __u32 comp_cnt)
4509 /* clear memory region that will be used for layout change */
4510 memset(&info->lti_layout_attr, 0, sizeof(struct lu_attr));
4511 info->lti_count = 0;
4513 if (info->lti_comp_size >= comp_cnt)
4516 if (info->lti_comp_size > 0) {
4517 OBD_FREE_PTR_ARRAY(info->lti_comp_idx, info->lti_comp_size);
4518 info->lti_comp_size = 0;
4521 OBD_ALLOC_PTR_ARRAY(info->lti_comp_idx, comp_cnt);
4522 if (!info->lti_comp_idx)
4525 info->lti_comp_size = comp_cnt;
4530 * Prepare new layout minus deleted components
4532 * Removes components marked for deletion (LCME_ID_INVAL) by copying to a new
4533 * layout and skipping those components. Removes stripe objects if any exist.
4536 * Reallocates layout components array (lo->ldo_comp_entries), invalidating
4537 * any pre-existing pointers to components.
4539 * Caller is responsible for updating mirror end (ldo_mirror[].lme_end).
4541 * \param[in] env execution environment for this thread
4542 * \param[in,out] lo object to update the layout of
4543 * \param[in] th transaction handle for this operation
4545 * \retval # of components deleted
4546 * \retval negative errno on error
4548 static int lod_layout_del_prep_layout(const struct lu_env *env,
4549 struct lod_object *lo,
4552 struct lod_layout_component *lod_comp;
4553 struct lod_thread_info *info = lod_env_info(env);
4554 int rc = 0, i, j, deleted = 0;
4558 LASSERT(lo->ldo_is_composite);
4559 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
4561 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
4565 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4566 lod_comp = &lo->ldo_comp_entries[i];
4568 if (lod_comp->llc_id != LCME_ID_INVAL) {
4569 /* Build array of things to keep */
4570 info->lti_comp_idx[info->lti_count++] = i;
4574 lod_obj_set_pool(lo, i, NULL);
4575 if (lod_comp->llc_ostlist.op_array) {
4576 OBD_FREE(lod_comp->llc_ostlist.op_array,
4577 lod_comp->llc_ostlist.op_size);
4578 lod_comp->llc_ostlist.op_array = NULL;
4579 lod_comp->llc_ostlist.op_size = 0;
4583 CDEBUG(D_LAYOUT, "deleting comp %d, left %d\n", i,
4584 lo->ldo_comp_cnt - deleted);
4586 /* No striping info for this component */
4587 if (lod_comp->llc_stripe == NULL)
4590 LASSERT(lod_comp->llc_stripe_count > 0);
4591 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4592 struct dt_object *obj = lod_comp->llc_stripe[j];
4597 /* components which are not init have no sub objects
4599 if (lod_comp_inited(lod_comp)) {
4600 rc = lod_sub_destroy(env, obj, th);
4605 lu_object_put(env, &obj->do_lu);
4606 lod_comp->llc_stripe[j] = NULL;
4608 OBD_FREE_PTR_ARRAY(lod_comp->llc_stripe,
4609 lod_comp->llc_stripes_allocated);
4610 lod_comp->llc_stripe = NULL;
4611 OBD_FREE_PTR_ARRAY(lod_comp->llc_ost_indices,
4612 lod_comp->llc_stripes_allocated);
4613 lod_comp->llc_ost_indices = NULL;
4614 lod_comp->llc_stripes_allocated = 0;
4617 /* info->lti_count has the amount of left components */
4618 LASSERTF(info->lti_count >= 0 && info->lti_count < lo->ldo_comp_cnt,
4619 "left = %d, lo->ldo_comp_cnt %d\n", (int)info->lti_count,
4620 (int)lo->ldo_comp_cnt);
4622 if (info->lti_count > 0) {
4623 struct lod_layout_component *comp_array;
4625 OBD_ALLOC_PTR_ARRAY(comp_array, info->lti_count);
4626 if (comp_array == NULL)
4627 GOTO(out, rc = -ENOMEM);
4629 for (i = 0; i < info->lti_count; i++) {
4630 memcpy(&comp_array[i],
4631 &lo->ldo_comp_entries[info->lti_comp_idx[i]],
4632 sizeof(*comp_array));
4635 OBD_FREE_PTR_ARRAY(lo->ldo_comp_entries, lo->ldo_comp_cnt);
4636 lo->ldo_comp_entries = comp_array;
4637 lo->ldo_comp_cnt = info->lti_count;
4639 lod_free_comp_entries(lo);
4644 return rc ? rc : deleted;
4648 * Delete layout component(s)
4650 * This function sets up the layout data in the env and does the setattrs
4651 * required to write out the new layout. The layout itself is modified in
4652 * lod_layout_del_prep_layout.
4654 * \param[in] env execution environment for this thread
4655 * \param[in] dt object
4656 * \param[in] th transaction handle
4658 * \retval 0 on success
4659 * \retval negative error number on failure
4661 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
4664 struct lod_object *lo = lod_dt_obj(dt);
4665 struct dt_object *next = dt_object_child(dt);
4666 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4669 LASSERT(lo->ldo_mirror_count == 1);
4671 mutex_lock(&lo->ldo_layout_mutex);
4673 rc = lod_layout_del_prep_layout(env, lo, th);
4677 /* Only do this if we didn't delete all components */
4678 if (lo->ldo_comp_cnt > 0) {
4679 lo->ldo_mirrors[0].lme_end = lo->ldo_comp_cnt - 1;
4680 lod_obj_inc_layout_gen(lo);
4683 LASSERT(dt_object_exists(dt));
4684 rc = dt_attr_get(env, next, attr);
4688 if (attr->la_size > 0) {
4690 attr->la_valid = LA_SIZE;
4691 rc = lod_sub_attr_set(env, next, attr, th);
4696 rc = lod_generate_and_set_lovea(env, lo, th);
4700 lod_striping_free_nolock(env, lo);
4702 mutex_unlock(&lo->ldo_layout_mutex);
4708 static int lod_get_default_lov_striping(const struct lu_env *env,
4709 struct lod_object *lo,
4710 struct lod_default_striping *lds,
4711 struct dt_allocation_hint *ah);
4713 * Implementation of dt_object_operations::do_xattr_set.
4715 * Sets specified extended attribute on the object. Three types of EAs are
4717 * LOV EA - stores striping for a regular file or default striping (when set
4719 * LMV EA - stores a marker for the striped directories
4720 * DMV EA - stores default directory striping
4722 * When striping is applied to a non-striped existing object (this is called
4723 * late striping), then LOD notices the caller wants to turn the object into a
4724 * striped one. The stripe objects are created and appropriate EA is set:
4725 * LOV EA storing all the stripes directly or LMV EA storing just a small header
4726 * with striping configuration.
4728 * \see dt_object_operations::do_xattr_set() in the API description for details.
4730 static int lod_xattr_set(const struct lu_env *env,
4731 struct dt_object *dt, const struct lu_buf *buf,
4732 const char *name, int fl, struct thandle *th)
4734 struct dt_object *next = dt_object_child(dt);
4739 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4740 !strcmp(name, XATTR_NAME_LMV)) {
4742 case LU_XATTR_CREATE:
4743 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
4746 case LU_XATTR_REPLACE:
4747 rc = lod_dir_layout_set(env, dt, buf, fl, th);
4754 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4755 strcmp(name, XATTR_NAME_LOV) == 0) {
4756 struct lod_default_striping *lds = lod_lds_buf_get(env);
4757 struct lov_user_md_v1 *v1 = buf->lb_buf;
4758 char pool[LOV_MAXPOOLNAME + 1];
4761 /* get existing striping config */
4762 rc = lod_get_default_lov_striping(env, lod_dt_obj(dt), lds,
4767 memset(pool, 0, sizeof(pool));
4768 if (lds->lds_def_striping_set == 1)
4769 lod_layout_get_pool(lds->lds_def_comp_entries,
4770 lds->lds_def_comp_cnt, pool,
4773 is_del = LOVEA_DELETE_VALUES(v1->lmm_stripe_size,
4774 v1->lmm_stripe_count,
4775 v1->lmm_stripe_offset,
4778 /* Retain the pool name if it is not given */
4779 if (v1->lmm_magic == LOV_USER_MAGIC_V1 && pool[0] != '\0' &&
4781 struct lod_thread_info *info = lod_env_info(env);
4782 struct lov_user_md_v3 *v3 = info->lti_ea_store;
4784 memset(v3, 0, sizeof(*v3));
4785 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
4786 v3->lmm_pattern = cpu_to_le32(v1->lmm_pattern);
4787 v3->lmm_stripe_count =
4788 cpu_to_le32(v1->lmm_stripe_count);
4789 v3->lmm_stripe_offset =
4790 cpu_to_le32(v1->lmm_stripe_offset);
4791 v3->lmm_stripe_size = cpu_to_le32(v1->lmm_stripe_size);
4793 strlcpy(v3->lmm_pool_name, pool,
4794 sizeof(v3->lmm_pool_name));
4796 info->lti_buf.lb_buf = v3;
4797 info->lti_buf.lb_len = sizeof(*v3);
4798 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4801 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name,
4805 if (lds->lds_def_striping_set == 1 &&
4806 lds->lds_def_comp_entries != NULL)
4807 lod_free_def_comp_entries(lds);
4810 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4811 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
4813 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
4816 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4817 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4818 strcmp(name, XATTR_LUSTRE_LOV) == 0 ||
4819 allowed_lustre_lov(name))) {
4820 /* in case of lov EA swap, just set it
4821 * if not, it is a replay so check striping match what we
4822 * already have during req replay, declare_xattr_set()
4823 * defines striping, then create() does the work */
4824 if (fl & LU_XATTR_REPLACE) {
4825 /* free stripes, then update disk */
4826 lod_striping_free(env, lod_dt_obj(dt));
4828 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
4829 } else if (fl & LU_XATTR_PURGE) {
4830 rc = lod_layout_purge(env, dt, buf, th);
4831 } else if (dt_object_remote(dt)) {
4832 /* This only happens during migration, see
4833 * mdd_migrate_create(), in which Master MDT will
4834 * create a remote target object, and only set
4835 * (migrating) stripe EA on the remote object,
4836 * and does not need creating each stripes. */
4837 rc = lod_sub_xattr_set(env, next, buf, name,
4839 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
4840 /* delete component(s) */
4841 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
4842 rc = lod_layout_del(env, dt, th);
4845 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
4846 * it's going to create create file with specified
4847 * component(s), the striping must have not being
4848 * cached in this case;
4850 * Otherwise, it's going to add/change component(s) to
4851 * an existing file, the striping must have been cached
4854 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
4855 !strcmp(name, XATTR_NAME_LOV),
4856 !lod_dt_obj(dt)->ldo_comp_cached));
4858 rc = lod_striped_create(env, dt, NULL, NULL, th);
4861 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
4862 rc = lod_replace_parent_fid(env, dt, buf, th, false);
4867 /* then all other xattr */
4868 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
4874 * Implementation of dt_object_operations::do_declare_xattr_del.
4876 * \see dt_object_operations::do_declare_xattr_del() in the API description
4879 static int lod_declare_xattr_del(const struct lu_env *env,
4880 struct dt_object *dt, const char *name,
4883 struct lod_object *lo = lod_dt_obj(dt);
4884 struct dt_object *next = dt_object_child(dt);
4889 rc = lod_sub_declare_xattr_del(env, next, name, th);
4893 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4896 /* NB: don't delete stripe LMV, because when we do this, normally we
4897 * will remove stripes, besides, if directory LMV is corrupt, this will
4898 * prevent deleting its LMV and fixing it (via LFSCK).
4900 if (!strcmp(name, XATTR_NAME_LMV))
4903 rc = lod_striping_load(env, lo);
4907 if (lo->ldo_dir_stripe_count == 0)
4910 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4911 struct dt_object *dto = lo->ldo_stripe[i];
4916 if (!dt_object_exists(dto))
4919 rc = lod_sub_declare_xattr_del(env, dto, name, th);
4928 * Implementation of dt_object_operations::do_xattr_del.
4930 * If EA storing a regular striping is being deleted, then release
4931 * all the references to the stripe objects in core.
4933 * \see dt_object_operations::do_xattr_del() in the API description for details.
4935 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
4936 const char *name, struct thandle *th)
4942 if (!strcmp(name, XATTR_NAME_LOV) || !strcmp(name, XATTR_NAME_LMV))
4943 lod_striping_free(env, lod_dt_obj(dt));
4945 rc = lod_xattr_del_internal(env, dt, name, th);
4951 * Implementation of dt_object_operations::do_xattr_list.
4953 * \see dt_object_operations::do_xattr_list() in the API description
4956 static int lod_xattr_list(const struct lu_env *env,
4957 struct dt_object *dt, const struct lu_buf *buf)
4959 return dt_xattr_list(env, dt_object_child(dt), buf);
4962 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
4964 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
4968 * Copy OST list from layout provided by user.
4970 * \param[in] lod_comp layout_component to be filled
4971 * \param[in] v3 LOV EA V3 user data
4973 * \retval 0 on success
4974 * \retval negative if failed
4976 int lod_comp_copy_ost_lists(struct lod_layout_component *lod_comp,
4977 struct lov_user_md_v3 *v3)
4983 if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
4984 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
4986 if (lod_comp->llc_ostlist.op_array) {
4987 if (lod_comp->llc_ostlist.op_size >=
4988 v3->lmm_stripe_count * sizeof(__u32)) {
4989 lod_comp->llc_ostlist.op_count =
4990 v3->lmm_stripe_count;
4993 OBD_FREE(lod_comp->llc_ostlist.op_array,
4994 lod_comp->llc_ostlist.op_size);
4997 /* copy ost list from lmm */
4998 lod_comp->llc_ostlist.op_count = v3->lmm_stripe_count;
4999 lod_comp->llc_ostlist.op_size = v3->lmm_stripe_count * sizeof(__u32);
5000 OBD_ALLOC(lod_comp->llc_ostlist.op_array,
5001 lod_comp->llc_ostlist.op_size);
5002 if (!lod_comp->llc_ostlist.op_array)
5005 for (j = 0; j < v3->lmm_stripe_count; j++) {
5006 lod_comp->llc_ostlist.op_array[j] =
5007 v3->lmm_objects[j].l_ost_idx;
5015 * Get default striping.
5017 * \param[in] env execution environment
5018 * \param[in] lo object
5019 * \param[out] lds default striping
5021 * \retval 0 on success
5022 * \retval negative if failed
5024 static int lod_get_default_lov_striping(const struct lu_env *env,
5025 struct lod_object *lo,
5026 struct lod_default_striping *lds,
5027 struct dt_allocation_hint *ah)
5029 struct lod_thread_info *info = lod_env_info(env);
5030 struct lov_user_md_v1 *v1 = NULL;
5031 struct lov_user_md_v3 *v3 = NULL;
5032 struct lov_comp_md_v1 *comp_v1 = NULL;
5040 rc = lod_get_lov_ea(env, lo);
5044 if (rc < (typeof(rc))sizeof(struct lov_user_md))
5047 v1 = info->lti_ea_store;
5048 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
5049 lustre_swab_lov_user_md_v1(v1);
5050 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
5051 v3 = (struct lov_user_md_v3 *)v1;
5052 lustre_swab_lov_user_md_v3(v3);
5053 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_SPECIFIC)) {
5054 v3 = (struct lov_user_md_v3 *)v1;
5055 lustre_swab_lov_user_md_v3(v3);
5056 lustre_swab_lov_user_md_objects(v3->lmm_objects,
5057 v3->lmm_stripe_count);
5058 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1) ||
5059 v1->lmm_magic == __swab32(LOV_USER_MAGIC_SEL)) {
5060 comp_v1 = (struct lov_comp_md_v1 *)v1;
5061 lustre_swab_lov_comp_md_v1(comp_v1);
5064 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
5065 v1->lmm_magic != LOV_MAGIC_COMP_V1 &&
5066 v1->lmm_magic != LOV_MAGIC_SEL &&
5067 v1->lmm_magic != LOV_USER_MAGIC_SPECIFIC)
5070 if ((v1->lmm_magic == LOV_MAGIC_COMP_V1 ||
5071 v1->lmm_magic == LOV_MAGIC_SEL) &&
5072 !(ah && ah->dah_append_stripes)) {
5073 comp_v1 = (struct lov_comp_md_v1 *)v1;
5074 comp_cnt = comp_v1->lcm_entry_count;
5077 mirror_cnt = comp_v1->lcm_mirror_count + 1;
5085 /* realloc default comp entries if necessary */
5086 rc = lod_def_striping_comp_resize(lds, comp_cnt);
5090 lds->lds_def_comp_cnt = comp_cnt;
5091 lds->lds_def_striping_is_composite = composite;
5092 lds->lds_def_mirror_cnt = mirror_cnt;
5094 for (i = 0; i < comp_cnt; i++) {
5095 struct lod_layout_component *lod_comp;
5098 lod_comp = &lds->lds_def_comp_entries[i];
5100 * reset lod_comp values, llc_stripes is always NULL in
5101 * the default striping template, llc_pool will be reset
5104 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
5107 v1 = (struct lov_user_md *)((char *)comp_v1 +
5108 comp_v1->lcm_entries[i].lcme_offset);
5109 lod_comp->llc_extent =
5110 comp_v1->lcm_entries[i].lcme_extent;
5111 /* We only inherit certain flags from the layout */
5112 lod_comp->llc_flags =
5113 comp_v1->lcm_entries[i].lcme_flags &
5114 LCME_TEMPLATE_FLAGS;
5117 if (!lov_pattern_supported(v1->lmm_pattern) &&
5118 !(v1->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
5119 lod_free_def_comp_entries(lds);
5123 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d stripe_offset=%d append_stripes=%d\n",
5124 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
5125 (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
5126 (int)v1->lmm_stripe_offset,
5127 ah ? ah->dah_append_stripes : 0);
5129 if (ah && ah->dah_append_stripes)
5130 lod_comp->llc_stripe_count = ah->dah_append_stripes;
5132 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
5133 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
5134 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
5135 lod_comp->llc_pattern = v1->lmm_pattern;
5138 if (ah && ah->dah_append_pool && ah->dah_append_pool[0]) {
5139 pool = ah->dah_append_pool;
5140 } else if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
5141 /* XXX: sanity check here */
5142 v3 = (struct lov_user_md_v3 *) v1;
5143 if (v3->lmm_pool_name[0] != '\0')
5144 pool = v3->lmm_pool_name;
5146 lod_set_def_pool(lds, i, pool);
5147 if (v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
5148 v3 = (struct lov_user_md_v3 *)v1;
5149 rc = lod_comp_copy_ost_lists(lod_comp, v3);
5152 } else if (lod_comp->llc_ostlist.op_array &&
5153 lod_comp->llc_ostlist.op_count) {
5154 for (j = 0; j < lod_comp->llc_ostlist.op_count; j++)
5155 lod_comp->llc_ostlist.op_array[j] = -1;
5156 lod_comp->llc_ostlist.op_count = 0;
5160 lds->lds_def_striping_set = 1;
5165 * Get default directory striping.
5167 * \param[in] env execution environment
5168 * \param[in] lo object
5169 * \param[out] lds default striping
5171 * \retval 0 on success
5172 * \retval negative if failed
5174 static int lod_get_default_lmv_striping(const struct lu_env *env,
5175 struct lod_object *lo,
5176 struct lod_default_striping *lds)
5178 struct lmv_user_md *lmu;
5181 lds->lds_dir_def_striping_set = 0;
5183 rc = lod_get_default_lmv_ea(env, lo);
5187 if (rc >= (int)sizeof(*lmu)) {
5188 struct lod_thread_info *info = lod_env_info(env);
5190 lmu = info->lti_ea_store;
5192 lds->lds_dir_def_stripe_count =
5193 le32_to_cpu(lmu->lum_stripe_count);
5194 lds->lds_dir_def_stripe_offset =
5195 le32_to_cpu(lmu->lum_stripe_offset);
5196 lds->lds_dir_def_hash_type =
5197 le32_to_cpu(lmu->lum_hash_type);
5198 lds->lds_dir_def_striping_set = 1;
5205 * Get default striping in the object.
5207 * Get object default striping and default directory striping.
5209 * \param[in] env execution environment
5210 * \param[in] lo object
5211 * \param[out] lds default striping
5213 * \retval 0 on success
5214 * \retval negative if failed
5216 static int lod_get_default_striping(const struct lu_env *env,
5217 struct lod_object *lo,
5218 struct lod_default_striping *lds)
5222 rc = lod_get_default_lov_striping(env, lo, lds, NULL);
5223 rc1 = lod_get_default_lmv_striping(env, lo, lds);
5224 if (rc == 0 && rc1 < 0)
5231 * Apply default striping on object.
5233 * If object striping pattern is not set, set to the one in default striping.
5234 * The default striping is from parent or fs.
5236 * \param[in] lo new object
5237 * \param[in] lds default striping
5238 * \param[in] mode new object's mode
5240 static void lod_striping_from_default(struct lod_object *lo,
5241 const struct lod_default_striping *lds,
5244 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5247 if (lds->lds_def_striping_set && S_ISREG(mode)) {
5248 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
5250 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
5251 lds->lds_def_comp_cnt);
5255 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
5256 if (lds->lds_def_mirror_cnt > 1)
5257 lo->ldo_flr_state = LCM_FL_RDONLY;
5259 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5260 struct lod_layout_component *obj_comp =
5261 &lo->ldo_comp_entries[i];
5262 struct lod_layout_component *def_comp =
5263 &lds->lds_def_comp_entries[i];
5265 CDEBUG(D_LAYOUT, "Inherit from default: flags=%#x "
5266 "size=%hu nr=%u offset=%u pattern=%#x pool=%s\n",
5267 def_comp->llc_flags,
5268 def_comp->llc_stripe_size,
5269 def_comp->llc_stripe_count,
5270 def_comp->llc_stripe_offset,
5271 def_comp->llc_pattern,
5272 def_comp->llc_pool ?: "");
5274 *obj_comp = *def_comp;
5275 if (def_comp->llc_pool != NULL) {
5276 /* pointer was copied from def_comp */
5277 obj_comp->llc_pool = NULL;
5278 lod_obj_set_pool(lo, i, def_comp->llc_pool);
5282 if (def_comp->llc_ostlist.op_array &&
5283 def_comp->llc_ostlist.op_count) {
5284 OBD_ALLOC(obj_comp->llc_ostlist.op_array,
5285 obj_comp->llc_ostlist.op_size);
5286 if (!obj_comp->llc_ostlist.op_array)
5288 memcpy(obj_comp->llc_ostlist.op_array,
5289 def_comp->llc_ostlist.op_array,
5290 obj_comp->llc_ostlist.op_size);
5291 } else if (def_comp->llc_ostlist.op_array) {
5292 obj_comp->llc_ostlist.op_array = NULL;
5296 * Don't initialize these fields for plain layout
5297 * (v1/v3) here, they are inherited in the order of
5298 * 'parent' -> 'fs default (root)' -> 'global default
5299 * values for stripe_count & stripe_size'.
5301 * see lod_ah_init().
5303 if (!lo->ldo_is_composite)
5306 lod_adjust_stripe_info(obj_comp, desc, 0);
5308 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
5309 if (lo->ldo_dir_stripe_count == 0)
5310 lo->ldo_dir_stripe_count =
5311 lds->lds_dir_def_stripe_count;
5312 if (lo->ldo_dir_stripe_offset == -1)
5313 lo->ldo_dir_stripe_offset =
5314 lds->lds_dir_def_stripe_offset;
5315 if (lo->ldo_dir_hash_type == 0)
5316 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
5318 CDEBUG(D_LAYOUT, "striping from default dir: count:%hu, "
5319 "offset:%u, hash_type:%u\n",
5320 lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
5321 lo->ldo_dir_hash_type);
5325 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root,
5328 struct lod_layout_component *lod_comp;
5330 if (lo->ldo_comp_cnt == 0)
5333 if (lo->ldo_is_composite)
5336 lod_comp = &lo->ldo_comp_entries[0];
5338 if (lod_comp->llc_stripe_count <= 0 ||
5339 lod_comp->llc_stripe_size <= 0)
5342 if (from_root && (lod_comp->llc_pool == NULL ||
5343 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
5346 if (append_pool && append_pool[0])
5353 * Implementation of dt_object_operations::do_ah_init.
5355 * This method is used to make a decision on the striping configuration for the
5356 * object being created. It can be taken from the \a parent object if it exists,
5357 * or filesystem's default. The resulting configuration (number of stripes,
5358 * stripe size/offset, pool name, etc) is stored in the object itself and will
5359 * be used by the methods like ->doo_declare_create().
5361 * \see dt_object_operations::do_ah_init() in the API description for details.
5363 static void lod_ah_init(const struct lu_env *env,
5364 struct dt_allocation_hint *ah,
5365 struct dt_object *parent,
5366 struct dt_object *child,
5369 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
5370 struct lod_thread_info *info = lod_env_info(env);
5371 struct lod_default_striping *lds = lod_lds_buf_get(env);
5372 struct dt_object *nextp = NULL;
5373 struct dt_object *nextc;
5374 struct lod_object *lp = NULL;
5375 struct lod_object *lc;
5376 struct lov_desc *desc;
5377 struct lod_layout_component *lod_comp;
5383 if (ah->dah_append_stripes == -1)
5384 ah->dah_append_stripes =
5385 d->lod_ost_descs.ltd_lov_desc.ld_tgt_count;
5387 if (likely(parent)) {
5388 nextp = dt_object_child(parent);
5389 lp = lod_dt_obj(parent);
5392 nextc = dt_object_child(child);
5393 lc = lod_dt_obj(child);
5395 LASSERT(!lod_obj_is_striped(child));
5396 /* default layout template may have been set on the regular file
5397 * when this is called from mdd_create_data() */
5398 if (S_ISREG(child_mode))
5399 lod_free_comp_entries(lc);
5401 if (!dt_object_exists(nextc))
5402 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
5404 if (S_ISDIR(child_mode)) {
5405 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
5407 /* other default values are 0 */
5408 lc->ldo_dir_stripe_offset = -1;
5410 /* no default striping configuration is needed for
5413 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5414 le32_to_cpu(lum1->lum_magic) == LMV_MAGIC_FOREIGN) {
5415 lc->ldo_dir_is_foreign = true;
5416 /* keep stripe_count 0 and stripe_offset -1 */
5417 CDEBUG(D_INFO, "no default striping for foreign dir\n");
5422 * If parent object is not root directory,
5423 * then get default striping from parent object.
5425 if (likely(lp != NULL)) {
5426 lod_get_default_striping(env, lp, lds);
5428 /* inherit default striping except ROOT */
5429 if ((lds->lds_def_striping_set ||
5430 lds->lds_dir_def_striping_set) &&
5431 !fid_is_root(lod_object_fid(lp)))
5432 lc->ldo_def_striping = lds;
5435 /* It should always honour the specified stripes */
5436 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
5437 * will have old magic. In this case, we should ignore the
5438 * stripe count and try to create dir by default stripe.
5440 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5441 (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
5442 le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
5443 lc->ldo_dir_stripe_count =
5444 le32_to_cpu(lum1->lum_stripe_count);
5445 lc->ldo_dir_stripe_offset =
5446 le32_to_cpu(lum1->lum_stripe_offset);
5447 lc->ldo_dir_hash_type =
5448 le32_to_cpu(lum1->lum_hash_type);
5450 "set dirstripe: count %hu, offset %d, hash %u\n",
5451 lc->ldo_dir_stripe_count,
5452 (int)lc->ldo_dir_stripe_offset,
5453 lc->ldo_dir_hash_type);
5455 /* transfer defaults LMV to new directory */
5456 lod_striping_from_default(lc, lds, child_mode);
5458 /* set count 0 to create normal directory */
5459 if (lc->ldo_dir_stripe_count == 1)
5460 lc->ldo_dir_stripe_count = 0;
5463 /* shrink the stripe_count to the avaible MDT count */
5464 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
5465 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)) {
5466 lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
5467 if (lc->ldo_dir_stripe_count == 1)
5468 lc->ldo_dir_stripe_count = 0;
5471 if (!(lc->ldo_dir_hash_type & LMV_HASH_TYPE_MASK))
5472 lc->ldo_dir_hash_type |=
5473 d->lod_mdt_descs.ltd_lmv_desc.ld_pattern;
5475 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
5476 lc->ldo_dir_stripe_count,
5477 (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
5482 /* child object regular file*/
5484 if (!lod_object_will_be_striped(S_ISREG(child_mode),
5485 lu_object_fid(&child->do_lu)))
5488 /* If object is going to be striped over OSTs, transfer default
5489 * striping information to the child, so that we can use it
5490 * during declaration and creation.
5492 * Try from the parent first.
5494 if (likely(lp != NULL)) {
5495 rc = lod_get_default_lov_striping(env, lp, lds, ah);
5497 lod_striping_from_default(lc, lds, child_mode);
5500 /* Initialize lod_device::lod_md_root object reference */
5501 if (d->lod_md_root == NULL) {
5502 struct dt_object *root;
5503 struct lod_object *lroot;
5505 lu_root_fid(&info->lti_fid);
5506 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
5507 if (!IS_ERR(root)) {
5508 lroot = lod_dt_obj(root);
5510 spin_lock(&d->lod_lock);
5511 if (d->lod_md_root != NULL)
5512 dt_object_put(env, &d->lod_md_root->ldo_obj);
5513 d->lod_md_root = lroot;
5514 spin_unlock(&d->lod_lock);
5518 /* try inherit layout from the root object (fs default) when:
5519 * - parent does not have default layout; or
5520 * - parent has plain(v1/v3) default layout, and some attributes
5521 * are not specified in the default layout;
5523 if (d->lod_md_root != NULL &&
5524 lod_need_inherit_more(lc, true, ah->dah_append_pool)) {
5525 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds,
5529 if (lc->ldo_comp_cnt == 0) {
5530 lod_striping_from_default(lc, lds, child_mode);
5531 } else if (!lds->lds_def_striping_is_composite) {
5532 struct lod_layout_component *def_comp;
5534 LASSERT(!lc->ldo_is_composite);
5535 lod_comp = &lc->ldo_comp_entries[0];
5536 def_comp = &lds->lds_def_comp_entries[0];
5538 if (lod_comp->llc_stripe_count <= 0)
5539 lod_comp->llc_stripe_count =
5540 def_comp->llc_stripe_count;
5541 if (lod_comp->llc_stripe_size <= 0)
5542 lod_comp->llc_stripe_size =
5543 def_comp->llc_stripe_size;
5544 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT &&
5545 (!lod_comp->llc_pool || !lod_comp->llc_pool[0]))
5546 lod_comp->llc_stripe_offset =
5547 def_comp->llc_stripe_offset;
5548 if (lod_comp->llc_pool == NULL)
5549 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
5554 * fs default striping may not be explicitly set, or historically set
5555 * in config log, use them.
5557 if (lod_need_inherit_more(lc, false, ah->dah_append_pool)) {
5558 if (lc->ldo_comp_cnt == 0) {
5559 rc = lod_alloc_comp_entries(lc, 0, 1);
5561 /* fail to allocate memory, will create a
5562 * non-striped file. */
5564 lc->ldo_is_composite = 0;
5565 lod_comp = &lc->ldo_comp_entries[0];
5566 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
5568 LASSERT(!lc->ldo_is_composite);
5569 lod_comp = &lc->ldo_comp_entries[0];
5570 desc = &d->lod_ost_descs.ltd_lov_desc;
5571 lod_adjust_stripe_info(lod_comp, desc, ah->dah_append_stripes);
5572 if (ah->dah_append_pool && ah->dah_append_pool[0])
5573 lod_obj_set_pool(lc, 0, ah->dah_append_pool);
5580 * Size initialization on late striping.
5582 * Propagate the size of a truncated object to a deferred striping.
5583 * This function handles a special case when truncate was done on a
5584 * non-striped object and now while the striping is being created
5585 * we can't lose that size, so we have to propagate it to the stripes
5588 * \param[in] env execution environment
5589 * \param[in] dt object
5590 * \param[in] th transaction handle
5592 * \retval 0 on success
5593 * \retval negative if failed
5595 static int lod_declare_init_size(const struct lu_env *env,
5596 struct dt_object *dt, struct thandle *th)
5598 struct dt_object *next = dt_object_child(dt);
5599 struct lod_object *lo = lod_dt_obj(dt);
5600 struct dt_object **objects = NULL;
5601 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
5602 uint64_t size, offs;
5603 int i, rc, stripe, stripe_count = 0, stripe_size = 0;
5604 struct lu_extent size_ext;
5607 if (!lod_obj_is_striped(dt))
5610 rc = dt_attr_get(env, next, attr);
5611 LASSERT(attr->la_valid & LA_SIZE);
5615 size = attr->la_size;
5619 size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
5620 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5621 struct lod_layout_component *lod_comp;
5622 struct lu_extent *extent;
5624 lod_comp = &lo->ldo_comp_entries[i];
5626 if (lod_comp->llc_stripe == NULL)
5629 extent = &lod_comp->llc_extent;
5630 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
5631 if (!lo->ldo_is_composite ||
5632 lu_extent_is_overlapped(extent, &size_ext)) {
5633 objects = lod_comp->llc_stripe;
5634 stripe_count = lod_comp->llc_stripe_count;
5635 stripe_size = lod_comp->llc_stripe_size;
5638 if (stripe_count == 0)
5641 LASSERT(objects != NULL && stripe_size != 0);
5642 do_div(size, stripe_size);
5643 stripe = do_div(size, stripe_count);
5644 LASSERT(objects[stripe] != NULL);
5646 size = size * stripe_size;
5647 offs = attr->la_size;
5648 size += do_div(offs, stripe_size);
5650 attr->la_valid = LA_SIZE;
5651 attr->la_size = size;
5653 rc = lod_sub_declare_attr_set(env, objects[stripe],
5662 * Declare creation of striped object.
5664 * The function declares creation stripes for a regular object. The function
5665 * also declares whether the stripes will be created with non-zero size if
5666 * previously size was set non-zero on the master object. If object \a dt is
5667 * not local, then only fully defined striping can be applied in \a lovea.
5668 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
5671 * \param[in] env execution environment
5672 * \param[in] dt object
5673 * \param[in] attr attributes the stripes will be created with
5674 * \param[in] lovea a buffer containing striping description
5675 * \param[in] th transaction handle
5677 * \retval 0 on success
5678 * \retval negative if failed
5680 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
5681 struct lu_attr *attr,
5682 const struct lu_buf *lovea, struct thandle *th)
5684 struct lod_thread_info *info = lod_env_info(env);
5685 struct dt_object *next = dt_object_child(dt);
5686 struct lod_object *lo = lod_dt_obj(dt);
5690 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
5691 GOTO(out, rc = -ENOMEM);
5693 if (!dt_object_remote(next)) {
5694 /* choose OST and generate appropriate objects */
5695 rc = lod_prepare_create(env, lo, attr, lovea, th);
5700 * declare storage for striping data
5702 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5704 /* LOD can not choose OST objects for remote objects, i.e.
5705 * stripes must be ready before that. Right now, it can only
5706 * happen during migrate, i.e. migrate process needs to create
5707 * remote regular file (mdd_migrate_create), then the migrate
5708 * process will provide stripeEA. */
5709 LASSERT(lovea != NULL);
5710 info->lti_buf = *lovea;
5713 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
5714 XATTR_NAME_LOV, 0, th);
5719 * if striping is created with local object's size > 0,
5720 * we have to propagate this size to specific object
5721 * the case is possible only when local object was created previously
5723 if (dt_object_exists(next))
5724 rc = lod_declare_init_size(env, dt, th);
5727 /* failed to create striping or to set initial size, let's reset
5728 * config so that others don't get confused */
5730 lod_striping_free(env, lo);
5736 * Whether subdirectories under \a dt should be created on MDTs by space QoS
5738 * If LMV_HASH_FLAG_SPACE is set on directory default layout, its subdirectories
5739 * should be created on MDT by space QoS.
5741 * \param[in] env execution environment
5742 * \param[in] dev lu device
5743 * \param[in] dt object
5745 * \retval 1 if directory should create subdir by space usage
5747 * \retval -ev if failed
5749 static inline int dt_object_qos_mkdir(const struct lu_env *env,
5750 struct lu_device *dev,
5751 struct dt_object *dt)
5753 struct lod_thread_info *info = lod_env_info(env);
5754 struct lu_object *obj;
5755 struct lod_object *lo;
5756 struct lmv_user_md *lmu;
5759 obj = lu_object_find_slice(env, dev, lu_object_fid(&dt->do_lu), NULL);
5761 return PTR_ERR(obj);
5763 lo = lu2lod_obj(obj);
5765 rc = lod_get_default_lmv_ea(env, lo);
5766 dt_object_put(env, dt);
5770 if (rc < (int)sizeof(*lmu))
5773 lmu = info->lti_ea_store;
5774 return le32_to_cpu(lmu->lum_stripe_offset) == LMV_OFFSET_DEFAULT;
5778 * Implementation of dt_object_operations::do_declare_create.
5780 * The method declares creation of a new object. If the object will be striped,
5781 * then helper functions are called to find FIDs for the stripes, declare
5782 * creation of the stripes and declare initialization of the striping
5783 * information to be stored in the master object.
5785 * \see dt_object_operations::do_declare_create() in the API description
5788 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
5789 struct lu_attr *attr,
5790 struct dt_allocation_hint *hint,
5791 struct dt_object_format *dof, struct thandle *th)
5793 struct dt_object *next = dt_object_child(dt);
5794 struct lod_object *lo = lod_dt_obj(dt);
5803 * first of all, we declare creation of local object
5805 rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
5810 * it's lod_ah_init() that has decided the object will be striped
5812 if (dof->dof_type == DFT_REGULAR) {
5813 /* callers don't want stripes */
5814 /* XXX: all tricky interactions with ->ah_make_hint() decided
5815 * to use striping, then ->declare_create() behaving differently
5816 * should be cleaned */
5817 if (dof->u.dof_reg.striped != 0)
5818 rc = lod_declare_striped_create(env, dt, attr,
5820 } else if (dof->dof_type == DFT_DIR) {
5821 struct seq_server_site *ss;
5822 struct lu_buf buf = { NULL };
5823 struct lu_buf *lmu = NULL;
5825 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
5827 /* If the parent has default stripeEA, and client
5828 * did not find it before sending create request,
5829 * then MDT will return -EREMOTE, and client will
5830 * retrieve the default stripeEA and re-create the
5833 * Note: if dah_eadata != NULL, it means creating the
5834 * striped directory with specified stripeEA, then it
5835 * should ignore the default stripeEA */
5836 if (hint != NULL && hint->dah_eadata == NULL) {
5837 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
5838 GOTO(out, rc = -EREMOTE);
5840 if (lo->ldo_dir_stripe_offset == LMV_OFFSET_DEFAULT) {
5841 struct lod_default_striping *lds;
5843 lds = lo->ldo_def_striping;
5845 * child and parent should be on the same MDT,
5846 * but if parent has default LMV, and the start
5847 * MDT offset is -1, it's allowed. This check
5848 * is not necessary after 2.12.22 because client
5849 * follows this already, but old client may not.
5851 if (hint->dah_parent &&
5852 dt_object_remote(hint->dah_parent) && lds &&
5853 lds->lds_dir_def_stripe_offset !=
5855 GOTO(out, rc = -EREMOTE);
5856 } else if (lo->ldo_dir_stripe_offset !=
5858 struct lod_device *lod;
5859 struct lu_tgt_desc *mdt = NULL;
5860 bool found_mdt = false;
5862 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5863 lod_foreach_mdt(lod, mdt) {
5864 if (mdt->ltd_index ==
5865 lo->ldo_dir_stripe_offset) {
5871 /* If the MDT indicated by stripe_offset can be
5872 * found, then tell client to resend the create
5873 * request to the correct MDT, otherwise return
5874 * error to client */
5876 GOTO(out, rc = -EREMOTE);
5878 GOTO(out, rc = -EINVAL);
5880 } else if (hint && hint->dah_eadata) {
5882 lmu->lb_buf = (void *)hint->dah_eadata;
5883 lmu->lb_len = hint->dah_eadata_len;
5886 rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
5890 /* failed to create striping or to set initial size, let's reset
5891 * config so that others don't get confused */
5893 lod_striping_free(env, lo);
5898 * Generate component ID for new created component.
5900 * \param[in] lo LOD object
5901 * \param[in] comp_idx index of ldo_comp_entries
5903 * \retval component ID on success
5904 * \retval LCME_ID_INVAL on failure
5906 static __u32 lod_gen_component_id(struct lod_object *lo,
5907 int mirror_id, int comp_idx)
5909 struct lod_layout_component *lod_comp;
5910 __u32 id, start, end;
5913 LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
5915 lod_obj_inc_layout_gen(lo);
5916 id = lo->ldo_layout_gen;
5917 if (likely(id <= SEQ_ID_MAX))
5918 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
5920 /* Layout generation wraps, need to check collisions. */
5921 start = id & SEQ_ID_MASK;
5924 for (id = start; id <= end; id++) {
5925 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5926 lod_comp = &lo->ldo_comp_entries[i];
5927 if (pflr_id(mirror_id, id) == lod_comp->llc_id)
5930 /* Found the ununsed ID */
5931 if (i == lo->ldo_comp_cnt)
5932 RETURN(pflr_id(mirror_id, id));
5934 if (end == LCME_ID_MAX) {
5936 end = min(lo->ldo_layout_gen & LCME_ID_MASK,
5937 (__u32)(LCME_ID_MAX - 1));
5941 RETURN(LCME_ID_INVAL);
5945 * Creation of a striped regular object.
5947 * The function is called to create the stripe objects for a regular
5948 * striped file. This can happen at the initial object creation or
5949 * when the caller asks LOD to do so using ->do_xattr_set() method
5950 * (so called late striping). Notice all the information are already
5951 * prepared in the form of the list of objects (ldo_stripe field).
5952 * This is done during declare phase.
5954 * \param[in] env execution environment
5955 * \param[in] dt object
5956 * \param[in] attr attributes the stripes will be created with
5957 * \param[in] dof format of stripes (see OSD API description)
5958 * \param[in] th transaction handle
5960 * \retval 0 on success
5961 * \retval negative if failed
5963 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
5964 struct lu_attr *attr, struct dt_object_format *dof,
5967 struct lod_layout_component *lod_comp;
5968 struct lod_object *lo = lod_dt_obj(dt);
5973 mutex_lock(&lo->ldo_layout_mutex);
5975 LASSERT((lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL) ||
5976 lo->ldo_is_foreign);
5978 mirror_id = 0; /* non-flr file's mirror_id is 0 */
5979 if (lo->ldo_mirror_count > 1) {
5980 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5981 lod_comp = &lo->ldo_comp_entries[i];
5982 if (lod_comp->llc_id != LCME_ID_INVAL &&
5983 mirror_id_of(lod_comp->llc_id) > mirror_id)
5984 mirror_id = mirror_id_of(lod_comp->llc_id);
5988 /* create all underlying objects */
5989 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5990 lod_comp = &lo->ldo_comp_entries[i];
5992 if (lod_comp->llc_id == LCME_ID_INVAL) {
5993 /* only the component of FLR layout with more than 1
5994 * mirror has mirror ID in its component ID.
5996 if (lod_comp->llc_extent.e_start == 0 &&
5997 lo->ldo_mirror_count > 1)
6000 lod_comp->llc_id = lod_gen_component_id(lo,
6002 if (lod_comp->llc_id == LCME_ID_INVAL)
6003 GOTO(out, rc = -ERANGE);
6006 if (lod_comp_inited(lod_comp))
6009 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
6010 lod_comp_set_init(lod_comp);
6012 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
6013 lod_comp_set_init(lod_comp);
6015 if (lod_comp->llc_stripe == NULL)
6018 LASSERT(lod_comp->llc_stripe_count);
6019 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6020 struct dt_object *object = lod_comp->llc_stripe[j];
6021 LASSERT(object != NULL);
6022 rc = lod_sub_create(env, object, attr, NULL, dof, th);
6026 lod_comp_set_init(lod_comp);
6029 rc = lod_fill_mirrors(lo);
6033 lo->ldo_comp_cached = 1;
6035 rc = lod_generate_and_set_lovea(env, lo, th);
6039 mutex_unlock(&lo->ldo_layout_mutex);
6044 lod_striping_free_nolock(env, lo);
6045 mutex_unlock(&lo->ldo_layout_mutex);
6050 static inline bool lod_obj_is_dom(struct dt_object *dt)
6052 struct lod_object *lo = lod_dt_obj(dt);
6054 if (!dt_object_exists(dt_object_child(dt)))
6057 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
6060 if (!lo->ldo_comp_cnt)
6063 return (lov_pattern(lo->ldo_comp_entries[0].llc_pattern) ==
6068 * Implementation of dt_object_operations::do_create.
6070 * If any of preceeding methods (like ->do_declare_create(),
6071 * ->do_ah_init(), etc) chose to create a striped object,
6072 * then this method will create the master and the stripes.
6074 * \see dt_object_operations::do_create() in the API description for details.
6076 static int lod_create(const struct lu_env *env, struct dt_object *dt,
6077 struct lu_attr *attr, struct dt_allocation_hint *hint,
6078 struct dt_object_format *dof, struct thandle *th)
6083 /* create local object */
6084 rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
6088 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
6089 (lod_obj_is_striped(dt) || lod_obj_is_dom(dt)) &&
6090 dof->u.dof_reg.striped != 0) {
6091 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
6092 rc = lod_striped_create(env, dt, attr, dof, th);
6099 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
6100 struct dt_object *dt, struct thandle *th,
6101 int comp_idx, int stripe_idx,
6102 struct lod_obj_stripe_cb_data *data)
6104 if (data->locd_declare)
6105 return lod_sub_declare_destroy(env, dt, th);
6107 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
6108 stripe_idx == cfs_fail_val)
6109 return lod_sub_destroy(env, dt, th);
6115 * Implementation of dt_object_operations::do_declare_destroy.
6117 * If the object is a striped directory, then the function declares reference
6118 * removal from the master object (this is an index) to the stripes and declares
6119 * destroy of all the stripes. In all the cases, it declares an intention to
6120 * destroy the object itself.
6122 * \see dt_object_operations::do_declare_destroy() in the API description
6125 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
6128 struct dt_object *next = dt_object_child(dt);
6129 struct lod_object *lo = lod_dt_obj(dt);
6130 struct lod_thread_info *info = lod_env_info(env);
6131 struct dt_object *stripe;
6132 char *stripe_name = info->lti_key;
6138 * load striping information, notice we don't do this when object
6139 * is being initialized as we don't need this information till
6140 * few specific cases like destroy, chown
6142 rc = lod_striping_load(env, lo);
6146 /* declare destroy for all underlying objects */
6147 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6148 rc = next->do_ops->do_index_try(env, next,
6149 &dt_directory_features);
6153 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6154 stripe = lo->ldo_stripe[i];
6158 rc = lod_sub_declare_ref_del(env, next, th);
6162 snprintf(stripe_name, sizeof(info->lti_key),
6164 PFID(lu_object_fid(&stripe->do_lu)), i);
6165 rc = lod_sub_declare_delete(env, next,
6166 (const struct dt_key *)stripe_name, th);
6173 * we declare destroy for the local object
6175 rc = lod_sub_declare_destroy(env, next, th);
6179 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
6180 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
6183 if (!lod_obj_is_striped(dt))
6186 /* declare destroy all striped objects */
6187 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6188 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6189 stripe = lo->ldo_stripe[i];
6193 if (!dt_object_exists(stripe))
6196 rc = lod_sub_declare_ref_del(env, stripe, th);
6200 rc = lod_sub_declare_destroy(env, stripe, th);
6205 struct lod_obj_stripe_cb_data data = { { 0 } };
6207 data.locd_declare = true;
6208 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6209 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6216 * Implementation of dt_object_operations::do_destroy.
6218 * If the object is a striped directory, then the function removes references
6219 * from the master object (this is an index) to the stripes and destroys all
6220 * the stripes. In all the cases, the function destroys the object itself.
6222 * \see dt_object_operations::do_destroy() in the API description for details.
6224 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
6227 struct dt_object *next = dt_object_child(dt);
6228 struct lod_object *lo = lod_dt_obj(dt);
6229 struct lod_thread_info *info = lod_env_info(env);
6230 char *stripe_name = info->lti_key;
6231 struct dt_object *stripe;
6237 /* destroy sub-stripe of master object */
6238 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6239 rc = next->do_ops->do_index_try(env, next,
6240 &dt_directory_features);
6244 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6245 stripe = lo->ldo_stripe[i];
6249 rc = lod_sub_ref_del(env, next, th);
6253 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
6254 PFID(lu_object_fid(&stripe->do_lu)), i);
6256 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
6257 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
6258 PFID(lu_object_fid(&stripe->do_lu)));
6260 rc = lod_sub_delete(env, next,
6261 (const struct dt_key *)stripe_name, th);
6267 rc = lod_sub_destroy(env, next, th);
6271 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
6272 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
6275 if (!lod_obj_is_striped(dt))
6278 /* destroy all striped objects */
6279 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6280 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6281 stripe = lo->ldo_stripe[i];
6285 if (!dt_object_exists(stripe))
6288 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
6289 i == cfs_fail_val) {
6290 dt_write_lock(env, stripe, DT_TGT_CHILD);
6291 rc = lod_sub_ref_del(env, stripe, th);
6292 dt_write_unlock(env, stripe);
6296 rc = lod_sub_destroy(env, stripe, th);
6302 struct lod_obj_stripe_cb_data data = { { 0 } };
6304 data.locd_declare = false;
6305 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6306 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6313 * Implementation of dt_object_operations::do_declare_ref_add.
6315 * \see dt_object_operations::do_declare_ref_add() in the API description
6318 static int lod_declare_ref_add(const struct lu_env *env,
6319 struct dt_object *dt, struct thandle *th)
6321 return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
6325 * Implementation of dt_object_operations::do_ref_add.
6327 * \see dt_object_operations::do_ref_add() in the API description for details.
6329 static int lod_ref_add(const struct lu_env *env,
6330 struct dt_object *dt, struct thandle *th)
6332 return lod_sub_ref_add(env, dt_object_child(dt), th);
6336 * Implementation of dt_object_operations::do_declare_ref_del.
6338 * \see dt_object_operations::do_declare_ref_del() in the API description
6341 static int lod_declare_ref_del(const struct lu_env *env,
6342 struct dt_object *dt, struct thandle *th)
6344 return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
6348 * Implementation of dt_object_operations::do_ref_del
6350 * \see dt_object_operations::do_ref_del() in the API description for details.
6352 static int lod_ref_del(const struct lu_env *env,
6353 struct dt_object *dt, struct thandle *th)
6355 return lod_sub_ref_del(env, dt_object_child(dt), th);
6359 * Implementation of dt_object_operations::do_object_sync.
6361 * \see dt_object_operations::do_object_sync() in the API description
6364 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
6365 __u64 start, __u64 end)
6367 return dt_object_sync(env, dt_object_child(dt), start, end);
6371 * Implementation of dt_object_operations::do_object_unlock.
6373 * Used to release LDLM lock(s).
6375 * \see dt_object_operations::do_object_unlock() in the API description
6378 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
6379 struct ldlm_enqueue_info *einfo,
6380 union ldlm_policy_data *policy)
6382 struct lod_object *lo = lod_dt_obj(dt);
6383 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
6384 int slave_locks_size;
6388 if (slave_locks == NULL)
6391 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
6392 /* Note: for remote lock for single stripe dir, MDT will cancel
6393 * the lock by lockh directly */
6394 LASSERT(!dt_object_remote(dt_object_child(dt)));
6396 /* locks were unlocked in MDT layer */
6397 for (i = 0; i < slave_locks->ha_count; i++)
6398 LASSERT(!lustre_handle_is_used(&slave_locks->ha_handles[i]));
6401 * NB, ha_count may not equal to ldo_dir_stripe_count, because dir
6402 * layout may change, e.g., shrink dir layout after migration.
6404 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6405 if (lo->ldo_stripe[i])
6406 dt_invalidate(env, lo->ldo_stripe[i]);
6409 slave_locks_size = offsetof(typeof(*slave_locks),
6410 ha_handles[slave_locks->ha_count]);
6411 OBD_FREE(slave_locks, slave_locks_size);
6412 einfo->ei_cbdata = NULL;
6418 * Implementation of dt_object_operations::do_object_lock.
6420 * Used to get LDLM lock on the non-striped and striped objects.
6422 * \see dt_object_operations::do_object_lock() in the API description
6425 static int lod_object_lock(const struct lu_env *env,
6426 struct dt_object *dt,
6427 struct lustre_handle *lh,
6428 struct ldlm_enqueue_info *einfo,
6429 union ldlm_policy_data *policy)
6431 struct lod_object *lo = lod_dt_obj(dt);
6432 int slave_locks_size;
6433 struct lustre_handle_array *slave_locks = NULL;
6438 /* remote object lock */
6439 if (!einfo->ei_enq_slave) {
6440 LASSERT(dt_object_remote(dt));
6441 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
6445 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
6448 rc = lod_striping_load(env, lo);
6453 if (lo->ldo_dir_stripe_count <= 1)
6456 slave_locks_size = offsetof(typeof(*slave_locks),
6457 ha_handles[lo->ldo_dir_stripe_count]);
6458 /* Freed in lod_object_unlock */
6459 OBD_ALLOC(slave_locks, slave_locks_size);
6462 slave_locks->ha_count = lo->ldo_dir_stripe_count;
6464 /* striped directory lock */
6465 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6466 struct lustre_handle lockh;
6467 struct ldlm_res_id *res_id;
6468 struct dt_object *stripe;
6470 stripe = lo->ldo_stripe[i];
6474 res_id = &lod_env_info(env)->lti_res_id;
6475 fid_build_reg_res_name(lu_object_fid(&stripe->do_lu), res_id);
6476 einfo->ei_res_id = res_id;
6478 if (dt_object_remote(stripe)) {
6479 set_bit(i, (void *)slave_locks->ha_map);
6480 rc = dt_object_lock(env, stripe, &lockh, einfo, policy);
6482 struct ldlm_namespace *ns = einfo->ei_namespace;
6483 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
6484 ldlm_completion_callback completion = einfo->ei_cb_cp;
6485 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
6487 if (einfo->ei_mode == LCK_PW ||
6488 einfo->ei_mode == LCK_EX)
6489 dlmflags |= LDLM_FL_COS_INCOMPAT;
6491 LASSERT(ns != NULL);
6492 rc = ldlm_cli_enqueue_local(env, ns, res_id, LDLM_IBITS,
6493 policy, einfo->ei_mode,
6494 &dlmflags, blocking,
6496 NULL, 0, LVB_T_NONE,
6501 ldlm_lock_decref_and_cancel(
6502 &slave_locks->ha_handles[i],
6504 OBD_FREE(slave_locks, slave_locks_size);
6507 slave_locks->ha_handles[i] = lockh;
6509 einfo->ei_cbdata = slave_locks;
6515 * Implementation of dt_object_operations::do_invalidate.
6517 * \see dt_object_operations::do_invalidate() in the API description for details
6519 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
6521 return dt_invalidate(env, dt_object_child(dt));
6524 static int lod_declare_instantiate_components(const struct lu_env *env,
6525 struct lod_object *lo,
6529 struct lod_thread_info *info = lod_env_info(env);
6534 LASSERT(info->lti_count < lo->ldo_comp_cnt);
6536 for (i = 0; i < info->lti_count; i++) {
6537 rc = lod_qos_prep_create(env, lo, NULL, th,
6538 info->lti_comp_idx[i], reserve);
6544 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6545 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
6546 &info->lti_buf, XATTR_NAME_LOV, 0, th);
6553 * Check OSTs for an existing component for further extension
6555 * Checks if OSTs are still healthy and not out of space. Gets free space
6556 * on OSTs (relative to allocation watermark rmb_low) and compares to
6557 * the proposed new_end for this component.
6559 * Decides whether or not to extend a component on its current OSTs.
6561 * \param[in] env execution environment for this thread
6562 * \param[in] lo object we're checking
6563 * \param[in] index index of this component
6564 * \param[in] extension_size extension size for this component
6565 * \param[in] extent layout extent for requested operation
6566 * \param[in] comp_extent extension component extent
6567 * \param[in] write if this is write operation
6569 * \retval true - OK to extend on current OSTs
6570 * \retval false - do not extend on current OSTs
6572 static bool lod_sel_osts_allowed(const struct lu_env *env,
6573 struct lod_object *lo,
6574 int index, __u64 reserve,
6575 struct lu_extent *extent,
6576 struct lu_extent *comp_extent, int write)
6578 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[index];
6579 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6580 struct lod_thread_info *tinfo = lod_env_info(env);
6581 struct obd_statfs *sfs = &tinfo->lti_osfs;
6582 __u64 available = 0;
6588 LASSERT(lod_comp->llc_stripe_count != 0);
6590 lod_getref(&lod->lod_ost_descs);
6591 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
6592 int index = lod_comp->llc_ost_indices[i];
6593 struct lod_tgt_desc *ost = OST_TGT(lod, index);
6594 struct obd_statfs_info info = { 0 };
6595 int j, repeated = 0;
6599 /* Get the number of times this OST repeats in this component.
6600 * Note: inter-component repeats are not counted as this is
6601 * considered as a rare case: we try to not repeat OST in other
6602 * components if possible. */
6603 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6604 if (index != lod_comp->llc_ost_indices[j])
6607 /* already handled */
6613 if (j < lod_comp->llc_stripe_count)
6616 if (!test_bit(index, lod->lod_ost_bitmap)) {
6617 CDEBUG(D_LAYOUT, "ost %d no longer present\n", index);
6622 rc = dt_statfs_info(env, ost->ltd_tgt, sfs, &info);
6624 CDEBUG(D_LAYOUT, "statfs failed for ost %d, error %d\n",
6630 if (sfs->os_state & OS_STATFS_ENOSPC ||
6631 sfs->os_state & OS_STATFS_READONLY ||
6632 sfs->os_state & OS_STATFS_DEGRADED) {
6633 CDEBUG(D_LAYOUT, "ost %d is not availble for SEL "
6634 "extension, state %u\n", index, sfs->os_state);
6640 available = sfs->os_bavail * sfs->os_bsize;
6641 /* 'available' is relative to the allocation threshold */
6642 available -= (__u64) info.os_reserved_mb_low << 20;
6644 CDEBUG(D_LAYOUT, "ost %d lowwm: %d highwm: %d, "
6645 "%llu %% blocks available, %llu %% blocks free\n",
6646 index, info.os_reserved_mb_low, info.os_reserved_mb_high,
6647 (100ull * sfs->os_bavail) / sfs->os_blocks,
6648 (100ull * sfs->os_bfree) / sfs->os_blocks);
6650 if (reserve * repeated > available) {
6652 CDEBUG(D_LAYOUT, "low space on ost %d, available %llu "
6653 "< extension size %llu repeated %d\n", index,
6654 available, reserve, repeated);
6658 lod_putref(lod, &lod->lod_ost_descs);
6664 * Adjust extents after component removal
6666 * When we remove an extension component, we move the start of the next
6667 * component to match the start of the extension component, so no space is left
6670 * \param[in] env execution environment for this thread
6671 * \param[in] lo object
6672 * \param[in] max_comp layout component
6673 * \param[in] index index of this component
6675 * \retval 0 on success
6676 * \retval negative errno on error
6678 static void lod_sel_adjust_extents(const struct lu_env *env,
6679 struct lod_object *lo,
6680 int max_comp, int index)
6682 struct lod_layout_component *lod_comp = NULL;
6683 struct lod_layout_component *next = NULL;
6684 struct lod_layout_component *prev = NULL;
6685 __u64 new_start = 0;
6689 /* Extension space component */
6690 lod_comp = &lo->ldo_comp_entries[index];
6691 next = &lo->ldo_comp_entries[index + 1];
6692 prev = &lo->ldo_comp_entries[index - 1];
6694 LASSERT(lod_comp != NULL && prev != NULL && next != NULL);
6695 LASSERT(lod_comp->llc_flags & LCME_FL_EXTENSION);
6697 /* Previous is being removed */
6698 if (prev && prev->llc_id == LCME_ID_INVAL)
6699 new_start = prev->llc_extent.e_start;
6701 new_start = lod_comp->llc_extent.e_start;
6703 for (i = index + 1; i < max_comp; i++) {
6704 lod_comp = &lo->ldo_comp_entries[i];
6706 start = lod_comp->llc_extent.e_start;
6707 lod_comp->llc_extent.e_start = new_start;
6709 /* We only move zero length extendable components */
6710 if (!(start == lod_comp->llc_extent.e_end))
6713 LASSERT(!(lod_comp->llc_flags & LCME_FL_INIT));
6715 lod_comp->llc_extent.e_end = new_start;
6719 /* Calculate the proposed 'new end' for a component we're extending */
6720 static __u64 lod_extension_new_end(__u64 extension_size, __u64 extent_end,
6721 __u32 stripe_size, __u64 component_end,
6722 __u64 extension_end)
6726 LASSERT(extension_size != 0 && stripe_size != 0);
6728 /* Round up to extension size */
6729 if (extent_end == OBD_OBJECT_EOF) {
6730 new_end = OBD_OBJECT_EOF;
6732 /* Add at least extension_size to the previous component_end,
6733 * covering the req layout extent */
6734 new_end = max(extent_end - component_end, extension_size);
6735 new_end = roundup(new_end, extension_size);
6736 new_end += component_end;
6738 /* Component end must be min stripe size aligned */
6739 if (new_end % stripe_size) {
6740 CDEBUG(D_LAYOUT, "new component end is not aligned "
6741 "by the stripe size %u: [%llu, %llu) ext size "
6742 "%llu new end %llu, aligning\n",
6743 stripe_size, component_end, extent_end,
6744 extension_size, new_end);
6745 new_end = roundup(new_end, stripe_size);
6749 if (new_end < extent_end)
6750 new_end = OBD_OBJECT_EOF;
6753 /* Don't extend past the end of the extension component */
6754 if (new_end > extension_end)
6755 new_end = extension_end;
6761 * Calculate the exact reservation (per-OST extension_size) on the OSTs being
6762 * instantiated. It needs to be calculated in advance and taken into account at
6763 * the instantiation time, because otherwise lod_statfs_and_check() may consider
6764 * an OST as OK, but SEL needs its extension_size to fit the free space and the
6765 * OST may turn out to be low-on-space, thus inappropriate OST may be used and
6768 * \param[in] lod_comp lod component we are checking
6770 * \retval size to reserved on each OST of lod_comp's stripe.
6772 static __u64 lod_sel_stripe_reserved(struct lod_layout_component *lod_comp)
6774 /* extension_size is file level, so we must divide by stripe count to
6775 * compare it to available space on a single OST */
6776 return lod_comp->llc_stripe_size * SEL_UNIT_SIZE /
6777 lod_comp->llc_stripe_count;
6780 /* As lod_sel_handler() could be re-entered for the same component several
6781 * times, this is the data for the next call. Fields could be changed to
6782 * component indexes when needed, (e.g. if there is no need to instantiate
6783 * all the previous components up to the current position) to tell the caller
6784 * where to start over from. */
6791 * Process extent updates for a particular layout component
6793 * Handle layout updates for a particular extension space component touched by
6794 * a layout update operation. Core function of self-extending PFL feature.
6796 * In general, this function processes exactly *one* stage of an extension
6797 * operation, modifying the layout accordingly, then returns to the caller.
6798 * The caller is responsible for restarting processing with the new layout,
6799 * which may repeatedly return to this function until the extension updates
6802 * This function does one of a few things to the layout:
6803 * 1. Extends the component before the current extension space component to
6804 * allow it to accomodate the requested operation (if space/policy permit that
6805 * component to continue on its current OSTs)
6807 * 2. If extension of the existing component fails, we do one of two things:
6808 * a. If there is a component after the extension space, we remove the
6809 * extension space component, move the start of the next component down
6810 * accordingly, then notify the caller to restart processing w/the new
6812 * b. If there is no following component, we try repeating the current
6813 * component, creating a new component using the current one as a
6814 * template (keeping its stripe properties but not specific striping),
6815 * and try assigning striping for this component. If there is sufficient
6816 * free space on the OSTs chosen for this component, it is instantiated
6817 * and i/o continues there.
6819 * If there is not sufficient space on the new OSTs, we remove this new
6820 * component & extend the current component.
6822 * Note further that uninited components followed by extension space can be zero
6823 * length meaning that we will try to extend them before initializing them, and
6824 * if that fails, they will be removed without initialization.
6826 * 3. If we extend to/beyond the end of an extension space component, that
6827 * component is exhausted (all of its range has been given to real components),
6828 * so we remove it and restart processing.
6830 * \param[in] env execution environment for this thread
6831 * \param[in,out] lo object to update the layout of
6832 * \param[in] extent layout extent for requested operation, update
6833 * layout to fit this operation
6834 * \param[in] th transaction handle for this operation
6835 * \param[in,out] max_comp the highest comp for the portion of the layout
6836 * we are operating on (For FLR, the chosen
6837 * replica). Updated because we may remove
6839 * \param[in] index index of the extension space component we're
6841 * \param[in] write if this is write op
6842 * \param[in,out] force if the extension is to be forced; set here
6843 to force it on the 2nd call for the same
6846 * \retval 0 on success
6847 * \retval negative errno on error
6849 static int lod_sel_handler(const struct lu_env *env,
6850 struct lod_object *lo,
6851 struct lu_extent *extent,
6852 struct thandle *th, int *max_comp,
6853 int index, int write,
6854 struct sel_data *sd)
6856 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6857 struct lod_thread_info *info = lod_env_info(env);
6858 struct lod_layout_component *lod_comp;
6859 struct lod_layout_component *prev;
6860 struct lod_layout_component *next = NULL;
6861 __u64 extension_size, reserve;
6868 /* First component cannot be extension space */
6870 CERROR("%s: "DFID" first component cannot be extension space\n",
6871 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6875 lod_comp = &lo->ldo_comp_entries[index];
6876 prev = &lo->ldo_comp_entries[index - 1];
6877 if ((index + 1) < *max_comp)
6878 next = &lo->ldo_comp_entries[index + 1];
6880 /* extension size uses the stripe size field as KiB */
6881 extension_size = lod_comp->llc_stripe_size * SEL_UNIT_SIZE;
6883 CDEBUG(D_LAYOUT, "prev start %llu, extension start %llu, extension end"
6884 " %llu, extension size %llu\n", prev->llc_extent.e_start,
6885 lod_comp->llc_extent.e_start, lod_comp->llc_extent.e_end,
6888 /* Two extension space components cannot be adjacent & extension space
6889 * components cannot be init */
6890 if ((prev->llc_flags & LCME_FL_EXTENSION) ||
6891 !(ergo(next, !(next->llc_flags & LCME_FL_EXTENSION))) ||
6892 lod_comp_inited(lod_comp)) {
6893 CERROR("%s: "DFID" invalid extension space components\n",
6894 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6898 reserve = lod_sel_stripe_reserved(lod_comp);
6900 if (!prev->llc_stripe) {
6901 CDEBUG(D_LAYOUT, "Previous component not inited\n");
6902 info->lti_count = 1;
6903 info->lti_comp_idx[0] = index - 1;
6904 rc = lod_declare_instantiate_components(env, lo, th, reserve);
6905 /* ENOSPC tells us we can't use this component. If there is
6906 * a next or we are repeating, we either spill over (next) or
6907 * extend the original comp (repeat). Otherwise, return the
6908 * error to the user. */
6909 if (rc == -ENOSPC && (next || sd->sd_repeat))
6915 if (sd->sd_force == 0 && rc == 0)
6916 rc = !lod_sel_osts_allowed(env, lo, index - 1, reserve, extent,
6917 &lod_comp->llc_extent, write);
6919 repeated = !!(sd->sd_repeat);
6923 /* Extend previous component */
6925 new_end = lod_extension_new_end(extension_size, extent->e_end,
6926 prev->llc_stripe_size,
6927 prev->llc_extent.e_end,
6928 lod_comp->llc_extent.e_end);
6930 CDEBUG(D_LAYOUT, "new end %llu\n", new_end);
6931 lod_comp->llc_extent.e_start = new_end;
6932 prev->llc_extent.e_end = new_end;
6934 if (prev->llc_extent.e_end == lod_comp->llc_extent.e_end) {
6935 CDEBUG(D_LAYOUT, "Extension component exhausted\n");
6936 lod_comp->llc_id = LCME_ID_INVAL;
6940 /* rc == 1, failed to extend current component */
6943 /* Normal 'spillover' case - Remove the extension
6944 * space component & bring down the start of the next
6946 lod_comp->llc_id = LCME_ID_INVAL;
6948 if (!(prev->llc_flags & LCME_FL_INIT)) {
6949 prev->llc_id = LCME_ID_INVAL;
6952 lod_sel_adjust_extents(env, lo, *max_comp, index);
6953 } else if (lod_comp_inited(prev)) {
6954 /* If there is no next, and the previous component is
6955 * INIT'ed, try repeating the previous component. */
6956 LASSERT(repeated == 0);
6957 rc = lod_layout_repeat_comp(env, lo, index - 1);
6961 /* The previous component is a repeated component.
6962 * Record this so we don't keep trying to repeat it. */
6965 /* If the previous component is not INIT'ed, this may
6966 * be a component we have just instantiated but failed
6967 * to extend. Or even a repeated component we failed
6968 * to prepare a striping for. Do not repeat but instead
6969 * remove the repeated component & force the extention
6970 * of the original one */
6973 prev->llc_id = LCME_ID_INVAL;
6980 rc = lod_layout_del_prep_layout(env, lo, NULL);
6983 LASSERTF(-rc == change,
6984 "number deleted %d != requested %d\n", -rc,
6987 *max_comp = *max_comp + change;
6989 /* lod_del_prep_layout reallocates ldo_comp_entries, so we must
6990 * refresh these pointers before using them */
6991 lod_comp = &lo->ldo_comp_entries[index];
6992 prev = &lo->ldo_comp_entries[index - 1];
6993 CDEBUG(D_LAYOUT, "After extent updates: prev start %llu, current start "
6994 "%llu, current end %llu max_comp %d ldo_comp_cnt %d\n",
6995 prev->llc_extent.e_start, lod_comp->llc_extent.e_start,
6996 lod_comp->llc_extent.e_end, *max_comp, lo->ldo_comp_cnt);
6998 /* Layout changed successfully */
7003 * Declare layout extent updates
7005 * Handles extensions. Identifies extension components touched by current
7006 * operation and passes them to processing function.
7008 * Restarts with updated layouts from the processing function until the current
7009 * operation no longer touches an extension space component.
7011 * \param[in] env execution environment for this thread
7012 * \param[in,out] lo object to update the layout of
7013 * \param[in] extent layout extent for requested operation, update layout to
7014 * fit this operation
7015 * \param[in] th transaction handle for this operation
7016 * \param[in] pick identifies chosen mirror for FLR layouts
7017 * \param[in] write if this is write op
7019 * \retval 1 on layout changed, 0 on no change
7020 * \retval negative errno on error
7022 static int lod_declare_update_extents(const struct lu_env *env,
7023 struct lod_object *lo, struct lu_extent *extent,
7024 struct thandle *th, int pick, int write)
7026 struct lod_thread_info *info = lod_env_info(env);
7027 struct lod_layout_component *lod_comp;
7028 bool layout_changed = false;
7029 struct sel_data sd = { 0 };
7037 /* This makes us work on the components of the chosen mirror */
7038 start_index = lo->ldo_mirrors[pick].lme_start;
7039 max_comp = lo->ldo_mirrors[pick].lme_end + 1;
7040 if (lo->ldo_flr_state == LCM_FL_NONE)
7041 LASSERT(start_index == 0 && max_comp == lo->ldo_comp_cnt);
7043 CDEBUG(D_LAYOUT, "extent->e_start %llu, extent->e_end %llu\n",
7044 extent->e_start, extent->e_end);
7045 for (i = start_index; i < max_comp; i++) {
7046 lod_comp = &lo->ldo_comp_entries[i];
7048 /* We've passed all components of interest */
7049 if (lod_comp->llc_extent.e_start >= extent->e_end)
7052 if (lod_comp->llc_flags & LCME_FL_EXTENSION) {
7053 layout_changed = true;
7054 rc = lod_sel_handler(env, lo, extent, th, &max_comp,
7059 /* Nothing has changed behind the prev one */
7065 /* We may have added or removed components. If so, we must update the
7066 * start & ends of all the mirrors after the current one, and the end
7067 * of the current mirror. */
7068 change = max_comp - 1 - lo->ldo_mirrors[pick].lme_end;
7070 lo->ldo_mirrors[pick].lme_end += change;
7071 for (i = pick + 1; i < lo->ldo_mirror_count; i++) {
7072 lo->ldo_mirrors[i].lme_start += change;
7073 lo->ldo_mirrors[i].lme_end += change;
7079 /* The amount of components has changed, adjust the lti_comp_idx */
7080 rc2 = lod_layout_data_init(info, lo->ldo_comp_cnt);
7082 return rc < 0 ? rc : rc2 < 0 ? rc2 : layout_changed;
7085 /* If striping is already instantiated or INIT'ed DOM? */
7086 static bool lod_is_instantiation_needed(struct lod_layout_component *comp)
7088 return !(((lov_pattern(comp->llc_pattern) == LOV_PATTERN_MDT) &&
7089 lod_comp_inited(comp)) || comp->llc_stripe);
7093 * Declare layout update for a non-FLR layout.
7095 * \param[in] env execution environment for this thread
7096 * \param[in,out] lo object to update the layout of
7097 * \param[in] layout layout intent for requested operation, "update" is
7098 * a process of reacting to this
7099 * \param[in] buf buffer containing lov ea (see comment on usage inline)
7100 * \param[in] th transaction handle for this operation
7102 * \retval 0 on success
7103 * \retval negative errno on error
7105 static int lod_declare_update_plain(const struct lu_env *env,
7106 struct lod_object *lo, struct layout_intent *layout,
7107 const struct lu_buf *buf, struct thandle *th)
7109 struct lod_thread_info *info = lod_env_info(env);
7110 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7111 struct lod_layout_component *lod_comp;
7112 struct lov_comp_md_v1 *comp_v1 = NULL;
7113 bool layout_changed = false;
7114 bool replay = false;
7118 LASSERT(lo->ldo_flr_state == LCM_FL_NONE);
7121 * In case the client is passing lovea, which only happens during
7122 * the replay of layout intent write RPC for now, we may need to
7123 * parse the lovea and apply new layout configuration.
7125 if (buf && buf->lb_len) {
7126 struct lov_user_md_v1 *v1 = buf->lb_buf;
7128 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
7129 v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
7130 LOV_MAGIC_COMP_V1)) {
7131 CERROR("%s: the replay buffer of layout extend "
7132 "(magic %#x) does not contain expected "
7133 "composite layout.\n",
7134 lod2obd(d)->obd_name, v1->lmm_magic);
7135 GOTO(out, rc = -EINVAL);
7138 rc = lod_use_defined_striping(env, lo, buf);
7141 lo->ldo_comp_cached = 1;
7143 rc = lod_get_lov_ea(env, lo);
7146 /* old on-disk EA is stored in info->lti_buf */
7147 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
7149 layout_changed = true;
7151 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
7155 /* non replay path */
7156 rc = lod_striping_load(env, lo);
7161 /* Make sure defined layout covers the requested write range. */
7162 lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
7163 if (lo->ldo_comp_cnt > 1 &&
7164 lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
7165 lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
7166 CDEBUG_LIMIT(replay ? D_ERROR : D_LAYOUT,
7167 "%s: the defined layout [0, %#llx) does not "
7168 "covers the write range "DEXT"\n",
7169 lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
7170 PEXT(&layout->li_extent));
7171 GOTO(out, rc = -EINVAL);
7174 CDEBUG(D_LAYOUT, "%s: "DFID": update components "DEXT"\n",
7175 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
7176 PEXT(&layout->li_extent));
7179 rc = lod_declare_update_extents(env, lo, &layout->li_extent,
7180 th, 0, layout->li_opc == LAYOUT_INTENT_WRITE);
7184 layout_changed = true;
7188 * Iterate ld->ldo_comp_entries, find the component whose extent under
7189 * the write range and not instantianted.
7191 for (i = 0; i < lo->ldo_comp_cnt; i++) {
7192 lod_comp = &lo->ldo_comp_entries[i];
7194 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
7198 /* If striping is instantiated or INIT'ed DOM skip */
7199 if (!lod_is_instantiation_needed(lod_comp))
7203 * In replay path, lod_comp is the EA passed by
7204 * client replay buffer, comp_v1 is the pre-recovery
7205 * on-disk EA, we'd sift out those components which
7206 * were init-ed in the on-disk EA.
7208 if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
7213 * this component hasn't instantiated in normal path, or during
7214 * replay it needs replay the instantiation.
7217 /* A released component is being extended */
7218 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
7219 GOTO(out, rc = -EINVAL);
7221 LASSERT(info->lti_comp_idx != NULL);
7222 info->lti_comp_idx[info->lti_count++] = i;
7223 layout_changed = true;
7226 if (!layout_changed)
7229 lod_obj_inc_layout_gen(lo);
7230 rc = lod_declare_instantiate_components(env, lo, th, 0);
7234 lod_striping_free(env, lo);
7238 static inline int lod_comp_index(struct lod_object *lo,
7239 struct lod_layout_component *lod_comp)
7241 LASSERT(lod_comp >= lo->ldo_comp_entries &&
7242 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
7244 return lod_comp - lo->ldo_comp_entries;
7248 * Stale other mirrors by writing extent.
7250 static int lod_stale_components(const struct lu_env *env, struct lod_object *lo,
7251 int primary, struct lu_extent *extent,
7254 struct lod_layout_component *pri_comp, *lod_comp;
7255 struct lu_extent pri_extent;
7260 /* The writing extent decides which components in the primary
7261 * are affected... */
7262 CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
7265 lod_foreach_mirror_comp(pri_comp, lo, primary) {
7266 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
7269 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
7270 lod_comp_index(lo, pri_comp),
7271 PEXT(&pri_comp->llc_extent));
7273 pri_extent.e_start = pri_comp->llc_extent.e_start;
7274 pri_extent.e_end = pri_comp->llc_extent.e_end;
7276 for (i = 0; i < lo->ldo_mirror_count; i++) {
7279 rc = lod_declare_update_extents(env, lo, &pri_extent,
7281 /* if update_extents changed the layout, it may have
7282 * reallocated the component array, so start over to
7283 * avoid using stale pointers */
7289 /* ... and then stale other components that are
7290 * overlapping with primary components */
7291 lod_foreach_mirror_comp(lod_comp, lo, i) {
7292 if (!lu_extent_is_overlapped(
7294 &lod_comp->llc_extent))
7297 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
7298 i, lod_comp_index(lo, lod_comp));
7300 lod_comp->llc_flags |= LCME_FL_STALE;
7301 lo->ldo_mirrors[i].lme_stale = 1;
7310 * check an OST's availability
7311 * \param[in] env execution environment
7312 * \param[in] lo lod object
7313 * \param[in] dt dt object
7314 * \param[in] index mirror index
7316 * \retval negative if failed
7317 * \retval 1 if \a dt is available
7318 * \retval 0 if \a dt is not available
7320 static inline int lod_check_ost_avail(const struct lu_env *env,
7321 struct lod_object *lo,
7322 struct dt_object *dt, int index)
7324 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7325 struct lod_tgt_desc *ost;
7327 int type = LU_SEQ_RANGE_OST;
7330 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &idx, &type);
7332 CERROR("%s: can't locate "DFID":rc = %d\n",
7333 lod2obd(lod)->obd_name, PFID(lu_object_fid(&dt->do_lu)),
7338 ost = OST_TGT(lod, idx);
7339 if (ost->ltd_statfs.os_state &
7340 (OS_STATFS_READONLY | OS_STATFS_ENOSPC | OS_STATFS_ENOINO |
7341 OS_STATFS_NOPRECREATE) ||
7342 ost->ltd_active == 0) {
7343 CDEBUG(D_LAYOUT, DFID ": mirror %d OST%d unavail, rc = %d\n",
7344 PFID(lod_object_fid(lo)), index, idx, rc);
7352 * Pick primary mirror for write
7353 * \param[in] env execution environment
7354 * \param[in] lo object
7355 * \param[in] extent write range
7357 static int lod_primary_pick(const struct lu_env *env, struct lod_object *lo,
7358 struct lu_extent *extent)
7360 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7361 unsigned int seq = 0;
7362 struct lod_layout_component *lod_comp;
7364 int picked = -1, second_pick = -1, third_pick = -1;
7367 if (OBD_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
7368 get_random_bytes(&seq, sizeof(seq));
7369 seq %= lo->ldo_mirror_count;
7373 * Pick a mirror as the primary, and check the availability of OSTs.
7375 * This algo can be revised later after knowing the topology of
7378 lod_qos_statfs_update(env, lod, &lod->lod_ost_descs);
7379 for (i = 0; i < lo->ldo_mirror_count; i++) {
7380 bool ost_avail = true;
7381 int index = (i + seq) % lo->ldo_mirror_count;
7383 if (lo->ldo_mirrors[index].lme_stale) {
7384 CDEBUG(D_LAYOUT, DFID": mirror %d stale\n",
7385 PFID(lod_object_fid(lo)), index);
7389 /* 2nd pick is for the primary mirror containing unavail OST */
7390 if (lo->ldo_mirrors[index].lme_prefer && second_pick < 0)
7391 second_pick = index;
7393 /* 3rd pick is for non-primary mirror containing unavail OST */
7394 if (second_pick < 0 && third_pick < 0)
7398 * we found a non-primary 1st pick, we'd like to find a
7399 * potential pirmary mirror.
7401 if (picked >= 0 && !lo->ldo_mirrors[index].lme_prefer)
7404 /* check the availability of OSTs */
7405 lod_foreach_mirror_comp(lod_comp, lo, index) {
7406 if (!lod_comp_inited(lod_comp) || !lod_comp->llc_stripe)
7409 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
7410 struct dt_object *dt = lod_comp->llc_stripe[j];
7412 rc = lod_check_ost_avail(env, lo, dt, index);
7419 } /* for all dt object in one component */
7422 } /* for all components in a mirror */
7425 * the OSTs where allocated objects locates in the components
7426 * of the mirror are available.
7431 /* this mirror has all OSTs available */
7435 * primary with all OSTs are available, this is the perfect
7438 if (lo->ldo_mirrors[index].lme_prefer)
7440 } /* for all mirrors */
7442 /* failed to pick a sound mirror, lower our expectation */
7444 picked = second_pick;
7446 picked = third_pick;
7453 static int lod_prepare_resync_mirror(const struct lu_env *env,
7454 struct lod_object *lo,
7457 struct lod_thread_info *info = lod_env_info(env);
7458 struct lod_layout_component *lod_comp;
7459 bool neg = !!(MIRROR_ID_NEG & mirror_id);
7462 mirror_id &= ~MIRROR_ID_NEG;
7464 for (i = 0; i < lo->ldo_mirror_count; i++) {
7465 if ((!neg && lo->ldo_mirrors[i].lme_id != mirror_id) ||
7466 (neg && lo->ldo_mirrors[i].lme_id == mirror_id))
7469 lod_foreach_mirror_comp(lod_comp, lo, i) {
7470 if (lod_comp_inited(lod_comp))
7473 info->lti_comp_idx[info->lti_count++] =
7474 lod_comp_index(lo, lod_comp);
7482 * figure out the components should be instantiated for resync.
7484 static int lod_prepare_resync(const struct lu_env *env, struct lod_object *lo,
7485 struct lu_extent *extent)
7487 struct lod_thread_info *info = lod_env_info(env);
7488 struct lod_layout_component *lod_comp;
7489 unsigned int need_sync = 0;
7493 DFID": instantiate all stale components in "DEXT"\n",
7494 PFID(lod_object_fid(lo)), PEXT(extent));
7497 * instantiate all components within this extent, even non-stale
7500 for (i = 0; i < lo->ldo_mirror_count; i++) {
7501 if (!lo->ldo_mirrors[i].lme_stale)
7504 lod_foreach_mirror_comp(lod_comp, lo, i) {
7505 if (!lu_extent_is_overlapped(extent,
7506 &lod_comp->llc_extent))
7511 if (lod_comp_inited(lod_comp))
7514 CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
7515 i, lod_comp_index(lo, lod_comp));
7516 info->lti_comp_idx[info->lti_count++] =
7517 lod_comp_index(lo, lod_comp);
7521 return need_sync ? 0 : -EALREADY;
7524 static int lod_declare_update_rdonly(const struct lu_env *env,
7525 struct lod_object *lo, struct md_layout_change *mlc,
7528 struct lod_thread_info *info = lod_env_info(env);
7529 struct lu_attr *layout_attr = &info->lti_layout_attr;
7530 struct lod_layout_component *lod_comp;
7531 struct lu_extent extent = { 0 };
7535 LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
7536 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7537 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7538 LASSERT(lo->ldo_mirror_count > 0);
7540 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7541 struct layout_intent *layout = mlc->mlc_intent;
7542 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7545 extent = layout->li_extent;
7546 CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
7547 PFID(lod_object_fid(lo)), PEXT(&extent));
7549 picked = lod_primary_pick(env, lo, &extent);
7553 CDEBUG(D_LAYOUT, DFID": picked mirror id %u as primary\n",
7554 PFID(lod_object_fid(lo)),
7555 lo->ldo_mirrors[picked].lme_id);
7557 /* Update extents of primary before staling */
7558 rc = lod_declare_update_extents(env, lo, &extent, th, picked,
7563 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
7565 * trunc transfers [0, size) in the intent extent, we'd
7566 * stale components overlapping [size, eof).
7568 extent.e_start = extent.e_end;
7569 extent.e_end = OBD_OBJECT_EOF;
7572 /* stale overlapping components from other mirrors */
7573 rc = lod_stale_components(env, lo, picked, &extent, th);
7577 /* restore truncate intent extent */
7578 if (layout->li_opc == LAYOUT_INTENT_TRUNC)
7579 extent.e_end = extent.e_start;
7581 /* instantiate components for the picked mirror, start from 0 */
7584 lod_foreach_mirror_comp(lod_comp, lo, picked) {
7585 if (!lu_extent_is_overlapped(&extent,
7586 &lod_comp->llc_extent))
7589 if (!lod_is_instantiation_needed(lod_comp))
7592 info->lti_comp_idx[info->lti_count++] =
7593 lod_comp_index(lo, lod_comp);
7596 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7597 } else { /* MD_LAYOUT_RESYNC */
7601 * could contain multiple non-stale mirrors, so we need to
7602 * prep uninited all components assuming any non-stale mirror
7603 * could be picked as the primary mirror.
7605 if (mlc->mlc_mirror_id == 0) {
7607 for (i = 0; i < lo->ldo_mirror_count; i++) {
7608 if (lo->ldo_mirrors[i].lme_stale)
7611 lod_foreach_mirror_comp(lod_comp, lo, i) {
7612 if (!lod_comp_inited(lod_comp))
7616 lod_comp->llc_extent.e_end)
7618 lod_comp->llc_extent.e_end;
7621 rc = lod_prepare_resync(env, lo, &extent);
7625 /* mirror write, try to init its all components */
7626 rc = lod_prepare_resync_mirror(env, lo,
7627 mlc->mlc_mirror_id);
7632 /* change the file state to SYNC_PENDING */
7633 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7636 /* Reset the layout version once it's becoming too large.
7637 * This way it can make sure that the layout version is
7638 * monotonously increased in this writing era. */
7639 lod_obj_inc_layout_gen(lo);
7640 if (lo->ldo_layout_gen > (LCME_ID_MAX >> 1)) {
7641 __u32 layout_version;
7643 get_random_bytes(&layout_version, sizeof(layout_version));
7644 lo->ldo_layout_gen = layout_version & 0xffff;
7647 rc = lod_declare_instantiate_components(env, lo, th, 0);
7651 layout_attr->la_valid = LA_LAYOUT_VERSION;
7652 layout_attr->la_layout_version = 0; /* set current version */
7653 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7654 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7655 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7661 lod_striping_free(env, lo);
7665 static int lod_declare_update_write_pending(const struct lu_env *env,
7666 struct lod_object *lo, struct md_layout_change *mlc,
7669 struct lod_thread_info *info = lod_env_info(env);
7670 struct lu_attr *layout_attr = &info->lti_layout_attr;
7671 struct lod_layout_component *lod_comp;
7672 struct lu_extent extent = { 0 };
7678 LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
7679 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7680 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7682 /* look for the first preferred mirror */
7683 for (i = 0; i < lo->ldo_mirror_count; i++) {
7684 if (lo->ldo_mirrors[i].lme_stale)
7686 if (lo->ldo_mirrors[i].lme_prefer == 0)
7693 /* no primary, use any in-sync */
7694 for (i = 0; i < lo->ldo_mirror_count; i++) {
7695 if (lo->ldo_mirrors[i].lme_stale)
7701 CERROR(DFID ": doesn't have a primary mirror\n",
7702 PFID(lod_object_fid(lo)));
7703 GOTO(out, rc = -ENODATA);
7707 CDEBUG(D_LAYOUT, DFID": found primary %u\n",
7708 PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
7710 LASSERT(!lo->ldo_mirrors[primary].lme_stale);
7712 /* for LAYOUT_WRITE opc, it has to do the following operations:
7713 * 1. stale overlapping componets from stale mirrors;
7714 * 2. instantiate components of the primary mirror;
7715 * 3. transfter layout version to all objects of the primary;
7717 * for LAYOUT_RESYNC opc, it will do:
7718 * 1. instantiate components of all stale mirrors;
7719 * 2. transfer layout version to all objects to close write era. */
7721 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7722 struct layout_intent *layout = mlc->mlc_intent;
7723 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7725 LASSERT(mlc->mlc_intent != NULL);
7727 extent = mlc->mlc_intent->li_extent;
7729 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
7730 PFID(lod_object_fid(lo)), PEXT(&extent));
7732 /* 1. Update extents of primary before staling */
7733 rc = lod_declare_update_extents(env, lo, &extent, th, primary,
7738 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
7740 * trunc transfers [0, size) in the intent extent, we'd
7741 * stale components overlapping [size, eof).
7743 extent.e_start = extent.e_end;
7744 extent.e_end = OBD_OBJECT_EOF;
7747 /* 2. stale overlapping components */
7748 rc = lod_stale_components(env, lo, primary, &extent, th);
7752 /* 3. find the components which need instantiating.
7753 * instantiate [0, mlc->mlc_intent->e_end) */
7755 /* restore truncate intent extent */
7756 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC)
7757 extent.e_end = extent.e_start;
7760 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7761 if (!lu_extent_is_overlapped(&extent,
7762 &lod_comp->llc_extent))
7765 if (!lod_is_instantiation_needed(lod_comp))
7768 CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
7769 primary, lod_comp_index(lo, lod_comp));
7770 info->lti_comp_idx[info->lti_count++] =
7771 lod_comp_index(lo, lod_comp);
7773 } else { /* MD_LAYOUT_RESYNC */
7774 if (mlc->mlc_mirror_id == 0) {
7776 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7777 if (!lod_comp_inited(lod_comp))
7780 extent.e_end = lod_comp->llc_extent.e_end;
7783 rc = lod_prepare_resync(env, lo, &extent);
7787 /* mirror write, try to init its all components */
7788 rc = lod_prepare_resync_mirror(env, lo,
7789 mlc->mlc_mirror_id);
7794 /* change the file state to SYNC_PENDING */
7795 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7798 rc = lod_declare_instantiate_components(env, lo, th, 0);
7802 /* 3. transfer layout version to OST objects.
7803 * transfer new layout version to OST objects so that stale writes
7804 * can be denied. It also ends an era of writing by setting
7805 * LU_LAYOUT_RESYNC. Normal client can never use this bit to
7806 * send write RPC; only resync RPCs could do it. */
7807 layout_attr->la_valid = LA_LAYOUT_VERSION;
7808 layout_attr->la_layout_version = 0; /* set current version */
7809 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7810 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7811 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7815 lod_obj_inc_layout_gen(lo);
7818 lod_striping_free(env, lo);
7822 static int lod_declare_update_sync_pending(const struct lu_env *env,
7823 struct lod_object *lo, struct md_layout_change *mlc,
7826 struct lod_thread_info *info = lod_env_info(env);
7827 unsigned sync_components = 0;
7828 unsigned resync_components = 0;
7833 LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
7834 LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
7835 mlc->mlc_opc == MD_LAYOUT_WRITE);
7837 CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
7838 PFID(lod_object_fid(lo)), mlc->mlc_opc);
7840 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7841 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
7842 PFID(lod_object_fid(lo)));
7844 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7845 return lod_declare_update_write_pending(env, lo, mlc, th);
7848 /* MD_LAYOUT_RESYNC_DONE */
7850 for (i = 0; i < lo->ldo_comp_cnt; i++) {
7851 struct lod_layout_component *lod_comp;
7854 lod_comp = &lo->ldo_comp_entries[i];
7856 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
7861 for (j = 0; j < mlc->mlc_resync_count; j++) {
7862 if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
7865 mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
7866 lod_comp->llc_flags &= ~LCME_FL_STALE;
7867 resync_components++;
7873 for (i = 0; i < mlc->mlc_resync_count; i++) {
7874 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
7877 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
7878 "or already synced\n", PFID(lod_object_fid(lo)),
7879 mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
7880 GOTO(out, rc = -EINVAL);
7883 if (!sync_components || (mlc->mlc_resync_count && !resync_components)) {
7884 CDEBUG(D_LAYOUT, DFID": no mirror in sync\n",
7885 PFID(lod_object_fid(lo)));
7887 /* tend to return an error code here to prevent
7888 * the MDT from setting SoM attribute */
7889 GOTO(out, rc = -EINVAL);
7892 CDEBUG(D_LAYOUT, DFID": synced %u resynced %u/%zu components\n",
7893 PFID(lod_object_fid(lo)),
7894 sync_components, resync_components, mlc->mlc_resync_count);
7896 lo->ldo_flr_state = LCM_FL_RDONLY;
7897 lod_obj_inc_layout_gen(lo);
7899 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
7900 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
7901 &info->lti_buf, XATTR_NAME_LOV, 0, th);
7906 lod_striping_free(env, lo);
7910 typedef int (*mlc_handler)(const struct lu_env *env, struct dt_object *dt,
7911 const struct md_layout_change *mlc,
7912 struct thandle *th);
7915 * Attach stripes after target's for migrating directory. NB, we
7916 * only need to declare this, the actual work is done inside
7917 * lod_xattr_set_lmv().
7919 * \param[in] env execution environment
7920 * \param[in] dt target object
7921 * \param[in] mlc layout change data
7922 * \param[in] th transaction handle
7924 * \retval 0 on success
7925 * \retval negative if failed
7927 static int lod_dir_declare_layout_attach(const struct lu_env *env,
7928 struct dt_object *dt,
7929 const struct md_layout_change *mlc,
7932 struct lod_thread_info *info = lod_env_info(env);
7933 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
7934 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
7935 struct lod_object *lo = lod_dt_obj(dt);
7936 struct dt_object *next = dt_object_child(dt);
7937 struct dt_object_format *dof = &info->lti_format;
7938 struct lmv_mds_md_v1 *lmv = mlc->mlc_buf.lb_buf;
7939 struct dt_object **stripes;
7940 __u32 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
7941 struct lu_fid *fid = &info->lti_fid;
7942 struct lod_tgt_desc *tgt;
7943 struct dt_object *dto;
7944 struct dt_device *tgt_dt;
7945 int type = LU_SEQ_RANGE_ANY;
7946 struct dt_insert_rec *rec = &info->lti_dt_rec;
7947 char *stripe_name = info->lti_key;
7948 struct lu_name *sname;
7949 struct linkea_data ldata = { NULL };
7950 struct lu_buf linkea_buf;
7957 if (!lmv_is_sane(lmv))
7960 if (!dt_try_as_dir(env, dt))
7963 dof->dof_type = DFT_DIR;
7965 OBD_ALLOC_PTR_ARRAY(stripes, (lo->ldo_dir_stripe_count + stripe_count));
7969 for (i = 0; i < lo->ldo_dir_stripe_count; i++)
7970 stripes[i] = lo->ldo_stripe[i];
7972 rec->rec_type = S_IFDIR;
7974 for (i = 0; i < stripe_count; i++) {
7976 &lmv->lmv_stripe_fids[i]);
7977 if (!fid_is_sane(fid))
7980 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
7984 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
7985 tgt_dt = lod->lod_child;
7987 tgt = LTD_TGT(ltd, idx);
7989 GOTO(out, rc = -ESTALE);
7990 tgt_dt = tgt->ltd_tgt;
7993 dto = dt_locate_at(env, tgt_dt, fid,
7994 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
7997 GOTO(out, rc = PTR_ERR(dto));
7999 stripes[i + lo->ldo_dir_stripe_count] = dto;
8001 if (!dt_try_as_dir(env, dto))
8002 GOTO(out, rc = -ENOTDIR);
8004 rc = lod_sub_declare_ref_add(env, dto, th);
8008 rec->rec_fid = lu_object_fid(&dto->do_lu);
8009 rc = lod_sub_declare_insert(env, dto,
8010 (const struct dt_rec *)rec,
8011 (const struct dt_key *)dot, th);
8015 rc = lod_sub_declare_insert(env, dto,
8016 (const struct dt_rec *)rec,
8017 (const struct dt_key *)dotdot, th);
8021 rc = lod_sub_declare_xattr_set(env, dto, &mlc->mlc_buf,
8022 XATTR_NAME_LMV, 0, th);
8026 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
8027 PFID(lu_object_fid(&dto->do_lu)),
8028 i + lo->ldo_dir_stripe_count);
8030 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
8031 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
8032 sname, lu_object_fid(&dt->do_lu));
8036 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
8037 linkea_buf.lb_len = ldata.ld_leh->leh_len;
8038 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
8039 XATTR_NAME_LINK, 0, th);
8043 rc = lod_sub_declare_insert(env, next,
8044 (const struct dt_rec *)rec,
8045 (const struct dt_key *)stripe_name,
8050 rc = lod_sub_declare_ref_add(env, next, th);
8056 OBD_FREE_PTR_ARRAY(lo->ldo_stripe,
8057 lo->ldo_dir_stripes_allocated);
8058 lo->ldo_stripe = stripes;
8059 lo->ldo_dir_migrate_offset = lo->ldo_dir_stripe_count;
8060 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_hash_type);
8061 lo->ldo_dir_stripe_count += stripe_count;
8062 lo->ldo_dir_stripes_allocated += stripe_count;
8064 /* plain directory split creates target as a plain directory, while
8065 * after source attached as the first stripe, it becomes a striped
8066 * directory, set correct do_index_ops, otherwise it can't be unlinked.
8068 dt->do_index_ops = &lod_striped_index_ops;
8072 i = lo->ldo_dir_stripe_count;
8073 while (i < lo->ldo_dir_stripe_count + stripe_count && stripes[i])
8074 dt_object_put(env, stripes[i++]);
8076 OBD_FREE_PTR_ARRAY(stripes, stripe_count + lo->ldo_dir_stripe_count);
8080 static int lod_dir_declare_layout_detach(const struct lu_env *env,
8081 struct dt_object *dt,
8082 const struct md_layout_change *unused,
8085 struct lod_thread_info *info = lod_env_info(env);
8086 struct lod_object *lo = lod_dt_obj(dt);
8087 struct dt_object *next = dt_object_child(dt);
8088 char *stripe_name = info->lti_key;
8089 struct dt_object *dto;
8093 if (!dt_try_as_dir(env, dt))
8096 if (!lo->ldo_dir_stripe_count)
8097 return lod_sub_declare_delete(env, next,
8098 (const struct dt_key *)dotdot, th);
8100 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8101 dto = lo->ldo_stripe[i];
8105 if (!dt_try_as_dir(env, dto))
8108 rc = lod_sub_declare_delete(env, dto,
8109 (const struct dt_key *)dotdot, th);
8113 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8114 PFID(lu_object_fid(&dto->do_lu)), i);
8116 rc = lod_sub_declare_delete(env, next,
8117 (const struct dt_key *)stripe_name, th);
8121 rc = lod_sub_declare_ref_del(env, next, th);
8129 static int dt_dir_is_empty(const struct lu_env *env,
8130 struct dt_object *obj)
8133 const struct dt_it_ops *iops;
8138 if (!dt_try_as_dir(env, obj))
8141 iops = &obj->do_index_ops->dio_it;
8142 it = iops->init(env, obj, LUDA_64BITHASH);
8144 RETURN(PTR_ERR(it));
8146 rc = iops->get(env, it, (const struct dt_key *)"");
8150 for (rc = 0, i = 0; rc == 0 && i < 3; ++i)
8151 rc = iops->next(env, it);
8157 /* Huh? Index contains no zero key? */
8162 iops->fini(env, it);
8167 static int lod_dir_declare_layout_shrink(const struct lu_env *env,
8168 struct dt_object *dt,
8169 const struct md_layout_change *mlc,
8172 struct lod_thread_info *info = lod_env_info(env);
8173 struct lod_object *lo = lod_dt_obj(dt);
8174 struct dt_object *next = dt_object_child(dt);
8175 struct lmv_user_md *lmu = mlc->mlc_buf.lb_buf;
8176 __u32 final_stripe_count;
8177 char *stripe_name = info->lti_key;
8178 struct lu_buf *lmv_buf = &info->lti_buf;
8179 struct dt_object *dto;
8185 if (!dt_try_as_dir(env, dt))
8188 /* shouldn't be called on plain directory */
8189 LASSERT(lo->ldo_dir_stripe_count);
8191 lmv_buf->lb_buf = &info->lti_lmv.lmv_md_v1;
8192 lmv_buf->lb_len = sizeof(info->lti_lmv.lmv_md_v1);
8194 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
8195 LASSERT(final_stripe_count &&
8196 final_stripe_count < lo->ldo_dir_stripe_count);
8198 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8199 dto = lo->ldo_stripe[i];
8203 if (i < final_stripe_count) {
8204 if (final_stripe_count == 1)
8207 rc = lod_sub_declare_xattr_set(env, dto, lmv_buf,
8209 LU_XATTR_REPLACE, th);
8216 rc = dt_dir_is_empty(env, dto);
8220 rc = lod_sub_declare_ref_del(env, dto, th);
8224 rc = lod_sub_declare_destroy(env, dto, th);
8228 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8229 PFID(lu_object_fid(&dto->do_lu)), i);
8231 rc = lod_sub_declare_delete(env, next,
8232 (const struct dt_key *)stripe_name, th);
8236 rc = lod_sub_declare_ref_del(env, next, th);
8241 rc = lod_sub_declare_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV,
8242 LU_XATTR_REPLACE, th);
8247 * Allocate stripes for split directory.
8249 * \param[in] env execution environment
8250 * \param[in] dt target object
8251 * \param[in] mlc layout change data
8252 * \param[in] th transaction handle
8254 * \retval 0 on success
8255 * \retval negative if failed
8257 static int lod_dir_declare_layout_split(const struct lu_env *env,
8258 struct dt_object *dt,
8259 const struct md_layout_change *mlc,
8262 struct lod_thread_info *info = lod_env_info(env);
8263 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
8264 struct lod_object *lo = lod_dt_obj(dt);
8265 struct dt_object_format *dof = &info->lti_format;
8266 struct lmv_user_md_v1 *lum = mlc->mlc_spec->u.sp_ea.eadata;
8267 struct dt_object **stripes;
8275 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
8276 LASSERT(le32_to_cpu(lum->lum_stripe_offset) == LMV_OFFSET_DEFAULT);
8278 saved_count = lo->ldo_dir_stripes_allocated;
8279 stripe_count = le32_to_cpu(lum->lum_stripe_count);
8280 if (stripe_count <= saved_count)
8283 dof->dof_type = DFT_DIR;
8285 OBD_ALLOC(stripes, sizeof(*stripes) * stripe_count);
8289 for (i = 0; i < lo->ldo_dir_stripes_allocated; i++)
8290 stripes[i] = lo->ldo_stripe[i];
8292 lod_qos_statfs_update(env, lod, &lod->lod_mdt_descs);
8293 rc = lod_mdt_alloc_qos(env, lo, stripes, saved_count, stripe_count);
8295 rc = lod_mdt_alloc_rr(env, lo, stripes, saved_count,
8298 OBD_FREE(stripes, sizeof(*stripes) * stripe_count);
8302 LASSERT(rc > saved_count);
8303 OBD_FREE(lo->ldo_stripe,
8304 sizeof(*stripes) * lo->ldo_dir_stripes_allocated);
8305 lo->ldo_stripe = stripes;
8306 lo->ldo_dir_striped = 1;
8307 lo->ldo_dir_stripe_count = rc;
8308 lo->ldo_dir_stripes_allocated = stripe_count;
8309 lo->ldo_dir_split_hash = lo->ldo_dir_hash_type;
8310 lo->ldo_dir_hash_type = le32_to_cpu(lum->lum_hash_type);
8311 if (!lmv_is_known_hash_type(lo->ldo_dir_hash_type))
8312 lo->ldo_dir_hash_type =
8313 lod->lod_mdt_descs.ltd_lmv_desc.ld_pattern;
8314 lo->ldo_dir_hash_type |= LMV_HASH_FLAG_SPLIT | LMV_HASH_FLAG_MIGRATION;
8315 lo->ldo_dir_split_offset = saved_count;
8316 lo->ldo_dir_layout_version++;
8317 lo->ldo_dir_stripe_loaded = 1;
8319 rc = lod_dir_declare_create_stripes(env, dt, mlc->mlc_attr, dof, th);
8321 lod_striping_free(env, lo);
8327 * detach all stripes from dir master object, NB, stripes are not destroyed, but
8328 * deleted from it's parent namespace, this function is called in two places:
8329 * 1. mdd_migrate_mdt() detach stripes from source, and attach them to
8331 * 2. mdd_dir_layout_update() detach stripe before turning 1-stripe directory to
8332 * a plain directory.
8334 * \param[in] env execution environment
8335 * \param[in] dt target object
8336 * \param[in] mlc layout change data
8337 * \param[in] th transaction handle
8339 * \retval 0 on success
8340 * \retval negative if failed
8342 static int lod_dir_layout_detach(const struct lu_env *env,
8343 struct dt_object *dt,
8344 const struct md_layout_change *mlc,
8347 struct lod_thread_info *info = lod_env_info(env);
8348 struct lod_object *lo = lod_dt_obj(dt);
8349 struct dt_object *next = dt_object_child(dt);
8350 char *stripe_name = info->lti_key;
8351 struct dt_object *dto;
8357 if (!lo->ldo_dir_stripe_count) {
8358 /* plain directory delete .. */
8359 rc = lod_sub_delete(env, next,
8360 (const struct dt_key *)dotdot, th);
8364 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8365 dto = lo->ldo_stripe[i];
8369 rc = lod_sub_delete(env, dto,
8370 (const struct dt_key *)dotdot, th);
8374 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8375 PFID(lu_object_fid(&dto->do_lu)), i);
8377 rc = lod_sub_delete(env, next,
8378 (const struct dt_key *)stripe_name, th);
8382 rc = lod_sub_ref_del(env, next, th);
8387 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8388 dto = lo->ldo_stripe[i];
8390 dt_object_put(env, dto);
8392 OBD_FREE_PTR_ARRAY(lo->ldo_stripe, lo->ldo_dir_stripes_allocated);
8393 lo->ldo_stripe = NULL;
8394 lo->ldo_dir_stripes_allocated = 0;
8395 lo->ldo_dir_stripe_count = 0;
8396 dt->do_index_ops = &lod_index_ops;
8401 static int lod_dir_layout_shrink(const struct lu_env *env,
8402 struct dt_object *dt,
8403 const struct md_layout_change *mlc,
8406 struct lod_thread_info *info = lod_env_info(env);
8407 struct lod_object *lo = lod_dt_obj(dt);
8408 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
8409 struct dt_object *next = dt_object_child(dt);
8410 struct lmv_user_md *lmu = mlc->mlc_buf.lb_buf;
8411 __u32 final_stripe_count;
8412 char *stripe_name = info->lti_key;
8413 struct dt_object *dto;
8414 struct lu_buf *lmv_buf = &info->lti_buf;
8415 struct lmv_mds_md_v1 *lmv = &info->lti_lmv.lmv_md_v1;
8417 int type = LU_SEQ_RANGE_ANY;
8423 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
8425 lmv_buf->lb_buf = lmv;
8426 lmv_buf->lb_len = sizeof(*lmv);
8427 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
8428 lmv->lmv_stripe_count = cpu_to_le32(final_stripe_count);
8429 lmv->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type) &
8430 cpu_to_le32(LMV_HASH_TYPE_MASK);
8431 lmv->lmv_layout_version =
8432 cpu_to_le32(lo->ldo_dir_layout_version + 1);
8434 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8435 dto = lo->ldo_stripe[i];
8439 if (i < final_stripe_count) {
8440 /* if only one stripe left, no need to update
8441 * LMV because this stripe will replace master
8442 * object and act as a plain directory.
8444 if (final_stripe_count == 1)
8448 rc = lod_fld_lookup(env, lod,
8449 lu_object_fid(&dto->do_lu),
8454 lmv->lmv_master_mdt_index = cpu_to_le32(mdtidx);
8455 rc = lod_sub_xattr_set(env, dto, lmv_buf,
8457 LU_XATTR_REPLACE, th);
8464 dt_write_lock(env, dto, DT_TGT_CHILD);
8465 rc = lod_sub_ref_del(env, dto, th);
8466 dt_write_unlock(env, dto);
8470 rc = lod_sub_destroy(env, dto, th);
8474 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8475 PFID(lu_object_fid(&dto->do_lu)), i);
8477 rc = lod_sub_delete(env, next,
8478 (const struct dt_key *)stripe_name, th);
8482 rc = lod_sub_ref_del(env, next, th);
8487 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &mdtidx,
8492 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
8493 lmv->lmv_master_mdt_index = cpu_to_le32(mdtidx);
8494 rc = lod_sub_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV,
8495 LU_XATTR_REPLACE, th);
8499 for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
8500 dto = lo->ldo_stripe[i];
8502 dt_object_put(env, dto);
8504 lo->ldo_dir_stripe_count = final_stripe_count;
8509 static mlc_handler dir_mlc_declare_ops[MD_LAYOUT_MAX] = {
8510 [MD_LAYOUT_ATTACH] = lod_dir_declare_layout_attach,
8511 [MD_LAYOUT_DETACH] = lod_dir_declare_layout_detach,
8512 [MD_LAYOUT_SHRINK] = lod_dir_declare_layout_shrink,
8513 [MD_LAYOUT_SPLIT] = lod_dir_declare_layout_split,
8516 static mlc_handler dir_mlc_ops[MD_LAYOUT_MAX] = {
8517 [MD_LAYOUT_DETACH] = lod_dir_layout_detach,
8518 [MD_LAYOUT_SHRINK] = lod_dir_layout_shrink,
8521 static int lod_declare_layout_change(const struct lu_env *env,
8522 struct dt_object *dt, struct md_layout_change *mlc,
8525 struct lod_thread_info *info = lod_env_info(env);
8526 struct lod_object *lo = lod_dt_obj(dt);
8531 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
8532 LASSERT(dir_mlc_declare_ops[mlc->mlc_opc]);
8533 rc = dir_mlc_declare_ops[mlc->mlc_opc](env, dt, mlc, th);
8537 if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
8538 dt_object_remote(dt_object_child(dt)))
8541 rc = lod_striping_load(env, lo);
8545 LASSERT(lo->ldo_comp_cnt > 0);
8547 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
8551 switch (lo->ldo_flr_state) {
8553 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
8557 rc = lod_declare_update_rdonly(env, lo, mlc, th);
8559 case LCM_FL_WRITE_PENDING:
8560 rc = lod_declare_update_write_pending(env, lo, mlc, th);
8562 case LCM_FL_SYNC_PENDING:
8563 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
8574 * Instantiate layout component objects which covers the intent write offset.
8576 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
8577 struct md_layout_change *mlc, struct thandle *th)
8579 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
8580 struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
8581 struct lod_object *lo = lod_dt_obj(dt);
8586 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
8587 LASSERT(dir_mlc_ops[mlc->mlc_opc]);
8588 rc = dir_mlc_ops[mlc->mlc_opc](env, dt, mlc, th);
8592 rc = lod_striped_create(env, dt, attr, NULL, th);
8593 if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
8594 layout_attr->la_layout_version |= lo->ldo_layout_gen;
8595 rc = lod_attr_set(env, dt, layout_attr, th);
8601 const struct dt_object_operations lod_obj_ops = {
8602 .do_read_lock = lod_read_lock,
8603 .do_write_lock = lod_write_lock,
8604 .do_read_unlock = lod_read_unlock,
8605 .do_write_unlock = lod_write_unlock,
8606 .do_write_locked = lod_write_locked,
8607 .do_attr_get = lod_attr_get,
8608 .do_declare_attr_set = lod_declare_attr_set,
8609 .do_attr_set = lod_attr_set,
8610 .do_xattr_get = lod_xattr_get,
8611 .do_declare_xattr_set = lod_declare_xattr_set,
8612 .do_xattr_set = lod_xattr_set,
8613 .do_declare_xattr_del = lod_declare_xattr_del,
8614 .do_xattr_del = lod_xattr_del,
8615 .do_xattr_list = lod_xattr_list,
8616 .do_ah_init = lod_ah_init,
8617 .do_declare_create = lod_declare_create,
8618 .do_create = lod_create,
8619 .do_declare_destroy = lod_declare_destroy,
8620 .do_destroy = lod_destroy,
8621 .do_index_try = lod_index_try,
8622 .do_declare_ref_add = lod_declare_ref_add,
8623 .do_ref_add = lod_ref_add,
8624 .do_declare_ref_del = lod_declare_ref_del,
8625 .do_ref_del = lod_ref_del,
8626 .do_object_sync = lod_object_sync,
8627 .do_object_lock = lod_object_lock,
8628 .do_object_unlock = lod_object_unlock,
8629 .do_invalidate = lod_invalidate,
8630 .do_declare_layout_change = lod_declare_layout_change,
8631 .do_layout_change = lod_layout_change,
8635 * Implementation of dt_body_operations::dbo_read.
8637 * \see dt_body_operations::dbo_read() in the API description for details.
8639 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
8640 struct lu_buf *buf, loff_t *pos)
8642 struct dt_object *next = dt_object_child(dt);
8644 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
8645 S_ISLNK(dt->do_lu.lo_header->loh_attr));
8646 return next->do_body_ops->dbo_read(env, next, buf, pos);
8650 * Implementation of dt_body_operations::dbo_declare_write.
8652 * \see dt_body_operations::dbo_declare_write() in the API description
8655 static ssize_t lod_declare_write(const struct lu_env *env,
8656 struct dt_object *dt,
8657 const struct lu_buf *buf, loff_t pos,
8660 return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
8664 * Implementation of dt_body_operations::dbo_write.
8666 * \see dt_body_operations::dbo_write() in the API description for details.
8668 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
8669 const struct lu_buf *buf, loff_t *pos,
8672 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
8673 S_ISLNK(dt->do_lu.lo_header->loh_attr));
8674 return lod_sub_write(env, dt_object_child(dt), buf, pos, th);
8677 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
8678 __u64 start, __u64 end, struct thandle *th)
8680 if (dt_object_remote(dt))
8683 return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
8686 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
8687 __u64 start, __u64 end, struct thandle *th)
8689 if (dt_object_remote(dt))
8692 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
8693 return lod_sub_punch(env, dt_object_child(dt), start, end, th);
8697 * different type of files use the same body_ops because object may be created
8698 * in OUT, where there is no chance to set correct body_ops for each type, so
8699 * body_ops themselves will check file type inside, see lod_read/write/punch for
8702 static const struct dt_body_operations lod_body_ops = {
8703 .dbo_read = lod_read,
8704 .dbo_declare_write = lod_declare_write,
8705 .dbo_write = lod_write,
8706 .dbo_declare_punch = lod_declare_punch,
8707 .dbo_punch = lod_punch,
8711 * Implementation of lu_object_operations::loo_object_init.
8713 * The function determines the type and the index of the target device using
8714 * sequence of the object's FID. Then passes control down to the
8715 * corresponding device:
8716 * OSD for the local objects, OSP for remote
8718 * \see lu_object_operations::loo_object_init() in the API description
8721 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
8722 const struct lu_object_conf *conf)
8724 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
8725 struct lu_device *cdev = NULL;
8726 struct lu_object *cobj;
8727 struct lod_tgt_descs *ltd = NULL;
8728 struct lod_tgt_desc *tgt;
8730 int type = LU_SEQ_RANGE_ANY;
8734 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
8738 if (type == LU_SEQ_RANGE_MDT &&
8739 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
8740 cdev = &lod->lod_child->dd_lu_dev;
8741 } else if (type == LU_SEQ_RANGE_MDT) {
8742 ltd = &lod->lod_mdt_descs;
8744 } else if (type == LU_SEQ_RANGE_OST) {
8745 ltd = &lod->lod_ost_descs;
8752 if (ltd->ltd_tgts_size > idx &&
8753 test_bit(idx, ltd->ltd_tgt_bitmap)) {
8754 tgt = LTD_TGT(ltd, idx);
8756 LASSERT(tgt != NULL);
8757 LASSERT(tgt->ltd_tgt != NULL);
8759 cdev = &(tgt->ltd_tgt->dd_lu_dev);
8761 lod_putref(lod, ltd);
8764 if (unlikely(cdev == NULL))
8767 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
8768 if (unlikely(cobj == NULL))
8771 lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
8773 lu_object_add(lo, cobj);
8780 * Alloc cached foreign LOV
8782 * \param[in] lo object
8783 * \param[in] size size of foreign LOV
8785 * \retval 0 on success
8786 * \retval negative if failed
8788 int lod_alloc_foreign_lov(struct lod_object *lo, size_t size)
8790 OBD_ALLOC_LARGE(lo->ldo_foreign_lov, size);
8791 if (lo->ldo_foreign_lov == NULL)
8793 lo->ldo_foreign_lov_size = size;
8794 lo->ldo_is_foreign = 1;
8800 * Free cached foreign LOV
8802 * \param[in] lo object
8804 void lod_free_foreign_lov(struct lod_object *lo)
8806 if (lo->ldo_foreign_lov != NULL)
8807 OBD_FREE_LARGE(lo->ldo_foreign_lov, lo->ldo_foreign_lov_size);
8808 lo->ldo_foreign_lov = NULL;
8809 lo->ldo_foreign_lov_size = 0;
8810 lo->ldo_is_foreign = 0;
8815 * Free cached foreign LMV
8817 * \param[in] lo object
8819 void lod_free_foreign_lmv(struct lod_object *lo)
8821 if (lo->ldo_foreign_lmv != NULL)
8822 OBD_FREE_LARGE(lo->ldo_foreign_lmv, lo->ldo_foreign_lmv_size);
8823 lo->ldo_foreign_lmv = NULL;
8824 lo->ldo_foreign_lmv_size = 0;
8825 lo->ldo_dir_is_foreign = 0;
8830 * Release resources associated with striping.
8832 * If the object is striped (regular or directory), then release
8833 * the stripe objects references and free the ldo_stripe array.
8835 * \param[in] env execution environment
8836 * \param[in] lo object
8838 void lod_striping_free_nolock(const struct lu_env *env, struct lod_object *lo)
8840 struct lod_layout_component *lod_comp;
8843 if (unlikely(lo->ldo_is_foreign)) {
8844 lod_free_foreign_lov(lo);
8845 lo->ldo_comp_cached = 0;
8846 } else if (unlikely(lo->ldo_dir_is_foreign)) {
8847 lod_free_foreign_lmv(lo);
8848 lo->ldo_dir_stripe_loaded = 0;
8849 } else if (lo->ldo_stripe != NULL) {
8850 LASSERT(lo->ldo_comp_entries == NULL);
8851 LASSERT(lo->ldo_dir_stripes_allocated > 0);
8853 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8854 if (lo->ldo_stripe[i])
8855 dt_object_put(env, lo->ldo_stripe[i]);
8858 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
8859 OBD_FREE(lo->ldo_stripe, j);
8860 lo->ldo_stripe = NULL;
8861 lo->ldo_dir_stripes_allocated = 0;
8862 lo->ldo_dir_stripe_loaded = 0;
8863 lo->ldo_dir_stripe_count = 0;
8864 } else if (lo->ldo_comp_entries != NULL) {
8865 for (i = 0; i < lo->ldo_comp_cnt; i++) {
8866 /* free lod_layout_component::llc_stripe array */
8867 lod_comp = &lo->ldo_comp_entries[i];
8869 if (lod_comp->llc_stripe == NULL)
8871 LASSERT(lod_comp->llc_stripes_allocated != 0);
8872 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
8873 if (lod_comp->llc_stripe[j] != NULL)
8875 &lod_comp->llc_stripe[j]->do_lu);
8877 OBD_FREE_PTR_ARRAY(lod_comp->llc_stripe,
8878 lod_comp->llc_stripes_allocated);
8879 lod_comp->llc_stripe = NULL;
8880 OBD_FREE_PTR_ARRAY(lod_comp->llc_ost_indices,
8881 lod_comp->llc_stripes_allocated);
8882 lod_comp->llc_ost_indices = NULL;
8883 lod_comp->llc_stripes_allocated = 0;
8885 lod_free_comp_entries(lo);
8886 lo->ldo_comp_cached = 0;
8890 void lod_striping_free(const struct lu_env *env, struct lod_object *lo)
8892 mutex_lock(&lo->ldo_layout_mutex);
8893 lod_striping_free_nolock(env, lo);
8894 mutex_unlock(&lo->ldo_layout_mutex);
8898 * Implementation of lu_object_operations::loo_object_free.
8900 * \see lu_object_operations::loo_object_free() in the API description
8903 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
8905 struct lod_object *lo = lu2lod_obj(o);
8907 /* release all underlying object pinned */
8908 lod_striping_free(env, lo);
8910 /* lo doesn't contain a lu_object_header, so we don't need call_rcu */
8911 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
8915 * Implementation of lu_object_operations::loo_object_release.
8917 * \see lu_object_operations::loo_object_release() in the API description
8920 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
8922 /* XXX: shouldn't we release everything here in case if object
8923 * creation failed before? */
8927 * Implementation of lu_object_operations::loo_object_print.
8929 * \see lu_object_operations::loo_object_print() in the API description
8932 static int lod_object_print(const struct lu_env *env, void *cookie,
8933 lu_printer_t p, const struct lu_object *l)
8935 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
8937 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
8940 const struct lu_object_operations lod_lu_obj_ops = {
8941 .loo_object_init = lod_object_init,
8942 .loo_object_free = lod_object_free,
8943 .loo_object_release = lod_object_release,
8944 .loo_object_print = lod_object_print,