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 if (comp_end != LUSTRE_EOF &&
1140 comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
1141 CWARN("Component end %llu is not a multiple of min size %u\n",
1142 comp_end, LOV_MIN_STRIPE_SIZE);
1143 comp_end = round_up(comp_end, LOV_MIN_STRIPE_SIZE);
1145 /* check stripe size is multiplier of comp_end */
1146 if (comp_end != LUSTRE_EOF &&
1147 comp_end % comp->llc_stripe_size) {
1148 /* fix that even for defined stripe size but warn
1149 * about the problem, that must not happen
1151 CWARN("Component end %llu is not aligned by the stripe size %u\n",
1152 comp_end, comp->llc_stripe_size);
1153 comp->llc_stripe_size = comp_end & ~(comp_end - 1);
1158 static inline void lod_adjust_stripe_info(struct lod_layout_component *comp,
1159 struct lov_desc *desc,
1162 if (comp->llc_pattern != LOV_PATTERN_MDT) {
1163 if (append_stripes) {
1164 comp->llc_stripe_count = append_stripes;
1165 } else if (!comp->llc_stripe_count) {
1166 comp->llc_stripe_count =
1167 desc->ld_default_stripe_count;
1171 lod_adjust_stripe_size(comp, desc->ld_default_stripe_size);
1174 int lod_obj_for_each_stripe(const struct lu_env *env, struct lod_object *lo,
1176 struct lod_obj_stripe_cb_data *data)
1178 struct lod_layout_component *lod_comp;
1182 LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
1183 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1184 lod_comp = &lo->ldo_comp_entries[i];
1186 if (lod_comp->llc_stripe == NULL)
1189 /* has stripe but not inited yet, this component has been
1190 * declared to be created, but hasn't created yet.
1192 if (!lod_comp_inited(lod_comp))
1195 if (data->locd_comp_skip_cb &&
1196 data->locd_comp_skip_cb(env, lo, i, data))
1199 if (data->locd_comp_cb) {
1200 rc = data->locd_comp_cb(env, lo, i, data);
1205 /* could used just to do sth about component, not each
1208 if (!data->locd_stripe_cb)
1211 LASSERT(lod_comp->llc_stripe_count > 0);
1212 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
1213 struct dt_object *dt = lod_comp->llc_stripe[j];
1217 rc = data->locd_stripe_cb(env, lo, dt, th, i, j, data);
1225 static bool lod_obj_attr_set_comp_skip_cb(const struct lu_env *env,
1226 struct lod_object *lo, int comp_idx,
1227 struct lod_obj_stripe_cb_data *data)
1229 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
1230 bool skipped = false;
1232 if (!(data->locd_attr->la_valid & LA_LAYOUT_VERSION))
1235 switch (lo->ldo_flr_state) {
1236 case LCM_FL_WRITE_PENDING: {
1239 /* skip stale components */
1240 if (lod_comp->llc_flags & LCME_FL_STALE) {
1245 /* skip valid and overlapping components, therefore any
1246 * attempts to write overlapped components will never succeed
1247 * because client will get EINPROGRESS. */
1248 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1252 if (lo->ldo_comp_entries[i].llc_flags & LCME_FL_STALE)
1255 if (lu_extent_is_overlapped(&lod_comp->llc_extent,
1256 &lo->ldo_comp_entries[i].llc_extent)) {
1264 LASSERTF(0, "impossible: %d\n", lo->ldo_flr_state);
1265 case LCM_FL_SYNC_PENDING:
1269 CDEBUG(D_LAYOUT, DFID": %s to set component %x to version: %u\n",
1270 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
1271 skipped ? "skipped" : "chose", lod_comp->llc_id,
1272 data->locd_attr->la_layout_version);
1278 lod_obj_stripe_attr_set_cb(const struct lu_env *env, struct lod_object *lo,
1279 struct dt_object *dt, struct thandle *th,
1280 int comp_idx, int stripe_idx,
1281 struct lod_obj_stripe_cb_data *data)
1283 if (data->locd_declare)
1284 return lod_sub_declare_attr_set(env, dt, data->locd_attr, th);
1286 if (data->locd_attr->la_valid & LA_LAYOUT_VERSION) {
1287 CDEBUG(D_LAYOUT, DFID": set layout version: %u, comp_idx: %d\n",
1288 PFID(lu_object_fid(&dt->do_lu)),
1289 data->locd_attr->la_layout_version, comp_idx);
1292 return lod_sub_attr_set(env, dt, data->locd_attr, th);
1296 * Implementation of dt_object_operations::do_declare_attr_set.
1298 * If the object is striped, then apply the changes to all the stripes.
1300 * \see dt_object_operations::do_declare_attr_set() in the API description
1303 static int lod_declare_attr_set(const struct lu_env *env,
1304 struct dt_object *dt,
1305 const struct lu_attr *attr,
1308 struct dt_object *next = dt_object_child(dt);
1309 struct lod_object *lo = lod_dt_obj(dt);
1314 * declare setattr on the local object
1316 rc = lod_sub_declare_attr_set(env, next, attr, th);
1320 /* osp_declare_attr_set() ignores all attributes other than
1321 * UID, GID, PROJID, and size, and osp_attr_set() ignores all
1322 * but UID, GID and PROJID. Declaration of size attr setting
1323 * happens through lod_declare_init_size(), and not through
1324 * this function. Therefore we need not load striping unless
1325 * ownership is changing. This should save memory and (we hope)
1326 * speed up rename().
1328 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1329 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1332 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1335 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID | LA_MODE |
1336 LA_ATIME | LA_MTIME | LA_CTIME |
1341 * load striping information, notice we don't do this when object
1342 * is being initialized as we don't need this information till
1343 * few specific cases like destroy, chown
1345 rc = lod_striping_load(env, lo);
1349 if (!lod_obj_is_striped(dt))
1353 * if object is striped declare changes on the stripes
1355 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1356 LASSERT(lo->ldo_stripe);
1357 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1358 if (lo->ldo_stripe[i] == NULL)
1360 if (!dt_object_exists(lo->ldo_stripe[i]))
1362 rc = lod_sub_declare_attr_set(env, lo->ldo_stripe[i],
1368 struct lod_obj_stripe_cb_data data = { { 0 } };
1370 data.locd_attr = attr;
1371 data.locd_declare = true;
1372 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1373 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1379 if (!dt_object_exists(next) || dt_object_remote(next) ||
1380 !S_ISREG(attr->la_mode))
1383 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1384 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
1388 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1389 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1390 struct lod_thread_info *info = lod_env_info(env);
1391 struct lu_buf *buf = &info->lti_buf;
1393 buf->lb_buf = info->lti_ea_store;
1394 buf->lb_len = info->lti_ea_store_size;
1395 rc = lod_sub_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1396 LU_XATTR_REPLACE, th);
1403 * Implementation of dt_object_operations::do_attr_set.
1405 * If the object is striped, then apply the changes to all or subset of
1406 * the stripes depending on the object type and specific attributes.
1408 * \see dt_object_operations::do_attr_set() in the API description for details.
1410 static int lod_attr_set(const struct lu_env *env,
1411 struct dt_object *dt,
1412 const struct lu_attr *attr,
1415 struct dt_object *next = dt_object_child(dt);
1416 struct lod_object *lo = lod_dt_obj(dt);
1421 * apply changes to the local object
1423 rc = lod_sub_attr_set(env, next, attr, th);
1427 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1428 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1431 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1434 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE | LA_PROJID |
1435 LA_ATIME | LA_MTIME | LA_CTIME |
1440 /* FIXME: a tricky case in the code path of mdd_layout_change():
1441 * the in-memory striping information has been freed in lod_xattr_set()
1442 * due to layout change. It has to load stripe here again. It only
1443 * changes flags of layout so declare_attr_set() is still accurate */
1444 rc = lod_striping_load(env, lo);
1448 if (!lod_obj_is_striped(dt))
1452 * if object is striped, apply changes to all the stripes
1454 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1455 LASSERT(lo->ldo_stripe);
1456 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1457 if (unlikely(lo->ldo_stripe[i] == NULL))
1460 if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1463 rc = lod_sub_attr_set(env, lo->ldo_stripe[i], attr, th);
1468 struct lod_obj_stripe_cb_data data = { { 0 } };
1470 data.locd_attr = attr;
1471 data.locd_declare = false;
1472 data.locd_comp_skip_cb = lod_obj_attr_set_comp_skip_cb;
1473 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1474 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1480 if (!dt_object_exists(next) || dt_object_remote(next) ||
1481 !S_ISREG(attr->la_mode))
1484 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1485 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
1489 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1490 struct lod_thread_info *info = lod_env_info(env);
1491 struct lu_buf *buf = &info->lti_buf;
1492 struct ost_id *oi = &info->lti_ostid;
1493 struct lu_fid *fid = &info->lti_fid;
1494 struct lov_mds_md_v1 *lmm;
1495 struct lov_ost_data_v1 *objs;
1498 rc = lod_get_lov_ea(env, lo);
1502 buf->lb_buf = info->lti_ea_store;
1503 buf->lb_len = info->lti_ea_store_size;
1504 lmm = info->lti_ea_store;
1505 magic = le32_to_cpu(lmm->lmm_magic);
1506 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
1507 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1508 struct lov_comp_md_entry_v1 *lcme =
1509 &lcm->lcm_entries[0];
1511 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1512 magic = le32_to_cpu(lmm->lmm_magic);
1515 if (magic == LOV_MAGIC_V1)
1516 objs = &(lmm->lmm_objects[0]);
1518 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1519 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1520 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1522 fid_to_ostid(fid, oi);
1523 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1525 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1526 LU_XATTR_REPLACE, th);
1527 } else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1528 struct lod_thread_info *info = lod_env_info(env);
1529 struct lu_buf *buf = &info->lti_buf;
1530 struct lov_comp_md_v1 *lcm;
1531 struct lov_comp_md_entry_v1 *lcme;
1533 rc = lod_get_lov_ea(env, lo);
1537 buf->lb_buf = info->lti_ea_store;
1538 buf->lb_len = info->lti_ea_store_size;
1540 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1 &&
1541 le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_SEL)
1544 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1545 lcme = &lcm->lcm_entries[0];
1546 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1547 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1549 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1550 LU_XATTR_REPLACE, th);
1557 * Implementation of dt_object_operations::do_xattr_get.
1559 * If LOV EA is requested from the root object and it's not
1560 * found, then return default striping for the filesystem.
1562 * \see dt_object_operations::do_xattr_get() in the API description for details.
1564 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1565 struct lu_buf *buf, const char *name)
1567 struct lod_thread_info *info = lod_env_info(env);
1568 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1573 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1574 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1575 struct lmv_mds_md_v1 *lmv1;
1576 struct lmv_foreign_md *lfm;
1579 if (rc > (typeof(rc))sizeof(*lmv1))
1582 /* short (<= sizeof(struct lmv_mds_md_v1)) foreign LMV case */
1583 /* XXX empty foreign LMV is not allowed */
1584 if (rc <= offsetof(typeof(*lfm), lfm_value))
1585 RETURN(rc = rc > 0 ? -EINVAL : rc);
1587 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1588 BUILD_BUG_ON(sizeof(*lmv1) > sizeof(info->lti_key));
1590 /* lti_buf is large enough for *lmv1 or a short
1591 * (<= sizeof(struct lmv_mds_md_v1)) foreign LMV
1593 info->lti_buf.lb_buf = info->lti_key;
1594 info->lti_buf.lb_len = sizeof(*lmv1);
1595 rc = dt_xattr_get(env, dt_object_child(dt),
1596 &info->lti_buf, name);
1597 if (unlikely(rc <= offsetof(typeof(*lfm),
1599 RETURN(rc = rc > 0 ? -EINVAL : rc);
1601 lfm = info->lti_buf.lb_buf;
1602 if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN)
1605 if (unlikely(rc != sizeof(*lmv1)))
1606 RETURN(rc = rc > 0 ? -EINVAL : rc);
1608 lmv1 = info->lti_buf.lb_buf;
1609 /* The on-disk LMV EA only contains header, but the
1610 * returned LMV EA size should contain the space for
1611 * the FIDs of all shards of the striped directory. */
1612 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1613 rc = lmv_mds_md_size(
1614 le32_to_cpu(lmv1->lmv_stripe_count),
1615 le32_to_cpu(lmv1->lmv_magic));
1618 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1621 if (rc != sizeof(*lmv1))
1622 RETURN(rc = rc > 0 ? -EINVAL : rc);
1624 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1628 RETURN(rc = rc1 != 0 ? rc1 : rc);
1631 if ((rc > 0) && buf->lb_buf && strcmp(name, XATTR_NAME_LOV) == 0) {
1632 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1634 if (lcm->lcm_magic == cpu_to_le32(LOV_MAGIC_SEL))
1635 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1638 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1642 * XXX: Only used by lfsck
1644 * lod returns default striping on the real root of the device
1645 * this is like the root stores default striping for the whole
1646 * filesystem. historically we've been using a different approach
1647 * and store it in the config.
1649 dt_root_get(env, dev->lod_child, &info->lti_fid);
1650 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1652 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1653 struct lov_user_md *lum = buf->lb_buf;
1654 struct lov_desc *desc = &dev->lod_ost_descs.ltd_lov_desc;
1656 if (buf->lb_buf == NULL) {
1658 } else if (buf->lb_len >= sizeof(*lum)) {
1659 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1660 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1661 lmm_oi_set_id(&lum->lmm_oi, 0);
1662 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1663 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1664 lum->lmm_stripe_size = cpu_to_le32(
1665 desc->ld_default_stripe_size);
1666 lum->lmm_stripe_count = cpu_to_le16(
1667 desc->ld_default_stripe_count);
1668 lum->lmm_stripe_offset = cpu_to_le16(
1669 desc->ld_default_stripe_offset);
1682 * Checks that the magic of the stripe is sane.
1684 * \param[in] lod lod device
1685 * \param[in] lum a buffer storing LMV EA to verify
1687 * \retval 0 if the EA is sane
1688 * \retval negative otherwise
1690 static int lod_verify_md_striping(struct lod_device *lod,
1691 const struct lmv_user_md_v1 *lum)
1693 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1694 CERROR("%s: invalid lmv_user_md: magic = %x, "
1695 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1696 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1697 (int)le32_to_cpu(lum->lum_stripe_offset),
1698 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1706 * Initialize LMV EA for a slave.
1708 * Initialize slave's LMV EA from the master's LMV EA.
1710 * \param[in] master_lmv a buffer containing master's EA
1711 * \param[out] slave_lmv a buffer where slave's EA will be stored
1714 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1715 const struct lmv_mds_md_v1 *master_lmv)
1717 *slave_lmv = *master_lmv;
1718 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1724 * Generate LMV EA from the object passed as \a dt. The object must have
1725 * the stripes created and initialized.
1727 * \param[in] env execution environment
1728 * \param[in] dt object
1729 * \param[out] lmv_buf buffer storing generated LMV EA
1731 * \retval 0 on success
1732 * \retval negative if failed
1734 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1735 struct lu_buf *lmv_buf)
1737 struct lod_thread_info *info = lod_env_info(env);
1738 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1739 struct lod_object *lo = lod_dt_obj(dt);
1740 struct lmv_mds_md_v1 *lmm1;
1742 int type = LU_SEQ_RANGE_ANY;
1747 LASSERT(lo->ldo_dir_striped != 0);
1748 LASSERT(lo->ldo_dir_stripe_count > 0);
1749 stripe_count = lo->ldo_dir_stripe_count;
1750 /* Only store the LMV EA heahder on the disk. */
1751 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1752 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1756 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1759 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1760 memset(lmm1, 0, sizeof(*lmm1));
1761 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1762 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1763 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1764 lmm1->lmv_layout_version = cpu_to_le32(lo->ldo_dir_layout_version);
1765 if (lod_is_layout_changing(lo)) {
1766 lmm1->lmv_migrate_hash = cpu_to_le32(lo->ldo_dir_migrate_hash);
1767 lmm1->lmv_migrate_offset =
1768 cpu_to_le32(lo->ldo_dir_migrate_offset);
1770 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1775 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1776 lmv_buf->lb_buf = info->lti_ea_store;
1777 lmv_buf->lb_len = sizeof(*lmm1);
1783 * Create in-core represenation for a striped directory.
1785 * Parse the buffer containing LMV EA and instantiate LU objects
1786 * representing the stripe objects. The pointers to the objects are
1787 * stored in ldo_stripe field of \a lo. This function is used when
1788 * we need to access an already created object (i.e. load from a disk).
1790 * \param[in] env execution environment
1791 * \param[in] lo lod object
1792 * \param[in] buf buffer containing LMV EA
1794 * \retval 0 on success
1795 * \retval negative if failed
1797 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1798 const struct lu_buf *buf)
1800 struct lod_thread_info *info = lod_env_info(env);
1801 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1802 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1803 struct dt_object **stripe;
1804 union lmv_mds_md *lmm = buf->lb_buf;
1805 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1806 struct lu_fid *fid = &info->lti_fid;
1811 LASSERT(mutex_is_locked(&lo->ldo_layout_mutex));
1813 /* XXX may be useless as not called for foreign LMV ?? */
1814 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_FOREIGN)
1817 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1818 lo->ldo_dir_slave_stripe = 1;
1822 if (!lmv_is_sane(lmv1))
1825 LASSERT(lo->ldo_stripe == NULL);
1826 OBD_ALLOC_PTR_ARRAY(stripe, le32_to_cpu(lmv1->lmv_stripe_count));
1830 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1831 struct dt_device *tgt_dt;
1832 struct dt_object *dto;
1833 int type = LU_SEQ_RANGE_ANY;
1836 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1837 if (!fid_is_sane(fid)) {
1842 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1846 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1847 tgt_dt = lod->lod_child;
1849 struct lod_tgt_desc *tgt;
1851 tgt = LTD_TGT(ltd, idx);
1853 GOTO(out, rc = -ESTALE);
1854 tgt_dt = tgt->ltd_tgt;
1857 dto = dt_locate_at(env, tgt_dt, fid,
1858 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1861 GOTO(out, rc = PTR_ERR(dto));
1866 lo->ldo_stripe = stripe;
1867 lo->ldo_dir_stripe_count = le32_to_cpu(lmv1->lmv_stripe_count);
1868 lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1869 lo->ldo_dir_layout_version = le32_to_cpu(lmv1->lmv_layout_version);
1870 lo->ldo_dir_migrate_offset = le32_to_cpu(lmv1->lmv_migrate_offset);
1871 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv1->lmv_migrate_hash);
1872 lo->ldo_dir_hash_type = le32_to_cpu(lmv1->lmv_hash_type);
1874 lod_striping_free_nolock(env, lo);
1880 * Declare create a striped directory.
1882 * Declare creating a striped directory with a given stripe pattern on the
1883 * specified MDTs. A striped directory is represented as a regular directory
1884 * - an index listing all the stripes. The stripes point back to the master
1885 * object with ".." and LinkEA. The master object gets LMV EA which
1886 * identifies it as a striped directory. The function allocates FIDs
1889 * \param[in] env execution environment
1890 * \param[in] dt object
1891 * \param[in] attr attributes to initialize the objects with
1892 * \param[in] dof type of objects to be created
1893 * \param[in] th transaction handle
1895 * \retval 0 on success
1896 * \retval negative if failed
1898 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1899 struct dt_object *dt,
1900 struct lu_attr *attr,
1901 struct dt_object_format *dof,
1904 struct lod_thread_info *info = lod_env_info(env);
1905 struct lu_buf lmv_buf;
1906 struct lu_buf slave_lmv_buf;
1907 struct lmv_mds_md_v1 *lmm;
1908 struct lmv_mds_md_v1 *slave_lmm = NULL;
1909 struct dt_insert_rec *rec = &info->lti_dt_rec;
1910 struct lod_object *lo = lod_dt_obj(dt);
1915 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1918 lmm = lmv_buf.lb_buf;
1920 OBD_ALLOC_PTR(slave_lmm);
1921 if (slave_lmm == NULL)
1922 GOTO(out, rc = -ENOMEM);
1924 lod_prep_slave_lmv_md(slave_lmm, lmm);
1925 slave_lmv_buf.lb_buf = slave_lmm;
1926 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1928 if (!dt_try_as_dir(env, dt_object_child(dt)))
1929 GOTO(out, rc = -EINVAL);
1931 rec->rec_type = S_IFDIR;
1932 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1933 struct dt_object *dto = lo->ldo_stripe[i];
1934 char *stripe_name = info->lti_key;
1935 struct lu_name *sname;
1936 struct linkea_data ldata = { NULL };
1937 struct lu_buf linkea_buf;
1939 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
1943 /* directory split skip create for existing stripes */
1944 if (!(lod_is_splitting(lo) && i < lo->ldo_dir_split_offset)) {
1945 rc = lod_sub_declare_create(env, dto, attr, NULL, dof,
1950 if (!dt_try_as_dir(env, dto))
1951 GOTO(out, rc = -EINVAL);
1953 rc = lod_sub_declare_ref_add(env, dto, th);
1957 rec->rec_fid = lu_object_fid(&dto->do_lu);
1958 rc = lod_sub_declare_insert(env, dto,
1959 (const struct dt_rec *)rec,
1960 (const struct dt_key *)dot,
1965 /* master stripe FID will be put to .. */
1966 rec->rec_fid = lu_object_fid(&dt->do_lu);
1967 rc = lod_sub_declare_insert(env, dto,
1968 (const struct dt_rec *)rec,
1969 (const struct dt_key *)dotdot,
1974 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1976 snprintf(stripe_name, sizeof(info->lti_key),
1978 PFID(lu_object_fid(&dto->do_lu)),
1981 snprintf(stripe_name, sizeof(info->lti_key),
1983 PFID(lu_object_fid(&dto->do_lu)), i);
1985 sname = lod_name_get(env, stripe_name,
1986 strlen(stripe_name));
1987 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1988 sname, lu_object_fid(&dt->do_lu));
1992 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1993 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1994 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
1995 XATTR_NAME_LINK, 0, th);
1999 rec->rec_fid = lu_object_fid(&dto->do_lu);
2000 rc = lod_sub_declare_insert(env, dt_object_child(dt),
2001 (const struct dt_rec *)rec,
2002 (const struct dt_key *)stripe_name, th);
2006 rc = lod_sub_declare_ref_add(env, dt_object_child(dt),
2012 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
2013 cfs_fail_val != i) {
2014 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
2016 slave_lmm->lmv_master_mdt_index =
2019 slave_lmm->lmv_master_mdt_index =
2021 rc = lod_sub_declare_xattr_set(env, dto, &slave_lmv_buf,
2022 XATTR_NAME_LMV, 0, th);
2028 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt),
2029 &lmv_buf, XATTR_NAME_LMV, 0, th);
2033 if (slave_lmm != NULL)
2034 OBD_FREE_PTR(slave_lmm);
2040 * Allocate a striping on a predefined set of MDTs.
2042 * Allocates new striping using the MDT index range provided by the data from
2043 * the lum_obejcts contained in the lmv_user_md passed to this method if
2044 * \a is_specific is true; or allocates new layout starting from MDT index in
2045 * lo->ldo_dir_stripe_offset. The exact order of MDTs is not important and
2046 * varies depending on MDT status. The number of stripes needed and stripe
2047 * offset are taken from the object. If that number cannot be met, then the
2048 * function returns an error and then it's the caller's responsibility to
2049 * release the stripes allocated. All the internal structures are protected,
2050 * but no concurrent allocation is allowed on the same objects.
2052 * \param[in] env execution environment for this thread
2053 * \param[in] lo LOD object
2054 * \param[out] stripes striping created
2055 * \param[out] mdt_indices MDT indices of striping created
2056 * \param[in] is_specific true if the MDTs are provided by lum; false if
2057 * only the starting MDT index is provided
2059 * \retval positive stripes allocated, including the first stripe allocated
2061 * \retval negative errno on failure
2063 static int lod_mdt_alloc_specific(const struct lu_env *env,
2064 struct lod_object *lo,
2065 struct dt_object **stripes,
2066 __u32 *mdt_indices, bool is_specific)
2068 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
2069 struct lu_tgt_descs *ltd = &lod->lod_mdt_descs;
2070 struct lu_tgt_desc *tgt = NULL;
2071 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2072 struct dt_device *tgt_dt = NULL;
2073 struct lu_fid fid = { 0 };
2074 struct dt_object *dto;
2076 u32 stripe_count = lo->ldo_dir_stripe_count;
2082 master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
2083 if (!is_specific && stripe_count > 1)
2084 /* Set the start index for the 2nd stripe allocation */
2085 mdt_indices[1] = (mdt_indices[0] + 1) %
2086 (lod->lod_remote_mdt_count + 1);
2088 for (; stripe_idx < stripe_count; stripe_idx++) {
2089 /* Try to find next avaible target */
2090 idx = mdt_indices[stripe_idx];
2091 for (j = 0; j < lod->lod_remote_mdt_count;
2092 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
2093 bool already_allocated = false;
2096 CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
2097 idx, lod->lod_remote_mdt_count + 1, stripe_idx);
2099 if (likely(!is_specific &&
2100 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
2101 /* check whether the idx already exists
2102 * in current allocated array */
2103 for (k = 0; k < stripe_idx; k++) {
2104 if (mdt_indices[k] == idx) {
2105 already_allocated = true;
2110 if (already_allocated)
2114 /* Sigh, this index is not in the bitmap, let's check
2115 * next available target */
2116 if (!test_bit(idx, ltd->ltd_tgt_bitmap) &&
2117 idx != master_index)
2120 if (idx == master_index) {
2121 /* Allocate the FID locally */
2122 tgt_dt = lod->lod_child;
2123 rc = dt_fid_alloc(env, tgt_dt, &fid, NULL,
2130 /* check the status of the OSP */
2131 tgt = LTD_TGT(ltd, idx);
2135 tgt_dt = tgt->ltd_tgt;
2136 if (!tgt->ltd_active)
2137 /* this OSP doesn't feel well */
2140 rc = dt_fid_alloc(env, tgt_dt, &fid, NULL, NULL);
2147 /* Can not allocate more stripes */
2148 if (j == lod->lod_remote_mdt_count) {
2149 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
2150 lod2obd(lod)->obd_name, stripe_count,
2155 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
2156 idx, stripe_idx, PFID(&fid));
2157 mdt_indices[stripe_idx] = idx;
2158 /* Set the start index for next stripe allocation */
2159 if (!is_specific && stripe_idx < stripe_count - 1) {
2161 * for large dir test, put all other slaves on one
2162 * remote MDT, otherwise we may save too many local
2163 * slave locks which will exceed RS_MAX_LOCKS.
2165 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)))
2167 mdt_indices[stripe_idx + 1] = (idx + 1) %
2168 (lod->lod_remote_mdt_count + 1);
2170 /* tgt_dt and fid must be ready after search avaible OSP
2171 * in the above loop */
2172 LASSERT(tgt_dt != NULL);
2173 LASSERT(fid_is_sane(&fid));
2175 /* fail a remote stripe FID allocation */
2176 if (stripe_idx && OBD_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_FID))
2179 dto = dt_locate_at(env, tgt_dt, &fid,
2180 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
2187 stripes[stripe_idx] = dto;
2193 for (j = 1; j < stripe_idx; j++) {
2194 LASSERT(stripes[j] != NULL);
2195 dt_object_put(env, stripes[j]);
2201 static int lod_prep_md_striped_create(const struct lu_env *env,
2202 struct dt_object *dt,
2203 struct lu_attr *attr,
2204 const struct lmv_user_md_v1 *lum,
2205 struct dt_object_format *dof,
2208 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
2209 struct lod_object *lo = lod_dt_obj(dt);
2210 struct dt_object **stripes;
2211 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2212 struct lu_fid fid = { 0 };
2219 /* The lum has been verifed in lod_verify_md_striping */
2220 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC ||
2221 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC);
2223 stripe_count = lo->ldo_dir_stripe_count;
2225 OBD_ALLOC_PTR_ARRAY(stripes, stripe_count);
2229 /* Allocate the first stripe locally */
2230 rc = dt_fid_alloc(env, lod->lod_child, &fid, NULL, NULL);
2234 stripes[0] = dt_locate_at(env, lod->lod_child, &fid,
2235 dt->do_lu.lo_dev->ld_site->ls_top_dev, &conf);
2236 if (IS_ERR(stripes[0]))
2237 GOTO(out, rc = PTR_ERR(stripes[0]));
2239 if (lo->ldo_dir_stripe_offset == LMV_OFFSET_DEFAULT) {
2240 lod_qos_statfs_update(env, lod, &lod->lod_mdt_descs);
2241 rc = lod_mdt_alloc_qos(env, lo, stripes, 1, stripe_count);
2243 rc = lod_mdt_alloc_rr(env, lo, stripes, 1,
2247 bool is_specific = false;
2249 OBD_ALLOC_PTR_ARRAY(idx_array, stripe_count);
2251 GOTO(out, rc = -ENOMEM);
2253 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) {
2255 for (i = 0; i < stripe_count; i++)
2257 le32_to_cpu(lum->lum_objects[i].lum_mds);
2260 /* stripe 0 is local */
2262 lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
2263 rc = lod_mdt_alloc_specific(env, lo, stripes, idx_array,
2265 OBD_FREE_PTR_ARRAY(idx_array, stripe_count);
2273 lo->ldo_dir_striped = 1;
2274 lo->ldo_stripe = stripes;
2275 lo->ldo_dir_stripe_count = rc;
2276 lo->ldo_dir_stripes_allocated = stripe_count;
2278 lo->ldo_dir_stripe_loaded = 1;
2280 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
2282 lod_striping_free(env, lo);
2288 if (!IS_ERR_OR_NULL(stripes[0]))
2289 dt_object_put(env, stripes[0]);
2290 for (i = 1; i < stripe_count; i++)
2291 LASSERT(!stripes[i]);
2292 OBD_FREE_PTR_ARRAY(stripes, stripe_count);
2299 * Alloc cached foreign LMV
2301 * \param[in] lo object
2302 * \param[in] size size of foreign LMV
2304 * \retval 0 on success
2305 * \retval negative if failed
2307 int lod_alloc_foreign_lmv(struct lod_object *lo, size_t size)
2309 OBD_ALLOC_LARGE(lo->ldo_foreign_lmv, size);
2310 if (lo->ldo_foreign_lmv == NULL)
2312 lo->ldo_foreign_lmv_size = size;
2313 lo->ldo_dir_is_foreign = 1;
2319 * Declare create striped md object.
2321 * The function declares intention to create a striped directory. This is a
2322 * wrapper for lod_prep_md_striped_create(). The only additional functionality
2323 * is to verify pattern \a lum_buf is good. Check that function for the details.
2325 * \param[in] env execution environment
2326 * \param[in] dt object
2327 * \param[in] attr attributes to initialize the objects with
2328 * \param[in] lum_buf a pattern specifying the number of stripes and
2330 * \param[in] dof type of objects to be created
2331 * \param[in] th transaction handle
2333 * \retval 0 on success
2334 * \retval negative if failed
2337 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
2338 struct dt_object *dt,
2339 struct lu_attr *attr,
2340 const struct lu_buf *lum_buf,
2341 struct dt_object_format *dof,
2344 struct lod_object *lo = lod_dt_obj(dt);
2345 struct lmv_user_md_v1 *lum = lum_buf->lb_buf;
2349 LASSERT(lum != NULL);
2351 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
2352 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
2353 (int)le32_to_cpu(lum->lum_stripe_offset));
2355 if (lo->ldo_dir_stripe_count == 0) {
2356 if (lo->ldo_dir_is_foreign) {
2357 rc = lod_alloc_foreign_lmv(lo, lum_buf->lb_len);
2360 memcpy(lo->ldo_foreign_lmv, lum, lum_buf->lb_len);
2361 lo->ldo_dir_stripe_loaded = 1;
2366 /* prepare dir striped objects */
2367 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2369 /* failed to create striping, let's reset
2370 * config so that others don't get confused */
2371 lod_striping_free(env, lo);
2379 * Set or replace striped directory layout, and LFSCK may set layout on a plain
2380 * directory, so don't check stripe count.
2382 * \param[in] env execution environment
2383 * \param[in] dt target object
2384 * \param[in] buf LMV buf which contains source stripe fids
2385 * \param[in] fl set or replace
2386 * \param[in] th transaction handle
2388 * \retval 0 on success
2389 * \retval negative if failed
2391 static int lod_dir_layout_set(const struct lu_env *env,
2392 struct dt_object *dt,
2393 const struct lu_buf *buf,
2397 struct dt_object *next = dt_object_child(dt);
2398 struct lod_object *lo = lod_dt_obj(dt);
2399 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2400 struct lmv_mds_md_v1 *lmv = buf->lb_buf;
2401 struct lmv_mds_md_v1 *slave_lmv;
2402 struct lu_buf slave_buf;
2408 if (!lmv_is_sane2(lmv))
2411 /* adjust hash for dir merge, which may not be set in user command */
2412 if (lmv_is_merging(lmv) && !lmv->lmv_migrate_hash)
2413 lmv->lmv_merge_hash =
2414 lod->lod_mdt_descs.ltd_lmv_desc.ld_pattern;
2416 LMV_DEBUG(D_INFO, lmv, "set");
2418 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LMV, fl, th);
2422 /* directory restripe may update stripe LMV directly */
2423 if (!lo->ldo_dir_stripe_count)
2426 lo->ldo_dir_hash_type = le32_to_cpu(lmv->lmv_hash_type);
2427 lo->ldo_dir_migrate_offset = le32_to_cpu(lmv->lmv_migrate_offset);
2428 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_migrate_hash);
2429 lo->ldo_dir_layout_version = le32_to_cpu(lmv->lmv_layout_version);
2431 OBD_ALLOC_PTR(slave_lmv);
2435 lod_prep_slave_lmv_md(slave_lmv, lmv);
2436 slave_buf.lb_buf = slave_lmv;
2437 slave_buf.lb_len = sizeof(*slave_lmv);
2439 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2440 if (!lo->ldo_stripe[i])
2443 if (!dt_object_exists(lo->ldo_stripe[i]))
2446 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], &slave_buf,
2447 XATTR_NAME_LMV, fl, th);
2452 OBD_FREE_PTR(slave_lmv);
2458 * Implementation of dt_object_operations::do_declare_xattr_set.
2460 * Used with regular (non-striped) objects. Basically it
2461 * initializes the striping information and applies the
2462 * change to all the stripes.
2464 * \see dt_object_operations::do_declare_xattr_set() in the API description
2467 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2468 struct dt_object *dt,
2469 const struct lu_buf *buf,
2470 const char *name, int fl,
2473 struct dt_object *next = dt_object_child(dt);
2474 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2475 struct lod_object *lo = lod_dt_obj(dt);
2480 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2481 struct lmv_user_md_v1 *lum;
2483 LASSERT(buf != NULL && buf->lb_buf != NULL);
2485 rc = lod_verify_md_striping(d, lum);
2488 } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2489 rc = lod_verify_striping(env, d, lo, buf, false);
2494 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2498 /* Note: Do not set LinkEA on sub-stripes, otherwise
2499 * it will confuse the fid2path process(see mdt_path_current()).
2500 * The linkEA between master and sub-stripes is set in
2501 * lod_xattr_set_lmv(). */
2502 if (strcmp(name, XATTR_NAME_LINK) == 0)
2505 /* set xattr to each stripes, if needed */
2506 rc = lod_striping_load(env, lo);
2510 if (lo->ldo_dir_stripe_count == 0)
2513 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2514 if (!lo->ldo_stripe[i])
2517 if (!dt_object_exists(lo->ldo_stripe[i]))
2520 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2530 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2531 struct lod_object *lo,
2532 struct dt_object *dt, struct thandle *th,
2533 int comp_idx, int stripe_idx,
2534 struct lod_obj_stripe_cb_data *data)
2536 struct lod_thread_info *info = lod_env_info(env);
2537 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
2538 struct filter_fid *ff = &info->lti_ff;
2539 struct lu_buf *buf = &info->lti_buf;
2543 buf->lb_len = sizeof(*ff);
2544 rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2552 * locd_buf is set if it's called by dir migration, which doesn't check
2555 if (data->locd_buf) {
2556 memset(ff, 0, sizeof(*ff));
2557 ff->ff_parent = *(struct lu_fid *)data->locd_buf->lb_buf;
2559 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
2561 if (lu_fid_eq(lod_object_fid(lo), &ff->ff_parent) &&
2562 ff->ff_layout.ol_comp_id == comp->llc_id)
2565 memset(ff, 0, sizeof(*ff));
2566 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2569 /* rewrite filter_fid */
2570 ff->ff_parent.f_ver = stripe_idx;
2571 ff->ff_layout.ol_stripe_size = comp->llc_stripe_size;
2572 ff->ff_layout.ol_stripe_count = comp->llc_stripe_count;
2573 ff->ff_layout.ol_comp_id = comp->llc_id;
2574 ff->ff_layout.ol_comp_start = comp->llc_extent.e_start;
2575 ff->ff_layout.ol_comp_end = comp->llc_extent.e_end;
2576 filter_fid_cpu_to_le(ff, ff, sizeof(*ff));
2578 if (data->locd_declare)
2579 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2580 LU_XATTR_REPLACE, th);
2582 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2583 LU_XATTR_REPLACE, th);
2589 * Reset parent FID on OST object
2591 * Replace parent FID with @dt object FID, which is only called during migration
2592 * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2593 * the FID is changed.
2595 * \param[in] env execution environment
2596 * \param[in] dt dt_object whose stripes's parent FID will be reset
2597 * \parem[in] th thandle
2598 * \param[in] declare if it is declare
2600 * \retval 0 if reset succeeds
2601 * \retval negative errno if reset fails
2603 static int lod_replace_parent_fid(const struct lu_env *env,
2604 struct dt_object *dt,
2605 const struct lu_buf *buf,
2606 struct thandle *th, bool declare)
2608 struct lod_object *lo = lod_dt_obj(dt);
2609 struct lod_thread_info *info = lod_env_info(env);
2610 struct filter_fid *ff;
2611 struct lod_obj_stripe_cb_data data = { { 0 } };
2615 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2617 /* set xattr to each stripes, if needed */
2618 rc = lod_striping_load(env, lo);
2622 if (!lod_obj_is_striped(dt))
2625 if (info->lti_ea_store_size < sizeof(*ff)) {
2626 rc = lod_ea_store_resize(info, sizeof(*ff));
2631 data.locd_declare = declare;
2632 data.locd_stripe_cb = lod_obj_stripe_replace_parent_fid_cb;
2633 data.locd_buf = buf;
2634 rc = lod_obj_for_each_stripe(env, lo, th, &data);
2639 __u16 lod_comp_entry_stripe_count(struct lod_object *lo,
2640 int comp_idx, bool is_dir)
2642 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2643 struct lod_layout_component *entry;
2648 entry = &lo->ldo_comp_entries[comp_idx];
2649 if (lod_comp_inited(entry))
2650 return entry->llc_stripe_count;
2651 else if ((__u16)-1 == entry->llc_stripe_count)
2652 return lod->lod_ost_count;
2654 return lod_get_stripe_count(lod, lo, comp_idx,
2655 entry->llc_stripe_count, false);
2658 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2660 int magic, size = 0, i;
2661 struct lod_layout_component *comp_entries;
2663 bool is_composite, is_foreign = false;
2666 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2667 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2669 lo->ldo_def_striping->lds_def_striping_is_composite;
2671 comp_cnt = lo->ldo_comp_cnt;
2672 comp_entries = lo->ldo_comp_entries;
2673 is_composite = lo->ldo_is_composite;
2674 is_foreign = lo->ldo_is_foreign;
2678 return lo->ldo_foreign_lov_size;
2680 LASSERT(comp_cnt != 0 && comp_entries != NULL);
2682 size = sizeof(struct lov_comp_md_v1) +
2683 sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2684 LASSERT(size % sizeof(__u64) == 0);
2687 for (i = 0; i < comp_cnt; i++) {
2690 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2691 stripe_count = lod_comp_entry_stripe_count(lo, i, is_dir);
2692 if (!is_dir && is_composite)
2693 lod_comp_shrink_stripe_count(&comp_entries[i],
2696 size += lov_user_md_size(stripe_count, magic);
2697 LASSERT(size % sizeof(__u64) == 0);
2703 * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2704 * the xattr value is binary lov_comp_md_v1 which contains component(s)
2707 * \param[in] env execution environment
2708 * \param[in] dt dt_object to add components on
2709 * \param[in] buf buffer contains components to be added
2710 * \parem[in] th thandle
2712 * \retval 0 on success
2713 * \retval negative errno on failure
2715 static int lod_declare_layout_add(const struct lu_env *env,
2716 struct dt_object *dt,
2717 const struct lu_buf *buf,
2720 struct lod_thread_info *info = lod_env_info(env);
2721 struct lod_layout_component *comp_array, *lod_comp, *old_array;
2722 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2723 struct dt_object *next = dt_object_child(dt);
2724 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
2725 struct lod_object *lo = lod_dt_obj(dt);
2726 struct lov_user_md_v3 *v3;
2727 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2729 int i, rc, array_cnt, old_array_cnt;
2732 LASSERT(lo->ldo_is_composite);
2734 if (lo->ldo_flr_state != LCM_FL_NONE)
2737 rc = lod_verify_striping(env, d, lo, buf, false);
2741 magic = comp_v1->lcm_magic;
2742 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2743 lustre_swab_lov_comp_md_v1(comp_v1);
2744 magic = comp_v1->lcm_magic;
2747 if (magic != LOV_USER_MAGIC_COMP_V1)
2750 array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2751 OBD_ALLOC_PTR_ARRAY(comp_array, array_cnt);
2752 if (comp_array == NULL)
2755 memcpy(comp_array, lo->ldo_comp_entries,
2756 sizeof(*comp_array) * lo->ldo_comp_cnt);
2758 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2759 struct lov_user_md_v1 *v1;
2760 struct lu_extent *ext;
2762 v1 = (struct lov_user_md *)((char *)comp_v1 +
2763 comp_v1->lcm_entries[i].lcme_offset);
2764 ext = &comp_v1->lcm_entries[i].lcme_extent;
2766 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2767 lod_comp->llc_extent.e_start = ext->e_start;
2768 lod_comp->llc_extent.e_end = ext->e_end;
2769 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2770 lod_comp->llc_flags = comp_v1->lcm_entries[i].lcme_flags;
2772 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2773 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2774 lod_adjust_stripe_info(lod_comp, desc, 0);
2776 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2777 v3 = (struct lov_user_md_v3 *) v1;
2778 if (v3->lmm_pool_name[0] != '\0') {
2779 rc = lod_set_pool(&lod_comp->llc_pool,
2787 old_array = lo->ldo_comp_entries;
2788 old_array_cnt = lo->ldo_comp_cnt;
2790 lo->ldo_comp_entries = comp_array;
2791 lo->ldo_comp_cnt = array_cnt;
2793 /* No need to increase layout generation here, it will be increased
2794 * later when generating component ID for the new components */
2796 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2797 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2798 XATTR_NAME_LOV, 0, th);
2800 lo->ldo_comp_entries = old_array;
2801 lo->ldo_comp_cnt = old_array_cnt;
2805 OBD_FREE_PTR_ARRAY(old_array, old_array_cnt);
2807 LASSERT(lo->ldo_mirror_count == 1);
2808 lo->ldo_mirrors[0].lme_end = array_cnt - 1;
2813 for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2814 lod_comp = &comp_array[i];
2815 if (lod_comp->llc_pool != NULL) {
2816 OBD_FREE(lod_comp->llc_pool,
2817 strlen(lod_comp->llc_pool) + 1);
2818 lod_comp->llc_pool = NULL;
2821 OBD_FREE_PTR_ARRAY(comp_array, array_cnt);
2826 * lod_last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2827 * @mirror_id: Mirror id to be checked.
2830 * This function checks if a mirror with specified @mirror_id is the last
2831 * non-stale mirror of a LOD object @lo.
2833 * Return: true or false.
2836 bool lod_last_non_stale_mirror(__u16 mirror_id, struct lod_object *lo)
2838 struct lod_layout_component *lod_comp;
2839 bool has_stale_flag;
2842 for (i = 0; i < lo->ldo_mirror_count; i++) {
2843 if (lo->ldo_mirrors[i].lme_id == mirror_id ||
2844 lo->ldo_mirrors[i].lme_stale)
2847 has_stale_flag = false;
2848 lod_foreach_mirror_comp(lod_comp, lo, i) {
2849 if (lod_comp->llc_flags & LCME_FL_STALE) {
2850 has_stale_flag = true;
2854 if (!has_stale_flag)
2862 * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2863 * the '$field' can only be 'flags' now. The xattr value is binary
2864 * lov_comp_md_v1 which contains the component ID(s) and the value of
2865 * the field to be modified.
2866 * Please update allowed_lustre_lov macro if $field groks more values
2869 * \param[in] env execution environment
2870 * \param[in] dt dt_object to be modified
2871 * \param[in] op operation string, like "set.flags"
2872 * \param[in] buf buffer contains components to be set
2873 * \parem[in] th thandle
2875 * \retval 0 on success
2876 * \retval negative errno on failure
2878 static int lod_declare_layout_set(const struct lu_env *env,
2879 struct dt_object *dt,
2880 char *op, const struct lu_buf *buf,
2883 struct lod_layout_component *lod_comp;
2884 struct lod_thread_info *info = lod_env_info(env);
2885 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2886 struct lod_object *lo = lod_dt_obj(dt);
2887 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2890 bool changed = false;
2893 /* Please update allowed_lustre_lov macro if op
2894 * groks more values in the future
2896 if (strcmp(op, "set.flags") != 0) {
2897 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2898 lod2obd(d)->obd_name, op);
2902 magic = comp_v1->lcm_magic;
2903 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2904 lustre_swab_lov_comp_md_v1(comp_v1);
2905 magic = comp_v1->lcm_magic;
2908 if (magic != LOV_USER_MAGIC_COMP_V1)
2911 if (comp_v1->lcm_entry_count == 0) {
2912 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2913 lod2obd(d)->obd_name);
2917 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2918 __u32 id = comp_v1->lcm_entries[i].lcme_id;
2919 __u32 flags = comp_v1->lcm_entries[i].lcme_flags;
2920 __u32 mirror_flag = flags & LCME_MIRROR_FLAGS;
2921 __u16 mirror_id = mirror_id_of(id);
2922 bool neg = flags & LCME_FL_NEG;
2924 if (flags & LCME_FL_INIT) {
2926 lod_striping_free(env, lo);
2930 flags &= ~(LCME_MIRROR_FLAGS | LCME_FL_NEG);
2931 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2932 lod_comp = &lo->ldo_comp_entries[j];
2934 /* lfs only put one flag in each entry */
2935 if ((flags && id != lod_comp->llc_id) ||
2936 (mirror_flag && mirror_id !=
2937 mirror_id_of(lod_comp->llc_id)))
2942 lod_comp->llc_flags &= ~flags;
2944 lod_comp->llc_flags &= ~mirror_flag;
2947 if ((flags & LCME_FL_STALE) &&
2948 lod_last_non_stale_mirror(mirror_id,
2951 lod_comp->llc_flags |= flags;
2954 lod_comp->llc_flags |= mirror_flag;
2955 if (mirror_flag & LCME_FL_NOSYNC)
2956 lod_comp->llc_timestamp =
2957 ktime_get_real_seconds();
2965 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2966 lod2obd(d)->obd_name);
2970 lod_obj_inc_layout_gen(lo);
2972 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2973 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), &info->lti_buf,
2974 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
2979 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2980 * and the xattr value is a unique component ID or a special lcme_id.
2982 * \param[in] env execution environment
2983 * \param[in] dt dt_object to be operated on
2984 * \param[in] buf buffer contains component ID or lcme_id
2985 * \parem[in] th thandle
2987 * \retval 0 on success
2988 * \retval negative errno on failure
2990 static int lod_declare_layout_del(const struct lu_env *env,
2991 struct dt_object *dt,
2992 const struct lu_buf *buf,
2995 struct lod_thread_info *info = lod_env_info(env);
2996 struct dt_object *next = dt_object_child(dt);
2997 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2998 struct lod_object *lo = lod_dt_obj(dt);
2999 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3000 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
3001 __u32 magic, id, flags, neg_flags = 0;
3005 LASSERT(lo->ldo_is_composite);
3007 if (lo->ldo_flr_state != LCM_FL_NONE)
3010 magic = comp_v1->lcm_magic;
3011 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3012 lustre_swab_lov_comp_md_v1(comp_v1);
3013 magic = comp_v1->lcm_magic;
3016 if (magic != LOV_USER_MAGIC_COMP_V1)
3019 id = comp_v1->lcm_entries[0].lcme_id;
3020 flags = comp_v1->lcm_entries[0].lcme_flags;
3022 if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
3023 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
3024 lod2obd(d)->obd_name, id, flags);
3028 if (id != LCME_ID_INVAL && flags != 0) {
3029 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
3030 lod2obd(d)->obd_name);
3034 if (id == LCME_ID_INVAL && !flags) {
3035 CDEBUG(D_LAYOUT, "%s: no id or flags specified.\n",
3036 lod2obd(d)->obd_name);
3040 if (flags & LCME_FL_NEG) {
3041 neg_flags = flags & ~LCME_FL_NEG;
3045 left = lo->ldo_comp_cnt;
3049 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3050 struct lod_layout_component *lod_comp;
3052 lod_comp = &lo->ldo_comp_entries[i];
3054 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
3056 else if (flags && !(flags & lod_comp->llc_flags))
3058 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
3061 if (left != (i + 1)) {
3062 CDEBUG(D_LAYOUT, "%s: this deletion will create "
3063 "a hole.\n", lod2obd(d)->obd_name);
3068 /* Mark the component as deleted */
3069 lod_comp->llc_id = LCME_ID_INVAL;
3071 /* Not instantiated component */
3072 if (lod_comp->llc_stripe == NULL)
3075 LASSERT(lod_comp->llc_stripe_count > 0);
3076 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
3077 struct dt_object *obj = lod_comp->llc_stripe[j];
3081 rc = lod_sub_declare_destroy(env, obj, th);
3087 LASSERTF(left >= 0, "left = %d\n", left);
3088 if (left == lo->ldo_comp_cnt) {
3089 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
3090 lod2obd(d)->obd_name, id);
3094 memset(attr, 0, sizeof(*attr));
3095 attr->la_valid = LA_SIZE;
3096 rc = lod_sub_declare_attr_set(env, next, attr, th);
3101 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
3102 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
3103 XATTR_NAME_LOV, 0, th);
3105 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
3112 * Declare layout add/set/del operations issued by special xattr names:
3114 * XATTR_LUSTRE_LOV.add add component(s) to existing file
3115 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
3116 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
3118 * \param[in] env execution environment
3119 * \param[in] dt object
3120 * \param[in] name name of xattr
3121 * \param[in] buf lu_buf contains xattr value
3122 * \param[in] th transaction handle
3124 * \retval 0 on success
3125 * \retval negative if failed
3127 static int lod_declare_modify_layout(const struct lu_env *env,
3128 struct dt_object *dt,
3130 const struct lu_buf *buf,
3133 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3134 struct lod_object *lo = lod_dt_obj(dt);
3136 int rc, len = strlen(XATTR_LUSTRE_LOV);
3139 LASSERT(dt_object_exists(dt));
3141 if (strlen(name) <= len || name[len] != '.') {
3142 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
3143 lod2obd(d)->obd_name, name);
3148 rc = lod_striping_load(env, lo);
3152 /* the layout to be modified must be a composite layout */
3153 if (!lo->ldo_is_composite) {
3154 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
3155 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3156 GOTO(unlock, rc = -EINVAL);
3159 op = (char *)name + len;
3160 if (strcmp(op, "add") == 0) {
3161 rc = lod_declare_layout_add(env, dt, buf, th);
3162 } else if (strcmp(op, "del") == 0) {
3163 rc = lod_declare_layout_del(env, dt, buf, th);
3164 } else if (strncmp(op, "set", strlen("set")) == 0) {
3165 rc = lod_declare_layout_set(env, dt, op, buf, th);
3167 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
3168 lod2obd(d)->obd_name, name);
3169 GOTO(unlock, rc = -ENOTSUPP);
3173 lod_striping_free(env, lo);
3179 * Convert a plain file lov_mds_md to a composite layout.
3181 * \param[in,out] info the thread info::lti_ea_store buffer contains little
3182 * endian plain file layout
3184 * \retval 0 on success, <0 on failure
3186 static int lod_layout_convert(struct lod_thread_info *info)
3188 struct lov_mds_md *lmm = info->lti_ea_store;
3189 struct lov_mds_md *lmm_save;
3190 struct lov_comp_md_v1 *lcm;
3191 struct lov_comp_md_entry_v1 *lcme;
3197 /* realloc buffer to a composite layout which contains one component */
3198 blob_size = lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
3199 le32_to_cpu(lmm->lmm_magic));
3200 size = sizeof(*lcm) + sizeof(*lcme) + blob_size;
3202 OBD_ALLOC_LARGE(lmm_save, blob_size);
3204 GOTO(out, rc = -ENOMEM);
3206 memcpy(lmm_save, lmm, blob_size);
3208 if (info->lti_ea_store_size < size) {
3209 rc = lod_ea_store_resize(info, size);
3214 lcm = info->lti_ea_store;
3215 memset(lcm, 0, sizeof(*lcm) + sizeof(*lcme));
3216 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
3217 lcm->lcm_size = cpu_to_le32(size);
3218 lcm->lcm_layout_gen = cpu_to_le32(le16_to_cpu(
3219 lmm_save->lmm_layout_gen));
3220 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
3221 lcm->lcm_entry_count = cpu_to_le16(1);
3223 lcme = &lcm->lcm_entries[0];
3224 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
3225 lcme->lcme_extent.e_start = 0;
3226 lcme->lcme_extent.e_end = cpu_to_le64(OBD_OBJECT_EOF);
3227 lcme->lcme_offset = cpu_to_le32(sizeof(*lcm) + sizeof(*lcme));
3228 lcme->lcme_size = cpu_to_le32(blob_size);
3230 memcpy((char *)lcm + lcme->lcme_offset, (char *)lmm_save, blob_size);
3235 OBD_FREE_LARGE(lmm_save, blob_size);
3240 * Merge layouts to form a mirrored file.
3242 static int lod_declare_layout_merge(const struct lu_env *env,
3243 struct dt_object *dt, const struct lu_buf *mbuf,
3246 struct lod_thread_info *info = lod_env_info(env);
3247 struct lu_buf *buf = &info->lti_buf;
3248 struct lod_object *lo = lod_dt_obj(dt);
3249 struct lov_comp_md_v1 *lcm;
3250 struct lov_comp_md_v1 *cur_lcm;
3251 struct lov_comp_md_v1 *merge_lcm;
3252 struct lov_comp_md_entry_v1 *lcme;
3253 struct lov_mds_md_v1 *lmm;
3256 __u16 cur_entry_count;
3257 __u16 merge_entry_count;
3259 __u16 mirror_id = 0;
3266 merge_lcm = mbuf->lb_buf;
3267 if (mbuf->lb_len < sizeof(*merge_lcm))
3270 /* must be an existing layout from disk */
3271 if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
3274 merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
3276 /* do not allow to merge two mirrored files */
3277 if (le16_to_cpu(merge_lcm->lcm_mirror_count))
3280 /* verify the target buffer */
3281 rc = lod_get_lov_ea(env, lo);
3283 RETURN(rc ? : -ENODATA);
3285 cur_lcm = info->lti_ea_store;
3286 switch (le32_to_cpu(cur_lcm->lcm_magic)) {
3289 rc = lod_layout_convert(info);
3291 case LOV_MAGIC_COMP_V1:
3301 /* info->lti_ea_store could be reallocated in lod_layout_convert() */
3302 cur_lcm = info->lti_ea_store;
3303 cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
3305 /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
3306 mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
3307 if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
3310 /* size of new layout */
3311 size = le32_to_cpu(cur_lcm->lcm_size) +
3312 le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
3314 memset(buf, 0, sizeof(*buf));
3315 lu_buf_alloc(buf, size);
3316 if (buf->lb_buf == NULL)
3320 memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
3322 offset = sizeof(*lcm) +
3323 sizeof(*lcme) * (cur_entry_count + merge_entry_count);
3324 for (i = 0; i < cur_entry_count; i++) {
3325 struct lov_comp_md_entry_v1 *cur_lcme;
3327 lcme = &lcm->lcm_entries[i];
3328 cur_lcme = &cur_lcm->lcm_entries[i];
3330 lcme->lcme_offset = cpu_to_le32(offset);
3331 memcpy((char *)lcm + offset,
3332 (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
3333 le32_to_cpu(lcme->lcme_size));
3335 offset += le32_to_cpu(lcme->lcme_size);
3337 if (mirror_count == 1 &&
3338 mirror_id_of(le32_to_cpu(lcme->lcme_id)) == 0) {
3339 /* Add mirror from a non-flr file, create new mirror ID.
3340 * Otherwise, keep existing mirror's component ID, used
3341 * for mirror extension.
3343 id = pflr_id(1, i + 1);
3344 lcme->lcme_id = cpu_to_le32(id);
3347 id = max(le32_to_cpu(lcme->lcme_id), id);
3350 mirror_id = mirror_id_of(id) + 1;
3352 /* check if first entry in new layout is DOM */
3353 lmm = (struct lov_mds_md_v1 *)((char *)merge_lcm +
3354 merge_lcm->lcm_entries[0].lcme_offset);
3355 merge_has_dom = lov_pattern(le32_to_cpu(lmm->lmm_pattern)) ==
3358 for (i = 0; i < merge_entry_count; i++) {
3359 struct lov_comp_md_entry_v1 *merge_lcme;
3361 merge_lcme = &merge_lcm->lcm_entries[i];
3362 lcme = &lcm->lcm_entries[cur_entry_count + i];
3364 *lcme = *merge_lcme;
3365 lcme->lcme_offset = cpu_to_le32(offset);
3366 if (merge_has_dom && i == 0)
3367 lcme->lcme_flags |= cpu_to_le32(LCME_FL_STALE);
3369 id = pflr_id(mirror_id, i + 1);
3370 lcme->lcme_id = cpu_to_le32(id);
3372 memcpy((char *)lcm + offset,
3373 (char *)merge_lcm + le32_to_cpu(merge_lcme->lcme_offset),
3374 le32_to_cpu(lcme->lcme_size));
3376 offset += le32_to_cpu(lcme->lcme_size);
3379 /* fixup layout information */
3380 lod_obj_inc_layout_gen(lo);
3381 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3382 lcm->lcm_size = cpu_to_le32(size);
3383 lcm->lcm_entry_count = cpu_to_le16(cur_entry_count + merge_entry_count);
3384 lcm->lcm_mirror_count = cpu_to_le16(mirror_count);
3385 if ((le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK) == LCM_FL_NONE)
3386 lcm->lcm_flags = cpu_to_le32(LCM_FL_RDONLY);
3388 rc = lod_striping_reload(env, lo, buf);
3392 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), buf,
3393 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3401 * Split layouts, just set the LOVEA with the layout from mbuf.
3403 static int lod_declare_layout_split(const struct lu_env *env,
3404 struct dt_object *dt, const struct lu_buf *mbuf,
3407 struct lod_object *lo = lod_dt_obj(dt);
3408 struct lov_comp_md_v1 *lcm = mbuf->lb_buf;
3412 rc = lod_striping_reload(env, lo, mbuf);
3416 lod_obj_inc_layout_gen(lo);
3417 /* fix on-disk layout gen */
3418 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3420 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), mbuf,
3421 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3426 * Implementation of dt_object_operations::do_declare_xattr_set.
3428 * \see dt_object_operations::do_declare_xattr_set() in the API description
3431 * the extension to the API:
3432 * - declaring LOVEA requests striping creation
3433 * - LU_XATTR_REPLACE means layout swap
3435 static int lod_declare_xattr_set(const struct lu_env *env,
3436 struct dt_object *dt,
3437 const struct lu_buf *buf,
3438 const char *name, int fl,
3441 struct dt_object *next = dt_object_child(dt);
3442 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3447 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
3448 if ((S_ISREG(mode) || mode == 0) &&
3449 !(fl & (LU_XATTR_REPLACE | LU_XATTR_MERGE | LU_XATTR_SPLIT)) &&
3450 (strcmp(name, XATTR_NAME_LOV) == 0 ||
3451 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
3453 * this is a request to create object's striping.
3455 * allow to declare predefined striping on a new (!mode) object
3456 * which is supposed to be replay of regular file creation
3457 * (when LOV setting is declared)
3459 * LU_XATTR_REPLACE is set to indicate a layout swap
3461 if (dt_object_exists(dt)) {
3462 rc = dt_attr_get(env, next, attr);
3466 memset(attr, 0, sizeof(*attr));
3467 attr->la_valid = LA_TYPE | LA_MODE;
3468 attr->la_mode = S_IFREG;
3470 rc = lod_declare_striped_create(env, dt, attr, buf, th);
3471 } else if (fl & LU_XATTR_MERGE) {
3472 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3473 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3474 rc = lod_declare_layout_merge(env, dt, buf, th);
3475 } else if (fl & LU_XATTR_SPLIT) {
3476 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3477 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3478 rc = lod_declare_layout_split(env, dt, buf, th);
3479 } else if (S_ISREG(mode) &&
3480 strlen(name) >= sizeof(XATTR_LUSTRE_LOV) + 3 &&
3481 allowed_lustre_lov(name)) {
3483 * this is a request to modify object's striping.
3484 * add/set/del component(s).
3486 if (!dt_object_exists(dt))
3489 rc = lod_declare_modify_layout(env, dt, name, buf, th);
3490 } else if (S_ISDIR(mode)) {
3491 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
3492 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3493 rc = lod_replace_parent_fid(env, dt, buf, th, true);
3495 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
3502 * Apply xattr changes to the object.
3504 * Applies xattr changes to the object and the stripes if the latter exist.
3506 * \param[in] env execution environment
3507 * \param[in] dt object
3508 * \param[in] buf buffer pointing to the new value of xattr
3509 * \param[in] name name of xattr
3510 * \param[in] fl flags
3511 * \param[in] th transaction handle
3513 * \retval 0 on success
3514 * \retval negative if failed
3516 static int lod_xattr_set_internal(const struct lu_env *env,
3517 struct dt_object *dt,
3518 const struct lu_buf *buf,
3519 const char *name, int fl,
3522 struct dt_object *next = dt_object_child(dt);
3523 struct lod_object *lo = lod_dt_obj(dt);
3528 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3529 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3532 /* Note: Do not set LinkEA on sub-stripes, otherwise
3533 * it will confuse the fid2path process(see mdt_path_current()).
3534 * The linkEA between master and sub-stripes is set in
3535 * lod_xattr_set_lmv(). */
3536 if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
3539 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3540 if (!lo->ldo_stripe[i])
3543 if (!dt_object_exists(lo->ldo_stripe[i]))
3546 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
3556 * Delete an extended attribute.
3558 * Deletes specified xattr from the object and the stripes if the latter exist.
3560 * \param[in] env execution environment
3561 * \param[in] dt object
3562 * \param[in] name name of xattr
3563 * \param[in] th transaction handle
3565 * \retval 0 on success
3566 * \retval negative if failed
3568 static int lod_xattr_del_internal(const struct lu_env *env,
3569 struct dt_object *dt,
3570 const char *name, struct thandle *th)
3572 struct dt_object *next = dt_object_child(dt);
3573 struct lod_object *lo = lod_dt_obj(dt);
3578 rc = lod_sub_xattr_del(env, next, name, th);
3579 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3582 if (lo->ldo_dir_stripe_count == 0)
3585 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3586 LASSERT(lo->ldo_stripe[i]);
3588 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3597 * Set default striping on a directory.
3599 * Sets specified striping on a directory object unless it matches the default
3600 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3601 * EA. This striping will be used when regular file is being created in this
3604 * \param[in] env execution environment
3605 * \param[in] dt the striped object
3606 * \param[in] buf buffer with the striping
3607 * \param[in] name name of EA
3608 * \param[in] fl xattr flag (see OSD API description)
3609 * \param[in] th transaction handle
3611 * \retval 0 on success
3612 * \retval negative if failed
3614 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
3615 struct dt_object *dt,
3616 const struct lu_buf *buf,
3617 const char *name, int fl,
3620 struct lov_user_md_v1 *lum;
3621 struct lov_user_md_v3 *v3 = NULL;
3622 const char *pool_name = NULL;
3627 LASSERT(buf != NULL && buf->lb_buf != NULL);
3630 switch (lum->lmm_magic) {
3631 case LOV_USER_MAGIC_SPECIFIC:
3632 case LOV_USER_MAGIC_V3:
3634 if (v3->lmm_pool_name[0] != '\0')
3635 pool_name = v3->lmm_pool_name;
3637 case LOV_USER_MAGIC_V1:
3638 /* if { size, offset, count } = { 0, -1, 0 } and no pool
3639 * (i.e. all default values specified) then delete default
3640 * striping from dir. */
3642 "set default striping: sz %u # %u offset %d %s %s\n",
3643 (unsigned)lum->lmm_stripe_size,
3644 (unsigned)lum->lmm_stripe_count,
3645 (int)lum->lmm_stripe_offset,
3646 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
3648 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
3649 lum->lmm_stripe_count,
3650 lum->lmm_stripe_offset,
3653 case LOV_USER_MAGIC_COMP_V1:
3655 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lum;
3656 struct lov_comp_md_entry_v1 *lcme;
3659 comp_cnt = le16_to_cpu(lcm->lcm_entry_count);
3660 for (i = 0; i < comp_cnt; i++) {
3661 lcme = &lcm->lcm_entries[i];
3662 if (lcme->lcme_flags & cpu_to_le32(LCME_FL_EXTENSION)) {
3663 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_SEL);
3672 CERROR("Invalid magic %x\n", lum->lmm_magic);
3677 rc = lod_xattr_del_internal(env, dt, name, th);
3681 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3688 * Set default striping on a directory object.
3690 * Sets specified striping on a directory object unless it matches the default
3691 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3692 * EA. This striping will be used when a new directory is being created in the
3695 * \param[in] env execution environment
3696 * \param[in] dt the striped object
3697 * \param[in] buf buffer with the striping
3698 * \param[in] name name of EA
3699 * \param[in] fl xattr flag (see OSD API description)
3700 * \param[in] th transaction handle
3702 * \retval 0 on success
3703 * \retval negative if failed
3705 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
3706 struct dt_object *dt,
3707 const struct lu_buf *buf,
3708 const char *name, int fl,
3711 struct lmv_user_md_v1 *lum;
3716 LASSERT(buf != NULL && buf->lb_buf != NULL);
3720 "set default stripe_count # %u stripe_offset %d hash %u\n",
3721 le32_to_cpu(lum->lum_stripe_count),
3722 (int)le32_to_cpu(lum->lum_stripe_offset),
3723 le32_to_cpu(lum->lum_hash_type));
3725 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
3726 le32_to_cpu(lum->lum_stripe_offset)) &&
3727 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
3728 rc = lod_xattr_del_internal(env, dt, name, th);
3732 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3741 * Turn directory into a striped directory.
3743 * During replay the client sends the striping created before MDT
3744 * failure, then the layer above LOD sends this defined striping
3745 * using ->do_xattr_set(), so LOD uses this method to replay creation
3746 * of the stripes. Notice the original information for the striping
3747 * (#stripes, FIDs, etc) was transferred in declare path.
3749 * \param[in] env execution environment
3750 * \param[in] dt the striped object
3751 * \param[in] buf not used currently
3752 * \param[in] name not used currently
3753 * \param[in] fl xattr flag (see OSD API description)
3754 * \param[in] th transaction handle
3756 * \retval 0 on success
3757 * \retval negative if failed
3759 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
3760 const struct lu_buf *buf, const char *name,
3761 int fl, struct thandle *th)
3763 struct lod_object *lo = lod_dt_obj(dt);
3764 struct lod_thread_info *info = lod_env_info(env);
3765 struct lu_attr *attr = &info->lti_attr;
3766 struct dt_object_format *dof = &info->lti_format;
3767 struct lu_buf lmv_buf;
3768 struct lu_buf slave_lmv_buf;
3769 struct lmv_mds_md_v1 *lmm;
3770 struct lmv_mds_md_v1 *slave_lmm = NULL;
3771 struct dt_insert_rec *rec = &info->lti_dt_rec;
3776 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3779 /* The stripes are supposed to be allocated in declare phase,
3780 * if there are no stripes being allocated, it will skip */
3781 if (lo->ldo_dir_stripe_count == 0) {
3782 if (lo->ldo_dir_is_foreign) {
3783 rc = lod_sub_xattr_set(env, dt_object_child(dt), buf,
3784 XATTR_NAME_LMV, fl, th);
3791 rc = dt_attr_get(env, dt_object_child(dt), attr);
3795 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_FLAGS |
3796 LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
3797 dof->dof_type = DFT_DIR;
3799 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
3802 lmm = lmv_buf.lb_buf;
3804 OBD_ALLOC_PTR(slave_lmm);
3805 if (slave_lmm == NULL)
3808 lod_prep_slave_lmv_md(slave_lmm, lmm);
3809 slave_lmv_buf.lb_buf = slave_lmm;
3810 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
3812 rec->rec_type = S_IFDIR;
3813 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3814 struct dt_object *dto = lo->ldo_stripe[i];
3815 char *stripe_name = info->lti_key;
3816 struct lu_name *sname;
3817 struct linkea_data ldata = { NULL };
3818 struct lu_buf linkea_buf;
3820 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
3824 /* fail a remote stripe creation */
3825 if (i && OBD_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_CREATE))
3828 /* don't create stripe if:
3829 * 1. it's source stripe of migrating directory
3830 * 2. it's existed stripe of splitting directory
3832 if ((lod_is_migrating(lo) && i >= lo->ldo_dir_migrate_offset) ||
3833 (lod_is_splitting(lo) && i < lo->ldo_dir_split_offset)) {
3834 if (!dt_object_exists(dto))
3835 GOTO(out, rc = -EINVAL);
3837 dt_write_lock(env, dto, DT_TGT_CHILD);
3838 rc = lod_sub_create(env, dto, attr, NULL, dof, th);
3840 dt_write_unlock(env, dto);
3844 rc = lod_sub_ref_add(env, dto, th);
3845 dt_write_unlock(env, dto);
3849 rec->rec_fid = lu_object_fid(&dto->do_lu);
3850 rc = lod_sub_insert(env, dto,
3851 (const struct dt_rec *)rec,
3852 (const struct dt_key *)dot, th);
3857 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
3858 cfs_fail_val != i) {
3859 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
3861 slave_lmm->lmv_master_mdt_index =
3864 slave_lmm->lmv_master_mdt_index =
3867 rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
3868 XATTR_NAME_LMV, 0, th);
3873 /* don't insert stripe if it's existed stripe of splitting
3874 * directory (this directory is striped).
3875 * NB, plain directory will insert itself as the first
3878 if (lod_is_splitting(lo) && lo->ldo_dir_split_offset > 1 &&
3879 lo->ldo_dir_split_offset > i)
3882 rec->rec_fid = lu_object_fid(&dt->do_lu);
3883 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
3884 (const struct dt_key *)dotdot, th);
3888 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
3890 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3891 PFID(lu_object_fid(&dto->do_lu)), i + 1);
3893 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3894 PFID(lu_object_fid(&dto->do_lu)), i);
3896 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
3897 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
3898 sname, lu_object_fid(&dt->do_lu));
3902 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
3903 linkea_buf.lb_len = ldata.ld_leh->leh_len;
3904 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
3905 XATTR_NAME_LINK, 0, th);
3909 rec->rec_fid = lu_object_fid(&dto->do_lu);
3910 rc = lod_sub_insert(env, dt_object_child(dt),
3911 (const struct dt_rec *)rec,
3912 (const struct dt_key *)stripe_name, th);
3916 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
3921 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
3922 rc = lod_sub_xattr_set(env, dt_object_child(dt),
3923 &lmv_buf, XATTR_NAME_LMV, fl, th);
3925 if (slave_lmm != NULL)
3926 OBD_FREE_PTR(slave_lmm);
3932 * Helper function to declare/execute creation of a striped directory
3934 * Called in declare/create object path, prepare striping for a directory
3935 * and prepare defaults data striping for the objects to be created in
3936 * that directory. Notice the function calls "declaration" or "execution"
3937 * methods depending on \a declare param. This is a consequence of the
3938 * current approach while we don't have natural distributed transactions:
3939 * we basically execute non-local updates in the declare phase. So, the
3940 * arguments for the both phases are the same and this is the reason for
3941 * this function to exist.
3943 * \param[in] env execution environment
3944 * \param[in] dt object
3945 * \param[in] attr attributes the stripes will be created with
3946 * \param[in] lmu lmv_user_md if MDT indices are specified
3947 * \param[in] dof format of stripes (see OSD API description)
3948 * \param[in] th transaction handle
3949 * \param[in] declare where to call "declare" or "execute" methods
3951 * \retval 0 on success
3952 * \retval negative if failed
3954 static int lod_dir_striping_create_internal(const struct lu_env *env,
3955 struct dt_object *dt,
3956 struct lu_attr *attr,
3957 const struct lu_buf *lmu,
3958 struct dt_object_format *dof,
3962 struct lod_thread_info *info = lod_env_info(env);
3963 struct lod_object *lo = lod_dt_obj(dt);
3964 const struct lod_default_striping *lds = lo->ldo_def_striping;
3968 LASSERT(ergo(lds != NULL,
3969 lds->lds_def_striping_set ||
3970 lds->lds_dir_def_striping_set));
3972 if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
3973 lo->ldo_dir_stripe_offset)) {
3975 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3976 int stripe_count = lo->ldo_dir_stripe_count;
3978 if (info->lti_ea_store_size < sizeof(*v1)) {
3979 rc = lod_ea_store_resize(info, sizeof(*v1));
3982 v1 = info->lti_ea_store;
3985 memset(v1, 0, sizeof(*v1));
3986 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3987 v1->lum_stripe_count = cpu_to_le32(stripe_count);
3988 v1->lum_stripe_offset =
3989 cpu_to_le32(lo->ldo_dir_stripe_offset);
3991 info->lti_buf.lb_buf = v1;
3992 info->lti_buf.lb_len = sizeof(*v1);
3993 lmu = &info->lti_buf;
3997 rc = lod_declare_xattr_set_lmv(env, dt, attr, lmu, dof,
4000 rc = lod_xattr_set_lmv(env, dt, lmu, XATTR_NAME_LMV, 0,
4005 /* foreign LMV EA case */
4007 struct lmv_foreign_md *lfm = lmu->lb_buf;
4009 if (lfm->lfm_magic == LMV_MAGIC_FOREIGN) {
4010 rc = lod_declare_xattr_set_lmv(env, dt, attr,
4014 if (lo->ldo_dir_is_foreign) {
4015 LASSERT(lo->ldo_foreign_lmv != NULL &&
4016 lo->ldo_foreign_lmv_size > 0);
4017 info->lti_buf.lb_buf = lo->ldo_foreign_lmv;
4018 info->lti_buf.lb_len = lo->ldo_foreign_lmv_size;
4019 lmu = &info->lti_buf;
4020 rc = lod_xattr_set_lmv(env, dt, lmu,
4021 XATTR_NAME_LMV, 0, th);
4026 /* Transfer default LMV striping from the parent */
4027 if (lds != NULL && lds->lds_dir_def_striping_set &&
4028 !(LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
4029 lds->lds_dir_def_stripe_offset) &&
4030 le32_to_cpu(lds->lds_dir_def_hash_type) !=
4031 LMV_HASH_TYPE_UNKNOWN)) {
4032 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
4034 if (info->lti_ea_store_size < sizeof(*v1)) {
4035 rc = lod_ea_store_resize(info, sizeof(*v1));
4038 v1 = info->lti_ea_store;
4041 memset(v1, 0, sizeof(*v1));
4042 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
4043 v1->lum_stripe_count =
4044 cpu_to_le32(lds->lds_dir_def_stripe_count);
4045 v1->lum_stripe_offset =
4046 cpu_to_le32(lds->lds_dir_def_stripe_offset);
4048 cpu_to_le32(lds->lds_dir_def_hash_type);
4050 info->lti_buf.lb_buf = v1;
4051 info->lti_buf.lb_len = sizeof(*v1);
4053 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4054 XATTR_NAME_DEFAULT_LMV,
4057 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
4059 XATTR_NAME_DEFAULT_LMV, 0,
4065 /* Transfer default LOV striping from the parent */
4066 if (lds != NULL && lds->lds_def_striping_set &&
4067 lds->lds_def_comp_cnt != 0) {
4068 struct lov_mds_md *lmm;
4069 int lmm_size = lod_comp_md_size(lo, true);
4071 if (info->lti_ea_store_size < lmm_size) {
4072 rc = lod_ea_store_resize(info, lmm_size);
4076 lmm = info->lti_ea_store;
4078 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
4082 info->lti_buf.lb_buf = lmm;
4083 info->lti_buf.lb_len = lmm_size;
4086 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4087 XATTR_NAME_LOV, 0, th);
4089 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4090 XATTR_NAME_LOV, 0, th);
4098 static int lod_declare_dir_striping_create(const struct lu_env *env,
4099 struct dt_object *dt,
4100 struct lu_attr *attr,
4102 struct dt_object_format *dof,
4105 return lod_dir_striping_create_internal(env, dt, attr, lmu, dof, th,
4109 static int lod_dir_striping_create(const struct lu_env *env,
4110 struct dt_object *dt,
4111 struct lu_attr *attr,
4112 struct dt_object_format *dof,
4115 return lod_dir_striping_create_internal(env, dt, attr, NULL, dof, th,
4120 * Make LOV EA for striped object.
4122 * Generate striping information and store it in the LOV EA of the given
4123 * object. The caller must ensure nobody else is calling the function
4124 * against the object concurrently. The transaction must be started.
4125 * FLDB service must be running as well; it's used to map FID to the target,
4126 * which is stored in LOV EA.
4128 * \param[in] env execution environment for this thread
4129 * \param[in] lo LOD object
4130 * \param[in] th transaction handle
4132 * \retval 0 if LOV EA is stored successfully
4133 * \retval negative error number on failure
4135 static int lod_generate_and_set_lovea(const struct lu_env *env,
4136 struct lod_object *lo,
4139 struct lod_thread_info *info = lod_env_info(env);
4140 struct dt_object *next = dt_object_child(&lo->ldo_obj);
4141 struct lov_mds_md_v1 *lmm;
4147 if (lo->ldo_comp_cnt == 0 && !lo->ldo_is_foreign) {
4148 lod_striping_free(env, lo);
4149 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
4153 lmm_size = lod_comp_md_size(lo, false);
4154 if (info->lti_ea_store_size < lmm_size) {
4155 rc = lod_ea_store_resize(info, lmm_size);
4159 lmm = info->lti_ea_store;
4161 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
4165 info->lti_buf.lb_buf = lmm;
4166 info->lti_buf.lb_len = lmm_size;
4167 rc = lod_sub_xattr_set(env, next, &info->lti_buf,
4168 XATTR_NAME_LOV, 0, th);
4172 static __u32 lod_gen_component_id(struct lod_object *lo,
4173 int mirror_id, int comp_idx);
4176 * Repeat an existing component
4178 * Creates a new layout by replicating an existing component. Uses striping
4179 * policy from previous component as a template for the striping for the new
4182 * New component starts with zero length, will be extended (or removed) before
4183 * returning layout to client.
4185 * NB: Reallocates layout components array (lo->ldo_comp_entries), invalidating
4186 * any pre-existing pointers to components. Handle with care.
4188 * \param[in] env execution environment for this thread
4189 * \param[in,out] lo object to update the layout of
4190 * \param[in] index index of component to copy
4192 * \retval 0 on success
4193 * \retval negative errno on error
4195 static int lod_layout_repeat_comp(const struct lu_env *env,
4196 struct lod_object *lo, int index)
4198 struct lod_layout_component *lod_comp;
4199 struct lod_layout_component *new_comp = NULL;
4200 struct lod_layout_component *comp_array;
4201 int rc = 0, i, new_cnt = lo->ldo_comp_cnt + 1;
4206 lod_comp = &lo->ldo_comp_entries[index];
4207 LASSERT(lod_comp_inited(lod_comp) && lod_comp->llc_id != LCME_ID_INVAL);
4209 CDEBUG(D_LAYOUT, "repeating component %d\n", index);
4211 OBD_ALLOC_PTR_ARRAY(comp_array, new_cnt);
4212 if (comp_array == NULL)
4213 GOTO(out, rc = -ENOMEM);
4215 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4216 memcpy(&comp_array[i + offset], &lo->ldo_comp_entries[i],
4217 sizeof(*comp_array));
4219 /* Duplicate this component in to the next slot */
4221 new_comp = &comp_array[i + 1];
4222 memcpy(&comp_array[i + 1], &lo->ldo_comp_entries[i],
4223 sizeof(*comp_array));
4224 /* We must now skip this new component when copying */
4229 /* Set up copied component */
4230 new_comp->llc_flags &= ~LCME_FL_INIT;
4231 new_comp->llc_stripe = NULL;
4232 new_comp->llc_stripes_allocated = 0;
4233 new_comp->llc_ost_indices = NULL;
4234 new_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4235 /* for uninstantiated components, layout gen stores default stripe
4237 new_comp->llc_layout_gen = lod_comp->llc_stripe_offset;
4238 /* This makes the repeated component zero-length, placed at the end of
4239 * the preceding component */
4240 new_comp->llc_extent.e_start = new_comp->llc_extent.e_end;
4241 new_comp->llc_timestamp = lod_comp->llc_timestamp;
4242 new_comp->llc_pool = NULL;
4244 rc = lod_set_pool(&new_comp->llc_pool, lod_comp->llc_pool);
4248 if (new_comp->llc_ostlist.op_array) {
4249 __u32 *op_array = NULL;
4251 OBD_ALLOC(op_array, new_comp->llc_ostlist.op_size);
4253 GOTO(out, rc = -ENOMEM);
4254 memcpy(op_array, &new_comp->llc_ostlist.op_array,
4255 new_comp->llc_ostlist.op_size);
4256 new_comp->llc_ostlist.op_array = op_array;
4259 OBD_FREE_PTR_ARRAY(lo->ldo_comp_entries, lo->ldo_comp_cnt);
4260 lo->ldo_comp_entries = comp_array;
4261 lo->ldo_comp_cnt = new_cnt;
4263 /* Generate an id for the new component */
4264 mirror_id = mirror_id_of(new_comp->llc_id);
4265 new_comp->llc_id = LCME_ID_INVAL;
4266 new_comp->llc_id = lod_gen_component_id(lo, mirror_id, index + 1);
4267 if (new_comp->llc_id == LCME_ID_INVAL)
4268 GOTO(out, rc = -ERANGE);
4273 OBD_FREE_PTR_ARRAY(comp_array, new_cnt);
4278 static int lod_layout_data_init(struct lod_thread_info *info, __u32 comp_cnt)
4282 /* clear memory region that will be used for layout change */
4283 memset(&info->lti_layout_attr, 0, sizeof(struct lu_attr));
4284 info->lti_count = 0;
4286 if (info->lti_comp_size >= comp_cnt)
4289 if (info->lti_comp_size > 0) {
4290 OBD_FREE_PTR_ARRAY(info->lti_comp_idx, info->lti_comp_size);
4291 info->lti_comp_size = 0;
4294 OBD_ALLOC_PTR_ARRAY(info->lti_comp_idx, comp_cnt);
4295 if (!info->lti_comp_idx)
4298 info->lti_comp_size = comp_cnt;
4303 * Prepare new layout minus deleted components
4305 * Removes components marked for deletion (LCME_ID_INVAL) by copying to a new
4306 * layout and skipping those components. Removes stripe objects if any exist.
4309 * Reallocates layout components array (lo->ldo_comp_entries), invalidating
4310 * any pre-existing pointers to components.
4312 * Caller is responsible for updating mirror end (ldo_mirror[].lme_end).
4314 * \param[in] env execution environment for this thread
4315 * \param[in,out] lo object to update the layout of
4316 * \param[in] th transaction handle for this operation
4318 * \retval # of components deleted
4319 * \retval negative errno on error
4321 static int lod_layout_del_prep_layout(const struct lu_env *env,
4322 struct lod_object *lo,
4325 struct lod_layout_component *lod_comp;
4326 struct lod_thread_info *info = lod_env_info(env);
4327 int rc = 0, i, j, deleted = 0;
4331 LASSERT(lo->ldo_is_composite);
4332 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
4334 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
4338 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4339 lod_comp = &lo->ldo_comp_entries[i];
4341 if (lod_comp->llc_id != LCME_ID_INVAL) {
4342 /* Build array of things to keep */
4343 info->lti_comp_idx[info->lti_count++] = i;
4347 lod_obj_set_pool(lo, i, NULL);
4348 if (lod_comp->llc_ostlist.op_array) {
4349 OBD_FREE(lod_comp->llc_ostlist.op_array,
4350 lod_comp->llc_ostlist.op_size);
4351 lod_comp->llc_ostlist.op_array = NULL;
4352 lod_comp->llc_ostlist.op_size = 0;
4356 CDEBUG(D_LAYOUT, "deleting comp %d, left %d\n", i,
4357 lo->ldo_comp_cnt - deleted);
4359 /* No striping info for this component */
4360 if (lod_comp->llc_stripe == NULL)
4363 LASSERT(lod_comp->llc_stripe_count > 0);
4364 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4365 struct dt_object *obj = lod_comp->llc_stripe[j];
4370 /* components which are not init have no sub objects
4372 if (lod_comp_inited(lod_comp)) {
4373 rc = lod_sub_destroy(env, obj, th);
4378 lu_object_put(env, &obj->do_lu);
4379 lod_comp->llc_stripe[j] = NULL;
4381 OBD_FREE_PTR_ARRAY(lod_comp->llc_stripe,
4382 lod_comp->llc_stripes_allocated);
4383 lod_comp->llc_stripe = NULL;
4384 OBD_FREE_PTR_ARRAY(lod_comp->llc_ost_indices,
4385 lod_comp->llc_stripes_allocated);
4386 lod_comp->llc_ost_indices = NULL;
4387 lod_comp->llc_stripes_allocated = 0;
4390 /* info->lti_count has the amount of left components */
4391 LASSERTF(info->lti_count >= 0 && info->lti_count < lo->ldo_comp_cnt,
4392 "left = %d, lo->ldo_comp_cnt %d\n", (int)info->lti_count,
4393 (int)lo->ldo_comp_cnt);
4395 if (info->lti_count > 0) {
4396 struct lod_layout_component *comp_array;
4398 OBD_ALLOC_PTR_ARRAY(comp_array, info->lti_count);
4399 if (comp_array == NULL)
4400 GOTO(out, rc = -ENOMEM);
4402 for (i = 0; i < info->lti_count; i++) {
4403 memcpy(&comp_array[i],
4404 &lo->ldo_comp_entries[info->lti_comp_idx[i]],
4405 sizeof(*comp_array));
4408 OBD_FREE_PTR_ARRAY(lo->ldo_comp_entries, lo->ldo_comp_cnt);
4409 lo->ldo_comp_entries = comp_array;
4410 lo->ldo_comp_cnt = info->lti_count;
4412 lod_free_comp_entries(lo);
4417 return rc ? rc : deleted;
4421 * Delete layout component(s)
4423 * This function sets up the layout data in the env and does the setattrs
4424 * required to write out the new layout. The layout itself is modified in
4425 * lod_layout_del_prep_layout.
4427 * \param[in] env execution environment for this thread
4428 * \param[in] dt object
4429 * \param[in] th transaction handle
4431 * \retval 0 on success
4432 * \retval negative error number on failure
4434 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
4437 struct lod_object *lo = lod_dt_obj(dt);
4438 struct dt_object *next = dt_object_child(dt);
4439 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4442 LASSERT(lo->ldo_mirror_count == 1);
4444 rc = lod_layout_del_prep_layout(env, lo, th);
4448 /* Only do this if we didn't delete all components */
4449 if (lo->ldo_comp_cnt > 0) {
4450 lo->ldo_mirrors[0].lme_end = lo->ldo_comp_cnt - 1;
4451 lod_obj_inc_layout_gen(lo);
4454 LASSERT(dt_object_exists(dt));
4455 rc = dt_attr_get(env, next, attr);
4459 if (attr->la_size > 0) {
4461 attr->la_valid = LA_SIZE;
4462 rc = lod_sub_attr_set(env, next, attr, th);
4467 rc = lod_generate_and_set_lovea(env, lo, th);
4471 lod_striping_free(env, lo);
4476 static int lod_get_default_lov_striping(const struct lu_env *env,
4477 struct lod_object *lo,
4478 struct lod_default_striping *lds,
4479 struct dt_allocation_hint *ah);
4481 * Implementation of dt_object_operations::do_xattr_set.
4483 * Sets specified extended attribute on the object. Three types of EAs are
4485 * LOV EA - stores striping for a regular file or default striping (when set
4487 * LMV EA - stores a marker for the striped directories
4488 * DMV EA - stores default directory striping
4490 * When striping is applied to a non-striped existing object (this is called
4491 * late striping), then LOD notices the caller wants to turn the object into a
4492 * striped one. The stripe objects are created and appropriate EA is set:
4493 * LOV EA storing all the stripes directly or LMV EA storing just a small header
4494 * with striping configuration.
4496 * \see dt_object_operations::do_xattr_set() in the API description for details.
4498 static int lod_xattr_set(const struct lu_env *env,
4499 struct dt_object *dt, const struct lu_buf *buf,
4500 const char *name, int fl, struct thandle *th)
4502 struct dt_object *next = dt_object_child(dt);
4507 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4508 !strcmp(name, XATTR_NAME_LMV)) {
4510 case LU_XATTR_CREATE:
4511 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
4514 case LU_XATTR_REPLACE:
4515 rc = lod_dir_layout_set(env, dt, buf, fl, th);
4522 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4523 strcmp(name, XATTR_NAME_LOV) == 0) {
4524 struct lod_default_striping *lds = lod_lds_buf_get(env);
4525 struct lov_user_md_v1 *v1 = buf->lb_buf;
4526 char pool[LOV_MAXPOOLNAME + 1];
4529 /* get existing striping config */
4530 rc = lod_get_default_lov_striping(env, lod_dt_obj(dt), lds,
4535 memset(pool, 0, sizeof(pool));
4536 if (lds->lds_def_striping_set == 1)
4537 lod_layout_get_pool(lds->lds_def_comp_entries,
4538 lds->lds_def_comp_cnt, pool,
4541 is_del = LOVEA_DELETE_VALUES(v1->lmm_stripe_size,
4542 v1->lmm_stripe_count,
4543 v1->lmm_stripe_offset,
4546 /* Retain the pool name if it is not given */
4547 if (v1->lmm_magic == LOV_USER_MAGIC_V1 && pool[0] != '\0' &&
4549 struct lod_thread_info *info = lod_env_info(env);
4550 struct lov_user_md_v3 *v3 = info->lti_ea_store;
4552 memset(v3, 0, sizeof(*v3));
4553 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
4554 v3->lmm_pattern = cpu_to_le32(v1->lmm_pattern);
4555 v3->lmm_stripe_count =
4556 cpu_to_le32(v1->lmm_stripe_count);
4557 v3->lmm_stripe_offset =
4558 cpu_to_le32(v1->lmm_stripe_offset);
4559 v3->lmm_stripe_size = cpu_to_le32(v1->lmm_stripe_size);
4561 strlcpy(v3->lmm_pool_name, pool,
4562 sizeof(v3->lmm_pool_name));
4564 info->lti_buf.lb_buf = v3;
4565 info->lti_buf.lb_len = sizeof(*v3);
4566 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4569 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name,
4573 if (lds->lds_def_striping_set == 1 &&
4574 lds->lds_def_comp_entries != NULL)
4575 lod_free_def_comp_entries(lds);
4578 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4579 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
4581 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
4584 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4585 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4586 strcmp(name, XATTR_LUSTRE_LOV) == 0 ||
4587 allowed_lustre_lov(name))) {
4588 /* in case of lov EA swap, just set it
4589 * if not, it is a replay so check striping match what we
4590 * already have during req replay, declare_xattr_set()
4591 * defines striping, then create() does the work */
4592 if (fl & LU_XATTR_REPLACE) {
4593 /* free stripes, then update disk */
4594 lod_striping_free(env, lod_dt_obj(dt));
4596 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
4597 } else if (dt_object_remote(dt)) {
4598 /* This only happens during migration, see
4599 * mdd_migrate_create(), in which Master MDT will
4600 * create a remote target object, and only set
4601 * (migrating) stripe EA on the remote object,
4602 * and does not need creating each stripes. */
4603 rc = lod_sub_xattr_set(env, next, buf, name,
4605 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
4606 /* delete component(s) */
4607 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
4608 rc = lod_layout_del(env, dt, th);
4611 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
4612 * it's going to create create file with specified
4613 * component(s), the striping must have not being
4614 * cached in this case;
4616 * Otherwise, it's going to add/change component(s) to
4617 * an existing file, the striping must have been cached
4620 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
4621 !strcmp(name, XATTR_NAME_LOV),
4622 !lod_dt_obj(dt)->ldo_comp_cached));
4624 rc = lod_striped_create(env, dt, NULL, NULL, th);
4627 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
4628 rc = lod_replace_parent_fid(env, dt, buf, th, false);
4633 /* then all other xattr */
4634 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
4640 * Implementation of dt_object_operations::do_declare_xattr_del.
4642 * \see dt_object_operations::do_declare_xattr_del() in the API description
4645 static int lod_declare_xattr_del(const struct lu_env *env,
4646 struct dt_object *dt, const char *name,
4649 struct lod_object *lo = lod_dt_obj(dt);
4650 struct dt_object *next = dt_object_child(dt);
4655 rc = lod_sub_declare_xattr_del(env, next, name, th);
4659 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4662 /* NB: don't delete stripe LMV, because when we do this, normally we
4663 * will remove stripes, besides, if directory LMV is corrupt, this will
4664 * prevent deleting its LMV and fixing it (via LFSCK).
4666 if (!strcmp(name, XATTR_NAME_LMV))
4669 rc = lod_striping_load(env, lo);
4673 if (lo->ldo_dir_stripe_count == 0)
4676 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4677 struct dt_object *dto = lo->ldo_stripe[i];
4682 rc = lod_sub_declare_xattr_del(env, dto, name, th);
4691 * Implementation of dt_object_operations::do_xattr_del.
4693 * If EA storing a regular striping is being deleted, then release
4694 * all the references to the stripe objects in core.
4696 * \see dt_object_operations::do_xattr_del() in the API description for details.
4698 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
4699 const char *name, struct thandle *th)
4701 struct dt_object *next = dt_object_child(dt);
4702 struct lod_object *lo = lod_dt_obj(dt);
4707 if (!strcmp(name, XATTR_NAME_LOV) || !strcmp(name, XATTR_NAME_LMV))
4708 lod_striping_free(env, lod_dt_obj(dt));
4710 rc = lod_sub_xattr_del(env, next, name, th);
4711 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
4714 if (!strcmp(name, XATTR_NAME_LMV))
4717 if (lo->ldo_dir_stripe_count == 0)
4720 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4721 struct dt_object *dto = lo->ldo_stripe[i];
4726 rc = lod_sub_xattr_del(env, dto, name, th);
4735 * Implementation of dt_object_operations::do_xattr_list.
4737 * \see dt_object_operations::do_xattr_list() in the API description
4740 static int lod_xattr_list(const struct lu_env *env,
4741 struct dt_object *dt, const struct lu_buf *buf)
4743 return dt_xattr_list(env, dt_object_child(dt), buf);
4746 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
4748 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
4752 * Copy OST list from layout provided by user.
4754 * \param[in] lod_comp layout_component to be filled
4755 * \param[in] v3 LOV EA V3 user data
4757 * \retval 0 on success
4758 * \retval negative if failed
4760 int lod_comp_copy_ost_lists(struct lod_layout_component *lod_comp,
4761 struct lov_user_md_v3 *v3)
4767 if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
4768 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
4770 if (lod_comp->llc_ostlist.op_array) {
4771 if (lod_comp->llc_ostlist.op_size >=
4772 v3->lmm_stripe_count * sizeof(__u32)) {
4773 lod_comp->llc_ostlist.op_count =
4774 v3->lmm_stripe_count;
4777 OBD_FREE(lod_comp->llc_ostlist.op_array,
4778 lod_comp->llc_ostlist.op_size);
4781 /* copy ost list from lmm */
4782 lod_comp->llc_ostlist.op_count = v3->lmm_stripe_count;
4783 lod_comp->llc_ostlist.op_size = v3->lmm_stripe_count * sizeof(__u32);
4784 OBD_ALLOC(lod_comp->llc_ostlist.op_array,
4785 lod_comp->llc_ostlist.op_size);
4786 if (!lod_comp->llc_ostlist.op_array)
4789 for (j = 0; j < v3->lmm_stripe_count; j++) {
4790 lod_comp->llc_ostlist.op_array[j] =
4791 v3->lmm_objects[j].l_ost_idx;
4799 * Get default striping.
4801 * \param[in] env execution environment
4802 * \param[in] lo object
4803 * \param[out] lds default striping
4805 * \retval 0 on success
4806 * \retval negative if failed
4808 static int lod_get_default_lov_striping(const struct lu_env *env,
4809 struct lod_object *lo,
4810 struct lod_default_striping *lds,
4811 struct dt_allocation_hint *ah)
4813 struct lod_thread_info *info = lod_env_info(env);
4814 struct lov_user_md_v1 *v1 = NULL;
4815 struct lov_user_md_v3 *v3 = NULL;
4816 struct lov_comp_md_v1 *comp_v1 = NULL;
4824 rc = lod_get_lov_ea(env, lo);
4828 if (rc < (typeof(rc))sizeof(struct lov_user_md))
4831 v1 = info->lti_ea_store;
4832 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
4833 lustre_swab_lov_user_md_v1(v1);
4834 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
4835 v3 = (struct lov_user_md_v3 *)v1;
4836 lustre_swab_lov_user_md_v3(v3);
4837 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_SPECIFIC)) {
4838 v3 = (struct lov_user_md_v3 *)v1;
4839 lustre_swab_lov_user_md_v3(v3);
4840 lustre_swab_lov_user_md_objects(v3->lmm_objects,
4841 v3->lmm_stripe_count);
4842 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1) ||
4843 v1->lmm_magic == __swab32(LOV_USER_MAGIC_SEL)) {
4844 comp_v1 = (struct lov_comp_md_v1 *)v1;
4845 lustre_swab_lov_comp_md_v1(comp_v1);
4848 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
4849 v1->lmm_magic != LOV_MAGIC_COMP_V1 &&
4850 v1->lmm_magic != LOV_MAGIC_SEL &&
4851 v1->lmm_magic != LOV_USER_MAGIC_SPECIFIC)
4854 if ((v1->lmm_magic == LOV_MAGIC_COMP_V1 ||
4855 v1->lmm_magic == LOV_MAGIC_SEL) &&
4856 !(ah && ah->dah_append_stripes)) {
4857 comp_v1 = (struct lov_comp_md_v1 *)v1;
4858 comp_cnt = comp_v1->lcm_entry_count;
4861 mirror_cnt = comp_v1->lcm_mirror_count + 1;
4869 /* realloc default comp entries if necessary */
4870 rc = lod_def_striping_comp_resize(lds, comp_cnt);
4874 lds->lds_def_comp_cnt = comp_cnt;
4875 lds->lds_def_striping_is_composite = composite;
4876 lds->lds_def_mirror_cnt = mirror_cnt;
4878 for (i = 0; i < comp_cnt; i++) {
4879 struct lod_layout_component *lod_comp;
4882 lod_comp = &lds->lds_def_comp_entries[i];
4884 * reset lod_comp values, llc_stripes is always NULL in
4885 * the default striping template, llc_pool will be reset
4888 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
4891 v1 = (struct lov_user_md *)((char *)comp_v1 +
4892 comp_v1->lcm_entries[i].lcme_offset);
4893 lod_comp->llc_extent =
4894 comp_v1->lcm_entries[i].lcme_extent;
4895 /* We only inherit certain flags from the layout */
4896 lod_comp->llc_flags =
4897 comp_v1->lcm_entries[i].lcme_flags &
4898 LCME_TEMPLATE_FLAGS;
4901 if (!lov_pattern_supported(v1->lmm_pattern) &&
4902 !(v1->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
4903 lod_free_def_comp_entries(lds);
4907 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d stripe_offset=%d append_stripes=%d\n",
4908 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
4909 (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
4910 (int)v1->lmm_stripe_offset,
4911 ah ? ah->dah_append_stripes : 0);
4913 if (ah && ah->dah_append_stripes)
4914 lod_comp->llc_stripe_count = ah->dah_append_stripes;
4916 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
4917 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
4918 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
4919 lod_comp->llc_pattern = v1->lmm_pattern;
4922 if (ah && ah->dah_append_pool && ah->dah_append_pool[0]) {
4923 pool = ah->dah_append_pool;
4924 } else if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
4925 /* XXX: sanity check here */
4926 v3 = (struct lov_user_md_v3 *) v1;
4927 if (v3->lmm_pool_name[0] != '\0')
4928 pool = v3->lmm_pool_name;
4930 lod_set_def_pool(lds, i, pool);
4931 if (v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
4932 v3 = (struct lov_user_md_v3 *)v1;
4933 rc = lod_comp_copy_ost_lists(lod_comp, v3);
4936 } else if (lod_comp->llc_ostlist.op_array &&
4937 lod_comp->llc_ostlist.op_count) {
4938 for (j = 0; j < lod_comp->llc_ostlist.op_count; j++)
4939 lod_comp->llc_ostlist.op_array[j] = -1;
4940 lod_comp->llc_ostlist.op_count = 0;
4944 lds->lds_def_striping_set = 1;
4949 * Get default directory striping.
4951 * \param[in] env execution environment
4952 * \param[in] lo object
4953 * \param[out] lds default striping
4955 * \retval 0 on success
4956 * \retval negative if failed
4958 static int lod_get_default_lmv_striping(const struct lu_env *env,
4959 struct lod_object *lo,
4960 struct lod_default_striping *lds)
4962 struct lmv_user_md *lmu;
4965 lds->lds_dir_def_striping_set = 0;
4967 rc = lod_get_default_lmv_ea(env, lo);
4971 if (rc >= (int)sizeof(*lmu)) {
4972 struct lod_thread_info *info = lod_env_info(env);
4974 lmu = info->lti_ea_store;
4976 lds->lds_dir_def_stripe_count =
4977 le32_to_cpu(lmu->lum_stripe_count);
4978 lds->lds_dir_def_stripe_offset =
4979 le32_to_cpu(lmu->lum_stripe_offset);
4980 lds->lds_dir_def_hash_type =
4981 le32_to_cpu(lmu->lum_hash_type);
4982 lds->lds_dir_def_striping_set = 1;
4989 * Get default striping in the object.
4991 * Get object default striping and default directory striping.
4993 * \param[in] env execution environment
4994 * \param[in] lo object
4995 * \param[out] lds default striping
4997 * \retval 0 on success
4998 * \retval negative if failed
5000 static int lod_get_default_striping(const struct lu_env *env,
5001 struct lod_object *lo,
5002 struct lod_default_striping *lds)
5006 rc = lod_get_default_lov_striping(env, lo, lds, NULL);
5007 rc1 = lod_get_default_lmv_striping(env, lo, lds);
5008 if (rc == 0 && rc1 < 0)
5015 * Apply default striping on object.
5017 * If object striping pattern is not set, set to the one in default striping.
5018 * The default striping is from parent or fs.
5020 * \param[in] lo new object
5021 * \param[in] lds default striping
5022 * \param[in] mode new object's mode
5024 static void lod_striping_from_default(struct lod_object *lo,
5025 const struct lod_default_striping *lds,
5028 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5031 if (lds->lds_def_striping_set && S_ISREG(mode)) {
5032 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
5034 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
5035 lds->lds_def_comp_cnt);
5039 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
5040 if (lds->lds_def_mirror_cnt > 1)
5041 lo->ldo_flr_state = LCM_FL_RDONLY;
5043 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5044 struct lod_layout_component *obj_comp =
5045 &lo->ldo_comp_entries[i];
5046 struct lod_layout_component *def_comp =
5047 &lds->lds_def_comp_entries[i];
5049 CDEBUG(D_LAYOUT, "Inherit from default: flags=%#x "
5050 "size=%hu nr=%u offset=%u pattern=%#x pool=%s\n",
5051 def_comp->llc_flags,
5052 def_comp->llc_stripe_size,
5053 def_comp->llc_stripe_count,
5054 def_comp->llc_stripe_offset,
5055 def_comp->llc_pattern,
5056 def_comp->llc_pool ?: "");
5058 *obj_comp = *def_comp;
5059 if (def_comp->llc_pool != NULL) {
5060 /* pointer was copied from def_comp */
5061 obj_comp->llc_pool = NULL;
5062 lod_obj_set_pool(lo, i, def_comp->llc_pool);
5066 if (def_comp->llc_ostlist.op_array &&
5067 def_comp->llc_ostlist.op_count) {
5068 OBD_ALLOC(obj_comp->llc_ostlist.op_array,
5069 obj_comp->llc_ostlist.op_size);
5070 if (!obj_comp->llc_ostlist.op_array)
5072 memcpy(obj_comp->llc_ostlist.op_array,
5073 def_comp->llc_ostlist.op_array,
5074 obj_comp->llc_ostlist.op_size);
5075 } else if (def_comp->llc_ostlist.op_array) {
5076 obj_comp->llc_ostlist.op_array = NULL;
5080 * Don't initialize these fields for plain layout
5081 * (v1/v3) here, they are inherited in the order of
5082 * 'parent' -> 'fs default (root)' -> 'global default
5083 * values for stripe_count & stripe_size'.
5085 * see lod_ah_init().
5087 if (!lo->ldo_is_composite)
5090 lod_adjust_stripe_info(obj_comp, desc, 0);
5092 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
5093 if (lo->ldo_dir_stripe_count == 0)
5094 lo->ldo_dir_stripe_count =
5095 lds->lds_dir_def_stripe_count;
5096 if (lo->ldo_dir_stripe_offset == -1)
5097 lo->ldo_dir_stripe_offset =
5098 lds->lds_dir_def_stripe_offset;
5099 if (lo->ldo_dir_hash_type == 0)
5100 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
5102 CDEBUG(D_LAYOUT, "striping from default dir: count:%hu, "
5103 "offset:%u, hash_type:%u\n",
5104 lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
5105 lo->ldo_dir_hash_type);
5109 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root,
5112 struct lod_layout_component *lod_comp;
5114 if (lo->ldo_comp_cnt == 0)
5117 if (lo->ldo_is_composite)
5120 lod_comp = &lo->ldo_comp_entries[0];
5122 if (lod_comp->llc_stripe_count <= 0 ||
5123 lod_comp->llc_stripe_size <= 0)
5126 if (from_root && (lod_comp->llc_pool == NULL ||
5127 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
5130 if (append_pool && append_pool[0])
5137 * Implementation of dt_object_operations::do_ah_init.
5139 * This method is used to make a decision on the striping configuration for the
5140 * object being created. It can be taken from the \a parent object if it exists,
5141 * or filesystem's default. The resulting configuration (number of stripes,
5142 * stripe size/offset, pool name, etc) is stored in the object itself and will
5143 * be used by the methods like ->doo_declare_create().
5145 * \see dt_object_operations::do_ah_init() in the API description for details.
5147 static void lod_ah_init(const struct lu_env *env,
5148 struct dt_allocation_hint *ah,
5149 struct dt_object *parent,
5150 struct dt_object *child,
5153 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
5154 struct lod_thread_info *info = lod_env_info(env);
5155 struct lod_default_striping *lds = lod_lds_buf_get(env);
5156 struct dt_object *nextp = NULL;
5157 struct dt_object *nextc;
5158 struct lod_object *lp = NULL;
5159 struct lod_object *lc;
5160 struct lov_desc *desc;
5161 struct lod_layout_component *lod_comp;
5167 if (ah->dah_append_stripes == -1)
5168 ah->dah_append_stripes =
5169 d->lod_ost_descs.ltd_lov_desc.ld_tgt_count;
5171 if (likely(parent)) {
5172 nextp = dt_object_child(parent);
5173 lp = lod_dt_obj(parent);
5176 nextc = dt_object_child(child);
5177 lc = lod_dt_obj(child);
5179 LASSERT(!lod_obj_is_striped(child));
5180 /* default layout template may have been set on the regular file
5181 * when this is called from mdd_create_data() */
5182 if (S_ISREG(child_mode))
5183 lod_free_comp_entries(lc);
5185 if (!dt_object_exists(nextc))
5186 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
5188 if (S_ISDIR(child_mode)) {
5189 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
5191 /* other default values are 0 */
5192 lc->ldo_dir_stripe_offset = -1;
5194 /* no default striping configuration is needed for
5197 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5198 le32_to_cpu(lum1->lum_magic) == LMV_MAGIC_FOREIGN) {
5199 lc->ldo_dir_is_foreign = true;
5200 /* keep stripe_count 0 and stripe_offset -1 */
5201 CDEBUG(D_INFO, "no default striping for foreign dir\n");
5206 * If parent object is not root directory,
5207 * then get default striping from parent object.
5209 if (likely(lp != NULL)) {
5210 lod_get_default_striping(env, lp, lds);
5212 /* inherit default striping except ROOT */
5213 if ((lds->lds_def_striping_set ||
5214 lds->lds_dir_def_striping_set) &&
5215 !fid_is_root(lod_object_fid(lp)))
5216 lc->ldo_def_striping = lds;
5219 /* It should always honour the specified stripes */
5220 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
5221 * will have old magic. In this case, we should ignore the
5222 * stripe count and try to create dir by default stripe.
5224 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5225 (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
5226 le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
5227 lc->ldo_dir_stripe_count =
5228 le32_to_cpu(lum1->lum_stripe_count);
5229 lc->ldo_dir_stripe_offset =
5230 le32_to_cpu(lum1->lum_stripe_offset);
5231 lc->ldo_dir_hash_type =
5232 le32_to_cpu(lum1->lum_hash_type);
5234 "set dirstripe: count %hu, offset %d, hash %u\n",
5235 lc->ldo_dir_stripe_count,
5236 (int)lc->ldo_dir_stripe_offset,
5237 lc->ldo_dir_hash_type);
5239 /* transfer defaults LMV to new directory */
5240 lod_striping_from_default(lc, lds, child_mode);
5242 /* set count 0 to create normal directory */
5243 if (lc->ldo_dir_stripe_count == 1)
5244 lc->ldo_dir_stripe_count = 0;
5247 /* shrink the stripe_count to the avaible MDT count */
5248 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
5249 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)) {
5250 lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
5251 if (lc->ldo_dir_stripe_count == 1)
5252 lc->ldo_dir_stripe_count = 0;
5255 if (!(lc->ldo_dir_hash_type & LMV_HASH_TYPE_MASK))
5256 lc->ldo_dir_hash_type |=
5257 d->lod_mdt_descs.ltd_lmv_desc.ld_pattern;
5259 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
5260 lc->ldo_dir_stripe_count,
5261 (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
5266 /* child object regular file*/
5268 if (!lod_object_will_be_striped(S_ISREG(child_mode),
5269 lu_object_fid(&child->do_lu)))
5272 /* If object is going to be striped over OSTs, transfer default
5273 * striping information to the child, so that we can use it
5274 * during declaration and creation.
5276 * Try from the parent first.
5278 if (likely(lp != NULL)) {
5279 rc = lod_get_default_lov_striping(env, lp, lds, ah);
5281 lod_striping_from_default(lc, lds, child_mode);
5284 /* Initialize lod_device::lod_md_root object reference */
5285 if (d->lod_md_root == NULL) {
5286 struct dt_object *root;
5287 struct lod_object *lroot;
5289 lu_root_fid(&info->lti_fid);
5290 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
5291 if (!IS_ERR(root)) {
5292 lroot = lod_dt_obj(root);
5294 spin_lock(&d->lod_lock);
5295 if (d->lod_md_root != NULL)
5296 dt_object_put(env, &d->lod_md_root->ldo_obj);
5297 d->lod_md_root = lroot;
5298 spin_unlock(&d->lod_lock);
5302 /* try inherit layout from the root object (fs default) when:
5303 * - parent does not have default layout; or
5304 * - parent has plain(v1/v3) default layout, and some attributes
5305 * are not specified in the default layout;
5307 if (d->lod_md_root != NULL &&
5308 lod_need_inherit_more(lc, true, ah->dah_append_pool)) {
5309 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds,
5313 if (lc->ldo_comp_cnt == 0) {
5314 lod_striping_from_default(lc, lds, child_mode);
5315 } else if (!lds->lds_def_striping_is_composite) {
5316 struct lod_layout_component *def_comp;
5318 LASSERT(!lc->ldo_is_composite);
5319 lod_comp = &lc->ldo_comp_entries[0];
5320 def_comp = &lds->lds_def_comp_entries[0];
5322 if (lod_comp->llc_stripe_count <= 0)
5323 lod_comp->llc_stripe_count =
5324 def_comp->llc_stripe_count;
5325 if (lod_comp->llc_stripe_size <= 0)
5326 lod_comp->llc_stripe_size =
5327 def_comp->llc_stripe_size;
5328 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT &&
5329 (!lod_comp->llc_pool || !lod_comp->llc_pool[0]))
5330 lod_comp->llc_stripe_offset =
5331 def_comp->llc_stripe_offset;
5332 if (lod_comp->llc_pool == NULL)
5333 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
5338 * fs default striping may not be explicitly set, or historically set
5339 * in config log, use them.
5341 if (lod_need_inherit_more(lc, false, ah->dah_append_pool)) {
5342 if (lc->ldo_comp_cnt == 0) {
5343 rc = lod_alloc_comp_entries(lc, 0, 1);
5345 /* fail to allocate memory, will create a
5346 * non-striped file. */
5348 lc->ldo_is_composite = 0;
5349 lod_comp = &lc->ldo_comp_entries[0];
5350 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
5352 LASSERT(!lc->ldo_is_composite);
5353 lod_comp = &lc->ldo_comp_entries[0];
5354 desc = &d->lod_ost_descs.ltd_lov_desc;
5355 lod_adjust_stripe_info(lod_comp, desc, ah->dah_append_stripes);
5356 if (ah->dah_append_pool && ah->dah_append_pool[0])
5357 lod_obj_set_pool(lc, 0, ah->dah_append_pool);
5364 * Size initialization on late striping.
5366 * Propagate the size of a truncated object to a deferred striping.
5367 * This function handles a special case when truncate was done on a
5368 * non-striped object and now while the striping is being created
5369 * we can't lose that size, so we have to propagate it to the stripes
5372 * \param[in] env execution environment
5373 * \param[in] dt object
5374 * \param[in] th transaction handle
5376 * \retval 0 on success
5377 * \retval negative if failed
5379 static int lod_declare_init_size(const struct lu_env *env,
5380 struct dt_object *dt, struct thandle *th)
5382 struct dt_object *next = dt_object_child(dt);
5383 struct lod_object *lo = lod_dt_obj(dt);
5384 struct dt_object **objects = NULL;
5385 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
5386 uint64_t size, offs;
5387 int i, rc, stripe, stripe_count = 0, stripe_size = 0;
5388 struct lu_extent size_ext;
5391 if (!lod_obj_is_striped(dt))
5394 rc = dt_attr_get(env, next, attr);
5395 LASSERT(attr->la_valid & LA_SIZE);
5399 size = attr->la_size;
5403 size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
5404 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5405 struct lod_layout_component *lod_comp;
5406 struct lu_extent *extent;
5408 lod_comp = &lo->ldo_comp_entries[i];
5410 if (lod_comp->llc_stripe == NULL)
5413 extent = &lod_comp->llc_extent;
5414 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
5415 if (!lo->ldo_is_composite ||
5416 lu_extent_is_overlapped(extent, &size_ext)) {
5417 objects = lod_comp->llc_stripe;
5418 stripe_count = lod_comp->llc_stripe_count;
5419 stripe_size = lod_comp->llc_stripe_size;
5422 if (stripe_count == 0)
5425 LASSERT(objects != NULL && stripe_size != 0);
5426 do_div(size, stripe_size);
5427 stripe = do_div(size, stripe_count);
5428 LASSERT(objects[stripe] != NULL);
5430 size = size * stripe_size;
5431 offs = attr->la_size;
5432 size += do_div(offs, stripe_size);
5434 attr->la_valid = LA_SIZE;
5435 attr->la_size = size;
5437 rc = lod_sub_declare_attr_set(env, objects[stripe],
5446 * Declare creation of striped object.
5448 * The function declares creation stripes for a regular object. The function
5449 * also declares whether the stripes will be created with non-zero size if
5450 * previously size was set non-zero on the master object. If object \a dt is
5451 * not local, then only fully defined striping can be applied in \a lovea.
5452 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
5455 * \param[in] env execution environment
5456 * \param[in] dt object
5457 * \param[in] attr attributes the stripes will be created with
5458 * \param[in] lovea a buffer containing striping description
5459 * \param[in] th transaction handle
5461 * \retval 0 on success
5462 * \retval negative if failed
5464 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
5465 struct lu_attr *attr,
5466 const struct lu_buf *lovea, struct thandle *th)
5468 struct lod_thread_info *info = lod_env_info(env);
5469 struct dt_object *next = dt_object_child(dt);
5470 struct lod_object *lo = lod_dt_obj(dt);
5474 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
5475 GOTO(out, rc = -ENOMEM);
5477 if (!dt_object_remote(next)) {
5478 /* choose OST and generate appropriate objects */
5479 rc = lod_prepare_create(env, lo, attr, lovea, th);
5484 * declare storage for striping data
5486 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5488 /* LOD can not choose OST objects for remote objects, i.e.
5489 * stripes must be ready before that. Right now, it can only
5490 * happen during migrate, i.e. migrate process needs to create
5491 * remote regular file (mdd_migrate_create), then the migrate
5492 * process will provide stripeEA. */
5493 LASSERT(lovea != NULL);
5494 info->lti_buf = *lovea;
5497 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
5498 XATTR_NAME_LOV, 0, th);
5503 * if striping is created with local object's size > 0,
5504 * we have to propagate this size to specific object
5505 * the case is possible only when local object was created previously
5507 if (dt_object_exists(next))
5508 rc = lod_declare_init_size(env, dt, th);
5511 /* failed to create striping or to set initial size, let's reset
5512 * config so that others don't get confused */
5514 lod_striping_free(env, lo);
5520 * Whether subdirectories under \a dt should be created on MDTs by space QoS
5522 * If LMV_HASH_FLAG_SPACE is set on directory default layout, its subdirectories
5523 * should be created on MDT by space QoS.
5525 * \param[in] env execution environment
5526 * \param[in] dev lu device
5527 * \param[in] dt object
5529 * \retval 1 if directory should create subdir by space usage
5531 * \retval -ev if failed
5533 static inline int dt_object_qos_mkdir(const struct lu_env *env,
5534 struct lu_device *dev,
5535 struct dt_object *dt)
5537 struct lod_thread_info *info = lod_env_info(env);
5538 struct lu_object *obj;
5539 struct lod_object *lo;
5540 struct lmv_user_md *lmu;
5543 obj = lu_object_find_slice(env, dev, lu_object_fid(&dt->do_lu), NULL);
5545 return PTR_ERR(obj);
5547 lo = lu2lod_obj(obj);
5549 rc = lod_get_default_lmv_ea(env, lo);
5550 dt_object_put(env, dt);
5554 if (rc < (int)sizeof(*lmu))
5557 lmu = info->lti_ea_store;
5558 return le32_to_cpu(lmu->lum_stripe_offset) == LMV_OFFSET_DEFAULT;
5562 * Implementation of dt_object_operations::do_declare_create.
5564 * The method declares creation of a new object. If the object will be striped,
5565 * then helper functions are called to find FIDs for the stripes, declare
5566 * creation of the stripes and declare initialization of the striping
5567 * information to be stored in the master object.
5569 * \see dt_object_operations::do_declare_create() in the API description
5572 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
5573 struct lu_attr *attr,
5574 struct dt_allocation_hint *hint,
5575 struct dt_object_format *dof, struct thandle *th)
5577 struct dt_object *next = dt_object_child(dt);
5578 struct lod_object *lo = lod_dt_obj(dt);
5587 * first of all, we declare creation of local object
5589 rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
5594 * it's lod_ah_init() that has decided the object will be striped
5596 if (dof->dof_type == DFT_REGULAR) {
5597 /* callers don't want stripes */
5598 /* XXX: all tricky interactions with ->ah_make_hint() decided
5599 * to use striping, then ->declare_create() behaving differently
5600 * should be cleaned */
5601 if (dof->u.dof_reg.striped != 0)
5602 rc = lod_declare_striped_create(env, dt, attr,
5604 } else if (dof->dof_type == DFT_DIR) {
5605 struct seq_server_site *ss;
5606 struct lu_buf buf = { NULL };
5607 struct lu_buf *lmu = NULL;
5609 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
5611 /* If the parent has default stripeEA, and client
5612 * did not find it before sending create request,
5613 * then MDT will return -EREMOTE, and client will
5614 * retrieve the default stripeEA and re-create the
5617 * Note: if dah_eadata != NULL, it means creating the
5618 * striped directory with specified stripeEA, then it
5619 * should ignore the default stripeEA */
5620 if (hint != NULL && hint->dah_eadata == NULL) {
5621 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
5622 GOTO(out, rc = -EREMOTE);
5624 if (lo->ldo_dir_stripe_offset == LMV_OFFSET_DEFAULT) {
5625 struct lod_default_striping *lds;
5627 lds = lo->ldo_def_striping;
5629 * child and parent should be on the same MDT,
5630 * but if parent has default LMV, and the start
5631 * MDT offset is -1, it's allowed. This check
5632 * is not necessary after 2.12.22 because client
5633 * follows this already, but old client may not.
5635 if (hint->dah_parent &&
5636 dt_object_remote(hint->dah_parent) && lds &&
5637 lds->lds_dir_def_stripe_offset !=
5639 GOTO(out, rc = -EREMOTE);
5640 } else if (lo->ldo_dir_stripe_offset !=
5642 struct lod_device *lod;
5643 struct lu_tgt_desc *mdt = NULL;
5644 bool found_mdt = false;
5646 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5647 lod_foreach_mdt(lod, mdt) {
5648 if (mdt->ltd_index ==
5649 lo->ldo_dir_stripe_offset) {
5655 /* If the MDT indicated by stripe_offset can be
5656 * found, then tell client to resend the create
5657 * request to the correct MDT, otherwise return
5658 * error to client */
5660 GOTO(out, rc = -EREMOTE);
5662 GOTO(out, rc = -EINVAL);
5664 } else if (hint && hint->dah_eadata) {
5666 lmu->lb_buf = (void *)hint->dah_eadata;
5667 lmu->lb_len = hint->dah_eadata_len;
5670 rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
5674 /* failed to create striping or to set initial size, let's reset
5675 * config so that others don't get confused */
5677 lod_striping_free(env, lo);
5682 * Generate component ID for new created component.
5684 * \param[in] lo LOD object
5685 * \param[in] comp_idx index of ldo_comp_entries
5687 * \retval component ID on success
5688 * \retval LCME_ID_INVAL on failure
5690 static __u32 lod_gen_component_id(struct lod_object *lo,
5691 int mirror_id, int comp_idx)
5693 struct lod_layout_component *lod_comp;
5694 __u32 id, start, end;
5697 LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
5699 lod_obj_inc_layout_gen(lo);
5700 id = lo->ldo_layout_gen;
5701 if (likely(id <= SEQ_ID_MAX))
5702 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
5704 /* Layout generation wraps, need to check collisions. */
5705 start = id & SEQ_ID_MASK;
5708 for (id = start; id <= end; id++) {
5709 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5710 lod_comp = &lo->ldo_comp_entries[i];
5711 if (pflr_id(mirror_id, id) == lod_comp->llc_id)
5714 /* Found the ununsed ID */
5715 if (i == lo->ldo_comp_cnt)
5716 RETURN(pflr_id(mirror_id, id));
5718 if (end == LCME_ID_MAX) {
5720 end = min(lo->ldo_layout_gen & LCME_ID_MASK,
5721 (__u32)(LCME_ID_MAX - 1));
5725 RETURN(LCME_ID_INVAL);
5729 * Creation of a striped regular object.
5731 * The function is called to create the stripe objects for a regular
5732 * striped file. This can happen at the initial object creation or
5733 * when the caller asks LOD to do so using ->do_xattr_set() method
5734 * (so called late striping). Notice all the information are already
5735 * prepared in the form of the list of objects (ldo_stripe field).
5736 * This is done during declare phase.
5738 * \param[in] env execution environment
5739 * \param[in] dt object
5740 * \param[in] attr attributes the stripes will be created with
5741 * \param[in] dof format of stripes (see OSD API description)
5742 * \param[in] th transaction handle
5744 * \retval 0 on success
5745 * \retval negative if failed
5747 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
5748 struct lu_attr *attr, struct dt_object_format *dof,
5751 struct lod_layout_component *lod_comp;
5752 struct lod_object *lo = lod_dt_obj(dt);
5757 LASSERT((lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL) ||
5758 lo->ldo_is_foreign);
5760 mirror_id = 0; /* non-flr file's mirror_id is 0 */
5761 if (lo->ldo_mirror_count > 1) {
5762 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5763 lod_comp = &lo->ldo_comp_entries[i];
5764 if (lod_comp->llc_id != LCME_ID_INVAL &&
5765 mirror_id_of(lod_comp->llc_id) > mirror_id)
5766 mirror_id = mirror_id_of(lod_comp->llc_id);
5770 /* create all underlying objects */
5771 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5772 lod_comp = &lo->ldo_comp_entries[i];
5774 if (lod_comp->llc_id == LCME_ID_INVAL) {
5775 /* only the component of FLR layout with more than 1
5776 * mirror has mirror ID in its component ID.
5778 if (lod_comp->llc_extent.e_start == 0 &&
5779 lo->ldo_mirror_count > 1)
5782 lod_comp->llc_id = lod_gen_component_id(lo,
5784 if (lod_comp->llc_id == LCME_ID_INVAL)
5785 GOTO(out, rc = -ERANGE);
5788 if (lod_comp_inited(lod_comp))
5791 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
5792 lod_comp_set_init(lod_comp);
5794 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
5795 lod_comp_set_init(lod_comp);
5797 if (lod_comp->llc_stripe == NULL)
5800 LASSERT(lod_comp->llc_stripe_count);
5801 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
5802 struct dt_object *object = lod_comp->llc_stripe[j];
5803 LASSERT(object != NULL);
5804 rc = lod_sub_create(env, object, attr, NULL, dof, th);
5808 lod_comp_set_init(lod_comp);
5811 rc = lod_fill_mirrors(lo);
5815 rc = lod_generate_and_set_lovea(env, lo, th);
5819 lo->ldo_comp_cached = 1;
5823 lod_striping_free(env, lo);
5827 static inline bool lod_obj_is_dom(struct dt_object *dt)
5829 struct lod_object *lo = lod_dt_obj(dt);
5831 if (!dt_object_exists(dt_object_child(dt)))
5834 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
5837 if (!lo->ldo_comp_cnt)
5840 return (lov_pattern(lo->ldo_comp_entries[0].llc_pattern) ==
5845 * Implementation of dt_object_operations::do_create.
5847 * If any of preceeding methods (like ->do_declare_create(),
5848 * ->do_ah_init(), etc) chose to create a striped object,
5849 * then this method will create the master and the stripes.
5851 * \see dt_object_operations::do_create() in the API description for details.
5853 static int lod_create(const struct lu_env *env, struct dt_object *dt,
5854 struct lu_attr *attr, struct dt_allocation_hint *hint,
5855 struct dt_object_format *dof, struct thandle *th)
5860 /* create local object */
5861 rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
5865 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
5866 (lod_obj_is_striped(dt) || lod_obj_is_dom(dt)) &&
5867 dof->u.dof_reg.striped != 0) {
5868 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
5869 rc = lod_striped_create(env, dt, attr, dof, th);
5876 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
5877 struct dt_object *dt, struct thandle *th,
5878 int comp_idx, int stripe_idx,
5879 struct lod_obj_stripe_cb_data *data)
5881 if (data->locd_declare)
5882 return lod_sub_declare_destroy(env, dt, th);
5883 else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
5884 stripe_idx == cfs_fail_val)
5885 return lod_sub_destroy(env, dt, th);
5891 * Implementation of dt_object_operations::do_declare_destroy.
5893 * If the object is a striped directory, then the function declares reference
5894 * removal from the master object (this is an index) to the stripes and declares
5895 * destroy of all the stripes. In all the cases, it declares an intention to
5896 * destroy the object itself.
5898 * \see dt_object_operations::do_declare_destroy() in the API description
5901 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
5904 struct dt_object *next = dt_object_child(dt);
5905 struct lod_object *lo = lod_dt_obj(dt);
5906 struct lod_thread_info *info = lod_env_info(env);
5907 struct dt_object *stripe;
5908 char *stripe_name = info->lti_key;
5914 * load striping information, notice we don't do this when object
5915 * is being initialized as we don't need this information till
5916 * few specific cases like destroy, chown
5918 rc = lod_striping_load(env, lo);
5922 /* declare destroy for all underlying objects */
5923 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5924 rc = next->do_ops->do_index_try(env, next,
5925 &dt_directory_features);
5929 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5930 stripe = lo->ldo_stripe[i];
5934 rc = lod_sub_declare_ref_del(env, next, th);
5938 snprintf(stripe_name, sizeof(info->lti_key),
5940 PFID(lu_object_fid(&stripe->do_lu)), i);
5941 rc = lod_sub_declare_delete(env, next,
5942 (const struct dt_key *)stripe_name, th);
5949 * we declare destroy for the local object
5951 rc = lod_sub_declare_destroy(env, next, th);
5955 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
5956 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
5959 if (!lod_obj_is_striped(dt))
5962 /* declare destroy all striped objects */
5963 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5964 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5965 stripe = lo->ldo_stripe[i];
5969 if (!dt_object_exists(stripe))
5972 rc = lod_sub_declare_ref_del(env, stripe, th);
5976 rc = lod_sub_declare_destroy(env, stripe, th);
5981 struct lod_obj_stripe_cb_data data = { { 0 } };
5983 data.locd_declare = true;
5984 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
5985 rc = lod_obj_for_each_stripe(env, lo, th, &data);
5992 * Implementation of dt_object_operations::do_destroy.
5994 * If the object is a striped directory, then the function removes references
5995 * from the master object (this is an index) to the stripes and destroys all
5996 * the stripes. In all the cases, the function destroys the object itself.
5998 * \see dt_object_operations::do_destroy() in the API description for details.
6000 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
6003 struct dt_object *next = dt_object_child(dt);
6004 struct lod_object *lo = lod_dt_obj(dt);
6005 struct lod_thread_info *info = lod_env_info(env);
6006 char *stripe_name = info->lti_key;
6007 struct dt_object *stripe;
6013 /* destroy sub-stripe of master object */
6014 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6015 rc = next->do_ops->do_index_try(env, next,
6016 &dt_directory_features);
6020 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6021 stripe = lo->ldo_stripe[i];
6025 rc = lod_sub_ref_del(env, next, th);
6029 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
6030 PFID(lu_object_fid(&stripe->do_lu)), i);
6032 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
6033 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
6034 PFID(lu_object_fid(&stripe->do_lu)));
6036 rc = lod_sub_delete(env, next,
6037 (const struct dt_key *)stripe_name, th);
6043 rc = lod_sub_destroy(env, next, th);
6047 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
6048 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
6051 if (!lod_obj_is_striped(dt))
6054 /* destroy all striped objects */
6055 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6056 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6057 stripe = lo->ldo_stripe[i];
6061 if (!dt_object_exists(stripe))
6064 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
6065 i == cfs_fail_val) {
6066 dt_write_lock(env, stripe, DT_TGT_CHILD);
6067 rc = lod_sub_ref_del(env, stripe, th);
6068 dt_write_unlock(env, stripe);
6072 rc = lod_sub_destroy(env, stripe, th);
6078 struct lod_obj_stripe_cb_data data = { { 0 } };
6080 data.locd_declare = false;
6081 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6082 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6089 * Implementation of dt_object_operations::do_declare_ref_add.
6091 * \see dt_object_operations::do_declare_ref_add() in the API description
6094 static int lod_declare_ref_add(const struct lu_env *env,
6095 struct dt_object *dt, struct thandle *th)
6097 return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
6101 * Implementation of dt_object_operations::do_ref_add.
6103 * \see dt_object_operations::do_ref_add() in the API description for details.
6105 static int lod_ref_add(const struct lu_env *env,
6106 struct dt_object *dt, struct thandle *th)
6108 return lod_sub_ref_add(env, dt_object_child(dt), th);
6112 * Implementation of dt_object_operations::do_declare_ref_del.
6114 * \see dt_object_operations::do_declare_ref_del() in the API description
6117 static int lod_declare_ref_del(const struct lu_env *env,
6118 struct dt_object *dt, struct thandle *th)
6120 return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
6124 * Implementation of dt_object_operations::do_ref_del
6126 * \see dt_object_operations::do_ref_del() in the API description for details.
6128 static int lod_ref_del(const struct lu_env *env,
6129 struct dt_object *dt, struct thandle *th)
6131 return lod_sub_ref_del(env, dt_object_child(dt), th);
6135 * Implementation of dt_object_operations::do_object_sync.
6137 * \see dt_object_operations::do_object_sync() in the API description
6140 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
6141 __u64 start, __u64 end)
6143 return dt_object_sync(env, dt_object_child(dt), start, end);
6147 * Implementation of dt_object_operations::do_object_unlock.
6149 * Used to release LDLM lock(s).
6151 * \see dt_object_operations::do_object_unlock() in the API description
6154 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
6155 struct ldlm_enqueue_info *einfo,
6156 union ldlm_policy_data *policy)
6158 struct lod_object *lo = lod_dt_obj(dt);
6159 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
6160 int slave_locks_size;
6164 if (slave_locks == NULL)
6167 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
6168 /* Note: for remote lock for single stripe dir, MDT will cancel
6169 * the lock by lockh directly */
6170 LASSERT(!dt_object_remote(dt_object_child(dt)));
6172 /* locks were unlocked in MDT layer */
6173 for (i = 0; i < slave_locks->ha_count; i++)
6174 LASSERT(!lustre_handle_is_used(&slave_locks->ha_handles[i]));
6177 * NB, ha_count may not equal to ldo_dir_stripe_count, because dir
6178 * layout may change, e.g., shrink dir layout after migration.
6180 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6181 if (lo->ldo_stripe[i])
6182 dt_invalidate(env, lo->ldo_stripe[i]);
6185 slave_locks_size = offsetof(typeof(*slave_locks),
6186 ha_handles[slave_locks->ha_count]);
6187 OBD_FREE(slave_locks, slave_locks_size);
6188 einfo->ei_cbdata = NULL;
6194 * Implementation of dt_object_operations::do_object_lock.
6196 * Used to get LDLM lock on the non-striped and striped objects.
6198 * \see dt_object_operations::do_object_lock() in the API description
6201 static int lod_object_lock(const struct lu_env *env,
6202 struct dt_object *dt,
6203 struct lustre_handle *lh,
6204 struct ldlm_enqueue_info *einfo,
6205 union ldlm_policy_data *policy)
6207 struct lod_object *lo = lod_dt_obj(dt);
6208 int slave_locks_size;
6209 struct lustre_handle_array *slave_locks = NULL;
6214 /* remote object lock */
6215 if (!einfo->ei_enq_slave) {
6216 LASSERT(dt_object_remote(dt));
6217 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
6221 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
6224 rc = lod_striping_load(env, lo);
6229 if (lo->ldo_dir_stripe_count <= 1)
6232 slave_locks_size = offsetof(typeof(*slave_locks),
6233 ha_handles[lo->ldo_dir_stripe_count]);
6234 /* Freed in lod_object_unlock */
6235 OBD_ALLOC(slave_locks, slave_locks_size);
6238 slave_locks->ha_count = lo->ldo_dir_stripe_count;
6240 /* striped directory lock */
6241 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6242 struct lustre_handle lockh;
6243 struct ldlm_res_id *res_id;
6244 struct dt_object *stripe;
6246 stripe = lo->ldo_stripe[i];
6250 res_id = &lod_env_info(env)->lti_res_id;
6251 fid_build_reg_res_name(lu_object_fid(&stripe->do_lu), res_id);
6252 einfo->ei_res_id = res_id;
6254 if (dt_object_remote(stripe)) {
6255 set_bit(i, (void *)slave_locks->ha_map);
6256 rc = dt_object_lock(env, stripe, &lockh, einfo, policy);
6258 struct ldlm_namespace *ns = einfo->ei_namespace;
6259 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
6260 ldlm_completion_callback completion = einfo->ei_cb_cp;
6261 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
6263 if (einfo->ei_mode == LCK_PW ||
6264 einfo->ei_mode == LCK_EX)
6265 dlmflags |= LDLM_FL_COS_INCOMPAT;
6267 LASSERT(ns != NULL);
6268 rc = ldlm_cli_enqueue_local(env, ns, res_id, LDLM_IBITS,
6269 policy, einfo->ei_mode,
6270 &dlmflags, blocking,
6272 NULL, 0, LVB_T_NONE,
6277 ldlm_lock_decref_and_cancel(
6278 &slave_locks->ha_handles[i],
6280 OBD_FREE(slave_locks, slave_locks_size);
6283 slave_locks->ha_handles[i] = lockh;
6285 einfo->ei_cbdata = slave_locks;
6291 * Implementation of dt_object_operations::do_invalidate.
6293 * \see dt_object_operations::do_invalidate() in the API description for details
6295 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
6297 return dt_invalidate(env, dt_object_child(dt));
6300 static int lod_declare_instantiate_components(const struct lu_env *env,
6301 struct lod_object *lo,
6305 struct lod_thread_info *info = lod_env_info(env);
6310 LASSERT(info->lti_count < lo->ldo_comp_cnt);
6312 for (i = 0; i < info->lti_count; i++) {
6313 rc = lod_qos_prep_create(env, lo, NULL, th,
6314 info->lti_comp_idx[i], reserve);
6320 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6321 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
6322 &info->lti_buf, XATTR_NAME_LOV, 0, th);
6329 * Check OSTs for an existing component for further extension
6331 * Checks if OSTs are still healthy and not out of space. Gets free space
6332 * on OSTs (relative to allocation watermark rmb_low) and compares to
6333 * the proposed new_end for this component.
6335 * Decides whether or not to extend a component on its current OSTs.
6337 * \param[in] env execution environment for this thread
6338 * \param[in] lo object we're checking
6339 * \param[in] index index of this component
6340 * \param[in] extension_size extension size for this component
6341 * \param[in] extent layout extent for requested operation
6342 * \param[in] comp_extent extension component extent
6343 * \param[in] write if this is write operation
6345 * \retval true - OK to extend on current OSTs
6346 * \retval false - do not extend on current OSTs
6348 static bool lod_sel_osts_allowed(const struct lu_env *env,
6349 struct lod_object *lo,
6350 int index, __u64 reserve,
6351 struct lu_extent *extent,
6352 struct lu_extent *comp_extent, int write)
6354 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[index];
6355 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6356 struct lod_thread_info *tinfo = lod_env_info(env);
6357 struct obd_statfs *sfs = &tinfo->lti_osfs;
6358 __u64 available = 0;
6364 LASSERT(lod_comp->llc_stripe_count != 0);
6366 lod_getref(&lod->lod_ost_descs);
6367 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
6368 int index = lod_comp->llc_ost_indices[i];
6369 struct lod_tgt_desc *ost = OST_TGT(lod, index);
6370 struct obd_statfs_info info = { 0 };
6371 int j, repeated = 0;
6375 /* Get the number of times this OST repeats in this component.
6376 * Note: inter-component repeats are not counted as this is
6377 * considered as a rare case: we try to not repeat OST in other
6378 * components if possible. */
6379 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6380 if (index != lod_comp->llc_ost_indices[j])
6383 /* already handled */
6389 if (j < lod_comp->llc_stripe_count)
6392 if (!test_bit(index, lod->lod_ost_bitmap)) {
6393 CDEBUG(D_LAYOUT, "ost %d no longer present\n", index);
6398 rc = dt_statfs_info(env, ost->ltd_tgt, sfs, &info);
6400 CDEBUG(D_LAYOUT, "statfs failed for ost %d, error %d\n",
6406 if (sfs->os_state & OS_STATFS_ENOSPC ||
6407 sfs->os_state & OS_STATFS_READONLY ||
6408 sfs->os_state & OS_STATFS_DEGRADED) {
6409 CDEBUG(D_LAYOUT, "ost %d is not availble for SEL "
6410 "extension, state %u\n", index, sfs->os_state);
6416 available = sfs->os_bavail * sfs->os_bsize;
6417 /* 'available' is relative to the allocation threshold */
6418 available -= (__u64) info.os_reserved_mb_low << 20;
6420 CDEBUG(D_LAYOUT, "ost %d lowwm: %d highwm: %d, "
6421 "%llu %% blocks available, %llu %% blocks free\n",
6422 index, info.os_reserved_mb_low, info.os_reserved_mb_high,
6423 (100ull * sfs->os_bavail) / sfs->os_blocks,
6424 (100ull * sfs->os_bfree) / sfs->os_blocks);
6426 if (reserve * repeated > available) {
6428 CDEBUG(D_LAYOUT, "low space on ost %d, available %llu "
6429 "< extension size %llu repeated %d\n", index,
6430 available, reserve, repeated);
6434 lod_putref(lod, &lod->lod_ost_descs);
6440 * Adjust extents after component removal
6442 * When we remove an extension component, we move the start of the next
6443 * component to match the start of the extension component, so no space is left
6446 * \param[in] env execution environment for this thread
6447 * \param[in] lo object
6448 * \param[in] max_comp layout component
6449 * \param[in] index index of this component
6451 * \retval 0 on success
6452 * \retval negative errno on error
6454 static void lod_sel_adjust_extents(const struct lu_env *env,
6455 struct lod_object *lo,
6456 int max_comp, int index)
6458 struct lod_layout_component *lod_comp = NULL;
6459 struct lod_layout_component *next = NULL;
6460 struct lod_layout_component *prev = NULL;
6461 __u64 new_start = 0;
6465 /* Extension space component */
6466 lod_comp = &lo->ldo_comp_entries[index];
6467 next = &lo->ldo_comp_entries[index + 1];
6468 prev = &lo->ldo_comp_entries[index - 1];
6470 LASSERT(lod_comp != NULL && prev != NULL && next != NULL);
6471 LASSERT(lod_comp->llc_flags & LCME_FL_EXTENSION);
6473 /* Previous is being removed */
6474 if (prev && prev->llc_id == LCME_ID_INVAL)
6475 new_start = prev->llc_extent.e_start;
6477 new_start = lod_comp->llc_extent.e_start;
6479 for (i = index + 1; i < max_comp; i++) {
6480 lod_comp = &lo->ldo_comp_entries[i];
6482 start = lod_comp->llc_extent.e_start;
6483 lod_comp->llc_extent.e_start = new_start;
6485 /* We only move zero length extendable components */
6486 if (!(start == lod_comp->llc_extent.e_end))
6489 LASSERT(!(lod_comp->llc_flags & LCME_FL_INIT));
6491 lod_comp->llc_extent.e_end = new_start;
6495 /* Calculate the proposed 'new end' for a component we're extending */
6496 static __u64 lod_extension_new_end(__u64 extension_size, __u64 extent_end,
6497 __u32 stripe_size, __u64 component_end,
6498 __u64 extension_end)
6502 LASSERT(extension_size != 0 && stripe_size != 0);
6504 /* Round up to extension size */
6505 if (extent_end == OBD_OBJECT_EOF) {
6506 new_end = OBD_OBJECT_EOF;
6508 /* Add at least extension_size to the previous component_end,
6509 * covering the req layout extent */
6510 new_end = max(extent_end - component_end, extension_size);
6511 new_end = roundup(new_end, extension_size);
6512 new_end += component_end;
6514 /* Component end must be min stripe size aligned */
6515 if (new_end % stripe_size) {
6516 CDEBUG(D_LAYOUT, "new component end is not aligned "
6517 "by the stripe size %u: [%llu, %llu) ext size "
6518 "%llu new end %llu, aligning\n",
6519 stripe_size, component_end, extent_end,
6520 extension_size, new_end);
6521 new_end = roundup(new_end, stripe_size);
6525 if (new_end < extent_end)
6526 new_end = OBD_OBJECT_EOF;
6529 /* Don't extend past the end of the extension component */
6530 if (new_end > extension_end)
6531 new_end = extension_end;
6537 * Calculate the exact reservation (per-OST extension_size) on the OSTs being
6538 * instantiated. It needs to be calculated in advance and taken into account at
6539 * the instantiation time, because otherwise lod_statfs_and_check() may consider
6540 * an OST as OK, but SEL needs its extension_size to fit the free space and the
6541 * OST may turn out to be low-on-space, thus inappropriate OST may be used and
6544 * \param[in] lod_comp lod component we are checking
6546 * \retval size to reserved on each OST of lod_comp's stripe.
6548 static __u64 lod_sel_stripe_reserved(struct lod_layout_component *lod_comp)
6550 /* extension_size is file level, so we must divide by stripe count to
6551 * compare it to available space on a single OST */
6552 return lod_comp->llc_stripe_size * SEL_UNIT_SIZE /
6553 lod_comp->llc_stripe_count;
6556 /* As lod_sel_handler() could be re-entered for the same component several
6557 * times, this is the data for the next call. Fields could be changed to
6558 * component indexes when needed, (e.g. if there is no need to instantiate
6559 * all the previous components up to the current position) to tell the caller
6560 * where to start over from. */
6567 * Process extent updates for a particular layout component
6569 * Handle layout updates for a particular extension space component touched by
6570 * a layout update operation. Core function of self-extending PFL feature.
6572 * In general, this function processes exactly *one* stage of an extension
6573 * operation, modifying the layout accordingly, then returns to the caller.
6574 * The caller is responsible for restarting processing with the new layout,
6575 * which may repeatedly return to this function until the extension updates
6578 * This function does one of a few things to the layout:
6579 * 1. Extends the component before the current extension space component to
6580 * allow it to accomodate the requested operation (if space/policy permit that
6581 * component to continue on its current OSTs)
6583 * 2. If extension of the existing component fails, we do one of two things:
6584 * a. If there is a component after the extension space, we remove the
6585 * extension space component, move the start of the next component down
6586 * accordingly, then notify the caller to restart processing w/the new
6588 * b. If there is no following component, we try repeating the current
6589 * component, creating a new component using the current one as a
6590 * template (keeping its stripe properties but not specific striping),
6591 * and try assigning striping for this component. If there is sufficient
6592 * free space on the OSTs chosen for this component, it is instantiated
6593 * and i/o continues there.
6595 * If there is not sufficient space on the new OSTs, we remove this new
6596 * component & extend the current component.
6598 * Note further that uninited components followed by extension space can be zero
6599 * length meaning that we will try to extend them before initializing them, and
6600 * if that fails, they will be removed without initialization.
6602 * 3. If we extend to/beyond the end of an extension space component, that
6603 * component is exhausted (all of its range has been given to real components),
6604 * so we remove it and restart processing.
6606 * \param[in] env execution environment for this thread
6607 * \param[in,out] lo object to update the layout of
6608 * \param[in] extent layout extent for requested operation, update
6609 * layout to fit this operation
6610 * \param[in] th transaction handle for this operation
6611 * \param[in,out] max_comp the highest comp for the portion of the layout
6612 * we are operating on (For FLR, the chosen
6613 * replica). Updated because we may remove
6615 * \param[in] index index of the extension space component we're
6617 * \param[in] write if this is write op
6618 * \param[in,out] force if the extension is to be forced; set here
6619 to force it on the 2nd call for the same
6622 * \retval 0 on success
6623 * \retval negative errno on error
6625 static int lod_sel_handler(const struct lu_env *env,
6626 struct lod_object *lo,
6627 struct lu_extent *extent,
6628 struct thandle *th, int *max_comp,
6629 int index, int write,
6630 struct sel_data *sd)
6632 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6633 struct lod_thread_info *info = lod_env_info(env);
6634 struct lod_layout_component *lod_comp;
6635 struct lod_layout_component *prev;
6636 struct lod_layout_component *next = NULL;
6637 __u64 extension_size, reserve;
6644 /* First component cannot be extension space */
6646 CERROR("%s: "DFID" first component cannot be extension space\n",
6647 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6651 lod_comp = &lo->ldo_comp_entries[index];
6652 prev = &lo->ldo_comp_entries[index - 1];
6653 if ((index + 1) < *max_comp)
6654 next = &lo->ldo_comp_entries[index + 1];
6656 /* extension size uses the stripe size field as KiB */
6657 extension_size = lod_comp->llc_stripe_size * SEL_UNIT_SIZE;
6659 CDEBUG(D_LAYOUT, "prev start %llu, extension start %llu, extension end"
6660 " %llu, extension size %llu\n", prev->llc_extent.e_start,
6661 lod_comp->llc_extent.e_start, lod_comp->llc_extent.e_end,
6664 /* Two extension space components cannot be adjacent & extension space
6665 * components cannot be init */
6666 if ((prev->llc_flags & LCME_FL_EXTENSION) ||
6667 !(ergo(next, !(next->llc_flags & LCME_FL_EXTENSION))) ||
6668 lod_comp_inited(lod_comp)) {
6669 CERROR("%s: "DFID" invalid extension space components\n",
6670 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6674 reserve = lod_sel_stripe_reserved(lod_comp);
6676 if (!prev->llc_stripe) {
6677 CDEBUG(D_LAYOUT, "Previous component not inited\n");
6678 info->lti_count = 1;
6679 info->lti_comp_idx[0] = index - 1;
6680 rc = lod_declare_instantiate_components(env, lo, th, reserve);
6681 /* ENOSPC tells us we can't use this component. If there is
6682 * a next or we are repeating, we either spill over (next) or
6683 * extend the original comp (repeat). Otherwise, return the
6684 * error to the user. */
6685 if (rc == -ENOSPC && (next || sd->sd_repeat))
6691 if (sd->sd_force == 0 && rc == 0)
6692 rc = !lod_sel_osts_allowed(env, lo, index - 1, reserve, extent,
6693 &lod_comp->llc_extent, write);
6695 repeated = !!(sd->sd_repeat);
6699 /* Extend previous component */
6701 new_end = lod_extension_new_end(extension_size, extent->e_end,
6702 prev->llc_stripe_size,
6703 prev->llc_extent.e_end,
6704 lod_comp->llc_extent.e_end);
6706 CDEBUG(D_LAYOUT, "new end %llu\n", new_end);
6707 lod_comp->llc_extent.e_start = new_end;
6708 prev->llc_extent.e_end = new_end;
6710 if (prev->llc_extent.e_end == lod_comp->llc_extent.e_end) {
6711 CDEBUG(D_LAYOUT, "Extension component exhausted\n");
6712 lod_comp->llc_id = LCME_ID_INVAL;
6716 /* rc == 1, failed to extend current component */
6719 /* Normal 'spillover' case - Remove the extension
6720 * space component & bring down the start of the next
6722 lod_comp->llc_id = LCME_ID_INVAL;
6724 if (!(prev->llc_flags & LCME_FL_INIT)) {
6725 prev->llc_id = LCME_ID_INVAL;
6728 lod_sel_adjust_extents(env, lo, *max_comp, index);
6729 } else if (lod_comp_inited(prev)) {
6730 /* If there is no next, and the previous component is
6731 * INIT'ed, try repeating the previous component. */
6732 LASSERT(repeated == 0);
6733 rc = lod_layout_repeat_comp(env, lo, index - 1);
6737 /* The previous component is a repeated component.
6738 * Record this so we don't keep trying to repeat it. */
6741 /* If the previous component is not INIT'ed, this may
6742 * be a component we have just instantiated but failed
6743 * to extend. Or even a repeated component we failed
6744 * to prepare a striping for. Do not repeat but instead
6745 * remove the repeated component & force the extention
6746 * of the original one */
6749 prev->llc_id = LCME_ID_INVAL;
6756 rc = lod_layout_del_prep_layout(env, lo, NULL);
6759 LASSERTF(-rc == change,
6760 "number deleted %d != requested %d\n", -rc,
6763 *max_comp = *max_comp + change;
6765 /* lod_del_prep_layout reallocates ldo_comp_entries, so we must
6766 * refresh these pointers before using them */
6767 lod_comp = &lo->ldo_comp_entries[index];
6768 prev = &lo->ldo_comp_entries[index - 1];
6769 CDEBUG(D_LAYOUT, "After extent updates: prev start %llu, current start "
6770 "%llu, current end %llu max_comp %d ldo_comp_cnt %d\n",
6771 prev->llc_extent.e_start, lod_comp->llc_extent.e_start,
6772 lod_comp->llc_extent.e_end, *max_comp, lo->ldo_comp_cnt);
6774 /* Layout changed successfully */
6779 * Declare layout extent updates
6781 * Handles extensions. Identifies extension components touched by current
6782 * operation and passes them to processing function.
6784 * Restarts with updated layouts from the processing function until the current
6785 * operation no longer touches an extension space component.
6787 * \param[in] env execution environment for this thread
6788 * \param[in,out] lo object to update the layout of
6789 * \param[in] extent layout extent for requested operation, update layout to
6790 * fit this operation
6791 * \param[in] th transaction handle for this operation
6792 * \param[in] pick identifies chosen mirror for FLR layouts
6793 * \param[in] write if this is write op
6795 * \retval 1 on layout changed, 0 on no change
6796 * \retval negative errno on error
6798 static int lod_declare_update_extents(const struct lu_env *env,
6799 struct lod_object *lo, struct lu_extent *extent,
6800 struct thandle *th, int pick, int write)
6802 struct lod_thread_info *info = lod_env_info(env);
6803 struct lod_layout_component *lod_comp;
6804 bool layout_changed = false;
6805 struct sel_data sd = { 0 };
6813 /* This makes us work on the components of the chosen mirror */
6814 start_index = lo->ldo_mirrors[pick].lme_start;
6815 max_comp = lo->ldo_mirrors[pick].lme_end + 1;
6816 if (lo->ldo_flr_state == LCM_FL_NONE)
6817 LASSERT(start_index == 0 && max_comp == lo->ldo_comp_cnt);
6819 CDEBUG(D_LAYOUT, "extent->e_start %llu, extent->e_end %llu\n",
6820 extent->e_start, extent->e_end);
6821 for (i = start_index; i < max_comp; i++) {
6822 lod_comp = &lo->ldo_comp_entries[i];
6824 /* We've passed all components of interest */
6825 if (lod_comp->llc_extent.e_start >= extent->e_end)
6828 if (lod_comp->llc_flags & LCME_FL_EXTENSION) {
6829 layout_changed = true;
6830 rc = lod_sel_handler(env, lo, extent, th, &max_comp,
6835 /* Nothing has changed behind the prev one */
6841 /* We may have added or removed components. If so, we must update the
6842 * start & ends of all the mirrors after the current one, and the end
6843 * of the current mirror. */
6844 change = max_comp - 1 - lo->ldo_mirrors[pick].lme_end;
6846 lo->ldo_mirrors[pick].lme_end += change;
6847 for (i = pick + 1; i < lo->ldo_mirror_count; i++) {
6848 lo->ldo_mirrors[i].lme_start += change;
6849 lo->ldo_mirrors[i].lme_end += change;
6855 /* The amount of components has changed, adjust the lti_comp_idx */
6856 rc2 = lod_layout_data_init(info, lo->ldo_comp_cnt);
6858 return rc < 0 ? rc : rc2 < 0 ? rc2 : layout_changed;
6861 /* If striping is already instantiated or INIT'ed DOM? */
6862 static bool lod_is_instantiation_needed(struct lod_layout_component *comp)
6864 return !(((lov_pattern(comp->llc_pattern) == LOV_PATTERN_MDT) &&
6865 lod_comp_inited(comp)) || comp->llc_stripe);
6869 * Declare layout update for a non-FLR layout.
6871 * \param[in] env execution environment for this thread
6872 * \param[in,out] lo object to update the layout of
6873 * \param[in] layout layout intent for requested operation, "update" is
6874 * a process of reacting to this
6875 * \param[in] buf buffer containing lov ea (see comment on usage inline)
6876 * \param[in] th transaction handle for this operation
6878 * \retval 0 on success
6879 * \retval negative errno on error
6881 static int lod_declare_update_plain(const struct lu_env *env,
6882 struct lod_object *lo, struct layout_intent *layout,
6883 const struct lu_buf *buf, struct thandle *th)
6885 struct lod_thread_info *info = lod_env_info(env);
6886 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6887 struct lod_layout_component *lod_comp;
6888 struct lov_comp_md_v1 *comp_v1 = NULL;
6889 bool layout_changed = false;
6890 bool replay = false;
6894 LASSERT(lo->ldo_flr_state == LCM_FL_NONE);
6897 * In case the client is passing lovea, which only happens during
6898 * the replay of layout intent write RPC for now, we may need to
6899 * parse the lovea and apply new layout configuration.
6901 if (buf && buf->lb_len) {
6902 struct lov_user_md_v1 *v1 = buf->lb_buf;
6904 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
6905 v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
6906 LOV_MAGIC_COMP_V1)) {
6907 CERROR("%s: the replay buffer of layout extend "
6908 "(magic %#x) does not contain expected "
6909 "composite layout.\n",
6910 lod2obd(d)->obd_name, v1->lmm_magic);
6911 GOTO(out, rc = -EINVAL);
6914 rc = lod_use_defined_striping(env, lo, buf);
6917 lo->ldo_comp_cached = 1;
6919 rc = lod_get_lov_ea(env, lo);
6922 /* old on-disk EA is stored in info->lti_buf */
6923 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
6925 layout_changed = true;
6927 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
6931 /* non replay path */
6932 rc = lod_striping_load(env, lo);
6937 /* Make sure defined layout covers the requested write range. */
6938 lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
6939 if (lo->ldo_comp_cnt > 1 &&
6940 lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
6941 lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
6942 CDEBUG_LIMIT(replay ? D_ERROR : D_LAYOUT,
6943 "%s: the defined layout [0, %#llx) does not "
6944 "covers the write range "DEXT"\n",
6945 lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
6946 PEXT(&layout->li_extent));
6947 GOTO(out, rc = -EINVAL);
6950 CDEBUG(D_LAYOUT, "%s: "DFID": update components "DEXT"\n",
6951 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
6952 PEXT(&layout->li_extent));
6955 rc = lod_declare_update_extents(env, lo, &layout->li_extent,
6956 th, 0, layout->li_opc == LAYOUT_INTENT_WRITE);
6960 layout_changed = true;
6964 * Iterate ld->ldo_comp_entries, find the component whose extent under
6965 * the write range and not instantianted.
6967 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6968 lod_comp = &lo->ldo_comp_entries[i];
6970 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
6974 /* If striping is instantiated or INIT'ed DOM skip */
6975 if (!lod_is_instantiation_needed(lod_comp))
6979 * In replay path, lod_comp is the EA passed by
6980 * client replay buffer, comp_v1 is the pre-recovery
6981 * on-disk EA, we'd sift out those components which
6982 * were init-ed in the on-disk EA.
6984 if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
6989 * this component hasn't instantiated in normal path, or during
6990 * replay it needs replay the instantiation.
6993 /* A released component is being extended */
6994 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
6995 GOTO(out, rc = -EINVAL);
6997 LASSERT(info->lti_comp_idx != NULL);
6998 info->lti_comp_idx[info->lti_count++] = i;
6999 layout_changed = true;
7002 if (!layout_changed)
7005 lod_obj_inc_layout_gen(lo);
7006 rc = lod_declare_instantiate_components(env, lo, th, 0);
7010 lod_striping_free(env, lo);
7014 static inline int lod_comp_index(struct lod_object *lo,
7015 struct lod_layout_component *lod_comp)
7017 LASSERT(lod_comp >= lo->ldo_comp_entries &&
7018 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
7020 return lod_comp - lo->ldo_comp_entries;
7024 * Stale other mirrors by writing extent.
7026 static int lod_stale_components(const struct lu_env *env, struct lod_object *lo,
7027 int primary, struct lu_extent *extent,
7030 struct lod_layout_component *pri_comp, *lod_comp;
7031 struct lu_extent pri_extent;
7036 /* The writing extent decides which components in the primary
7037 * are affected... */
7038 CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
7041 lod_foreach_mirror_comp(pri_comp, lo, primary) {
7042 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
7045 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
7046 lod_comp_index(lo, pri_comp),
7047 PEXT(&pri_comp->llc_extent));
7049 pri_extent.e_start = pri_comp->llc_extent.e_start;
7050 pri_extent.e_end = pri_comp->llc_extent.e_end;
7052 for (i = 0; i < lo->ldo_mirror_count; i++) {
7055 rc = lod_declare_update_extents(env, lo, &pri_extent,
7057 /* if update_extents changed the layout, it may have
7058 * reallocated the component array, so start over to
7059 * avoid using stale pointers */
7065 /* ... and then stale other components that are
7066 * overlapping with primary components */
7067 lod_foreach_mirror_comp(lod_comp, lo, i) {
7068 if (!lu_extent_is_overlapped(
7070 &lod_comp->llc_extent))
7073 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
7074 i, lod_comp_index(lo, lod_comp));
7076 lod_comp->llc_flags |= LCME_FL_STALE;
7077 lo->ldo_mirrors[i].lme_stale = 1;
7086 * check an OST's availability
7087 * \param[in] env execution environment
7088 * \param[in] lo lod object
7089 * \param[in] dt dt object
7090 * \param[in] index mirror index
7092 * \retval negative if failed
7093 * \retval 1 if \a dt is available
7094 * \retval 0 if \a dt is not available
7096 static inline int lod_check_ost_avail(const struct lu_env *env,
7097 struct lod_object *lo,
7098 struct dt_object *dt, int index)
7100 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7101 struct lod_tgt_desc *ost;
7103 int type = LU_SEQ_RANGE_OST;
7106 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &idx, &type);
7108 CERROR("%s: can't locate "DFID":rc = %d\n",
7109 lod2obd(lod)->obd_name, PFID(lu_object_fid(&dt->do_lu)),
7114 ost = OST_TGT(lod, idx);
7115 if (ost->ltd_statfs.os_state &
7116 (OS_STATFS_READONLY | OS_STATFS_ENOSPC | OS_STATFS_ENOINO |
7117 OS_STATFS_NOPRECREATE) ||
7118 ost->ltd_active == 0) {
7119 CDEBUG(D_LAYOUT, DFID ": mirror %d OST%d unavail, rc = %d\n",
7120 PFID(lod_object_fid(lo)), index, idx, rc);
7128 * Pick primary mirror for write
7129 * \param[in] env execution environment
7130 * \param[in] lo object
7131 * \param[in] extent write range
7133 static int lod_primary_pick(const struct lu_env *env, struct lod_object *lo,
7134 struct lu_extent *extent)
7136 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7137 unsigned int seq = 0;
7138 struct lod_layout_component *lod_comp;
7140 int picked = -1, second_pick = -1, third_pick = -1;
7143 if (OBD_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
7144 get_random_bytes(&seq, sizeof(seq));
7145 seq %= lo->ldo_mirror_count;
7149 * Pick a mirror as the primary, and check the availability of OSTs.
7151 * This algo can be revised later after knowing the topology of
7154 lod_qos_statfs_update(env, lod, &lod->lod_ost_descs);
7155 for (i = 0; i < lo->ldo_mirror_count; i++) {
7156 bool ost_avail = true;
7157 int index = (i + seq) % lo->ldo_mirror_count;
7159 if (lo->ldo_mirrors[index].lme_stale) {
7160 CDEBUG(D_LAYOUT, DFID": mirror %d stale\n",
7161 PFID(lod_object_fid(lo)), index);
7165 /* 2nd pick is for the primary mirror containing unavail OST */
7166 if (lo->ldo_mirrors[index].lme_primary && second_pick < 0)
7167 second_pick = index;
7169 /* 3rd pick is for non-primary mirror containing unavail OST */
7170 if (second_pick < 0 && third_pick < 0)
7174 * we found a non-primary 1st pick, we'd like to find a
7175 * potential pirmary mirror.
7177 if (picked >= 0 && !lo->ldo_mirrors[index].lme_primary)
7180 /* check the availability of OSTs */
7181 lod_foreach_mirror_comp(lod_comp, lo, index) {
7182 if (!lod_comp_inited(lod_comp) || !lod_comp->llc_stripe)
7185 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
7186 struct dt_object *dt = lod_comp->llc_stripe[j];
7188 rc = lod_check_ost_avail(env, lo, dt, index);
7195 } /* for all dt object in one component */
7198 } /* for all components in a mirror */
7201 * the OSTs where allocated objects locates in the components
7202 * of the mirror are available.
7207 /* this mirror has all OSTs available */
7211 * primary with all OSTs are available, this is the perfect
7214 if (lo->ldo_mirrors[index].lme_primary)
7216 } /* for all mirrors */
7218 /* failed to pick a sound mirror, lower our expectation */
7220 picked = second_pick;
7222 picked = third_pick;
7229 static int lod_prepare_resync_mirror(const struct lu_env *env,
7230 struct lod_object *lo,
7233 struct lod_thread_info *info = lod_env_info(env);
7234 struct lod_layout_component *lod_comp;
7235 bool neg = !!(MIRROR_ID_NEG & mirror_id);
7238 mirror_id &= ~MIRROR_ID_NEG;
7240 for (i = 0; i < lo->ldo_mirror_count; i++) {
7241 if ((!neg && lo->ldo_mirrors[i].lme_id != mirror_id) ||
7242 (neg && lo->ldo_mirrors[i].lme_id == mirror_id))
7245 lod_foreach_mirror_comp(lod_comp, lo, i) {
7246 if (lod_comp_inited(lod_comp))
7249 info->lti_comp_idx[info->lti_count++] =
7250 lod_comp_index(lo, lod_comp);
7258 * figure out the components should be instantiated for resync.
7260 static int lod_prepare_resync(const struct lu_env *env, struct lod_object *lo,
7261 struct lu_extent *extent)
7263 struct lod_thread_info *info = lod_env_info(env);
7264 struct lod_layout_component *lod_comp;
7265 unsigned int need_sync = 0;
7269 DFID": instantiate all stale components in "DEXT"\n",
7270 PFID(lod_object_fid(lo)), PEXT(extent));
7273 * instantiate all components within this extent, even non-stale
7276 for (i = 0; i < lo->ldo_mirror_count; i++) {
7277 if (!lo->ldo_mirrors[i].lme_stale)
7280 lod_foreach_mirror_comp(lod_comp, lo, i) {
7281 if (!lu_extent_is_overlapped(extent,
7282 &lod_comp->llc_extent))
7287 if (lod_comp_inited(lod_comp))
7290 CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
7291 i, lod_comp_index(lo, lod_comp));
7292 info->lti_comp_idx[info->lti_count++] =
7293 lod_comp_index(lo, lod_comp);
7297 return need_sync ? 0 : -EALREADY;
7300 static int lod_declare_update_rdonly(const struct lu_env *env,
7301 struct lod_object *lo, struct md_layout_change *mlc,
7304 struct lod_thread_info *info = lod_env_info(env);
7305 struct lu_attr *layout_attr = &info->lti_layout_attr;
7306 struct lod_layout_component *lod_comp;
7307 struct lu_extent extent = { 0 };
7311 LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
7312 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7313 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7314 LASSERT(lo->ldo_mirror_count > 0);
7316 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7317 struct layout_intent *layout = mlc->mlc_intent;
7318 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7321 extent = layout->li_extent;
7322 CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
7323 PFID(lod_object_fid(lo)), PEXT(&extent));
7325 picked = lod_primary_pick(env, lo, &extent);
7329 CDEBUG(D_LAYOUT, DFID": picked mirror id %u as primary\n",
7330 PFID(lod_object_fid(lo)),
7331 lo->ldo_mirrors[picked].lme_id);
7333 /* Update extents of primary before staling */
7334 rc = lod_declare_update_extents(env, lo, &extent, th, picked,
7339 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
7341 * trunc transfers [0, size) in the intent extent, we'd
7342 * stale components overlapping [size, eof).
7344 extent.e_start = extent.e_end;
7345 extent.e_end = OBD_OBJECT_EOF;
7348 /* stale overlapping components from other mirrors */
7349 rc = lod_stale_components(env, lo, picked, &extent, th);
7353 /* restore truncate intent extent */
7354 if (layout->li_opc == LAYOUT_INTENT_TRUNC)
7355 extent.e_end = extent.e_start;
7357 /* instantiate components for the picked mirror, start from 0 */
7360 lod_foreach_mirror_comp(lod_comp, lo, picked) {
7361 if (!lu_extent_is_overlapped(&extent,
7362 &lod_comp->llc_extent))
7365 if (!lod_is_instantiation_needed(lod_comp))
7368 info->lti_comp_idx[info->lti_count++] =
7369 lod_comp_index(lo, lod_comp);
7372 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7373 } else { /* MD_LAYOUT_RESYNC */
7377 * could contain multiple non-stale mirrors, so we need to
7378 * prep uninited all components assuming any non-stale mirror
7379 * could be picked as the primary mirror.
7381 if (mlc->mlc_mirror_id == 0) {
7383 for (i = 0; i < lo->ldo_mirror_count; i++) {
7384 if (lo->ldo_mirrors[i].lme_stale)
7387 lod_foreach_mirror_comp(lod_comp, lo, i) {
7388 if (!lod_comp_inited(lod_comp))
7392 lod_comp->llc_extent.e_end)
7394 lod_comp->llc_extent.e_end;
7397 rc = lod_prepare_resync(env, lo, &extent);
7401 /* mirror write, try to init its all components */
7402 rc = lod_prepare_resync_mirror(env, lo,
7403 mlc->mlc_mirror_id);
7408 /* change the file state to SYNC_PENDING */
7409 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7412 /* Reset the layout version once it's becoming too large.
7413 * This way it can make sure that the layout version is
7414 * monotonously increased in this writing era. */
7415 lod_obj_inc_layout_gen(lo);
7416 if (lo->ldo_layout_gen > (LCME_ID_MAX >> 1)) {
7417 __u32 layout_version;
7419 get_random_bytes(&layout_version, sizeof(layout_version));
7420 lo->ldo_layout_gen = layout_version & 0xffff;
7423 rc = lod_declare_instantiate_components(env, lo, th, 0);
7427 layout_attr->la_valid = LA_LAYOUT_VERSION;
7428 layout_attr->la_layout_version = 0; /* set current version */
7429 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7430 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7431 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7437 lod_striping_free(env, lo);
7441 static int lod_declare_update_write_pending(const struct lu_env *env,
7442 struct lod_object *lo, struct md_layout_change *mlc,
7445 struct lod_thread_info *info = lod_env_info(env);
7446 struct lu_attr *layout_attr = &info->lti_layout_attr;
7447 struct lod_layout_component *lod_comp;
7448 struct lu_extent extent = { 0 };
7454 LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
7455 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7456 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7458 /* look for the primary mirror */
7459 for (i = 0; i < lo->ldo_mirror_count; i++) {
7460 if (lo->ldo_mirrors[i].lme_stale)
7463 LASSERTF(primary < 0, DFID " has multiple primary: %u / %u\n",
7464 PFID(lod_object_fid(lo)),
7465 lo->ldo_mirrors[i].lme_id,
7466 lo->ldo_mirrors[primary].lme_id);
7471 CERROR(DFID ": doesn't have a primary mirror\n",
7472 PFID(lod_object_fid(lo)));
7473 GOTO(out, rc = -ENODATA);
7476 CDEBUG(D_LAYOUT, DFID": found primary %u\n",
7477 PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
7479 LASSERT(!lo->ldo_mirrors[primary].lme_stale);
7481 /* for LAYOUT_WRITE opc, it has to do the following operations:
7482 * 1. stale overlapping componets from stale mirrors;
7483 * 2. instantiate components of the primary mirror;
7484 * 3. transfter layout version to all objects of the primary;
7486 * for LAYOUT_RESYNC opc, it will do:
7487 * 1. instantiate components of all stale mirrors;
7488 * 2. transfer layout version to all objects to close write era. */
7490 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7491 struct layout_intent *layout = mlc->mlc_intent;
7492 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7494 LASSERT(mlc->mlc_intent != NULL);
7496 extent = mlc->mlc_intent->li_extent;
7498 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
7499 PFID(lod_object_fid(lo)), PEXT(&extent));
7501 /* 1. Update extents of primary before staling */
7502 rc = lod_declare_update_extents(env, lo, &extent, th, primary,
7507 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
7509 * trunc transfers [0, size) in the intent extent, we'd
7510 * stale components overlapping [size, eof).
7512 extent.e_start = extent.e_end;
7513 extent.e_end = OBD_OBJECT_EOF;
7516 /* 2. stale overlapping components */
7517 rc = lod_stale_components(env, lo, primary, &extent, th);
7521 /* 3. find the components which need instantiating.
7522 * instantiate [0, mlc->mlc_intent->e_end) */
7524 /* restore truncate intent extent */
7525 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC)
7526 extent.e_end = extent.e_start;
7529 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7530 if (!lu_extent_is_overlapped(&extent,
7531 &lod_comp->llc_extent))
7534 if (!lod_is_instantiation_needed(lod_comp))
7537 CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
7538 primary, lod_comp_index(lo, lod_comp));
7539 info->lti_comp_idx[info->lti_count++] =
7540 lod_comp_index(lo, lod_comp);
7542 } else { /* MD_LAYOUT_RESYNC */
7543 if (mlc->mlc_mirror_id == 0) {
7545 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7546 if (!lod_comp_inited(lod_comp))
7549 extent.e_end = lod_comp->llc_extent.e_end;
7552 rc = lod_prepare_resync(env, lo, &extent);
7556 /* mirror write, try to init its all components */
7557 rc = lod_prepare_resync_mirror(env, lo,
7558 mlc->mlc_mirror_id);
7563 /* change the file state to SYNC_PENDING */
7564 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7567 rc = lod_declare_instantiate_components(env, lo, th, 0);
7571 /* 3. transfer layout version to OST objects.
7572 * transfer new layout version to OST objects so that stale writes
7573 * can be denied. It also ends an era of writing by setting
7574 * LU_LAYOUT_RESYNC. Normal client can never use this bit to
7575 * send write RPC; only resync RPCs could do it. */
7576 layout_attr->la_valid = LA_LAYOUT_VERSION;
7577 layout_attr->la_layout_version = 0; /* set current version */
7578 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7579 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7580 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7584 lod_obj_inc_layout_gen(lo);
7587 lod_striping_free(env, lo);
7591 static int lod_declare_update_sync_pending(const struct lu_env *env,
7592 struct lod_object *lo, struct md_layout_change *mlc,
7595 struct lod_thread_info *info = lod_env_info(env);
7596 unsigned sync_components = 0;
7597 unsigned resync_components = 0;
7602 LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
7603 LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
7604 mlc->mlc_opc == MD_LAYOUT_WRITE);
7606 CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
7607 PFID(lod_object_fid(lo)), mlc->mlc_opc);
7609 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7610 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
7611 PFID(lod_object_fid(lo)));
7613 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7614 return lod_declare_update_write_pending(env, lo, mlc, th);
7617 /* MD_LAYOUT_RESYNC_DONE */
7619 for (i = 0; i < lo->ldo_comp_cnt; i++) {
7620 struct lod_layout_component *lod_comp;
7623 lod_comp = &lo->ldo_comp_entries[i];
7625 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
7630 for (j = 0; j < mlc->mlc_resync_count; j++) {
7631 if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
7634 mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
7635 lod_comp->llc_flags &= ~LCME_FL_STALE;
7636 resync_components++;
7642 for (i = 0; i < mlc->mlc_resync_count; i++) {
7643 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
7646 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
7647 "or already synced\n", PFID(lod_object_fid(lo)),
7648 mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
7649 GOTO(out, rc = -EINVAL);
7652 if (!sync_components || (mlc->mlc_resync_count && !resync_components)) {
7653 CDEBUG(D_LAYOUT, DFID": no mirror in sync\n",
7654 PFID(lod_object_fid(lo)));
7656 /* tend to return an error code here to prevent
7657 * the MDT from setting SoM attribute */
7658 GOTO(out, rc = -EINVAL);
7661 CDEBUG(D_LAYOUT, DFID": synced %u resynced %u/%zu components\n",
7662 PFID(lod_object_fid(lo)),
7663 sync_components, resync_components, mlc->mlc_resync_count);
7665 lo->ldo_flr_state = LCM_FL_RDONLY;
7666 lod_obj_inc_layout_gen(lo);
7668 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
7669 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
7670 &info->lti_buf, XATTR_NAME_LOV, 0, th);
7675 lod_striping_free(env, lo);
7679 typedef int (*mlc_handler)(const struct lu_env *env, struct dt_object *dt,
7680 const struct md_layout_change *mlc,
7681 struct thandle *th);
7684 * Attach stripes after target's for migrating directory. NB, we
7685 * only need to declare this, the actual work is done inside
7686 * lod_xattr_set_lmv().
7688 * \param[in] env execution environment
7689 * \param[in] dt target object
7690 * \param[in] mlc layout change data
7691 * \param[in] th transaction handle
7693 * \retval 0 on success
7694 * \retval negative if failed
7696 static int lod_dir_declare_layout_attach(const struct lu_env *env,
7697 struct dt_object *dt,
7698 const struct md_layout_change *mlc,
7701 struct lod_thread_info *info = lod_env_info(env);
7702 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
7703 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
7704 struct lod_object *lo = lod_dt_obj(dt);
7705 struct dt_object *next = dt_object_child(dt);
7706 struct dt_object_format *dof = &info->lti_format;
7707 struct lmv_mds_md_v1 *lmv = mlc->mlc_buf.lb_buf;
7708 struct dt_object **stripes;
7709 __u32 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
7710 struct lu_fid *fid = &info->lti_fid;
7711 struct lod_tgt_desc *tgt;
7712 struct dt_object *dto;
7713 struct dt_device *tgt_dt;
7714 int type = LU_SEQ_RANGE_ANY;
7715 struct dt_insert_rec *rec = &info->lti_dt_rec;
7716 char *stripe_name = info->lti_key;
7717 struct lu_name *sname;
7718 struct linkea_data ldata = { NULL };
7719 struct lu_buf linkea_buf;
7726 if (!lmv_is_sane(lmv))
7729 if (!dt_try_as_dir(env, dt))
7732 dof->dof_type = DFT_DIR;
7734 OBD_ALLOC_PTR_ARRAY(stripes, (lo->ldo_dir_stripe_count + stripe_count));
7738 for (i = 0; i < lo->ldo_dir_stripe_count; i++)
7739 stripes[i] = lo->ldo_stripe[i];
7741 rec->rec_type = S_IFDIR;
7743 for (i = 0; i < stripe_count; i++) {
7745 &lmv->lmv_stripe_fids[i]);
7746 if (!fid_is_sane(fid))
7749 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
7753 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
7754 tgt_dt = lod->lod_child;
7756 tgt = LTD_TGT(ltd, idx);
7758 GOTO(out, rc = -ESTALE);
7759 tgt_dt = tgt->ltd_tgt;
7762 dto = dt_locate_at(env, tgt_dt, fid,
7763 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
7766 GOTO(out, rc = PTR_ERR(dto));
7768 stripes[i + lo->ldo_dir_stripe_count] = dto;
7770 if (!dt_try_as_dir(env, dto))
7771 GOTO(out, rc = -ENOTDIR);
7773 rc = lod_sub_declare_ref_add(env, dto, th);
7777 rec->rec_fid = lu_object_fid(&dto->do_lu);
7778 rc = lod_sub_declare_insert(env, dto,
7779 (const struct dt_rec *)rec,
7780 (const struct dt_key *)dot, th);
7784 rc = lod_sub_declare_insert(env, dto,
7785 (const struct dt_rec *)rec,
7786 (const struct dt_key *)dotdot, th);
7790 rc = lod_sub_declare_xattr_set(env, dto, &mlc->mlc_buf,
7791 XATTR_NAME_LMV, 0, th);
7795 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
7796 PFID(lu_object_fid(&dto->do_lu)),
7797 i + lo->ldo_dir_stripe_count);
7799 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
7800 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
7801 sname, lu_object_fid(&dt->do_lu));
7805 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
7806 linkea_buf.lb_len = ldata.ld_leh->leh_len;
7807 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
7808 XATTR_NAME_LINK, 0, th);
7812 rc = lod_sub_declare_insert(env, next,
7813 (const struct dt_rec *)rec,
7814 (const struct dt_key *)stripe_name,
7819 rc = lod_sub_declare_ref_add(env, next, th);
7825 OBD_FREE_PTR_ARRAY(lo->ldo_stripe,
7826 lo->ldo_dir_stripes_allocated);
7827 lo->ldo_stripe = stripes;
7828 lo->ldo_dir_migrate_offset = lo->ldo_dir_stripe_count;
7829 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_hash_type);
7830 lo->ldo_dir_stripe_count += stripe_count;
7831 lo->ldo_dir_stripes_allocated += stripe_count;
7833 /* plain directory split creates target as a plain directory, while
7834 * after source attached as the first stripe, it becomes a striped
7835 * directory, set correct do_index_ops, otherwise it can't be unlinked.
7837 dt->do_index_ops = &lod_striped_index_ops;
7841 i = lo->ldo_dir_stripe_count;
7842 while (i < lo->ldo_dir_stripe_count + stripe_count && stripes[i])
7843 dt_object_put(env, stripes[i++]);
7845 OBD_FREE_PTR_ARRAY(stripes, stripe_count + lo->ldo_dir_stripe_count);
7849 static int lod_dir_declare_layout_detach(const struct lu_env *env,
7850 struct dt_object *dt,
7851 const struct md_layout_change *unused,
7854 struct lod_thread_info *info = lod_env_info(env);
7855 struct lod_object *lo = lod_dt_obj(dt);
7856 struct dt_object *next = dt_object_child(dt);
7857 char *stripe_name = info->lti_key;
7858 struct dt_object *dto;
7862 if (!dt_try_as_dir(env, dt))
7865 if (!lo->ldo_dir_stripe_count)
7866 return lod_sub_declare_delete(env, next,
7867 (const struct dt_key *)dotdot, th);
7869 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
7870 dto = lo->ldo_stripe[i];
7874 if (!dt_try_as_dir(env, dto))
7877 rc = lod_sub_declare_delete(env, dto,
7878 (const struct dt_key *)dotdot, th);
7882 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
7883 PFID(lu_object_fid(&dto->do_lu)), i);
7885 rc = lod_sub_declare_delete(env, next,
7886 (const struct dt_key *)stripe_name, th);
7890 rc = lod_sub_declare_ref_del(env, next, th);
7898 static int dt_dir_is_empty(const struct lu_env *env,
7899 struct dt_object *obj)
7902 const struct dt_it_ops *iops;
7907 if (!dt_try_as_dir(env, obj))
7910 iops = &obj->do_index_ops->dio_it;
7911 it = iops->init(env, obj, LUDA_64BITHASH);
7913 RETURN(PTR_ERR(it));
7915 rc = iops->get(env, it, (const struct dt_key *)"");
7919 for (rc = 0, i = 0; rc == 0 && i < 3; ++i)
7920 rc = iops->next(env, it);
7926 /* Huh? Index contains no zero key? */
7931 iops->fini(env, it);
7936 static int lod_dir_declare_layout_shrink(const struct lu_env *env,
7937 struct dt_object *dt,
7938 const struct md_layout_change *mlc,
7941 struct lod_thread_info *info = lod_env_info(env);
7942 struct lod_object *lo = lod_dt_obj(dt);
7943 struct dt_object *next = dt_object_child(dt);
7944 struct lmv_user_md *lmu = mlc->mlc_buf.lb_buf;
7945 __u32 final_stripe_count;
7946 char *stripe_name = info->lti_key;
7947 struct lu_buf *lmv_buf = &info->lti_buf;
7948 struct dt_object *dto;
7954 if (!dt_try_as_dir(env, dt))
7957 /* shouldn't be called on plain directory */
7958 LASSERT(lo->ldo_dir_stripe_count);
7960 lmv_buf->lb_buf = &info->lti_lmv.lmv_md_v1;
7961 lmv_buf->lb_len = sizeof(info->lti_lmv.lmv_md_v1);
7963 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
7964 LASSERT(final_stripe_count &&
7965 final_stripe_count < lo->ldo_dir_stripe_count);
7967 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
7968 dto = lo->ldo_stripe[i];
7972 if (i < final_stripe_count) {
7973 if (final_stripe_count == 1)
7976 rc = lod_sub_declare_xattr_set(env, dto, lmv_buf,
7978 LU_XATTR_REPLACE, th);
7985 rc = dt_dir_is_empty(env, dto);
7989 rc = lod_sub_declare_ref_del(env, dto, th);
7993 rc = lod_sub_declare_destroy(env, dto, th);
7997 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
7998 PFID(lu_object_fid(&dto->do_lu)), i);
8000 rc = lod_sub_declare_delete(env, next,
8001 (const struct dt_key *)stripe_name, th);
8005 rc = lod_sub_declare_ref_del(env, next, th);
8010 rc = lod_sub_declare_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV,
8011 LU_XATTR_REPLACE, th);
8016 * Allocate stripes for split directory.
8018 * \param[in] env execution environment
8019 * \param[in] dt target object
8020 * \param[in] mlc layout change data
8021 * \param[in] th transaction handle
8023 * \retval 0 on success
8024 * \retval negative if failed
8026 static int lod_dir_declare_layout_split(const struct lu_env *env,
8027 struct dt_object *dt,
8028 const struct md_layout_change *mlc,
8031 struct lod_thread_info *info = lod_env_info(env);
8032 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
8033 struct lod_object *lo = lod_dt_obj(dt);
8034 struct dt_object_format *dof = &info->lti_format;
8035 struct lmv_user_md_v1 *lum = mlc->mlc_spec->u.sp_ea.eadata;
8036 struct dt_object **stripes;
8044 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
8045 LASSERT(le32_to_cpu(lum->lum_stripe_offset) == LMV_OFFSET_DEFAULT);
8047 saved_count = lo->ldo_dir_stripes_allocated;
8048 stripe_count = le32_to_cpu(lum->lum_stripe_count);
8049 if (stripe_count <= saved_count)
8052 dof->dof_type = DFT_DIR;
8054 OBD_ALLOC(stripes, sizeof(*stripes) * stripe_count);
8058 for (i = 0; i < lo->ldo_dir_stripes_allocated; i++)
8059 stripes[i] = lo->ldo_stripe[i];
8061 lod_qos_statfs_update(env, lod, &lod->lod_mdt_descs);
8062 rc = lod_mdt_alloc_qos(env, lo, stripes, saved_count, stripe_count);
8064 rc = lod_mdt_alloc_rr(env, lo, stripes, saved_count,
8067 OBD_FREE(stripes, sizeof(*stripes) * stripe_count);
8071 LASSERT(rc > saved_count);
8072 OBD_FREE(lo->ldo_stripe,
8073 sizeof(*stripes) * lo->ldo_dir_stripes_allocated);
8074 lo->ldo_stripe = stripes;
8075 lo->ldo_dir_striped = 1;
8076 lo->ldo_dir_stripe_count = rc;
8077 lo->ldo_dir_stripes_allocated = stripe_count;
8078 lo->ldo_dir_split_hash = lo->ldo_dir_hash_type;
8079 lo->ldo_dir_hash_type = le32_to_cpu(lum->lum_hash_type);
8080 if (!lmv_is_known_hash_type(lo->ldo_dir_hash_type))
8081 lo->ldo_dir_hash_type =
8082 lod->lod_mdt_descs.ltd_lmv_desc.ld_pattern;
8083 lo->ldo_dir_hash_type |= LMV_HASH_FLAG_SPLIT | LMV_HASH_FLAG_MIGRATION;
8084 lo->ldo_dir_split_offset = saved_count;
8085 lo->ldo_dir_layout_version++;
8086 lo->ldo_dir_stripe_loaded = 1;
8088 rc = lod_dir_declare_create_stripes(env, dt, mlc->mlc_attr, dof, th);
8090 lod_striping_free(env, lo);
8096 * detach all stripes from dir master object, NB, stripes are not destroyed, but
8097 * deleted from it's parent namespace, this function is called in two places:
8098 * 1. mdd_migrate_mdt() detach stripes from source, and attach them to
8100 * 2. mdd_dir_layout_update() detach stripe before turning 1-stripe directory to
8101 * a plain directory.
8103 * \param[in] env execution environment
8104 * \param[in] dt target object
8105 * \param[in] mlc layout change data
8106 * \param[in] th transaction handle
8108 * \retval 0 on success
8109 * \retval negative if failed
8111 static int lod_dir_layout_detach(const struct lu_env *env,
8112 struct dt_object *dt,
8113 const struct md_layout_change *mlc,
8116 struct lod_thread_info *info = lod_env_info(env);
8117 struct lod_object *lo = lod_dt_obj(dt);
8118 struct dt_object *next = dt_object_child(dt);
8119 char *stripe_name = info->lti_key;
8120 struct dt_object *dto;
8126 if (!lo->ldo_dir_stripe_count) {
8127 /* plain directory delete .. */
8128 rc = lod_sub_delete(env, next,
8129 (const struct dt_key *)dotdot, th);
8133 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8134 dto = lo->ldo_stripe[i];
8138 rc = lod_sub_delete(env, dto,
8139 (const struct dt_key *)dotdot, th);
8143 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8144 PFID(lu_object_fid(&dto->do_lu)), i);
8146 rc = lod_sub_delete(env, next,
8147 (const struct dt_key *)stripe_name, th);
8151 rc = lod_sub_ref_del(env, next, th);
8156 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8157 dto = lo->ldo_stripe[i];
8159 dt_object_put(env, dto);
8161 OBD_FREE_PTR_ARRAY(lo->ldo_stripe, lo->ldo_dir_stripes_allocated);
8162 lo->ldo_stripe = NULL;
8163 lo->ldo_dir_stripes_allocated = 0;
8164 lo->ldo_dir_stripe_count = 0;
8165 dt->do_index_ops = &lod_index_ops;
8170 static int lod_dir_layout_shrink(const struct lu_env *env,
8171 struct dt_object *dt,
8172 const struct md_layout_change *mlc,
8175 struct lod_thread_info *info = lod_env_info(env);
8176 struct lod_object *lo = lod_dt_obj(dt);
8177 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
8178 struct dt_object *next = dt_object_child(dt);
8179 struct lmv_user_md *lmu = mlc->mlc_buf.lb_buf;
8180 __u32 final_stripe_count;
8181 char *stripe_name = info->lti_key;
8182 struct dt_object *dto;
8183 struct lu_buf *lmv_buf = &info->lti_buf;
8184 struct lmv_mds_md_v1 *lmv = &info->lti_lmv.lmv_md_v1;
8186 int type = LU_SEQ_RANGE_ANY;
8192 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
8194 lmv_buf->lb_buf = lmv;
8195 lmv_buf->lb_len = sizeof(*lmv);
8196 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
8197 lmv->lmv_stripe_count = cpu_to_le32(final_stripe_count);
8198 lmv->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type) &
8199 cpu_to_le32(LMV_HASH_TYPE_MASK);
8200 lmv->lmv_layout_version =
8201 cpu_to_le32(lo->ldo_dir_layout_version + 1);
8203 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8204 dto = lo->ldo_stripe[i];
8208 if (i < final_stripe_count) {
8209 /* if only one stripe left, no need to update
8210 * LMV because this stripe will replace master
8211 * object and act as a plain directory.
8213 if (final_stripe_count == 1)
8217 rc = lod_fld_lookup(env, lod,
8218 lu_object_fid(&dto->do_lu),
8223 lmv->lmv_master_mdt_index = cpu_to_le32(mdtidx);
8224 rc = lod_sub_xattr_set(env, dto, lmv_buf,
8226 LU_XATTR_REPLACE, th);
8233 dt_write_lock(env, dto, DT_TGT_CHILD);
8234 rc = lod_sub_ref_del(env, dto, th);
8235 dt_write_unlock(env, dto);
8239 rc = lod_sub_destroy(env, dto, th);
8243 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8244 PFID(lu_object_fid(&dto->do_lu)), i);
8246 rc = lod_sub_delete(env, next,
8247 (const struct dt_key *)stripe_name, th);
8251 rc = lod_sub_ref_del(env, next, th);
8256 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &mdtidx,
8261 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
8262 lmv->lmv_master_mdt_index = cpu_to_le32(mdtidx);
8263 rc = lod_sub_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV,
8264 LU_XATTR_REPLACE, th);
8268 for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
8269 dto = lo->ldo_stripe[i];
8271 dt_object_put(env, dto);
8273 lo->ldo_dir_stripe_count = final_stripe_count;
8278 static mlc_handler dir_mlc_declare_ops[MD_LAYOUT_MAX] = {
8279 [MD_LAYOUT_ATTACH] = lod_dir_declare_layout_attach,
8280 [MD_LAYOUT_DETACH] = lod_dir_declare_layout_detach,
8281 [MD_LAYOUT_SHRINK] = lod_dir_declare_layout_shrink,
8282 [MD_LAYOUT_SPLIT] = lod_dir_declare_layout_split,
8285 static mlc_handler dir_mlc_ops[MD_LAYOUT_MAX] = {
8286 [MD_LAYOUT_DETACH] = lod_dir_layout_detach,
8287 [MD_LAYOUT_SHRINK] = lod_dir_layout_shrink,
8290 static int lod_declare_layout_change(const struct lu_env *env,
8291 struct dt_object *dt, struct md_layout_change *mlc,
8294 struct lod_thread_info *info = lod_env_info(env);
8295 struct lod_object *lo = lod_dt_obj(dt);
8300 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
8301 LASSERT(dir_mlc_declare_ops[mlc->mlc_opc]);
8302 rc = dir_mlc_declare_ops[mlc->mlc_opc](env, dt, mlc, th);
8306 if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
8307 dt_object_remote(dt_object_child(dt)))
8310 rc = lod_striping_load(env, lo);
8314 LASSERT(lo->ldo_comp_cnt > 0);
8316 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
8320 switch (lo->ldo_flr_state) {
8322 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
8326 rc = lod_declare_update_rdonly(env, lo, mlc, th);
8328 case LCM_FL_WRITE_PENDING:
8329 rc = lod_declare_update_write_pending(env, lo, mlc, th);
8331 case LCM_FL_SYNC_PENDING:
8332 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
8343 * Instantiate layout component objects which covers the intent write offset.
8345 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
8346 struct md_layout_change *mlc, struct thandle *th)
8348 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
8349 struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
8350 struct lod_object *lo = lod_dt_obj(dt);
8355 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
8356 LASSERT(dir_mlc_ops[mlc->mlc_opc]);
8357 rc = dir_mlc_ops[mlc->mlc_opc](env, dt, mlc, th);
8361 rc = lod_striped_create(env, dt, attr, NULL, th);
8362 if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
8363 layout_attr->la_layout_version |= lo->ldo_layout_gen;
8364 rc = lod_attr_set(env, dt, layout_attr, th);
8370 struct dt_object_operations lod_obj_ops = {
8371 .do_read_lock = lod_read_lock,
8372 .do_write_lock = lod_write_lock,
8373 .do_read_unlock = lod_read_unlock,
8374 .do_write_unlock = lod_write_unlock,
8375 .do_write_locked = lod_write_locked,
8376 .do_attr_get = lod_attr_get,
8377 .do_declare_attr_set = lod_declare_attr_set,
8378 .do_attr_set = lod_attr_set,
8379 .do_xattr_get = lod_xattr_get,
8380 .do_declare_xattr_set = lod_declare_xattr_set,
8381 .do_xattr_set = lod_xattr_set,
8382 .do_declare_xattr_del = lod_declare_xattr_del,
8383 .do_xattr_del = lod_xattr_del,
8384 .do_xattr_list = lod_xattr_list,
8385 .do_ah_init = lod_ah_init,
8386 .do_declare_create = lod_declare_create,
8387 .do_create = lod_create,
8388 .do_declare_destroy = lod_declare_destroy,
8389 .do_destroy = lod_destroy,
8390 .do_index_try = lod_index_try,
8391 .do_declare_ref_add = lod_declare_ref_add,
8392 .do_ref_add = lod_ref_add,
8393 .do_declare_ref_del = lod_declare_ref_del,
8394 .do_ref_del = lod_ref_del,
8395 .do_object_sync = lod_object_sync,
8396 .do_object_lock = lod_object_lock,
8397 .do_object_unlock = lod_object_unlock,
8398 .do_invalidate = lod_invalidate,
8399 .do_declare_layout_change = lod_declare_layout_change,
8400 .do_layout_change = lod_layout_change,
8404 * Implementation of dt_body_operations::dbo_read.
8406 * \see dt_body_operations::dbo_read() in the API description for details.
8408 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
8409 struct lu_buf *buf, loff_t *pos)
8411 struct dt_object *next = dt_object_child(dt);
8413 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
8414 S_ISLNK(dt->do_lu.lo_header->loh_attr));
8415 return next->do_body_ops->dbo_read(env, next, buf, pos);
8419 * Implementation of dt_body_operations::dbo_declare_write.
8421 * \see dt_body_operations::dbo_declare_write() in the API description
8424 static ssize_t lod_declare_write(const struct lu_env *env,
8425 struct dt_object *dt,
8426 const struct lu_buf *buf, loff_t pos,
8429 return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
8433 * Implementation of dt_body_operations::dbo_write.
8435 * \see dt_body_operations::dbo_write() in the API description for details.
8437 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
8438 const struct lu_buf *buf, loff_t *pos,
8441 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
8442 S_ISLNK(dt->do_lu.lo_header->loh_attr));
8443 return lod_sub_write(env, dt_object_child(dt), buf, pos, th);
8446 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
8447 __u64 start, __u64 end, struct thandle *th)
8449 if (dt_object_remote(dt))
8452 return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
8455 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
8456 __u64 start, __u64 end, struct thandle *th)
8458 if (dt_object_remote(dt))
8461 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
8462 return lod_sub_punch(env, dt_object_child(dt), start, end, th);
8466 * different type of files use the same body_ops because object may be created
8467 * in OUT, where there is no chance to set correct body_ops for each type, so
8468 * body_ops themselves will check file type inside, see lod_read/write/punch for
8471 const struct dt_body_operations lod_body_ops = {
8472 .dbo_read = lod_read,
8473 .dbo_declare_write = lod_declare_write,
8474 .dbo_write = lod_write,
8475 .dbo_declare_punch = lod_declare_punch,
8476 .dbo_punch = lod_punch,
8480 * Implementation of lu_object_operations::loo_object_init.
8482 * The function determines the type and the index of the target device using
8483 * sequence of the object's FID. Then passes control down to the
8484 * corresponding device:
8485 * OSD for the local objects, OSP for remote
8487 * \see lu_object_operations::loo_object_init() in the API description
8490 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
8491 const struct lu_object_conf *conf)
8493 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
8494 struct lu_device *cdev = NULL;
8495 struct lu_object *cobj;
8496 struct lod_tgt_descs *ltd = NULL;
8497 struct lod_tgt_desc *tgt;
8499 int type = LU_SEQ_RANGE_ANY;
8503 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
8507 if (type == LU_SEQ_RANGE_MDT &&
8508 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
8509 cdev = &lod->lod_child->dd_lu_dev;
8510 } else if (type == LU_SEQ_RANGE_MDT) {
8511 ltd = &lod->lod_mdt_descs;
8513 } else if (type == LU_SEQ_RANGE_OST) {
8514 ltd = &lod->lod_ost_descs;
8521 if (ltd->ltd_tgts_size > idx &&
8522 test_bit(idx, ltd->ltd_tgt_bitmap)) {
8523 tgt = LTD_TGT(ltd, idx);
8525 LASSERT(tgt != NULL);
8526 LASSERT(tgt->ltd_tgt != NULL);
8528 cdev = &(tgt->ltd_tgt->dd_lu_dev);
8530 lod_putref(lod, ltd);
8533 if (unlikely(cdev == NULL))
8536 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
8537 if (unlikely(cobj == NULL))
8540 lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
8542 lu_object_add(lo, cobj);
8549 * Alloc cached foreign LOV
8551 * \param[in] lo object
8552 * \param[in] size size of foreign LOV
8554 * \retval 0 on success
8555 * \retval negative if failed
8557 int lod_alloc_foreign_lov(struct lod_object *lo, size_t size)
8559 OBD_ALLOC_LARGE(lo->ldo_foreign_lov, size);
8560 if (lo->ldo_foreign_lov == NULL)
8562 lo->ldo_foreign_lov_size = size;
8563 lo->ldo_is_foreign = 1;
8569 * Free cached foreign LOV
8571 * \param[in] lo object
8573 void lod_free_foreign_lov(struct lod_object *lo)
8575 if (lo->ldo_foreign_lov != NULL)
8576 OBD_FREE_LARGE(lo->ldo_foreign_lov, lo->ldo_foreign_lov_size);
8577 lo->ldo_foreign_lov = NULL;
8578 lo->ldo_foreign_lov_size = 0;
8579 lo->ldo_is_foreign = 0;
8584 * Free cached foreign LMV
8586 * \param[in] lo object
8588 void lod_free_foreign_lmv(struct lod_object *lo)
8590 if (lo->ldo_foreign_lmv != NULL)
8591 OBD_FREE_LARGE(lo->ldo_foreign_lmv, lo->ldo_foreign_lmv_size);
8592 lo->ldo_foreign_lmv = NULL;
8593 lo->ldo_foreign_lmv_size = 0;
8594 lo->ldo_dir_is_foreign = 0;
8599 * Release resources associated with striping.
8601 * If the object is striped (regular or directory), then release
8602 * the stripe objects references and free the ldo_stripe array.
8604 * \param[in] env execution environment
8605 * \param[in] lo object
8607 void lod_striping_free_nolock(const struct lu_env *env, struct lod_object *lo)
8609 struct lod_layout_component *lod_comp;
8612 if (unlikely(lo->ldo_is_foreign)) {
8613 lod_free_foreign_lov(lo);
8614 lo->ldo_comp_cached = 0;
8615 } else if (unlikely(lo->ldo_dir_is_foreign)) {
8616 lod_free_foreign_lmv(lo);
8617 lo->ldo_dir_stripe_loaded = 0;
8618 } else if (lo->ldo_stripe != NULL) {
8619 LASSERT(lo->ldo_comp_entries == NULL);
8620 LASSERT(lo->ldo_dir_stripes_allocated > 0);
8622 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8623 if (lo->ldo_stripe[i])
8624 dt_object_put(env, lo->ldo_stripe[i]);
8627 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
8628 OBD_FREE(lo->ldo_stripe, j);
8629 lo->ldo_stripe = NULL;
8630 lo->ldo_dir_stripes_allocated = 0;
8631 lo->ldo_dir_stripe_loaded = 0;
8632 lo->ldo_dir_stripe_count = 0;
8633 } else if (lo->ldo_comp_entries != NULL) {
8634 for (i = 0; i < lo->ldo_comp_cnt; i++) {
8635 /* free lod_layout_component::llc_stripe array */
8636 lod_comp = &lo->ldo_comp_entries[i];
8638 if (lod_comp->llc_stripe == NULL)
8640 LASSERT(lod_comp->llc_stripes_allocated != 0);
8641 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
8642 if (lod_comp->llc_stripe[j] != NULL)
8644 &lod_comp->llc_stripe[j]->do_lu);
8646 OBD_FREE_PTR_ARRAY(lod_comp->llc_stripe,
8647 lod_comp->llc_stripes_allocated);
8648 lod_comp->llc_stripe = NULL;
8649 OBD_FREE_PTR_ARRAY(lod_comp->llc_ost_indices,
8650 lod_comp->llc_stripes_allocated);
8651 lod_comp->llc_ost_indices = NULL;
8652 lod_comp->llc_stripes_allocated = 0;
8654 lod_free_comp_entries(lo);
8655 lo->ldo_comp_cached = 0;
8659 void lod_striping_free(const struct lu_env *env, struct lod_object *lo)
8661 mutex_lock(&lo->ldo_layout_mutex);
8662 lod_striping_free_nolock(env, lo);
8663 mutex_unlock(&lo->ldo_layout_mutex);
8667 * Implementation of lu_object_operations::loo_object_free.
8669 * \see lu_object_operations::loo_object_free() in the API description
8672 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
8674 struct lod_object *lo = lu2lod_obj(o);
8676 /* release all underlying object pinned */
8677 lod_striping_free(env, lo);
8679 /* lo doesn't contain a lu_object_header, so we don't need call_rcu */
8680 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
8684 * Implementation of lu_object_operations::loo_object_release.
8686 * \see lu_object_operations::loo_object_release() in the API description
8689 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
8691 /* XXX: shouldn't we release everything here in case if object
8692 * creation failed before? */
8696 * Implementation of lu_object_operations::loo_object_print.
8698 * \see lu_object_operations::loo_object_print() in the API description
8701 static int lod_object_print(const struct lu_env *env, void *cookie,
8702 lu_printer_t p, const struct lu_object *l)
8704 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
8706 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
8709 struct lu_object_operations lod_lu_obj_ops = {
8710 .loo_object_init = lod_object_init,
8711 .loo_object_free = lod_object_free,
8712 .loo_object_release = lod_object_release,
8713 .loo_object_print = lod_object_print,