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 = scnprintf(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 BUILD_BUG_ON(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.
2976 * Please update allowed_lustre_lov macro if $field groks more values
2979 * \param[in] env execution environment
2980 * \param[in] dt dt_object to be modified
2981 * \param[in] op operation string, like "set.flags"
2982 * \param[in] buf buffer contains components to be set
2983 * \parem[in] th thandle
2985 * \retval 0 on success
2986 * \retval negative errno on failure
2988 static int lod_declare_layout_set(const struct lu_env *env,
2989 struct dt_object *dt,
2990 char *op, const struct lu_buf *buf,
2993 struct lod_layout_component *lod_comp;
2994 struct lod_thread_info *info = lod_env_info(env);
2995 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2996 struct lod_object *lo = lod_dt_obj(dt);
2997 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
3000 bool changed = false;
3003 /* Please update allowed_lustre_lov macro if op
3004 * groks more values in the future
3006 if (strcmp(op, "set.flags") != 0) {
3007 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
3008 lod2obd(d)->obd_name, op);
3012 magic = comp_v1->lcm_magic;
3013 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3014 lustre_swab_lov_comp_md_v1(comp_v1);
3015 magic = comp_v1->lcm_magic;
3018 if (magic != LOV_USER_MAGIC_COMP_V1)
3021 if (comp_v1->lcm_entry_count == 0) {
3022 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
3023 lod2obd(d)->obd_name);
3027 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3028 __u32 id = comp_v1->lcm_entries[i].lcme_id;
3029 __u32 flags = comp_v1->lcm_entries[i].lcme_flags;
3030 __u32 mirror_flag = flags & LCME_MIRROR_FLAGS;
3031 __u16 mirror_id = mirror_id_of(id);
3032 bool neg = flags & LCME_FL_NEG;
3034 if (flags & LCME_FL_INIT) {
3036 lod_striping_free(env, lo);
3040 flags &= ~(LCME_MIRROR_FLAGS | LCME_FL_NEG);
3041 for (j = 0; j < lo->ldo_comp_cnt; j++) {
3042 lod_comp = &lo->ldo_comp_entries[j];
3044 /* lfs only put one flag in each entry */
3045 if ((flags && id != lod_comp->llc_id) ||
3046 (mirror_flag && mirror_id !=
3047 mirror_id_of(lod_comp->llc_id)))
3052 lod_comp->llc_flags &= ~flags;
3054 lod_comp->llc_flags &= ~mirror_flag;
3057 if ((flags & LCME_FL_STALE) &&
3058 lod_last_non_stale_mirror(mirror_id,
3061 lod_comp->llc_flags |= flags;
3064 lod_comp->llc_flags |= mirror_flag;
3065 if (mirror_flag & LCME_FL_NOSYNC)
3066 lod_comp->llc_timestamp =
3067 ktime_get_real_seconds();
3075 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
3076 lod2obd(d)->obd_name);
3080 lod_obj_inc_layout_gen(lo);
3082 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
3083 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), &info->lti_buf,
3084 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3089 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
3090 * and the xattr value is a unique component ID or a special lcme_id.
3092 * \param[in] env execution environment
3093 * \param[in] dt dt_object to be operated on
3094 * \param[in] buf buffer contains component ID or lcme_id
3095 * \parem[in] th thandle
3097 * \retval 0 on success
3098 * \retval negative errno on failure
3100 static int lod_declare_layout_del(const struct lu_env *env,
3101 struct dt_object *dt,
3102 const struct lu_buf *buf,
3105 struct lod_thread_info *info = lod_env_info(env);
3106 struct dt_object *next = dt_object_child(dt);
3107 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3108 struct lod_object *lo = lod_dt_obj(dt);
3109 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3110 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
3111 __u32 magic, id, flags, neg_flags = 0;
3115 LASSERT(lo->ldo_is_composite);
3117 if (lo->ldo_flr_state != LCM_FL_NONE)
3120 magic = comp_v1->lcm_magic;
3121 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3122 lustre_swab_lov_comp_md_v1(comp_v1);
3123 magic = comp_v1->lcm_magic;
3126 if (magic != LOV_USER_MAGIC_COMP_V1)
3129 id = comp_v1->lcm_entries[0].lcme_id;
3130 flags = comp_v1->lcm_entries[0].lcme_flags;
3132 if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
3133 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
3134 lod2obd(d)->obd_name, id, flags);
3138 if (id != LCME_ID_INVAL && flags != 0) {
3139 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
3140 lod2obd(d)->obd_name);
3144 if (id == LCME_ID_INVAL && !flags) {
3145 CDEBUG(D_LAYOUT, "%s: no id or flags specified.\n",
3146 lod2obd(d)->obd_name);
3150 if (flags & LCME_FL_NEG) {
3151 neg_flags = flags & ~LCME_FL_NEG;
3155 left = lo->ldo_comp_cnt;
3159 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3160 struct lod_layout_component *lod_comp;
3162 lod_comp = &lo->ldo_comp_entries[i];
3164 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
3166 else if (flags && !(flags & lod_comp->llc_flags))
3168 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
3171 if (left != (i + 1)) {
3172 CDEBUG(D_LAYOUT, "%s: this deletion will create "
3173 "a hole.\n", lod2obd(d)->obd_name);
3178 /* Mark the component as deleted */
3179 lod_comp->llc_id = LCME_ID_INVAL;
3181 /* Not instantiated component */
3182 if (lod_comp->llc_stripe == NULL)
3185 LASSERT(lod_comp->llc_stripe_count > 0);
3186 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
3187 struct dt_object *obj = lod_comp->llc_stripe[j];
3191 rc = lod_sub_declare_destroy(env, obj, th);
3197 LASSERTF(left >= 0, "left = %d\n", left);
3198 if (left == lo->ldo_comp_cnt) {
3199 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
3200 lod2obd(d)->obd_name, id);
3204 memset(attr, 0, sizeof(*attr));
3205 attr->la_valid = LA_SIZE;
3206 rc = lod_sub_declare_attr_set(env, next, attr, th);
3211 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
3212 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
3213 XATTR_NAME_LOV, 0, th);
3215 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
3222 * Declare layout add/set/del operations issued by special xattr names:
3224 * XATTR_LUSTRE_LOV.add add component(s) to existing file
3225 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
3226 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
3228 * \param[in] env execution environment
3229 * \param[in] dt object
3230 * \param[in] name name of xattr
3231 * \param[in] buf lu_buf contains xattr value
3232 * \param[in] th transaction handle
3234 * \retval 0 on success
3235 * \retval negative if failed
3237 static int lod_declare_modify_layout(const struct lu_env *env,
3238 struct dt_object *dt,
3240 const struct lu_buf *buf,
3243 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3244 struct lod_object *lo = lod_dt_obj(dt);
3246 int rc, len = strlen(XATTR_LUSTRE_LOV);
3249 LASSERT(dt_object_exists(dt));
3251 if (strlen(name) <= len || name[len] != '.') {
3252 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
3253 lod2obd(d)->obd_name, name);
3258 rc = lod_striping_load(env, lo);
3262 /* the layout to be modified must be a composite layout */
3263 if (!lo->ldo_is_composite) {
3264 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
3265 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3266 GOTO(unlock, rc = -EINVAL);
3269 op = (char *)name + len;
3270 if (strcmp(op, "add") == 0) {
3271 rc = lod_declare_layout_add(env, dt, buf, th);
3272 } else if (strcmp(op, "del") == 0) {
3273 rc = lod_declare_layout_del(env, dt, buf, th);
3274 } else if (strncmp(op, "set", strlen("set")) == 0) {
3275 rc = lod_declare_layout_set(env, dt, op, buf, th);
3277 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
3278 lod2obd(d)->obd_name, name);
3279 GOTO(unlock, rc = -ENOTSUPP);
3283 lod_striping_free(env, lo);
3289 * Convert a plain file lov_mds_md to a composite layout.
3291 * \param[in,out] info the thread info::lti_ea_store buffer contains little
3292 * endian plain file layout
3294 * \retval 0 on success, <0 on failure
3296 static int lod_layout_convert(struct lod_thread_info *info)
3298 struct lov_mds_md *lmm = info->lti_ea_store;
3299 struct lov_mds_md *lmm_save;
3300 struct lov_comp_md_v1 *lcm;
3301 struct lov_comp_md_entry_v1 *lcme;
3307 /* realloc buffer to a composite layout which contains one component */
3308 blob_size = lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
3309 le32_to_cpu(lmm->lmm_magic));
3310 size = sizeof(*lcm) + sizeof(*lcme) + blob_size;
3312 OBD_ALLOC_LARGE(lmm_save, blob_size);
3314 GOTO(out, rc = -ENOMEM);
3316 memcpy(lmm_save, lmm, blob_size);
3318 if (info->lti_ea_store_size < size) {
3319 rc = lod_ea_store_resize(info, size);
3324 lcm = info->lti_ea_store;
3325 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
3326 lcm->lcm_size = cpu_to_le32(size);
3327 lcm->lcm_layout_gen = cpu_to_le32(le16_to_cpu(
3328 lmm_save->lmm_layout_gen));
3329 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
3330 lcm->lcm_entry_count = cpu_to_le16(1);
3331 lcm->lcm_mirror_count = 0;
3333 lcme = &lcm->lcm_entries[0];
3334 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
3335 lcme->lcme_extent.e_start = 0;
3336 lcme->lcme_extent.e_end = cpu_to_le64(OBD_OBJECT_EOF);
3337 lcme->lcme_offset = cpu_to_le32(sizeof(*lcm) + sizeof(*lcme));
3338 lcme->lcme_size = cpu_to_le32(blob_size);
3340 memcpy((char *)lcm + lcme->lcme_offset, (char *)lmm_save, blob_size);
3345 OBD_FREE_LARGE(lmm_save, blob_size);
3350 * Merge layouts to form a mirrored file.
3352 static int lod_declare_layout_merge(const struct lu_env *env,
3353 struct dt_object *dt, const struct lu_buf *mbuf,
3356 struct lod_thread_info *info = lod_env_info(env);
3357 struct lu_buf *buf = &info->lti_buf;
3358 struct lod_object *lo = lod_dt_obj(dt);
3359 struct lov_comp_md_v1 *lcm;
3360 struct lov_comp_md_v1 *cur_lcm;
3361 struct lov_comp_md_v1 *merge_lcm;
3362 struct lov_comp_md_entry_v1 *lcme;
3363 struct lov_mds_md_v1 *lmm;
3366 __u16 cur_entry_count;
3367 __u16 merge_entry_count;
3369 __u16 mirror_id = 0;
3376 merge_lcm = mbuf->lb_buf;
3377 if (mbuf->lb_len < sizeof(*merge_lcm))
3380 /* must be an existing layout from disk */
3381 if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
3384 merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
3386 /* do not allow to merge two mirrored files */
3387 if (le16_to_cpu(merge_lcm->lcm_mirror_count))
3390 /* verify the target buffer */
3391 rc = lod_get_lov_ea(env, lo);
3393 RETURN(rc ? : -ENODATA);
3395 cur_lcm = info->lti_ea_store;
3396 switch (le32_to_cpu(cur_lcm->lcm_magic)) {
3399 rc = lod_layout_convert(info);
3401 case LOV_MAGIC_COMP_V1:
3411 /* info->lti_ea_store could be reallocated in lod_layout_convert() */
3412 cur_lcm = info->lti_ea_store;
3413 cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
3415 /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
3416 mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
3417 if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
3420 /* size of new layout */
3421 size = le32_to_cpu(cur_lcm->lcm_size) +
3422 le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
3424 memset(buf, 0, sizeof(*buf));
3425 lu_buf_alloc(buf, size);
3426 if (buf->lb_buf == NULL)
3430 memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
3432 offset = sizeof(*lcm) +
3433 sizeof(*lcme) * (cur_entry_count + merge_entry_count);
3434 for (i = 0; i < cur_entry_count; i++) {
3435 struct lov_comp_md_entry_v1 *cur_lcme;
3437 lcme = &lcm->lcm_entries[i];
3438 cur_lcme = &cur_lcm->lcm_entries[i];
3440 lcme->lcme_offset = cpu_to_le32(offset);
3441 memcpy((char *)lcm + offset,
3442 (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
3443 le32_to_cpu(lcme->lcme_size));
3445 offset += le32_to_cpu(lcme->lcme_size);
3447 if (mirror_count == 1 &&
3448 mirror_id_of(le32_to_cpu(lcme->lcme_id)) == 0) {
3449 /* Add mirror from a non-flr file, create new mirror ID.
3450 * Otherwise, keep existing mirror's component ID, used
3451 * for mirror extension.
3453 id = pflr_id(1, i + 1);
3454 lcme->lcme_id = cpu_to_le32(id);
3457 id = MAX(le32_to_cpu(lcme->lcme_id), id);
3460 mirror_id = mirror_id_of(id) + 1;
3462 /* check if first entry in new layout is DOM */
3463 lmm = (struct lov_mds_md_v1 *)((char *)merge_lcm +
3464 merge_lcm->lcm_entries[0].lcme_offset);
3465 merge_has_dom = lov_pattern(le32_to_cpu(lmm->lmm_pattern)) ==
3468 for (i = 0; i < merge_entry_count; i++) {
3469 struct lov_comp_md_entry_v1 *merge_lcme;
3471 merge_lcme = &merge_lcm->lcm_entries[i];
3472 lcme = &lcm->lcm_entries[cur_entry_count + i];
3474 *lcme = *merge_lcme;
3475 lcme->lcme_offset = cpu_to_le32(offset);
3476 if (merge_has_dom && i == 0)
3477 lcme->lcme_flags |= cpu_to_le32(LCME_FL_STALE);
3479 id = pflr_id(mirror_id, i + 1);
3480 lcme->lcme_id = cpu_to_le32(id);
3482 memcpy((char *)lcm + offset,
3483 (char *)merge_lcm + le32_to_cpu(merge_lcme->lcme_offset),
3484 le32_to_cpu(lcme->lcme_size));
3486 offset += le32_to_cpu(lcme->lcme_size);
3489 /* fixup layout information */
3490 lod_obj_inc_layout_gen(lo);
3491 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3492 lcm->lcm_size = cpu_to_le32(size);
3493 lcm->lcm_entry_count = cpu_to_le16(cur_entry_count + merge_entry_count);
3494 lcm->lcm_mirror_count = cpu_to_le16(mirror_count);
3495 if ((le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK) == LCM_FL_NONE)
3496 lcm->lcm_flags = cpu_to_le32(LCM_FL_RDONLY);
3498 rc = lod_striping_reload(env, lo, buf);
3502 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), buf,
3503 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3511 * Split layouts, just set the LOVEA with the layout from mbuf.
3513 static int lod_declare_layout_split(const struct lu_env *env,
3514 struct dt_object *dt, const struct lu_buf *mbuf,
3517 struct lod_object *lo = lod_dt_obj(dt);
3518 struct lov_comp_md_v1 *lcm = mbuf->lb_buf;
3522 lod_obj_inc_layout_gen(lo);
3523 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3525 rc = lod_striping_reload(env, lo, mbuf);
3529 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), mbuf,
3530 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3535 * Implementation of dt_object_operations::do_declare_xattr_set.
3537 * \see dt_object_operations::do_declare_xattr_set() in the API description
3540 * the extension to the API:
3541 * - declaring LOVEA requests striping creation
3542 * - LU_XATTR_REPLACE means layout swap
3544 static int lod_declare_xattr_set(const struct lu_env *env,
3545 struct dt_object *dt,
3546 const struct lu_buf *buf,
3547 const char *name, int fl,
3550 struct dt_object *next = dt_object_child(dt);
3551 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3556 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
3557 if ((S_ISREG(mode) || mode == 0) &&
3558 !(fl & (LU_XATTR_REPLACE | LU_XATTR_MERGE | LU_XATTR_SPLIT)) &&
3559 (strcmp(name, XATTR_NAME_LOV) == 0 ||
3560 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
3562 * this is a request to create object's striping.
3564 * allow to declare predefined striping on a new (!mode) object
3565 * which is supposed to be replay of regular file creation
3566 * (when LOV setting is declared)
3568 * LU_XATTR_REPLACE is set to indicate a layout swap
3570 if (dt_object_exists(dt)) {
3571 rc = dt_attr_get(env, next, attr);
3575 memset(attr, 0, sizeof(*attr));
3576 attr->la_valid = LA_TYPE | LA_MODE;
3577 attr->la_mode = S_IFREG;
3579 rc = lod_declare_striped_create(env, dt, attr, buf, th);
3580 } else if (fl & LU_XATTR_MERGE) {
3581 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3582 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3583 rc = lod_declare_layout_merge(env, dt, buf, th);
3584 } else if (fl & LU_XATTR_SPLIT) {
3585 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3586 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3587 rc = lod_declare_layout_split(env, dt, buf, th);
3588 } else if (S_ISREG(mode) &&
3589 strlen(name) >= sizeof(XATTR_LUSTRE_LOV) + 3 &&
3590 allowed_lustre_lov(name)) {
3592 * this is a request to modify object's striping.
3593 * add/set/del component(s).
3595 if (!dt_object_exists(dt))
3598 rc = lod_declare_modify_layout(env, dt, name, buf, th);
3599 } else if (strncmp(name, XATTR_NAME_LMV, strlen(XATTR_NAME_LMV)) == 0 &&
3600 strlen(name) > strlen(XATTR_NAME_LMV)) {
3601 const char *op = name + strlen(XATTR_NAME_LMV);
3604 if (strcmp(op, ".add") == 0)
3605 rc = lod_dir_declare_layout_add(env, dt, buf, th);
3606 else if (strcmp(op, ".del") == 0)
3607 rc = lod_dir_declare_layout_delete(env, dt, buf, th);
3608 else if (strcmp(op, ".set") == 0)
3609 rc = lod_sub_declare_xattr_set(env, next, buf,
3610 XATTR_NAME_LMV, fl, th);
3613 } else if (S_ISDIR(mode)) {
3614 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
3615 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3616 rc = lod_replace_parent_fid(env, dt, buf, th, true);
3618 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
3625 * Apply xattr changes to the object.
3627 * Applies xattr changes to the object and the stripes if the latter exist.
3629 * \param[in] env execution environment
3630 * \param[in] dt object
3631 * \param[in] buf buffer pointing to the new value of xattr
3632 * \param[in] name name of xattr
3633 * \param[in] fl flags
3634 * \param[in] th transaction handle
3636 * \retval 0 on success
3637 * \retval negative if failed
3639 static int lod_xattr_set_internal(const struct lu_env *env,
3640 struct dt_object *dt,
3641 const struct lu_buf *buf,
3642 const char *name, int fl,
3645 struct dt_object *next = dt_object_child(dt);
3646 struct lod_object *lo = lod_dt_obj(dt);
3651 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3652 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3655 /* Note: Do not set LinkEA on sub-stripes, otherwise
3656 * it will confuse the fid2path process(see mdt_path_current()).
3657 * The linkEA between master and sub-stripes is set in
3658 * lod_xattr_set_lmv(). */
3659 if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
3662 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3663 if (!lo->ldo_stripe[i])
3666 if (!dt_object_exists(lo->ldo_stripe[i]))
3669 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
3679 * Delete an extended attribute.
3681 * Deletes specified xattr from the object and the stripes if the latter exist.
3683 * \param[in] env execution environment
3684 * \param[in] dt object
3685 * \param[in] name name of xattr
3686 * \param[in] th transaction handle
3688 * \retval 0 on success
3689 * \retval negative if failed
3691 static int lod_xattr_del_internal(const struct lu_env *env,
3692 struct dt_object *dt,
3693 const char *name, struct thandle *th)
3695 struct dt_object *next = dt_object_child(dt);
3696 struct lod_object *lo = lod_dt_obj(dt);
3701 rc = lod_sub_xattr_del(env, next, name, th);
3702 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3705 if (lo->ldo_dir_stripe_count == 0)
3708 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3709 LASSERT(lo->ldo_stripe[i]);
3711 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3720 * Set default striping on a directory.
3722 * Sets specified striping on a directory object unless it matches the default
3723 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3724 * EA. This striping will be used when regular file is being created in this
3727 * \param[in] env execution environment
3728 * \param[in] dt the striped object
3729 * \param[in] buf buffer with the striping
3730 * \param[in] name name of EA
3731 * \param[in] fl xattr flag (see OSD API description)
3732 * \param[in] th transaction handle
3734 * \retval 0 on success
3735 * \retval negative if failed
3737 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
3738 struct dt_object *dt,
3739 const struct lu_buf *buf,
3740 const char *name, int fl,
3743 struct lov_user_md_v1 *lum;
3744 struct lov_user_md_v3 *v3 = NULL;
3745 const char *pool_name = NULL;
3750 LASSERT(buf != NULL && buf->lb_buf != NULL);
3753 switch (lum->lmm_magic) {
3754 case LOV_USER_MAGIC_SPECIFIC:
3755 case LOV_USER_MAGIC_V3:
3757 if (v3->lmm_pool_name[0] != '\0')
3758 pool_name = v3->lmm_pool_name;
3760 case LOV_USER_MAGIC_V1:
3761 /* if { size, offset, count } = { 0, -1, 0 } and no pool
3762 * (i.e. all default values specified) then delete default
3763 * striping from dir. */
3765 "set default striping: sz %u # %u offset %d %s %s\n",
3766 (unsigned)lum->lmm_stripe_size,
3767 (unsigned)lum->lmm_stripe_count,
3768 (int)lum->lmm_stripe_offset,
3769 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
3771 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
3772 lum->lmm_stripe_count,
3773 lum->lmm_stripe_offset,
3776 case LOV_USER_MAGIC_COMP_V1:
3778 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lum;
3779 struct lov_comp_md_entry_v1 *lcme;
3782 comp_cnt = le16_to_cpu(lcm->lcm_entry_count);
3783 for (i = 0; i < comp_cnt; i++) {
3784 lcme = &lcm->lcm_entries[i];
3785 if (lcme->lcme_flags & cpu_to_le32(LCME_FL_EXTENSION)) {
3786 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_SEL);
3795 CERROR("Invalid magic %x\n", lum->lmm_magic);
3800 rc = lod_xattr_del_internal(env, dt, name, th);
3804 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3811 * Set default striping on a directory object.
3813 * Sets specified striping on a directory object unless it matches the default
3814 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3815 * EA. This striping will be used when a new directory is being created in the
3818 * \param[in] env execution environment
3819 * \param[in] dt the striped object
3820 * \param[in] buf buffer with the striping
3821 * \param[in] name name of EA
3822 * \param[in] fl xattr flag (see OSD API description)
3823 * \param[in] th transaction handle
3825 * \retval 0 on success
3826 * \retval negative if failed
3828 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
3829 struct dt_object *dt,
3830 const struct lu_buf *buf,
3831 const char *name, int fl,
3834 struct lmv_user_md_v1 *lum;
3839 LASSERT(buf != NULL && buf->lb_buf != NULL);
3843 "set default stripe_count # %u stripe_offset %d hash %u\n",
3844 le32_to_cpu(lum->lum_stripe_count),
3845 (int)le32_to_cpu(lum->lum_stripe_offset),
3846 le32_to_cpu(lum->lum_hash_type));
3848 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
3849 le32_to_cpu(lum->lum_stripe_offset)) &&
3850 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
3851 rc = lod_xattr_del_internal(env, dt, name, th);
3855 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3864 * Turn directory into a striped directory.
3866 * During replay the client sends the striping created before MDT
3867 * failure, then the layer above LOD sends this defined striping
3868 * using ->do_xattr_set(), so LOD uses this method to replay creation
3869 * of the stripes. Notice the original information for the striping
3870 * (#stripes, FIDs, etc) was transferred in declare path.
3872 * \param[in] env execution environment
3873 * \param[in] dt the striped object
3874 * \param[in] buf not used currently
3875 * \param[in] name not used currently
3876 * \param[in] fl xattr flag (see OSD API description)
3877 * \param[in] th transaction handle
3879 * \retval 0 on success
3880 * \retval negative if failed
3882 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
3883 const struct lu_buf *buf, const char *name,
3884 int fl, struct thandle *th)
3886 struct lod_object *lo = lod_dt_obj(dt);
3887 struct lod_thread_info *info = lod_env_info(env);
3888 struct lu_attr *attr = &info->lti_attr;
3889 struct dt_object_format *dof = &info->lti_format;
3890 struct lu_buf lmv_buf;
3891 struct lu_buf slave_lmv_buf;
3892 struct lmv_mds_md_v1 *lmm;
3893 struct lmv_mds_md_v1 *slave_lmm = NULL;
3894 struct dt_insert_rec *rec = &info->lti_dt_rec;
3899 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3902 /* The stripes are supposed to be allocated in declare phase,
3903 * if there are no stripes being allocated, it will skip */
3904 if (lo->ldo_dir_stripe_count == 0) {
3905 if (lo->ldo_dir_is_foreign) {
3906 rc = lod_sub_xattr_set(env, dt_object_child(dt), buf,
3907 XATTR_NAME_LMV, fl, th);
3914 rc = dt_attr_get(env, dt_object_child(dt), attr);
3918 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
3919 LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
3920 dof->dof_type = DFT_DIR;
3922 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
3925 lmm = lmv_buf.lb_buf;
3927 OBD_ALLOC_PTR(slave_lmm);
3928 if (slave_lmm == NULL)
3931 lod_prep_slave_lmv_md(slave_lmm, lmm);
3932 slave_lmv_buf.lb_buf = slave_lmm;
3933 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
3935 rec->rec_type = S_IFDIR;
3936 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3937 struct dt_object *dto = lo->ldo_stripe[i];
3938 char *stripe_name = info->lti_key;
3939 struct lu_name *sname;
3940 struct linkea_data ldata = { NULL };
3941 struct lu_buf linkea_buf;
3943 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
3947 /* fail a remote stripe creation */
3948 if (i && OBD_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_CREATE))
3951 /* if it's source stripe of migrating directory, don't create */
3952 if (!((lo->ldo_dir_hash_type & LMV_HASH_FLAG_MIGRATION) &&
3953 i >= lo->ldo_dir_migrate_offset)) {
3954 dt_write_lock(env, dto, DT_TGT_CHILD);
3955 rc = lod_sub_create(env, dto, attr, NULL, dof, th);
3957 dt_write_unlock(env, dto);
3961 rc = lod_sub_ref_add(env, dto, th);
3962 dt_write_unlock(env, dto);
3966 rec->rec_fid = lu_object_fid(&dto->do_lu);
3967 rc = lod_sub_insert(env, dto,
3968 (const struct dt_rec *)rec,
3969 (const struct dt_key *)dot, th);
3974 rec->rec_fid = lu_object_fid(&dt->do_lu);
3975 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
3976 (const struct dt_key *)dotdot, th);
3980 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
3981 cfs_fail_val != i) {
3982 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
3984 slave_lmm->lmv_master_mdt_index =
3987 slave_lmm->lmv_master_mdt_index =
3990 rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
3991 XATTR_NAME_LMV, fl, th);
3996 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
3998 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3999 PFID(lu_object_fid(&dto->do_lu)), i + 1);
4001 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4002 PFID(lu_object_fid(&dto->do_lu)), i);
4004 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
4005 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
4006 sname, lu_object_fid(&dt->do_lu));
4010 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
4011 linkea_buf.lb_len = ldata.ld_leh->leh_len;
4012 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
4013 XATTR_NAME_LINK, 0, th);
4017 rec->rec_fid = lu_object_fid(&dto->do_lu);
4018 rc = lod_sub_insert(env, dt_object_child(dt),
4019 (const struct dt_rec *)rec,
4020 (const struct dt_key *)stripe_name, th);
4024 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
4029 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
4030 rc = lod_sub_xattr_set(env, dt_object_child(dt),
4031 &lmv_buf, XATTR_NAME_LMV, fl, th);
4033 if (slave_lmm != NULL)
4034 OBD_FREE_PTR(slave_lmm);
4040 * Helper function to declare/execute creation of a striped directory
4042 * Called in declare/create object path, prepare striping for a directory
4043 * and prepare defaults data striping for the objects to be created in
4044 * that directory. Notice the function calls "declaration" or "execution"
4045 * methods depending on \a declare param. This is a consequence of the
4046 * current approach while we don't have natural distributed transactions:
4047 * we basically execute non-local updates in the declare phase. So, the
4048 * arguments for the both phases are the same and this is the reason for
4049 * this function to exist.
4051 * \param[in] env execution environment
4052 * \param[in] dt object
4053 * \param[in] attr attributes the stripes will be created with
4054 * \param[in] lmu lmv_user_md if MDT indices are specified
4055 * \param[in] dof format of stripes (see OSD API description)
4056 * \param[in] th transaction handle
4057 * \param[in] declare where to call "declare" or "execute" methods
4059 * \retval 0 on success
4060 * \retval negative if failed
4062 static int lod_dir_striping_create_internal(const struct lu_env *env,
4063 struct dt_object *dt,
4064 struct lu_attr *attr,
4065 const struct lu_buf *lmu,
4066 struct dt_object_format *dof,
4070 struct lod_thread_info *info = lod_env_info(env);
4071 struct lod_object *lo = lod_dt_obj(dt);
4072 const struct lod_default_striping *lds = lo->ldo_def_striping;
4076 LASSERT(ergo(lds != NULL,
4077 lds->lds_def_striping_set ||
4078 lds->lds_dir_def_striping_set));
4080 if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
4081 lo->ldo_dir_stripe_offset)) {
4083 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
4084 int stripe_count = lo->ldo_dir_stripe_count;
4086 if (info->lti_ea_store_size < sizeof(*v1)) {
4087 rc = lod_ea_store_resize(info, sizeof(*v1));
4090 v1 = info->lti_ea_store;
4093 memset(v1, 0, sizeof(*v1));
4094 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
4095 v1->lum_stripe_count = cpu_to_le32(stripe_count);
4096 v1->lum_stripe_offset =
4097 cpu_to_le32(lo->ldo_dir_stripe_offset);
4099 info->lti_buf.lb_buf = v1;
4100 info->lti_buf.lb_len = sizeof(*v1);
4101 lmu = &info->lti_buf;
4105 rc = lod_declare_xattr_set_lmv(env, dt, attr, lmu, dof,
4108 rc = lod_xattr_set_lmv(env, dt, lmu, XATTR_NAME_LMV, 0,
4113 /* foreign LMV EA case */
4115 struct lmv_foreign_md *lfm = lmu->lb_buf;
4117 if (lfm->lfm_magic == LMV_MAGIC_FOREIGN) {
4118 rc = lod_declare_xattr_set_lmv(env, dt, attr,
4122 if (lo->ldo_dir_is_foreign) {
4123 LASSERT(lo->ldo_foreign_lmv != NULL &&
4124 lo->ldo_foreign_lmv_size > 0);
4125 info->lti_buf.lb_buf = lo->ldo_foreign_lmv;
4126 info->lti_buf.lb_len = lo->ldo_foreign_lmv_size;
4127 lmu = &info->lti_buf;
4128 rc = lod_xattr_set_lmv(env, dt, lmu,
4129 XATTR_NAME_LMV, 0, th);
4134 /* Transfer default LMV striping from the parent */
4135 if (lds != NULL && lds->lds_dir_def_striping_set &&
4136 !(LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
4137 lds->lds_dir_def_stripe_offset) &&
4138 le32_to_cpu(lds->lds_dir_def_hash_type) !=
4139 LMV_HASH_TYPE_UNKNOWN)) {
4140 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
4142 if (info->lti_ea_store_size < sizeof(*v1)) {
4143 rc = lod_ea_store_resize(info, sizeof(*v1));
4146 v1 = info->lti_ea_store;
4149 memset(v1, 0, sizeof(*v1));
4150 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
4151 v1->lum_stripe_count =
4152 cpu_to_le32(lds->lds_dir_def_stripe_count);
4153 v1->lum_stripe_offset =
4154 cpu_to_le32(lds->lds_dir_def_stripe_offset);
4156 cpu_to_le32(lds->lds_dir_def_hash_type);
4158 info->lti_buf.lb_buf = v1;
4159 info->lti_buf.lb_len = sizeof(*v1);
4161 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4162 XATTR_NAME_DEFAULT_LMV,
4165 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
4167 XATTR_NAME_DEFAULT_LMV, 0,
4173 /* Transfer default LOV striping from the parent */
4174 if (lds != NULL && lds->lds_def_striping_set &&
4175 lds->lds_def_comp_cnt != 0) {
4176 struct lov_mds_md *lmm;
4177 int lmm_size = lod_comp_md_size(lo, true);
4179 if (info->lti_ea_store_size < lmm_size) {
4180 rc = lod_ea_store_resize(info, lmm_size);
4184 lmm = info->lti_ea_store;
4186 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
4190 info->lti_buf.lb_buf = lmm;
4191 info->lti_buf.lb_len = lmm_size;
4194 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4195 XATTR_NAME_LOV, 0, th);
4197 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4198 XATTR_NAME_LOV, 0, th);
4206 static int lod_declare_dir_striping_create(const struct lu_env *env,
4207 struct dt_object *dt,
4208 struct lu_attr *attr,
4210 struct dt_object_format *dof,
4213 return lod_dir_striping_create_internal(env, dt, attr, lmu, dof, th,
4217 static int lod_dir_striping_create(const struct lu_env *env,
4218 struct dt_object *dt,
4219 struct lu_attr *attr,
4220 struct dt_object_format *dof,
4223 return lod_dir_striping_create_internal(env, dt, attr, NULL, dof, th,
4228 * Make LOV EA for striped object.
4230 * Generate striping information and store it in the LOV EA of the given
4231 * object. The caller must ensure nobody else is calling the function
4232 * against the object concurrently. The transaction must be started.
4233 * FLDB service must be running as well; it's used to map FID to the target,
4234 * which is stored in LOV EA.
4236 * \param[in] env execution environment for this thread
4237 * \param[in] lo LOD object
4238 * \param[in] th transaction handle
4240 * \retval 0 if LOV EA is stored successfully
4241 * \retval negative error number on failure
4243 static int lod_generate_and_set_lovea(const struct lu_env *env,
4244 struct lod_object *lo,
4247 struct lod_thread_info *info = lod_env_info(env);
4248 struct dt_object *next = dt_object_child(&lo->ldo_obj);
4249 struct lov_mds_md_v1 *lmm;
4255 if (lo->ldo_comp_cnt == 0 && !lo->ldo_is_foreign) {
4256 lod_striping_free(env, lo);
4257 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
4261 lmm_size = lod_comp_md_size(lo, false);
4262 if (info->lti_ea_store_size < lmm_size) {
4263 rc = lod_ea_store_resize(info, lmm_size);
4267 lmm = info->lti_ea_store;
4269 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
4273 info->lti_buf.lb_buf = lmm;
4274 info->lti_buf.lb_len = lmm_size;
4275 rc = lod_sub_xattr_set(env, next, &info->lti_buf,
4276 XATTR_NAME_LOV, 0, th);
4280 static __u32 lod_gen_component_id(struct lod_object *lo,
4281 int mirror_id, int comp_idx);
4284 * Repeat an existing component
4286 * Creates a new layout by replicating an existing component. Uses striping
4287 * policy from previous component as a template for the striping for the new
4290 * New component starts with zero length, will be extended (or removed) before
4291 * returning layout to client.
4293 * NB: Reallocates layout components array (lo->ldo_comp_entries), invalidating
4294 * any pre-existing pointers to components. Handle with care.
4296 * \param[in] env execution environment for this thread
4297 * \param[in,out] lo object to update the layout of
4298 * \param[in] index index of component to copy
4300 * \retval 0 on success
4301 * \retval negative errno on error
4303 static int lod_layout_repeat_comp(const struct lu_env *env,
4304 struct lod_object *lo, int index)
4306 struct lod_layout_component *lod_comp;
4307 struct lod_layout_component *new_comp = NULL;
4308 struct lod_layout_component *comp_array;
4309 int rc = 0, i, new_cnt = lo->ldo_comp_cnt + 1;
4314 lod_comp = &lo->ldo_comp_entries[index];
4315 LASSERT(lod_comp_inited(lod_comp) && lod_comp->llc_id != LCME_ID_INVAL);
4317 CDEBUG(D_LAYOUT, "repeating component %d\n", index);
4319 OBD_ALLOC(comp_array, sizeof(*comp_array) * new_cnt);
4320 if (comp_array == NULL)
4321 GOTO(out, rc = -ENOMEM);
4323 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4324 memcpy(&comp_array[i + offset], &lo->ldo_comp_entries[i],
4325 sizeof(*comp_array));
4327 /* Duplicate this component in to the next slot */
4329 new_comp = &comp_array[i + 1];
4330 memcpy(&comp_array[i + 1], &lo->ldo_comp_entries[i],
4331 sizeof(*comp_array));
4332 /* We must now skip this new component when copying */
4337 /* Set up copied component */
4338 new_comp->llc_flags &= ~LCME_FL_INIT;
4339 new_comp->llc_stripe = NULL;
4340 new_comp->llc_stripes_allocated = 0;
4341 new_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4342 /* for uninstantiated components, layout gen stores default stripe
4344 new_comp->llc_layout_gen = lod_comp->llc_stripe_offset;
4345 /* This makes the repeated component zero-length, placed at the end of
4346 * the preceding component */
4347 new_comp->llc_extent.e_start = new_comp->llc_extent.e_end;
4348 new_comp->llc_timestamp = lod_comp->llc_timestamp;
4349 new_comp->llc_pool = NULL;
4351 rc = lod_set_pool(&new_comp->llc_pool, lod_comp->llc_pool);
4355 if (new_comp->llc_ostlist.op_array) {
4356 __u32 *op_array = NULL;
4358 OBD_ALLOC(op_array, new_comp->llc_ostlist.op_size);
4360 GOTO(out, rc = -ENOMEM);
4361 memcpy(op_array, &new_comp->llc_ostlist.op_array,
4362 new_comp->llc_ostlist.op_size);
4363 new_comp->llc_ostlist.op_array = op_array;
4366 OBD_FREE(lo->ldo_comp_entries,
4367 sizeof(*comp_array) * lo->ldo_comp_cnt);
4368 lo->ldo_comp_entries = comp_array;
4369 lo->ldo_comp_cnt = new_cnt;
4371 /* Generate an id for the new component */
4372 mirror_id = mirror_id_of(new_comp->llc_id);
4373 new_comp->llc_id = LCME_ID_INVAL;
4374 new_comp->llc_id = lod_gen_component_id(lo, mirror_id, index + 1);
4375 if (new_comp->llc_id == LCME_ID_INVAL)
4376 GOTO(out, rc = -ERANGE);
4381 OBD_FREE(comp_array, sizeof(*comp_array) * new_cnt);
4386 static int lod_layout_data_init(struct lod_thread_info *info, __u32 comp_cnt)
4390 /* clear memory region that will be used for layout change */
4391 memset(&info->lti_layout_attr, 0, sizeof(struct lu_attr));
4392 info->lti_count = 0;
4394 if (info->lti_comp_size >= comp_cnt)
4397 if (info->lti_comp_size > 0) {
4398 OBD_FREE(info->lti_comp_idx,
4399 info->lti_comp_size * sizeof(__u32));
4400 info->lti_comp_size = 0;
4403 OBD_ALLOC(info->lti_comp_idx, comp_cnt * sizeof(__u32));
4404 if (!info->lti_comp_idx)
4407 info->lti_comp_size = comp_cnt;
4412 * Prepare new layout minus deleted components
4414 * Removes components marked for deletion (LCME_ID_INVAL) by copying to a new
4415 * layout and skipping those components. Removes stripe objects if any exist.
4418 * Reallocates layout components array (lo->ldo_comp_entries), invalidating
4419 * any pre-existing pointers to components.
4421 * Caller is responsible for updating mirror end (ldo_mirror[].lme_end).
4423 * \param[in] env execution environment for this thread
4424 * \param[in,out] lo object to update the layout of
4425 * \param[in] th transaction handle for this operation
4427 * \retval # of components deleted
4428 * \retval negative errno on error
4430 static int lod_layout_del_prep_layout(const struct lu_env *env,
4431 struct lod_object *lo,
4434 struct lod_layout_component *lod_comp;
4435 struct lod_thread_info *info = lod_env_info(env);
4436 int rc = 0, i, j, deleted = 0;
4440 LASSERT(lo->ldo_is_composite);
4441 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
4443 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
4447 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4448 lod_comp = &lo->ldo_comp_entries[i];
4450 if (lod_comp->llc_id != LCME_ID_INVAL) {
4451 /* Build array of things to keep */
4452 info->lti_comp_idx[info->lti_count++] = i;
4456 lod_obj_set_pool(lo, i, NULL);
4457 if (lod_comp->llc_ostlist.op_array) {
4458 OBD_FREE(lod_comp->llc_ostlist.op_array,
4459 lod_comp->llc_ostlist.op_size);
4460 lod_comp->llc_ostlist.op_array = NULL;
4461 lod_comp->llc_ostlist.op_size = 0;
4465 CDEBUG(D_LAYOUT, "deleting comp %d, left %d\n", i,
4466 lo->ldo_comp_cnt - deleted);
4468 /* No striping info for this component */
4469 if (lod_comp->llc_stripe == NULL)
4472 LASSERT(lod_comp->llc_stripe_count > 0);
4473 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4474 struct dt_object *obj = lod_comp->llc_stripe[j];
4479 /* components which are not init have no sub objects
4481 if (lod_comp_inited(lod_comp)) {
4482 rc = lod_sub_destroy(env, obj, th);
4487 lu_object_put(env, &obj->do_lu);
4488 lod_comp->llc_stripe[j] = NULL;
4490 OBD_FREE(lod_comp->llc_stripe, sizeof(*lod_comp->llc_stripe) *
4491 lod_comp->llc_stripes_allocated);
4492 lod_comp->llc_stripe = NULL;
4493 OBD_FREE(lod_comp->llc_ost_indices,
4494 sizeof(__u32) * lod_comp->llc_stripes_allocated);
4495 lod_comp->llc_ost_indices = NULL;
4496 lod_comp->llc_stripes_allocated = 0;
4499 /* info->lti_count has the amount of left components */
4500 LASSERTF(info->lti_count >= 0 && info->lti_count < lo->ldo_comp_cnt,
4501 "left = %d, lo->ldo_comp_cnt %d\n", (int)info->lti_count,
4502 (int)lo->ldo_comp_cnt);
4504 if (info->lti_count > 0) {
4505 struct lod_layout_component *comp_array;
4507 OBD_ALLOC(comp_array, sizeof(*comp_array) * info->lti_count);
4508 if (comp_array == NULL)
4509 GOTO(out, rc = -ENOMEM);
4511 for (i = 0; i < info->lti_count; i++) {
4512 memcpy(&comp_array[i],
4513 &lo->ldo_comp_entries[info->lti_comp_idx[i]],
4514 sizeof(*comp_array));
4517 OBD_FREE(lo->ldo_comp_entries,
4518 sizeof(*comp_array) * lo->ldo_comp_cnt);
4519 lo->ldo_comp_entries = comp_array;
4520 lo->ldo_comp_cnt = info->lti_count;
4522 lod_free_comp_entries(lo);
4527 return rc ? rc : deleted;
4531 * Delete layout component(s)
4533 * This function sets up the layout data in the env and does the setattrs
4534 * required to write out the new layout. The layout itself is modified in
4535 * lod_layout_del_prep_layout.
4537 * \param[in] env execution environment for this thread
4538 * \param[in] dt object
4539 * \param[in] th transaction handle
4541 * \retval 0 on success
4542 * \retval negative error number on failure
4544 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
4547 struct lod_object *lo = lod_dt_obj(dt);
4548 struct dt_object *next = dt_object_child(dt);
4549 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4552 LASSERT(lo->ldo_mirror_count == 1);
4554 rc = lod_layout_del_prep_layout(env, lo, th);
4558 /* Only do this if we didn't delete all components */
4559 if (lo->ldo_comp_cnt > 0) {
4560 lo->ldo_mirrors[0].lme_end = lo->ldo_comp_cnt - 1;
4561 lod_obj_inc_layout_gen(lo);
4564 LASSERT(dt_object_exists(dt));
4565 rc = dt_attr_get(env, next, attr);
4569 if (attr->la_size > 0) {
4571 attr->la_valid = LA_SIZE;
4572 rc = lod_sub_attr_set(env, next, attr, th);
4577 rc = lod_generate_and_set_lovea(env, lo, th);
4581 lod_striping_free(env, lo);
4586 static int lod_get_default_lov_striping(const struct lu_env *env,
4587 struct lod_object *lo,
4588 struct lod_default_striping *lds,
4589 struct dt_allocation_hint *ah);
4591 * Implementation of dt_object_operations::do_xattr_set.
4593 * Sets specified extended attribute on the object. Three types of EAs are
4595 * LOV EA - stores striping for a regular file or default striping (when set
4597 * LMV EA - stores a marker for the striped directories
4598 * DMV EA - stores default directory striping
4600 * When striping is applied to a non-striped existing object (this is called
4601 * late striping), then LOD notices the caller wants to turn the object into a
4602 * striped one. The stripe objects are created and appropriate EA is set:
4603 * LOV EA storing all the stripes directly or LMV EA storing just a small header
4604 * with striping configuration.
4606 * \see dt_object_operations::do_xattr_set() in the API description for details.
4608 static int lod_xattr_set(const struct lu_env *env,
4609 struct dt_object *dt, const struct lu_buf *buf,
4610 const char *name, int fl, struct thandle *th)
4612 struct dt_object *next = dt_object_child(dt);
4616 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4617 strcmp(name, XATTR_NAME_LMV) == 0) {
4618 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
4620 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4621 strncmp(name, XATTR_NAME_LMV, strlen(XATTR_NAME_LMV)) == 0 &&
4622 strlen(name) > strlen(XATTR_NAME_LMV)) {
4623 const char *op = name + strlen(XATTR_NAME_LMV);
4627 * XATTR_NAME_LMV".add" is never called, but only declared,
4628 * because lod_xattr_set_lmv() will do the addition.
4630 if (strcmp(op, ".del") == 0)
4631 rc = lod_dir_layout_delete(env, dt, buf, th);
4632 else if (strcmp(op, ".set") == 0)
4633 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LMV,
4637 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4638 strcmp(name, XATTR_NAME_LOV) == 0) {
4639 struct lod_default_striping *lds = lod_lds_buf_get(env);
4640 struct lov_user_md_v1 *v1 = buf->lb_buf;
4641 char pool[LOV_MAXPOOLNAME + 1];
4644 /* get existing striping config */
4645 rc = lod_get_default_lov_striping(env, lod_dt_obj(dt), lds,
4650 memset(pool, 0, sizeof(pool));
4651 if (lds->lds_def_striping_set == 1)
4652 lod_layout_get_pool(lds->lds_def_comp_entries,
4653 lds->lds_def_comp_cnt, pool,
4656 is_del = LOVEA_DELETE_VALUES(v1->lmm_stripe_size,
4657 v1->lmm_stripe_count,
4658 v1->lmm_stripe_offset,
4661 /* Retain the pool name if it is not given */
4662 if (v1->lmm_magic == LOV_USER_MAGIC_V1 && pool[0] != '\0' &&
4664 struct lod_thread_info *info = lod_env_info(env);
4665 struct lov_user_md_v3 *v3 = info->lti_ea_store;
4667 memset(v3, 0, sizeof(*v3));
4668 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
4669 v3->lmm_pattern = cpu_to_le32(v1->lmm_pattern);
4670 v3->lmm_stripe_count =
4671 cpu_to_le32(v1->lmm_stripe_count);
4672 v3->lmm_stripe_offset =
4673 cpu_to_le32(v1->lmm_stripe_offset);
4674 v3->lmm_stripe_size = cpu_to_le32(v1->lmm_stripe_size);
4676 strlcpy(v3->lmm_pool_name, pool,
4677 sizeof(v3->lmm_pool_name));
4679 info->lti_buf.lb_buf = v3;
4680 info->lti_buf.lb_len = sizeof(*v3);
4681 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4684 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name,
4688 if (lds->lds_def_striping_set == 1 &&
4689 lds->lds_def_comp_entries != NULL)
4690 lod_free_def_comp_entries(lds);
4693 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4694 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
4696 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
4699 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4700 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4701 strcmp(name, XATTR_LUSTRE_LOV) == 0 ||
4702 allowed_lustre_lov(name))) {
4703 /* in case of lov EA swap, just set it
4704 * if not, it is a replay so check striping match what we
4705 * already have during req replay, declare_xattr_set()
4706 * defines striping, then create() does the work */
4707 if (fl & LU_XATTR_REPLACE) {
4708 /* free stripes, then update disk */
4709 lod_striping_free(env, lod_dt_obj(dt));
4711 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
4712 } else if (dt_object_remote(dt)) {
4713 /* This only happens during migration, see
4714 * mdd_migrate_create(), in which Master MDT will
4715 * create a remote target object, and only set
4716 * (migrating) stripe EA on the remote object,
4717 * and does not need creating each stripes. */
4718 rc = lod_sub_xattr_set(env, next, buf, name,
4720 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
4721 /* delete component(s) */
4722 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
4723 rc = lod_layout_del(env, dt, th);
4726 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
4727 * it's going to create create file with specified
4728 * component(s), the striping must have not being
4729 * cached in this case;
4731 * Otherwise, it's going to add/change component(s) to
4732 * an existing file, the striping must have been cached
4735 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
4736 !strcmp(name, XATTR_NAME_LOV),
4737 !lod_dt_obj(dt)->ldo_comp_cached));
4739 rc = lod_striped_create(env, dt, NULL, NULL, th);
4742 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
4743 rc = lod_replace_parent_fid(env, dt, buf, th, false);
4748 /* then all other xattr */
4749 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
4755 * Implementation of dt_object_operations::do_declare_xattr_del.
4757 * \see dt_object_operations::do_declare_xattr_del() in the API description
4760 static int lod_declare_xattr_del(const struct lu_env *env,
4761 struct dt_object *dt, const char *name,
4764 struct lod_object *lo = lod_dt_obj(dt);
4765 struct dt_object *next = dt_object_child(dt);
4770 rc = lod_sub_declare_xattr_del(env, next, name, th);
4774 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4777 /* NB: don't delete stripe LMV, because when we do this, normally we
4778 * will remove stripes, besides, if directory LMV is corrupt, this will
4779 * prevent deleting its LMV and fixing it (via LFSCK).
4781 if (!strcmp(name, XATTR_NAME_LMV))
4784 rc = lod_striping_load(env, lo);
4788 if (lo->ldo_dir_stripe_count == 0)
4791 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4792 struct dt_object *dto = lo->ldo_stripe[i];
4797 rc = lod_sub_declare_xattr_del(env, dto, name, th);
4806 * Implementation of dt_object_operations::do_xattr_del.
4808 * If EA storing a regular striping is being deleted, then release
4809 * all the references to the stripe objects in core.
4811 * \see dt_object_operations::do_xattr_del() in the API description for details.
4813 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
4814 const char *name, struct thandle *th)
4816 struct dt_object *next = dt_object_child(dt);
4817 struct lod_object *lo = lod_dt_obj(dt);
4822 if (!strcmp(name, XATTR_NAME_LOV) || !strcmp(name, XATTR_NAME_LMV))
4823 lod_striping_free(env, lod_dt_obj(dt));
4825 rc = lod_sub_xattr_del(env, next, name, th);
4826 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
4829 if (!strcmp(name, XATTR_NAME_LMV))
4832 if (lo->ldo_dir_stripe_count == 0)
4835 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4836 struct dt_object *dto = lo->ldo_stripe[i];
4841 rc = lod_sub_xattr_del(env, dto, name, th);
4850 * Implementation of dt_object_operations::do_xattr_list.
4852 * \see dt_object_operations::do_xattr_list() in the API description
4855 static int lod_xattr_list(const struct lu_env *env,
4856 struct dt_object *dt, const struct lu_buf *buf)
4858 return dt_xattr_list(env, dt_object_child(dt), buf);
4861 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
4863 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
4867 * Copy OST list from layout provided by user.
4869 * \param[in] lod_comp layout_component to be filled
4870 * \param[in] v3 LOV EA V3 user data
4872 * \retval 0 on success
4873 * \retval negative if failed
4875 int lod_comp_copy_ost_lists(struct lod_layout_component *lod_comp,
4876 struct lov_user_md_v3 *v3)
4882 if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
4883 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
4885 if (lod_comp->llc_ostlist.op_array) {
4886 if (lod_comp->llc_ostlist.op_size >=
4887 v3->lmm_stripe_count * sizeof(__u32)) {
4888 lod_comp->llc_ostlist.op_count =
4889 v3->lmm_stripe_count;
4892 OBD_FREE(lod_comp->llc_ostlist.op_array,
4893 lod_comp->llc_ostlist.op_size);
4896 /* copy ost list from lmm */
4897 lod_comp->llc_ostlist.op_count = v3->lmm_stripe_count;
4898 lod_comp->llc_ostlist.op_size = v3->lmm_stripe_count * sizeof(__u32);
4899 OBD_ALLOC(lod_comp->llc_ostlist.op_array,
4900 lod_comp->llc_ostlist.op_size);
4901 if (!lod_comp->llc_ostlist.op_array)
4904 for (j = 0; j < v3->lmm_stripe_count; j++) {
4905 lod_comp->llc_ostlist.op_array[j] =
4906 v3->lmm_objects[j].l_ost_idx;
4914 * Get default striping.
4916 * \param[in] env execution environment
4917 * \param[in] lo object
4918 * \param[out] lds default striping
4920 * \retval 0 on success
4921 * \retval negative if failed
4923 static int lod_get_default_lov_striping(const struct lu_env *env,
4924 struct lod_object *lo,
4925 struct lod_default_striping *lds,
4926 struct dt_allocation_hint *ah)
4928 struct lod_thread_info *info = lod_env_info(env);
4929 struct lov_user_md_v1 *v1 = NULL;
4930 struct lov_user_md_v3 *v3 = NULL;
4931 struct lov_comp_md_v1 *comp_v1 = NULL;
4939 rc = lod_get_lov_ea(env, lo);
4943 if (rc < (typeof(rc))sizeof(struct lov_user_md))
4946 v1 = info->lti_ea_store;
4947 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
4948 lustre_swab_lov_user_md_v1(v1);
4949 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
4950 v3 = (struct lov_user_md_v3 *)v1;
4951 lustre_swab_lov_user_md_v3(v3);
4952 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_SPECIFIC)) {
4953 v3 = (struct lov_user_md_v3 *)v1;
4954 lustre_swab_lov_user_md_v3(v3);
4955 lustre_swab_lov_user_md_objects(v3->lmm_objects,
4956 v3->lmm_stripe_count);
4957 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1) ||
4958 v1->lmm_magic == __swab32(LOV_USER_MAGIC_SEL)) {
4959 comp_v1 = (struct lov_comp_md_v1 *)v1;
4960 lustre_swab_lov_comp_md_v1(comp_v1);
4963 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
4964 v1->lmm_magic != LOV_MAGIC_COMP_V1 &&
4965 v1->lmm_magic != LOV_MAGIC_SEL &&
4966 v1->lmm_magic != LOV_USER_MAGIC_SPECIFIC)
4969 if ((v1->lmm_magic == LOV_MAGIC_COMP_V1 ||
4970 v1->lmm_magic == LOV_MAGIC_SEL) &&
4971 !(ah && ah->dah_append_stripes)) {
4972 comp_v1 = (struct lov_comp_md_v1 *)v1;
4973 comp_cnt = comp_v1->lcm_entry_count;
4976 mirror_cnt = comp_v1->lcm_mirror_count + 1;
4984 /* realloc default comp entries if necessary */
4985 rc = lod_def_striping_comp_resize(lds, comp_cnt);
4989 lds->lds_def_comp_cnt = comp_cnt;
4990 lds->lds_def_striping_is_composite = composite;
4991 lds->lds_def_mirror_cnt = mirror_cnt;
4993 for (i = 0; i < comp_cnt; i++) {
4994 struct lod_layout_component *lod_comp;
4997 lod_comp = &lds->lds_def_comp_entries[i];
4999 * reset lod_comp values, llc_stripes is always NULL in
5000 * the default striping template, llc_pool will be reset
5003 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
5006 v1 = (struct lov_user_md *)((char *)comp_v1 +
5007 comp_v1->lcm_entries[i].lcme_offset);
5008 lod_comp->llc_extent =
5009 comp_v1->lcm_entries[i].lcme_extent;
5010 /* We only inherit certain flags from the layout */
5011 lod_comp->llc_flags =
5012 comp_v1->lcm_entries[i].lcme_flags &
5013 LCME_TEMPLATE_FLAGS;
5016 if (!lov_pattern_supported(v1->lmm_pattern) &&
5017 !(v1->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
5018 lod_free_def_comp_entries(lds);
5022 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d stripe_offset=%d append_stripes=%d\n",
5023 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
5024 (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
5025 (int)v1->lmm_stripe_offset,
5026 ah ? ah->dah_append_stripes : 0);
5028 if (ah && ah->dah_append_stripes)
5029 lod_comp->llc_stripe_count = ah->dah_append_stripes;
5031 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
5032 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
5033 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
5034 lod_comp->llc_pattern = v1->lmm_pattern;
5037 if (ah && ah->dah_append_pool && ah->dah_append_pool[0]) {
5038 pool = ah->dah_append_pool;
5039 } else if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
5040 /* XXX: sanity check here */
5041 v3 = (struct lov_user_md_v3 *) v1;
5042 if (v3->lmm_pool_name[0] != '\0')
5043 pool = v3->lmm_pool_name;
5045 lod_set_def_pool(lds, i, pool);
5046 if (v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
5047 v3 = (struct lov_user_md_v3 *)v1;
5048 rc = lod_comp_copy_ost_lists(lod_comp, v3);
5051 } else if (lod_comp->llc_ostlist.op_array &&
5052 lod_comp->llc_ostlist.op_count) {
5053 for (j = 0; j < lod_comp->llc_ostlist.op_count; j++)
5054 lod_comp->llc_ostlist.op_array[j] = -1;
5055 lod_comp->llc_ostlist.op_count = 0;
5059 lds->lds_def_striping_set = 1;
5064 * Get default directory striping.
5066 * \param[in] env execution environment
5067 * \param[in] lo object
5068 * \param[out] lds default striping
5070 * \retval 0 on success
5071 * \retval negative if failed
5073 static int lod_get_default_lmv_striping(const struct lu_env *env,
5074 struct lod_object *lo,
5075 struct lod_default_striping *lds)
5077 struct lmv_user_md *lmu;
5080 lds->lds_dir_def_striping_set = 0;
5082 rc = lod_get_default_lmv_ea(env, lo);
5086 if (rc >= (int)sizeof(*lmu)) {
5087 struct lod_thread_info *info = lod_env_info(env);
5089 lmu = info->lti_ea_store;
5091 lds->lds_dir_def_stripe_count =
5092 le32_to_cpu(lmu->lum_stripe_count);
5093 lds->lds_dir_def_stripe_offset =
5094 le32_to_cpu(lmu->lum_stripe_offset);
5095 lds->lds_dir_def_hash_type =
5096 le32_to_cpu(lmu->lum_hash_type);
5097 lds->lds_dir_def_striping_set = 1;
5104 * Get default striping in the object.
5106 * Get object default striping and default directory striping.
5108 * \param[in] env execution environment
5109 * \param[in] lo object
5110 * \param[out] lds default striping
5112 * \retval 0 on success
5113 * \retval negative if failed
5115 static int lod_get_default_striping(const struct lu_env *env,
5116 struct lod_object *lo,
5117 struct lod_default_striping *lds)
5121 rc = lod_get_default_lov_striping(env, lo, lds, NULL);
5122 rc1 = lod_get_default_lmv_striping(env, lo, lds);
5123 if (rc == 0 && rc1 < 0)
5130 * Apply default striping on object.
5132 * If object striping pattern is not set, set to the one in default striping.
5133 * The default striping is from parent or fs.
5135 * \param[in] lo new object
5136 * \param[in] lds default striping
5137 * \param[in] mode new object's mode
5139 static void lod_striping_from_default(struct lod_object *lo,
5140 const struct lod_default_striping *lds,
5143 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5146 if (lds->lds_def_striping_set && S_ISREG(mode)) {
5147 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
5149 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
5150 lds->lds_def_comp_cnt);
5154 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
5155 if (lds->lds_def_mirror_cnt > 1)
5156 lo->ldo_flr_state = LCM_FL_RDONLY;
5158 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5159 struct lod_layout_component *obj_comp =
5160 &lo->ldo_comp_entries[i];
5161 struct lod_layout_component *def_comp =
5162 &lds->lds_def_comp_entries[i];
5164 CDEBUG(D_LAYOUT, "Inherit from default: flags=%#x "
5165 "size=%hu nr=%u offset=%u pattern=%#x pool=%s\n",
5166 def_comp->llc_flags,
5167 def_comp->llc_stripe_size,
5168 def_comp->llc_stripe_count,
5169 def_comp->llc_stripe_offset,
5170 def_comp->llc_pattern,
5171 def_comp->llc_pool ?: "");
5173 *obj_comp = *def_comp;
5174 if (def_comp->llc_pool != NULL) {
5175 /* pointer was copied from def_comp */
5176 obj_comp->llc_pool = NULL;
5177 lod_obj_set_pool(lo, i, def_comp->llc_pool);
5181 if (def_comp->llc_ostlist.op_array &&
5182 def_comp->llc_ostlist.op_count) {
5183 OBD_ALLOC(obj_comp->llc_ostlist.op_array,
5184 obj_comp->llc_ostlist.op_size);
5185 if (!obj_comp->llc_ostlist.op_array)
5187 memcpy(obj_comp->llc_ostlist.op_array,
5188 def_comp->llc_ostlist.op_array,
5189 obj_comp->llc_ostlist.op_size);
5190 } else if (def_comp->llc_ostlist.op_array) {
5191 obj_comp->llc_ostlist.op_array = NULL;
5195 * Don't initialize these fields for plain layout
5196 * (v1/v3) here, they are inherited in the order of
5197 * 'parent' -> 'fs default (root)' -> 'global default
5198 * values for stripe_count & stripe_size'.
5200 * see lod_ah_init().
5202 if (!lo->ldo_is_composite)
5205 lod_adjust_stripe_info(obj_comp, desc, 0);
5207 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
5208 if (lo->ldo_dir_stripe_count == 0)
5209 lo->ldo_dir_stripe_count =
5210 lds->lds_dir_def_stripe_count;
5211 if (lo->ldo_dir_stripe_offset == -1)
5212 lo->ldo_dir_stripe_offset =
5213 lds->lds_dir_def_stripe_offset;
5214 if (lo->ldo_dir_hash_type == 0)
5215 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
5217 CDEBUG(D_LAYOUT, "striping from default dir: count:%hu, "
5218 "offset:%u, hash_type:%u\n",
5219 lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
5220 lo->ldo_dir_hash_type);
5224 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root,
5227 struct lod_layout_component *lod_comp;
5229 if (lo->ldo_comp_cnt == 0)
5232 if (lo->ldo_is_composite)
5235 lod_comp = &lo->ldo_comp_entries[0];
5237 if (lod_comp->llc_stripe_count <= 0 ||
5238 lod_comp->llc_stripe_size <= 0)
5241 if (from_root && (lod_comp->llc_pool == NULL ||
5242 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
5245 if (append_pool && append_pool[0])
5252 * Implementation of dt_object_operations::do_ah_init.
5254 * This method is used to make a decision on the striping configuration for the
5255 * object being created. It can be taken from the \a parent object if it exists,
5256 * or filesystem's default. The resulting configuration (number of stripes,
5257 * stripe size/offset, pool name, etc) is stored in the object itself and will
5258 * be used by the methods like ->doo_declare_create().
5260 * \see dt_object_operations::do_ah_init() in the API description for details.
5262 static void lod_ah_init(const struct lu_env *env,
5263 struct dt_allocation_hint *ah,
5264 struct dt_object *parent,
5265 struct dt_object *child,
5268 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
5269 struct lod_thread_info *info = lod_env_info(env);
5270 struct lod_default_striping *lds = lod_lds_buf_get(env);
5271 struct dt_object *nextp = NULL;
5272 struct dt_object *nextc;
5273 struct lod_object *lp = NULL;
5274 struct lod_object *lc;
5275 struct lov_desc *desc;
5276 struct lod_layout_component *lod_comp;
5282 if (ah->dah_append_stripes == -1)
5283 ah->dah_append_stripes =
5284 d->lod_ost_descs.ltd_lov_desc.ld_tgt_count;
5286 if (likely(parent)) {
5287 nextp = dt_object_child(parent);
5288 lp = lod_dt_obj(parent);
5291 nextc = dt_object_child(child);
5292 lc = lod_dt_obj(child);
5294 LASSERT(!lod_obj_is_striped(child));
5295 /* default layout template may have been set on the regular file
5296 * when this is called from mdd_create_data() */
5297 if (S_ISREG(child_mode))
5298 lod_free_comp_entries(lc);
5300 if (!dt_object_exists(nextc))
5301 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
5303 if (S_ISDIR(child_mode)) {
5304 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
5306 /* other default values are 0 */
5307 lc->ldo_dir_stripe_offset = -1;
5309 /* no default striping configuration is needed for
5312 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5313 le32_to_cpu(lum1->lum_magic) == LMV_MAGIC_FOREIGN) {
5314 lc->ldo_dir_is_foreign = true;
5315 /* keep stripe_count 0 and stripe_offset -1 */
5316 CDEBUG(D_INFO, "no default striping for foreign dir\n");
5321 * If parent object is not root directory,
5322 * then get default striping from parent object.
5324 if (likely(lp != NULL)) {
5325 lod_get_default_striping(env, lp, lds);
5327 /* inherit default striping except ROOT */
5328 if ((lds->lds_def_striping_set ||
5329 lds->lds_dir_def_striping_set) &&
5330 !fid_is_root(lod_object_fid(lp)))
5331 lc->ldo_def_striping = lds;
5334 /* It should always honour the specified stripes */
5335 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
5336 * will have old magic. In this case, we should ignore the
5337 * stripe count and try to create dir by default stripe.
5339 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5340 (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
5341 le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
5342 lc->ldo_dir_stripe_count =
5343 le32_to_cpu(lum1->lum_stripe_count);
5344 lc->ldo_dir_stripe_offset =
5345 le32_to_cpu(lum1->lum_stripe_offset);
5346 lc->ldo_dir_hash_type =
5347 le32_to_cpu(lum1->lum_hash_type);
5349 "set dirstripe: count %hu, offset %d, hash %u\n",
5350 lc->ldo_dir_stripe_count,
5351 (int)lc->ldo_dir_stripe_offset,
5352 lc->ldo_dir_hash_type);
5354 /* transfer defaults LMV to new directory */
5355 lod_striping_from_default(lc, lds, child_mode);
5357 /* set count 0 to create normal directory */
5358 if (lc->ldo_dir_stripe_count == 1)
5359 lc->ldo_dir_stripe_count = 0;
5362 /* shrink the stripe_count to the avaible MDT count */
5363 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
5364 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)) {
5365 lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
5366 if (lc->ldo_dir_stripe_count == 1)
5367 lc->ldo_dir_stripe_count = 0;
5370 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
5371 lc->ldo_dir_stripe_count,
5372 (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
5377 /* child object regular file*/
5379 if (!lod_object_will_be_striped(S_ISREG(child_mode),
5380 lu_object_fid(&child->do_lu)))
5383 /* If object is going to be striped over OSTs, transfer default
5384 * striping information to the child, so that we can use it
5385 * during declaration and creation.
5387 * Try from the parent first.
5389 if (likely(lp != NULL)) {
5390 rc = lod_get_default_lov_striping(env, lp, lds, ah);
5392 lod_striping_from_default(lc, lds, child_mode);
5395 /* Initialize lod_device::lod_md_root object reference */
5396 if (d->lod_md_root == NULL) {
5397 struct dt_object *root;
5398 struct lod_object *lroot;
5400 lu_root_fid(&info->lti_fid);
5401 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
5402 if (!IS_ERR(root)) {
5403 lroot = lod_dt_obj(root);
5405 spin_lock(&d->lod_lock);
5406 if (d->lod_md_root != NULL)
5407 dt_object_put(env, &d->lod_md_root->ldo_obj);
5408 d->lod_md_root = lroot;
5409 spin_unlock(&d->lod_lock);
5413 /* try inherit layout from the root object (fs default) when:
5414 * - parent does not have default layout; or
5415 * - parent has plain(v1/v3) default layout, and some attributes
5416 * are not specified in the default layout;
5418 if (d->lod_md_root != NULL &&
5419 lod_need_inherit_more(lc, true, ah->dah_append_pool)) {
5420 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds,
5424 if (lc->ldo_comp_cnt == 0) {
5425 lod_striping_from_default(lc, lds, child_mode);
5426 } else if (!lds->lds_def_striping_is_composite) {
5427 struct lod_layout_component *def_comp;
5429 LASSERT(!lc->ldo_is_composite);
5430 lod_comp = &lc->ldo_comp_entries[0];
5431 def_comp = &lds->lds_def_comp_entries[0];
5433 if (lod_comp->llc_stripe_count <= 0)
5434 lod_comp->llc_stripe_count =
5435 def_comp->llc_stripe_count;
5436 if (lod_comp->llc_stripe_size <= 0)
5437 lod_comp->llc_stripe_size =
5438 def_comp->llc_stripe_size;
5439 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT &&
5440 (!lod_comp->llc_pool || !lod_comp->llc_pool[0]))
5441 lod_comp->llc_stripe_offset =
5442 def_comp->llc_stripe_offset;
5443 if (lod_comp->llc_pool == NULL)
5444 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
5449 * fs default striping may not be explicitly set, or historically set
5450 * in config log, use them.
5452 if (lod_need_inherit_more(lc, false, ah->dah_append_pool)) {
5453 if (lc->ldo_comp_cnt == 0) {
5454 rc = lod_alloc_comp_entries(lc, 0, 1);
5456 /* fail to allocate memory, will create a
5457 * non-striped file. */
5459 lc->ldo_is_composite = 0;
5460 lod_comp = &lc->ldo_comp_entries[0];
5461 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
5463 LASSERT(!lc->ldo_is_composite);
5464 lod_comp = &lc->ldo_comp_entries[0];
5465 desc = &d->lod_ost_descs.ltd_lov_desc;
5466 lod_adjust_stripe_info(lod_comp, desc, ah->dah_append_stripes);
5467 if (ah->dah_append_pool && ah->dah_append_pool[0])
5468 lod_obj_set_pool(lc, 0, ah->dah_append_pool);
5474 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
5476 * Size initialization on late striping.
5478 * Propagate the size of a truncated object to a deferred striping.
5479 * This function handles a special case when truncate was done on a
5480 * non-striped object and now while the striping is being created
5481 * we can't lose that size, so we have to propagate it to the stripes
5484 * \param[in] env execution environment
5485 * \param[in] dt object
5486 * \param[in] th transaction handle
5488 * \retval 0 on success
5489 * \retval negative if failed
5491 static int lod_declare_init_size(const struct lu_env *env,
5492 struct dt_object *dt, struct thandle *th)
5494 struct dt_object *next = dt_object_child(dt);
5495 struct lod_object *lo = lod_dt_obj(dt);
5496 struct dt_object **objects = NULL;
5497 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
5498 uint64_t size, offs;
5499 int i, rc, stripe, stripe_count = 0, stripe_size = 0;
5500 struct lu_extent size_ext;
5503 if (!lod_obj_is_striped(dt))
5506 rc = dt_attr_get(env, next, attr);
5507 LASSERT(attr->la_valid & LA_SIZE);
5511 size = attr->la_size;
5515 size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
5516 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5517 struct lod_layout_component *lod_comp;
5518 struct lu_extent *extent;
5520 lod_comp = &lo->ldo_comp_entries[i];
5522 if (lod_comp->llc_stripe == NULL)
5525 extent = &lod_comp->llc_extent;
5526 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
5527 if (!lo->ldo_is_composite ||
5528 lu_extent_is_overlapped(extent, &size_ext)) {
5529 objects = lod_comp->llc_stripe;
5530 stripe_count = lod_comp->llc_stripe_count;
5531 stripe_size = lod_comp->llc_stripe_size;
5534 if (stripe_count == 0)
5537 LASSERT(objects != NULL && stripe_size != 0);
5538 /* ll_do_div64(a, b) returns a % b, and a = a / b */
5539 ll_do_div64(size, (__u64)stripe_size);
5540 stripe = ll_do_div64(size, (__u64)stripe_count);
5541 LASSERT(objects[stripe] != NULL);
5543 size = size * stripe_size;
5544 offs = attr->la_size;
5545 size += ll_do_div64(offs, stripe_size);
5547 attr->la_valid = LA_SIZE;
5548 attr->la_size = size;
5550 rc = lod_sub_declare_attr_set(env, objects[stripe],
5559 * Declare creation of striped object.
5561 * The function declares creation stripes for a regular object. The function
5562 * also declares whether the stripes will be created with non-zero size if
5563 * previously size was set non-zero on the master object. If object \a dt is
5564 * not local, then only fully defined striping can be applied in \a lovea.
5565 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
5568 * \param[in] env execution environment
5569 * \param[in] dt object
5570 * \param[in] attr attributes the stripes will be created with
5571 * \param[in] lovea a buffer containing striping description
5572 * \param[in] th transaction handle
5574 * \retval 0 on success
5575 * \retval negative if failed
5577 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
5578 struct lu_attr *attr,
5579 const struct lu_buf *lovea, struct thandle *th)
5581 struct lod_thread_info *info = lod_env_info(env);
5582 struct dt_object *next = dt_object_child(dt);
5583 struct lod_object *lo = lod_dt_obj(dt);
5587 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
5588 GOTO(out, rc = -ENOMEM);
5590 if (!dt_object_remote(next)) {
5591 /* choose OST and generate appropriate objects */
5592 rc = lod_prepare_create(env, lo, attr, lovea, th);
5597 * declare storage for striping data
5599 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5601 /* LOD can not choose OST objects for remote objects, i.e.
5602 * stripes must be ready before that. Right now, it can only
5603 * happen during migrate, i.e. migrate process needs to create
5604 * remote regular file (mdd_migrate_create), then the migrate
5605 * process will provide stripeEA. */
5606 LASSERT(lovea != NULL);
5607 info->lti_buf = *lovea;
5610 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
5611 XATTR_NAME_LOV, 0, th);
5616 * if striping is created with local object's size > 0,
5617 * we have to propagate this size to specific object
5618 * the case is possible only when local object was created previously
5620 if (dt_object_exists(next))
5621 rc = lod_declare_init_size(env, dt, th);
5624 /* failed to create striping or to set initial size, let's reset
5625 * config so that others don't get confused */
5627 lod_striping_free(env, lo);
5633 * Whether subdirectories under \a dt should be created on MDTs by space QoS
5635 * If LMV_HASH_FLAG_SPACE is set on directory default layout, its subdirectories
5636 * should be created on MDT by space QoS.
5638 * \param[in] env execution environment
5639 * \param[in] dev lu device
5640 * \param[in] dt object
5642 * \retval 1 if directory should create subdir by space usage
5644 * \retval -ev if failed
5646 static inline int dt_object_qos_mkdir(const struct lu_env *env,
5647 struct lu_device *dev,
5648 struct dt_object *dt)
5650 struct lod_thread_info *info = lod_env_info(env);
5651 struct lu_object *obj;
5652 struct lod_object *lo;
5653 struct lmv_user_md *lmu;
5656 obj = lu_object_find_slice(env, dev, lu_object_fid(&dt->do_lu), NULL);
5658 return PTR_ERR(obj);
5660 lo = lu2lod_obj(obj);
5662 rc = lod_get_default_lmv_ea(env, lo);
5663 dt_object_put(env, dt);
5667 if (rc < (int)sizeof(*lmu))
5670 lmu = info->lti_ea_store;
5671 return le32_to_cpu(lmu->lum_stripe_offset) == LMV_OFFSET_DEFAULT;
5675 * Implementation of dt_object_operations::do_declare_create.
5677 * The method declares creation of a new object. If the object will be striped,
5678 * then helper functions are called to find FIDs for the stripes, declare
5679 * creation of the stripes and declare initialization of the striping
5680 * information to be stored in the master object.
5682 * \see dt_object_operations::do_declare_create() in the API description
5685 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
5686 struct lu_attr *attr,
5687 struct dt_allocation_hint *hint,
5688 struct dt_object_format *dof, struct thandle *th)
5690 struct dt_object *next = dt_object_child(dt);
5691 struct lod_object *lo = lod_dt_obj(dt);
5700 * first of all, we declare creation of local object
5702 rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
5707 * it's lod_ah_init() that has decided the object will be striped
5709 if (dof->dof_type == DFT_REGULAR) {
5710 /* callers don't want stripes */
5711 /* XXX: all tricky interactions with ->ah_make_hint() decided
5712 * to use striping, then ->declare_create() behaving differently
5713 * should be cleaned */
5714 if (dof->u.dof_reg.striped != 0)
5715 rc = lod_declare_striped_create(env, dt, attr,
5717 } else if (dof->dof_type == DFT_DIR) {
5718 struct seq_server_site *ss;
5719 struct lu_buf buf = { NULL };
5720 struct lu_buf *lmu = NULL;
5722 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
5724 /* If the parent has default stripeEA, and client
5725 * did not find it before sending create request,
5726 * then MDT will return -EREMOTE, and client will
5727 * retrieve the default stripeEA and re-create the
5730 * Note: if dah_eadata != NULL, it means creating the
5731 * striped directory with specified stripeEA, then it
5732 * should ignore the default stripeEA */
5733 if (hint != NULL && hint->dah_eadata == NULL) {
5734 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
5735 GOTO(out, rc = -EREMOTE);
5737 if (lo->ldo_dir_stripe_offset == LMV_OFFSET_DEFAULT) {
5738 struct lod_default_striping *lds;
5740 lds = lo->ldo_def_striping;
5742 * child and parent should be on the same MDT,
5743 * but if parent has default LMV, and the start
5744 * MDT offset is -1, it's allowed. This check
5745 * is not necessary after 2.12.22 because client
5746 * follows this already, but old client may not.
5748 if (hint->dah_parent &&
5749 dt_object_remote(hint->dah_parent) && lds &&
5750 lds->lds_dir_def_stripe_offset !=
5752 GOTO(out, rc = -EREMOTE);
5753 } else if (lo->ldo_dir_stripe_offset !=
5755 struct lod_device *lod;
5756 struct lu_tgt_desc *mdt = NULL;
5757 bool found_mdt = false;
5759 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5760 lod_foreach_mdt(lod, mdt) {
5761 if (mdt->ltd_index ==
5762 lo->ldo_dir_stripe_offset) {
5768 /* If the MDT indicated by stripe_offset can be
5769 * found, then tell client to resend the create
5770 * request to the correct MDT, otherwise return
5771 * error to client */
5773 GOTO(out, rc = -EREMOTE);
5775 GOTO(out, rc = -EINVAL);
5777 } else if (hint && hint->dah_eadata) {
5779 lmu->lb_buf = (void *)hint->dah_eadata;
5780 lmu->lb_len = hint->dah_eadata_len;
5783 rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
5787 /* failed to create striping or to set initial size, let's reset
5788 * config so that others don't get confused */
5790 lod_striping_free(env, lo);
5795 * Generate component ID for new created component.
5797 * \param[in] lo LOD object
5798 * \param[in] comp_idx index of ldo_comp_entries
5800 * \retval component ID on success
5801 * \retval LCME_ID_INVAL on failure
5803 static __u32 lod_gen_component_id(struct lod_object *lo,
5804 int mirror_id, int comp_idx)
5806 struct lod_layout_component *lod_comp;
5807 __u32 id, start, end;
5810 LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
5812 lod_obj_inc_layout_gen(lo);
5813 id = lo->ldo_layout_gen;
5814 if (likely(id <= SEQ_ID_MAX))
5815 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
5817 /* Layout generation wraps, need to check collisions. */
5818 start = id & SEQ_ID_MASK;
5821 for (id = start; id <= end; id++) {
5822 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5823 lod_comp = &lo->ldo_comp_entries[i];
5824 if (pflr_id(mirror_id, id) == lod_comp->llc_id)
5827 /* Found the ununsed ID */
5828 if (i == lo->ldo_comp_cnt)
5829 RETURN(pflr_id(mirror_id, id));
5831 if (end == LCME_ID_MAX) {
5833 end = min(lo->ldo_layout_gen & LCME_ID_MASK,
5834 (__u32)(LCME_ID_MAX - 1));
5838 RETURN(LCME_ID_INVAL);
5842 * Creation of a striped regular object.
5844 * The function is called to create the stripe objects for a regular
5845 * striped file. This can happen at the initial object creation or
5846 * when the caller asks LOD to do so using ->do_xattr_set() method
5847 * (so called late striping). Notice all the information are already
5848 * prepared in the form of the list of objects (ldo_stripe field).
5849 * This is done during declare phase.
5851 * \param[in] env execution environment
5852 * \param[in] dt object
5853 * \param[in] attr attributes the stripes will be created with
5854 * \param[in] dof format of stripes (see OSD API description)
5855 * \param[in] th transaction handle
5857 * \retval 0 on success
5858 * \retval negative if failed
5860 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
5861 struct lu_attr *attr, struct dt_object_format *dof,
5864 struct lod_layout_component *lod_comp;
5865 struct lod_object *lo = lod_dt_obj(dt);
5870 LASSERT((lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL) ||
5871 lo->ldo_is_foreign);
5873 mirror_id = 0; /* non-flr file's mirror_id is 0 */
5874 if (lo->ldo_mirror_count > 1) {
5875 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5876 lod_comp = &lo->ldo_comp_entries[i];
5877 if (lod_comp->llc_id != LCME_ID_INVAL &&
5878 mirror_id_of(lod_comp->llc_id) > mirror_id)
5879 mirror_id = mirror_id_of(lod_comp->llc_id);
5883 /* create all underlying objects */
5884 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5885 lod_comp = &lo->ldo_comp_entries[i];
5887 if (lod_comp->llc_id == LCME_ID_INVAL) {
5888 /* only the component of FLR layout with more than 1
5889 * mirror has mirror ID in its component ID.
5891 if (lod_comp->llc_extent.e_start == 0 &&
5892 lo->ldo_mirror_count > 1)
5895 lod_comp->llc_id = lod_gen_component_id(lo,
5897 if (lod_comp->llc_id == LCME_ID_INVAL)
5898 GOTO(out, rc = -ERANGE);
5901 if (lod_comp_inited(lod_comp))
5904 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
5905 lod_comp_set_init(lod_comp);
5907 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
5908 lod_comp_set_init(lod_comp);
5910 if (lod_comp->llc_stripe == NULL)
5913 LASSERT(lod_comp->llc_stripe_count);
5914 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
5915 struct dt_object *object = lod_comp->llc_stripe[j];
5916 LASSERT(object != NULL);
5917 rc = lod_sub_create(env, object, attr, NULL, dof, th);
5921 lod_comp_set_init(lod_comp);
5924 rc = lod_fill_mirrors(lo);
5928 rc = lod_generate_and_set_lovea(env, lo, th);
5932 lo->ldo_comp_cached = 1;
5936 lod_striping_free(env, lo);
5940 static inline bool lod_obj_is_dom(struct dt_object *dt)
5942 struct lod_object *lo = lod_dt_obj(dt);
5944 if (!dt_object_exists(dt_object_child(dt)))
5947 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
5950 if (!lo->ldo_comp_cnt)
5953 return (lov_pattern(lo->ldo_comp_entries[0].llc_pattern) ==
5958 * Implementation of dt_object_operations::do_create.
5960 * If any of preceeding methods (like ->do_declare_create(),
5961 * ->do_ah_init(), etc) chose to create a striped object,
5962 * then this method will create the master and the stripes.
5964 * \see dt_object_operations::do_create() in the API description for details.
5966 static int lod_create(const struct lu_env *env, struct dt_object *dt,
5967 struct lu_attr *attr, struct dt_allocation_hint *hint,
5968 struct dt_object_format *dof, struct thandle *th)
5973 /* create local object */
5974 rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
5978 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
5979 (lod_obj_is_striped(dt) || lod_obj_is_dom(dt)) &&
5980 dof->u.dof_reg.striped != 0) {
5981 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
5982 rc = lod_striped_create(env, dt, attr, dof, th);
5989 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
5990 struct dt_object *dt, struct thandle *th,
5991 int comp_idx, int stripe_idx,
5992 struct lod_obj_stripe_cb_data *data)
5994 if (data->locd_declare)
5995 return lod_sub_declare_destroy(env, dt, th);
5996 else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
5997 stripe_idx == cfs_fail_val)
5998 return lod_sub_destroy(env, dt, th);
6004 * Implementation of dt_object_operations::do_declare_destroy.
6006 * If the object is a striped directory, then the function declares reference
6007 * removal from the master object (this is an index) to the stripes and declares
6008 * destroy of all the stripes. In all the cases, it declares an intention to
6009 * destroy the object itself.
6011 * \see dt_object_operations::do_declare_destroy() in the API description
6014 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
6017 struct dt_object *next = dt_object_child(dt);
6018 struct lod_object *lo = lod_dt_obj(dt);
6019 struct lod_thread_info *info = lod_env_info(env);
6020 struct dt_object *stripe;
6021 char *stripe_name = info->lti_key;
6027 * load striping information, notice we don't do this when object
6028 * is being initialized as we don't need this information till
6029 * few specific cases like destroy, chown
6031 rc = lod_striping_load(env, lo);
6035 /* declare destroy for all underlying objects */
6036 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6037 rc = next->do_ops->do_index_try(env, next,
6038 &dt_directory_features);
6042 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6043 stripe = lo->ldo_stripe[i];
6047 rc = lod_sub_declare_ref_del(env, next, th);
6051 snprintf(stripe_name, sizeof(info->lti_key),
6053 PFID(lu_object_fid(&stripe->do_lu)), i);
6054 rc = lod_sub_declare_delete(env, next,
6055 (const struct dt_key *)stripe_name, th);
6062 * we declare destroy for the local object
6064 rc = lod_sub_declare_destroy(env, next, th);
6068 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
6069 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
6072 if (!lod_obj_is_striped(dt))
6075 /* declare destroy all striped objects */
6076 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6077 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6078 stripe = lo->ldo_stripe[i];
6082 if (!dt_object_exists(stripe))
6085 rc = lod_sub_declare_ref_del(env, stripe, th);
6089 rc = lod_sub_declare_destroy(env, stripe, th);
6094 struct lod_obj_stripe_cb_data data = { { 0 } };
6096 data.locd_declare = true;
6097 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6098 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6105 * Implementation of dt_object_operations::do_destroy.
6107 * If the object is a striped directory, then the function removes references
6108 * from the master object (this is an index) to the stripes and destroys all
6109 * the stripes. In all the cases, the function destroys the object itself.
6111 * \see dt_object_operations::do_destroy() in the API description for details.
6113 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
6116 struct dt_object *next = dt_object_child(dt);
6117 struct lod_object *lo = lod_dt_obj(dt);
6118 struct lod_thread_info *info = lod_env_info(env);
6119 char *stripe_name = info->lti_key;
6120 struct dt_object *stripe;
6126 /* destroy sub-stripe of master object */
6127 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6128 rc = next->do_ops->do_index_try(env, next,
6129 &dt_directory_features);
6133 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6134 stripe = lo->ldo_stripe[i];
6138 rc = lod_sub_ref_del(env, next, th);
6142 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
6143 PFID(lu_object_fid(&stripe->do_lu)), i);
6145 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
6146 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
6147 PFID(lu_object_fid(&stripe->do_lu)));
6149 rc = lod_sub_delete(env, next,
6150 (const struct dt_key *)stripe_name, th);
6156 rc = lod_sub_destroy(env, next, th);
6160 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
6161 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
6164 if (!lod_obj_is_striped(dt))
6167 /* destroy all striped objects */
6168 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6169 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6170 stripe = lo->ldo_stripe[i];
6174 if (!dt_object_exists(stripe))
6177 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
6178 i == cfs_fail_val) {
6179 dt_write_lock(env, stripe, DT_TGT_CHILD);
6180 rc = lod_sub_ref_del(env, stripe, th);
6181 dt_write_unlock(env, stripe);
6185 rc = lod_sub_destroy(env, stripe, th);
6191 struct lod_obj_stripe_cb_data data = { { 0 } };
6193 data.locd_declare = false;
6194 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6195 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6202 * Implementation of dt_object_operations::do_declare_ref_add.
6204 * \see dt_object_operations::do_declare_ref_add() in the API description
6207 static int lod_declare_ref_add(const struct lu_env *env,
6208 struct dt_object *dt, struct thandle *th)
6210 return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
6214 * Implementation of dt_object_operations::do_ref_add.
6216 * \see dt_object_operations::do_ref_add() in the API description for details.
6218 static int lod_ref_add(const struct lu_env *env,
6219 struct dt_object *dt, struct thandle *th)
6221 return lod_sub_ref_add(env, dt_object_child(dt), th);
6225 * Implementation of dt_object_operations::do_declare_ref_del.
6227 * \see dt_object_operations::do_declare_ref_del() in the API description
6230 static int lod_declare_ref_del(const struct lu_env *env,
6231 struct dt_object *dt, struct thandle *th)
6233 return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
6237 * Implementation of dt_object_operations::do_ref_del
6239 * \see dt_object_operations::do_ref_del() in the API description for details.
6241 static int lod_ref_del(const struct lu_env *env,
6242 struct dt_object *dt, struct thandle *th)
6244 return lod_sub_ref_del(env, dt_object_child(dt), th);
6248 * Implementation of dt_object_operations::do_object_sync.
6250 * \see dt_object_operations::do_object_sync() in the API description
6253 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
6254 __u64 start, __u64 end)
6256 return dt_object_sync(env, dt_object_child(dt), start, end);
6260 * Implementation of dt_object_operations::do_object_unlock.
6262 * Used to release LDLM lock(s).
6264 * \see dt_object_operations::do_object_unlock() in the API description
6267 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
6268 struct ldlm_enqueue_info *einfo,
6269 union ldlm_policy_data *policy)
6271 struct lod_object *lo = lod_dt_obj(dt);
6272 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
6273 int slave_locks_size;
6277 if (slave_locks == NULL)
6280 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
6281 /* Note: for remote lock for single stripe dir, MDT will cancel
6282 * the lock by lockh directly */
6283 LASSERT(!dt_object_remote(dt_object_child(dt)));
6285 /* locks were unlocked in MDT layer */
6286 for (i = 0; i < slave_locks->ha_count; i++)
6287 LASSERT(!lustre_handle_is_used(&slave_locks->ha_handles[i]));
6290 * NB, ha_count may not equal to ldo_dir_stripe_count, because dir
6291 * layout may change, e.g., shrink dir layout after migration.
6293 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6294 if (lo->ldo_stripe[i])
6295 dt_invalidate(env, lo->ldo_stripe[i]);
6298 slave_locks_size = offsetof(typeof(*slave_locks),
6299 ha_handles[slave_locks->ha_count]);
6300 OBD_FREE(slave_locks, slave_locks_size);
6301 einfo->ei_cbdata = NULL;
6307 * Implementation of dt_object_operations::do_object_lock.
6309 * Used to get LDLM lock on the non-striped and striped objects.
6311 * \see dt_object_operations::do_object_lock() in the API description
6314 static int lod_object_lock(const struct lu_env *env,
6315 struct dt_object *dt,
6316 struct lustre_handle *lh,
6317 struct ldlm_enqueue_info *einfo,
6318 union ldlm_policy_data *policy)
6320 struct lod_object *lo = lod_dt_obj(dt);
6321 int slave_locks_size;
6322 struct lustre_handle_array *slave_locks = NULL;
6327 /* remote object lock */
6328 if (!einfo->ei_enq_slave) {
6329 LASSERT(dt_object_remote(dt));
6330 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
6334 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
6337 rc = lod_striping_load(env, lo);
6342 if (lo->ldo_dir_stripe_count <= 1)
6345 slave_locks_size = offsetof(typeof(*slave_locks),
6346 ha_handles[lo->ldo_dir_stripe_count]);
6347 /* Freed in lod_object_unlock */
6348 OBD_ALLOC(slave_locks, slave_locks_size);
6351 slave_locks->ha_count = lo->ldo_dir_stripe_count;
6353 /* striped directory lock */
6354 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6355 struct lustre_handle lockh;
6356 struct ldlm_res_id *res_id;
6357 struct dt_object *stripe;
6359 stripe = lo->ldo_stripe[i];
6363 res_id = &lod_env_info(env)->lti_res_id;
6364 fid_build_reg_res_name(lu_object_fid(&stripe->do_lu), res_id);
6365 einfo->ei_res_id = res_id;
6367 if (dt_object_remote(stripe)) {
6368 set_bit(i, (void *)slave_locks->ha_map);
6369 rc = dt_object_lock(env, stripe, &lockh, einfo, policy);
6371 struct ldlm_namespace *ns = einfo->ei_namespace;
6372 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
6373 ldlm_completion_callback completion = einfo->ei_cb_cp;
6374 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
6376 if (einfo->ei_mode == LCK_PW ||
6377 einfo->ei_mode == LCK_EX)
6378 dlmflags |= LDLM_FL_COS_INCOMPAT;
6380 LASSERT(ns != NULL);
6381 rc = ldlm_cli_enqueue_local(env, ns, res_id, LDLM_IBITS,
6382 policy, einfo->ei_mode,
6383 &dlmflags, blocking,
6385 NULL, 0, LVB_T_NONE,
6390 ldlm_lock_decref_and_cancel(
6391 &slave_locks->ha_handles[i],
6393 OBD_FREE(slave_locks, slave_locks_size);
6396 slave_locks->ha_handles[i] = lockh;
6398 einfo->ei_cbdata = slave_locks;
6404 * Implementation of dt_object_operations::do_invalidate.
6406 * \see dt_object_operations::do_invalidate() in the API description for details
6408 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
6410 return dt_invalidate(env, dt_object_child(dt));
6413 static int lod_declare_instantiate_components(const struct lu_env *env,
6414 struct lod_object *lo, struct thandle *th)
6416 struct lod_thread_info *info = lod_env_info(env);
6421 LASSERT(info->lti_count < lo->ldo_comp_cnt);
6423 for (i = 0; i < info->lti_count; i++) {
6424 rc = lod_qos_prep_create(env, lo, NULL, th,
6425 info->lti_comp_idx[i]);
6431 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6432 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
6433 &info->lti_buf, XATTR_NAME_LOV, 0, th);
6440 * Check OSTs for an existing component for further extension
6442 * Checks if OSTs are still healthy and not out of space. Gets free space
6443 * on OSTs (relative to allocation watermark rmb_low) and compares to
6444 * the proposed new_end for this component.
6446 * Decides whether or not to extend a component on its current OSTs.
6448 * \param[in] env execution environment for this thread
6449 * \param[in] lo object we're checking
6450 * \param[in] index index of this component
6451 * \param[in] extension_size extension size for this component
6452 * \param[in] extent layout extent for requested operation
6453 * \param[in] comp_extent extension component extent
6454 * \param[in] write if this is write operation
6456 * \retval true - OK to extend on current OSTs
6457 * \retval false - do not extend on current OSTs
6459 static bool lod_sel_osts_allowed(const struct lu_env *env,
6460 struct lod_object *lo,
6461 int index, __u64 extension_size,
6462 struct lu_extent *extent,
6463 struct lu_extent *comp_extent, int write)
6465 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[index];
6466 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6467 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
6468 __u64 available = 0;
6475 LASSERT(lod_comp->llc_stripe_count != 0);
6478 (extent->e_start == 0 && extent->e_end == OBD_OBJECT_EOF)) {
6479 /* truncate or append */
6480 size = extension_size;
6482 /* In case of write op, check the real write extent,
6483 * it may be larger than the extension_size */
6484 size = roundup(min(extent->e_end, comp_extent->e_end) -
6485 max(extent->e_start, comp_extent->e_start),
6488 /* extension_size is file level, so we must divide by stripe count to
6489 * compare it to available space on a single OST */
6490 size /= lod_comp->llc_stripe_count;
6492 lod_getref(&lod->lod_ost_descs);
6493 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
6494 int index = lod_comp->llc_ost_indices[i];
6495 struct lod_tgt_desc *ost = OST_TGT(lod, index);
6496 struct obd_statfs_info info = { 0 };
6497 int j, repeated = 0;
6501 /* Get the number of times this OST repeats in this component.
6502 * Note: inter-component repeats are not counted as this is
6503 * considered as a rare case: we try to not repeat OST in other
6504 * components if possible. */
6505 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6506 if (index != lod_comp->llc_ost_indices[j])
6509 /* already handled */
6515 if (j < lod_comp->llc_stripe_count)
6518 if (!cfs_bitmap_check(lod->lod_ost_bitmap, index)) {
6519 CDEBUG(D_LAYOUT, "ost %d no longer present\n", index);
6524 rc = dt_statfs_info(env, ost->ltd_tgt, sfs, &info);
6526 CDEBUG(D_LAYOUT, "statfs failed for ost %d, error %d\n",
6532 if (sfs->os_state & OS_STATE_ENOSPC ||
6533 sfs->os_state & OS_STATE_READONLY ||
6534 sfs->os_state & OS_STATE_DEGRADED) {
6535 CDEBUG(D_LAYOUT, "ost %d is not availble for SEL "
6536 "extension, state %u\n", index, sfs->os_state);
6542 available = sfs->os_bavail * sfs->os_bsize;
6543 /* 'available' is relative to the allocation threshold */
6544 available -= (__u64) info.os_reserved_mb_low << 20;
6546 CDEBUG(D_LAYOUT, "ost %d lowwm: %d highwm: %d, "
6547 "%llu %% blocks available, %llu %% blocks free\n",
6548 index, info.os_reserved_mb_low, info.os_reserved_mb_high,
6549 (100ull * sfs->os_bavail) / sfs->os_blocks,
6550 (100ull * sfs->os_bfree) / sfs->os_blocks);
6552 if (size * repeated > available) {
6554 CDEBUG(D_LAYOUT, "low space on ost %d, available %llu "
6555 "< extension size %llu\n", index, available,
6560 lod_putref(lod, &lod->lod_ost_descs);
6566 * Adjust extents after component removal
6568 * When we remove an extension component, we move the start of the next
6569 * component to match the start of the extension component, so no space is left
6572 * \param[in] env execution environment for this thread
6573 * \param[in] lo object
6574 * \param[in] max_comp layout component
6575 * \param[in] index index of this component
6577 * \retval 0 on success
6578 * \retval negative errno on error
6580 static void lod_sel_adjust_extents(const struct lu_env *env,
6581 struct lod_object *lo,
6582 int max_comp, int index)
6584 struct lod_layout_component *lod_comp = NULL;
6585 struct lod_layout_component *next = NULL;
6586 struct lod_layout_component *prev = NULL;
6587 __u64 new_start = 0;
6591 /* Extension space component */
6592 lod_comp = &lo->ldo_comp_entries[index];
6593 next = &lo->ldo_comp_entries[index + 1];
6594 prev = &lo->ldo_comp_entries[index - 1];
6596 LASSERT(lod_comp != NULL && prev != NULL && next != NULL);
6597 LASSERT(lod_comp->llc_flags & LCME_FL_EXTENSION);
6599 /* Previous is being removed */
6600 if (prev && prev->llc_id == LCME_ID_INVAL)
6601 new_start = prev->llc_extent.e_start;
6603 new_start = lod_comp->llc_extent.e_start;
6605 for (i = index + 1; i < max_comp; i++) {
6606 lod_comp = &lo->ldo_comp_entries[i];
6608 start = lod_comp->llc_extent.e_start;
6609 lod_comp->llc_extent.e_start = new_start;
6611 /* We only move zero length extendable components */
6612 if (!(start == lod_comp->llc_extent.e_end))
6615 LASSERT(!(lod_comp->llc_flags & LCME_FL_INIT));
6617 lod_comp->llc_extent.e_end = new_start;
6621 /* Calculate the proposed 'new end' for a component we're extending */
6622 static __u64 lod_extension_new_end(__u64 extension_size, __u64 extent_end,
6623 __u32 stripe_size, __u64 component_end,
6624 __u64 extension_end)
6628 LASSERT(extension_size != 0 && stripe_size != 0);
6630 /* Round up to extension size */
6631 if (extent_end == OBD_OBJECT_EOF) {
6632 new_end = OBD_OBJECT_EOF;
6634 /* Add at least extension_size to the previous component_end,
6635 * covering the req layout extent */
6636 new_end = max(extent_end - component_end, extension_size);
6637 new_end = roundup(new_end, extension_size);
6638 new_end += component_end;
6640 /* Component end must be min stripe size aligned */
6641 if (new_end % stripe_size) {
6642 CDEBUG(D_LAYOUT, "new component end is not aligned "
6643 "by the stripe size %u: [%llu, %llu) ext size "
6644 "%llu new end %llu, aligning\n",
6645 stripe_size, component_end, extent_end,
6646 extension_size, new_end);
6647 new_end = roundup(new_end, stripe_size);
6651 if (new_end < extent_end)
6652 new_end = OBD_OBJECT_EOF;
6655 /* Don't extend past the end of the extension component */
6656 if (new_end > extension_end)
6657 new_end = extension_end;
6662 /* As lod_sel_handler() could be re-entered for the same component several
6663 * times, this is the data for the next call. Fields could be changed to
6664 * component indexes when needed, (e.g. if there is no need to instantiate
6665 * all the previous components up to the current position) to tell the caller
6666 * where to start over from. */
6673 * Process extent updates for a particular layout component
6675 * Handle layout updates for a particular extension space component touched by
6676 * a layout update operation. Core function of self-extending PFL feature.
6678 * In general, this function processes exactly *one* stage of an extension
6679 * operation, modifying the layout accordingly, then returns to the caller.
6680 * The caller is responsible for restarting processing with the new layout,
6681 * which may repeatedly return to this function until the extension updates
6684 * This function does one of a few things to the layout:
6685 * 1. Extends the component before the current extension space component to
6686 * allow it to accomodate the requested operation (if space/policy permit that
6687 * component to continue on its current OSTs)
6689 * 2. If extension of the existing component fails, we do one of two things:
6690 * a. If there is a component after the extension space, we remove the
6691 * extension space component, move the start of the next component down
6692 * accordingly, then notify the caller to restart processing w/the new
6694 * b. If there is no following component, we try repeating the current
6695 * component, creating a new component using the current one as a
6696 * template (keeping its stripe properties but not specific striping),
6697 * and try assigning striping for this component. If there is sufficient
6698 * free space on the OSTs chosen for this component, it is instantiated
6699 * and i/o continues there.
6701 * If there is not sufficient space on the new OSTs, we remove this new
6702 * component & extend the current component.
6704 * Note further that uninited components followed by extension space can be zero
6705 * length meaning that we will try to extend them before initializing them, and
6706 * if that fails, they will be removed without initialization.
6708 * 3. If we extend to/beyond the end of an extension space component, that
6709 * component is exhausted (all of its range has been given to real components),
6710 * so we remove it and restart processing.
6712 * \param[in] env execution environment for this thread
6713 * \param[in,out] lo object to update the layout of
6714 * \param[in] extent layout extent for requested operation, update
6715 * layout to fit this operation
6716 * \param[in] th transaction handle for this operation
6717 * \param[in,out] max_comp the highest comp for the portion of the layout
6718 * we are operating on (For FLR, the chosen
6719 * replica). Updated because we may remove
6721 * \param[in] index index of the extension space component we're
6723 * \param[in] write if this is write op
6724 * \param[in,out] force if the extension is to be forced; set here
6725 to force it on the 2nd call for the same
6728 * \retval 0 on success
6729 * \retval negative errno on error
6731 static int lod_sel_handler(const struct lu_env *env,
6732 struct lod_object *lo,
6733 struct lu_extent *extent,
6734 struct thandle *th, int *max_comp,
6735 int index, int write,
6736 struct sel_data *sd)
6738 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6739 struct lod_thread_info *info = lod_env_info(env);
6740 struct lod_layout_component *lod_comp;
6741 struct lod_layout_component *prev;
6742 struct lod_layout_component *next = NULL;
6743 __u64 extension_size;
6750 /* First component cannot be extension space */
6752 CERROR("%s: "DFID" first component cannot be extension space\n",
6753 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6757 lod_comp = &lo->ldo_comp_entries[index];
6758 prev = &lo->ldo_comp_entries[index - 1];
6759 if ((index + 1) < *max_comp)
6760 next = &lo->ldo_comp_entries[index + 1];
6762 /* extension size uses the stripe size field as KiB */
6763 extension_size = lod_comp->llc_stripe_size * SEL_UNIT_SIZE;
6765 CDEBUG(D_LAYOUT, "prev start %llu, extension start %llu, extension end"
6766 " %llu, extension size %llu\n", prev->llc_extent.e_start,
6767 lod_comp->llc_extent.e_start, lod_comp->llc_extent.e_end,
6770 /* Two extension space components cannot be adjacent & extension space
6771 * components cannot be init */
6772 if ((prev->llc_flags & LCME_FL_EXTENSION) ||
6773 !(ergo(next, !(next->llc_flags & LCME_FL_EXTENSION))) ||
6774 lod_comp_inited(lod_comp)) {
6775 CERROR("%s: "DFID" invalid extension space components\n",
6776 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6780 if (!prev->llc_stripe) {
6781 CDEBUG(D_LAYOUT, "Previous component not inited\n");
6782 info->lti_count = 1;
6783 info->lti_comp_idx[0] = index - 1;
6784 rc = lod_declare_instantiate_components(env, lo, th);
6785 /* ENOSPC tells us we can't use this component. If there is
6786 * a next or we are repeating, we either spill over (next) or
6787 * extend the original comp (repeat). Otherwise, return the
6788 * error to the user. */
6789 if (rc == -ENOSPC && (next || sd->sd_repeat))
6795 if (sd->sd_force == 0 && rc == 0)
6796 rc = !lod_sel_osts_allowed(env, lo, index - 1,
6797 extension_size, extent,
6798 &lod_comp->llc_extent, write);
6800 repeated = !!(sd->sd_repeat);
6804 /* Extend previous component */
6806 new_end = lod_extension_new_end(extension_size, extent->e_end,
6807 prev->llc_stripe_size,
6808 prev->llc_extent.e_end,
6809 lod_comp->llc_extent.e_end);
6811 CDEBUG(D_LAYOUT, "new end %llu\n", new_end);
6812 lod_comp->llc_extent.e_start = new_end;
6813 prev->llc_extent.e_end = new_end;
6815 if (prev->llc_extent.e_end == lod_comp->llc_extent.e_end) {
6816 CDEBUG(D_LAYOUT, "Extension component exhausted\n");
6817 lod_comp->llc_id = LCME_ID_INVAL;
6821 /* rc == 1, failed to extend current component */
6824 /* Normal 'spillover' case - Remove the extension
6825 * space component & bring down the start of the next
6827 lod_comp->llc_id = LCME_ID_INVAL;
6829 if (!(prev->llc_flags & LCME_FL_INIT)) {
6830 prev->llc_id = LCME_ID_INVAL;
6833 lod_sel_adjust_extents(env, lo, *max_comp, index);
6834 } else if (lod_comp_inited(prev)) {
6835 /* If there is no next, and the previous component is
6836 * INIT'ed, try repeating the previous component. */
6837 LASSERT(repeated == 0);
6838 rc = lod_layout_repeat_comp(env, lo, index - 1);
6842 /* The previous component is a repeated component.
6843 * Record this so we don't keep trying to repeat it. */
6846 /* If the previous component is not INIT'ed, this may
6847 * be a component we have just instantiated but failed
6848 * to extend. Or even a repeated component we failed
6849 * to prepare a striping for. Do not repeat but instead
6850 * remove the repeated component & force the extention
6851 * of the original one */
6854 prev->llc_id = LCME_ID_INVAL;
6861 rc = lod_layout_del_prep_layout(env, lo, NULL);
6864 LASSERTF(-rc == change,
6865 "number deleted %d != requested %d\n", -rc,
6868 *max_comp = *max_comp + change;
6870 /* lod_del_prep_layout reallocates ldo_comp_entries, so we must
6871 * refresh these pointers before using them */
6872 lod_comp = &lo->ldo_comp_entries[index];
6873 prev = &lo->ldo_comp_entries[index - 1];
6874 CDEBUG(D_LAYOUT, "After extent updates: prev start %llu, current start "
6875 "%llu, current end %llu max_comp %d ldo_comp_cnt %d\n",
6876 prev->llc_extent.e_start, lod_comp->llc_extent.e_start,
6877 lod_comp->llc_extent.e_end, *max_comp, lo->ldo_comp_cnt);
6879 /* Layout changed successfully */
6884 * Declare layout extent updates
6886 * Handles extensions. Identifies extension components touched by current
6887 * operation and passes them to processing function.
6889 * Restarts with updated layouts from the processing function until the current
6890 * operation no longer touches an extension space component.
6892 * \param[in] env execution environment for this thread
6893 * \param[in,out] lo object to update the layout of
6894 * \param[in] extent layout extent for requested operation, update layout to
6895 * fit this operation
6896 * \param[in] th transaction handle for this operation
6897 * \param[in] pick identifies chosen mirror for FLR layouts
6898 * \param[in] write if this is write op
6900 * \retval 1 on layout changed, 0 on no change
6901 * \retval negative errno on error
6903 static int lod_declare_update_extents(const struct lu_env *env,
6904 struct lod_object *lo, struct lu_extent *extent,
6905 struct thandle *th, int pick, int write)
6907 struct lod_thread_info *info = lod_env_info(env);
6908 struct lod_layout_component *lod_comp;
6909 bool layout_changed = false;
6910 struct sel_data sd = { 0 };
6918 /* This makes us work on the components of the chosen mirror */
6919 start_index = lo->ldo_mirrors[pick].lme_start;
6920 max_comp = lo->ldo_mirrors[pick].lme_end + 1;
6921 if (lo->ldo_flr_state == LCM_FL_NONE)
6922 LASSERT(start_index == 0 && max_comp == lo->ldo_comp_cnt);
6924 CDEBUG(D_LAYOUT, "extent->e_start %llu, extent->e_end %llu\n",
6925 extent->e_start, extent->e_end);
6926 for (i = start_index; i < max_comp; i++) {
6927 lod_comp = &lo->ldo_comp_entries[i];
6929 /* We've passed all components of interest */
6930 if (lod_comp->llc_extent.e_start >= extent->e_end)
6933 if (lod_comp->llc_flags & LCME_FL_EXTENSION) {
6934 layout_changed = true;
6935 rc = lod_sel_handler(env, lo, extent, th, &max_comp,
6940 /* Nothing has changed behind the prev one */
6946 /* We may have added or removed components. If so, we must update the
6947 * start & ends of all the mirrors after the current one, and the end
6948 * of the current mirror. */
6949 change = max_comp - 1 - lo->ldo_mirrors[pick].lme_end;
6951 lo->ldo_mirrors[pick].lme_end += change;
6952 for (i = pick + 1; i < lo->ldo_mirror_count; i++) {
6953 lo->ldo_mirrors[i].lme_start += change;
6954 lo->ldo_mirrors[i].lme_end += change;
6960 /* The amount of components has changed, adjust the lti_comp_idx */
6961 rc2 = lod_layout_data_init(info, lo->ldo_comp_cnt);
6963 return rc < 0 ? rc : rc2 < 0 ? rc2 : layout_changed;
6966 /* If striping is already instantiated or INIT'ed DOM? */
6967 static bool lod_is_instantiation_needed(struct lod_layout_component *comp)
6969 return !(((lov_pattern(comp->llc_pattern) == LOV_PATTERN_MDT) &&
6970 lod_comp_inited(comp)) || comp->llc_stripe);
6974 * Declare layout update for a non-FLR layout.
6976 * \param[in] env execution environment for this thread
6977 * \param[in,out] lo object to update the layout of
6978 * \param[in] layout layout intent for requested operation, "update" is
6979 * a process of reacting to this
6980 * \param[in] buf buffer containing lov ea (see comment on usage inline)
6981 * \param[in] th transaction handle for this operation
6983 * \retval 0 on success
6984 * \retval negative errno on error
6986 static int lod_declare_update_plain(const struct lu_env *env,
6987 struct lod_object *lo, struct layout_intent *layout,
6988 const struct lu_buf *buf, struct thandle *th)
6990 struct lod_thread_info *info = lod_env_info(env);
6991 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6992 struct lod_layout_component *lod_comp;
6993 struct lov_comp_md_v1 *comp_v1 = NULL;
6994 bool layout_changed = false;
6995 bool replay = false;
6999 LASSERT(lo->ldo_flr_state == LCM_FL_NONE);
7002 * In case the client is passing lovea, which only happens during
7003 * the replay of layout intent write RPC for now, we may need to
7004 * parse the lovea and apply new layout configuration.
7006 if (buf && buf->lb_len) {
7007 struct lov_user_md_v1 *v1 = buf->lb_buf;
7009 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
7010 v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
7011 LOV_MAGIC_COMP_V1)) {
7012 CERROR("%s: the replay buffer of layout extend "
7013 "(magic %#x) does not contain expected "
7014 "composite layout.\n",
7015 lod2obd(d)->obd_name, v1->lmm_magic);
7016 GOTO(out, rc = -EINVAL);
7019 rc = lod_use_defined_striping(env, lo, buf);
7022 lo->ldo_comp_cached = 1;
7024 rc = lod_get_lov_ea(env, lo);
7027 /* old on-disk EA is stored in info->lti_buf */
7028 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
7030 layout_changed = true;
7032 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
7036 /* non replay path */
7037 rc = lod_striping_load(env, lo);
7042 /* Make sure defined layout covers the requested write range. */
7043 lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
7044 if (lo->ldo_comp_cnt > 1 &&
7045 lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
7046 lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
7047 CDEBUG_LIMIT(replay ? D_ERROR : D_LAYOUT,
7048 "%s: the defined layout [0, %#llx) does not "
7049 "covers the write range "DEXT"\n",
7050 lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
7051 PEXT(&layout->li_extent));
7052 GOTO(out, rc = -EINVAL);
7055 CDEBUG(D_LAYOUT, "%s: "DFID": update components "DEXT"\n",
7056 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
7057 PEXT(&layout->li_extent));
7060 rc = lod_declare_update_extents(env, lo, &layout->li_extent,
7061 th, 0, layout->li_opc == LAYOUT_INTENT_WRITE);
7065 layout_changed = true;
7069 * Iterate ld->ldo_comp_entries, find the component whose extent under
7070 * the write range and not instantianted.
7072 for (i = 0; i < lo->ldo_comp_cnt; i++) {
7073 lod_comp = &lo->ldo_comp_entries[i];
7075 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
7079 /* If striping is instantiated or INIT'ed DOM skip */
7080 if (!lod_is_instantiation_needed(lod_comp))
7084 * In replay path, lod_comp is the EA passed by
7085 * client replay buffer, comp_v1 is the pre-recovery
7086 * on-disk EA, we'd sift out those components which
7087 * were init-ed in the on-disk EA.
7089 if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
7094 * this component hasn't instantiated in normal path, or during
7095 * replay it needs replay the instantiation.
7098 /* A released component is being extended */
7099 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
7100 GOTO(out, rc = -EINVAL);
7102 LASSERT(info->lti_comp_idx != NULL);
7103 info->lti_comp_idx[info->lti_count++] = i;
7104 layout_changed = true;
7107 if (!layout_changed)
7110 lod_obj_inc_layout_gen(lo);
7111 rc = lod_declare_instantiate_components(env, lo, th);
7115 lod_striping_free(env, lo);
7119 static inline int lod_comp_index(struct lod_object *lo,
7120 struct lod_layout_component *lod_comp)
7122 LASSERT(lod_comp >= lo->ldo_comp_entries &&
7123 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
7125 return lod_comp - lo->ldo_comp_entries;
7129 * Stale other mirrors by writing extent.
7131 static int lod_stale_components(const struct lu_env *env, struct lod_object *lo,
7132 int primary, struct lu_extent *extent,
7135 struct lod_layout_component *pri_comp, *lod_comp;
7136 struct lu_extent pri_extent;
7141 /* The writing extent decides which components in the primary
7142 * are affected... */
7143 CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
7146 lod_foreach_mirror_comp(pri_comp, lo, primary) {
7147 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
7150 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
7151 lod_comp_index(lo, pri_comp),
7152 PEXT(&pri_comp->llc_extent));
7154 pri_extent.e_start = pri_comp->llc_extent.e_start;
7155 pri_extent.e_end = pri_comp->llc_extent.e_end;
7157 for (i = 0; i < lo->ldo_mirror_count; i++) {
7160 rc = lod_declare_update_extents(env, lo, &pri_extent,
7162 /* if update_extents changed the layout, it may have
7163 * reallocated the component array, so start over to
7164 * avoid using stale pointers */
7170 /* ... and then stale other components that are
7171 * overlapping with primary components */
7172 lod_foreach_mirror_comp(lod_comp, lo, i) {
7173 if (!lu_extent_is_overlapped(
7175 &lod_comp->llc_extent))
7178 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
7179 i, lod_comp_index(lo, lod_comp));
7181 lod_comp->llc_flags |= LCME_FL_STALE;
7182 lo->ldo_mirrors[i].lme_stale = 1;
7191 * check an OST's availability
7192 * \param[in] env execution environment
7193 * \param[in] lo lod object
7194 * \param[in] dt dt object
7195 * \param[in] index mirror index
7197 * \retval negative if failed
7198 * \retval 1 if \a dt is available
7199 * \retval 0 if \a dt is not available
7201 static inline int lod_check_ost_avail(const struct lu_env *env,
7202 struct lod_object *lo,
7203 struct dt_object *dt, int index)
7205 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7206 struct lod_tgt_desc *ost;
7208 int type = LU_SEQ_RANGE_OST;
7211 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &idx, &type);
7213 CERROR("%s: can't locate "DFID":rc = %d\n",
7214 lod2obd(lod)->obd_name, PFID(lu_object_fid(&dt->do_lu)),
7219 ost = OST_TGT(lod, idx);
7220 if (ost->ltd_statfs.os_state &
7221 (OS_STATE_READONLY | OS_STATE_ENOSPC | OS_STATE_ENOINO |
7222 OS_STATE_NOPRECREATE) ||
7223 ost->ltd_active == 0) {
7224 CDEBUG(D_LAYOUT, DFID ": mirror %d OST%d unavail, rc = %d\n",
7225 PFID(lod_object_fid(lo)), index, idx, rc);
7233 * Pick primary mirror for write
7234 * \param[in] env execution environment
7235 * \param[in] lo object
7236 * \param[in] extent write range
7238 static int lod_primary_pick(const struct lu_env *env, struct lod_object *lo,
7239 struct lu_extent *extent)
7241 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7242 unsigned int seq = 0;
7243 struct lod_layout_component *lod_comp;
7245 int picked = -1, second_pick = -1, third_pick = -1;
7248 if (OBD_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
7249 get_random_bytes(&seq, sizeof(seq));
7250 seq %= lo->ldo_mirror_count;
7254 * Pick a mirror as the primary, and check the availability of OSTs.
7256 * This algo can be revised later after knowing the topology of
7259 lod_qos_statfs_update(env, lod, &lod->lod_ost_descs);
7260 for (i = 0; i < lo->ldo_mirror_count; i++) {
7261 bool ost_avail = true;
7262 int index = (i + seq) % lo->ldo_mirror_count;
7264 if (lo->ldo_mirrors[index].lme_stale) {
7265 CDEBUG(D_LAYOUT, DFID": mirror %d stale\n",
7266 PFID(lod_object_fid(lo)), index);
7270 /* 2nd pick is for the primary mirror containing unavail OST */
7271 if (lo->ldo_mirrors[index].lme_primary && second_pick < 0)
7272 second_pick = index;
7274 /* 3rd pick is for non-primary mirror containing unavail OST */
7275 if (second_pick < 0 && third_pick < 0)
7279 * we found a non-primary 1st pick, we'd like to find a
7280 * potential pirmary mirror.
7282 if (picked >= 0 && !lo->ldo_mirrors[index].lme_primary)
7285 /* check the availability of OSTs */
7286 lod_foreach_mirror_comp(lod_comp, lo, index) {
7287 if (!lod_comp_inited(lod_comp) || !lod_comp->llc_stripe)
7290 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
7291 struct dt_object *dt = lod_comp->llc_stripe[j];
7293 rc = lod_check_ost_avail(env, lo, dt, index);
7300 } /* for all dt object in one component */
7303 } /* for all components in a mirror */
7306 * the OSTs where allocated objects locates in the components
7307 * of the mirror are available.
7312 /* this mirror has all OSTs available */
7316 * primary with all OSTs are available, this is the perfect
7319 if (lo->ldo_mirrors[index].lme_primary)
7321 } /* for all mirrors */
7323 /* failed to pick a sound mirror, lower our expectation */
7325 picked = second_pick;
7327 picked = third_pick;
7334 static int lod_prepare_resync_mirror(const struct lu_env *env,
7335 struct lod_object *lo,
7338 struct lod_thread_info *info = lod_env_info(env);
7339 struct lod_layout_component *lod_comp;
7340 bool neg = !!(MIRROR_ID_NEG & mirror_id);
7343 mirror_id &= ~MIRROR_ID_NEG;
7345 for (i = 0; i < lo->ldo_mirror_count; i++) {
7346 if ((!neg && lo->ldo_mirrors[i].lme_id != mirror_id) ||
7347 (neg && lo->ldo_mirrors[i].lme_id == mirror_id))
7350 lod_foreach_mirror_comp(lod_comp, lo, i) {
7351 if (lod_comp_inited(lod_comp))
7354 info->lti_comp_idx[info->lti_count++] =
7355 lod_comp_index(lo, lod_comp);
7363 * figure out the components should be instantiated for resync.
7365 static int lod_prepare_resync(const struct lu_env *env, struct lod_object *lo,
7366 struct lu_extent *extent)
7368 struct lod_thread_info *info = lod_env_info(env);
7369 struct lod_layout_component *lod_comp;
7370 unsigned int need_sync = 0;
7374 DFID": instantiate all stale components in "DEXT"\n",
7375 PFID(lod_object_fid(lo)), PEXT(extent));
7378 * instantiate all components within this extent, even non-stale
7381 for (i = 0; i < lo->ldo_mirror_count; i++) {
7382 if (!lo->ldo_mirrors[i].lme_stale)
7385 lod_foreach_mirror_comp(lod_comp, lo, i) {
7386 if (!lu_extent_is_overlapped(extent,
7387 &lod_comp->llc_extent))
7392 if (lod_comp_inited(lod_comp))
7395 CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
7396 i, lod_comp_index(lo, lod_comp));
7397 info->lti_comp_idx[info->lti_count++] =
7398 lod_comp_index(lo, lod_comp);
7402 return need_sync ? 0 : -EALREADY;
7405 static int lod_declare_update_rdonly(const struct lu_env *env,
7406 struct lod_object *lo, struct md_layout_change *mlc,
7409 struct lod_thread_info *info = lod_env_info(env);
7410 struct lu_attr *layout_attr = &info->lti_layout_attr;
7411 struct lod_layout_component *lod_comp;
7412 struct lu_extent extent = { 0 };
7416 LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
7417 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7418 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7419 LASSERT(lo->ldo_mirror_count > 0);
7421 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7422 struct layout_intent *layout = mlc->mlc_intent;
7423 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7426 extent = layout->li_extent;
7427 CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
7428 PFID(lod_object_fid(lo)), PEXT(&extent));
7430 picked = lod_primary_pick(env, lo, &extent);
7434 CDEBUG(D_LAYOUT, DFID": picked mirror id %u as primary\n",
7435 PFID(lod_object_fid(lo)),
7436 lo->ldo_mirrors[picked].lme_id);
7438 /* Update extents of primary before staling */
7439 rc = lod_declare_update_extents(env, lo, &extent, th, picked,
7444 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
7446 * trunc transfers [0, size) in the intent extent, we'd
7447 * stale components overlapping [size, eof).
7449 extent.e_start = extent.e_end;
7450 extent.e_end = OBD_OBJECT_EOF;
7453 /* stale overlapping components from other mirrors */
7454 rc = lod_stale_components(env, lo, picked, &extent, th);
7458 /* restore truncate intent extent */
7459 if (layout->li_opc == LAYOUT_INTENT_TRUNC)
7460 extent.e_end = extent.e_start;
7462 /* instantiate components for the picked mirror, start from 0 */
7465 lod_foreach_mirror_comp(lod_comp, lo, picked) {
7466 if (!lu_extent_is_overlapped(&extent,
7467 &lod_comp->llc_extent))
7470 if (!lod_is_instantiation_needed(lod_comp))
7473 info->lti_comp_idx[info->lti_count++] =
7474 lod_comp_index(lo, lod_comp);
7477 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7478 } else { /* MD_LAYOUT_RESYNC */
7482 * could contain multiple non-stale mirrors, so we need to
7483 * prep uninited all components assuming any non-stale mirror
7484 * could be picked as the primary mirror.
7486 if (mlc->mlc_mirror_id == 0) {
7488 for (i = 0; i < lo->ldo_mirror_count; i++) {
7489 if (lo->ldo_mirrors[i].lme_stale)
7492 lod_foreach_mirror_comp(lod_comp, lo, i) {
7493 if (!lod_comp_inited(lod_comp))
7497 lod_comp->llc_extent.e_end)
7499 lod_comp->llc_extent.e_end;
7502 rc = lod_prepare_resync(env, lo, &extent);
7506 /* mirror write, try to init its all components */
7507 rc = lod_prepare_resync_mirror(env, lo,
7508 mlc->mlc_mirror_id);
7513 /* change the file state to SYNC_PENDING */
7514 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7517 /* Reset the layout version once it's becoming too large.
7518 * This way it can make sure that the layout version is
7519 * monotonously increased in this writing era. */
7520 lod_obj_inc_layout_gen(lo);
7521 if (lo->ldo_layout_gen > (LCME_ID_MAX >> 1)) {
7522 __u32 layout_version;
7524 get_random_bytes(&layout_version, sizeof(layout_version));
7525 lo->ldo_layout_gen = layout_version & 0xffff;
7528 rc = lod_declare_instantiate_components(env, lo, th);
7532 layout_attr->la_valid = LA_LAYOUT_VERSION;
7533 layout_attr->la_layout_version = 0; /* set current version */
7534 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7535 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7536 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7542 lod_striping_free(env, lo);
7546 static int lod_declare_update_write_pending(const struct lu_env *env,
7547 struct lod_object *lo, struct md_layout_change *mlc,
7550 struct lod_thread_info *info = lod_env_info(env);
7551 struct lu_attr *layout_attr = &info->lti_layout_attr;
7552 struct lod_layout_component *lod_comp;
7553 struct lu_extent extent = { 0 };
7559 LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
7560 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7561 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7563 /* look for the primary mirror */
7564 for (i = 0; i < lo->ldo_mirror_count; i++) {
7565 if (lo->ldo_mirrors[i].lme_stale)
7568 LASSERTF(primary < 0, DFID " has multiple primary: %u / %u",
7569 PFID(lod_object_fid(lo)),
7570 lo->ldo_mirrors[i].lme_id,
7571 lo->ldo_mirrors[primary].lme_id);
7576 CERROR(DFID ": doesn't have a primary mirror\n",
7577 PFID(lod_object_fid(lo)));
7578 GOTO(out, rc = -ENODATA);
7581 CDEBUG(D_LAYOUT, DFID": found primary %u\n",
7582 PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
7584 LASSERT(!lo->ldo_mirrors[primary].lme_stale);
7586 /* for LAYOUT_WRITE opc, it has to do the following operations:
7587 * 1. stale overlapping componets from stale mirrors;
7588 * 2. instantiate components of the primary mirror;
7589 * 3. transfter layout version to all objects of the primary;
7591 * for LAYOUT_RESYNC opc, it will do:
7592 * 1. instantiate components of all stale mirrors;
7593 * 2. transfer layout version to all objects to close write era. */
7595 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7596 struct layout_intent *layout = mlc->mlc_intent;
7597 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7599 LASSERT(mlc->mlc_intent != NULL);
7601 extent = mlc->mlc_intent->li_extent;
7603 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
7604 PFID(lod_object_fid(lo)), PEXT(&extent));
7606 /* 1. Update extents of primary before staling */
7607 rc = lod_declare_update_extents(env, lo, &extent, th, primary,
7612 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
7614 * trunc transfers [0, size) in the intent extent, we'd
7615 * stale components overlapping [size, eof).
7617 extent.e_start = extent.e_end;
7618 extent.e_end = OBD_OBJECT_EOF;
7621 /* 2. stale overlapping components */
7622 rc = lod_stale_components(env, lo, primary, &extent, th);
7626 /* 3. find the components which need instantiating.
7627 * instantiate [0, mlc->mlc_intent->e_end) */
7629 /* restore truncate intent extent */
7630 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC)
7631 extent.e_end = extent.e_start;
7634 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7635 if (!lu_extent_is_overlapped(&extent,
7636 &lod_comp->llc_extent))
7639 if (!lod_is_instantiation_needed(lod_comp))
7642 CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
7643 primary, lod_comp_index(lo, lod_comp));
7644 info->lti_comp_idx[info->lti_count++] =
7645 lod_comp_index(lo, lod_comp);
7647 } else { /* MD_LAYOUT_RESYNC */
7648 if (mlc->mlc_mirror_id == 0) {
7650 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7651 if (!lod_comp_inited(lod_comp))
7654 extent.e_end = lod_comp->llc_extent.e_end;
7657 rc = lod_prepare_resync(env, lo, &extent);
7661 /* mirror write, try to init its all components */
7662 rc = lod_prepare_resync_mirror(env, lo,
7663 mlc->mlc_mirror_id);
7668 /* change the file state to SYNC_PENDING */
7669 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7672 rc = lod_declare_instantiate_components(env, lo, th);
7676 /* 3. transfer layout version to OST objects.
7677 * transfer new layout version to OST objects so that stale writes
7678 * can be denied. It also ends an era of writing by setting
7679 * LU_LAYOUT_RESYNC. Normal client can never use this bit to
7680 * send write RPC; only resync RPCs could do it. */
7681 layout_attr->la_valid = LA_LAYOUT_VERSION;
7682 layout_attr->la_layout_version = 0; /* set current version */
7683 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7684 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7685 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7689 lod_obj_inc_layout_gen(lo);
7692 lod_striping_free(env, lo);
7696 static int lod_declare_update_sync_pending(const struct lu_env *env,
7697 struct lod_object *lo, struct md_layout_change *mlc,
7700 struct lod_thread_info *info = lod_env_info(env);
7701 unsigned sync_components = 0;
7702 unsigned resync_components = 0;
7707 LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
7708 LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
7709 mlc->mlc_opc == MD_LAYOUT_WRITE);
7711 CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
7712 PFID(lod_object_fid(lo)), mlc->mlc_opc);
7714 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7715 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
7716 PFID(lod_object_fid(lo)));
7718 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7719 return lod_declare_update_write_pending(env, lo, mlc, th);
7722 /* MD_LAYOUT_RESYNC_DONE */
7724 for (i = 0; i < lo->ldo_comp_cnt; i++) {
7725 struct lod_layout_component *lod_comp;
7728 lod_comp = &lo->ldo_comp_entries[i];
7730 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
7735 for (j = 0; j < mlc->mlc_resync_count; j++) {
7736 if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
7739 mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
7740 lod_comp->llc_flags &= ~LCME_FL_STALE;
7741 resync_components++;
7747 for (i = 0; i < mlc->mlc_resync_count; i++) {
7748 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
7751 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
7752 "or already synced\n", PFID(lod_object_fid(lo)),
7753 mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
7754 GOTO(out, rc = -EINVAL);
7757 if (!sync_components || (mlc->mlc_resync_count && !resync_components)) {
7758 CDEBUG(D_LAYOUT, DFID": no mirror in sync\n",
7759 PFID(lod_object_fid(lo)));
7761 /* tend to return an error code here to prevent
7762 * the MDT from setting SoM attribute */
7763 GOTO(out, rc = -EINVAL);
7766 CDEBUG(D_LAYOUT, DFID": synced %u resynced %u/%zu components\n",
7767 PFID(lod_object_fid(lo)),
7768 sync_components, resync_components, mlc->mlc_resync_count);
7770 lo->ldo_flr_state = LCM_FL_RDONLY;
7771 lod_obj_inc_layout_gen(lo);
7773 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
7774 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
7775 &info->lti_buf, XATTR_NAME_LOV, 0, th);
7780 lod_striping_free(env, lo);
7784 static int lod_declare_layout_change(const struct lu_env *env,
7785 struct dt_object *dt, struct md_layout_change *mlc,
7788 struct lod_thread_info *info = lod_env_info(env);
7789 struct lod_object *lo = lod_dt_obj(dt);
7793 if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
7794 dt_object_remote(dt_object_child(dt)))
7797 rc = lod_striping_load(env, lo);
7801 LASSERT(lo->ldo_comp_cnt > 0);
7803 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
7807 switch (lo->ldo_flr_state) {
7809 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
7813 rc = lod_declare_update_rdonly(env, lo, mlc, th);
7815 case LCM_FL_WRITE_PENDING:
7816 rc = lod_declare_update_write_pending(env, lo, mlc, th);
7818 case LCM_FL_SYNC_PENDING:
7819 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
7830 * Instantiate layout component objects which covers the intent write offset.
7832 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
7833 struct md_layout_change *mlc, struct thandle *th)
7835 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
7836 struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
7837 struct lod_object *lo = lod_dt_obj(dt);
7840 rc = lod_striped_create(env, dt, attr, NULL, th);
7841 if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
7842 layout_attr->la_layout_version |= lo->ldo_layout_gen;
7843 rc = lod_attr_set(env, dt, layout_attr, th);
7849 struct dt_object_operations lod_obj_ops = {
7850 .do_read_lock = lod_read_lock,
7851 .do_write_lock = lod_write_lock,
7852 .do_read_unlock = lod_read_unlock,
7853 .do_write_unlock = lod_write_unlock,
7854 .do_write_locked = lod_write_locked,
7855 .do_attr_get = lod_attr_get,
7856 .do_declare_attr_set = lod_declare_attr_set,
7857 .do_attr_set = lod_attr_set,
7858 .do_xattr_get = lod_xattr_get,
7859 .do_declare_xattr_set = lod_declare_xattr_set,
7860 .do_xattr_set = lod_xattr_set,
7861 .do_declare_xattr_del = lod_declare_xattr_del,
7862 .do_xattr_del = lod_xattr_del,
7863 .do_xattr_list = lod_xattr_list,
7864 .do_ah_init = lod_ah_init,
7865 .do_declare_create = lod_declare_create,
7866 .do_create = lod_create,
7867 .do_declare_destroy = lod_declare_destroy,
7868 .do_destroy = lod_destroy,
7869 .do_index_try = lod_index_try,
7870 .do_declare_ref_add = lod_declare_ref_add,
7871 .do_ref_add = lod_ref_add,
7872 .do_declare_ref_del = lod_declare_ref_del,
7873 .do_ref_del = lod_ref_del,
7874 .do_object_sync = lod_object_sync,
7875 .do_object_lock = lod_object_lock,
7876 .do_object_unlock = lod_object_unlock,
7877 .do_invalidate = lod_invalidate,
7878 .do_declare_layout_change = lod_declare_layout_change,
7879 .do_layout_change = lod_layout_change,
7883 * Implementation of dt_body_operations::dbo_read.
7885 * \see dt_body_operations::dbo_read() in the API description for details.
7887 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
7888 struct lu_buf *buf, loff_t *pos)
7890 struct dt_object *next = dt_object_child(dt);
7892 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
7893 S_ISLNK(dt->do_lu.lo_header->loh_attr));
7894 return next->do_body_ops->dbo_read(env, next, buf, pos);
7898 * Implementation of dt_body_operations::dbo_declare_write.
7900 * \see dt_body_operations::dbo_declare_write() in the API description
7903 static ssize_t lod_declare_write(const struct lu_env *env,
7904 struct dt_object *dt,
7905 const struct lu_buf *buf, loff_t pos,
7908 return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
7912 * Implementation of dt_body_operations::dbo_write.
7914 * \see dt_body_operations::dbo_write() in the API description for details.
7916 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
7917 const struct lu_buf *buf, loff_t *pos,
7920 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
7921 S_ISLNK(dt->do_lu.lo_header->loh_attr));
7922 return lod_sub_write(env, dt_object_child(dt), buf, pos, th);
7925 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
7926 __u64 start, __u64 end, struct thandle *th)
7928 if (dt_object_remote(dt))
7931 return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
7934 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
7935 __u64 start, __u64 end, struct thandle *th)
7937 if (dt_object_remote(dt))
7940 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
7941 return lod_sub_punch(env, dt_object_child(dt), start, end, th);
7945 * different type of files use the same body_ops because object may be created
7946 * in OUT, where there is no chance to set correct body_ops for each type, so
7947 * body_ops themselves will check file type inside, see lod_read/write/punch for
7950 const struct dt_body_operations lod_body_ops = {
7951 .dbo_read = lod_read,
7952 .dbo_declare_write = lod_declare_write,
7953 .dbo_write = lod_write,
7954 .dbo_declare_punch = lod_declare_punch,
7955 .dbo_punch = lod_punch,
7959 * Implementation of lu_object_operations::loo_object_init.
7961 * The function determines the type and the index of the target device using
7962 * sequence of the object's FID. Then passes control down to the
7963 * corresponding device:
7964 * OSD for the local objects, OSP for remote
7966 * \see lu_object_operations::loo_object_init() in the API description
7969 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
7970 const struct lu_object_conf *conf)
7972 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
7973 struct lu_device *cdev = NULL;
7974 struct lu_object *cobj;
7975 struct lod_tgt_descs *ltd = NULL;
7976 struct lod_tgt_desc *tgt;
7978 int type = LU_SEQ_RANGE_ANY;
7982 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
7986 if (type == LU_SEQ_RANGE_MDT &&
7987 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
7988 cdev = &lod->lod_child->dd_lu_dev;
7989 } else if (type == LU_SEQ_RANGE_MDT) {
7990 ltd = &lod->lod_mdt_descs;
7992 } else if (type == LU_SEQ_RANGE_OST) {
7993 ltd = &lod->lod_ost_descs;
8000 if (ltd->ltd_tgts_size > idx &&
8001 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
8002 tgt = LTD_TGT(ltd, idx);
8004 LASSERT(tgt != NULL);
8005 LASSERT(tgt->ltd_tgt != NULL);
8007 cdev = &(tgt->ltd_tgt->dd_lu_dev);
8009 lod_putref(lod, ltd);
8012 if (unlikely(cdev == NULL))
8015 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
8016 if (unlikely(cobj == NULL))
8019 lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
8021 lu_object_add(lo, cobj);
8028 * Alloc cached foreign LOV
8030 * \param[in] lo object
8031 * \param[in] size size of foreign LOV
8033 * \retval 0 on success
8034 * \retval negative if failed
8036 int lod_alloc_foreign_lov(struct lod_object *lo, size_t size)
8038 OBD_ALLOC_LARGE(lo->ldo_foreign_lov, size);
8039 if (lo->ldo_foreign_lov == NULL)
8041 lo->ldo_foreign_lov_size = size;
8042 lo->ldo_is_foreign = 1;
8048 * Free cached foreign LOV
8050 * \param[in] lo object
8052 void lod_free_foreign_lov(struct lod_object *lo)
8054 if (lo->ldo_foreign_lov != NULL)
8055 OBD_FREE_LARGE(lo->ldo_foreign_lov, lo->ldo_foreign_lov_size);
8056 lo->ldo_foreign_lov = NULL;
8057 lo->ldo_foreign_lov_size = 0;
8058 lo->ldo_is_foreign = 0;
8063 * Free cached foreign LMV
8065 * \param[in] lo object
8067 void lod_free_foreign_lmv(struct lod_object *lo)
8069 if (lo->ldo_foreign_lmv != NULL)
8070 OBD_FREE_LARGE(lo->ldo_foreign_lmv, lo->ldo_foreign_lmv_size);
8071 lo->ldo_foreign_lmv = NULL;
8072 lo->ldo_foreign_lmv_size = 0;
8073 lo->ldo_dir_is_foreign = 0;
8078 * Release resources associated with striping.
8080 * If the object is striped (regular or directory), then release
8081 * the stripe objects references and free the ldo_stripe array.
8083 * \param[in] env execution environment
8084 * \param[in] lo object
8086 void lod_striping_free_nolock(const struct lu_env *env, struct lod_object *lo)
8088 struct lod_layout_component *lod_comp;
8091 if (unlikely(lo->ldo_is_foreign)) {
8092 lod_free_foreign_lov(lo);
8093 lo->ldo_comp_cached = 0;
8094 } else if (unlikely(lo->ldo_dir_is_foreign)) {
8095 lod_free_foreign_lmv(lo);
8096 lo->ldo_dir_stripe_loaded = 0;
8097 } else if (lo->ldo_stripe != NULL) {
8098 LASSERT(lo->ldo_comp_entries == NULL);
8099 LASSERT(lo->ldo_dir_stripes_allocated > 0);
8101 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8102 if (lo->ldo_stripe[i])
8103 dt_object_put(env, lo->ldo_stripe[i]);
8106 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
8107 OBD_FREE(lo->ldo_stripe, j);
8108 lo->ldo_stripe = NULL;
8109 lo->ldo_dir_stripes_allocated = 0;
8110 lo->ldo_dir_stripe_loaded = 0;
8111 lo->ldo_dir_stripe_count = 0;
8112 } else if (lo->ldo_comp_entries != NULL) {
8113 for (i = 0; i < lo->ldo_comp_cnt; i++) {
8114 /* free lod_layout_component::llc_stripe array */
8115 lod_comp = &lo->ldo_comp_entries[i];
8117 if (lod_comp->llc_stripe == NULL)
8119 LASSERT(lod_comp->llc_stripes_allocated != 0);
8120 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
8121 if (lod_comp->llc_stripe[j] != NULL)
8123 &lod_comp->llc_stripe[j]->do_lu);
8125 OBD_FREE(lod_comp->llc_stripe,
8126 sizeof(struct dt_object *) *
8127 lod_comp->llc_stripes_allocated);
8128 lod_comp->llc_stripe = NULL;
8129 OBD_FREE(lod_comp->llc_ost_indices,
8131 lod_comp->llc_stripes_allocated);
8132 lod_comp->llc_ost_indices = NULL;
8133 lod_comp->llc_stripes_allocated = 0;
8135 lod_free_comp_entries(lo);
8136 lo->ldo_comp_cached = 0;
8140 void lod_striping_free(const struct lu_env *env, struct lod_object *lo)
8142 mutex_lock(&lo->ldo_layout_mutex);
8143 lod_striping_free_nolock(env, lo);
8144 mutex_unlock(&lo->ldo_layout_mutex);
8148 * Implementation of lu_object_operations::loo_object_free.
8150 * \see lu_object_operations::loo_object_free() in the API description
8153 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
8155 struct lod_object *lo = lu2lod_obj(o);
8157 /* release all underlying object pinned */
8158 lod_striping_free(env, lo);
8160 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
8164 * Implementation of lu_object_operations::loo_object_release.
8166 * \see lu_object_operations::loo_object_release() in the API description
8169 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
8171 /* XXX: shouldn't we release everything here in case if object
8172 * creation failed before? */
8176 * Implementation of lu_object_operations::loo_object_print.
8178 * \see lu_object_operations::loo_object_print() in the API description
8181 static int lod_object_print(const struct lu_env *env, void *cookie,
8182 lu_printer_t p, const struct lu_object *l)
8184 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
8186 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
8189 struct lu_object_operations lod_lu_obj_ops = {
8190 .loo_object_init = lod_object_init,
8191 .loo_object_free = lod_object_free,
8192 .loo_object_release = lod_object_release,
8193 .loo_object_print = lod_object_print,