4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * lustre/lod/lod_object.c
31 * This file contains implementations of methods for the OSD API
32 * for the Logical Object Device (LOD) layer, which provides a virtual
33 * local OSD object interface to the MDD layer, and abstracts the
34 * addressing of local (OSD) and remote (OSP) objects. The API is
35 * described in the file lustre/include/dt_object.h and in
36 * Documentation/osd-api.txt.
38 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
41 #define DEBUG_SUBSYSTEM S_MDS
43 #include <linux/random.h>
46 #include <obd_class.h>
47 #include <obd_support.h>
49 #include <lustre_fid.h>
50 #include <lustre_linkea.h>
51 #include <lustre_lmv.h>
52 #include <uapi/linux/lustre/lustre_param.h>
53 #include <lustre_swab.h>
54 #include <uapi/linux/lustre/lustre_ver.h>
55 #include <lprocfs_status.h>
56 #include <md_object.h>
58 #include "lod_internal.h"
60 static const char dot[] = ".";
61 static const char dotdot[] = "..";
64 * Implementation of dt_index_operations::dio_lookup
66 * Used with regular (non-striped) objects.
68 * \see dt_index_operations::dio_lookup() in the API description for details.
70 static int lod_lookup(const struct lu_env *env, struct dt_object *dt,
71 struct dt_rec *rec, const struct dt_key *key)
73 struct dt_object *next = dt_object_child(dt);
74 return next->do_index_ops->dio_lookup(env, next, rec, key);
78 * Implementation of dt_index_operations::dio_declare_insert.
80 * Used with regular (non-striped) objects.
82 * \see dt_index_operations::dio_declare_insert() in the API description
85 static int lod_declare_insert(const struct lu_env *env, struct dt_object *dt,
86 const struct dt_rec *rec,
87 const struct dt_key *key, struct thandle *th)
89 return lod_sub_declare_insert(env, dt_object_child(dt), rec, key, th);
93 * Implementation of dt_index_operations::dio_insert.
95 * Used with regular (non-striped) objects
97 * \see dt_index_operations::dio_insert() in the API description for details.
99 static int lod_insert(const struct lu_env *env, struct dt_object *dt,
100 const struct dt_rec *rec, const struct dt_key *key,
103 return lod_sub_insert(env, dt_object_child(dt), rec, key, th);
107 * Implementation of dt_index_operations::dio_declare_delete.
109 * Used with regular (non-striped) objects.
111 * \see dt_index_operations::dio_declare_delete() in the API description
114 static int lod_declare_delete(const struct lu_env *env, struct dt_object *dt,
115 const struct dt_key *key, struct thandle *th)
117 return lod_sub_declare_delete(env, dt_object_child(dt), key, th);
121 * Implementation of dt_index_operations::dio_delete.
123 * Used with regular (non-striped) objects.
125 * \see dt_index_operations::dio_delete() in the API description for details.
127 static int lod_delete(const struct lu_env *env, struct dt_object *dt,
128 const struct dt_key *key, struct thandle *th)
130 return lod_sub_delete(env, dt_object_child(dt), key, th);
134 * Implementation of dt_it_ops::init.
136 * Used with regular (non-striped) objects.
138 * \see dt_it_ops::init() in the API description for details.
140 static struct dt_it *lod_it_init(const struct lu_env *env,
141 struct dt_object *dt, __u32 attr)
143 struct dt_object *next = dt_object_child(dt);
144 struct lod_it *it = &lod_env_info(env)->lti_it;
145 struct dt_it *it_next;
147 it_next = next->do_index_ops->dio_it.init(env, next, attr);
151 /* currently we do not use more than one iterator per thread
152 * so we store it in thread info. if at some point we need
153 * more active iterators in a single thread, we can allocate
155 LASSERT(it->lit_obj == NULL);
157 it->lit_it = it_next;
160 return (struct dt_it *)it;
163 #define LOD_CHECK_IT(env, it) \
165 LASSERT((it)->lit_obj != NULL); \
166 LASSERT((it)->lit_it != NULL); \
170 * Implementation of dt_index_operations::dio_it.fini.
172 * Used with regular (non-striped) objects.
174 * \see dt_index_operations::dio_it.fini() in the API description for details.
176 static void lod_it_fini(const struct lu_env *env, struct dt_it *di)
178 struct lod_it *it = (struct lod_it *)di;
180 LOD_CHECK_IT(env, it);
181 it->lit_obj->do_index_ops->dio_it.fini(env, it->lit_it);
183 /* the iterator not in use any more */
189 * Implementation of dt_it_ops::get.
191 * Used with regular (non-striped) objects.
193 * \see dt_it_ops::get() in the API description for details.
195 static int lod_it_get(const struct lu_env *env, struct dt_it *di,
196 const struct dt_key *key)
198 const struct lod_it *it = (const struct lod_it *)di;
200 LOD_CHECK_IT(env, it);
201 return it->lit_obj->do_index_ops->dio_it.get(env, it->lit_it, key);
205 * Implementation of dt_it_ops::put.
207 * Used with regular (non-striped) objects.
209 * \see dt_it_ops::put() in the API description for details.
211 static void lod_it_put(const struct lu_env *env, struct dt_it *di)
213 struct lod_it *it = (struct lod_it *)di;
215 LOD_CHECK_IT(env, it);
216 return it->lit_obj->do_index_ops->dio_it.put(env, it->lit_it);
220 * Implementation of dt_it_ops::next.
222 * Used with regular (non-striped) objects
224 * \see dt_it_ops::next() in the API description for details.
226 static int lod_it_next(const struct lu_env *env, struct dt_it *di)
228 struct lod_it *it = (struct lod_it *)di;
230 LOD_CHECK_IT(env, it);
231 return it->lit_obj->do_index_ops->dio_it.next(env, it->lit_it);
235 * Implementation of dt_it_ops::key.
237 * Used with regular (non-striped) objects.
239 * \see dt_it_ops::key() in the API description for details.
241 static struct dt_key *lod_it_key(const struct lu_env *env,
242 const struct dt_it *di)
244 const struct lod_it *it = (const struct lod_it *)di;
246 LOD_CHECK_IT(env, it);
247 return it->lit_obj->do_index_ops->dio_it.key(env, it->lit_it);
251 * Implementation of dt_it_ops::key_size.
253 * Used with regular (non-striped) objects.
255 * \see dt_it_ops::key_size() in the API description for details.
257 static int lod_it_key_size(const struct lu_env *env, const struct dt_it *di)
259 struct lod_it *it = (struct lod_it *)di;
261 LOD_CHECK_IT(env, it);
262 return it->lit_obj->do_index_ops->dio_it.key_size(env, it->lit_it);
266 * Implementation of dt_it_ops::rec.
268 * Used with regular (non-striped) objects.
270 * \see dt_it_ops::rec() in the API description for details.
272 static int lod_it_rec(const struct lu_env *env, const struct dt_it *di,
273 struct dt_rec *rec, __u32 attr)
275 const struct lod_it *it = (const struct lod_it *)di;
277 LOD_CHECK_IT(env, it);
278 return it->lit_obj->do_index_ops->dio_it.rec(env, it->lit_it, rec,
283 * Implementation of dt_it_ops::rec_size.
285 * Used with regular (non-striped) objects.
287 * \see dt_it_ops::rec_size() in the API description for details.
289 static int lod_it_rec_size(const struct lu_env *env, const struct dt_it *di,
292 const struct lod_it *it = (const struct lod_it *)di;
294 LOD_CHECK_IT(env, it);
295 return it->lit_obj->do_index_ops->dio_it.rec_size(env, it->lit_it,
300 * Implementation of dt_it_ops::store.
302 * Used with regular (non-striped) objects.
304 * \see dt_it_ops::store() in the API description for details.
306 static __u64 lod_it_store(const struct lu_env *env, const struct dt_it *di)
308 const struct lod_it *it = (const struct lod_it *)di;
310 LOD_CHECK_IT(env, it);
311 return it->lit_obj->do_index_ops->dio_it.store(env, it->lit_it);
315 * Implementation of dt_it_ops::load.
317 * Used with regular (non-striped) objects.
319 * \see dt_it_ops::load() in the API description for details.
321 static int lod_it_load(const struct lu_env *env, const struct dt_it *di,
324 const struct lod_it *it = (const struct lod_it *)di;
326 LOD_CHECK_IT(env, it);
327 return it->lit_obj->do_index_ops->dio_it.load(env, it->lit_it, hash);
331 * Implementation of dt_it_ops::key_rec.
333 * Used with regular (non-striped) objects.
335 * \see dt_it_ops::rec() in the API description for details.
337 static int lod_it_key_rec(const struct lu_env *env, const struct dt_it *di,
340 const struct lod_it *it = (const struct lod_it *)di;
342 LOD_CHECK_IT(env, it);
343 return it->lit_obj->do_index_ops->dio_it.key_rec(env, it->lit_it,
347 static struct dt_index_operations lod_index_ops = {
348 .dio_lookup = lod_lookup,
349 .dio_declare_insert = lod_declare_insert,
350 .dio_insert = lod_insert,
351 .dio_declare_delete = lod_declare_delete,
352 .dio_delete = lod_delete,
360 .key_size = lod_it_key_size,
362 .rec_size = lod_it_rec_size,
363 .store = lod_it_store,
365 .key_rec = lod_it_key_rec,
370 * Implementation of dt_it_ops::init.
372 * Used with striped objects. Internally just initializes the iterator
373 * on the first stripe.
375 * \see dt_it_ops::init() in the API description for details.
377 static struct dt_it *lod_striped_it_init(const struct lu_env *env,
378 struct dt_object *dt, __u32 attr)
380 struct lod_object *lo = lod_dt_obj(dt);
381 struct dt_object *next;
382 struct lod_it *it = &lod_env_info(env)->lti_it;
383 struct dt_it *it_next;
386 LASSERT(lo->ldo_dir_stripe_count > 0);
389 next = lo->ldo_stripe[index];
390 if (next && dt_object_exists(next))
392 } while (++index < lo->ldo_dir_stripe_count);
394 /* no valid stripe */
395 if (!next || !dt_object_exists(next))
396 return ERR_PTR(-ENODEV);
398 LASSERT(next->do_index_ops != NULL);
400 it_next = next->do_index_ops->dio_it.init(env, next, attr);
404 /* currently we do not use more than one iterator per thread
405 * so we store it in thread info. if at some point we need
406 * more active iterators in a single thread, we can allocate
408 LASSERT(it->lit_obj == NULL);
410 it->lit_stripe_index = index;
412 it->lit_it = it_next;
415 return (struct dt_it *)it;
418 #define LOD_CHECK_STRIPED_IT(env, it, lo) \
420 LASSERT((it)->lit_obj != NULL); \
421 LASSERT((it)->lit_it != NULL); \
422 LASSERT((lo)->ldo_dir_stripe_count > 0); \
423 LASSERT((it)->lit_stripe_index < (lo)->ldo_dir_stripe_count); \
427 * Implementation of dt_it_ops::fini.
429 * Used with striped objects.
431 * \see dt_it_ops::fini() in the API description for details.
433 static void lod_striped_it_fini(const struct lu_env *env, struct dt_it *di)
435 struct lod_it *it = (struct lod_it *)di;
436 struct lod_object *lo = lod_dt_obj(it->lit_obj);
437 struct dt_object *next;
439 /* If lit_it == NULL, then it means the sub_it has been finished,
440 * which only happens in failure cases, see lod_striped_it_next() */
441 if (it->lit_it != NULL) {
442 LOD_CHECK_STRIPED_IT(env, it, lo);
444 next = lo->ldo_stripe[it->lit_stripe_index];
446 LASSERT(next->do_index_ops != NULL);
447 next->do_index_ops->dio_it.fini(env, it->lit_it);
451 /* the iterator not in use any more */
454 it->lit_stripe_index = 0;
458 * Implementation of dt_it_ops::get.
460 * Right now it's not used widely, only to reset the iterator to the
461 * initial position. It should be possible to implement a full version
462 * which chooses a correct stripe to be able to position with any key.
464 * \see dt_it_ops::get() in the API description for details.
466 static int lod_striped_it_get(const struct lu_env *env, struct dt_it *di,
467 const struct dt_key *key)
469 const struct lod_it *it = (const struct lod_it *)di;
470 struct lod_object *lo = lod_dt_obj(it->lit_obj);
471 struct dt_object *next;
473 LOD_CHECK_STRIPED_IT(env, it, lo);
475 next = lo->ldo_stripe[it->lit_stripe_index];
476 LASSERT(next != NULL);
477 LASSERT(dt_object_exists(next));
478 LASSERT(next->do_index_ops != NULL);
480 return next->do_index_ops->dio_it.get(env, it->lit_it, key);
484 * Implementation of dt_it_ops::put.
486 * Used with striped objects.
488 * \see dt_it_ops::put() in the API description for details.
490 static void lod_striped_it_put(const struct lu_env *env, struct dt_it *di)
492 struct lod_it *it = (struct lod_it *)di;
493 struct lod_object *lo = lod_dt_obj(it->lit_obj);
494 struct dt_object *next;
497 * If lit_it == NULL, then it means the sub_it has been finished,
498 * which only happens in failure cases, see lod_striped_it_next()
503 LOD_CHECK_STRIPED_IT(env, it, lo);
505 next = lo->ldo_stripe[it->lit_stripe_index];
506 LASSERT(next != NULL);
507 LASSERT(next->do_index_ops != NULL);
509 return next->do_index_ops->dio_it.put(env, it->lit_it);
513 * Implementation of dt_it_ops::next.
515 * Used with striped objects. When the end of the current stripe is
516 * reached, the method takes the next stripe's iterator.
518 * \see dt_it_ops::next() in the API description for details.
520 static int lod_striped_it_next(const struct lu_env *env, struct dt_it *di)
522 struct lod_it *it = (struct lod_it *)di;
523 struct lod_object *lo = lod_dt_obj(it->lit_obj);
524 struct dt_object *next;
525 struct dt_it *it_next;
531 LOD_CHECK_STRIPED_IT(env, it, lo);
533 next = lo->ldo_stripe[it->lit_stripe_index];
534 LASSERT(next != NULL);
535 LASSERT(dt_object_exists(next));
536 LASSERT(next->do_index_ops != NULL);
538 rc = next->do_index_ops->dio_it.next(env, it->lit_it);
542 if (rc == 0 && it->lit_stripe_index == 0)
545 if (rc == 0 && it->lit_stripe_index > 0) {
546 struct lu_dirent *ent;
548 ent = (struct lu_dirent *)lod_env_info(env)->lti_key;
550 rc = next->do_index_ops->dio_it.rec(env, it->lit_it,
551 (struct dt_rec *)ent,
556 /* skip . and .. for slave stripe */
557 if ((strncmp(ent->lde_name, ".",
558 le16_to_cpu(ent->lde_namelen)) == 0 &&
559 le16_to_cpu(ent->lde_namelen) == 1) ||
560 (strncmp(ent->lde_name, "..",
561 le16_to_cpu(ent->lde_namelen)) == 0 &&
562 le16_to_cpu(ent->lde_namelen) == 2))
568 next->do_index_ops->dio_it.put(env, it->lit_it);
569 next->do_index_ops->dio_it.fini(env, it->lit_it);
572 /* go to next stripe */
573 index = it->lit_stripe_index;
574 while (++index < lo->ldo_dir_stripe_count) {
575 next = lo->ldo_stripe[index];
579 if (!dt_object_exists(next))
582 rc = next->do_ops->do_index_try(env, next,
583 &dt_directory_features);
587 LASSERT(next->do_index_ops != NULL);
589 it_next = next->do_index_ops->dio_it.init(env, next,
592 RETURN(PTR_ERR(it_next));
594 rc = next->do_index_ops->dio_it.get(env, it_next,
595 (const struct dt_key *)"");
597 RETURN(rc == 0 ? -EIO : rc);
599 it->lit_it = it_next;
600 it->lit_stripe_index = index;
609 * Implementation of dt_it_ops::key.
611 * Used with striped objects.
613 * \see dt_it_ops::key() in the API description for details.
615 static struct dt_key *lod_striped_it_key(const struct lu_env *env,
616 const struct dt_it *di)
618 const struct lod_it *it = (const struct lod_it *)di;
619 struct lod_object *lo = lod_dt_obj(it->lit_obj);
620 struct dt_object *next;
622 LOD_CHECK_STRIPED_IT(env, it, lo);
624 next = lo->ldo_stripe[it->lit_stripe_index];
625 LASSERT(next != NULL);
626 LASSERT(next->do_index_ops != NULL);
628 return next->do_index_ops->dio_it.key(env, it->lit_it);
632 * Implementation of dt_it_ops::key_size.
634 * Used with striped objects.
636 * \see dt_it_ops::size() in the API description for details.
638 static int lod_striped_it_key_size(const struct lu_env *env,
639 const struct dt_it *di)
641 struct lod_it *it = (struct lod_it *)di;
642 struct lod_object *lo = lod_dt_obj(it->lit_obj);
643 struct dt_object *next;
645 LOD_CHECK_STRIPED_IT(env, it, lo);
647 next = lo->ldo_stripe[it->lit_stripe_index];
648 LASSERT(next != NULL);
649 LASSERT(next->do_index_ops != NULL);
651 return next->do_index_ops->dio_it.key_size(env, it->lit_it);
655 * Implementation of dt_it_ops::rec.
657 * Used with striped objects.
659 * \see dt_it_ops::rec() in the API description for details.
661 static int lod_striped_it_rec(const struct lu_env *env, const struct dt_it *di,
662 struct dt_rec *rec, __u32 attr)
664 const struct lod_it *it = (const struct lod_it *)di;
665 struct lod_object *lo = lod_dt_obj(it->lit_obj);
666 struct dt_object *next;
668 LOD_CHECK_STRIPED_IT(env, it, lo);
670 next = lo->ldo_stripe[it->lit_stripe_index];
671 LASSERT(next != NULL);
672 LASSERT(next->do_index_ops != NULL);
674 return next->do_index_ops->dio_it.rec(env, it->lit_it, rec, attr);
678 * Implementation of dt_it_ops::rec_size.
680 * Used with striped objects.
682 * \see dt_it_ops::rec_size() in the API description for details.
684 static int lod_striped_it_rec_size(const struct lu_env *env,
685 const struct dt_it *di, __u32 attr)
687 struct lod_it *it = (struct lod_it *)di;
688 struct lod_object *lo = lod_dt_obj(it->lit_obj);
689 struct dt_object *next;
691 LOD_CHECK_STRIPED_IT(env, it, lo);
693 next = lo->ldo_stripe[it->lit_stripe_index];
694 LASSERT(next != NULL);
695 LASSERT(next->do_index_ops != NULL);
697 return next->do_index_ops->dio_it.rec_size(env, it->lit_it, attr);
701 * Implementation of dt_it_ops::store.
703 * Used with striped objects.
705 * \see dt_it_ops::store() in the API description for details.
707 static __u64 lod_striped_it_store(const struct lu_env *env,
708 const struct dt_it *di)
710 const struct lod_it *it = (const struct lod_it *)di;
711 struct lod_object *lo = lod_dt_obj(it->lit_obj);
712 struct dt_object *next;
714 LOD_CHECK_STRIPED_IT(env, it, lo);
716 next = lo->ldo_stripe[it->lit_stripe_index];
717 LASSERT(next != NULL);
718 LASSERT(next->do_index_ops != NULL);
720 return next->do_index_ops->dio_it.store(env, it->lit_it);
724 * Implementation of dt_it_ops::load.
726 * Used with striped objects.
728 * \see dt_it_ops::load() in the API description for details.
730 static int lod_striped_it_load(const struct lu_env *env,
731 const struct dt_it *di, __u64 hash)
733 const struct lod_it *it = (const struct lod_it *)di;
734 struct lod_object *lo = lod_dt_obj(it->lit_obj);
735 struct dt_object *next;
737 LOD_CHECK_STRIPED_IT(env, it, lo);
739 next = lo->ldo_stripe[it->lit_stripe_index];
740 LASSERT(next != NULL);
741 LASSERT(next->do_index_ops != NULL);
743 return next->do_index_ops->dio_it.load(env, it->lit_it, hash);
746 static struct dt_index_operations lod_striped_index_ops = {
747 .dio_lookup = lod_lookup,
748 .dio_declare_insert = lod_declare_insert,
749 .dio_insert = lod_insert,
750 .dio_declare_delete = lod_declare_delete,
751 .dio_delete = lod_delete,
753 .init = lod_striped_it_init,
754 .fini = lod_striped_it_fini,
755 .get = lod_striped_it_get,
756 .put = lod_striped_it_put,
757 .next = lod_striped_it_next,
758 .key = lod_striped_it_key,
759 .key_size = lod_striped_it_key_size,
760 .rec = lod_striped_it_rec,
761 .rec_size = lod_striped_it_rec_size,
762 .store = lod_striped_it_store,
763 .load = lod_striped_it_load,
768 * Append the FID for each shard of the striped directory after the
769 * given LMV EA header.
771 * To simplify striped directory and the consistency verification,
772 * we only store the LMV EA header on disk, for both master object
773 * and slave objects. When someone wants to know the whole LMV EA,
774 * such as client readdir(), we can build the entrie LMV EA on the
775 * MDT side (in RAM) via iterating the sub-directory entries that
776 * are contained in the master object of the stripe directory.
778 * For the master object of the striped directroy, the valid name
779 * for each shard is composed of the ${shard_FID}:${shard_idx}.
781 * There may be holes in the LMV EA if some shards' name entries
782 * are corrupted or lost.
784 * \param[in] env pointer to the thread context
785 * \param[in] lo pointer to the master object of the striped directory
786 * \param[in] buf pointer to the lu_buf which will hold the LMV EA
787 * \param[in] resize whether re-allocate the buffer if it is not big enough
789 * \retval positive size of the LMV EA
790 * \retval 0 for nothing to be loaded
791 * \retval negative error number on failure
793 int lod_load_lmv_shards(const struct lu_env *env, struct lod_object *lo,
794 struct lu_buf *buf, bool resize)
796 struct lu_dirent *ent =
797 (struct lu_dirent *)lod_env_info(env)->lti_key;
798 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
799 struct dt_object *obj = dt_object_child(&lo->ldo_obj);
800 struct lmv_mds_md_v1 *lmv1 = buf->lb_buf;
802 const struct dt_it_ops *iops;
804 __u32 magic = le32_to_cpu(lmv1->lmv_magic);
809 if (magic != LMV_MAGIC_V1)
812 stripes = le32_to_cpu(lmv1->lmv_stripe_count);
816 rc = lmv_mds_md_size(stripes, magic);
820 if (buf->lb_len < lmv1_size) {
829 lu_buf_alloc(buf, lmv1_size);
834 memcpy(buf->lb_buf, tbuf.lb_buf, tbuf.lb_len);
837 if (unlikely(!dt_try_as_dir(env, obj)))
840 memset(&lmv1->lmv_stripe_fids[0], 0, stripes * sizeof(struct lu_fid));
841 iops = &obj->do_index_ops->dio_it;
842 it = iops->init(env, obj, LUDA_64BITHASH);
846 rc = iops->load(env, it, 0);
848 rc = iops->next(env, it);
853 char name[FID_LEN + 2] = "";
858 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
864 fid_le_to_cpu(&fid, &ent->lde_fid);
865 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
866 if (ent->lde_name[0] == '.') {
867 if (ent->lde_namelen == 1)
870 if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
874 len = snprintf(name, sizeof(name),
875 DFID":", PFID(&ent->lde_fid));
876 /* The ent->lde_name is composed of ${FID}:${index} */
877 if (ent->lde_namelen < len + 1 ||
878 memcmp(ent->lde_name, name, len) != 0) {
879 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
880 "%s: invalid shard name %.*s with the FID "DFID
881 " for the striped directory "DFID", %s\n",
882 lod2obd(lod)->obd_name, ent->lde_namelen,
883 ent->lde_name, PFID(&fid),
884 PFID(lu_object_fid(&obj->do_lu)),
885 lod->lod_lmv_failout ? "failout" : "skip");
887 if (lod->lod_lmv_failout)
895 if (ent->lde_name[len] < '0' ||
896 ent->lde_name[len] > '9') {
897 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
898 "%s: invalid shard name %.*s with the "
899 "FID "DFID" for the striped directory "
901 lod2obd(lod)->obd_name, ent->lde_namelen,
902 ent->lde_name, PFID(&fid),
903 PFID(lu_object_fid(&obj->do_lu)),
904 lod->lod_lmv_failout ?
907 if (lod->lod_lmv_failout)
913 index = index * 10 + ent->lde_name[len++] - '0';
914 } while (len < ent->lde_namelen);
916 if (len == ent->lde_namelen) {
917 /* Out of LMV EA range. */
918 if (index >= stripes) {
919 CERROR("%s: the shard %.*s for the striped "
920 "directory "DFID" is out of the known "
921 "LMV EA range [0 - %u], failout\n",
922 lod2obd(lod)->obd_name, ent->lde_namelen,
924 PFID(lu_object_fid(&obj->do_lu)),
930 /* The slot has been occupied. */
931 if (!fid_is_zero(&lmv1->lmv_stripe_fids[index])) {
935 &lmv1->lmv_stripe_fids[index]);
936 CERROR("%s: both the shard "DFID" and "DFID
937 " for the striped directory "DFID
938 " claim the same LMV EA slot at the "
939 "index %d, failout\n",
940 lod2obd(lod)->obd_name,
941 PFID(&fid0), PFID(&fid),
942 PFID(lu_object_fid(&obj->do_lu)), index);
947 /* stored as LE mode */
948 lmv1->lmv_stripe_fids[index] = ent->lde_fid;
951 rc = iops->next(env, it);
958 RETURN(rc > 0 ? lmv_mds_md_size(stripes, magic) : rc);
962 * Implementation of dt_object_operations::do_index_try.
964 * \see dt_object_operations::do_index_try() in the API description for details.
966 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
967 const struct dt_index_features *feat)
969 struct lod_object *lo = lod_dt_obj(dt);
970 struct dt_object *next = dt_object_child(dt);
974 LASSERT(next->do_ops);
975 LASSERT(next->do_ops->do_index_try);
977 rc = lod_striping_load(env, lo);
981 rc = next->do_ops->do_index_try(env, next, feat);
985 if (lo->ldo_dir_stripe_count > 0) {
988 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
989 if (!lo->ldo_stripe[i])
991 if (!dt_object_exists(lo->ldo_stripe[i]))
993 rc = lo->ldo_stripe[i]->do_ops->do_index_try(env,
994 lo->ldo_stripe[i], feat);
998 dt->do_index_ops = &lod_striped_index_ops;
1000 dt->do_index_ops = &lod_index_ops;
1007 * Implementation of dt_object_operations::do_read_lock.
1009 * \see dt_object_operations::do_read_lock() in the API description for details.
1011 static void lod_read_lock(const struct lu_env *env, struct dt_object *dt,
1014 dt_read_lock(env, dt_object_child(dt), role);
1018 * Implementation of dt_object_operations::do_write_lock.
1020 * \see dt_object_operations::do_write_lock() in the API description for
1023 static void lod_write_lock(const struct lu_env *env, struct dt_object *dt,
1026 dt_write_lock(env, dt_object_child(dt), role);
1030 * Implementation of dt_object_operations::do_read_unlock.
1032 * \see dt_object_operations::do_read_unlock() in the API description for
1035 static void lod_read_unlock(const struct lu_env *env, struct dt_object *dt)
1037 dt_read_unlock(env, dt_object_child(dt));
1041 * Implementation of dt_object_operations::do_write_unlock.
1043 * \see dt_object_operations::do_write_unlock() in the API description for
1046 static void lod_write_unlock(const struct lu_env *env, struct dt_object *dt)
1048 dt_write_unlock(env, dt_object_child(dt));
1052 * Implementation of dt_object_operations::do_write_locked.
1054 * \see dt_object_operations::do_write_locked() in the API description for
1057 static int lod_write_locked(const struct lu_env *env, struct dt_object *dt)
1059 return dt_write_locked(env, dt_object_child(dt));
1063 * Implementation of dt_object_operations::do_attr_get.
1065 * \see dt_object_operations::do_attr_get() in the API description for details.
1067 static int lod_attr_get(const struct lu_env *env,
1068 struct dt_object *dt,
1069 struct lu_attr *attr)
1071 /* Note: for striped directory, client will merge attributes
1072 * from all of the sub-stripes see lmv_merge_attr(), and there
1073 * no MDD logic depend on directory nlink/size/time, so we can
1074 * always use master inode nlink and size for now. */
1075 return dt_attr_get(env, dt_object_child(dt), attr);
1078 static inline void lod_adjust_stripe_info(struct lod_layout_component *comp,
1079 struct lov_desc *desc,
1082 if (comp->llc_pattern != LOV_PATTERN_MDT) {
1083 if (append_stripes) {
1084 comp->llc_stripe_count = append_stripes;
1085 } else if (!comp->llc_stripe_count) {
1086 comp->llc_stripe_count =
1087 desc->ld_default_stripe_count;
1090 if (comp->llc_stripe_size <= 0)
1091 comp->llc_stripe_size = desc->ld_default_stripe_size;
1094 int lod_obj_for_each_stripe(const struct lu_env *env, struct lod_object *lo,
1096 struct lod_obj_stripe_cb_data *data)
1098 struct lod_layout_component *lod_comp;
1102 LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
1103 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1104 lod_comp = &lo->ldo_comp_entries[i];
1106 if (lod_comp->llc_stripe == NULL)
1109 /* has stripe but not inited yet, this component has been
1110 * declared to be created, but hasn't created yet.
1112 if (!lod_comp_inited(lod_comp))
1115 if (data->locd_comp_skip_cb &&
1116 data->locd_comp_skip_cb(env, lo, i, data))
1119 if (data->locd_comp_cb) {
1120 rc = data->locd_comp_cb(env, lo, i, data);
1125 /* could used just to do sth about component, not each
1128 if (!data->locd_stripe_cb)
1131 LASSERT(lod_comp->llc_stripe_count > 0);
1132 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
1133 struct dt_object *dt = lod_comp->llc_stripe[j];
1137 rc = data->locd_stripe_cb(env, lo, dt, th, i, j, data);
1145 static bool lod_obj_attr_set_comp_skip_cb(const struct lu_env *env,
1146 struct lod_object *lo, int comp_idx,
1147 struct lod_obj_stripe_cb_data *data)
1149 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
1150 bool skipped = false;
1152 if (!(data->locd_attr->la_valid & LA_LAYOUT_VERSION))
1155 switch (lo->ldo_flr_state) {
1156 case LCM_FL_WRITE_PENDING: {
1159 /* skip stale components */
1160 if (lod_comp->llc_flags & LCME_FL_STALE) {
1165 /* skip valid and overlapping components, therefore any
1166 * attempts to write overlapped components will never succeed
1167 * because client will get EINPROGRESS. */
1168 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1172 if (lo->ldo_comp_entries[i].llc_flags & LCME_FL_STALE)
1175 if (lu_extent_is_overlapped(&lod_comp->llc_extent,
1176 &lo->ldo_comp_entries[i].llc_extent)) {
1184 LASSERTF(0, "impossible: %d\n", lo->ldo_flr_state);
1185 case LCM_FL_SYNC_PENDING:
1189 CDEBUG(D_LAYOUT, DFID": %s to set component %x to version: %u\n",
1190 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
1191 skipped ? "skipped" : "chose", lod_comp->llc_id,
1192 data->locd_attr->la_layout_version);
1198 lod_obj_stripe_attr_set_cb(const struct lu_env *env, struct lod_object *lo,
1199 struct dt_object *dt, struct thandle *th,
1200 int comp_idx, int stripe_idx,
1201 struct lod_obj_stripe_cb_data *data)
1203 if (data->locd_declare)
1204 return lod_sub_declare_attr_set(env, dt, data->locd_attr, th);
1206 if (data->locd_attr->la_valid & LA_LAYOUT_VERSION) {
1207 CDEBUG(D_LAYOUT, DFID": set layout version: %u, comp_idx: %d\n",
1208 PFID(lu_object_fid(&dt->do_lu)),
1209 data->locd_attr->la_layout_version, comp_idx);
1212 return lod_sub_attr_set(env, dt, data->locd_attr, th);
1216 * Implementation of dt_object_operations::do_declare_attr_set.
1218 * If the object is striped, then apply the changes to all the stripes.
1220 * \see dt_object_operations::do_declare_attr_set() in the API description
1223 static int lod_declare_attr_set(const struct lu_env *env,
1224 struct dt_object *dt,
1225 const struct lu_attr *attr,
1228 struct dt_object *next = dt_object_child(dt);
1229 struct lod_object *lo = lod_dt_obj(dt);
1234 * declare setattr on the local object
1236 rc = lod_sub_declare_attr_set(env, next, attr, th);
1240 /* osp_declare_attr_set() ignores all attributes other than
1241 * UID, GID, PROJID, and size, and osp_attr_set() ignores all
1242 * but UID, GID and PROJID. Declaration of size attr setting
1243 * happens through lod_declare_init_size(), and not through
1244 * this function. Therefore we need not load striping unless
1245 * ownership is changing. This should save memory and (we hope)
1246 * speed up rename().
1248 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1249 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1252 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1255 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID | LA_MODE |
1256 LA_ATIME | LA_MTIME | LA_CTIME |
1261 * load striping information, notice we don't do this when object
1262 * is being initialized as we don't need this information till
1263 * few specific cases like destroy, chown
1265 rc = lod_striping_load(env, lo);
1269 if (!lod_obj_is_striped(dt))
1273 * if object is striped declare changes on the stripes
1275 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1276 LASSERT(lo->ldo_stripe);
1277 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1278 if (lo->ldo_stripe[i] == NULL)
1280 if (!dt_object_exists(lo->ldo_stripe[i]))
1282 rc = lod_sub_declare_attr_set(env, lo->ldo_stripe[i],
1288 struct lod_obj_stripe_cb_data data = { { 0 } };
1290 data.locd_attr = attr;
1291 data.locd_declare = true;
1292 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1293 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1299 if (!dt_object_exists(next) || dt_object_remote(next) ||
1300 !S_ISREG(attr->la_mode))
1303 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1304 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
1308 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1309 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1310 struct lod_thread_info *info = lod_env_info(env);
1311 struct lu_buf *buf = &info->lti_buf;
1313 buf->lb_buf = info->lti_ea_store;
1314 buf->lb_len = info->lti_ea_store_size;
1315 rc = lod_sub_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1316 LU_XATTR_REPLACE, th);
1323 * Implementation of dt_object_operations::do_attr_set.
1325 * If the object is striped, then apply the changes to all or subset of
1326 * the stripes depending on the object type and specific attributes.
1328 * \see dt_object_operations::do_attr_set() in the API description for details.
1330 static int lod_attr_set(const struct lu_env *env,
1331 struct dt_object *dt,
1332 const struct lu_attr *attr,
1335 struct dt_object *next = dt_object_child(dt);
1336 struct lod_object *lo = lod_dt_obj(dt);
1341 * apply changes to the local object
1343 rc = lod_sub_attr_set(env, next, attr, th);
1347 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1348 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1351 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1354 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE | LA_PROJID |
1355 LA_ATIME | LA_MTIME | LA_CTIME |
1360 /* FIXME: a tricky case in the code path of mdd_layout_change():
1361 * the in-memory striping information has been freed in lod_xattr_set()
1362 * due to layout change. It has to load stripe here again. It only
1363 * changes flags of layout so declare_attr_set() is still accurate */
1364 rc = lod_striping_load(env, lo);
1368 if (!lod_obj_is_striped(dt))
1372 * if object is striped, apply changes to all the stripes
1374 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1375 LASSERT(lo->ldo_stripe);
1376 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1377 if (unlikely(lo->ldo_stripe[i] == NULL))
1380 if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1383 rc = lod_sub_attr_set(env, lo->ldo_stripe[i], attr, th);
1388 struct lod_obj_stripe_cb_data data = { { 0 } };
1390 data.locd_attr = attr;
1391 data.locd_declare = false;
1392 data.locd_comp_skip_cb = lod_obj_attr_set_comp_skip_cb;
1393 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1394 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1400 if (!dt_object_exists(next) || dt_object_remote(next) ||
1401 !S_ISREG(attr->la_mode))
1404 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1405 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
1409 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1410 struct lod_thread_info *info = lod_env_info(env);
1411 struct lu_buf *buf = &info->lti_buf;
1412 struct ost_id *oi = &info->lti_ostid;
1413 struct lu_fid *fid = &info->lti_fid;
1414 struct lov_mds_md_v1 *lmm;
1415 struct lov_ost_data_v1 *objs;
1418 rc = lod_get_lov_ea(env, lo);
1422 buf->lb_buf = info->lti_ea_store;
1423 buf->lb_len = info->lti_ea_store_size;
1424 lmm = info->lti_ea_store;
1425 magic = le32_to_cpu(lmm->lmm_magic);
1426 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
1427 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1428 struct lov_comp_md_entry_v1 *lcme =
1429 &lcm->lcm_entries[0];
1431 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1432 magic = le32_to_cpu(lmm->lmm_magic);
1435 if (magic == LOV_MAGIC_V1)
1436 objs = &(lmm->lmm_objects[0]);
1438 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1439 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1440 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1442 fid_to_ostid(fid, oi);
1443 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1445 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1446 LU_XATTR_REPLACE, th);
1447 } else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1448 struct lod_thread_info *info = lod_env_info(env);
1449 struct lu_buf *buf = &info->lti_buf;
1450 struct lov_comp_md_v1 *lcm;
1451 struct lov_comp_md_entry_v1 *lcme;
1453 rc = lod_get_lov_ea(env, lo);
1457 buf->lb_buf = info->lti_ea_store;
1458 buf->lb_len = info->lti_ea_store_size;
1460 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1 &&
1461 le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_SEL)
1464 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1465 lcme = &lcm->lcm_entries[0];
1466 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1467 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1469 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1470 LU_XATTR_REPLACE, th);
1477 * Implementation of dt_object_operations::do_xattr_get.
1479 * If LOV EA is requested from the root object and it's not
1480 * found, then return default striping for the filesystem.
1482 * \see dt_object_operations::do_xattr_get() in the API description for details.
1484 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1485 struct lu_buf *buf, const char *name)
1487 struct lod_thread_info *info = lod_env_info(env);
1488 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1493 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1494 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1495 struct lmv_mds_md_v1 *lmv1;
1496 struct lmv_foreign_md *lfm;
1499 if (rc > (typeof(rc))sizeof(*lmv1))
1502 /* short (<= sizeof(struct lmv_mds_md_v1)) foreign LMV case */
1503 /* XXX empty foreign LMV is not allowed */
1504 if (rc <= offsetof(typeof(*lfm), lfm_value))
1505 RETURN(rc = rc > 0 ? -EINVAL : rc);
1507 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1508 CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1510 /* lti_buf is large enough for *lmv1 or a short
1511 * (<= sizeof(struct lmv_mds_md_v1)) foreign LMV
1513 info->lti_buf.lb_buf = info->lti_key;
1514 info->lti_buf.lb_len = sizeof(*lmv1);
1515 rc = dt_xattr_get(env, dt_object_child(dt),
1516 &info->lti_buf, name);
1517 if (unlikely(rc <= offsetof(typeof(*lfm),
1519 RETURN(rc = rc > 0 ? -EINVAL : rc);
1521 lfm = info->lti_buf.lb_buf;
1522 if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN)
1525 if (unlikely(rc != sizeof(*lmv1)))
1526 RETURN(rc = rc > 0 ? -EINVAL : rc);
1528 lmv1 = info->lti_buf.lb_buf;
1529 /* The on-disk LMV EA only contains header, but the
1530 * returned LMV EA size should contain the space for
1531 * the FIDs of all shards of the striped directory. */
1532 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1533 rc = lmv_mds_md_size(
1534 le32_to_cpu(lmv1->lmv_stripe_count),
1538 if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN)
1541 if (rc != sizeof(*lmv1))
1542 RETURN(rc = rc > 0 ? -EINVAL : rc);
1544 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1548 RETURN(rc = rc1 != 0 ? rc1 : rc);
1551 if ((rc > 0) && buf->lb_buf && strcmp(name, XATTR_NAME_LOV) == 0) {
1552 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1554 if (lcm->lcm_magic == cpu_to_le32(LOV_MAGIC_SEL))
1555 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1558 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1562 * XXX: Only used by lfsck
1564 * lod returns default striping on the real root of the device
1565 * this is like the root stores default striping for the whole
1566 * filesystem. historically we've been using a different approach
1567 * and store it in the config.
1569 dt_root_get(env, dev->lod_child, &info->lti_fid);
1570 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1572 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1573 struct lov_user_md *lum = buf->lb_buf;
1574 struct lov_desc *desc = &dev->lod_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);
1948 static int lod_prep_md_striped_create(const struct lu_env *env,
1949 struct dt_object *dt,
1950 struct lu_attr *attr,
1951 const struct lmv_user_md_v1 *lum,
1952 struct dt_object_format *dof,
1955 struct lod_thread_info *info = lod_env_info(env);
1956 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1957 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1958 struct lod_object *lo = lod_dt_obj(dt);
1959 struct dt_object **stripe;
1966 bool is_specific = false;
1969 /* The lum has been verifed in lod_verify_md_striping */
1970 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC ||
1971 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC);
1973 stripe_count = lo->ldo_dir_stripe_count;
1975 OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1976 if (idx_array == NULL)
1979 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1981 GOTO(out_free, rc = -ENOMEM);
1983 /* Start index must be the master MDT */
1984 master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1985 idx_array[0] = master_index;
1986 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) {
1988 for (i = 1; i < stripe_count; i++)
1989 idx_array[i] = le32_to_cpu(lum->lum_objects[i].lum_mds);
1992 for (i = 0; i < stripe_count; i++) {
1993 struct lod_tgt_desc *tgt = NULL;
1994 struct dt_object *dto;
1995 struct lu_fid fid = { 0 };
1997 struct lu_object_conf conf = { 0 };
1998 struct dt_device *tgt_dt = NULL;
2000 /* Try to find next avaible target */
2002 for (j = 0; j < lod->lod_remote_mdt_count;
2003 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
2004 bool already_allocated = false;
2007 CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
2008 idx, lod->lod_remote_mdt_count + 1, i);
2010 if (likely(!is_specific &&
2011 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
2012 /* check whether the idx already exists
2013 * in current allocated array */
2014 for (k = 0; k < i; k++) {
2015 if (idx_array[k] == idx) {
2016 already_allocated = true;
2021 if (already_allocated)
2025 /* Sigh, this index is not in the bitmap, let's check
2026 * next available target */
2027 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) &&
2028 idx != master_index)
2031 if (idx == master_index) {
2032 /* Allocate the FID locally */
2033 rc = obd_fid_alloc(env, lod->lod_child_exp,
2037 tgt_dt = lod->lod_child;
2041 /* check the status of the OSP */
2042 tgt = LTD_TGT(ltd, idx);
2046 tgt_dt = tgt->ltd_tgt;
2047 rc = dt_statfs(env, tgt_dt, &info->lti_osfs);
2049 /* this OSP doesn't feel well */
2054 rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
2063 /* Can not allocate more stripes */
2064 if (j == lod->lod_remote_mdt_count) {
2065 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
2066 lod2obd(lod)->obd_name, stripe_count, i);
2070 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
2071 idx, i, PFID(&fid));
2073 /* Set the start index for next stripe allocation */
2074 if (!is_specific && i < stripe_count - 1) {
2076 * for large dir test, put all other slaves on one
2077 * remote MDT, otherwise we may save too many local
2078 * slave locks which will exceed RS_MAX_LOCKS.
2080 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)))
2082 idx_array[i + 1] = (idx + 1) %
2083 (lod->lod_remote_mdt_count + 1);
2085 /* tgt_dt and fid must be ready after search avaible OSP
2086 * in the above loop */
2087 LASSERT(tgt_dt != NULL);
2088 LASSERT(fid_is_sane(&fid));
2090 /* fail a remote stripe FID allocation */
2091 if (i && OBD_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_FID))
2094 conf.loc_flags = LOC_F_NEW;
2095 dto = dt_locate_at(env, tgt_dt, &fid,
2096 dt->do_lu.lo_dev->ld_site->ls_top_dev,
2099 GOTO(out_put, rc = PTR_ERR(dto));
2103 lo->ldo_dir_striped = 1;
2104 lo->ldo_stripe = stripe;
2105 lo->ldo_dir_stripe_count = i;
2106 lo->ldo_dir_stripes_allocated = stripe_count;
2108 lo->ldo_dir_stripe_loaded = 1;
2110 if (lo->ldo_dir_stripe_count == 0)
2111 GOTO(out_put, rc = -ENOSPC);
2113 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
2119 for (i = 0; i < stripe_count; i++)
2120 if (stripe[i] != NULL)
2121 dt_object_put(env, stripe[i]);
2122 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
2123 lo->ldo_dir_stripe_count = 0;
2124 lo->ldo_dir_stripes_allocated = 0;
2125 lo->ldo_stripe = NULL;
2129 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
2136 * Alloc cached foreign LMV
2138 * \param[in] lo object
2139 * \param[in] size size of foreign LMV
2141 * \retval 0 on success
2142 * \retval negative if failed
2144 int lod_alloc_foreign_lmv(struct lod_object *lo, size_t size)
2146 OBD_ALLOC_LARGE(lo->ldo_foreign_lmv, size);
2147 if (lo->ldo_foreign_lmv == NULL)
2149 lo->ldo_foreign_lmv_size = size;
2150 lo->ldo_dir_is_foreign = 1;
2156 * Declare create striped md object.
2158 * The function declares intention to create a striped directory. This is a
2159 * wrapper for lod_prep_md_striped_create(). The only additional functionality
2160 * is to verify pattern \a lum_buf is good. Check that function for the details.
2162 * \param[in] env execution environment
2163 * \param[in] dt object
2164 * \param[in] attr attributes to initialize the objects with
2165 * \param[in] lum_buf a pattern specifying the number of stripes and
2167 * \param[in] dof type of objects to be created
2168 * \param[in] th transaction handle
2170 * \retval 0 on success
2171 * \retval negative if failed
2174 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
2175 struct dt_object *dt,
2176 struct lu_attr *attr,
2177 const struct lu_buf *lum_buf,
2178 struct dt_object_format *dof,
2181 struct lod_object *lo = lod_dt_obj(dt);
2182 struct lmv_user_md_v1 *lum = lum_buf->lb_buf;
2186 LASSERT(lum != NULL);
2188 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
2189 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
2190 (int)le32_to_cpu(lum->lum_stripe_offset));
2192 if (lo->ldo_dir_stripe_count == 0) {
2193 if (lo->ldo_dir_is_foreign) {
2194 rc = lod_alloc_foreign_lmv(lo, lum_buf->lb_len);
2197 memcpy(lo->ldo_foreign_lmv, lum, lum_buf->lb_len);
2198 lo->ldo_dir_stripe_loaded = 1;
2203 /* prepare dir striped objects */
2204 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2206 /* failed to create striping, let's reset
2207 * config so that others don't get confused */
2208 lod_striping_free(env, lo);
2216 * Append source stripes after target stripes for migrating directory. NB, we
2217 * only need to declare this, the append is done inside lod_xattr_set_lmv().
2219 * \param[in] env execution environment
2220 * \param[in] dt target object
2221 * \param[in] buf LMV buf which contains source stripe fids
2222 * \param[in] th transaction handle
2224 * \retval 0 on success
2225 * \retval negative if failed
2227 static int lod_dir_declare_layout_add(const struct lu_env *env,
2228 struct dt_object *dt,
2229 const struct lu_buf *buf,
2232 struct lod_thread_info *info = lod_env_info(env);
2233 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
2234 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
2235 struct lod_object *lo = lod_dt_obj(dt);
2236 struct dt_object *next = dt_object_child(dt);
2237 struct dt_object_format *dof = &info->lti_format;
2238 struct lmv_mds_md_v1 *lmv = buf->lb_buf;
2239 struct dt_object **stripe;
2240 __u32 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
2241 struct lu_fid *fid = &info->lti_fid;
2242 struct lod_tgt_desc *tgt;
2243 struct dt_object *dto;
2244 struct dt_device *tgt_dt;
2245 int type = LU_SEQ_RANGE_ANY;
2246 struct dt_insert_rec *rec = &info->lti_dt_rec;
2247 char *stripe_name = info->lti_key;
2248 struct lu_name *sname;
2249 struct linkea_data ldata = { NULL };
2250 struct lu_buf linkea_buf;
2257 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
2260 if (stripe_count == 0)
2263 dof->dof_type = DFT_DIR;
2266 sizeof(*stripe) * (lo->ldo_dir_stripe_count + stripe_count));
2270 for (i = 0; i < lo->ldo_dir_stripe_count; i++)
2271 stripe[i] = lo->ldo_stripe[i];
2273 for (i = 0; i < stripe_count; i++) {
2275 &lmv->lmv_stripe_fids[i]);
2276 if (!fid_is_sane(fid))
2279 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
2283 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
2284 tgt_dt = lod->lod_child;
2286 tgt = LTD_TGT(ltd, idx);
2288 GOTO(out, rc = -ESTALE);
2289 tgt_dt = tgt->ltd_tgt;
2292 dto = dt_locate_at(env, tgt_dt, fid,
2293 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
2296 GOTO(out, rc = PTR_ERR(dto));
2298 stripe[i + lo->ldo_dir_stripe_count] = dto;
2300 if (!dt_try_as_dir(env, dto))
2301 GOTO(out, rc = -ENOTDIR);
2303 rc = lod_sub_declare_ref_add(env, dto, th);
2307 rc = lod_sub_declare_insert(env, dto,
2308 (const struct dt_rec *)rec,
2309 (const struct dt_key *)dot, th);
2313 rc = lod_sub_declare_insert(env, dto,
2314 (const struct dt_rec *)rec,
2315 (const struct dt_key *)dotdot, th);
2319 rc = lod_sub_declare_xattr_set(env, dto, buf,
2320 XATTR_NAME_LMV, 0, th);
2324 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
2325 PFID(lu_object_fid(&dto->do_lu)),
2326 i + lo->ldo_dir_stripe_count);
2328 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2329 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
2330 sname, lu_object_fid(&dt->do_lu));
2334 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
2335 linkea_buf.lb_len = ldata.ld_leh->leh_len;
2336 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
2337 XATTR_NAME_LINK, 0, th);
2341 rc = lod_sub_declare_insert(env, next,
2342 (const struct dt_rec *)rec,
2343 (const struct dt_key *)stripe_name,
2348 rc = lod_sub_declare_ref_add(env, next, th);
2354 OBD_FREE(lo->ldo_stripe,
2355 sizeof(*stripe) * lo->ldo_dir_stripes_allocated);
2356 lo->ldo_stripe = stripe;
2357 lo->ldo_dir_migrate_offset = lo->ldo_dir_stripe_count;
2358 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_hash_type);
2359 lo->ldo_dir_stripe_count += stripe_count;
2360 lo->ldo_dir_stripes_allocated += stripe_count;
2361 lo->ldo_dir_hash_type |= LMV_HASH_FLAG_MIGRATION;
2365 i = lo->ldo_dir_stripe_count;
2366 while (i < lo->ldo_dir_stripe_count + stripe_count && stripe[i])
2367 dt_object_put(env, stripe[i++]);
2370 sizeof(*stripe) * (stripe_count + lo->ldo_dir_stripe_count));
2374 static int lod_dir_declare_layout_delete(const struct lu_env *env,
2375 struct dt_object *dt,
2376 const struct lu_buf *buf,
2379 struct lod_thread_info *info = lod_env_info(env);
2380 struct lod_object *lo = lod_dt_obj(dt);
2381 struct dt_object *next = dt_object_child(dt);
2382 struct lmv_user_md *lmu = buf->lb_buf;
2383 __u32 final_stripe_count;
2384 char *stripe_name = info->lti_key;
2385 struct dt_object *dto;
2392 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
2393 if (final_stripe_count >= lo->ldo_dir_stripe_count)
2396 for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
2397 dto = lo->ldo_stripe[i];
2401 if (!dt_try_as_dir(env, dto))
2404 rc = lod_sub_declare_delete(env, dto,
2405 (const struct dt_key *)dot, th);
2409 rc = lod_sub_declare_ref_del(env, dto, th);
2413 rc = lod_sub_declare_delete(env, dto,
2414 (const struct dt_key *)dotdot, th);
2418 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2419 PFID(lu_object_fid(&dto->do_lu)), i);
2421 rc = lod_sub_declare_delete(env, next,
2422 (const struct dt_key *)stripe_name, th);
2426 rc = lod_sub_declare_ref_del(env, next, th);
2435 * delete stripes from dir master object, the lum_stripe_count in argument is
2436 * the final stripe count, the stripes after that will be deleted, NB, they
2437 * are not destroyed, but deleted from it's parent namespace, this function
2438 * will be called in two places:
2439 * 1. mdd_migrate_create() delete stripes from source, and append them to
2441 * 2. mdd_dir_layout_shrink() delete stripes from source, and destroy them.
2443 static int lod_dir_layout_delete(const struct lu_env *env,
2444 struct dt_object *dt,
2445 const struct lu_buf *buf,
2448 struct lod_thread_info *info = lod_env_info(env);
2449 struct lod_object *lo = lod_dt_obj(dt);
2450 struct dt_object *next = dt_object_child(dt);
2451 struct lmv_user_md *lmu = buf->lb_buf;
2452 __u32 final_stripe_count;
2453 char *stripe_name = info->lti_key;
2454 struct dt_object *dto;
2463 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
2464 if (final_stripe_count >= lo->ldo_dir_stripe_count)
2467 for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
2468 dto = lo->ldo_stripe[i];
2472 rc = lod_sub_delete(env, dto,
2473 (const struct dt_key *)dotdot, th);
2477 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2478 PFID(lu_object_fid(&dto->do_lu)), i);
2480 rc = lod_sub_delete(env, next,
2481 (const struct dt_key *)stripe_name, th);
2485 rc = lod_sub_ref_del(env, next, th);
2490 lod_striping_free(env, lod_dt_obj(dt));
2496 * Implementation of dt_object_operations::do_declare_xattr_set.
2498 * Used with regular (non-striped) objects. Basically it
2499 * initializes the striping information and applies the
2500 * change to all the stripes.
2502 * \see dt_object_operations::do_declare_xattr_set() in the API description
2505 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2506 struct dt_object *dt,
2507 const struct lu_buf *buf,
2508 const char *name, int fl,
2511 struct dt_object *next = dt_object_child(dt);
2512 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2513 struct lod_object *lo = lod_dt_obj(dt);
2518 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2519 struct lmv_user_md_v1 *lum;
2521 LASSERT(buf != NULL && buf->lb_buf != NULL);
2523 rc = lod_verify_md_striping(d, lum);
2526 } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2527 rc = lod_verify_striping(d, lo, buf, false);
2532 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2536 /* Note: Do not set LinkEA on sub-stripes, otherwise
2537 * it will confuse the fid2path process(see mdt_path_current()).
2538 * The linkEA between master and sub-stripes is set in
2539 * lod_xattr_set_lmv(). */
2540 if (strcmp(name, XATTR_NAME_LINK) == 0)
2543 /* set xattr to each stripes, if needed */
2544 rc = lod_striping_load(env, lo);
2548 if (lo->ldo_dir_stripe_count == 0)
2551 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2552 if (!lo->ldo_stripe[i])
2555 if (!dt_object_exists(lo->ldo_stripe[i]))
2558 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2568 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2569 struct lod_object *lo,
2570 struct dt_object *dt, struct thandle *th,
2571 int comp_idx, int stripe_idx,
2572 struct lod_obj_stripe_cb_data *data)
2574 struct lod_thread_info *info = lod_env_info(env);
2575 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
2576 struct filter_fid *ff = &info->lti_ff;
2577 struct lu_buf *buf = &info->lti_buf;
2581 buf->lb_len = sizeof(*ff);
2582 rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2590 * locd_buf is set if it's called by dir migration, which doesn't check
2593 if (data->locd_buf) {
2594 memset(ff, 0, sizeof(*ff));
2595 ff->ff_parent = *(struct lu_fid *)data->locd_buf->lb_buf;
2597 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
2599 if (lu_fid_eq(lod_object_fid(lo), &ff->ff_parent) &&
2600 ff->ff_layout.ol_comp_id == comp->llc_id)
2603 memset(ff, 0, sizeof(*ff));
2604 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2607 /* rewrite filter_fid */
2608 ff->ff_parent.f_ver = stripe_idx;
2609 ff->ff_layout.ol_stripe_size = comp->llc_stripe_size;
2610 ff->ff_layout.ol_stripe_count = comp->llc_stripe_count;
2611 ff->ff_layout.ol_comp_id = comp->llc_id;
2612 ff->ff_layout.ol_comp_start = comp->llc_extent.e_start;
2613 ff->ff_layout.ol_comp_end = comp->llc_extent.e_end;
2614 filter_fid_cpu_to_le(ff, ff, sizeof(*ff));
2616 if (data->locd_declare)
2617 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2618 LU_XATTR_REPLACE, th);
2620 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2621 LU_XATTR_REPLACE, th);
2627 * Reset parent FID on OST object
2629 * Replace parent FID with @dt object FID, which is only called during migration
2630 * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2631 * the FID is changed.
2633 * \param[in] env execution environment
2634 * \param[in] dt dt_object whose stripes's parent FID will be reset
2635 * \parem[in] th thandle
2636 * \param[in] declare if it is declare
2638 * \retval 0 if reset succeeds
2639 * \retval negative errno if reset fails
2641 static int lod_replace_parent_fid(const struct lu_env *env,
2642 struct dt_object *dt,
2643 const struct lu_buf *buf,
2644 struct thandle *th, bool declare)
2646 struct lod_object *lo = lod_dt_obj(dt);
2647 struct lod_thread_info *info = lod_env_info(env);
2648 struct filter_fid *ff;
2649 struct lod_obj_stripe_cb_data data = { { 0 } };
2653 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2655 /* set xattr to each stripes, if needed */
2656 rc = lod_striping_load(env, lo);
2660 if (!lod_obj_is_striped(dt))
2663 if (info->lti_ea_store_size < sizeof(*ff)) {
2664 rc = lod_ea_store_resize(info, sizeof(*ff));
2669 data.locd_declare = declare;
2670 data.locd_stripe_cb = lod_obj_stripe_replace_parent_fid_cb;
2671 data.locd_buf = buf;
2672 rc = lod_obj_for_each_stripe(env, lo, th, &data);
2677 inline __u16 lod_comp_entry_stripe_count(struct lod_object *lo,
2678 struct lod_layout_component *entry,
2681 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2685 else if (lod_comp_inited(entry))
2686 return entry->llc_stripe_count;
2687 else if ((__u16)-1 == entry->llc_stripe_count)
2688 return lod->lod_desc.ld_tgt_count;
2690 return lod_get_stripe_count(lod, lo,
2691 entry->llc_stripe_count, false);
2694 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2696 int magic, size = 0, i;
2697 struct lod_layout_component *comp_entries;
2699 bool is_composite, is_foreign = false;
2702 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2703 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2705 lo->ldo_def_striping->lds_def_striping_is_composite;
2707 comp_cnt = lo->ldo_comp_cnt;
2708 comp_entries = lo->ldo_comp_entries;
2709 is_composite = lo->ldo_is_composite;
2710 is_foreign = lo->ldo_is_foreign;
2714 return lo->ldo_foreign_lov_size;
2716 LASSERT(comp_cnt != 0 && comp_entries != NULL);
2718 size = sizeof(struct lov_comp_md_v1) +
2719 sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2720 LASSERT(size % sizeof(__u64) == 0);
2723 for (i = 0; i < comp_cnt; i++) {
2726 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2727 stripe_count = lod_comp_entry_stripe_count(lo, &comp_entries[i],
2729 if (!is_dir && is_composite)
2730 lod_comp_shrink_stripe_count(&comp_entries[i],
2733 size += lov_user_md_size(stripe_count, magic);
2734 LASSERT(size % sizeof(__u64) == 0);
2740 * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2741 * the xattr value is binary lov_comp_md_v1 which contains component(s)
2744 * \param[in] env execution environment
2745 * \param[in] dt dt_object to add components on
2746 * \param[in] buf buffer contains components to be added
2747 * \parem[in] th thandle
2749 * \retval 0 on success
2750 * \retval negative errno on failure
2752 static int lod_declare_layout_add(const struct lu_env *env,
2753 struct dt_object *dt,
2754 const struct lu_buf *buf,
2757 struct lod_thread_info *info = lod_env_info(env);
2758 struct lod_layout_component *comp_array, *lod_comp, *old_array;
2759 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2760 struct dt_object *next = dt_object_child(dt);
2761 struct lov_desc *desc = &d->lod_desc;
2762 struct lod_object *lo = lod_dt_obj(dt);
2763 struct lov_user_md_v3 *v3;
2764 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2766 int i, rc, array_cnt, old_array_cnt;
2769 LASSERT(lo->ldo_is_composite);
2771 if (lo->ldo_flr_state != LCM_FL_NONE)
2774 rc = lod_verify_striping(d, lo, buf, false);
2778 magic = comp_v1->lcm_magic;
2779 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2780 lustre_swab_lov_comp_md_v1(comp_v1);
2781 magic = comp_v1->lcm_magic;
2784 if (magic != LOV_USER_MAGIC_COMP_V1)
2787 array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2788 OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2789 if (comp_array == NULL)
2792 memcpy(comp_array, lo->ldo_comp_entries,
2793 sizeof(*comp_array) * lo->ldo_comp_cnt);
2795 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2796 struct lov_user_md_v1 *v1;
2797 struct lu_extent *ext;
2799 v1 = (struct lov_user_md *)((char *)comp_v1 +
2800 comp_v1->lcm_entries[i].lcme_offset);
2801 ext = &comp_v1->lcm_entries[i].lcme_extent;
2803 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2804 lod_comp->llc_extent.e_start = ext->e_start;
2805 lod_comp->llc_extent.e_end = ext->e_end;
2806 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2807 lod_comp->llc_flags = comp_v1->lcm_entries[i].lcme_flags;
2809 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2810 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2811 lod_adjust_stripe_info(lod_comp, desc, 0);
2813 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2814 v3 = (struct lov_user_md_v3 *) v1;
2815 if (v3->lmm_pool_name[0] != '\0') {
2816 rc = lod_set_pool(&lod_comp->llc_pool,
2824 old_array = lo->ldo_comp_entries;
2825 old_array_cnt = lo->ldo_comp_cnt;
2827 lo->ldo_comp_entries = comp_array;
2828 lo->ldo_comp_cnt = array_cnt;
2830 /* No need to increase layout generation here, it will be increased
2831 * later when generating component ID for the new components */
2833 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2834 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2835 XATTR_NAME_LOV, 0, th);
2837 lo->ldo_comp_entries = old_array;
2838 lo->ldo_comp_cnt = old_array_cnt;
2842 OBD_FREE(old_array, sizeof(*lod_comp) * old_array_cnt);
2844 LASSERT(lo->ldo_mirror_count == 1);
2845 lo->ldo_mirrors[0].lme_end = array_cnt - 1;
2850 for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2851 lod_comp = &comp_array[i];
2852 if (lod_comp->llc_pool != NULL) {
2853 OBD_FREE(lod_comp->llc_pool,
2854 strlen(lod_comp->llc_pool) + 1);
2855 lod_comp->llc_pool = NULL;
2858 OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2863 * lod_last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2864 * @mirror_id: Mirror id to be checked.
2867 * This function checks if a mirror with specified @mirror_id is the last
2868 * non-stale mirror of a LOD object @lo.
2870 * Return: true or false.
2873 bool lod_last_non_stale_mirror(__u16 mirror_id, struct lod_object *lo)
2875 struct lod_layout_component *lod_comp;
2876 bool has_stale_flag;
2879 for (i = 0; i < lo->ldo_mirror_count; i++) {
2880 if (lo->ldo_mirrors[i].lme_id == mirror_id ||
2881 lo->ldo_mirrors[i].lme_stale)
2884 has_stale_flag = false;
2885 lod_foreach_mirror_comp(lod_comp, lo, i) {
2886 if (lod_comp->llc_flags & LCME_FL_STALE) {
2887 has_stale_flag = true;
2891 if (!has_stale_flag)
2899 * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2900 * the '$field' can only be 'flags' now. The xattr value is binary
2901 * lov_comp_md_v1 which contains the component ID(s) and the value of
2902 * the field to be modified.
2904 * \param[in] env execution environment
2905 * \param[in] dt dt_object to be modified
2906 * \param[in] op operation string, like "set.flags"
2907 * \param[in] buf buffer contains components to be set
2908 * \parem[in] th thandle
2910 * \retval 0 on success
2911 * \retval negative errno on failure
2913 static int lod_declare_layout_set(const struct lu_env *env,
2914 struct dt_object *dt,
2915 char *op, const struct lu_buf *buf,
2918 struct lod_layout_component *lod_comp;
2919 struct lod_thread_info *info = lod_env_info(env);
2920 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2921 struct lod_object *lo = lod_dt_obj(dt);
2922 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2925 bool changed = false;
2928 if (strcmp(op, "set.flags") != 0) {
2929 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2930 lod2obd(d)->obd_name, op);
2934 magic = comp_v1->lcm_magic;
2935 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2936 lustre_swab_lov_comp_md_v1(comp_v1);
2937 magic = comp_v1->lcm_magic;
2940 if (magic != LOV_USER_MAGIC_COMP_V1)
2943 if (comp_v1->lcm_entry_count == 0) {
2944 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2945 lod2obd(d)->obd_name);
2949 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2950 __u32 id = comp_v1->lcm_entries[i].lcme_id;
2951 __u32 flags = comp_v1->lcm_entries[i].lcme_flags;
2952 __u32 mirror_flag = flags & LCME_MIRROR_FLAGS;
2953 __u16 mirror_id = mirror_id_of(id);
2954 bool neg = flags & LCME_FL_NEG;
2956 if (flags & LCME_FL_INIT) {
2958 lod_striping_free(env, lo);
2962 flags &= ~(LCME_MIRROR_FLAGS | LCME_FL_NEG);
2963 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2964 lod_comp = &lo->ldo_comp_entries[j];
2966 /* lfs only put one flag in each entry */
2967 if ((flags && id != lod_comp->llc_id) ||
2968 (mirror_flag && mirror_id !=
2969 mirror_id_of(lod_comp->llc_id)))
2974 lod_comp->llc_flags &= ~flags;
2976 lod_comp->llc_flags &= ~mirror_flag;
2979 if ((flags & LCME_FL_STALE) &&
2980 lod_last_non_stale_mirror(mirror_id,
2983 lod_comp->llc_flags |= flags;
2986 lod_comp->llc_flags |= mirror_flag;
2987 if (mirror_flag & LCME_FL_NOSYNC)
2988 lod_comp->llc_timestamp =
2989 ktime_get_real_seconds();
2997 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2998 lod2obd(d)->obd_name);
3002 lod_obj_inc_layout_gen(lo);
3004 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
3005 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), &info->lti_buf,
3006 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3011 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
3012 * and the xattr value is a unique component ID or a special lcme_id.
3014 * \param[in] env execution environment
3015 * \param[in] dt dt_object to be operated on
3016 * \param[in] buf buffer contains component ID or lcme_id
3017 * \parem[in] th thandle
3019 * \retval 0 on success
3020 * \retval negative errno on failure
3022 static int lod_declare_layout_del(const struct lu_env *env,
3023 struct dt_object *dt,
3024 const struct lu_buf *buf,
3027 struct lod_thread_info *info = lod_env_info(env);
3028 struct dt_object *next = dt_object_child(dt);
3029 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3030 struct lod_object *lo = lod_dt_obj(dt);
3031 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3032 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
3033 __u32 magic, id, flags, neg_flags = 0;
3037 LASSERT(lo->ldo_is_composite);
3039 if (lo->ldo_flr_state != LCM_FL_NONE)
3042 magic = comp_v1->lcm_magic;
3043 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3044 lustre_swab_lov_comp_md_v1(comp_v1);
3045 magic = comp_v1->lcm_magic;
3048 if (magic != LOV_USER_MAGIC_COMP_V1)
3051 id = comp_v1->lcm_entries[0].lcme_id;
3052 flags = comp_v1->lcm_entries[0].lcme_flags;
3054 if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
3055 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
3056 lod2obd(d)->obd_name, id, flags);
3060 if (id != LCME_ID_INVAL && flags != 0) {
3061 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
3062 lod2obd(d)->obd_name);
3066 if (id == LCME_ID_INVAL && !flags) {
3067 CDEBUG(D_LAYOUT, "%s: no id or flags specified.\n",
3068 lod2obd(d)->obd_name);
3072 if (flags & LCME_FL_NEG) {
3073 neg_flags = flags & ~LCME_FL_NEG;
3077 left = lo->ldo_comp_cnt;
3081 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3082 struct lod_layout_component *lod_comp;
3084 lod_comp = &lo->ldo_comp_entries[i];
3086 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
3088 else if (flags && !(flags & lod_comp->llc_flags))
3090 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
3093 if (left != (i + 1)) {
3094 CDEBUG(D_LAYOUT, "%s: this deletion will create "
3095 "a hole.\n", lod2obd(d)->obd_name);
3100 /* Mark the component as deleted */
3101 lod_comp->llc_id = LCME_ID_INVAL;
3103 /* Not instantiated component */
3104 if (lod_comp->llc_stripe == NULL)
3107 LASSERT(lod_comp->llc_stripe_count > 0);
3108 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
3109 struct dt_object *obj = lod_comp->llc_stripe[j];
3113 rc = lod_sub_declare_destroy(env, obj, th);
3119 LASSERTF(left >= 0, "left = %d\n", left);
3120 if (left == lo->ldo_comp_cnt) {
3121 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
3122 lod2obd(d)->obd_name, id);
3126 memset(attr, 0, sizeof(*attr));
3127 attr->la_valid = LA_SIZE;
3128 rc = lod_sub_declare_attr_set(env, next, attr, th);
3133 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
3134 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
3135 XATTR_NAME_LOV, 0, th);
3137 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
3144 * Declare layout add/set/del operations issued by special xattr names:
3146 * XATTR_LUSTRE_LOV.add add component(s) to existing file
3147 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
3148 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
3150 * \param[in] env execution environment
3151 * \param[in] dt object
3152 * \param[in] name name of xattr
3153 * \param[in] buf lu_buf contains xattr value
3154 * \param[in] th transaction handle
3156 * \retval 0 on success
3157 * \retval negative if failed
3159 static int lod_declare_modify_layout(const struct lu_env *env,
3160 struct dt_object *dt,
3162 const struct lu_buf *buf,
3165 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3166 struct lod_object *lo = lod_dt_obj(dt);
3168 int rc, len = strlen(XATTR_LUSTRE_LOV);
3171 LASSERT(dt_object_exists(dt));
3173 if (strlen(name) <= len || name[len] != '.') {
3174 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
3175 lod2obd(d)->obd_name, name);
3180 rc = lod_striping_load(env, lo);
3184 /* the layout to be modified must be a composite layout */
3185 if (!lo->ldo_is_composite) {
3186 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
3187 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3188 GOTO(unlock, rc = -EINVAL);
3191 op = (char *)name + len;
3192 if (strcmp(op, "add") == 0) {
3193 rc = lod_declare_layout_add(env, dt, buf, th);
3194 } else if (strcmp(op, "del") == 0) {
3195 rc = lod_declare_layout_del(env, dt, buf, th);
3196 } else if (strncmp(op, "set", strlen("set")) == 0) {
3197 rc = lod_declare_layout_set(env, dt, op, buf, th);
3199 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
3200 lod2obd(d)->obd_name, name);
3201 GOTO(unlock, rc = -ENOTSUPP);
3205 lod_striping_free(env, lo);
3211 * Convert a plain file lov_mds_md to a composite layout.
3213 * \param[in,out] info the thread info::lti_ea_store buffer contains little
3214 * endian plain file layout
3216 * \retval 0 on success, <0 on failure
3218 static int lod_layout_convert(struct lod_thread_info *info)
3220 struct lov_mds_md *lmm = info->lti_ea_store;
3221 struct lov_mds_md *lmm_save;
3222 struct lov_comp_md_v1 *lcm;
3223 struct lov_comp_md_entry_v1 *lcme;
3229 /* realloc buffer to a composite layout which contains one component */
3230 blob_size = lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
3231 le32_to_cpu(lmm->lmm_magic));
3232 size = sizeof(*lcm) + sizeof(*lcme) + blob_size;
3234 OBD_ALLOC_LARGE(lmm_save, blob_size);
3236 GOTO(out, rc = -ENOMEM);
3238 memcpy(lmm_save, lmm, blob_size);
3240 if (info->lti_ea_store_size < size) {
3241 rc = lod_ea_store_resize(info, size);
3246 lcm = info->lti_ea_store;
3247 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
3248 lcm->lcm_size = cpu_to_le32(size);
3249 lcm->lcm_layout_gen = cpu_to_le32(le16_to_cpu(
3250 lmm_save->lmm_layout_gen));
3251 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
3252 lcm->lcm_entry_count = cpu_to_le16(1);
3253 lcm->lcm_mirror_count = 0;
3255 lcme = &lcm->lcm_entries[0];
3256 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
3257 lcme->lcme_extent.e_start = 0;
3258 lcme->lcme_extent.e_end = cpu_to_le64(OBD_OBJECT_EOF);
3259 lcme->lcme_offset = cpu_to_le32(sizeof(*lcm) + sizeof(*lcme));
3260 lcme->lcme_size = cpu_to_le32(blob_size);
3262 memcpy((char *)lcm + lcme->lcme_offset, (char *)lmm_save, blob_size);
3267 OBD_FREE_LARGE(lmm_save, blob_size);
3272 * Merge layouts to form a mirrored file.
3274 static int lod_declare_layout_merge(const struct lu_env *env,
3275 struct dt_object *dt, const struct lu_buf *mbuf,
3278 struct lod_thread_info *info = lod_env_info(env);
3279 struct lu_buf *buf = &info->lti_buf;
3280 struct lod_object *lo = lod_dt_obj(dt);
3281 struct lov_comp_md_v1 *lcm;
3282 struct lov_comp_md_v1 *cur_lcm;
3283 struct lov_comp_md_v1 *merge_lcm;
3284 struct lov_comp_md_entry_v1 *lcme;
3285 struct lov_mds_md_v1 *lmm;
3288 __u16 cur_entry_count;
3289 __u16 merge_entry_count;
3291 __u16 mirror_id = 0;
3298 merge_lcm = mbuf->lb_buf;
3299 if (mbuf->lb_len < sizeof(*merge_lcm))
3302 /* must be an existing layout from disk */
3303 if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
3306 merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
3308 /* do not allow to merge two mirrored files */
3309 if (le16_to_cpu(merge_lcm->lcm_mirror_count))
3312 /* verify the target buffer */
3313 rc = lod_get_lov_ea(env, lo);
3315 RETURN(rc ? : -ENODATA);
3317 cur_lcm = info->lti_ea_store;
3318 switch (le32_to_cpu(cur_lcm->lcm_magic)) {
3321 rc = lod_layout_convert(info);
3323 case LOV_MAGIC_COMP_V1:
3333 /* info->lti_ea_store could be reallocated in lod_layout_convert() */
3334 cur_lcm = info->lti_ea_store;
3335 cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
3337 /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
3338 mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
3339 if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
3342 /* size of new layout */
3343 size = le32_to_cpu(cur_lcm->lcm_size) +
3344 le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
3346 memset(buf, 0, sizeof(*buf));
3347 lu_buf_alloc(buf, size);
3348 if (buf->lb_buf == NULL)
3352 memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
3354 offset = sizeof(*lcm) +
3355 sizeof(*lcme) * (cur_entry_count + merge_entry_count);
3356 for (i = 0; i < cur_entry_count; i++) {
3357 struct lov_comp_md_entry_v1 *cur_lcme;
3359 lcme = &lcm->lcm_entries[i];
3360 cur_lcme = &cur_lcm->lcm_entries[i];
3362 lcme->lcme_offset = cpu_to_le32(offset);
3363 memcpy((char *)lcm + offset,
3364 (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
3365 le32_to_cpu(lcme->lcme_size));
3367 offset += le32_to_cpu(lcme->lcme_size);
3369 if (mirror_count == 1 &&
3370 mirror_id_of(le32_to_cpu(lcme->lcme_id)) == 0) {
3371 /* Add mirror from a non-flr file, create new mirror ID.
3372 * Otherwise, keep existing mirror's component ID, used
3373 * for mirror extension.
3375 id = pflr_id(1, i + 1);
3376 lcme->lcme_id = cpu_to_le32(id);
3379 id = MAX(le32_to_cpu(lcme->lcme_id), id);
3382 mirror_id = mirror_id_of(id) + 1;
3384 /* check if first entry in new layout is DOM */
3385 lmm = (struct lov_mds_md_v1 *)((char *)merge_lcm +
3386 merge_lcm->lcm_entries[0].lcme_offset);
3387 merge_has_dom = lov_pattern(le32_to_cpu(lmm->lmm_pattern)) ==
3390 for (i = 0; i < merge_entry_count; i++) {
3391 struct lov_comp_md_entry_v1 *merge_lcme;
3393 merge_lcme = &merge_lcm->lcm_entries[i];
3394 lcme = &lcm->lcm_entries[cur_entry_count + i];
3396 *lcme = *merge_lcme;
3397 lcme->lcme_offset = cpu_to_le32(offset);
3398 if (merge_has_dom && i == 0)
3399 lcme->lcme_flags |= cpu_to_le32(LCME_FL_STALE);
3401 id = pflr_id(mirror_id, i + 1);
3402 lcme->lcme_id = cpu_to_le32(id);
3404 memcpy((char *)lcm + offset,
3405 (char *)merge_lcm + le32_to_cpu(merge_lcme->lcme_offset),
3406 le32_to_cpu(lcme->lcme_size));
3408 offset += le32_to_cpu(lcme->lcme_size);
3411 /* fixup layout information */
3412 lod_obj_inc_layout_gen(lo);
3413 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3414 lcm->lcm_size = cpu_to_le32(size);
3415 lcm->lcm_entry_count = cpu_to_le16(cur_entry_count + merge_entry_count);
3416 lcm->lcm_mirror_count = cpu_to_le16(mirror_count);
3417 if ((le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK) == LCM_FL_NONE)
3418 lcm->lcm_flags = cpu_to_le32(LCM_FL_RDONLY);
3420 rc = lod_striping_reload(env, lo, buf);
3424 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), buf,
3425 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3433 * Split layouts, just set the LOVEA with the layout from mbuf.
3435 static int lod_declare_layout_split(const struct lu_env *env,
3436 struct dt_object *dt, const struct lu_buf *mbuf,
3439 struct lod_object *lo = lod_dt_obj(dt);
3440 struct lov_comp_md_v1 *lcm = mbuf->lb_buf;
3444 lod_obj_inc_layout_gen(lo);
3445 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3447 rc = lod_striping_reload(env, lo, mbuf);
3451 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), mbuf,
3452 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3457 * Implementation of dt_object_operations::do_declare_xattr_set.
3459 * \see dt_object_operations::do_declare_xattr_set() in the API description
3462 * the extension to the API:
3463 * - declaring LOVEA requests striping creation
3464 * - LU_XATTR_REPLACE means layout swap
3466 static int lod_declare_xattr_set(const struct lu_env *env,
3467 struct dt_object *dt,
3468 const struct lu_buf *buf,
3469 const char *name, int fl,
3472 struct dt_object *next = dt_object_child(dt);
3473 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3478 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
3479 if ((S_ISREG(mode) || mode == 0) &&
3480 !(fl & (LU_XATTR_REPLACE | LU_XATTR_MERGE | LU_XATTR_SPLIT)) &&
3481 (strcmp(name, XATTR_NAME_LOV) == 0 ||
3482 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
3484 * this is a request to create object's striping.
3486 * allow to declare predefined striping on a new (!mode) object
3487 * which is supposed to be replay of regular file creation
3488 * (when LOV setting is declared)
3490 * LU_XATTR_REPLACE is set to indicate a layout swap
3492 if (dt_object_exists(dt)) {
3493 rc = dt_attr_get(env, next, attr);
3497 memset(attr, 0, sizeof(*attr));
3498 attr->la_valid = LA_TYPE | LA_MODE;
3499 attr->la_mode = S_IFREG;
3501 rc = lod_declare_striped_create(env, dt, attr, buf, th);
3502 } else if (fl & LU_XATTR_MERGE) {
3503 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3504 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3505 rc = lod_declare_layout_merge(env, dt, buf, th);
3506 } else if (fl & LU_XATTR_SPLIT) {
3507 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3508 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3509 rc = lod_declare_layout_split(env, dt, buf, th);
3510 } else if (S_ISREG(mode) &&
3511 strlen(name) > strlen(XATTR_LUSTRE_LOV) + 1 &&
3512 strncmp(name, XATTR_LUSTRE_LOV,
3513 strlen(XATTR_LUSTRE_LOV)) == 0) {
3515 * this is a request to modify object's striping.
3516 * add/set/del component(s).
3518 if (!dt_object_exists(dt))
3521 rc = lod_declare_modify_layout(env, dt, name, buf, th);
3522 } else if (strncmp(name, XATTR_NAME_LMV, strlen(XATTR_NAME_LMV)) == 0 &&
3523 strlen(name) > strlen(XATTR_NAME_LMV) + 1) {
3524 const char *op = name + strlen(XATTR_NAME_LMV) + 1;
3527 if (strcmp(op, "add") == 0)
3528 rc = lod_dir_declare_layout_add(env, dt, buf, th);
3529 else if (strcmp(op, "del") == 0)
3530 rc = lod_dir_declare_layout_delete(env, dt, buf, th);
3531 else if (strcmp(op, "set") == 0)
3532 rc = lod_sub_declare_xattr_set(env, next, buf,
3533 XATTR_NAME_LMV, fl, th);
3536 } else if (S_ISDIR(mode)) {
3537 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
3538 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3539 rc = lod_replace_parent_fid(env, dt, buf, th, true);
3541 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
3548 * Apply xattr changes to the object.
3550 * Applies xattr changes to the object and the stripes if the latter exist.
3552 * \param[in] env execution environment
3553 * \param[in] dt object
3554 * \param[in] buf buffer pointing to the new value of xattr
3555 * \param[in] name name of xattr
3556 * \param[in] fl flags
3557 * \param[in] th transaction handle
3559 * \retval 0 on success
3560 * \retval negative if failed
3562 static int lod_xattr_set_internal(const struct lu_env *env,
3563 struct dt_object *dt,
3564 const struct lu_buf *buf,
3565 const char *name, int fl,
3568 struct dt_object *next = dt_object_child(dt);
3569 struct lod_object *lo = lod_dt_obj(dt);
3574 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3575 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3578 /* Note: Do not set LinkEA on sub-stripes, otherwise
3579 * it will confuse the fid2path process(see mdt_path_current()).
3580 * The linkEA between master and sub-stripes is set in
3581 * lod_xattr_set_lmv(). */
3582 if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
3585 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3586 if (!lo->ldo_stripe[i])
3589 if (!dt_object_exists(lo->ldo_stripe[i]))
3592 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
3602 * Delete an extended attribute.
3604 * Deletes specified xattr from the object and the stripes if the latter exist.
3606 * \param[in] env execution environment
3607 * \param[in] dt object
3608 * \param[in] name name of xattr
3609 * \param[in] th transaction handle
3611 * \retval 0 on success
3612 * \retval negative if failed
3614 static int lod_xattr_del_internal(const struct lu_env *env,
3615 struct dt_object *dt,
3616 const char *name, struct thandle *th)
3618 struct dt_object *next = dt_object_child(dt);
3619 struct lod_object *lo = lod_dt_obj(dt);
3624 rc = lod_sub_xattr_del(env, next, name, th);
3625 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3628 if (lo->ldo_dir_stripe_count == 0)
3631 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3632 LASSERT(lo->ldo_stripe[i]);
3634 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3643 * Set default striping on a directory.
3645 * Sets specified striping on a directory object unless it matches the default
3646 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3647 * EA. This striping will be used when regular file is being created in this
3650 * \param[in] env execution environment
3651 * \param[in] dt the striped object
3652 * \param[in] buf buffer with the striping
3653 * \param[in] name name of EA
3654 * \param[in] fl xattr flag (see OSD API description)
3655 * \param[in] th transaction handle
3657 * \retval 0 on success
3658 * \retval negative if failed
3660 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
3661 struct dt_object *dt,
3662 const struct lu_buf *buf,
3663 const char *name, int fl,
3666 struct lov_user_md_v1 *lum;
3667 struct lov_user_md_v3 *v3 = NULL;
3668 const char *pool_name = NULL;
3673 LASSERT(buf != NULL && buf->lb_buf != NULL);
3676 switch (lum->lmm_magic) {
3677 case LOV_USER_MAGIC_SPECIFIC:
3678 case LOV_USER_MAGIC_V3:
3680 if (v3->lmm_pool_name[0] != '\0')
3681 pool_name = v3->lmm_pool_name;
3683 case LOV_USER_MAGIC_V1:
3684 /* if { size, offset, count } = { 0, -1, 0 } and no pool
3685 * (i.e. all default values specified) then delete default
3686 * striping from dir. */
3688 "set default striping: sz %u # %u offset %d %s %s\n",
3689 (unsigned)lum->lmm_stripe_size,
3690 (unsigned)lum->lmm_stripe_count,
3691 (int)lum->lmm_stripe_offset,
3692 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
3694 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
3695 lum->lmm_stripe_count,
3696 lum->lmm_stripe_offset,
3699 case LOV_USER_MAGIC_COMP_V1:
3701 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lum;
3702 struct lov_comp_md_entry_v1 *lcme;
3705 comp_cnt = le16_to_cpu(lcm->lcm_entry_count);
3706 for (i = 0; i < comp_cnt; i++) {
3707 lcme = &lcm->lcm_entries[i];
3708 if (lcme->lcme_flags & cpu_to_le32(LCME_FL_EXTENSION)) {
3709 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_SEL);
3718 CERROR("Invalid magic %x\n", lum->lmm_magic);
3723 rc = lod_xattr_del_internal(env, dt, name, th);
3727 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3734 * Set default striping on a directory object.
3736 * Sets specified striping on a directory object unless it matches the default
3737 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3738 * EA. This striping will be used when a new directory is being created in the
3741 * \param[in] env execution environment
3742 * \param[in] dt the striped object
3743 * \param[in] buf buffer with the striping
3744 * \param[in] name name of EA
3745 * \param[in] fl xattr flag (see OSD API description)
3746 * \param[in] th transaction handle
3748 * \retval 0 on success
3749 * \retval negative if failed
3751 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
3752 struct dt_object *dt,
3753 const struct lu_buf *buf,
3754 const char *name, int fl,
3757 struct lmv_user_md_v1 *lum;
3762 LASSERT(buf != NULL && buf->lb_buf != NULL);
3766 "set default stripe_count # %u stripe_offset %d hash %u\n",
3767 le32_to_cpu(lum->lum_stripe_count),
3768 (int)le32_to_cpu(lum->lum_stripe_offset),
3769 le32_to_cpu(lum->lum_hash_type));
3771 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
3772 le32_to_cpu(lum->lum_stripe_offset)) &&
3773 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC &&
3774 !(le32_to_cpu(lum->lum_hash_type) & LMV_HASH_FLAG_SPACE)) {
3775 rc = lod_xattr_del_internal(env, dt, name, th);
3779 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3788 * Turn directory into a striped directory.
3790 * During replay the client sends the striping created before MDT
3791 * failure, then the layer above LOD sends this defined striping
3792 * using ->do_xattr_set(), so LOD uses this method to replay creation
3793 * of the stripes. Notice the original information for the striping
3794 * (#stripes, FIDs, etc) was transferred in declare path.
3796 * \param[in] env execution environment
3797 * \param[in] dt the striped object
3798 * \param[in] buf not used currently
3799 * \param[in] name not used currently
3800 * \param[in] fl xattr flag (see OSD API description)
3801 * \param[in] th transaction handle
3803 * \retval 0 on success
3804 * \retval negative if failed
3806 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
3807 const struct lu_buf *buf, const char *name,
3808 int fl, struct thandle *th)
3810 struct lod_object *lo = lod_dt_obj(dt);
3811 struct lod_thread_info *info = lod_env_info(env);
3812 struct lu_attr *attr = &info->lti_attr;
3813 struct dt_object_format *dof = &info->lti_format;
3814 struct lu_buf lmv_buf;
3815 struct lu_buf slave_lmv_buf;
3816 struct lmv_mds_md_v1 *lmm;
3817 struct lmv_mds_md_v1 *slave_lmm = NULL;
3818 struct dt_insert_rec *rec = &info->lti_dt_rec;
3823 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3826 /* The stripes are supposed to be allocated in declare phase,
3827 * if there are no stripes being allocated, it will skip */
3828 if (lo->ldo_dir_stripe_count == 0) {
3829 if (lo->ldo_dir_is_foreign) {
3830 rc = lod_sub_xattr_set(env, dt_object_child(dt), buf,
3831 XATTR_NAME_LMV, fl, th);
3838 rc = dt_attr_get(env, dt_object_child(dt), attr);
3842 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
3843 LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
3844 dof->dof_type = DFT_DIR;
3846 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
3849 lmm = lmv_buf.lb_buf;
3851 OBD_ALLOC_PTR(slave_lmm);
3852 if (slave_lmm == NULL)
3855 lod_prep_slave_lmv_md(slave_lmm, lmm);
3856 slave_lmv_buf.lb_buf = slave_lmm;
3857 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
3859 rec->rec_type = S_IFDIR;
3860 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3861 struct dt_object *dto = lo->ldo_stripe[i];
3862 char *stripe_name = info->lti_key;
3863 struct lu_name *sname;
3864 struct linkea_data ldata = { NULL };
3865 struct lu_buf linkea_buf;
3867 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
3871 /* fail a remote stripe creation */
3872 if (i && OBD_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_CREATE))
3875 /* if it's source stripe of migrating directory, don't create */
3876 if (!((lo->ldo_dir_hash_type & LMV_HASH_FLAG_MIGRATION) &&
3877 i >= lo->ldo_dir_migrate_offset)) {
3878 dt_write_lock(env, dto, DT_TGT_CHILD);
3879 rc = lod_sub_create(env, dto, attr, NULL, dof, th);
3881 dt_write_unlock(env, dto);
3885 rc = lod_sub_ref_add(env, dto, th);
3886 dt_write_unlock(env, dto);
3890 rec->rec_fid = lu_object_fid(&dto->do_lu);
3891 rc = lod_sub_insert(env, dto,
3892 (const struct dt_rec *)rec,
3893 (const struct dt_key *)dot, th);
3898 rec->rec_fid = lu_object_fid(&dt->do_lu);
3899 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
3900 (const struct dt_key *)dotdot, th);
3904 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
3905 cfs_fail_val != i) {
3906 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
3908 slave_lmm->lmv_master_mdt_index =
3911 slave_lmm->lmv_master_mdt_index =
3914 rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
3915 XATTR_NAME_LMV, fl, th);
3920 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
3922 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3923 PFID(lu_object_fid(&dto->do_lu)), i + 1);
3925 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3926 PFID(lu_object_fid(&dto->do_lu)), i);
3928 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
3929 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
3930 sname, lu_object_fid(&dt->do_lu));
3934 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
3935 linkea_buf.lb_len = ldata.ld_leh->leh_len;
3936 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
3937 XATTR_NAME_LINK, 0, th);
3941 rec->rec_fid = lu_object_fid(&dto->do_lu);
3942 rc = lod_sub_insert(env, dt_object_child(dt),
3943 (const struct dt_rec *)rec,
3944 (const struct dt_key *)stripe_name, th);
3948 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
3953 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
3954 rc = lod_sub_xattr_set(env, dt_object_child(dt),
3955 &lmv_buf, XATTR_NAME_LMV, fl, th);
3957 if (slave_lmm != NULL)
3958 OBD_FREE_PTR(slave_lmm);
3964 * Helper function to declare/execute creation of a striped directory
3966 * Called in declare/create object path, prepare striping for a directory
3967 * and prepare defaults data striping for the objects to be created in
3968 * that directory. Notice the function calls "declaration" or "execution"
3969 * methods depending on \a declare param. This is a consequence of the
3970 * current approach while we don't have natural distributed transactions:
3971 * we basically execute non-local updates in the declare phase. So, the
3972 * arguments for the both phases are the same and this is the reason for
3973 * this function to exist.
3975 * \param[in] env execution environment
3976 * \param[in] dt object
3977 * \param[in] attr attributes the stripes will be created with
3978 * \param[in] lmu lmv_user_md if MDT indices are specified
3979 * \param[in] dof format of stripes (see OSD API description)
3980 * \param[in] th transaction handle
3981 * \param[in] declare where to call "declare" or "execute" methods
3983 * \retval 0 on success
3984 * \retval negative if failed
3986 static int lod_dir_striping_create_internal(const struct lu_env *env,
3987 struct dt_object *dt,
3988 struct lu_attr *attr,
3989 const struct lu_buf *lmu,
3990 struct dt_object_format *dof,
3994 struct lod_thread_info *info = lod_env_info(env);
3995 struct lod_object *lo = lod_dt_obj(dt);
3996 const struct lod_default_striping *lds = lo->ldo_def_striping;
4000 LASSERT(ergo(lds != NULL,
4001 lds->lds_def_striping_set ||
4002 lds->lds_dir_def_striping_set));
4004 if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
4005 lo->ldo_dir_stripe_offset)) {
4007 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
4008 int stripe_count = lo->ldo_dir_stripe_count;
4010 if (info->lti_ea_store_size < sizeof(*v1)) {
4011 rc = lod_ea_store_resize(info, sizeof(*v1));
4014 v1 = info->lti_ea_store;
4017 memset(v1, 0, sizeof(*v1));
4018 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
4019 v1->lum_stripe_count = cpu_to_le32(stripe_count);
4020 v1->lum_stripe_offset =
4021 cpu_to_le32(lo->ldo_dir_stripe_offset);
4023 info->lti_buf.lb_buf = v1;
4024 info->lti_buf.lb_len = sizeof(*v1);
4025 lmu = &info->lti_buf;
4029 rc = lod_declare_xattr_set_lmv(env, dt, attr, lmu, dof,
4032 rc = lod_xattr_set_lmv(env, dt, lmu, XATTR_NAME_LMV, 0,
4037 /* foreign LMV EA case */
4039 struct lmv_foreign_md *lfm = lmu->lb_buf;
4041 if (lfm->lfm_magic == LMV_MAGIC_FOREIGN) {
4042 rc = lod_declare_xattr_set_lmv(env, dt, attr,
4046 if (lo->ldo_dir_is_foreign) {
4047 LASSERT(lo->ldo_foreign_lmv != NULL &&
4048 lo->ldo_foreign_lmv_size > 0);
4049 info->lti_buf.lb_buf = lo->ldo_foreign_lmv;
4050 info->lti_buf.lb_len = lo->ldo_foreign_lmv_size;
4051 lmu = &info->lti_buf;
4052 rc = lod_xattr_set_lmv(env, dt, lmu,
4053 XATTR_NAME_LMV, 0, th);
4058 /* Transfer default LMV striping from the parent */
4059 if (lds != NULL && lds->lds_dir_def_striping_set &&
4060 !(LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
4061 lds->lds_dir_def_stripe_offset) &&
4062 le32_to_cpu(lds->lds_dir_def_hash_type) !=
4063 LMV_HASH_TYPE_UNKNOWN)) {
4064 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
4066 if (info->lti_ea_store_size < sizeof(*v1)) {
4067 rc = lod_ea_store_resize(info, sizeof(*v1));
4070 v1 = info->lti_ea_store;
4073 memset(v1, 0, sizeof(*v1));
4074 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
4075 v1->lum_stripe_count =
4076 cpu_to_le32(lds->lds_dir_def_stripe_count);
4077 v1->lum_stripe_offset =
4078 cpu_to_le32(lds->lds_dir_def_stripe_offset);
4080 cpu_to_le32(lds->lds_dir_def_hash_type);
4082 info->lti_buf.lb_buf = v1;
4083 info->lti_buf.lb_len = sizeof(*v1);
4085 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4086 XATTR_NAME_DEFAULT_LMV,
4089 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
4091 XATTR_NAME_DEFAULT_LMV, 0,
4097 /* Transfer default LOV striping from the parent */
4098 if (lds != NULL && lds->lds_def_striping_set &&
4099 lds->lds_def_comp_cnt != 0) {
4100 struct lov_mds_md *lmm;
4101 int lmm_size = lod_comp_md_size(lo, true);
4103 if (info->lti_ea_store_size < lmm_size) {
4104 rc = lod_ea_store_resize(info, lmm_size);
4108 lmm = info->lti_ea_store;
4110 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
4114 info->lti_buf.lb_buf = lmm;
4115 info->lti_buf.lb_len = lmm_size;
4118 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4119 XATTR_NAME_LOV, 0, th);
4121 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4122 XATTR_NAME_LOV, 0, th);
4130 static int lod_declare_dir_striping_create(const struct lu_env *env,
4131 struct dt_object *dt,
4132 struct lu_attr *attr,
4134 struct dt_object_format *dof,
4137 return lod_dir_striping_create_internal(env, dt, attr, lmu, dof, th,
4141 static int lod_dir_striping_create(const struct lu_env *env,
4142 struct dt_object *dt,
4143 struct lu_attr *attr,
4144 struct dt_object_format *dof,
4147 return lod_dir_striping_create_internal(env, dt, attr, NULL, dof, th,
4152 * Make LOV EA for striped object.
4154 * Generate striping information and store it in the LOV EA of the given
4155 * object. The caller must ensure nobody else is calling the function
4156 * against the object concurrently. The transaction must be started.
4157 * FLDB service must be running as well; it's used to map FID to the target,
4158 * which is stored in LOV EA.
4160 * \param[in] env execution environment for this thread
4161 * \param[in] lo LOD object
4162 * \param[in] th transaction handle
4164 * \retval 0 if LOV EA is stored successfully
4165 * \retval negative error number on failure
4167 static int lod_generate_and_set_lovea(const struct lu_env *env,
4168 struct lod_object *lo,
4171 struct lod_thread_info *info = lod_env_info(env);
4172 struct dt_object *next = dt_object_child(&lo->ldo_obj);
4173 struct lov_mds_md_v1 *lmm;
4179 if (lo->ldo_comp_cnt == 0 && !lo->ldo_is_foreign) {
4180 lod_striping_free(env, lo);
4181 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
4185 lmm_size = lod_comp_md_size(lo, false);
4186 if (info->lti_ea_store_size < lmm_size) {
4187 rc = lod_ea_store_resize(info, lmm_size);
4191 lmm = info->lti_ea_store;
4193 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
4197 info->lti_buf.lb_buf = lmm;
4198 info->lti_buf.lb_len = lmm_size;
4199 rc = lod_sub_xattr_set(env, next, &info->lti_buf,
4200 XATTR_NAME_LOV, 0, th);
4204 static __u32 lod_gen_component_id(struct lod_object *lo,
4205 int mirror_id, int comp_idx);
4208 * Repeat an existing component
4210 * Creates a new layout by replicating an existing component. Uses striping
4211 * policy from previous component as a template for the striping for the new
4214 * New component starts with zero length, will be extended (or removed) before
4215 * returning layout to client.
4217 * NB: Reallocates layout components array (lo->ldo_comp_entries), invalidating
4218 * any pre-existing pointers to components. Handle with care.
4220 * \param[in] env execution environment for this thread
4221 * \param[in,out] lo object to update the layout of
4222 * \param[in] index index of component to copy
4224 * \retval 0 on success
4225 * \retval negative errno on error
4227 static int lod_layout_repeat_comp(const struct lu_env *env,
4228 struct lod_object *lo, int index)
4230 struct lod_layout_component *lod_comp;
4231 struct lod_layout_component *new_comp = NULL;
4232 struct lod_layout_component *comp_array;
4233 int rc = 0, i, new_cnt = lo->ldo_comp_cnt + 1;
4238 lod_comp = &lo->ldo_comp_entries[index];
4239 LASSERT(lod_comp_inited(lod_comp) && lod_comp->llc_id != LCME_ID_INVAL);
4241 CDEBUG(D_LAYOUT, "repeating component %d\n", index);
4243 OBD_ALLOC(comp_array, sizeof(*comp_array) * new_cnt);
4244 if (comp_array == NULL)
4245 GOTO(out, rc = -ENOMEM);
4247 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4248 memcpy(&comp_array[i + offset], &lo->ldo_comp_entries[i],
4249 sizeof(*comp_array));
4251 /* Duplicate this component in to the next slot */
4253 new_comp = &comp_array[i + 1];
4254 memcpy(&comp_array[i + 1], &lo->ldo_comp_entries[i],
4255 sizeof(*comp_array));
4256 /* We must now skip this new component when copying */
4261 /* Set up copied component */
4262 new_comp->llc_flags &= ~LCME_FL_INIT;
4263 new_comp->llc_stripe = NULL;
4264 new_comp->llc_stripes_allocated = 0;
4265 new_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4266 /* for uninstantiated components, layout gen stores default stripe
4268 new_comp->llc_layout_gen = lod_comp->llc_stripe_offset;
4269 /* This makes the repeated component zero-length, placed at the end of
4270 * the preceding component */
4271 new_comp->llc_extent.e_start = new_comp->llc_extent.e_end;
4272 new_comp->llc_timestamp = lod_comp->llc_timestamp;
4273 new_comp->llc_pool = NULL;
4275 rc = lod_set_pool(&new_comp->llc_pool, lod_comp->llc_pool);
4279 if (new_comp->llc_ostlist.op_array) {
4280 __u32 *op_array = NULL;
4282 OBD_ALLOC(op_array, new_comp->llc_ostlist.op_size);
4284 GOTO(out, rc = -ENOMEM);
4285 memcpy(op_array, &new_comp->llc_ostlist.op_array,
4286 new_comp->llc_ostlist.op_size);
4287 new_comp->llc_ostlist.op_array = op_array;
4290 OBD_FREE(lo->ldo_comp_entries,
4291 sizeof(*comp_array) * lo->ldo_comp_cnt);
4292 lo->ldo_comp_entries = comp_array;
4293 lo->ldo_comp_cnt = new_cnt;
4295 /* Generate an id for the new component */
4296 mirror_id = mirror_id_of(new_comp->llc_id);
4297 new_comp->llc_id = LCME_ID_INVAL;
4298 new_comp->llc_id = lod_gen_component_id(lo, mirror_id, index + 1);
4299 if (new_comp->llc_id == LCME_ID_INVAL)
4300 GOTO(out, rc = -ERANGE);
4305 OBD_FREE(comp_array, sizeof(*comp_array) * new_cnt);
4310 static int lod_layout_data_init(struct lod_thread_info *info, __u32 comp_cnt)
4314 /* clear memory region that will be used for layout change */
4315 memset(&info->lti_layout_attr, 0, sizeof(struct lu_attr));
4316 info->lti_count = 0;
4318 if (info->lti_comp_size >= comp_cnt)
4321 if (info->lti_comp_size > 0) {
4322 OBD_FREE(info->lti_comp_idx,
4323 info->lti_comp_size * sizeof(__u32));
4324 info->lti_comp_size = 0;
4327 OBD_ALLOC(info->lti_comp_idx, comp_cnt * sizeof(__u32));
4328 if (!info->lti_comp_idx)
4331 info->lti_comp_size = comp_cnt;
4336 * Prepare new layout minus deleted components
4338 * Removes components marked for deletion (LCME_ID_INVAL) by copying to a new
4339 * layout and skipping those components. Removes stripe objects if any exist.
4342 * Reallocates layout components array (lo->ldo_comp_entries), invalidating
4343 * any pre-existing pointers to components.
4345 * Caller is responsible for updating mirror end (ldo_mirror[].lme_end).
4347 * \param[in] env execution environment for this thread
4348 * \param[in,out] lo object to update the layout of
4349 * \param[in] th transaction handle for this operation
4351 * \retval # of components deleted
4352 * \retval negative errno on error
4354 static int lod_layout_del_prep_layout(const struct lu_env *env,
4355 struct lod_object *lo,
4358 struct lod_layout_component *lod_comp;
4359 struct lod_thread_info *info = lod_env_info(env);
4360 int rc = 0, i, j, deleted = 0;
4364 LASSERT(lo->ldo_is_composite);
4365 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
4367 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
4371 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4372 lod_comp = &lo->ldo_comp_entries[i];
4374 if (lod_comp->llc_id != LCME_ID_INVAL) {
4375 /* Build array of things to keep */
4376 info->lti_comp_idx[info->lti_count++] = i;
4380 lod_obj_set_pool(lo, i, NULL);
4381 if (lod_comp->llc_ostlist.op_array) {
4382 OBD_FREE(lod_comp->llc_ostlist.op_array,
4383 lod_comp->llc_ostlist.op_size);
4384 lod_comp->llc_ostlist.op_array = NULL;
4385 lod_comp->llc_ostlist.op_size = 0;
4389 CDEBUG(D_LAYOUT, "deleting comp %d, left %d\n", i,
4390 lo->ldo_comp_cnt - deleted);
4392 /* No striping info for this component */
4393 if (lod_comp->llc_stripe == NULL)
4396 LASSERT(lod_comp->llc_stripe_count > 0);
4397 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4398 struct dt_object *obj = lod_comp->llc_stripe[j];
4403 /* components which are not init have no sub objects
4405 if (lod_comp_inited(lod_comp)) {
4406 rc = lod_sub_destroy(env, obj, th);
4411 lu_object_put(env, &obj->do_lu);
4412 lod_comp->llc_stripe[j] = NULL;
4414 OBD_FREE(lod_comp->llc_stripe, sizeof(struct dt_object *) *
4415 lod_comp->llc_stripes_allocated);
4416 lod_comp->llc_stripe = NULL;
4417 OBD_FREE(lod_comp->llc_ost_indices,
4418 sizeof(__u32) * lod_comp->llc_stripes_allocated);
4419 lod_comp->llc_ost_indices = NULL;
4420 lod_comp->llc_stripes_allocated = 0;
4423 /* info->lti_count has the amount of left components */
4424 LASSERTF(info->lti_count >= 0 && info->lti_count < lo->ldo_comp_cnt,
4425 "left = %d, lo->ldo_comp_cnt %d\n", (int)info->lti_count,
4426 (int)lo->ldo_comp_cnt);
4428 if (info->lti_count > 0) {
4429 struct lod_layout_component *comp_array;
4431 OBD_ALLOC(comp_array, sizeof(*comp_array) * info->lti_count);
4432 if (comp_array == NULL)
4433 GOTO(out, rc = -ENOMEM);
4435 for (i = 0; i < info->lti_count; i++) {
4436 memcpy(&comp_array[i],
4437 &lo->ldo_comp_entries[info->lti_comp_idx[i]],
4438 sizeof(*comp_array));
4441 OBD_FREE(lo->ldo_comp_entries,
4442 sizeof(*comp_array) * lo->ldo_comp_cnt);
4443 lo->ldo_comp_entries = comp_array;
4444 lo->ldo_comp_cnt = info->lti_count;
4446 lod_free_comp_entries(lo);
4451 return rc ? rc : deleted;
4455 * Delete layout component(s)
4457 * This function sets up the layout data in the env and does the setattrs
4458 * required to write out the new layout. The layout itself is modified in
4459 * lod_layout_del_prep_layout.
4461 * \param[in] env execution environment for this thread
4462 * \param[in] dt object
4463 * \param[in] th transaction handle
4465 * \retval 0 on success
4466 * \retval negative error number on failure
4468 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
4471 struct lod_object *lo = lod_dt_obj(dt);
4472 struct dt_object *next = dt_object_child(dt);
4473 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4476 LASSERT(lo->ldo_mirror_count == 1);
4478 rc = lod_layout_del_prep_layout(env, lo, th);
4482 /* Only do this if we didn't delete all components */
4483 if (lo->ldo_comp_cnt > 0) {
4484 lo->ldo_mirrors[0].lme_end = lo->ldo_comp_cnt - 1;
4485 lod_obj_inc_layout_gen(lo);
4488 LASSERT(dt_object_exists(dt));
4489 rc = dt_attr_get(env, next, attr);
4493 if (attr->la_size > 0) {
4495 attr->la_valid = LA_SIZE;
4496 rc = lod_sub_attr_set(env, next, attr, th);
4501 rc = lod_generate_and_set_lovea(env, lo, th);
4505 lod_striping_free(env, lo);
4510 static int lod_get_default_lov_striping(const struct lu_env *env,
4511 struct lod_object *lo,
4512 struct lod_default_striping *lds,
4513 struct dt_allocation_hint *ah);
4515 * Implementation of dt_object_operations::do_xattr_set.
4517 * Sets specified extended attribute on the object. Three types of EAs are
4519 * LOV EA - stores striping for a regular file or default striping (when set
4521 * LMV EA - stores a marker for the striped directories
4522 * DMV EA - stores default directory striping
4524 * When striping is applied to a non-striped existing object (this is called
4525 * late striping), then LOD notices the caller wants to turn the object into a
4526 * striped one. The stripe objects are created and appropriate EA is set:
4527 * LOV EA storing all the stripes directly or LMV EA storing just a small header
4528 * with striping configuration.
4530 * \see dt_object_operations::do_xattr_set() in the API description for details.
4532 static int lod_xattr_set(const struct lu_env *env,
4533 struct dt_object *dt, const struct lu_buf *buf,
4534 const char *name, int fl, struct thandle *th)
4536 struct dt_object *next = dt_object_child(dt);
4540 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4541 strcmp(name, XATTR_NAME_LMV) == 0) {
4542 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
4544 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4545 strncmp(name, XATTR_NAME_LMV, strlen(XATTR_NAME_LMV)) == 0 &&
4546 strlen(name) > strlen(XATTR_NAME_LMV) + 1) {
4547 const char *op = name + strlen(XATTR_NAME_LMV) + 1;
4551 * XATTR_NAME_LMV".add" is never called, but only declared,
4552 * because lod_xattr_set_lmv() will do the addition.
4554 if (strcmp(op, "del") == 0)
4555 rc = lod_dir_layout_delete(env, dt, buf, th);
4556 else if (strcmp(op, "set") == 0)
4557 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LMV,
4561 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4562 strcmp(name, XATTR_NAME_LOV) == 0) {
4563 struct lod_default_striping *lds = lod_lds_buf_get(env);
4564 struct lov_user_md_v1 *v1 = buf->lb_buf;
4565 char pool[LOV_MAXPOOLNAME + 1];
4568 /* get existing striping config */
4569 rc = lod_get_default_lov_striping(env, lod_dt_obj(dt), lds,
4574 memset(pool, 0, sizeof(pool));
4575 if (lds->lds_def_striping_set == 1)
4576 lod_layout_get_pool(lds->lds_def_comp_entries,
4577 lds->lds_def_comp_cnt, pool,
4580 is_del = LOVEA_DELETE_VALUES(v1->lmm_stripe_size,
4581 v1->lmm_stripe_count,
4582 v1->lmm_stripe_offset,
4585 /* Retain the pool name if it is not given */
4586 if (v1->lmm_magic == LOV_USER_MAGIC_V1 && pool[0] != '\0' &&
4588 struct lod_thread_info *info = lod_env_info(env);
4589 struct lov_user_md_v3 *v3 = info->lti_ea_store;
4591 memset(v3, 0, sizeof(*v3));
4592 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
4593 v3->lmm_pattern = cpu_to_le32(v1->lmm_pattern);
4594 v3->lmm_stripe_count =
4595 cpu_to_le32(v1->lmm_stripe_count);
4596 v3->lmm_stripe_offset =
4597 cpu_to_le32(v1->lmm_stripe_offset);
4598 v3->lmm_stripe_size = cpu_to_le32(v1->lmm_stripe_size);
4600 strlcpy(v3->lmm_pool_name, pool,
4601 sizeof(v3->lmm_pool_name));
4603 info->lti_buf.lb_buf = v3;
4604 info->lti_buf.lb_len = sizeof(*v3);
4605 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4608 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name,
4612 if (lds->lds_def_striping_set == 1 &&
4613 lds->lds_def_comp_entries != NULL)
4614 lod_free_def_comp_entries(lds);
4617 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4618 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
4620 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
4623 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4624 (!strcmp(name, XATTR_NAME_LOV) ||
4625 !strncmp(name, XATTR_LUSTRE_LOV,
4626 strlen(XATTR_LUSTRE_LOV)))) {
4627 /* in case of lov EA swap, just set it
4628 * if not, it is a replay so check striping match what we
4629 * already have during req replay, declare_xattr_set()
4630 * defines striping, then create() does the work */
4631 if (fl & LU_XATTR_REPLACE) {
4632 /* free stripes, then update disk */
4633 lod_striping_free(env, lod_dt_obj(dt));
4635 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
4636 } else if (dt_object_remote(dt)) {
4637 /* This only happens during migration, see
4638 * mdd_migrate_create(), in which Master MDT will
4639 * create a remote target object, and only set
4640 * (migrating) stripe EA on the remote object,
4641 * and does not need creating each stripes. */
4642 rc = lod_sub_xattr_set(env, next, buf, name,
4644 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
4645 /* delete component(s) */
4646 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
4647 rc = lod_layout_del(env, dt, th);
4650 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
4651 * it's going to create create file with specified
4652 * component(s), the striping must have not being
4653 * cached in this case;
4655 * Otherwise, it's going to add/change component(s) to
4656 * an existing file, the striping must have been cached
4659 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
4660 !strcmp(name, XATTR_NAME_LOV),
4661 !lod_dt_obj(dt)->ldo_comp_cached));
4663 rc = lod_striped_create(env, dt, NULL, NULL, th);
4666 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
4667 rc = lod_replace_parent_fid(env, dt, buf, th, false);
4672 /* then all other xattr */
4673 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
4679 * Implementation of dt_object_operations::do_declare_xattr_del.
4681 * \see dt_object_operations::do_declare_xattr_del() in the API description
4684 static int lod_declare_xattr_del(const struct lu_env *env,
4685 struct dt_object *dt, const char *name,
4688 struct lod_object *lo = lod_dt_obj(dt);
4689 struct dt_object *next = dt_object_child(dt);
4694 rc = lod_sub_declare_xattr_del(env, next, name, th);
4698 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4701 /* NB: don't delete stripe LMV, because when we do this, normally we
4702 * will remove stripes, besides, if directory LMV is corrupt, this will
4703 * prevent deleting its LMV and fixing it (via LFSCK).
4705 if (!strcmp(name, XATTR_NAME_LMV))
4708 rc = lod_striping_load(env, lo);
4712 if (lo->ldo_dir_stripe_count == 0)
4715 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4716 struct dt_object *dto = lo->ldo_stripe[i];
4721 rc = lod_sub_declare_xattr_del(env, dto, name, th);
4730 * Implementation of dt_object_operations::do_xattr_del.
4732 * If EA storing a regular striping is being deleted, then release
4733 * all the references to the stripe objects in core.
4735 * \see dt_object_operations::do_xattr_del() in the API description for details.
4737 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
4738 const char *name, struct thandle *th)
4740 struct dt_object *next = dt_object_child(dt);
4741 struct lod_object *lo = lod_dt_obj(dt);
4746 if (!strcmp(name, XATTR_NAME_LOV) || !strcmp(name, XATTR_NAME_LMV))
4747 lod_striping_free(env, lod_dt_obj(dt));
4749 rc = lod_sub_xattr_del(env, next, name, th);
4750 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
4753 if (!strcmp(name, XATTR_NAME_LMV))
4756 if (lo->ldo_dir_stripe_count == 0)
4759 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4760 struct dt_object *dto = lo->ldo_stripe[i];
4765 rc = lod_sub_xattr_del(env, dto, name, th);
4774 * Implementation of dt_object_operations::do_xattr_list.
4776 * \see dt_object_operations::do_xattr_list() in the API description
4779 static int lod_xattr_list(const struct lu_env *env,
4780 struct dt_object *dt, const struct lu_buf *buf)
4782 return dt_xattr_list(env, dt_object_child(dt), buf);
4785 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
4787 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
4791 * Copy OST list from layout provided by user.
4793 * \param[in] lod_comp layout_component to be filled
4794 * \param[in] v3 LOV EA V3 user data
4796 * \retval 0 on success
4797 * \retval negative if failed
4799 int lod_comp_copy_ost_lists(struct lod_layout_component *lod_comp,
4800 struct lov_user_md_v3 *v3)
4806 if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
4807 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
4809 if (lod_comp->llc_ostlist.op_array) {
4810 if (lod_comp->llc_ostlist.op_size >=
4811 v3->lmm_stripe_count * sizeof(__u32)) {
4812 lod_comp->llc_ostlist.op_count =
4813 v3->lmm_stripe_count;
4816 OBD_FREE(lod_comp->llc_ostlist.op_array,
4817 lod_comp->llc_ostlist.op_size);
4820 /* copy ost list from lmm */
4821 lod_comp->llc_ostlist.op_count = v3->lmm_stripe_count;
4822 lod_comp->llc_ostlist.op_size = v3->lmm_stripe_count * sizeof(__u32);
4823 OBD_ALLOC(lod_comp->llc_ostlist.op_array,
4824 lod_comp->llc_ostlist.op_size);
4825 if (!lod_comp->llc_ostlist.op_array)
4828 for (j = 0; j < v3->lmm_stripe_count; j++) {
4829 lod_comp->llc_ostlist.op_array[j] =
4830 v3->lmm_objects[j].l_ost_idx;
4838 * Get default striping.
4840 * \param[in] env execution environment
4841 * \param[in] lo object
4842 * \param[out] lds default striping
4844 * \retval 0 on success
4845 * \retval negative if failed
4847 static int lod_get_default_lov_striping(const struct lu_env *env,
4848 struct lod_object *lo,
4849 struct lod_default_striping *lds,
4850 struct dt_allocation_hint *ah)
4852 struct lod_thread_info *info = lod_env_info(env);
4853 struct lov_user_md_v1 *v1 = NULL;
4854 struct lov_user_md_v3 *v3 = NULL;
4855 struct lov_comp_md_v1 *comp_v1 = NULL;
4863 rc = lod_get_lov_ea(env, lo);
4867 if (rc < (typeof(rc))sizeof(struct lov_user_md))
4870 v1 = info->lti_ea_store;
4871 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
4872 lustre_swab_lov_user_md_v1(v1);
4873 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
4874 v3 = (struct lov_user_md_v3 *)v1;
4875 lustre_swab_lov_user_md_v3(v3);
4876 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_SPECIFIC)) {
4877 v3 = (struct lov_user_md_v3 *)v1;
4878 lustre_swab_lov_user_md_v3(v3);
4879 lustre_swab_lov_user_md_objects(v3->lmm_objects,
4880 v3->lmm_stripe_count);
4881 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1) ||
4882 v1->lmm_magic == __swab32(LOV_USER_MAGIC_SEL)) {
4883 comp_v1 = (struct lov_comp_md_v1 *)v1;
4884 lustre_swab_lov_comp_md_v1(comp_v1);
4887 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
4888 v1->lmm_magic != LOV_MAGIC_COMP_V1 &&
4889 v1->lmm_magic != LOV_MAGIC_SEL &&
4890 v1->lmm_magic != LOV_USER_MAGIC_SPECIFIC)
4893 if ((v1->lmm_magic == LOV_MAGIC_COMP_V1 ||
4894 v1->lmm_magic == LOV_MAGIC_SEL) &&
4895 !(ah && ah->dah_append_stripes)) {
4896 comp_v1 = (struct lov_comp_md_v1 *)v1;
4897 comp_cnt = comp_v1->lcm_entry_count;
4900 mirror_cnt = comp_v1->lcm_mirror_count + 1;
4908 /* realloc default comp entries if necessary */
4909 rc = lod_def_striping_comp_resize(lds, comp_cnt);
4913 lds->lds_def_comp_cnt = comp_cnt;
4914 lds->lds_def_striping_is_composite = composite;
4915 lds->lds_def_mirror_cnt = mirror_cnt;
4917 for (i = 0; i < comp_cnt; i++) {
4918 struct lod_layout_component *lod_comp;
4921 lod_comp = &lds->lds_def_comp_entries[i];
4923 * reset lod_comp values, llc_stripes is always NULL in
4924 * the default striping template, llc_pool will be reset
4927 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
4930 v1 = (struct lov_user_md *)((char *)comp_v1 +
4931 comp_v1->lcm_entries[i].lcme_offset);
4932 lod_comp->llc_extent =
4933 comp_v1->lcm_entries[i].lcme_extent;
4934 /* We only inherit certain flags from the layout */
4935 lod_comp->llc_flags =
4936 comp_v1->lcm_entries[i].lcme_flags &
4937 LCME_TEMPLATE_FLAGS;
4940 if (!lov_pattern_supported(v1->lmm_pattern) &&
4941 !(v1->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
4942 lod_free_def_comp_entries(lds);
4946 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d stripe_offset=%d append_stripes=%d\n",
4947 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
4948 (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
4949 (int)v1->lmm_stripe_offset,
4950 ah ? ah->dah_append_stripes : 0);
4952 if (ah && ah->dah_append_stripes)
4953 lod_comp->llc_stripe_count = ah->dah_append_stripes;
4955 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
4956 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
4957 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
4958 lod_comp->llc_pattern = v1->lmm_pattern;
4961 if (ah && ah->dah_append_pool && ah->dah_append_pool[0]) {
4962 pool = ah->dah_append_pool;
4963 } else if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
4964 /* XXX: sanity check here */
4965 v3 = (struct lov_user_md_v3 *) v1;
4966 if (v3->lmm_pool_name[0] != '\0')
4967 pool = v3->lmm_pool_name;
4969 lod_set_def_pool(lds, i, pool);
4970 if (v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
4971 v3 = (struct lov_user_md_v3 *)v1;
4972 rc = lod_comp_copy_ost_lists(lod_comp, v3);
4975 } else if (lod_comp->llc_ostlist.op_array &&
4976 lod_comp->llc_ostlist.op_count) {
4977 for (j = 0; j < lod_comp->llc_ostlist.op_count; j++)
4978 lod_comp->llc_ostlist.op_array[j] = -1;
4979 lod_comp->llc_ostlist.op_count = 0;
4983 lds->lds_def_striping_set = 1;
4988 * Get default directory striping.
4990 * \param[in] env execution environment
4991 * \param[in] lo object
4992 * \param[out] lds default striping
4994 * \retval 0 on success
4995 * \retval negative if failed
4997 static int lod_get_default_lmv_striping(const struct lu_env *env,
4998 struct lod_object *lo,
4999 struct lod_default_striping *lds)
5001 struct lmv_user_md *lmu;
5004 lds->lds_dir_def_striping_set = 0;
5006 rc = lod_get_default_lmv_ea(env, lo);
5010 if (rc >= (int)sizeof(*lmu)) {
5011 struct lod_thread_info *info = lod_env_info(env);
5013 lmu = info->lti_ea_store;
5015 lds->lds_dir_def_stripe_count =
5016 le32_to_cpu(lmu->lum_stripe_count);
5017 lds->lds_dir_def_stripe_offset =
5018 le32_to_cpu(lmu->lum_stripe_offset);
5019 lds->lds_dir_def_hash_type =
5020 le32_to_cpu(lmu->lum_hash_type);
5021 lds->lds_dir_def_striping_set = 1;
5028 * Get default striping in the object.
5030 * Get object default striping and default directory striping.
5032 * \param[in] env execution environment
5033 * \param[in] lo object
5034 * \param[out] lds default striping
5036 * \retval 0 on success
5037 * \retval negative if failed
5039 static int lod_get_default_striping(const struct lu_env *env,
5040 struct lod_object *lo,
5041 struct lod_default_striping *lds)
5045 rc = lod_get_default_lov_striping(env, lo, lds, NULL);
5046 rc1 = lod_get_default_lmv_striping(env, lo, lds);
5047 if (rc == 0 && rc1 < 0)
5054 * Apply default striping on object.
5056 * If object striping pattern is not set, set to the one in default striping.
5057 * The default striping is from parent or fs.
5059 * \param[in] lo new object
5060 * \param[in] lds default striping
5061 * \param[in] mode new object's mode
5063 static void lod_striping_from_default(struct lod_object *lo,
5064 const struct lod_default_striping *lds,
5067 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5068 struct lov_desc *desc = &d->lod_desc;
5071 if (lds->lds_def_striping_set && S_ISREG(mode)) {
5072 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
5073 lds->lds_def_comp_cnt);
5077 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
5078 if (lds->lds_def_mirror_cnt > 1)
5079 lo->ldo_flr_state = LCM_FL_RDONLY;
5081 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5082 struct lod_layout_component *obj_comp =
5083 &lo->ldo_comp_entries[i];
5084 struct lod_layout_component *def_comp =
5085 &lds->lds_def_comp_entries[i];
5087 CDEBUG(D_LAYOUT, "Inherit from default: flags=%#x "
5088 "size=%hu nr=%u offset=%u pattern=%#x pool=%s\n",
5089 def_comp->llc_flags,
5090 def_comp->llc_stripe_size,
5091 def_comp->llc_stripe_count,
5092 def_comp->llc_stripe_offset,
5093 def_comp->llc_pattern,
5094 def_comp->llc_pool ?: "");
5096 *obj_comp = *def_comp;
5097 if (def_comp->llc_pool != NULL) {
5098 /* pointer was copied from def_comp */
5099 obj_comp->llc_pool = NULL;
5100 lod_obj_set_pool(lo, i, def_comp->llc_pool);
5104 if (def_comp->llc_ostlist.op_array &&
5105 def_comp->llc_ostlist.op_count) {
5106 OBD_ALLOC(obj_comp->llc_ostlist.op_array,
5107 obj_comp->llc_ostlist.op_size);
5108 if (!obj_comp->llc_ostlist.op_array)
5110 memcpy(obj_comp->llc_ostlist.op_array,
5111 def_comp->llc_ostlist.op_array,
5112 obj_comp->llc_ostlist.op_size);
5113 } else if (def_comp->llc_ostlist.op_array) {
5114 obj_comp->llc_ostlist.op_array = NULL;
5118 * Don't initialize these fields for plain layout
5119 * (v1/v3) here, they are inherited in the order of
5120 * 'parent' -> 'fs default (root)' -> 'global default
5121 * values for stripe_count & stripe_size'.
5123 * see lod_ah_init().
5125 if (!lo->ldo_is_composite)
5128 lod_adjust_stripe_info(obj_comp, desc, 0);
5130 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
5131 if (lo->ldo_dir_stripe_count == 0)
5132 lo->ldo_dir_stripe_count =
5133 lds->lds_dir_def_stripe_count;
5134 if (lo->ldo_dir_stripe_offset == -1)
5135 lo->ldo_dir_stripe_offset =
5136 lds->lds_dir_def_stripe_offset;
5137 if (lo->ldo_dir_hash_type == 0)
5138 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type &
5139 ~LMV_HASH_FLAG_SPACE;
5141 CDEBUG(D_LAYOUT, "striping from default dir: count:%hu, "
5142 "offset:%u, hash_type:%u\n",
5143 lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
5144 lo->ldo_dir_hash_type);
5148 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root,
5151 struct lod_layout_component *lod_comp;
5153 if (lo->ldo_comp_cnt == 0)
5156 if (lo->ldo_is_composite)
5159 lod_comp = &lo->ldo_comp_entries[0];
5161 if (lod_comp->llc_stripe_count <= 0 ||
5162 lod_comp->llc_stripe_size <= 0)
5165 if (from_root && (lod_comp->llc_pool == NULL ||
5166 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
5169 if (append_pool && append_pool[0])
5176 * Implementation of dt_object_operations::do_ah_init.
5178 * This method is used to make a decision on the striping configuration for the
5179 * object being created. It can be taken from the \a parent object if it exists,
5180 * or filesystem's default. The resulting configuration (number of stripes,
5181 * stripe size/offset, pool name, etc) is stored in the object itself and will
5182 * be used by the methods like ->doo_declare_create().
5184 * \see dt_object_operations::do_ah_init() in the API description for details.
5186 static void lod_ah_init(const struct lu_env *env,
5187 struct dt_allocation_hint *ah,
5188 struct dt_object *parent,
5189 struct dt_object *child,
5192 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
5193 struct lod_thread_info *info = lod_env_info(env);
5194 struct lod_default_striping *lds = lod_lds_buf_get(env);
5195 struct dt_object *nextp = NULL;
5196 struct dt_object *nextc;
5197 struct lod_object *lp = NULL;
5198 struct lod_object *lc;
5199 struct lov_desc *desc;
5200 struct lod_layout_component *lod_comp;
5206 if (ah->dah_append_stripes == -1)
5207 ah->dah_append_stripes = d->lod_desc.ld_tgt_count;
5209 if (likely(parent)) {
5210 nextp = dt_object_child(parent);
5211 lp = lod_dt_obj(parent);
5214 nextc = dt_object_child(child);
5215 lc = lod_dt_obj(child);
5217 LASSERT(!lod_obj_is_striped(child));
5218 /* default layout template may have been set on the regular file
5219 * when this is called from mdd_create_data() */
5220 if (S_ISREG(child_mode))
5221 lod_free_comp_entries(lc);
5223 if (!dt_object_exists(nextc))
5224 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
5226 if (S_ISDIR(child_mode)) {
5227 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
5229 /* other default values are 0 */
5230 lc->ldo_dir_stripe_offset = -1;
5232 /* no default striping configuration is needed for
5235 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5236 le32_to_cpu(lum1->lum_magic) == LMV_MAGIC_FOREIGN) {
5237 lc->ldo_dir_is_foreign = true;
5238 /* keep stripe_count 0 and stripe_offset -1 */
5239 CDEBUG(D_INFO, "no default striping for foreign dir\n");
5244 * If parent object is not root directory,
5245 * then get default striping from parent object.
5247 if (likely(lp != NULL)) {
5248 lod_get_default_striping(env, lp, lds);
5250 /* inherit default striping except ROOT */
5251 if ((lds->lds_def_striping_set ||
5252 lds->lds_dir_def_striping_set) &&
5253 !fid_is_root(lod_object_fid(lp)))
5254 lc->ldo_def_striping = lds;
5257 /* It should always honour the specified stripes */
5258 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
5259 * will have old magic. In this case, we should ignore the
5260 * stripe count and try to create dir by default stripe.
5262 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5263 (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
5264 le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
5265 lc->ldo_dir_stripe_count =
5266 le32_to_cpu(lum1->lum_stripe_count);
5267 lc->ldo_dir_stripe_offset =
5268 le32_to_cpu(lum1->lum_stripe_offset);
5269 lc->ldo_dir_hash_type =
5270 le32_to_cpu(lum1->lum_hash_type);
5272 "set dirstripe: count %hu, offset %d, hash %u\n",
5273 lc->ldo_dir_stripe_count,
5274 (int)lc->ldo_dir_stripe_offset,
5275 lc->ldo_dir_hash_type);
5277 /* transfer defaults LMV to new directory */
5278 lod_striping_from_default(lc, lds, child_mode);
5280 /* set count 0 to create normal directory */
5281 if (lc->ldo_dir_stripe_count == 1)
5282 lc->ldo_dir_stripe_count = 0;
5285 /* shrink the stripe_count to the avaible MDT count */
5286 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
5287 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)) {
5288 lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
5289 if (lc->ldo_dir_stripe_count == 1)
5290 lc->ldo_dir_stripe_count = 0;
5293 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
5294 lc->ldo_dir_stripe_count,
5295 (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
5300 /* child object regular file*/
5302 if (!lod_object_will_be_striped(S_ISREG(child_mode),
5303 lu_object_fid(&child->do_lu)))
5306 /* If object is going to be striped over OSTs, transfer default
5307 * striping information to the child, so that we can use it
5308 * during declaration and creation.
5310 * Try from the parent first.
5312 if (likely(lp != NULL)) {
5313 rc = lod_get_default_lov_striping(env, lp, lds, ah);
5315 lod_striping_from_default(lc, lds, child_mode);
5318 /* Initialize lod_device::lod_md_root object reference */
5319 if (d->lod_md_root == NULL) {
5320 struct dt_object *root;
5321 struct lod_object *lroot;
5323 lu_root_fid(&info->lti_fid);
5324 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
5325 if (!IS_ERR(root)) {
5326 lroot = lod_dt_obj(root);
5328 spin_lock(&d->lod_lock);
5329 if (d->lod_md_root != NULL)
5330 dt_object_put(env, &d->lod_md_root->ldo_obj);
5331 d->lod_md_root = lroot;
5332 spin_unlock(&d->lod_lock);
5336 /* try inherit layout from the root object (fs default) when:
5337 * - parent does not have default layout; or
5338 * - parent has plain(v1/v3) default layout, and some attributes
5339 * are not specified in the default layout;
5341 if (d->lod_md_root != NULL &&
5342 lod_need_inherit_more(lc, true, ah->dah_append_pool)) {
5343 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds,
5347 if (lc->ldo_comp_cnt == 0) {
5348 lod_striping_from_default(lc, lds, child_mode);
5349 } else if (!lds->lds_def_striping_is_composite) {
5350 struct lod_layout_component *def_comp;
5352 LASSERT(!lc->ldo_is_composite);
5353 lod_comp = &lc->ldo_comp_entries[0];
5354 def_comp = &lds->lds_def_comp_entries[0];
5356 if (lod_comp->llc_stripe_count <= 0)
5357 lod_comp->llc_stripe_count =
5358 def_comp->llc_stripe_count;
5359 if (lod_comp->llc_stripe_size <= 0)
5360 lod_comp->llc_stripe_size =
5361 def_comp->llc_stripe_size;
5362 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT &&
5363 (!lod_comp->llc_pool || !lod_comp->llc_pool[0]))
5364 lod_comp->llc_stripe_offset =
5365 def_comp->llc_stripe_offset;
5366 if (lod_comp->llc_pool == NULL)
5367 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
5372 * fs default striping may not be explicitly set, or historically set
5373 * in config log, use them.
5375 if (lod_need_inherit_more(lc, false, ah->dah_append_pool)) {
5376 if (lc->ldo_comp_cnt == 0) {
5377 rc = lod_alloc_comp_entries(lc, 0, 1);
5379 /* fail to allocate memory, will create a
5380 * non-striped file. */
5382 lc->ldo_is_composite = 0;
5383 lod_comp = &lc->ldo_comp_entries[0];
5384 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
5386 LASSERT(!lc->ldo_is_composite);
5387 lod_comp = &lc->ldo_comp_entries[0];
5388 desc = &d->lod_desc;
5389 lod_adjust_stripe_info(lod_comp, desc, ah->dah_append_stripes);
5390 if (ah->dah_append_pool && ah->dah_append_pool[0])
5391 lod_obj_set_pool(lc, 0, ah->dah_append_pool);
5397 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
5399 * Size initialization on late striping.
5401 * Propagate the size of a truncated object to a deferred striping.
5402 * This function handles a special case when truncate was done on a
5403 * non-striped object and now while the striping is being created
5404 * we can't lose that size, so we have to propagate it to the stripes
5407 * \param[in] env execution environment
5408 * \param[in] dt object
5409 * \param[in] th transaction handle
5411 * \retval 0 on success
5412 * \retval negative if failed
5414 static int lod_declare_init_size(const struct lu_env *env,
5415 struct dt_object *dt, struct thandle *th)
5417 struct dt_object *next = dt_object_child(dt);
5418 struct lod_object *lo = lod_dt_obj(dt);
5419 struct dt_object **objects = NULL;
5420 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
5421 uint64_t size, offs;
5422 int i, rc, stripe, stripe_count = 0, stripe_size = 0;
5423 struct lu_extent size_ext;
5426 if (!lod_obj_is_striped(dt))
5429 rc = dt_attr_get(env, next, attr);
5430 LASSERT(attr->la_valid & LA_SIZE);
5434 size = attr->la_size;
5438 size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
5439 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5440 struct lod_layout_component *lod_comp;
5441 struct lu_extent *extent;
5443 lod_comp = &lo->ldo_comp_entries[i];
5445 if (lod_comp->llc_stripe == NULL)
5448 extent = &lod_comp->llc_extent;
5449 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
5450 if (!lo->ldo_is_composite ||
5451 lu_extent_is_overlapped(extent, &size_ext)) {
5452 objects = lod_comp->llc_stripe;
5453 stripe_count = lod_comp->llc_stripe_count;
5454 stripe_size = lod_comp->llc_stripe_size;
5457 if (stripe_count == 0)
5460 LASSERT(objects != NULL && stripe_size != 0);
5461 /* ll_do_div64(a, b) returns a % b, and a = a / b */
5462 ll_do_div64(size, (__u64)stripe_size);
5463 stripe = ll_do_div64(size, (__u64)stripe_count);
5464 LASSERT(objects[stripe] != NULL);
5466 size = size * stripe_size;
5467 offs = attr->la_size;
5468 size += ll_do_div64(offs, stripe_size);
5470 attr->la_valid = LA_SIZE;
5471 attr->la_size = size;
5473 rc = lod_sub_declare_attr_set(env, objects[stripe],
5482 * Declare creation of striped object.
5484 * The function declares creation stripes for a regular object. The function
5485 * also declares whether the stripes will be created with non-zero size if
5486 * previously size was set non-zero on the master object. If object \a dt is
5487 * not local, then only fully defined striping can be applied in \a lovea.
5488 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
5491 * \param[in] env execution environment
5492 * \param[in] dt object
5493 * \param[in] attr attributes the stripes will be created with
5494 * \param[in] lovea a buffer containing striping description
5495 * \param[in] th transaction handle
5497 * \retval 0 on success
5498 * \retval negative if failed
5500 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
5501 struct lu_attr *attr,
5502 const struct lu_buf *lovea, struct thandle *th)
5504 struct lod_thread_info *info = lod_env_info(env);
5505 struct dt_object *next = dt_object_child(dt);
5506 struct lod_object *lo = lod_dt_obj(dt);
5510 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
5511 GOTO(out, rc = -ENOMEM);
5513 if (!dt_object_remote(next)) {
5514 /* choose OST and generate appropriate objects */
5515 rc = lod_prepare_create(env, lo, attr, lovea, th);
5520 * declare storage for striping data
5522 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5524 /* LOD can not choose OST objects for remote objects, i.e.
5525 * stripes must be ready before that. Right now, it can only
5526 * happen during migrate, i.e. migrate process needs to create
5527 * remote regular file (mdd_migrate_create), then the migrate
5528 * process will provide stripeEA. */
5529 LASSERT(lovea != NULL);
5530 info->lti_buf = *lovea;
5533 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
5534 XATTR_NAME_LOV, 0, th);
5539 * if striping is created with local object's size > 0,
5540 * we have to propagate this size to specific object
5541 * the case is possible only when local object was created previously
5543 if (dt_object_exists(next))
5544 rc = lod_declare_init_size(env, dt, th);
5547 /* failed to create striping or to set initial size, let's reset
5548 * config so that others don't get confused */
5550 lod_striping_free(env, lo);
5556 * Whether subdirectories under \a dt should be created on MDTs by space QoS
5558 * If LMV_HASH_FLAG_SPACE is set on directory default layout, its subdirectories
5559 * should be created on MDT by space QoS.
5561 * \param[in] env execution environment
5562 * \param[in] dev lu device
5563 * \param[in] dt object
5565 * \retval 1 if directory should create subdir by space usage
5567 * \retval -ev if failed
5569 static inline int dt_object_qos_mkdir(const struct lu_env *env,
5570 struct lu_device *dev,
5571 struct dt_object *dt)
5573 struct lod_thread_info *info = lod_env_info(env);
5574 struct lu_object *obj;
5575 struct lod_object *lo;
5576 struct lmv_user_md *lmu;
5579 obj = lu_object_find_slice(env, dev, lu_object_fid(&dt->do_lu), NULL);
5581 return PTR_ERR(obj);
5583 lo = lu2lod_obj(obj);
5585 rc = lod_get_default_lmv_ea(env, lo);
5586 dt_object_put(env, dt);
5590 if (rc < (int)sizeof(*lmu))
5593 lmu = info->lti_ea_store;
5594 return !!(le32_to_cpu(lmu->lum_hash_type) & LMV_HASH_FLAG_SPACE);
5598 * Implementation of dt_object_operations::do_declare_create.
5600 * The method declares creation of a new object. If the object will be striped,
5601 * then helper functions are called to find FIDs for the stripes, declare
5602 * creation of the stripes and declare initialization of the striping
5603 * information to be stored in the master object.
5605 * \see dt_object_operations::do_declare_create() in the API description
5608 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
5609 struct lu_attr *attr,
5610 struct dt_allocation_hint *hint,
5611 struct dt_object_format *dof, struct thandle *th)
5613 struct dt_object *next = dt_object_child(dt);
5614 struct lod_object *lo = lod_dt_obj(dt);
5623 * first of all, we declare creation of local object
5625 rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
5630 * it's lod_ah_init() that has decided the object will be striped
5632 if (dof->dof_type == DFT_REGULAR) {
5633 /* callers don't want stripes */
5634 /* XXX: all tricky interactions with ->ah_make_hint() decided
5635 * to use striping, then ->declare_create() behaving differently
5636 * should be cleaned */
5637 if (dof->u.dof_reg.striped != 0)
5638 rc = lod_declare_striped_create(env, dt, attr,
5640 } else if (dof->dof_type == DFT_DIR) {
5641 struct seq_server_site *ss;
5642 struct lu_buf buf = { NULL };
5643 struct lu_buf *lmu = NULL;
5645 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
5647 /* If the parent has default stripeEA, and client
5648 * did not find it before sending create request,
5649 * then MDT will return -EREMOTE, and client will
5650 * retrieve the default stripeEA and re-create the
5653 * Note: if dah_eadata != NULL, it means creating the
5654 * striped directory with specified stripeEA, then it
5655 * should ignore the default stripeEA */
5656 if (hint != NULL && hint->dah_eadata == NULL) {
5657 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
5658 GOTO(out, rc = -EREMOTE);
5660 if (lo->ldo_dir_stripe_offset == -1) {
5662 * child and parent should be in the same MDT,
5663 * but if parent has plain layout, it's allowed.
5665 if (hint->dah_parent &&
5666 dt_object_remote(hint->dah_parent)) {
5667 rc = dt_object_qos_mkdir(env,
5668 lo->ldo_obj.do_lu.lo_dev,
5671 GOTO(out, rc ? rc : -EREMOTE);
5673 } else if (lo->ldo_dir_stripe_offset !=
5675 struct lod_device *lod;
5676 struct lod_tgt_descs *ltd;
5677 struct lod_tgt_desc *tgt = NULL;
5678 bool found_mdt = false;
5680 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5681 ltd = &lod->lod_mdt_descs;
5682 ltd_foreach_tgt(ltd, tgt) {
5683 if (tgt->ltd_index ==
5684 lo->ldo_dir_stripe_offset) {
5690 /* If the MDT indicated by stripe_offset can be
5691 * found, then tell client to resend the create
5692 * request to the correct MDT, otherwise return
5693 * error to client */
5695 GOTO(out, rc = -EREMOTE);
5697 GOTO(out, rc = -EINVAL);
5699 } else if (hint && hint->dah_eadata) {
5701 lmu->lb_buf = (void *)hint->dah_eadata;
5702 lmu->lb_len = hint->dah_eadata_len;
5705 rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
5709 /* failed to create striping or to set initial size, let's reset
5710 * config so that others don't get confused */
5712 lod_striping_free(env, lo);
5717 * Generate component ID for new created component.
5719 * \param[in] lo LOD object
5720 * \param[in] comp_idx index of ldo_comp_entries
5722 * \retval component ID on success
5723 * \retval LCME_ID_INVAL on failure
5725 static __u32 lod_gen_component_id(struct lod_object *lo,
5726 int mirror_id, int comp_idx)
5728 struct lod_layout_component *lod_comp;
5729 __u32 id, start, end;
5732 LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
5734 lod_obj_inc_layout_gen(lo);
5735 id = lo->ldo_layout_gen;
5736 if (likely(id <= SEQ_ID_MAX))
5737 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
5739 /* Layout generation wraps, need to check collisions. */
5740 start = id & SEQ_ID_MASK;
5743 for (id = start; id <= end; id++) {
5744 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5745 lod_comp = &lo->ldo_comp_entries[i];
5746 if (pflr_id(mirror_id, id) == lod_comp->llc_id)
5749 /* Found the ununsed ID */
5750 if (i == lo->ldo_comp_cnt)
5751 RETURN(pflr_id(mirror_id, id));
5753 if (end == LCME_ID_MAX) {
5755 end = min(lo->ldo_layout_gen & LCME_ID_MASK,
5756 (__u32)(LCME_ID_MAX - 1));
5760 RETURN(LCME_ID_INVAL);
5764 * Creation of a striped regular object.
5766 * The function is called to create the stripe objects for a regular
5767 * striped file. This can happen at the initial object creation or
5768 * when the caller asks LOD to do so using ->do_xattr_set() method
5769 * (so called late striping). Notice all the information are already
5770 * prepared in the form of the list of objects (ldo_stripe field).
5771 * This is done during declare phase.
5773 * \param[in] env execution environment
5774 * \param[in] dt object
5775 * \param[in] attr attributes the stripes will be created with
5776 * \param[in] dof format of stripes (see OSD API description)
5777 * \param[in] th transaction handle
5779 * \retval 0 on success
5780 * \retval negative if failed
5782 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
5783 struct lu_attr *attr, struct dt_object_format *dof,
5786 struct lod_layout_component *lod_comp;
5787 struct lod_object *lo = lod_dt_obj(dt);
5792 LASSERT((lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL) ||
5793 lo->ldo_is_foreign);
5795 mirror_id = 0; /* non-flr file's mirror_id is 0 */
5796 if (lo->ldo_mirror_count > 1) {
5797 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5798 lod_comp = &lo->ldo_comp_entries[i];
5799 if (lod_comp->llc_id != LCME_ID_INVAL &&
5800 mirror_id_of(lod_comp->llc_id) > mirror_id)
5801 mirror_id = mirror_id_of(lod_comp->llc_id);
5805 /* create all underlying objects */
5806 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5807 lod_comp = &lo->ldo_comp_entries[i];
5809 if (lod_comp->llc_id == LCME_ID_INVAL) {
5810 /* only the component of FLR layout with more than 1
5811 * mirror has mirror ID in its component ID.
5813 if (lod_comp->llc_extent.e_start == 0 &&
5814 lo->ldo_mirror_count > 1)
5817 lod_comp->llc_id = lod_gen_component_id(lo,
5819 if (lod_comp->llc_id == LCME_ID_INVAL)
5820 GOTO(out, rc = -ERANGE);
5823 if (lod_comp_inited(lod_comp))
5826 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
5827 lod_comp_set_init(lod_comp);
5829 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
5830 lod_comp_set_init(lod_comp);
5832 if (lod_comp->llc_stripe == NULL)
5835 LASSERT(lod_comp->llc_stripe_count);
5836 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
5837 struct dt_object *object = lod_comp->llc_stripe[j];
5838 LASSERT(object != NULL);
5839 rc = lod_sub_create(env, object, attr, NULL, dof, th);
5843 lod_comp_set_init(lod_comp);
5846 rc = lod_fill_mirrors(lo);
5850 rc = lod_generate_and_set_lovea(env, lo, th);
5854 lo->ldo_comp_cached = 1;
5858 lod_striping_free(env, lo);
5862 static inline bool lod_obj_is_dom(struct dt_object *dt)
5864 struct lod_object *lo = lod_dt_obj(dt);
5866 if (!dt_object_exists(dt_object_child(dt)))
5869 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
5872 if (!lo->ldo_comp_cnt)
5875 return (lov_pattern(lo->ldo_comp_entries[0].llc_pattern) ==
5880 * Implementation of dt_object_operations::do_create.
5882 * If any of preceeding methods (like ->do_declare_create(),
5883 * ->do_ah_init(), etc) chose to create a striped object,
5884 * then this method will create the master and the stripes.
5886 * \see dt_object_operations::do_create() in the API description for details.
5888 static int lod_create(const struct lu_env *env, struct dt_object *dt,
5889 struct lu_attr *attr, struct dt_allocation_hint *hint,
5890 struct dt_object_format *dof, struct thandle *th)
5895 /* create local object */
5896 rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
5900 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
5901 (lod_obj_is_striped(dt) || lod_obj_is_dom(dt)) &&
5902 dof->u.dof_reg.striped != 0) {
5903 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
5904 rc = lod_striped_create(env, dt, attr, dof, th);
5911 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
5912 struct dt_object *dt, struct thandle *th,
5913 int comp_idx, int stripe_idx,
5914 struct lod_obj_stripe_cb_data *data)
5916 if (data->locd_declare)
5917 return lod_sub_declare_destroy(env, dt, th);
5918 else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
5919 stripe_idx == cfs_fail_val)
5920 return lod_sub_destroy(env, dt, th);
5926 * Implementation of dt_object_operations::do_declare_destroy.
5928 * If the object is a striped directory, then the function declares reference
5929 * removal from the master object (this is an index) to the stripes and declares
5930 * destroy of all the stripes. In all the cases, it declares an intention to
5931 * destroy the object itself.
5933 * \see dt_object_operations::do_declare_destroy() in the API description
5936 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
5939 struct dt_object *next = dt_object_child(dt);
5940 struct lod_object *lo = lod_dt_obj(dt);
5941 struct lod_thread_info *info = lod_env_info(env);
5942 struct dt_object *stripe;
5943 char *stripe_name = info->lti_key;
5949 * load striping information, notice we don't do this when object
5950 * is being initialized as we don't need this information till
5951 * few specific cases like destroy, chown
5953 rc = lod_striping_load(env, lo);
5957 /* declare destroy for all underlying objects */
5958 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5959 rc = next->do_ops->do_index_try(env, next,
5960 &dt_directory_features);
5964 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5965 stripe = lo->ldo_stripe[i];
5969 rc = lod_sub_declare_ref_del(env, next, th);
5973 snprintf(stripe_name, sizeof(info->lti_key),
5975 PFID(lu_object_fid(&stripe->do_lu)), i);
5976 rc = lod_sub_declare_delete(env, next,
5977 (const struct dt_key *)stripe_name, th);
5984 * we declare destroy for the local object
5986 rc = lod_sub_declare_destroy(env, next, th);
5990 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
5991 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
5994 if (!lod_obj_is_striped(dt))
5997 /* declare destroy all striped objects */
5998 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5999 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6000 stripe = lo->ldo_stripe[i];
6004 if (!dt_object_exists(stripe))
6007 rc = lod_sub_declare_ref_del(env, stripe, th);
6011 rc = lod_sub_declare_destroy(env, stripe, th);
6016 struct lod_obj_stripe_cb_data data = { { 0 } };
6018 data.locd_declare = true;
6019 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6020 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6027 * Implementation of dt_object_operations::do_destroy.
6029 * If the object is a striped directory, then the function removes references
6030 * from the master object (this is an index) to the stripes and destroys all
6031 * the stripes. In all the cases, the function destroys the object itself.
6033 * \see dt_object_operations::do_destroy() in the API description for details.
6035 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
6038 struct dt_object *next = dt_object_child(dt);
6039 struct lod_object *lo = lod_dt_obj(dt);
6040 struct lod_thread_info *info = lod_env_info(env);
6041 char *stripe_name = info->lti_key;
6042 struct dt_object *stripe;
6048 /* destroy sub-stripe of master object */
6049 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6050 rc = next->do_ops->do_index_try(env, next,
6051 &dt_directory_features);
6055 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6056 stripe = lo->ldo_stripe[i];
6060 rc = lod_sub_ref_del(env, next, th);
6064 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
6065 PFID(lu_object_fid(&stripe->do_lu)), i);
6067 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
6068 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
6069 PFID(lu_object_fid(&stripe->do_lu)));
6071 rc = lod_sub_delete(env, next,
6072 (const struct dt_key *)stripe_name, th);
6078 rc = lod_sub_destroy(env, next, th);
6082 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
6083 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
6086 if (!lod_obj_is_striped(dt))
6089 /* destroy all striped objects */
6090 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6091 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6092 stripe = lo->ldo_stripe[i];
6096 if (!dt_object_exists(stripe))
6099 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
6100 i == cfs_fail_val) {
6101 dt_write_lock(env, stripe, DT_TGT_CHILD);
6102 rc = lod_sub_ref_del(env, stripe, th);
6103 dt_write_unlock(env, stripe);
6107 rc = lod_sub_destroy(env, stripe, th);
6113 struct lod_obj_stripe_cb_data data = { { 0 } };
6115 data.locd_declare = false;
6116 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6117 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6124 * Implementation of dt_object_operations::do_declare_ref_add.
6126 * \see dt_object_operations::do_declare_ref_add() in the API description
6129 static int lod_declare_ref_add(const struct lu_env *env,
6130 struct dt_object *dt, struct thandle *th)
6132 return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
6136 * Implementation of dt_object_operations::do_ref_add.
6138 * \see dt_object_operations::do_ref_add() in the API description for details.
6140 static int lod_ref_add(const struct lu_env *env,
6141 struct dt_object *dt, struct thandle *th)
6143 return lod_sub_ref_add(env, dt_object_child(dt), th);
6147 * Implementation of dt_object_operations::do_declare_ref_del.
6149 * \see dt_object_operations::do_declare_ref_del() in the API description
6152 static int lod_declare_ref_del(const struct lu_env *env,
6153 struct dt_object *dt, struct thandle *th)
6155 return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
6159 * Implementation of dt_object_operations::do_ref_del
6161 * \see dt_object_operations::do_ref_del() in the API description for details.
6163 static int lod_ref_del(const struct lu_env *env,
6164 struct dt_object *dt, struct thandle *th)
6166 return lod_sub_ref_del(env, dt_object_child(dt), th);
6170 * Implementation of dt_object_operations::do_object_sync.
6172 * \see dt_object_operations::do_object_sync() in the API description
6175 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
6176 __u64 start, __u64 end)
6178 return dt_object_sync(env, dt_object_child(dt), start, end);
6182 * Implementation of dt_object_operations::do_object_unlock.
6184 * Used to release LDLM lock(s).
6186 * \see dt_object_operations::do_object_unlock() in the API description
6189 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
6190 struct ldlm_enqueue_info *einfo,
6191 union ldlm_policy_data *policy)
6193 struct lod_object *lo = lod_dt_obj(dt);
6194 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
6195 int slave_locks_size;
6199 if (slave_locks == NULL)
6202 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
6203 /* Note: for remote lock for single stripe dir, MDT will cancel
6204 * the lock by lockh directly */
6205 LASSERT(!dt_object_remote(dt_object_child(dt)));
6207 /* locks were unlocked in MDT layer */
6208 for (i = 0; i < slave_locks->ha_count; i++)
6209 LASSERT(!lustre_handle_is_used(&slave_locks->ha_handles[i]));
6212 * NB, ha_count may not equal to ldo_dir_stripe_count, because dir
6213 * layout may change, e.g., shrink dir layout after migration.
6215 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6216 if (lo->ldo_stripe[i])
6217 dt_invalidate(env, lo->ldo_stripe[i]);
6220 slave_locks_size = offsetof(typeof(*slave_locks),
6221 ha_handles[slave_locks->ha_count]);
6222 OBD_FREE(slave_locks, slave_locks_size);
6223 einfo->ei_cbdata = NULL;
6229 * Implementation of dt_object_operations::do_object_lock.
6231 * Used to get LDLM lock on the non-striped and striped objects.
6233 * \see dt_object_operations::do_object_lock() in the API description
6236 static int lod_object_lock(const struct lu_env *env,
6237 struct dt_object *dt,
6238 struct lustre_handle *lh,
6239 struct ldlm_enqueue_info *einfo,
6240 union ldlm_policy_data *policy)
6242 struct lod_object *lo = lod_dt_obj(dt);
6243 int slave_locks_size;
6244 struct lustre_handle_array *slave_locks = NULL;
6249 /* remote object lock */
6250 if (!einfo->ei_enq_slave) {
6251 LASSERT(dt_object_remote(dt));
6252 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
6256 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
6259 rc = lod_striping_load(env, lo);
6264 if (lo->ldo_dir_stripe_count <= 1)
6267 slave_locks_size = offsetof(typeof(*slave_locks),
6268 ha_handles[lo->ldo_dir_stripe_count]);
6269 /* Freed in lod_object_unlock */
6270 OBD_ALLOC(slave_locks, slave_locks_size);
6273 slave_locks->ha_count = lo->ldo_dir_stripe_count;
6275 /* striped directory lock */
6276 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6277 struct lustre_handle lockh;
6278 struct ldlm_res_id *res_id;
6279 struct dt_object *stripe;
6281 stripe = lo->ldo_stripe[i];
6285 res_id = &lod_env_info(env)->lti_res_id;
6286 fid_build_reg_res_name(lu_object_fid(&stripe->do_lu), res_id);
6287 einfo->ei_res_id = res_id;
6289 if (dt_object_remote(stripe)) {
6290 set_bit(i, (void *)slave_locks->ha_map);
6291 rc = dt_object_lock(env, stripe, &lockh, einfo, policy);
6293 struct ldlm_namespace *ns = einfo->ei_namespace;
6294 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
6295 ldlm_completion_callback completion = einfo->ei_cb_cp;
6296 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
6298 if (einfo->ei_mode == LCK_PW ||
6299 einfo->ei_mode == LCK_EX)
6300 dlmflags |= LDLM_FL_COS_INCOMPAT;
6302 LASSERT(ns != NULL);
6303 rc = ldlm_cli_enqueue_local(env, ns, res_id, LDLM_IBITS,
6304 policy, einfo->ei_mode,
6305 &dlmflags, blocking,
6307 NULL, 0, LVB_T_NONE,
6312 ldlm_lock_decref_and_cancel(
6313 &slave_locks->ha_handles[i],
6315 OBD_FREE(slave_locks, slave_locks_size);
6318 slave_locks->ha_handles[i] = lockh;
6320 einfo->ei_cbdata = slave_locks;
6326 * Implementation of dt_object_operations::do_invalidate.
6328 * \see dt_object_operations::do_invalidate() in the API description for details
6330 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
6332 return dt_invalidate(env, dt_object_child(dt));
6335 static int lod_declare_instantiate_components(const struct lu_env *env,
6336 struct lod_object *lo, struct thandle *th)
6338 struct lod_thread_info *info = lod_env_info(env);
6343 LASSERT(info->lti_count < lo->ldo_comp_cnt);
6345 for (i = 0; i < info->lti_count; i++) {
6346 rc = lod_qos_prep_create(env, lo, NULL, th,
6347 info->lti_comp_idx[i]);
6353 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6354 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
6355 &info->lti_buf, XATTR_NAME_LOV, 0, th);
6362 * Check OSTs for an existing component for further extension
6364 * Checks if OSTs are still healthy and not out of space. Gets free space
6365 * on OSTs (relative to allocation watermark rmb_low) and compares to
6366 * the proposed new_end for this component.
6368 * Decides whether or not to extend a component on its current OSTs.
6370 * \param[in] env execution environment for this thread
6371 * \param[in] lo object we're checking
6372 * \param[in] index index of this component
6373 * \param[in] extension_size extension size for this component
6374 * \param[in] extent layout extent for requested operation
6375 * \param[in] comp_extent extension component extent
6376 * \param[in] write if this is write operation
6378 * \retval true - OK to extend on current OSTs
6379 * \retval false - do not extend on current OSTs
6381 static bool lod_sel_osts_allowed(const struct lu_env *env,
6382 struct lod_object *lo,
6383 int index, __u64 extension_size,
6384 struct lu_extent *extent,
6385 struct lu_extent *comp_extent, int write)
6387 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[index];
6388 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6389 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
6390 __u64 available = 0;
6397 LASSERT(lod_comp->llc_stripe_count != 0);
6400 (extent->e_start == 0 && extent->e_end == OBD_OBJECT_EOF)) {
6401 /* truncate or append */
6402 size = extension_size;
6404 /* In case of write op, check the real write extent,
6405 * it may be larger than the extension_size */
6406 size = roundup(min(extent->e_end, comp_extent->e_end) -
6407 max(extent->e_start, comp_extent->e_start),
6410 /* extension_size is file level, so we must divide by stripe count to
6411 * compare it to available space on a single OST */
6412 size /= lod_comp->llc_stripe_count;
6414 lod_getref(&lod->lod_ost_descs);
6415 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
6416 int index = lod_comp->llc_ost_indices[i];
6417 struct lod_tgt_desc *ost = OST_TGT(lod, index);
6418 struct obd_statfs_info info = { 0 };
6419 int j, repeated = 0;
6423 /* Get the number of times this OST repeats in this component.
6424 * Note: inter-component repeats are not counted as this is
6425 * considered as a rare case: we try to not repeat OST in other
6426 * components if possible. */
6427 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6428 if (index != lod_comp->llc_ost_indices[j])
6431 /* already handled */
6437 if (j < lod_comp->llc_stripe_count)
6440 if (!cfs_bitmap_check(lod->lod_ost_bitmap, index)) {
6441 CDEBUG(D_LAYOUT, "ost %d no longer present\n", index);
6446 rc = dt_statfs_info(env, ost->ltd_ost, sfs, &info);
6448 CDEBUG(D_LAYOUT, "statfs failed for ost %d, error %d\n",
6454 if (sfs->os_state & OS_STATE_ENOSPC ||
6455 sfs->os_state & OS_STATE_READONLY ||
6456 sfs->os_state & OS_STATE_DEGRADED) {
6457 CDEBUG(D_LAYOUT, "ost %d is not availble for SEL "
6458 "extension, state %u\n", index, sfs->os_state);
6464 available = sfs->os_bavail * sfs->os_bsize;
6465 /* 'available' is relative to the allocation threshold */
6466 available -= (__u64) info.os_reserved_mb_low << 20;
6468 CDEBUG(D_LAYOUT, "ost %d lowwm: %d highwm: %d, "
6469 "%llu %% blocks available, %llu %% blocks free\n",
6470 index, info.os_reserved_mb_low, info.os_reserved_mb_high,
6471 (100ull * sfs->os_bavail) / sfs->os_blocks,
6472 (100ull * sfs->os_bfree) / sfs->os_blocks);
6474 if (size * repeated > available) {
6476 CDEBUG(D_LAYOUT, "low space on ost %d, available %llu "
6477 "< extension size %llu\n", index, available,
6482 lod_putref(lod, &lod->lod_ost_descs);
6488 * Adjust extents after component removal
6490 * When we remove an extension component, we move the start of the next
6491 * component to match the start of the extension component, so no space is left
6494 * \param[in] env execution environment for this thread
6495 * \param[in] lo object
6496 * \param[in] max_comp layout component
6497 * \param[in] index index of this component
6499 * \retval 0 on success
6500 * \retval negative errno on error
6502 static void lod_sel_adjust_extents(const struct lu_env *env,
6503 struct lod_object *lo,
6504 int max_comp, int index)
6506 struct lod_layout_component *lod_comp = NULL;
6507 struct lod_layout_component *next = NULL;
6508 struct lod_layout_component *prev = NULL;
6509 __u64 new_start = 0;
6513 /* Extension space component */
6514 lod_comp = &lo->ldo_comp_entries[index];
6515 next = &lo->ldo_comp_entries[index + 1];
6516 prev = &lo->ldo_comp_entries[index - 1];
6518 LASSERT(lod_comp != NULL && prev != NULL && next != NULL);
6519 LASSERT(lod_comp->llc_flags & LCME_FL_EXTENSION);
6521 /* Previous is being removed */
6522 if (prev && prev->llc_id == LCME_ID_INVAL)
6523 new_start = prev->llc_extent.e_start;
6525 new_start = lod_comp->llc_extent.e_start;
6527 for (i = index + 1; i < max_comp; i++) {
6528 lod_comp = &lo->ldo_comp_entries[i];
6530 start = lod_comp->llc_extent.e_start;
6531 lod_comp->llc_extent.e_start = new_start;
6533 /* We only move zero length extendable components */
6534 if (!(start == lod_comp->llc_extent.e_end))
6537 LASSERT(!(lod_comp->llc_flags & LCME_FL_INIT));
6539 lod_comp->llc_extent.e_end = new_start;
6543 /* Calculate the proposed 'new end' for a component we're extending */
6544 static __u64 lod_extension_new_end(__u64 extension_size, __u64 extent_end,
6545 __u32 stripe_size, __u64 component_end,
6546 __u64 extension_end)
6550 LASSERT(extension_size != 0 && stripe_size != 0);
6552 /* Round up to extension size */
6553 if (extent_end == OBD_OBJECT_EOF) {
6554 new_end = OBD_OBJECT_EOF;
6556 /* Add at least extension_size to the previous component_end,
6557 * covering the req layout extent */
6558 new_end = max(extent_end - component_end, extension_size);
6559 new_end = roundup(new_end, extension_size);
6560 new_end += component_end;
6562 /* Component end must be min stripe size aligned */
6563 if (new_end % stripe_size) {
6564 CDEBUG(D_LAYOUT, "new component end is not aligned "
6565 "by the stripe size %u: [%llu, %llu) ext size "
6566 "%llu new end %llu, aligning\n",
6567 stripe_size, component_end, extent_end,
6568 extension_size, new_end);
6569 new_end = roundup(new_end, stripe_size);
6573 if (new_end < extent_end)
6574 new_end = OBD_OBJECT_EOF;
6577 /* Don't extend past the end of the extension component */
6578 if (new_end > extension_end)
6579 new_end = extension_end;
6584 /* As lod_sel_handler() could be re-entered for the same component several
6585 * times, this is the data for the next call. Fields could be changed to
6586 * component indexes when needed, (e.g. if there is no need to instantiate
6587 * all the previous components up to the current position) to tell the caller
6588 * where to start over from. */
6595 * Process extent updates for a particular layout component
6597 * Handle layout updates for a particular extension space component touched by
6598 * a layout update operation. Core function of self-extending PFL feature.
6600 * In general, this function processes exactly *one* stage of an extension
6601 * operation, modifying the layout accordingly, then returns to the caller.
6602 * The caller is responsible for restarting processing with the new layout,
6603 * which may repeatedly return to this function until the extension updates
6606 * This function does one of a few things to the layout:
6607 * 1. Extends the component before the current extension space component to
6608 * allow it to accomodate the requested operation (if space/policy permit that
6609 * component to continue on its current OSTs)
6611 * 2. If extension of the existing component fails, we do one of two things:
6612 * a. If there is a component after the extension space, we remove the
6613 * extension space component, move the start of the next component down
6614 * accordingly, then notify the caller to restart processing w/the new
6616 * b. If there is no following component, we try repeating the current
6617 * component, creating a new component using the current one as a
6618 * template (keeping its stripe properties but not specific striping),
6619 * and try assigning striping for this component. If there is sufficient
6620 * free space on the OSTs chosen for this component, it is instantiated
6621 * and i/o continues there.
6623 * If there is not sufficient space on the new OSTs, we remove this new
6624 * component & extend the current component.
6626 * Note further that uninited components followed by extension space can be zero
6627 * length meaning that we will try to extend them before initializing them, and
6628 * if that fails, they will be removed without initialization.
6630 * 3. If we extend to/beyond the end of an extension space component, that
6631 * component is exhausted (all of its range has been given to real components),
6632 * so we remove it and restart processing.
6634 * \param[in] env execution environment for this thread
6635 * \param[in,out] lo object to update the layout of
6636 * \param[in] extent layout extent for requested operation, update
6637 * layout to fit this operation
6638 * \param[in] th transaction handle for this operation
6639 * \param[in,out] max_comp the highest comp for the portion of the layout
6640 * we are operating on (For FLR, the chosen
6641 * replica). Updated because we may remove
6643 * \param[in] index index of the extension space component we're
6645 * \param[in] write if this is write op
6646 * \param[in,out] force if the extension is to be forced; set here
6647 to force it on the 2nd call for the same
6650 * \retval 0 on success
6651 * \retval negative errno on error
6653 static int lod_sel_handler(const struct lu_env *env,
6654 struct lod_object *lo,
6655 struct lu_extent *extent,
6656 struct thandle *th, int *max_comp,
6657 int index, int write,
6658 struct sel_data *sd)
6660 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6661 struct lod_thread_info *info = lod_env_info(env);
6662 struct lod_layout_component *lod_comp;
6663 struct lod_layout_component *prev;
6664 struct lod_layout_component *next = NULL;
6665 __u64 extension_size;
6672 /* First component cannot be extension space */
6674 CERROR("%s: "DFID" first component cannot be extension space\n",
6675 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6679 lod_comp = &lo->ldo_comp_entries[index];
6680 prev = &lo->ldo_comp_entries[index - 1];
6681 if ((index + 1) < *max_comp)
6682 next = &lo->ldo_comp_entries[index + 1];
6684 /* extension size uses the stripe size field as KiB */
6685 extension_size = lod_comp->llc_stripe_size * SEL_UNIT_SIZE;
6687 CDEBUG(D_LAYOUT, "prev start %llu, extension start %llu, extension end"
6688 " %llu, extension size %llu\n", prev->llc_extent.e_start,
6689 lod_comp->llc_extent.e_start, lod_comp->llc_extent.e_end,
6692 /* Two extension space components cannot be adjacent & extension space
6693 * components cannot be init */
6694 if ((prev->llc_flags & LCME_FL_EXTENSION) ||
6695 !(ergo(next, !(next->llc_flags & LCME_FL_EXTENSION))) ||
6696 lod_comp_inited(lod_comp)) {
6697 CERROR("%s: "DFID" invalid extension space components\n",
6698 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6702 if (!prev->llc_stripe) {
6703 CDEBUG(D_LAYOUT, "Previous component not inited\n");
6704 info->lti_count = 1;
6705 info->lti_comp_idx[0] = index - 1;
6706 rc = lod_declare_instantiate_components(env, lo, th);
6707 /* ENOSPC tells us we can't use this component. If there is
6708 * a next or we are repeating, we either spill over (next) or
6709 * extend the original comp (repeat). Otherwise, return the
6710 * error to the user. */
6711 if (rc == -ENOSPC && (next || sd->sd_repeat))
6717 if (sd->sd_force == 0 && rc == 0)
6718 rc = !lod_sel_osts_allowed(env, lo, index - 1,
6719 extension_size, extent,
6720 &lod_comp->llc_extent, write);
6722 repeated = !!(sd->sd_repeat);
6726 /* Extend previous component */
6728 new_end = lod_extension_new_end(extension_size, extent->e_end,
6729 prev->llc_stripe_size,
6730 prev->llc_extent.e_end,
6731 lod_comp->llc_extent.e_end);
6733 CDEBUG(D_LAYOUT, "new end %llu\n", new_end);
6734 lod_comp->llc_extent.e_start = new_end;
6735 prev->llc_extent.e_end = new_end;
6737 if (prev->llc_extent.e_end == lod_comp->llc_extent.e_end) {
6738 CDEBUG(D_LAYOUT, "Extension component exhausted\n");
6739 lod_comp->llc_id = LCME_ID_INVAL;
6743 /* rc == 1, failed to extend current component */
6746 /* Normal 'spillover' case - Remove the extension
6747 * space component & bring down the start of the next
6749 lod_comp->llc_id = LCME_ID_INVAL;
6751 if (!(prev->llc_flags & LCME_FL_INIT)) {
6752 prev->llc_id = LCME_ID_INVAL;
6755 lod_sel_adjust_extents(env, lo, *max_comp, index);
6756 } else if (lod_comp_inited(prev)) {
6757 /* If there is no next, and the previous component is
6758 * INIT'ed, try repeating the previous component. */
6759 LASSERT(repeated == 0);
6760 rc = lod_layout_repeat_comp(env, lo, index - 1);
6764 /* The previous component is a repeated component.
6765 * Record this so we don't keep trying to repeat it. */
6768 /* If the previous component is not INIT'ed, this may
6769 * be a component we have just instantiated but failed
6770 * to extend. Or even a repeated component we failed
6771 * to prepare a striping for. Do not repeat but instead
6772 * remove the repeated component & force the extention
6773 * of the original one */
6776 prev->llc_id = LCME_ID_INVAL;
6783 rc = lod_layout_del_prep_layout(env, lo, NULL);
6786 LASSERTF(-rc == change,
6787 "number deleted %d != requested %d\n", -rc,
6790 *max_comp = *max_comp + change;
6792 /* lod_del_prep_layout reallocates ldo_comp_entries, so we must
6793 * refresh these pointers before using them */
6794 lod_comp = &lo->ldo_comp_entries[index];
6795 prev = &lo->ldo_comp_entries[index - 1];
6796 CDEBUG(D_LAYOUT, "After extent updates: prev start %llu, current start "
6797 "%llu, current end %llu max_comp %d ldo_comp_cnt %d\n",
6798 prev->llc_extent.e_start, lod_comp->llc_extent.e_start,
6799 lod_comp->llc_extent.e_end, *max_comp, lo->ldo_comp_cnt);
6801 /* Layout changed successfully */
6806 * Declare layout extent updates
6808 * Handles extensions. Identifies extension components touched by current
6809 * operation and passes them to processing function.
6811 * Restarts with updated layouts from the processing function until the current
6812 * operation no longer touches an extension space component.
6814 * \param[in] env execution environment for this thread
6815 * \param[in,out] lo object to update the layout of
6816 * \param[in] extent layout extent for requested operation, update layout to
6817 * fit this operation
6818 * \param[in] th transaction handle for this operation
6819 * \param[in] pick identifies chosen mirror for FLR layouts
6820 * \param[in] write if this is write op
6822 * \retval 1 on layout changed, 0 on no change
6823 * \retval negative errno on error
6825 static int lod_declare_update_extents(const struct lu_env *env,
6826 struct lod_object *lo, struct lu_extent *extent,
6827 struct thandle *th, int pick, int write)
6829 struct lod_thread_info *info = lod_env_info(env);
6830 struct lod_layout_component *lod_comp;
6831 bool layout_changed = false;
6832 struct sel_data sd = { 0 };
6840 /* This makes us work on the components of the chosen mirror */
6841 start_index = lo->ldo_mirrors[pick].lme_start;
6842 max_comp = lo->ldo_mirrors[pick].lme_end + 1;
6843 if (lo->ldo_flr_state == LCM_FL_NONE)
6844 LASSERT(start_index == 0 && max_comp == lo->ldo_comp_cnt);
6846 CDEBUG(D_LAYOUT, "extent->e_start %llu, extent->e_end %llu\n",
6847 extent->e_start, extent->e_end);
6848 for (i = start_index; i < max_comp; i++) {
6849 lod_comp = &lo->ldo_comp_entries[i];
6851 /* We've passed all components of interest */
6852 if (lod_comp->llc_extent.e_start >= extent->e_end)
6855 if (lod_comp->llc_flags & LCME_FL_EXTENSION) {
6856 layout_changed = true;
6857 rc = lod_sel_handler(env, lo, extent, th, &max_comp,
6862 /* Nothing has changed behind the prev one */
6868 /* We may have added or removed components. If so, we must update the
6869 * start & ends of all the mirrors after the current one, and the end
6870 * of the current mirror. */
6871 change = max_comp - 1 - lo->ldo_mirrors[pick].lme_end;
6873 lo->ldo_mirrors[pick].lme_end += change;
6874 for (i = pick + 1; i < lo->ldo_mirror_count; i++) {
6875 lo->ldo_mirrors[i].lme_start += change;
6876 lo->ldo_mirrors[i].lme_end += change;
6882 /* The amount of components has changed, adjust the lti_comp_idx */
6883 rc2 = lod_layout_data_init(info, lo->ldo_comp_cnt);
6885 return rc < 0 ? rc : rc2 < 0 ? rc2 : layout_changed;
6888 /* If striping is already instantiated or INIT'ed DOM? */
6889 static bool lod_is_instantiation_needed(struct lod_layout_component *comp)
6891 return !(((lov_pattern(comp->llc_pattern) == LOV_PATTERN_MDT) &&
6892 lod_comp_inited(comp)) || comp->llc_stripe);
6896 * Declare layout update for a non-FLR layout.
6898 * \param[in] env execution environment for this thread
6899 * \param[in,out] lo object to update the layout of
6900 * \param[in] layout layout intent for requested operation, "update" is
6901 * a process of reacting to this
6902 * \param[in] buf buffer containing lov ea (see comment on usage inline)
6903 * \param[in] th transaction handle for this operation
6905 * \retval 0 on success
6906 * \retval negative errno on error
6908 static int lod_declare_update_plain(const struct lu_env *env,
6909 struct lod_object *lo, struct layout_intent *layout,
6910 const struct lu_buf *buf, struct thandle *th)
6912 struct lod_thread_info *info = lod_env_info(env);
6913 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6914 struct lod_layout_component *lod_comp;
6915 struct lov_comp_md_v1 *comp_v1 = NULL;
6916 bool layout_changed = false;
6917 bool replay = false;
6921 LASSERT(lo->ldo_flr_state == LCM_FL_NONE);
6924 * In case the client is passing lovea, which only happens during
6925 * the replay of layout intent write RPC for now, we may need to
6926 * parse the lovea and apply new layout configuration.
6928 if (buf && buf->lb_len) {
6929 struct lov_user_md_v1 *v1 = buf->lb_buf;
6931 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
6932 v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
6933 LOV_MAGIC_COMP_V1)) {
6934 CERROR("%s: the replay buffer of layout extend "
6935 "(magic %#x) does not contain expected "
6936 "composite layout.\n",
6937 lod2obd(d)->obd_name, v1->lmm_magic);
6938 GOTO(out, rc = -EINVAL);
6941 rc = lod_use_defined_striping(env, lo, buf);
6944 lo->ldo_comp_cached = 1;
6946 rc = lod_get_lov_ea(env, lo);
6949 /* old on-disk EA is stored in info->lti_buf */
6950 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
6952 layout_changed = true;
6954 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
6958 /* non replay path */
6959 rc = lod_striping_load(env, lo);
6964 /* Make sure defined layout covers the requested write range. */
6965 lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
6966 if (lo->ldo_comp_cnt > 1 &&
6967 lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
6968 lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
6969 CDEBUG_LIMIT(replay ? D_ERROR : D_LAYOUT,
6970 "%s: the defined layout [0, %#llx) does not "
6971 "covers the write range "DEXT"\n",
6972 lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
6973 PEXT(&layout->li_extent));
6974 GOTO(out, rc = -EINVAL);
6977 CDEBUG(D_LAYOUT, "%s: "DFID": update components "DEXT"\n",
6978 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
6979 PEXT(&layout->li_extent));
6982 rc = lod_declare_update_extents(env, lo, &layout->li_extent,
6983 th, 0, layout->li_opc == LAYOUT_INTENT_WRITE);
6987 layout_changed = true;
6991 * Iterate ld->ldo_comp_entries, find the component whose extent under
6992 * the write range and not instantianted.
6994 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6995 lod_comp = &lo->ldo_comp_entries[i];
6997 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
7001 /* If striping is instantiated or INIT'ed DOM skip */
7002 if (!lod_is_instantiation_needed(lod_comp))
7006 * In replay path, lod_comp is the EA passed by
7007 * client replay buffer, comp_v1 is the pre-recovery
7008 * on-disk EA, we'd sift out those components which
7009 * were init-ed in the on-disk EA.
7011 if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
7016 * this component hasn't instantiated in normal path, or during
7017 * replay it needs replay the instantiation.
7020 /* A released component is being extended */
7021 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
7022 GOTO(out, rc = -EINVAL);
7024 LASSERT(info->lti_comp_idx != NULL);
7025 info->lti_comp_idx[info->lti_count++] = i;
7026 layout_changed = true;
7029 if (!layout_changed)
7032 lod_obj_inc_layout_gen(lo);
7033 rc = lod_declare_instantiate_components(env, lo, th);
7037 lod_striping_free(env, lo);
7041 static inline int lod_comp_index(struct lod_object *lo,
7042 struct lod_layout_component *lod_comp)
7044 LASSERT(lod_comp >= lo->ldo_comp_entries &&
7045 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
7047 return lod_comp - lo->ldo_comp_entries;
7051 * Stale other mirrors by writing extent.
7053 static int lod_stale_components(const struct lu_env *env, struct lod_object *lo,
7054 int primary, struct lu_extent *extent,
7057 struct lod_layout_component *pri_comp, *lod_comp;
7058 struct lu_extent pri_extent;
7063 /* The writing extent decides which components in the primary
7064 * are affected... */
7065 CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
7068 lod_foreach_mirror_comp(pri_comp, lo, primary) {
7069 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
7072 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
7073 lod_comp_index(lo, pri_comp),
7074 PEXT(&pri_comp->llc_extent));
7076 pri_extent.e_start = pri_comp->llc_extent.e_start;
7077 pri_extent.e_end = pri_comp->llc_extent.e_end;
7079 for (i = 0; i < lo->ldo_mirror_count; i++) {
7082 rc = lod_declare_update_extents(env, lo, &pri_extent,
7084 /* if update_extents changed the layout, it may have
7085 * reallocated the component array, so start over to
7086 * avoid using stale pointers */
7092 /* ... and then stale other components that are
7093 * overlapping with primary components */
7094 lod_foreach_mirror_comp(lod_comp, lo, i) {
7095 if (!lu_extent_is_overlapped(
7097 &lod_comp->llc_extent))
7100 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
7101 i, lod_comp_index(lo, lod_comp));
7103 lod_comp->llc_flags |= LCME_FL_STALE;
7104 lo->ldo_mirrors[i].lme_stale = 1;
7113 * check an OST's availability
7114 * \param[in] env execution environment
7115 * \param[in] lo lod object
7116 * \param[in] dt dt object
7117 * \param[in] index mirror index
7119 * \retval negative if failed
7120 * \retval 1 if \a dt is available
7121 * \retval 0 if \a dt is not available
7123 static inline int lod_check_ost_avail(const struct lu_env *env,
7124 struct lod_object *lo,
7125 struct dt_object *dt, int index)
7127 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7128 struct lod_tgt_desc *ost;
7130 int type = LU_SEQ_RANGE_OST;
7133 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &idx, &type);
7135 CERROR("%s: can't locate "DFID":rc = %d\n",
7136 lod2obd(lod)->obd_name, PFID(lu_object_fid(&dt->do_lu)),
7141 ost = OST_TGT(lod, idx);
7142 if (ost->ltd_statfs.os_state &
7143 (OS_STATE_READONLY | OS_STATE_ENOSPC | OS_STATE_ENOINO |
7144 OS_STATE_NOPRECREATE) ||
7145 ost->ltd_active == 0) {
7146 CDEBUG(D_LAYOUT, DFID ": mirror %d OST%d unavail, rc = %d\n",
7147 PFID(lod_object_fid(lo)), index, idx, rc);
7155 * Pick primary mirror for write
7156 * \param[in] env execution environment
7157 * \param[in] lo object
7158 * \param[in] extent write range
7160 static int lod_primary_pick(const struct lu_env *env, struct lod_object *lo,
7161 struct lu_extent *extent)
7163 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7164 unsigned int seq = 0;
7165 struct lod_layout_component *lod_comp;
7167 int picked = -1, second_pick = -1, third_pick = -1;
7170 if (OBD_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
7171 get_random_bytes(&seq, sizeof(seq));
7172 seq %= lo->ldo_mirror_count;
7176 * Pick a mirror as the primary, and check the availability of OSTs.
7178 * This algo can be revised later after knowing the topology of
7181 lod_qos_statfs_update(env, lod);
7182 for (i = 0; i < lo->ldo_mirror_count; i++) {
7183 bool ost_avail = true;
7184 int index = (i + seq) % lo->ldo_mirror_count;
7186 if (lo->ldo_mirrors[index].lme_stale) {
7187 CDEBUG(D_LAYOUT, DFID": mirror %d stale\n",
7188 PFID(lod_object_fid(lo)), index);
7192 /* 2nd pick is for the primary mirror containing unavail OST */
7193 if (lo->ldo_mirrors[index].lme_primary && second_pick < 0)
7194 second_pick = index;
7196 /* 3rd pick is for non-primary mirror containing unavail OST */
7197 if (second_pick < 0 && third_pick < 0)
7201 * we found a non-primary 1st pick, we'd like to find a
7202 * potential pirmary mirror.
7204 if (picked >= 0 && !lo->ldo_mirrors[index].lme_primary)
7207 /* check the availability of OSTs */
7208 lod_foreach_mirror_comp(lod_comp, lo, index) {
7209 if (!lod_comp_inited(lod_comp) || !lod_comp->llc_stripe)
7212 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
7213 struct dt_object *dt = lod_comp->llc_stripe[j];
7215 rc = lod_check_ost_avail(env, lo, dt, index);
7222 } /* for all dt object in one component */
7225 } /* for all components in a mirror */
7228 * the OSTs where allocated objects locates in the components
7229 * of the mirror are available.
7234 /* this mirror has all OSTs available */
7238 * primary with all OSTs are available, this is the perfect
7241 if (lo->ldo_mirrors[index].lme_primary)
7243 } /* for all mirrors */
7245 /* failed to pick a sound mirror, lower our expectation */
7247 picked = second_pick;
7249 picked = third_pick;
7256 static int lod_prepare_resync_mirror(const struct lu_env *env,
7257 struct lod_object *lo,
7260 struct lod_thread_info *info = lod_env_info(env);
7261 struct lod_layout_component *lod_comp;
7262 bool neg = !!(MIRROR_ID_NEG & mirror_id);
7265 mirror_id &= ~MIRROR_ID_NEG;
7267 for (i = 0; i < lo->ldo_mirror_count; i++) {
7268 if ((!neg && lo->ldo_mirrors[i].lme_id != mirror_id) ||
7269 (neg && lo->ldo_mirrors[i].lme_id == mirror_id))
7272 lod_foreach_mirror_comp(lod_comp, lo, i) {
7273 if (lod_comp_inited(lod_comp))
7276 info->lti_comp_idx[info->lti_count++] =
7277 lod_comp_index(lo, lod_comp);
7285 * figure out the components should be instantiated for resync.
7287 static int lod_prepare_resync(const struct lu_env *env, struct lod_object *lo,
7288 struct lu_extent *extent)
7290 struct lod_thread_info *info = lod_env_info(env);
7291 struct lod_layout_component *lod_comp;
7292 unsigned int need_sync = 0;
7296 DFID": instantiate all stale components in "DEXT"\n",
7297 PFID(lod_object_fid(lo)), PEXT(extent));
7300 * instantiate all components within this extent, even non-stale
7303 for (i = 0; i < lo->ldo_mirror_count; i++) {
7304 if (!lo->ldo_mirrors[i].lme_stale)
7307 lod_foreach_mirror_comp(lod_comp, lo, i) {
7308 if (!lu_extent_is_overlapped(extent,
7309 &lod_comp->llc_extent))
7314 if (lod_comp_inited(lod_comp))
7317 CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
7318 i, lod_comp_index(lo, lod_comp));
7319 info->lti_comp_idx[info->lti_count++] =
7320 lod_comp_index(lo, lod_comp);
7324 return need_sync ? 0 : -EALREADY;
7327 static int lod_declare_update_rdonly(const struct lu_env *env,
7328 struct lod_object *lo, struct md_layout_change *mlc,
7331 struct lod_thread_info *info = lod_env_info(env);
7332 struct lu_attr *layout_attr = &info->lti_layout_attr;
7333 struct lod_layout_component *lod_comp;
7334 struct lu_extent extent = { 0 };
7338 LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
7339 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7340 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7341 LASSERT(lo->ldo_mirror_count > 0);
7343 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7344 struct layout_intent *layout = mlc->mlc_intent;
7345 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7348 extent = layout->li_extent;
7349 CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
7350 PFID(lod_object_fid(lo)), PEXT(&extent));
7352 picked = lod_primary_pick(env, lo, &extent);
7356 CDEBUG(D_LAYOUT, DFID": picked mirror id %u as primary\n",
7357 PFID(lod_object_fid(lo)),
7358 lo->ldo_mirrors[picked].lme_id);
7360 /* Update extents of primary before staling */
7361 rc = lod_declare_update_extents(env, lo, &extent, th, picked,
7366 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
7368 * trunc transfers [0, size) in the intent extent, we'd
7369 * stale components overlapping [size, eof).
7371 extent.e_start = extent.e_end;
7372 extent.e_end = OBD_OBJECT_EOF;
7375 /* stale overlapping components from other mirrors */
7376 rc = lod_stale_components(env, lo, picked, &extent, th);
7380 /* restore truncate intent extent */
7381 if (layout->li_opc == LAYOUT_INTENT_TRUNC)
7382 extent.e_end = extent.e_start;
7384 /* instantiate components for the picked mirror, start from 0 */
7387 lod_foreach_mirror_comp(lod_comp, lo, picked) {
7388 if (!lu_extent_is_overlapped(&extent,
7389 &lod_comp->llc_extent))
7392 if (!lod_is_instantiation_needed(lod_comp))
7395 info->lti_comp_idx[info->lti_count++] =
7396 lod_comp_index(lo, lod_comp);
7399 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7400 } else { /* MD_LAYOUT_RESYNC */
7404 * could contain multiple non-stale mirrors, so we need to
7405 * prep uninited all components assuming any non-stale mirror
7406 * could be picked as the primary mirror.
7408 if (mlc->mlc_mirror_id == 0) {
7410 for (i = 0; i < lo->ldo_mirror_count; i++) {
7411 if (lo->ldo_mirrors[i].lme_stale)
7414 lod_foreach_mirror_comp(lod_comp, lo, i) {
7415 if (!lod_comp_inited(lod_comp))
7419 lod_comp->llc_extent.e_end)
7421 lod_comp->llc_extent.e_end;
7424 rc = lod_prepare_resync(env, lo, &extent);
7428 /* mirror write, try to init its all components */
7429 rc = lod_prepare_resync_mirror(env, lo,
7430 mlc->mlc_mirror_id);
7435 /* change the file state to SYNC_PENDING */
7436 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7439 /* Reset the layout version once it's becoming too large.
7440 * This way it can make sure that the layout version is
7441 * monotonously increased in this writing era. */
7442 lod_obj_inc_layout_gen(lo);
7443 if (lo->ldo_layout_gen > (LCME_ID_MAX >> 1)) {
7444 __u32 layout_version;
7446 get_random_bytes(&layout_version, sizeof(layout_version));
7447 lo->ldo_layout_gen = layout_version & 0xffff;
7450 rc = lod_declare_instantiate_components(env, lo, th);
7454 layout_attr->la_valid = LA_LAYOUT_VERSION;
7455 layout_attr->la_layout_version = 0; /* set current version */
7456 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7457 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7458 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7464 lod_striping_free(env, lo);
7468 static int lod_declare_update_write_pending(const struct lu_env *env,
7469 struct lod_object *lo, struct md_layout_change *mlc,
7472 struct lod_thread_info *info = lod_env_info(env);
7473 struct lu_attr *layout_attr = &info->lti_layout_attr;
7474 struct lod_layout_component *lod_comp;
7475 struct lu_extent extent = { 0 };
7481 LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
7482 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7483 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7485 /* look for the primary mirror */
7486 for (i = 0; i < lo->ldo_mirror_count; i++) {
7487 if (lo->ldo_mirrors[i].lme_stale)
7490 LASSERTF(primary < 0, DFID " has multiple primary: %u / %u",
7491 PFID(lod_object_fid(lo)),
7492 lo->ldo_mirrors[i].lme_id,
7493 lo->ldo_mirrors[primary].lme_id);
7498 CERROR(DFID ": doesn't have a primary mirror\n",
7499 PFID(lod_object_fid(lo)));
7500 GOTO(out, rc = -ENODATA);
7503 CDEBUG(D_LAYOUT, DFID": found primary %u\n",
7504 PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
7506 LASSERT(!lo->ldo_mirrors[primary].lme_stale);
7508 /* for LAYOUT_WRITE opc, it has to do the following operations:
7509 * 1. stale overlapping componets from stale mirrors;
7510 * 2. instantiate components of the primary mirror;
7511 * 3. transfter layout version to all objects of the primary;
7513 * for LAYOUT_RESYNC opc, it will do:
7514 * 1. instantiate components of all stale mirrors;
7515 * 2. transfer layout version to all objects to close write era. */
7517 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7518 struct layout_intent *layout = mlc->mlc_intent;
7519 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7521 LASSERT(mlc->mlc_intent != NULL);
7523 extent = mlc->mlc_intent->li_extent;
7525 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
7526 PFID(lod_object_fid(lo)), PEXT(&extent));
7528 /* 1. Update extents of primary before staling */
7529 rc = lod_declare_update_extents(env, lo, &extent, th, primary,
7534 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
7536 * trunc transfers [0, size) in the intent extent, we'd
7537 * stale components overlapping [size, eof).
7539 extent.e_start = extent.e_end;
7540 extent.e_end = OBD_OBJECT_EOF;
7543 /* 2. stale overlapping components */
7544 rc = lod_stale_components(env, lo, primary, &extent, th);
7548 /* 3. find the components which need instantiating.
7549 * instantiate [0, mlc->mlc_intent->e_end) */
7551 /* restore truncate intent extent */
7552 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC)
7553 extent.e_end = extent.e_start;
7556 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7557 if (!lu_extent_is_overlapped(&extent,
7558 &lod_comp->llc_extent))
7561 if (!lod_is_instantiation_needed(lod_comp))
7564 CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
7565 primary, lod_comp_index(lo, lod_comp));
7566 info->lti_comp_idx[info->lti_count++] =
7567 lod_comp_index(lo, lod_comp);
7569 } else { /* MD_LAYOUT_RESYNC */
7570 if (mlc->mlc_mirror_id == 0) {
7572 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7573 if (!lod_comp_inited(lod_comp))
7576 extent.e_end = lod_comp->llc_extent.e_end;
7579 rc = lod_prepare_resync(env, lo, &extent);
7583 /* mirror write, try to init its all components */
7584 rc = lod_prepare_resync_mirror(env, lo,
7585 mlc->mlc_mirror_id);
7590 /* change the file state to SYNC_PENDING */
7591 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7594 rc = lod_declare_instantiate_components(env, lo, th);
7598 /* 3. transfer layout version to OST objects.
7599 * transfer new layout version to OST objects so that stale writes
7600 * can be denied. It also ends an era of writing by setting
7601 * LU_LAYOUT_RESYNC. Normal client can never use this bit to
7602 * send write RPC; only resync RPCs could do it. */
7603 layout_attr->la_valid = LA_LAYOUT_VERSION;
7604 layout_attr->la_layout_version = 0; /* set current version */
7605 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7606 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7607 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7611 lod_obj_inc_layout_gen(lo);
7614 lod_striping_free(env, lo);
7618 static int lod_declare_update_sync_pending(const struct lu_env *env,
7619 struct lod_object *lo, struct md_layout_change *mlc,
7622 struct lod_thread_info *info = lod_env_info(env);
7623 unsigned sync_components = 0;
7624 unsigned resync_components = 0;
7629 LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
7630 LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
7631 mlc->mlc_opc == MD_LAYOUT_WRITE);
7633 CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
7634 PFID(lod_object_fid(lo)), mlc->mlc_opc);
7636 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7637 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
7638 PFID(lod_object_fid(lo)));
7640 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7641 return lod_declare_update_write_pending(env, lo, mlc, th);
7644 /* MD_LAYOUT_RESYNC_DONE */
7646 for (i = 0; i < lo->ldo_comp_cnt; i++) {
7647 struct lod_layout_component *lod_comp;
7650 lod_comp = &lo->ldo_comp_entries[i];
7652 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
7657 for (j = 0; j < mlc->mlc_resync_count; j++) {
7658 if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
7661 mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
7662 lod_comp->llc_flags &= ~LCME_FL_STALE;
7663 resync_components++;
7669 for (i = 0; i < mlc->mlc_resync_count; i++) {
7670 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
7673 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
7674 "or already synced\n", PFID(lod_object_fid(lo)),
7675 mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
7676 GOTO(out, rc = -EINVAL);
7679 if (!sync_components || (mlc->mlc_resync_count && !resync_components)) {
7680 CDEBUG(D_LAYOUT, DFID": no mirror in sync\n",
7681 PFID(lod_object_fid(lo)));
7683 /* tend to return an error code here to prevent
7684 * the MDT from setting SoM attribute */
7685 GOTO(out, rc = -EINVAL);
7688 CDEBUG(D_LAYOUT, DFID": synced %u resynced %u/%zu components\n",
7689 PFID(lod_object_fid(lo)),
7690 sync_components, resync_components, mlc->mlc_resync_count);
7692 lo->ldo_flr_state = LCM_FL_RDONLY;
7693 lod_obj_inc_layout_gen(lo);
7695 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
7696 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
7697 &info->lti_buf, XATTR_NAME_LOV, 0, th);
7702 lod_striping_free(env, lo);
7706 static int lod_declare_layout_change(const struct lu_env *env,
7707 struct dt_object *dt, struct md_layout_change *mlc,
7710 struct lod_thread_info *info = lod_env_info(env);
7711 struct lod_object *lo = lod_dt_obj(dt);
7715 if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
7716 dt_object_remote(dt_object_child(dt)))
7719 rc = lod_striping_load(env, lo);
7723 LASSERT(lo->ldo_comp_cnt > 0);
7725 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
7729 switch (lo->ldo_flr_state) {
7731 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
7735 rc = lod_declare_update_rdonly(env, lo, mlc, th);
7737 case LCM_FL_WRITE_PENDING:
7738 rc = lod_declare_update_write_pending(env, lo, mlc, th);
7740 case LCM_FL_SYNC_PENDING:
7741 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
7752 * Instantiate layout component objects which covers the intent write offset.
7754 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
7755 struct md_layout_change *mlc, struct thandle *th)
7757 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
7758 struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
7759 struct lod_object *lo = lod_dt_obj(dt);
7762 rc = lod_striped_create(env, dt, attr, NULL, th);
7763 if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
7764 layout_attr->la_layout_version |= lo->ldo_layout_gen;
7765 rc = lod_attr_set(env, dt, layout_attr, th);
7771 struct dt_object_operations lod_obj_ops = {
7772 .do_read_lock = lod_read_lock,
7773 .do_write_lock = lod_write_lock,
7774 .do_read_unlock = lod_read_unlock,
7775 .do_write_unlock = lod_write_unlock,
7776 .do_write_locked = lod_write_locked,
7777 .do_attr_get = lod_attr_get,
7778 .do_declare_attr_set = lod_declare_attr_set,
7779 .do_attr_set = lod_attr_set,
7780 .do_xattr_get = lod_xattr_get,
7781 .do_declare_xattr_set = lod_declare_xattr_set,
7782 .do_xattr_set = lod_xattr_set,
7783 .do_declare_xattr_del = lod_declare_xattr_del,
7784 .do_xattr_del = lod_xattr_del,
7785 .do_xattr_list = lod_xattr_list,
7786 .do_ah_init = lod_ah_init,
7787 .do_declare_create = lod_declare_create,
7788 .do_create = lod_create,
7789 .do_declare_destroy = lod_declare_destroy,
7790 .do_destroy = lod_destroy,
7791 .do_index_try = lod_index_try,
7792 .do_declare_ref_add = lod_declare_ref_add,
7793 .do_ref_add = lod_ref_add,
7794 .do_declare_ref_del = lod_declare_ref_del,
7795 .do_ref_del = lod_ref_del,
7796 .do_object_sync = lod_object_sync,
7797 .do_object_lock = lod_object_lock,
7798 .do_object_unlock = lod_object_unlock,
7799 .do_invalidate = lod_invalidate,
7800 .do_declare_layout_change = lod_declare_layout_change,
7801 .do_layout_change = lod_layout_change,
7805 * Implementation of dt_body_operations::dbo_read.
7807 * \see dt_body_operations::dbo_read() in the API description for details.
7809 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
7810 struct lu_buf *buf, loff_t *pos)
7812 struct dt_object *next = dt_object_child(dt);
7814 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
7815 S_ISLNK(dt->do_lu.lo_header->loh_attr));
7816 return next->do_body_ops->dbo_read(env, next, buf, pos);
7820 * Implementation of dt_body_operations::dbo_declare_write.
7822 * \see dt_body_operations::dbo_declare_write() in the API description
7825 static ssize_t lod_declare_write(const struct lu_env *env,
7826 struct dt_object *dt,
7827 const struct lu_buf *buf, loff_t pos,
7830 return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
7834 * Implementation of dt_body_operations::dbo_write.
7836 * \see dt_body_operations::dbo_write() in the API description for details.
7838 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
7839 const struct lu_buf *buf, loff_t *pos,
7842 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
7843 S_ISLNK(dt->do_lu.lo_header->loh_attr));
7844 return lod_sub_write(env, dt_object_child(dt), buf, pos, th);
7847 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
7848 __u64 start, __u64 end, struct thandle *th)
7850 if (dt_object_remote(dt))
7853 return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
7856 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
7857 __u64 start, __u64 end, struct thandle *th)
7859 if (dt_object_remote(dt))
7862 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
7863 return lod_sub_punch(env, dt_object_child(dt), start, end, th);
7867 * different type of files use the same body_ops because object may be created
7868 * in OUT, where there is no chance to set correct body_ops for each type, so
7869 * body_ops themselves will check file type inside, see lod_read/write/punch for
7872 const struct dt_body_operations lod_body_ops = {
7873 .dbo_read = lod_read,
7874 .dbo_declare_write = lod_declare_write,
7875 .dbo_write = lod_write,
7876 .dbo_declare_punch = lod_declare_punch,
7877 .dbo_punch = lod_punch,
7881 * Implementation of lu_object_operations::loo_object_init.
7883 * The function determines the type and the index of the target device using
7884 * sequence of the object's FID. Then passes control down to the
7885 * corresponding device:
7886 * OSD for the local objects, OSP for remote
7888 * \see lu_object_operations::loo_object_init() in the API description
7891 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
7892 const struct lu_object_conf *conf)
7894 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
7895 struct lu_device *cdev = NULL;
7896 struct lu_object *cobj;
7897 struct lod_tgt_descs *ltd = NULL;
7898 struct lod_tgt_desc *tgt;
7900 int type = LU_SEQ_RANGE_ANY;
7904 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
7908 if (type == LU_SEQ_RANGE_MDT &&
7909 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
7910 cdev = &lod->lod_child->dd_lu_dev;
7911 } else if (type == LU_SEQ_RANGE_MDT) {
7912 ltd = &lod->lod_mdt_descs;
7914 } else if (type == LU_SEQ_RANGE_OST) {
7915 ltd = &lod->lod_ost_descs;
7922 if (ltd->ltd_tgts_size > idx &&
7923 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
7924 tgt = LTD_TGT(ltd, idx);
7926 LASSERT(tgt != NULL);
7927 LASSERT(tgt->ltd_tgt != NULL);
7929 cdev = &(tgt->ltd_tgt->dd_lu_dev);
7931 lod_putref(lod, ltd);
7934 if (unlikely(cdev == NULL))
7937 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
7938 if (unlikely(cobj == NULL))
7941 lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
7943 lu_object_add(lo, cobj);
7950 * Alloc cached foreign LOV
7952 * \param[in] lo object
7953 * \param[in] size size of foreign LOV
7955 * \retval 0 on success
7956 * \retval negative if failed
7958 int lod_alloc_foreign_lov(struct lod_object *lo, size_t size)
7960 OBD_ALLOC_LARGE(lo->ldo_foreign_lov, size);
7961 if (lo->ldo_foreign_lov == NULL)
7963 lo->ldo_foreign_lov_size = size;
7964 lo->ldo_is_foreign = 1;
7970 * Free cached foreign LOV
7972 * \param[in] lo object
7974 void lod_free_foreign_lov(struct lod_object *lo)
7976 if (lo->ldo_foreign_lov != NULL)
7977 OBD_FREE_LARGE(lo->ldo_foreign_lov, lo->ldo_foreign_lov_size);
7978 lo->ldo_foreign_lov = NULL;
7979 lo->ldo_foreign_lov_size = 0;
7980 lo->ldo_is_foreign = 0;
7985 * Free cached foreign LMV
7987 * \param[in] lo object
7989 void lod_free_foreign_lmv(struct lod_object *lo)
7991 if (lo->ldo_foreign_lmv != NULL)
7992 OBD_FREE_LARGE(lo->ldo_foreign_lmv, lo->ldo_foreign_lmv_size);
7993 lo->ldo_foreign_lmv = NULL;
7994 lo->ldo_foreign_lmv_size = 0;
7995 lo->ldo_dir_is_foreign = 0;
8000 * Release resources associated with striping.
8002 * If the object is striped (regular or directory), then release
8003 * the stripe objects references and free the ldo_stripe array.
8005 * \param[in] env execution environment
8006 * \param[in] lo object
8008 void lod_striping_free_nolock(const struct lu_env *env, struct lod_object *lo)
8010 struct lod_layout_component *lod_comp;
8013 if (unlikely(lo->ldo_is_foreign)) {
8014 lod_free_foreign_lov(lo);
8015 lo->ldo_comp_cached = 0;
8016 } else if (unlikely(lo->ldo_dir_is_foreign)) {
8017 lod_free_foreign_lmv(lo);
8018 lo->ldo_dir_stripe_loaded = 0;
8019 } else if (lo->ldo_stripe != NULL) {
8020 LASSERT(lo->ldo_comp_entries == NULL);
8021 LASSERT(lo->ldo_dir_stripes_allocated > 0);
8023 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8024 if (lo->ldo_stripe[i])
8025 dt_object_put(env, lo->ldo_stripe[i]);
8028 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
8029 OBD_FREE(lo->ldo_stripe, j);
8030 lo->ldo_stripe = NULL;
8031 lo->ldo_dir_stripes_allocated = 0;
8032 lo->ldo_dir_stripe_loaded = 0;
8033 lo->ldo_dir_stripe_count = 0;
8034 } else if (lo->ldo_comp_entries != NULL) {
8035 for (i = 0; i < lo->ldo_comp_cnt; i++) {
8036 /* free lod_layout_component::llc_stripe array */
8037 lod_comp = &lo->ldo_comp_entries[i];
8039 if (lod_comp->llc_stripe == NULL)
8041 LASSERT(lod_comp->llc_stripes_allocated != 0);
8042 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
8043 if (lod_comp->llc_stripe[j] != NULL)
8045 &lod_comp->llc_stripe[j]->do_lu);
8047 OBD_FREE(lod_comp->llc_stripe,
8048 sizeof(struct dt_object *) *
8049 lod_comp->llc_stripes_allocated);
8050 lod_comp->llc_stripe = NULL;
8051 OBD_FREE(lod_comp->llc_ost_indices,
8053 lod_comp->llc_stripes_allocated);
8054 lod_comp->llc_ost_indices = NULL;
8055 lod_comp->llc_stripes_allocated = 0;
8057 lod_free_comp_entries(lo);
8058 lo->ldo_comp_cached = 0;
8062 void lod_striping_free(const struct lu_env *env, struct lod_object *lo)
8064 mutex_lock(&lo->ldo_layout_mutex);
8065 lod_striping_free_nolock(env, lo);
8066 mutex_unlock(&lo->ldo_layout_mutex);
8070 * Implementation of lu_object_operations::loo_object_free.
8072 * \see lu_object_operations::loo_object_free() in the API description
8075 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
8077 struct lod_object *lo = lu2lod_obj(o);
8079 /* release all underlying object pinned */
8080 lod_striping_free(env, lo);
8082 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
8086 * Implementation of lu_object_operations::loo_object_release.
8088 * \see lu_object_operations::loo_object_release() in the API description
8091 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
8093 /* XXX: shouldn't we release everything here in case if object
8094 * creation failed before? */
8098 * Implementation of lu_object_operations::loo_object_print.
8100 * \see lu_object_operations::loo_object_print() in the API description
8103 static int lod_object_print(const struct lu_env *env, void *cookie,
8104 lu_printer_t p, const struct lu_object *l)
8106 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
8108 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
8111 struct lu_object_operations lod_lu_obj_ops = {
8112 .loo_object_init = lod_object_init,
8113 .loo_object_free = lod_object_free,
8114 .loo_object_release = lod_object_release,
8115 .loo_object_print = lod_object_print,