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 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 struct dt_index_operations lod_striped_index_ops = {
791 .dio_lookup = lod_striped_lookup,
792 .dio_declare_insert = lod_declare_insert,
793 .dio_insert = lod_insert,
794 .dio_declare_delete = lod_declare_delete,
795 .dio_delete = lod_delete,
797 .init = lod_striped_it_init,
798 .fini = lod_striped_it_fini,
799 .get = lod_striped_it_get,
800 .put = lod_striped_it_put,
801 .next = lod_striped_it_next,
802 .key = lod_striped_it_key,
803 .key_size = lod_striped_it_key_size,
804 .rec = lod_striped_it_rec,
805 .rec_size = lod_striped_it_rec_size,
806 .store = lod_striped_it_store,
807 .load = lod_striped_it_load,
812 * Append the FID for each shard of the striped directory after the
813 * given LMV EA header.
815 * To simplify striped directory and the consistency verification,
816 * we only store the LMV EA header on disk, for both master object
817 * and slave objects. When someone wants to know the whole LMV EA,
818 * such as client readdir(), we can build the entrie LMV EA on the
819 * MDT side (in RAM) via iterating the sub-directory entries that
820 * are contained in the master object of the stripe directory.
822 * For the master object of the striped directroy, the valid name
823 * for each shard is composed of the ${shard_FID}:${shard_idx}.
825 * There may be holes in the LMV EA if some shards' name entries
826 * are corrupted or lost.
828 * \param[in] env pointer to the thread context
829 * \param[in] lo pointer to the master object of the striped directory
830 * \param[in] buf pointer to the lu_buf which will hold the LMV EA
831 * \param[in] resize whether re-allocate the buffer if it is not big enough
833 * \retval positive size of the LMV EA
834 * \retval 0 for nothing to be loaded
835 * \retval negative error number on failure
837 int lod_load_lmv_shards(const struct lu_env *env, struct lod_object *lo,
838 struct lu_buf *buf, bool resize)
840 struct lu_dirent *ent =
841 (struct lu_dirent *)lod_env_info(env)->lti_key;
842 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
843 struct dt_object *obj = dt_object_child(&lo->ldo_obj);
844 struct lmv_mds_md_v1 *lmv1 = buf->lb_buf;
846 const struct dt_it_ops *iops;
848 __u32 magic = le32_to_cpu(lmv1->lmv_magic);
853 if (magic != LMV_MAGIC_V1)
856 stripes = le32_to_cpu(lmv1->lmv_stripe_count);
860 rc = lmv_mds_md_size(stripes, magic);
864 if (buf->lb_len < lmv1_size) {
873 lu_buf_alloc(buf, lmv1_size);
878 memcpy(buf->lb_buf, tbuf.lb_buf, tbuf.lb_len);
881 if (unlikely(!dt_try_as_dir(env, obj)))
884 memset(&lmv1->lmv_stripe_fids[0], 0, stripes * sizeof(struct lu_fid));
885 iops = &obj->do_index_ops->dio_it;
886 it = iops->init(env, obj, LUDA_64BITHASH);
890 rc = iops->load(env, it, 0);
892 rc = iops->next(env, it);
897 char name[FID_LEN + 2] = "";
902 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
908 fid_le_to_cpu(&fid, &ent->lde_fid);
909 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
910 if (ent->lde_name[0] == '.') {
911 if (ent->lde_namelen == 1)
914 if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
918 len = scnprintf(name, sizeof(name),
919 DFID":", PFID(&ent->lde_fid));
920 /* The ent->lde_name is composed of ${FID}:${index} */
921 if (ent->lde_namelen < len + 1 ||
922 memcmp(ent->lde_name, name, len) != 0) {
923 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
924 "%s: invalid shard name %.*s with the FID "DFID
925 " for the striped directory "DFID", %s\n",
926 lod2obd(lod)->obd_name, ent->lde_namelen,
927 ent->lde_name, PFID(&fid),
928 PFID(lu_object_fid(&obj->do_lu)),
929 lod->lod_lmv_failout ? "failout" : "skip");
931 if (lod->lod_lmv_failout)
939 if (ent->lde_name[len] < '0' ||
940 ent->lde_name[len] > '9') {
941 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
942 "%s: invalid shard name %.*s with the "
943 "FID "DFID" for the striped directory "
945 lod2obd(lod)->obd_name, ent->lde_namelen,
946 ent->lde_name, PFID(&fid),
947 PFID(lu_object_fid(&obj->do_lu)),
948 lod->lod_lmv_failout ?
951 if (lod->lod_lmv_failout)
957 index = index * 10 + ent->lde_name[len++] - '0';
958 } while (len < ent->lde_namelen);
960 if (len == ent->lde_namelen) {
961 /* Out of LMV EA range. */
962 if (index >= stripes) {
963 CERROR("%s: the shard %.*s for the striped "
964 "directory "DFID" is out of the known "
965 "LMV EA range [0 - %u], failout\n",
966 lod2obd(lod)->obd_name, ent->lde_namelen,
968 PFID(lu_object_fid(&obj->do_lu)),
974 /* The slot has been occupied. */
975 if (!fid_is_zero(&lmv1->lmv_stripe_fids[index])) {
979 &lmv1->lmv_stripe_fids[index]);
980 CERROR("%s: both the shard "DFID" and "DFID
981 " for the striped directory "DFID
982 " claim the same LMV EA slot at the "
983 "index %d, failout\n",
984 lod2obd(lod)->obd_name,
985 PFID(&fid0), PFID(&fid),
986 PFID(lu_object_fid(&obj->do_lu)), index);
991 /* stored as LE mode */
992 lmv1->lmv_stripe_fids[index] = ent->lde_fid;
995 rc = iops->next(env, it);
1000 iops->fini(env, it);
1002 RETURN(rc > 0 ? lmv_mds_md_size(stripes, magic) : rc);
1006 * Implementation of dt_object_operations::do_index_try.
1008 * \see dt_object_operations::do_index_try() in the API description for details.
1010 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
1011 const struct dt_index_features *feat)
1013 struct lod_object *lo = lod_dt_obj(dt);
1014 struct dt_object *next = dt_object_child(dt);
1018 LASSERT(next->do_ops);
1019 LASSERT(next->do_ops->do_index_try);
1021 rc = lod_striping_load(env, lo);
1025 rc = next->do_ops->do_index_try(env, next, feat);
1029 if (lo->ldo_dir_stripe_count > 0) {
1032 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1033 if (!lo->ldo_stripe[i])
1035 if (!dt_object_exists(lo->ldo_stripe[i]))
1037 rc = lo->ldo_stripe[i]->do_ops->do_index_try(env,
1038 lo->ldo_stripe[i], feat);
1042 dt->do_index_ops = &lod_striped_index_ops;
1044 dt->do_index_ops = &lod_index_ops;
1051 * Implementation of dt_object_operations::do_read_lock.
1053 * \see dt_object_operations::do_read_lock() in the API description for details.
1055 static void lod_read_lock(const struct lu_env *env, struct dt_object *dt,
1058 dt_read_lock(env, dt_object_child(dt), role);
1062 * Implementation of dt_object_operations::do_write_lock.
1064 * \see dt_object_operations::do_write_lock() in the API description for
1067 static void lod_write_lock(const struct lu_env *env, struct dt_object *dt,
1070 dt_write_lock(env, dt_object_child(dt), role);
1074 * Implementation of dt_object_operations::do_read_unlock.
1076 * \see dt_object_operations::do_read_unlock() in the API description for
1079 static void lod_read_unlock(const struct lu_env *env, struct dt_object *dt)
1081 dt_read_unlock(env, dt_object_child(dt));
1085 * Implementation of dt_object_operations::do_write_unlock.
1087 * \see dt_object_operations::do_write_unlock() in the API description for
1090 static void lod_write_unlock(const struct lu_env *env, struct dt_object *dt)
1092 dt_write_unlock(env, dt_object_child(dt));
1096 * Implementation of dt_object_operations::do_write_locked.
1098 * \see dt_object_operations::do_write_locked() in the API description for
1101 static int lod_write_locked(const struct lu_env *env, struct dt_object *dt)
1103 return dt_write_locked(env, dt_object_child(dt));
1107 * Implementation of dt_object_operations::do_attr_get.
1109 * \see dt_object_operations::do_attr_get() in the API description for details.
1111 static int lod_attr_get(const struct lu_env *env,
1112 struct dt_object *dt,
1113 struct lu_attr *attr)
1115 /* Note: for striped directory, client will merge attributes
1116 * from all of the sub-stripes see lmv_merge_attr(), and there
1117 * no MDD logic depend on directory nlink/size/time, so we can
1118 * always use master inode nlink and size for now. */
1119 return dt_attr_get(env, dt_object_child(dt), attr);
1122 void lod_adjust_stripe_size(struct lod_layout_component *comp,
1123 __u32 def_stripe_size)
1125 __u64 comp_end = comp->llc_extent.e_end;
1127 /* Choose stripe size if not set. Note that default stripe size can't
1128 * be used as is, because it must be multiplier of given component end.
1129 * - first check if default stripe size can be used
1130 * - if not than select the lowest set bit from component end and use
1131 * that value as stripe size
1133 if (!comp->llc_stripe_size) {
1134 if (comp_end == LUSTRE_EOF || !(comp_end % def_stripe_size))
1135 comp->llc_stripe_size = def_stripe_size;
1137 comp->llc_stripe_size = comp_end & ~(comp_end - 1);
1139 /* check stripe size is multiplier of comp_end */
1140 if (comp_end != LUSTRE_EOF &&
1141 comp_end % comp->llc_stripe_size) {
1142 /* fix that even for defined stripe size but warn
1143 * about the problem, that must not happen
1145 CWARN("Component end %llu is not aligned by the stripe size %u\n",
1146 comp_end, comp->llc_stripe_size);
1148 comp->llc_stripe_size = comp_end & ~(comp_end - 1);
1153 static inline void lod_adjust_stripe_info(struct lod_layout_component *comp,
1154 struct lov_desc *desc,
1157 if (comp->llc_pattern != LOV_PATTERN_MDT) {
1158 if (append_stripes) {
1159 comp->llc_stripe_count = append_stripes;
1160 } else if (!comp->llc_stripe_count) {
1161 comp->llc_stripe_count =
1162 desc->ld_default_stripe_count;
1166 lod_adjust_stripe_size(comp, desc->ld_default_stripe_size);
1169 int lod_obj_for_each_stripe(const struct lu_env *env, struct lod_object *lo,
1171 struct lod_obj_stripe_cb_data *data)
1173 struct lod_layout_component *lod_comp;
1177 LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
1178 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1179 lod_comp = &lo->ldo_comp_entries[i];
1181 if (lod_comp->llc_stripe == NULL)
1184 /* has stripe but not inited yet, this component has been
1185 * declared to be created, but hasn't created yet.
1187 if (!lod_comp_inited(lod_comp))
1190 if (data->locd_comp_skip_cb &&
1191 data->locd_comp_skip_cb(env, lo, i, data))
1194 if (data->locd_comp_cb) {
1195 rc = data->locd_comp_cb(env, lo, i, data);
1200 /* could used just to do sth about component, not each
1203 if (!data->locd_stripe_cb)
1206 LASSERT(lod_comp->llc_stripe_count > 0);
1207 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
1208 struct dt_object *dt = lod_comp->llc_stripe[j];
1212 rc = data->locd_stripe_cb(env, lo, dt, th, i, j, data);
1220 static bool lod_obj_attr_set_comp_skip_cb(const struct lu_env *env,
1221 struct lod_object *lo, int comp_idx,
1222 struct lod_obj_stripe_cb_data *data)
1224 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
1225 bool skipped = false;
1227 if (!(data->locd_attr->la_valid & LA_LAYOUT_VERSION))
1230 switch (lo->ldo_flr_state) {
1231 case LCM_FL_WRITE_PENDING: {
1234 /* skip stale components */
1235 if (lod_comp->llc_flags & LCME_FL_STALE) {
1240 /* skip valid and overlapping components, therefore any
1241 * attempts to write overlapped components will never succeed
1242 * because client will get EINPROGRESS. */
1243 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1247 if (lo->ldo_comp_entries[i].llc_flags & LCME_FL_STALE)
1250 if (lu_extent_is_overlapped(&lod_comp->llc_extent,
1251 &lo->ldo_comp_entries[i].llc_extent)) {
1259 LASSERTF(0, "impossible: %d\n", lo->ldo_flr_state);
1260 case LCM_FL_SYNC_PENDING:
1264 CDEBUG(D_LAYOUT, DFID": %s to set component %x to version: %u\n",
1265 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
1266 skipped ? "skipped" : "chose", lod_comp->llc_id,
1267 data->locd_attr->la_layout_version);
1273 lod_obj_stripe_attr_set_cb(const struct lu_env *env, struct lod_object *lo,
1274 struct dt_object *dt, struct thandle *th,
1275 int comp_idx, int stripe_idx,
1276 struct lod_obj_stripe_cb_data *data)
1278 if (data->locd_declare)
1279 return lod_sub_declare_attr_set(env, dt, data->locd_attr, th);
1281 if (data->locd_attr->la_valid & LA_LAYOUT_VERSION) {
1282 CDEBUG(D_LAYOUT, DFID": set layout version: %u, comp_idx: %d\n",
1283 PFID(lu_object_fid(&dt->do_lu)),
1284 data->locd_attr->la_layout_version, comp_idx);
1287 return lod_sub_attr_set(env, dt, data->locd_attr, th);
1291 * Implementation of dt_object_operations::do_declare_attr_set.
1293 * If the object is striped, then apply the changes to all the stripes.
1295 * \see dt_object_operations::do_declare_attr_set() in the API description
1298 static int lod_declare_attr_set(const struct lu_env *env,
1299 struct dt_object *dt,
1300 const struct lu_attr *attr,
1303 struct dt_object *next = dt_object_child(dt);
1304 struct lod_object *lo = lod_dt_obj(dt);
1309 * declare setattr on the local object
1311 rc = lod_sub_declare_attr_set(env, next, attr, th);
1315 /* osp_declare_attr_set() ignores all attributes other than
1316 * UID, GID, PROJID, and size, and osp_attr_set() ignores all
1317 * but UID, GID and PROJID. Declaration of size attr setting
1318 * happens through lod_declare_init_size(), and not through
1319 * this function. Therefore we need not load striping unless
1320 * ownership is changing. This should save memory and (we hope)
1321 * speed up rename().
1323 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1324 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1327 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1330 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID | LA_MODE |
1331 LA_ATIME | LA_MTIME | LA_CTIME |
1336 * load striping information, notice we don't do this when object
1337 * is being initialized as we don't need this information till
1338 * few specific cases like destroy, chown
1340 rc = lod_striping_load(env, lo);
1344 if (!lod_obj_is_striped(dt))
1348 * if object is striped declare changes on the stripes
1350 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1351 LASSERT(lo->ldo_stripe);
1352 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1353 if (lo->ldo_stripe[i] == NULL)
1355 if (!dt_object_exists(lo->ldo_stripe[i]))
1357 rc = lod_sub_declare_attr_set(env, lo->ldo_stripe[i],
1363 struct lod_obj_stripe_cb_data data = { { 0 } };
1365 data.locd_attr = attr;
1366 data.locd_declare = true;
1367 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1368 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1374 if (!dt_object_exists(next) || dt_object_remote(next) ||
1375 !S_ISREG(attr->la_mode))
1378 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1379 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
1383 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1384 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1385 struct lod_thread_info *info = lod_env_info(env);
1386 struct lu_buf *buf = &info->lti_buf;
1388 buf->lb_buf = info->lti_ea_store;
1389 buf->lb_len = info->lti_ea_store_size;
1390 rc = lod_sub_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1391 LU_XATTR_REPLACE, th);
1398 * Implementation of dt_object_operations::do_attr_set.
1400 * If the object is striped, then apply the changes to all or subset of
1401 * the stripes depending on the object type and specific attributes.
1403 * \see dt_object_operations::do_attr_set() in the API description for details.
1405 static int lod_attr_set(const struct lu_env *env,
1406 struct dt_object *dt,
1407 const struct lu_attr *attr,
1410 struct dt_object *next = dt_object_child(dt);
1411 struct lod_object *lo = lod_dt_obj(dt);
1416 * apply changes to the local object
1418 rc = lod_sub_attr_set(env, next, attr, th);
1422 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1423 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1426 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1429 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE | LA_PROJID |
1430 LA_ATIME | LA_MTIME | LA_CTIME |
1435 /* FIXME: a tricky case in the code path of mdd_layout_change():
1436 * the in-memory striping information has been freed in lod_xattr_set()
1437 * due to layout change. It has to load stripe here again. It only
1438 * changes flags of layout so declare_attr_set() is still accurate */
1439 rc = lod_striping_load(env, lo);
1443 if (!lod_obj_is_striped(dt))
1447 * if object is striped, apply changes to all the stripes
1449 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1450 LASSERT(lo->ldo_stripe);
1451 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1452 if (unlikely(lo->ldo_stripe[i] == NULL))
1455 if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1458 rc = lod_sub_attr_set(env, lo->ldo_stripe[i], attr, th);
1463 struct lod_obj_stripe_cb_data data = { { 0 } };
1465 data.locd_attr = attr;
1466 data.locd_declare = false;
1467 data.locd_comp_skip_cb = lod_obj_attr_set_comp_skip_cb;
1468 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1469 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1475 if (!dt_object_exists(next) || dt_object_remote(next) ||
1476 !S_ISREG(attr->la_mode))
1479 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1480 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
1484 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1485 struct lod_thread_info *info = lod_env_info(env);
1486 struct lu_buf *buf = &info->lti_buf;
1487 struct ost_id *oi = &info->lti_ostid;
1488 struct lu_fid *fid = &info->lti_fid;
1489 struct lov_mds_md_v1 *lmm;
1490 struct lov_ost_data_v1 *objs;
1493 rc = lod_get_lov_ea(env, lo);
1497 buf->lb_buf = info->lti_ea_store;
1498 buf->lb_len = info->lti_ea_store_size;
1499 lmm = info->lti_ea_store;
1500 magic = le32_to_cpu(lmm->lmm_magic);
1501 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
1502 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1503 struct lov_comp_md_entry_v1 *lcme =
1504 &lcm->lcm_entries[0];
1506 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1507 magic = le32_to_cpu(lmm->lmm_magic);
1510 if (magic == LOV_MAGIC_V1)
1511 objs = &(lmm->lmm_objects[0]);
1513 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1514 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1515 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1517 fid_to_ostid(fid, oi);
1518 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1520 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1521 LU_XATTR_REPLACE, th);
1522 } else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1523 struct lod_thread_info *info = lod_env_info(env);
1524 struct lu_buf *buf = &info->lti_buf;
1525 struct lov_comp_md_v1 *lcm;
1526 struct lov_comp_md_entry_v1 *lcme;
1528 rc = lod_get_lov_ea(env, lo);
1532 buf->lb_buf = info->lti_ea_store;
1533 buf->lb_len = info->lti_ea_store_size;
1535 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1 &&
1536 le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_SEL)
1539 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1540 lcme = &lcm->lcm_entries[0];
1541 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1542 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1544 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1545 LU_XATTR_REPLACE, th);
1552 * Implementation of dt_object_operations::do_xattr_get.
1554 * If LOV EA is requested from the root object and it's not
1555 * found, then return default striping for the filesystem.
1557 * \see dt_object_operations::do_xattr_get() in the API description for details.
1559 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1560 struct lu_buf *buf, const char *name)
1562 struct lod_thread_info *info = lod_env_info(env);
1563 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1568 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1569 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1570 struct lmv_mds_md_v1 *lmv1;
1571 struct lmv_foreign_md *lfm;
1574 if (rc > (typeof(rc))sizeof(*lmv1))
1577 /* short (<= sizeof(struct lmv_mds_md_v1)) foreign LMV case */
1578 /* XXX empty foreign LMV is not allowed */
1579 if (rc <= offsetof(typeof(*lfm), lfm_value))
1580 RETURN(rc = rc > 0 ? -EINVAL : rc);
1582 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1583 BUILD_BUG_ON(sizeof(*lmv1) > sizeof(info->lti_key));
1585 /* lti_buf is large enough for *lmv1 or a short
1586 * (<= sizeof(struct lmv_mds_md_v1)) foreign LMV
1588 info->lti_buf.lb_buf = info->lti_key;
1589 info->lti_buf.lb_len = sizeof(*lmv1);
1590 rc = dt_xattr_get(env, dt_object_child(dt),
1591 &info->lti_buf, name);
1592 if (unlikely(rc <= offsetof(typeof(*lfm),
1594 RETURN(rc = rc > 0 ? -EINVAL : rc);
1596 lfm = info->lti_buf.lb_buf;
1597 if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN)
1600 if (unlikely(rc != sizeof(*lmv1)))
1601 RETURN(rc = rc > 0 ? -EINVAL : rc);
1603 lmv1 = info->lti_buf.lb_buf;
1604 /* The on-disk LMV EA only contains header, but the
1605 * returned LMV EA size should contain the space for
1606 * the FIDs of all shards of the striped directory. */
1607 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1608 rc = lmv_mds_md_size(
1609 le32_to_cpu(lmv1->lmv_stripe_count),
1610 le32_to_cpu(lmv1->lmv_magic));
1613 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1616 if (rc != sizeof(*lmv1))
1617 RETURN(rc = rc > 0 ? -EINVAL : rc);
1619 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1623 RETURN(rc = rc1 != 0 ? rc1 : rc);
1626 if ((rc > 0) && buf->lb_buf && strcmp(name, XATTR_NAME_LOV) == 0) {
1627 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1629 if (lcm->lcm_magic == cpu_to_le32(LOV_MAGIC_SEL))
1630 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1633 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1637 * XXX: Only used by lfsck
1639 * lod returns default striping on the real root of the device
1640 * this is like the root stores default striping for the whole
1641 * filesystem. historically we've been using a different approach
1642 * and store it in the config.
1644 dt_root_get(env, dev->lod_child, &info->lti_fid);
1645 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1647 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1648 struct lov_user_md *lum = buf->lb_buf;
1649 struct lov_desc *desc = &dev->lod_ost_descs.ltd_lov_desc;
1651 if (buf->lb_buf == NULL) {
1653 } else if (buf->lb_len >= sizeof(*lum)) {
1654 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1655 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1656 lmm_oi_set_id(&lum->lmm_oi, 0);
1657 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1658 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1659 lum->lmm_stripe_size = cpu_to_le32(
1660 desc->ld_default_stripe_size);
1661 lum->lmm_stripe_count = cpu_to_le16(
1662 desc->ld_default_stripe_count);
1663 lum->lmm_stripe_offset = cpu_to_le16(
1664 desc->ld_default_stripe_offset);
1677 * Checks that the magic of the stripe is sane.
1679 * \param[in] lod lod device
1680 * \param[in] lum a buffer storing LMV EA to verify
1682 * \retval 0 if the EA is sane
1683 * \retval negative otherwise
1685 static int lod_verify_md_striping(struct lod_device *lod,
1686 const struct lmv_user_md_v1 *lum)
1688 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1689 CERROR("%s: invalid lmv_user_md: magic = %x, "
1690 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1691 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1692 (int)le32_to_cpu(lum->lum_stripe_offset),
1693 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1701 * Initialize LMV EA for a slave.
1703 * Initialize slave's LMV EA from the master's LMV EA.
1705 * \param[in] master_lmv a buffer containing master's EA
1706 * \param[out] slave_lmv a buffer where slave's EA will be stored
1709 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1710 const struct lmv_mds_md_v1 *master_lmv)
1712 *slave_lmv = *master_lmv;
1713 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1719 * Generate LMV EA from the object passed as \a dt. The object must have
1720 * the stripes created and initialized.
1722 * \param[in] env execution environment
1723 * \param[in] dt object
1724 * \param[out] lmv_buf buffer storing generated LMV EA
1726 * \retval 0 on success
1727 * \retval negative if failed
1729 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1730 struct lu_buf *lmv_buf)
1732 struct lod_thread_info *info = lod_env_info(env);
1733 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1734 struct lod_object *lo = lod_dt_obj(dt);
1735 struct lmv_mds_md_v1 *lmm1;
1737 int type = LU_SEQ_RANGE_ANY;
1742 LASSERT(lo->ldo_dir_striped != 0);
1743 LASSERT(lo->ldo_dir_stripe_count > 0);
1744 stripe_count = lo->ldo_dir_stripe_count;
1745 /* Only store the LMV EA heahder on the disk. */
1746 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1747 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1751 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1754 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1755 memset(lmm1, 0, sizeof(*lmm1));
1756 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1757 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1758 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1759 lmm1->lmv_layout_version = cpu_to_le32(lo->ldo_dir_layout_version);
1760 if (lod_is_layout_changing(lo)) {
1761 lmm1->lmv_migrate_hash = cpu_to_le32(lo->ldo_dir_migrate_hash);
1762 lmm1->lmv_migrate_offset =
1763 cpu_to_le32(lo->ldo_dir_migrate_offset);
1765 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1770 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1771 lmv_buf->lb_buf = info->lti_ea_store;
1772 lmv_buf->lb_len = sizeof(*lmm1);
1778 * Create in-core represenation for a striped directory.
1780 * Parse the buffer containing LMV EA and instantiate LU objects
1781 * representing the stripe objects. The pointers to the objects are
1782 * stored in ldo_stripe field of \a lo. This function is used when
1783 * we need to access an already created object (i.e. load from a disk).
1785 * \param[in] env execution environment
1786 * \param[in] lo lod object
1787 * \param[in] buf buffer containing LMV EA
1789 * \retval 0 on success
1790 * \retval negative if failed
1792 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1793 const struct lu_buf *buf)
1795 struct lod_thread_info *info = lod_env_info(env);
1796 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1797 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1798 struct dt_object **stripe;
1799 union lmv_mds_md *lmm = buf->lb_buf;
1800 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1801 struct lu_fid *fid = &info->lti_fid;
1806 LASSERT(mutex_is_locked(&lo->ldo_layout_mutex));
1808 /* XXX may be useless as not called for foreign LMV ?? */
1809 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_FOREIGN)
1812 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1813 lo->ldo_dir_slave_stripe = 1;
1817 if (!lmv_is_sane(lmv1))
1820 LASSERT(lo->ldo_stripe == NULL);
1821 OBD_ALLOC_PTR_ARRAY(stripe, le32_to_cpu(lmv1->lmv_stripe_count));
1825 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1826 struct dt_device *tgt_dt;
1827 struct dt_object *dto;
1828 int type = LU_SEQ_RANGE_ANY;
1831 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1832 if (!fid_is_sane(fid)) {
1837 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1841 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1842 tgt_dt = lod->lod_child;
1844 struct lod_tgt_desc *tgt;
1846 tgt = LTD_TGT(ltd, idx);
1848 GOTO(out, rc = -ESTALE);
1849 tgt_dt = tgt->ltd_tgt;
1852 dto = dt_locate_at(env, tgt_dt, fid,
1853 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1856 GOTO(out, rc = PTR_ERR(dto));
1861 lo->ldo_stripe = stripe;
1862 lo->ldo_dir_stripe_count = le32_to_cpu(lmv1->lmv_stripe_count);
1863 lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1864 lo->ldo_dir_layout_version = le32_to_cpu(lmv1->lmv_layout_version);
1865 lo->ldo_dir_migrate_offset = le32_to_cpu(lmv1->lmv_migrate_offset);
1866 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv1->lmv_migrate_hash);
1867 lo->ldo_dir_hash_type = le32_to_cpu(lmv1->lmv_hash_type);
1869 lod_striping_free_nolock(env, lo);
1875 * Declare create a striped directory.
1877 * Declare creating a striped directory with a given stripe pattern on the
1878 * specified MDTs. A striped directory is represented as a regular directory
1879 * - an index listing all the stripes. The stripes point back to the master
1880 * object with ".." and LinkEA. The master object gets LMV EA which
1881 * identifies it as a striped directory. The function allocates FIDs
1884 * \param[in] env execution environment
1885 * \param[in] dt object
1886 * \param[in] attr attributes to initialize the objects with
1887 * \param[in] dof type of objects to be created
1888 * \param[in] th transaction handle
1890 * \retval 0 on success
1891 * \retval negative if failed
1893 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1894 struct dt_object *dt,
1895 struct lu_attr *attr,
1896 struct dt_object_format *dof,
1899 struct lod_thread_info *info = lod_env_info(env);
1900 struct lu_buf lmv_buf;
1901 struct lu_buf slave_lmv_buf;
1902 struct lmv_mds_md_v1 *lmm;
1903 struct lmv_mds_md_v1 *slave_lmm = NULL;
1904 struct dt_insert_rec *rec = &info->lti_dt_rec;
1905 struct lod_object *lo = lod_dt_obj(dt);
1910 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1913 lmm = lmv_buf.lb_buf;
1915 OBD_ALLOC_PTR(slave_lmm);
1916 if (slave_lmm == NULL)
1917 GOTO(out, rc = -ENOMEM);
1919 lod_prep_slave_lmv_md(slave_lmm, lmm);
1920 slave_lmv_buf.lb_buf = slave_lmm;
1921 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1923 if (!dt_try_as_dir(env, dt_object_child(dt)))
1924 GOTO(out, rc = -EINVAL);
1926 rec->rec_type = S_IFDIR;
1927 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1928 struct dt_object *dto = lo->ldo_stripe[i];
1929 char *stripe_name = info->lti_key;
1930 struct lu_name *sname;
1931 struct linkea_data ldata = { NULL };
1932 struct lu_buf linkea_buf;
1934 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
1938 /* directory split skip create for existing stripes */
1939 if (!(lod_is_splitting(lo) && i < lo->ldo_dir_split_offset)) {
1940 rc = lod_sub_declare_create(env, dto, attr, NULL, dof,
1945 if (!dt_try_as_dir(env, dto))
1946 GOTO(out, rc = -EINVAL);
1948 rc = lod_sub_declare_ref_add(env, dto, th);
1952 rec->rec_fid = lu_object_fid(&dto->do_lu);
1953 rc = lod_sub_declare_insert(env, dto,
1954 (const struct dt_rec *)rec,
1955 (const struct dt_key *)dot,
1960 /* master stripe FID will be put to .. */
1961 rec->rec_fid = lu_object_fid(&dt->do_lu);
1962 rc = lod_sub_declare_insert(env, dto,
1963 (const struct dt_rec *)rec,
1964 (const struct dt_key *)dotdot,
1969 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1971 snprintf(stripe_name, sizeof(info->lti_key),
1973 PFID(lu_object_fid(&dto->do_lu)),
1976 snprintf(stripe_name, sizeof(info->lti_key),
1978 PFID(lu_object_fid(&dto->do_lu)), i);
1980 sname = lod_name_get(env, stripe_name,
1981 strlen(stripe_name));
1982 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1983 sname, lu_object_fid(&dt->do_lu));
1987 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1988 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1989 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
1990 XATTR_NAME_LINK, 0, th);
1994 rec->rec_fid = lu_object_fid(&dto->do_lu);
1995 rc = lod_sub_declare_insert(env, dt_object_child(dt),
1996 (const struct dt_rec *)rec,
1997 (const struct dt_key *)stripe_name, th);
2001 rc = lod_sub_declare_ref_add(env, dt_object_child(dt),
2007 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
2008 cfs_fail_val != i) {
2009 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
2011 slave_lmm->lmv_master_mdt_index =
2014 slave_lmm->lmv_master_mdt_index =
2016 rc = lod_sub_declare_xattr_set(env, dto, &slave_lmv_buf,
2017 XATTR_NAME_LMV, 0, th);
2023 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt),
2024 &lmv_buf, XATTR_NAME_LMV, 0, th);
2028 if (slave_lmm != NULL)
2029 OBD_FREE_PTR(slave_lmm);
2035 * Allocate a striping on a predefined set of MDTs.
2037 * Allocates new striping using the MDT index range provided by the data from
2038 * the lum_obejcts contained in the lmv_user_md passed to this method if
2039 * \a is_specific is true; or allocates new layout starting from MDT index in
2040 * lo->ldo_dir_stripe_offset. The exact order of MDTs is not important and
2041 * varies depending on MDT status. The number of stripes needed and stripe
2042 * offset are taken from the object. If that number cannot be met, then the
2043 * function returns an error and then it's the caller's responsibility to
2044 * release the stripes allocated. All the internal structures are protected,
2045 * but no concurrent allocation is allowed on the same objects.
2047 * \param[in] env execution environment for this thread
2048 * \param[in] lo LOD object
2049 * \param[out] stripes striping created
2050 * \param[out] mdt_indices MDT indices of striping created
2051 * \param[in] is_specific true if the MDTs are provided by lum; false if
2052 * only the starting MDT index is provided
2054 * \retval positive stripes allocated, including the first stripe allocated
2056 * \retval negative errno on failure
2058 static int lod_mdt_alloc_specific(const struct lu_env *env,
2059 struct lod_object *lo,
2060 struct dt_object **stripes,
2061 __u32 *mdt_indices, bool is_specific)
2063 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
2064 struct lu_tgt_descs *ltd = &lod->lod_mdt_descs;
2065 struct lu_tgt_desc *tgt = NULL;
2066 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2067 struct dt_device *tgt_dt = NULL;
2068 struct lu_fid fid = { 0 };
2069 struct dt_object *dto;
2071 u32 stripe_count = lo->ldo_dir_stripe_count;
2077 master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
2078 if (!is_specific && stripe_count > 1)
2079 /* Set the start index for the 2nd stripe allocation */
2080 mdt_indices[1] = (mdt_indices[0] + 1) %
2081 (lod->lod_remote_mdt_count + 1);
2083 for (; stripe_idx < stripe_count; stripe_idx++) {
2084 /* Try to find next avaible target */
2085 idx = mdt_indices[stripe_idx];
2086 for (j = 0; j < lod->lod_remote_mdt_count;
2087 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
2088 bool already_allocated = false;
2091 CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
2092 idx, lod->lod_remote_mdt_count + 1, stripe_idx);
2094 if (likely(!is_specific &&
2095 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
2096 /* check whether the idx already exists
2097 * in current allocated array */
2098 for (k = 0; k < stripe_idx; k++) {
2099 if (mdt_indices[k] == idx) {
2100 already_allocated = true;
2105 if (already_allocated)
2109 /* Sigh, this index is not in the bitmap, let's check
2110 * next available target */
2111 if (!test_bit(idx, ltd->ltd_tgt_bitmap) &&
2112 idx != master_index)
2115 if (idx == master_index) {
2116 /* Allocate the FID locally */
2117 tgt_dt = lod->lod_child;
2118 rc = dt_fid_alloc(env, tgt_dt, &fid, NULL,
2125 /* check the status of the OSP */
2126 tgt = LTD_TGT(ltd, idx);
2130 tgt_dt = tgt->ltd_tgt;
2131 if (!tgt->ltd_active)
2132 /* this OSP doesn't feel well */
2135 rc = dt_fid_alloc(env, tgt_dt, &fid, NULL, NULL);
2142 /* Can not allocate more stripes */
2143 if (j == lod->lod_remote_mdt_count) {
2144 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
2145 lod2obd(lod)->obd_name, stripe_count,
2150 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
2151 idx, stripe_idx, PFID(&fid));
2152 mdt_indices[stripe_idx] = idx;
2153 /* Set the start index for next stripe allocation */
2154 if (!is_specific && stripe_idx < stripe_count - 1) {
2156 * for large dir test, put all other slaves on one
2157 * remote MDT, otherwise we may save too many local
2158 * slave locks which will exceed RS_MAX_LOCKS.
2160 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)))
2162 mdt_indices[stripe_idx + 1] = (idx + 1) %
2163 (lod->lod_remote_mdt_count + 1);
2165 /* tgt_dt and fid must be ready after search avaible OSP
2166 * in the above loop */
2167 LASSERT(tgt_dt != NULL);
2168 LASSERT(fid_is_sane(&fid));
2170 /* fail a remote stripe FID allocation */
2171 if (stripe_idx && OBD_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_FID))
2174 dto = dt_locate_at(env, tgt_dt, &fid,
2175 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
2182 stripes[stripe_idx] = dto;
2188 for (j = 1; j < stripe_idx; j++) {
2189 LASSERT(stripes[j] != NULL);
2190 dt_object_put(env, stripes[j]);
2196 static int lod_prep_md_striped_create(const struct lu_env *env,
2197 struct dt_object *dt,
2198 struct lu_attr *attr,
2199 const struct lmv_user_md_v1 *lum,
2200 struct dt_object_format *dof,
2203 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
2204 struct lod_object *lo = lod_dt_obj(dt);
2205 struct dt_object **stripes;
2206 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2207 struct lu_fid fid = { 0 };
2214 /* The lum has been verifed in lod_verify_md_striping */
2215 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC ||
2216 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC);
2218 stripe_count = lo->ldo_dir_stripe_count;
2220 OBD_ALLOC_PTR_ARRAY(stripes, stripe_count);
2224 /* Allocate the first stripe locally */
2225 rc = dt_fid_alloc(env, lod->lod_child, &fid, NULL, NULL);
2229 stripes[0] = dt_locate_at(env, lod->lod_child, &fid,
2230 dt->do_lu.lo_dev->ld_site->ls_top_dev, &conf);
2231 if (IS_ERR(stripes[0]))
2232 GOTO(out, rc = PTR_ERR(stripes[0]));
2234 if (lo->ldo_dir_stripe_offset == LMV_OFFSET_DEFAULT) {
2235 lod_qos_statfs_update(env, lod, &lod->lod_mdt_descs);
2236 rc = lod_mdt_alloc_qos(env, lo, stripes, 1, stripe_count);
2238 rc = lod_mdt_alloc_rr(env, lo, stripes, 1,
2242 bool is_specific = false;
2244 OBD_ALLOC_PTR_ARRAY(idx_array, stripe_count);
2246 GOTO(out, rc = -ENOMEM);
2248 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) {
2250 for (i = 0; i < stripe_count; i++)
2252 le32_to_cpu(lum->lum_objects[i].lum_mds);
2255 /* stripe 0 is local */
2257 lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
2258 rc = lod_mdt_alloc_specific(env, lo, stripes, idx_array,
2260 OBD_FREE_PTR_ARRAY(idx_array, stripe_count);
2268 lo->ldo_dir_striped = 1;
2269 lo->ldo_stripe = stripes;
2270 lo->ldo_dir_stripe_count = rc;
2271 lo->ldo_dir_stripes_allocated = stripe_count;
2273 lo->ldo_dir_stripe_loaded = 1;
2275 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
2277 lod_striping_free(env, lo);
2283 if (!IS_ERR_OR_NULL(stripes[0]))
2284 dt_object_put(env, stripes[0]);
2285 for (i = 1; i < stripe_count; i++)
2286 LASSERT(!stripes[i]);
2287 OBD_FREE_PTR_ARRAY(stripes, stripe_count);
2294 * Alloc cached foreign LMV
2296 * \param[in] lo object
2297 * \param[in] size size of foreign LMV
2299 * \retval 0 on success
2300 * \retval negative if failed
2302 int lod_alloc_foreign_lmv(struct lod_object *lo, size_t size)
2304 OBD_ALLOC_LARGE(lo->ldo_foreign_lmv, size);
2305 if (lo->ldo_foreign_lmv == NULL)
2307 lo->ldo_foreign_lmv_size = size;
2308 lo->ldo_dir_is_foreign = 1;
2314 * Declare create striped md object.
2316 * The function declares intention to create a striped directory. This is a
2317 * wrapper for lod_prep_md_striped_create(). The only additional functionality
2318 * is to verify pattern \a lum_buf is good. Check that function for the details.
2320 * \param[in] env execution environment
2321 * \param[in] dt object
2322 * \param[in] attr attributes to initialize the objects with
2323 * \param[in] lum_buf a pattern specifying the number of stripes and
2325 * \param[in] dof type of objects to be created
2326 * \param[in] th transaction handle
2328 * \retval 0 on success
2329 * \retval negative if failed
2332 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
2333 struct dt_object *dt,
2334 struct lu_attr *attr,
2335 const struct lu_buf *lum_buf,
2336 struct dt_object_format *dof,
2339 struct lod_object *lo = lod_dt_obj(dt);
2340 struct lmv_user_md_v1 *lum = lum_buf->lb_buf;
2344 LASSERT(lum != NULL);
2346 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
2347 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
2348 (int)le32_to_cpu(lum->lum_stripe_offset));
2350 if (lo->ldo_dir_stripe_count == 0) {
2351 if (lo->ldo_dir_is_foreign) {
2352 rc = lod_alloc_foreign_lmv(lo, lum_buf->lb_len);
2355 memcpy(lo->ldo_foreign_lmv, lum, lum_buf->lb_len);
2356 lo->ldo_dir_stripe_loaded = 1;
2361 /* prepare dir striped objects */
2362 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2364 /* failed to create striping, let's reset
2365 * config so that others don't get confused */
2366 lod_striping_free(env, lo);
2374 * Set or replace striped directory layout, and LFSCK may set layout on a plain
2375 * directory, so don't check stripe count.
2377 * \param[in] env execution environment
2378 * \param[in] dt target object
2379 * \param[in] buf LMV buf which contains source stripe fids
2380 * \param[in] fl set or replace
2381 * \param[in] th transaction handle
2383 * \retval 0 on success
2384 * \retval negative if failed
2386 static int lod_dir_layout_set(const struct lu_env *env,
2387 struct dt_object *dt,
2388 const struct lu_buf *buf,
2392 struct dt_object *next = dt_object_child(dt);
2393 struct lod_object *lo = lod_dt_obj(dt);
2394 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2395 struct lmv_mds_md_v1 *lmv = buf->lb_buf;
2396 struct lmv_mds_md_v1 *slave_lmv;
2397 struct lu_buf slave_buf;
2403 if (!lmv_is_sane2(lmv))
2406 /* adjust hash for dir merge, which may not be set in user command */
2407 if (lmv_is_merging(lmv) && !lmv->lmv_migrate_hash)
2408 lmv->lmv_merge_hash =
2409 lod->lod_mdt_descs.ltd_lmv_desc.ld_pattern;
2411 LMV_DEBUG(D_INFO, lmv, "set");
2413 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LMV, fl, th);
2417 /* directory restripe may update stripe LMV directly */
2418 if (!lo->ldo_dir_stripe_count)
2421 lo->ldo_dir_hash_type = le32_to_cpu(lmv->lmv_hash_type);
2422 lo->ldo_dir_migrate_offset = le32_to_cpu(lmv->lmv_migrate_offset);
2423 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_migrate_hash);
2424 lo->ldo_dir_layout_version = le32_to_cpu(lmv->lmv_layout_version);
2426 OBD_ALLOC_PTR(slave_lmv);
2430 lod_prep_slave_lmv_md(slave_lmv, lmv);
2431 slave_buf.lb_buf = slave_lmv;
2432 slave_buf.lb_len = sizeof(*slave_lmv);
2434 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2435 if (!lo->ldo_stripe[i])
2438 if (!dt_object_exists(lo->ldo_stripe[i]))
2441 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], &slave_buf,
2442 XATTR_NAME_LMV, fl, th);
2447 OBD_FREE_PTR(slave_lmv);
2453 * Implementation of dt_object_operations::do_declare_xattr_set.
2455 * Used with regular (non-striped) objects. Basically it
2456 * initializes the striping information and applies the
2457 * change to all the stripes.
2459 * \see dt_object_operations::do_declare_xattr_set() in the API description
2462 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2463 struct dt_object *dt,
2464 const struct lu_buf *buf,
2465 const char *name, int fl,
2468 struct dt_object *next = dt_object_child(dt);
2469 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2470 struct lod_object *lo = lod_dt_obj(dt);
2475 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2476 struct lmv_user_md_v1 *lum;
2478 LASSERT(buf != NULL && buf->lb_buf != NULL);
2480 rc = lod_verify_md_striping(d, lum);
2483 } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2484 rc = lod_verify_striping(env, d, lo, buf, false);
2489 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2493 /* Note: Do not set LinkEA on sub-stripes, otherwise
2494 * it will confuse the fid2path process(see mdt_path_current()).
2495 * The linkEA between master and sub-stripes is set in
2496 * lod_xattr_set_lmv(). */
2497 if (strcmp(name, XATTR_NAME_LINK) == 0)
2500 /* set xattr to each stripes, if needed */
2501 rc = lod_striping_load(env, lo);
2505 if (lo->ldo_dir_stripe_count == 0)
2508 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2509 if (!lo->ldo_stripe[i])
2512 if (!dt_object_exists(lo->ldo_stripe[i]))
2515 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2525 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2526 struct lod_object *lo,
2527 struct dt_object *dt, struct thandle *th,
2528 int comp_idx, int stripe_idx,
2529 struct lod_obj_stripe_cb_data *data)
2531 struct lod_thread_info *info = lod_env_info(env);
2532 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
2533 struct filter_fid *ff = &info->lti_ff;
2534 struct lu_buf *buf = &info->lti_buf;
2538 buf->lb_len = sizeof(*ff);
2539 rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2547 * locd_buf is set if it's called by dir migration, which doesn't check
2550 if (data->locd_buf) {
2551 memset(ff, 0, sizeof(*ff));
2552 ff->ff_parent = *(struct lu_fid *)data->locd_buf->lb_buf;
2554 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
2556 if (lu_fid_eq(lod_object_fid(lo), &ff->ff_parent) &&
2557 ff->ff_layout.ol_comp_id == comp->llc_id)
2560 memset(ff, 0, sizeof(*ff));
2561 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2564 /* rewrite filter_fid */
2565 ff->ff_parent.f_ver = stripe_idx;
2566 ff->ff_layout.ol_stripe_size = comp->llc_stripe_size;
2567 ff->ff_layout.ol_stripe_count = comp->llc_stripe_count;
2568 ff->ff_layout.ol_comp_id = comp->llc_id;
2569 ff->ff_layout.ol_comp_start = comp->llc_extent.e_start;
2570 ff->ff_layout.ol_comp_end = comp->llc_extent.e_end;
2571 filter_fid_cpu_to_le(ff, ff, sizeof(*ff));
2573 if (data->locd_declare)
2574 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2575 LU_XATTR_REPLACE, th);
2577 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2578 LU_XATTR_REPLACE, th);
2584 * Reset parent FID on OST object
2586 * Replace parent FID with @dt object FID, which is only called during migration
2587 * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2588 * the FID is changed.
2590 * \param[in] env execution environment
2591 * \param[in] dt dt_object whose stripes's parent FID will be reset
2592 * \parem[in] th thandle
2593 * \param[in] declare if it is declare
2595 * \retval 0 if reset succeeds
2596 * \retval negative errno if reset fails
2598 static int lod_replace_parent_fid(const struct lu_env *env,
2599 struct dt_object *dt,
2600 const struct lu_buf *buf,
2601 struct thandle *th, bool declare)
2603 struct lod_object *lo = lod_dt_obj(dt);
2604 struct lod_thread_info *info = lod_env_info(env);
2605 struct filter_fid *ff;
2606 struct lod_obj_stripe_cb_data data = { { 0 } };
2610 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2612 /* set xattr to each stripes, if needed */
2613 rc = lod_striping_load(env, lo);
2617 if (!lod_obj_is_striped(dt))
2620 if (info->lti_ea_store_size < sizeof(*ff)) {
2621 rc = lod_ea_store_resize(info, sizeof(*ff));
2626 data.locd_declare = declare;
2627 data.locd_stripe_cb = lod_obj_stripe_replace_parent_fid_cb;
2628 data.locd_buf = buf;
2629 rc = lod_obj_for_each_stripe(env, lo, th, &data);
2634 inline __u16 lod_comp_entry_stripe_count(struct lod_object *lo,
2635 int comp_idx, bool is_dir)
2637 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2638 struct lod_layout_component *entry;
2643 entry = &lo->ldo_comp_entries[comp_idx];
2644 if (lod_comp_inited(entry))
2645 return entry->llc_stripe_count;
2646 else if ((__u16)-1 == entry->llc_stripe_count)
2647 return lod->lod_ost_count;
2649 return lod_get_stripe_count(lod, lo, comp_idx,
2650 entry->llc_stripe_count, false);
2653 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2655 int magic, size = 0, i;
2656 struct lod_layout_component *comp_entries;
2658 bool is_composite, is_foreign = false;
2661 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2662 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2664 lo->ldo_def_striping->lds_def_striping_is_composite;
2666 comp_cnt = lo->ldo_comp_cnt;
2667 comp_entries = lo->ldo_comp_entries;
2668 is_composite = lo->ldo_is_composite;
2669 is_foreign = lo->ldo_is_foreign;
2673 return lo->ldo_foreign_lov_size;
2675 LASSERT(comp_cnt != 0 && comp_entries != NULL);
2677 size = sizeof(struct lov_comp_md_v1) +
2678 sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2679 LASSERT(size % sizeof(__u64) == 0);
2682 for (i = 0; i < comp_cnt; i++) {
2685 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2686 stripe_count = lod_comp_entry_stripe_count(lo, i, is_dir);
2687 if (!is_dir && is_composite)
2688 lod_comp_shrink_stripe_count(&comp_entries[i],
2691 size += lov_user_md_size(stripe_count, magic);
2692 LASSERT(size % sizeof(__u64) == 0);
2698 * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2699 * the xattr value is binary lov_comp_md_v1 which contains component(s)
2702 * \param[in] env execution environment
2703 * \param[in] dt dt_object to add components on
2704 * \param[in] buf buffer contains components to be added
2705 * \parem[in] th thandle
2707 * \retval 0 on success
2708 * \retval negative errno on failure
2710 static int lod_declare_layout_add(const struct lu_env *env,
2711 struct dt_object *dt,
2712 const struct lu_buf *buf,
2715 struct lod_thread_info *info = lod_env_info(env);
2716 struct lod_layout_component *comp_array, *lod_comp, *old_array;
2717 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2718 struct dt_object *next = dt_object_child(dt);
2719 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
2720 struct lod_object *lo = lod_dt_obj(dt);
2721 struct lov_user_md_v3 *v3;
2722 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2724 int i, rc, array_cnt, old_array_cnt;
2727 LASSERT(lo->ldo_is_composite);
2729 if (lo->ldo_flr_state != LCM_FL_NONE)
2732 rc = lod_verify_striping(env, d, lo, buf, false);
2736 magic = comp_v1->lcm_magic;
2737 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2738 lustre_swab_lov_comp_md_v1(comp_v1);
2739 magic = comp_v1->lcm_magic;
2742 if (magic != LOV_USER_MAGIC_COMP_V1)
2745 array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2746 OBD_ALLOC_PTR_ARRAY(comp_array, array_cnt);
2747 if (comp_array == NULL)
2750 memcpy(comp_array, lo->ldo_comp_entries,
2751 sizeof(*comp_array) * lo->ldo_comp_cnt);
2753 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2754 struct lov_user_md_v1 *v1;
2755 struct lu_extent *ext;
2757 v1 = (struct lov_user_md *)((char *)comp_v1 +
2758 comp_v1->lcm_entries[i].lcme_offset);
2759 ext = &comp_v1->lcm_entries[i].lcme_extent;
2761 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2762 lod_comp->llc_extent.e_start = ext->e_start;
2763 lod_comp->llc_extent.e_end = ext->e_end;
2764 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2765 lod_comp->llc_flags = comp_v1->lcm_entries[i].lcme_flags;
2767 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2768 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2769 lod_adjust_stripe_info(lod_comp, desc, 0);
2771 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2772 v3 = (struct lov_user_md_v3 *) v1;
2773 if (v3->lmm_pool_name[0] != '\0') {
2774 rc = lod_set_pool(&lod_comp->llc_pool,
2782 old_array = lo->ldo_comp_entries;
2783 old_array_cnt = lo->ldo_comp_cnt;
2785 lo->ldo_comp_entries = comp_array;
2786 lo->ldo_comp_cnt = array_cnt;
2788 /* No need to increase layout generation here, it will be increased
2789 * later when generating component ID for the new components */
2791 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2792 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2793 XATTR_NAME_LOV, 0, th);
2795 lo->ldo_comp_entries = old_array;
2796 lo->ldo_comp_cnt = old_array_cnt;
2800 OBD_FREE_PTR_ARRAY(old_array, old_array_cnt);
2802 LASSERT(lo->ldo_mirror_count == 1);
2803 lo->ldo_mirrors[0].lme_end = array_cnt - 1;
2808 for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2809 lod_comp = &comp_array[i];
2810 if (lod_comp->llc_pool != NULL) {
2811 OBD_FREE(lod_comp->llc_pool,
2812 strlen(lod_comp->llc_pool) + 1);
2813 lod_comp->llc_pool = NULL;
2816 OBD_FREE_PTR_ARRAY(comp_array, array_cnt);
2821 * lod_last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2822 * @mirror_id: Mirror id to be checked.
2825 * This function checks if a mirror with specified @mirror_id is the last
2826 * non-stale mirror of a LOD object @lo.
2828 * Return: true or false.
2831 bool lod_last_non_stale_mirror(__u16 mirror_id, struct lod_object *lo)
2833 struct lod_layout_component *lod_comp;
2834 bool has_stale_flag;
2837 for (i = 0; i < lo->ldo_mirror_count; i++) {
2838 if (lo->ldo_mirrors[i].lme_id == mirror_id ||
2839 lo->ldo_mirrors[i].lme_stale)
2842 has_stale_flag = false;
2843 lod_foreach_mirror_comp(lod_comp, lo, i) {
2844 if (lod_comp->llc_flags & LCME_FL_STALE) {
2845 has_stale_flag = true;
2849 if (!has_stale_flag)
2857 * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2858 * the '$field' can only be 'flags' now. The xattr value is binary
2859 * lov_comp_md_v1 which contains the component ID(s) and the value of
2860 * the field to be modified.
2861 * Please update allowed_lustre_lov macro if $field groks more values
2864 * \param[in] env execution environment
2865 * \param[in] dt dt_object to be modified
2866 * \param[in] op operation string, like "set.flags"
2867 * \param[in] buf buffer contains components to be set
2868 * \parem[in] th thandle
2870 * \retval 0 on success
2871 * \retval negative errno on failure
2873 static int lod_declare_layout_set(const struct lu_env *env,
2874 struct dt_object *dt,
2875 char *op, const struct lu_buf *buf,
2878 struct lod_layout_component *lod_comp;
2879 struct lod_thread_info *info = lod_env_info(env);
2880 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2881 struct lod_object *lo = lod_dt_obj(dt);
2882 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2885 bool changed = false;
2888 /* Please update allowed_lustre_lov macro if op
2889 * groks more values in the future
2891 if (strcmp(op, "set.flags") != 0) {
2892 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2893 lod2obd(d)->obd_name, op);
2897 magic = comp_v1->lcm_magic;
2898 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2899 lustre_swab_lov_comp_md_v1(comp_v1);
2900 magic = comp_v1->lcm_magic;
2903 if (magic != LOV_USER_MAGIC_COMP_V1)
2906 if (comp_v1->lcm_entry_count == 0) {
2907 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2908 lod2obd(d)->obd_name);
2912 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2913 __u32 id = comp_v1->lcm_entries[i].lcme_id;
2914 __u32 flags = comp_v1->lcm_entries[i].lcme_flags;
2915 __u32 mirror_flag = flags & LCME_MIRROR_FLAGS;
2916 __u16 mirror_id = mirror_id_of(id);
2917 bool neg = flags & LCME_FL_NEG;
2919 if (flags & LCME_FL_INIT) {
2921 lod_striping_free(env, lo);
2925 flags &= ~(LCME_MIRROR_FLAGS | LCME_FL_NEG);
2926 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2927 lod_comp = &lo->ldo_comp_entries[j];
2929 /* lfs only put one flag in each entry */
2930 if ((flags && id != lod_comp->llc_id) ||
2931 (mirror_flag && mirror_id !=
2932 mirror_id_of(lod_comp->llc_id)))
2937 lod_comp->llc_flags &= ~flags;
2939 lod_comp->llc_flags &= ~mirror_flag;
2942 if ((flags & LCME_FL_STALE) &&
2943 lod_last_non_stale_mirror(mirror_id,
2946 lod_comp->llc_flags |= flags;
2949 lod_comp->llc_flags |= mirror_flag;
2950 if (mirror_flag & LCME_FL_NOSYNC)
2951 lod_comp->llc_timestamp =
2952 ktime_get_real_seconds();
2960 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2961 lod2obd(d)->obd_name);
2965 lod_obj_inc_layout_gen(lo);
2967 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2968 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), &info->lti_buf,
2969 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
2974 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2975 * and the xattr value is a unique component ID or a special lcme_id.
2977 * \param[in] env execution environment
2978 * \param[in] dt dt_object to be operated on
2979 * \param[in] buf buffer contains component ID or lcme_id
2980 * \parem[in] th thandle
2982 * \retval 0 on success
2983 * \retval negative errno on failure
2985 static int lod_declare_layout_del(const struct lu_env *env,
2986 struct dt_object *dt,
2987 const struct lu_buf *buf,
2990 struct lod_thread_info *info = lod_env_info(env);
2991 struct dt_object *next = dt_object_child(dt);
2992 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2993 struct lod_object *lo = lod_dt_obj(dt);
2994 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2995 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2996 __u32 magic, id, flags, neg_flags = 0;
3000 LASSERT(lo->ldo_is_composite);
3002 if (lo->ldo_flr_state != LCM_FL_NONE)
3005 magic = comp_v1->lcm_magic;
3006 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3007 lustre_swab_lov_comp_md_v1(comp_v1);
3008 magic = comp_v1->lcm_magic;
3011 if (magic != LOV_USER_MAGIC_COMP_V1)
3014 id = comp_v1->lcm_entries[0].lcme_id;
3015 flags = comp_v1->lcm_entries[0].lcme_flags;
3017 if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
3018 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
3019 lod2obd(d)->obd_name, id, flags);
3023 if (id != LCME_ID_INVAL && flags != 0) {
3024 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
3025 lod2obd(d)->obd_name);
3029 if (id == LCME_ID_INVAL && !flags) {
3030 CDEBUG(D_LAYOUT, "%s: no id or flags specified.\n",
3031 lod2obd(d)->obd_name);
3035 if (flags & LCME_FL_NEG) {
3036 neg_flags = flags & ~LCME_FL_NEG;
3040 left = lo->ldo_comp_cnt;
3044 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3045 struct lod_layout_component *lod_comp;
3047 lod_comp = &lo->ldo_comp_entries[i];
3049 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
3051 else if (flags && !(flags & lod_comp->llc_flags))
3053 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
3056 if (left != (i + 1)) {
3057 CDEBUG(D_LAYOUT, "%s: this deletion will create "
3058 "a hole.\n", lod2obd(d)->obd_name);
3063 /* Mark the component as deleted */
3064 lod_comp->llc_id = LCME_ID_INVAL;
3066 /* Not instantiated component */
3067 if (lod_comp->llc_stripe == NULL)
3070 LASSERT(lod_comp->llc_stripe_count > 0);
3071 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
3072 struct dt_object *obj = lod_comp->llc_stripe[j];
3076 rc = lod_sub_declare_destroy(env, obj, th);
3082 LASSERTF(left >= 0, "left = %d\n", left);
3083 if (left == lo->ldo_comp_cnt) {
3084 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
3085 lod2obd(d)->obd_name, id);
3089 memset(attr, 0, sizeof(*attr));
3090 attr->la_valid = LA_SIZE;
3091 rc = lod_sub_declare_attr_set(env, next, attr, th);
3096 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
3097 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
3098 XATTR_NAME_LOV, 0, th);
3100 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
3107 * Declare layout add/set/del operations issued by special xattr names:
3109 * XATTR_LUSTRE_LOV.add add component(s) to existing file
3110 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
3111 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
3113 * \param[in] env execution environment
3114 * \param[in] dt object
3115 * \param[in] name name of xattr
3116 * \param[in] buf lu_buf contains xattr value
3117 * \param[in] th transaction handle
3119 * \retval 0 on success
3120 * \retval negative if failed
3122 static int lod_declare_modify_layout(const struct lu_env *env,
3123 struct dt_object *dt,
3125 const struct lu_buf *buf,
3128 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3129 struct lod_object *lo = lod_dt_obj(dt);
3131 int rc, len = strlen(XATTR_LUSTRE_LOV);
3134 LASSERT(dt_object_exists(dt));
3136 if (strlen(name) <= len || name[len] != '.') {
3137 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
3138 lod2obd(d)->obd_name, name);
3143 rc = lod_striping_load(env, lo);
3147 /* the layout to be modified must be a composite layout */
3148 if (!lo->ldo_is_composite) {
3149 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
3150 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3151 GOTO(unlock, rc = -EINVAL);
3154 op = (char *)name + len;
3155 if (strcmp(op, "add") == 0) {
3156 rc = lod_declare_layout_add(env, dt, buf, th);
3157 } else if (strcmp(op, "del") == 0) {
3158 rc = lod_declare_layout_del(env, dt, buf, th);
3159 } else if (strncmp(op, "set", strlen("set")) == 0) {
3160 rc = lod_declare_layout_set(env, dt, op, buf, th);
3162 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
3163 lod2obd(d)->obd_name, name);
3164 GOTO(unlock, rc = -ENOTSUPP);
3168 lod_striping_free(env, lo);
3174 * Convert a plain file lov_mds_md to a composite layout.
3176 * \param[in,out] info the thread info::lti_ea_store buffer contains little
3177 * endian plain file layout
3179 * \retval 0 on success, <0 on failure
3181 static int lod_layout_convert(struct lod_thread_info *info)
3183 struct lov_mds_md *lmm = info->lti_ea_store;
3184 struct lov_mds_md *lmm_save;
3185 struct lov_comp_md_v1 *lcm;
3186 struct lov_comp_md_entry_v1 *lcme;
3192 /* realloc buffer to a composite layout which contains one component */
3193 blob_size = lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
3194 le32_to_cpu(lmm->lmm_magic));
3195 size = sizeof(*lcm) + sizeof(*lcme) + blob_size;
3197 OBD_ALLOC_LARGE(lmm_save, blob_size);
3199 GOTO(out, rc = -ENOMEM);
3201 memcpy(lmm_save, lmm, blob_size);
3203 if (info->lti_ea_store_size < size) {
3204 rc = lod_ea_store_resize(info, size);
3209 lcm = info->lti_ea_store;
3210 memset(lcm, 0, sizeof(*lcm) + sizeof(*lcme));
3211 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
3212 lcm->lcm_size = cpu_to_le32(size);
3213 lcm->lcm_layout_gen = cpu_to_le32(le16_to_cpu(
3214 lmm_save->lmm_layout_gen));
3215 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
3216 lcm->lcm_entry_count = cpu_to_le16(1);
3218 lcme = &lcm->lcm_entries[0];
3219 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
3220 lcme->lcme_extent.e_start = 0;
3221 lcme->lcme_extent.e_end = cpu_to_le64(OBD_OBJECT_EOF);
3222 lcme->lcme_offset = cpu_to_le32(sizeof(*lcm) + sizeof(*lcme));
3223 lcme->lcme_size = cpu_to_le32(blob_size);
3225 memcpy((char *)lcm + lcme->lcme_offset, (char *)lmm_save, blob_size);
3230 OBD_FREE_LARGE(lmm_save, blob_size);
3235 * Merge layouts to form a mirrored file.
3237 static int lod_declare_layout_merge(const struct lu_env *env,
3238 struct dt_object *dt, const struct lu_buf *mbuf,
3241 struct lod_thread_info *info = lod_env_info(env);
3242 struct lu_buf *buf = &info->lti_buf;
3243 struct lod_object *lo = lod_dt_obj(dt);
3244 struct lov_comp_md_v1 *lcm;
3245 struct lov_comp_md_v1 *cur_lcm;
3246 struct lov_comp_md_v1 *merge_lcm;
3247 struct lov_comp_md_entry_v1 *lcme;
3248 struct lov_mds_md_v1 *lmm;
3251 __u16 cur_entry_count;
3252 __u16 merge_entry_count;
3254 __u16 mirror_id = 0;
3261 merge_lcm = mbuf->lb_buf;
3262 if (mbuf->lb_len < sizeof(*merge_lcm))
3265 /* must be an existing layout from disk */
3266 if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
3269 merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
3271 /* do not allow to merge two mirrored files */
3272 if (le16_to_cpu(merge_lcm->lcm_mirror_count))
3275 /* verify the target buffer */
3276 rc = lod_get_lov_ea(env, lo);
3278 RETURN(rc ? : -ENODATA);
3280 cur_lcm = info->lti_ea_store;
3281 switch (le32_to_cpu(cur_lcm->lcm_magic)) {
3284 rc = lod_layout_convert(info);
3286 case LOV_MAGIC_COMP_V1:
3296 /* info->lti_ea_store could be reallocated in lod_layout_convert() */
3297 cur_lcm = info->lti_ea_store;
3298 cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
3300 /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
3301 mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
3302 if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
3305 /* size of new layout */
3306 size = le32_to_cpu(cur_lcm->lcm_size) +
3307 le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
3309 memset(buf, 0, sizeof(*buf));
3310 lu_buf_alloc(buf, size);
3311 if (buf->lb_buf == NULL)
3315 memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
3317 offset = sizeof(*lcm) +
3318 sizeof(*lcme) * (cur_entry_count + merge_entry_count);
3319 for (i = 0; i < cur_entry_count; i++) {
3320 struct lov_comp_md_entry_v1 *cur_lcme;
3322 lcme = &lcm->lcm_entries[i];
3323 cur_lcme = &cur_lcm->lcm_entries[i];
3325 lcme->lcme_offset = cpu_to_le32(offset);
3326 memcpy((char *)lcm + offset,
3327 (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
3328 le32_to_cpu(lcme->lcme_size));
3330 offset += le32_to_cpu(lcme->lcme_size);
3332 if (mirror_count == 1 &&
3333 mirror_id_of(le32_to_cpu(lcme->lcme_id)) == 0) {
3334 /* Add mirror from a non-flr file, create new mirror ID.
3335 * Otherwise, keep existing mirror's component ID, used
3336 * for mirror extension.
3338 id = pflr_id(1, i + 1);
3339 lcme->lcme_id = cpu_to_le32(id);
3342 id = max(le32_to_cpu(lcme->lcme_id), id);
3345 mirror_id = mirror_id_of(id) + 1;
3347 /* check if first entry in new layout is DOM */
3348 lmm = (struct lov_mds_md_v1 *)((char *)merge_lcm +
3349 merge_lcm->lcm_entries[0].lcme_offset);
3350 merge_has_dom = lov_pattern(le32_to_cpu(lmm->lmm_pattern)) ==
3353 for (i = 0; i < merge_entry_count; i++) {
3354 struct lov_comp_md_entry_v1 *merge_lcme;
3356 merge_lcme = &merge_lcm->lcm_entries[i];
3357 lcme = &lcm->lcm_entries[cur_entry_count + i];
3359 *lcme = *merge_lcme;
3360 lcme->lcme_offset = cpu_to_le32(offset);
3361 if (merge_has_dom && i == 0)
3362 lcme->lcme_flags |= cpu_to_le32(LCME_FL_STALE);
3364 id = pflr_id(mirror_id, i + 1);
3365 lcme->lcme_id = cpu_to_le32(id);
3367 memcpy((char *)lcm + offset,
3368 (char *)merge_lcm + le32_to_cpu(merge_lcme->lcme_offset),
3369 le32_to_cpu(lcme->lcme_size));
3371 offset += le32_to_cpu(lcme->lcme_size);
3374 /* fixup layout information */
3375 lod_obj_inc_layout_gen(lo);
3376 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3377 lcm->lcm_size = cpu_to_le32(size);
3378 lcm->lcm_entry_count = cpu_to_le16(cur_entry_count + merge_entry_count);
3379 lcm->lcm_mirror_count = cpu_to_le16(mirror_count);
3380 if ((le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK) == LCM_FL_NONE)
3381 lcm->lcm_flags = cpu_to_le32(LCM_FL_RDONLY);
3383 rc = lod_striping_reload(env, lo, buf);
3387 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), buf,
3388 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3396 * Split layouts, just set the LOVEA with the layout from mbuf.
3398 static int lod_declare_layout_split(const struct lu_env *env,
3399 struct dt_object *dt, const struct lu_buf *mbuf,
3402 struct lod_object *lo = lod_dt_obj(dt);
3403 struct lov_comp_md_v1 *lcm = mbuf->lb_buf;
3407 rc = lod_striping_reload(env, lo, mbuf);
3411 lod_obj_inc_layout_gen(lo);
3412 /* fix on-disk layout gen */
3413 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3415 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), mbuf,
3416 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3421 * Implementation of dt_object_operations::do_declare_xattr_set.
3423 * \see dt_object_operations::do_declare_xattr_set() in the API description
3426 * the extension to the API:
3427 * - declaring LOVEA requests striping creation
3428 * - LU_XATTR_REPLACE means layout swap
3430 static int lod_declare_xattr_set(const struct lu_env *env,
3431 struct dt_object *dt,
3432 const struct lu_buf *buf,
3433 const char *name, int fl,
3436 struct dt_object *next = dt_object_child(dt);
3437 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3442 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
3443 if ((S_ISREG(mode) || mode == 0) &&
3444 !(fl & (LU_XATTR_REPLACE | LU_XATTR_MERGE | LU_XATTR_SPLIT)) &&
3445 (strcmp(name, XATTR_NAME_LOV) == 0 ||
3446 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
3448 * this is a request to create object's striping.
3450 * allow to declare predefined striping on a new (!mode) object
3451 * which is supposed to be replay of regular file creation
3452 * (when LOV setting is declared)
3454 * LU_XATTR_REPLACE is set to indicate a layout swap
3456 if (dt_object_exists(dt)) {
3457 rc = dt_attr_get(env, next, attr);
3461 memset(attr, 0, sizeof(*attr));
3462 attr->la_valid = LA_TYPE | LA_MODE;
3463 attr->la_mode = S_IFREG;
3465 rc = lod_declare_striped_create(env, dt, attr, buf, th);
3466 } else if (fl & LU_XATTR_MERGE) {
3467 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3468 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3469 rc = lod_declare_layout_merge(env, dt, buf, th);
3470 } else if (fl & LU_XATTR_SPLIT) {
3471 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3472 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3473 rc = lod_declare_layout_split(env, dt, buf, th);
3474 } else if (S_ISREG(mode) &&
3475 strlen(name) >= sizeof(XATTR_LUSTRE_LOV) + 3 &&
3476 allowed_lustre_lov(name)) {
3478 * this is a request to modify object's striping.
3479 * add/set/del component(s).
3481 if (!dt_object_exists(dt))
3484 rc = lod_declare_modify_layout(env, dt, name, buf, th);
3485 } else if (S_ISDIR(mode)) {
3486 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
3487 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3488 rc = lod_replace_parent_fid(env, dt, buf, th, true);
3490 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
3497 * Apply xattr changes to the object.
3499 * Applies xattr changes to the object and the stripes if the latter exist.
3501 * \param[in] env execution environment
3502 * \param[in] dt object
3503 * \param[in] buf buffer pointing to the new value of xattr
3504 * \param[in] name name of xattr
3505 * \param[in] fl flags
3506 * \param[in] th transaction handle
3508 * \retval 0 on success
3509 * \retval negative if failed
3511 static int lod_xattr_set_internal(const struct lu_env *env,
3512 struct dt_object *dt,
3513 const struct lu_buf *buf,
3514 const char *name, int fl,
3517 struct dt_object *next = dt_object_child(dt);
3518 struct lod_object *lo = lod_dt_obj(dt);
3523 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3524 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3527 /* Note: Do not set LinkEA on sub-stripes, otherwise
3528 * it will confuse the fid2path process(see mdt_path_current()).
3529 * The linkEA between master and sub-stripes is set in
3530 * lod_xattr_set_lmv(). */
3531 if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
3534 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3535 if (!lo->ldo_stripe[i])
3538 if (!dt_object_exists(lo->ldo_stripe[i]))
3541 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
3551 * Delete an extended attribute.
3553 * Deletes specified xattr from the object and the stripes if the latter exist.
3555 * \param[in] env execution environment
3556 * \param[in] dt object
3557 * \param[in] name name of xattr
3558 * \param[in] th transaction handle
3560 * \retval 0 on success
3561 * \retval negative if failed
3563 static int lod_xattr_del_internal(const struct lu_env *env,
3564 struct dt_object *dt,
3565 const char *name, struct thandle *th)
3567 struct dt_object *next = dt_object_child(dt);
3568 struct lod_object *lo = lod_dt_obj(dt);
3573 rc = lod_sub_xattr_del(env, next, name, th);
3574 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3577 if (lo->ldo_dir_stripe_count == 0)
3580 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3581 LASSERT(lo->ldo_stripe[i]);
3583 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3592 * Set default striping on a directory.
3594 * Sets specified striping on a directory object unless it matches the default
3595 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3596 * EA. This striping will be used when regular file is being created in this
3599 * \param[in] env execution environment
3600 * \param[in] dt the striped object
3601 * \param[in] buf buffer with the striping
3602 * \param[in] name name of EA
3603 * \param[in] fl xattr flag (see OSD API description)
3604 * \param[in] th transaction handle
3606 * \retval 0 on success
3607 * \retval negative if failed
3609 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
3610 struct dt_object *dt,
3611 const struct lu_buf *buf,
3612 const char *name, int fl,
3615 struct lov_user_md_v1 *lum;
3616 struct lov_user_md_v3 *v3 = NULL;
3617 const char *pool_name = NULL;
3622 LASSERT(buf != NULL && buf->lb_buf != NULL);
3625 switch (lum->lmm_magic) {
3626 case LOV_USER_MAGIC_SPECIFIC:
3627 case LOV_USER_MAGIC_V3:
3629 if (v3->lmm_pool_name[0] != '\0')
3630 pool_name = v3->lmm_pool_name;
3632 case LOV_USER_MAGIC_V1:
3633 /* if { size, offset, count } = { 0, -1, 0 } and no pool
3634 * (i.e. all default values specified) then delete default
3635 * striping from dir. */
3637 "set default striping: sz %u # %u offset %d %s %s\n",
3638 (unsigned)lum->lmm_stripe_size,
3639 (unsigned)lum->lmm_stripe_count,
3640 (int)lum->lmm_stripe_offset,
3641 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
3643 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
3644 lum->lmm_stripe_count,
3645 lum->lmm_stripe_offset,
3648 case LOV_USER_MAGIC_COMP_V1:
3650 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lum;
3651 struct lov_comp_md_entry_v1 *lcme;
3654 comp_cnt = le16_to_cpu(lcm->lcm_entry_count);
3655 for (i = 0; i < comp_cnt; i++) {
3656 lcme = &lcm->lcm_entries[i];
3657 if (lcme->lcme_flags & cpu_to_le32(LCME_FL_EXTENSION)) {
3658 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_SEL);
3667 CERROR("Invalid magic %x\n", lum->lmm_magic);
3672 rc = lod_xattr_del_internal(env, dt, name, th);
3676 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3683 * Set default striping on a directory object.
3685 * Sets specified striping on a directory object unless it matches the default
3686 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3687 * EA. This striping will be used when a new directory is being created in the
3690 * \param[in] env execution environment
3691 * \param[in] dt the striped object
3692 * \param[in] buf buffer with the striping
3693 * \param[in] name name of EA
3694 * \param[in] fl xattr flag (see OSD API description)
3695 * \param[in] th transaction handle
3697 * \retval 0 on success
3698 * \retval negative if failed
3700 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
3701 struct dt_object *dt,
3702 const struct lu_buf *buf,
3703 const char *name, int fl,
3706 struct lmv_user_md_v1 *lum;
3711 LASSERT(buf != NULL && buf->lb_buf != NULL);
3715 "set default stripe_count # %u stripe_offset %d hash %u\n",
3716 le32_to_cpu(lum->lum_stripe_count),
3717 (int)le32_to_cpu(lum->lum_stripe_offset),
3718 le32_to_cpu(lum->lum_hash_type));
3720 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
3721 le32_to_cpu(lum->lum_stripe_offset)) &&
3722 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
3723 rc = lod_xattr_del_internal(env, dt, name, th);
3727 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3736 * Turn directory into a striped directory.
3738 * During replay the client sends the striping created before MDT
3739 * failure, then the layer above LOD sends this defined striping
3740 * using ->do_xattr_set(), so LOD uses this method to replay creation
3741 * of the stripes. Notice the original information for the striping
3742 * (#stripes, FIDs, etc) was transferred in declare path.
3744 * \param[in] env execution environment
3745 * \param[in] dt the striped object
3746 * \param[in] buf not used currently
3747 * \param[in] name not used currently
3748 * \param[in] fl xattr flag (see OSD API description)
3749 * \param[in] th transaction handle
3751 * \retval 0 on success
3752 * \retval negative if failed
3754 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
3755 const struct lu_buf *buf, const char *name,
3756 int fl, struct thandle *th)
3758 struct lod_object *lo = lod_dt_obj(dt);
3759 struct lod_thread_info *info = lod_env_info(env);
3760 struct lu_attr *attr = &info->lti_attr;
3761 struct dt_object_format *dof = &info->lti_format;
3762 struct lu_buf lmv_buf;
3763 struct lu_buf slave_lmv_buf;
3764 struct lmv_mds_md_v1 *lmm;
3765 struct lmv_mds_md_v1 *slave_lmm = NULL;
3766 struct dt_insert_rec *rec = &info->lti_dt_rec;
3771 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3774 /* The stripes are supposed to be allocated in declare phase,
3775 * if there are no stripes being allocated, it will skip */
3776 if (lo->ldo_dir_stripe_count == 0) {
3777 if (lo->ldo_dir_is_foreign) {
3778 rc = lod_sub_xattr_set(env, dt_object_child(dt), buf,
3779 XATTR_NAME_LMV, fl, th);
3786 rc = dt_attr_get(env, dt_object_child(dt), attr);
3790 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_FLAGS |
3791 LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
3792 dof->dof_type = DFT_DIR;
3794 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
3797 lmm = lmv_buf.lb_buf;
3799 OBD_ALLOC_PTR(slave_lmm);
3800 if (slave_lmm == NULL)
3803 lod_prep_slave_lmv_md(slave_lmm, lmm);
3804 slave_lmv_buf.lb_buf = slave_lmm;
3805 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
3807 rec->rec_type = S_IFDIR;
3808 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3809 struct dt_object *dto = lo->ldo_stripe[i];
3810 char *stripe_name = info->lti_key;
3811 struct lu_name *sname;
3812 struct linkea_data ldata = { NULL };
3813 struct lu_buf linkea_buf;
3815 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
3819 /* fail a remote stripe creation */
3820 if (i && OBD_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_CREATE))
3823 /* don't create stripe if:
3824 * 1. it's source stripe of migrating directory
3825 * 2. it's existed stripe of splitting directory
3827 if ((lod_is_migrating(lo) && i >= lo->ldo_dir_migrate_offset) ||
3828 (lod_is_splitting(lo) && i < lo->ldo_dir_split_offset)) {
3829 if (!dt_object_exists(dto))
3830 GOTO(out, rc = -EINVAL);
3832 dt_write_lock(env, dto, DT_TGT_CHILD);
3833 rc = lod_sub_create(env, dto, attr, NULL, dof, th);
3835 dt_write_unlock(env, dto);
3839 rc = lod_sub_ref_add(env, dto, th);
3840 dt_write_unlock(env, dto);
3844 rec->rec_fid = lu_object_fid(&dto->do_lu);
3845 rc = lod_sub_insert(env, dto,
3846 (const struct dt_rec *)rec,
3847 (const struct dt_key *)dot, th);
3852 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
3853 cfs_fail_val != i) {
3854 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
3856 slave_lmm->lmv_master_mdt_index =
3859 slave_lmm->lmv_master_mdt_index =
3862 rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
3863 XATTR_NAME_LMV, 0, th);
3868 /* don't insert stripe if it's existed stripe of splitting
3869 * directory (this directory is striped).
3870 * NB, plain directory will insert itself as the first
3873 if (lod_is_splitting(lo) && lo->ldo_dir_split_offset > 1 &&
3874 lo->ldo_dir_split_offset > i)
3877 rec->rec_fid = lu_object_fid(&dt->do_lu);
3878 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
3879 (const struct dt_key *)dotdot, th);
3883 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
3885 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3886 PFID(lu_object_fid(&dto->do_lu)), i + 1);
3888 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3889 PFID(lu_object_fid(&dto->do_lu)), i);
3891 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
3892 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
3893 sname, lu_object_fid(&dt->do_lu));
3897 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
3898 linkea_buf.lb_len = ldata.ld_leh->leh_len;
3899 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
3900 XATTR_NAME_LINK, 0, th);
3904 rec->rec_fid = lu_object_fid(&dto->do_lu);
3905 rc = lod_sub_insert(env, dt_object_child(dt),
3906 (const struct dt_rec *)rec,
3907 (const struct dt_key *)stripe_name, th);
3911 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
3916 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
3917 rc = lod_sub_xattr_set(env, dt_object_child(dt),
3918 &lmv_buf, XATTR_NAME_LMV, fl, th);
3920 if (slave_lmm != NULL)
3921 OBD_FREE_PTR(slave_lmm);
3927 * Helper function to declare/execute creation of a striped directory
3929 * Called in declare/create object path, prepare striping for a directory
3930 * and prepare defaults data striping for the objects to be created in
3931 * that directory. Notice the function calls "declaration" or "execution"
3932 * methods depending on \a declare param. This is a consequence of the
3933 * current approach while we don't have natural distributed transactions:
3934 * we basically execute non-local updates in the declare phase. So, the
3935 * arguments for the both phases are the same and this is the reason for
3936 * this function to exist.
3938 * \param[in] env execution environment
3939 * \param[in] dt object
3940 * \param[in] attr attributes the stripes will be created with
3941 * \param[in] lmu lmv_user_md if MDT indices are specified
3942 * \param[in] dof format of stripes (see OSD API description)
3943 * \param[in] th transaction handle
3944 * \param[in] declare where to call "declare" or "execute" methods
3946 * \retval 0 on success
3947 * \retval negative if failed
3949 static int lod_dir_striping_create_internal(const struct lu_env *env,
3950 struct dt_object *dt,
3951 struct lu_attr *attr,
3952 const struct lu_buf *lmu,
3953 struct dt_object_format *dof,
3957 struct lod_thread_info *info = lod_env_info(env);
3958 struct lod_object *lo = lod_dt_obj(dt);
3959 const struct lod_default_striping *lds = lo->ldo_def_striping;
3963 LASSERT(ergo(lds != NULL,
3964 lds->lds_def_striping_set ||
3965 lds->lds_dir_def_striping_set));
3967 if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
3968 lo->ldo_dir_stripe_offset)) {
3970 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3971 int stripe_count = lo->ldo_dir_stripe_count;
3973 if (info->lti_ea_store_size < sizeof(*v1)) {
3974 rc = lod_ea_store_resize(info, sizeof(*v1));
3977 v1 = info->lti_ea_store;
3980 memset(v1, 0, sizeof(*v1));
3981 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3982 v1->lum_stripe_count = cpu_to_le32(stripe_count);
3983 v1->lum_stripe_offset =
3984 cpu_to_le32(lo->ldo_dir_stripe_offset);
3986 info->lti_buf.lb_buf = v1;
3987 info->lti_buf.lb_len = sizeof(*v1);
3988 lmu = &info->lti_buf;
3992 rc = lod_declare_xattr_set_lmv(env, dt, attr, lmu, dof,
3995 rc = lod_xattr_set_lmv(env, dt, lmu, XATTR_NAME_LMV, 0,
4000 /* foreign LMV EA case */
4002 struct lmv_foreign_md *lfm = lmu->lb_buf;
4004 if (lfm->lfm_magic == LMV_MAGIC_FOREIGN) {
4005 rc = lod_declare_xattr_set_lmv(env, dt, attr,
4009 if (lo->ldo_dir_is_foreign) {
4010 LASSERT(lo->ldo_foreign_lmv != NULL &&
4011 lo->ldo_foreign_lmv_size > 0);
4012 info->lti_buf.lb_buf = lo->ldo_foreign_lmv;
4013 info->lti_buf.lb_len = lo->ldo_foreign_lmv_size;
4014 lmu = &info->lti_buf;
4015 rc = lod_xattr_set_lmv(env, dt, lmu,
4016 XATTR_NAME_LMV, 0, th);
4021 /* Transfer default LMV striping from the parent */
4022 if (lds != NULL && lds->lds_dir_def_striping_set &&
4023 !(LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
4024 lds->lds_dir_def_stripe_offset) &&
4025 le32_to_cpu(lds->lds_dir_def_hash_type) !=
4026 LMV_HASH_TYPE_UNKNOWN)) {
4027 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
4029 if (info->lti_ea_store_size < sizeof(*v1)) {
4030 rc = lod_ea_store_resize(info, sizeof(*v1));
4033 v1 = info->lti_ea_store;
4036 memset(v1, 0, sizeof(*v1));
4037 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
4038 v1->lum_stripe_count =
4039 cpu_to_le32(lds->lds_dir_def_stripe_count);
4040 v1->lum_stripe_offset =
4041 cpu_to_le32(lds->lds_dir_def_stripe_offset);
4043 cpu_to_le32(lds->lds_dir_def_hash_type);
4045 info->lti_buf.lb_buf = v1;
4046 info->lti_buf.lb_len = sizeof(*v1);
4048 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4049 XATTR_NAME_DEFAULT_LMV,
4052 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
4054 XATTR_NAME_DEFAULT_LMV, 0,
4060 /* Transfer default LOV striping from the parent */
4061 if (lds != NULL && lds->lds_def_striping_set &&
4062 lds->lds_def_comp_cnt != 0) {
4063 struct lov_mds_md *lmm;
4064 int lmm_size = lod_comp_md_size(lo, true);
4066 if (info->lti_ea_store_size < lmm_size) {
4067 rc = lod_ea_store_resize(info, lmm_size);
4071 lmm = info->lti_ea_store;
4073 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
4077 info->lti_buf.lb_buf = lmm;
4078 info->lti_buf.lb_len = lmm_size;
4081 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4082 XATTR_NAME_LOV, 0, th);
4084 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4085 XATTR_NAME_LOV, 0, th);
4093 static int lod_declare_dir_striping_create(const struct lu_env *env,
4094 struct dt_object *dt,
4095 struct lu_attr *attr,
4097 struct dt_object_format *dof,
4100 return lod_dir_striping_create_internal(env, dt, attr, lmu, dof, th,
4104 static int lod_dir_striping_create(const struct lu_env *env,
4105 struct dt_object *dt,
4106 struct lu_attr *attr,
4107 struct dt_object_format *dof,
4110 return lod_dir_striping_create_internal(env, dt, attr, NULL, dof, th,
4115 * Make LOV EA for striped object.
4117 * Generate striping information and store it in the LOV EA of the given
4118 * object. The caller must ensure nobody else is calling the function
4119 * against the object concurrently. The transaction must be started.
4120 * FLDB service must be running as well; it's used to map FID to the target,
4121 * which is stored in LOV EA.
4123 * \param[in] env execution environment for this thread
4124 * \param[in] lo LOD object
4125 * \param[in] th transaction handle
4127 * \retval 0 if LOV EA is stored successfully
4128 * \retval negative error number on failure
4130 static int lod_generate_and_set_lovea(const struct lu_env *env,
4131 struct lod_object *lo,
4134 struct lod_thread_info *info = lod_env_info(env);
4135 struct dt_object *next = dt_object_child(&lo->ldo_obj);
4136 struct lov_mds_md_v1 *lmm;
4142 if (lo->ldo_comp_cnt == 0 && !lo->ldo_is_foreign) {
4143 lod_striping_free(env, lo);
4144 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
4148 lmm_size = lod_comp_md_size(lo, false);
4149 if (info->lti_ea_store_size < lmm_size) {
4150 rc = lod_ea_store_resize(info, lmm_size);
4154 lmm = info->lti_ea_store;
4156 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
4160 info->lti_buf.lb_buf = lmm;
4161 info->lti_buf.lb_len = lmm_size;
4162 rc = lod_sub_xattr_set(env, next, &info->lti_buf,
4163 XATTR_NAME_LOV, 0, th);
4167 static __u32 lod_gen_component_id(struct lod_object *lo,
4168 int mirror_id, int comp_idx);
4171 * Repeat an existing component
4173 * Creates a new layout by replicating an existing component. Uses striping
4174 * policy from previous component as a template for the striping for the new
4177 * New component starts with zero length, will be extended (or removed) before
4178 * returning layout to client.
4180 * NB: Reallocates layout components array (lo->ldo_comp_entries), invalidating
4181 * any pre-existing pointers to components. Handle with care.
4183 * \param[in] env execution environment for this thread
4184 * \param[in,out] lo object to update the layout of
4185 * \param[in] index index of component to copy
4187 * \retval 0 on success
4188 * \retval negative errno on error
4190 static int lod_layout_repeat_comp(const struct lu_env *env,
4191 struct lod_object *lo, int index)
4193 struct lod_layout_component *lod_comp;
4194 struct lod_layout_component *new_comp = NULL;
4195 struct lod_layout_component *comp_array;
4196 int rc = 0, i, new_cnt = lo->ldo_comp_cnt + 1;
4201 lod_comp = &lo->ldo_comp_entries[index];
4202 LASSERT(lod_comp_inited(lod_comp) && lod_comp->llc_id != LCME_ID_INVAL);
4204 CDEBUG(D_LAYOUT, "repeating component %d\n", index);
4206 OBD_ALLOC_PTR_ARRAY(comp_array, new_cnt);
4207 if (comp_array == NULL)
4208 GOTO(out, rc = -ENOMEM);
4210 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4211 memcpy(&comp_array[i + offset], &lo->ldo_comp_entries[i],
4212 sizeof(*comp_array));
4214 /* Duplicate this component in to the next slot */
4216 new_comp = &comp_array[i + 1];
4217 memcpy(&comp_array[i + 1], &lo->ldo_comp_entries[i],
4218 sizeof(*comp_array));
4219 /* We must now skip this new component when copying */
4224 /* Set up copied component */
4225 new_comp->llc_flags &= ~LCME_FL_INIT;
4226 new_comp->llc_stripe = NULL;
4227 new_comp->llc_stripes_allocated = 0;
4228 new_comp->llc_ost_indices = NULL;
4229 new_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4230 /* for uninstantiated components, layout gen stores default stripe
4232 new_comp->llc_layout_gen = lod_comp->llc_stripe_offset;
4233 /* This makes the repeated component zero-length, placed at the end of
4234 * the preceding component */
4235 new_comp->llc_extent.e_start = new_comp->llc_extent.e_end;
4236 new_comp->llc_timestamp = lod_comp->llc_timestamp;
4237 new_comp->llc_pool = NULL;
4239 rc = lod_set_pool(&new_comp->llc_pool, lod_comp->llc_pool);
4243 if (new_comp->llc_ostlist.op_array) {
4244 __u32 *op_array = NULL;
4246 OBD_ALLOC(op_array, new_comp->llc_ostlist.op_size);
4248 GOTO(out, rc = -ENOMEM);
4249 memcpy(op_array, &new_comp->llc_ostlist.op_array,
4250 new_comp->llc_ostlist.op_size);
4251 new_comp->llc_ostlist.op_array = op_array;
4254 OBD_FREE_PTR_ARRAY(lo->ldo_comp_entries, lo->ldo_comp_cnt);
4255 lo->ldo_comp_entries = comp_array;
4256 lo->ldo_comp_cnt = new_cnt;
4258 /* Generate an id for the new component */
4259 mirror_id = mirror_id_of(new_comp->llc_id);
4260 new_comp->llc_id = LCME_ID_INVAL;
4261 new_comp->llc_id = lod_gen_component_id(lo, mirror_id, index + 1);
4262 if (new_comp->llc_id == LCME_ID_INVAL)
4263 GOTO(out, rc = -ERANGE);
4268 OBD_FREE_PTR_ARRAY(comp_array, new_cnt);
4273 static int lod_layout_data_init(struct lod_thread_info *info, __u32 comp_cnt)
4277 /* clear memory region that will be used for layout change */
4278 memset(&info->lti_layout_attr, 0, sizeof(struct lu_attr));
4279 info->lti_count = 0;
4281 if (info->lti_comp_size >= comp_cnt)
4284 if (info->lti_comp_size > 0) {
4285 OBD_FREE_PTR_ARRAY(info->lti_comp_idx, info->lti_comp_size);
4286 info->lti_comp_size = 0;
4289 OBD_ALLOC_PTR_ARRAY(info->lti_comp_idx, comp_cnt);
4290 if (!info->lti_comp_idx)
4293 info->lti_comp_size = comp_cnt;
4298 * Prepare new layout minus deleted components
4300 * Removes components marked for deletion (LCME_ID_INVAL) by copying to a new
4301 * layout and skipping those components. Removes stripe objects if any exist.
4304 * Reallocates layout components array (lo->ldo_comp_entries), invalidating
4305 * any pre-existing pointers to components.
4307 * Caller is responsible for updating mirror end (ldo_mirror[].lme_end).
4309 * \param[in] env execution environment for this thread
4310 * \param[in,out] lo object to update the layout of
4311 * \param[in] th transaction handle for this operation
4313 * \retval # of components deleted
4314 * \retval negative errno on error
4316 static int lod_layout_del_prep_layout(const struct lu_env *env,
4317 struct lod_object *lo,
4320 struct lod_layout_component *lod_comp;
4321 struct lod_thread_info *info = lod_env_info(env);
4322 int rc = 0, i, j, deleted = 0;
4326 LASSERT(lo->ldo_is_composite);
4327 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
4329 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
4333 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4334 lod_comp = &lo->ldo_comp_entries[i];
4336 if (lod_comp->llc_id != LCME_ID_INVAL) {
4337 /* Build array of things to keep */
4338 info->lti_comp_idx[info->lti_count++] = i;
4342 lod_obj_set_pool(lo, i, NULL);
4343 if (lod_comp->llc_ostlist.op_array) {
4344 OBD_FREE(lod_comp->llc_ostlist.op_array,
4345 lod_comp->llc_ostlist.op_size);
4346 lod_comp->llc_ostlist.op_array = NULL;
4347 lod_comp->llc_ostlist.op_size = 0;
4351 CDEBUG(D_LAYOUT, "deleting comp %d, left %d\n", i,
4352 lo->ldo_comp_cnt - deleted);
4354 /* No striping info for this component */
4355 if (lod_comp->llc_stripe == NULL)
4358 LASSERT(lod_comp->llc_stripe_count > 0);
4359 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4360 struct dt_object *obj = lod_comp->llc_stripe[j];
4365 /* components which are not init have no sub objects
4367 if (lod_comp_inited(lod_comp)) {
4368 rc = lod_sub_destroy(env, obj, th);
4373 lu_object_put(env, &obj->do_lu);
4374 lod_comp->llc_stripe[j] = NULL;
4376 OBD_FREE_PTR_ARRAY(lod_comp->llc_stripe,
4377 lod_comp->llc_stripes_allocated);
4378 lod_comp->llc_stripe = NULL;
4379 OBD_FREE_PTR_ARRAY(lod_comp->llc_ost_indices,
4380 lod_comp->llc_stripes_allocated);
4381 lod_comp->llc_ost_indices = NULL;
4382 lod_comp->llc_stripes_allocated = 0;
4385 /* info->lti_count has the amount of left components */
4386 LASSERTF(info->lti_count >= 0 && info->lti_count < lo->ldo_comp_cnt,
4387 "left = %d, lo->ldo_comp_cnt %d\n", (int)info->lti_count,
4388 (int)lo->ldo_comp_cnt);
4390 if (info->lti_count > 0) {
4391 struct lod_layout_component *comp_array;
4393 OBD_ALLOC_PTR_ARRAY(comp_array, info->lti_count);
4394 if (comp_array == NULL)
4395 GOTO(out, rc = -ENOMEM);
4397 for (i = 0; i < info->lti_count; i++) {
4398 memcpy(&comp_array[i],
4399 &lo->ldo_comp_entries[info->lti_comp_idx[i]],
4400 sizeof(*comp_array));
4403 OBD_FREE_PTR_ARRAY(lo->ldo_comp_entries, lo->ldo_comp_cnt);
4404 lo->ldo_comp_entries = comp_array;
4405 lo->ldo_comp_cnt = info->lti_count;
4407 lod_free_comp_entries(lo);
4412 return rc ? rc : deleted;
4416 * Delete layout component(s)
4418 * This function sets up the layout data in the env and does the setattrs
4419 * required to write out the new layout. The layout itself is modified in
4420 * lod_layout_del_prep_layout.
4422 * \param[in] env execution environment for this thread
4423 * \param[in] dt object
4424 * \param[in] th transaction handle
4426 * \retval 0 on success
4427 * \retval negative error number on failure
4429 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
4432 struct lod_object *lo = lod_dt_obj(dt);
4433 struct dt_object *next = dt_object_child(dt);
4434 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4437 LASSERT(lo->ldo_mirror_count == 1);
4439 rc = lod_layout_del_prep_layout(env, lo, th);
4443 /* Only do this if we didn't delete all components */
4444 if (lo->ldo_comp_cnt > 0) {
4445 lo->ldo_mirrors[0].lme_end = lo->ldo_comp_cnt - 1;
4446 lod_obj_inc_layout_gen(lo);
4449 LASSERT(dt_object_exists(dt));
4450 rc = dt_attr_get(env, next, attr);
4454 if (attr->la_size > 0) {
4456 attr->la_valid = LA_SIZE;
4457 rc = lod_sub_attr_set(env, next, attr, th);
4462 rc = lod_generate_and_set_lovea(env, lo, th);
4466 lod_striping_free(env, lo);
4471 static int lod_get_default_lov_striping(const struct lu_env *env,
4472 struct lod_object *lo,
4473 struct lod_default_striping *lds,
4474 struct dt_allocation_hint *ah);
4476 * Implementation of dt_object_operations::do_xattr_set.
4478 * Sets specified extended attribute on the object. Three types of EAs are
4480 * LOV EA - stores striping for a regular file or default striping (when set
4482 * LMV EA - stores a marker for the striped directories
4483 * DMV EA - stores default directory striping
4485 * When striping is applied to a non-striped existing object (this is called
4486 * late striping), then LOD notices the caller wants to turn the object into a
4487 * striped one. The stripe objects are created and appropriate EA is set:
4488 * LOV EA storing all the stripes directly or LMV EA storing just a small header
4489 * with striping configuration.
4491 * \see dt_object_operations::do_xattr_set() in the API description for details.
4493 static int lod_xattr_set(const struct lu_env *env,
4494 struct dt_object *dt, const struct lu_buf *buf,
4495 const char *name, int fl, struct thandle *th)
4497 struct dt_object *next = dt_object_child(dt);
4502 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4503 !strcmp(name, XATTR_NAME_LMV)) {
4505 case LU_XATTR_CREATE:
4506 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
4509 case LU_XATTR_REPLACE:
4510 rc = lod_dir_layout_set(env, dt, buf, fl, th);
4517 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4518 strcmp(name, XATTR_NAME_LOV) == 0) {
4519 struct lod_default_striping *lds = lod_lds_buf_get(env);
4520 struct lov_user_md_v1 *v1 = buf->lb_buf;
4521 char pool[LOV_MAXPOOLNAME + 1];
4524 /* get existing striping config */
4525 rc = lod_get_default_lov_striping(env, lod_dt_obj(dt), lds,
4530 memset(pool, 0, sizeof(pool));
4531 if (lds->lds_def_striping_set == 1)
4532 lod_layout_get_pool(lds->lds_def_comp_entries,
4533 lds->lds_def_comp_cnt, pool,
4536 is_del = LOVEA_DELETE_VALUES(v1->lmm_stripe_size,
4537 v1->lmm_stripe_count,
4538 v1->lmm_stripe_offset,
4541 /* Retain the pool name if it is not given */
4542 if (v1->lmm_magic == LOV_USER_MAGIC_V1 && pool[0] != '\0' &&
4544 struct lod_thread_info *info = lod_env_info(env);
4545 struct lov_user_md_v3 *v3 = info->lti_ea_store;
4547 memset(v3, 0, sizeof(*v3));
4548 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
4549 v3->lmm_pattern = cpu_to_le32(v1->lmm_pattern);
4550 v3->lmm_stripe_count =
4551 cpu_to_le32(v1->lmm_stripe_count);
4552 v3->lmm_stripe_offset =
4553 cpu_to_le32(v1->lmm_stripe_offset);
4554 v3->lmm_stripe_size = cpu_to_le32(v1->lmm_stripe_size);
4556 strlcpy(v3->lmm_pool_name, pool,
4557 sizeof(v3->lmm_pool_name));
4559 info->lti_buf.lb_buf = v3;
4560 info->lti_buf.lb_len = sizeof(*v3);
4561 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4564 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name,
4568 if (lds->lds_def_striping_set == 1 &&
4569 lds->lds_def_comp_entries != NULL)
4570 lod_free_def_comp_entries(lds);
4573 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4574 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
4576 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
4579 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4580 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4581 strcmp(name, XATTR_LUSTRE_LOV) == 0 ||
4582 allowed_lustre_lov(name))) {
4583 /* in case of lov EA swap, just set it
4584 * if not, it is a replay so check striping match what we
4585 * already have during req replay, declare_xattr_set()
4586 * defines striping, then create() does the work */
4587 if (fl & LU_XATTR_REPLACE) {
4588 /* free stripes, then update disk */
4589 lod_striping_free(env, lod_dt_obj(dt));
4591 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
4592 } else if (dt_object_remote(dt)) {
4593 /* This only happens during migration, see
4594 * mdd_migrate_create(), in which Master MDT will
4595 * create a remote target object, and only set
4596 * (migrating) stripe EA on the remote object,
4597 * and does not need creating each stripes. */
4598 rc = lod_sub_xattr_set(env, next, buf, name,
4600 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
4601 /* delete component(s) */
4602 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
4603 rc = lod_layout_del(env, dt, th);
4606 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
4607 * it's going to create create file with specified
4608 * component(s), the striping must have not being
4609 * cached in this case;
4611 * Otherwise, it's going to add/change component(s) to
4612 * an existing file, the striping must have been cached
4615 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
4616 !strcmp(name, XATTR_NAME_LOV),
4617 !lod_dt_obj(dt)->ldo_comp_cached));
4619 rc = lod_striped_create(env, dt, NULL, NULL, th);
4622 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
4623 rc = lod_replace_parent_fid(env, dt, buf, th, false);
4628 /* then all other xattr */
4629 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
4635 * Implementation of dt_object_operations::do_declare_xattr_del.
4637 * \see dt_object_operations::do_declare_xattr_del() in the API description
4640 static int lod_declare_xattr_del(const struct lu_env *env,
4641 struct dt_object *dt, const char *name,
4644 struct lod_object *lo = lod_dt_obj(dt);
4645 struct dt_object *next = dt_object_child(dt);
4650 rc = lod_sub_declare_xattr_del(env, next, name, th);
4654 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4657 /* NB: don't delete stripe LMV, because when we do this, normally we
4658 * will remove stripes, besides, if directory LMV is corrupt, this will
4659 * prevent deleting its LMV and fixing it (via LFSCK).
4661 if (!strcmp(name, XATTR_NAME_LMV))
4664 rc = lod_striping_load(env, lo);
4668 if (lo->ldo_dir_stripe_count == 0)
4671 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4672 struct dt_object *dto = lo->ldo_stripe[i];
4677 rc = lod_sub_declare_xattr_del(env, dto, name, th);
4686 * Implementation of dt_object_operations::do_xattr_del.
4688 * If EA storing a regular striping is being deleted, then release
4689 * all the references to the stripe objects in core.
4691 * \see dt_object_operations::do_xattr_del() in the API description for details.
4693 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
4694 const char *name, struct thandle *th)
4696 struct dt_object *next = dt_object_child(dt);
4697 struct lod_object *lo = lod_dt_obj(dt);
4702 if (!strcmp(name, XATTR_NAME_LOV) || !strcmp(name, XATTR_NAME_LMV))
4703 lod_striping_free(env, lod_dt_obj(dt));
4705 rc = lod_sub_xattr_del(env, next, name, th);
4706 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
4709 if (!strcmp(name, XATTR_NAME_LMV))
4712 if (lo->ldo_dir_stripe_count == 0)
4715 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4716 struct dt_object *dto = lo->ldo_stripe[i];
4721 rc = lod_sub_xattr_del(env, dto, name, th);
4730 * Implementation of dt_object_operations::do_xattr_list.
4732 * \see dt_object_operations::do_xattr_list() in the API description
4735 static int lod_xattr_list(const struct lu_env *env,
4736 struct dt_object *dt, const struct lu_buf *buf)
4738 return dt_xattr_list(env, dt_object_child(dt), buf);
4741 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
4743 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
4747 * Copy OST list from layout provided by user.
4749 * \param[in] lod_comp layout_component to be filled
4750 * \param[in] v3 LOV EA V3 user data
4752 * \retval 0 on success
4753 * \retval negative if failed
4755 int lod_comp_copy_ost_lists(struct lod_layout_component *lod_comp,
4756 struct lov_user_md_v3 *v3)
4762 if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
4763 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
4765 if (lod_comp->llc_ostlist.op_array) {
4766 if (lod_comp->llc_ostlist.op_size >=
4767 v3->lmm_stripe_count * sizeof(__u32)) {
4768 lod_comp->llc_ostlist.op_count =
4769 v3->lmm_stripe_count;
4772 OBD_FREE(lod_comp->llc_ostlist.op_array,
4773 lod_comp->llc_ostlist.op_size);
4776 /* copy ost list from lmm */
4777 lod_comp->llc_ostlist.op_count = v3->lmm_stripe_count;
4778 lod_comp->llc_ostlist.op_size = v3->lmm_stripe_count * sizeof(__u32);
4779 OBD_ALLOC(lod_comp->llc_ostlist.op_array,
4780 lod_comp->llc_ostlist.op_size);
4781 if (!lod_comp->llc_ostlist.op_array)
4784 for (j = 0; j < v3->lmm_stripe_count; j++) {
4785 lod_comp->llc_ostlist.op_array[j] =
4786 v3->lmm_objects[j].l_ost_idx;
4794 * Get default striping.
4796 * \param[in] env execution environment
4797 * \param[in] lo object
4798 * \param[out] lds default striping
4800 * \retval 0 on success
4801 * \retval negative if failed
4803 static int lod_get_default_lov_striping(const struct lu_env *env,
4804 struct lod_object *lo,
4805 struct lod_default_striping *lds,
4806 struct dt_allocation_hint *ah)
4808 struct lod_thread_info *info = lod_env_info(env);
4809 struct lov_user_md_v1 *v1 = NULL;
4810 struct lov_user_md_v3 *v3 = NULL;
4811 struct lov_comp_md_v1 *comp_v1 = NULL;
4819 rc = lod_get_lov_ea(env, lo);
4823 if (rc < (typeof(rc))sizeof(struct lov_user_md))
4826 v1 = info->lti_ea_store;
4827 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
4828 lustre_swab_lov_user_md_v1(v1);
4829 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
4830 v3 = (struct lov_user_md_v3 *)v1;
4831 lustre_swab_lov_user_md_v3(v3);
4832 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_SPECIFIC)) {
4833 v3 = (struct lov_user_md_v3 *)v1;
4834 lustre_swab_lov_user_md_v3(v3);
4835 lustre_swab_lov_user_md_objects(v3->lmm_objects,
4836 v3->lmm_stripe_count);
4837 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1) ||
4838 v1->lmm_magic == __swab32(LOV_USER_MAGIC_SEL)) {
4839 comp_v1 = (struct lov_comp_md_v1 *)v1;
4840 lustre_swab_lov_comp_md_v1(comp_v1);
4843 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
4844 v1->lmm_magic != LOV_MAGIC_COMP_V1 &&
4845 v1->lmm_magic != LOV_MAGIC_SEL &&
4846 v1->lmm_magic != LOV_USER_MAGIC_SPECIFIC)
4849 if ((v1->lmm_magic == LOV_MAGIC_COMP_V1 ||
4850 v1->lmm_magic == LOV_MAGIC_SEL) &&
4851 !(ah && ah->dah_append_stripes)) {
4852 comp_v1 = (struct lov_comp_md_v1 *)v1;
4853 comp_cnt = comp_v1->lcm_entry_count;
4856 mirror_cnt = comp_v1->lcm_mirror_count + 1;
4864 /* realloc default comp entries if necessary */
4865 rc = lod_def_striping_comp_resize(lds, comp_cnt);
4869 lds->lds_def_comp_cnt = comp_cnt;
4870 lds->lds_def_striping_is_composite = composite;
4871 lds->lds_def_mirror_cnt = mirror_cnt;
4873 for (i = 0; i < comp_cnt; i++) {
4874 struct lod_layout_component *lod_comp;
4877 lod_comp = &lds->lds_def_comp_entries[i];
4879 * reset lod_comp values, llc_stripes is always NULL in
4880 * the default striping template, llc_pool will be reset
4883 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
4886 v1 = (struct lov_user_md *)((char *)comp_v1 +
4887 comp_v1->lcm_entries[i].lcme_offset);
4888 lod_comp->llc_extent =
4889 comp_v1->lcm_entries[i].lcme_extent;
4890 /* We only inherit certain flags from the layout */
4891 lod_comp->llc_flags =
4892 comp_v1->lcm_entries[i].lcme_flags &
4893 LCME_TEMPLATE_FLAGS;
4896 if (!lov_pattern_supported(v1->lmm_pattern) &&
4897 !(v1->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
4898 lod_free_def_comp_entries(lds);
4902 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d stripe_offset=%d append_stripes=%d\n",
4903 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
4904 (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
4905 (int)v1->lmm_stripe_offset,
4906 ah ? ah->dah_append_stripes : 0);
4908 if (ah && ah->dah_append_stripes)
4909 lod_comp->llc_stripe_count = ah->dah_append_stripes;
4911 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
4912 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
4913 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
4914 lod_comp->llc_pattern = v1->lmm_pattern;
4917 if (ah && ah->dah_append_pool && ah->dah_append_pool[0]) {
4918 pool = ah->dah_append_pool;
4919 } else if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
4920 /* XXX: sanity check here */
4921 v3 = (struct lov_user_md_v3 *) v1;
4922 if (v3->lmm_pool_name[0] != '\0')
4923 pool = v3->lmm_pool_name;
4925 lod_set_def_pool(lds, i, pool);
4926 if (v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
4927 v3 = (struct lov_user_md_v3 *)v1;
4928 rc = lod_comp_copy_ost_lists(lod_comp, v3);
4931 } else if (lod_comp->llc_ostlist.op_array &&
4932 lod_comp->llc_ostlist.op_count) {
4933 for (j = 0; j < lod_comp->llc_ostlist.op_count; j++)
4934 lod_comp->llc_ostlist.op_array[j] = -1;
4935 lod_comp->llc_ostlist.op_count = 0;
4939 lds->lds_def_striping_set = 1;
4944 * Get default directory striping.
4946 * \param[in] env execution environment
4947 * \param[in] lo object
4948 * \param[out] lds default striping
4950 * \retval 0 on success
4951 * \retval negative if failed
4953 static int lod_get_default_lmv_striping(const struct lu_env *env,
4954 struct lod_object *lo,
4955 struct lod_default_striping *lds)
4957 struct lmv_user_md *lmu;
4960 lds->lds_dir_def_striping_set = 0;
4962 rc = lod_get_default_lmv_ea(env, lo);
4966 if (rc >= (int)sizeof(*lmu)) {
4967 struct lod_thread_info *info = lod_env_info(env);
4969 lmu = info->lti_ea_store;
4971 lds->lds_dir_def_stripe_count =
4972 le32_to_cpu(lmu->lum_stripe_count);
4973 lds->lds_dir_def_stripe_offset =
4974 le32_to_cpu(lmu->lum_stripe_offset);
4975 lds->lds_dir_def_hash_type =
4976 le32_to_cpu(lmu->lum_hash_type);
4977 lds->lds_dir_def_striping_set = 1;
4984 * Get default striping in the object.
4986 * Get object default striping and default directory striping.
4988 * \param[in] env execution environment
4989 * \param[in] lo object
4990 * \param[out] lds default striping
4992 * \retval 0 on success
4993 * \retval negative if failed
4995 static int lod_get_default_striping(const struct lu_env *env,
4996 struct lod_object *lo,
4997 struct lod_default_striping *lds)
5001 rc = lod_get_default_lov_striping(env, lo, lds, NULL);
5002 rc1 = lod_get_default_lmv_striping(env, lo, lds);
5003 if (rc == 0 && rc1 < 0)
5010 * Apply default striping on object.
5012 * If object striping pattern is not set, set to the one in default striping.
5013 * The default striping is from parent or fs.
5015 * \param[in] lo new object
5016 * \param[in] lds default striping
5017 * \param[in] mode new object's mode
5019 static void lod_striping_from_default(struct lod_object *lo,
5020 const struct lod_default_striping *lds,
5023 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5026 if (lds->lds_def_striping_set && S_ISREG(mode)) {
5027 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
5029 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
5030 lds->lds_def_comp_cnt);
5034 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
5035 if (lds->lds_def_mirror_cnt > 1)
5036 lo->ldo_flr_state = LCM_FL_RDONLY;
5038 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5039 struct lod_layout_component *obj_comp =
5040 &lo->ldo_comp_entries[i];
5041 struct lod_layout_component *def_comp =
5042 &lds->lds_def_comp_entries[i];
5044 CDEBUG(D_LAYOUT, "Inherit from default: flags=%#x "
5045 "size=%hu nr=%u offset=%u pattern=%#x pool=%s\n",
5046 def_comp->llc_flags,
5047 def_comp->llc_stripe_size,
5048 def_comp->llc_stripe_count,
5049 def_comp->llc_stripe_offset,
5050 def_comp->llc_pattern,
5051 def_comp->llc_pool ?: "");
5053 *obj_comp = *def_comp;
5054 if (def_comp->llc_pool != NULL) {
5055 /* pointer was copied from def_comp */
5056 obj_comp->llc_pool = NULL;
5057 lod_obj_set_pool(lo, i, def_comp->llc_pool);
5061 if (def_comp->llc_ostlist.op_array &&
5062 def_comp->llc_ostlist.op_count) {
5063 OBD_ALLOC(obj_comp->llc_ostlist.op_array,
5064 obj_comp->llc_ostlist.op_size);
5065 if (!obj_comp->llc_ostlist.op_array)
5067 memcpy(obj_comp->llc_ostlist.op_array,
5068 def_comp->llc_ostlist.op_array,
5069 obj_comp->llc_ostlist.op_size);
5070 } else if (def_comp->llc_ostlist.op_array) {
5071 obj_comp->llc_ostlist.op_array = NULL;
5075 * Don't initialize these fields for plain layout
5076 * (v1/v3) here, they are inherited in the order of
5077 * 'parent' -> 'fs default (root)' -> 'global default
5078 * values for stripe_count & stripe_size'.
5080 * see lod_ah_init().
5082 if (!lo->ldo_is_composite)
5085 lod_adjust_stripe_info(obj_comp, desc, 0);
5087 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
5088 if (lo->ldo_dir_stripe_count == 0)
5089 lo->ldo_dir_stripe_count =
5090 lds->lds_dir_def_stripe_count;
5091 if (lo->ldo_dir_stripe_offset == -1)
5092 lo->ldo_dir_stripe_offset =
5093 lds->lds_dir_def_stripe_offset;
5094 if (lo->ldo_dir_hash_type == 0)
5095 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
5097 CDEBUG(D_LAYOUT, "striping from default dir: count:%hu, "
5098 "offset:%u, hash_type:%u\n",
5099 lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
5100 lo->ldo_dir_hash_type);
5104 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root,
5107 struct lod_layout_component *lod_comp;
5109 if (lo->ldo_comp_cnt == 0)
5112 if (lo->ldo_is_composite)
5115 lod_comp = &lo->ldo_comp_entries[0];
5117 if (lod_comp->llc_stripe_count <= 0 ||
5118 lod_comp->llc_stripe_size <= 0)
5121 if (from_root && (lod_comp->llc_pool == NULL ||
5122 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
5125 if (append_pool && append_pool[0])
5132 * Implementation of dt_object_operations::do_ah_init.
5134 * This method is used to make a decision on the striping configuration for the
5135 * object being created. It can be taken from the \a parent object if it exists,
5136 * or filesystem's default. The resulting configuration (number of stripes,
5137 * stripe size/offset, pool name, etc) is stored in the object itself and will
5138 * be used by the methods like ->doo_declare_create().
5140 * \see dt_object_operations::do_ah_init() in the API description for details.
5142 static void lod_ah_init(const struct lu_env *env,
5143 struct dt_allocation_hint *ah,
5144 struct dt_object *parent,
5145 struct dt_object *child,
5148 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
5149 struct lod_thread_info *info = lod_env_info(env);
5150 struct lod_default_striping *lds = lod_lds_buf_get(env);
5151 struct dt_object *nextp = NULL;
5152 struct dt_object *nextc;
5153 struct lod_object *lp = NULL;
5154 struct lod_object *lc;
5155 struct lov_desc *desc;
5156 struct lod_layout_component *lod_comp;
5162 if (ah->dah_append_stripes == -1)
5163 ah->dah_append_stripes =
5164 d->lod_ost_descs.ltd_lov_desc.ld_tgt_count;
5166 if (likely(parent)) {
5167 nextp = dt_object_child(parent);
5168 lp = lod_dt_obj(parent);
5171 nextc = dt_object_child(child);
5172 lc = lod_dt_obj(child);
5174 LASSERT(!lod_obj_is_striped(child));
5175 /* default layout template may have been set on the regular file
5176 * when this is called from mdd_create_data() */
5177 if (S_ISREG(child_mode))
5178 lod_free_comp_entries(lc);
5180 if (!dt_object_exists(nextc))
5181 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
5183 if (S_ISDIR(child_mode)) {
5184 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
5186 /* other default values are 0 */
5187 lc->ldo_dir_stripe_offset = -1;
5189 /* no default striping configuration is needed for
5192 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5193 le32_to_cpu(lum1->lum_magic) == LMV_MAGIC_FOREIGN) {
5194 lc->ldo_dir_is_foreign = true;
5195 /* keep stripe_count 0 and stripe_offset -1 */
5196 CDEBUG(D_INFO, "no default striping for foreign dir\n");
5201 * If parent object is not root directory,
5202 * then get default striping from parent object.
5204 if (likely(lp != NULL)) {
5205 lod_get_default_striping(env, lp, lds);
5207 /* inherit default striping except ROOT */
5208 if ((lds->lds_def_striping_set ||
5209 lds->lds_dir_def_striping_set) &&
5210 !fid_is_root(lod_object_fid(lp)))
5211 lc->ldo_def_striping = lds;
5214 /* It should always honour the specified stripes */
5215 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
5216 * will have old magic. In this case, we should ignore the
5217 * stripe count and try to create dir by default stripe.
5219 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5220 (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
5221 le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
5222 lc->ldo_dir_stripe_count =
5223 le32_to_cpu(lum1->lum_stripe_count);
5224 lc->ldo_dir_stripe_offset =
5225 le32_to_cpu(lum1->lum_stripe_offset);
5226 lc->ldo_dir_hash_type =
5227 le32_to_cpu(lum1->lum_hash_type);
5229 "set dirstripe: count %hu, offset %d, hash %u\n",
5230 lc->ldo_dir_stripe_count,
5231 (int)lc->ldo_dir_stripe_offset,
5232 lc->ldo_dir_hash_type);
5234 /* transfer defaults LMV to new directory */
5235 lod_striping_from_default(lc, lds, child_mode);
5237 /* set count 0 to create normal directory */
5238 if (lc->ldo_dir_stripe_count == 1)
5239 lc->ldo_dir_stripe_count = 0;
5242 /* shrink the stripe_count to the avaible MDT count */
5243 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
5244 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)) {
5245 lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
5246 if (lc->ldo_dir_stripe_count == 1)
5247 lc->ldo_dir_stripe_count = 0;
5250 if (!(lc->ldo_dir_hash_type & LMV_HASH_TYPE_MASK))
5251 lc->ldo_dir_hash_type |=
5252 d->lod_mdt_descs.ltd_lmv_desc.ld_pattern;
5254 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
5255 lc->ldo_dir_stripe_count,
5256 (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
5261 /* child object regular file*/
5263 if (!lod_object_will_be_striped(S_ISREG(child_mode),
5264 lu_object_fid(&child->do_lu)))
5267 /* If object is going to be striped over OSTs, transfer default
5268 * striping information to the child, so that we can use it
5269 * during declaration and creation.
5271 * Try from the parent first.
5273 if (likely(lp != NULL)) {
5274 rc = lod_get_default_lov_striping(env, lp, lds, ah);
5276 lod_striping_from_default(lc, lds, child_mode);
5279 /* Initialize lod_device::lod_md_root object reference */
5280 if (d->lod_md_root == NULL) {
5281 struct dt_object *root;
5282 struct lod_object *lroot;
5284 lu_root_fid(&info->lti_fid);
5285 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
5286 if (!IS_ERR(root)) {
5287 lroot = lod_dt_obj(root);
5289 spin_lock(&d->lod_lock);
5290 if (d->lod_md_root != NULL)
5291 dt_object_put(env, &d->lod_md_root->ldo_obj);
5292 d->lod_md_root = lroot;
5293 spin_unlock(&d->lod_lock);
5297 /* try inherit layout from the root object (fs default) when:
5298 * - parent does not have default layout; or
5299 * - parent has plain(v1/v3) default layout, and some attributes
5300 * are not specified in the default layout;
5302 if (d->lod_md_root != NULL &&
5303 lod_need_inherit_more(lc, true, ah->dah_append_pool)) {
5304 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds,
5308 if (lc->ldo_comp_cnt == 0) {
5309 lod_striping_from_default(lc, lds, child_mode);
5310 } else if (!lds->lds_def_striping_is_composite) {
5311 struct lod_layout_component *def_comp;
5313 LASSERT(!lc->ldo_is_composite);
5314 lod_comp = &lc->ldo_comp_entries[0];
5315 def_comp = &lds->lds_def_comp_entries[0];
5317 if (lod_comp->llc_stripe_count <= 0)
5318 lod_comp->llc_stripe_count =
5319 def_comp->llc_stripe_count;
5320 if (lod_comp->llc_stripe_size <= 0)
5321 lod_comp->llc_stripe_size =
5322 def_comp->llc_stripe_size;
5323 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT &&
5324 (!lod_comp->llc_pool || !lod_comp->llc_pool[0]))
5325 lod_comp->llc_stripe_offset =
5326 def_comp->llc_stripe_offset;
5327 if (lod_comp->llc_pool == NULL)
5328 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
5333 * fs default striping may not be explicitly set, or historically set
5334 * in config log, use them.
5336 if (lod_need_inherit_more(lc, false, ah->dah_append_pool)) {
5337 if (lc->ldo_comp_cnt == 0) {
5338 rc = lod_alloc_comp_entries(lc, 0, 1);
5340 /* fail to allocate memory, will create a
5341 * non-striped file. */
5343 lc->ldo_is_composite = 0;
5344 lod_comp = &lc->ldo_comp_entries[0];
5345 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
5347 LASSERT(!lc->ldo_is_composite);
5348 lod_comp = &lc->ldo_comp_entries[0];
5349 desc = &d->lod_ost_descs.ltd_lov_desc;
5350 lod_adjust_stripe_info(lod_comp, desc, ah->dah_append_stripes);
5351 if (ah->dah_append_pool && ah->dah_append_pool[0])
5352 lod_obj_set_pool(lc, 0, ah->dah_append_pool);
5359 * Size initialization on late striping.
5361 * Propagate the size of a truncated object to a deferred striping.
5362 * This function handles a special case when truncate was done on a
5363 * non-striped object and now while the striping is being created
5364 * we can't lose that size, so we have to propagate it to the stripes
5367 * \param[in] env execution environment
5368 * \param[in] dt object
5369 * \param[in] th transaction handle
5371 * \retval 0 on success
5372 * \retval negative if failed
5374 static int lod_declare_init_size(const struct lu_env *env,
5375 struct dt_object *dt, struct thandle *th)
5377 struct dt_object *next = dt_object_child(dt);
5378 struct lod_object *lo = lod_dt_obj(dt);
5379 struct dt_object **objects = NULL;
5380 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
5381 uint64_t size, offs;
5382 int i, rc, stripe, stripe_count = 0, stripe_size = 0;
5383 struct lu_extent size_ext;
5386 if (!lod_obj_is_striped(dt))
5389 rc = dt_attr_get(env, next, attr);
5390 LASSERT(attr->la_valid & LA_SIZE);
5394 size = attr->la_size;
5398 size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
5399 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5400 struct lod_layout_component *lod_comp;
5401 struct lu_extent *extent;
5403 lod_comp = &lo->ldo_comp_entries[i];
5405 if (lod_comp->llc_stripe == NULL)
5408 extent = &lod_comp->llc_extent;
5409 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
5410 if (!lo->ldo_is_composite ||
5411 lu_extent_is_overlapped(extent, &size_ext)) {
5412 objects = lod_comp->llc_stripe;
5413 stripe_count = lod_comp->llc_stripe_count;
5414 stripe_size = lod_comp->llc_stripe_size;
5417 if (stripe_count == 0)
5420 LASSERT(objects != NULL && stripe_size != 0);
5421 do_div(size, stripe_size);
5422 stripe = do_div(size, stripe_count);
5423 LASSERT(objects[stripe] != NULL);
5425 size = size * stripe_size;
5426 offs = attr->la_size;
5427 size += do_div(offs, stripe_size);
5429 attr->la_valid = LA_SIZE;
5430 attr->la_size = size;
5432 rc = lod_sub_declare_attr_set(env, objects[stripe],
5441 * Declare creation of striped object.
5443 * The function declares creation stripes for a regular object. The function
5444 * also declares whether the stripes will be created with non-zero size if
5445 * previously size was set non-zero on the master object. If object \a dt is
5446 * not local, then only fully defined striping can be applied in \a lovea.
5447 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
5450 * \param[in] env execution environment
5451 * \param[in] dt object
5452 * \param[in] attr attributes the stripes will be created with
5453 * \param[in] lovea a buffer containing striping description
5454 * \param[in] th transaction handle
5456 * \retval 0 on success
5457 * \retval negative if failed
5459 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
5460 struct lu_attr *attr,
5461 const struct lu_buf *lovea, struct thandle *th)
5463 struct lod_thread_info *info = lod_env_info(env);
5464 struct dt_object *next = dt_object_child(dt);
5465 struct lod_object *lo = lod_dt_obj(dt);
5469 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
5470 GOTO(out, rc = -ENOMEM);
5472 if (!dt_object_remote(next)) {
5473 /* choose OST and generate appropriate objects */
5474 rc = lod_prepare_create(env, lo, attr, lovea, th);
5479 * declare storage for striping data
5481 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5483 /* LOD can not choose OST objects for remote objects, i.e.
5484 * stripes must be ready before that. Right now, it can only
5485 * happen during migrate, i.e. migrate process needs to create
5486 * remote regular file (mdd_migrate_create), then the migrate
5487 * process will provide stripeEA. */
5488 LASSERT(lovea != NULL);
5489 info->lti_buf = *lovea;
5492 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
5493 XATTR_NAME_LOV, 0, th);
5498 * if striping is created with local object's size > 0,
5499 * we have to propagate this size to specific object
5500 * the case is possible only when local object was created previously
5502 if (dt_object_exists(next))
5503 rc = lod_declare_init_size(env, dt, th);
5506 /* failed to create striping or to set initial size, let's reset
5507 * config so that others don't get confused */
5509 lod_striping_free(env, lo);
5515 * Whether subdirectories under \a dt should be created on MDTs by space QoS
5517 * If LMV_HASH_FLAG_SPACE is set on directory default layout, its subdirectories
5518 * should be created on MDT by space QoS.
5520 * \param[in] env execution environment
5521 * \param[in] dev lu device
5522 * \param[in] dt object
5524 * \retval 1 if directory should create subdir by space usage
5526 * \retval -ev if failed
5528 static inline int dt_object_qos_mkdir(const struct lu_env *env,
5529 struct lu_device *dev,
5530 struct dt_object *dt)
5532 struct lod_thread_info *info = lod_env_info(env);
5533 struct lu_object *obj;
5534 struct lod_object *lo;
5535 struct lmv_user_md *lmu;
5538 obj = lu_object_find_slice(env, dev, lu_object_fid(&dt->do_lu), NULL);
5540 return PTR_ERR(obj);
5542 lo = lu2lod_obj(obj);
5544 rc = lod_get_default_lmv_ea(env, lo);
5545 dt_object_put(env, dt);
5549 if (rc < (int)sizeof(*lmu))
5552 lmu = info->lti_ea_store;
5553 return le32_to_cpu(lmu->lum_stripe_offset) == LMV_OFFSET_DEFAULT;
5557 * Implementation of dt_object_operations::do_declare_create.
5559 * The method declares creation of a new object. If the object will be striped,
5560 * then helper functions are called to find FIDs for the stripes, declare
5561 * creation of the stripes and declare initialization of the striping
5562 * information to be stored in the master object.
5564 * \see dt_object_operations::do_declare_create() in the API description
5567 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
5568 struct lu_attr *attr,
5569 struct dt_allocation_hint *hint,
5570 struct dt_object_format *dof, struct thandle *th)
5572 struct dt_object *next = dt_object_child(dt);
5573 struct lod_object *lo = lod_dt_obj(dt);
5582 * first of all, we declare creation of local object
5584 rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
5589 * it's lod_ah_init() that has decided the object will be striped
5591 if (dof->dof_type == DFT_REGULAR) {
5592 /* callers don't want stripes */
5593 /* XXX: all tricky interactions with ->ah_make_hint() decided
5594 * to use striping, then ->declare_create() behaving differently
5595 * should be cleaned */
5596 if (dof->u.dof_reg.striped != 0)
5597 rc = lod_declare_striped_create(env, dt, attr,
5599 } else if (dof->dof_type == DFT_DIR) {
5600 struct seq_server_site *ss;
5601 struct lu_buf buf = { NULL };
5602 struct lu_buf *lmu = NULL;
5604 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
5606 /* If the parent has default stripeEA, and client
5607 * did not find it before sending create request,
5608 * then MDT will return -EREMOTE, and client will
5609 * retrieve the default stripeEA and re-create the
5612 * Note: if dah_eadata != NULL, it means creating the
5613 * striped directory with specified stripeEA, then it
5614 * should ignore the default stripeEA */
5615 if (hint != NULL && hint->dah_eadata == NULL) {
5616 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
5617 GOTO(out, rc = -EREMOTE);
5619 if (lo->ldo_dir_stripe_offset == LMV_OFFSET_DEFAULT) {
5620 struct lod_default_striping *lds;
5622 lds = lo->ldo_def_striping;
5624 * child and parent should be on the same MDT,
5625 * but if parent has default LMV, and the start
5626 * MDT offset is -1, it's allowed. This check
5627 * is not necessary after 2.12.22 because client
5628 * follows this already, but old client may not.
5630 if (hint->dah_parent &&
5631 dt_object_remote(hint->dah_parent) && lds &&
5632 lds->lds_dir_def_stripe_offset !=
5634 GOTO(out, rc = -EREMOTE);
5635 } else if (lo->ldo_dir_stripe_offset !=
5637 struct lod_device *lod;
5638 struct lu_tgt_desc *mdt = NULL;
5639 bool found_mdt = false;
5641 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5642 lod_foreach_mdt(lod, mdt) {
5643 if (mdt->ltd_index ==
5644 lo->ldo_dir_stripe_offset) {
5650 /* If the MDT indicated by stripe_offset can be
5651 * found, then tell client to resend the create
5652 * request to the correct MDT, otherwise return
5653 * error to client */
5655 GOTO(out, rc = -EREMOTE);
5657 GOTO(out, rc = -EINVAL);
5659 } else if (hint && hint->dah_eadata) {
5661 lmu->lb_buf = (void *)hint->dah_eadata;
5662 lmu->lb_len = hint->dah_eadata_len;
5665 rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
5669 /* failed to create striping or to set initial size, let's reset
5670 * config so that others don't get confused */
5672 lod_striping_free(env, lo);
5677 * Generate component ID for new created component.
5679 * \param[in] lo LOD object
5680 * \param[in] comp_idx index of ldo_comp_entries
5682 * \retval component ID on success
5683 * \retval LCME_ID_INVAL on failure
5685 static __u32 lod_gen_component_id(struct lod_object *lo,
5686 int mirror_id, int comp_idx)
5688 struct lod_layout_component *lod_comp;
5689 __u32 id, start, end;
5692 LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
5694 lod_obj_inc_layout_gen(lo);
5695 id = lo->ldo_layout_gen;
5696 if (likely(id <= SEQ_ID_MAX))
5697 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
5699 /* Layout generation wraps, need to check collisions. */
5700 start = id & SEQ_ID_MASK;
5703 for (id = start; id <= end; id++) {
5704 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5705 lod_comp = &lo->ldo_comp_entries[i];
5706 if (pflr_id(mirror_id, id) == lod_comp->llc_id)
5709 /* Found the ununsed ID */
5710 if (i == lo->ldo_comp_cnt)
5711 RETURN(pflr_id(mirror_id, id));
5713 if (end == LCME_ID_MAX) {
5715 end = min(lo->ldo_layout_gen & LCME_ID_MASK,
5716 (__u32)(LCME_ID_MAX - 1));
5720 RETURN(LCME_ID_INVAL);
5724 * Creation of a striped regular object.
5726 * The function is called to create the stripe objects for a regular
5727 * striped file. This can happen at the initial object creation or
5728 * when the caller asks LOD to do so using ->do_xattr_set() method
5729 * (so called late striping). Notice all the information are already
5730 * prepared in the form of the list of objects (ldo_stripe field).
5731 * This is done during declare phase.
5733 * \param[in] env execution environment
5734 * \param[in] dt object
5735 * \param[in] attr attributes the stripes will be created with
5736 * \param[in] dof format of stripes (see OSD API description)
5737 * \param[in] th transaction handle
5739 * \retval 0 on success
5740 * \retval negative if failed
5742 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
5743 struct lu_attr *attr, struct dt_object_format *dof,
5746 struct lod_layout_component *lod_comp;
5747 struct lod_object *lo = lod_dt_obj(dt);
5752 LASSERT((lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL) ||
5753 lo->ldo_is_foreign);
5755 mirror_id = 0; /* non-flr file's mirror_id is 0 */
5756 if (lo->ldo_mirror_count > 1) {
5757 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5758 lod_comp = &lo->ldo_comp_entries[i];
5759 if (lod_comp->llc_id != LCME_ID_INVAL &&
5760 mirror_id_of(lod_comp->llc_id) > mirror_id)
5761 mirror_id = mirror_id_of(lod_comp->llc_id);
5765 /* create all underlying objects */
5766 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5767 lod_comp = &lo->ldo_comp_entries[i];
5769 if (lod_comp->llc_id == LCME_ID_INVAL) {
5770 /* only the component of FLR layout with more than 1
5771 * mirror has mirror ID in its component ID.
5773 if (lod_comp->llc_extent.e_start == 0 &&
5774 lo->ldo_mirror_count > 1)
5777 lod_comp->llc_id = lod_gen_component_id(lo,
5779 if (lod_comp->llc_id == LCME_ID_INVAL)
5780 GOTO(out, rc = -ERANGE);
5783 if (lod_comp_inited(lod_comp))
5786 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
5787 lod_comp_set_init(lod_comp);
5789 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
5790 lod_comp_set_init(lod_comp);
5792 if (lod_comp->llc_stripe == NULL)
5795 LASSERT(lod_comp->llc_stripe_count);
5796 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
5797 struct dt_object *object = lod_comp->llc_stripe[j];
5798 LASSERT(object != NULL);
5799 rc = lod_sub_create(env, object, attr, NULL, dof, th);
5803 lod_comp_set_init(lod_comp);
5806 rc = lod_fill_mirrors(lo);
5810 rc = lod_generate_and_set_lovea(env, lo, th);
5814 lo->ldo_comp_cached = 1;
5818 lod_striping_free(env, lo);
5822 static inline bool lod_obj_is_dom(struct dt_object *dt)
5824 struct lod_object *lo = lod_dt_obj(dt);
5826 if (!dt_object_exists(dt_object_child(dt)))
5829 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
5832 if (!lo->ldo_comp_cnt)
5835 return (lov_pattern(lo->ldo_comp_entries[0].llc_pattern) ==
5840 * Implementation of dt_object_operations::do_create.
5842 * If any of preceeding methods (like ->do_declare_create(),
5843 * ->do_ah_init(), etc) chose to create a striped object,
5844 * then this method will create the master and the stripes.
5846 * \see dt_object_operations::do_create() in the API description for details.
5848 static int lod_create(const struct lu_env *env, struct dt_object *dt,
5849 struct lu_attr *attr, struct dt_allocation_hint *hint,
5850 struct dt_object_format *dof, struct thandle *th)
5855 /* create local object */
5856 rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
5860 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
5861 (lod_obj_is_striped(dt) || lod_obj_is_dom(dt)) &&
5862 dof->u.dof_reg.striped != 0) {
5863 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
5864 rc = lod_striped_create(env, dt, attr, dof, th);
5871 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
5872 struct dt_object *dt, struct thandle *th,
5873 int comp_idx, int stripe_idx,
5874 struct lod_obj_stripe_cb_data *data)
5876 if (data->locd_declare)
5877 return lod_sub_declare_destroy(env, dt, th);
5878 else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
5879 stripe_idx == cfs_fail_val)
5880 return lod_sub_destroy(env, dt, th);
5886 * Implementation of dt_object_operations::do_declare_destroy.
5888 * If the object is a striped directory, then the function declares reference
5889 * removal from the master object (this is an index) to the stripes and declares
5890 * destroy of all the stripes. In all the cases, it declares an intention to
5891 * destroy the object itself.
5893 * \see dt_object_operations::do_declare_destroy() in the API description
5896 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
5899 struct dt_object *next = dt_object_child(dt);
5900 struct lod_object *lo = lod_dt_obj(dt);
5901 struct lod_thread_info *info = lod_env_info(env);
5902 struct dt_object *stripe;
5903 char *stripe_name = info->lti_key;
5909 * load striping information, notice we don't do this when object
5910 * is being initialized as we don't need this information till
5911 * few specific cases like destroy, chown
5913 rc = lod_striping_load(env, lo);
5917 /* declare destroy for all underlying objects */
5918 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5919 rc = next->do_ops->do_index_try(env, next,
5920 &dt_directory_features);
5924 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5925 stripe = lo->ldo_stripe[i];
5929 rc = lod_sub_declare_ref_del(env, next, th);
5933 snprintf(stripe_name, sizeof(info->lti_key),
5935 PFID(lu_object_fid(&stripe->do_lu)), i);
5936 rc = lod_sub_declare_delete(env, next,
5937 (const struct dt_key *)stripe_name, th);
5944 * we declare destroy for the local object
5946 rc = lod_sub_declare_destroy(env, next, th);
5950 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
5951 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
5954 if (!lod_obj_is_striped(dt))
5957 /* declare destroy all striped objects */
5958 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5959 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5960 stripe = lo->ldo_stripe[i];
5964 if (!dt_object_exists(stripe))
5967 rc = lod_sub_declare_ref_del(env, stripe, th);
5971 rc = lod_sub_declare_destroy(env, stripe, th);
5976 struct lod_obj_stripe_cb_data data = { { 0 } };
5978 data.locd_declare = true;
5979 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
5980 rc = lod_obj_for_each_stripe(env, lo, th, &data);
5987 * Implementation of dt_object_operations::do_destroy.
5989 * If the object is a striped directory, then the function removes references
5990 * from the master object (this is an index) to the stripes and destroys all
5991 * the stripes. In all the cases, the function destroys the object itself.
5993 * \see dt_object_operations::do_destroy() in the API description for details.
5995 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
5998 struct dt_object *next = dt_object_child(dt);
5999 struct lod_object *lo = lod_dt_obj(dt);
6000 struct lod_thread_info *info = lod_env_info(env);
6001 char *stripe_name = info->lti_key;
6002 struct dt_object *stripe;
6008 /* destroy sub-stripe of master object */
6009 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6010 rc = next->do_ops->do_index_try(env, next,
6011 &dt_directory_features);
6015 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6016 stripe = lo->ldo_stripe[i];
6020 rc = lod_sub_ref_del(env, next, th);
6024 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
6025 PFID(lu_object_fid(&stripe->do_lu)), i);
6027 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
6028 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
6029 PFID(lu_object_fid(&stripe->do_lu)));
6031 rc = lod_sub_delete(env, next,
6032 (const struct dt_key *)stripe_name, th);
6038 rc = lod_sub_destroy(env, next, th);
6042 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
6043 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
6046 if (!lod_obj_is_striped(dt))
6049 /* destroy all striped objects */
6050 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6051 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6052 stripe = lo->ldo_stripe[i];
6056 if (!dt_object_exists(stripe))
6059 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
6060 i == cfs_fail_val) {
6061 dt_write_lock(env, stripe, DT_TGT_CHILD);
6062 rc = lod_sub_ref_del(env, stripe, th);
6063 dt_write_unlock(env, stripe);
6067 rc = lod_sub_destroy(env, stripe, th);
6073 struct lod_obj_stripe_cb_data data = { { 0 } };
6075 data.locd_declare = false;
6076 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6077 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6084 * Implementation of dt_object_operations::do_declare_ref_add.
6086 * \see dt_object_operations::do_declare_ref_add() in the API description
6089 static int lod_declare_ref_add(const struct lu_env *env,
6090 struct dt_object *dt, struct thandle *th)
6092 return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
6096 * Implementation of dt_object_operations::do_ref_add.
6098 * \see dt_object_operations::do_ref_add() in the API description for details.
6100 static int lod_ref_add(const struct lu_env *env,
6101 struct dt_object *dt, struct thandle *th)
6103 return lod_sub_ref_add(env, dt_object_child(dt), th);
6107 * Implementation of dt_object_operations::do_declare_ref_del.
6109 * \see dt_object_operations::do_declare_ref_del() in the API description
6112 static int lod_declare_ref_del(const struct lu_env *env,
6113 struct dt_object *dt, struct thandle *th)
6115 return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
6119 * Implementation of dt_object_operations::do_ref_del
6121 * \see dt_object_operations::do_ref_del() in the API description for details.
6123 static int lod_ref_del(const struct lu_env *env,
6124 struct dt_object *dt, struct thandle *th)
6126 return lod_sub_ref_del(env, dt_object_child(dt), th);
6130 * Implementation of dt_object_operations::do_object_sync.
6132 * \see dt_object_operations::do_object_sync() in the API description
6135 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
6136 __u64 start, __u64 end)
6138 return dt_object_sync(env, dt_object_child(dt), start, end);
6142 * Implementation of dt_object_operations::do_object_unlock.
6144 * Used to release LDLM lock(s).
6146 * \see dt_object_operations::do_object_unlock() in the API description
6149 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
6150 struct ldlm_enqueue_info *einfo,
6151 union ldlm_policy_data *policy)
6153 struct lod_object *lo = lod_dt_obj(dt);
6154 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
6155 int slave_locks_size;
6159 if (slave_locks == NULL)
6162 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
6163 /* Note: for remote lock for single stripe dir, MDT will cancel
6164 * the lock by lockh directly */
6165 LASSERT(!dt_object_remote(dt_object_child(dt)));
6167 /* locks were unlocked in MDT layer */
6168 for (i = 0; i < slave_locks->ha_count; i++)
6169 LASSERT(!lustre_handle_is_used(&slave_locks->ha_handles[i]));
6172 * NB, ha_count may not equal to ldo_dir_stripe_count, because dir
6173 * layout may change, e.g., shrink dir layout after migration.
6175 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6176 if (lo->ldo_stripe[i])
6177 dt_invalidate(env, lo->ldo_stripe[i]);
6180 slave_locks_size = offsetof(typeof(*slave_locks),
6181 ha_handles[slave_locks->ha_count]);
6182 OBD_FREE(slave_locks, slave_locks_size);
6183 einfo->ei_cbdata = NULL;
6189 * Implementation of dt_object_operations::do_object_lock.
6191 * Used to get LDLM lock on the non-striped and striped objects.
6193 * \see dt_object_operations::do_object_lock() in the API description
6196 static int lod_object_lock(const struct lu_env *env,
6197 struct dt_object *dt,
6198 struct lustre_handle *lh,
6199 struct ldlm_enqueue_info *einfo,
6200 union ldlm_policy_data *policy)
6202 struct lod_object *lo = lod_dt_obj(dt);
6203 int slave_locks_size;
6204 struct lustre_handle_array *slave_locks = NULL;
6209 /* remote object lock */
6210 if (!einfo->ei_enq_slave) {
6211 LASSERT(dt_object_remote(dt));
6212 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
6216 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
6219 rc = lod_striping_load(env, lo);
6224 if (lo->ldo_dir_stripe_count <= 1)
6227 slave_locks_size = offsetof(typeof(*slave_locks),
6228 ha_handles[lo->ldo_dir_stripe_count]);
6229 /* Freed in lod_object_unlock */
6230 OBD_ALLOC(slave_locks, slave_locks_size);
6233 slave_locks->ha_count = lo->ldo_dir_stripe_count;
6235 /* striped directory lock */
6236 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6237 struct lustre_handle lockh;
6238 struct ldlm_res_id *res_id;
6239 struct dt_object *stripe;
6241 stripe = lo->ldo_stripe[i];
6245 res_id = &lod_env_info(env)->lti_res_id;
6246 fid_build_reg_res_name(lu_object_fid(&stripe->do_lu), res_id);
6247 einfo->ei_res_id = res_id;
6249 if (dt_object_remote(stripe)) {
6250 set_bit(i, (void *)slave_locks->ha_map);
6251 rc = dt_object_lock(env, stripe, &lockh, einfo, policy);
6253 struct ldlm_namespace *ns = einfo->ei_namespace;
6254 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
6255 ldlm_completion_callback completion = einfo->ei_cb_cp;
6256 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
6258 if (einfo->ei_mode == LCK_PW ||
6259 einfo->ei_mode == LCK_EX)
6260 dlmflags |= LDLM_FL_COS_INCOMPAT;
6262 LASSERT(ns != NULL);
6263 rc = ldlm_cli_enqueue_local(env, ns, res_id, LDLM_IBITS,
6264 policy, einfo->ei_mode,
6265 &dlmflags, blocking,
6267 NULL, 0, LVB_T_NONE,
6272 ldlm_lock_decref_and_cancel(
6273 &slave_locks->ha_handles[i],
6275 OBD_FREE(slave_locks, slave_locks_size);
6278 slave_locks->ha_handles[i] = lockh;
6280 einfo->ei_cbdata = slave_locks;
6286 * Implementation of dt_object_operations::do_invalidate.
6288 * \see dt_object_operations::do_invalidate() in the API description for details
6290 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
6292 return dt_invalidate(env, dt_object_child(dt));
6295 static int lod_declare_instantiate_components(const struct lu_env *env,
6296 struct lod_object *lo,
6300 struct lod_thread_info *info = lod_env_info(env);
6305 LASSERT(info->lti_count < lo->ldo_comp_cnt);
6307 for (i = 0; i < info->lti_count; i++) {
6308 rc = lod_qos_prep_create(env, lo, NULL, th,
6309 info->lti_comp_idx[i], reserve);
6315 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6316 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
6317 &info->lti_buf, XATTR_NAME_LOV, 0, th);
6324 * Check OSTs for an existing component for further extension
6326 * Checks if OSTs are still healthy and not out of space. Gets free space
6327 * on OSTs (relative to allocation watermark rmb_low) and compares to
6328 * the proposed new_end for this component.
6330 * Decides whether or not to extend a component on its current OSTs.
6332 * \param[in] env execution environment for this thread
6333 * \param[in] lo object we're checking
6334 * \param[in] index index of this component
6335 * \param[in] extension_size extension size for this component
6336 * \param[in] extent layout extent for requested operation
6337 * \param[in] comp_extent extension component extent
6338 * \param[in] write if this is write operation
6340 * \retval true - OK to extend on current OSTs
6341 * \retval false - do not extend on current OSTs
6343 static bool lod_sel_osts_allowed(const struct lu_env *env,
6344 struct lod_object *lo,
6345 int index, __u64 reserve,
6346 struct lu_extent *extent,
6347 struct lu_extent *comp_extent, int write)
6349 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[index];
6350 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6351 struct lod_thread_info *tinfo = lod_env_info(env);
6352 struct obd_statfs *sfs = &tinfo->lti_osfs;
6353 __u64 available = 0;
6359 LASSERT(lod_comp->llc_stripe_count != 0);
6361 lod_getref(&lod->lod_ost_descs);
6362 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
6363 int index = lod_comp->llc_ost_indices[i];
6364 struct lod_tgt_desc *ost = OST_TGT(lod, index);
6365 struct obd_statfs_info info = { 0 };
6366 int j, repeated = 0;
6370 /* Get the number of times this OST repeats in this component.
6371 * Note: inter-component repeats are not counted as this is
6372 * considered as a rare case: we try to not repeat OST in other
6373 * components if possible. */
6374 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6375 if (index != lod_comp->llc_ost_indices[j])
6378 /* already handled */
6384 if (j < lod_comp->llc_stripe_count)
6387 if (!test_bit(index, lod->lod_ost_bitmap)) {
6388 CDEBUG(D_LAYOUT, "ost %d no longer present\n", index);
6393 rc = dt_statfs_info(env, ost->ltd_tgt, sfs, &info);
6395 CDEBUG(D_LAYOUT, "statfs failed for ost %d, error %d\n",
6401 if (sfs->os_state & OS_STATFS_ENOSPC ||
6402 sfs->os_state & OS_STATFS_READONLY ||
6403 sfs->os_state & OS_STATFS_DEGRADED) {
6404 CDEBUG(D_LAYOUT, "ost %d is not availble for SEL "
6405 "extension, state %u\n", index, sfs->os_state);
6411 available = sfs->os_bavail * sfs->os_bsize;
6412 /* 'available' is relative to the allocation threshold */
6413 available -= (__u64) info.os_reserved_mb_low << 20;
6415 CDEBUG(D_LAYOUT, "ost %d lowwm: %d highwm: %d, "
6416 "%llu %% blocks available, %llu %% blocks free\n",
6417 index, info.os_reserved_mb_low, info.os_reserved_mb_high,
6418 (100ull * sfs->os_bavail) / sfs->os_blocks,
6419 (100ull * sfs->os_bfree) / sfs->os_blocks);
6421 if (reserve * repeated > available) {
6423 CDEBUG(D_LAYOUT, "low space on ost %d, available %llu "
6424 "< extension size %llu repeated %d\n", index,
6425 available, reserve, repeated);
6429 lod_putref(lod, &lod->lod_ost_descs);
6435 * Adjust extents after component removal
6437 * When we remove an extension component, we move the start of the next
6438 * component to match the start of the extension component, so no space is left
6441 * \param[in] env execution environment for this thread
6442 * \param[in] lo object
6443 * \param[in] max_comp layout component
6444 * \param[in] index index of this component
6446 * \retval 0 on success
6447 * \retval negative errno on error
6449 static void lod_sel_adjust_extents(const struct lu_env *env,
6450 struct lod_object *lo,
6451 int max_comp, int index)
6453 struct lod_layout_component *lod_comp = NULL;
6454 struct lod_layout_component *next = NULL;
6455 struct lod_layout_component *prev = NULL;
6456 __u64 new_start = 0;
6460 /* Extension space component */
6461 lod_comp = &lo->ldo_comp_entries[index];
6462 next = &lo->ldo_comp_entries[index + 1];
6463 prev = &lo->ldo_comp_entries[index - 1];
6465 LASSERT(lod_comp != NULL && prev != NULL && next != NULL);
6466 LASSERT(lod_comp->llc_flags & LCME_FL_EXTENSION);
6468 /* Previous is being removed */
6469 if (prev && prev->llc_id == LCME_ID_INVAL)
6470 new_start = prev->llc_extent.e_start;
6472 new_start = lod_comp->llc_extent.e_start;
6474 for (i = index + 1; i < max_comp; i++) {
6475 lod_comp = &lo->ldo_comp_entries[i];
6477 start = lod_comp->llc_extent.e_start;
6478 lod_comp->llc_extent.e_start = new_start;
6480 /* We only move zero length extendable components */
6481 if (!(start == lod_comp->llc_extent.e_end))
6484 LASSERT(!(lod_comp->llc_flags & LCME_FL_INIT));
6486 lod_comp->llc_extent.e_end = new_start;
6490 /* Calculate the proposed 'new end' for a component we're extending */
6491 static __u64 lod_extension_new_end(__u64 extension_size, __u64 extent_end,
6492 __u32 stripe_size, __u64 component_end,
6493 __u64 extension_end)
6497 LASSERT(extension_size != 0 && stripe_size != 0);
6499 /* Round up to extension size */
6500 if (extent_end == OBD_OBJECT_EOF) {
6501 new_end = OBD_OBJECT_EOF;
6503 /* Add at least extension_size to the previous component_end,
6504 * covering the req layout extent */
6505 new_end = max(extent_end - component_end, extension_size);
6506 new_end = roundup(new_end, extension_size);
6507 new_end += component_end;
6509 /* Component end must be min stripe size aligned */
6510 if (new_end % stripe_size) {
6511 CDEBUG(D_LAYOUT, "new component end is not aligned "
6512 "by the stripe size %u: [%llu, %llu) ext size "
6513 "%llu new end %llu, aligning\n",
6514 stripe_size, component_end, extent_end,
6515 extension_size, new_end);
6516 new_end = roundup(new_end, stripe_size);
6520 if (new_end < extent_end)
6521 new_end = OBD_OBJECT_EOF;
6524 /* Don't extend past the end of the extension component */
6525 if (new_end > extension_end)
6526 new_end = extension_end;
6532 * Calculate the exact reservation (per-OST extension_size) on the OSTs being
6533 * instantiated. It needs to be calculated in advance and taken into account at
6534 * the instantiation time, because otherwise lod_statfs_and_check() may consider
6535 * an OST as OK, but SEL needs its extension_size to fit the free space and the
6536 * OST may turn out to be low-on-space, thus inappropriate OST may be used and
6539 * \param[in] lod_comp lod component we are checking
6541 * \retval size to reserved on each OST of lod_comp's stripe.
6543 static __u64 lod_sel_stripe_reserved(struct lod_layout_component *lod_comp)
6545 /* extension_size is file level, so we must divide by stripe count to
6546 * compare it to available space on a single OST */
6547 return lod_comp->llc_stripe_size * SEL_UNIT_SIZE /
6548 lod_comp->llc_stripe_count;
6551 /* As lod_sel_handler() could be re-entered for the same component several
6552 * times, this is the data for the next call. Fields could be changed to
6553 * component indexes when needed, (e.g. if there is no need to instantiate
6554 * all the previous components up to the current position) to tell the caller
6555 * where to start over from. */
6562 * Process extent updates for a particular layout component
6564 * Handle layout updates for a particular extension space component touched by
6565 * a layout update operation. Core function of self-extending PFL feature.
6567 * In general, this function processes exactly *one* stage of an extension
6568 * operation, modifying the layout accordingly, then returns to the caller.
6569 * The caller is responsible for restarting processing with the new layout,
6570 * which may repeatedly return to this function until the extension updates
6573 * This function does one of a few things to the layout:
6574 * 1. Extends the component before the current extension space component to
6575 * allow it to accomodate the requested operation (if space/policy permit that
6576 * component to continue on its current OSTs)
6578 * 2. If extension of the existing component fails, we do one of two things:
6579 * a. If there is a component after the extension space, we remove the
6580 * extension space component, move the start of the next component down
6581 * accordingly, then notify the caller to restart processing w/the new
6583 * b. If there is no following component, we try repeating the current
6584 * component, creating a new component using the current one as a
6585 * template (keeping its stripe properties but not specific striping),
6586 * and try assigning striping for this component. If there is sufficient
6587 * free space on the OSTs chosen for this component, it is instantiated
6588 * and i/o continues there.
6590 * If there is not sufficient space on the new OSTs, we remove this new
6591 * component & extend the current component.
6593 * Note further that uninited components followed by extension space can be zero
6594 * length meaning that we will try to extend them before initializing them, and
6595 * if that fails, they will be removed without initialization.
6597 * 3. If we extend to/beyond the end of an extension space component, that
6598 * component is exhausted (all of its range has been given to real components),
6599 * so we remove it and restart processing.
6601 * \param[in] env execution environment for this thread
6602 * \param[in,out] lo object to update the layout of
6603 * \param[in] extent layout extent for requested operation, update
6604 * layout to fit this operation
6605 * \param[in] th transaction handle for this operation
6606 * \param[in,out] max_comp the highest comp for the portion of the layout
6607 * we are operating on (For FLR, the chosen
6608 * replica). Updated because we may remove
6610 * \param[in] index index of the extension space component we're
6612 * \param[in] write if this is write op
6613 * \param[in,out] force if the extension is to be forced; set here
6614 to force it on the 2nd call for the same
6617 * \retval 0 on success
6618 * \retval negative errno on error
6620 static int lod_sel_handler(const struct lu_env *env,
6621 struct lod_object *lo,
6622 struct lu_extent *extent,
6623 struct thandle *th, int *max_comp,
6624 int index, int write,
6625 struct sel_data *sd)
6627 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6628 struct lod_thread_info *info = lod_env_info(env);
6629 struct lod_layout_component *lod_comp;
6630 struct lod_layout_component *prev;
6631 struct lod_layout_component *next = NULL;
6632 __u64 extension_size, reserve;
6639 /* First component cannot be extension space */
6641 CERROR("%s: "DFID" first component cannot be extension space\n",
6642 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6646 lod_comp = &lo->ldo_comp_entries[index];
6647 prev = &lo->ldo_comp_entries[index - 1];
6648 if ((index + 1) < *max_comp)
6649 next = &lo->ldo_comp_entries[index + 1];
6651 /* extension size uses the stripe size field as KiB */
6652 extension_size = lod_comp->llc_stripe_size * SEL_UNIT_SIZE;
6654 CDEBUG(D_LAYOUT, "prev start %llu, extension start %llu, extension end"
6655 " %llu, extension size %llu\n", prev->llc_extent.e_start,
6656 lod_comp->llc_extent.e_start, lod_comp->llc_extent.e_end,
6659 /* Two extension space components cannot be adjacent & extension space
6660 * components cannot be init */
6661 if ((prev->llc_flags & LCME_FL_EXTENSION) ||
6662 !(ergo(next, !(next->llc_flags & LCME_FL_EXTENSION))) ||
6663 lod_comp_inited(lod_comp)) {
6664 CERROR("%s: "DFID" invalid extension space components\n",
6665 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6669 reserve = lod_sel_stripe_reserved(lod_comp);
6671 if (!prev->llc_stripe) {
6672 CDEBUG(D_LAYOUT, "Previous component not inited\n");
6673 info->lti_count = 1;
6674 info->lti_comp_idx[0] = index - 1;
6675 rc = lod_declare_instantiate_components(env, lo, th, reserve);
6676 /* ENOSPC tells us we can't use this component. If there is
6677 * a next or we are repeating, we either spill over (next) or
6678 * extend the original comp (repeat). Otherwise, return the
6679 * error to the user. */
6680 if (rc == -ENOSPC && (next || sd->sd_repeat))
6686 if (sd->sd_force == 0 && rc == 0)
6687 rc = !lod_sel_osts_allowed(env, lo, index - 1, reserve, extent,
6688 &lod_comp->llc_extent, write);
6690 repeated = !!(sd->sd_repeat);
6694 /* Extend previous component */
6696 new_end = lod_extension_new_end(extension_size, extent->e_end,
6697 prev->llc_stripe_size,
6698 prev->llc_extent.e_end,
6699 lod_comp->llc_extent.e_end);
6701 CDEBUG(D_LAYOUT, "new end %llu\n", new_end);
6702 lod_comp->llc_extent.e_start = new_end;
6703 prev->llc_extent.e_end = new_end;
6705 if (prev->llc_extent.e_end == lod_comp->llc_extent.e_end) {
6706 CDEBUG(D_LAYOUT, "Extension component exhausted\n");
6707 lod_comp->llc_id = LCME_ID_INVAL;
6711 /* rc == 1, failed to extend current component */
6714 /* Normal 'spillover' case - Remove the extension
6715 * space component & bring down the start of the next
6717 lod_comp->llc_id = LCME_ID_INVAL;
6719 if (!(prev->llc_flags & LCME_FL_INIT)) {
6720 prev->llc_id = LCME_ID_INVAL;
6723 lod_sel_adjust_extents(env, lo, *max_comp, index);
6724 } else if (lod_comp_inited(prev)) {
6725 /* If there is no next, and the previous component is
6726 * INIT'ed, try repeating the previous component. */
6727 LASSERT(repeated == 0);
6728 rc = lod_layout_repeat_comp(env, lo, index - 1);
6732 /* The previous component is a repeated component.
6733 * Record this so we don't keep trying to repeat it. */
6736 /* If the previous component is not INIT'ed, this may
6737 * be a component we have just instantiated but failed
6738 * to extend. Or even a repeated component we failed
6739 * to prepare a striping for. Do not repeat but instead
6740 * remove the repeated component & force the extention
6741 * of the original one */
6744 prev->llc_id = LCME_ID_INVAL;
6751 rc = lod_layout_del_prep_layout(env, lo, NULL);
6754 LASSERTF(-rc == change,
6755 "number deleted %d != requested %d\n", -rc,
6758 *max_comp = *max_comp + change;
6760 /* lod_del_prep_layout reallocates ldo_comp_entries, so we must
6761 * refresh these pointers before using them */
6762 lod_comp = &lo->ldo_comp_entries[index];
6763 prev = &lo->ldo_comp_entries[index - 1];
6764 CDEBUG(D_LAYOUT, "After extent updates: prev start %llu, current start "
6765 "%llu, current end %llu max_comp %d ldo_comp_cnt %d\n",
6766 prev->llc_extent.e_start, lod_comp->llc_extent.e_start,
6767 lod_comp->llc_extent.e_end, *max_comp, lo->ldo_comp_cnt);
6769 /* Layout changed successfully */
6774 * Declare layout extent updates
6776 * Handles extensions. Identifies extension components touched by current
6777 * operation and passes them to processing function.
6779 * Restarts with updated layouts from the processing function until the current
6780 * operation no longer touches an extension space component.
6782 * \param[in] env execution environment for this thread
6783 * \param[in,out] lo object to update the layout of
6784 * \param[in] extent layout extent for requested operation, update layout to
6785 * fit this operation
6786 * \param[in] th transaction handle for this operation
6787 * \param[in] pick identifies chosen mirror for FLR layouts
6788 * \param[in] write if this is write op
6790 * \retval 1 on layout changed, 0 on no change
6791 * \retval negative errno on error
6793 static int lod_declare_update_extents(const struct lu_env *env,
6794 struct lod_object *lo, struct lu_extent *extent,
6795 struct thandle *th, int pick, int write)
6797 struct lod_thread_info *info = lod_env_info(env);
6798 struct lod_layout_component *lod_comp;
6799 bool layout_changed = false;
6800 struct sel_data sd = { 0 };
6808 /* This makes us work on the components of the chosen mirror */
6809 start_index = lo->ldo_mirrors[pick].lme_start;
6810 max_comp = lo->ldo_mirrors[pick].lme_end + 1;
6811 if (lo->ldo_flr_state == LCM_FL_NONE)
6812 LASSERT(start_index == 0 && max_comp == lo->ldo_comp_cnt);
6814 CDEBUG(D_LAYOUT, "extent->e_start %llu, extent->e_end %llu\n",
6815 extent->e_start, extent->e_end);
6816 for (i = start_index; i < max_comp; i++) {
6817 lod_comp = &lo->ldo_comp_entries[i];
6819 /* We've passed all components of interest */
6820 if (lod_comp->llc_extent.e_start >= extent->e_end)
6823 if (lod_comp->llc_flags & LCME_FL_EXTENSION) {
6824 layout_changed = true;
6825 rc = lod_sel_handler(env, lo, extent, th, &max_comp,
6830 /* Nothing has changed behind the prev one */
6836 /* We may have added or removed components. If so, we must update the
6837 * start & ends of all the mirrors after the current one, and the end
6838 * of the current mirror. */
6839 change = max_comp - 1 - lo->ldo_mirrors[pick].lme_end;
6841 lo->ldo_mirrors[pick].lme_end += change;
6842 for (i = pick + 1; i < lo->ldo_mirror_count; i++) {
6843 lo->ldo_mirrors[i].lme_start += change;
6844 lo->ldo_mirrors[i].lme_end += change;
6850 /* The amount of components has changed, adjust the lti_comp_idx */
6851 rc2 = lod_layout_data_init(info, lo->ldo_comp_cnt);
6853 return rc < 0 ? rc : rc2 < 0 ? rc2 : layout_changed;
6856 /* If striping is already instantiated or INIT'ed DOM? */
6857 static bool lod_is_instantiation_needed(struct lod_layout_component *comp)
6859 return !(((lov_pattern(comp->llc_pattern) == LOV_PATTERN_MDT) &&
6860 lod_comp_inited(comp)) || comp->llc_stripe);
6864 * Declare layout update for a non-FLR layout.
6866 * \param[in] env execution environment for this thread
6867 * \param[in,out] lo object to update the layout of
6868 * \param[in] layout layout intent for requested operation, "update" is
6869 * a process of reacting to this
6870 * \param[in] buf buffer containing lov ea (see comment on usage inline)
6871 * \param[in] th transaction handle for this operation
6873 * \retval 0 on success
6874 * \retval negative errno on error
6876 static int lod_declare_update_plain(const struct lu_env *env,
6877 struct lod_object *lo, struct layout_intent *layout,
6878 const struct lu_buf *buf, struct thandle *th)
6880 struct lod_thread_info *info = lod_env_info(env);
6881 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6882 struct lod_layout_component *lod_comp;
6883 struct lov_comp_md_v1 *comp_v1 = NULL;
6884 bool layout_changed = false;
6885 bool replay = false;
6889 LASSERT(lo->ldo_flr_state == LCM_FL_NONE);
6892 * In case the client is passing lovea, which only happens during
6893 * the replay of layout intent write RPC for now, we may need to
6894 * parse the lovea and apply new layout configuration.
6896 if (buf && buf->lb_len) {
6897 struct lov_user_md_v1 *v1 = buf->lb_buf;
6899 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
6900 v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
6901 LOV_MAGIC_COMP_V1)) {
6902 CERROR("%s: the replay buffer of layout extend "
6903 "(magic %#x) does not contain expected "
6904 "composite layout.\n",
6905 lod2obd(d)->obd_name, v1->lmm_magic);
6906 GOTO(out, rc = -EINVAL);
6909 rc = lod_use_defined_striping(env, lo, buf);
6912 lo->ldo_comp_cached = 1;
6914 rc = lod_get_lov_ea(env, lo);
6917 /* old on-disk EA is stored in info->lti_buf */
6918 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
6920 layout_changed = true;
6922 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
6926 /* non replay path */
6927 rc = lod_striping_load(env, lo);
6932 /* Make sure defined layout covers the requested write range. */
6933 lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
6934 if (lo->ldo_comp_cnt > 1 &&
6935 lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
6936 lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
6937 CDEBUG_LIMIT(replay ? D_ERROR : D_LAYOUT,
6938 "%s: the defined layout [0, %#llx) does not "
6939 "covers the write range "DEXT"\n",
6940 lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
6941 PEXT(&layout->li_extent));
6942 GOTO(out, rc = -EINVAL);
6945 CDEBUG(D_LAYOUT, "%s: "DFID": update components "DEXT"\n",
6946 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
6947 PEXT(&layout->li_extent));
6950 rc = lod_declare_update_extents(env, lo, &layout->li_extent,
6951 th, 0, layout->li_opc == LAYOUT_INTENT_WRITE);
6955 layout_changed = true;
6959 * Iterate ld->ldo_comp_entries, find the component whose extent under
6960 * the write range and not instantianted.
6962 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6963 lod_comp = &lo->ldo_comp_entries[i];
6965 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
6969 /* If striping is instantiated or INIT'ed DOM skip */
6970 if (!lod_is_instantiation_needed(lod_comp))
6974 * In replay path, lod_comp is the EA passed by
6975 * client replay buffer, comp_v1 is the pre-recovery
6976 * on-disk EA, we'd sift out those components which
6977 * were init-ed in the on-disk EA.
6979 if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
6984 * this component hasn't instantiated in normal path, or during
6985 * replay it needs replay the instantiation.
6988 /* A released component is being extended */
6989 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
6990 GOTO(out, rc = -EINVAL);
6992 LASSERT(info->lti_comp_idx != NULL);
6993 info->lti_comp_idx[info->lti_count++] = i;
6994 layout_changed = true;
6997 if (!layout_changed)
7000 lod_obj_inc_layout_gen(lo);
7001 rc = lod_declare_instantiate_components(env, lo, th, 0);
7005 lod_striping_free(env, lo);
7009 static inline int lod_comp_index(struct lod_object *lo,
7010 struct lod_layout_component *lod_comp)
7012 LASSERT(lod_comp >= lo->ldo_comp_entries &&
7013 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
7015 return lod_comp - lo->ldo_comp_entries;
7019 * Stale other mirrors by writing extent.
7021 static int lod_stale_components(const struct lu_env *env, struct lod_object *lo,
7022 int primary, struct lu_extent *extent,
7025 struct lod_layout_component *pri_comp, *lod_comp;
7026 struct lu_extent pri_extent;
7031 /* The writing extent decides which components in the primary
7032 * are affected... */
7033 CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
7036 lod_foreach_mirror_comp(pri_comp, lo, primary) {
7037 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
7040 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
7041 lod_comp_index(lo, pri_comp),
7042 PEXT(&pri_comp->llc_extent));
7044 pri_extent.e_start = pri_comp->llc_extent.e_start;
7045 pri_extent.e_end = pri_comp->llc_extent.e_end;
7047 for (i = 0; i < lo->ldo_mirror_count; i++) {
7050 rc = lod_declare_update_extents(env, lo, &pri_extent,
7052 /* if update_extents changed the layout, it may have
7053 * reallocated the component array, so start over to
7054 * avoid using stale pointers */
7060 /* ... and then stale other components that are
7061 * overlapping with primary components */
7062 lod_foreach_mirror_comp(lod_comp, lo, i) {
7063 if (!lu_extent_is_overlapped(
7065 &lod_comp->llc_extent))
7068 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
7069 i, lod_comp_index(lo, lod_comp));
7071 lod_comp->llc_flags |= LCME_FL_STALE;
7072 lo->ldo_mirrors[i].lme_stale = 1;
7081 * check an OST's availability
7082 * \param[in] env execution environment
7083 * \param[in] lo lod object
7084 * \param[in] dt dt object
7085 * \param[in] index mirror index
7087 * \retval negative if failed
7088 * \retval 1 if \a dt is available
7089 * \retval 0 if \a dt is not available
7091 static inline int lod_check_ost_avail(const struct lu_env *env,
7092 struct lod_object *lo,
7093 struct dt_object *dt, int index)
7095 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7096 struct lod_tgt_desc *ost;
7098 int type = LU_SEQ_RANGE_OST;
7101 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &idx, &type);
7103 CERROR("%s: can't locate "DFID":rc = %d\n",
7104 lod2obd(lod)->obd_name, PFID(lu_object_fid(&dt->do_lu)),
7109 ost = OST_TGT(lod, idx);
7110 if (ost->ltd_statfs.os_state &
7111 (OS_STATFS_READONLY | OS_STATFS_ENOSPC | OS_STATFS_ENOINO |
7112 OS_STATFS_NOPRECREATE) ||
7113 ost->ltd_active == 0) {
7114 CDEBUG(D_LAYOUT, DFID ": mirror %d OST%d unavail, rc = %d\n",
7115 PFID(lod_object_fid(lo)), index, idx, rc);
7123 * Pick primary mirror for write
7124 * \param[in] env execution environment
7125 * \param[in] lo object
7126 * \param[in] extent write range
7128 static int lod_primary_pick(const struct lu_env *env, struct lod_object *lo,
7129 struct lu_extent *extent)
7131 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7132 unsigned int seq = 0;
7133 struct lod_layout_component *lod_comp;
7135 int picked = -1, second_pick = -1, third_pick = -1;
7138 if (OBD_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
7139 get_random_bytes(&seq, sizeof(seq));
7140 seq %= lo->ldo_mirror_count;
7144 * Pick a mirror as the primary, and check the availability of OSTs.
7146 * This algo can be revised later after knowing the topology of
7149 lod_qos_statfs_update(env, lod, &lod->lod_ost_descs);
7150 for (i = 0; i < lo->ldo_mirror_count; i++) {
7151 bool ost_avail = true;
7152 int index = (i + seq) % lo->ldo_mirror_count;
7154 if (lo->ldo_mirrors[index].lme_stale) {
7155 CDEBUG(D_LAYOUT, DFID": mirror %d stale\n",
7156 PFID(lod_object_fid(lo)), index);
7160 /* 2nd pick is for the primary mirror containing unavail OST */
7161 if (lo->ldo_mirrors[index].lme_primary && second_pick < 0)
7162 second_pick = index;
7164 /* 3rd pick is for non-primary mirror containing unavail OST */
7165 if (second_pick < 0 && third_pick < 0)
7169 * we found a non-primary 1st pick, we'd like to find a
7170 * potential pirmary mirror.
7172 if (picked >= 0 && !lo->ldo_mirrors[index].lme_primary)
7175 /* check the availability of OSTs */
7176 lod_foreach_mirror_comp(lod_comp, lo, index) {
7177 if (!lod_comp_inited(lod_comp) || !lod_comp->llc_stripe)
7180 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
7181 struct dt_object *dt = lod_comp->llc_stripe[j];
7183 rc = lod_check_ost_avail(env, lo, dt, index);
7190 } /* for all dt object in one component */
7193 } /* for all components in a mirror */
7196 * the OSTs where allocated objects locates in the components
7197 * of the mirror are available.
7202 /* this mirror has all OSTs available */
7206 * primary with all OSTs are available, this is the perfect
7209 if (lo->ldo_mirrors[index].lme_primary)
7211 } /* for all mirrors */
7213 /* failed to pick a sound mirror, lower our expectation */
7215 picked = second_pick;
7217 picked = third_pick;
7224 static int lod_prepare_resync_mirror(const struct lu_env *env,
7225 struct lod_object *lo,
7228 struct lod_thread_info *info = lod_env_info(env);
7229 struct lod_layout_component *lod_comp;
7230 bool neg = !!(MIRROR_ID_NEG & mirror_id);
7233 mirror_id &= ~MIRROR_ID_NEG;
7235 for (i = 0; i < lo->ldo_mirror_count; i++) {
7236 if ((!neg && lo->ldo_mirrors[i].lme_id != mirror_id) ||
7237 (neg && lo->ldo_mirrors[i].lme_id == mirror_id))
7240 lod_foreach_mirror_comp(lod_comp, lo, i) {
7241 if (lod_comp_inited(lod_comp))
7244 info->lti_comp_idx[info->lti_count++] =
7245 lod_comp_index(lo, lod_comp);
7253 * figure out the components should be instantiated for resync.
7255 static int lod_prepare_resync(const struct lu_env *env, struct lod_object *lo,
7256 struct lu_extent *extent)
7258 struct lod_thread_info *info = lod_env_info(env);
7259 struct lod_layout_component *lod_comp;
7260 unsigned int need_sync = 0;
7264 DFID": instantiate all stale components in "DEXT"\n",
7265 PFID(lod_object_fid(lo)), PEXT(extent));
7268 * instantiate all components within this extent, even non-stale
7271 for (i = 0; i < lo->ldo_mirror_count; i++) {
7272 if (!lo->ldo_mirrors[i].lme_stale)
7275 lod_foreach_mirror_comp(lod_comp, lo, i) {
7276 if (!lu_extent_is_overlapped(extent,
7277 &lod_comp->llc_extent))
7282 if (lod_comp_inited(lod_comp))
7285 CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
7286 i, lod_comp_index(lo, lod_comp));
7287 info->lti_comp_idx[info->lti_count++] =
7288 lod_comp_index(lo, lod_comp);
7292 return need_sync ? 0 : -EALREADY;
7295 static int lod_declare_update_rdonly(const struct lu_env *env,
7296 struct lod_object *lo, struct md_layout_change *mlc,
7299 struct lod_thread_info *info = lod_env_info(env);
7300 struct lu_attr *layout_attr = &info->lti_layout_attr;
7301 struct lod_layout_component *lod_comp;
7302 struct lu_extent extent = { 0 };
7306 LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
7307 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7308 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7309 LASSERT(lo->ldo_mirror_count > 0);
7311 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7312 struct layout_intent *layout = mlc->mlc_intent;
7313 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7316 extent = layout->li_extent;
7317 CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
7318 PFID(lod_object_fid(lo)), PEXT(&extent));
7320 picked = lod_primary_pick(env, lo, &extent);
7324 CDEBUG(D_LAYOUT, DFID": picked mirror id %u as primary\n",
7325 PFID(lod_object_fid(lo)),
7326 lo->ldo_mirrors[picked].lme_id);
7328 /* Update extents of primary before staling */
7329 rc = lod_declare_update_extents(env, lo, &extent, th, picked,
7334 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
7336 * trunc transfers [0, size) in the intent extent, we'd
7337 * stale components overlapping [size, eof).
7339 extent.e_start = extent.e_end;
7340 extent.e_end = OBD_OBJECT_EOF;
7343 /* stale overlapping components from other mirrors */
7344 rc = lod_stale_components(env, lo, picked, &extent, th);
7348 /* restore truncate intent extent */
7349 if (layout->li_opc == LAYOUT_INTENT_TRUNC)
7350 extent.e_end = extent.e_start;
7352 /* instantiate components for the picked mirror, start from 0 */
7355 lod_foreach_mirror_comp(lod_comp, lo, picked) {
7356 if (!lu_extent_is_overlapped(&extent,
7357 &lod_comp->llc_extent))
7360 if (!lod_is_instantiation_needed(lod_comp))
7363 info->lti_comp_idx[info->lti_count++] =
7364 lod_comp_index(lo, lod_comp);
7367 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7368 } else { /* MD_LAYOUT_RESYNC */
7372 * could contain multiple non-stale mirrors, so we need to
7373 * prep uninited all components assuming any non-stale mirror
7374 * could be picked as the primary mirror.
7376 if (mlc->mlc_mirror_id == 0) {
7378 for (i = 0; i < lo->ldo_mirror_count; i++) {
7379 if (lo->ldo_mirrors[i].lme_stale)
7382 lod_foreach_mirror_comp(lod_comp, lo, i) {
7383 if (!lod_comp_inited(lod_comp))
7387 lod_comp->llc_extent.e_end)
7389 lod_comp->llc_extent.e_end;
7392 rc = lod_prepare_resync(env, lo, &extent);
7396 /* mirror write, try to init its all components */
7397 rc = lod_prepare_resync_mirror(env, lo,
7398 mlc->mlc_mirror_id);
7403 /* change the file state to SYNC_PENDING */
7404 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7407 /* Reset the layout version once it's becoming too large.
7408 * This way it can make sure that the layout version is
7409 * monotonously increased in this writing era. */
7410 lod_obj_inc_layout_gen(lo);
7411 if (lo->ldo_layout_gen > (LCME_ID_MAX >> 1)) {
7412 __u32 layout_version;
7414 get_random_bytes(&layout_version, sizeof(layout_version));
7415 lo->ldo_layout_gen = layout_version & 0xffff;
7418 rc = lod_declare_instantiate_components(env, lo, th, 0);
7422 layout_attr->la_valid = LA_LAYOUT_VERSION;
7423 layout_attr->la_layout_version = 0; /* set current version */
7424 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7425 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7426 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7432 lod_striping_free(env, lo);
7436 static int lod_declare_update_write_pending(const struct lu_env *env,
7437 struct lod_object *lo, struct md_layout_change *mlc,
7440 struct lod_thread_info *info = lod_env_info(env);
7441 struct lu_attr *layout_attr = &info->lti_layout_attr;
7442 struct lod_layout_component *lod_comp;
7443 struct lu_extent extent = { 0 };
7449 LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
7450 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7451 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7453 /* look for the primary mirror */
7454 for (i = 0; i < lo->ldo_mirror_count; i++) {
7455 if (lo->ldo_mirrors[i].lme_stale)
7458 LASSERTF(primary < 0, DFID " has multiple primary: %u / %u\n",
7459 PFID(lod_object_fid(lo)),
7460 lo->ldo_mirrors[i].lme_id,
7461 lo->ldo_mirrors[primary].lme_id);
7466 CERROR(DFID ": doesn't have a primary mirror\n",
7467 PFID(lod_object_fid(lo)));
7468 GOTO(out, rc = -ENODATA);
7471 CDEBUG(D_LAYOUT, DFID": found primary %u\n",
7472 PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
7474 LASSERT(!lo->ldo_mirrors[primary].lme_stale);
7476 /* for LAYOUT_WRITE opc, it has to do the following operations:
7477 * 1. stale overlapping componets from stale mirrors;
7478 * 2. instantiate components of the primary mirror;
7479 * 3. transfter layout version to all objects of the primary;
7481 * for LAYOUT_RESYNC opc, it will do:
7482 * 1. instantiate components of all stale mirrors;
7483 * 2. transfer layout version to all objects to close write era. */
7485 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7486 struct layout_intent *layout = mlc->mlc_intent;
7487 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7489 LASSERT(mlc->mlc_intent != NULL);
7491 extent = mlc->mlc_intent->li_extent;
7493 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
7494 PFID(lod_object_fid(lo)), PEXT(&extent));
7496 /* 1. Update extents of primary before staling */
7497 rc = lod_declare_update_extents(env, lo, &extent, th, primary,
7502 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
7504 * trunc transfers [0, size) in the intent extent, we'd
7505 * stale components overlapping [size, eof).
7507 extent.e_start = extent.e_end;
7508 extent.e_end = OBD_OBJECT_EOF;
7511 /* 2. stale overlapping components */
7512 rc = lod_stale_components(env, lo, primary, &extent, th);
7516 /* 3. find the components which need instantiating.
7517 * instantiate [0, mlc->mlc_intent->e_end) */
7519 /* restore truncate intent extent */
7520 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC)
7521 extent.e_end = extent.e_start;
7524 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7525 if (!lu_extent_is_overlapped(&extent,
7526 &lod_comp->llc_extent))
7529 if (!lod_is_instantiation_needed(lod_comp))
7532 CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
7533 primary, lod_comp_index(lo, lod_comp));
7534 info->lti_comp_idx[info->lti_count++] =
7535 lod_comp_index(lo, lod_comp);
7537 } else { /* MD_LAYOUT_RESYNC */
7538 if (mlc->mlc_mirror_id == 0) {
7540 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7541 if (!lod_comp_inited(lod_comp))
7544 extent.e_end = lod_comp->llc_extent.e_end;
7547 rc = lod_prepare_resync(env, lo, &extent);
7551 /* mirror write, try to init its all components */
7552 rc = lod_prepare_resync_mirror(env, lo,
7553 mlc->mlc_mirror_id);
7558 /* change the file state to SYNC_PENDING */
7559 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7562 rc = lod_declare_instantiate_components(env, lo, th, 0);
7566 /* 3. transfer layout version to OST objects.
7567 * transfer new layout version to OST objects so that stale writes
7568 * can be denied. It also ends an era of writing by setting
7569 * LU_LAYOUT_RESYNC. Normal client can never use this bit to
7570 * send write RPC; only resync RPCs could do it. */
7571 layout_attr->la_valid = LA_LAYOUT_VERSION;
7572 layout_attr->la_layout_version = 0; /* set current version */
7573 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7574 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7575 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7579 lod_obj_inc_layout_gen(lo);
7582 lod_striping_free(env, lo);
7586 static int lod_declare_update_sync_pending(const struct lu_env *env,
7587 struct lod_object *lo, struct md_layout_change *mlc,
7590 struct lod_thread_info *info = lod_env_info(env);
7591 unsigned sync_components = 0;
7592 unsigned resync_components = 0;
7597 LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
7598 LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
7599 mlc->mlc_opc == MD_LAYOUT_WRITE);
7601 CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
7602 PFID(lod_object_fid(lo)), mlc->mlc_opc);
7604 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7605 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
7606 PFID(lod_object_fid(lo)));
7608 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7609 return lod_declare_update_write_pending(env, lo, mlc, th);
7612 /* MD_LAYOUT_RESYNC_DONE */
7614 for (i = 0; i < lo->ldo_comp_cnt; i++) {
7615 struct lod_layout_component *lod_comp;
7618 lod_comp = &lo->ldo_comp_entries[i];
7620 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
7625 for (j = 0; j < mlc->mlc_resync_count; j++) {
7626 if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
7629 mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
7630 lod_comp->llc_flags &= ~LCME_FL_STALE;
7631 resync_components++;
7637 for (i = 0; i < mlc->mlc_resync_count; i++) {
7638 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
7641 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
7642 "or already synced\n", PFID(lod_object_fid(lo)),
7643 mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
7644 GOTO(out, rc = -EINVAL);
7647 if (!sync_components || (mlc->mlc_resync_count && !resync_components)) {
7648 CDEBUG(D_LAYOUT, DFID": no mirror in sync\n",
7649 PFID(lod_object_fid(lo)));
7651 /* tend to return an error code here to prevent
7652 * the MDT from setting SoM attribute */
7653 GOTO(out, rc = -EINVAL);
7656 CDEBUG(D_LAYOUT, DFID": synced %u resynced %u/%zu components\n",
7657 PFID(lod_object_fid(lo)),
7658 sync_components, resync_components, mlc->mlc_resync_count);
7660 lo->ldo_flr_state = LCM_FL_RDONLY;
7661 lod_obj_inc_layout_gen(lo);
7663 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
7664 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
7665 &info->lti_buf, XATTR_NAME_LOV, 0, th);
7670 lod_striping_free(env, lo);
7674 typedef int (*mlc_handler)(const struct lu_env *env, struct dt_object *dt,
7675 const struct md_layout_change *mlc,
7676 struct thandle *th);
7679 * Attach stripes after target's for migrating directory. NB, we
7680 * only need to declare this, the actual work is done inside
7681 * lod_xattr_set_lmv().
7683 * \param[in] env execution environment
7684 * \param[in] dt target object
7685 * \param[in] mlc layout change data
7686 * \param[in] th transaction handle
7688 * \retval 0 on success
7689 * \retval negative if failed
7691 static int lod_dir_declare_layout_attach(const struct lu_env *env,
7692 struct dt_object *dt,
7693 const struct md_layout_change *mlc,
7696 struct lod_thread_info *info = lod_env_info(env);
7697 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
7698 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
7699 struct lod_object *lo = lod_dt_obj(dt);
7700 struct dt_object *next = dt_object_child(dt);
7701 struct dt_object_format *dof = &info->lti_format;
7702 struct lmv_mds_md_v1 *lmv = mlc->mlc_buf.lb_buf;
7703 struct dt_object **stripes;
7704 __u32 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
7705 struct lu_fid *fid = &info->lti_fid;
7706 struct lod_tgt_desc *tgt;
7707 struct dt_object *dto;
7708 struct dt_device *tgt_dt;
7709 int type = LU_SEQ_RANGE_ANY;
7710 struct dt_insert_rec *rec = &info->lti_dt_rec;
7711 char *stripe_name = info->lti_key;
7712 struct lu_name *sname;
7713 struct linkea_data ldata = { NULL };
7714 struct lu_buf linkea_buf;
7721 if (!lmv_is_sane(lmv))
7724 if (!dt_try_as_dir(env, dt))
7727 dof->dof_type = DFT_DIR;
7729 OBD_ALLOC_PTR_ARRAY(stripes, (lo->ldo_dir_stripe_count + stripe_count));
7733 for (i = 0; i < lo->ldo_dir_stripe_count; i++)
7734 stripes[i] = lo->ldo_stripe[i];
7736 rec->rec_type = S_IFDIR;
7738 for (i = 0; i < stripe_count; i++) {
7740 &lmv->lmv_stripe_fids[i]);
7741 if (!fid_is_sane(fid))
7744 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
7748 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
7749 tgt_dt = lod->lod_child;
7751 tgt = LTD_TGT(ltd, idx);
7753 GOTO(out, rc = -ESTALE);
7754 tgt_dt = tgt->ltd_tgt;
7757 dto = dt_locate_at(env, tgt_dt, fid,
7758 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
7761 GOTO(out, rc = PTR_ERR(dto));
7763 stripes[i + lo->ldo_dir_stripe_count] = dto;
7765 if (!dt_try_as_dir(env, dto))
7766 GOTO(out, rc = -ENOTDIR);
7768 rc = lod_sub_declare_ref_add(env, dto, th);
7772 rec->rec_fid = lu_object_fid(&dto->do_lu);
7773 rc = lod_sub_declare_insert(env, dto,
7774 (const struct dt_rec *)rec,
7775 (const struct dt_key *)dot, th);
7779 rc = lod_sub_declare_insert(env, dto,
7780 (const struct dt_rec *)rec,
7781 (const struct dt_key *)dotdot, th);
7785 rc = lod_sub_declare_xattr_set(env, dto, &mlc->mlc_buf,
7786 XATTR_NAME_LMV, 0, th);
7790 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
7791 PFID(lu_object_fid(&dto->do_lu)),
7792 i + lo->ldo_dir_stripe_count);
7794 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
7795 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
7796 sname, lu_object_fid(&dt->do_lu));
7800 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
7801 linkea_buf.lb_len = ldata.ld_leh->leh_len;
7802 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
7803 XATTR_NAME_LINK, 0, th);
7807 rc = lod_sub_declare_insert(env, next,
7808 (const struct dt_rec *)rec,
7809 (const struct dt_key *)stripe_name,
7814 rc = lod_sub_declare_ref_add(env, next, th);
7820 OBD_FREE_PTR_ARRAY(lo->ldo_stripe,
7821 lo->ldo_dir_stripes_allocated);
7822 lo->ldo_stripe = stripes;
7823 lo->ldo_dir_migrate_offset = lo->ldo_dir_stripe_count;
7824 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_hash_type);
7825 lo->ldo_dir_stripe_count += stripe_count;
7826 lo->ldo_dir_stripes_allocated += stripe_count;
7828 /* plain directory split creates target as a plain directory, while
7829 * after source attached as the first stripe, it becomes a striped
7830 * directory, set correct do_index_ops, otherwise it can't be unlinked.
7832 dt->do_index_ops = &lod_striped_index_ops;
7836 i = lo->ldo_dir_stripe_count;
7837 while (i < lo->ldo_dir_stripe_count + stripe_count && stripes[i])
7838 dt_object_put(env, stripes[i++]);
7840 OBD_FREE_PTR_ARRAY(stripes, stripe_count + lo->ldo_dir_stripe_count);
7844 static int lod_dir_declare_layout_detach(const struct lu_env *env,
7845 struct dt_object *dt,
7846 const struct md_layout_change *unused,
7849 struct lod_thread_info *info = lod_env_info(env);
7850 struct lod_object *lo = lod_dt_obj(dt);
7851 struct dt_object *next = dt_object_child(dt);
7852 char *stripe_name = info->lti_key;
7853 struct dt_object *dto;
7857 if (!dt_try_as_dir(env, dt))
7860 if (!lo->ldo_dir_stripe_count)
7861 return lod_sub_declare_delete(env, next,
7862 (const struct dt_key *)dotdot, th);
7864 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
7865 dto = lo->ldo_stripe[i];
7869 if (!dt_try_as_dir(env, dto))
7872 rc = lod_sub_declare_delete(env, dto,
7873 (const struct dt_key *)dotdot, th);
7877 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
7878 PFID(lu_object_fid(&dto->do_lu)), i);
7880 rc = lod_sub_declare_delete(env, next,
7881 (const struct dt_key *)stripe_name, th);
7885 rc = lod_sub_declare_ref_del(env, next, th);
7893 static int dt_dir_is_empty(const struct lu_env *env,
7894 struct dt_object *obj)
7897 const struct dt_it_ops *iops;
7902 if (!dt_try_as_dir(env, obj))
7905 iops = &obj->do_index_ops->dio_it;
7906 it = iops->init(env, obj, LUDA_64BITHASH);
7908 RETURN(PTR_ERR(it));
7910 rc = iops->get(env, it, (const struct dt_key *)"");
7914 for (rc = 0, i = 0; rc == 0 && i < 3; ++i)
7915 rc = iops->next(env, it);
7921 /* Huh? Index contains no zero key? */
7926 iops->fini(env, it);
7931 static int lod_dir_declare_layout_shrink(const struct lu_env *env,
7932 struct dt_object *dt,
7933 const struct md_layout_change *mlc,
7936 struct lod_thread_info *info = lod_env_info(env);
7937 struct lod_object *lo = lod_dt_obj(dt);
7938 struct dt_object *next = dt_object_child(dt);
7939 struct lmv_user_md *lmu = mlc->mlc_buf.lb_buf;
7940 __u32 final_stripe_count;
7941 char *stripe_name = info->lti_key;
7942 struct lu_buf *lmv_buf = &info->lti_buf;
7943 struct dt_object *dto;
7949 if (!dt_try_as_dir(env, dt))
7952 /* shouldn't be called on plain directory */
7953 LASSERT(lo->ldo_dir_stripe_count);
7955 lmv_buf->lb_buf = &info->lti_lmv.lmv_md_v1;
7956 lmv_buf->lb_len = sizeof(info->lti_lmv.lmv_md_v1);
7958 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
7959 LASSERT(final_stripe_count &&
7960 final_stripe_count < lo->ldo_dir_stripe_count);
7962 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
7963 dto = lo->ldo_stripe[i];
7967 if (i < final_stripe_count) {
7968 if (final_stripe_count == 1)
7971 rc = lod_sub_declare_xattr_set(env, dto, lmv_buf,
7973 LU_XATTR_REPLACE, th);
7980 rc = dt_dir_is_empty(env, dto);
7984 rc = lod_sub_declare_ref_del(env, dto, th);
7988 rc = lod_sub_declare_destroy(env, dto, th);
7992 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
7993 PFID(lu_object_fid(&dto->do_lu)), i);
7995 rc = lod_sub_declare_delete(env, next,
7996 (const struct dt_key *)stripe_name, th);
8000 rc = lod_sub_declare_ref_del(env, next, th);
8005 rc = lod_sub_declare_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV,
8006 LU_XATTR_REPLACE, th);
8011 * Allocate stripes for split directory.
8013 * \param[in] env execution environment
8014 * \param[in] dt target object
8015 * \param[in] mlc layout change data
8016 * \param[in] th transaction handle
8018 * \retval 0 on success
8019 * \retval negative if failed
8021 static int lod_dir_declare_layout_split(const struct lu_env *env,
8022 struct dt_object *dt,
8023 const struct md_layout_change *mlc,
8026 struct lod_thread_info *info = lod_env_info(env);
8027 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
8028 struct lod_object *lo = lod_dt_obj(dt);
8029 struct dt_object_format *dof = &info->lti_format;
8030 struct lmv_user_md_v1 *lum = mlc->mlc_spec->u.sp_ea.eadata;
8031 struct dt_object **stripes;
8039 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
8040 LASSERT(le32_to_cpu(lum->lum_stripe_offset) == LMV_OFFSET_DEFAULT);
8042 saved_count = lo->ldo_dir_stripes_allocated;
8043 stripe_count = le32_to_cpu(lum->lum_stripe_count);
8044 if (stripe_count <= saved_count)
8047 dof->dof_type = DFT_DIR;
8049 OBD_ALLOC(stripes, sizeof(*stripes) * stripe_count);
8053 for (i = 0; i < lo->ldo_dir_stripes_allocated; i++)
8054 stripes[i] = lo->ldo_stripe[i];
8056 lod_qos_statfs_update(env, lod, &lod->lod_mdt_descs);
8057 rc = lod_mdt_alloc_qos(env, lo, stripes, saved_count, stripe_count);
8059 rc = lod_mdt_alloc_rr(env, lo, stripes, saved_count,
8062 OBD_FREE(stripes, sizeof(*stripes) * stripe_count);
8066 LASSERT(rc > saved_count);
8067 OBD_FREE(lo->ldo_stripe,
8068 sizeof(*stripes) * lo->ldo_dir_stripes_allocated);
8069 lo->ldo_stripe = stripes;
8070 lo->ldo_dir_striped = 1;
8071 lo->ldo_dir_stripe_count = rc;
8072 lo->ldo_dir_stripes_allocated = stripe_count;
8073 lo->ldo_dir_split_hash = lo->ldo_dir_hash_type;
8074 lo->ldo_dir_hash_type = le32_to_cpu(lum->lum_hash_type);
8075 if (!lmv_is_known_hash_type(lo->ldo_dir_hash_type))
8076 lo->ldo_dir_hash_type =
8077 lod->lod_mdt_descs.ltd_lmv_desc.ld_pattern;
8078 lo->ldo_dir_hash_type |= LMV_HASH_FLAG_SPLIT | LMV_HASH_FLAG_MIGRATION;
8079 lo->ldo_dir_split_offset = saved_count;
8080 lo->ldo_dir_layout_version++;
8081 lo->ldo_dir_stripe_loaded = 1;
8083 rc = lod_dir_declare_create_stripes(env, dt, mlc->mlc_attr, dof, th);
8085 lod_striping_free(env, lo);
8091 * detach all stripes from dir master object, NB, stripes are not destroyed, but
8092 * deleted from it's parent namespace, this function is called in two places:
8093 * 1. mdd_migrate_mdt() detach stripes from source, and attach them to
8095 * 2. mdd_dir_layout_update() detach stripe before turning 1-stripe directory to
8096 * a plain directory.
8098 * \param[in] env execution environment
8099 * \param[in] dt target object
8100 * \param[in] mlc layout change data
8101 * \param[in] th transaction handle
8103 * \retval 0 on success
8104 * \retval negative if failed
8106 static int lod_dir_layout_detach(const struct lu_env *env,
8107 struct dt_object *dt,
8108 const struct md_layout_change *mlc,
8111 struct lod_thread_info *info = lod_env_info(env);
8112 struct lod_object *lo = lod_dt_obj(dt);
8113 struct dt_object *next = dt_object_child(dt);
8114 char *stripe_name = info->lti_key;
8115 struct dt_object *dto;
8121 if (!lo->ldo_dir_stripe_count) {
8122 /* plain directory delete .. */
8123 rc = lod_sub_delete(env, next,
8124 (const struct dt_key *)dotdot, th);
8128 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8129 dto = lo->ldo_stripe[i];
8133 rc = lod_sub_delete(env, dto,
8134 (const struct dt_key *)dotdot, th);
8138 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8139 PFID(lu_object_fid(&dto->do_lu)), i);
8141 rc = lod_sub_delete(env, next,
8142 (const struct dt_key *)stripe_name, th);
8146 rc = lod_sub_ref_del(env, next, th);
8151 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8152 dto = lo->ldo_stripe[i];
8154 dt_object_put(env, dto);
8156 OBD_FREE_PTR_ARRAY(lo->ldo_stripe, lo->ldo_dir_stripes_allocated);
8157 lo->ldo_stripe = NULL;
8158 lo->ldo_dir_stripes_allocated = 0;
8159 lo->ldo_dir_stripe_count = 0;
8160 dt->do_index_ops = &lod_index_ops;
8165 static int lod_dir_layout_shrink(const struct lu_env *env,
8166 struct dt_object *dt,
8167 const struct md_layout_change *mlc,
8170 struct lod_thread_info *info = lod_env_info(env);
8171 struct lod_object *lo = lod_dt_obj(dt);
8172 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
8173 struct dt_object *next = dt_object_child(dt);
8174 struct lmv_user_md *lmu = mlc->mlc_buf.lb_buf;
8175 __u32 final_stripe_count;
8176 char *stripe_name = info->lti_key;
8177 struct dt_object *dto;
8178 struct lu_buf *lmv_buf = &info->lti_buf;
8179 struct lmv_mds_md_v1 *lmv = &info->lti_lmv.lmv_md_v1;
8181 int type = LU_SEQ_RANGE_ANY;
8187 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
8189 lmv_buf->lb_buf = lmv;
8190 lmv_buf->lb_len = sizeof(*lmv);
8191 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
8192 lmv->lmv_stripe_count = cpu_to_le32(final_stripe_count);
8193 lmv->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type) &
8194 cpu_to_le32(LMV_HASH_TYPE_MASK);
8195 lmv->lmv_layout_version =
8196 cpu_to_le32(lo->ldo_dir_layout_version + 1);
8198 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8199 dto = lo->ldo_stripe[i];
8203 if (i < final_stripe_count) {
8204 /* if only one stripe left, no need to update
8205 * LMV because this stripe will replace master
8206 * object and act as a plain directory.
8208 if (final_stripe_count == 1)
8212 rc = lod_fld_lookup(env, lod,
8213 lu_object_fid(&dto->do_lu),
8218 lmv->lmv_master_mdt_index = cpu_to_le32(mdtidx);
8219 rc = lod_sub_xattr_set(env, dto, lmv_buf,
8221 LU_XATTR_REPLACE, th);
8228 dt_write_lock(env, dto, DT_TGT_CHILD);
8229 rc = lod_sub_ref_del(env, dto, th);
8230 dt_write_unlock(env, dto);
8234 rc = lod_sub_destroy(env, dto, th);
8238 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8239 PFID(lu_object_fid(&dto->do_lu)), i);
8241 rc = lod_sub_delete(env, next,
8242 (const struct dt_key *)stripe_name, th);
8246 rc = lod_sub_ref_del(env, next, th);
8251 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &mdtidx,
8256 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
8257 lmv->lmv_master_mdt_index = cpu_to_le32(mdtidx);
8258 rc = lod_sub_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV,
8259 LU_XATTR_REPLACE, th);
8263 for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
8264 dto = lo->ldo_stripe[i];
8266 dt_object_put(env, dto);
8268 lo->ldo_dir_stripe_count = final_stripe_count;
8273 static mlc_handler dir_mlc_declare_ops[MD_LAYOUT_MAX] = {
8274 [MD_LAYOUT_ATTACH] = lod_dir_declare_layout_attach,
8275 [MD_LAYOUT_DETACH] = lod_dir_declare_layout_detach,
8276 [MD_LAYOUT_SHRINK] = lod_dir_declare_layout_shrink,
8277 [MD_LAYOUT_SPLIT] = lod_dir_declare_layout_split,
8280 static mlc_handler dir_mlc_ops[MD_LAYOUT_MAX] = {
8281 [MD_LAYOUT_DETACH] = lod_dir_layout_detach,
8282 [MD_LAYOUT_SHRINK] = lod_dir_layout_shrink,
8285 static int lod_declare_layout_change(const struct lu_env *env,
8286 struct dt_object *dt, struct md_layout_change *mlc,
8289 struct lod_thread_info *info = lod_env_info(env);
8290 struct lod_object *lo = lod_dt_obj(dt);
8295 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
8296 LASSERT(dir_mlc_declare_ops[mlc->mlc_opc]);
8297 rc = dir_mlc_declare_ops[mlc->mlc_opc](env, dt, mlc, th);
8301 if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
8302 dt_object_remote(dt_object_child(dt)))
8305 rc = lod_striping_load(env, lo);
8309 LASSERT(lo->ldo_comp_cnt > 0);
8311 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
8315 switch (lo->ldo_flr_state) {
8317 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
8321 rc = lod_declare_update_rdonly(env, lo, mlc, th);
8323 case LCM_FL_WRITE_PENDING:
8324 rc = lod_declare_update_write_pending(env, lo, mlc, th);
8326 case LCM_FL_SYNC_PENDING:
8327 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
8338 * Instantiate layout component objects which covers the intent write offset.
8340 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
8341 struct md_layout_change *mlc, struct thandle *th)
8343 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
8344 struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
8345 struct lod_object *lo = lod_dt_obj(dt);
8350 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
8351 LASSERT(dir_mlc_ops[mlc->mlc_opc]);
8352 rc = dir_mlc_ops[mlc->mlc_opc](env, dt, mlc, th);
8356 rc = lod_striped_create(env, dt, attr, NULL, th);
8357 if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
8358 layout_attr->la_layout_version |= lo->ldo_layout_gen;
8359 rc = lod_attr_set(env, dt, layout_attr, th);
8365 struct dt_object_operations lod_obj_ops = {
8366 .do_read_lock = lod_read_lock,
8367 .do_write_lock = lod_write_lock,
8368 .do_read_unlock = lod_read_unlock,
8369 .do_write_unlock = lod_write_unlock,
8370 .do_write_locked = lod_write_locked,
8371 .do_attr_get = lod_attr_get,
8372 .do_declare_attr_set = lod_declare_attr_set,
8373 .do_attr_set = lod_attr_set,
8374 .do_xattr_get = lod_xattr_get,
8375 .do_declare_xattr_set = lod_declare_xattr_set,
8376 .do_xattr_set = lod_xattr_set,
8377 .do_declare_xattr_del = lod_declare_xattr_del,
8378 .do_xattr_del = lod_xattr_del,
8379 .do_xattr_list = lod_xattr_list,
8380 .do_ah_init = lod_ah_init,
8381 .do_declare_create = lod_declare_create,
8382 .do_create = lod_create,
8383 .do_declare_destroy = lod_declare_destroy,
8384 .do_destroy = lod_destroy,
8385 .do_index_try = lod_index_try,
8386 .do_declare_ref_add = lod_declare_ref_add,
8387 .do_ref_add = lod_ref_add,
8388 .do_declare_ref_del = lod_declare_ref_del,
8389 .do_ref_del = lod_ref_del,
8390 .do_object_sync = lod_object_sync,
8391 .do_object_lock = lod_object_lock,
8392 .do_object_unlock = lod_object_unlock,
8393 .do_invalidate = lod_invalidate,
8394 .do_declare_layout_change = lod_declare_layout_change,
8395 .do_layout_change = lod_layout_change,
8399 * Implementation of dt_body_operations::dbo_read.
8401 * \see dt_body_operations::dbo_read() in the API description for details.
8403 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
8404 struct lu_buf *buf, loff_t *pos)
8406 struct dt_object *next = dt_object_child(dt);
8408 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
8409 S_ISLNK(dt->do_lu.lo_header->loh_attr));
8410 return next->do_body_ops->dbo_read(env, next, buf, pos);
8414 * Implementation of dt_body_operations::dbo_declare_write.
8416 * \see dt_body_operations::dbo_declare_write() in the API description
8419 static ssize_t lod_declare_write(const struct lu_env *env,
8420 struct dt_object *dt,
8421 const struct lu_buf *buf, loff_t pos,
8424 return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
8428 * Implementation of dt_body_operations::dbo_write.
8430 * \see dt_body_operations::dbo_write() in the API description for details.
8432 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
8433 const struct lu_buf *buf, loff_t *pos,
8436 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
8437 S_ISLNK(dt->do_lu.lo_header->loh_attr));
8438 return lod_sub_write(env, dt_object_child(dt), buf, pos, th);
8441 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
8442 __u64 start, __u64 end, struct thandle *th)
8444 if (dt_object_remote(dt))
8447 return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
8450 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
8451 __u64 start, __u64 end, struct thandle *th)
8453 if (dt_object_remote(dt))
8456 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
8457 return lod_sub_punch(env, dt_object_child(dt), start, end, th);
8461 * different type of files use the same body_ops because object may be created
8462 * in OUT, where there is no chance to set correct body_ops for each type, so
8463 * body_ops themselves will check file type inside, see lod_read/write/punch for
8466 const struct dt_body_operations lod_body_ops = {
8467 .dbo_read = lod_read,
8468 .dbo_declare_write = lod_declare_write,
8469 .dbo_write = lod_write,
8470 .dbo_declare_punch = lod_declare_punch,
8471 .dbo_punch = lod_punch,
8475 * Implementation of lu_object_operations::loo_object_init.
8477 * The function determines the type and the index of the target device using
8478 * sequence of the object's FID. Then passes control down to the
8479 * corresponding device:
8480 * OSD for the local objects, OSP for remote
8482 * \see lu_object_operations::loo_object_init() in the API description
8485 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
8486 const struct lu_object_conf *conf)
8488 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
8489 struct lu_device *cdev = NULL;
8490 struct lu_object *cobj;
8491 struct lod_tgt_descs *ltd = NULL;
8492 struct lod_tgt_desc *tgt;
8494 int type = LU_SEQ_RANGE_ANY;
8498 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
8502 if (type == LU_SEQ_RANGE_MDT &&
8503 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
8504 cdev = &lod->lod_child->dd_lu_dev;
8505 } else if (type == LU_SEQ_RANGE_MDT) {
8506 ltd = &lod->lod_mdt_descs;
8508 } else if (type == LU_SEQ_RANGE_OST) {
8509 ltd = &lod->lod_ost_descs;
8516 if (ltd->ltd_tgts_size > idx &&
8517 test_bit(idx, ltd->ltd_tgt_bitmap)) {
8518 tgt = LTD_TGT(ltd, idx);
8520 LASSERT(tgt != NULL);
8521 LASSERT(tgt->ltd_tgt != NULL);
8523 cdev = &(tgt->ltd_tgt->dd_lu_dev);
8525 lod_putref(lod, ltd);
8528 if (unlikely(cdev == NULL))
8531 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
8532 if (unlikely(cobj == NULL))
8535 lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
8537 lu_object_add(lo, cobj);
8544 * Alloc cached foreign LOV
8546 * \param[in] lo object
8547 * \param[in] size size of foreign LOV
8549 * \retval 0 on success
8550 * \retval negative if failed
8552 int lod_alloc_foreign_lov(struct lod_object *lo, size_t size)
8554 OBD_ALLOC_LARGE(lo->ldo_foreign_lov, size);
8555 if (lo->ldo_foreign_lov == NULL)
8557 lo->ldo_foreign_lov_size = size;
8558 lo->ldo_is_foreign = 1;
8564 * Free cached foreign LOV
8566 * \param[in] lo object
8568 void lod_free_foreign_lov(struct lod_object *lo)
8570 if (lo->ldo_foreign_lov != NULL)
8571 OBD_FREE_LARGE(lo->ldo_foreign_lov, lo->ldo_foreign_lov_size);
8572 lo->ldo_foreign_lov = NULL;
8573 lo->ldo_foreign_lov_size = 0;
8574 lo->ldo_is_foreign = 0;
8579 * Free cached foreign LMV
8581 * \param[in] lo object
8583 void lod_free_foreign_lmv(struct lod_object *lo)
8585 if (lo->ldo_foreign_lmv != NULL)
8586 OBD_FREE_LARGE(lo->ldo_foreign_lmv, lo->ldo_foreign_lmv_size);
8587 lo->ldo_foreign_lmv = NULL;
8588 lo->ldo_foreign_lmv_size = 0;
8589 lo->ldo_dir_is_foreign = 0;
8594 * Release resources associated with striping.
8596 * If the object is striped (regular or directory), then release
8597 * the stripe objects references and free the ldo_stripe array.
8599 * \param[in] env execution environment
8600 * \param[in] lo object
8602 void lod_striping_free_nolock(const struct lu_env *env, struct lod_object *lo)
8604 struct lod_layout_component *lod_comp;
8607 if (unlikely(lo->ldo_is_foreign)) {
8608 lod_free_foreign_lov(lo);
8609 lo->ldo_comp_cached = 0;
8610 } else if (unlikely(lo->ldo_dir_is_foreign)) {
8611 lod_free_foreign_lmv(lo);
8612 lo->ldo_dir_stripe_loaded = 0;
8613 } else if (lo->ldo_stripe != NULL) {
8614 LASSERT(lo->ldo_comp_entries == NULL);
8615 LASSERT(lo->ldo_dir_stripes_allocated > 0);
8617 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8618 if (lo->ldo_stripe[i])
8619 dt_object_put(env, lo->ldo_stripe[i]);
8622 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
8623 OBD_FREE(lo->ldo_stripe, j);
8624 lo->ldo_stripe = NULL;
8625 lo->ldo_dir_stripes_allocated = 0;
8626 lo->ldo_dir_stripe_loaded = 0;
8627 lo->ldo_dir_stripe_count = 0;
8628 } else if (lo->ldo_comp_entries != NULL) {
8629 for (i = 0; i < lo->ldo_comp_cnt; i++) {
8630 /* free lod_layout_component::llc_stripe array */
8631 lod_comp = &lo->ldo_comp_entries[i];
8633 if (lod_comp->llc_stripe == NULL)
8635 LASSERT(lod_comp->llc_stripes_allocated != 0);
8636 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
8637 if (lod_comp->llc_stripe[j] != NULL)
8639 &lod_comp->llc_stripe[j]->do_lu);
8641 OBD_FREE_PTR_ARRAY(lod_comp->llc_stripe,
8642 lod_comp->llc_stripes_allocated);
8643 lod_comp->llc_stripe = NULL;
8644 OBD_FREE_PTR_ARRAY(lod_comp->llc_ost_indices,
8645 lod_comp->llc_stripes_allocated);
8646 lod_comp->llc_ost_indices = NULL;
8647 lod_comp->llc_stripes_allocated = 0;
8649 lod_free_comp_entries(lo);
8650 lo->ldo_comp_cached = 0;
8654 void lod_striping_free(const struct lu_env *env, struct lod_object *lo)
8656 mutex_lock(&lo->ldo_layout_mutex);
8657 lod_striping_free_nolock(env, lo);
8658 mutex_unlock(&lo->ldo_layout_mutex);
8662 * Implementation of lu_object_operations::loo_object_free.
8664 * \see lu_object_operations::loo_object_free() in the API description
8667 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
8669 struct lod_object *lo = lu2lod_obj(o);
8671 /* release all underlying object pinned */
8672 lod_striping_free(env, lo);
8674 /* lo doesn't contain a lu_object_header, so we don't need call_rcu */
8675 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
8679 * Implementation of lu_object_operations::loo_object_release.
8681 * \see lu_object_operations::loo_object_release() in the API description
8684 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
8686 /* XXX: shouldn't we release everything here in case if object
8687 * creation failed before? */
8691 * Implementation of lu_object_operations::loo_object_print.
8693 * \see lu_object_operations::loo_object_print() in the API description
8696 static int lod_object_print(const struct lu_env *env, void *cookie,
8697 lu_printer_t p, const struct lu_object *l)
8699 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
8701 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
8704 struct lu_object_operations lod_lu_obj_ops = {
8705 .loo_object_init = lod_object_init,
8706 .loo_object_free = lod_object_free,
8707 .loo_object_release = lod_object_release,
8708 .loo_object_print = lod_object_print,