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, true)))
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);
1228 lod_obj_stripe_attr_set_cb(const struct lu_env *env, struct lod_object *lo,
1229 struct dt_object *dt, struct thandle *th,
1230 int comp_idx, int stripe_idx,
1231 struct lod_obj_stripe_cb_data *data)
1233 if (data->locd_declare)
1234 return lod_sub_declare_attr_set(env, dt, data->locd_attr, th);
1236 if (data->locd_attr->la_valid & LA_LAYOUT_VERSION) {
1237 CDEBUG(D_LAYOUT, DFID": set layout version: %u, comp_idx: %d\n",
1238 PFID(lu_object_fid(&dt->do_lu)),
1239 data->locd_attr->la_layout_version, comp_idx);
1242 return lod_sub_attr_set(env, dt, data->locd_attr, th);
1246 * Implementation of dt_object_operations::do_declare_attr_set.
1248 * If the object is striped, then apply the changes to all the stripes.
1250 * \see dt_object_operations::do_declare_attr_set() in the API description
1253 static int lod_declare_attr_set(const struct lu_env *env,
1254 struct dt_object *dt,
1255 const struct lu_attr *attr,
1258 struct dt_object *next = dt_object_child(dt);
1259 struct lod_object *lo = lod_dt_obj(dt);
1264 * declare setattr on the local object
1266 rc = lod_sub_declare_attr_set(env, next, attr, th);
1270 /* osp_declare_attr_set() ignores all attributes other than
1271 * UID, GID, PROJID, and size, and osp_attr_set() ignores all
1272 * but UID, GID and PROJID. Declaration of size attr setting
1273 * happens through lod_declare_init_size(), and not through
1274 * this function. Therefore we need not load striping unless
1275 * ownership is changing. This should save memory and (we hope)
1276 * speed up rename().
1278 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1279 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1282 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1285 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID | LA_MODE |
1286 LA_ATIME | LA_MTIME | LA_CTIME |
1291 * load striping information, notice we don't do this when object
1292 * is being initialized as we don't need this information till
1293 * few specific cases like destroy, chown
1295 rc = lod_striping_load(env, lo);
1299 if (!lod_obj_is_striped(dt))
1303 * if object is striped declare changes on the stripes
1305 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1306 LASSERT(lo->ldo_stripe);
1307 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1308 if (lo->ldo_stripe[i] == NULL)
1310 if (!dt_object_exists(lo->ldo_stripe[i]))
1312 rc = lod_sub_declare_attr_set(env, lo->ldo_stripe[i],
1318 struct lod_obj_stripe_cb_data data = { { 0 } };
1320 data.locd_attr = attr;
1321 data.locd_declare = true;
1322 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1323 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1329 if (!dt_object_exists(next) || dt_object_remote(next) ||
1330 !S_ISREG(attr->la_mode))
1333 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1334 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
1338 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1339 CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1340 struct lod_thread_info *info = lod_env_info(env);
1341 struct lu_buf *buf = &info->lti_buf;
1343 buf->lb_buf = info->lti_ea_store;
1344 buf->lb_len = info->lti_ea_store_size;
1345 rc = lod_sub_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1346 LU_XATTR_REPLACE, th);
1353 * Implementation of dt_object_operations::do_attr_set.
1355 * If the object is striped, then apply the changes to all or subset of
1356 * the stripes depending on the object type and specific attributes.
1358 * \see dt_object_operations::do_attr_set() in the API description for details.
1360 static int lod_attr_set(const struct lu_env *env,
1361 struct dt_object *dt,
1362 const struct lu_attr *attr,
1365 struct dt_object *next = dt_object_child(dt);
1366 struct lod_object *lo = lod_dt_obj(dt);
1371 * apply changes to the local object
1373 rc = lod_sub_attr_set(env, next, attr, th);
1377 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1378 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1381 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1384 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE | LA_PROJID |
1385 LA_ATIME | LA_MTIME | LA_CTIME |
1390 /* FIXME: a tricky case in the code path of mdd_layout_change():
1391 * the in-memory striping information has been freed in lod_xattr_set()
1392 * due to layout change. It has to load stripe here again. It only
1393 * changes flags of layout so declare_attr_set() is still accurate */
1394 rc = lod_striping_load(env, lo);
1398 if (!lod_obj_is_striped(dt))
1402 * if object is striped, apply changes to all the stripes
1404 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1405 LASSERT(lo->ldo_stripe);
1406 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1407 if (unlikely(lo->ldo_stripe[i] == NULL))
1410 if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1413 rc = lod_sub_attr_set(env, lo->ldo_stripe[i], attr, th);
1418 struct lod_obj_stripe_cb_data data = { { 0 } };
1420 data.locd_attr = attr;
1421 data.locd_declare = false;
1422 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1423 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1429 if (!dt_object_exists(next) || dt_object_remote(next) ||
1430 !S_ISREG(attr->la_mode))
1433 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1434 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
1438 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1439 struct lod_thread_info *info = lod_env_info(env);
1440 struct lu_buf *buf = &info->lti_buf;
1441 struct ost_id *oi = &info->lti_ostid;
1442 struct lu_fid *fid = &info->lti_fid;
1443 struct lov_mds_md_v1 *lmm;
1444 struct lov_ost_data_v1 *objs;
1447 rc = lod_get_lov_ea(env, lo);
1451 buf->lb_buf = info->lti_ea_store;
1452 buf->lb_len = info->lti_ea_store_size;
1453 lmm = info->lti_ea_store;
1454 magic = le32_to_cpu(lmm->lmm_magic);
1455 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
1456 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1457 struct lov_comp_md_entry_v1 *lcme =
1458 &lcm->lcm_entries[0];
1460 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1461 magic = le32_to_cpu(lmm->lmm_magic);
1464 if (magic == LOV_MAGIC_V1)
1465 objs = &(lmm->lmm_objects[0]);
1467 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1468 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1469 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1471 fid_to_ostid(fid, oi);
1472 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1474 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1475 LU_XATTR_REPLACE, th);
1476 } else if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1477 struct lod_thread_info *info = lod_env_info(env);
1478 struct lu_buf *buf = &info->lti_buf;
1479 struct lov_comp_md_v1 *lcm;
1480 struct lov_comp_md_entry_v1 *lcme;
1482 rc = lod_get_lov_ea(env, lo);
1486 buf->lb_buf = info->lti_ea_store;
1487 buf->lb_len = info->lti_ea_store_size;
1489 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1 &&
1490 le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_SEL)
1493 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1494 lcme = &lcm->lcm_entries[0];
1495 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1496 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1498 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1499 LU_XATTR_REPLACE, th);
1506 * Implementation of dt_object_operations::do_xattr_get.
1508 * If LOV EA is requested from the root object and it's not
1509 * found, then return default striping for the filesystem.
1511 * \see dt_object_operations::do_xattr_get() in the API description for details.
1513 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1514 struct lu_buf *buf, const char *name)
1516 struct lod_thread_info *info = lod_env_info(env);
1517 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1522 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1523 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1524 struct lmv_mds_md_v1 *lmv1;
1525 struct lmv_foreign_md *lfm;
1528 if (rc > (typeof(rc))sizeof(*lmv1))
1531 /* short (<= sizeof(struct lmv_mds_md_v1)) foreign LMV case */
1532 /* XXX empty foreign LMV is not allowed */
1533 if (rc <= offsetof(typeof(*lfm), lfm_value))
1534 RETURN(rc = rc > 0 ? -EINVAL : rc);
1536 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1537 BUILD_BUG_ON(sizeof(*lmv1) > sizeof(info->lti_key));
1539 /* lti_buf is large enough for *lmv1 or a short
1540 * (<= sizeof(struct lmv_mds_md_v1)) foreign LMV
1542 info->lti_buf.lb_buf = info->lti_key;
1543 info->lti_buf.lb_len = sizeof(*lmv1);
1544 rc = dt_xattr_get(env, dt_object_child(dt),
1545 &info->lti_buf, name);
1546 if (unlikely(rc <= offsetof(typeof(*lfm),
1548 RETURN(rc = rc > 0 ? -EINVAL : rc);
1550 lfm = info->lti_buf.lb_buf;
1551 if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN)
1554 if (unlikely(rc != sizeof(*lmv1)))
1555 RETURN(rc = rc > 0 ? -EINVAL : rc);
1557 lmv1 = info->lti_buf.lb_buf;
1558 /* The on-disk LMV EA only contains header, but the
1559 * returned LMV EA size should contain the space for
1560 * the FIDs of all shards of the striped directory. */
1561 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1562 rc = lmv_mds_md_size(
1563 le32_to_cpu(lmv1->lmv_stripe_count),
1564 le32_to_cpu(lmv1->lmv_magic));
1567 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1570 if (rc != sizeof(*lmv1))
1571 RETURN(rc = rc > 0 ? -EINVAL : rc);
1573 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1577 RETURN(rc = rc1 != 0 ? rc1 : rc);
1580 if ((rc > 0) && buf->lb_buf && strcmp(name, XATTR_NAME_LOV) == 0) {
1581 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1583 if (lcm->lcm_magic == cpu_to_le32(LOV_MAGIC_SEL))
1584 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1587 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1591 * XXX: Only used by lfsck
1593 * lod returns default striping on the real root of the device
1594 * this is like the root stores default striping for the whole
1595 * filesystem. historically we've been using a different approach
1596 * and store it in the config.
1598 dt_root_get(env, dev->lod_child, &info->lti_fid);
1599 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1601 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1602 struct lov_user_md *lum = buf->lb_buf;
1603 struct lov_desc *desc = &dev->lod_ost_descs.ltd_lov_desc;
1605 if (buf->lb_buf == NULL) {
1607 } else if (buf->lb_len >= sizeof(*lum)) {
1608 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1609 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1610 lmm_oi_set_id(&lum->lmm_oi, 0);
1611 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1612 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1613 lum->lmm_stripe_size = cpu_to_le32(
1614 desc->ld_default_stripe_size);
1615 lum->lmm_stripe_count = cpu_to_le16(
1616 desc->ld_default_stripe_count);
1617 lum->lmm_stripe_offset = cpu_to_le16(
1618 desc->ld_default_stripe_offset);
1631 * Checks that the magic of the stripe is sane.
1633 * \param[in] lod lod device
1634 * \param[in] lum a buffer storing LMV EA to verify
1636 * \retval 0 if the EA is sane
1637 * \retval negative otherwise
1639 static int lod_verify_md_striping(struct lod_device *lod,
1640 const struct lmv_user_md_v1 *lum)
1642 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1643 CERROR("%s: invalid lmv_user_md: magic = %x, "
1644 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1645 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1646 (int)le32_to_cpu(lum->lum_stripe_offset),
1647 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1655 * Initialize LMV EA for a slave.
1657 * Initialize slave's LMV EA from the master's LMV EA.
1659 * \param[in] master_lmv a buffer containing master's EA
1660 * \param[out] slave_lmv a buffer where slave's EA will be stored
1663 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1664 const struct lmv_mds_md_v1 *master_lmv)
1666 *slave_lmv = *master_lmv;
1667 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1673 * Generate LMV EA from the object passed as \a dt. The object must have
1674 * the stripes created and initialized.
1676 * \param[in] env execution environment
1677 * \param[in] dt object
1678 * \param[out] lmv_buf buffer storing generated LMV EA
1680 * \retval 0 on success
1681 * \retval negative if failed
1683 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1684 struct lu_buf *lmv_buf)
1686 struct lod_thread_info *info = lod_env_info(env);
1687 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1688 struct lod_object *lo = lod_dt_obj(dt);
1689 struct lmv_mds_md_v1 *lmm1;
1691 int type = LU_SEQ_RANGE_ANY;
1696 LASSERT(lo->ldo_dir_striped != 0);
1697 LASSERT(lo->ldo_dir_stripe_count > 0);
1698 stripe_count = lo->ldo_dir_stripe_count;
1699 /* Only store the LMV EA heahder on the disk. */
1700 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1701 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1705 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1708 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1709 memset(lmm1, 0, sizeof(*lmm1));
1710 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1711 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1712 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1713 lmm1->lmv_layout_version = cpu_to_le32(lo->ldo_dir_layout_version);
1714 if (lod_is_layout_changing(lo)) {
1715 lmm1->lmv_migrate_hash = cpu_to_le32(lo->ldo_dir_migrate_hash);
1716 lmm1->lmv_migrate_offset =
1717 cpu_to_le32(lo->ldo_dir_migrate_offset);
1719 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1724 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1725 lmv_buf->lb_buf = info->lti_ea_store;
1726 lmv_buf->lb_len = sizeof(*lmm1);
1732 * Create in-core represenation for a striped directory.
1734 * Parse the buffer containing LMV EA and instantiate LU objects
1735 * representing the stripe objects. The pointers to the objects are
1736 * stored in ldo_stripe field of \a lo. This function is used when
1737 * we need to access an already created object (i.e. load from a disk).
1739 * \param[in] env execution environment
1740 * \param[in] lo lod object
1741 * \param[in] buf buffer containing LMV EA
1743 * \retval 0 on success
1744 * \retval negative if failed
1746 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1747 const struct lu_buf *buf)
1749 struct lod_thread_info *info = lod_env_info(env);
1750 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1751 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1752 struct dt_object **stripe;
1753 union lmv_mds_md *lmm = buf->lb_buf;
1754 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1755 struct lu_fid *fid = &info->lti_fid;
1760 LASSERT(mutex_is_locked(&lo->ldo_layout_mutex));
1762 /* XXX may be useless as not called for foreign LMV ?? */
1763 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_FOREIGN)
1766 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1767 lo->ldo_dir_slave_stripe = 1;
1771 if (!lmv_is_sane(lmv1))
1774 LASSERT(lo->ldo_stripe == NULL);
1775 OBD_ALLOC_PTR_ARRAY(stripe, le32_to_cpu(lmv1->lmv_stripe_count));
1779 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1780 struct dt_device *tgt_dt;
1781 struct dt_object *dto;
1782 int type = LU_SEQ_RANGE_ANY;
1785 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1786 if (!fid_is_sane(fid)) {
1791 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1795 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1796 tgt_dt = lod->lod_child;
1798 struct lod_tgt_desc *tgt;
1800 tgt = LTD_TGT(ltd, idx);
1802 GOTO(out, rc = -ESTALE);
1803 tgt_dt = tgt->ltd_tgt;
1806 dto = dt_locate_at(env, tgt_dt, fid,
1807 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1810 GOTO(out, rc = PTR_ERR(dto));
1815 lo->ldo_stripe = stripe;
1816 lo->ldo_is_foreign = 0;
1817 lo->ldo_dir_stripe_count = le32_to_cpu(lmv1->lmv_stripe_count);
1818 lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1819 lo->ldo_dir_layout_version = le32_to_cpu(lmv1->lmv_layout_version);
1820 lo->ldo_dir_migrate_offset = le32_to_cpu(lmv1->lmv_migrate_offset);
1821 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv1->lmv_migrate_hash);
1822 lo->ldo_dir_hash_type = le32_to_cpu(lmv1->lmv_hash_type);
1824 lod_striping_free_nolock(env, lo);
1830 * Declare create a striped directory.
1832 * Declare creating a striped directory with a given stripe pattern on the
1833 * specified MDTs. A striped directory is represented as a regular directory
1834 * - an index listing all the stripes. The stripes point back to the master
1835 * object with ".." and LinkEA. The master object gets LMV EA which
1836 * identifies it as a striped directory. The function allocates FIDs
1839 * \param[in] env execution environment
1840 * \param[in] dt object
1841 * \param[in] attr attributes to initialize the objects with
1842 * \param[in] dof type of objects to be created
1843 * \param[in] th transaction handle
1845 * \retval 0 on success
1846 * \retval negative if failed
1848 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1849 struct dt_object *dt,
1850 struct lu_attr *attr,
1851 struct dt_object_format *dof,
1854 struct lod_thread_info *info = lod_env_info(env);
1855 struct lu_buf lmv_buf;
1856 struct lu_buf slave_lmv_buf;
1857 struct lmv_mds_md_v1 *lmm;
1858 struct lmv_mds_md_v1 *slave_lmm = NULL;
1859 struct dt_insert_rec *rec = &info->lti_dt_rec;
1860 struct lod_object *lo = lod_dt_obj(dt);
1865 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1868 lmm = lmv_buf.lb_buf;
1870 OBD_ALLOC_PTR(slave_lmm);
1871 if (slave_lmm == NULL)
1872 GOTO(out, rc = -ENOMEM);
1874 lod_prep_slave_lmv_md(slave_lmm, lmm);
1875 slave_lmv_buf.lb_buf = slave_lmm;
1876 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1878 if (!dt_try_as_dir(env, dt_object_child(dt), false))
1879 GOTO(out, rc = -EINVAL);
1881 rec->rec_type = S_IFDIR;
1882 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1883 struct dt_object *dto = lo->ldo_stripe[i];
1884 char *stripe_name = info->lti_key;
1885 struct lu_name *sname;
1886 struct linkea_data ldata = { NULL };
1887 struct lu_buf linkea_buf;
1889 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
1893 /* directory split skip create for existing stripes */
1894 if (!(lod_is_splitting(lo) && i < lo->ldo_dir_split_offset)) {
1895 rc = lod_sub_declare_create(env, dto, attr, NULL, dof,
1900 if (!dt_try_as_dir(env, dto, false))
1901 GOTO(out, rc = -EINVAL);
1903 rc = lod_sub_declare_ref_add(env, dto, th);
1907 rec->rec_fid = lu_object_fid(&dto->do_lu);
1908 rc = lod_sub_declare_insert(env, dto,
1909 (const struct dt_rec *)rec,
1910 (const struct dt_key *)dot,
1915 /* master stripe FID will be put to .. */
1916 rec->rec_fid = lu_object_fid(&dt->do_lu);
1917 rc = lod_sub_declare_insert(env, dto,
1918 (const struct dt_rec *)rec,
1919 (const struct dt_key *)dotdot,
1924 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1926 snprintf(stripe_name, sizeof(info->lti_key),
1928 PFID(lu_object_fid(&dto->do_lu)),
1931 snprintf(stripe_name, sizeof(info->lti_key),
1933 PFID(lu_object_fid(&dto->do_lu)), i);
1935 sname = lod_name_get(env, stripe_name,
1936 strlen(stripe_name));
1937 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1938 sname, lu_object_fid(&dt->do_lu));
1942 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1943 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1944 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
1945 XATTR_NAME_LINK, 0, th);
1949 rec->rec_fid = lu_object_fid(&dto->do_lu);
1950 rc = lod_sub_declare_insert(env, dt_object_child(dt),
1951 (const struct dt_rec *)rec,
1952 (const struct dt_key *)stripe_name, th);
1956 rc = lod_sub_declare_ref_add(env, dt_object_child(dt),
1962 if (!CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1963 cfs_fail_val != i) {
1964 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1966 slave_lmm->lmv_master_mdt_index =
1969 slave_lmm->lmv_master_mdt_index =
1971 rc = lod_sub_declare_xattr_set(env, dto, &slave_lmv_buf,
1972 XATTR_NAME_LMV, 0, th);
1978 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt),
1979 &lmv_buf, XATTR_NAME_LMV, 0, th);
1983 if (slave_lmm != NULL)
1984 OBD_FREE_PTR(slave_lmm);
1990 * Allocate a striping on a predefined set of MDTs.
1992 * Allocates new striping using the MDT index range provided by the data from
1993 * the lum_obejcts contained in the lmv_user_md passed to this method if
1994 * \a is_specific is true; or allocates new layout starting from MDT index in
1995 * lo->ldo_dir_stripe_offset. The exact order of MDTs is not important and
1996 * varies depending on MDT status. The number of stripes needed and stripe
1997 * offset are taken from the object. If that number cannot be met, then the
1998 * function returns an error and then it's the caller's responsibility to
1999 * release the stripes allocated. All the internal structures are protected,
2000 * but no concurrent allocation is allowed on the same objects.
2002 * \param[in] env execution environment for this thread
2003 * \param[in] lo LOD object
2004 * \param[out] stripes striping created
2005 * \param[out] mdt_indices MDT indices of striping created
2006 * \param[in] is_specific true if the MDTs are provided by lum; false if
2007 * only the starting MDT index is provided
2009 * \retval positive stripes allocated, including the first stripe allocated
2011 * \retval negative errno on failure
2013 static int lod_mdt_alloc_specific(const struct lu_env *env,
2014 struct lod_object *lo,
2015 struct dt_object **stripes,
2016 __u32 *mdt_indices, bool is_specific)
2018 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
2019 struct lu_tgt_descs *ltd = &lod->lod_mdt_descs;
2020 struct lu_tgt_desc *tgt = NULL;
2021 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2022 struct dt_device *tgt_dt = NULL;
2023 struct lu_fid fid = { 0 };
2024 struct dt_object *dto;
2026 u32 stripe_count = lo->ldo_dir_stripe_count;
2032 master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
2033 if (!is_specific && stripe_count > 1)
2034 /* Set the start index for the 2nd stripe allocation */
2035 mdt_indices[1] = (mdt_indices[0] + 1) %
2036 (lod->lod_remote_mdt_count + 1);
2038 for (; stripe_idx < stripe_count; stripe_idx++) {
2039 /* Try to find next avaible target */
2040 idx = mdt_indices[stripe_idx];
2041 for (j = 0; j < lod->lod_remote_mdt_count;
2042 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
2043 bool already_allocated = false;
2046 CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
2047 idx, lod->lod_remote_mdt_count + 1, stripe_idx);
2049 if (likely(!is_specific &&
2050 !CFS_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
2051 /* check whether the idx already exists
2052 * in current allocated array */
2053 for (k = 0; k < stripe_idx; k++) {
2054 if (mdt_indices[k] == idx) {
2055 already_allocated = true;
2060 if (already_allocated)
2064 /* Sigh, this index is not in the bitmap, let's check
2065 * next available target */
2066 if (!test_bit(idx, ltd->ltd_tgt_bitmap) &&
2067 idx != master_index)
2070 if (idx == master_index) {
2071 /* Allocate the FID locally */
2072 tgt_dt = lod->lod_child;
2073 rc = dt_fid_alloc(env, tgt_dt, &fid, NULL,
2080 /* check the status of the OSP */
2081 tgt = LTD_TGT(ltd, idx);
2085 tgt_dt = tgt->ltd_tgt;
2086 if (!tgt->ltd_active)
2087 /* this OSP doesn't feel well */
2090 rc = dt_fid_alloc(env, tgt_dt, &fid, NULL, NULL);
2097 /* Can not allocate more stripes */
2098 if (j == lod->lod_remote_mdt_count) {
2099 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
2100 lod2obd(lod)->obd_name, stripe_count,
2105 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
2106 idx, stripe_idx, PFID(&fid));
2107 mdt_indices[stripe_idx] = idx;
2108 /* Set the start index for next stripe allocation */
2109 if (!is_specific && stripe_idx < stripe_count - 1) {
2111 * for large dir test, put all other slaves on one
2112 * remote MDT, otherwise we may save too many local
2113 * slave locks which will exceed RS_MAX_LOCKS.
2115 if (unlikely(CFS_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)))
2117 mdt_indices[stripe_idx + 1] = (idx + 1) %
2118 (lod->lod_remote_mdt_count + 1);
2120 /* tgt_dt and fid must be ready after search avaible OSP
2121 * in the above loop */
2122 LASSERT(tgt_dt != NULL);
2123 LASSERT(fid_is_sane(&fid));
2125 /* fail a remote stripe FID allocation */
2126 if (stripe_idx && CFS_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_FID))
2129 dto = dt_locate_at(env, tgt_dt, &fid,
2130 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
2137 stripes[stripe_idx] = dto;
2143 for (j = 1; j < stripe_idx; j++) {
2144 LASSERT(stripes[j] != NULL);
2145 dt_object_put(env, stripes[j]);
2151 static int lod_prep_md_striped_create(const struct lu_env *env,
2152 struct dt_object *dt,
2153 struct lu_attr *attr,
2154 const struct lmv_user_md_v1 *lum,
2155 struct dt_object_format *dof,
2158 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
2159 struct lod_object *lo = lod_dt_obj(dt);
2160 struct dt_object **stripes;
2161 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2162 struct lu_fid fid = { 0 };
2169 /* The lum has been verifed in lod_verify_md_striping */
2170 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC ||
2171 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC);
2173 stripe_count = lo->ldo_dir_stripe_count;
2175 OBD_ALLOC_PTR_ARRAY(stripes, stripe_count);
2179 /* Allocate the first stripe locally */
2180 rc = dt_fid_alloc(env, lod->lod_child, &fid, NULL, NULL);
2184 stripes[0] = dt_locate_at(env, lod->lod_child, &fid,
2185 dt->do_lu.lo_dev->ld_site->ls_top_dev, &conf);
2186 if (IS_ERR(stripes[0]))
2187 GOTO(out, rc = PTR_ERR(stripes[0]));
2189 if (lo->ldo_dir_stripe_offset == LMV_OFFSET_DEFAULT) {
2190 lod_qos_statfs_update(env, lod, &lod->lod_mdt_descs);
2191 rc = lod_mdt_alloc_qos(env, lo, stripes, 1, stripe_count);
2193 rc = lod_mdt_alloc_rr(env, lo, stripes, 1,
2197 bool is_specific = false;
2199 OBD_ALLOC_PTR_ARRAY(idx_array, stripe_count);
2201 GOTO(out, rc = -ENOMEM);
2203 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) {
2205 for (i = 0; i < stripe_count; i++)
2207 le32_to_cpu(lum->lum_objects[i].lum_mds);
2210 /* stripe 0 is local */
2212 lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
2213 rc = lod_mdt_alloc_specific(env, lo, stripes, idx_array,
2215 OBD_FREE_PTR_ARRAY(idx_array, stripe_count);
2223 lo->ldo_dir_striped = 1;
2224 lo->ldo_stripe = stripes;
2225 lo->ldo_dir_stripe_count = rc;
2226 lo->ldo_dir_stripes_allocated = stripe_count;
2228 lo->ldo_dir_stripe_loaded = 1;
2230 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
2232 lod_striping_free(env, lo);
2238 if (!IS_ERR_OR_NULL(stripes[0]))
2239 dt_object_put(env, stripes[0]);
2240 for (i = 1; i < stripe_count; i++)
2241 LASSERT(!stripes[i]);
2242 OBD_FREE_PTR_ARRAY(stripes, stripe_count);
2249 * Alloc cached foreign LOV
2251 * \param[in] lo object
2252 * \param[in] size size of foreign LOV
2254 * \retval 0 on success
2255 * \retval negative if failed
2257 int lod_alloc_foreign_lov(struct lod_object *lo, size_t size)
2259 OBD_ALLOC_LARGE(lo->ldo_foreign_lov, size);
2260 if (lo->ldo_foreign_lov == NULL)
2262 lo->ldo_foreign_lov_size = size;
2263 lo->ldo_is_foreign = 1;
2269 * Free cached foreign LOV
2271 * \param[in] lo object
2273 void lod_free_foreign_lov(struct lod_object *lo)
2275 if (lo->ldo_foreign_lov != NULL)
2276 OBD_FREE_LARGE(lo->ldo_foreign_lov, lo->ldo_foreign_lov_size);
2277 lo->ldo_foreign_lov = NULL;
2278 lo->ldo_foreign_lov_size = 0;
2279 lo->ldo_is_foreign = 0;
2284 * Alloc cached foreign LMV
2286 * \param[in] lo object
2287 * \param[in] size size of foreign LMV
2289 * \retval 0 on success
2290 * \retval negative if failed
2292 int lod_alloc_foreign_lmv(struct lod_object *lo, size_t size)
2294 OBD_ALLOC_LARGE(lo->ldo_foreign_lmv, size);
2295 if (lo->ldo_foreign_lmv == NULL)
2297 lo->ldo_foreign_lmv_size = size;
2298 lo->ldo_is_foreign = 1;
2305 * Free cached foreign LMV
2307 * \param[in] lo object
2309 void lod_free_foreign_lmv(struct lod_object *lo)
2311 if (lo->ldo_foreign_lmv != NULL)
2312 OBD_FREE_LARGE(lo->ldo_foreign_lmv, lo->ldo_foreign_lmv_size);
2313 lo->ldo_foreign_lmv = NULL;
2314 lo->ldo_foreign_lmv_size = 0;
2315 lo->ldo_is_foreign = 0;
2319 * Declare create striped md object.
2321 * The function declares intention to create a striped directory. This is a
2322 * wrapper for lod_prep_md_striped_create(). The only additional functionality
2323 * is to verify pattern \a lum_buf is good. Check that function for the details.
2325 * \param[in] env execution environment
2326 * \param[in] dt object
2327 * \param[in] attr attributes to initialize the objects with
2328 * \param[in] lum_buf a pattern specifying the number of stripes and
2330 * \param[in] dof type of objects to be created
2331 * \param[in] th transaction handle
2333 * \retval 0 on success
2334 * \retval negative if failed
2337 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
2338 struct dt_object *dt,
2339 struct lu_attr *attr,
2340 const struct lu_buf *lum_buf,
2341 struct dt_object_format *dof,
2344 struct lod_object *lo = lod_dt_obj(dt);
2345 struct lmv_user_md_v1 *lum = lum_buf->lb_buf;
2349 LASSERT(lum != NULL);
2352 "lum magic=%x hash=%x count=%u offset=%d inherit=%u rr=%u\n",
2353 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_hash_type),
2354 le32_to_cpu(lum->lum_stripe_count),
2355 (int)le32_to_cpu(lum->lum_stripe_offset),
2356 lum->lum_max_inherit, lum->lum_max_inherit_rr);
2358 if (lo->ldo_dir_stripe_count == 0) {
2359 if (lo->ldo_is_foreign) {
2360 rc = lod_alloc_foreign_lmv(lo, lum_buf->lb_len);
2363 memcpy(lo->ldo_foreign_lmv, lum, lum_buf->lb_len);
2364 lo->ldo_dir_stripe_loaded = 1;
2369 /* prepare dir striped objects */
2370 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2372 /* failed to create striping, let's reset
2373 * config so that others don't get confused */
2374 lod_striping_free(env, lo);
2382 * Set or replace striped directory layout, and LFSCK may set layout on a plain
2383 * directory, so don't check stripe count.
2385 * \param[in] env execution environment
2386 * \param[in] dt target object
2387 * \param[in] lmv_buf LMV buf which contains source stripe FIDs
2388 * \param[in] fl set or replace
2389 * \param[in] th transaction handle
2391 * \retval 0 on success
2392 * \retval negative if failed
2394 static int lod_dir_layout_set(const struct lu_env *env,
2395 struct dt_object *dt,
2396 const struct lu_buf *lmv_buf,
2400 struct dt_object *next = dt_object_child(dt);
2401 struct lod_object *lo = lod_dt_obj(dt);
2402 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2403 struct lmv_mds_md_v1 *lmv = lmv_buf->lb_buf;
2404 struct lmv_mds_md_v1 *slave_lmv;
2405 struct lu_buf slave_buf;
2411 if (!lmv_is_sane2(lmv))
2414 /* adjust hash for dir merge, which may not be set in user command */
2415 if (lmv_is_merging(lmv) &&
2416 !(lmv->lmv_migrate_hash & LMV_HASH_TYPE_MASK))
2417 lmv->lmv_merge_hash |=
2418 lod->lod_mdt_descs.ltd_lmv_desc.ld_pattern &
2421 LMV_DEBUG(D_INFO, lmv, "set");
2423 rc = lod_sub_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV, fl, th);
2427 /* directory restripe may update stripe LMV directly */
2428 if (!lo->ldo_dir_stripe_count)
2431 lo->ldo_dir_hash_type = le32_to_cpu(lmv->lmv_hash_type);
2432 lo->ldo_dir_migrate_offset = le32_to_cpu(lmv->lmv_migrate_offset);
2433 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_migrate_hash);
2434 lo->ldo_dir_layout_version = le32_to_cpu(lmv->lmv_layout_version);
2436 OBD_ALLOC_PTR(slave_lmv);
2440 lod_prep_slave_lmv_md(slave_lmv, lmv);
2441 slave_buf.lb_buf = slave_lmv;
2442 slave_buf.lb_len = sizeof(*slave_lmv);
2444 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2445 if (!lo->ldo_stripe[i])
2448 if (!dt_object_exists(lo->ldo_stripe[i]))
2451 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], &slave_buf,
2452 XATTR_NAME_LMV, fl, th);
2457 OBD_FREE_PTR(slave_lmv);
2463 * Implementation of dt_object_operations::do_declare_xattr_set.
2465 * Used with regular (non-striped) objects. Basically it
2466 * initializes the striping information and applies the
2467 * change to all the stripes.
2469 * \see dt_object_operations::do_declare_xattr_set() in the API description
2472 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2473 struct dt_object *dt,
2474 const struct lu_buf *buf,
2475 const char *name, int fl,
2478 struct dt_object *next = dt_object_child(dt);
2479 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2480 struct lod_object *lo = lod_dt_obj(dt);
2485 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2486 struct lmv_user_md_v1 *lum;
2488 LASSERT(buf != NULL && buf->lb_buf != NULL);
2490 rc = lod_verify_md_striping(d, lum);
2493 } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2494 rc = lod_verify_striping(env, d, lo, buf, false);
2499 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2503 /* Note: Do not set LinkEA on sub-stripes, otherwise
2504 * it will confuse the fid2path process(see mdt_path_current()).
2505 * The linkEA between master and sub-stripes is set in
2506 * lod_xattr_set_lmv(). */
2507 if (strcmp(name, XATTR_NAME_LINK) == 0)
2510 /* set xattr to each stripes, if needed */
2511 rc = lod_striping_load(env, lo);
2515 if (lo->ldo_dir_stripe_count == 0)
2518 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2519 if (!lo->ldo_stripe[i])
2522 if (!dt_object_exists(lo->ldo_stripe[i]))
2525 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2535 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2536 struct lod_object *lo,
2537 struct dt_object *dt, struct thandle *th,
2538 int comp_idx, int stripe_idx,
2539 struct lod_obj_stripe_cb_data *data)
2541 struct lod_thread_info *info = lod_env_info(env);
2542 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
2543 struct filter_fid *ff = &info->lti_ff;
2544 struct lu_buf *buf = &info->lti_buf;
2548 buf->lb_len = sizeof(*ff);
2549 rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2557 * locd_buf is set if it's called by dir migration, which doesn't check
2560 if (data->locd_buf) {
2561 memset(ff, 0, sizeof(*ff));
2562 ff->ff_parent = *(struct lu_fid *)data->locd_buf->lb_buf;
2564 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
2566 if (lu_fid_eq(lod_object_fid(lo), &ff->ff_parent) &&
2567 ff->ff_layout.ol_comp_id == comp->llc_id)
2570 memset(ff, 0, sizeof(*ff));
2571 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2574 /* rewrite filter_fid */
2575 ff->ff_parent.f_ver = stripe_idx;
2576 ff->ff_layout.ol_stripe_size = comp->llc_stripe_size;
2577 ff->ff_layout.ol_stripe_count = comp->llc_stripe_count;
2578 ff->ff_layout.ol_comp_id = comp->llc_id;
2579 ff->ff_layout.ol_comp_start = comp->llc_extent.e_start;
2580 ff->ff_layout.ol_comp_end = comp->llc_extent.e_end;
2581 filter_fid_cpu_to_le(ff, ff, sizeof(*ff));
2583 if (data->locd_declare)
2584 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2585 LU_XATTR_REPLACE, th);
2587 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2588 LU_XATTR_REPLACE, th);
2594 * Reset parent FID on OST object
2596 * Replace parent FID with @dt object FID, which is only called during migration
2597 * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2598 * the FID is changed.
2600 * \param[in] env execution environment
2601 * \param[in] dt dt_object whose stripes's parent FID will be reset
2602 * \parem[in] th thandle
2603 * \param[in] declare if it is declare
2605 * \retval 0 if reset succeeds
2606 * \retval negative errno if reset fails
2608 static int lod_replace_parent_fid(const struct lu_env *env,
2609 struct dt_object *dt,
2610 const struct lu_buf *buf,
2611 struct thandle *th, bool declare)
2613 struct lod_object *lo = lod_dt_obj(dt);
2614 struct lod_thread_info *info = lod_env_info(env);
2615 struct filter_fid *ff;
2616 struct lod_obj_stripe_cb_data data = { { 0 } };
2620 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2622 /* set xattr to each stripes, if needed */
2623 rc = lod_striping_load(env, lo);
2627 if (!lod_obj_is_striped(dt))
2630 if (info->lti_ea_store_size < sizeof(*ff)) {
2631 rc = lod_ea_store_resize(info, sizeof(*ff));
2636 data.locd_declare = declare;
2637 data.locd_stripe_cb = lod_obj_stripe_replace_parent_fid_cb;
2638 data.locd_buf = buf;
2639 rc = lod_obj_for_each_stripe(env, lo, th, &data);
2644 __u16 lod_comp_entry_stripe_count(struct lod_object *lo, int comp_idx,
2647 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2648 struct lod_layout_component *entry;
2649 enum lod_uses_hint flags = LOD_USES_ASSIGNED_STRIPE;
2654 entry = &lo->ldo_comp_entries[comp_idx];
2655 if (lod_comp_inited(entry))
2656 return entry->llc_stripe_count;
2657 if (entry->llc_stripe_count == LOV_ALL_STRIPES)
2658 return lod_get_stripe_count_plain(lod, lo,
2659 entry->llc_stripe_count,
2660 entry->llc_pattern &
2661 LOV_PATTERN_OVERSTRIPING,
2664 return lod_get_stripe_count(lod, lo, comp_idx, entry->llc_stripe_count,
2665 entry->llc_pattern & LOV_PATTERN_OVERSTRIPING,
2669 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2671 int magic, size = 0, i;
2672 struct lod_layout_component *comp_entries;
2674 bool is_composite, is_foreign = false;
2677 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2678 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2680 lo->ldo_def_striping->lds_def_striping_is_composite;
2682 comp_cnt = lo->ldo_comp_cnt;
2683 comp_entries = lo->ldo_comp_entries;
2684 is_composite = lo->ldo_is_composite;
2685 is_foreign = lo->ldo_is_foreign;
2689 return lo->ldo_foreign_lov_size;
2691 LASSERT(comp_cnt != 0 && comp_entries != NULL);
2693 size = sizeof(struct lov_comp_md_v1) +
2694 sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2695 LASSERT(size % sizeof(__u64) == 0);
2698 for (i = 0; i < comp_cnt; i++) {
2701 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2702 stripe_count = lod_comp_entry_stripe_count(lo, i, is_dir);
2703 if (!is_dir && is_composite)
2704 lod_comp_shrink_stripe_count(&comp_entries[i],
2707 size += lov_user_md_size(stripe_count, magic);
2708 LASSERT(size % sizeof(__u64) == 0);
2714 * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2715 * the xattr value is binary lov_comp_md_v1 which contains component(s)
2718 * \param[in] env execution environment
2719 * \param[in] dt dt_object to add components on
2720 * \param[in] buf buffer contains components to be added
2721 * \parem[in] th thandle
2723 * \retval 0 on success
2724 * \retval negative errno on failure
2726 static int lod_declare_layout_add(const struct lu_env *env,
2727 struct dt_object *dt,
2728 const struct lu_buf *buf,
2731 struct lod_thread_info *info = lod_env_info(env);
2732 struct lod_layout_component *comp_array, *lod_comp, *old_array;
2733 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2734 struct dt_object *next = dt_object_child(dt);
2735 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
2736 struct lod_object *lo = lod_dt_obj(dt);
2737 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2739 int i, rc, array_cnt, old_array_cnt;
2742 LASSERT(lo->ldo_is_composite);
2744 if (lo->ldo_flr_state != LCM_FL_NONE)
2747 rc = lod_verify_striping(env, d, lo, buf, false);
2751 magic = comp_v1->lcm_magic;
2752 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2753 lustre_swab_lov_comp_md_v1(comp_v1);
2754 magic = comp_v1->lcm_magic;
2757 if (magic != LOV_USER_MAGIC_COMP_V1)
2760 mutex_lock(&lo->ldo_layout_mutex);
2762 array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2763 OBD_ALLOC_PTR_ARRAY(comp_array, array_cnt);
2764 if (comp_array == NULL) {
2765 mutex_unlock(&lo->ldo_layout_mutex);
2770 memcpy(comp_array, lo->ldo_comp_entries,
2771 sizeof(*comp_array) * lo->ldo_comp_cnt);
2773 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2774 struct lov_user_md_v1 *v1;
2775 struct lu_extent *ext;
2777 v1 = (struct lov_user_md *)((char *)comp_v1 +
2778 comp_v1->lcm_entries[i].lcme_offset);
2779 ext = &comp_v1->lcm_entries[i].lcme_extent;
2781 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2782 lod_comp->llc_extent.e_start = ext->e_start;
2783 lod_comp->llc_extent.e_end = ext->e_end;
2784 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2785 lod_comp->llc_flags = comp_v1->lcm_entries[i].lcme_flags;
2787 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2788 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2790 * limit stripe count so that it's less than/equal to
2791 * extent_size / stripe_size.
2793 * Note: extension size reused llc_stripe_size field and
2794 * uninstantiated component could be defined with
2795 * extent_start == extent_end as extension component will
2798 if (!(lod_comp->llc_flags & LCME_FL_EXTENSION) &&
2799 (lod_comp_inited(lod_comp) ||
2800 lod_comp->llc_extent.e_start <
2801 lod_comp->llc_extent.e_end) &&
2802 lod_comp->llc_stripe_count != LOV_ALL_STRIPES &&
2803 ext->e_end != OBD_OBJECT_EOF &&
2804 (__u64)(lod_comp->llc_stripe_count *
2805 lod_comp->llc_stripe_size) >
2806 (ext->e_end - ext->e_start))
2807 lod_comp->llc_stripe_count =
2808 DIV_ROUND_UP(ext->e_end - ext->e_start,
2809 lod_comp->llc_stripe_size);
2810 lod_adjust_stripe_info(lod_comp, desc, 0);
2812 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2813 struct lov_user_md_v3 *v3 = (typeof(*v3) *) v1;
2815 if (v3->lmm_pool_name[0] != '\0' &&
2816 !lov_pool_is_ignored(v3->lmm_pool_name)) {
2817 rc = lod_set_pool(&lod_comp->llc_pool,
2825 old_array = lo->ldo_comp_entries;
2826 old_array_cnt = lo->ldo_comp_cnt;
2828 lo->ldo_comp_entries = comp_array;
2829 lo->ldo_comp_cnt = array_cnt;
2831 /* No need to increase layout generation here, it will be increased
2832 * later when generating component ID for the new components */
2834 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2835 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2836 XATTR_NAME_LOV, 0, th);
2838 lo->ldo_comp_entries = old_array;
2839 lo->ldo_comp_cnt = old_array_cnt;
2843 OBD_FREE_PTR_ARRAY(old_array, old_array_cnt);
2845 LASSERT(lo->ldo_mirror_count == 1);
2846 lo->ldo_mirrors[0].lme_end = array_cnt - 1;
2848 mutex_unlock(&lo->ldo_layout_mutex);
2853 for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2854 lod_comp = &comp_array[i];
2855 if (lod_comp->llc_pool != NULL) {
2856 OBD_FREE(lod_comp->llc_pool,
2857 strlen(lod_comp->llc_pool) + 1);
2858 lod_comp->llc_pool = NULL;
2861 OBD_FREE_PTR_ARRAY(comp_array, array_cnt);
2862 mutex_unlock(&lo->ldo_layout_mutex);
2868 * lod_last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2869 * @mirror_id: Mirror id to be checked.
2872 * This function checks if a mirror with specified @mirror_id is the last
2873 * non-stale mirror of a LOD object @lo.
2875 * Return: true or false.
2878 bool lod_last_non_stale_mirror(__u16 mirror_id, struct lod_object *lo)
2880 struct lod_layout_component *lod_comp;
2881 bool has_stale_flag;
2884 for (i = 0; i < lo->ldo_mirror_count; i++) {
2885 if (lo->ldo_mirrors[i].lme_id == mirror_id ||
2886 lo->ldo_mirrors[i].lme_stale)
2889 has_stale_flag = false;
2890 lod_foreach_mirror_comp(lod_comp, lo, i) {
2891 if (lod_comp->llc_flags & LCME_FL_STALE) {
2892 has_stale_flag = true;
2896 if (!has_stale_flag)
2904 * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2905 * the '$field' can only be 'flags' now. The xattr value is binary
2906 * lov_comp_md_v1 which contains the component ID(s) and the value of
2907 * the field to be modified.
2908 * Please update allowed_lustre_lov macro if $field groks more values
2911 * \param[in] env execution environment
2912 * \param[in] dt dt_object to be modified
2913 * \param[in] op operation string, like "set.flags"
2914 * \param[in] buf buffer contains components to be set
2915 * \parem[in] th thandle
2917 * \retval 0 on success
2918 * \retval negative errno on failure
2920 static int lod_declare_layout_set(const struct lu_env *env,
2921 struct dt_object *dt,
2922 char *op, const struct lu_buf *buf,
2925 struct lod_layout_component *lod_comp;
2926 struct lod_thread_info *info = lod_env_info(env);
2927 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2928 struct lod_object *lo = lod_dt_obj(dt);
2929 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2932 bool changed = false;
2935 /* Please update allowed_lustre_lov macro if op
2936 * groks more values in the future
2938 if (strcmp(op, "set.flags") != 0) {
2939 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2940 lod2obd(d)->obd_name, op);
2944 magic = comp_v1->lcm_magic;
2945 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2946 lustre_swab_lov_comp_md_v1(comp_v1);
2947 magic = comp_v1->lcm_magic;
2950 if (magic != LOV_USER_MAGIC_COMP_V1)
2953 if (comp_v1->lcm_entry_count == 0) {
2954 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2955 lod2obd(d)->obd_name);
2959 mutex_lock(&lo->ldo_layout_mutex);
2960 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2961 __u32 id = comp_v1->lcm_entries[i].lcme_id;
2962 __u32 flags = comp_v1->lcm_entries[i].lcme_flags;
2963 __u32 mirror_flag = flags & LCME_MIRROR_FLAGS;
2964 __u16 mirror_id = mirror_id_of(id);
2965 bool neg = flags & LCME_FL_NEG;
2967 if (flags & LCME_FL_INIT) {
2969 lod_striping_free_nolock(env, lo);
2970 mutex_unlock(&lo->ldo_layout_mutex);
2974 flags &= ~(LCME_MIRROR_FLAGS | LCME_FL_NEG);
2975 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2976 lod_comp = &lo->ldo_comp_entries[j];
2978 /* lfs only put one flag in each entry */
2979 if ((flags && id != lod_comp->llc_id) ||
2980 (mirror_flag && mirror_id !=
2981 mirror_id_of(lod_comp->llc_id)))
2986 lod_comp->llc_flags &= ~flags;
2988 lod_comp->llc_flags &= ~mirror_flag;
2991 if ((flags & LCME_FL_STALE) &&
2992 lod_last_non_stale_mirror(mirror_id,
2995 &lo->ldo_layout_mutex);
2998 lod_comp->llc_flags |= flags;
3001 lod_comp->llc_flags |= mirror_flag;
3002 if (mirror_flag & LCME_FL_NOSYNC)
3003 lod_comp->llc_timestamp =
3004 ktime_get_real_seconds();
3010 mutex_unlock(&lo->ldo_layout_mutex);
3013 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
3014 lod2obd(d)->obd_name);
3018 lod_obj_inc_layout_gen(lo);
3020 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
3021 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), &info->lti_buf,
3022 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3027 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
3028 * and the xattr value is a unique component ID or a special lcme_id.
3030 * \param[in] env execution environment
3031 * \param[in] dt dt_object to be operated on
3032 * \param[in] buf buffer contains component ID or lcme_id
3033 * \parem[in] th thandle
3035 * \retval 0 on success
3036 * \retval negative errno on failure
3038 static int lod_declare_layout_del(const struct lu_env *env,
3039 struct dt_object *dt,
3040 const struct lu_buf *buf,
3043 struct lod_thread_info *info = lod_env_info(env);
3044 struct dt_object *next = dt_object_child(dt);
3045 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3046 struct lod_object *lo = lod_dt_obj(dt);
3047 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3048 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
3049 __u32 magic, id, flags, neg_flags = 0;
3053 LASSERT(lo->ldo_is_composite);
3055 if (lo->ldo_flr_state != LCM_FL_NONE)
3058 magic = comp_v1->lcm_magic;
3059 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3060 lustre_swab_lov_comp_md_v1(comp_v1);
3061 magic = comp_v1->lcm_magic;
3064 if (magic != LOV_USER_MAGIC_COMP_V1)
3067 id = comp_v1->lcm_entries[0].lcme_id;
3068 flags = comp_v1->lcm_entries[0].lcme_flags;
3070 if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
3071 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
3072 lod2obd(d)->obd_name, id, flags);
3076 if (id != LCME_ID_INVAL && flags != 0) {
3077 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
3078 lod2obd(d)->obd_name);
3082 if (id == LCME_ID_INVAL && !flags) {
3083 CDEBUG(D_LAYOUT, "%s: no id or flags specified.\n",
3084 lod2obd(d)->obd_name);
3088 if (flags & LCME_FL_NEG) {
3089 neg_flags = flags & ~LCME_FL_NEG;
3093 mutex_lock(&lo->ldo_layout_mutex);
3095 left = lo->ldo_comp_cnt;
3097 mutex_unlock(&lo->ldo_layout_mutex);
3101 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3102 struct lod_layout_component *lod_comp;
3104 lod_comp = &lo->ldo_comp_entries[i];
3106 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
3108 else if (flags && !(flags & lod_comp->llc_flags))
3110 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
3113 if (left != (i + 1)) {
3114 CDEBUG(D_LAYOUT, "%s: this deletion will create "
3115 "a hole.\n", lod2obd(d)->obd_name);
3116 mutex_unlock(&lo->ldo_layout_mutex);
3121 /* Mark the component as deleted */
3122 lod_comp->llc_id = LCME_ID_INVAL;
3124 /* Not instantiated component */
3125 if (lod_comp->llc_stripe == NULL)
3128 LASSERT(lod_comp->llc_stripe_count > 0);
3129 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
3130 struct dt_object *obj = lod_comp->llc_stripe[j];
3134 rc = lod_sub_declare_destroy(env, obj, th);
3136 mutex_unlock(&lo->ldo_layout_mutex);
3142 LASSERTF(left >= 0, "left = %d\n", left);
3143 if (left == lo->ldo_comp_cnt) {
3144 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
3145 lod2obd(d)->obd_name, id);
3146 mutex_unlock(&lo->ldo_layout_mutex);
3150 mutex_unlock(&lo->ldo_layout_mutex);
3152 memset(attr, 0, sizeof(*attr));
3153 attr->la_valid = LA_SIZE;
3154 rc = lod_sub_declare_attr_set(env, next, attr, th);
3159 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
3160 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
3161 XATTR_NAME_LOV, 0, th);
3163 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
3170 * Declare layout add/set/del operations issued by special xattr names:
3172 * XATTR_LUSTRE_LOV.add add component(s) to existing file
3173 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
3174 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
3176 * \param[in] env execution environment
3177 * \param[in] dt object
3178 * \param[in] name name of xattr
3179 * \param[in] buf lu_buf contains xattr value
3180 * \param[in] th transaction handle
3182 * \retval 0 on success
3183 * \retval negative if failed
3185 static int lod_declare_modify_layout(const struct lu_env *env,
3186 struct dt_object *dt,
3188 const struct lu_buf *buf,
3191 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3192 struct lod_object *lo = lod_dt_obj(dt);
3194 int rc, len = strlen(XATTR_LUSTRE_LOV);
3197 LASSERT(dt_object_exists(dt));
3199 if (strlen(name) <= len || name[len] != '.') {
3200 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
3201 lod2obd(d)->obd_name, name);
3206 rc = lod_striping_load(env, lo);
3210 /* the layout to be modified must be a composite layout */
3211 if (!lo->ldo_is_composite) {
3212 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
3213 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3214 GOTO(unlock, rc = -EINVAL);
3217 op = (char *)name + len;
3218 if (strcmp(op, "add") == 0) {
3219 rc = lod_declare_layout_add(env, dt, buf, th);
3220 } else if (strcmp(op, "del") == 0) {
3221 rc = lod_declare_layout_del(env, dt, buf, th);
3222 } else if (strncmp(op, "set", strlen("set")) == 0) {
3223 rc = lod_declare_layout_set(env, dt, op, buf, th);
3225 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
3226 lod2obd(d)->obd_name, name);
3227 GOTO(unlock, rc = -ENOTSUPP);
3231 lod_striping_free(env, lo);
3237 * Convert a plain file lov_mds_md to a composite layout.
3239 * \param[in,out] info the thread info::lti_ea_store buffer contains little
3240 * endian plain file layout
3242 * \retval 0 on success, <0 on failure
3244 static int lod_layout_convert(struct lod_thread_info *info)
3246 struct lov_mds_md *lmm = info->lti_ea_store;
3247 struct lov_mds_md *lmm_save;
3248 struct lov_comp_md_v1 *lcm;
3249 struct lov_comp_md_entry_v1 *lcme;
3255 /* realloc buffer to a composite layout which contains one component */
3256 blob_size = lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
3257 le32_to_cpu(lmm->lmm_magic));
3258 size = sizeof(*lcm) + sizeof(*lcme) + blob_size;
3260 OBD_ALLOC_LARGE(lmm_save, blob_size);
3262 GOTO(out, rc = -ENOMEM);
3264 memcpy(lmm_save, lmm, blob_size);
3266 if (info->lti_ea_store_size < size) {
3267 rc = lod_ea_store_resize(info, size);
3272 lcm = info->lti_ea_store;
3273 memset(lcm, 0, sizeof(*lcm) + sizeof(*lcme));
3274 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
3275 lcm->lcm_size = cpu_to_le32(size);
3276 lcm->lcm_layout_gen = cpu_to_le32(le16_to_cpu(
3277 lmm_save->lmm_layout_gen));
3278 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
3279 lcm->lcm_entry_count = cpu_to_le16(1);
3281 lcme = &lcm->lcm_entries[0];
3282 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
3283 lcme->lcme_extent.e_start = 0;
3284 lcme->lcme_extent.e_end = cpu_to_le64(OBD_OBJECT_EOF);
3285 lcme->lcme_offset = cpu_to_le32(sizeof(*lcm) + sizeof(*lcme));
3286 lcme->lcme_size = cpu_to_le32(blob_size);
3288 memcpy((char *)lcm + lcme->lcme_offset, (char *)lmm_save, blob_size);
3293 OBD_FREE_LARGE(lmm_save, blob_size);
3298 * Merge layouts to form a mirrored file.
3300 static int lod_declare_layout_merge(const struct lu_env *env,
3301 struct dt_object *dt, const struct lu_buf *mbuf,
3304 struct lod_thread_info *info = lod_env_info(env);
3305 struct lu_attr *layout_attr = &info->lti_layout_attr;
3306 struct lu_buf *buf = &info->lti_buf;
3307 struct lod_object *lo = lod_dt_obj(dt);
3308 struct lov_comp_md_v1 *lcm;
3309 struct lov_comp_md_v1 *cur_lcm;
3310 struct lov_comp_md_v1 *merge_lcm;
3311 struct lov_comp_md_entry_v1 *lcme;
3312 struct lov_mds_md_v1 *lmm;
3315 __u16 cur_entry_count;
3316 __u16 merge_entry_count;
3318 __u16 mirror_id = 0;
3325 merge_lcm = mbuf->lb_buf;
3326 if (mbuf->lb_len < sizeof(*merge_lcm))
3329 /* must be an existing layout from disk */
3330 if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
3333 merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
3335 /* do not allow to merge two mirrored files */
3336 if (le16_to_cpu(merge_lcm->lcm_mirror_count))
3339 /* verify the target buffer */
3340 rc = lod_get_lov_ea(env, lo);
3342 RETURN(rc ? : -ENODATA);
3344 cur_lcm = info->lti_ea_store;
3345 switch (le32_to_cpu(cur_lcm->lcm_magic)) {
3348 rc = lod_layout_convert(info);
3350 case LOV_MAGIC_COMP_V1:
3360 /* info->lti_ea_store could be reallocated in lod_layout_convert() */
3361 cur_lcm = info->lti_ea_store;
3362 cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
3364 /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
3365 mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
3366 if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
3369 /* size of new layout */
3370 size = le32_to_cpu(cur_lcm->lcm_size) +
3371 le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
3373 memset(buf, 0, sizeof(*buf));
3374 lu_buf_alloc(buf, size);
3375 if (buf->lb_buf == NULL)
3379 memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
3381 offset = sizeof(*lcm) +
3382 sizeof(*lcme) * (cur_entry_count + merge_entry_count);
3383 for (i = 0; i < cur_entry_count; i++) {
3384 struct lov_comp_md_entry_v1 *cur_lcme;
3386 lcme = &lcm->lcm_entries[i];
3387 cur_lcme = &cur_lcm->lcm_entries[i];
3389 lcme->lcme_offset = cpu_to_le32(offset);
3390 memcpy((char *)lcm + offset,
3391 (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
3392 le32_to_cpu(lcme->lcme_size));
3394 offset += le32_to_cpu(lcme->lcme_size);
3396 if (mirror_count == 1 &&
3397 mirror_id_of(le32_to_cpu(lcme->lcme_id)) == 0) {
3398 /* Add mirror from a non-flr file, create new mirror ID.
3399 * Otherwise, keep existing mirror's component ID, used
3400 * for mirror extension.
3402 id = pflr_id(1, i + 1);
3403 lcme->lcme_id = cpu_to_le32(id);
3406 id = max(le32_to_cpu(lcme->lcme_id), id);
3409 mirror_id = mirror_id_of(id) + 1;
3411 /* check if first entry in new layout is DOM */
3412 lmm = (struct lov_mds_md_v1 *)((char *)merge_lcm +
3413 merge_lcm->lcm_entries[0].lcme_offset);
3414 merge_has_dom = lov_pattern(le32_to_cpu(lmm->lmm_pattern)) &
3417 for (i = 0; i < merge_entry_count; i++) {
3418 struct lov_comp_md_entry_v1 *merge_lcme;
3420 merge_lcme = &merge_lcm->lcm_entries[i];
3421 lcme = &lcm->lcm_entries[cur_entry_count + i];
3423 *lcme = *merge_lcme;
3424 lcme->lcme_offset = cpu_to_le32(offset);
3425 if (merge_has_dom && i == 0)
3426 lcme->lcme_flags |= cpu_to_le32(LCME_FL_STALE);
3428 id = pflr_id(mirror_id, i + 1);
3429 lcme->lcme_id = cpu_to_le32(id);
3431 memcpy((char *)lcm + offset,
3432 (char *)merge_lcm + le32_to_cpu(merge_lcme->lcme_offset),
3433 le32_to_cpu(lcme->lcme_size));
3435 offset += le32_to_cpu(lcme->lcme_size);
3438 /* fixup layout information */
3439 lcm->lcm_size = cpu_to_le32(size);
3440 lcm->lcm_entry_count = cpu_to_le16(cur_entry_count + merge_entry_count);
3441 lcm->lcm_mirror_count = cpu_to_le16(mirror_count);
3442 if ((le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK) == LCM_FL_NONE)
3443 lcm->lcm_flags = cpu_to_le32(LCM_FL_RDONLY);
3445 rc = lod_striping_reload(env, lo, buf, 0);
3449 lod_obj_inc_layout_gen(lo);
3450 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3452 /* transfer layout version to OST objects. */
3453 if (lo->ldo_mirror_count > 1) {
3454 struct lod_obj_stripe_cb_data data = { {0} };
3456 layout_attr->la_valid = LA_LAYOUT_VERSION;
3457 layout_attr->la_layout_version = 0;
3458 data.locd_attr = layout_attr;
3459 data.locd_declare = true;
3460 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
3461 rc = lod_obj_for_each_stripe(env, lo, th, &data);
3466 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), buf,
3467 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3475 * Split layouts, just set the LOVEA with the layout from mbuf.
3477 static int lod_declare_layout_split(const struct lu_env *env,
3478 struct dt_object *dt, const struct lu_buf *mbuf,
3481 struct lod_thread_info *info = lod_env_info(env);
3482 struct lu_attr *layout_attr = &info->lti_layout_attr;
3483 struct lod_object *lo = lod_dt_obj(dt);
3484 struct lov_comp_md_v1 *lcm = mbuf->lb_buf;
3488 rc = lod_striping_reload(env, lo, mbuf, LVF_ALL_STALE);
3492 lod_obj_inc_layout_gen(lo);
3493 /* fix on-disk layout gen */
3494 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3496 /* transfer layout version to OST objects. */
3497 if (lo->ldo_mirror_count > 1) {
3498 struct lod_obj_stripe_cb_data data = { {0} };
3500 layout_attr->la_valid = LA_LAYOUT_VERSION;
3501 layout_attr->la_layout_version = 0;
3502 data.locd_attr = layout_attr;
3503 data.locd_declare = true;
3504 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
3505 rc = lod_obj_for_each_stripe(env, lo, th, &data);
3510 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), mbuf,
3511 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3515 static int lod_layout_declare_or_purge_mirror(const struct lu_env *env,
3516 struct dt_object *dt, const struct lu_buf *buf,
3517 struct thandle *th, bool declare)
3519 struct lod_thread_info *info = lod_env_info(env);
3520 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3521 struct lod_object *lo = lod_dt_obj(dt);
3522 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
3523 struct lov_comp_md_entry_v1 *entry;
3524 struct lov_mds_md_v1 *lmm;
3525 struct dt_object **sub_objs = NULL;
3526 int rc = 0, i, k, array_count = 0;
3531 * other ops (like lod_declare_destroy) could destroying sub objects
3534 mutex_lock(&lo->ldo_layout_mutex);
3537 /* prepare sub-objects array */
3538 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3539 entry = &comp_v1->lcm_entries[i];
3541 if (!(entry->lcme_flags & LCME_FL_INIT))
3544 lmm = (struct lov_mds_md_v1 *)
3545 ((char *)comp_v1 + entry->lcme_offset);
3546 array_count += lmm->lmm_stripe_count;
3548 OBD_ALLOC_PTR_ARRAY(sub_objs, array_count);
3549 if (sub_objs == NULL) {
3550 mutex_unlock(&lo->ldo_layout_mutex);
3555 k = 0; /* sub_objs index */
3556 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3557 struct lov_ost_data_v1 *objs;
3558 struct lu_object *o, *n;
3559 struct dt_object *dto;
3560 struct lu_device *nd;
3561 struct lov_mds_md_v3 *v3;
3565 entry = &comp_v1->lcm_entries[i];
3567 if (!(entry->lcme_flags & LCME_FL_INIT))
3570 lmm = (struct lov_mds_md_v1 *)
3571 ((char *)comp_v1 + entry->lcme_offset);
3572 v3 = (struct lov_mds_md_v3 *)lmm;
3573 if (lmm->lmm_magic == LOV_MAGIC_V3)
3574 objs = &v3->lmm_objects[0];
3576 objs = &lmm->lmm_objects[0];
3578 for (j = 0; j < lmm->lmm_stripe_count; j++) {
3579 idx = objs[j].l_ost_idx;
3580 rc = ostid_to_fid(&info->lti_fid, &objs[j].l_ost_oi,
3585 if (!fid_is_sane(&info->lti_fid)) {
3586 CERROR("%s: sub-object insane fid "DFID"\n",
3587 lod2obd(d)->obd_name,
3588 PFID(&info->lti_fid));
3589 GOTO(out, rc = -EINVAL);
3592 lod_getref(&d->lod_ost_descs);
3594 rc = validate_lod_and_idx(d, idx);
3596 lod_putref(d, &d->lod_ost_descs);
3600 nd = &OST_TGT(d, idx)->ltd_tgt->dd_lu_dev;
3601 lod_putref(d, &d->lod_ost_descs);
3603 o = lu_object_find_at(env, nd, &info->lti_fid, NULL);
3605 GOTO(out, rc = PTR_ERR(o));
3607 n = lu_object_locate(o->lo_header, nd->ld_type);
3609 lu_object_put(env, n);
3610 GOTO(out, rc = -ENOENT);
3613 dto = container_of(n, struct dt_object, do_lu);
3616 rc = lod_sub_declare_destroy(env, dto, th);
3617 dt_object_put(env, dto);
3622 * collect to-be-destroyed sub objects, the
3623 * reference would be released after actual
3629 } /* for each stripe */
3630 } /* for each component in the mirror */
3635 /* destroy the sub objects */
3636 for (; i < k; i++) {
3637 rc = lod_sub_destroy(env, sub_objs[i], th);
3640 dt_object_put(env, sub_objs[i]);
3644 * if a sub object destroy failed, we'd release sub objects
3645 * reference get from above sub_objs collection.
3648 dt_object_put(env, sub_objs[i]);
3650 OBD_FREE_PTR_ARRAY(sub_objs, array_count);
3652 mutex_unlock(&lo->ldo_layout_mutex);
3658 * Purge layouts, delete sub objects in the mirror stored in the vic_buf,
3659 * and set the LOVEA with the layout from mbuf.
3661 static int lod_declare_layout_purge(const struct lu_env *env,
3662 struct dt_object *dt, const struct lu_buf *buf,
3665 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3666 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
3671 if (le32_to_cpu(comp_v1->lcm_magic) != LOV_MAGIC_COMP_V1) {
3672 CERROR("%s: invalid layout magic %#x != %#x\n",
3673 lod2obd(d)->obd_name, le32_to_cpu(comp_v1->lcm_magic),
3678 if (cpu_to_le32(LOV_MAGIC_COMP_V1) != LOV_MAGIC_COMP_V1)
3679 lustre_swab_lov_comp_md_v1(comp_v1);
3681 /* from now on, @buf contains cpu endian data */
3683 if (comp_v1->lcm_mirror_count != 0) {
3684 CERROR("%s: can only purge one mirror from "DFID"\n",
3685 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3689 /* delcare sub objects deletion in the mirror stored in @buf */
3690 rc = lod_layout_declare_or_purge_mirror(env, dt, buf, th, true);
3694 /* delete sub objects from the mirror stored in @buf */
3695 static int lod_layout_purge(const struct lu_env *env, struct dt_object *dt,
3696 const struct lu_buf *buf, struct thandle *th)
3701 rc = lod_layout_declare_or_purge_mirror(env, dt, buf, th, false);
3706 * Implementation of dt_object_operations::do_declare_xattr_set.
3708 * \see dt_object_operations::do_declare_xattr_set() in the API description
3711 * the extension to the API:
3712 * - declaring LOVEA requests striping creation
3713 * - LU_XATTR_REPLACE means layout swap
3715 static int lod_declare_xattr_set(const struct lu_env *env,
3716 struct dt_object *dt,
3717 const struct lu_buf *buf,
3718 const char *name, int fl,
3721 struct dt_object *next = dt_object_child(dt);
3722 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3727 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
3728 if ((S_ISREG(mode) || mode == 0) &&
3729 !(fl & (LU_XATTR_REPLACE | LU_XATTR_MERGE | LU_XATTR_SPLIT |
3731 (strcmp(name, XATTR_NAME_LOV) == 0 ||
3732 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
3734 * this is a request to create object's striping.
3736 * allow to declare predefined striping on a new (!mode) object
3737 * which is supposed to be replay of regular file creation
3738 * (when LOV setting is declared)
3740 * LU_XATTR_REPLACE is set to indicate a layout swap
3742 if (dt_object_exists(dt)) {
3743 rc = dt_attr_get(env, next, attr);
3747 memset(attr, 0, sizeof(*attr));
3748 attr->la_valid = LA_TYPE | LA_MODE;
3749 attr->la_mode = S_IFREG;
3751 rc = lod_declare_striped_create(env, dt, attr, buf, th);
3752 } else if (fl & LU_XATTR_MERGE) {
3753 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3754 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3755 rc = lod_declare_layout_merge(env, dt, buf, th);
3756 } else if (fl & LU_XATTR_SPLIT) {
3757 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3758 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3759 rc = lod_declare_layout_split(env, dt, buf, th);
3760 } else if (fl & LU_XATTR_PURGE) {
3761 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3762 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3763 rc = lod_declare_layout_purge(env, dt, buf, th);
3764 } else if (S_ISREG(mode) &&
3765 strlen(name) >= sizeof(XATTR_LUSTRE_LOV) + 3 &&
3766 allowed_lustre_lov(name)) {
3768 * this is a request to modify object's striping.
3769 * add/set/del component(s).
3771 if (!dt_object_exists(dt))
3774 rc = lod_declare_modify_layout(env, dt, name, buf, th);
3775 } else if (S_ISDIR(mode)) {
3776 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
3777 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3778 rc = lod_replace_parent_fid(env, dt, buf, th, true);
3780 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
3787 * Apply xattr changes to the object.
3789 * Applies xattr changes to the object and the stripes if the latter exist.
3791 * \param[in] env execution environment
3792 * \param[in] dt object
3793 * \param[in] buf buffer pointing to the new value of xattr
3794 * \param[in] name name of xattr
3795 * \param[in] fl flags
3796 * \param[in] th transaction handle
3798 * \retval 0 on success
3799 * \retval negative if failed
3801 static int lod_xattr_set_internal(const struct lu_env *env,
3802 struct dt_object *dt,
3803 const struct lu_buf *buf,
3804 const char *name, int fl,
3807 struct dt_object *next = dt_object_child(dt);
3808 struct lod_object *lo = lod_dt_obj(dt);
3813 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3814 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3817 /* Note: Do not set LinkEA on sub-stripes, otherwise
3818 * it will confuse the fid2path process(see mdt_path_current()).
3819 * The linkEA between master and sub-stripes is set in
3820 * lod_xattr_set_lmv(). */
3821 if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
3824 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3825 if (!lo->ldo_stripe[i])
3828 if (!dt_object_exists(lo->ldo_stripe[i]))
3831 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
3841 * Delete an extended attribute.
3843 * Deletes specified xattr from the object and the stripes if the latter exist.
3845 * \param[in] env execution environment
3846 * \param[in] dt object
3847 * \param[in] name name of xattr
3848 * \param[in] th transaction handle
3850 * \retval 0 on success
3851 * \retval negative if failed
3853 static int lod_xattr_del_internal(const struct lu_env *env,
3854 struct dt_object *dt,
3855 const char *name, struct thandle *th)
3857 struct dt_object *next = dt_object_child(dt);
3858 struct lod_object *lo = lod_dt_obj(dt);
3864 rc = lod_sub_xattr_del(env, next, name, th);
3865 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3868 if (lo->ldo_dir_stripe_count == 0)
3871 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3872 if (!lo->ldo_stripe[i])
3875 if (!dt_object_exists(lo->ldo_stripe[i]))
3878 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3887 * Set default striping on a directory.
3889 * Sets specified striping on a directory object unless it matches the default
3890 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3891 * EA. This striping will be used when regular file is being created in this
3894 * \param[in] env execution environment
3895 * \param[in] dt the striped object
3896 * \param[in] buf buffer with the striping
3897 * \param[in] name name of EA
3898 * \param[in] fl xattr flag (see OSD API description)
3899 * \param[in] th transaction handle
3901 * \retval 0 on success
3902 * \retval negative if failed
3904 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
3905 struct dt_object *dt,
3906 const struct lu_buf *buf,
3907 const char *name, int fl,
3910 struct lov_user_md_v1 *lum;
3911 struct lov_user_md_v3 *v3 = NULL;
3912 const char *pool_name = NULL;
3917 LASSERT(buf != NULL && buf->lb_buf != NULL);
3920 switch (lum->lmm_magic) {
3921 case LOV_USER_MAGIC_SPECIFIC:
3922 case LOV_USER_MAGIC_V3:
3924 if (lov_pool_is_reserved(v3->lmm_pool_name))
3925 memset(v3->lmm_pool_name, 0, sizeof(v3->lmm_pool_name));
3926 else if (v3->lmm_pool_name[0] != '\0')
3927 pool_name = v3->lmm_pool_name;
3929 case LOV_USER_MAGIC_V1:
3930 /* if { size, offset, count } = { 0, -1, 0 } and no pool
3931 * (i.e. all default values specified) then delete default
3932 * striping from dir. */
3934 "set default striping: sz %u # %u offset %d %s %s\n",
3935 (unsigned)lum->lmm_stripe_size,
3936 (unsigned)lum->lmm_stripe_count,
3937 (int)lum->lmm_stripe_offset,
3938 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
3940 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
3941 lum->lmm_stripe_count,
3942 lum->lmm_stripe_offset,
3945 case LOV_USER_MAGIC_COMP_V1:
3947 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lum;
3948 struct lov_comp_md_entry_v1 *lcme;
3951 comp_cnt = le16_to_cpu(lcm->lcm_entry_count);
3952 for (i = 0; i < comp_cnt; i++) {
3953 lcme = &lcm->lcm_entries[i];
3954 if (lcme->lcme_flags & cpu_to_le32(LCME_FL_EXTENSION)) {
3955 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_SEL);
3964 CERROR("Invalid magic %x\n", lum->lmm_magic);
3969 rc = lod_xattr_del_internal(env, dt, name, th);
3973 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3979 static int lod_get_default_lov_striping(const struct lu_env *env,
3980 struct lod_object *lo,
3981 struct lod_default_striping *lds,
3982 struct dt_allocation_hint *ah);
3985 * Helper function to convert compound layout to compound layout with
3988 * Copy lcm_entries array of \a src to \a tgt. Replace lov_user_md_v1
3989 * components of \a src with lov_user_md_v3 using \a pool.
3991 * \param[in] src source layout
3992 * \param[in] pool pool to use in \a tgt
3993 * \param[out] tgt target layout
3995 static void embed_pool_to_comp_v1(const struct lov_comp_md_v1 *src,
3997 struct lov_comp_md_v1 *tgt)
4000 struct lov_user_md_v1 *lum;
4001 struct lov_user_md_v3 *lum3;
4002 struct lov_comp_md_entry_v1 *entry;
4006 entry = tgt->lcm_entries;
4008 for (i = 0; i < le16_to_cpu(src->lcm_entry_count); i++, entry++) {
4009 *entry = src->lcm_entries[i];
4010 offset = le32_to_cpu(src->lcm_entries[i].lcme_offset);
4011 entry->lcme_offset = cpu_to_le32(offset + shift);
4013 lum = (struct lov_user_md_v1 *)((char *)src + offset);
4014 lum3 = (struct lov_user_md_v3 *)((char *)tgt + offset + shift);
4015 *(struct lov_user_md_v1 *)lum3 = *lum;
4016 if (lum->lmm_pattern & cpu_to_le32(LOV_PATTERN_MDT)) {
4017 lum3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
4019 lum3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
4020 entry->lcme_size = cpu_to_le32(sizeof(*lum3));
4021 strlcpy(lum3->lmm_pool_name, pool,
4022 sizeof(lum3->lmm_pool_name));
4023 shift += sizeof(*lum3) - sizeof(*lum);
4029 * Set default striping on a directory.
4031 * Sets specified striping on a directory object unless it matches the default
4032 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
4033 * EA. This striping will be used when regular file is being created in this
4035 * If current default striping includes a pool but specifed striping
4036 * does not - retain the pool if it exists.
4038 * \param[in] env execution environment
4039 * \param[in] dt the striped object
4040 * \param[in] buf buffer with the striping
4041 * \param[in] name name of EA
4042 * \param[in] fl xattr flag (see OSD API description)
4043 * \param[in] th transaction handle
4045 * \retval 0 on success
4046 * \retval negative if failed
4048 static int lod_xattr_set_default_lov_on_dir(const struct lu_env *env,
4049 struct dt_object *dt,
4050 const struct lu_buf *buf,
4051 const char *name, int fl,
4054 struct lod_default_striping *lds = lod_lds_buf_get(env);
4055 struct lov_user_md_v1 *v1 = buf->lb_buf;
4056 char pool[LOV_MAXPOOLNAME + 1];
4062 /* get existing striping config */
4063 rc = lod_get_default_lov_striping(env, lod_dt_obj(dt), lds, NULL);
4067 memset(pool, 0, sizeof(pool));
4068 if (lds->lds_def_striping_set == 1)
4069 lod_layout_get_pool(lds->lds_def_comp_entries,
4070 lds->lds_def_comp_cnt, pool,
4073 is_del = LOVEA_DELETE_VALUES(v1->lmm_stripe_size,
4074 v1->lmm_stripe_count,
4075 v1->lmm_stripe_offset,
4078 /* Retain the pool name if it is not given */
4079 if (v1->lmm_magic == LOV_USER_MAGIC_V1 && pool[0] != '\0' &&
4081 struct lod_thread_info *info = lod_env_info(env);
4082 struct lov_user_md_v3 *v3 = info->lti_ea_store;
4084 memset(v3, 0, sizeof(*v3));
4085 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
4086 v3->lmm_pattern = cpu_to_le32(v1->lmm_pattern);
4087 v3->lmm_stripe_count = cpu_to_le32(v1->lmm_stripe_count);
4088 v3->lmm_stripe_offset = cpu_to_le32(v1->lmm_stripe_offset);
4089 v3->lmm_stripe_size = cpu_to_le32(v1->lmm_stripe_size);
4091 strlcpy(v3->lmm_pool_name, pool, sizeof(v3->lmm_pool_name));
4093 info->lti_buf.lb_buf = v3;
4094 info->lti_buf.lb_len = sizeof(*v3);
4095 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4097 } else if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1 &&
4098 pool[0] != '\0' && !is_del) {
4100 * try to retain the pool from default layout if the
4101 * specified component layout does not provide pool
4104 struct lod_thread_info *info = lod_env_info(env);
4105 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
4106 struct lov_comp_md_v1 *comp_v1p;
4107 struct lov_user_md_v1 *lum;
4111 struct lov_comp_md_entry_v1 *entry;
4114 entry_count = le16_to_cpu(comp_v1->lcm_entry_count);
4115 size = sizeof(*comp_v1) +
4116 entry_count * sizeof(comp_v1->lcm_entries[0]);
4117 entry = comp_v1->lcm_entries;
4118 for (i = 0; i < entry_count; i++, entry++) {
4119 offset = le32_to_cpu(entry->lcme_offset);
4120 lum = (struct lov_user_md_v1 *)((char *)comp_v1 +
4122 if (le32_to_cpu(lum->lmm_magic) != LOV_USER_MAGIC_V1)
4123 /* the i-th component includes pool info */
4125 if (lum->lmm_pattern & cpu_to_le32(LOV_PATTERN_MDT))
4126 size += sizeof(struct lov_user_md_v1);
4128 size += sizeof(struct lov_user_md_v3);
4131 if (i == entry_count) {
4133 * re-compose the layout to include the pool for
4136 if (info->lti_ea_store_size < size)
4137 rc = lod_ea_store_resize(info, size);
4140 comp_v1p = info->lti_ea_store;
4141 *comp_v1p = *comp_v1;
4142 comp_v1p->lcm_size = cpu_to_le32(size);
4143 embed_pool_to_comp_v1(comp_v1, pool, comp_v1p);
4145 info->lti_buf.lb_buf = comp_v1p;
4146 info->lti_buf.lb_len = size;
4147 rc = lod_xattr_set_lov_on_dir(env, dt,
4152 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl,
4156 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
4159 if (lds->lds_def_striping_set == 1 && lds->lds_def_comp_entries != NULL)
4160 lod_free_def_comp_entries(lds);
4166 * Set default striping on a directory object.
4168 * Sets specified striping on a directory object unless it matches the default
4169 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
4170 * EA. This striping will be used when a new directory is being created in the
4173 * \param[in] env execution environment
4174 * \param[in] dt the striped object
4175 * \param[in] buf buffer with the striping
4176 * \param[in] name name of EA
4177 * \param[in] fl xattr flag (see OSD API description)
4178 * \param[in] th transaction handle
4180 * \retval 0 on success
4181 * \retval negative if failed
4183 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
4184 struct dt_object *dt,
4185 const struct lu_buf *buf,
4186 const char *name, int fl,
4189 struct lmv_user_md_v1 *lum;
4194 LASSERT(buf != NULL && buf->lb_buf != NULL);
4198 "set default stripe_count # %u stripe_offset %d hash %u\n",
4199 le32_to_cpu(lum->lum_stripe_count),
4200 (int)le32_to_cpu(lum->lum_stripe_offset),
4201 le32_to_cpu(lum->lum_hash_type));
4203 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
4204 le32_to_cpu(lum->lum_stripe_offset)) &&
4205 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
4206 rc = lod_xattr_del_internal(env, dt, name, th);
4210 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
4219 * Turn directory into a striped directory.
4221 * During replay the client sends the striping created before MDT
4222 * failure, then the layer above LOD sends this defined striping
4223 * using ->do_xattr_set(), so LOD uses this method to replay creation
4224 * of the stripes. Notice the original information for the striping
4225 * (#stripes, FIDs, etc) was transferred in declare path.
4227 * \param[in] env execution environment
4228 * \param[in] dt the striped object
4229 * \param[in] buf not used currently
4230 * \param[in] name not used currently
4231 * \param[in] fl xattr flag (see OSD API description)
4232 * \param[in] th transaction handle
4234 * \retval 0 on success
4235 * \retval negative if failed
4237 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
4238 const struct lu_buf *buf, const char *name,
4239 int fl, struct thandle *th)
4241 struct lod_object *lo = lod_dt_obj(dt);
4242 struct lod_thread_info *info = lod_env_info(env);
4243 struct lu_attr *attr = &info->lti_attr;
4244 struct dt_object_format *dof = &info->lti_format;
4245 struct lu_buf lmv_buf;
4246 struct lu_buf slave_lmv_buf;
4247 struct lmv_mds_md_v1 *lmm;
4248 struct lmv_mds_md_v1 *slave_lmm = NULL;
4249 struct dt_insert_rec *rec = &info->lti_dt_rec;
4254 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4257 /* The stripes are supposed to be allocated in declare phase,
4258 * if there are no stripes being allocated, it will skip */
4259 if (lo->ldo_dir_stripe_count == 0) {
4260 if (lo->ldo_is_foreign) {
4261 rc = lod_sub_xattr_set(env, dt_object_child(dt), buf,
4262 XATTR_NAME_LMV, fl, th);
4269 rc = dt_attr_get(env, dt_object_child(dt), attr);
4273 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_FLAGS |
4274 LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
4275 dof->dof_type = DFT_DIR;
4277 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
4280 lmm = lmv_buf.lb_buf;
4282 OBD_ALLOC_PTR(slave_lmm);
4283 if (slave_lmm == NULL)
4286 lod_prep_slave_lmv_md(slave_lmm, lmm);
4287 slave_lmv_buf.lb_buf = slave_lmm;
4288 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
4290 rec->rec_type = S_IFDIR;
4291 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4292 struct dt_object *dto = lo->ldo_stripe[i];
4293 char *stripe_name = info->lti_key;
4294 struct lu_name *sname;
4295 struct linkea_data ldata = { NULL };
4296 struct lu_buf linkea_buf;
4298 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
4302 /* fail a remote stripe creation */
4303 if (i && CFS_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_CREATE))
4306 /* don't create stripe if:
4307 * 1. it's source stripe of migrating directory
4308 * 2. it's existed stripe of splitting directory
4310 if ((lod_is_migrating(lo) && i >= lo->ldo_dir_migrate_offset) ||
4311 (lod_is_splitting(lo) && i < lo->ldo_dir_split_offset)) {
4312 if (!dt_object_exists(dto))
4313 GOTO(out, rc = -EINVAL);
4315 dt_write_lock(env, dto, DT_TGT_CHILD);
4316 rc = lod_sub_create(env, dto, attr, NULL, dof, th);
4318 dt_write_unlock(env, dto);
4322 rc = lod_sub_ref_add(env, dto, th);
4323 dt_write_unlock(env, dto);
4327 rec->rec_fid = lu_object_fid(&dto->do_lu);
4328 rc = lod_sub_insert(env, dto,
4329 (const struct dt_rec *)rec,
4330 (const struct dt_key *)dot, th);
4335 if (!CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
4336 cfs_fail_val != i) {
4337 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
4339 slave_lmm->lmv_master_mdt_index =
4342 slave_lmm->lmv_master_mdt_index =
4345 rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
4346 XATTR_NAME_LMV, 0, th);
4351 /* don't insert stripe if it's existed stripe of splitting
4352 * directory (this directory is striped).
4353 * NB, plain directory will insert itself as the first
4356 if (lod_is_splitting(lo) && lo->ldo_dir_split_offset > 1 &&
4357 lo->ldo_dir_split_offset > i)
4360 rec->rec_fid = lu_object_fid(&dt->do_lu);
4361 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
4362 (const struct dt_key *)dotdot, th);
4366 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
4368 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4369 PFID(lu_object_fid(&dto->do_lu)), i + 1);
4371 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4372 PFID(lu_object_fid(&dto->do_lu)), i);
4374 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
4375 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
4376 sname, lu_object_fid(&dt->do_lu));
4380 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
4381 linkea_buf.lb_len = ldata.ld_leh->leh_len;
4382 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
4383 XATTR_NAME_LINK, 0, th);
4387 rec->rec_fid = lu_object_fid(&dto->do_lu);
4388 rc = lod_sub_insert(env, dt_object_child(dt),
4389 (const struct dt_rec *)rec,
4390 (const struct dt_key *)stripe_name, th);
4394 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
4399 if (!CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
4400 rc = lod_sub_xattr_set(env, dt_object_child(dt),
4401 &lmv_buf, XATTR_NAME_LMV, fl, th);
4403 if (slave_lmm != NULL)
4404 OBD_FREE_PTR(slave_lmm);
4410 * Helper function to declare/execute creation of a striped directory
4412 * Called in declare/create object path, prepare striping for a directory
4413 * and prepare defaults data striping for the objects to be created in
4414 * that directory. Notice the function calls "declaration" or "execution"
4415 * methods depending on \a declare param. This is a consequence of the
4416 * current approach while we don't have natural distributed transactions:
4417 * we basically execute non-local updates in the declare phase. So, the
4418 * arguments for the both phases are the same and this is the reason for
4419 * this function to exist.
4421 * \param[in] env execution environment
4422 * \param[in] dt object
4423 * \param[in] attr attributes the stripes will be created with
4424 * \param[in] lmu lmv_user_md if MDT indices are specified
4425 * \param[in] dof format of stripes (see OSD API description)
4426 * \param[in] th transaction handle
4427 * \param[in] declare where to call "declare" or "execute" methods
4429 * \retval 0 on success
4430 * \retval negative if failed
4432 static int lod_dir_striping_create_internal(const struct lu_env *env,
4433 struct dt_object *dt,
4434 struct lu_attr *attr,
4435 const struct lu_buf *lmu,
4436 struct dt_object_format *dof,
4440 struct lod_thread_info *info = lod_env_info(env);
4441 struct lod_object *lo = lod_dt_obj(dt);
4442 const struct lod_default_striping *lds = lo->ldo_def_striping;
4446 LASSERT(ergo(lds != NULL,
4447 lds->lds_def_striping_set ||
4448 lds->lds_dir_def_striping_set));
4450 if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
4451 lo->ldo_dir_stripe_offset)) {
4453 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
4454 int stripe_count = lo->ldo_dir_stripe_count;
4456 if (info->lti_ea_store_size < sizeof(*v1)) {
4457 rc = lod_ea_store_resize(info, sizeof(*v1));
4460 v1 = info->lti_ea_store;
4463 memset(v1, 0, sizeof(*v1));
4464 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
4465 v1->lum_stripe_count = cpu_to_le32(stripe_count);
4466 v1->lum_stripe_offset =
4467 cpu_to_le32(lo->ldo_dir_stripe_offset);
4469 info->lti_buf.lb_buf = v1;
4470 info->lti_buf.lb_len = sizeof(*v1);
4471 lmu = &info->lti_buf;
4475 rc = lod_declare_xattr_set_lmv(env, dt, attr, lmu, dof,
4478 rc = lod_xattr_set_lmv(env, dt, lmu, XATTR_NAME_LMV, 0,
4483 /* foreign LMV EA case */
4485 struct lmv_foreign_md *lfm = lmu->lb_buf;
4487 if (lfm->lfm_magic == LMV_MAGIC_FOREIGN) {
4488 rc = lod_declare_xattr_set_lmv(env, dt, attr,
4492 if (lo->ldo_is_foreign) {
4493 LASSERT(lo->ldo_foreign_lmv != NULL &&
4494 lo->ldo_foreign_lmv_size > 0);
4495 info->lti_buf.lb_buf = lo->ldo_foreign_lmv;
4496 info->lti_buf.lb_len = lo->ldo_foreign_lmv_size;
4497 lmu = &info->lti_buf;
4498 rc = lod_xattr_set_lmv(env, dt, lmu,
4499 XATTR_NAME_LMV, 0, th);
4504 /* Transfer default LMV striping from the parent */
4505 if (lds != NULL && lds->lds_dir_def_striping_set &&
4506 lds->lds_dir_def_max_inherit != LMV_INHERIT_END &&
4507 lds->lds_dir_def_max_inherit != LMV_INHERIT_NONE &&
4508 !(LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
4509 lds->lds_dir_def_stripe_offset) &&
4510 le32_to_cpu(lds->lds_dir_def_hash_type) !=
4511 LMV_HASH_TYPE_UNKNOWN)) {
4512 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
4514 if (info->lti_ea_store_size < sizeof(*v1)) {
4515 rc = lod_ea_store_resize(info, sizeof(*v1));
4518 v1 = info->lti_ea_store;
4521 memset(v1, 0, sizeof(*v1));
4522 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
4523 v1->lum_stripe_count =
4524 cpu_to_le32(lds->lds_dir_def_stripe_count);
4525 v1->lum_stripe_offset =
4526 cpu_to_le32(lds->lds_dir_def_stripe_offset);
4528 cpu_to_le32(lds->lds_dir_def_hash_type);
4529 v1->lum_max_inherit =
4530 lmv_inherit_next(lds->lds_dir_def_max_inherit);
4531 v1->lum_max_inherit_rr =
4532 lmv_inherit_rr_next(lds->lds_dir_def_max_inherit_rr);
4534 info->lti_buf.lb_buf = v1;
4535 info->lti_buf.lb_len = sizeof(*v1);
4537 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4538 XATTR_NAME_DEFAULT_LMV,
4541 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
4543 XATTR_NAME_DEFAULT_LMV, 0,
4549 /* Transfer default LOV striping from the parent */
4550 if (lds != NULL && lds->lds_def_striping_set &&
4551 lds->lds_def_comp_cnt != 0) {
4552 struct lov_mds_md *lmm;
4553 int lmm_size = lod_comp_md_size(lo, true);
4555 if (info->lti_ea_store_size < lmm_size) {
4556 rc = lod_ea_store_resize(info, lmm_size);
4560 lmm = info->lti_ea_store;
4562 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
4566 info->lti_buf.lb_buf = lmm;
4567 info->lti_buf.lb_len = lmm_size;
4570 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4571 XATTR_NAME_LOV, 0, th);
4573 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4574 XATTR_NAME_LOV, 0, th);
4579 /* ldo_def_striping is not allocated, clear after use, in case directory
4580 * layout is changed later.
4583 lo->ldo_def_striping = NULL;
4588 static int lod_declare_dir_striping_create(const struct lu_env *env,
4589 struct dt_object *dt,
4590 struct lu_attr *attr,
4592 struct dt_object_format *dof,
4595 return lod_dir_striping_create_internal(env, dt, attr, lmu, dof, th,
4599 static int lod_dir_striping_create(const struct lu_env *env,
4600 struct dt_object *dt,
4601 struct lu_attr *attr,
4602 struct dt_object_format *dof,
4605 return lod_dir_striping_create_internal(env, dt, attr, NULL, dof, th,
4610 * Make LOV EA for striped object.
4612 * Generate striping information and store it in the LOV EA of the given
4613 * object. The caller must ensure nobody else is calling the function
4614 * against the object concurrently. The transaction must be started.
4615 * FLDB service must be running as well; it's used to map FID to the target,
4616 * which is stored in LOV EA.
4618 * \param[in] env execution environment for this thread
4619 * \param[in] lo LOD object
4620 * \param[in] th transaction handle
4622 * \retval 0 if LOV EA is stored successfully
4623 * \retval negative error number on failure
4625 static int lod_generate_and_set_lovea(const struct lu_env *env,
4626 struct lod_object *lo,
4629 struct lod_thread_info *info = lod_env_info(env);
4630 struct dt_object *next = dt_object_child(&lo->ldo_obj);
4631 struct lov_mds_md_v1 *lmm;
4637 if (lo->ldo_comp_cnt == 0 && !lo->ldo_is_foreign) {
4638 lod_striping_free_nolock(env, lo);
4639 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
4643 lmm_size = lod_comp_md_size(lo, false);
4644 if (info->lti_ea_store_size < lmm_size) {
4645 rc = lod_ea_store_resize(info, lmm_size);
4649 lmm = info->lti_ea_store;
4651 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
4655 info->lti_buf.lb_buf = lmm;
4656 info->lti_buf.lb_len = lmm_size;
4657 rc = lod_sub_xattr_set(env, next, &info->lti_buf,
4658 XATTR_NAME_LOV, 0, th);
4662 static __u32 lod_gen_component_id(struct lod_object *lo,
4663 int mirror_id, int comp_idx);
4666 * Repeat an existing component
4668 * Creates a new layout by replicating an existing component. Uses striping
4669 * policy from previous component as a template for the striping for the new
4672 * New component starts with zero length, will be extended (or removed) before
4673 * returning layout to client.
4675 * NB: Reallocates layout components array (lo->ldo_comp_entries), invalidating
4676 * any pre-existing pointers to components. Handle with care.
4678 * \param[in] env execution environment for this thread
4679 * \param[in,out] lo object to update the layout of
4680 * \param[in] index index of component to copy
4682 * \retval 0 on success
4683 * \retval negative errno on error
4685 static int lod_layout_repeat_comp(const struct lu_env *env,
4686 struct lod_object *lo, int index)
4688 struct lod_layout_component *lod_comp;
4689 struct lod_layout_component *new_comp = NULL;
4690 struct lod_layout_component *comp_array;
4691 int rc = 0, i, new_cnt = lo->ldo_comp_cnt + 1;
4696 lod_comp = &lo->ldo_comp_entries[index];
4697 LASSERT(lod_comp_inited(lod_comp) && lod_comp->llc_id != LCME_ID_INVAL);
4699 CDEBUG(D_LAYOUT, "repeating component %d\n", index);
4701 OBD_ALLOC_PTR_ARRAY(comp_array, new_cnt);
4702 if (comp_array == NULL)
4703 GOTO(out, rc = -ENOMEM);
4705 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4706 memcpy(&comp_array[i + offset], &lo->ldo_comp_entries[i],
4707 sizeof(*comp_array));
4709 /* Duplicate this component in to the next slot */
4711 new_comp = &comp_array[i + 1];
4712 memcpy(&comp_array[i + 1], &lo->ldo_comp_entries[i],
4713 sizeof(*comp_array));
4714 /* We must now skip this new component when copying */
4719 /* Set up copied component */
4720 new_comp->llc_flags &= ~LCME_FL_INIT;
4721 new_comp->llc_stripe = NULL;
4722 new_comp->llc_stripes_allocated = 0;
4723 new_comp->llc_ost_indices = NULL;
4724 new_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4725 /* for uninstantiated components, layout gen stores default stripe
4727 new_comp->llc_layout_gen = lod_comp->llc_stripe_offset;
4728 /* This makes the repeated component zero-length, placed at the end of
4729 * the preceding component */
4730 new_comp->llc_extent.e_start = new_comp->llc_extent.e_end;
4731 new_comp->llc_timestamp = lod_comp->llc_timestamp;
4732 new_comp->llc_pool = NULL;
4734 rc = lod_set_pool(&new_comp->llc_pool, lod_comp->llc_pool);
4738 if (new_comp->llc_ostlist.op_array) {
4739 __u32 *op_array = NULL;
4741 OBD_ALLOC(op_array, new_comp->llc_ostlist.op_size);
4743 GOTO(out, rc = -ENOMEM);
4744 memcpy(op_array, &new_comp->llc_ostlist.op_array,
4745 new_comp->llc_ostlist.op_size);
4746 new_comp->llc_ostlist.op_array = op_array;
4749 OBD_FREE_PTR_ARRAY(lo->ldo_comp_entries, lo->ldo_comp_cnt);
4750 lo->ldo_comp_entries = comp_array;
4751 lo->ldo_comp_cnt = new_cnt;
4753 /* Generate an id for the new component */
4754 mirror_id = mirror_id_of(new_comp->llc_id);
4755 new_comp->llc_id = LCME_ID_INVAL;
4756 new_comp->llc_id = lod_gen_component_id(lo, mirror_id, index + 1);
4757 if (new_comp->llc_id == LCME_ID_INVAL)
4758 GOTO(out, rc = -ERANGE);
4763 OBD_FREE_PTR_ARRAY(comp_array, new_cnt);
4768 static int lod_layout_data_init(struct lod_thread_info *info, __u32 comp_cnt)
4772 /* clear memory region that will be used for layout change */
4773 memset(&info->lti_layout_attr, 0, sizeof(struct lu_attr));
4774 info->lti_count = 0;
4776 if (info->lti_comp_size >= comp_cnt)
4779 if (info->lti_comp_size > 0) {
4780 OBD_FREE_PTR_ARRAY(info->lti_comp_idx, info->lti_comp_size);
4781 info->lti_comp_size = 0;
4784 OBD_ALLOC_PTR_ARRAY(info->lti_comp_idx, comp_cnt);
4785 if (!info->lti_comp_idx)
4788 info->lti_comp_size = comp_cnt;
4793 * Prepare new layout minus deleted components
4795 * Removes components marked for deletion (LCME_ID_INVAL) by copying to a new
4796 * layout and skipping those components. Removes stripe objects if any exist.
4799 * Reallocates layout components array (lo->ldo_comp_entries), invalidating
4800 * any pre-existing pointers to components.
4802 * Caller is responsible for updating mirror end (ldo_mirror[].lme_end).
4804 * \param[in] env execution environment for this thread
4805 * \param[in,out] lo object to update the layout of
4806 * \param[in] th transaction handle for this operation
4808 * \retval # of components deleted
4809 * \retval negative errno on error
4811 static int lod_layout_del_prep_layout(const struct lu_env *env,
4812 struct lod_object *lo,
4815 struct lod_layout_component *lod_comp;
4816 struct lod_thread_info *info = lod_env_info(env);
4817 int rc = 0, i, j, deleted = 0;
4821 LASSERT(lo->ldo_is_composite);
4822 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
4824 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
4828 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4829 lod_comp = &lo->ldo_comp_entries[i];
4831 if (lod_comp->llc_id != LCME_ID_INVAL) {
4832 /* Build array of things to keep */
4833 info->lti_comp_idx[info->lti_count++] = i;
4837 lod_obj_set_pool(lo, i, NULL);
4838 if (lod_comp->llc_ostlist.op_array) {
4839 OBD_FREE(lod_comp->llc_ostlist.op_array,
4840 lod_comp->llc_ostlist.op_size);
4841 lod_comp->llc_ostlist.op_array = NULL;
4842 lod_comp->llc_ostlist.op_size = 0;
4846 CDEBUG(D_LAYOUT, "deleting comp %d, left %d\n", i,
4847 lo->ldo_comp_cnt - deleted);
4849 /* No striping info for this component */
4850 if (lod_comp->llc_stripe == NULL)
4853 LASSERT(lod_comp->llc_stripe_count > 0);
4854 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4855 struct dt_object *obj = lod_comp->llc_stripe[j];
4860 /* components which are not init have no sub objects
4862 if (lod_comp_inited(lod_comp)) {
4863 rc = lod_sub_destroy(env, obj, th);
4868 lu_object_put(env, &obj->do_lu);
4869 lod_comp->llc_stripe[j] = NULL;
4871 OBD_FREE_PTR_ARRAY(lod_comp->llc_stripe,
4872 lod_comp->llc_stripes_allocated);
4873 lod_comp->llc_stripe = NULL;
4874 OBD_FREE_PTR_ARRAY(lod_comp->llc_ost_indices,
4875 lod_comp->llc_stripes_allocated);
4876 lod_comp->llc_ost_indices = NULL;
4877 lod_comp->llc_stripes_allocated = 0;
4880 /* info->lti_count has the amount of left components */
4881 LASSERTF(info->lti_count >= 0 && info->lti_count < lo->ldo_comp_cnt,
4882 "left = %d, lo->ldo_comp_cnt %d\n", (int)info->lti_count,
4883 (int)lo->ldo_comp_cnt);
4885 if (info->lti_count > 0) {
4886 struct lod_layout_component *comp_array;
4888 OBD_ALLOC_PTR_ARRAY(comp_array, info->lti_count);
4889 if (comp_array == NULL)
4890 GOTO(out, rc = -ENOMEM);
4892 for (i = 0; i < info->lti_count; i++) {
4893 memcpy(&comp_array[i],
4894 &lo->ldo_comp_entries[info->lti_comp_idx[i]],
4895 sizeof(*comp_array));
4898 OBD_FREE_PTR_ARRAY(lo->ldo_comp_entries, lo->ldo_comp_cnt);
4899 lo->ldo_comp_entries = comp_array;
4900 lo->ldo_comp_cnt = info->lti_count;
4902 lod_free_comp_entries(lo);
4907 return rc ? rc : deleted;
4911 * Delete layout component(s)
4913 * This function sets up the layout data in the env and does the setattrs
4914 * required to write out the new layout. The layout itself is modified in
4915 * lod_layout_del_prep_layout.
4917 * \param[in] env execution environment for this thread
4918 * \param[in] dt object
4919 * \param[in] th transaction handle
4921 * \retval 0 on success
4922 * \retval negative error number on failure
4924 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
4927 struct lod_object *lo = lod_dt_obj(dt);
4928 struct dt_object *next = dt_object_child(dt);
4929 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4932 LASSERT(lo->ldo_mirror_count == 1);
4934 mutex_lock(&lo->ldo_layout_mutex);
4936 rc = lod_layout_del_prep_layout(env, lo, th);
4940 /* Only do this if we didn't delete all components */
4941 if (lo->ldo_comp_cnt > 0) {
4942 lo->ldo_mirrors[0].lme_end = lo->ldo_comp_cnt - 1;
4943 lod_obj_inc_layout_gen(lo);
4946 LASSERT(dt_object_exists(dt));
4947 rc = dt_attr_get(env, next, attr);
4951 if (attr->la_size > 0) {
4953 attr->la_valid = LA_SIZE;
4954 rc = lod_sub_attr_set(env, next, attr, th);
4959 rc = lod_generate_and_set_lovea(env, lo, th);
4963 lod_striping_free_nolock(env, lo);
4965 mutex_unlock(&lo->ldo_layout_mutex);
4972 * Implementation of dt_object_operations::do_xattr_set.
4974 * Sets specified extended attribute on the object. Three types of EAs are
4976 * LOV EA - stores striping for a regular file or default striping (when set
4978 * LMV EA - stores a marker for the striped directories
4979 * DMV EA - stores default directory striping
4981 * When striping is applied to a non-striped existing object (this is called
4982 * late striping), then LOD notices the caller wants to turn the object into a
4983 * striped one. The stripe objects are created and appropriate EA is set:
4984 * LOV EA storing all the stripes directly or LMV EA storing just a small header
4985 * with striping configuration.
4987 * \see dt_object_operations::do_xattr_set() in the API description for details.
4989 static int lod_xattr_set(const struct lu_env *env,
4990 struct dt_object *dt, const struct lu_buf *buf,
4991 const char *name, int fl, struct thandle *th)
4993 struct dt_object *next = dt_object_child(dt);
4994 struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
4995 struct lod_object *lo = lod_dt_obj(dt);
4996 struct lod_obj_stripe_cb_data data = { {0} };
5001 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
5002 !strcmp(name, XATTR_NAME_LMV)) {
5004 case LU_XATTR_CREATE:
5005 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
5008 case LU_XATTR_REPLACE:
5009 rc = lod_dir_layout_set(env, dt, buf, fl, th);
5016 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
5017 strcmp(name, XATTR_NAME_LOV) == 0) {
5018 rc = lod_xattr_set_default_lov_on_dir(env, dt, buf, name, fl,
5021 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
5022 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
5024 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
5027 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
5028 (strcmp(name, XATTR_NAME_LOV) == 0 ||
5029 strcmp(name, XATTR_LUSTRE_LOV) == 0 ||
5030 allowed_lustre_lov(name))) {
5031 /* in case of lov EA swap, just set it
5032 * if not, it is a replay so check striping match what we
5033 * already have during req replay, declare_xattr_set()
5034 * defines striping, then create() does the work */
5035 if (fl & LU_XATTR_REPLACE) {
5036 /* free stripes, then update disk */
5037 lod_striping_free(env, lod_dt_obj(dt));
5039 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
5040 } else if (fl & LU_XATTR_SPLIT) {
5041 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
5045 rc = lod_striping_reload(env, lo, buf, LVF_ALL_STALE);
5049 if (lo->ldo_mirror_count > 1 &&
5050 layout_attr->la_valid & LA_LAYOUT_VERSION) {
5052 layout_attr->la_layout_version =
5054 data.locd_attr = layout_attr;
5055 data.locd_declare = false;
5056 data.locd_stripe_cb =
5057 lod_obj_stripe_attr_set_cb;
5058 rc = lod_obj_for_each_stripe(env, lo, th,
5063 } else if (fl & LU_XATTR_PURGE) {
5064 rc = lod_layout_purge(env, dt, buf, th);
5065 } else if (dt_object_remote(dt)) {
5066 /* This only happens during migration, see
5067 * mdd_migrate_create(), in which Master MDT will
5068 * create a remote target object, and only set
5069 * (migrating) stripe EA on the remote object,
5070 * and does not need creating each stripes. */
5071 rc = lod_sub_xattr_set(env, next, buf, name,
5073 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
5074 /* delete component(s) */
5075 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
5076 rc = lod_layout_del(env, dt, th);
5079 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
5080 * it's going to create create file with specified
5081 * component(s), the striping must have not being
5082 * cached in this case;
5084 * Otherwise, it's going to add/change component(s) to
5085 * an existing file, the striping must have been cached
5088 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
5089 !strcmp(name, XATTR_NAME_LOV),
5090 !lod_dt_obj(dt)->ldo_comp_cached));
5092 rc = lod_striped_create(env, dt, NULL, NULL, th);
5096 if (fl & LU_XATTR_MERGE && lo->ldo_mirror_count > 1 &&
5097 layout_attr->la_valid & LA_LAYOUT_VERSION) {
5098 /* mirror merge exec phase */
5099 layout_attr->la_layout_version =
5101 data.locd_attr = layout_attr;
5102 data.locd_declare = false;
5103 data.locd_stripe_cb =
5104 lod_obj_stripe_attr_set_cb;
5105 rc = lod_obj_for_each_stripe(env, lo, th,
5112 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
5113 rc = lod_replace_parent_fid(env, dt, buf, th, false);
5118 /* then all other xattr */
5119 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
5125 * Implementation of dt_object_operations::do_declare_xattr_del.
5127 * \see dt_object_operations::do_declare_xattr_del() in the API description
5130 static int lod_declare_xattr_del(const struct lu_env *env,
5131 struct dt_object *dt, const char *name,
5134 struct lod_object *lo = lod_dt_obj(dt);
5135 struct dt_object *next = dt_object_child(dt);
5140 rc = lod_sub_declare_xattr_del(env, next, name, th);
5144 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
5147 /* NB: don't delete stripe LMV, because when we do this, normally we
5148 * will remove stripes, besides, if directory LMV is corrupt, this will
5149 * prevent deleting its LMV and fixing it (via LFSCK).
5151 if (!strcmp(name, XATTR_NAME_LMV))
5154 rc = lod_striping_load(env, lo);
5158 if (lo->ldo_dir_stripe_count == 0)
5161 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5162 struct dt_object *dto = lo->ldo_stripe[i];
5167 if (!dt_object_exists(dto))
5170 rc = lod_sub_declare_xattr_del(env, dto, name, th);
5179 * Implementation of dt_object_operations::do_xattr_del.
5181 * If EA storing a regular striping is being deleted, then release
5182 * all the references to the stripe objects in core.
5184 * \see dt_object_operations::do_xattr_del() in the API description for details.
5186 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
5187 const char *name, struct thandle *th)
5193 if (!strcmp(name, XATTR_NAME_LOV) || !strcmp(name, XATTR_NAME_LMV))
5194 lod_striping_free(env, lod_dt_obj(dt));
5196 rc = lod_xattr_del_internal(env, dt, name, th);
5202 * Implementation of dt_object_operations::do_xattr_list.
5204 * \see dt_object_operations::do_xattr_list() in the API description
5207 static int lod_xattr_list(const struct lu_env *env,
5208 struct dt_object *dt, const struct lu_buf *buf)
5210 return dt_xattr_list(env, dt_object_child(dt), buf);
5213 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
5215 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
5219 * Copy OST list from layout provided by user.
5221 * \param[in] lod_comp layout_component to be filled
5222 * \param[in] v3 LOV EA V3 user data
5224 * \retval 0 on success
5225 * \retval negative if failed
5227 int lod_comp_copy_ost_lists(struct lod_layout_component *lod_comp,
5228 struct lov_user_md_v3 *v3)
5234 if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
5235 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
5237 if (lod_comp->llc_ostlist.op_array) {
5238 if (lod_comp->llc_ostlist.op_size >=
5239 v3->lmm_stripe_count * sizeof(__u32)) {
5240 lod_comp->llc_ostlist.op_count =
5241 v3->lmm_stripe_count;
5244 OBD_FREE(lod_comp->llc_ostlist.op_array,
5245 lod_comp->llc_ostlist.op_size);
5248 /* copy ost list from lmm */
5249 lod_comp->llc_ostlist.op_count = v3->lmm_stripe_count;
5250 lod_comp->llc_ostlist.op_size = v3->lmm_stripe_count * sizeof(__u32);
5251 OBD_ALLOC(lod_comp->llc_ostlist.op_array,
5252 lod_comp->llc_ostlist.op_size);
5253 if (!lod_comp->llc_ostlist.op_array)
5256 for (j = 0; j < v3->lmm_stripe_count; j++) {
5257 lod_comp->llc_ostlist.op_array[j] =
5258 v3->lmm_objects[j].l_ost_idx;
5266 * Get default striping.
5268 * \param[in] env execution environment
5269 * \param[in] lo object
5270 * \param[out] lds default striping
5272 * \retval 0 on success
5273 * \retval negative if failed
5275 static int lod_get_default_lov_striping(const struct lu_env *env,
5276 struct lod_object *lo,
5277 struct lod_default_striping *lds,
5278 struct dt_allocation_hint *dah)
5280 struct lod_thread_info *info = lod_env_info(env);
5281 struct lov_user_md_v1 *v1 = NULL;
5282 struct lov_user_md_v3 *v3 = NULL;
5283 struct lov_comp_md_v1 *lcm = NULL;
5285 int append_stripe_count = dah != NULL ? dah->dah_append_stripe_count : 0;
5286 const char *append_pool = (dah != NULL &&
5287 dah->dah_append_pool != NULL &&
5288 dah->dah_append_pool[0] != '\0') ?
5289 dah->dah_append_pool : NULL;
5290 __u16 entry_count = 1;
5291 __u16 mirror_count = 0;
5292 bool want_composite = false;
5297 lds->lds_def_striping_set = 0;
5299 rc = lod_get_lov_ea(env, lo);
5303 if (rc < (typeof(rc))sizeof(struct lov_user_md))
5306 magic = *(__u32 *)info->lti_ea_store;
5307 if (magic == __swab32(LOV_USER_MAGIC_V1)) {
5308 lustre_swab_lov_user_md_v1(info->lti_ea_store);
5309 } else if (magic == __swab32(LOV_USER_MAGIC_V3)) {
5310 lustre_swab_lov_user_md_v3(info->lti_ea_store);
5311 } else if (magic == __swab32(LOV_USER_MAGIC_SPECIFIC)) {
5312 v3 = (struct lov_user_md_v3 *)info->lti_ea_store;
5313 lustre_swab_lov_user_md_v3(v3);
5314 lustre_swab_lov_user_md_objects(v3->lmm_objects,
5315 v3->lmm_stripe_count);
5316 } else if (magic == __swab32(LOV_USER_MAGIC_COMP_V1) ||
5317 magic == __swab32(LOV_USER_MAGIC_SEL)) {
5318 lustre_swab_lov_comp_md_v1(info->lti_ea_store);
5324 case LOV_USER_MAGIC_SPECIFIC:
5325 v1 = info->lti_ea_store;
5327 case LOV_MAGIC_COMP_V1:
5329 lcm = info->lti_ea_store;
5330 entry_count = lcm->lcm_entry_count;
5331 if (entry_count == 0)
5334 mirror_count = lcm->lcm_mirror_count + 1;
5335 want_composite = true;
5341 if (append_stripe_count != 0 || append_pool != NULL) {
5344 want_composite = false;
5347 /* realloc default comp entries if necessary */
5348 rc = lod_def_striping_comp_resize(lds, entry_count);
5352 lds->lds_def_comp_cnt = entry_count;
5353 lds->lds_def_striping_is_composite = want_composite;
5354 lds->lds_def_mirror_cnt = mirror_count;
5356 for (i = 0; i < entry_count; i++) {
5357 struct lod_layout_component *llc = &lds->lds_def_comp_entries[i];
5361 * reset llc values, llc_stripes is always NULL in the
5362 * default striping template, llc_pool will be reset
5363 * later below using lod_set_pool().
5365 * XXX At this point llc_pool may point to valid (!)
5366 * kmalloced strings from previous RPCs.
5368 memset(llc, 0, offsetof(typeof(*llc), llc_pool));
5371 v1 = (struct lov_user_md *)((char *)lcm +
5372 lcm->lcm_entries[i].lcme_offset);
5374 if (want_composite) {
5375 llc->llc_extent = lcm->lcm_entries[i].lcme_extent;
5376 /* We only inherit certain flags from the layout */
5377 llc->llc_flags = lcm->lcm_entries[i].lcme_flags &
5378 LCME_TEMPLATE_FLAGS;
5382 CDEBUG(D_LAYOUT, DFID" magic = %#08x, pattern = %#x, stripe_count = %hu, stripe_size = %u, stripe_offset = %hu, append_pool = '%s', append_stripe_count = %d\n",
5383 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
5386 v1->lmm_stripe_count,
5387 v1->lmm_stripe_size,
5388 v1->lmm_stripe_offset,
5390 append_stripe_count);
5392 if (!lov_pattern_supported(v1->lmm_pattern) &&
5393 !(v1->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
5394 lod_free_def_comp_entries(lds);
5398 llc->llc_stripe_count = v1->lmm_stripe_count;
5399 llc->llc_stripe_size = v1->lmm_stripe_size;
5400 llc->llc_stripe_offset = v1->lmm_stripe_offset;
5401 llc->llc_pattern = v1->lmm_pattern;
5403 if (append_stripe_count != 0 || append_pool != NULL)
5404 llc->llc_pattern = LOV_PATTERN_RAID0;
5406 if (append_stripe_count != 0)
5407 llc->llc_stripe_count = append_stripe_count;
5410 if (append_pool != NULL) {
5412 } else if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
5413 /* XXX: sanity check here */
5414 v3 = (struct lov_user_md_v3 *)v1;
5415 if (v3->lmm_pool_name[0] != '\0')
5416 pool = v3->lmm_pool_name;
5419 lod_set_pool(&llc->llc_pool, pool);
5421 if (v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC &&
5422 append_stripe_count == 0 &&
5423 append_pool == NULL) {
5424 v3 = (struct lov_user_md_v3 *)v1;
5425 rc = lod_comp_copy_ost_lists(llc, v3);
5428 } else if (llc->llc_ostlist.op_array &&
5429 llc->llc_ostlist.op_count) {
5430 for (j = 0; j < llc->llc_ostlist.op_count; j++)
5431 llc->llc_ostlist.op_array[j] = -1;
5432 llc->llc_ostlist.op_count = 0;
5436 lds->lds_def_striping_set = 1;
5440 static inline void lod_lum2lds(struct lod_default_striping *lds,
5441 const struct lmv_user_md *lum)
5443 lds->lds_dir_def_stripe_count = le32_to_cpu(lum->lum_stripe_count);
5444 lds->lds_dir_def_stripe_offset = le32_to_cpu(lum->lum_stripe_offset);
5445 lds->lds_dir_def_hash_type = le32_to_cpu(lum->lum_hash_type);
5446 lds->lds_dir_def_max_inherit = lum->lum_max_inherit;
5447 lds->lds_dir_def_max_inherit_rr = lum->lum_max_inherit_rr;
5448 lds->lds_dir_def_striping_set = 1;
5452 * Get default directory striping.
5454 * \param[in] env execution environment
5455 * \param[in] lo object
5456 * \param[out] lds default striping
5458 * \retval 0 on success
5459 * \retval negative if failed
5461 static int lod_get_default_lmv_striping(const struct lu_env *env,
5462 struct lod_object *lo,
5463 struct lod_default_striping *lds)
5465 struct lmv_user_md *lmu;
5468 lds->lds_dir_def_striping_set = 0;
5470 rc = lod_get_default_lmv_ea(env, lo);
5474 if (rc >= (int)sizeof(*lmu)) {
5475 struct lod_thread_info *info = lod_env_info(env);
5477 lmu = info->lti_ea_store;
5478 lod_lum2lds(lds, lmu);
5485 * Get default striping in the object.
5487 * Get object default striping and default directory striping.
5489 * \param[in] env execution environment
5490 * \param[in] lo object
5491 * \param[out] lds default striping
5493 * \retval 0 on success
5494 * \retval negative if failed
5496 static int lod_get_default_striping(const struct lu_env *env,
5497 struct lod_object *lo,
5498 struct dt_allocation_hint *ah,
5499 struct lod_default_striping *lds)
5503 rc = lod_get_default_lov_striping(env, lo, lds, NULL);
5504 if (lds->lds_def_striping_set) {
5505 struct lod_thread_info *info = lod_env_info(env);
5506 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5508 rc = lod_verify_striping(env, d, lo, &info->lti_buf, false);
5510 lds->lds_def_striping_set = 0;
5513 if (ah->dah_eadata_is_dmv) {
5514 lod_lum2lds(lds, ah->dah_eadata);
5515 } else if (ah->dah_dmv_imp_inherit) {
5516 lds->lds_dir_def_striping_set = 0;
5518 rc1 = lod_get_default_lmv_striping(env, lo, lds);
5519 if (rc == 0 && rc1 < 0)
5527 * Apply default striping on object.
5529 * If object striping pattern is not set, set to the one in default striping.
5530 * The default striping is from parent or fs.
5532 * \param[in] lo new object
5533 * \param[in] lds default striping
5534 * \param[in] mode new object's mode
5536 static void lod_striping_from_default(struct lod_object *lo,
5537 const struct lod_default_striping *lds,
5540 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5543 if (lds->lds_def_striping_set && S_ISREG(mode)) {
5544 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
5546 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
5547 lds->lds_def_comp_cnt);
5551 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
5552 if (lds->lds_def_mirror_cnt > 1)
5553 lo->ldo_flr_state = LCM_FL_RDONLY;
5555 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5556 struct lod_layout_component *obj_comp =
5557 &lo->ldo_comp_entries[i];
5558 struct lod_layout_component *def_comp =
5559 &lds->lds_def_comp_entries[i];
5562 "inherit "DFID" file layout from default: flags=%#x size=%u nr=%u offset=%u pattern=%#x pool=%s\n",
5563 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
5564 def_comp->llc_flags,
5565 def_comp->llc_stripe_size,
5566 def_comp->llc_stripe_count,
5567 def_comp->llc_stripe_offset,
5568 def_comp->llc_pattern,
5569 def_comp->llc_pool ?: "");
5571 *obj_comp = *def_comp;
5572 if (def_comp->llc_pool != NULL) {
5573 /* pointer was copied from def_comp */
5574 obj_comp->llc_pool = NULL;
5575 lod_obj_set_pool(lo, i, def_comp->llc_pool);
5579 if (def_comp->llc_ostlist.op_array &&
5580 def_comp->llc_ostlist.op_count) {
5581 OBD_ALLOC(obj_comp->llc_ostlist.op_array,
5582 obj_comp->llc_ostlist.op_size);
5583 if (!obj_comp->llc_ostlist.op_array)
5585 memcpy(obj_comp->llc_ostlist.op_array,
5586 def_comp->llc_ostlist.op_array,
5587 obj_comp->llc_ostlist.op_size);
5588 } else if (def_comp->llc_ostlist.op_array) {
5589 obj_comp->llc_ostlist.op_array = NULL;
5593 * Don't initialize these fields for plain layout
5594 * (v1/v3) here, they are inherited in the order of
5595 * 'parent' -> 'fs default (root)' -> 'global default
5596 * values for stripe_count & stripe_size'.
5598 * see lod_ah_init().
5600 if (!lo->ldo_is_composite)
5603 lod_adjust_stripe_info(obj_comp, desc, 0);
5605 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
5606 if (lo->ldo_dir_stripe_count == 0)
5607 lo->ldo_dir_stripe_count =
5608 lds->lds_dir_def_stripe_count;
5609 if (lo->ldo_dir_stripe_offset == -1)
5610 lo->ldo_dir_stripe_offset =
5611 lds->lds_dir_def_stripe_offset;
5612 if (lo->ldo_dir_hash_type == LMV_HASH_TYPE_UNKNOWN)
5613 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
5616 "inherit "DFID" dir layout from default: count=%hu offset=%u hash_type=%x\n",
5617 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
5618 lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
5619 lo->ldo_dir_hash_type);
5623 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root,
5624 const char *append_pool)
5626 struct lod_layout_component *lod_comp;
5628 if (lo->ldo_comp_cnt == 0)
5631 if (lo->ldo_is_composite)
5634 lod_comp = &lo->ldo_comp_entries[0];
5636 if (lod_comp->llc_stripe_count <= 0 ||
5637 lod_comp->llc_stripe_size <= 0)
5640 if (from_root && (lod_comp->llc_pool == NULL ||
5641 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
5644 if (append_pool && append_pool[0])
5651 * Implementation of dt_object_operations::do_ah_init.
5653 * This method is used to make a decision on the striping configuration for the
5654 * object being created. It can be taken from the \a parent object if it exists,
5655 * or filesystem's default. The resulting configuration (number of stripes,
5656 * stripe size/offset, pool name, hash_type, etc.) is stored in the object
5657 * itself and will be used by the methods like ->doo_declare_create().
5659 * \see dt_object_operations::do_ah_init() in the API description for details.
5661 static void lod_ah_init(const struct lu_env *env,
5662 struct dt_allocation_hint *ah,
5663 struct dt_object *parent,
5664 struct dt_object *child,
5667 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
5668 struct lod_thread_info *info = lod_env_info(env);
5669 struct lod_default_striping *lds = lod_lds_buf_get(env);
5670 struct dt_object *nextp = NULL;
5671 struct dt_object *nextc;
5672 struct lod_object *lp = NULL;
5673 struct lod_object *lc;
5674 struct lov_desc *desc;
5675 struct lod_layout_component *lod_comp;
5681 if (ah->dah_append_stripe_count == -1)
5682 ah->dah_append_stripe_count =
5683 d->lod_ost_descs.ltd_lov_desc.ld_tgt_count;
5685 if (likely(parent)) {
5686 nextp = dt_object_child(parent);
5687 lp = lod_dt_obj(parent);
5690 nextc = dt_object_child(child);
5691 lc = lod_dt_obj(child);
5693 LASSERT(!lod_obj_is_striped(child));
5694 /* default layout template may have been set on the regular file
5695 * when this is called from mdd_create_data() */
5696 if (S_ISREG(child_mode))
5697 lod_free_comp_entries(lc);
5699 if (!dt_object_exists(nextc))
5700 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
5702 if (S_ISDIR(child_mode)) {
5703 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
5705 /* other default values are 0 */
5706 lc->ldo_dir_stripe_offset = -1;
5708 /* no default striping configuration is needed for
5711 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5712 le32_to_cpu(lum1->lum_magic) == LMV_MAGIC_FOREIGN) {
5713 lc->ldo_is_foreign = true;
5714 /* keep stripe_count 0 and stripe_offset -1 */
5715 CDEBUG(D_INFO, "no default striping for foreign dir\n");
5719 if (likely(lp != NULL))
5720 lod_get_default_striping(env, lp, ah, lds);
5722 /* It should always honour the specified stripes */
5723 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
5724 * will have old magic. In this case, we should ignore the
5725 * stripe count and try to create dir by default stripe.
5727 if (ah->dah_eadata && ah->dah_eadata_len &&
5728 !ah->dah_eadata_is_dmv &&
5729 (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
5730 le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
5731 lc->ldo_dir_stripe_count =
5732 le32_to_cpu(lum1->lum_stripe_count);
5733 lc->ldo_dir_stripe_offset =
5734 le32_to_cpu(lum1->lum_stripe_offset);
5735 lc->ldo_dir_hash_type =
5736 le32_to_cpu(lum1->lum_hash_type);
5738 "set dirstripe: count %hu, offset %d, hash %x\n",
5739 lc->ldo_dir_stripe_count,
5740 (int)lc->ldo_dir_stripe_offset,
5741 lc->ldo_dir_hash_type);
5743 if (d->lod_mdt_descs.ltd_lmv_desc.ld_active_tgt_count &&
5744 lc->ldo_dir_stripe_count < 2 &&
5745 lum1->lum_max_inherit != LMV_INHERIT_NONE) {
5746 /* when filesystem-wide default LMV is set, dirs
5747 * will be created on MDT by space usage, but if
5748 * dir is created with "lfs mkdir -c 1 ...", its
5749 * subdirs should be kept on the same MDT. To
5750 * guarantee this, set default LMV for such dir.
5752 lds->lds_dir_def_stripe_count =
5753 le32_to_cpu(lum1->lum_stripe_count);
5754 /* if "-1" stripe offset is set, save current
5755 * MDT index in default LMV.
5757 if (le32_to_cpu(lum1->lum_stripe_offset) ==
5759 lds->lds_dir_def_stripe_offset =
5760 lod2lu_dev(d)->ld_site->ld_seq_site->ss_node_id;
5762 lds->lds_dir_def_stripe_offset =
5763 le32_to_cpu(lum1->lum_stripe_offset);
5764 lds->lds_dir_def_hash_type =
5765 le32_to_cpu(lum1->lum_hash_type);
5766 lds->lds_dir_def_max_inherit =
5767 lum1->lum_max_inherit;
5768 /* it will be decreased by 1 later in setting */
5769 if (lum1->lum_max_inherit >= LMV_INHERIT_END &&
5770 lum1->lum_max_inherit < LMV_INHERIT_MAX)
5771 lds->lds_dir_def_max_inherit++;
5772 lds->lds_dir_def_max_inherit_rr =
5773 lum1->lum_max_inherit_rr;
5774 lds->lds_dir_def_striping_set = 1;
5775 /* don't inherit LOV from ROOT */
5776 if (lds->lds_def_striping_set &&
5777 fid_is_root(lod_object_fid(lp)))
5778 lds->lds_def_striping_set = 0;
5779 lc->ldo_def_striping = lds;
5780 } else if (lds->lds_def_striping_set &&
5781 !fid_is_root(lod_object_fid(lp))) {
5782 /* don't inherit default LMV for "lfs mkdir" */
5783 lds->lds_dir_def_striping_set = 0;
5784 lc->ldo_def_striping = lds;
5787 /* inherit default striping except ROOT */
5788 if ((lds->lds_def_striping_set ||
5789 lds->lds_dir_def_striping_set) &&
5790 !fid_is_root(lod_object_fid(lp)))
5791 lc->ldo_def_striping = lds;
5793 /* transfer defaults LMV to new directory */
5794 lod_striping_from_default(lc, lds, child_mode);
5796 /* set count 0 to create normal directory */
5797 if (lc->ldo_dir_stripe_count == 1)
5798 lc->ldo_dir_stripe_count = 0;
5800 /* do not save default LMV on server */
5801 if (ah->dah_dmv_imp_inherit) {
5802 lds->lds_dir_def_striping_set = 0;
5803 if (!lds->lds_def_striping_set)
5804 lc->ldo_def_striping = NULL;
5808 /* shrink the stripe count to max_mdt_stripecount if it is -1
5809 * and max_mdt_stripecount is not 0
5811 if (lc->ldo_dir_stripe_count == (__u16)(-1) &&
5812 d->lod_max_mdt_stripecount)
5813 lc->ldo_dir_stripe_count = d->lod_max_mdt_stripecount;
5815 /* shrink the stripe_count to the avaible MDT count */
5816 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
5817 !CFS_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)) {
5818 lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
5819 if (lc->ldo_dir_stripe_count == 1)
5820 lc->ldo_dir_stripe_count = 0;
5823 if (!lmv_is_known_hash_type(lc->ldo_dir_hash_type))
5824 lc->ldo_dir_hash_type =
5825 (lc->ldo_dir_hash_type & LMV_HASH_FLAG_KNOWN) |
5826 d->lod_mdt_descs.ltd_lmv_desc.ld_pattern;
5828 /* make sure all fscrypt metadata stays on same mdt */
5829 if (child->do_lu.lo_header->loh_attr & LOHA_FSCRYPT_MD) {
5830 lc->ldo_dir_stripe_count = 0;
5831 lds->lds_dir_def_stripe_offset =
5832 lod2lu_dev(d)->ld_site->ld_seq_site->ss_node_id;
5833 lds->lds_dir_def_striping_set = 1;
5834 lc->ldo_def_striping = lds;
5837 CDEBUG(D_INFO, "final dir stripe_count=%hu offset=%d hash=%u\n",
5838 lc->ldo_dir_stripe_count,
5839 (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
5844 /* child object regular file*/
5846 if (!lod_object_will_be_striped(S_ISREG(child_mode),
5847 lu_object_fid(&child->do_lu)))
5850 /* If object is going to be striped over OSTs, transfer default
5851 * striping information to the child, so that we can use it
5852 * during declaration and creation.
5854 * Try from the parent first.
5856 if (likely(lp != NULL)) {
5857 rc = lod_get_default_lov_striping(env, lp, lds, ah);
5858 if (rc == 0 && lds->lds_def_striping_set) {
5859 rc = lod_verify_striping(env, d, lp, &info->lti_buf,
5862 lod_striping_from_default(lc, lds, child_mode);
5866 /* Initialize lod_device::lod_md_root object reference */
5867 if (d->lod_md_root == NULL) {
5868 struct dt_object *root;
5869 struct lod_object *lroot;
5871 lu_root_fid(&info->lti_fid);
5872 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
5873 if (!IS_ERR(root)) {
5874 lroot = lod_dt_obj(root);
5876 spin_lock(&d->lod_lock);
5877 if (d->lod_md_root != NULL)
5878 dt_object_put(env, &d->lod_md_root->ldo_obj);
5879 d->lod_md_root = lroot;
5880 spin_unlock(&d->lod_lock);
5884 /* try inherit layout from the root object (fs default) when:
5885 * - parent does not have default layout; or
5886 * - parent has plain(v1/v3) default layout, and some attributes
5887 * are not specified in the default layout;
5889 if (d->lod_md_root != NULL &&
5890 lod_need_inherit_more(lc, true, ah->dah_append_pool)) {
5891 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds,
5893 if (rc || !lds->lds_def_striping_set)
5896 rc = lod_verify_striping(env, d, d->lod_md_root, &info->lti_buf,
5901 if (lc->ldo_comp_cnt == 0) {
5902 lod_striping_from_default(lc, lds, child_mode);
5903 } else if (!lds->lds_def_striping_is_composite) {
5904 struct lod_layout_component *def_comp;
5906 LASSERT(!lc->ldo_is_composite);
5907 lod_comp = &lc->ldo_comp_entries[0];
5908 def_comp = &lds->lds_def_comp_entries[0];
5910 if (lod_comp->llc_stripe_count <= 0)
5911 lod_comp->llc_stripe_count =
5912 def_comp->llc_stripe_count;
5913 if (lod_comp->llc_stripe_size <= 0)
5914 lod_comp->llc_stripe_size =
5915 def_comp->llc_stripe_size;
5916 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT &&
5917 (!lod_comp->llc_pool || !lod_comp->llc_pool[0]))
5918 lod_comp->llc_stripe_offset =
5919 def_comp->llc_stripe_offset;
5920 if (lod_comp->llc_pool == NULL)
5921 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
5926 * fs default striping may not be explicitly set, or historically set
5927 * in config log, use them.
5929 if (lod_need_inherit_more(lc, false, ah->dah_append_pool)) {
5930 if (lc->ldo_comp_cnt == 0) {
5931 rc = lod_alloc_comp_entries(lc, 0, 1);
5933 /* fail to allocate memory, will create a
5934 * non-striped file. */
5936 lc->ldo_is_composite = 0;
5937 lod_comp = &lc->ldo_comp_entries[0];
5938 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
5940 LASSERT(!lc->ldo_is_composite);
5941 lod_comp = &lc->ldo_comp_entries[0];
5942 desc = &d->lod_ost_descs.ltd_lov_desc;
5943 lod_adjust_stripe_info(lod_comp, desc,
5944 ah->dah_append_stripe_count);
5945 if (ah->dah_append_pool && ah->dah_append_pool[0])
5946 lod_obj_set_pool(lc, 0, ah->dah_append_pool);
5953 * Size initialization on late striping.
5955 * Propagate the size of a truncated object to a deferred striping.
5956 * This function handles a special case when truncate was done on a
5957 * non-striped object and now while the striping is being created
5958 * we can't lose that size, so we have to propagate it to the stripes
5961 * \param[in] env execution environment
5962 * \param[in] dt object
5963 * \param[in] th transaction handle
5965 * \retval 0 on success
5966 * \retval negative if failed
5968 static int lod_declare_init_size(const struct lu_env *env,
5969 struct dt_object *dt, struct thandle *th)
5971 struct dt_object *next = dt_object_child(dt);
5972 struct lod_object *lo = lod_dt_obj(dt);
5973 struct dt_object **objects = NULL;
5974 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
5975 uint64_t size, offs;
5976 int i, rc, stripe, stripe_count = 0, stripe_size = 0;
5977 struct lu_extent size_ext;
5980 if (!lod_obj_is_striped(dt))
5983 rc = dt_attr_get(env, next, attr);
5984 LASSERT(attr->la_valid & LA_SIZE);
5988 size = attr->la_size;
5992 size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
5993 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5994 struct lod_layout_component *lod_comp;
5995 struct lu_extent *extent;
5997 lod_comp = &lo->ldo_comp_entries[i];
5999 if (lod_comp->llc_stripe == NULL)
6002 extent = &lod_comp->llc_extent;
6003 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
6004 if (!lo->ldo_is_composite ||
6005 lu_extent_is_overlapped(extent, &size_ext)) {
6006 objects = lod_comp->llc_stripe;
6007 stripe_count = lod_comp->llc_stripe_count;
6008 stripe_size = lod_comp->llc_stripe_size;
6011 if (stripe_count == 0)
6014 LASSERT(objects != NULL && stripe_size != 0);
6015 do_div(size, stripe_size);
6016 stripe = do_div(size, stripe_count);
6017 LASSERT(objects[stripe] != NULL);
6019 size = size * stripe_size;
6020 offs = attr->la_size;
6021 size += do_div(offs, stripe_size);
6023 attr->la_valid = LA_SIZE;
6024 attr->la_size = size;
6026 rc = lod_sub_declare_attr_set(env, objects[stripe],
6035 * Declare creation of striped object.
6037 * The function declares creation stripes for a regular object. The function
6038 * also declares whether the stripes will be created with non-zero size if
6039 * previously size was set non-zero on the master object. If object \a dt is
6040 * not local, then only fully defined striping can be applied in \a lovea.
6041 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
6044 * \param[in] env execution environment
6045 * \param[in] dt object
6046 * \param[in] attr attributes the stripes will be created with
6047 * \param[in] lovea a buffer containing striping description
6048 * \param[in] th transaction handle
6050 * \retval 0 on success
6051 * \retval negative if failed
6053 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
6054 struct lu_attr *attr,
6055 const struct lu_buf *lovea, struct thandle *th)
6057 struct lod_thread_info *info = lod_env_info(env);
6058 struct dt_object *next = dt_object_child(dt);
6059 struct lod_object *lo = lod_dt_obj(dt);
6063 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
6064 GOTO(out, rc = -ENOMEM);
6066 if (!dt_object_remote(next)) {
6067 /* choose OST and generate appropriate objects */
6068 rc = lod_prepare_create(env, lo, attr, lovea, th);
6073 * declare storage for striping data
6075 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6077 /* LOD can not choose OST objects for remote objects, i.e.
6078 * stripes must be ready before that. Right now, it can only
6079 * happen during migrate, i.e. migrate process needs to create
6080 * remote regular file (mdd_migrate_create), then the migrate
6081 * process will provide stripeEA. */
6082 LASSERT(lovea != NULL);
6083 info->lti_buf = *lovea;
6086 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
6087 XATTR_NAME_LOV, 0, th);
6092 * if striping is created with local object's size > 0,
6093 * we have to propagate this size to specific object
6094 * the case is possible only when local object was created previously
6096 if (dt_object_exists(next))
6097 rc = lod_declare_init_size(env, dt, th);
6100 /* failed to create striping or to set initial size, let's reset
6101 * config so that others don't get confused */
6103 lod_striping_free(env, lo);
6109 * Whether subdirectories under \a dt should be created on MDTs by space QoS
6111 * If LMV_HASH_FLAG_SPACE is set on directory default layout, its subdirectories
6112 * should be created on MDT by space QoS.
6114 * \param[in] env execution environment
6115 * \param[in] dev lu device
6116 * \param[in] dt object
6118 * \retval 1 if directory should create subdir by space usage
6120 * \retval -ev if failed
6122 static inline int dt_object_qos_mkdir(const struct lu_env *env,
6123 struct lu_device *dev,
6124 struct dt_object *dt)
6126 struct lod_thread_info *info = lod_env_info(env);
6127 struct lu_object *obj;
6128 struct lod_object *lo;
6129 struct lmv_user_md *lmu;
6132 obj = lu_object_find_slice(env, dev, lu_object_fid(&dt->do_lu), NULL);
6134 return PTR_ERR(obj);
6136 lo = lu2lod_obj(obj);
6138 rc = lod_get_default_lmv_ea(env, lo);
6139 dt_object_put(env, dt);
6143 if (rc < (int)sizeof(*lmu))
6146 lmu = info->lti_ea_store;
6147 return le32_to_cpu(lmu->lum_stripe_offset) == LMV_OFFSET_DEFAULT;
6151 * Implementation of dt_object_operations::do_declare_create.
6153 * The method declares creation of a new object. If the object will be striped,
6154 * then helper functions are called to find FIDs for the stripes, declare
6155 * creation of the stripes and declare initialization of the striping
6156 * information to be stored in the master object.
6158 * \see dt_object_operations::do_declare_create() in the API description
6161 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
6162 struct lu_attr *attr,
6163 struct dt_allocation_hint *hint,
6164 struct dt_object_format *dof, struct thandle *th)
6166 struct dt_object *next = dt_object_child(dt);
6167 struct lod_object *lo = lod_dt_obj(dt);
6176 * first of all, we declare creation of local object
6178 rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
6183 * it's lod_ah_init() that has decided the object will be striped
6185 if (dof->dof_type == DFT_REGULAR) {
6186 /* callers don't want stripes */
6187 /* XXX: all tricky interactions with ->ah_make_hint() decided
6188 * to use striping, then ->declare_create() behaving differently
6189 * should be cleaned */
6190 if (dof->u.dof_reg.striped != 0)
6191 rc = lod_declare_striped_create(env, dt, attr,
6193 } else if (dof->dof_type == DFT_DIR) {
6194 struct seq_server_site *ss;
6195 struct lu_buf buf = { NULL };
6196 struct lu_buf *lmu = NULL;
6198 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
6200 /* If the parent has default stripeEA, and client
6201 * did not find it before sending create request,
6202 * then MDT will return -EREMOTE, and client will
6203 * retrieve the default stripeEA and re-create the
6206 * Note: if dah_eadata != NULL, it means creating the
6207 * striped directory with specified stripeEA, then it
6208 * should ignore the default stripeEA */
6209 if (hint != NULL && hint->dah_eadata == NULL) {
6210 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
6211 GOTO(out, rc = -EREMOTE);
6213 if (lo->ldo_dir_stripe_offset != LMV_OFFSET_DEFAULT &&
6214 lo->ldo_dir_stripe_offset != ss->ss_node_id) {
6215 struct lod_device *lod;
6216 struct lu_tgt_desc *mdt = NULL;
6217 bool found_mdt = false;
6219 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6220 lod_foreach_mdt(lod, mdt) {
6221 if (mdt->ltd_index ==
6222 lo->ldo_dir_stripe_offset) {
6228 /* If the MDT indicated by stripe_offset can be
6229 * found, then tell client to resend the create
6230 * request to the correct MDT, otherwise return
6231 * error to client */
6233 GOTO(out, rc = -EREMOTE);
6235 GOTO(out, rc = -EINVAL);
6237 } else if (hint && hint->dah_eadata) {
6239 lmu->lb_buf = (void *)hint->dah_eadata;
6240 lmu->lb_len = hint->dah_eadata_len;
6243 rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
6247 /* failed to create striping or to set initial size, let's reset
6248 * config so that others don't get confused */
6250 lod_striping_free(env, lo);
6255 * Generate component ID for new created component.
6257 * \param[in] lo LOD object
6258 * \param[in] comp_idx index of ldo_comp_entries
6260 * \retval component ID on success
6261 * \retval LCME_ID_INVAL on failure
6263 static __u32 lod_gen_component_id(struct lod_object *lo,
6264 int mirror_id, int comp_idx)
6266 struct lod_layout_component *lod_comp;
6267 __u32 id, start, end;
6270 LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
6272 lod_obj_inc_layout_gen(lo);
6273 id = lo->ldo_layout_gen;
6274 if (likely(id <= SEQ_ID_MAX))
6275 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
6277 /* Layout generation wraps, need to check collisions. */
6278 start = id & SEQ_ID_MASK;
6281 for (id = start; id <= end; id++) {
6282 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6283 lod_comp = &lo->ldo_comp_entries[i];
6284 if (pflr_id(mirror_id, id) == lod_comp->llc_id)
6287 /* Found the ununsed ID */
6288 if (i == lo->ldo_comp_cnt)
6289 RETURN(pflr_id(mirror_id, id));
6292 if (end == SEQ_ID_MAX) {
6293 end = min_t(__u32, start, SEQ_ID_MAX) - 1;
6298 RETURN(LCME_ID_INVAL);
6302 * Creation of a striped regular object.
6304 * The function is called to create the stripe objects for a regular
6305 * striped file. This can happen at the initial object creation or
6306 * when the caller asks LOD to do so using ->do_xattr_set() method
6307 * (so called late striping). Notice all the information are already
6308 * prepared in the form of the list of objects (ldo_stripe field).
6309 * This is done during declare phase.
6311 * \param[in] env execution environment
6312 * \param[in] dt object
6313 * \param[in] attr attributes the stripes will be created with
6314 * \param[in] dof format of stripes (see OSD API description)
6315 * \param[in] th transaction handle
6317 * \retval 0 on success
6318 * \retval negative if failed
6320 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
6321 struct lu_attr *attr, struct dt_object_format *dof,
6324 struct lod_layout_component *lod_comp;
6325 struct lod_object *lo = lod_dt_obj(dt);
6330 mutex_lock(&lo->ldo_layout_mutex);
6332 LASSERT((lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL) ||
6333 lo->ldo_is_foreign);
6335 mirror_id = 0; /* non-flr file's mirror_id is 0 */
6336 if (lo->ldo_mirror_count > 1) {
6337 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6338 lod_comp = &lo->ldo_comp_entries[i];
6339 if (lod_comp->llc_id != LCME_ID_INVAL &&
6340 mirror_id_of(lod_comp->llc_id) > mirror_id)
6341 mirror_id = mirror_id_of(lod_comp->llc_id);
6345 /* create all underlying objects */
6346 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6347 lod_comp = &lo->ldo_comp_entries[i];
6349 if (lod_comp->llc_id == LCME_ID_INVAL) {
6350 /* only the component of FLR layout with more than 1
6351 * mirror has mirror ID in its component ID.
6353 if (lod_comp->llc_extent.e_start == 0 &&
6354 lo->ldo_mirror_count > 1)
6357 lod_comp->llc_id = lod_gen_component_id(lo,
6359 if (lod_comp->llc_id == LCME_ID_INVAL)
6360 GOTO(out, rc = -ERANGE);
6363 if (lod_comp_inited(lod_comp))
6366 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
6367 lod_comp_set_init(lod_comp);
6369 if (lov_pattern(lod_comp->llc_pattern) & LOV_PATTERN_MDT)
6370 lod_comp_set_init(lod_comp);
6372 if (lod_comp->llc_stripe == NULL)
6375 LASSERT(lod_comp->llc_stripe_count);
6376 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6377 struct dt_object *object = lod_comp->llc_stripe[j];
6378 LASSERT(object != NULL);
6379 rc = lod_sub_create(env, object, attr, NULL, dof, th);
6383 lod_comp_set_init(lod_comp);
6386 rc = lod_fill_mirrors(lo);
6390 lo->ldo_comp_cached = 1;
6392 rc = lod_generate_and_set_lovea(env, lo, th);
6396 mutex_unlock(&lo->ldo_layout_mutex);
6401 lod_striping_free_nolock(env, lo);
6402 mutex_unlock(&lo->ldo_layout_mutex);
6407 static inline bool lod_obj_is_dom(struct dt_object *dt)
6409 struct lod_object *lo = lod_dt_obj(dt);
6411 if (!dt_object_exists(dt_object_child(dt)))
6414 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
6417 if (!lo->ldo_comp_cnt)
6420 return (lov_pattern(lo->ldo_comp_entries[0].llc_pattern) &
6425 * Implementation of dt_object_operations::do_create.
6427 * If any of preceeding methods (like ->do_declare_create(),
6428 * ->do_ah_init(), etc) chose to create a striped object,
6429 * then this method will create the master and the stripes.
6431 * \see dt_object_operations::do_create() in the API description for details.
6433 static int lod_create(const struct lu_env *env, struct dt_object *dt,
6434 struct lu_attr *attr, struct dt_allocation_hint *hint,
6435 struct dt_object_format *dof, struct thandle *th)
6440 /* create local object */
6441 rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
6445 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
6446 (lod_obj_is_striped(dt) || lod_obj_is_dom(dt)) &&
6447 dof->u.dof_reg.striped != 0) {
6448 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
6449 rc = lod_striped_create(env, dt, attr, dof, th);
6456 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
6457 struct dt_object *dt, struct thandle *th,
6458 int comp_idx, int stripe_idx,
6459 struct lod_obj_stripe_cb_data *data)
6461 if (data->locd_declare)
6462 return lod_sub_declare_destroy(env, dt, th);
6464 if (!CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
6465 stripe_idx == cfs_fail_val)
6466 return lod_sub_destroy(env, dt, th);
6472 * Implementation of dt_object_operations::do_declare_destroy.
6474 * If the object is a striped directory, then the function declares reference
6475 * removal from the master object (this is an index) to the stripes and declares
6476 * destroy of all the stripes. In all the cases, it declares an intention to
6477 * destroy the object itself.
6479 * \see dt_object_operations::do_declare_destroy() in the API description
6482 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
6485 struct dt_object *next = dt_object_child(dt);
6486 struct lod_object *lo = lod_dt_obj(dt);
6487 struct lod_thread_info *info = lod_env_info(env);
6488 struct dt_object *stripe;
6489 char *stripe_name = info->lti_key;
6495 * load striping information, notice we don't do this when object
6496 * is being initialized as we don't need this information till
6497 * few specific cases like destroy, chown
6499 rc = lod_striping_load(env, lo);
6503 /* declare destroy for all underlying objects */
6504 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6505 rc = next->do_ops->do_index_try(env, next,
6506 &dt_directory_features);
6510 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6511 stripe = lo->ldo_stripe[i];
6515 rc = lod_sub_declare_ref_del(env, next, th);
6519 snprintf(stripe_name, sizeof(info->lti_key),
6521 PFID(lu_object_fid(&stripe->do_lu)), i);
6522 rc = lod_sub_declare_delete(env, next,
6523 (const struct dt_key *)stripe_name, th);
6530 * we declare destroy for the local object
6532 rc = lod_sub_declare_destroy(env, next, th);
6536 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
6537 CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
6540 if (!lod_obj_is_striped(dt))
6543 /* declare destroy all striped objects */
6544 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6545 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6546 stripe = lo->ldo_stripe[i];
6550 if (!dt_object_exists(stripe))
6553 rc = lod_sub_declare_ref_del(env, stripe, th);
6557 rc = lod_sub_declare_destroy(env, stripe, th);
6562 struct lod_obj_stripe_cb_data data = { { 0 } };
6564 data.locd_declare = true;
6565 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6566 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6573 * Implementation of dt_object_operations::do_destroy.
6575 * If the object is a striped directory, then the function removes references
6576 * from the master object (this is an index) to the stripes and destroys all
6577 * the stripes. In all the cases, the function destroys the object itself.
6579 * \see dt_object_operations::do_destroy() in the API description for details.
6581 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
6584 struct dt_object *next = dt_object_child(dt);
6585 struct lod_object *lo = lod_dt_obj(dt);
6586 struct lod_thread_info *info = lod_env_info(env);
6587 char *stripe_name = info->lti_key;
6588 struct dt_object *stripe;
6594 /* destroy sub-stripe of master object */
6595 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6596 rc = next->do_ops->do_index_try(env, next,
6597 &dt_directory_features);
6601 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6602 stripe = lo->ldo_stripe[i];
6606 rc = lod_sub_ref_del(env, next, th);
6610 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
6611 PFID(lu_object_fid(&stripe->do_lu)), i);
6613 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
6614 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
6615 PFID(lu_object_fid(&stripe->do_lu)));
6617 rc = lod_sub_delete(env, next,
6618 (const struct dt_key *)stripe_name, th);
6624 rc = lod_sub_destroy(env, next, th);
6628 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
6629 CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
6632 if (!lod_obj_is_striped(dt))
6635 /* destroy all striped objects */
6636 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6637 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6638 stripe = lo->ldo_stripe[i];
6642 if (!dt_object_exists(stripe))
6645 if (!CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
6646 i == cfs_fail_val) {
6647 dt_write_lock(env, stripe, DT_TGT_CHILD);
6648 rc = lod_sub_ref_del(env, stripe, th);
6649 dt_write_unlock(env, stripe);
6653 rc = lod_sub_destroy(env, stripe, th);
6659 struct lod_obj_stripe_cb_data data = { { 0 } };
6661 data.locd_declare = false;
6662 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6663 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6670 * Implementation of dt_object_operations::do_declare_ref_add.
6672 * \see dt_object_operations::do_declare_ref_add() in the API description
6675 static int lod_declare_ref_add(const struct lu_env *env,
6676 struct dt_object *dt, struct thandle *th)
6678 return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
6682 * Implementation of dt_object_operations::do_ref_add.
6684 * \see dt_object_operations::do_ref_add() in the API description for details.
6686 static int lod_ref_add(const struct lu_env *env,
6687 struct dt_object *dt, struct thandle *th)
6689 return lod_sub_ref_add(env, dt_object_child(dt), th);
6693 * Implementation of dt_object_operations::do_declare_ref_del.
6695 * \see dt_object_operations::do_declare_ref_del() in the API description
6698 static int lod_declare_ref_del(const struct lu_env *env,
6699 struct dt_object *dt, struct thandle *th)
6701 return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
6705 * Implementation of dt_object_operations::do_ref_del
6707 * \see dt_object_operations::do_ref_del() in the API description for details.
6709 static int lod_ref_del(const struct lu_env *env,
6710 struct dt_object *dt, struct thandle *th)
6712 return lod_sub_ref_del(env, dt_object_child(dt), th);
6716 * Implementation of dt_object_operations::do_object_sync.
6718 * \see dt_object_operations::do_object_sync() in the API description
6721 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
6722 __u64 start, __u64 end)
6724 return dt_object_sync(env, dt_object_child(dt), start, end);
6728 * Implementation of dt_object_operations::do_object_unlock.
6730 * Used to release LDLM lock(s).
6732 * \see dt_object_operations::do_object_unlock() in the API description
6735 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
6736 struct ldlm_enqueue_info *einfo,
6737 union ldlm_policy_data *policy)
6739 struct lod_object *lo = lod_dt_obj(dt);
6740 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
6741 int slave_locks_size;
6745 if (slave_locks == NULL)
6748 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
6749 /* Note: for remote lock for single stripe dir, MDT will cancel
6750 * the lock by lockh directly */
6751 LASSERT(!dt_object_remote(dt_object_child(dt)));
6753 /* locks were unlocked in MDT layer */
6754 for (i = 0; i < slave_locks->ha_count; i++)
6755 LASSERT(!lustre_handle_is_used(&slave_locks->ha_handles[i]));
6758 * NB, ha_count may not equal to ldo_dir_stripe_count, because dir
6759 * layout may change, e.g., shrink dir layout after migration.
6761 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6762 if (lo->ldo_stripe[i])
6763 dt_invalidate(env, lo->ldo_stripe[i]);
6766 slave_locks_size = offsetof(typeof(*slave_locks),
6767 ha_handles[slave_locks->ha_count]);
6768 OBD_FREE(slave_locks, slave_locks_size);
6769 einfo->ei_cbdata = NULL;
6775 * Implementation of dt_object_operations::do_object_lock.
6777 * Used to get LDLM lock on the non-striped and striped objects.
6779 * \see dt_object_operations::do_object_lock() in the API description
6782 static int lod_object_lock(const struct lu_env *env,
6783 struct dt_object *dt,
6784 struct lustre_handle *lh,
6785 struct ldlm_enqueue_info *einfo,
6786 union ldlm_policy_data *policy)
6788 struct lod_object *lo = lod_dt_obj(dt);
6789 int slave_locks_size;
6790 struct lustre_handle_array *slave_locks = NULL;
6795 /* remote object lock */
6796 if (!einfo->ei_enq_slave) {
6797 LASSERT(dt_object_remote(dt));
6798 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
6802 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
6805 rc = lod_striping_load(env, lo);
6810 if (lo->ldo_dir_stripe_count <= 1)
6813 slave_locks_size = offsetof(typeof(*slave_locks),
6814 ha_handles[lo->ldo_dir_stripe_count]);
6815 /* Freed in lod_object_unlock */
6816 OBD_ALLOC(slave_locks, slave_locks_size);
6819 slave_locks->ha_count = lo->ldo_dir_stripe_count;
6821 /* striped directory lock */
6822 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6823 struct lustre_handle lockh;
6824 struct ldlm_res_id *res_id;
6825 struct dt_object *stripe;
6827 stripe = lo->ldo_stripe[i];
6831 res_id = &lod_env_info(env)->lti_res_id;
6832 fid_build_reg_res_name(lu_object_fid(&stripe->do_lu), res_id);
6833 einfo->ei_res_id = res_id;
6835 if (dt_object_remote(stripe)) {
6836 set_bit(i, (void *)slave_locks->ha_map);
6837 rc = dt_object_lock(env, stripe, &lockh, einfo, policy);
6839 struct ldlm_namespace *ns = einfo->ei_namespace;
6840 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
6841 ldlm_completion_callback completion = einfo->ei_cb_cp;
6842 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
6844 LASSERT(ns != NULL);
6845 rc = ldlm_cli_enqueue_local(env, ns, res_id, LDLM_IBITS,
6846 policy, einfo->ei_mode,
6847 &dlmflags, blocking,
6849 NULL, 0, LVB_T_NONE,
6854 ldlm_lock_decref_and_cancel(
6855 &slave_locks->ha_handles[i],
6857 OBD_FREE(slave_locks, slave_locks_size);
6860 slave_locks->ha_handles[i] = lockh;
6862 einfo->ei_cbdata = slave_locks;
6868 * Implementation of dt_object_operations::do_invalidate.
6870 * \see dt_object_operations::do_invalidate() in the API description for details
6872 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
6874 return dt_invalidate(env, dt_object_child(dt));
6877 static int lod_declare_instantiate_components(const struct lu_env *env,
6878 struct lod_object *lo,
6882 struct lod_thread_info *info = lod_env_info(env);
6887 LASSERT(info->lti_count < lo->ldo_comp_cnt);
6889 for (i = 0; i < info->lti_count; i++) {
6890 rc = lod_qos_prep_create(env, lo, NULL, th,
6891 info->lti_comp_idx[i], reserve);
6897 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6898 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
6899 &info->lti_buf, XATTR_NAME_LOV, 0, th);
6906 * Check OSTs for an existing component for further extension
6908 * Checks if OSTs are still healthy and not out of space. Gets free space
6909 * on OSTs (relative to allocation watermark rmb_low) and compares to
6910 * the proposed new_end for this component.
6912 * Decides whether or not to extend a component on its current OSTs.
6914 * \param[in] env execution environment for this thread
6915 * \param[in] lo object we're checking
6916 * \param[in] index index of this component
6917 * \param[in] extension_size extension size for this component
6918 * \param[in] extent layout extent for requested operation
6919 * \param[in] comp_extent extension component extent
6920 * \param[in] write if this is write operation
6922 * \retval true - OK to extend on current OSTs
6923 * \retval false - do not extend on current OSTs
6925 static bool lod_sel_osts_allowed(const struct lu_env *env,
6926 struct lod_object *lo,
6927 int index, __u64 reserve,
6928 struct lu_extent *extent,
6929 struct lu_extent *comp_extent, int write)
6931 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[index];
6932 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6933 struct lod_thread_info *tinfo = lod_env_info(env);
6934 struct obd_statfs *sfs = &tinfo->lti_osfs;
6935 __u64 available = 0;
6941 LASSERT(lod_comp->llc_stripe_count != 0);
6943 lod_getref(&lod->lod_ost_descs);
6944 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
6945 int index = lod_comp->llc_ost_indices[i];
6946 struct lod_tgt_desc *ost = OST_TGT(lod, index);
6947 struct obd_statfs_info info = { 0 };
6948 int j, repeated = 0;
6952 /* Get the number of times this OST repeats in this component.
6953 * Note: inter-component repeats are not counted as this is
6954 * considered as a rare case: we try to not repeat OST in other
6955 * components if possible. */
6956 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6957 if (index != lod_comp->llc_ost_indices[j])
6960 /* already handled */
6966 if (j < lod_comp->llc_stripe_count)
6969 if (!test_bit(index, lod->lod_ost_bitmap)) {
6970 CDEBUG(D_LAYOUT, "ost %d no longer present\n", index);
6975 rc = dt_statfs_info(env, ost->ltd_tgt, sfs, &info);
6977 CDEBUG(D_LAYOUT, "statfs failed for ost %d, error %d\n",
6983 if (sfs->os_state & OS_STATFS_ENOSPC ||
6984 sfs->os_state & OS_STATFS_READONLY ||
6985 sfs->os_state & OS_STATFS_DEGRADED) {
6986 CDEBUG(D_LAYOUT, "ost %d is not availble for SEL "
6987 "extension, state %u\n", index, sfs->os_state);
6993 available = sfs->os_bavail * sfs->os_bsize;
6994 /* 'available' is relative to the allocation threshold */
6995 available -= (__u64) info.os_reserved_mb_low << 20;
6997 CDEBUG(D_LAYOUT, "ost %d lowwm: %d highwm: %d, "
6998 "%llu %% blocks available, %llu %% blocks free\n",
6999 index, info.os_reserved_mb_low, info.os_reserved_mb_high,
7000 (100ull * sfs->os_bavail) / sfs->os_blocks,
7001 (100ull * sfs->os_bfree) / sfs->os_blocks);
7003 if (reserve * repeated > available) {
7005 CDEBUG(D_LAYOUT, "low space on ost %d, available %llu "
7006 "< extension size %llu repeated %d\n", index,
7007 available, reserve, repeated);
7011 lod_putref(lod, &lod->lod_ost_descs);
7017 * Adjust extents after component removal
7019 * When we remove an extension component, we move the start of the next
7020 * component to match the start of the extension component, so no space is left
7023 * \param[in] env execution environment for this thread
7024 * \param[in] lo object
7025 * \param[in] max_comp layout component
7026 * \param[in] index index of this component
7028 * \retval 0 on success
7029 * \retval negative errno on error
7031 static void lod_sel_adjust_extents(const struct lu_env *env,
7032 struct lod_object *lo,
7033 int max_comp, int index)
7035 struct lod_layout_component *lod_comp = NULL;
7036 struct lod_layout_component *next = NULL;
7037 struct lod_layout_component *prev = NULL;
7038 __u64 new_start = 0;
7042 /* Extension space component */
7043 lod_comp = &lo->ldo_comp_entries[index];
7044 next = &lo->ldo_comp_entries[index + 1];
7045 prev = &lo->ldo_comp_entries[index - 1];
7047 LASSERT(lod_comp != NULL && prev != NULL && next != NULL);
7048 LASSERT(lod_comp->llc_flags & LCME_FL_EXTENSION);
7050 /* Previous is being removed */
7051 if (prev && prev->llc_id == LCME_ID_INVAL)
7052 new_start = prev->llc_extent.e_start;
7054 new_start = lod_comp->llc_extent.e_start;
7056 for (i = index + 1; i < max_comp; i++) {
7057 lod_comp = &lo->ldo_comp_entries[i];
7059 start = lod_comp->llc_extent.e_start;
7060 lod_comp->llc_extent.e_start = new_start;
7062 /* We only move zero length extendable components */
7063 if (!(start == lod_comp->llc_extent.e_end))
7066 LASSERT(!(lod_comp->llc_flags & LCME_FL_INIT));
7068 lod_comp->llc_extent.e_end = new_start;
7072 /* Calculate the proposed 'new end' for a component we're extending */
7073 static __u64 lod_extension_new_end(__u64 extension_size, __u64 extent_end,
7074 __u32 stripe_size, __u64 component_end,
7075 __u64 extension_end)
7079 LASSERT(extension_size != 0 && stripe_size != 0);
7081 /* Round up to extension size */
7082 if (extent_end == OBD_OBJECT_EOF) {
7083 new_end = OBD_OBJECT_EOF;
7085 /* Add at least extension_size to the previous component_end,
7086 * covering the req layout extent */
7087 new_end = max(extent_end - component_end, extension_size);
7088 new_end = roundup(new_end, extension_size);
7089 new_end += component_end;
7091 /* Component end must be min stripe size aligned */
7092 if (new_end % stripe_size) {
7093 CDEBUG(D_LAYOUT, "new component end is not aligned "
7094 "by the stripe size %u: [%llu, %llu) ext size "
7095 "%llu new end %llu, aligning\n",
7096 stripe_size, component_end, extent_end,
7097 extension_size, new_end);
7098 new_end = roundup(new_end, stripe_size);
7102 if (new_end < extent_end)
7103 new_end = OBD_OBJECT_EOF;
7106 /* Don't extend past the end of the extension component */
7107 if (new_end > extension_end)
7108 new_end = extension_end;
7114 * Calculate the exact reservation (per-OST extension_size) on the OSTs being
7115 * instantiated. It needs to be calculated in advance and taken into account at
7116 * the instantiation time, because otherwise lod_statfs_and_check() may consider
7117 * an OST as OK, but SEL needs its extension_size to fit the free space and the
7118 * OST may turn out to be low-on-space, thus inappropriate OST may be used and
7121 * \param[in] lod_comp lod component we are checking
7123 * \retval size to reserved on each OST of lod_comp's stripe.
7125 static __u64 lod_sel_stripe_reserved(struct lod_layout_component *lod_comp)
7127 /* extension_size is file level, so we must divide by stripe count to
7128 * compare it to available space on a single OST */
7129 return lod_comp->llc_stripe_size * SEL_UNIT_SIZE /
7130 lod_comp->llc_stripe_count;
7133 /* As lod_sel_handler() could be re-entered for the same component several
7134 * times, this is the data for the next call. Fields could be changed to
7135 * component indexes when needed, (e.g. if there is no need to instantiate
7136 * all the previous components up to the current position) to tell the caller
7137 * where to start over from. */
7144 * Process extent updates for a particular layout component
7146 * Handle layout updates for a particular extension space component touched by
7147 * a layout update operation. Core function of self-extending PFL feature.
7149 * In general, this function processes exactly *one* stage of an extension
7150 * operation, modifying the layout accordingly, then returns to the caller.
7151 * The caller is responsible for restarting processing with the new layout,
7152 * which may repeatedly return to this function until the extension updates
7155 * This function does one of a few things to the layout:
7156 * 1. Extends the component before the current extension space component to
7157 * allow it to accomodate the requested operation (if space/policy permit that
7158 * component to continue on its current OSTs)
7160 * 2. If extension of the existing component fails, we do one of two things:
7161 * a. If there is a component after the extension space, we remove the
7162 * extension space component, move the start of the next component down
7163 * accordingly, then notify the caller to restart processing w/the new
7165 * b. If there is no following component, we try repeating the current
7166 * component, creating a new component using the current one as a
7167 * template (keeping its stripe properties but not specific striping),
7168 * and try assigning striping for this component. If there is sufficient
7169 * free space on the OSTs chosen for this component, it is instantiated
7170 * and i/o continues there.
7172 * If there is not sufficient space on the new OSTs, we remove this new
7173 * component & extend the current component.
7175 * Note further that uninited components followed by extension space can be zero
7176 * length meaning that we will try to extend them before initializing them, and
7177 * if that fails, they will be removed without initialization.
7179 * 3. If we extend to/beyond the end of an extension space component, that
7180 * component is exhausted (all of its range has been given to real components),
7181 * so we remove it and restart processing.
7183 * \param[in] env execution environment for this thread
7184 * \param[in,out] lo object to update the layout of
7185 * \param[in] extent layout extent for requested operation, update
7186 * layout to fit this operation
7187 * \param[in] th transaction handle for this operation
7188 * \param[in,out] max_comp the highest comp for the portion of the layout
7189 * we are operating on (For FLR, the chosen
7190 * replica). Updated because we may remove
7192 * \param[in] index index of the extension space component we're
7194 * \param[in] write if this is write op
7195 * \param[in,out] force if the extension is to be forced; set here
7196 to force it on the 2nd call for the same
7199 * \retval 0 on success
7200 * \retval negative errno on error
7202 static int lod_sel_handler(const struct lu_env *env,
7203 struct lod_object *lo,
7204 struct lu_extent *extent,
7205 struct thandle *th, int *max_comp,
7206 int index, int write,
7207 struct sel_data *sd)
7209 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7210 struct lod_thread_info *info = lod_env_info(env);
7211 struct lod_layout_component *lod_comp;
7212 struct lod_layout_component *prev;
7213 struct lod_layout_component *next = NULL;
7214 __u64 extension_size, reserve;
7221 /* First component cannot be extension space */
7223 CERROR("%s: "DFID" first component cannot be extension space\n",
7224 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
7228 lod_comp = &lo->ldo_comp_entries[index];
7229 prev = &lo->ldo_comp_entries[index - 1];
7230 if ((index + 1) < *max_comp)
7231 next = &lo->ldo_comp_entries[index + 1];
7233 /* extension size uses the stripe size field as KiB */
7234 extension_size = lod_comp->llc_stripe_size * SEL_UNIT_SIZE;
7236 CDEBUG(D_LAYOUT, "prev start %llu, extension start %llu, extension end"
7237 " %llu, extension size %llu\n", prev->llc_extent.e_start,
7238 lod_comp->llc_extent.e_start, lod_comp->llc_extent.e_end,
7241 /* Two extension space components cannot be adjacent & extension space
7242 * components cannot be init */
7243 if ((prev->llc_flags & LCME_FL_EXTENSION) ||
7244 !(ergo(next, !(next->llc_flags & LCME_FL_EXTENSION))) ||
7245 lod_comp_inited(lod_comp)) {
7246 CERROR("%s: "DFID" invalid extension space components\n",
7247 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
7251 reserve = lod_sel_stripe_reserved(lod_comp);
7253 if (!prev->llc_stripe) {
7254 CDEBUG(D_LAYOUT, "Previous component not inited\n");
7255 info->lti_count = 1;
7256 info->lti_comp_idx[0] = index - 1;
7257 rc = lod_declare_instantiate_components(env, lo, th, reserve);
7258 /* ENOSPC tells us we can't use this component. If there is
7259 * a next or we are repeating, we either spill over (next) or
7260 * extend the original comp (repeat). Otherwise, return the
7261 * error to the user. */
7262 if (rc == -ENOSPC && (next || sd->sd_repeat))
7268 if (sd->sd_force == 0 && rc == 0)
7269 rc = !lod_sel_osts_allowed(env, lo, index - 1, reserve, extent,
7270 &lod_comp->llc_extent, write);
7272 repeated = !!(sd->sd_repeat);
7276 /* Extend previous component */
7278 new_end = lod_extension_new_end(extension_size, extent->e_end,
7279 prev->llc_stripe_size,
7280 prev->llc_extent.e_end,
7281 lod_comp->llc_extent.e_end);
7283 CDEBUG(D_LAYOUT, "new end %llu\n", new_end);
7284 lod_comp->llc_extent.e_start = new_end;
7285 prev->llc_extent.e_end = new_end;
7287 if (prev->llc_extent.e_end == lod_comp->llc_extent.e_end) {
7288 CDEBUG(D_LAYOUT, "Extension component exhausted\n");
7289 lod_comp->llc_id = LCME_ID_INVAL;
7293 /* rc == 1, failed to extend current component */
7296 /* Normal 'spillover' case - Remove the extension
7297 * space component & bring down the start of the next
7299 lod_comp->llc_id = LCME_ID_INVAL;
7301 if (!(prev->llc_flags & LCME_FL_INIT)) {
7302 prev->llc_id = LCME_ID_INVAL;
7305 lod_sel_adjust_extents(env, lo, *max_comp, index);
7306 } else if (lod_comp_inited(prev)) {
7307 /* If there is no next, and the previous component is
7308 * INIT'ed, try repeating the previous component. */
7309 LASSERT(repeated == 0);
7310 rc = lod_layout_repeat_comp(env, lo, index - 1);
7314 /* The previous component is a repeated component.
7315 * Record this so we don't keep trying to repeat it. */
7318 /* If the previous component is not INIT'ed, this may
7319 * be a component we have just instantiated but failed
7320 * to extend. Or even a repeated component we failed
7321 * to prepare a striping for. Do not repeat but instead
7322 * remove the repeated component & force the extention
7323 * of the original one */
7326 prev->llc_id = LCME_ID_INVAL;
7333 rc = lod_layout_del_prep_layout(env, lo, NULL);
7336 LASSERTF(-rc == change,
7337 "number deleted %d != requested %d\n", -rc,
7340 *max_comp = *max_comp + change;
7342 /* lod_del_prep_layout reallocates ldo_comp_entries, so we must
7343 * refresh these pointers before using them */
7344 lod_comp = &lo->ldo_comp_entries[index];
7345 prev = &lo->ldo_comp_entries[index - 1];
7346 CDEBUG(D_LAYOUT, "After extent updates: prev start %llu, current start "
7347 "%llu, current end %llu max_comp %d ldo_comp_cnt %d\n",
7348 prev->llc_extent.e_start, lod_comp->llc_extent.e_start,
7349 lod_comp->llc_extent.e_end, *max_comp, lo->ldo_comp_cnt);
7351 /* Layout changed successfully */
7356 * Declare layout extent updates
7358 * Handles extensions. Identifies extension components touched by current
7359 * operation and passes them to processing function.
7361 * Restarts with updated layouts from the processing function until the current
7362 * operation no longer touches an extension space component.
7364 * \param[in] env execution environment for this thread
7365 * \param[in,out] lo object to update the layout of
7366 * \param[in] extent layout extent for requested operation, update layout to
7367 * fit this operation
7368 * \param[in] th transaction handle for this operation
7369 * \param[in] pick identifies chosen mirror for FLR layouts
7370 * \param[in] write if this is write op
7372 * \retval 1 on layout changed, 0 on no change
7373 * \retval negative errno on error
7375 static int lod_declare_update_extents(const struct lu_env *env,
7376 struct lod_object *lo, struct lu_extent *extent,
7377 struct thandle *th, int pick, int write)
7379 struct lod_thread_info *info = lod_env_info(env);
7380 struct lod_layout_component *lod_comp;
7381 bool layout_changed = false;
7382 struct sel_data sd = { 0 };
7390 /* This makes us work on the components of the chosen mirror */
7391 start_index = lo->ldo_mirrors[pick].lme_start;
7392 max_comp = lo->ldo_mirrors[pick].lme_end + 1;
7393 if (lo->ldo_flr_state == LCM_FL_NONE)
7394 LASSERT(start_index == 0 && max_comp == lo->ldo_comp_cnt);
7396 CDEBUG(D_LAYOUT, "extent->e_start %llu, extent->e_end %llu\n",
7397 extent->e_start, extent->e_end);
7398 for (i = start_index; i < max_comp; i++) {
7399 lod_comp = &lo->ldo_comp_entries[i];
7401 /* We've passed all components of interest */
7402 if (lod_comp->llc_extent.e_start >= extent->e_end)
7405 if (lod_comp->llc_flags & LCME_FL_EXTENSION) {
7406 layout_changed = true;
7407 rc = lod_sel_handler(env, lo, extent, th, &max_comp,
7412 /* Nothing has changed behind the prev one */
7418 /* We may have added or removed components. If so, we must update the
7419 * start & ends of all the mirrors after the current one, and the end
7420 * of the current mirror. */
7421 change = max_comp - 1 - lo->ldo_mirrors[pick].lme_end;
7423 lo->ldo_mirrors[pick].lme_end += change;
7424 for (i = pick + 1; i < lo->ldo_mirror_count; i++) {
7425 lo->ldo_mirrors[i].lme_start += change;
7426 lo->ldo_mirrors[i].lme_end += change;
7432 /* The amount of components has changed, adjust the lti_comp_idx */
7433 rc2 = lod_layout_data_init(info, lo->ldo_comp_cnt);
7435 return rc < 0 ? rc : rc2 < 0 ? rc2 : layout_changed;
7438 /* If striping is already instantiated or INIT'ed DOM? */
7439 static bool lod_is_instantiation_needed(struct lod_layout_component *comp)
7441 return !(((lov_pattern(comp->llc_pattern) & LOV_PATTERN_MDT) &&
7442 lod_comp_inited(comp)) || comp->llc_stripe);
7446 * Declare layout update for a non-FLR layout.
7448 * \param[in] env execution environment for this thread
7449 * \param[in,out] lo object to update the layout of
7450 * \param[in] layout layout intent for requested operation, "update" is
7451 * a process of reacting to this
7452 * \param[in] buf buffer containing lov ea (see comment on usage inline)
7453 * \param[in] th transaction handle for this operation
7455 * \retval 0 on success
7456 * \retval negative errno on error
7458 static int lod_declare_update_plain(const struct lu_env *env,
7459 struct lod_object *lo, struct layout_intent *layout,
7460 const struct lu_buf *buf, struct thandle *th)
7462 struct lod_thread_info *info = lod_env_info(env);
7463 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7464 struct lod_layout_component *lod_comp;
7465 struct lov_comp_md_v1 *comp_v1 = NULL;
7466 bool layout_changed = false;
7467 bool replay = false;
7471 LASSERT(lo->ldo_flr_state == LCM_FL_NONE);
7474 * In case the client is passing lovea, which only happens during
7475 * the replay of layout intent write RPC for now, we may need to
7476 * parse the lovea and apply new layout configuration.
7478 if (buf && buf->lb_len) {
7479 struct lov_user_md_v1 *v1 = buf->lb_buf;
7481 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
7482 v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
7483 LOV_MAGIC_COMP_V1)) {
7484 CERROR("%s: the replay buffer of layout extend "
7485 "(magic %#x) does not contain expected "
7486 "composite layout.\n",
7487 lod2obd(d)->obd_name, v1->lmm_magic);
7488 GOTO(out, rc = -EINVAL);
7491 rc = lod_use_defined_striping(env, lo, buf);
7494 lo->ldo_comp_cached = 1;
7496 rc = lod_get_lov_ea(env, lo);
7499 /* old on-disk EA is stored in info->lti_buf */
7500 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
7502 layout_changed = true;
7504 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
7508 /* non replay path */
7509 rc = lod_striping_load(env, lo);
7514 /* Make sure defined layout covers the requested write range. */
7515 lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
7516 if (lo->ldo_comp_cnt > 1 &&
7517 lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
7518 lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
7519 CDEBUG_LIMIT(replay ? D_ERROR : D_LAYOUT,
7520 "%s: the defined layout [0, %#llx) does not "
7521 "covers the write range "DEXT"\n",
7522 lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
7523 PEXT(&layout->li_extent));
7524 GOTO(out, rc = -EINVAL);
7527 CDEBUG(D_LAYOUT, "%s: "DFID": update components "DEXT"\n",
7528 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
7529 PEXT(&layout->li_extent));
7532 rc = lod_declare_update_extents(env, lo, &layout->li_extent,
7533 th, 0, layout->li_opc == LAYOUT_INTENT_WRITE);
7537 layout_changed = true;
7541 * Iterate ld->ldo_comp_entries, find the component whose extent under
7542 * the write range and not instantianted.
7544 for (i = 0; i < lo->ldo_comp_cnt; i++) {
7545 lod_comp = &lo->ldo_comp_entries[i];
7547 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
7551 /* If striping is instantiated or INIT'ed DOM skip */
7552 if (!lod_is_instantiation_needed(lod_comp))
7556 * In replay path, lod_comp is the EA passed by
7557 * client replay buffer, comp_v1 is the pre-recovery
7558 * on-disk EA, we'd sift out those components which
7559 * were init-ed in the on-disk EA.
7561 if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
7566 * this component hasn't instantiated in normal path, or during
7567 * replay it needs replay the instantiation.
7570 /* A released component is being extended */
7571 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
7572 GOTO(out, rc = -EINVAL);
7574 LASSERT(info->lti_comp_idx != NULL);
7575 info->lti_comp_idx[info->lti_count++] = i;
7576 layout_changed = true;
7579 if (!layout_changed)
7582 lod_obj_inc_layout_gen(lo);
7583 rc = lod_declare_instantiate_components(env, lo, th, 0);
7587 lod_striping_free(env, lo);
7591 static inline int lod_comp_index(struct lod_object *lo,
7592 struct lod_layout_component *lod_comp)
7594 LASSERT(lod_comp >= lo->ldo_comp_entries &&
7595 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
7597 return lod_comp - lo->ldo_comp_entries;
7601 * Stale other mirrors by writing extent.
7603 static int lod_stale_components(const struct lu_env *env, struct lod_object *lo,
7604 int primary, struct lu_extent *extent,
7607 struct lod_layout_component *pri_comp, *lod_comp;
7608 struct lu_extent pri_extent;
7613 /* The writing extent decides which components in the primary
7614 * are affected... */
7615 CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
7618 lod_foreach_mirror_comp(pri_comp, lo, primary) {
7619 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
7622 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
7623 lod_comp_index(lo, pri_comp),
7624 PEXT(&pri_comp->llc_extent));
7626 pri_extent.e_start = pri_comp->llc_extent.e_start;
7627 pri_extent.e_end = pri_comp->llc_extent.e_end;
7629 for (i = 0; i < lo->ldo_mirror_count; i++) {
7632 rc = lod_declare_update_extents(env, lo, &pri_extent,
7634 /* if update_extents changed the layout, it may have
7635 * reallocated the component array, so start over to
7636 * avoid using stale pointers */
7642 /* ... and then stale other components that are
7643 * overlapping with primary components */
7644 lod_foreach_mirror_comp(lod_comp, lo, i) {
7645 if (!lu_extent_is_overlapped(
7647 &lod_comp->llc_extent))
7650 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
7651 i, lod_comp_index(lo, lod_comp));
7653 lod_comp->llc_flags |= LCME_FL_STALE;
7654 lo->ldo_mirrors[i].lme_stale = 1;
7663 * check an OST's availability
7664 * \param[in] env execution environment
7665 * \param[in] lo lod object
7666 * \param[in] dt dt object
7667 * \param[in] index mirror index
7669 * \retval negative if failed
7670 * \retval 1 if \a dt is available
7671 * \retval 0 if \a dt is not available
7673 static inline int lod_check_ost_avail(const struct lu_env *env,
7674 struct lod_object *lo,
7675 struct dt_object *dt, int index)
7677 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7678 struct lod_tgt_desc *ost;
7680 int type = LU_SEQ_RANGE_OST;
7683 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &idx, &type);
7685 CERROR("%s: can't locate "DFID":rc = %d\n",
7686 lod2obd(lod)->obd_name, PFID(lu_object_fid(&dt->do_lu)),
7691 ost = OST_TGT(lod, idx);
7692 if (ost->ltd_active == 0) {
7693 CDEBUG(D_LAYOUT, DFID ": mirror %d OST%d unavail\n",
7694 PFID(lod_object_fid(lo)), index, idx);
7702 * Pick primary mirror for write
7703 * \param[in] env execution environment
7704 * \param[in] lo object
7705 * \param[in] extent write range
7707 static int lod_primary_pick(const struct lu_env *env, struct lod_object *lo,
7708 struct lu_extent *extent)
7710 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7711 unsigned int seq = 0;
7712 struct lod_layout_component *lod_comp;
7714 int picked = -1, second_pick = -1, third_pick = -1;
7717 if (CFS_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
7718 get_random_bytes(&seq, sizeof(seq));
7719 seq %= lo->ldo_mirror_count;
7723 * Pick a mirror as the primary, and check the availability of OSTs.
7725 * This algo can be revised later after knowing the topology of
7728 lod_qos_statfs_update(env, lod, &lod->lod_ost_descs);
7730 rc = lod_fill_mirrors(lo);
7734 for (i = 0; i < lo->ldo_mirror_count; i++) {
7735 bool ost_avail = true;
7736 int index = (i + seq) % lo->ldo_mirror_count;
7738 if (lo->ldo_mirrors[index].lme_stale) {
7739 CDEBUG(D_LAYOUT, DFID": mirror %d stale\n",
7740 PFID(lod_object_fid(lo)), index);
7744 /* 2nd pick is for the primary mirror containing unavail OST */
7745 if (lo->ldo_mirrors[index].lme_prefer && second_pick < 0)
7746 second_pick = index;
7748 /* 3rd pick is for non-primary mirror containing unavail OST */
7749 if (second_pick < 0 && third_pick < 0)
7753 * we found a non-primary 1st pick, we'd like to find a
7754 * potential pirmary mirror.
7756 if (picked >= 0 && !lo->ldo_mirrors[index].lme_prefer)
7759 /* check the availability of OSTs */
7760 lod_foreach_mirror_comp(lod_comp, lo, index) {
7761 if (!lod_comp_inited(lod_comp) || !lod_comp->llc_stripe)
7764 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
7765 struct dt_object *dt = lod_comp->llc_stripe[j];
7767 rc = lod_check_ost_avail(env, lo, dt, index);
7774 } /* for all dt object in one component */
7777 } /* for all components in a mirror */
7780 * the OSTs where allocated objects locates in the components
7781 * of the mirror are available.
7786 /* this mirror has all OSTs available */
7790 * primary with all OSTs are available, this is the perfect
7793 if (lo->ldo_mirrors[index].lme_prefer)
7795 } /* for all mirrors */
7797 /* failed to pick a sound mirror, lower our expectation */
7799 picked = second_pick;
7801 picked = third_pick;
7808 static int lod_prepare_resync_mirror(const struct lu_env *env,
7809 struct lod_object *lo,
7812 struct lod_thread_info *info = lod_env_info(env);
7813 struct lod_layout_component *lod_comp;
7814 bool neg = !!(MIRROR_ID_NEG & mirror_id);
7817 mirror_id &= ~MIRROR_ID_NEG;
7819 for (i = 0; i < lo->ldo_mirror_count; i++) {
7820 if ((!neg && lo->ldo_mirrors[i].lme_id != mirror_id) ||
7821 (neg && lo->ldo_mirrors[i].lme_id == mirror_id))
7824 lod_foreach_mirror_comp(lod_comp, lo, i) {
7825 if (lod_comp_inited(lod_comp))
7828 info->lti_comp_idx[info->lti_count++] =
7829 lod_comp_index(lo, lod_comp);
7837 * figure out the components should be instantiated for resync.
7839 static int lod_prepare_resync(const struct lu_env *env, struct lod_object *lo,
7840 struct lu_extent *extent)
7842 struct lod_thread_info *info = lod_env_info(env);
7843 struct lod_layout_component *lod_comp;
7844 unsigned int need_sync = 0;
7848 DFID": instantiate all stale components in "DEXT"\n",
7849 PFID(lod_object_fid(lo)), PEXT(extent));
7852 * instantiate all components within this extent, even non-stale
7855 for (i = 0; i < lo->ldo_mirror_count; i++) {
7856 if (!lo->ldo_mirrors[i].lme_stale)
7859 lod_foreach_mirror_comp(lod_comp, lo, i) {
7860 if (!lu_extent_is_overlapped(extent,
7861 &lod_comp->llc_extent))
7866 if (lod_comp_inited(lod_comp))
7869 CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
7870 i, lod_comp_index(lo, lod_comp));
7871 info->lti_comp_idx[info->lti_count++] =
7872 lod_comp_index(lo, lod_comp);
7876 return need_sync ? 0 : -EALREADY;
7879 static int lod_declare_update_rdonly(const struct lu_env *env,
7880 struct lod_object *lo, struct md_layout_change *mlc,
7883 struct lod_thread_info *info = lod_env_info(env);
7884 struct lu_attr *layout_attr = &info->lti_layout_attr;
7885 struct lod_layout_component *lod_comp;
7886 struct lu_extent extent = { 0 };
7890 LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
7891 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7892 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7893 LASSERT(lo->ldo_mirror_count > 0);
7895 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7896 struct layout_intent *layout = mlc->mlc_intent;
7897 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7900 extent = layout->li_extent;
7901 CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
7902 PFID(lod_object_fid(lo)), PEXT(&extent));
7904 picked = lod_primary_pick(env, lo, &extent);
7908 CDEBUG(D_LAYOUT, DFID": picked mirror id %u as primary\n",
7909 PFID(lod_object_fid(lo)),
7910 lo->ldo_mirrors[picked].lme_id);
7912 /* Update extents of primary before staling */
7913 rc = lod_declare_update_extents(env, lo, &extent, th, picked,
7918 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
7920 * trunc transfers [0, size) in the intent extent, we'd
7921 * stale components overlapping [size, eof).
7923 extent.e_start = extent.e_end;
7924 extent.e_end = OBD_OBJECT_EOF;
7927 /* stale overlapping components from other mirrors */
7928 rc = lod_stale_components(env, lo, picked, &extent, th);
7932 /* restore truncate intent extent */
7933 if (layout->li_opc == LAYOUT_INTENT_TRUNC)
7934 extent.e_end = extent.e_start;
7936 /* instantiate components for the picked mirror, start from 0 */
7939 lod_foreach_mirror_comp(lod_comp, lo, picked) {
7940 if (!lu_extent_is_overlapped(&extent,
7941 &lod_comp->llc_extent))
7944 if (!lod_is_instantiation_needed(lod_comp))
7947 info->lti_comp_idx[info->lti_count++] =
7948 lod_comp_index(lo, lod_comp);
7951 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7952 } else { /* MD_LAYOUT_RESYNC */
7956 * could contain multiple non-stale mirrors, so we need to
7957 * prep uninited all components assuming any non-stale mirror
7958 * could be picked as the primary mirror.
7960 if (mlc->mlc_mirror_id == 0) {
7962 for (i = 0; i < lo->ldo_mirror_count; i++) {
7963 if (lo->ldo_mirrors[i].lme_stale)
7966 lod_foreach_mirror_comp(lod_comp, lo, i) {
7967 if (!lod_comp_inited(lod_comp))
7971 lod_comp->llc_extent.e_end)
7973 lod_comp->llc_extent.e_end;
7976 rc = lod_prepare_resync(env, lo, &extent);
7980 /* mirror write, try to init its all components */
7981 rc = lod_prepare_resync_mirror(env, lo,
7982 mlc->mlc_mirror_id);
7987 /* change the file state to SYNC_PENDING */
7988 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7991 /* Reset the layout version once it's becoming too large.
7992 * This way it can make sure that the layout version is
7993 * monotonously increased in this writing era. */
7994 lod_obj_inc_layout_gen(lo);
7996 rc = lod_declare_instantiate_components(env, lo, th, 0);
8000 layout_attr->la_valid = LA_LAYOUT_VERSION;
8001 layout_attr->la_layout_version = 0;
8002 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
8003 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
8004 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
8010 lod_striping_free(env, lo);
8014 static int lod_declare_update_write_pending(const struct lu_env *env,
8015 struct lod_object *lo, struct md_layout_change *mlc,
8018 struct lod_thread_info *info = lod_env_info(env);
8019 struct lu_attr *layout_attr = &info->lti_layout_attr;
8020 struct lod_layout_component *lod_comp;
8021 struct lu_extent extent = { 0 };
8027 LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
8028 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
8029 mlc->mlc_opc == MD_LAYOUT_RESYNC);
8031 /* look for the first preferred mirror */
8032 for (i = 0; i < lo->ldo_mirror_count; i++) {
8033 if (lo->ldo_mirrors[i].lme_stale)
8035 if (lo->ldo_mirrors[i].lme_prefer == 0)
8042 /* no primary, use any in-sync */
8043 for (i = 0; i < lo->ldo_mirror_count; i++) {
8044 if (lo->ldo_mirrors[i].lme_stale)
8050 CERROR(DFID ": doesn't have a primary mirror\n",
8051 PFID(lod_object_fid(lo)));
8052 GOTO(out, rc = -ENODATA);
8056 CDEBUG(D_LAYOUT, DFID": found primary %u\n",
8057 PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
8059 LASSERT(!lo->ldo_mirrors[primary].lme_stale);
8061 /* for LAYOUT_WRITE opc, it has to do the following operations:
8062 * 1. stale overlapping componets from stale mirrors;
8063 * 2. instantiate components of the primary mirror;
8064 * 3. transfter layout version to all objects of the primary;
8066 * for LAYOUT_RESYNC opc, it will do:
8067 * 1. instantiate components of all stale mirrors;
8068 * 2. transfer layout version to all objects to close write era. */
8070 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
8071 struct layout_intent *layout = mlc->mlc_intent;
8072 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
8074 LASSERT(mlc->mlc_intent != NULL);
8076 extent = mlc->mlc_intent->li_extent;
8078 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
8079 PFID(lod_object_fid(lo)), PEXT(&extent));
8081 /* 1. Update extents of primary before staling */
8082 rc = lod_declare_update_extents(env, lo, &extent, th, primary,
8087 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
8089 * trunc transfers [0, size) in the intent extent, we'd
8090 * stale components overlapping [size, eof).
8092 extent.e_start = extent.e_end;
8093 extent.e_end = OBD_OBJECT_EOF;
8096 /* 2. stale overlapping components */
8097 rc = lod_stale_components(env, lo, primary, &extent, th);
8101 /* 3. find the components which need instantiating.
8102 * instantiate [0, mlc->mlc_intent->e_end) */
8104 /* restore truncate intent extent */
8105 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC)
8106 extent.e_end = extent.e_start;
8109 lod_foreach_mirror_comp(lod_comp, lo, primary) {
8110 if (!lu_extent_is_overlapped(&extent,
8111 &lod_comp->llc_extent))
8114 if (!lod_is_instantiation_needed(lod_comp))
8117 CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
8118 primary, lod_comp_index(lo, lod_comp));
8119 info->lti_comp_idx[info->lti_count++] =
8120 lod_comp_index(lo, lod_comp);
8122 } else { /* MD_LAYOUT_RESYNC */
8123 if (mlc->mlc_mirror_id == 0) {
8125 lod_foreach_mirror_comp(lod_comp, lo, primary) {
8126 if (!lod_comp_inited(lod_comp))
8129 extent.e_end = lod_comp->llc_extent.e_end;
8132 rc = lod_prepare_resync(env, lo, &extent);
8136 /* mirror write, try to init its all components */
8137 rc = lod_prepare_resync_mirror(env, lo,
8138 mlc->mlc_mirror_id);
8143 /* change the file state to SYNC_PENDING */
8144 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
8147 rc = lod_declare_instantiate_components(env, lo, th, 0);
8151 lod_obj_inc_layout_gen(lo);
8153 /* 3. transfer layout version to OST objects.
8154 * transfer new layout version to OST objects so that stale writes
8155 * can be denied. It also ends an era of writing by setting
8156 * LU_LAYOUT_RESYNC. Normal client can never use this bit to
8157 * send write RPC; only resync RPCs could do it. */
8158 layout_attr->la_valid = LA_LAYOUT_VERSION;
8159 layout_attr->la_layout_version = 0;
8160 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
8161 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
8162 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
8167 lod_striping_free(env, lo);
8171 static int lod_declare_update_sync_pending(const struct lu_env *env,
8172 struct lod_object *lo, struct md_layout_change *mlc,
8175 struct lod_thread_info *info = lod_env_info(env);
8176 struct lu_attr *layout_attr = &info->lti_layout_attr;
8177 unsigned sync_components = 0;
8178 unsigned resync_components = 0;
8183 LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
8184 LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
8185 mlc->mlc_opc == MD_LAYOUT_WRITE);
8187 CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
8188 PFID(lod_object_fid(lo)), mlc->mlc_opc);
8190 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
8191 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
8192 PFID(lod_object_fid(lo)));
8194 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
8195 return lod_declare_update_write_pending(env, lo, mlc, th);
8198 /* MD_LAYOUT_RESYNC_DONE */
8200 for (i = 0; i < lo->ldo_comp_cnt; i++) {
8201 struct lod_layout_component *lod_comp;
8204 lod_comp = &lo->ldo_comp_entries[i];
8206 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
8211 for (j = 0; j < mlc->mlc_resync_count; j++) {
8212 if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
8215 mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
8216 lod_comp->llc_flags &= ~LCME_FL_STALE;
8217 resync_components++;
8223 for (i = 0; i < mlc->mlc_resync_count; i++) {
8224 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
8227 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
8228 "or already synced\n", PFID(lod_object_fid(lo)),
8229 mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
8230 GOTO(out, rc = -EINVAL);
8233 if (!sync_components || (mlc->mlc_resync_count && !resync_components)) {
8234 CDEBUG(D_LAYOUT, DFID": no mirror in sync\n",
8235 PFID(lod_object_fid(lo)));
8237 /* tend to return an error code here to prevent
8238 * the MDT from setting SoM attribute */
8239 GOTO(out, rc = -EINVAL);
8242 CDEBUG(D_LAYOUT, DFID": synced %u resynced %u/%zu components\n",
8243 PFID(lod_object_fid(lo)),
8244 sync_components, resync_components, mlc->mlc_resync_count);
8246 lo->ldo_flr_state = LCM_FL_RDONLY;
8247 lod_obj_inc_layout_gen(lo);
8249 layout_attr->la_valid = LA_LAYOUT_VERSION;
8250 layout_attr->la_layout_version = 0;
8251 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
8255 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
8256 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
8257 &info->lti_buf, XATTR_NAME_LOV, 0, th);
8262 lod_striping_free(env, lo);
8266 typedef int (*mlc_handler)(const struct lu_env *env, struct dt_object *dt,
8267 const struct md_layout_change *mlc,
8268 struct thandle *th);
8271 * Attach stripes after target's for migrating directory. NB, we
8272 * only need to declare this, the actual work is done inside
8273 * lod_xattr_set_lmv().
8275 * \param[in] env execution environment
8276 * \param[in] dt target object
8277 * \param[in] mlc layout change data
8278 * \param[in] th transaction handle
8280 * \retval 0 on success
8281 * \retval negative if failed
8283 static int lod_dir_declare_layout_attach(const struct lu_env *env,
8284 struct dt_object *dt,
8285 const struct md_layout_change *mlc,
8288 struct lod_thread_info *info = lod_env_info(env);
8289 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
8290 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
8291 struct lod_object *lo = lod_dt_obj(dt);
8292 struct dt_object *next = dt_object_child(dt);
8293 struct dt_object_format *dof = &info->lti_format;
8294 struct lmv_mds_md_v1 *lmv = mlc->mlc_buf.lb_buf;
8295 struct dt_object **stripes;
8296 __u32 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
8297 struct lu_fid *fid = &info->lti_fid;
8298 struct lod_tgt_desc *tgt;
8299 struct dt_object *dto;
8300 struct dt_device *tgt_dt;
8301 int type = LU_SEQ_RANGE_ANY;
8302 struct dt_insert_rec *rec = &info->lti_dt_rec;
8303 char *stripe_name = info->lti_key;
8304 struct lu_name *sname;
8305 struct linkea_data ldata = { NULL };
8306 struct lu_buf linkea_buf;
8313 if (!lmv_is_sane(lmv))
8316 if (!dt_try_as_dir(env, dt, false))
8319 dof->dof_type = DFT_DIR;
8321 OBD_ALLOC_PTR_ARRAY(stripes, (lo->ldo_dir_stripe_count + stripe_count));
8325 for (i = 0; i < lo->ldo_dir_stripe_count; i++)
8326 stripes[i] = lo->ldo_stripe[i];
8328 rec->rec_type = S_IFDIR;
8330 for (i = 0; i < stripe_count; i++) {
8332 &lmv->lmv_stripe_fids[i]);
8333 if (!fid_is_sane(fid))
8336 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
8340 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
8341 tgt_dt = lod->lod_child;
8343 tgt = LTD_TGT(ltd, idx);
8345 GOTO(out, rc = -ESTALE);
8346 tgt_dt = tgt->ltd_tgt;
8349 dto = dt_locate_at(env, tgt_dt, fid,
8350 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
8353 GOTO(out, rc = PTR_ERR(dto));
8355 stripes[i + lo->ldo_dir_stripe_count] = dto;
8357 if (!dt_try_as_dir(env, dto, true))
8358 GOTO(out, rc = -ENOTDIR);
8360 rc = lod_sub_declare_ref_add(env, dto, th);
8364 rec->rec_fid = lu_object_fid(&dto->do_lu);
8365 rc = lod_sub_declare_insert(env, dto,
8366 (const struct dt_rec *)rec,
8367 (const struct dt_key *)dot, th);
8371 rc = lod_sub_declare_insert(env, dto,
8372 (const struct dt_rec *)rec,
8373 (const struct dt_key *)dotdot, th);
8377 rc = lod_sub_declare_xattr_set(env, dto, &mlc->mlc_buf,
8378 XATTR_NAME_LMV, 0, th);
8382 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
8383 PFID(lu_object_fid(&dto->do_lu)),
8384 i + lo->ldo_dir_stripe_count);
8386 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
8387 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
8388 sname, lu_object_fid(&dt->do_lu));
8392 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
8393 linkea_buf.lb_len = ldata.ld_leh->leh_len;
8394 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
8395 XATTR_NAME_LINK, 0, th);
8399 rc = lod_sub_declare_insert(env, next,
8400 (const struct dt_rec *)rec,
8401 (const struct dt_key *)stripe_name,
8406 rc = lod_sub_declare_ref_add(env, next, th);
8412 OBD_FREE_PTR_ARRAY(lo->ldo_stripe,
8413 lo->ldo_dir_stripes_allocated);
8414 lo->ldo_stripe = stripes;
8415 lo->ldo_is_foreign = 0;
8416 lo->ldo_dir_migrate_offset = lo->ldo_dir_stripe_count;
8417 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_hash_type);
8418 lo->ldo_dir_stripe_count += stripe_count;
8419 lo->ldo_dir_stripes_allocated += stripe_count;
8421 /* plain directory split creates target as a plain directory, while
8422 * after source attached as the first stripe, it becomes a striped
8423 * directory, set correct do_index_ops, otherwise it can't be unlinked.
8425 dt->do_index_ops = &lod_striped_index_ops;
8429 i = lo->ldo_dir_stripe_count;
8430 while (i < lo->ldo_dir_stripe_count + stripe_count && stripes[i])
8431 dt_object_put(env, stripes[i++]);
8433 OBD_FREE_PTR_ARRAY(stripes, stripe_count + lo->ldo_dir_stripe_count);
8437 static int lod_dir_declare_layout_detach(const struct lu_env *env,
8438 struct dt_object *dt,
8439 const struct md_layout_change *unused,
8442 struct lod_thread_info *info = lod_env_info(env);
8443 struct lod_object *lo = lod_dt_obj(dt);
8444 struct dt_object *next = dt_object_child(dt);
8445 char *stripe_name = info->lti_key;
8446 struct dt_object *dto;
8450 if (!dt_try_as_dir(env, dt, true))
8453 if (!lo->ldo_dir_stripe_count)
8454 return lod_sub_declare_delete(env, next,
8455 (const struct dt_key *)dotdot, th);
8457 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8458 dto = lo->ldo_stripe[i];
8462 if (!dt_try_as_dir(env, dto, true))
8465 rc = lod_sub_declare_delete(env, dto,
8466 (const struct dt_key *)dotdot, th);
8470 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8471 PFID(lu_object_fid(&dto->do_lu)), i);
8473 rc = lod_sub_declare_delete(env, next,
8474 (const struct dt_key *)stripe_name, th);
8478 rc = lod_sub_declare_ref_del(env, next, th);
8486 static int dt_dir_is_empty(const struct lu_env *env,
8487 struct dt_object *obj)
8490 const struct dt_it_ops *iops;
8495 if (!dt_try_as_dir(env, obj, true))
8498 iops = &obj->do_index_ops->dio_it;
8499 it = iops->init(env, obj, LUDA_64BITHASH);
8501 RETURN(PTR_ERR(it));
8503 rc = iops->get(env, it, (const struct dt_key *)"");
8507 for (rc = 0, i = 0; rc == 0 && i < 3; ++i)
8508 rc = iops->next(env, it);
8514 /* Huh? Index contains no zero key? */
8519 iops->fini(env, it);
8524 static int lod_dir_declare_layout_shrink(const struct lu_env *env,
8525 struct dt_object *dt,
8526 const struct md_layout_change *mlc,
8529 struct lod_thread_info *info = lod_env_info(env);
8530 struct lod_object *lo = lod_dt_obj(dt);
8531 struct dt_object *next = dt_object_child(dt);
8532 struct lmv_user_md *lmu = mlc->mlc_buf.lb_buf;
8533 char *stripe_name = info->lti_key;
8534 struct lu_buf *lmv_buf = &info->lti_buf;
8535 __u32 final_stripe_count;
8536 struct dt_object *dto;
8542 if (!dt_try_as_dir(env, dt, true))
8545 /* shouldn't be called on plain directory */
8546 LASSERT(lo->ldo_dir_stripe_count);
8548 lmv_buf->lb_buf = &info->lti_lmv.lmv_md_v1;
8549 lmv_buf->lb_len = sizeof(info->lti_lmv.lmv_md_v1);
8551 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
8552 LASSERT(final_stripe_count &&
8553 final_stripe_count < lo->ldo_dir_stripe_count);
8555 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8556 dto = lo->ldo_stripe[i];
8560 if (i < final_stripe_count) {
8561 rc = lod_sub_declare_xattr_set(env, dto, lmv_buf,
8563 LU_XATTR_REPLACE, th);
8570 rc = dt_dir_is_empty(env, dto);
8574 rc = lod_sub_declare_ref_del(env, dto, th);
8578 rc = lod_sub_declare_destroy(env, dto, th);
8582 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8583 PFID(lu_object_fid(&dto->do_lu)), i);
8585 rc = lod_sub_declare_delete(env, next,
8586 (const struct dt_key *)stripe_name, th);
8590 rc = lod_sub_declare_ref_del(env, next, th);
8595 rc = lod_sub_declare_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV,
8596 LU_XATTR_REPLACE, th);
8601 * Allocate stripes for split directory.
8603 * \param[in] env execution environment
8604 * \param[in] dt target object
8605 * \param[in] mlc layout change data
8606 * \param[in] th transaction handle
8608 * \retval 0 on success
8609 * \retval negative if failed
8611 static int lod_dir_declare_layout_split(const struct lu_env *env,
8612 struct dt_object *dt,
8613 const struct md_layout_change *mlc,
8616 struct lod_thread_info *info = lod_env_info(env);
8617 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
8618 struct lod_object *lo = lod_dt_obj(dt);
8619 struct dt_object_format *dof = &info->lti_format;
8620 struct lmv_user_md_v1 *lum = mlc->mlc_spec->u.sp_ea.eadata;
8621 struct dt_object **stripes;
8629 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
8630 LASSERT(le32_to_cpu(lum->lum_stripe_offset) == LMV_OFFSET_DEFAULT);
8632 saved_count = lo->ldo_dir_stripes_allocated;
8633 stripe_count = le32_to_cpu(lum->lum_stripe_count);
8634 if (stripe_count <= saved_count)
8637 dof->dof_type = DFT_DIR;
8639 OBD_ALLOC(stripes, sizeof(*stripes) * stripe_count);
8643 for (i = 0; i < lo->ldo_dir_stripes_allocated; i++)
8644 stripes[i] = lo->ldo_stripe[i];
8646 lod_qos_statfs_update(env, lod, &lod->lod_mdt_descs);
8647 rc = lod_mdt_alloc_qos(env, lo, stripes, saved_count, stripe_count);
8649 rc = lod_mdt_alloc_rr(env, lo, stripes, saved_count,
8652 OBD_FREE(stripes, sizeof(*stripes) * stripe_count);
8656 LASSERT(rc > saved_count);
8657 OBD_FREE(lo->ldo_stripe,
8658 sizeof(*stripes) * lo->ldo_dir_stripes_allocated);
8659 lo->ldo_stripe = stripes;
8660 lo->ldo_is_foreign = 0;
8661 lo->ldo_dir_striped = 1;
8662 lo->ldo_dir_stripe_count = rc;
8663 lo->ldo_dir_stripes_allocated = stripe_count;
8664 lo->ldo_dir_split_hash = lo->ldo_dir_hash_type;
8665 lo->ldo_dir_hash_type = le32_to_cpu(lum->lum_hash_type);
8666 if (!lmv_is_known_hash_type(lo->ldo_dir_hash_type))
8667 lo->ldo_dir_hash_type =
8668 lod->lod_mdt_descs.ltd_lmv_desc.ld_pattern;
8669 lo->ldo_dir_hash_type |= LMV_HASH_FLAG_SPLIT | LMV_HASH_FLAG_MIGRATION;
8670 lo->ldo_dir_split_offset = saved_count;
8671 lo->ldo_dir_layout_version++;
8672 lo->ldo_dir_stripe_loaded = 1;
8674 rc = lod_dir_declare_create_stripes(env, dt, mlc->mlc_attr, dof, th);
8676 lod_striping_free(env, lo);
8682 * detach all stripes from dir master object, NB, stripes are not destroyed, but
8683 * deleted from it's parent namespace, this function is called in two places:
8684 * 1. mdd_migrate_mdt() detach stripes from source, and attach them to
8686 * 2. mdd_dir_layout_update() detach stripe before turning 1-stripe directory to
8687 * a plain directory.
8689 * \param[in] env execution environment
8690 * \param[in] dt target object
8691 * \param[in] mlc layout change data
8692 * \param[in] th transaction handle
8694 * \retval 0 on success
8695 * \retval negative if failed
8697 static int lod_dir_layout_detach(const struct lu_env *env,
8698 struct dt_object *dt,
8699 const struct md_layout_change *mlc,
8702 struct lod_thread_info *info = lod_env_info(env);
8703 struct lod_object *lo = lod_dt_obj(dt);
8704 struct dt_object *next = dt_object_child(dt);
8705 char *stripe_name = info->lti_key;
8706 struct dt_object *dto;
8712 if (!lo->ldo_dir_stripe_count) {
8713 /* plain directory delete .. */
8714 rc = lod_sub_delete(env, next,
8715 (const struct dt_key *)dotdot, th);
8719 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8720 dto = lo->ldo_stripe[i];
8724 rc = lod_sub_delete(env, dto,
8725 (const struct dt_key *)dotdot, th);
8729 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8730 PFID(lu_object_fid(&dto->do_lu)), i);
8732 rc = lod_sub_delete(env, next,
8733 (const struct dt_key *)stripe_name, th);
8737 rc = lod_sub_ref_del(env, next, th);
8742 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8743 dto = lo->ldo_stripe[i];
8745 dt_object_put(env, dto);
8747 OBD_FREE_PTR_ARRAY(lo->ldo_stripe, lo->ldo_dir_stripes_allocated);
8748 lo->ldo_stripe = NULL;
8749 lo->ldo_dir_stripes_allocated = 0;
8750 lo->ldo_dir_stripe_count = 0;
8751 dt->do_index_ops = &lod_index_ops;
8756 static int lod_dir_layout_shrink(const struct lu_env *env,
8757 struct dt_object *dt,
8758 const struct md_layout_change *mlc,
8761 struct lod_thread_info *info = lod_env_info(env);
8762 struct lod_object *lo = lod_dt_obj(dt);
8763 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
8764 struct dt_object *next = dt_object_child(dt);
8765 struct lmv_user_md *lmu = mlc->mlc_buf.lb_buf;
8766 __u32 final_stripe_count;
8767 char *stripe_name = info->lti_key;
8768 struct dt_object *dto;
8769 struct lu_buf *lmv_buf = &info->lti_buf;
8770 struct lmv_mds_md_v1 *lmv = &info->lti_lmv.lmv_md_v1;
8772 int type = LU_SEQ_RANGE_ANY;
8778 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
8780 lmv_buf->lb_buf = lmv;
8781 lmv_buf->lb_len = sizeof(*lmv);
8782 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
8783 lmv->lmv_stripe_count = cpu_to_le32(final_stripe_count);
8784 lmv->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type) &
8785 cpu_to_le32(LMV_HASH_TYPE_MASK |
8786 LMV_HASH_FLAG_FIXED);
8787 lmv->lmv_layout_version =
8788 cpu_to_le32(lo->ldo_dir_layout_version + 1);
8789 lmv->lmv_migrate_offset = 0;
8790 lmv->lmv_migrate_hash = 0;
8792 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8793 dto = lo->ldo_stripe[i];
8797 if (i < final_stripe_count) {
8798 rc = lod_fld_lookup(env, lod,
8799 lu_object_fid(&dto->do_lu),
8804 lmv->lmv_master_mdt_index = cpu_to_le32(mdtidx);
8805 rc = lod_sub_xattr_set(env, dto, lmv_buf,
8807 LU_XATTR_REPLACE, th);
8814 dt_write_lock(env, dto, DT_TGT_CHILD);
8815 rc = lod_sub_ref_del(env, dto, th);
8816 dt_write_unlock(env, dto);
8820 rc = lod_sub_destroy(env, dto, th);
8824 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8825 PFID(lu_object_fid(&dto->do_lu)), i);
8827 rc = lod_sub_delete(env, next,
8828 (const struct dt_key *)stripe_name, th);
8832 rc = lod_sub_ref_del(env, next, th);
8837 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &mdtidx,
8842 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
8843 lmv->lmv_master_mdt_index = cpu_to_le32(mdtidx);
8844 rc = lod_sub_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV,
8845 LU_XATTR_REPLACE, th);
8849 for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
8850 dto = lo->ldo_stripe[i];
8852 dt_object_put(env, dto);
8854 lo->ldo_dir_stripe_count = final_stripe_count;
8859 static mlc_handler dir_mlc_declare_ops[MD_LAYOUT_MAX] = {
8860 [MD_LAYOUT_ATTACH] = lod_dir_declare_layout_attach,
8861 [MD_LAYOUT_DETACH] = lod_dir_declare_layout_detach,
8862 [MD_LAYOUT_SHRINK] = lod_dir_declare_layout_shrink,
8863 [MD_LAYOUT_SPLIT] = lod_dir_declare_layout_split,
8866 static mlc_handler dir_mlc_ops[MD_LAYOUT_MAX] = {
8867 [MD_LAYOUT_DETACH] = lod_dir_layout_detach,
8868 [MD_LAYOUT_SHRINK] = lod_dir_layout_shrink,
8871 static int lod_declare_layout_change(const struct lu_env *env,
8872 struct dt_object *dt, struct md_layout_change *mlc,
8875 struct lod_thread_info *info = lod_env_info(env);
8876 struct lod_object *lo = lod_dt_obj(dt);
8881 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
8882 LASSERT(dir_mlc_declare_ops[mlc->mlc_opc]);
8883 rc = dir_mlc_declare_ops[mlc->mlc_opc](env, dt, mlc, th);
8887 if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
8888 dt_object_remote(dt_object_child(dt)))
8891 rc = lod_striping_load(env, lo);
8895 LASSERT(lo->ldo_comp_cnt > 0);
8897 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
8901 switch (lo->ldo_flr_state) {
8903 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
8907 rc = lod_declare_update_rdonly(env, lo, mlc, th);
8909 case LCM_FL_WRITE_PENDING:
8910 rc = lod_declare_update_write_pending(env, lo, mlc, th);
8912 case LCM_FL_SYNC_PENDING:
8913 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
8924 * Instantiate layout component objects which covers the intent write offset.
8926 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
8927 struct md_layout_change *mlc, struct thandle *th)
8929 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
8930 struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
8931 struct lod_object *lo = lod_dt_obj(dt);
8936 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
8937 LASSERT(dir_mlc_ops[mlc->mlc_opc]);
8938 rc = dir_mlc_ops[mlc->mlc_opc](env, dt, mlc, th);
8942 rc = lod_striped_create(env, dt, attr, NULL, th);
8943 if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
8944 layout_attr->la_layout_version |= lo->ldo_layout_gen;
8945 rc = lod_attr_set(env, dt, layout_attr, th);
8951 const struct dt_object_operations lod_obj_ops = {
8952 .do_read_lock = lod_read_lock,
8953 .do_write_lock = lod_write_lock,
8954 .do_read_unlock = lod_read_unlock,
8955 .do_write_unlock = lod_write_unlock,
8956 .do_write_locked = lod_write_locked,
8957 .do_attr_get = lod_attr_get,
8958 .do_declare_attr_set = lod_declare_attr_set,
8959 .do_attr_set = lod_attr_set,
8960 .do_xattr_get = lod_xattr_get,
8961 .do_declare_xattr_set = lod_declare_xattr_set,
8962 .do_xattr_set = lod_xattr_set,
8963 .do_declare_xattr_del = lod_declare_xattr_del,
8964 .do_xattr_del = lod_xattr_del,
8965 .do_xattr_list = lod_xattr_list,
8966 .do_ah_init = lod_ah_init,
8967 .do_declare_create = lod_declare_create,
8968 .do_create = lod_create,
8969 .do_declare_destroy = lod_declare_destroy,
8970 .do_destroy = lod_destroy,
8971 .do_index_try = lod_index_try,
8972 .do_declare_ref_add = lod_declare_ref_add,
8973 .do_ref_add = lod_ref_add,
8974 .do_declare_ref_del = lod_declare_ref_del,
8975 .do_ref_del = lod_ref_del,
8976 .do_object_sync = lod_object_sync,
8977 .do_object_lock = lod_object_lock,
8978 .do_object_unlock = lod_object_unlock,
8979 .do_invalidate = lod_invalidate,
8980 .do_declare_layout_change = lod_declare_layout_change,
8981 .do_layout_change = lod_layout_change,
8985 * Implementation of dt_body_operations::dbo_read.
8987 * \see dt_body_operations::dbo_read() in the API description for details.
8989 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
8990 struct lu_buf *buf, loff_t *pos)
8992 struct dt_object *next = dt_object_child(dt);
8994 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
8995 S_ISLNK(dt->do_lu.lo_header->loh_attr));
8996 return next->do_body_ops->dbo_read(env, next, buf, pos);
9000 * Implementation of dt_body_operations::dbo_declare_write.
9002 * \see dt_body_operations::dbo_declare_write() in the API description
9005 static ssize_t lod_declare_write(const struct lu_env *env,
9006 struct dt_object *dt,
9007 const struct lu_buf *buf, loff_t pos,
9010 return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
9014 * Implementation of dt_body_operations::dbo_write.
9016 * \see dt_body_operations::dbo_write() in the API description for details.
9018 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
9019 const struct lu_buf *buf, loff_t *pos,
9022 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
9023 S_ISLNK(dt->do_lu.lo_header->loh_attr));
9024 return lod_sub_write(env, dt_object_child(dt), buf, pos, th);
9027 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
9028 __u64 start, __u64 end, struct thandle *th)
9030 if (dt_object_remote(dt))
9033 return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
9036 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
9037 __u64 start, __u64 end, struct thandle *th)
9039 if (dt_object_remote(dt))
9042 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
9043 return lod_sub_punch(env, dt_object_child(dt), start, end, th);
9047 * different type of files use the same body_ops because object may be created
9048 * in OUT, where there is no chance to set correct body_ops for each type, so
9049 * body_ops themselves will check file type inside, see lod_read/write/punch for
9052 static const struct dt_body_operations lod_body_ops = {
9053 .dbo_read = lod_read,
9054 .dbo_declare_write = lod_declare_write,
9055 .dbo_write = lod_write,
9056 .dbo_declare_punch = lod_declare_punch,
9057 .dbo_punch = lod_punch,
9061 * Implementation of lu_object_operations::loo_object_init.
9063 * The function determines the type and the index of the target device using
9064 * sequence of the object's FID. Then passes control down to the
9065 * corresponding device:
9066 * OSD for the local objects, OSP for remote
9068 * \see lu_object_operations::loo_object_init() in the API description
9071 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
9072 const struct lu_object_conf *conf)
9074 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
9075 struct lu_device *cdev = NULL;
9076 struct lu_object *cobj;
9077 struct lod_tgt_descs *ltd = NULL;
9078 struct lod_tgt_desc *tgt;
9080 int type = LU_SEQ_RANGE_ANY;
9084 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
9088 if (type == LU_SEQ_RANGE_MDT &&
9089 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
9090 cdev = &lod->lod_child->dd_lu_dev;
9091 } else if (type == LU_SEQ_RANGE_MDT) {
9092 ltd = &lod->lod_mdt_descs;
9094 } else if (type == LU_SEQ_RANGE_OST) {
9095 ltd = &lod->lod_ost_descs;
9102 if (ltd->ltd_tgts_size > idx &&
9103 test_bit(idx, ltd->ltd_tgt_bitmap)) {
9104 tgt = LTD_TGT(ltd, idx);
9106 LASSERT(tgt != NULL);
9107 LASSERT(tgt->ltd_tgt != NULL);
9109 cdev = &(tgt->ltd_tgt->dd_lu_dev);
9111 lod_putref(lod, ltd);
9114 if (unlikely(cdev == NULL))
9117 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
9118 if (unlikely(cobj == NULL))
9121 lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
9123 lu_object_add(lo, cobj);
9130 * Release resources associated with striping.
9132 * If the object is striped (regular or directory), then release
9133 * the stripe objects references and free the ldo_stripe array.
9135 * \param[in] env execution environment
9136 * \param[in] lo object
9138 void lod_striping_free_nolock(const struct lu_env *env, struct lod_object *lo)
9140 struct lod_layout_component *lod_comp;
9141 __u32 obj_attr = lo->ldo_obj.do_lu.lo_header->loh_attr;
9144 if (unlikely(lo->ldo_is_foreign)) {
9145 if (S_ISREG(obj_attr)) {
9146 lod_free_foreign_lov(lo);
9147 lo->ldo_comp_cached = 0;
9148 } else if (S_ISDIR(obj_attr)) {
9149 lod_free_foreign_lmv(lo);
9150 lo->ldo_dir_stripe_loaded = 0;
9152 } else if (lo->ldo_stripe != NULL) {
9153 LASSERT(lo->ldo_comp_entries == NULL);
9154 LASSERT(lo->ldo_dir_stripes_allocated > 0);
9156 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
9157 if (lo->ldo_stripe[i])
9158 dt_object_put(env, lo->ldo_stripe[i]);
9161 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
9162 OBD_FREE(lo->ldo_stripe, j);
9163 lo->ldo_stripe = NULL;
9164 lo->ldo_dir_stripes_allocated = 0;
9165 lo->ldo_dir_stripe_loaded = 0;
9166 lo->ldo_dir_stripe_count = 0;
9167 lo->ldo_obj.do_index_ops = NULL;
9168 } else if (lo->ldo_comp_entries != NULL) {
9169 for (i = 0; i < lo->ldo_comp_cnt; i++) {
9170 /* free lod_layout_component::llc_stripe array */
9171 lod_comp = &lo->ldo_comp_entries[i];
9173 if (lod_comp->llc_stripe == NULL)
9175 LASSERT(lod_comp->llc_stripes_allocated != 0);
9176 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
9177 if (lod_comp->llc_stripe[j] != NULL)
9179 &lod_comp->llc_stripe[j]->do_lu);
9181 OBD_FREE_PTR_ARRAY(lod_comp->llc_stripe,
9182 lod_comp->llc_stripes_allocated);
9183 lod_comp->llc_stripe = NULL;
9184 OBD_FREE_PTR_ARRAY(lod_comp->llc_ost_indices,
9185 lod_comp->llc_stripes_allocated);
9186 lod_comp->llc_ost_indices = NULL;
9187 lod_comp->llc_stripes_allocated = 0;
9189 lod_free_comp_entries(lo);
9190 lo->ldo_comp_cached = 0;
9194 void lod_striping_free(const struct lu_env *env, struct lod_object *lo)
9196 mutex_lock(&lo->ldo_layout_mutex);
9197 lod_striping_free_nolock(env, lo);
9198 mutex_unlock(&lo->ldo_layout_mutex);
9202 * Implementation of lu_object_operations::loo_object_free.
9204 * \see lu_object_operations::loo_object_free() in the API description
9207 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
9209 struct lod_object *lo = lu2lod_obj(o);
9211 /* release all underlying object pinned */
9212 lod_striping_free(env, lo);
9214 /* lo doesn't contain a lu_object_header, so we don't need call_rcu */
9215 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
9219 * Implementation of lu_object_operations::loo_object_release.
9221 * \see lu_object_operations::loo_object_release() in the API description
9224 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
9226 /* XXX: shouldn't we release everything here in case if object
9227 * creation failed before? */
9231 * Implementation of lu_object_operations::loo_object_print.
9233 * \see lu_object_operations::loo_object_print() in the API description
9236 static int lod_object_print(const struct lu_env *env, void *cookie,
9237 lu_printer_t p, const struct lu_object *l)
9239 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
9241 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
9244 const struct lu_object_operations lod_lu_obj_ops = {
9245 .loo_object_init = lod_object_init,
9246 .loo_object_free = lod_object_free,
9247 .loo_object_release = lod_object_release,
9248 .loo_object_print = lod_object_print,