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_it_ops::init.
372 * Used with striped objects. Internally just initializes the iterator
373 * on the first stripe.
375 * \see dt_it_ops::init() in the API description for details.
377 static struct dt_it *lod_striped_it_init(const struct lu_env *env,
378 struct dt_object *dt, __u32 attr)
380 struct lod_object *lo = lod_dt_obj(dt);
381 struct dt_object *next;
382 struct lod_it *it = &lod_env_info(env)->lti_it;
383 struct dt_it *it_next;
386 LASSERT(lo->ldo_dir_stripe_count > 0);
389 next = lo->ldo_stripe[index];
390 if (next && dt_object_exists(next))
392 } while (++index < lo->ldo_dir_stripe_count);
394 /* no valid stripe */
395 if (!next || !dt_object_exists(next))
396 return ERR_PTR(-ENODEV);
398 LASSERT(next->do_index_ops != NULL);
400 it_next = next->do_index_ops->dio_it.init(env, next, attr);
404 /* currently we do not use more than one iterator per thread
405 * so we store it in thread info. if at some point we need
406 * more active iterators in a single thread, we can allocate
408 LASSERT(it->lit_obj == NULL);
410 it->lit_stripe_index = index;
412 it->lit_it = it_next;
415 return (struct dt_it *)it;
418 #define LOD_CHECK_STRIPED_IT(env, it, lo) \
420 LASSERT((it)->lit_obj != NULL); \
421 LASSERT((it)->lit_it != NULL); \
422 LASSERT((lo)->ldo_dir_stripe_count > 0); \
423 LASSERT((it)->lit_stripe_index < (lo)->ldo_dir_stripe_count); \
427 * Implementation of dt_it_ops::fini.
429 * Used with striped objects.
431 * \see dt_it_ops::fini() in the API description for details.
433 static void lod_striped_it_fini(const struct lu_env *env, struct dt_it *di)
435 struct lod_it *it = (struct lod_it *)di;
436 struct lod_object *lo = lod_dt_obj(it->lit_obj);
437 struct dt_object *next;
439 /* If lit_it == NULL, then it means the sub_it has been finished,
440 * which only happens in failure cases, see lod_striped_it_next() */
441 if (it->lit_it != NULL) {
442 LOD_CHECK_STRIPED_IT(env, it, lo);
444 next = lo->ldo_stripe[it->lit_stripe_index];
446 LASSERT(next->do_index_ops != NULL);
447 next->do_index_ops->dio_it.fini(env, it->lit_it);
451 /* the iterator not in use any more */
454 it->lit_stripe_index = 0;
458 * Implementation of dt_it_ops::get.
460 * Right now it's not used widely, only to reset the iterator to the
461 * initial position. It should be possible to implement a full version
462 * which chooses a correct stripe to be able to position with any key.
464 * \see dt_it_ops::get() in the API description for details.
466 static int lod_striped_it_get(const struct lu_env *env, struct dt_it *di,
467 const struct dt_key *key)
469 const struct lod_it *it = (const struct lod_it *)di;
470 struct lod_object *lo = lod_dt_obj(it->lit_obj);
471 struct dt_object *next;
473 LOD_CHECK_STRIPED_IT(env, it, lo);
475 next = lo->ldo_stripe[it->lit_stripe_index];
476 LASSERT(next != NULL);
477 LASSERT(dt_object_exists(next));
478 LASSERT(next->do_index_ops != NULL);
480 return next->do_index_ops->dio_it.get(env, it->lit_it, key);
484 * Implementation of dt_it_ops::put.
486 * Used with striped objects.
488 * \see dt_it_ops::put() in the API description for details.
490 static void lod_striped_it_put(const struct lu_env *env, struct dt_it *di)
492 struct lod_it *it = (struct lod_it *)di;
493 struct lod_object *lo = lod_dt_obj(it->lit_obj);
494 struct dt_object *next;
497 * If lit_it == NULL, then it means the sub_it has been finished,
498 * which only happens in failure cases, see lod_striped_it_next()
503 LOD_CHECK_STRIPED_IT(env, it, lo);
505 next = lo->ldo_stripe[it->lit_stripe_index];
506 LASSERT(next != NULL);
507 LASSERT(next->do_index_ops != NULL);
509 return next->do_index_ops->dio_it.put(env, it->lit_it);
513 * Implementation of dt_it_ops::next.
515 * Used with striped objects. When the end of the current stripe is
516 * reached, the method takes the next stripe's iterator.
518 * \see dt_it_ops::next() in the API description for details.
520 static int lod_striped_it_next(const struct lu_env *env, struct dt_it *di)
522 struct lod_it *it = (struct lod_it *)di;
523 struct lod_object *lo = lod_dt_obj(it->lit_obj);
524 struct dt_object *next;
525 struct dt_it *it_next;
531 LOD_CHECK_STRIPED_IT(env, it, lo);
533 next = lo->ldo_stripe[it->lit_stripe_index];
534 LASSERT(next != NULL);
535 LASSERT(dt_object_exists(next));
536 LASSERT(next->do_index_ops != NULL);
538 rc = next->do_index_ops->dio_it.next(env, it->lit_it);
542 if (rc == 0 && it->lit_stripe_index == 0)
545 if (rc == 0 && it->lit_stripe_index > 0) {
546 struct lu_dirent *ent;
548 ent = (struct lu_dirent *)lod_env_info(env)->lti_key;
550 rc = next->do_index_ops->dio_it.rec(env, it->lit_it,
551 (struct dt_rec *)ent,
556 /* skip . and .. for slave stripe */
557 if ((strncmp(ent->lde_name, ".",
558 le16_to_cpu(ent->lde_namelen)) == 0 &&
559 le16_to_cpu(ent->lde_namelen) == 1) ||
560 (strncmp(ent->lde_name, "..",
561 le16_to_cpu(ent->lde_namelen)) == 0 &&
562 le16_to_cpu(ent->lde_namelen) == 2))
568 next->do_index_ops->dio_it.put(env, it->lit_it);
569 next->do_index_ops->dio_it.fini(env, it->lit_it);
572 /* go to next stripe */
573 index = it->lit_stripe_index;
574 while (++index < lo->ldo_dir_stripe_count) {
575 next = lo->ldo_stripe[index];
579 if (!dt_object_exists(next))
582 rc = next->do_ops->do_index_try(env, next,
583 &dt_directory_features);
587 LASSERT(next->do_index_ops != NULL);
589 it_next = next->do_index_ops->dio_it.init(env, next,
592 RETURN(PTR_ERR(it_next));
594 rc = next->do_index_ops->dio_it.get(env, it_next,
595 (const struct dt_key *)"");
597 RETURN(rc == 0 ? -EIO : rc);
599 it->lit_it = it_next;
600 it->lit_stripe_index = index;
609 * Implementation of dt_it_ops::key.
611 * Used with striped objects.
613 * \see dt_it_ops::key() in the API description for details.
615 static struct dt_key *lod_striped_it_key(const struct lu_env *env,
616 const struct dt_it *di)
618 const struct lod_it *it = (const struct lod_it *)di;
619 struct lod_object *lo = lod_dt_obj(it->lit_obj);
620 struct dt_object *next;
622 LOD_CHECK_STRIPED_IT(env, it, lo);
624 next = lo->ldo_stripe[it->lit_stripe_index];
625 LASSERT(next != NULL);
626 LASSERT(next->do_index_ops != NULL);
628 return next->do_index_ops->dio_it.key(env, it->lit_it);
632 * Implementation of dt_it_ops::key_size.
634 * Used with striped objects.
636 * \see dt_it_ops::size() in the API description for details.
638 static int lod_striped_it_key_size(const struct lu_env *env,
639 const struct dt_it *di)
641 struct lod_it *it = (struct lod_it *)di;
642 struct lod_object *lo = lod_dt_obj(it->lit_obj);
643 struct dt_object *next;
645 LOD_CHECK_STRIPED_IT(env, it, lo);
647 next = lo->ldo_stripe[it->lit_stripe_index];
648 LASSERT(next != NULL);
649 LASSERT(next->do_index_ops != NULL);
651 return next->do_index_ops->dio_it.key_size(env, it->lit_it);
655 * Implementation of dt_it_ops::rec.
657 * Used with striped objects.
659 * \see dt_it_ops::rec() in the API description for details.
661 static int lod_striped_it_rec(const struct lu_env *env, const struct dt_it *di,
662 struct dt_rec *rec, __u32 attr)
664 const struct lod_it *it = (const struct lod_it *)di;
665 struct lod_object *lo = lod_dt_obj(it->lit_obj);
666 struct dt_object *next;
668 LOD_CHECK_STRIPED_IT(env, it, lo);
670 next = lo->ldo_stripe[it->lit_stripe_index];
671 LASSERT(next != NULL);
672 LASSERT(next->do_index_ops != NULL);
674 return next->do_index_ops->dio_it.rec(env, it->lit_it, rec, attr);
678 * Implementation of dt_it_ops::rec_size.
680 * Used with striped objects.
682 * \see dt_it_ops::rec_size() in the API description for details.
684 static int lod_striped_it_rec_size(const struct lu_env *env,
685 const struct dt_it *di, __u32 attr)
687 struct lod_it *it = (struct lod_it *)di;
688 struct lod_object *lo = lod_dt_obj(it->lit_obj);
689 struct dt_object *next;
691 LOD_CHECK_STRIPED_IT(env, it, lo);
693 next = lo->ldo_stripe[it->lit_stripe_index];
694 LASSERT(next != NULL);
695 LASSERT(next->do_index_ops != NULL);
697 return next->do_index_ops->dio_it.rec_size(env, it->lit_it, attr);
701 * Implementation of dt_it_ops::store.
703 * Used with striped objects.
705 * \see dt_it_ops::store() in the API description for details.
707 static __u64 lod_striped_it_store(const struct lu_env *env,
708 const struct dt_it *di)
710 const struct lod_it *it = (const struct lod_it *)di;
711 struct lod_object *lo = lod_dt_obj(it->lit_obj);
712 struct dt_object *next;
714 LOD_CHECK_STRIPED_IT(env, it, lo);
716 next = lo->ldo_stripe[it->lit_stripe_index];
717 LASSERT(next != NULL);
718 LASSERT(next->do_index_ops != NULL);
720 return next->do_index_ops->dio_it.store(env, it->lit_it);
724 * Implementation of dt_it_ops::load.
726 * Used with striped objects.
728 * \see dt_it_ops::load() in the API description for details.
730 static int lod_striped_it_load(const struct lu_env *env,
731 const struct dt_it *di, __u64 hash)
733 const struct lod_it *it = (const struct lod_it *)di;
734 struct lod_object *lo = lod_dt_obj(it->lit_obj);
735 struct dt_object *next;
737 LOD_CHECK_STRIPED_IT(env, it, lo);
739 next = lo->ldo_stripe[it->lit_stripe_index];
740 LASSERT(next != NULL);
741 LASSERT(next->do_index_ops != NULL);
743 return next->do_index_ops->dio_it.load(env, it->lit_it, hash);
746 static struct dt_index_operations lod_striped_index_ops = {
747 .dio_lookup = lod_lookup,
748 .dio_declare_insert = lod_declare_insert,
749 .dio_insert = lod_insert,
750 .dio_declare_delete = lod_declare_delete,
751 .dio_delete = lod_delete,
753 .init = lod_striped_it_init,
754 .fini = lod_striped_it_fini,
755 .get = lod_striped_it_get,
756 .put = lod_striped_it_put,
757 .next = lod_striped_it_next,
758 .key = lod_striped_it_key,
759 .key_size = lod_striped_it_key_size,
760 .rec = lod_striped_it_rec,
761 .rec_size = lod_striped_it_rec_size,
762 .store = lod_striped_it_store,
763 .load = lod_striped_it_load,
768 * Append the FID for each shard of the striped directory after the
769 * given LMV EA header.
771 * To simplify striped directory and the consistency verification,
772 * we only store the LMV EA header on disk, for both master object
773 * and slave objects. When someone wants to know the whole LMV EA,
774 * such as client readdir(), we can build the entrie LMV EA on the
775 * MDT side (in RAM) via iterating the sub-directory entries that
776 * are contained in the master object of the stripe directory.
778 * For the master object of the striped directroy, the valid name
779 * for each shard is composed of the ${shard_FID}:${shard_idx}.
781 * There may be holes in the LMV EA if some shards' name entries
782 * are corrupted or lost.
784 * \param[in] env pointer to the thread context
785 * \param[in] lo pointer to the master object of the striped directory
786 * \param[in] buf pointer to the lu_buf which will hold the LMV EA
787 * \param[in] resize whether re-allocate the buffer if it is not big enough
789 * \retval positive size of the LMV EA
790 * \retval 0 for nothing to be loaded
791 * \retval negative error number on failure
793 int lod_load_lmv_shards(const struct lu_env *env, struct lod_object *lo,
794 struct lu_buf *buf, bool resize)
796 struct lu_dirent *ent =
797 (struct lu_dirent *)lod_env_info(env)->lti_key;
798 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
799 struct dt_object *obj = dt_object_child(&lo->ldo_obj);
800 struct lmv_mds_md_v1 *lmv1 = buf->lb_buf;
802 const struct dt_it_ops *iops;
804 __u32 magic = le32_to_cpu(lmv1->lmv_magic);
809 if (magic != LMV_MAGIC_V1)
812 stripes = le32_to_cpu(lmv1->lmv_stripe_count);
816 rc = lmv_mds_md_size(stripes, magic);
820 if (buf->lb_len < lmv1_size) {
829 lu_buf_alloc(buf, lmv1_size);
834 memcpy(buf->lb_buf, tbuf.lb_buf, tbuf.lb_len);
837 if (unlikely(!dt_try_as_dir(env, obj)))
840 memset(&lmv1->lmv_stripe_fids[0], 0, stripes * sizeof(struct lu_fid));
841 iops = &obj->do_index_ops->dio_it;
842 it = iops->init(env, obj, LUDA_64BITHASH);
846 rc = iops->load(env, it, 0);
848 rc = iops->next(env, it);
853 char name[FID_LEN + 2] = "";
858 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
864 fid_le_to_cpu(&fid, &ent->lde_fid);
865 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
866 if (ent->lde_name[0] == '.') {
867 if (ent->lde_namelen == 1)
870 if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
874 len = snprintf(name, sizeof(name),
875 DFID":", PFID(&ent->lde_fid));
876 /* The ent->lde_name is composed of ${FID}:${index} */
877 if (ent->lde_namelen < len + 1 ||
878 memcmp(ent->lde_name, name, len) != 0) {
879 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
880 "%s: invalid shard name %.*s with the FID "DFID
881 " for the striped directory "DFID", %s\n",
882 lod2obd(lod)->obd_name, ent->lde_namelen,
883 ent->lde_name, PFID(&fid),
884 PFID(lu_object_fid(&obj->do_lu)),
885 lod->lod_lmv_failout ? "failout" : "skip");
887 if (lod->lod_lmv_failout)
895 if (ent->lde_name[len] < '0' ||
896 ent->lde_name[len] > '9') {
897 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
898 "%s: invalid shard name %.*s with the "
899 "FID "DFID" for the striped directory "
901 lod2obd(lod)->obd_name, ent->lde_namelen,
902 ent->lde_name, PFID(&fid),
903 PFID(lu_object_fid(&obj->do_lu)),
904 lod->lod_lmv_failout ?
907 if (lod->lod_lmv_failout)
913 index = index * 10 + ent->lde_name[len++] - '0';
914 } while (len < ent->lde_namelen);
916 if (len == ent->lde_namelen) {
917 /* Out of LMV EA range. */
918 if (index >= stripes) {
919 CERROR("%s: the shard %.*s for the striped "
920 "directory "DFID" is out of the known "
921 "LMV EA range [0 - %u], failout\n",
922 lod2obd(lod)->obd_name, ent->lde_namelen,
924 PFID(lu_object_fid(&obj->do_lu)),
930 /* The slot has been occupied. */
931 if (!fid_is_zero(&lmv1->lmv_stripe_fids[index])) {
935 &lmv1->lmv_stripe_fids[index]);
936 CERROR("%s: both the shard "DFID" and "DFID
937 " for the striped directory "DFID
938 " claim the same LMV EA slot at the "
939 "index %d, failout\n",
940 lod2obd(lod)->obd_name,
941 PFID(&fid0), PFID(&fid),
942 PFID(lu_object_fid(&obj->do_lu)), index);
947 /* stored as LE mode */
948 lmv1->lmv_stripe_fids[index] = ent->lde_fid;
951 rc = iops->next(env, it);
958 RETURN(rc > 0 ? lmv_mds_md_size(stripes, magic) : rc);
962 * Implementation of dt_object_operations::do_index_try.
964 * \see dt_object_operations::do_index_try() in the API description for details.
966 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
967 const struct dt_index_features *feat)
969 struct lod_object *lo = lod_dt_obj(dt);
970 struct dt_object *next = dt_object_child(dt);
974 LASSERT(next->do_ops);
975 LASSERT(next->do_ops->do_index_try);
977 rc = lod_striping_load(env, lo);
981 rc = next->do_ops->do_index_try(env, next, feat);
985 if (lo->ldo_dir_stripe_count > 0) {
988 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
989 if (!lo->ldo_stripe[i])
991 if (!dt_object_exists(lo->ldo_stripe[i]))
993 rc = lo->ldo_stripe[i]->do_ops->do_index_try(env,
994 lo->ldo_stripe[i], feat);
998 dt->do_index_ops = &lod_striped_index_ops;
1000 dt->do_index_ops = &lod_index_ops;
1007 * Implementation of dt_object_operations::do_read_lock.
1009 * \see dt_object_operations::do_read_lock() in the API description for details.
1011 static void lod_read_lock(const struct lu_env *env, struct dt_object *dt,
1014 dt_read_lock(env, dt_object_child(dt), role);
1018 * Implementation of dt_object_operations::do_write_lock.
1020 * \see dt_object_operations::do_write_lock() in the API description for
1023 static void lod_write_lock(const struct lu_env *env, struct dt_object *dt,
1026 dt_write_lock(env, dt_object_child(dt), role);
1030 * Implementation of dt_object_operations::do_read_unlock.
1032 * \see dt_object_operations::do_read_unlock() in the API description for
1035 static void lod_read_unlock(const struct lu_env *env, struct dt_object *dt)
1037 dt_read_unlock(env, dt_object_child(dt));
1041 * Implementation of dt_object_operations::do_write_unlock.
1043 * \see dt_object_operations::do_write_unlock() in the API description for
1046 static void lod_write_unlock(const struct lu_env *env, struct dt_object *dt)
1048 dt_write_unlock(env, dt_object_child(dt));
1052 * Implementation of dt_object_operations::do_write_locked.
1054 * \see dt_object_operations::do_write_locked() in the API description for
1057 static int lod_write_locked(const struct lu_env *env, struct dt_object *dt)
1059 return dt_write_locked(env, dt_object_child(dt));
1063 * Implementation of dt_object_operations::do_attr_get.
1065 * \see dt_object_operations::do_attr_get() in the API description for details.
1067 static int lod_attr_get(const struct lu_env *env,
1068 struct dt_object *dt,
1069 struct lu_attr *attr)
1071 /* Note: for striped directory, client will merge attributes
1072 * from all of the sub-stripes see lmv_merge_attr(), and there
1073 * no MDD logic depend on directory nlink/size/time, so we can
1074 * always use master inode nlink and size for now. */
1075 return dt_attr_get(env, dt_object_child(dt), attr);
1078 static inline void lod_adjust_stripe_info(struct lod_layout_component *comp,
1079 struct lov_desc *desc,
1082 if (comp->llc_pattern != LOV_PATTERN_MDT) {
1083 if (append_stripes) {
1084 comp->llc_stripe_count = append_stripes;
1085 } else if (!comp->llc_stripe_count) {
1086 comp->llc_stripe_count =
1087 desc->ld_default_stripe_count;
1090 if (comp->llc_stripe_size <= 0)
1091 comp->llc_stripe_size = desc->ld_default_stripe_size;
1094 int lod_obj_for_each_stripe(const struct lu_env *env, struct lod_object *lo,
1096 struct lod_obj_stripe_cb_data *data)
1098 struct lod_layout_component *lod_comp;
1102 LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
1103 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1104 lod_comp = &lo->ldo_comp_entries[i];
1106 if (lod_comp->llc_stripe == NULL)
1109 /* has stripe but not inited yet, this component has been
1110 * declared to be created, but hasn't created yet.
1112 if (!lod_comp_inited(lod_comp))
1115 if (data->locd_comp_skip_cb &&
1116 data->locd_comp_skip_cb(env, lo, i, data))
1119 if (data->locd_comp_cb) {
1120 rc = data->locd_comp_cb(env, lo, i, data);
1125 /* could used just to do sth about component, not each
1128 if (!data->locd_stripe_cb)
1131 LASSERT(lod_comp->llc_stripe_count > 0);
1132 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
1133 struct dt_object *dt = lod_comp->llc_stripe[j];
1137 rc = data->locd_stripe_cb(env, lo, dt, th, i, j, data);
1145 static bool lod_obj_attr_set_comp_skip_cb(const struct lu_env *env,
1146 struct lod_object *lo, int comp_idx,
1147 struct lod_obj_stripe_cb_data *data)
1149 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
1150 bool skipped = false;
1152 if (!(data->locd_attr->la_valid & LA_LAYOUT_VERSION))
1155 switch (lo->ldo_flr_state) {
1156 case LCM_FL_WRITE_PENDING: {
1159 /* skip stale components */
1160 if (lod_comp->llc_flags & LCME_FL_STALE) {
1165 /* skip valid and overlapping components, therefore any
1166 * attempts to write overlapped components will never succeed
1167 * because client will get EINPROGRESS. */
1168 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1172 if (lo->ldo_comp_entries[i].llc_flags & LCME_FL_STALE)
1175 if (lu_extent_is_overlapped(&lod_comp->llc_extent,
1176 &lo->ldo_comp_entries[i].llc_extent)) {
1184 LASSERTF(0, "impossible: %d\n", lo->ldo_flr_state);
1185 case LCM_FL_SYNC_PENDING:
1189 CDEBUG(D_LAYOUT, DFID": %s to set component %x to version: %u\n",
1190 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
1191 skipped ? "skipped" : "chose", lod_comp->llc_id,
1192 data->locd_attr->la_layout_version);
1198 lod_obj_stripe_attr_set_cb(const struct lu_env *env, struct lod_object *lo,
1199 struct dt_object *dt, struct thandle *th,
1200 int comp_idx, int stripe_idx,
1201 struct lod_obj_stripe_cb_data *data)
1203 if (data->locd_declare)
1204 return lod_sub_declare_attr_set(env, dt, data->locd_attr, th);
1206 if (data->locd_attr->la_valid & LA_LAYOUT_VERSION) {
1207 CDEBUG(D_LAYOUT, DFID": set layout version: %u, comp_idx: %d\n",
1208 PFID(lu_object_fid(&dt->do_lu)),
1209 data->locd_attr->la_layout_version, comp_idx);
1212 return lod_sub_attr_set(env, dt, data->locd_attr, th);
1216 * Implementation of dt_object_operations::do_declare_attr_set.
1218 * If the object is striped, then apply the changes to all the stripes.
1220 * \see dt_object_operations::do_declare_attr_set() in the API description
1223 static int lod_declare_attr_set(const struct lu_env *env,
1224 struct dt_object *dt,
1225 const struct lu_attr *attr,
1228 struct dt_object *next = dt_object_child(dt);
1229 struct lod_object *lo = lod_dt_obj(dt);
1234 * declare setattr on the local object
1236 rc = lod_sub_declare_attr_set(env, next, attr, th);
1240 /* osp_declare_attr_set() ignores all attributes other than
1241 * UID, GID, PROJID, and size, and osp_attr_set() ignores all
1242 * but UID, GID and PROJID. Declaration of size attr setting
1243 * happens through lod_declare_init_size(), and not through
1244 * this function. Therefore we need not load striping unless
1245 * ownership is changing. This should save memory and (we hope)
1246 * speed up rename().
1248 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1249 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1252 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1255 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID | LA_MODE |
1256 LA_ATIME | LA_MTIME | LA_CTIME |
1261 * load striping information, notice we don't do this when object
1262 * is being initialized as we don't need this information till
1263 * few specific cases like destroy, chown
1265 rc = lod_striping_load(env, lo);
1269 if (!lod_obj_is_striped(dt))
1273 * if object is striped declare changes on the stripes
1275 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1276 LASSERT(lo->ldo_stripe);
1277 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1278 if (lo->ldo_stripe[i] == NULL)
1280 if (!dt_object_exists(lo->ldo_stripe[i]))
1282 rc = lod_sub_declare_attr_set(env, lo->ldo_stripe[i],
1288 struct lod_obj_stripe_cb_data data = { { 0 } };
1290 data.locd_attr = attr;
1291 data.locd_declare = true;
1292 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1293 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1299 if (!dt_object_exists(next) || dt_object_remote(next) ||
1300 !S_ISREG(attr->la_mode))
1303 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1304 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
1308 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1309 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1310 struct lod_thread_info *info = lod_env_info(env);
1311 struct lu_buf *buf = &info->lti_buf;
1313 buf->lb_buf = info->lti_ea_store;
1314 buf->lb_len = info->lti_ea_store_size;
1315 rc = lod_sub_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1316 LU_XATTR_REPLACE, th);
1323 * Implementation of dt_object_operations::do_attr_set.
1325 * If the object is striped, then apply the changes to all or subset of
1326 * the stripes depending on the object type and specific attributes.
1328 * \see dt_object_operations::do_attr_set() in the API description for details.
1330 static int lod_attr_set(const struct lu_env *env,
1331 struct dt_object *dt,
1332 const struct lu_attr *attr,
1335 struct dt_object *next = dt_object_child(dt);
1336 struct lod_object *lo = lod_dt_obj(dt);
1341 * apply changes to the local object
1343 rc = lod_sub_attr_set(env, next, attr, th);
1347 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1348 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1351 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1354 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE | LA_PROJID |
1355 LA_ATIME | LA_MTIME | LA_CTIME |
1360 /* FIXME: a tricky case in the code path of mdd_layout_change():
1361 * the in-memory striping information has been freed in lod_xattr_set()
1362 * due to layout change. It has to load stripe here again. It only
1363 * changes flags of layout so declare_attr_set() is still accurate */
1364 rc = lod_striping_load(env, lo);
1368 if (!lod_obj_is_striped(dt))
1372 * if object is striped, apply changes to all the stripes
1374 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1375 LASSERT(lo->ldo_stripe);
1376 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1377 if (unlikely(lo->ldo_stripe[i] == NULL))
1380 if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1383 rc = lod_sub_attr_set(env, lo->ldo_stripe[i], attr, th);
1388 struct lod_obj_stripe_cb_data data = { { 0 } };
1390 data.locd_attr = attr;
1391 data.locd_declare = false;
1392 data.locd_comp_skip_cb = lod_obj_attr_set_comp_skip_cb;
1393 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1394 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1400 if (!dt_object_exists(next) || dt_object_remote(next) ||
1401 !S_ISREG(attr->la_mode))
1404 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1405 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
1409 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1410 struct lod_thread_info *info = lod_env_info(env);
1411 struct lu_buf *buf = &info->lti_buf;
1412 struct ost_id *oi = &info->lti_ostid;
1413 struct lu_fid *fid = &info->lti_fid;
1414 struct lov_mds_md_v1 *lmm;
1415 struct lov_ost_data_v1 *objs;
1418 rc = lod_get_lov_ea(env, lo);
1422 buf->lb_buf = info->lti_ea_store;
1423 buf->lb_len = info->lti_ea_store_size;
1424 lmm = info->lti_ea_store;
1425 magic = le32_to_cpu(lmm->lmm_magic);
1426 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
1427 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1428 struct lov_comp_md_entry_v1 *lcme =
1429 &lcm->lcm_entries[0];
1431 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1432 magic = le32_to_cpu(lmm->lmm_magic);
1435 if (magic == LOV_MAGIC_V1)
1436 objs = &(lmm->lmm_objects[0]);
1438 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1439 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1440 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1442 fid_to_ostid(fid, oi);
1443 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1445 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1446 LU_XATTR_REPLACE, th);
1447 } else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1448 struct lod_thread_info *info = lod_env_info(env);
1449 struct lu_buf *buf = &info->lti_buf;
1450 struct lov_comp_md_v1 *lcm;
1451 struct lov_comp_md_entry_v1 *lcme;
1453 rc = lod_get_lov_ea(env, lo);
1457 buf->lb_buf = info->lti_ea_store;
1458 buf->lb_len = info->lti_ea_store_size;
1460 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1 &&
1461 le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_SEL)
1464 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1465 lcme = &lcm->lcm_entries[0];
1466 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1467 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1469 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1470 LU_XATTR_REPLACE, th);
1477 * Implementation of dt_object_operations::do_xattr_get.
1479 * If LOV EA is requested from the root object and it's not
1480 * found, then return default striping for the filesystem.
1482 * \see dt_object_operations::do_xattr_get() in the API description for details.
1484 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1485 struct lu_buf *buf, const char *name)
1487 struct lod_thread_info *info = lod_env_info(env);
1488 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1493 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1494 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1495 struct lmv_mds_md_v1 *lmv1;
1496 struct lmv_foreign_md *lfm;
1499 if (rc > (typeof(rc))sizeof(*lmv1))
1502 /* short (<= sizeof(struct lmv_mds_md_v1)) foreign LMV case */
1503 /* XXX empty foreign LMV is not allowed */
1504 if (rc <= offsetof(typeof(*lfm), lfm_value))
1505 RETURN(rc = rc > 0 ? -EINVAL : rc);
1507 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1508 CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1510 /* lti_buf is large enough for *lmv1 or a short
1511 * (<= sizeof(struct lmv_mds_md_v1)) foreign LMV
1513 info->lti_buf.lb_buf = info->lti_key;
1514 info->lti_buf.lb_len = sizeof(*lmv1);
1515 rc = dt_xattr_get(env, dt_object_child(dt),
1516 &info->lti_buf, name);
1517 if (unlikely(rc <= offsetof(typeof(*lfm),
1519 RETURN(rc = rc > 0 ? -EINVAL : rc);
1521 lfm = info->lti_buf.lb_buf;
1522 if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN)
1525 if (unlikely(rc != sizeof(*lmv1)))
1526 RETURN(rc = rc > 0 ? -EINVAL : rc);
1528 lmv1 = info->lti_buf.lb_buf;
1529 /* The on-disk LMV EA only contains header, but the
1530 * returned LMV EA size should contain the space for
1531 * the FIDs of all shards of the striped directory. */
1532 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1533 rc = lmv_mds_md_size(
1534 le32_to_cpu(lmv1->lmv_stripe_count),
1538 if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN)
1541 if (rc != sizeof(*lmv1))
1542 RETURN(rc = rc > 0 ? -EINVAL : rc);
1544 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1548 RETURN(rc = rc1 != 0 ? rc1 : rc);
1551 if ((rc > 0) && buf->lb_buf && strcmp(name, XATTR_NAME_LOV) == 0) {
1552 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1554 if (lcm->lcm_magic == cpu_to_le32(LOV_MAGIC_SEL))
1555 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1558 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1562 * XXX: Only used by lfsck
1564 * lod returns default striping on the real root of the device
1565 * this is like the root stores default striping for the whole
1566 * filesystem. historically we've been using a different approach
1567 * and store it in the config.
1569 dt_root_get(env, dev->lod_child, &info->lti_fid);
1570 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1572 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1573 struct lov_user_md *lum = buf->lb_buf;
1574 struct lov_desc *desc = &dev->lod_ost_descs.ltd_lov_desc;
1576 if (buf->lb_buf == NULL) {
1578 } else if (buf->lb_len >= sizeof(*lum)) {
1579 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1580 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1581 lmm_oi_set_id(&lum->lmm_oi, 0);
1582 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1583 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1584 lum->lmm_stripe_size = cpu_to_le32(
1585 desc->ld_default_stripe_size);
1586 lum->lmm_stripe_count = cpu_to_le16(
1587 desc->ld_default_stripe_count);
1588 lum->lmm_stripe_offset = cpu_to_le16(
1589 desc->ld_default_stripe_offset);
1602 * Checks that the magic of the stripe is sane.
1604 * \param[in] lod lod device
1605 * \param[in] lum a buffer storing LMV EA to verify
1607 * \retval 0 if the EA is sane
1608 * \retval negative otherwise
1610 static int lod_verify_md_striping(struct lod_device *lod,
1611 const struct lmv_user_md_v1 *lum)
1613 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1614 CERROR("%s: invalid lmv_user_md: magic = %x, "
1615 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1616 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1617 (int)le32_to_cpu(lum->lum_stripe_offset),
1618 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1626 * Initialize LMV EA for a slave.
1628 * Initialize slave's LMV EA from the master's LMV EA.
1630 * \param[in] master_lmv a buffer containing master's EA
1631 * \param[out] slave_lmv a buffer where slave's EA will be stored
1634 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1635 const struct lmv_mds_md_v1 *master_lmv)
1637 *slave_lmv = *master_lmv;
1638 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1644 * Generate LMV EA from the object passed as \a dt. The object must have
1645 * the stripes created and initialized.
1647 * \param[in] env execution environment
1648 * \param[in] dt object
1649 * \param[out] lmv_buf buffer storing generated LMV EA
1651 * \retval 0 on success
1652 * \retval negative if failed
1654 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1655 struct lu_buf *lmv_buf)
1657 struct lod_thread_info *info = lod_env_info(env);
1658 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1659 struct lod_object *lo = lod_dt_obj(dt);
1660 struct lmv_mds_md_v1 *lmm1;
1662 int type = LU_SEQ_RANGE_ANY;
1667 LASSERT(lo->ldo_dir_striped != 0);
1668 LASSERT(lo->ldo_dir_stripe_count > 0);
1669 stripe_count = lo->ldo_dir_stripe_count;
1670 /* Only store the LMV EA heahder on the disk. */
1671 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1672 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1676 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1679 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1680 memset(lmm1, 0, sizeof(*lmm1));
1681 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1682 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1683 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1684 if (lo->ldo_dir_hash_type & LMV_HASH_FLAG_MIGRATION) {
1685 lmm1->lmv_migrate_hash = cpu_to_le32(lo->ldo_dir_migrate_hash);
1686 lmm1->lmv_migrate_offset =
1687 cpu_to_le32(lo->ldo_dir_migrate_offset);
1689 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1694 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1695 lmv_buf->lb_buf = info->lti_ea_store;
1696 lmv_buf->lb_len = sizeof(*lmm1);
1702 * Create in-core represenation for a striped directory.
1704 * Parse the buffer containing LMV EA and instantiate LU objects
1705 * representing the stripe objects. The pointers to the objects are
1706 * stored in ldo_stripe field of \a lo. This function is used when
1707 * we need to access an already created object (i.e. load from a disk).
1709 * \param[in] env execution environment
1710 * \param[in] lo lod object
1711 * \param[in] buf buffer containing LMV EA
1713 * \retval 0 on success
1714 * \retval negative if failed
1716 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1717 const struct lu_buf *buf)
1719 struct lod_thread_info *info = lod_env_info(env);
1720 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1721 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1722 struct dt_object **stripe;
1723 union lmv_mds_md *lmm = buf->lb_buf;
1724 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1725 struct lu_fid *fid = &info->lti_fid;
1730 LASSERT(mutex_is_locked(&lo->ldo_layout_mutex));
1732 /* XXX may be useless as not called for foreign LMV ?? */
1733 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_FOREIGN)
1736 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1737 lo->ldo_dir_slave_stripe = 1;
1741 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1744 if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1747 LASSERT(lo->ldo_stripe == NULL);
1748 OBD_ALLOC(stripe, sizeof(stripe[0]) *
1749 (le32_to_cpu(lmv1->lmv_stripe_count)));
1753 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1754 struct dt_device *tgt_dt;
1755 struct dt_object *dto;
1756 int type = LU_SEQ_RANGE_ANY;
1759 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1760 if (!fid_is_sane(fid)) {
1765 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1769 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1770 tgt_dt = lod->lod_child;
1772 struct lod_tgt_desc *tgt;
1774 tgt = LTD_TGT(ltd, idx);
1776 GOTO(out, rc = -ESTALE);
1777 tgt_dt = tgt->ltd_tgt;
1780 dto = dt_locate_at(env, tgt_dt, fid,
1781 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1784 GOTO(out, rc = PTR_ERR(dto));
1789 lo->ldo_stripe = stripe;
1790 lo->ldo_dir_stripe_count = le32_to_cpu(lmv1->lmv_stripe_count);
1791 lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1793 lod_striping_free_nolock(env, lo);
1799 * Declare create a striped directory.
1801 * Declare creating a striped directory with a given stripe pattern on the
1802 * specified MDTs. A striped directory is represented as a regular directory
1803 * - an index listing all the stripes. The stripes point back to the master
1804 * object with ".." and LinkEA. The master object gets LMV EA which
1805 * identifies it as a striped directory. The function allocates FIDs
1808 * \param[in] env execution environment
1809 * \param[in] dt object
1810 * \param[in] attr attributes to initialize the objects with
1811 * \param[in] dof type of objects to be created
1812 * \param[in] th transaction handle
1814 * \retval 0 on success
1815 * \retval negative if failed
1817 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1818 struct dt_object *dt,
1819 struct lu_attr *attr,
1820 struct dt_object_format *dof,
1823 struct lod_thread_info *info = lod_env_info(env);
1824 struct lu_buf lmv_buf;
1825 struct lu_buf slave_lmv_buf;
1826 struct lmv_mds_md_v1 *lmm;
1827 struct lmv_mds_md_v1 *slave_lmm = NULL;
1828 struct dt_insert_rec *rec = &info->lti_dt_rec;
1829 struct lod_object *lo = lod_dt_obj(dt);
1834 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1837 lmm = lmv_buf.lb_buf;
1839 OBD_ALLOC_PTR(slave_lmm);
1840 if (slave_lmm == NULL)
1841 GOTO(out, rc = -ENOMEM);
1843 lod_prep_slave_lmv_md(slave_lmm, lmm);
1844 slave_lmv_buf.lb_buf = slave_lmm;
1845 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1847 if (!dt_try_as_dir(env, dt_object_child(dt)))
1848 GOTO(out, rc = -EINVAL);
1850 rec->rec_type = S_IFDIR;
1851 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1852 struct dt_object *dto = lo->ldo_stripe[i];
1853 char *stripe_name = info->lti_key;
1854 struct lu_name *sname;
1855 struct linkea_data ldata = { NULL };
1856 struct lu_buf linkea_buf;
1858 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
1862 rc = lod_sub_declare_create(env, dto, attr, NULL, dof, th);
1866 if (!dt_try_as_dir(env, dto))
1867 GOTO(out, rc = -EINVAL);
1869 rc = lod_sub_declare_ref_add(env, dto, th);
1873 rec->rec_fid = lu_object_fid(&dto->do_lu);
1874 rc = lod_sub_declare_insert(env, dto,
1875 (const struct dt_rec *)rec,
1876 (const struct dt_key *)dot, th);
1880 /* master stripe FID will be put to .. */
1881 rec->rec_fid = lu_object_fid(&dt->do_lu);
1882 rc = lod_sub_declare_insert(env, dto,
1883 (const struct dt_rec *)rec,
1884 (const struct dt_key *)dotdot, th);
1888 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1889 cfs_fail_val != i) {
1890 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1892 slave_lmm->lmv_master_mdt_index =
1895 slave_lmm->lmv_master_mdt_index =
1897 rc = lod_sub_declare_xattr_set(env, dto, &slave_lmv_buf,
1898 XATTR_NAME_LMV, 0, th);
1903 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1905 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1906 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1908 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1909 PFID(lu_object_fid(&dto->do_lu)), i);
1911 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1912 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1913 sname, lu_object_fid(&dt->do_lu));
1917 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1918 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1919 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
1920 XATTR_NAME_LINK, 0, th);
1924 rec->rec_fid = lu_object_fid(&dto->do_lu);
1925 rc = lod_sub_declare_insert(env, dt_object_child(dt),
1926 (const struct dt_rec *)rec,
1927 (const struct dt_key *)stripe_name,
1932 rc = lod_sub_declare_ref_add(env, dt_object_child(dt), th);
1937 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt),
1938 &lmv_buf, XATTR_NAME_LMV, 0, th);
1942 if (slave_lmm != NULL)
1943 OBD_FREE_PTR(slave_lmm);
1949 * Allocate a striping on a predefined set of MDTs.
1951 * Allocates new striping using the MDT index range provided by the data from
1952 * the lum_obejcts contained in the lmv_user_md passed to this method if
1953 * \a is_specific is true; or allocates new layout starting from MDT index in
1954 * lo->ldo_dir_stripe_offset. The exact order of MDTs is not important and
1955 * varies depending on MDT status. The number of stripes needed and stripe
1956 * offset are taken from the object. If that number cannot be met, then the
1957 * function returns an error and then it's the caller's responsibility to
1958 * release the stripes allocated. All the internal structures are protected,
1959 * but no concurrent allocation is allowed on the same objects.
1961 * \param[in] env execution environment for this thread
1962 * \param[in] lo LOD object
1963 * \param[out] stripes striping created
1964 * \param[out] mdt_indices MDT indices of striping created
1965 * \param[in] is_specific true if the MDTs are provided by lum; false if
1966 * only the starting MDT index is provided
1968 * \retval positive stripes allocated, including the first stripe allocated
1970 * \retval negative errno on failure
1972 static int lod_mdt_alloc_specific(const struct lu_env *env,
1973 struct lod_object *lo,
1974 struct dt_object **stripes,
1975 __u32 *mdt_indices, bool is_specific)
1977 struct lod_thread_info *info = lod_env_info(env);
1978 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1979 struct lu_tgt_descs *ltd = &lod->lod_mdt_descs;
1980 struct lu_tgt_desc *tgt = NULL;
1981 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
1982 struct dt_device *tgt_dt = NULL;
1983 struct lu_fid fid = { 0 };
1984 struct dt_object *dto;
1986 u32 stripe_count = lo->ldo_dir_stripe_count;
1992 master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1993 if (stripe_count > 1)
1994 /* Set the start index for the 2nd stripe allocation */
1995 mdt_indices[1] = (mdt_indices[0] + 1) %
1996 (lod->lod_remote_mdt_count + 1);
1998 for (; stripe_idx < stripe_count; stripe_idx++) {
1999 /* Try to find next avaible target */
2000 idx = mdt_indices[stripe_idx];
2001 for (j = 0; j < lod->lod_remote_mdt_count;
2002 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
2003 bool already_allocated = false;
2006 CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
2007 idx, lod->lod_remote_mdt_count + 1, stripe_idx);
2009 if (likely(!is_specific &&
2010 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
2011 /* check whether the idx already exists
2012 * in current allocated array */
2013 for (k = 0; k < stripe_idx; k++) {
2014 if (mdt_indices[k] == idx) {
2015 already_allocated = true;
2020 if (already_allocated)
2024 /* Sigh, this index is not in the bitmap, let's check
2025 * next available target */
2026 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) &&
2027 idx != master_index)
2030 if (idx == master_index) {
2031 /* Allocate the FID locally */
2032 rc = obd_fid_alloc(env, lod->lod_child_exp,
2036 tgt_dt = lod->lod_child;
2040 /* check the status of the OSP */
2041 tgt = LTD_TGT(ltd, idx);
2045 tgt_dt = tgt->ltd_tgt;
2046 rc = dt_statfs(env, tgt_dt, &info->lti_osfs);
2048 /* this OSP doesn't feel well */
2051 rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
2058 /* Can not allocate more stripes */
2059 if (j == lod->lod_remote_mdt_count) {
2060 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
2061 lod2obd(lod)->obd_name, stripe_count,
2066 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
2067 idx, stripe_idx, PFID(&fid));
2068 mdt_indices[stripe_idx] = idx;
2069 /* Set the start index for next stripe allocation */
2070 if (!is_specific && stripe_idx < stripe_count - 1) {
2072 * for large dir test, put all other slaves on one
2073 * remote MDT, otherwise we may save too many local
2074 * slave locks which will exceed RS_MAX_LOCKS.
2076 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)))
2078 mdt_indices[stripe_idx + 1] = (idx + 1) %
2079 (lod->lod_remote_mdt_count + 1);
2081 /* tgt_dt and fid must be ready after search avaible OSP
2082 * in the above loop */
2083 LASSERT(tgt_dt != NULL);
2084 LASSERT(fid_is_sane(&fid));
2086 /* fail a remote stripe FID allocation */
2087 if (stripe_idx && OBD_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_FID))
2090 dto = dt_locate_at(env, tgt_dt, &fid,
2091 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
2098 stripes[stripe_idx] = dto;
2104 for (j = 1; j < stripe_idx; j++) {
2105 LASSERT(stripes[j] != NULL);
2106 dt_object_put(env, stripes[j]);
2112 static int lod_prep_md_striped_create(const struct lu_env *env,
2113 struct dt_object *dt,
2114 struct lu_attr *attr,
2115 const struct lmv_user_md_v1 *lum,
2116 struct dt_object_format *dof,
2119 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
2120 struct lod_object *lo = lod_dt_obj(dt);
2121 struct dt_object **stripes;
2122 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2123 struct lu_fid fid = { 0 };
2130 /* The lum has been verifed in lod_verify_md_striping */
2131 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC ||
2132 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC);
2134 stripe_count = lo->ldo_dir_stripe_count;
2136 OBD_ALLOC(stripes, sizeof(stripes[0]) * stripe_count);
2140 /* Allocate the first stripe locally */
2141 rc = obd_fid_alloc(env, lod->lod_child_exp, &fid, NULL);
2145 stripes[0] = dt_locate_at(env, lod->lod_child, &fid,
2146 dt->do_lu.lo_dev->ld_site->ls_top_dev, &conf);
2147 if (IS_ERR(stripes[0]))
2148 GOTO(out, rc = PTR_ERR(stripes[0]));
2150 if (lo->ldo_dir_stripe_offset == LMV_OFFSET_DEFAULT) {
2151 lod_qos_statfs_update(env, lod, &lod->lod_mdt_descs);
2152 rc = lod_mdt_alloc_qos(env, lo, stripes);
2154 rc = lod_mdt_alloc_rr(env, lo, stripes);
2157 bool is_specific = false;
2159 OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
2161 GOTO(out, rc = -ENOMEM);
2163 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) {
2165 for (i = 0; i < stripe_count; i++)
2167 le32_to_cpu(lum->lum_objects[i].lum_mds);
2170 /* stripe 0 is local */
2172 lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
2173 rc = lod_mdt_alloc_specific(env, lo, stripes, idx_array,
2175 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
2183 lo->ldo_dir_striped = 1;
2184 lo->ldo_stripe = stripes;
2185 lo->ldo_dir_stripe_count = rc;
2186 lo->ldo_dir_stripes_allocated = stripe_count;
2188 lo->ldo_dir_stripe_loaded = 1;
2190 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
2192 lod_striping_free(env, lo);
2198 if (!IS_ERR_OR_NULL(stripes[0]))
2199 dt_object_put(env, stripes[0]);
2200 for (i = 1; i < stripe_count; i++)
2201 LASSERT(!stripes[i]);
2202 OBD_FREE(stripes, sizeof(stripes[0]) * stripe_count);
2209 * Alloc cached foreign LMV
2211 * \param[in] lo object
2212 * \param[in] size size of foreign LMV
2214 * \retval 0 on success
2215 * \retval negative if failed
2217 int lod_alloc_foreign_lmv(struct lod_object *lo, size_t size)
2219 OBD_ALLOC_LARGE(lo->ldo_foreign_lmv, size);
2220 if (lo->ldo_foreign_lmv == NULL)
2222 lo->ldo_foreign_lmv_size = size;
2223 lo->ldo_dir_is_foreign = 1;
2229 * Declare create striped md object.
2231 * The function declares intention to create a striped directory. This is a
2232 * wrapper for lod_prep_md_striped_create(). The only additional functionality
2233 * is to verify pattern \a lum_buf is good. Check that function for the details.
2235 * \param[in] env execution environment
2236 * \param[in] dt object
2237 * \param[in] attr attributes to initialize the objects with
2238 * \param[in] lum_buf a pattern specifying the number of stripes and
2240 * \param[in] dof type of objects to be created
2241 * \param[in] th transaction handle
2243 * \retval 0 on success
2244 * \retval negative if failed
2247 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
2248 struct dt_object *dt,
2249 struct lu_attr *attr,
2250 const struct lu_buf *lum_buf,
2251 struct dt_object_format *dof,
2254 struct lod_object *lo = lod_dt_obj(dt);
2255 struct lmv_user_md_v1 *lum = lum_buf->lb_buf;
2259 LASSERT(lum != NULL);
2261 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
2262 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
2263 (int)le32_to_cpu(lum->lum_stripe_offset));
2265 if (lo->ldo_dir_stripe_count == 0) {
2266 if (lo->ldo_dir_is_foreign) {
2267 rc = lod_alloc_foreign_lmv(lo, lum_buf->lb_len);
2270 memcpy(lo->ldo_foreign_lmv, lum, lum_buf->lb_len);
2271 lo->ldo_dir_stripe_loaded = 1;
2276 /* prepare dir striped objects */
2277 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2279 /* failed to create striping, let's reset
2280 * config so that others don't get confused */
2281 lod_striping_free(env, lo);
2289 * Append source stripes after target stripes for migrating directory. NB, we
2290 * only need to declare this, the append is done inside lod_xattr_set_lmv().
2292 * \param[in] env execution environment
2293 * \param[in] dt target object
2294 * \param[in] buf LMV buf which contains source stripe fids
2295 * \param[in] th transaction handle
2297 * \retval 0 on success
2298 * \retval negative if failed
2300 static int lod_dir_declare_layout_add(const struct lu_env *env,
2301 struct dt_object *dt,
2302 const struct lu_buf *buf,
2305 struct lod_thread_info *info = lod_env_info(env);
2306 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
2307 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
2308 struct lod_object *lo = lod_dt_obj(dt);
2309 struct dt_object *next = dt_object_child(dt);
2310 struct dt_object_format *dof = &info->lti_format;
2311 struct lmv_mds_md_v1 *lmv = buf->lb_buf;
2312 struct dt_object **stripe;
2313 __u32 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
2314 struct lu_fid *fid = &info->lti_fid;
2315 struct lod_tgt_desc *tgt;
2316 struct dt_object *dto;
2317 struct dt_device *tgt_dt;
2318 int type = LU_SEQ_RANGE_ANY;
2319 struct dt_insert_rec *rec = &info->lti_dt_rec;
2320 char *stripe_name = info->lti_key;
2321 struct lu_name *sname;
2322 struct linkea_data ldata = { NULL };
2323 struct lu_buf linkea_buf;
2330 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
2333 if (stripe_count == 0)
2336 dof->dof_type = DFT_DIR;
2339 sizeof(*stripe) * (lo->ldo_dir_stripe_count + stripe_count));
2343 for (i = 0; i < lo->ldo_dir_stripe_count; i++)
2344 stripe[i] = lo->ldo_stripe[i];
2346 for (i = 0; i < stripe_count; i++) {
2348 &lmv->lmv_stripe_fids[i]);
2349 if (!fid_is_sane(fid))
2352 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
2356 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
2357 tgt_dt = lod->lod_child;
2359 tgt = LTD_TGT(ltd, idx);
2361 GOTO(out, rc = -ESTALE);
2362 tgt_dt = tgt->ltd_tgt;
2365 dto = dt_locate_at(env, tgt_dt, fid,
2366 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
2369 GOTO(out, rc = PTR_ERR(dto));
2371 stripe[i + lo->ldo_dir_stripe_count] = dto;
2373 if (!dt_try_as_dir(env, dto))
2374 GOTO(out, rc = -ENOTDIR);
2376 rc = lod_sub_declare_ref_add(env, dto, th);
2380 rc = lod_sub_declare_insert(env, dto,
2381 (const struct dt_rec *)rec,
2382 (const struct dt_key *)dot, th);
2386 rc = lod_sub_declare_insert(env, dto,
2387 (const struct dt_rec *)rec,
2388 (const struct dt_key *)dotdot, th);
2392 rc = lod_sub_declare_xattr_set(env, dto, buf,
2393 XATTR_NAME_LMV, 0, th);
2397 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
2398 PFID(lu_object_fid(&dto->do_lu)),
2399 i + lo->ldo_dir_stripe_count);
2401 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2402 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
2403 sname, lu_object_fid(&dt->do_lu));
2407 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
2408 linkea_buf.lb_len = ldata.ld_leh->leh_len;
2409 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
2410 XATTR_NAME_LINK, 0, th);
2414 rc = lod_sub_declare_insert(env, next,
2415 (const struct dt_rec *)rec,
2416 (const struct dt_key *)stripe_name,
2421 rc = lod_sub_declare_ref_add(env, next, th);
2427 OBD_FREE(lo->ldo_stripe,
2428 sizeof(*stripe) * lo->ldo_dir_stripes_allocated);
2429 lo->ldo_stripe = stripe;
2430 lo->ldo_dir_migrate_offset = lo->ldo_dir_stripe_count;
2431 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_hash_type);
2432 lo->ldo_dir_stripe_count += stripe_count;
2433 lo->ldo_dir_stripes_allocated += stripe_count;
2434 lo->ldo_dir_hash_type |= LMV_HASH_FLAG_MIGRATION;
2438 i = lo->ldo_dir_stripe_count;
2439 while (i < lo->ldo_dir_stripe_count + stripe_count && stripe[i])
2440 dt_object_put(env, stripe[i++]);
2443 sizeof(*stripe) * (stripe_count + lo->ldo_dir_stripe_count));
2447 static int lod_dir_declare_layout_delete(const struct lu_env *env,
2448 struct dt_object *dt,
2449 const struct lu_buf *buf,
2452 struct lod_thread_info *info = lod_env_info(env);
2453 struct lod_object *lo = lod_dt_obj(dt);
2454 struct dt_object *next = dt_object_child(dt);
2455 struct lmv_user_md *lmu = buf->lb_buf;
2456 __u32 final_stripe_count;
2457 char *stripe_name = info->lti_key;
2458 struct dt_object *dto;
2465 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
2466 if (final_stripe_count >= lo->ldo_dir_stripe_count)
2469 for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
2470 dto = lo->ldo_stripe[i];
2474 if (!dt_try_as_dir(env, dto))
2477 rc = lod_sub_declare_delete(env, dto,
2478 (const struct dt_key *)dot, th);
2482 rc = lod_sub_declare_ref_del(env, dto, th);
2486 rc = lod_sub_declare_delete(env, dto,
2487 (const struct dt_key *)dotdot, th);
2491 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2492 PFID(lu_object_fid(&dto->do_lu)), i);
2494 rc = lod_sub_declare_delete(env, next,
2495 (const struct dt_key *)stripe_name, th);
2499 rc = lod_sub_declare_ref_del(env, next, th);
2508 * delete stripes from dir master object, the lum_stripe_count in argument is
2509 * the final stripe count, the stripes after that will be deleted, NB, they
2510 * are not destroyed, but deleted from it's parent namespace, this function
2511 * will be called in two places:
2512 * 1. mdd_migrate_create() delete stripes from source, and append them to
2514 * 2. mdd_dir_layout_shrink() delete stripes from source, and destroy them.
2516 static int lod_dir_layout_delete(const struct lu_env *env,
2517 struct dt_object *dt,
2518 const struct lu_buf *buf,
2521 struct lod_thread_info *info = lod_env_info(env);
2522 struct lod_object *lo = lod_dt_obj(dt);
2523 struct dt_object *next = dt_object_child(dt);
2524 struct lmv_user_md *lmu = buf->lb_buf;
2525 __u32 final_stripe_count;
2526 char *stripe_name = info->lti_key;
2527 struct dt_object *dto;
2536 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
2537 if (final_stripe_count >= lo->ldo_dir_stripe_count)
2540 for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
2541 dto = lo->ldo_stripe[i];
2545 rc = lod_sub_delete(env, dto,
2546 (const struct dt_key *)dotdot, th);
2550 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2551 PFID(lu_object_fid(&dto->do_lu)), i);
2553 rc = lod_sub_delete(env, next,
2554 (const struct dt_key *)stripe_name, th);
2558 rc = lod_sub_ref_del(env, next, th);
2563 lod_striping_free(env, lod_dt_obj(dt));
2569 * Implementation of dt_object_operations::do_declare_xattr_set.
2571 * Used with regular (non-striped) objects. Basically it
2572 * initializes the striping information and applies the
2573 * change to all the stripes.
2575 * \see dt_object_operations::do_declare_xattr_set() in the API description
2578 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2579 struct dt_object *dt,
2580 const struct lu_buf *buf,
2581 const char *name, int fl,
2584 struct dt_object *next = dt_object_child(dt);
2585 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2586 struct lod_object *lo = lod_dt_obj(dt);
2591 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2592 struct lmv_user_md_v1 *lum;
2594 LASSERT(buf != NULL && buf->lb_buf != NULL);
2596 rc = lod_verify_md_striping(d, lum);
2599 } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2600 rc = lod_verify_striping(d, lo, buf, false);
2605 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2609 /* Note: Do not set LinkEA on sub-stripes, otherwise
2610 * it will confuse the fid2path process(see mdt_path_current()).
2611 * The linkEA between master and sub-stripes is set in
2612 * lod_xattr_set_lmv(). */
2613 if (strcmp(name, XATTR_NAME_LINK) == 0)
2616 /* set xattr to each stripes, if needed */
2617 rc = lod_striping_load(env, lo);
2621 if (lo->ldo_dir_stripe_count == 0)
2624 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2625 if (!lo->ldo_stripe[i])
2628 if (!dt_object_exists(lo->ldo_stripe[i]))
2631 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2641 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2642 struct lod_object *lo,
2643 struct dt_object *dt, struct thandle *th,
2644 int comp_idx, int stripe_idx,
2645 struct lod_obj_stripe_cb_data *data)
2647 struct lod_thread_info *info = lod_env_info(env);
2648 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
2649 struct filter_fid *ff = &info->lti_ff;
2650 struct lu_buf *buf = &info->lti_buf;
2654 buf->lb_len = sizeof(*ff);
2655 rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2663 * locd_buf is set if it's called by dir migration, which doesn't check
2666 if (data->locd_buf) {
2667 memset(ff, 0, sizeof(*ff));
2668 ff->ff_parent = *(struct lu_fid *)data->locd_buf->lb_buf;
2670 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
2672 if (lu_fid_eq(lod_object_fid(lo), &ff->ff_parent) &&
2673 ff->ff_layout.ol_comp_id == comp->llc_id)
2676 memset(ff, 0, sizeof(*ff));
2677 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2680 /* rewrite filter_fid */
2681 ff->ff_parent.f_ver = stripe_idx;
2682 ff->ff_layout.ol_stripe_size = comp->llc_stripe_size;
2683 ff->ff_layout.ol_stripe_count = comp->llc_stripe_count;
2684 ff->ff_layout.ol_comp_id = comp->llc_id;
2685 ff->ff_layout.ol_comp_start = comp->llc_extent.e_start;
2686 ff->ff_layout.ol_comp_end = comp->llc_extent.e_end;
2687 filter_fid_cpu_to_le(ff, ff, sizeof(*ff));
2689 if (data->locd_declare)
2690 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2691 LU_XATTR_REPLACE, th);
2693 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2694 LU_XATTR_REPLACE, th);
2700 * Reset parent FID on OST object
2702 * Replace parent FID with @dt object FID, which is only called during migration
2703 * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2704 * the FID is changed.
2706 * \param[in] env execution environment
2707 * \param[in] dt dt_object whose stripes's parent FID will be reset
2708 * \parem[in] th thandle
2709 * \param[in] declare if it is declare
2711 * \retval 0 if reset succeeds
2712 * \retval negative errno if reset fails
2714 static int lod_replace_parent_fid(const struct lu_env *env,
2715 struct dt_object *dt,
2716 const struct lu_buf *buf,
2717 struct thandle *th, bool declare)
2719 struct lod_object *lo = lod_dt_obj(dt);
2720 struct lod_thread_info *info = lod_env_info(env);
2721 struct filter_fid *ff;
2722 struct lod_obj_stripe_cb_data data = { { 0 } };
2726 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2728 /* set xattr to each stripes, if needed */
2729 rc = lod_striping_load(env, lo);
2733 if (!lod_obj_is_striped(dt))
2736 if (info->lti_ea_store_size < sizeof(*ff)) {
2737 rc = lod_ea_store_resize(info, sizeof(*ff));
2742 data.locd_declare = declare;
2743 data.locd_stripe_cb = lod_obj_stripe_replace_parent_fid_cb;
2744 data.locd_buf = buf;
2745 rc = lod_obj_for_each_stripe(env, lo, th, &data);
2750 inline __u16 lod_comp_entry_stripe_count(struct lod_object *lo,
2751 struct lod_layout_component *entry,
2754 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2758 else if (lod_comp_inited(entry))
2759 return entry->llc_stripe_count;
2760 else if ((__u16)-1 == entry->llc_stripe_count)
2761 return lod->lod_ost_count;
2763 return lod_get_stripe_count(lod, lo,
2764 entry->llc_stripe_count, false);
2767 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2769 int magic, size = 0, i;
2770 struct lod_layout_component *comp_entries;
2772 bool is_composite, is_foreign = false;
2775 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2776 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2778 lo->ldo_def_striping->lds_def_striping_is_composite;
2780 comp_cnt = lo->ldo_comp_cnt;
2781 comp_entries = lo->ldo_comp_entries;
2782 is_composite = lo->ldo_is_composite;
2783 is_foreign = lo->ldo_is_foreign;
2787 return lo->ldo_foreign_lov_size;
2789 LASSERT(comp_cnt != 0 && comp_entries != NULL);
2791 size = sizeof(struct lov_comp_md_v1) +
2792 sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2793 LASSERT(size % sizeof(__u64) == 0);
2796 for (i = 0; i < comp_cnt; i++) {
2799 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2800 stripe_count = lod_comp_entry_stripe_count(lo, &comp_entries[i],
2802 if (!is_dir && is_composite)
2803 lod_comp_shrink_stripe_count(&comp_entries[i],
2806 size += lov_user_md_size(stripe_count, magic);
2807 LASSERT(size % sizeof(__u64) == 0);
2813 * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2814 * the xattr value is binary lov_comp_md_v1 which contains component(s)
2817 * \param[in] env execution environment
2818 * \param[in] dt dt_object to add components on
2819 * \param[in] buf buffer contains components to be added
2820 * \parem[in] th thandle
2822 * \retval 0 on success
2823 * \retval negative errno on failure
2825 static int lod_declare_layout_add(const struct lu_env *env,
2826 struct dt_object *dt,
2827 const struct lu_buf *buf,
2830 struct lod_thread_info *info = lod_env_info(env);
2831 struct lod_layout_component *comp_array, *lod_comp, *old_array;
2832 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2833 struct dt_object *next = dt_object_child(dt);
2834 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
2835 struct lod_object *lo = lod_dt_obj(dt);
2836 struct lov_user_md_v3 *v3;
2837 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2839 int i, rc, array_cnt, old_array_cnt;
2842 LASSERT(lo->ldo_is_composite);
2844 if (lo->ldo_flr_state != LCM_FL_NONE)
2847 rc = lod_verify_striping(d, lo, buf, false);
2851 magic = comp_v1->lcm_magic;
2852 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2853 lustre_swab_lov_comp_md_v1(comp_v1);
2854 magic = comp_v1->lcm_magic;
2857 if (magic != LOV_USER_MAGIC_COMP_V1)
2860 array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2861 OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2862 if (comp_array == NULL)
2865 memcpy(comp_array, lo->ldo_comp_entries,
2866 sizeof(*comp_array) * lo->ldo_comp_cnt);
2868 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2869 struct lov_user_md_v1 *v1;
2870 struct lu_extent *ext;
2872 v1 = (struct lov_user_md *)((char *)comp_v1 +
2873 comp_v1->lcm_entries[i].lcme_offset);
2874 ext = &comp_v1->lcm_entries[i].lcme_extent;
2876 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2877 lod_comp->llc_extent.e_start = ext->e_start;
2878 lod_comp->llc_extent.e_end = ext->e_end;
2879 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2880 lod_comp->llc_flags = comp_v1->lcm_entries[i].lcme_flags;
2882 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2883 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2884 lod_adjust_stripe_info(lod_comp, desc, 0);
2886 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2887 v3 = (struct lov_user_md_v3 *) v1;
2888 if (v3->lmm_pool_name[0] != '\0') {
2889 rc = lod_set_pool(&lod_comp->llc_pool,
2897 old_array = lo->ldo_comp_entries;
2898 old_array_cnt = lo->ldo_comp_cnt;
2900 lo->ldo_comp_entries = comp_array;
2901 lo->ldo_comp_cnt = array_cnt;
2903 /* No need to increase layout generation here, it will be increased
2904 * later when generating component ID for the new components */
2906 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2907 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2908 XATTR_NAME_LOV, 0, th);
2910 lo->ldo_comp_entries = old_array;
2911 lo->ldo_comp_cnt = old_array_cnt;
2915 OBD_FREE(old_array, sizeof(*lod_comp) * old_array_cnt);
2917 LASSERT(lo->ldo_mirror_count == 1);
2918 lo->ldo_mirrors[0].lme_end = array_cnt - 1;
2923 for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2924 lod_comp = &comp_array[i];
2925 if (lod_comp->llc_pool != NULL) {
2926 OBD_FREE(lod_comp->llc_pool,
2927 strlen(lod_comp->llc_pool) + 1);
2928 lod_comp->llc_pool = NULL;
2931 OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2936 * lod_last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2937 * @mirror_id: Mirror id to be checked.
2940 * This function checks if a mirror with specified @mirror_id is the last
2941 * non-stale mirror of a LOD object @lo.
2943 * Return: true or false.
2946 bool lod_last_non_stale_mirror(__u16 mirror_id, struct lod_object *lo)
2948 struct lod_layout_component *lod_comp;
2949 bool has_stale_flag;
2952 for (i = 0; i < lo->ldo_mirror_count; i++) {
2953 if (lo->ldo_mirrors[i].lme_id == mirror_id ||
2954 lo->ldo_mirrors[i].lme_stale)
2957 has_stale_flag = false;
2958 lod_foreach_mirror_comp(lod_comp, lo, i) {
2959 if (lod_comp->llc_flags & LCME_FL_STALE) {
2960 has_stale_flag = true;
2964 if (!has_stale_flag)
2972 * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2973 * the '$field' can only be 'flags' now. The xattr value is binary
2974 * lov_comp_md_v1 which contains the component ID(s) and the value of
2975 * the field to be modified.
2977 * \param[in] env execution environment
2978 * \param[in] dt dt_object to be modified
2979 * \param[in] op operation string, like "set.flags"
2980 * \param[in] buf buffer contains components to be set
2981 * \parem[in] th thandle
2983 * \retval 0 on success
2984 * \retval negative errno on failure
2986 static int lod_declare_layout_set(const struct lu_env *env,
2987 struct dt_object *dt,
2988 char *op, const struct lu_buf *buf,
2991 struct lod_layout_component *lod_comp;
2992 struct lod_thread_info *info = lod_env_info(env);
2993 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2994 struct lod_object *lo = lod_dt_obj(dt);
2995 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2998 bool changed = false;
3001 if (strcmp(op, "set.flags") != 0) {
3002 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
3003 lod2obd(d)->obd_name, op);
3007 magic = comp_v1->lcm_magic;
3008 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3009 lustre_swab_lov_comp_md_v1(comp_v1);
3010 magic = comp_v1->lcm_magic;
3013 if (magic != LOV_USER_MAGIC_COMP_V1)
3016 if (comp_v1->lcm_entry_count == 0) {
3017 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
3018 lod2obd(d)->obd_name);
3022 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3023 __u32 id = comp_v1->lcm_entries[i].lcme_id;
3024 __u32 flags = comp_v1->lcm_entries[i].lcme_flags;
3025 __u32 mirror_flag = flags & LCME_MIRROR_FLAGS;
3026 __u16 mirror_id = mirror_id_of(id);
3027 bool neg = flags & LCME_FL_NEG;
3029 if (flags & LCME_FL_INIT) {
3031 lod_striping_free(env, lo);
3035 flags &= ~(LCME_MIRROR_FLAGS | LCME_FL_NEG);
3036 for (j = 0; j < lo->ldo_comp_cnt; j++) {
3037 lod_comp = &lo->ldo_comp_entries[j];
3039 /* lfs only put one flag in each entry */
3040 if ((flags && id != lod_comp->llc_id) ||
3041 (mirror_flag && mirror_id !=
3042 mirror_id_of(lod_comp->llc_id)))
3047 lod_comp->llc_flags &= ~flags;
3049 lod_comp->llc_flags &= ~mirror_flag;
3052 if ((flags & LCME_FL_STALE) &&
3053 lod_last_non_stale_mirror(mirror_id,
3056 lod_comp->llc_flags |= flags;
3059 lod_comp->llc_flags |= mirror_flag;
3060 if (mirror_flag & LCME_FL_NOSYNC)
3061 lod_comp->llc_timestamp =
3062 ktime_get_real_seconds();
3070 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
3071 lod2obd(d)->obd_name);
3075 lod_obj_inc_layout_gen(lo);
3077 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
3078 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), &info->lti_buf,
3079 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3084 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
3085 * and the xattr value is a unique component ID or a special lcme_id.
3087 * \param[in] env execution environment
3088 * \param[in] dt dt_object to be operated on
3089 * \param[in] buf buffer contains component ID or lcme_id
3090 * \parem[in] th thandle
3092 * \retval 0 on success
3093 * \retval negative errno on failure
3095 static int lod_declare_layout_del(const struct lu_env *env,
3096 struct dt_object *dt,
3097 const struct lu_buf *buf,
3100 struct lod_thread_info *info = lod_env_info(env);
3101 struct dt_object *next = dt_object_child(dt);
3102 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3103 struct lod_object *lo = lod_dt_obj(dt);
3104 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3105 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
3106 __u32 magic, id, flags, neg_flags = 0;
3110 LASSERT(lo->ldo_is_composite);
3112 if (lo->ldo_flr_state != LCM_FL_NONE)
3115 magic = comp_v1->lcm_magic;
3116 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3117 lustre_swab_lov_comp_md_v1(comp_v1);
3118 magic = comp_v1->lcm_magic;
3121 if (magic != LOV_USER_MAGIC_COMP_V1)
3124 id = comp_v1->lcm_entries[0].lcme_id;
3125 flags = comp_v1->lcm_entries[0].lcme_flags;
3127 if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
3128 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
3129 lod2obd(d)->obd_name, id, flags);
3133 if (id != LCME_ID_INVAL && flags != 0) {
3134 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
3135 lod2obd(d)->obd_name);
3139 if (id == LCME_ID_INVAL && !flags) {
3140 CDEBUG(D_LAYOUT, "%s: no id or flags specified.\n",
3141 lod2obd(d)->obd_name);
3145 if (flags & LCME_FL_NEG) {
3146 neg_flags = flags & ~LCME_FL_NEG;
3150 left = lo->ldo_comp_cnt;
3154 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3155 struct lod_layout_component *lod_comp;
3157 lod_comp = &lo->ldo_comp_entries[i];
3159 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
3161 else if (flags && !(flags & lod_comp->llc_flags))
3163 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
3166 if (left != (i + 1)) {
3167 CDEBUG(D_LAYOUT, "%s: this deletion will create "
3168 "a hole.\n", lod2obd(d)->obd_name);
3173 /* Mark the component as deleted */
3174 lod_comp->llc_id = LCME_ID_INVAL;
3176 /* Not instantiated component */
3177 if (lod_comp->llc_stripe == NULL)
3180 LASSERT(lod_comp->llc_stripe_count > 0);
3181 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
3182 struct dt_object *obj = lod_comp->llc_stripe[j];
3186 rc = lod_sub_declare_destroy(env, obj, th);
3192 LASSERTF(left >= 0, "left = %d\n", left);
3193 if (left == lo->ldo_comp_cnt) {
3194 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
3195 lod2obd(d)->obd_name, id);
3199 memset(attr, 0, sizeof(*attr));
3200 attr->la_valid = LA_SIZE;
3201 rc = lod_sub_declare_attr_set(env, next, attr, th);
3206 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
3207 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
3208 XATTR_NAME_LOV, 0, th);
3210 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
3217 * Declare layout add/set/del operations issued by special xattr names:
3219 * XATTR_LUSTRE_LOV.add add component(s) to existing file
3220 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
3221 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
3223 * \param[in] env execution environment
3224 * \param[in] dt object
3225 * \param[in] name name of xattr
3226 * \param[in] buf lu_buf contains xattr value
3227 * \param[in] th transaction handle
3229 * \retval 0 on success
3230 * \retval negative if failed
3232 static int lod_declare_modify_layout(const struct lu_env *env,
3233 struct dt_object *dt,
3235 const struct lu_buf *buf,
3238 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3239 struct lod_object *lo = lod_dt_obj(dt);
3241 int rc, len = strlen(XATTR_LUSTRE_LOV);
3244 LASSERT(dt_object_exists(dt));
3246 if (strlen(name) <= len || name[len] != '.') {
3247 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
3248 lod2obd(d)->obd_name, name);
3253 rc = lod_striping_load(env, lo);
3257 /* the layout to be modified must be a composite layout */
3258 if (!lo->ldo_is_composite) {
3259 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
3260 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3261 GOTO(unlock, rc = -EINVAL);
3264 op = (char *)name + len;
3265 if (strcmp(op, "add") == 0) {
3266 rc = lod_declare_layout_add(env, dt, buf, th);
3267 } else if (strcmp(op, "del") == 0) {
3268 rc = lod_declare_layout_del(env, dt, buf, th);
3269 } else if (strncmp(op, "set", strlen("set")) == 0) {
3270 rc = lod_declare_layout_set(env, dt, op, buf, th);
3272 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
3273 lod2obd(d)->obd_name, name);
3274 GOTO(unlock, rc = -ENOTSUPP);
3278 lod_striping_free(env, lo);
3284 * Convert a plain file lov_mds_md to a composite layout.
3286 * \param[in,out] info the thread info::lti_ea_store buffer contains little
3287 * endian plain file layout
3289 * \retval 0 on success, <0 on failure
3291 static int lod_layout_convert(struct lod_thread_info *info)
3293 struct lov_mds_md *lmm = info->lti_ea_store;
3294 struct lov_mds_md *lmm_save;
3295 struct lov_comp_md_v1 *lcm;
3296 struct lov_comp_md_entry_v1 *lcme;
3302 /* realloc buffer to a composite layout which contains one component */
3303 blob_size = lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
3304 le32_to_cpu(lmm->lmm_magic));
3305 size = sizeof(*lcm) + sizeof(*lcme) + blob_size;
3307 OBD_ALLOC_LARGE(lmm_save, blob_size);
3309 GOTO(out, rc = -ENOMEM);
3311 memcpy(lmm_save, lmm, blob_size);
3313 if (info->lti_ea_store_size < size) {
3314 rc = lod_ea_store_resize(info, size);
3319 lcm = info->lti_ea_store;
3320 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
3321 lcm->lcm_size = cpu_to_le32(size);
3322 lcm->lcm_layout_gen = cpu_to_le32(le16_to_cpu(
3323 lmm_save->lmm_layout_gen));
3324 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
3325 lcm->lcm_entry_count = cpu_to_le16(1);
3326 lcm->lcm_mirror_count = 0;
3328 lcme = &lcm->lcm_entries[0];
3329 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
3330 lcme->lcme_extent.e_start = 0;
3331 lcme->lcme_extent.e_end = cpu_to_le64(OBD_OBJECT_EOF);
3332 lcme->lcme_offset = cpu_to_le32(sizeof(*lcm) + sizeof(*lcme));
3333 lcme->lcme_size = cpu_to_le32(blob_size);
3335 memcpy((char *)lcm + lcme->lcme_offset, (char *)lmm_save, blob_size);
3340 OBD_FREE_LARGE(lmm_save, blob_size);
3345 * Merge layouts to form a mirrored file.
3347 static int lod_declare_layout_merge(const struct lu_env *env,
3348 struct dt_object *dt, const struct lu_buf *mbuf,
3351 struct lod_thread_info *info = lod_env_info(env);
3352 struct lu_buf *buf = &info->lti_buf;
3353 struct lod_object *lo = lod_dt_obj(dt);
3354 struct lov_comp_md_v1 *lcm;
3355 struct lov_comp_md_v1 *cur_lcm;
3356 struct lov_comp_md_v1 *merge_lcm;
3357 struct lov_comp_md_entry_v1 *lcme;
3358 struct lov_mds_md_v1 *lmm;
3361 __u16 cur_entry_count;
3362 __u16 merge_entry_count;
3364 __u16 mirror_id = 0;
3371 merge_lcm = mbuf->lb_buf;
3372 if (mbuf->lb_len < sizeof(*merge_lcm))
3375 /* must be an existing layout from disk */
3376 if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
3379 merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
3381 /* do not allow to merge two mirrored files */
3382 if (le16_to_cpu(merge_lcm->lcm_mirror_count))
3385 /* verify the target buffer */
3386 rc = lod_get_lov_ea(env, lo);
3388 RETURN(rc ? : -ENODATA);
3390 cur_lcm = info->lti_ea_store;
3391 switch (le32_to_cpu(cur_lcm->lcm_magic)) {
3394 rc = lod_layout_convert(info);
3396 case LOV_MAGIC_COMP_V1:
3406 /* info->lti_ea_store could be reallocated in lod_layout_convert() */
3407 cur_lcm = info->lti_ea_store;
3408 cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
3410 /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
3411 mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
3412 if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
3415 /* size of new layout */
3416 size = le32_to_cpu(cur_lcm->lcm_size) +
3417 le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
3419 memset(buf, 0, sizeof(*buf));
3420 lu_buf_alloc(buf, size);
3421 if (buf->lb_buf == NULL)
3425 memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
3427 offset = sizeof(*lcm) +
3428 sizeof(*lcme) * (cur_entry_count + merge_entry_count);
3429 for (i = 0; i < cur_entry_count; i++) {
3430 struct lov_comp_md_entry_v1 *cur_lcme;
3432 lcme = &lcm->lcm_entries[i];
3433 cur_lcme = &cur_lcm->lcm_entries[i];
3435 lcme->lcme_offset = cpu_to_le32(offset);
3436 memcpy((char *)lcm + offset,
3437 (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
3438 le32_to_cpu(lcme->lcme_size));
3440 offset += le32_to_cpu(lcme->lcme_size);
3442 if (mirror_count == 1 &&
3443 mirror_id_of(le32_to_cpu(lcme->lcme_id)) == 0) {
3444 /* Add mirror from a non-flr file, create new mirror ID.
3445 * Otherwise, keep existing mirror's component ID, used
3446 * for mirror extension.
3448 id = pflr_id(1, i + 1);
3449 lcme->lcme_id = cpu_to_le32(id);
3452 id = MAX(le32_to_cpu(lcme->lcme_id), id);
3455 mirror_id = mirror_id_of(id) + 1;
3457 /* check if first entry in new layout is DOM */
3458 lmm = (struct lov_mds_md_v1 *)((char *)merge_lcm +
3459 merge_lcm->lcm_entries[0].lcme_offset);
3460 merge_has_dom = lov_pattern(le32_to_cpu(lmm->lmm_pattern)) ==
3463 for (i = 0; i < merge_entry_count; i++) {
3464 struct lov_comp_md_entry_v1 *merge_lcme;
3466 merge_lcme = &merge_lcm->lcm_entries[i];
3467 lcme = &lcm->lcm_entries[cur_entry_count + i];
3469 *lcme = *merge_lcme;
3470 lcme->lcme_offset = cpu_to_le32(offset);
3471 if (merge_has_dom && i == 0)
3472 lcme->lcme_flags |= cpu_to_le32(LCME_FL_STALE);
3474 id = pflr_id(mirror_id, i + 1);
3475 lcme->lcme_id = cpu_to_le32(id);
3477 memcpy((char *)lcm + offset,
3478 (char *)merge_lcm + le32_to_cpu(merge_lcme->lcme_offset),
3479 le32_to_cpu(lcme->lcme_size));
3481 offset += le32_to_cpu(lcme->lcme_size);
3484 /* fixup layout information */
3485 lod_obj_inc_layout_gen(lo);
3486 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3487 lcm->lcm_size = cpu_to_le32(size);
3488 lcm->lcm_entry_count = cpu_to_le16(cur_entry_count + merge_entry_count);
3489 lcm->lcm_mirror_count = cpu_to_le16(mirror_count);
3490 if ((le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK) == LCM_FL_NONE)
3491 lcm->lcm_flags = cpu_to_le32(LCM_FL_RDONLY);
3493 rc = lod_striping_reload(env, lo, buf);
3497 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), buf,
3498 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3506 * Split layouts, just set the LOVEA with the layout from mbuf.
3508 static int lod_declare_layout_split(const struct lu_env *env,
3509 struct dt_object *dt, const struct lu_buf *mbuf,
3512 struct lod_object *lo = lod_dt_obj(dt);
3513 struct lov_comp_md_v1 *lcm = mbuf->lb_buf;
3517 lod_obj_inc_layout_gen(lo);
3518 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3520 rc = lod_striping_reload(env, lo, mbuf);
3524 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), mbuf,
3525 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3530 * Implementation of dt_object_operations::do_declare_xattr_set.
3532 * \see dt_object_operations::do_declare_xattr_set() in the API description
3535 * the extension to the API:
3536 * - declaring LOVEA requests striping creation
3537 * - LU_XATTR_REPLACE means layout swap
3539 static int lod_declare_xattr_set(const struct lu_env *env,
3540 struct dt_object *dt,
3541 const struct lu_buf *buf,
3542 const char *name, int fl,
3545 struct dt_object *next = dt_object_child(dt);
3546 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3551 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
3552 if ((S_ISREG(mode) || mode == 0) &&
3553 !(fl & (LU_XATTR_REPLACE | LU_XATTR_MERGE | LU_XATTR_SPLIT)) &&
3554 (strcmp(name, XATTR_NAME_LOV) == 0 ||
3555 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
3557 * this is a request to create object's striping.
3559 * allow to declare predefined striping on a new (!mode) object
3560 * which is supposed to be replay of regular file creation
3561 * (when LOV setting is declared)
3563 * LU_XATTR_REPLACE is set to indicate a layout swap
3565 if (dt_object_exists(dt)) {
3566 rc = dt_attr_get(env, next, attr);
3570 memset(attr, 0, sizeof(*attr));
3571 attr->la_valid = LA_TYPE | LA_MODE;
3572 attr->la_mode = S_IFREG;
3574 rc = lod_declare_striped_create(env, dt, attr, buf, th);
3575 } else if (fl & LU_XATTR_MERGE) {
3576 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3577 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3578 rc = lod_declare_layout_merge(env, dt, buf, th);
3579 } else if (fl & LU_XATTR_SPLIT) {
3580 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3581 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3582 rc = lod_declare_layout_split(env, dt, buf, th);
3583 } else if (S_ISREG(mode) &&
3584 strlen(name) > strlen(XATTR_LUSTRE_LOV) + 1 &&
3585 strncmp(name, XATTR_LUSTRE_LOV,
3586 strlen(XATTR_LUSTRE_LOV)) == 0) {
3588 * this is a request to modify object's striping.
3589 * add/set/del component(s).
3591 if (!dt_object_exists(dt))
3594 rc = lod_declare_modify_layout(env, dt, name, buf, th);
3595 } else if (strncmp(name, XATTR_NAME_LMV, strlen(XATTR_NAME_LMV)) == 0 &&
3596 strlen(name) > strlen(XATTR_NAME_LMV) + 1) {
3597 const char *op = name + strlen(XATTR_NAME_LMV) + 1;
3600 if (strcmp(op, "add") == 0)
3601 rc = lod_dir_declare_layout_add(env, dt, buf, th);
3602 else if (strcmp(op, "del") == 0)
3603 rc = lod_dir_declare_layout_delete(env, dt, buf, th);
3604 else if (strcmp(op, "set") == 0)
3605 rc = lod_sub_declare_xattr_set(env, next, buf,
3606 XATTR_NAME_LMV, fl, th);
3609 } else if (S_ISDIR(mode)) {
3610 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
3611 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3612 rc = lod_replace_parent_fid(env, dt, buf, th, true);
3614 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
3621 * Apply xattr changes to the object.
3623 * Applies xattr changes to the object and the stripes if the latter exist.
3625 * \param[in] env execution environment
3626 * \param[in] dt object
3627 * \param[in] buf buffer pointing to the new value of xattr
3628 * \param[in] name name of xattr
3629 * \param[in] fl flags
3630 * \param[in] th transaction handle
3632 * \retval 0 on success
3633 * \retval negative if failed
3635 static int lod_xattr_set_internal(const struct lu_env *env,
3636 struct dt_object *dt,
3637 const struct lu_buf *buf,
3638 const char *name, int fl,
3641 struct dt_object *next = dt_object_child(dt);
3642 struct lod_object *lo = lod_dt_obj(dt);
3647 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3648 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3651 /* Note: Do not set LinkEA on sub-stripes, otherwise
3652 * it will confuse the fid2path process(see mdt_path_current()).
3653 * The linkEA between master and sub-stripes is set in
3654 * lod_xattr_set_lmv(). */
3655 if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
3658 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3659 if (!lo->ldo_stripe[i])
3662 if (!dt_object_exists(lo->ldo_stripe[i]))
3665 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
3675 * Delete an extended attribute.
3677 * Deletes specified xattr from the object and the stripes if the latter exist.
3679 * \param[in] env execution environment
3680 * \param[in] dt object
3681 * \param[in] name name of xattr
3682 * \param[in] th transaction handle
3684 * \retval 0 on success
3685 * \retval negative if failed
3687 static int lod_xattr_del_internal(const struct lu_env *env,
3688 struct dt_object *dt,
3689 const char *name, struct thandle *th)
3691 struct dt_object *next = dt_object_child(dt);
3692 struct lod_object *lo = lod_dt_obj(dt);
3697 rc = lod_sub_xattr_del(env, next, name, th);
3698 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3701 if (lo->ldo_dir_stripe_count == 0)
3704 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3705 LASSERT(lo->ldo_stripe[i]);
3707 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3716 * Set default striping on a directory.
3718 * Sets specified striping on a directory object unless it matches the default
3719 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3720 * EA. This striping will be used when regular file is being created in this
3723 * \param[in] env execution environment
3724 * \param[in] dt the striped object
3725 * \param[in] buf buffer with the striping
3726 * \param[in] name name of EA
3727 * \param[in] fl xattr flag (see OSD API description)
3728 * \param[in] th transaction handle
3730 * \retval 0 on success
3731 * \retval negative if failed
3733 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
3734 struct dt_object *dt,
3735 const struct lu_buf *buf,
3736 const char *name, int fl,
3739 struct lov_user_md_v1 *lum;
3740 struct lov_user_md_v3 *v3 = NULL;
3741 const char *pool_name = NULL;
3746 LASSERT(buf != NULL && buf->lb_buf != NULL);
3749 switch (lum->lmm_magic) {
3750 case LOV_USER_MAGIC_SPECIFIC:
3751 case LOV_USER_MAGIC_V3:
3753 if (v3->lmm_pool_name[0] != '\0')
3754 pool_name = v3->lmm_pool_name;
3756 case LOV_USER_MAGIC_V1:
3757 /* if { size, offset, count } = { 0, -1, 0 } and no pool
3758 * (i.e. all default values specified) then delete default
3759 * striping from dir. */
3761 "set default striping: sz %u # %u offset %d %s %s\n",
3762 (unsigned)lum->lmm_stripe_size,
3763 (unsigned)lum->lmm_stripe_count,
3764 (int)lum->lmm_stripe_offset,
3765 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
3767 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
3768 lum->lmm_stripe_count,
3769 lum->lmm_stripe_offset,
3772 case LOV_USER_MAGIC_COMP_V1:
3774 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lum;
3775 struct lov_comp_md_entry_v1 *lcme;
3778 comp_cnt = le16_to_cpu(lcm->lcm_entry_count);
3779 for (i = 0; i < comp_cnt; i++) {
3780 lcme = &lcm->lcm_entries[i];
3781 if (lcme->lcme_flags & cpu_to_le32(LCME_FL_EXTENSION)) {
3782 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_SEL);
3791 CERROR("Invalid magic %x\n", lum->lmm_magic);
3796 rc = lod_xattr_del_internal(env, dt, name, th);
3800 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3807 * Set default striping on a directory object.
3809 * Sets specified striping on a directory object unless it matches the default
3810 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3811 * EA. This striping will be used when a new directory is being created in the
3814 * \param[in] env execution environment
3815 * \param[in] dt the striped object
3816 * \param[in] buf buffer with the striping
3817 * \param[in] name name of EA
3818 * \param[in] fl xattr flag (see OSD API description)
3819 * \param[in] th transaction handle
3821 * \retval 0 on success
3822 * \retval negative if failed
3824 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
3825 struct dt_object *dt,
3826 const struct lu_buf *buf,
3827 const char *name, int fl,
3830 struct lmv_user_md_v1 *lum;
3835 LASSERT(buf != NULL && buf->lb_buf != NULL);
3839 "set default stripe_count # %u stripe_offset %d hash %u\n",
3840 le32_to_cpu(lum->lum_stripe_count),
3841 (int)le32_to_cpu(lum->lum_stripe_offset),
3842 le32_to_cpu(lum->lum_hash_type));
3844 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
3845 le32_to_cpu(lum->lum_stripe_offset)) &&
3846 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
3847 rc = lod_xattr_del_internal(env, dt, name, th);
3851 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3860 * Turn directory into a striped directory.
3862 * During replay the client sends the striping created before MDT
3863 * failure, then the layer above LOD sends this defined striping
3864 * using ->do_xattr_set(), so LOD uses this method to replay creation
3865 * of the stripes. Notice the original information for the striping
3866 * (#stripes, FIDs, etc) was transferred in declare path.
3868 * \param[in] env execution environment
3869 * \param[in] dt the striped object
3870 * \param[in] buf not used currently
3871 * \param[in] name not used currently
3872 * \param[in] fl xattr flag (see OSD API description)
3873 * \param[in] th transaction handle
3875 * \retval 0 on success
3876 * \retval negative if failed
3878 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
3879 const struct lu_buf *buf, const char *name,
3880 int fl, struct thandle *th)
3882 struct lod_object *lo = lod_dt_obj(dt);
3883 struct lod_thread_info *info = lod_env_info(env);
3884 struct lu_attr *attr = &info->lti_attr;
3885 struct dt_object_format *dof = &info->lti_format;
3886 struct lu_buf lmv_buf;
3887 struct lu_buf slave_lmv_buf;
3888 struct lmv_mds_md_v1 *lmm;
3889 struct lmv_mds_md_v1 *slave_lmm = NULL;
3890 struct dt_insert_rec *rec = &info->lti_dt_rec;
3895 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3898 /* The stripes are supposed to be allocated in declare phase,
3899 * if there are no stripes being allocated, it will skip */
3900 if (lo->ldo_dir_stripe_count == 0) {
3901 if (lo->ldo_dir_is_foreign) {
3902 rc = lod_sub_xattr_set(env, dt_object_child(dt), buf,
3903 XATTR_NAME_LMV, fl, th);
3910 rc = dt_attr_get(env, dt_object_child(dt), attr);
3914 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
3915 LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
3916 dof->dof_type = DFT_DIR;
3918 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
3921 lmm = lmv_buf.lb_buf;
3923 OBD_ALLOC_PTR(slave_lmm);
3924 if (slave_lmm == NULL)
3927 lod_prep_slave_lmv_md(slave_lmm, lmm);
3928 slave_lmv_buf.lb_buf = slave_lmm;
3929 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
3931 rec->rec_type = S_IFDIR;
3932 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3933 struct dt_object *dto = lo->ldo_stripe[i];
3934 char *stripe_name = info->lti_key;
3935 struct lu_name *sname;
3936 struct linkea_data ldata = { NULL };
3937 struct lu_buf linkea_buf;
3939 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
3943 /* fail a remote stripe creation */
3944 if (i && OBD_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_CREATE))
3947 /* if it's source stripe of migrating directory, don't create */
3948 if (!((lo->ldo_dir_hash_type & LMV_HASH_FLAG_MIGRATION) &&
3949 i >= lo->ldo_dir_migrate_offset)) {
3950 dt_write_lock(env, dto, DT_TGT_CHILD);
3951 rc = lod_sub_create(env, dto, attr, NULL, dof, th);
3953 dt_write_unlock(env, dto);
3957 rc = lod_sub_ref_add(env, dto, th);
3958 dt_write_unlock(env, dto);
3962 rec->rec_fid = lu_object_fid(&dto->do_lu);
3963 rc = lod_sub_insert(env, dto,
3964 (const struct dt_rec *)rec,
3965 (const struct dt_key *)dot, th);
3970 rec->rec_fid = lu_object_fid(&dt->do_lu);
3971 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
3972 (const struct dt_key *)dotdot, th);
3976 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
3977 cfs_fail_val != i) {
3978 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
3980 slave_lmm->lmv_master_mdt_index =
3983 slave_lmm->lmv_master_mdt_index =
3986 rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
3987 XATTR_NAME_LMV, fl, th);
3992 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
3994 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3995 PFID(lu_object_fid(&dto->do_lu)), i + 1);
3997 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3998 PFID(lu_object_fid(&dto->do_lu)), i);
4000 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
4001 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
4002 sname, lu_object_fid(&dt->do_lu));
4006 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
4007 linkea_buf.lb_len = ldata.ld_leh->leh_len;
4008 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
4009 XATTR_NAME_LINK, 0, th);
4013 rec->rec_fid = lu_object_fid(&dto->do_lu);
4014 rc = lod_sub_insert(env, dt_object_child(dt),
4015 (const struct dt_rec *)rec,
4016 (const struct dt_key *)stripe_name, th);
4020 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
4025 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
4026 rc = lod_sub_xattr_set(env, dt_object_child(dt),
4027 &lmv_buf, XATTR_NAME_LMV, fl, th);
4029 if (slave_lmm != NULL)
4030 OBD_FREE_PTR(slave_lmm);
4036 * Helper function to declare/execute creation of a striped directory
4038 * Called in declare/create object path, prepare striping for a directory
4039 * and prepare defaults data striping for the objects to be created in
4040 * that directory. Notice the function calls "declaration" or "execution"
4041 * methods depending on \a declare param. This is a consequence of the
4042 * current approach while we don't have natural distributed transactions:
4043 * we basically execute non-local updates in the declare phase. So, the
4044 * arguments for the both phases are the same and this is the reason for
4045 * this function to exist.
4047 * \param[in] env execution environment
4048 * \param[in] dt object
4049 * \param[in] attr attributes the stripes will be created with
4050 * \param[in] lmu lmv_user_md if MDT indices are specified
4051 * \param[in] dof format of stripes (see OSD API description)
4052 * \param[in] th transaction handle
4053 * \param[in] declare where to call "declare" or "execute" methods
4055 * \retval 0 on success
4056 * \retval negative if failed
4058 static int lod_dir_striping_create_internal(const struct lu_env *env,
4059 struct dt_object *dt,
4060 struct lu_attr *attr,
4061 const struct lu_buf *lmu,
4062 struct dt_object_format *dof,
4066 struct lod_thread_info *info = lod_env_info(env);
4067 struct lod_object *lo = lod_dt_obj(dt);
4068 const struct lod_default_striping *lds = lo->ldo_def_striping;
4072 LASSERT(ergo(lds != NULL,
4073 lds->lds_def_striping_set ||
4074 lds->lds_dir_def_striping_set));
4076 if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
4077 lo->ldo_dir_stripe_offset)) {
4079 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
4080 int stripe_count = lo->ldo_dir_stripe_count;
4082 if (info->lti_ea_store_size < sizeof(*v1)) {
4083 rc = lod_ea_store_resize(info, sizeof(*v1));
4086 v1 = info->lti_ea_store;
4089 memset(v1, 0, sizeof(*v1));
4090 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
4091 v1->lum_stripe_count = cpu_to_le32(stripe_count);
4092 v1->lum_stripe_offset =
4093 cpu_to_le32(lo->ldo_dir_stripe_offset);
4095 info->lti_buf.lb_buf = v1;
4096 info->lti_buf.lb_len = sizeof(*v1);
4097 lmu = &info->lti_buf;
4101 rc = lod_declare_xattr_set_lmv(env, dt, attr, lmu, dof,
4104 rc = lod_xattr_set_lmv(env, dt, lmu, XATTR_NAME_LMV, 0,
4109 /* foreign LMV EA case */
4111 struct lmv_foreign_md *lfm = lmu->lb_buf;
4113 if (lfm->lfm_magic == LMV_MAGIC_FOREIGN) {
4114 rc = lod_declare_xattr_set_lmv(env, dt, attr,
4118 if (lo->ldo_dir_is_foreign) {
4119 LASSERT(lo->ldo_foreign_lmv != NULL &&
4120 lo->ldo_foreign_lmv_size > 0);
4121 info->lti_buf.lb_buf = lo->ldo_foreign_lmv;
4122 info->lti_buf.lb_len = lo->ldo_foreign_lmv_size;
4123 lmu = &info->lti_buf;
4124 rc = lod_xattr_set_lmv(env, dt, lmu,
4125 XATTR_NAME_LMV, 0, th);
4130 /* Transfer default LMV striping from the parent */
4131 if (lds != NULL && lds->lds_dir_def_striping_set &&
4132 !(LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
4133 lds->lds_dir_def_stripe_offset) &&
4134 le32_to_cpu(lds->lds_dir_def_hash_type) !=
4135 LMV_HASH_TYPE_UNKNOWN)) {
4136 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
4138 if (info->lti_ea_store_size < sizeof(*v1)) {
4139 rc = lod_ea_store_resize(info, sizeof(*v1));
4142 v1 = info->lti_ea_store;
4145 memset(v1, 0, sizeof(*v1));
4146 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
4147 v1->lum_stripe_count =
4148 cpu_to_le32(lds->lds_dir_def_stripe_count);
4149 v1->lum_stripe_offset =
4150 cpu_to_le32(lds->lds_dir_def_stripe_offset);
4152 cpu_to_le32(lds->lds_dir_def_hash_type);
4154 info->lti_buf.lb_buf = v1;
4155 info->lti_buf.lb_len = sizeof(*v1);
4157 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4158 XATTR_NAME_DEFAULT_LMV,
4161 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
4163 XATTR_NAME_DEFAULT_LMV, 0,
4169 /* Transfer default LOV striping from the parent */
4170 if (lds != NULL && lds->lds_def_striping_set &&
4171 lds->lds_def_comp_cnt != 0) {
4172 struct lov_mds_md *lmm;
4173 int lmm_size = lod_comp_md_size(lo, true);
4175 if (info->lti_ea_store_size < lmm_size) {
4176 rc = lod_ea_store_resize(info, lmm_size);
4180 lmm = info->lti_ea_store;
4182 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
4186 info->lti_buf.lb_buf = lmm;
4187 info->lti_buf.lb_len = lmm_size;
4190 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4191 XATTR_NAME_LOV, 0, th);
4193 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4194 XATTR_NAME_LOV, 0, th);
4202 static int lod_declare_dir_striping_create(const struct lu_env *env,
4203 struct dt_object *dt,
4204 struct lu_attr *attr,
4206 struct dt_object_format *dof,
4209 return lod_dir_striping_create_internal(env, dt, attr, lmu, dof, th,
4213 static int lod_dir_striping_create(const struct lu_env *env,
4214 struct dt_object *dt,
4215 struct lu_attr *attr,
4216 struct dt_object_format *dof,
4219 return lod_dir_striping_create_internal(env, dt, attr, NULL, dof, th,
4224 * Make LOV EA for striped object.
4226 * Generate striping information and store it in the LOV EA of the given
4227 * object. The caller must ensure nobody else is calling the function
4228 * against the object concurrently. The transaction must be started.
4229 * FLDB service must be running as well; it's used to map FID to the target,
4230 * which is stored in LOV EA.
4232 * \param[in] env execution environment for this thread
4233 * \param[in] lo LOD object
4234 * \param[in] th transaction handle
4236 * \retval 0 if LOV EA is stored successfully
4237 * \retval negative error number on failure
4239 static int lod_generate_and_set_lovea(const struct lu_env *env,
4240 struct lod_object *lo,
4243 struct lod_thread_info *info = lod_env_info(env);
4244 struct dt_object *next = dt_object_child(&lo->ldo_obj);
4245 struct lov_mds_md_v1 *lmm;
4251 if (lo->ldo_comp_cnt == 0 && !lo->ldo_is_foreign) {
4252 lod_striping_free(env, lo);
4253 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
4257 lmm_size = lod_comp_md_size(lo, false);
4258 if (info->lti_ea_store_size < lmm_size) {
4259 rc = lod_ea_store_resize(info, lmm_size);
4263 lmm = info->lti_ea_store;
4265 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
4269 info->lti_buf.lb_buf = lmm;
4270 info->lti_buf.lb_len = lmm_size;
4271 rc = lod_sub_xattr_set(env, next, &info->lti_buf,
4272 XATTR_NAME_LOV, 0, th);
4276 static __u32 lod_gen_component_id(struct lod_object *lo,
4277 int mirror_id, int comp_idx);
4280 * Repeat an existing component
4282 * Creates a new layout by replicating an existing component. Uses striping
4283 * policy from previous component as a template for the striping for the new
4286 * New component starts with zero length, will be extended (or removed) before
4287 * returning layout to client.
4289 * NB: Reallocates layout components array (lo->ldo_comp_entries), invalidating
4290 * any pre-existing pointers to components. Handle with care.
4292 * \param[in] env execution environment for this thread
4293 * \param[in,out] lo object to update the layout of
4294 * \param[in] index index of component to copy
4296 * \retval 0 on success
4297 * \retval negative errno on error
4299 static int lod_layout_repeat_comp(const struct lu_env *env,
4300 struct lod_object *lo, int index)
4302 struct lod_layout_component *lod_comp;
4303 struct lod_layout_component *new_comp = NULL;
4304 struct lod_layout_component *comp_array;
4305 int rc = 0, i, new_cnt = lo->ldo_comp_cnt + 1;
4310 lod_comp = &lo->ldo_comp_entries[index];
4311 LASSERT(lod_comp_inited(lod_comp) && lod_comp->llc_id != LCME_ID_INVAL);
4313 CDEBUG(D_LAYOUT, "repeating component %d\n", index);
4315 OBD_ALLOC(comp_array, sizeof(*comp_array) * new_cnt);
4316 if (comp_array == NULL)
4317 GOTO(out, rc = -ENOMEM);
4319 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4320 memcpy(&comp_array[i + offset], &lo->ldo_comp_entries[i],
4321 sizeof(*comp_array));
4323 /* Duplicate this component in to the next slot */
4325 new_comp = &comp_array[i + 1];
4326 memcpy(&comp_array[i + 1], &lo->ldo_comp_entries[i],
4327 sizeof(*comp_array));
4328 /* We must now skip this new component when copying */
4333 /* Set up copied component */
4334 new_comp->llc_flags &= ~LCME_FL_INIT;
4335 new_comp->llc_stripe = NULL;
4336 new_comp->llc_stripes_allocated = 0;
4337 new_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4338 /* for uninstantiated components, layout gen stores default stripe
4340 new_comp->llc_layout_gen = lod_comp->llc_stripe_offset;
4341 /* This makes the repeated component zero-length, placed at the end of
4342 * the preceding component */
4343 new_comp->llc_extent.e_start = new_comp->llc_extent.e_end;
4344 new_comp->llc_timestamp = lod_comp->llc_timestamp;
4345 new_comp->llc_pool = NULL;
4347 rc = lod_set_pool(&new_comp->llc_pool, lod_comp->llc_pool);
4351 if (new_comp->llc_ostlist.op_array) {
4352 __u32 *op_array = NULL;
4354 OBD_ALLOC(op_array, new_comp->llc_ostlist.op_size);
4356 GOTO(out, rc = -ENOMEM);
4357 memcpy(op_array, &new_comp->llc_ostlist.op_array,
4358 new_comp->llc_ostlist.op_size);
4359 new_comp->llc_ostlist.op_array = op_array;
4362 OBD_FREE(lo->ldo_comp_entries,
4363 sizeof(*comp_array) * lo->ldo_comp_cnt);
4364 lo->ldo_comp_entries = comp_array;
4365 lo->ldo_comp_cnt = new_cnt;
4367 /* Generate an id for the new component */
4368 mirror_id = mirror_id_of(new_comp->llc_id);
4369 new_comp->llc_id = LCME_ID_INVAL;
4370 new_comp->llc_id = lod_gen_component_id(lo, mirror_id, index + 1);
4371 if (new_comp->llc_id == LCME_ID_INVAL)
4372 GOTO(out, rc = -ERANGE);
4377 OBD_FREE(comp_array, sizeof(*comp_array) * new_cnt);
4382 static int lod_layout_data_init(struct lod_thread_info *info, __u32 comp_cnt)
4386 /* clear memory region that will be used for layout change */
4387 memset(&info->lti_layout_attr, 0, sizeof(struct lu_attr));
4388 info->lti_count = 0;
4390 if (info->lti_comp_size >= comp_cnt)
4393 if (info->lti_comp_size > 0) {
4394 OBD_FREE(info->lti_comp_idx,
4395 info->lti_comp_size * sizeof(__u32));
4396 info->lti_comp_size = 0;
4399 OBD_ALLOC(info->lti_comp_idx, comp_cnt * sizeof(__u32));
4400 if (!info->lti_comp_idx)
4403 info->lti_comp_size = comp_cnt;
4408 * Prepare new layout minus deleted components
4410 * Removes components marked for deletion (LCME_ID_INVAL) by copying to a new
4411 * layout and skipping those components. Removes stripe objects if any exist.
4414 * Reallocates layout components array (lo->ldo_comp_entries), invalidating
4415 * any pre-existing pointers to components.
4417 * Caller is responsible for updating mirror end (ldo_mirror[].lme_end).
4419 * \param[in] env execution environment for this thread
4420 * \param[in,out] lo object to update the layout of
4421 * \param[in] th transaction handle for this operation
4423 * \retval # of components deleted
4424 * \retval negative errno on error
4426 static int lod_layout_del_prep_layout(const struct lu_env *env,
4427 struct lod_object *lo,
4430 struct lod_layout_component *lod_comp;
4431 struct lod_thread_info *info = lod_env_info(env);
4432 int rc = 0, i, j, deleted = 0;
4436 LASSERT(lo->ldo_is_composite);
4437 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
4439 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
4443 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4444 lod_comp = &lo->ldo_comp_entries[i];
4446 if (lod_comp->llc_id != LCME_ID_INVAL) {
4447 /* Build array of things to keep */
4448 info->lti_comp_idx[info->lti_count++] = i;
4452 lod_obj_set_pool(lo, i, NULL);
4453 if (lod_comp->llc_ostlist.op_array) {
4454 OBD_FREE(lod_comp->llc_ostlist.op_array,
4455 lod_comp->llc_ostlist.op_size);
4456 lod_comp->llc_ostlist.op_array = NULL;
4457 lod_comp->llc_ostlist.op_size = 0;
4461 CDEBUG(D_LAYOUT, "deleting comp %d, left %d\n", i,
4462 lo->ldo_comp_cnt - deleted);
4464 /* No striping info for this component */
4465 if (lod_comp->llc_stripe == NULL)
4468 LASSERT(lod_comp->llc_stripe_count > 0);
4469 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4470 struct dt_object *obj = lod_comp->llc_stripe[j];
4475 /* components which are not init have no sub objects
4477 if (lod_comp_inited(lod_comp)) {
4478 rc = lod_sub_destroy(env, obj, th);
4483 lu_object_put(env, &obj->do_lu);
4484 lod_comp->llc_stripe[j] = NULL;
4486 OBD_FREE(lod_comp->llc_stripe, sizeof(struct dt_object *) *
4487 lod_comp->llc_stripes_allocated);
4488 lod_comp->llc_stripe = NULL;
4489 OBD_FREE(lod_comp->llc_ost_indices,
4490 sizeof(__u32) * lod_comp->llc_stripes_allocated);
4491 lod_comp->llc_ost_indices = NULL;
4492 lod_comp->llc_stripes_allocated = 0;
4495 /* info->lti_count has the amount of left components */
4496 LASSERTF(info->lti_count >= 0 && info->lti_count < lo->ldo_comp_cnt,
4497 "left = %d, lo->ldo_comp_cnt %d\n", (int)info->lti_count,
4498 (int)lo->ldo_comp_cnt);
4500 if (info->lti_count > 0) {
4501 struct lod_layout_component *comp_array;
4503 OBD_ALLOC(comp_array, sizeof(*comp_array) * info->lti_count);
4504 if (comp_array == NULL)
4505 GOTO(out, rc = -ENOMEM);
4507 for (i = 0; i < info->lti_count; i++) {
4508 memcpy(&comp_array[i],
4509 &lo->ldo_comp_entries[info->lti_comp_idx[i]],
4510 sizeof(*comp_array));
4513 OBD_FREE(lo->ldo_comp_entries,
4514 sizeof(*comp_array) * lo->ldo_comp_cnt);
4515 lo->ldo_comp_entries = comp_array;
4516 lo->ldo_comp_cnt = info->lti_count;
4518 lod_free_comp_entries(lo);
4523 return rc ? rc : deleted;
4527 * Delete layout component(s)
4529 * This function sets up the layout data in the env and does the setattrs
4530 * required to write out the new layout. The layout itself is modified in
4531 * lod_layout_del_prep_layout.
4533 * \param[in] env execution environment for this thread
4534 * \param[in] dt object
4535 * \param[in] th transaction handle
4537 * \retval 0 on success
4538 * \retval negative error number on failure
4540 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
4543 struct lod_object *lo = lod_dt_obj(dt);
4544 struct dt_object *next = dt_object_child(dt);
4545 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4548 LASSERT(lo->ldo_mirror_count == 1);
4550 rc = lod_layout_del_prep_layout(env, lo, th);
4554 /* Only do this if we didn't delete all components */
4555 if (lo->ldo_comp_cnt > 0) {
4556 lo->ldo_mirrors[0].lme_end = lo->ldo_comp_cnt - 1;
4557 lod_obj_inc_layout_gen(lo);
4560 LASSERT(dt_object_exists(dt));
4561 rc = dt_attr_get(env, next, attr);
4565 if (attr->la_size > 0) {
4567 attr->la_valid = LA_SIZE;
4568 rc = lod_sub_attr_set(env, next, attr, th);
4573 rc = lod_generate_and_set_lovea(env, lo, th);
4577 lod_striping_free(env, lo);
4582 static int lod_get_default_lov_striping(const struct lu_env *env,
4583 struct lod_object *lo,
4584 struct lod_default_striping *lds,
4585 struct dt_allocation_hint *ah);
4587 * Implementation of dt_object_operations::do_xattr_set.
4589 * Sets specified extended attribute on the object. Three types of EAs are
4591 * LOV EA - stores striping for a regular file or default striping (when set
4593 * LMV EA - stores a marker for the striped directories
4594 * DMV EA - stores default directory striping
4596 * When striping is applied to a non-striped existing object (this is called
4597 * late striping), then LOD notices the caller wants to turn the object into a
4598 * striped one. The stripe objects are created and appropriate EA is set:
4599 * LOV EA storing all the stripes directly or LMV EA storing just a small header
4600 * with striping configuration.
4602 * \see dt_object_operations::do_xattr_set() in the API description for details.
4604 static int lod_xattr_set(const struct lu_env *env,
4605 struct dt_object *dt, const struct lu_buf *buf,
4606 const char *name, int fl, struct thandle *th)
4608 struct dt_object *next = dt_object_child(dt);
4612 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4613 strcmp(name, XATTR_NAME_LMV) == 0) {
4614 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
4616 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4617 strncmp(name, XATTR_NAME_LMV, strlen(XATTR_NAME_LMV)) == 0 &&
4618 strlen(name) > strlen(XATTR_NAME_LMV) + 1) {
4619 const char *op = name + strlen(XATTR_NAME_LMV) + 1;
4623 * XATTR_NAME_LMV".add" is never called, but only declared,
4624 * because lod_xattr_set_lmv() will do the addition.
4626 if (strcmp(op, "del") == 0)
4627 rc = lod_dir_layout_delete(env, dt, buf, th);
4628 else if (strcmp(op, "set") == 0)
4629 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LMV,
4633 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4634 strcmp(name, XATTR_NAME_LOV) == 0) {
4635 struct lod_default_striping *lds = lod_lds_buf_get(env);
4636 struct lov_user_md_v1 *v1 = buf->lb_buf;
4637 char pool[LOV_MAXPOOLNAME + 1];
4640 /* get existing striping config */
4641 rc = lod_get_default_lov_striping(env, lod_dt_obj(dt), lds,
4646 memset(pool, 0, sizeof(pool));
4647 if (lds->lds_def_striping_set == 1)
4648 lod_layout_get_pool(lds->lds_def_comp_entries,
4649 lds->lds_def_comp_cnt, pool,
4652 is_del = LOVEA_DELETE_VALUES(v1->lmm_stripe_size,
4653 v1->lmm_stripe_count,
4654 v1->lmm_stripe_offset,
4657 /* Retain the pool name if it is not given */
4658 if (v1->lmm_magic == LOV_USER_MAGIC_V1 && pool[0] != '\0' &&
4660 struct lod_thread_info *info = lod_env_info(env);
4661 struct lov_user_md_v3 *v3 = info->lti_ea_store;
4663 memset(v3, 0, sizeof(*v3));
4664 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
4665 v3->lmm_pattern = cpu_to_le32(v1->lmm_pattern);
4666 v3->lmm_stripe_count =
4667 cpu_to_le32(v1->lmm_stripe_count);
4668 v3->lmm_stripe_offset =
4669 cpu_to_le32(v1->lmm_stripe_offset);
4670 v3->lmm_stripe_size = cpu_to_le32(v1->lmm_stripe_size);
4672 strlcpy(v3->lmm_pool_name, pool,
4673 sizeof(v3->lmm_pool_name));
4675 info->lti_buf.lb_buf = v3;
4676 info->lti_buf.lb_len = sizeof(*v3);
4677 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4680 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name,
4684 if (lds->lds_def_striping_set == 1 &&
4685 lds->lds_def_comp_entries != NULL)
4686 lod_free_def_comp_entries(lds);
4689 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4690 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
4692 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
4695 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4696 (!strcmp(name, XATTR_NAME_LOV) ||
4697 !strncmp(name, XATTR_LUSTRE_LOV,
4698 strlen(XATTR_LUSTRE_LOV)))) {
4699 /* in case of lov EA swap, just set it
4700 * if not, it is a replay so check striping match what we
4701 * already have during req replay, declare_xattr_set()
4702 * defines striping, then create() does the work */
4703 if (fl & LU_XATTR_REPLACE) {
4704 /* free stripes, then update disk */
4705 lod_striping_free(env, lod_dt_obj(dt));
4707 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
4708 } else if (dt_object_remote(dt)) {
4709 /* This only happens during migration, see
4710 * mdd_migrate_create(), in which Master MDT will
4711 * create a remote target object, and only set
4712 * (migrating) stripe EA on the remote object,
4713 * and does not need creating each stripes. */
4714 rc = lod_sub_xattr_set(env, next, buf, name,
4716 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
4717 /* delete component(s) */
4718 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
4719 rc = lod_layout_del(env, dt, th);
4722 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
4723 * it's going to create create file with specified
4724 * component(s), the striping must have not being
4725 * cached in this case;
4727 * Otherwise, it's going to add/change component(s) to
4728 * an existing file, the striping must have been cached
4731 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
4732 !strcmp(name, XATTR_NAME_LOV),
4733 !lod_dt_obj(dt)->ldo_comp_cached));
4735 rc = lod_striped_create(env, dt, NULL, NULL, th);
4738 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
4739 rc = lod_replace_parent_fid(env, dt, buf, th, false);
4744 /* then all other xattr */
4745 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
4751 * Implementation of dt_object_operations::do_declare_xattr_del.
4753 * \see dt_object_operations::do_declare_xattr_del() in the API description
4756 static int lod_declare_xattr_del(const struct lu_env *env,
4757 struct dt_object *dt, const char *name,
4760 struct lod_object *lo = lod_dt_obj(dt);
4761 struct dt_object *next = dt_object_child(dt);
4766 rc = lod_sub_declare_xattr_del(env, next, name, th);
4770 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4773 /* NB: don't delete stripe LMV, because when we do this, normally we
4774 * will remove stripes, besides, if directory LMV is corrupt, this will
4775 * prevent deleting its LMV and fixing it (via LFSCK).
4777 if (!strcmp(name, XATTR_NAME_LMV))
4780 rc = lod_striping_load(env, lo);
4784 if (lo->ldo_dir_stripe_count == 0)
4787 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4788 struct dt_object *dto = lo->ldo_stripe[i];
4793 rc = lod_sub_declare_xattr_del(env, dto, name, th);
4802 * Implementation of dt_object_operations::do_xattr_del.
4804 * If EA storing a regular striping is being deleted, then release
4805 * all the references to the stripe objects in core.
4807 * \see dt_object_operations::do_xattr_del() in the API description for details.
4809 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
4810 const char *name, struct thandle *th)
4812 struct dt_object *next = dt_object_child(dt);
4813 struct lod_object *lo = lod_dt_obj(dt);
4818 if (!strcmp(name, XATTR_NAME_LOV) || !strcmp(name, XATTR_NAME_LMV))
4819 lod_striping_free(env, lod_dt_obj(dt));
4821 rc = lod_sub_xattr_del(env, next, name, th);
4822 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
4825 if (!strcmp(name, XATTR_NAME_LMV))
4828 if (lo->ldo_dir_stripe_count == 0)
4831 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4832 struct dt_object *dto = lo->ldo_stripe[i];
4837 rc = lod_sub_xattr_del(env, dto, name, th);
4846 * Implementation of dt_object_operations::do_xattr_list.
4848 * \see dt_object_operations::do_xattr_list() in the API description
4851 static int lod_xattr_list(const struct lu_env *env,
4852 struct dt_object *dt, const struct lu_buf *buf)
4854 return dt_xattr_list(env, dt_object_child(dt), buf);
4857 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
4859 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
4863 * Copy OST list from layout provided by user.
4865 * \param[in] lod_comp layout_component to be filled
4866 * \param[in] v3 LOV EA V3 user data
4868 * \retval 0 on success
4869 * \retval negative if failed
4871 int lod_comp_copy_ost_lists(struct lod_layout_component *lod_comp,
4872 struct lov_user_md_v3 *v3)
4878 if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
4879 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
4881 if (lod_comp->llc_ostlist.op_array) {
4882 if (lod_comp->llc_ostlist.op_size >=
4883 v3->lmm_stripe_count * sizeof(__u32)) {
4884 lod_comp->llc_ostlist.op_count =
4885 v3->lmm_stripe_count;
4888 OBD_FREE(lod_comp->llc_ostlist.op_array,
4889 lod_comp->llc_ostlist.op_size);
4892 /* copy ost list from lmm */
4893 lod_comp->llc_ostlist.op_count = v3->lmm_stripe_count;
4894 lod_comp->llc_ostlist.op_size = v3->lmm_stripe_count * sizeof(__u32);
4895 OBD_ALLOC(lod_comp->llc_ostlist.op_array,
4896 lod_comp->llc_ostlist.op_size);
4897 if (!lod_comp->llc_ostlist.op_array)
4900 for (j = 0; j < v3->lmm_stripe_count; j++) {
4901 lod_comp->llc_ostlist.op_array[j] =
4902 v3->lmm_objects[j].l_ost_idx;
4910 * Get default striping.
4912 * \param[in] env execution environment
4913 * \param[in] lo object
4914 * \param[out] lds default striping
4916 * \retval 0 on success
4917 * \retval negative if failed
4919 static int lod_get_default_lov_striping(const struct lu_env *env,
4920 struct lod_object *lo,
4921 struct lod_default_striping *lds,
4922 struct dt_allocation_hint *ah)
4924 struct lod_thread_info *info = lod_env_info(env);
4925 struct lov_user_md_v1 *v1 = NULL;
4926 struct lov_user_md_v3 *v3 = NULL;
4927 struct lov_comp_md_v1 *comp_v1 = NULL;
4935 rc = lod_get_lov_ea(env, lo);
4939 if (rc < (typeof(rc))sizeof(struct lov_user_md))
4942 v1 = info->lti_ea_store;
4943 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
4944 lustre_swab_lov_user_md_v1(v1);
4945 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
4946 v3 = (struct lov_user_md_v3 *)v1;
4947 lustre_swab_lov_user_md_v3(v3);
4948 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_SPECIFIC)) {
4949 v3 = (struct lov_user_md_v3 *)v1;
4950 lustre_swab_lov_user_md_v3(v3);
4951 lustre_swab_lov_user_md_objects(v3->lmm_objects,
4952 v3->lmm_stripe_count);
4953 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1) ||
4954 v1->lmm_magic == __swab32(LOV_USER_MAGIC_SEL)) {
4955 comp_v1 = (struct lov_comp_md_v1 *)v1;
4956 lustre_swab_lov_comp_md_v1(comp_v1);
4959 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
4960 v1->lmm_magic != LOV_MAGIC_COMP_V1 &&
4961 v1->lmm_magic != LOV_MAGIC_SEL &&
4962 v1->lmm_magic != LOV_USER_MAGIC_SPECIFIC)
4965 if ((v1->lmm_magic == LOV_MAGIC_COMP_V1 ||
4966 v1->lmm_magic == LOV_MAGIC_SEL) &&
4967 !(ah && ah->dah_append_stripes)) {
4968 comp_v1 = (struct lov_comp_md_v1 *)v1;
4969 comp_cnt = comp_v1->lcm_entry_count;
4972 mirror_cnt = comp_v1->lcm_mirror_count + 1;
4980 /* realloc default comp entries if necessary */
4981 rc = lod_def_striping_comp_resize(lds, comp_cnt);
4985 lds->lds_def_comp_cnt = comp_cnt;
4986 lds->lds_def_striping_is_composite = composite;
4987 lds->lds_def_mirror_cnt = mirror_cnt;
4989 for (i = 0; i < comp_cnt; i++) {
4990 struct lod_layout_component *lod_comp;
4993 lod_comp = &lds->lds_def_comp_entries[i];
4995 * reset lod_comp values, llc_stripes is always NULL in
4996 * the default striping template, llc_pool will be reset
4999 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
5002 v1 = (struct lov_user_md *)((char *)comp_v1 +
5003 comp_v1->lcm_entries[i].lcme_offset);
5004 lod_comp->llc_extent =
5005 comp_v1->lcm_entries[i].lcme_extent;
5006 /* We only inherit certain flags from the layout */
5007 lod_comp->llc_flags =
5008 comp_v1->lcm_entries[i].lcme_flags &
5009 LCME_TEMPLATE_FLAGS;
5012 if (!lov_pattern_supported(v1->lmm_pattern) &&
5013 !(v1->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
5014 lod_free_def_comp_entries(lds);
5018 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d stripe_offset=%d append_stripes=%d\n",
5019 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
5020 (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
5021 (int)v1->lmm_stripe_offset,
5022 ah ? ah->dah_append_stripes : 0);
5024 if (ah && ah->dah_append_stripes)
5025 lod_comp->llc_stripe_count = ah->dah_append_stripes;
5027 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
5028 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
5029 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
5030 lod_comp->llc_pattern = v1->lmm_pattern;
5033 if (ah && ah->dah_append_pool && ah->dah_append_pool[0]) {
5034 pool = ah->dah_append_pool;
5035 } else if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
5036 /* XXX: sanity check here */
5037 v3 = (struct lov_user_md_v3 *) v1;
5038 if (v3->lmm_pool_name[0] != '\0')
5039 pool = v3->lmm_pool_name;
5041 lod_set_def_pool(lds, i, pool);
5042 if (v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
5043 v3 = (struct lov_user_md_v3 *)v1;
5044 rc = lod_comp_copy_ost_lists(lod_comp, v3);
5047 } else if (lod_comp->llc_ostlist.op_array &&
5048 lod_comp->llc_ostlist.op_count) {
5049 for (j = 0; j < lod_comp->llc_ostlist.op_count; j++)
5050 lod_comp->llc_ostlist.op_array[j] = -1;
5051 lod_comp->llc_ostlist.op_count = 0;
5055 lds->lds_def_striping_set = 1;
5060 * Get default directory striping.
5062 * \param[in] env execution environment
5063 * \param[in] lo object
5064 * \param[out] lds default striping
5066 * \retval 0 on success
5067 * \retval negative if failed
5069 static int lod_get_default_lmv_striping(const struct lu_env *env,
5070 struct lod_object *lo,
5071 struct lod_default_striping *lds)
5073 struct lmv_user_md *lmu;
5076 lds->lds_dir_def_striping_set = 0;
5078 rc = lod_get_default_lmv_ea(env, lo);
5082 if (rc >= (int)sizeof(*lmu)) {
5083 struct lod_thread_info *info = lod_env_info(env);
5085 lmu = info->lti_ea_store;
5087 lds->lds_dir_def_stripe_count =
5088 le32_to_cpu(lmu->lum_stripe_count);
5089 lds->lds_dir_def_stripe_offset =
5090 le32_to_cpu(lmu->lum_stripe_offset);
5091 lds->lds_dir_def_hash_type =
5092 le32_to_cpu(lmu->lum_hash_type);
5093 lds->lds_dir_def_striping_set = 1;
5100 * Get default striping in the object.
5102 * Get object default striping and default directory striping.
5104 * \param[in] env execution environment
5105 * \param[in] lo object
5106 * \param[out] lds default striping
5108 * \retval 0 on success
5109 * \retval negative if failed
5111 static int lod_get_default_striping(const struct lu_env *env,
5112 struct lod_object *lo,
5113 struct lod_default_striping *lds)
5117 rc = lod_get_default_lov_striping(env, lo, lds, NULL);
5118 rc1 = lod_get_default_lmv_striping(env, lo, lds);
5119 if (rc == 0 && rc1 < 0)
5126 * Apply default striping on object.
5128 * If object striping pattern is not set, set to the one in default striping.
5129 * The default striping is from parent or fs.
5131 * \param[in] lo new object
5132 * \param[in] lds default striping
5133 * \param[in] mode new object's mode
5135 static void lod_striping_from_default(struct lod_object *lo,
5136 const struct lod_default_striping *lds,
5139 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5142 if (lds->lds_def_striping_set && S_ISREG(mode)) {
5143 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
5145 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
5146 lds->lds_def_comp_cnt);
5150 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
5151 if (lds->lds_def_mirror_cnt > 1)
5152 lo->ldo_flr_state = LCM_FL_RDONLY;
5154 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5155 struct lod_layout_component *obj_comp =
5156 &lo->ldo_comp_entries[i];
5157 struct lod_layout_component *def_comp =
5158 &lds->lds_def_comp_entries[i];
5160 CDEBUG(D_LAYOUT, "Inherit from default: flags=%#x "
5161 "size=%hu nr=%u offset=%u pattern=%#x pool=%s\n",
5162 def_comp->llc_flags,
5163 def_comp->llc_stripe_size,
5164 def_comp->llc_stripe_count,
5165 def_comp->llc_stripe_offset,
5166 def_comp->llc_pattern,
5167 def_comp->llc_pool ?: "");
5169 *obj_comp = *def_comp;
5170 if (def_comp->llc_pool != NULL) {
5171 /* pointer was copied from def_comp */
5172 obj_comp->llc_pool = NULL;
5173 lod_obj_set_pool(lo, i, def_comp->llc_pool);
5177 if (def_comp->llc_ostlist.op_array &&
5178 def_comp->llc_ostlist.op_count) {
5179 OBD_ALLOC(obj_comp->llc_ostlist.op_array,
5180 obj_comp->llc_ostlist.op_size);
5181 if (!obj_comp->llc_ostlist.op_array)
5183 memcpy(obj_comp->llc_ostlist.op_array,
5184 def_comp->llc_ostlist.op_array,
5185 obj_comp->llc_ostlist.op_size);
5186 } else if (def_comp->llc_ostlist.op_array) {
5187 obj_comp->llc_ostlist.op_array = NULL;
5191 * Don't initialize these fields for plain layout
5192 * (v1/v3) here, they are inherited in the order of
5193 * 'parent' -> 'fs default (root)' -> 'global default
5194 * values for stripe_count & stripe_size'.
5196 * see lod_ah_init().
5198 if (!lo->ldo_is_composite)
5201 lod_adjust_stripe_info(obj_comp, desc, 0);
5203 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
5204 if (lo->ldo_dir_stripe_count == 0)
5205 lo->ldo_dir_stripe_count =
5206 lds->lds_dir_def_stripe_count;
5207 if (lo->ldo_dir_stripe_offset == -1)
5208 lo->ldo_dir_stripe_offset =
5209 lds->lds_dir_def_stripe_offset;
5210 if (lo->ldo_dir_hash_type == 0)
5211 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
5213 CDEBUG(D_LAYOUT, "striping from default dir: count:%hu, "
5214 "offset:%u, hash_type:%u\n",
5215 lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
5216 lo->ldo_dir_hash_type);
5220 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root,
5223 struct lod_layout_component *lod_comp;
5225 if (lo->ldo_comp_cnt == 0)
5228 if (lo->ldo_is_composite)
5231 lod_comp = &lo->ldo_comp_entries[0];
5233 if (lod_comp->llc_stripe_count <= 0 ||
5234 lod_comp->llc_stripe_size <= 0)
5237 if (from_root && (lod_comp->llc_pool == NULL ||
5238 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
5241 if (append_pool && append_pool[0])
5248 * Implementation of dt_object_operations::do_ah_init.
5250 * This method is used to make a decision on the striping configuration for the
5251 * object being created. It can be taken from the \a parent object if it exists,
5252 * or filesystem's default. The resulting configuration (number of stripes,
5253 * stripe size/offset, pool name, etc) is stored in the object itself and will
5254 * be used by the methods like ->doo_declare_create().
5256 * \see dt_object_operations::do_ah_init() in the API description for details.
5258 static void lod_ah_init(const struct lu_env *env,
5259 struct dt_allocation_hint *ah,
5260 struct dt_object *parent,
5261 struct dt_object *child,
5264 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
5265 struct lod_thread_info *info = lod_env_info(env);
5266 struct lod_default_striping *lds = lod_lds_buf_get(env);
5267 struct dt_object *nextp = NULL;
5268 struct dt_object *nextc;
5269 struct lod_object *lp = NULL;
5270 struct lod_object *lc;
5271 struct lov_desc *desc;
5272 struct lod_layout_component *lod_comp;
5278 if (ah->dah_append_stripes == -1)
5279 ah->dah_append_stripes =
5280 d->lod_ost_descs.ltd_lov_desc.ld_tgt_count;
5282 if (likely(parent)) {
5283 nextp = dt_object_child(parent);
5284 lp = lod_dt_obj(parent);
5287 nextc = dt_object_child(child);
5288 lc = lod_dt_obj(child);
5290 LASSERT(!lod_obj_is_striped(child));
5291 /* default layout template may have been set on the regular file
5292 * when this is called from mdd_create_data() */
5293 if (S_ISREG(child_mode))
5294 lod_free_comp_entries(lc);
5296 if (!dt_object_exists(nextc))
5297 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
5299 if (S_ISDIR(child_mode)) {
5300 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
5302 /* other default values are 0 */
5303 lc->ldo_dir_stripe_offset = -1;
5305 /* no default striping configuration is needed for
5308 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5309 le32_to_cpu(lum1->lum_magic) == LMV_MAGIC_FOREIGN) {
5310 lc->ldo_dir_is_foreign = true;
5311 /* keep stripe_count 0 and stripe_offset -1 */
5312 CDEBUG(D_INFO, "no default striping for foreign dir\n");
5317 * If parent object is not root directory,
5318 * then get default striping from parent object.
5320 if (likely(lp != NULL)) {
5321 lod_get_default_striping(env, lp, lds);
5323 /* inherit default striping except ROOT */
5324 if ((lds->lds_def_striping_set ||
5325 lds->lds_dir_def_striping_set) &&
5326 !fid_is_root(lod_object_fid(lp)))
5327 lc->ldo_def_striping = lds;
5330 /* It should always honour the specified stripes */
5331 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
5332 * will have old magic. In this case, we should ignore the
5333 * stripe count and try to create dir by default stripe.
5335 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5336 (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
5337 le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
5338 lc->ldo_dir_stripe_count =
5339 le32_to_cpu(lum1->lum_stripe_count);
5340 lc->ldo_dir_stripe_offset =
5341 le32_to_cpu(lum1->lum_stripe_offset);
5342 lc->ldo_dir_hash_type =
5343 le32_to_cpu(lum1->lum_hash_type);
5345 "set dirstripe: count %hu, offset %d, hash %u\n",
5346 lc->ldo_dir_stripe_count,
5347 (int)lc->ldo_dir_stripe_offset,
5348 lc->ldo_dir_hash_type);
5350 /* transfer defaults LMV to new directory */
5351 lod_striping_from_default(lc, lds, child_mode);
5353 /* set count 0 to create normal directory */
5354 if (lc->ldo_dir_stripe_count == 1)
5355 lc->ldo_dir_stripe_count = 0;
5358 /* shrink the stripe_count to the avaible MDT count */
5359 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
5360 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)) {
5361 lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
5362 if (lc->ldo_dir_stripe_count == 1)
5363 lc->ldo_dir_stripe_count = 0;
5366 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
5367 lc->ldo_dir_stripe_count,
5368 (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
5373 /* child object regular file*/
5375 if (!lod_object_will_be_striped(S_ISREG(child_mode),
5376 lu_object_fid(&child->do_lu)))
5379 /* If object is going to be striped over OSTs, transfer default
5380 * striping information to the child, so that we can use it
5381 * during declaration and creation.
5383 * Try from the parent first.
5385 if (likely(lp != NULL)) {
5386 rc = lod_get_default_lov_striping(env, lp, lds, ah);
5388 lod_striping_from_default(lc, lds, child_mode);
5391 /* Initialize lod_device::lod_md_root object reference */
5392 if (d->lod_md_root == NULL) {
5393 struct dt_object *root;
5394 struct lod_object *lroot;
5396 lu_root_fid(&info->lti_fid);
5397 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
5398 if (!IS_ERR(root)) {
5399 lroot = lod_dt_obj(root);
5401 spin_lock(&d->lod_lock);
5402 if (d->lod_md_root != NULL)
5403 dt_object_put(env, &d->lod_md_root->ldo_obj);
5404 d->lod_md_root = lroot;
5405 spin_unlock(&d->lod_lock);
5409 /* try inherit layout from the root object (fs default) when:
5410 * - parent does not have default layout; or
5411 * - parent has plain(v1/v3) default layout, and some attributes
5412 * are not specified in the default layout;
5414 if (d->lod_md_root != NULL &&
5415 lod_need_inherit_more(lc, true, ah->dah_append_pool)) {
5416 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds,
5420 if (lc->ldo_comp_cnt == 0) {
5421 lod_striping_from_default(lc, lds, child_mode);
5422 } else if (!lds->lds_def_striping_is_composite) {
5423 struct lod_layout_component *def_comp;
5425 LASSERT(!lc->ldo_is_composite);
5426 lod_comp = &lc->ldo_comp_entries[0];
5427 def_comp = &lds->lds_def_comp_entries[0];
5429 if (lod_comp->llc_stripe_count <= 0)
5430 lod_comp->llc_stripe_count =
5431 def_comp->llc_stripe_count;
5432 if (lod_comp->llc_stripe_size <= 0)
5433 lod_comp->llc_stripe_size =
5434 def_comp->llc_stripe_size;
5435 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT &&
5436 (!lod_comp->llc_pool || !lod_comp->llc_pool[0]))
5437 lod_comp->llc_stripe_offset =
5438 def_comp->llc_stripe_offset;
5439 if (lod_comp->llc_pool == NULL)
5440 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
5445 * fs default striping may not be explicitly set, or historically set
5446 * in config log, use them.
5448 if (lod_need_inherit_more(lc, false, ah->dah_append_pool)) {
5449 if (lc->ldo_comp_cnt == 0) {
5450 rc = lod_alloc_comp_entries(lc, 0, 1);
5452 /* fail to allocate memory, will create a
5453 * non-striped file. */
5455 lc->ldo_is_composite = 0;
5456 lod_comp = &lc->ldo_comp_entries[0];
5457 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
5459 LASSERT(!lc->ldo_is_composite);
5460 lod_comp = &lc->ldo_comp_entries[0];
5461 desc = &d->lod_ost_descs.ltd_lov_desc;
5462 lod_adjust_stripe_info(lod_comp, desc, ah->dah_append_stripes);
5463 if (ah->dah_append_pool && ah->dah_append_pool[0])
5464 lod_obj_set_pool(lc, 0, ah->dah_append_pool);
5470 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
5472 * Size initialization on late striping.
5474 * Propagate the size of a truncated object to a deferred striping.
5475 * This function handles a special case when truncate was done on a
5476 * non-striped object and now while the striping is being created
5477 * we can't lose that size, so we have to propagate it to the stripes
5480 * \param[in] env execution environment
5481 * \param[in] dt object
5482 * \param[in] th transaction handle
5484 * \retval 0 on success
5485 * \retval negative if failed
5487 static int lod_declare_init_size(const struct lu_env *env,
5488 struct dt_object *dt, struct thandle *th)
5490 struct dt_object *next = dt_object_child(dt);
5491 struct lod_object *lo = lod_dt_obj(dt);
5492 struct dt_object **objects = NULL;
5493 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
5494 uint64_t size, offs;
5495 int i, rc, stripe, stripe_count = 0, stripe_size = 0;
5496 struct lu_extent size_ext;
5499 if (!lod_obj_is_striped(dt))
5502 rc = dt_attr_get(env, next, attr);
5503 LASSERT(attr->la_valid & LA_SIZE);
5507 size = attr->la_size;
5511 size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
5512 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5513 struct lod_layout_component *lod_comp;
5514 struct lu_extent *extent;
5516 lod_comp = &lo->ldo_comp_entries[i];
5518 if (lod_comp->llc_stripe == NULL)
5521 extent = &lod_comp->llc_extent;
5522 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
5523 if (!lo->ldo_is_composite ||
5524 lu_extent_is_overlapped(extent, &size_ext)) {
5525 objects = lod_comp->llc_stripe;
5526 stripe_count = lod_comp->llc_stripe_count;
5527 stripe_size = lod_comp->llc_stripe_size;
5530 if (stripe_count == 0)
5533 LASSERT(objects != NULL && stripe_size != 0);
5534 /* ll_do_div64(a, b) returns a % b, and a = a / b */
5535 ll_do_div64(size, (__u64)stripe_size);
5536 stripe = ll_do_div64(size, (__u64)stripe_count);
5537 LASSERT(objects[stripe] != NULL);
5539 size = size * stripe_size;
5540 offs = attr->la_size;
5541 size += ll_do_div64(offs, stripe_size);
5543 attr->la_valid = LA_SIZE;
5544 attr->la_size = size;
5546 rc = lod_sub_declare_attr_set(env, objects[stripe],
5555 * Declare creation of striped object.
5557 * The function declares creation stripes for a regular object. The function
5558 * also declares whether the stripes will be created with non-zero size if
5559 * previously size was set non-zero on the master object. If object \a dt is
5560 * not local, then only fully defined striping can be applied in \a lovea.
5561 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
5564 * \param[in] env execution environment
5565 * \param[in] dt object
5566 * \param[in] attr attributes the stripes will be created with
5567 * \param[in] lovea a buffer containing striping description
5568 * \param[in] th transaction handle
5570 * \retval 0 on success
5571 * \retval negative if failed
5573 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
5574 struct lu_attr *attr,
5575 const struct lu_buf *lovea, struct thandle *th)
5577 struct lod_thread_info *info = lod_env_info(env);
5578 struct dt_object *next = dt_object_child(dt);
5579 struct lod_object *lo = lod_dt_obj(dt);
5583 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
5584 GOTO(out, rc = -ENOMEM);
5586 if (!dt_object_remote(next)) {
5587 /* choose OST and generate appropriate objects */
5588 rc = lod_prepare_create(env, lo, attr, lovea, th);
5593 * declare storage for striping data
5595 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5597 /* LOD can not choose OST objects for remote objects, i.e.
5598 * stripes must be ready before that. Right now, it can only
5599 * happen during migrate, i.e. migrate process needs to create
5600 * remote regular file (mdd_migrate_create), then the migrate
5601 * process will provide stripeEA. */
5602 LASSERT(lovea != NULL);
5603 info->lti_buf = *lovea;
5606 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
5607 XATTR_NAME_LOV, 0, th);
5612 * if striping is created with local object's size > 0,
5613 * we have to propagate this size to specific object
5614 * the case is possible only when local object was created previously
5616 if (dt_object_exists(next))
5617 rc = lod_declare_init_size(env, dt, th);
5620 /* failed to create striping or to set initial size, let's reset
5621 * config so that others don't get confused */
5623 lod_striping_free(env, lo);
5629 * Whether subdirectories under \a dt should be created on MDTs by space QoS
5631 * If LMV_HASH_FLAG_SPACE is set on directory default layout, its subdirectories
5632 * should be created on MDT by space QoS.
5634 * \param[in] env execution environment
5635 * \param[in] dev lu device
5636 * \param[in] dt object
5638 * \retval 1 if directory should create subdir by space usage
5640 * \retval -ev if failed
5642 static inline int dt_object_qos_mkdir(const struct lu_env *env,
5643 struct lu_device *dev,
5644 struct dt_object *dt)
5646 struct lod_thread_info *info = lod_env_info(env);
5647 struct lu_object *obj;
5648 struct lod_object *lo;
5649 struct lmv_user_md *lmu;
5652 obj = lu_object_find_slice(env, dev, lu_object_fid(&dt->do_lu), NULL);
5654 return PTR_ERR(obj);
5656 lo = lu2lod_obj(obj);
5658 rc = lod_get_default_lmv_ea(env, lo);
5659 dt_object_put(env, dt);
5663 if (rc < (int)sizeof(*lmu))
5666 lmu = info->lti_ea_store;
5667 return le32_to_cpu(lmu->lum_stripe_offset) == LMV_OFFSET_DEFAULT;
5671 * Implementation of dt_object_operations::do_declare_create.
5673 * The method declares creation of a new object. If the object will be striped,
5674 * then helper functions are called to find FIDs for the stripes, declare
5675 * creation of the stripes and declare initialization of the striping
5676 * information to be stored in the master object.
5678 * \see dt_object_operations::do_declare_create() in the API description
5681 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
5682 struct lu_attr *attr,
5683 struct dt_allocation_hint *hint,
5684 struct dt_object_format *dof, struct thandle *th)
5686 struct dt_object *next = dt_object_child(dt);
5687 struct lod_object *lo = lod_dt_obj(dt);
5696 * first of all, we declare creation of local object
5698 rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
5703 * it's lod_ah_init() that has decided the object will be striped
5705 if (dof->dof_type == DFT_REGULAR) {
5706 /* callers don't want stripes */
5707 /* XXX: all tricky interactions with ->ah_make_hint() decided
5708 * to use striping, then ->declare_create() behaving differently
5709 * should be cleaned */
5710 if (dof->u.dof_reg.striped != 0)
5711 rc = lod_declare_striped_create(env, dt, attr,
5713 } else if (dof->dof_type == DFT_DIR) {
5714 struct seq_server_site *ss;
5715 struct lu_buf buf = { NULL };
5716 struct lu_buf *lmu = NULL;
5718 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
5720 /* If the parent has default stripeEA, and client
5721 * did not find it before sending create request,
5722 * then MDT will return -EREMOTE, and client will
5723 * retrieve the default stripeEA and re-create the
5726 * Note: if dah_eadata != NULL, it means creating the
5727 * striped directory with specified stripeEA, then it
5728 * should ignore the default stripeEA */
5729 if (hint != NULL && hint->dah_eadata == NULL) {
5730 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
5731 GOTO(out, rc = -EREMOTE);
5733 if (lo->ldo_dir_stripe_offset == LMV_OFFSET_DEFAULT) {
5734 struct lod_default_striping *lds;
5736 lds = lo->ldo_def_striping;
5738 * child and parent should be on the same MDT,
5739 * but if parent has default LMV, and the start
5740 * MDT offset is -1, it's allowed. This check
5741 * is not necessary after 2.12.22 because client
5742 * follows this already, but old client may not.
5744 if (hint->dah_parent &&
5745 dt_object_remote(hint->dah_parent) && lds &&
5746 lds->lds_dir_def_stripe_offset !=
5748 GOTO(out, rc = -EREMOTE);
5749 } else if (lo->ldo_dir_stripe_offset !=
5751 struct lod_device *lod;
5752 struct lu_tgt_desc *mdt = NULL;
5753 bool found_mdt = false;
5755 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5756 lod_foreach_mdt(lod, mdt) {
5757 if (mdt->ltd_index ==
5758 lo->ldo_dir_stripe_offset) {
5764 /* If the MDT indicated by stripe_offset can be
5765 * found, then tell client to resend the create
5766 * request to the correct MDT, otherwise return
5767 * error to client */
5769 GOTO(out, rc = -EREMOTE);
5771 GOTO(out, rc = -EINVAL);
5773 } else if (hint && hint->dah_eadata) {
5775 lmu->lb_buf = (void *)hint->dah_eadata;
5776 lmu->lb_len = hint->dah_eadata_len;
5779 rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
5783 /* failed to create striping or to set initial size, let's reset
5784 * config so that others don't get confused */
5786 lod_striping_free(env, lo);
5791 * Generate component ID for new created component.
5793 * \param[in] lo LOD object
5794 * \param[in] comp_idx index of ldo_comp_entries
5796 * \retval component ID on success
5797 * \retval LCME_ID_INVAL on failure
5799 static __u32 lod_gen_component_id(struct lod_object *lo,
5800 int mirror_id, int comp_idx)
5802 struct lod_layout_component *lod_comp;
5803 __u32 id, start, end;
5806 LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
5808 lod_obj_inc_layout_gen(lo);
5809 id = lo->ldo_layout_gen;
5810 if (likely(id <= SEQ_ID_MAX))
5811 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
5813 /* Layout generation wraps, need to check collisions. */
5814 start = id & SEQ_ID_MASK;
5817 for (id = start; id <= end; id++) {
5818 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5819 lod_comp = &lo->ldo_comp_entries[i];
5820 if (pflr_id(mirror_id, id) == lod_comp->llc_id)
5823 /* Found the ununsed ID */
5824 if (i == lo->ldo_comp_cnt)
5825 RETURN(pflr_id(mirror_id, id));
5827 if (end == LCME_ID_MAX) {
5829 end = min(lo->ldo_layout_gen & LCME_ID_MASK,
5830 (__u32)(LCME_ID_MAX - 1));
5834 RETURN(LCME_ID_INVAL);
5838 * Creation of a striped regular object.
5840 * The function is called to create the stripe objects for a regular
5841 * striped file. This can happen at the initial object creation or
5842 * when the caller asks LOD to do so using ->do_xattr_set() method
5843 * (so called late striping). Notice all the information are already
5844 * prepared in the form of the list of objects (ldo_stripe field).
5845 * This is done during declare phase.
5847 * \param[in] env execution environment
5848 * \param[in] dt object
5849 * \param[in] attr attributes the stripes will be created with
5850 * \param[in] dof format of stripes (see OSD API description)
5851 * \param[in] th transaction handle
5853 * \retval 0 on success
5854 * \retval negative if failed
5856 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
5857 struct lu_attr *attr, struct dt_object_format *dof,
5860 struct lod_layout_component *lod_comp;
5861 struct lod_object *lo = lod_dt_obj(dt);
5866 LASSERT((lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL) ||
5867 lo->ldo_is_foreign);
5869 mirror_id = 0; /* non-flr file's mirror_id is 0 */
5870 if (lo->ldo_mirror_count > 1) {
5871 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5872 lod_comp = &lo->ldo_comp_entries[i];
5873 if (lod_comp->llc_id != LCME_ID_INVAL &&
5874 mirror_id_of(lod_comp->llc_id) > mirror_id)
5875 mirror_id = mirror_id_of(lod_comp->llc_id);
5879 /* create all underlying objects */
5880 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5881 lod_comp = &lo->ldo_comp_entries[i];
5883 if (lod_comp->llc_id == LCME_ID_INVAL) {
5884 /* only the component of FLR layout with more than 1
5885 * mirror has mirror ID in its component ID.
5887 if (lod_comp->llc_extent.e_start == 0 &&
5888 lo->ldo_mirror_count > 1)
5891 lod_comp->llc_id = lod_gen_component_id(lo,
5893 if (lod_comp->llc_id == LCME_ID_INVAL)
5894 GOTO(out, rc = -ERANGE);
5897 if (lod_comp_inited(lod_comp))
5900 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
5901 lod_comp_set_init(lod_comp);
5903 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
5904 lod_comp_set_init(lod_comp);
5906 if (lod_comp->llc_stripe == NULL)
5909 LASSERT(lod_comp->llc_stripe_count);
5910 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
5911 struct dt_object *object = lod_comp->llc_stripe[j];
5912 LASSERT(object != NULL);
5913 rc = lod_sub_create(env, object, attr, NULL, dof, th);
5917 lod_comp_set_init(lod_comp);
5920 rc = lod_fill_mirrors(lo);
5924 rc = lod_generate_and_set_lovea(env, lo, th);
5928 lo->ldo_comp_cached = 1;
5932 lod_striping_free(env, lo);
5936 static inline bool lod_obj_is_dom(struct dt_object *dt)
5938 struct lod_object *lo = lod_dt_obj(dt);
5940 if (!dt_object_exists(dt_object_child(dt)))
5943 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
5946 if (!lo->ldo_comp_cnt)
5949 return (lov_pattern(lo->ldo_comp_entries[0].llc_pattern) ==
5954 * Implementation of dt_object_operations::do_create.
5956 * If any of preceeding methods (like ->do_declare_create(),
5957 * ->do_ah_init(), etc) chose to create a striped object,
5958 * then this method will create the master and the stripes.
5960 * \see dt_object_operations::do_create() in the API description for details.
5962 static int lod_create(const struct lu_env *env, struct dt_object *dt,
5963 struct lu_attr *attr, struct dt_allocation_hint *hint,
5964 struct dt_object_format *dof, struct thandle *th)
5969 /* create local object */
5970 rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
5974 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
5975 (lod_obj_is_striped(dt) || lod_obj_is_dom(dt)) &&
5976 dof->u.dof_reg.striped != 0) {
5977 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
5978 rc = lod_striped_create(env, dt, attr, dof, th);
5985 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
5986 struct dt_object *dt, struct thandle *th,
5987 int comp_idx, int stripe_idx,
5988 struct lod_obj_stripe_cb_data *data)
5990 if (data->locd_declare)
5991 return lod_sub_declare_destroy(env, dt, th);
5992 else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
5993 stripe_idx == cfs_fail_val)
5994 return lod_sub_destroy(env, dt, th);
6000 * Implementation of dt_object_operations::do_declare_destroy.
6002 * If the object is a striped directory, then the function declares reference
6003 * removal from the master object (this is an index) to the stripes and declares
6004 * destroy of all the stripes. In all the cases, it declares an intention to
6005 * destroy the object itself.
6007 * \see dt_object_operations::do_declare_destroy() in the API description
6010 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
6013 struct dt_object *next = dt_object_child(dt);
6014 struct lod_object *lo = lod_dt_obj(dt);
6015 struct lod_thread_info *info = lod_env_info(env);
6016 struct dt_object *stripe;
6017 char *stripe_name = info->lti_key;
6023 * load striping information, notice we don't do this when object
6024 * is being initialized as we don't need this information till
6025 * few specific cases like destroy, chown
6027 rc = lod_striping_load(env, lo);
6031 /* declare destroy for all underlying objects */
6032 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6033 rc = next->do_ops->do_index_try(env, next,
6034 &dt_directory_features);
6038 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6039 stripe = lo->ldo_stripe[i];
6043 rc = lod_sub_declare_ref_del(env, next, th);
6047 snprintf(stripe_name, sizeof(info->lti_key),
6049 PFID(lu_object_fid(&stripe->do_lu)), i);
6050 rc = lod_sub_declare_delete(env, next,
6051 (const struct dt_key *)stripe_name, th);
6058 * we declare destroy for the local object
6060 rc = lod_sub_declare_destroy(env, next, th);
6064 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
6065 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
6068 if (!lod_obj_is_striped(dt))
6071 /* declare destroy all striped objects */
6072 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6073 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6074 stripe = lo->ldo_stripe[i];
6078 if (!dt_object_exists(stripe))
6081 rc = lod_sub_declare_ref_del(env, stripe, th);
6085 rc = lod_sub_declare_destroy(env, stripe, th);
6090 struct lod_obj_stripe_cb_data data = { { 0 } };
6092 data.locd_declare = true;
6093 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6094 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6101 * Implementation of dt_object_operations::do_destroy.
6103 * If the object is a striped directory, then the function removes references
6104 * from the master object (this is an index) to the stripes and destroys all
6105 * the stripes. In all the cases, the function destroys the object itself.
6107 * \see dt_object_operations::do_destroy() in the API description for details.
6109 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
6112 struct dt_object *next = dt_object_child(dt);
6113 struct lod_object *lo = lod_dt_obj(dt);
6114 struct lod_thread_info *info = lod_env_info(env);
6115 char *stripe_name = info->lti_key;
6116 struct dt_object *stripe;
6122 /* destroy sub-stripe of master object */
6123 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6124 rc = next->do_ops->do_index_try(env, next,
6125 &dt_directory_features);
6129 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6130 stripe = lo->ldo_stripe[i];
6134 rc = lod_sub_ref_del(env, next, th);
6138 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
6139 PFID(lu_object_fid(&stripe->do_lu)), i);
6141 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
6142 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
6143 PFID(lu_object_fid(&stripe->do_lu)));
6145 rc = lod_sub_delete(env, next,
6146 (const struct dt_key *)stripe_name, th);
6152 rc = lod_sub_destroy(env, next, th);
6156 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
6157 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
6160 if (!lod_obj_is_striped(dt))
6163 /* destroy all striped objects */
6164 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6165 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6166 stripe = lo->ldo_stripe[i];
6170 if (!dt_object_exists(stripe))
6173 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
6174 i == cfs_fail_val) {
6175 dt_write_lock(env, stripe, DT_TGT_CHILD);
6176 rc = lod_sub_ref_del(env, stripe, th);
6177 dt_write_unlock(env, stripe);
6181 rc = lod_sub_destroy(env, stripe, th);
6187 struct lod_obj_stripe_cb_data data = { { 0 } };
6189 data.locd_declare = false;
6190 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6191 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6198 * Implementation of dt_object_operations::do_declare_ref_add.
6200 * \see dt_object_operations::do_declare_ref_add() in the API description
6203 static int lod_declare_ref_add(const struct lu_env *env,
6204 struct dt_object *dt, struct thandle *th)
6206 return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
6210 * Implementation of dt_object_operations::do_ref_add.
6212 * \see dt_object_operations::do_ref_add() in the API description for details.
6214 static int lod_ref_add(const struct lu_env *env,
6215 struct dt_object *dt, struct thandle *th)
6217 return lod_sub_ref_add(env, dt_object_child(dt), th);
6221 * Implementation of dt_object_operations::do_declare_ref_del.
6223 * \see dt_object_operations::do_declare_ref_del() in the API description
6226 static int lod_declare_ref_del(const struct lu_env *env,
6227 struct dt_object *dt, struct thandle *th)
6229 return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
6233 * Implementation of dt_object_operations::do_ref_del
6235 * \see dt_object_operations::do_ref_del() in the API description for details.
6237 static int lod_ref_del(const struct lu_env *env,
6238 struct dt_object *dt, struct thandle *th)
6240 return lod_sub_ref_del(env, dt_object_child(dt), th);
6244 * Implementation of dt_object_operations::do_object_sync.
6246 * \see dt_object_operations::do_object_sync() in the API description
6249 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
6250 __u64 start, __u64 end)
6252 return dt_object_sync(env, dt_object_child(dt), start, end);
6256 * Implementation of dt_object_operations::do_object_unlock.
6258 * Used to release LDLM lock(s).
6260 * \see dt_object_operations::do_object_unlock() in the API description
6263 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
6264 struct ldlm_enqueue_info *einfo,
6265 union ldlm_policy_data *policy)
6267 struct lod_object *lo = lod_dt_obj(dt);
6268 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
6269 int slave_locks_size;
6273 if (slave_locks == NULL)
6276 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
6277 /* Note: for remote lock for single stripe dir, MDT will cancel
6278 * the lock by lockh directly */
6279 LASSERT(!dt_object_remote(dt_object_child(dt)));
6281 /* locks were unlocked in MDT layer */
6282 for (i = 0; i < slave_locks->ha_count; i++)
6283 LASSERT(!lustre_handle_is_used(&slave_locks->ha_handles[i]));
6286 * NB, ha_count may not equal to ldo_dir_stripe_count, because dir
6287 * layout may change, e.g., shrink dir layout after migration.
6289 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6290 if (lo->ldo_stripe[i])
6291 dt_invalidate(env, lo->ldo_stripe[i]);
6294 slave_locks_size = offsetof(typeof(*slave_locks),
6295 ha_handles[slave_locks->ha_count]);
6296 OBD_FREE(slave_locks, slave_locks_size);
6297 einfo->ei_cbdata = NULL;
6303 * Implementation of dt_object_operations::do_object_lock.
6305 * Used to get LDLM lock on the non-striped and striped objects.
6307 * \see dt_object_operations::do_object_lock() in the API description
6310 static int lod_object_lock(const struct lu_env *env,
6311 struct dt_object *dt,
6312 struct lustre_handle *lh,
6313 struct ldlm_enqueue_info *einfo,
6314 union ldlm_policy_data *policy)
6316 struct lod_object *lo = lod_dt_obj(dt);
6317 int slave_locks_size;
6318 struct lustre_handle_array *slave_locks = NULL;
6323 /* remote object lock */
6324 if (!einfo->ei_enq_slave) {
6325 LASSERT(dt_object_remote(dt));
6326 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
6330 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
6333 rc = lod_striping_load(env, lo);
6338 if (lo->ldo_dir_stripe_count <= 1)
6341 slave_locks_size = offsetof(typeof(*slave_locks),
6342 ha_handles[lo->ldo_dir_stripe_count]);
6343 /* Freed in lod_object_unlock */
6344 OBD_ALLOC(slave_locks, slave_locks_size);
6347 slave_locks->ha_count = lo->ldo_dir_stripe_count;
6349 /* striped directory lock */
6350 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6351 struct lustre_handle lockh;
6352 struct ldlm_res_id *res_id;
6353 struct dt_object *stripe;
6355 stripe = lo->ldo_stripe[i];
6359 res_id = &lod_env_info(env)->lti_res_id;
6360 fid_build_reg_res_name(lu_object_fid(&stripe->do_lu), res_id);
6361 einfo->ei_res_id = res_id;
6363 if (dt_object_remote(stripe)) {
6364 set_bit(i, (void *)slave_locks->ha_map);
6365 rc = dt_object_lock(env, stripe, &lockh, einfo, policy);
6367 struct ldlm_namespace *ns = einfo->ei_namespace;
6368 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
6369 ldlm_completion_callback completion = einfo->ei_cb_cp;
6370 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
6372 if (einfo->ei_mode == LCK_PW ||
6373 einfo->ei_mode == LCK_EX)
6374 dlmflags |= LDLM_FL_COS_INCOMPAT;
6376 LASSERT(ns != NULL);
6377 rc = ldlm_cli_enqueue_local(env, ns, res_id, LDLM_IBITS,
6378 policy, einfo->ei_mode,
6379 &dlmflags, blocking,
6381 NULL, 0, LVB_T_NONE,
6386 ldlm_lock_decref_and_cancel(
6387 &slave_locks->ha_handles[i],
6389 OBD_FREE(slave_locks, slave_locks_size);
6392 slave_locks->ha_handles[i] = lockh;
6394 einfo->ei_cbdata = slave_locks;
6400 * Implementation of dt_object_operations::do_invalidate.
6402 * \see dt_object_operations::do_invalidate() in the API description for details
6404 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
6406 return dt_invalidate(env, dt_object_child(dt));
6409 static int lod_declare_instantiate_components(const struct lu_env *env,
6410 struct lod_object *lo, struct thandle *th)
6412 struct lod_thread_info *info = lod_env_info(env);
6417 LASSERT(info->lti_count < lo->ldo_comp_cnt);
6419 for (i = 0; i < info->lti_count; i++) {
6420 rc = lod_qos_prep_create(env, lo, NULL, th,
6421 info->lti_comp_idx[i]);
6427 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6428 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
6429 &info->lti_buf, XATTR_NAME_LOV, 0, th);
6436 * Check OSTs for an existing component for further extension
6438 * Checks if OSTs are still healthy and not out of space. Gets free space
6439 * on OSTs (relative to allocation watermark rmb_low) and compares to
6440 * the proposed new_end for this component.
6442 * Decides whether or not to extend a component on its current OSTs.
6444 * \param[in] env execution environment for this thread
6445 * \param[in] lo object we're checking
6446 * \param[in] index index of this component
6447 * \param[in] extension_size extension size for this component
6448 * \param[in] extent layout extent for requested operation
6449 * \param[in] comp_extent extension component extent
6450 * \param[in] write if this is write operation
6452 * \retval true - OK to extend on current OSTs
6453 * \retval false - do not extend on current OSTs
6455 static bool lod_sel_osts_allowed(const struct lu_env *env,
6456 struct lod_object *lo,
6457 int index, __u64 extension_size,
6458 struct lu_extent *extent,
6459 struct lu_extent *comp_extent, int write)
6461 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[index];
6462 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6463 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
6464 __u64 available = 0;
6471 LASSERT(lod_comp->llc_stripe_count != 0);
6474 (extent->e_start == 0 && extent->e_end == OBD_OBJECT_EOF)) {
6475 /* truncate or append */
6476 size = extension_size;
6478 /* In case of write op, check the real write extent,
6479 * it may be larger than the extension_size */
6480 size = roundup(min(extent->e_end, comp_extent->e_end) -
6481 max(extent->e_start, comp_extent->e_start),
6484 /* extension_size is file level, so we must divide by stripe count to
6485 * compare it to available space on a single OST */
6486 size /= lod_comp->llc_stripe_count;
6488 lod_getref(&lod->lod_ost_descs);
6489 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
6490 int index = lod_comp->llc_ost_indices[i];
6491 struct lod_tgt_desc *ost = OST_TGT(lod, index);
6492 struct obd_statfs_info info = { 0 };
6493 int j, repeated = 0;
6497 /* Get the number of times this OST repeats in this component.
6498 * Note: inter-component repeats are not counted as this is
6499 * considered as a rare case: we try to not repeat OST in other
6500 * components if possible. */
6501 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6502 if (index != lod_comp->llc_ost_indices[j])
6505 /* already handled */
6511 if (j < lod_comp->llc_stripe_count)
6514 if (!cfs_bitmap_check(lod->lod_ost_bitmap, index)) {
6515 CDEBUG(D_LAYOUT, "ost %d no longer present\n", index);
6520 rc = dt_statfs_info(env, ost->ltd_tgt, sfs, &info);
6522 CDEBUG(D_LAYOUT, "statfs failed for ost %d, error %d\n",
6528 if (sfs->os_state & OS_STATE_ENOSPC ||
6529 sfs->os_state & OS_STATE_READONLY ||
6530 sfs->os_state & OS_STATE_DEGRADED) {
6531 CDEBUG(D_LAYOUT, "ost %d is not availble for SEL "
6532 "extension, state %u\n", index, sfs->os_state);
6538 available = sfs->os_bavail * sfs->os_bsize;
6539 /* 'available' is relative to the allocation threshold */
6540 available -= (__u64) info.os_reserved_mb_low << 20;
6542 CDEBUG(D_LAYOUT, "ost %d lowwm: %d highwm: %d, "
6543 "%llu %% blocks available, %llu %% blocks free\n",
6544 index, info.os_reserved_mb_low, info.os_reserved_mb_high,
6545 (100ull * sfs->os_bavail) / sfs->os_blocks,
6546 (100ull * sfs->os_bfree) / sfs->os_blocks);
6548 if (size * repeated > available) {
6550 CDEBUG(D_LAYOUT, "low space on ost %d, available %llu "
6551 "< extension size %llu\n", index, available,
6556 lod_putref(lod, &lod->lod_ost_descs);
6562 * Adjust extents after component removal
6564 * When we remove an extension component, we move the start of the next
6565 * component to match the start of the extension component, so no space is left
6568 * \param[in] env execution environment for this thread
6569 * \param[in] lo object
6570 * \param[in] max_comp layout component
6571 * \param[in] index index of this component
6573 * \retval 0 on success
6574 * \retval negative errno on error
6576 static void lod_sel_adjust_extents(const struct lu_env *env,
6577 struct lod_object *lo,
6578 int max_comp, int index)
6580 struct lod_layout_component *lod_comp = NULL;
6581 struct lod_layout_component *next = NULL;
6582 struct lod_layout_component *prev = NULL;
6583 __u64 new_start = 0;
6587 /* Extension space component */
6588 lod_comp = &lo->ldo_comp_entries[index];
6589 next = &lo->ldo_comp_entries[index + 1];
6590 prev = &lo->ldo_comp_entries[index - 1];
6592 LASSERT(lod_comp != NULL && prev != NULL && next != NULL);
6593 LASSERT(lod_comp->llc_flags & LCME_FL_EXTENSION);
6595 /* Previous is being removed */
6596 if (prev && prev->llc_id == LCME_ID_INVAL)
6597 new_start = prev->llc_extent.e_start;
6599 new_start = lod_comp->llc_extent.e_start;
6601 for (i = index + 1; i < max_comp; i++) {
6602 lod_comp = &lo->ldo_comp_entries[i];
6604 start = lod_comp->llc_extent.e_start;
6605 lod_comp->llc_extent.e_start = new_start;
6607 /* We only move zero length extendable components */
6608 if (!(start == lod_comp->llc_extent.e_end))
6611 LASSERT(!(lod_comp->llc_flags & LCME_FL_INIT));
6613 lod_comp->llc_extent.e_end = new_start;
6617 /* Calculate the proposed 'new end' for a component we're extending */
6618 static __u64 lod_extension_new_end(__u64 extension_size, __u64 extent_end,
6619 __u32 stripe_size, __u64 component_end,
6620 __u64 extension_end)
6624 LASSERT(extension_size != 0 && stripe_size != 0);
6626 /* Round up to extension size */
6627 if (extent_end == OBD_OBJECT_EOF) {
6628 new_end = OBD_OBJECT_EOF;
6630 /* Add at least extension_size to the previous component_end,
6631 * covering the req layout extent */
6632 new_end = max(extent_end - component_end, extension_size);
6633 new_end = roundup(new_end, extension_size);
6634 new_end += component_end;
6636 /* Component end must be min stripe size aligned */
6637 if (new_end % stripe_size) {
6638 CDEBUG(D_LAYOUT, "new component end is not aligned "
6639 "by the stripe size %u: [%llu, %llu) ext size "
6640 "%llu new end %llu, aligning\n",
6641 stripe_size, component_end, extent_end,
6642 extension_size, new_end);
6643 new_end = roundup(new_end, stripe_size);
6647 if (new_end < extent_end)
6648 new_end = OBD_OBJECT_EOF;
6651 /* Don't extend past the end of the extension component */
6652 if (new_end > extension_end)
6653 new_end = extension_end;
6658 /* As lod_sel_handler() could be re-entered for the same component several
6659 * times, this is the data for the next call. Fields could be changed to
6660 * component indexes when needed, (e.g. if there is no need to instantiate
6661 * all the previous components up to the current position) to tell the caller
6662 * where to start over from. */
6669 * Process extent updates for a particular layout component
6671 * Handle layout updates for a particular extension space component touched by
6672 * a layout update operation. Core function of self-extending PFL feature.
6674 * In general, this function processes exactly *one* stage of an extension
6675 * operation, modifying the layout accordingly, then returns to the caller.
6676 * The caller is responsible for restarting processing with the new layout,
6677 * which may repeatedly return to this function until the extension updates
6680 * This function does one of a few things to the layout:
6681 * 1. Extends the component before the current extension space component to
6682 * allow it to accomodate the requested operation (if space/policy permit that
6683 * component to continue on its current OSTs)
6685 * 2. If extension of the existing component fails, we do one of two things:
6686 * a. If there is a component after the extension space, we remove the
6687 * extension space component, move the start of the next component down
6688 * accordingly, then notify the caller to restart processing w/the new
6690 * b. If there is no following component, we try repeating the current
6691 * component, creating a new component using the current one as a
6692 * template (keeping its stripe properties but not specific striping),
6693 * and try assigning striping for this component. If there is sufficient
6694 * free space on the OSTs chosen for this component, it is instantiated
6695 * and i/o continues there.
6697 * If there is not sufficient space on the new OSTs, we remove this new
6698 * component & extend the current component.
6700 * Note further that uninited components followed by extension space can be zero
6701 * length meaning that we will try to extend them before initializing them, and
6702 * if that fails, they will be removed without initialization.
6704 * 3. If we extend to/beyond the end of an extension space component, that
6705 * component is exhausted (all of its range has been given to real components),
6706 * so we remove it and restart processing.
6708 * \param[in] env execution environment for this thread
6709 * \param[in,out] lo object to update the layout of
6710 * \param[in] extent layout extent for requested operation, update
6711 * layout to fit this operation
6712 * \param[in] th transaction handle for this operation
6713 * \param[in,out] max_comp the highest comp for the portion of the layout
6714 * we are operating on (For FLR, the chosen
6715 * replica). Updated because we may remove
6717 * \param[in] index index of the extension space component we're
6719 * \param[in] write if this is write op
6720 * \param[in,out] force if the extension is to be forced; set here
6721 to force it on the 2nd call for the same
6724 * \retval 0 on success
6725 * \retval negative errno on error
6727 static int lod_sel_handler(const struct lu_env *env,
6728 struct lod_object *lo,
6729 struct lu_extent *extent,
6730 struct thandle *th, int *max_comp,
6731 int index, int write,
6732 struct sel_data *sd)
6734 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6735 struct lod_thread_info *info = lod_env_info(env);
6736 struct lod_layout_component *lod_comp;
6737 struct lod_layout_component *prev;
6738 struct lod_layout_component *next = NULL;
6739 __u64 extension_size;
6746 /* First component cannot be extension space */
6748 CERROR("%s: "DFID" first component cannot be extension space\n",
6749 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6753 lod_comp = &lo->ldo_comp_entries[index];
6754 prev = &lo->ldo_comp_entries[index - 1];
6755 if ((index + 1) < *max_comp)
6756 next = &lo->ldo_comp_entries[index + 1];
6758 /* extension size uses the stripe size field as KiB */
6759 extension_size = lod_comp->llc_stripe_size * SEL_UNIT_SIZE;
6761 CDEBUG(D_LAYOUT, "prev start %llu, extension start %llu, extension end"
6762 " %llu, extension size %llu\n", prev->llc_extent.e_start,
6763 lod_comp->llc_extent.e_start, lod_comp->llc_extent.e_end,
6766 /* Two extension space components cannot be adjacent & extension space
6767 * components cannot be init */
6768 if ((prev->llc_flags & LCME_FL_EXTENSION) ||
6769 !(ergo(next, !(next->llc_flags & LCME_FL_EXTENSION))) ||
6770 lod_comp_inited(lod_comp)) {
6771 CERROR("%s: "DFID" invalid extension space components\n",
6772 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6776 if (!prev->llc_stripe) {
6777 CDEBUG(D_LAYOUT, "Previous component not inited\n");
6778 info->lti_count = 1;
6779 info->lti_comp_idx[0] = index - 1;
6780 rc = lod_declare_instantiate_components(env, lo, th);
6781 /* ENOSPC tells us we can't use this component. If there is
6782 * a next or we are repeating, we either spill over (next) or
6783 * extend the original comp (repeat). Otherwise, return the
6784 * error to the user. */
6785 if (rc == -ENOSPC && (next || sd->sd_repeat))
6791 if (sd->sd_force == 0 && rc == 0)
6792 rc = !lod_sel_osts_allowed(env, lo, index - 1,
6793 extension_size, extent,
6794 &lod_comp->llc_extent, write);
6796 repeated = !!(sd->sd_repeat);
6800 /* Extend previous component */
6802 new_end = lod_extension_new_end(extension_size, extent->e_end,
6803 prev->llc_stripe_size,
6804 prev->llc_extent.e_end,
6805 lod_comp->llc_extent.e_end);
6807 CDEBUG(D_LAYOUT, "new end %llu\n", new_end);
6808 lod_comp->llc_extent.e_start = new_end;
6809 prev->llc_extent.e_end = new_end;
6811 if (prev->llc_extent.e_end == lod_comp->llc_extent.e_end) {
6812 CDEBUG(D_LAYOUT, "Extension component exhausted\n");
6813 lod_comp->llc_id = LCME_ID_INVAL;
6817 /* rc == 1, failed to extend current component */
6820 /* Normal 'spillover' case - Remove the extension
6821 * space component & bring down the start of the next
6823 lod_comp->llc_id = LCME_ID_INVAL;
6825 if (!(prev->llc_flags & LCME_FL_INIT)) {
6826 prev->llc_id = LCME_ID_INVAL;
6829 lod_sel_adjust_extents(env, lo, *max_comp, index);
6830 } else if (lod_comp_inited(prev)) {
6831 /* If there is no next, and the previous component is
6832 * INIT'ed, try repeating the previous component. */
6833 LASSERT(repeated == 0);
6834 rc = lod_layout_repeat_comp(env, lo, index - 1);
6838 /* The previous component is a repeated component.
6839 * Record this so we don't keep trying to repeat it. */
6842 /* If the previous component is not INIT'ed, this may
6843 * be a component we have just instantiated but failed
6844 * to extend. Or even a repeated component we failed
6845 * to prepare a striping for. Do not repeat but instead
6846 * remove the repeated component & force the extention
6847 * of the original one */
6850 prev->llc_id = LCME_ID_INVAL;
6857 rc = lod_layout_del_prep_layout(env, lo, NULL);
6860 LASSERTF(-rc == change,
6861 "number deleted %d != requested %d\n", -rc,
6864 *max_comp = *max_comp + change;
6866 /* lod_del_prep_layout reallocates ldo_comp_entries, so we must
6867 * refresh these pointers before using them */
6868 lod_comp = &lo->ldo_comp_entries[index];
6869 prev = &lo->ldo_comp_entries[index - 1];
6870 CDEBUG(D_LAYOUT, "After extent updates: prev start %llu, current start "
6871 "%llu, current end %llu max_comp %d ldo_comp_cnt %d\n",
6872 prev->llc_extent.e_start, lod_comp->llc_extent.e_start,
6873 lod_comp->llc_extent.e_end, *max_comp, lo->ldo_comp_cnt);
6875 /* Layout changed successfully */
6880 * Declare layout extent updates
6882 * Handles extensions. Identifies extension components touched by current
6883 * operation and passes them to processing function.
6885 * Restarts with updated layouts from the processing function until the current
6886 * operation no longer touches an extension space component.
6888 * \param[in] env execution environment for this thread
6889 * \param[in,out] lo object to update the layout of
6890 * \param[in] extent layout extent for requested operation, update layout to
6891 * fit this operation
6892 * \param[in] th transaction handle for this operation
6893 * \param[in] pick identifies chosen mirror for FLR layouts
6894 * \param[in] write if this is write op
6896 * \retval 1 on layout changed, 0 on no change
6897 * \retval negative errno on error
6899 static int lod_declare_update_extents(const struct lu_env *env,
6900 struct lod_object *lo, struct lu_extent *extent,
6901 struct thandle *th, int pick, int write)
6903 struct lod_thread_info *info = lod_env_info(env);
6904 struct lod_layout_component *lod_comp;
6905 bool layout_changed = false;
6906 struct sel_data sd = { 0 };
6914 /* This makes us work on the components of the chosen mirror */
6915 start_index = lo->ldo_mirrors[pick].lme_start;
6916 max_comp = lo->ldo_mirrors[pick].lme_end + 1;
6917 if (lo->ldo_flr_state == LCM_FL_NONE)
6918 LASSERT(start_index == 0 && max_comp == lo->ldo_comp_cnt);
6920 CDEBUG(D_LAYOUT, "extent->e_start %llu, extent->e_end %llu\n",
6921 extent->e_start, extent->e_end);
6922 for (i = start_index; i < max_comp; i++) {
6923 lod_comp = &lo->ldo_comp_entries[i];
6925 /* We've passed all components of interest */
6926 if (lod_comp->llc_extent.e_start >= extent->e_end)
6929 if (lod_comp->llc_flags & LCME_FL_EXTENSION) {
6930 layout_changed = true;
6931 rc = lod_sel_handler(env, lo, extent, th, &max_comp,
6936 /* Nothing has changed behind the prev one */
6942 /* We may have added or removed components. If so, we must update the
6943 * start & ends of all the mirrors after the current one, and the end
6944 * of the current mirror. */
6945 change = max_comp - 1 - lo->ldo_mirrors[pick].lme_end;
6947 lo->ldo_mirrors[pick].lme_end += change;
6948 for (i = pick + 1; i < lo->ldo_mirror_count; i++) {
6949 lo->ldo_mirrors[i].lme_start += change;
6950 lo->ldo_mirrors[i].lme_end += change;
6956 /* The amount of components has changed, adjust the lti_comp_idx */
6957 rc2 = lod_layout_data_init(info, lo->ldo_comp_cnt);
6959 return rc < 0 ? rc : rc2 < 0 ? rc2 : layout_changed;
6962 /* If striping is already instantiated or INIT'ed DOM? */
6963 static bool lod_is_instantiation_needed(struct lod_layout_component *comp)
6965 return !(((lov_pattern(comp->llc_pattern) == LOV_PATTERN_MDT) &&
6966 lod_comp_inited(comp)) || comp->llc_stripe);
6970 * Declare layout update for a non-FLR layout.
6972 * \param[in] env execution environment for this thread
6973 * \param[in,out] lo object to update the layout of
6974 * \param[in] layout layout intent for requested operation, "update" is
6975 * a process of reacting to this
6976 * \param[in] buf buffer containing lov ea (see comment on usage inline)
6977 * \param[in] th transaction handle for this operation
6979 * \retval 0 on success
6980 * \retval negative errno on error
6982 static int lod_declare_update_plain(const struct lu_env *env,
6983 struct lod_object *lo, struct layout_intent *layout,
6984 const struct lu_buf *buf, struct thandle *th)
6986 struct lod_thread_info *info = lod_env_info(env);
6987 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6988 struct lod_layout_component *lod_comp;
6989 struct lov_comp_md_v1 *comp_v1 = NULL;
6990 bool layout_changed = false;
6991 bool replay = false;
6995 LASSERT(lo->ldo_flr_state == LCM_FL_NONE);
6998 * In case the client is passing lovea, which only happens during
6999 * the replay of layout intent write RPC for now, we may need to
7000 * parse the lovea and apply new layout configuration.
7002 if (buf && buf->lb_len) {
7003 struct lov_user_md_v1 *v1 = buf->lb_buf;
7005 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
7006 v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
7007 LOV_MAGIC_COMP_V1)) {
7008 CERROR("%s: the replay buffer of layout extend "
7009 "(magic %#x) does not contain expected "
7010 "composite layout.\n",
7011 lod2obd(d)->obd_name, v1->lmm_magic);
7012 GOTO(out, rc = -EINVAL);
7015 rc = lod_use_defined_striping(env, lo, buf);
7018 lo->ldo_comp_cached = 1;
7020 rc = lod_get_lov_ea(env, lo);
7023 /* old on-disk EA is stored in info->lti_buf */
7024 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
7026 layout_changed = true;
7028 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
7032 /* non replay path */
7033 rc = lod_striping_load(env, lo);
7038 /* Make sure defined layout covers the requested write range. */
7039 lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
7040 if (lo->ldo_comp_cnt > 1 &&
7041 lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
7042 lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
7043 CDEBUG_LIMIT(replay ? D_ERROR : D_LAYOUT,
7044 "%s: the defined layout [0, %#llx) does not "
7045 "covers the write range "DEXT"\n",
7046 lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
7047 PEXT(&layout->li_extent));
7048 GOTO(out, rc = -EINVAL);
7051 CDEBUG(D_LAYOUT, "%s: "DFID": update components "DEXT"\n",
7052 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
7053 PEXT(&layout->li_extent));
7056 rc = lod_declare_update_extents(env, lo, &layout->li_extent,
7057 th, 0, layout->li_opc == LAYOUT_INTENT_WRITE);
7061 layout_changed = true;
7065 * Iterate ld->ldo_comp_entries, find the component whose extent under
7066 * the write range and not instantianted.
7068 for (i = 0; i < lo->ldo_comp_cnt; i++) {
7069 lod_comp = &lo->ldo_comp_entries[i];
7071 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
7075 /* If striping is instantiated or INIT'ed DOM skip */
7076 if (!lod_is_instantiation_needed(lod_comp))
7080 * In replay path, lod_comp is the EA passed by
7081 * client replay buffer, comp_v1 is the pre-recovery
7082 * on-disk EA, we'd sift out those components which
7083 * were init-ed in the on-disk EA.
7085 if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
7090 * this component hasn't instantiated in normal path, or during
7091 * replay it needs replay the instantiation.
7094 /* A released component is being extended */
7095 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
7096 GOTO(out, rc = -EINVAL);
7098 LASSERT(info->lti_comp_idx != NULL);
7099 info->lti_comp_idx[info->lti_count++] = i;
7100 layout_changed = true;
7103 if (!layout_changed)
7106 lod_obj_inc_layout_gen(lo);
7107 rc = lod_declare_instantiate_components(env, lo, th);
7111 lod_striping_free(env, lo);
7115 static inline int lod_comp_index(struct lod_object *lo,
7116 struct lod_layout_component *lod_comp)
7118 LASSERT(lod_comp >= lo->ldo_comp_entries &&
7119 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
7121 return lod_comp - lo->ldo_comp_entries;
7125 * Stale other mirrors by writing extent.
7127 static int lod_stale_components(const struct lu_env *env, struct lod_object *lo,
7128 int primary, struct lu_extent *extent,
7131 struct lod_layout_component *pri_comp, *lod_comp;
7132 struct lu_extent pri_extent;
7137 /* The writing extent decides which components in the primary
7138 * are affected... */
7139 CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
7142 lod_foreach_mirror_comp(pri_comp, lo, primary) {
7143 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
7146 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
7147 lod_comp_index(lo, pri_comp),
7148 PEXT(&pri_comp->llc_extent));
7150 pri_extent.e_start = pri_comp->llc_extent.e_start;
7151 pri_extent.e_end = pri_comp->llc_extent.e_end;
7153 for (i = 0; i < lo->ldo_mirror_count; i++) {
7156 rc = lod_declare_update_extents(env, lo, &pri_extent,
7158 /* if update_extents changed the layout, it may have
7159 * reallocated the component array, so start over to
7160 * avoid using stale pointers */
7166 /* ... and then stale other components that are
7167 * overlapping with primary components */
7168 lod_foreach_mirror_comp(lod_comp, lo, i) {
7169 if (!lu_extent_is_overlapped(
7171 &lod_comp->llc_extent))
7174 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
7175 i, lod_comp_index(lo, lod_comp));
7177 lod_comp->llc_flags |= LCME_FL_STALE;
7178 lo->ldo_mirrors[i].lme_stale = 1;
7187 * check an OST's availability
7188 * \param[in] env execution environment
7189 * \param[in] lo lod object
7190 * \param[in] dt dt object
7191 * \param[in] index mirror index
7193 * \retval negative if failed
7194 * \retval 1 if \a dt is available
7195 * \retval 0 if \a dt is not available
7197 static inline int lod_check_ost_avail(const struct lu_env *env,
7198 struct lod_object *lo,
7199 struct dt_object *dt, int index)
7201 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7202 struct lod_tgt_desc *ost;
7204 int type = LU_SEQ_RANGE_OST;
7207 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &idx, &type);
7209 CERROR("%s: can't locate "DFID":rc = %d\n",
7210 lod2obd(lod)->obd_name, PFID(lu_object_fid(&dt->do_lu)),
7215 ost = OST_TGT(lod, idx);
7216 if (ost->ltd_statfs.os_state &
7217 (OS_STATE_READONLY | OS_STATE_ENOSPC | OS_STATE_ENOINO |
7218 OS_STATE_NOPRECREATE) ||
7219 ost->ltd_active == 0) {
7220 CDEBUG(D_LAYOUT, DFID ": mirror %d OST%d unavail, rc = %d\n",
7221 PFID(lod_object_fid(lo)), index, idx, rc);
7229 * Pick primary mirror for write
7230 * \param[in] env execution environment
7231 * \param[in] lo object
7232 * \param[in] extent write range
7234 static int lod_primary_pick(const struct lu_env *env, struct lod_object *lo,
7235 struct lu_extent *extent)
7237 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7238 unsigned int seq = 0;
7239 struct lod_layout_component *lod_comp;
7241 int picked = -1, second_pick = -1, third_pick = -1;
7244 if (OBD_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
7245 get_random_bytes(&seq, sizeof(seq));
7246 seq %= lo->ldo_mirror_count;
7250 * Pick a mirror as the primary, and check the availability of OSTs.
7252 * This algo can be revised later after knowing the topology of
7255 lod_qos_statfs_update(env, lod, &lod->lod_ost_descs);
7256 for (i = 0; i < lo->ldo_mirror_count; i++) {
7257 bool ost_avail = true;
7258 int index = (i + seq) % lo->ldo_mirror_count;
7260 if (lo->ldo_mirrors[index].lme_stale) {
7261 CDEBUG(D_LAYOUT, DFID": mirror %d stale\n",
7262 PFID(lod_object_fid(lo)), index);
7266 /* 2nd pick is for the primary mirror containing unavail OST */
7267 if (lo->ldo_mirrors[index].lme_primary && second_pick < 0)
7268 second_pick = index;
7270 /* 3rd pick is for non-primary mirror containing unavail OST */
7271 if (second_pick < 0 && third_pick < 0)
7275 * we found a non-primary 1st pick, we'd like to find a
7276 * potential pirmary mirror.
7278 if (picked >= 0 && !lo->ldo_mirrors[index].lme_primary)
7281 /* check the availability of OSTs */
7282 lod_foreach_mirror_comp(lod_comp, lo, index) {
7283 if (!lod_comp_inited(lod_comp) || !lod_comp->llc_stripe)
7286 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
7287 struct dt_object *dt = lod_comp->llc_stripe[j];
7289 rc = lod_check_ost_avail(env, lo, dt, index);
7296 } /* for all dt object in one component */
7299 } /* for all components in a mirror */
7302 * the OSTs where allocated objects locates in the components
7303 * of the mirror are available.
7308 /* this mirror has all OSTs available */
7312 * primary with all OSTs are available, this is the perfect
7315 if (lo->ldo_mirrors[index].lme_primary)
7317 } /* for all mirrors */
7319 /* failed to pick a sound mirror, lower our expectation */
7321 picked = second_pick;
7323 picked = third_pick;
7330 static int lod_prepare_resync_mirror(const struct lu_env *env,
7331 struct lod_object *lo,
7334 struct lod_thread_info *info = lod_env_info(env);
7335 struct lod_layout_component *lod_comp;
7336 bool neg = !!(MIRROR_ID_NEG & mirror_id);
7339 mirror_id &= ~MIRROR_ID_NEG;
7341 for (i = 0; i < lo->ldo_mirror_count; i++) {
7342 if ((!neg && lo->ldo_mirrors[i].lme_id != mirror_id) ||
7343 (neg && lo->ldo_mirrors[i].lme_id == mirror_id))
7346 lod_foreach_mirror_comp(lod_comp, lo, i) {
7347 if (lod_comp_inited(lod_comp))
7350 info->lti_comp_idx[info->lti_count++] =
7351 lod_comp_index(lo, lod_comp);
7359 * figure out the components should be instantiated for resync.
7361 static int lod_prepare_resync(const struct lu_env *env, struct lod_object *lo,
7362 struct lu_extent *extent)
7364 struct lod_thread_info *info = lod_env_info(env);
7365 struct lod_layout_component *lod_comp;
7366 unsigned int need_sync = 0;
7370 DFID": instantiate all stale components in "DEXT"\n",
7371 PFID(lod_object_fid(lo)), PEXT(extent));
7374 * instantiate all components within this extent, even non-stale
7377 for (i = 0; i < lo->ldo_mirror_count; i++) {
7378 if (!lo->ldo_mirrors[i].lme_stale)
7381 lod_foreach_mirror_comp(lod_comp, lo, i) {
7382 if (!lu_extent_is_overlapped(extent,
7383 &lod_comp->llc_extent))
7388 if (lod_comp_inited(lod_comp))
7391 CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
7392 i, lod_comp_index(lo, lod_comp));
7393 info->lti_comp_idx[info->lti_count++] =
7394 lod_comp_index(lo, lod_comp);
7398 return need_sync ? 0 : -EALREADY;
7401 static int lod_declare_update_rdonly(const struct lu_env *env,
7402 struct lod_object *lo, struct md_layout_change *mlc,
7405 struct lod_thread_info *info = lod_env_info(env);
7406 struct lu_attr *layout_attr = &info->lti_layout_attr;
7407 struct lod_layout_component *lod_comp;
7408 struct lu_extent extent = { 0 };
7412 LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
7413 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7414 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7415 LASSERT(lo->ldo_mirror_count > 0);
7417 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7418 struct layout_intent *layout = mlc->mlc_intent;
7419 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7422 extent = layout->li_extent;
7423 CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
7424 PFID(lod_object_fid(lo)), PEXT(&extent));
7426 picked = lod_primary_pick(env, lo, &extent);
7430 CDEBUG(D_LAYOUT, DFID": picked mirror id %u as primary\n",
7431 PFID(lod_object_fid(lo)),
7432 lo->ldo_mirrors[picked].lme_id);
7434 /* Update extents of primary before staling */
7435 rc = lod_declare_update_extents(env, lo, &extent, th, picked,
7440 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
7442 * trunc transfers [0, size) in the intent extent, we'd
7443 * stale components overlapping [size, eof).
7445 extent.e_start = extent.e_end;
7446 extent.e_end = OBD_OBJECT_EOF;
7449 /* stale overlapping components from other mirrors */
7450 rc = lod_stale_components(env, lo, picked, &extent, th);
7454 /* restore truncate intent extent */
7455 if (layout->li_opc == LAYOUT_INTENT_TRUNC)
7456 extent.e_end = extent.e_start;
7458 /* instantiate components for the picked mirror, start from 0 */
7461 lod_foreach_mirror_comp(lod_comp, lo, picked) {
7462 if (!lu_extent_is_overlapped(&extent,
7463 &lod_comp->llc_extent))
7466 if (!lod_is_instantiation_needed(lod_comp))
7469 info->lti_comp_idx[info->lti_count++] =
7470 lod_comp_index(lo, lod_comp);
7473 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7474 } else { /* MD_LAYOUT_RESYNC */
7478 * could contain multiple non-stale mirrors, so we need to
7479 * prep uninited all components assuming any non-stale mirror
7480 * could be picked as the primary mirror.
7482 if (mlc->mlc_mirror_id == 0) {
7484 for (i = 0; i < lo->ldo_mirror_count; i++) {
7485 if (lo->ldo_mirrors[i].lme_stale)
7488 lod_foreach_mirror_comp(lod_comp, lo, i) {
7489 if (!lod_comp_inited(lod_comp))
7493 lod_comp->llc_extent.e_end)
7495 lod_comp->llc_extent.e_end;
7498 rc = lod_prepare_resync(env, lo, &extent);
7502 /* mirror write, try to init its all components */
7503 rc = lod_prepare_resync_mirror(env, lo,
7504 mlc->mlc_mirror_id);
7509 /* change the file state to SYNC_PENDING */
7510 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7513 /* Reset the layout version once it's becoming too large.
7514 * This way it can make sure that the layout version is
7515 * monotonously increased in this writing era. */
7516 lod_obj_inc_layout_gen(lo);
7517 if (lo->ldo_layout_gen > (LCME_ID_MAX >> 1)) {
7518 __u32 layout_version;
7520 get_random_bytes(&layout_version, sizeof(layout_version));
7521 lo->ldo_layout_gen = layout_version & 0xffff;
7524 rc = lod_declare_instantiate_components(env, lo, th);
7528 layout_attr->la_valid = LA_LAYOUT_VERSION;
7529 layout_attr->la_layout_version = 0; /* set current version */
7530 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7531 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7532 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7538 lod_striping_free(env, lo);
7542 static int lod_declare_update_write_pending(const struct lu_env *env,
7543 struct lod_object *lo, struct md_layout_change *mlc,
7546 struct lod_thread_info *info = lod_env_info(env);
7547 struct lu_attr *layout_attr = &info->lti_layout_attr;
7548 struct lod_layout_component *lod_comp;
7549 struct lu_extent extent = { 0 };
7555 LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
7556 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7557 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7559 /* look for the primary mirror */
7560 for (i = 0; i < lo->ldo_mirror_count; i++) {
7561 if (lo->ldo_mirrors[i].lme_stale)
7564 LASSERTF(primary < 0, DFID " has multiple primary: %u / %u",
7565 PFID(lod_object_fid(lo)),
7566 lo->ldo_mirrors[i].lme_id,
7567 lo->ldo_mirrors[primary].lme_id);
7572 CERROR(DFID ": doesn't have a primary mirror\n",
7573 PFID(lod_object_fid(lo)));
7574 GOTO(out, rc = -ENODATA);
7577 CDEBUG(D_LAYOUT, DFID": found primary %u\n",
7578 PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
7580 LASSERT(!lo->ldo_mirrors[primary].lme_stale);
7582 /* for LAYOUT_WRITE opc, it has to do the following operations:
7583 * 1. stale overlapping componets from stale mirrors;
7584 * 2. instantiate components of the primary mirror;
7585 * 3. transfter layout version to all objects of the primary;
7587 * for LAYOUT_RESYNC opc, it will do:
7588 * 1. instantiate components of all stale mirrors;
7589 * 2. transfer layout version to all objects to close write era. */
7591 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7592 struct layout_intent *layout = mlc->mlc_intent;
7593 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7595 LASSERT(mlc->mlc_intent != NULL);
7597 extent = mlc->mlc_intent->li_extent;
7599 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
7600 PFID(lod_object_fid(lo)), PEXT(&extent));
7602 /* 1. Update extents of primary before staling */
7603 rc = lod_declare_update_extents(env, lo, &extent, th, primary,
7608 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
7610 * trunc transfers [0, size) in the intent extent, we'd
7611 * stale components overlapping [size, eof).
7613 extent.e_start = extent.e_end;
7614 extent.e_end = OBD_OBJECT_EOF;
7617 /* 2. stale overlapping components */
7618 rc = lod_stale_components(env, lo, primary, &extent, th);
7622 /* 3. find the components which need instantiating.
7623 * instantiate [0, mlc->mlc_intent->e_end) */
7625 /* restore truncate intent extent */
7626 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC)
7627 extent.e_end = extent.e_start;
7630 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7631 if (!lu_extent_is_overlapped(&extent,
7632 &lod_comp->llc_extent))
7635 if (!lod_is_instantiation_needed(lod_comp))
7638 CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
7639 primary, lod_comp_index(lo, lod_comp));
7640 info->lti_comp_idx[info->lti_count++] =
7641 lod_comp_index(lo, lod_comp);
7643 } else { /* MD_LAYOUT_RESYNC */
7644 if (mlc->mlc_mirror_id == 0) {
7646 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7647 if (!lod_comp_inited(lod_comp))
7650 extent.e_end = lod_comp->llc_extent.e_end;
7653 rc = lod_prepare_resync(env, lo, &extent);
7657 /* mirror write, try to init its all components */
7658 rc = lod_prepare_resync_mirror(env, lo,
7659 mlc->mlc_mirror_id);
7664 /* change the file state to SYNC_PENDING */
7665 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7668 rc = lod_declare_instantiate_components(env, lo, th);
7672 /* 3. transfer layout version to OST objects.
7673 * transfer new layout version to OST objects so that stale writes
7674 * can be denied. It also ends an era of writing by setting
7675 * LU_LAYOUT_RESYNC. Normal client can never use this bit to
7676 * send write RPC; only resync RPCs could do it. */
7677 layout_attr->la_valid = LA_LAYOUT_VERSION;
7678 layout_attr->la_layout_version = 0; /* set current version */
7679 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7680 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7681 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7685 lod_obj_inc_layout_gen(lo);
7688 lod_striping_free(env, lo);
7692 static int lod_declare_update_sync_pending(const struct lu_env *env,
7693 struct lod_object *lo, struct md_layout_change *mlc,
7696 struct lod_thread_info *info = lod_env_info(env);
7697 unsigned sync_components = 0;
7698 unsigned resync_components = 0;
7703 LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
7704 LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
7705 mlc->mlc_opc == MD_LAYOUT_WRITE);
7707 CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
7708 PFID(lod_object_fid(lo)), mlc->mlc_opc);
7710 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7711 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
7712 PFID(lod_object_fid(lo)));
7714 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7715 return lod_declare_update_write_pending(env, lo, mlc, th);
7718 /* MD_LAYOUT_RESYNC_DONE */
7720 for (i = 0; i < lo->ldo_comp_cnt; i++) {
7721 struct lod_layout_component *lod_comp;
7724 lod_comp = &lo->ldo_comp_entries[i];
7726 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
7731 for (j = 0; j < mlc->mlc_resync_count; j++) {
7732 if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
7735 mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
7736 lod_comp->llc_flags &= ~LCME_FL_STALE;
7737 resync_components++;
7743 for (i = 0; i < mlc->mlc_resync_count; i++) {
7744 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
7747 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
7748 "or already synced\n", PFID(lod_object_fid(lo)),
7749 mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
7750 GOTO(out, rc = -EINVAL);
7753 if (!sync_components || (mlc->mlc_resync_count && !resync_components)) {
7754 CDEBUG(D_LAYOUT, DFID": no mirror in sync\n",
7755 PFID(lod_object_fid(lo)));
7757 /* tend to return an error code here to prevent
7758 * the MDT from setting SoM attribute */
7759 GOTO(out, rc = -EINVAL);
7762 CDEBUG(D_LAYOUT, DFID": synced %u resynced %u/%zu components\n",
7763 PFID(lod_object_fid(lo)),
7764 sync_components, resync_components, mlc->mlc_resync_count);
7766 lo->ldo_flr_state = LCM_FL_RDONLY;
7767 lod_obj_inc_layout_gen(lo);
7769 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
7770 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
7771 &info->lti_buf, XATTR_NAME_LOV, 0, th);
7776 lod_striping_free(env, lo);
7780 static int lod_declare_layout_change(const struct lu_env *env,
7781 struct dt_object *dt, struct md_layout_change *mlc,
7784 struct lod_thread_info *info = lod_env_info(env);
7785 struct lod_object *lo = lod_dt_obj(dt);
7789 if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
7790 dt_object_remote(dt_object_child(dt)))
7793 rc = lod_striping_load(env, lo);
7797 LASSERT(lo->ldo_comp_cnt > 0);
7799 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
7803 switch (lo->ldo_flr_state) {
7805 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
7809 rc = lod_declare_update_rdonly(env, lo, mlc, th);
7811 case LCM_FL_WRITE_PENDING:
7812 rc = lod_declare_update_write_pending(env, lo, mlc, th);
7814 case LCM_FL_SYNC_PENDING:
7815 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
7826 * Instantiate layout component objects which covers the intent write offset.
7828 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
7829 struct md_layout_change *mlc, struct thandle *th)
7831 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
7832 struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
7833 struct lod_object *lo = lod_dt_obj(dt);
7836 rc = lod_striped_create(env, dt, attr, NULL, th);
7837 if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
7838 layout_attr->la_layout_version |= lo->ldo_layout_gen;
7839 rc = lod_attr_set(env, dt, layout_attr, th);
7845 struct dt_object_operations lod_obj_ops = {
7846 .do_read_lock = lod_read_lock,
7847 .do_write_lock = lod_write_lock,
7848 .do_read_unlock = lod_read_unlock,
7849 .do_write_unlock = lod_write_unlock,
7850 .do_write_locked = lod_write_locked,
7851 .do_attr_get = lod_attr_get,
7852 .do_declare_attr_set = lod_declare_attr_set,
7853 .do_attr_set = lod_attr_set,
7854 .do_xattr_get = lod_xattr_get,
7855 .do_declare_xattr_set = lod_declare_xattr_set,
7856 .do_xattr_set = lod_xattr_set,
7857 .do_declare_xattr_del = lod_declare_xattr_del,
7858 .do_xattr_del = lod_xattr_del,
7859 .do_xattr_list = lod_xattr_list,
7860 .do_ah_init = lod_ah_init,
7861 .do_declare_create = lod_declare_create,
7862 .do_create = lod_create,
7863 .do_declare_destroy = lod_declare_destroy,
7864 .do_destroy = lod_destroy,
7865 .do_index_try = lod_index_try,
7866 .do_declare_ref_add = lod_declare_ref_add,
7867 .do_ref_add = lod_ref_add,
7868 .do_declare_ref_del = lod_declare_ref_del,
7869 .do_ref_del = lod_ref_del,
7870 .do_object_sync = lod_object_sync,
7871 .do_object_lock = lod_object_lock,
7872 .do_object_unlock = lod_object_unlock,
7873 .do_invalidate = lod_invalidate,
7874 .do_declare_layout_change = lod_declare_layout_change,
7875 .do_layout_change = lod_layout_change,
7879 * Implementation of dt_body_operations::dbo_read.
7881 * \see dt_body_operations::dbo_read() in the API description for details.
7883 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
7884 struct lu_buf *buf, loff_t *pos)
7886 struct dt_object *next = dt_object_child(dt);
7888 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
7889 S_ISLNK(dt->do_lu.lo_header->loh_attr));
7890 return next->do_body_ops->dbo_read(env, next, buf, pos);
7894 * Implementation of dt_body_operations::dbo_declare_write.
7896 * \see dt_body_operations::dbo_declare_write() in the API description
7899 static ssize_t lod_declare_write(const struct lu_env *env,
7900 struct dt_object *dt,
7901 const struct lu_buf *buf, loff_t pos,
7904 return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
7908 * Implementation of dt_body_operations::dbo_write.
7910 * \see dt_body_operations::dbo_write() in the API description for details.
7912 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
7913 const struct lu_buf *buf, loff_t *pos,
7916 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
7917 S_ISLNK(dt->do_lu.lo_header->loh_attr));
7918 return lod_sub_write(env, dt_object_child(dt), buf, pos, th);
7921 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
7922 __u64 start, __u64 end, struct thandle *th)
7924 if (dt_object_remote(dt))
7927 return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
7930 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
7931 __u64 start, __u64 end, struct thandle *th)
7933 if (dt_object_remote(dt))
7936 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
7937 return lod_sub_punch(env, dt_object_child(dt), start, end, th);
7941 * different type of files use the same body_ops because object may be created
7942 * in OUT, where there is no chance to set correct body_ops for each type, so
7943 * body_ops themselves will check file type inside, see lod_read/write/punch for
7946 const struct dt_body_operations lod_body_ops = {
7947 .dbo_read = lod_read,
7948 .dbo_declare_write = lod_declare_write,
7949 .dbo_write = lod_write,
7950 .dbo_declare_punch = lod_declare_punch,
7951 .dbo_punch = lod_punch,
7955 * Implementation of lu_object_operations::loo_object_init.
7957 * The function determines the type and the index of the target device using
7958 * sequence of the object's FID. Then passes control down to the
7959 * corresponding device:
7960 * OSD for the local objects, OSP for remote
7962 * \see lu_object_operations::loo_object_init() in the API description
7965 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
7966 const struct lu_object_conf *conf)
7968 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
7969 struct lu_device *cdev = NULL;
7970 struct lu_object *cobj;
7971 struct lod_tgt_descs *ltd = NULL;
7972 struct lod_tgt_desc *tgt;
7974 int type = LU_SEQ_RANGE_ANY;
7978 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
7982 if (type == LU_SEQ_RANGE_MDT &&
7983 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
7984 cdev = &lod->lod_child->dd_lu_dev;
7985 } else if (type == LU_SEQ_RANGE_MDT) {
7986 ltd = &lod->lod_mdt_descs;
7988 } else if (type == LU_SEQ_RANGE_OST) {
7989 ltd = &lod->lod_ost_descs;
7996 if (ltd->ltd_tgts_size > idx &&
7997 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
7998 tgt = LTD_TGT(ltd, idx);
8000 LASSERT(tgt != NULL);
8001 LASSERT(tgt->ltd_tgt != NULL);
8003 cdev = &(tgt->ltd_tgt->dd_lu_dev);
8005 lod_putref(lod, ltd);
8008 if (unlikely(cdev == NULL))
8011 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
8012 if (unlikely(cobj == NULL))
8015 lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
8017 lu_object_add(lo, cobj);
8024 * Alloc cached foreign LOV
8026 * \param[in] lo object
8027 * \param[in] size size of foreign LOV
8029 * \retval 0 on success
8030 * \retval negative if failed
8032 int lod_alloc_foreign_lov(struct lod_object *lo, size_t size)
8034 OBD_ALLOC_LARGE(lo->ldo_foreign_lov, size);
8035 if (lo->ldo_foreign_lov == NULL)
8037 lo->ldo_foreign_lov_size = size;
8038 lo->ldo_is_foreign = 1;
8044 * Free cached foreign LOV
8046 * \param[in] lo object
8048 void lod_free_foreign_lov(struct lod_object *lo)
8050 if (lo->ldo_foreign_lov != NULL)
8051 OBD_FREE_LARGE(lo->ldo_foreign_lov, lo->ldo_foreign_lov_size);
8052 lo->ldo_foreign_lov = NULL;
8053 lo->ldo_foreign_lov_size = 0;
8054 lo->ldo_is_foreign = 0;
8059 * Free cached foreign LMV
8061 * \param[in] lo object
8063 void lod_free_foreign_lmv(struct lod_object *lo)
8065 if (lo->ldo_foreign_lmv != NULL)
8066 OBD_FREE_LARGE(lo->ldo_foreign_lmv, lo->ldo_foreign_lmv_size);
8067 lo->ldo_foreign_lmv = NULL;
8068 lo->ldo_foreign_lmv_size = 0;
8069 lo->ldo_dir_is_foreign = 0;
8074 * Release resources associated with striping.
8076 * If the object is striped (regular or directory), then release
8077 * the stripe objects references and free the ldo_stripe array.
8079 * \param[in] env execution environment
8080 * \param[in] lo object
8082 void lod_striping_free_nolock(const struct lu_env *env, struct lod_object *lo)
8084 struct lod_layout_component *lod_comp;
8087 if (unlikely(lo->ldo_is_foreign)) {
8088 lod_free_foreign_lov(lo);
8089 lo->ldo_comp_cached = 0;
8090 } else if (unlikely(lo->ldo_dir_is_foreign)) {
8091 lod_free_foreign_lmv(lo);
8092 lo->ldo_dir_stripe_loaded = 0;
8093 } else if (lo->ldo_stripe != NULL) {
8094 LASSERT(lo->ldo_comp_entries == NULL);
8095 LASSERT(lo->ldo_dir_stripes_allocated > 0);
8097 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8098 if (lo->ldo_stripe[i])
8099 dt_object_put(env, lo->ldo_stripe[i]);
8102 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
8103 OBD_FREE(lo->ldo_stripe, j);
8104 lo->ldo_stripe = NULL;
8105 lo->ldo_dir_stripes_allocated = 0;
8106 lo->ldo_dir_stripe_loaded = 0;
8107 lo->ldo_dir_stripe_count = 0;
8108 } else if (lo->ldo_comp_entries != NULL) {
8109 for (i = 0; i < lo->ldo_comp_cnt; i++) {
8110 /* free lod_layout_component::llc_stripe array */
8111 lod_comp = &lo->ldo_comp_entries[i];
8113 if (lod_comp->llc_stripe == NULL)
8115 LASSERT(lod_comp->llc_stripes_allocated != 0);
8116 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
8117 if (lod_comp->llc_stripe[j] != NULL)
8119 &lod_comp->llc_stripe[j]->do_lu);
8121 OBD_FREE(lod_comp->llc_stripe,
8122 sizeof(struct dt_object *) *
8123 lod_comp->llc_stripes_allocated);
8124 lod_comp->llc_stripe = NULL;
8125 OBD_FREE(lod_comp->llc_ost_indices,
8127 lod_comp->llc_stripes_allocated);
8128 lod_comp->llc_ost_indices = NULL;
8129 lod_comp->llc_stripes_allocated = 0;
8131 lod_free_comp_entries(lo);
8132 lo->ldo_comp_cached = 0;
8136 void lod_striping_free(const struct lu_env *env, struct lod_object *lo)
8138 mutex_lock(&lo->ldo_layout_mutex);
8139 lod_striping_free_nolock(env, lo);
8140 mutex_unlock(&lo->ldo_layout_mutex);
8144 * Implementation of lu_object_operations::loo_object_free.
8146 * \see lu_object_operations::loo_object_free() in the API description
8149 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
8151 struct lod_object *lo = lu2lod_obj(o);
8153 /* release all underlying object pinned */
8154 lod_striping_free(env, lo);
8156 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
8160 * Implementation of lu_object_operations::loo_object_release.
8162 * \see lu_object_operations::loo_object_release() in the API description
8165 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
8167 /* XXX: shouldn't we release everything here in case if object
8168 * creation failed before? */
8172 * Implementation of lu_object_operations::loo_object_print.
8174 * \see lu_object_operations::loo_object_print() in the API description
8177 static int lod_object_print(const struct lu_env *env, void *cookie,
8178 lu_printer_t p, const struct lu_object *l)
8180 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
8182 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
8185 struct lu_object_operations lod_lu_obj_ops = {
8186 .loo_object_init = lod_object_init,
8187 .loo_object_free = lod_object_free,
8188 .loo_object_release = lod_object_release,
8189 .loo_object_print = lod_object_print,