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,
101 struct thandle *th, int ign)
103 return lod_sub_insert(env, dt_object_child(dt), rec, key, th, ign);
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);
387 next = lo->ldo_stripe[0];
388 LASSERT(next != NULL);
389 LASSERT(next->do_index_ops != NULL);
391 it_next = next->do_index_ops->dio_it.init(env, next, attr);
395 /* currently we do not use more than one iterator per thread
396 * so we store it in thread info. if at some point we need
397 * more active iterators in a single thread, we can allocate
399 LASSERT(it->lit_obj == NULL);
401 it->lit_stripe_index = 0;
403 it->lit_it = it_next;
406 return (struct dt_it *)it;
409 #define LOD_CHECK_STRIPED_IT(env, it, lo) \
411 LASSERT((it)->lit_obj != NULL); \
412 LASSERT((it)->lit_it != NULL); \
413 LASSERT((lo)->ldo_dir_stripe_count > 0); \
414 LASSERT((it)->lit_stripe_index < (lo)->ldo_dir_stripe_count); \
418 * Implementation of dt_it_ops::fini.
420 * Used with striped objects.
422 * \see dt_it_ops::fini() in the API description for details.
424 static void lod_striped_it_fini(const struct lu_env *env, struct dt_it *di)
426 struct lod_it *it = (struct lod_it *)di;
427 struct lod_object *lo = lod_dt_obj(it->lit_obj);
428 struct dt_object *next;
430 /* If lit_it == NULL, then it means the sub_it has been finished,
431 * which only happens in failure cases, see lod_striped_it_next() */
432 if (it->lit_it != NULL) {
433 LOD_CHECK_STRIPED_IT(env, it, lo);
435 next = lo->ldo_stripe[it->lit_stripe_index];
436 LASSERT(next != NULL);
437 LASSERT(next->do_index_ops != NULL);
439 next->do_index_ops->dio_it.fini(env, it->lit_it);
442 /* the iterator not in use any more */
445 it->lit_stripe_index = 0;
449 * Implementation of dt_it_ops::get.
451 * Right now it's not used widely, only to reset the iterator to the
452 * initial position. It should be possible to implement a full version
453 * which chooses a correct stripe to be able to position with any key.
455 * \see dt_it_ops::get() in the API description for details.
457 static int lod_striped_it_get(const struct lu_env *env, struct dt_it *di,
458 const struct dt_key *key)
460 const struct lod_it *it = (const struct lod_it *)di;
461 struct lod_object *lo = lod_dt_obj(it->lit_obj);
462 struct dt_object *next;
465 LOD_CHECK_STRIPED_IT(env, it, lo);
467 next = lo->ldo_stripe[it->lit_stripe_index];
468 LASSERT(next != NULL);
469 LASSERT(next->do_index_ops != NULL);
471 return next->do_index_ops->dio_it.get(env, it->lit_it, key);
475 * Implementation of dt_it_ops::put.
477 * Used with striped objects.
479 * \see dt_it_ops::put() in the API description for details.
481 static void lod_striped_it_put(const struct lu_env *env, struct dt_it *di)
483 struct lod_it *it = (struct lod_it *)di;
484 struct lod_object *lo = lod_dt_obj(it->lit_obj);
485 struct dt_object *next;
487 LOD_CHECK_STRIPED_IT(env, it, lo);
489 next = lo->ldo_stripe[it->lit_stripe_index];
490 LASSERT(next != NULL);
491 LASSERT(next->do_index_ops != NULL);
493 return next->do_index_ops->dio_it.put(env, it->lit_it);
497 * Implementation of dt_it_ops::next.
499 * Used with striped objects. When the end of the current stripe is
500 * reached, the method takes the next stripe's iterator.
502 * \see dt_it_ops::next() in the API description for details.
504 static int lod_striped_it_next(const struct lu_env *env, struct dt_it *di)
506 struct lod_it *it = (struct lod_it *)di;
507 struct lod_object *lo = lod_dt_obj(it->lit_obj);
508 struct dt_object *next;
509 struct dt_it *it_next;
513 LOD_CHECK_STRIPED_IT(env, it, lo);
515 next = lo->ldo_stripe[it->lit_stripe_index];
516 LASSERT(next != NULL);
517 LASSERT(next->do_index_ops != NULL);
519 rc = next->do_index_ops->dio_it.next(env, it->lit_it);
523 if (rc == 0 && it->lit_stripe_index == 0)
526 if (rc == 0 && it->lit_stripe_index > 0) {
527 struct lu_dirent *ent;
529 ent = (struct lu_dirent *)lod_env_info(env)->lti_key;
531 rc = next->do_index_ops->dio_it.rec(env, it->lit_it,
532 (struct dt_rec *)ent,
537 /* skip . and .. for slave stripe */
538 if ((strncmp(ent->lde_name, ".",
539 le16_to_cpu(ent->lde_namelen)) == 0 &&
540 le16_to_cpu(ent->lde_namelen) == 1) ||
541 (strncmp(ent->lde_name, "..",
542 le16_to_cpu(ent->lde_namelen)) == 0 &&
543 le16_to_cpu(ent->lde_namelen) == 2))
549 /* go to next stripe */
550 if (it->lit_stripe_index + 1 >= lo->ldo_dir_stripe_count)
553 it->lit_stripe_index++;
555 next->do_index_ops->dio_it.put(env, it->lit_it);
556 next->do_index_ops->dio_it.fini(env, it->lit_it);
559 next = lo->ldo_stripe[it->lit_stripe_index];
560 LASSERT(next != NULL);
561 rc = next->do_ops->do_index_try(env, next, &dt_directory_features);
565 LASSERT(next->do_index_ops != NULL);
567 it_next = next->do_index_ops->dio_it.init(env, next, it->lit_attr);
568 if (!IS_ERR(it_next)) {
569 it->lit_it = it_next;
572 rc = PTR_ERR(it_next);
579 * Implementation of dt_it_ops::key.
581 * Used with striped objects.
583 * \see dt_it_ops::key() in the API description for details.
585 static struct dt_key *lod_striped_it_key(const struct lu_env *env,
586 const struct dt_it *di)
588 const struct lod_it *it = (const struct lod_it *)di;
589 struct lod_object *lo = lod_dt_obj(it->lit_obj);
590 struct dt_object *next;
592 LOD_CHECK_STRIPED_IT(env, it, lo);
594 next = lo->ldo_stripe[it->lit_stripe_index];
595 LASSERT(next != NULL);
596 LASSERT(next->do_index_ops != NULL);
598 return next->do_index_ops->dio_it.key(env, it->lit_it);
602 * Implementation of dt_it_ops::key_size.
604 * Used with striped objects.
606 * \see dt_it_ops::size() in the API description for details.
608 static int lod_striped_it_key_size(const struct lu_env *env,
609 const struct dt_it *di)
611 struct lod_it *it = (struct lod_it *)di;
612 struct lod_object *lo = lod_dt_obj(it->lit_obj);
613 struct dt_object *next;
615 LOD_CHECK_STRIPED_IT(env, it, lo);
617 next = lo->ldo_stripe[it->lit_stripe_index];
618 LASSERT(next != NULL);
619 LASSERT(next->do_index_ops != NULL);
621 return next->do_index_ops->dio_it.key_size(env, it->lit_it);
625 * Implementation of dt_it_ops::rec.
627 * Used with striped objects.
629 * \see dt_it_ops::rec() in the API description for details.
631 static int lod_striped_it_rec(const struct lu_env *env, const struct dt_it *di,
632 struct dt_rec *rec, __u32 attr)
634 const struct lod_it *it = (const struct lod_it *)di;
635 struct lod_object *lo = lod_dt_obj(it->lit_obj);
636 struct dt_object *next;
638 LOD_CHECK_STRIPED_IT(env, it, lo);
640 next = lo->ldo_stripe[it->lit_stripe_index];
641 LASSERT(next != NULL);
642 LASSERT(next->do_index_ops != NULL);
644 return next->do_index_ops->dio_it.rec(env, it->lit_it, rec, attr);
648 * Implementation of dt_it_ops::rec_size.
650 * Used with striped objects.
652 * \see dt_it_ops::rec_size() in the API description for details.
654 static int lod_striped_it_rec_size(const struct lu_env *env,
655 const struct dt_it *di, __u32 attr)
657 struct lod_it *it = (struct lod_it *)di;
658 struct lod_object *lo = lod_dt_obj(it->lit_obj);
659 struct dt_object *next;
661 LOD_CHECK_STRIPED_IT(env, it, lo);
663 next = lo->ldo_stripe[it->lit_stripe_index];
664 LASSERT(next != NULL);
665 LASSERT(next->do_index_ops != NULL);
667 return next->do_index_ops->dio_it.rec_size(env, it->lit_it, attr);
671 * Implementation of dt_it_ops::store.
673 * Used with striped objects.
675 * \see dt_it_ops::store() in the API description for details.
677 static __u64 lod_striped_it_store(const struct lu_env *env,
678 const struct dt_it *di)
680 const struct lod_it *it = (const struct lod_it *)di;
681 struct lod_object *lo = lod_dt_obj(it->lit_obj);
682 struct dt_object *next;
684 LOD_CHECK_STRIPED_IT(env, it, lo);
686 next = lo->ldo_stripe[it->lit_stripe_index];
687 LASSERT(next != NULL);
688 LASSERT(next->do_index_ops != NULL);
690 return next->do_index_ops->dio_it.store(env, it->lit_it);
694 * Implementation of dt_it_ops::load.
696 * Used with striped objects.
698 * \see dt_it_ops::load() in the API description for details.
700 static int lod_striped_it_load(const struct lu_env *env,
701 const struct dt_it *di, __u64 hash)
703 const struct lod_it *it = (const struct lod_it *)di;
704 struct lod_object *lo = lod_dt_obj(it->lit_obj);
705 struct dt_object *next;
707 LOD_CHECK_STRIPED_IT(env, it, lo);
709 next = lo->ldo_stripe[it->lit_stripe_index];
710 LASSERT(next != NULL);
711 LASSERT(next->do_index_ops != NULL);
713 return next->do_index_ops->dio_it.load(env, it->lit_it, hash);
716 static struct dt_index_operations lod_striped_index_ops = {
717 .dio_lookup = lod_lookup,
718 .dio_declare_insert = lod_declare_insert,
719 .dio_insert = lod_insert,
720 .dio_declare_delete = lod_declare_delete,
721 .dio_delete = lod_delete,
723 .init = lod_striped_it_init,
724 .fini = lod_striped_it_fini,
725 .get = lod_striped_it_get,
726 .put = lod_striped_it_put,
727 .next = lod_striped_it_next,
728 .key = lod_striped_it_key,
729 .key_size = lod_striped_it_key_size,
730 .rec = lod_striped_it_rec,
731 .rec_size = lod_striped_it_rec_size,
732 .store = lod_striped_it_store,
733 .load = lod_striped_it_load,
738 * Append the FID for each shard of the striped directory after the
739 * given LMV EA header.
741 * To simplify striped directory and the consistency verification,
742 * we only store the LMV EA header on disk, for both master object
743 * and slave objects. When someone wants to know the whole LMV EA,
744 * such as client readdir(), we can build the entrie LMV EA on the
745 * MDT side (in RAM) via iterating the sub-directory entries that
746 * are contained in the master object of the stripe directory.
748 * For the master object of the striped directroy, the valid name
749 * for each shard is composed of the ${shard_FID}:${shard_idx}.
751 * There may be holes in the LMV EA if some shards' name entries
752 * are corrupted or lost.
754 * \param[in] env pointer to the thread context
755 * \param[in] lo pointer to the master object of the striped directory
756 * \param[in] buf pointer to the lu_buf which will hold the LMV EA
757 * \param[in] resize whether re-allocate the buffer if it is not big enough
759 * \retval positive size of the LMV EA
760 * \retval 0 for nothing to be loaded
761 * \retval negative error number on failure
763 int lod_load_lmv_shards(const struct lu_env *env, struct lod_object *lo,
764 struct lu_buf *buf, bool resize)
766 struct lu_dirent *ent =
767 (struct lu_dirent *)lod_env_info(env)->lti_key;
768 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
769 struct dt_object *obj = dt_object_child(&lo->ldo_obj);
770 struct lmv_mds_md_v1 *lmv1 = buf->lb_buf;
772 const struct dt_it_ops *iops;
774 __u32 magic = le32_to_cpu(lmv1->lmv_magic);
779 /* If it is not a striped directory, then load nothing. */
780 if (magic != LMV_MAGIC_V1)
783 /* If it is in migration (or failure), then load nothing. */
784 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
787 stripes = le32_to_cpu(lmv1->lmv_stripe_count);
791 rc = lmv_mds_md_size(stripes, magic);
795 if (buf->lb_len < lmv1_size) {
804 lu_buf_alloc(buf, lmv1_size);
809 memcpy(buf->lb_buf, tbuf.lb_buf, tbuf.lb_len);
812 if (unlikely(!dt_try_as_dir(env, obj)))
815 memset(&lmv1->lmv_stripe_fids[0], 0, stripes * sizeof(struct lu_fid));
816 iops = &obj->do_index_ops->dio_it;
817 it = iops->init(env, obj, LUDA_64BITHASH);
821 rc = iops->load(env, it, 0);
823 rc = iops->next(env, it);
828 char name[FID_LEN + 2] = "";
833 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
839 fid_le_to_cpu(&fid, &ent->lde_fid);
840 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
841 if (ent->lde_name[0] == '.') {
842 if (ent->lde_namelen == 1)
845 if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
849 len = snprintf(name, sizeof(name),
850 DFID":", PFID(&ent->lde_fid));
851 /* The ent->lde_name is composed of ${FID}:${index} */
852 if (ent->lde_namelen < len + 1 ||
853 memcmp(ent->lde_name, name, len) != 0) {
854 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
855 "%s: invalid shard name %.*s with the FID "DFID
856 " for the striped directory "DFID", %s\n",
857 lod2obd(lod)->obd_name, ent->lde_namelen,
858 ent->lde_name, PFID(&fid),
859 PFID(lu_object_fid(&obj->do_lu)),
860 lod->lod_lmv_failout ? "failout" : "skip");
862 if (lod->lod_lmv_failout)
870 if (ent->lde_name[len] < '0' ||
871 ent->lde_name[len] > '9') {
872 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
873 "%s: invalid shard name %.*s with the "
874 "FID "DFID" for the striped directory "
876 lod2obd(lod)->obd_name, ent->lde_namelen,
877 ent->lde_name, PFID(&fid),
878 PFID(lu_object_fid(&obj->do_lu)),
879 lod->lod_lmv_failout ?
882 if (lod->lod_lmv_failout)
888 index = index * 10 + ent->lde_name[len++] - '0';
889 } while (len < ent->lde_namelen);
891 if (len == ent->lde_namelen) {
892 /* Out of LMV EA range. */
893 if (index >= stripes) {
894 CERROR("%s: the shard %.*s for the striped "
895 "directory "DFID" is out of the known "
896 "LMV EA range [0 - %u], failout\n",
897 lod2obd(lod)->obd_name, ent->lde_namelen,
899 PFID(lu_object_fid(&obj->do_lu)),
905 /* The slot has been occupied. */
906 if (!fid_is_zero(&lmv1->lmv_stripe_fids[index])) {
910 &lmv1->lmv_stripe_fids[index]);
911 CERROR("%s: both the shard "DFID" and "DFID
912 " for the striped directory "DFID
913 " claim the same LMV EA slot at the "
914 "index %d, failout\n",
915 lod2obd(lod)->obd_name,
916 PFID(&fid0), PFID(&fid),
917 PFID(lu_object_fid(&obj->do_lu)), index);
922 /* stored as LE mode */
923 lmv1->lmv_stripe_fids[index] = ent->lde_fid;
926 rc = iops->next(env, it);
933 RETURN(rc > 0 ? lmv_mds_md_size(stripes, magic) : rc);
937 * Implementation of dt_object_operations::do_index_try.
939 * \see dt_object_operations::do_index_try() in the API description for details.
941 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
942 const struct dt_index_features *feat)
944 struct lod_object *lo = lod_dt_obj(dt);
945 struct dt_object *next = dt_object_child(dt);
949 LASSERT(next->do_ops);
950 LASSERT(next->do_ops->do_index_try);
952 rc = lod_striping_load(env, lo);
956 rc = next->do_ops->do_index_try(env, next, feat);
960 if (lo->ldo_dir_stripe_count > 0) {
963 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
964 if (dt_object_exists(lo->ldo_stripe[i]) == 0)
966 rc = lo->ldo_stripe[i]->do_ops->do_index_try(env,
967 lo->ldo_stripe[i], feat);
971 dt->do_index_ops = &lod_striped_index_ops;
973 dt->do_index_ops = &lod_index_ops;
980 * Implementation of dt_object_operations::do_read_lock.
982 * \see dt_object_operations::do_read_lock() in the API description for details.
984 static void lod_read_lock(const struct lu_env *env, struct dt_object *dt,
987 dt_read_lock(env, dt_object_child(dt), role);
991 * Implementation of dt_object_operations::do_write_lock.
993 * \see dt_object_operations::do_write_lock() in the API description for
996 static void lod_write_lock(const struct lu_env *env, struct dt_object *dt,
999 dt_write_lock(env, dt_object_child(dt), role);
1003 * Implementation of dt_object_operations::do_read_unlock.
1005 * \see dt_object_operations::do_read_unlock() in the API description for
1008 static void lod_read_unlock(const struct lu_env *env, struct dt_object *dt)
1010 dt_read_unlock(env, dt_object_child(dt));
1014 * Implementation of dt_object_operations::do_write_unlock.
1016 * \see dt_object_operations::do_write_unlock() in the API description for
1019 static void lod_write_unlock(const struct lu_env *env, struct dt_object *dt)
1021 dt_write_unlock(env, dt_object_child(dt));
1025 * Implementation of dt_object_operations::do_write_locked.
1027 * \see dt_object_operations::do_write_locked() in the API description for
1030 static int lod_write_locked(const struct lu_env *env, struct dt_object *dt)
1032 return dt_write_locked(env, dt_object_child(dt));
1036 * Implementation of dt_object_operations::do_attr_get.
1038 * \see dt_object_operations::do_attr_get() in the API description for details.
1040 static int lod_attr_get(const struct lu_env *env,
1041 struct dt_object *dt,
1042 struct lu_attr *attr)
1044 /* Note: for striped directory, client will merge attributes
1045 * from all of the sub-stripes see lmv_merge_attr(), and there
1046 * no MDD logic depend on directory nlink/size/time, so we can
1047 * always use master inode nlink and size for now. */
1048 return dt_attr_get(env, dt_object_child(dt), attr);
1051 static inline void lod_adjust_stripe_info(struct lod_layout_component *comp,
1052 struct lov_desc *desc)
1054 if (comp->llc_pattern != LOV_PATTERN_MDT) {
1055 if (!comp->llc_stripe_count)
1056 comp->llc_stripe_count =
1057 desc->ld_default_stripe_count;
1059 if (comp->llc_stripe_size <= 0)
1060 comp->llc_stripe_size = desc->ld_default_stripe_size;
1063 int lod_obj_for_each_stripe(const struct lu_env *env, struct lod_object *lo,
1065 struct lod_obj_stripe_cb_data *data)
1067 struct lod_layout_component *lod_comp;
1071 LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
1072 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1073 lod_comp = &lo->ldo_comp_entries[i];
1075 if (lod_comp->llc_stripe == NULL)
1078 /* has stripe but not inited yet, this component has been
1079 * declared to be created, but hasn't created yet.
1081 if (!lod_comp_inited(lod_comp))
1084 if (data->locd_comp_skip_cb &&
1085 data->locd_comp_skip_cb(env, lo, i, data))
1088 if (data->locd_comp_cb) {
1089 rc = data->locd_comp_cb(env, lo, i, data);
1094 /* could used just to do sth about component, not each
1097 if (!data->locd_stripe_cb)
1100 LASSERT(lod_comp->llc_stripe_count > 0);
1101 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
1102 struct dt_object *dt = lod_comp->llc_stripe[j];
1106 rc = data->locd_stripe_cb(env, lo, dt, th, i, j, data);
1114 static bool lod_obj_attr_set_comp_skip_cb(const struct lu_env *env,
1115 struct lod_object *lo, int comp_idx,
1116 struct lod_obj_stripe_cb_data *data)
1118 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
1119 bool skipped = false;
1121 if (!(data->locd_attr->la_valid & LA_LAYOUT_VERSION))
1124 switch (lo->ldo_flr_state) {
1125 case LCM_FL_WRITE_PENDING: {
1128 /* skip stale components */
1129 if (lod_comp->llc_flags & LCME_FL_STALE) {
1134 /* skip valid and overlapping components, therefore any
1135 * attempts to write overlapped components will never succeed
1136 * because client will get EINPROGRESS. */
1137 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1141 if (lo->ldo_comp_entries[i].llc_flags & LCME_FL_STALE)
1144 if (lu_extent_is_overlapped(&lod_comp->llc_extent,
1145 &lo->ldo_comp_entries[i].llc_extent)) {
1153 LASSERTF(0, "impossible: %d\n", lo->ldo_flr_state);
1154 case LCM_FL_SYNC_PENDING:
1158 CDEBUG(D_LAYOUT, DFID": %s to set component %x to version: %u\n",
1159 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
1160 skipped ? "skipped" : "chose", lod_comp->llc_id,
1161 data->locd_attr->la_layout_version);
1167 lod_obj_stripe_attr_set_cb(const struct lu_env *env, struct lod_object *lo,
1168 struct dt_object *dt, struct thandle *th,
1169 int comp_idx, int stripe_idx,
1170 struct lod_obj_stripe_cb_data *data)
1172 if (data->locd_declare)
1173 return lod_sub_declare_attr_set(env, dt, data->locd_attr, th);
1175 if (data->locd_attr->la_valid & LA_LAYOUT_VERSION) {
1176 CDEBUG(D_LAYOUT, DFID": set layout version: %u, comp_idx: %d\n",
1177 PFID(lu_object_fid(&dt->do_lu)),
1178 data->locd_attr->la_layout_version, comp_idx);
1181 return lod_sub_attr_set(env, dt, data->locd_attr, th);
1185 * Implementation of dt_object_operations::do_declare_attr_set.
1187 * If the object is striped, then apply the changes to all the stripes.
1189 * \see dt_object_operations::do_declare_attr_set() in the API description
1192 static int lod_declare_attr_set(const struct lu_env *env,
1193 struct dt_object *dt,
1194 const struct lu_attr *attr,
1197 struct dt_object *next = dt_object_child(dt);
1198 struct lod_object *lo = lod_dt_obj(dt);
1203 * declare setattr on the local object
1205 rc = lod_sub_declare_attr_set(env, next, attr, th);
1209 /* osp_declare_attr_set() ignores all attributes other than
1210 * UID, GID, PROJID, and size, and osp_attr_set() ignores all
1211 * but UID, GID and PROJID. Declaration of size attr setting
1212 * happens through lod_declare_init_size(), and not through
1213 * this function. Therefore we need not load striping unless
1214 * ownership is changing. This should save memory and (we hope)
1215 * speed up rename().
1217 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1218 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1221 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1224 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID | LA_MODE |
1225 LA_ATIME | LA_MTIME | LA_CTIME |
1230 * load striping information, notice we don't do this when object
1231 * is being initialized as we don't need this information till
1232 * few specific cases like destroy, chown
1234 rc = lod_striping_load(env, lo);
1238 if (!lod_obj_is_striped(dt))
1242 * if object is striped declare changes on the stripes
1244 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1245 LASSERT(lo->ldo_stripe);
1246 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1247 if (lo->ldo_stripe[i] == NULL)
1249 rc = lod_sub_declare_attr_set(env, lo->ldo_stripe[i],
1255 struct lod_obj_stripe_cb_data data = { { 0 } };
1257 data.locd_attr = attr;
1258 data.locd_declare = true;
1259 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1260 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1266 if (!dt_object_exists(next) || dt_object_remote(next) ||
1267 !S_ISREG(attr->la_mode))
1270 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1271 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
1275 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1276 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1277 struct lod_thread_info *info = lod_env_info(env);
1278 struct lu_buf *buf = &info->lti_buf;
1280 buf->lb_buf = info->lti_ea_store;
1281 buf->lb_len = info->lti_ea_store_size;
1282 rc = lod_sub_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1283 LU_XATTR_REPLACE, th);
1290 * Implementation of dt_object_operations::do_attr_set.
1292 * If the object is striped, then apply the changes to all or subset of
1293 * the stripes depending on the object type and specific attributes.
1295 * \see dt_object_operations::do_attr_set() in the API description for details.
1297 static int lod_attr_set(const struct lu_env *env,
1298 struct dt_object *dt,
1299 const struct lu_attr *attr,
1302 struct dt_object *next = dt_object_child(dt);
1303 struct lod_object *lo = lod_dt_obj(dt);
1308 * apply changes to the local object
1310 rc = lod_sub_attr_set(env, next, attr, th);
1314 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1315 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1318 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1321 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE | LA_PROJID |
1322 LA_ATIME | LA_MTIME | LA_CTIME |
1327 /* FIXME: a tricky case in the code path of mdd_layout_change():
1328 * the in-memory striping information has been freed in lod_xattr_set()
1329 * due to layout change. It has to load stripe here again. It only
1330 * changes flags of layout so declare_attr_set() is still accurate */
1331 rc = lod_striping_load(env, lo);
1335 if (!lod_obj_is_striped(dt))
1339 * if object is striped, apply changes to all the stripes
1341 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1342 LASSERT(lo->ldo_stripe);
1343 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1344 if (unlikely(lo->ldo_stripe[i] == NULL))
1347 if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1350 rc = lod_sub_attr_set(env, lo->ldo_stripe[i], attr, th);
1355 struct lod_obj_stripe_cb_data data = { { 0 } };
1357 data.locd_attr = attr;
1358 data.locd_declare = false;
1359 data.locd_comp_skip_cb = lod_obj_attr_set_comp_skip_cb;
1360 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1361 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1367 if (!dt_object_exists(next) || dt_object_remote(next) ||
1368 !S_ISREG(attr->la_mode))
1371 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1372 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
1376 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1377 struct lod_thread_info *info = lod_env_info(env);
1378 struct lu_buf *buf = &info->lti_buf;
1379 struct ost_id *oi = &info->lti_ostid;
1380 struct lu_fid *fid = &info->lti_fid;
1381 struct lov_mds_md_v1 *lmm;
1382 struct lov_ost_data_v1 *objs;
1385 rc = lod_get_lov_ea(env, lo);
1389 buf->lb_buf = info->lti_ea_store;
1390 buf->lb_len = info->lti_ea_store_size;
1391 lmm = info->lti_ea_store;
1392 magic = le32_to_cpu(lmm->lmm_magic);
1393 if (magic == LOV_MAGIC_COMP_V1) {
1394 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1395 struct lov_comp_md_entry_v1 *lcme =
1396 &lcm->lcm_entries[0];
1398 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1399 magic = le32_to_cpu(lmm->lmm_magic);
1402 if (magic == LOV_MAGIC_V1)
1403 objs = &(lmm->lmm_objects[0]);
1405 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1406 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1407 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1409 fid_to_ostid(fid, oi);
1410 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1412 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1413 LU_XATTR_REPLACE, th);
1414 } else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1415 struct lod_thread_info *info = lod_env_info(env);
1416 struct lu_buf *buf = &info->lti_buf;
1417 struct lov_comp_md_v1 *lcm;
1418 struct lov_comp_md_entry_v1 *lcme;
1420 rc = lod_get_lov_ea(env, lo);
1424 buf->lb_buf = info->lti_ea_store;
1425 buf->lb_len = info->lti_ea_store_size;
1427 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
1430 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1431 lcme = &lcm->lcm_entries[0];
1432 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1433 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1435 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1436 LU_XATTR_REPLACE, th);
1443 * Implementation of dt_object_operations::do_xattr_get.
1445 * If LOV EA is requested from the root object and it's not
1446 * found, then return default striping for the filesystem.
1448 * \see dt_object_operations::do_xattr_get() in the API description for details.
1450 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1451 struct lu_buf *buf, const char *name)
1453 struct lod_thread_info *info = lod_env_info(env);
1454 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1459 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1460 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1461 struct lmv_mds_md_v1 *lmv1;
1464 if (rc > (typeof(rc))sizeof(*lmv1))
1467 if (rc < (typeof(rc))sizeof(*lmv1))
1468 RETURN(rc = rc > 0 ? -EINVAL : rc);
1470 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1471 CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1473 info->lti_buf.lb_buf = info->lti_key;
1474 info->lti_buf.lb_len = sizeof(*lmv1);
1475 rc = dt_xattr_get(env, dt_object_child(dt),
1476 &info->lti_buf, name);
1477 if (unlikely(rc != sizeof(*lmv1)))
1478 RETURN(rc = rc > 0 ? -EINVAL : rc);
1480 lmv1 = info->lti_buf.lb_buf;
1481 /* The on-disk LMV EA only contains header, but the
1482 * returned LMV EA size should contain the space for
1483 * the FIDs of all shards of the striped directory. */
1484 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1485 rc = lmv_mds_md_size(
1486 le32_to_cpu(lmv1->lmv_stripe_count),
1489 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1493 RETURN(rc = rc1 != 0 ? rc1 : rc);
1496 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1500 * XXX: Only used by lfsck
1502 * lod returns default striping on the real root of the device
1503 * this is like the root stores default striping for the whole
1504 * filesystem. historically we've been using a different approach
1505 * and store it in the config.
1507 dt_root_get(env, dev->lod_child, &info->lti_fid);
1508 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1510 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1511 struct lov_user_md *lum = buf->lb_buf;
1512 struct lov_desc *desc = &dev->lod_desc;
1514 if (buf->lb_buf == NULL) {
1516 } else if (buf->lb_len >= sizeof(*lum)) {
1517 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1518 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1519 lmm_oi_set_id(&lum->lmm_oi, 0);
1520 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1521 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1522 lum->lmm_stripe_size = cpu_to_le32(
1523 desc->ld_default_stripe_size);
1524 lum->lmm_stripe_count = cpu_to_le16(
1525 desc->ld_default_stripe_count);
1526 lum->lmm_stripe_offset = cpu_to_le16(
1527 desc->ld_default_stripe_offset);
1540 * Checks that the magic of the stripe is sane.
1542 * \param[in] lod lod device
1543 * \param[in] lum a buffer storing LMV EA to verify
1545 * \retval 0 if the EA is sane
1546 * \retval negative otherwise
1548 static int lod_verify_md_striping(struct lod_device *lod,
1549 const struct lmv_user_md_v1 *lum)
1551 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1552 CERROR("%s: invalid lmv_user_md: magic = %x, "
1553 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1554 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1555 (int)le32_to_cpu(lum->lum_stripe_offset),
1556 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1564 * Initialize LMV EA for a slave.
1566 * Initialize slave's LMV EA from the master's LMV EA.
1568 * \param[in] master_lmv a buffer containing master's EA
1569 * \param[out] slave_lmv a buffer where slave's EA will be stored
1572 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1573 const struct lmv_mds_md_v1 *master_lmv)
1575 *slave_lmv = *master_lmv;
1576 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1582 * Generate LMV EA from the object passed as \a dt. The object must have
1583 * the stripes created and initialized.
1585 * \param[in] env execution environment
1586 * \param[in] dt object
1587 * \param[out] lmv_buf buffer storing generated LMV EA
1589 * \retval 0 on success
1590 * \retval negative if failed
1592 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1593 struct lu_buf *lmv_buf)
1595 struct lod_thread_info *info = lod_env_info(env);
1596 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1597 struct lod_object *lo = lod_dt_obj(dt);
1598 struct lmv_mds_md_v1 *lmm1;
1600 int type = LU_SEQ_RANGE_ANY;
1605 LASSERT(lo->ldo_dir_striped != 0);
1606 LASSERT(lo->ldo_dir_stripe_count > 0);
1607 stripe_count = lo->ldo_dir_stripe_count;
1608 /* Only store the LMV EA heahder on the disk. */
1609 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1610 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1614 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1617 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1618 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1619 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1620 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1621 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1626 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1627 lmv_buf->lb_buf = info->lti_ea_store;
1628 lmv_buf->lb_len = sizeof(*lmm1);
1634 * Create in-core represenation for a striped directory.
1636 * Parse the buffer containing LMV EA and instantiate LU objects
1637 * representing the stripe objects. The pointers to the objects are
1638 * stored in ldo_stripe field of \a lo. This function is used when
1639 * we need to access an already created object (i.e. load from a disk).
1641 * \param[in] env execution environment
1642 * \param[in] lo lod object
1643 * \param[in] buf buffer containing LMV EA
1645 * \retval 0 on success
1646 * \retval negative if failed
1648 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1649 const struct lu_buf *buf)
1651 struct lod_thread_info *info = lod_env_info(env);
1652 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1653 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1654 struct dt_object **stripe;
1655 union lmv_mds_md *lmm = buf->lb_buf;
1656 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1657 struct lu_fid *fid = &info->lti_fid;
1662 LASSERT(mutex_is_locked(&lo->ldo_layout_mutex));
1664 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1667 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1668 lo->ldo_dir_slave_stripe = 1;
1672 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1675 if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1678 LASSERT(lo->ldo_stripe == NULL);
1679 OBD_ALLOC(stripe, sizeof(stripe[0]) *
1680 (le32_to_cpu(lmv1->lmv_stripe_count)));
1684 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1685 struct dt_device *tgt_dt;
1686 struct dt_object *dto;
1687 int type = LU_SEQ_RANGE_ANY;
1690 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1691 if (!fid_is_sane(fid))
1692 GOTO(out, rc = -ESTALE);
1694 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1698 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1699 tgt_dt = lod->lod_child;
1701 struct lod_tgt_desc *tgt;
1703 tgt = LTD_TGT(ltd, idx);
1705 GOTO(out, rc = -ESTALE);
1706 tgt_dt = tgt->ltd_tgt;
1709 dto = dt_locate_at(env, tgt_dt, fid,
1710 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1713 GOTO(out, rc = PTR_ERR(dto));
1718 lo->ldo_stripe = stripe;
1719 lo->ldo_dir_stripe_count = le32_to_cpu(lmv1->lmv_stripe_count);
1720 lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1722 lod_striping_free_nolock(env, lo);
1728 * Declare create a striped directory.
1730 * Declare creating a striped directory with a given stripe pattern on the
1731 * specified MDTs. A striped directory is represented as a regular directory
1732 * - an index listing all the stripes. The stripes point back to the master
1733 * object with ".." and LinkEA. The master object gets LMV EA which
1734 * identifies it as a striped directory. The function allocates FIDs
1737 * \param[in] env execution environment
1738 * \param[in] dt object
1739 * \param[in] attr attributes to initialize the objects with
1740 * \param[in] dof type of objects to be created
1741 * \param[in] th transaction handle
1743 * \retval 0 on success
1744 * \retval negative if failed
1746 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1747 struct dt_object *dt,
1748 struct lu_attr *attr,
1749 struct dt_object_format *dof,
1752 struct lod_thread_info *info = lod_env_info(env);
1753 struct lu_buf lmv_buf;
1754 struct lu_buf slave_lmv_buf;
1755 struct lmv_mds_md_v1 *lmm;
1756 struct lmv_mds_md_v1 *slave_lmm = NULL;
1757 struct dt_insert_rec *rec = &info->lti_dt_rec;
1758 struct lod_object *lo = lod_dt_obj(dt);
1763 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1766 lmm = lmv_buf.lb_buf;
1768 OBD_ALLOC_PTR(slave_lmm);
1769 if (slave_lmm == NULL)
1770 GOTO(out, rc = -ENOMEM);
1772 lod_prep_slave_lmv_md(slave_lmm, lmm);
1773 slave_lmv_buf.lb_buf = slave_lmm;
1774 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1776 if (!dt_try_as_dir(env, dt_object_child(dt)))
1777 GOTO(out, rc = -EINVAL);
1779 rec->rec_type = S_IFDIR;
1780 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1781 struct dt_object *dto = lo->ldo_stripe[i];
1782 char *stripe_name = info->lti_key;
1783 struct lu_name *sname;
1784 struct linkea_data ldata = { NULL };
1785 struct lu_buf linkea_buf;
1787 rc = lod_sub_declare_create(env, dto, attr, NULL, dof, th);
1791 if (!dt_try_as_dir(env, dto))
1792 GOTO(out, rc = -EINVAL);
1794 rc = lod_sub_declare_ref_add(env, dto, th);
1798 rec->rec_fid = lu_object_fid(&dto->do_lu);
1799 rc = lod_sub_declare_insert(env, dto,
1800 (const struct dt_rec *)rec,
1801 (const struct dt_key *)dot, th);
1805 /* master stripe FID will be put to .. */
1806 rec->rec_fid = lu_object_fid(&dt->do_lu);
1807 rc = lod_sub_declare_insert(env, dto,
1808 (const struct dt_rec *)rec,
1809 (const struct dt_key *)dotdot, th);
1813 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1814 cfs_fail_val != i) {
1815 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1817 slave_lmm->lmv_master_mdt_index =
1820 slave_lmm->lmv_master_mdt_index =
1822 rc = lod_sub_declare_xattr_set(env, dto, &slave_lmv_buf,
1823 XATTR_NAME_LMV, 0, th);
1828 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1830 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1831 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1833 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1834 PFID(lu_object_fid(&dto->do_lu)), i);
1836 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1837 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1838 sname, lu_object_fid(&dt->do_lu));
1842 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1843 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1844 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
1845 XATTR_NAME_LINK, 0, th);
1849 rec->rec_fid = lu_object_fid(&dto->do_lu);
1850 rc = lod_sub_declare_insert(env, dt_object_child(dt),
1851 (const struct dt_rec *)rec,
1852 (const struct dt_key *)stripe_name,
1857 rc = lod_sub_declare_ref_add(env, dt_object_child(dt), th);
1862 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt),
1863 &lmv_buf, XATTR_NAME_LMV, 0, th);
1867 if (slave_lmm != NULL)
1868 OBD_FREE_PTR(slave_lmm);
1873 static int lod_prep_md_striped_create(const struct lu_env *env,
1874 struct dt_object *dt,
1875 struct lu_attr *attr,
1876 const struct lmv_user_md_v1 *lum,
1877 struct dt_object_format *dof,
1880 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1881 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1882 struct lod_object *lo = lod_dt_obj(dt);
1883 struct dt_object **stripe;
1890 bool is_specific = false;
1893 /* The lum has been verifed in lod_verify_md_striping */
1894 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC ||
1895 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC);
1896 LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1898 stripe_count = le32_to_cpu(lum->lum_stripe_count);
1900 OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1901 if (idx_array == NULL)
1904 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1906 GOTO(out_free, rc = -ENOMEM);
1908 /* Start index must be the master MDT */
1909 master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1910 idx_array[0] = master_index;
1911 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) {
1913 for (i = 1; i < stripe_count; i++)
1914 idx_array[i] = le32_to_cpu(lum->lum_objects[i].lum_mds);
1917 for (i = 0; i < stripe_count; i++) {
1918 struct lod_tgt_desc *tgt = NULL;
1919 struct dt_object *dto;
1920 struct lu_fid fid = { 0 };
1922 struct lu_object_conf conf = { 0 };
1923 struct dt_device *tgt_dt = NULL;
1925 /* Try to find next avaible target */
1927 for (j = 0; j < lod->lod_remote_mdt_count;
1928 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1929 bool already_allocated = false;
1932 CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
1933 idx, lod->lod_remote_mdt_count + 1, i);
1935 if (likely(!is_specific &&
1936 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
1937 /* check whether the idx already exists
1938 * in current allocated array */
1939 for (k = 0; k < i; k++) {
1940 if (idx_array[k] == idx) {
1941 already_allocated = true;
1946 if (already_allocated)
1950 /* Sigh, this index is not in the bitmap, let's check
1951 * next available target */
1952 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) &&
1953 idx != master_index)
1956 if (idx == master_index) {
1957 /* Allocate the FID locally */
1958 rc = obd_fid_alloc(env, lod->lod_child_exp,
1962 tgt_dt = lod->lod_child;
1966 /* check the status of the OSP */
1967 tgt = LTD_TGT(ltd, idx);
1971 tgt_dt = tgt->ltd_tgt;
1972 rc = dt_statfs(env, tgt_dt, NULL);
1974 /* this OSP doesn't feel well */
1979 rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1988 /* Can not allocate more stripes */
1989 if (j == lod->lod_remote_mdt_count) {
1990 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1991 lod2obd(lod)->obd_name, stripe_count, i);
1995 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
1996 idx, i, PFID(&fid));
1998 /* Set the start index for next stripe allocation */
1999 if (!is_specific && i < stripe_count - 1) {
2001 * for large dir test, put all other slaves on one
2002 * remote MDT, otherwise we may save too many local
2003 * slave locks which will exceed RS_MAX_LOCKS.
2005 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)))
2007 idx_array[i + 1] = (idx + 1) %
2008 (lod->lod_remote_mdt_count + 1);
2010 /* tgt_dt and fid must be ready after search avaible OSP
2011 * in the above loop */
2012 LASSERT(tgt_dt != NULL);
2013 LASSERT(fid_is_sane(&fid));
2014 conf.loc_flags = LOC_F_NEW;
2015 dto = dt_locate_at(env, tgt_dt, &fid,
2016 dt->do_lu.lo_dev->ld_site->ls_top_dev,
2019 GOTO(out_put, rc = PTR_ERR(dto));
2023 lo->ldo_dir_striped = 1;
2024 lo->ldo_stripe = stripe;
2025 lo->ldo_dir_stripe_count = i;
2026 lo->ldo_dir_stripes_allocated = stripe_count;
2028 lo->ldo_dir_stripe_loaded = 1;
2030 if (lo->ldo_dir_stripe_count == 0)
2031 GOTO(out_put, rc = -ENOSPC);
2033 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
2039 for (i = 0; i < stripe_count; i++)
2040 if (stripe[i] != NULL)
2041 dt_object_put(env, stripe[i]);
2042 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
2043 lo->ldo_dir_stripe_count = 0;
2044 lo->ldo_dir_stripes_allocated = 0;
2045 lo->ldo_stripe = NULL;
2049 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
2055 * Declare create striped md object.
2057 * The function declares intention to create a striped directory. This is a
2058 * wrapper for lod_prep_md_striped_create(). The only additional functionality
2059 * is to verify pattern \a lum_buf is good. Check that function for the details.
2061 * \param[in] env execution environment
2062 * \param[in] dt object
2063 * \param[in] attr attributes to initialize the objects with
2064 * \param[in] lum_buf a pattern specifying the number of stripes and
2066 * \param[in] dof type of objects to be created
2067 * \param[in] th transaction handle
2069 * \retval 0 on success
2070 * \retval negative if failed
2073 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
2074 struct dt_object *dt,
2075 struct lu_attr *attr,
2076 const struct lu_buf *lum_buf,
2077 struct dt_object_format *dof,
2080 struct lod_object *lo = lod_dt_obj(dt);
2081 struct lmv_user_md_v1 *lum = lum_buf->lb_buf;
2085 LASSERT(lum != NULL);
2087 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
2088 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
2089 (int)le32_to_cpu(lum->lum_stripe_offset));
2091 if (lo->ldo_dir_stripe_count == 0)
2094 /* prepare dir striped objects */
2095 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2097 /* failed to create striping, let's reset
2098 * config so that others don't get confused */
2099 lod_striping_free(env, lo);
2107 * Implementation of dt_object_operations::do_declare_xattr_set.
2109 * Used with regular (non-striped) objects. Basically it
2110 * initializes the striping information and applies the
2111 * change to all the stripes.
2113 * \see dt_object_operations::do_declare_xattr_set() in the API description
2116 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2117 struct dt_object *dt,
2118 const struct lu_buf *buf,
2119 const char *name, int fl,
2122 struct dt_object *next = dt_object_child(dt);
2123 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2124 struct lod_object *lo = lod_dt_obj(dt);
2129 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2130 struct lmv_user_md_v1 *lum;
2132 LASSERT(buf != NULL && buf->lb_buf != NULL);
2134 rc = lod_verify_md_striping(d, lum);
2137 } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2138 rc = lod_verify_striping(d, lo, buf, false);
2143 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2147 /* Note: Do not set LinkEA on sub-stripes, otherwise
2148 * it will confuse the fid2path process(see mdt_path_current()).
2149 * The linkEA between master and sub-stripes is set in
2150 * lod_xattr_set_lmv(). */
2151 if (strcmp(name, XATTR_NAME_LINK) == 0)
2154 /* set xattr to each stripes, if needed */
2155 rc = lod_striping_load(env, lo);
2159 if (lo->ldo_dir_stripe_count == 0)
2162 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2163 LASSERT(lo->ldo_stripe[i]);
2165 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2175 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2176 struct lod_object *lo,
2177 struct dt_object *dt, struct thandle *th,
2178 int comp_idx, int stripe_idx,
2179 struct lod_obj_stripe_cb_data *data)
2181 struct lod_thread_info *info = lod_env_info(env);
2182 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
2183 struct filter_fid *ff = &info->lti_ff;
2184 struct lu_buf *buf = &info->lti_buf;
2188 buf->lb_len = sizeof(*ff);
2189 rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2196 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
2197 if (lu_fid_eq(lu_object_fid(&lo->ldo_obj.do_lu), &ff->ff_parent) &&
2198 ff->ff_layout.ol_comp_id == comp->llc_id)
2201 /* rewrite filter_fid */
2202 memset(ff, 0, sizeof(*ff));
2203 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2204 ff->ff_parent.f_ver = stripe_idx;
2205 ff->ff_layout.ol_stripe_size = comp->llc_stripe_size;
2206 ff->ff_layout.ol_stripe_count = comp->llc_stripe_count;
2207 ff->ff_layout.ol_comp_id = comp->llc_id;
2208 ff->ff_layout.ol_comp_start = comp->llc_extent.e_start;
2209 ff->ff_layout.ol_comp_end = comp->llc_extent.e_end;
2210 filter_fid_cpu_to_le(ff, ff, sizeof(*ff));
2212 if (data->locd_declare)
2213 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2214 LU_XATTR_REPLACE, th);
2216 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2217 LU_XATTR_REPLACE, th);
2223 * Reset parent FID on OST object
2225 * Replace parent FID with @dt object FID, which is only called during migration
2226 * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2227 * the FID is changed.
2229 * \param[in] env execution environment
2230 * \param[in] dt dt_object whose stripes's parent FID will be reset
2231 * \parem[in] th thandle
2232 * \param[in] declare if it is declare
2234 * \retval 0 if reset succeeds
2235 * \retval negative errno if reset fails
2237 static int lod_replace_parent_fid(const struct lu_env *env,
2238 struct dt_object *dt,
2239 struct thandle *th, bool declare)
2241 struct lod_object *lo = lod_dt_obj(dt);
2242 struct lod_thread_info *info = lod_env_info(env);
2243 struct lu_buf *buf = &info->lti_buf;
2244 struct filter_fid *ff;
2245 struct lod_obj_stripe_cb_data data = { { 0 } };
2249 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2251 /* set xattr to each stripes, if needed */
2252 rc = lod_striping_load(env, lo);
2256 if (!lod_obj_is_striped(dt))
2259 if (info->lti_ea_store_size < sizeof(*ff)) {
2260 rc = lod_ea_store_resize(info, sizeof(*ff));
2265 buf->lb_buf = info->lti_ea_store;
2266 buf->lb_len = info->lti_ea_store_size;
2268 data.locd_declare = declare;
2269 data.locd_stripe_cb = lod_obj_stripe_replace_parent_fid_cb;
2270 rc = lod_obj_for_each_stripe(env, lo, th, &data);
2275 inline __u16 lod_comp_entry_stripe_count(struct lod_object *lo,
2276 struct lod_layout_component *entry,
2279 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2283 else if (lod_comp_inited(entry))
2284 return entry->llc_stripe_count;
2285 else if ((__u16)-1 == entry->llc_stripe_count)
2286 return lod->lod_desc.ld_tgt_count;
2288 return lod_get_stripe_count(lod, lo, entry->llc_stripe_count);
2291 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2293 int magic, size = 0, i;
2294 struct lod_layout_component *comp_entries;
2299 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2300 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2302 lo->ldo_def_striping->lds_def_striping_is_composite;
2304 comp_cnt = lo->ldo_comp_cnt;
2305 comp_entries = lo->ldo_comp_entries;
2306 is_composite = lo->ldo_is_composite;
2310 LASSERT(comp_cnt != 0 && comp_entries != NULL);
2312 size = sizeof(struct lov_comp_md_v1) +
2313 sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2314 LASSERT(size % sizeof(__u64) == 0);
2317 for (i = 0; i < comp_cnt; i++) {
2320 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2321 stripe_count = lod_comp_entry_stripe_count(lo, &comp_entries[i],
2323 if (!is_dir && is_composite)
2324 lod_comp_shrink_stripe_count(&comp_entries[i],
2327 size += lov_user_md_size(stripe_count, magic);
2328 LASSERT(size % sizeof(__u64) == 0);
2334 * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2335 * the xattr value is binary lov_comp_md_v1 which contains component(s)
2338 * \param[in] env execution environment
2339 * \param[in] dt dt_object to add components on
2340 * \param[in] buf buffer contains components to be added
2341 * \parem[in] th thandle
2343 * \retval 0 on success
2344 * \retval negative errno on failure
2346 static int lod_declare_layout_add(const struct lu_env *env,
2347 struct dt_object *dt,
2348 const struct lu_buf *buf,
2351 struct lod_thread_info *info = lod_env_info(env);
2352 struct lod_layout_component *comp_array, *lod_comp, *old_array;
2353 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2354 struct dt_object *next = dt_object_child(dt);
2355 struct lov_desc *desc = &d->lod_desc;
2356 struct lod_object *lo = lod_dt_obj(dt);
2357 struct lov_user_md_v3 *v3;
2358 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2360 int i, rc, array_cnt, old_array_cnt;
2363 LASSERT(lo->ldo_is_composite);
2365 if (lo->ldo_flr_state != LCM_FL_NONE)
2368 rc = lod_verify_striping(d, lo, buf, false);
2372 magic = comp_v1->lcm_magic;
2373 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2374 lustre_swab_lov_comp_md_v1(comp_v1);
2375 magic = comp_v1->lcm_magic;
2378 if (magic != LOV_USER_MAGIC_COMP_V1)
2381 array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2382 OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2383 if (comp_array == NULL)
2386 memcpy(comp_array, lo->ldo_comp_entries,
2387 sizeof(*comp_array) * lo->ldo_comp_cnt);
2389 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2390 struct lov_user_md_v1 *v1;
2391 struct lu_extent *ext;
2393 v1 = (struct lov_user_md *)((char *)comp_v1 +
2394 comp_v1->lcm_entries[i].lcme_offset);
2395 ext = &comp_v1->lcm_entries[i].lcme_extent;
2397 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2398 lod_comp->llc_extent.e_start = ext->e_start;
2399 lod_comp->llc_extent.e_end = ext->e_end;
2400 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2401 lod_comp->llc_flags = comp_v1->lcm_entries[i].lcme_flags;
2403 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2404 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2405 lod_adjust_stripe_info(lod_comp, desc);
2407 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2408 v3 = (struct lov_user_md_v3 *) v1;
2409 if (v3->lmm_pool_name[0] != '\0') {
2410 rc = lod_set_pool(&lod_comp->llc_pool,
2418 old_array = lo->ldo_comp_entries;
2419 old_array_cnt = lo->ldo_comp_cnt;
2421 lo->ldo_comp_entries = comp_array;
2422 lo->ldo_comp_cnt = array_cnt;
2424 /* No need to increase layout generation here, it will be increased
2425 * later when generating component ID for the new components */
2427 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2428 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2429 XATTR_NAME_LOV, 0, th);
2431 lo->ldo_comp_entries = old_array;
2432 lo->ldo_comp_cnt = old_array_cnt;
2436 OBD_FREE(old_array, sizeof(*lod_comp) * old_array_cnt);
2438 LASSERT(lo->ldo_mirror_count == 1);
2439 lo->ldo_mirrors[0].lme_end = array_cnt - 1;
2444 for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2445 lod_comp = &comp_array[i];
2446 if (lod_comp->llc_pool != NULL) {
2447 OBD_FREE(lod_comp->llc_pool,
2448 strlen(lod_comp->llc_pool) + 1);
2449 lod_comp->llc_pool = NULL;
2452 OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2457 * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2458 * the '$field' can only be 'flags' now. The xattr value is binary
2459 * lov_comp_md_v1 which contains the component ID(s) and the value of
2460 * the field to be modified.
2462 * \param[in] env execution environment
2463 * \param[in] dt dt_object to be modified
2464 * \param[in] op operation string, like "set.flags"
2465 * \param[in] buf buffer contains components to be set
2466 * \parem[in] th thandle
2468 * \retval 0 on success
2469 * \retval negative errno on failure
2471 static int lod_declare_layout_set(const struct lu_env *env,
2472 struct dt_object *dt,
2473 char *op, const struct lu_buf *buf,
2476 struct lod_layout_component *lod_comp;
2477 struct lod_thread_info *info = lod_env_info(env);
2478 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2479 struct lod_object *lo = lod_dt_obj(dt);
2480 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2483 bool changed = false;
2486 if (strcmp(op, "set.flags") != 0) {
2487 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2488 lod2obd(d)->obd_name, op);
2492 magic = comp_v1->lcm_magic;
2493 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2494 lustre_swab_lov_comp_md_v1(comp_v1);
2495 magic = comp_v1->lcm_magic;
2498 if (magic != LOV_USER_MAGIC_COMP_V1)
2501 if (comp_v1->lcm_entry_count == 0) {
2502 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2503 lod2obd(d)->obd_name);
2507 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2508 __u32 id = comp_v1->lcm_entries[i].lcme_id;
2509 __u32 flags = comp_v1->lcm_entries[i].lcme_flags;
2511 if (flags & LCME_FL_INIT) {
2513 lod_striping_free(env, lo);
2517 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2518 lod_comp = &lo->ldo_comp_entries[j];
2519 if (id != lod_comp->llc_id)
2522 if (flags & LCME_FL_NEG) {
2523 flags &= ~LCME_FL_NEG;
2524 lod_comp->llc_flags &= ~flags;
2526 lod_comp->llc_flags |= flags;
2533 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2534 lod2obd(d)->obd_name);
2538 lod_obj_inc_layout_gen(lo);
2540 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2541 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), &info->lti_buf,
2542 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
2547 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2548 * and the xattr value is a unique component ID or a special lcme_id.
2550 * \param[in] env execution environment
2551 * \param[in] dt dt_object to be operated on
2552 * \param[in] buf buffer contains component ID or lcme_id
2553 * \parem[in] th thandle
2555 * \retval 0 on success
2556 * \retval negative errno on failure
2558 static int lod_declare_layout_del(const struct lu_env *env,
2559 struct dt_object *dt,
2560 const struct lu_buf *buf,
2563 struct lod_thread_info *info = lod_env_info(env);
2564 struct dt_object *next = dt_object_child(dt);
2565 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2566 struct lod_object *lo = lod_dt_obj(dt);
2567 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2568 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2569 __u32 magic, id, flags, neg_flags = 0;
2573 LASSERT(lo->ldo_is_composite);
2575 if (lo->ldo_flr_state != LCM_FL_NONE)
2578 magic = comp_v1->lcm_magic;
2579 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2580 lustre_swab_lov_comp_md_v1(comp_v1);
2581 magic = comp_v1->lcm_magic;
2584 if (magic != LOV_USER_MAGIC_COMP_V1)
2587 id = comp_v1->lcm_entries[0].lcme_id;
2588 flags = comp_v1->lcm_entries[0].lcme_flags;
2590 if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
2591 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
2592 lod2obd(d)->obd_name, id, flags);
2596 if (id != LCME_ID_INVAL && flags != 0) {
2597 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
2598 lod2obd(d)->obd_name);
2602 if (id == LCME_ID_INVAL && !flags) {
2603 CDEBUG(D_LAYOUT, "%s: no id or flags specified.\n",
2604 lod2obd(d)->obd_name);
2608 if (flags & LCME_FL_NEG) {
2609 neg_flags = flags & ~LCME_FL_NEG;
2613 left = lo->ldo_comp_cnt;
2617 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
2618 struct lod_layout_component *lod_comp;
2620 lod_comp = &lo->ldo_comp_entries[i];
2622 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
2624 else if (flags && !(flags & lod_comp->llc_flags))
2626 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
2629 if (left != (i + 1)) {
2630 CDEBUG(D_LAYOUT, "%s: this deletion will create "
2631 "a hole.\n", lod2obd(d)->obd_name);
2636 /* Mark the component as deleted */
2637 lod_comp->llc_id = LCME_ID_INVAL;
2639 /* Not instantiated component */
2640 if (lod_comp->llc_stripe == NULL)
2643 LASSERT(lod_comp->llc_stripe_count > 0);
2644 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
2645 struct dt_object *obj = lod_comp->llc_stripe[j];
2649 rc = lod_sub_declare_destroy(env, obj, th);
2655 LASSERTF(left >= 0, "left = %d\n", left);
2656 if (left == lo->ldo_comp_cnt) {
2657 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
2658 lod2obd(d)->obd_name, id);
2662 memset(attr, 0, sizeof(*attr));
2663 attr->la_valid = LA_SIZE;
2664 rc = lod_sub_declare_attr_set(env, next, attr, th);
2669 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2670 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2671 XATTR_NAME_LOV, 0, th);
2673 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
2680 * Declare layout add/set/del operations issued by special xattr names:
2682 * XATTR_LUSTRE_LOV.add add component(s) to existing file
2683 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
2684 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
2686 * \param[in] env execution environment
2687 * \param[in] dt object
2688 * \param[in] name name of xattr
2689 * \param[in] buf lu_buf contains xattr value
2690 * \param[in] th transaction handle
2692 * \retval 0 on success
2693 * \retval negative if failed
2695 static int lod_declare_modify_layout(const struct lu_env *env,
2696 struct dt_object *dt,
2698 const struct lu_buf *buf,
2701 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2702 struct lod_object *lo = lod_dt_obj(dt);
2704 int rc, len = strlen(XATTR_LUSTRE_LOV);
2707 LASSERT(dt_object_exists(dt));
2709 if (strlen(name) <= len || name[len] != '.') {
2710 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
2711 lod2obd(d)->obd_name, name);
2716 rc = lod_striping_load(env, lo);
2720 /* the layout to be modified must be a composite layout */
2721 if (!lo->ldo_is_composite) {
2722 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
2723 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
2724 GOTO(unlock, rc = -EINVAL);
2727 op = (char *)name + len;
2728 if (strcmp(op, "add") == 0) {
2729 rc = lod_declare_layout_add(env, dt, buf, th);
2730 } else if (strcmp(op, "del") == 0) {
2731 rc = lod_declare_layout_del(env, dt, buf, th);
2732 } else if (strncmp(op, "set", strlen("set")) == 0) {
2733 rc = lod_declare_layout_set(env, dt, op, buf, th);
2735 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
2736 lod2obd(d)->obd_name, name);
2737 GOTO(unlock, rc = -ENOTSUPP);
2741 lod_striping_free(env, lo);
2747 * Convert a plain file lov_mds_md to a composite layout.
2749 * \param[in,out] info the thread info::lti_ea_store buffer contains little
2750 * endian plain file layout
2752 * \retval 0 on success, <0 on failure
2754 static int lod_layout_convert(struct lod_thread_info *info)
2756 struct lov_mds_md *lmm = info->lti_ea_store;
2757 struct lov_mds_md *lmm_save;
2758 struct lov_comp_md_v1 *lcm;
2759 struct lov_comp_md_entry_v1 *lcme;
2765 /* realloc buffer to a composite layout which contains one component */
2766 blob_size = lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
2767 le32_to_cpu(lmm->lmm_magic));
2768 size = sizeof(*lcm) + sizeof(*lcme) + blob_size;
2770 OBD_ALLOC_LARGE(lmm_save, blob_size);
2772 GOTO(out, rc = -ENOMEM);
2774 memcpy(lmm_save, lmm, blob_size);
2776 if (info->lti_ea_store_size < size) {
2777 rc = lod_ea_store_resize(info, size);
2782 lcm = info->lti_ea_store;
2783 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
2784 lcm->lcm_size = cpu_to_le32(size);
2785 lcm->lcm_layout_gen = cpu_to_le32(le16_to_cpu(
2786 lmm_save->lmm_layout_gen));
2787 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
2788 lcm->lcm_entry_count = cpu_to_le16(1);
2789 lcm->lcm_mirror_count = 0;
2791 lcme = &lcm->lcm_entries[0];
2792 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
2793 lcme->lcme_extent.e_start = 0;
2794 lcme->lcme_extent.e_end = cpu_to_le64(OBD_OBJECT_EOF);
2795 lcme->lcme_offset = cpu_to_le32(sizeof(*lcm) + sizeof(*lcme));
2796 lcme->lcme_size = cpu_to_le32(blob_size);
2798 memcpy((char *)lcm + lcme->lcme_offset, (char *)lmm_save, blob_size);
2803 OBD_FREE_LARGE(lmm_save, blob_size);
2808 * Merge layouts to form a mirrored file.
2810 static int lod_declare_layout_merge(const struct lu_env *env,
2811 struct dt_object *dt, const struct lu_buf *mbuf,
2814 struct lod_thread_info *info = lod_env_info(env);
2815 struct lu_buf *buf = &info->lti_buf;
2816 struct lod_object *lo = lod_dt_obj(dt);
2817 struct lov_comp_md_v1 *lcm;
2818 struct lov_comp_md_v1 *cur_lcm;
2819 struct lov_comp_md_v1 *merge_lcm;
2820 struct lov_comp_md_entry_v1 *lcme;
2823 __u16 cur_entry_count;
2824 __u16 merge_entry_count;
2826 __u16 mirror_id = 0;
2831 merge_lcm = mbuf->lb_buf;
2832 if (mbuf->lb_len < sizeof(*merge_lcm))
2835 /* must be an existing layout from disk */
2836 if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
2839 merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
2841 /* do not allow to merge two mirrored files */
2842 if (le16_to_cpu(merge_lcm->lcm_mirror_count))
2845 /* verify the target buffer */
2846 rc = lod_get_lov_ea(env, lo);
2848 RETURN(rc ? : -ENODATA);
2850 cur_lcm = info->lti_ea_store;
2851 switch (le32_to_cpu(cur_lcm->lcm_magic)) {
2854 rc = lod_layout_convert(info);
2856 case LOV_MAGIC_COMP_V1:
2865 /* info->lti_ea_store could be reallocated in lod_layout_convert() */
2866 cur_lcm = info->lti_ea_store;
2867 cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
2869 /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
2870 mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
2871 if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
2874 /* size of new layout */
2875 size = le32_to_cpu(cur_lcm->lcm_size) +
2876 le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
2878 memset(buf, 0, sizeof(*buf));
2879 lu_buf_alloc(buf, size);
2880 if (buf->lb_buf == NULL)
2884 memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
2886 offset = sizeof(*lcm) +
2887 sizeof(*lcme) * (cur_entry_count + merge_entry_count);
2888 for (i = 0; i < cur_entry_count; i++) {
2889 struct lov_comp_md_entry_v1 *cur_lcme;
2891 lcme = &lcm->lcm_entries[i];
2892 cur_lcme = &cur_lcm->lcm_entries[i];
2894 lcme->lcme_offset = cpu_to_le32(offset);
2895 memcpy((char *)lcm + offset,
2896 (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
2897 le32_to_cpu(lcme->lcme_size));
2899 offset += le32_to_cpu(lcme->lcme_size);
2901 if (mirror_count == 1 &&
2902 mirror_id_of(le32_to_cpu(lcme->lcme_id)) == 0) {
2903 /* Add mirror from a non-flr file, create new mirror ID.
2904 * Otherwise, keep existing mirror's component ID, used
2905 * for mirror extension.
2907 id = pflr_id(1, i + 1);
2908 lcme->lcme_id = cpu_to_le32(id);
2911 id = MAX(le32_to_cpu(lcme->lcme_id), id);
2914 mirror_id = mirror_id_of(id) + 1;
2915 for (i = 0; i < merge_entry_count; i++) {
2916 struct lov_comp_md_entry_v1 *merge_lcme;
2918 merge_lcme = &merge_lcm->lcm_entries[i];
2919 lcme = &lcm->lcm_entries[cur_entry_count + i];
2921 *lcme = *merge_lcme;
2922 lcme->lcme_offset = cpu_to_le32(offset);
2924 id = pflr_id(mirror_id, i + 1);
2925 lcme->lcme_id = cpu_to_le32(id);
2927 memcpy((char *)lcm + offset,
2928 (char *)merge_lcm + le32_to_cpu(merge_lcme->lcme_offset),
2929 le32_to_cpu(lcme->lcme_size));
2931 offset += le32_to_cpu(lcme->lcme_size);
2934 /* fixup layout information */
2935 lod_obj_inc_layout_gen(lo);
2936 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
2937 lcm->lcm_size = cpu_to_le32(size);
2938 lcm->lcm_entry_count = cpu_to_le16(cur_entry_count + merge_entry_count);
2939 lcm->lcm_mirror_count = cpu_to_le16(mirror_count);
2940 if ((le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK) == LCM_FL_NONE)
2941 lcm->lcm_flags = cpu_to_le32(LCM_FL_RDONLY);
2943 rc = lod_striping_reload(env, lo, buf);
2947 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), buf,
2948 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
2956 * Split layouts, just set the LOVEA with the layout from mbuf.
2958 static int lod_declare_layout_split(const struct lu_env *env,
2959 struct dt_object *dt, const struct lu_buf *mbuf,
2962 struct lod_object *lo = lod_dt_obj(dt);
2963 struct lov_comp_md_v1 *lcm = mbuf->lb_buf;
2967 lod_obj_inc_layout_gen(lo);
2968 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
2970 rc = lod_striping_reload(env, lo, mbuf);
2974 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), mbuf,
2975 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
2980 * Implementation of dt_object_operations::do_declare_xattr_set.
2982 * \see dt_object_operations::do_declare_xattr_set() in the API description
2985 * the extension to the API:
2986 * - declaring LOVEA requests striping creation
2987 * - LU_XATTR_REPLACE means layout swap
2989 static int lod_declare_xattr_set(const struct lu_env *env,
2990 struct dt_object *dt,
2991 const struct lu_buf *buf,
2992 const char *name, int fl,
2995 struct dt_object *next = dt_object_child(dt);
2996 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3001 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
3002 if ((S_ISREG(mode) || mode == 0) &&
3003 !(fl & (LU_XATTR_REPLACE | LU_XATTR_MERGE | LU_XATTR_SPLIT)) &&
3004 (strcmp(name, XATTR_NAME_LOV) == 0 ||
3005 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
3007 * this is a request to create object's striping.
3009 * allow to declare predefined striping on a new (!mode) object
3010 * which is supposed to be replay of regular file creation
3011 * (when LOV setting is declared)
3013 * LU_XATTR_REPLACE is set to indicate a layout swap
3015 if (dt_object_exists(dt)) {
3016 rc = dt_attr_get(env, next, attr);
3020 memset(attr, 0, sizeof(*attr));
3021 attr->la_valid = LA_TYPE | LA_MODE;
3022 attr->la_mode = S_IFREG;
3024 rc = lod_declare_striped_create(env, dt, attr, buf, th);
3025 } else if (fl & LU_XATTR_MERGE) {
3026 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3027 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3028 rc = lod_declare_layout_merge(env, dt, buf, th);
3029 } else if (fl & LU_XATTR_SPLIT) {
3030 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3031 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3032 rc = lod_declare_layout_split(env, dt, buf, th);
3033 } else if (S_ISREG(mode) &&
3034 strlen(name) > strlen(XATTR_LUSTRE_LOV) + 1 &&
3035 strncmp(name, XATTR_LUSTRE_LOV,
3036 strlen(XATTR_LUSTRE_LOV)) == 0) {
3038 * this is a request to modify object's striping.
3039 * add/set/del component(s).
3041 if (!dt_object_exists(dt))
3044 rc = lod_declare_modify_layout(env, dt, name, buf, th);
3045 } else if (S_ISDIR(mode)) {
3046 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
3047 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3048 rc = lod_replace_parent_fid(env, dt, th, true);
3050 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
3057 * Apply xattr changes to the object.
3059 * Applies xattr changes to the object and the stripes if the latter exist.
3061 * \param[in] env execution environment
3062 * \param[in] dt object
3063 * \param[in] buf buffer pointing to the new value of xattr
3064 * \param[in] name name of xattr
3065 * \param[in] fl flags
3066 * \param[in] th transaction handle
3068 * \retval 0 on success
3069 * \retval negative if failed
3071 static int lod_xattr_set_internal(const struct lu_env *env,
3072 struct dt_object *dt,
3073 const struct lu_buf *buf,
3074 const char *name, int fl,
3077 struct dt_object *next = dt_object_child(dt);
3078 struct lod_object *lo = lod_dt_obj(dt);
3083 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3084 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3087 /* Note: Do not set LinkEA on sub-stripes, otherwise
3088 * it will confuse the fid2path process(see mdt_path_current()).
3089 * The linkEA between master and sub-stripes is set in
3090 * lod_xattr_set_lmv(). */
3091 if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
3094 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3095 LASSERT(lo->ldo_stripe[i]);
3097 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
3107 * Delete an extended attribute.
3109 * Deletes specified xattr from the object and the stripes if the latter exist.
3111 * \param[in] env execution environment
3112 * \param[in] dt object
3113 * \param[in] name name of xattr
3114 * \param[in] th transaction handle
3116 * \retval 0 on success
3117 * \retval negative if failed
3119 static int lod_xattr_del_internal(const struct lu_env *env,
3120 struct dt_object *dt,
3121 const char *name, struct thandle *th)
3123 struct dt_object *next = dt_object_child(dt);
3124 struct lod_object *lo = lod_dt_obj(dt);
3129 rc = lod_sub_xattr_del(env, next, name, th);
3130 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3133 if (lo->ldo_dir_stripe_count == 0)
3136 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3137 LASSERT(lo->ldo_stripe[i]);
3139 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3148 * Set default striping on a directory.
3150 * Sets specified striping on a directory object unless it matches the default
3151 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3152 * EA. This striping will be used when regular file is being created in this
3155 * \param[in] env execution environment
3156 * \param[in] dt the striped object
3157 * \param[in] buf buffer with the striping
3158 * \param[in] name name of EA
3159 * \param[in] fl xattr flag (see OSD API description)
3160 * \param[in] th transaction handle
3162 * \retval 0 on success
3163 * \retval negative if failed
3165 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
3166 struct dt_object *dt,
3167 const struct lu_buf *buf,
3168 const char *name, int fl,
3171 struct lov_user_md_v1 *lum;
3172 struct lov_user_md_v3 *v3 = NULL;
3173 const char *pool_name = NULL;
3178 LASSERT(buf != NULL && buf->lb_buf != NULL);
3181 switch (lum->lmm_magic) {
3182 case LOV_USER_MAGIC_SPECIFIC:
3183 case LOV_USER_MAGIC_V3:
3185 if (v3->lmm_pool_name[0] != '\0')
3186 pool_name = v3->lmm_pool_name;
3188 case LOV_USER_MAGIC_V1:
3189 /* if { size, offset, count } = { 0, -1, 0 } and no pool
3190 * (i.e. all default values specified) then delete default
3191 * striping from dir. */
3193 "set default striping: sz %u # %u offset %d %s %s\n",
3194 (unsigned)lum->lmm_stripe_size,
3195 (unsigned)lum->lmm_stripe_count,
3196 (int)lum->lmm_stripe_offset,
3197 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
3199 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
3200 lum->lmm_stripe_count,
3201 lum->lmm_stripe_offset,
3204 case LOV_USER_MAGIC_COMP_V1:
3208 CERROR("Invalid magic %x\n", lum->lmm_magic);
3213 rc = lod_xattr_del_internal(env, dt, name, th);
3217 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3224 * Set default striping on a directory object.
3226 * Sets specified striping on a directory object unless it matches the default
3227 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3228 * EA. This striping will be used when a new directory is being created in the
3231 * \param[in] env execution environment
3232 * \param[in] dt the striped object
3233 * \param[in] buf buffer with the striping
3234 * \param[in] name name of EA
3235 * \param[in] fl xattr flag (see OSD API description)
3236 * \param[in] th transaction handle
3238 * \retval 0 on success
3239 * \retval negative if failed
3241 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
3242 struct dt_object *dt,
3243 const struct lu_buf *buf,
3244 const char *name, int fl,
3247 struct lmv_user_md_v1 *lum;
3251 LASSERT(buf != NULL && buf->lb_buf != NULL);
3254 CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
3255 le32_to_cpu(lum->lum_stripe_count),
3256 (int)le32_to_cpu(lum->lum_stripe_offset));
3258 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
3259 le32_to_cpu(lum->lum_stripe_offset)) &&
3260 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
3261 rc = lod_xattr_del_internal(env, dt, name, th);
3265 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3274 * Turn directory into a striped directory.
3276 * During replay the client sends the striping created before MDT
3277 * failure, then the layer above LOD sends this defined striping
3278 * using ->do_xattr_set(), so LOD uses this method to replay creation
3279 * of the stripes. Notice the original information for the striping
3280 * (#stripes, FIDs, etc) was transferred in declare path.
3282 * \param[in] env execution environment
3283 * \param[in] dt the striped object
3284 * \param[in] buf not used currently
3285 * \param[in] name not used currently
3286 * \param[in] fl xattr flag (see OSD API description)
3287 * \param[in] th transaction handle
3289 * \retval 0 on success
3290 * \retval negative if failed
3292 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
3293 const struct lu_buf *buf, const char *name,
3294 int fl, struct thandle *th)
3296 struct lod_object *lo = lod_dt_obj(dt);
3297 struct lod_thread_info *info = lod_env_info(env);
3298 struct lu_attr *attr = &info->lti_attr;
3299 struct dt_object_format *dof = &info->lti_format;
3300 struct lu_buf lmv_buf;
3301 struct lu_buf slave_lmv_buf;
3302 struct lmv_mds_md_v1 *lmm;
3303 struct lmv_mds_md_v1 *slave_lmm = NULL;
3304 struct dt_insert_rec *rec = &info->lti_dt_rec;
3309 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3312 /* The stripes are supposed to be allocated in declare phase,
3313 * if there are no stripes being allocated, it will skip */
3314 if (lo->ldo_dir_stripe_count == 0)
3317 rc = dt_attr_get(env, dt_object_child(dt), attr);
3321 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
3322 LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
3323 dof->dof_type = DFT_DIR;
3325 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
3328 lmm = lmv_buf.lb_buf;
3330 OBD_ALLOC_PTR(slave_lmm);
3331 if (slave_lmm == NULL)
3334 lod_prep_slave_lmv_md(slave_lmm, lmm);
3335 slave_lmv_buf.lb_buf = slave_lmm;
3336 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
3338 rec->rec_type = S_IFDIR;
3339 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3340 struct dt_object *dto;
3341 char *stripe_name = info->lti_key;
3342 struct lu_name *sname;
3343 struct linkea_data ldata = { NULL };
3344 struct lu_buf linkea_buf;
3346 dto = lo->ldo_stripe[i];
3348 dt_write_lock(env, dto, MOR_TGT_CHILD);
3349 rc = lod_sub_create(env, dto, attr, NULL, dof, th);
3351 dt_write_unlock(env, dto);
3355 rc = lod_sub_ref_add(env, dto, th);
3356 dt_write_unlock(env, dto);
3360 rec->rec_fid = lu_object_fid(&dto->do_lu);
3361 rc = lod_sub_insert(env, dto, (const struct dt_rec *)rec,
3362 (const struct dt_key *)dot, th, 0);
3366 rec->rec_fid = lu_object_fid(&dt->do_lu);
3367 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
3368 (const struct dt_key *)dotdot, th, 0);
3372 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
3373 cfs_fail_val != i) {
3374 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
3376 slave_lmm->lmv_master_mdt_index =
3379 slave_lmm->lmv_master_mdt_index =
3382 rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
3383 XATTR_NAME_LMV, fl, th);
3388 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
3390 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3391 PFID(lu_object_fid(&dto->do_lu)), i + 1);
3393 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3394 PFID(lu_object_fid(&dto->do_lu)), i);
3396 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
3397 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
3398 sname, lu_object_fid(&dt->do_lu));
3402 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
3403 linkea_buf.lb_len = ldata.ld_leh->leh_len;
3404 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
3405 XATTR_NAME_LINK, 0, th);
3409 rec->rec_fid = lu_object_fid(&dto->do_lu);
3410 rc = lod_sub_insert(env, dt_object_child(dt),
3411 (const struct dt_rec *)rec,
3412 (const struct dt_key *)stripe_name, th, 0);
3416 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
3421 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
3422 rc = lod_sub_xattr_set(env, dt_object_child(dt),
3423 &lmv_buf, XATTR_NAME_LMV, fl, th);
3425 if (slave_lmm != NULL)
3426 OBD_FREE_PTR(slave_lmm);
3432 * Helper function to declare/execute creation of a striped directory
3434 * Called in declare/create object path, prepare striping for a directory
3435 * and prepare defaults data striping for the objects to be created in
3436 * that directory. Notice the function calls "declaration" or "execution"
3437 * methods depending on \a declare param. This is a consequence of the
3438 * current approach while we don't have natural distributed transactions:
3439 * we basically execute non-local updates in the declare phase. So, the
3440 * arguments for the both phases are the same and this is the reason for
3441 * this function to exist.
3443 * \param[in] env execution environment
3444 * \param[in] dt object
3445 * \param[in] attr attributes the stripes will be created with
3446 * \param[in] lmu lmv_user_md if MDT indices are specified
3447 * \param[in] dof format of stripes (see OSD API description)
3448 * \param[in] th transaction handle
3449 * \param[in] declare where to call "declare" or "execute" methods
3451 * \retval 0 on success
3452 * \retval negative if failed
3454 static int lod_dir_striping_create_internal(const struct lu_env *env,
3455 struct dt_object *dt,
3456 struct lu_attr *attr,
3457 const struct lu_buf *lmu,
3458 struct dt_object_format *dof,
3462 struct lod_thread_info *info = lod_env_info(env);
3463 struct lod_object *lo = lod_dt_obj(dt);
3464 const struct lod_default_striping *lds = lo->ldo_def_striping;
3468 LASSERT(ergo(lds != NULL,
3469 lds->lds_def_striping_set ||
3470 lds->lds_dir_def_striping_set));
3472 if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
3473 lo->ldo_dir_stripe_offset)) {
3475 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3476 int stripe_count = lo->ldo_dir_stripe_count;
3478 if (info->lti_ea_store_size < sizeof(*v1)) {
3479 rc = lod_ea_store_resize(info, sizeof(*v1));
3482 v1 = info->lti_ea_store;
3485 memset(v1, 0, sizeof(*v1));
3486 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3487 v1->lum_stripe_count = cpu_to_le32(stripe_count);
3488 v1->lum_stripe_offset =
3489 cpu_to_le32(lo->ldo_dir_stripe_offset);
3491 info->lti_buf.lb_buf = v1;
3492 info->lti_buf.lb_len = sizeof(*v1);
3493 lmu = &info->lti_buf;
3497 rc = lod_declare_xattr_set_lmv(env, dt, attr, lmu, dof,
3500 rc = lod_xattr_set_lmv(env, dt, lmu, XATTR_NAME_LMV, 0,
3506 /* Transfer default LMV striping from the parent */
3507 if (lds != NULL && lds->lds_dir_def_striping_set &&
3508 !LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
3509 lds->lds_dir_def_stripe_offset)) {
3510 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3512 if (info->lti_ea_store_size < sizeof(*v1)) {
3513 rc = lod_ea_store_resize(info, sizeof(*v1));
3516 v1 = info->lti_ea_store;
3519 memset(v1, 0, sizeof(*v1));
3520 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3521 v1->lum_stripe_count =
3522 cpu_to_le32(lds->lds_dir_def_stripe_count);
3523 v1->lum_stripe_offset =
3524 cpu_to_le32(lds->lds_dir_def_stripe_offset);
3526 cpu_to_le32(lds->lds_dir_def_hash_type);
3528 info->lti_buf.lb_buf = v1;
3529 info->lti_buf.lb_len = sizeof(*v1);
3531 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3532 XATTR_NAME_DEFAULT_LMV,
3535 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
3537 XATTR_NAME_DEFAULT_LMV, 0,
3543 /* Transfer default LOV striping from the parent */
3544 if (lds != NULL && lds->lds_def_striping_set &&
3545 lds->lds_def_comp_cnt != 0) {
3546 struct lov_mds_md *lmm;
3547 int lmm_size = lod_comp_md_size(lo, true);
3549 if (info->lti_ea_store_size < lmm_size) {
3550 rc = lod_ea_store_resize(info, lmm_size);
3554 lmm = info->lti_ea_store;
3556 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
3560 info->lti_buf.lb_buf = lmm;
3561 info->lti_buf.lb_len = lmm_size;
3564 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3565 XATTR_NAME_LOV, 0, th);
3567 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
3568 XATTR_NAME_LOV, 0, th);
3576 static int lod_declare_dir_striping_create(const struct lu_env *env,
3577 struct dt_object *dt,
3578 struct lu_attr *attr,
3580 struct dt_object_format *dof,
3583 return lod_dir_striping_create_internal(env, dt, attr, lmu, dof, th,
3587 static int lod_dir_striping_create(const struct lu_env *env,
3588 struct dt_object *dt,
3589 struct lu_attr *attr,
3590 struct dt_object_format *dof,
3593 return lod_dir_striping_create_internal(env, dt, attr, NULL, dof, th,
3598 * Make LOV EA for striped object.
3600 * Generate striping information and store it in the LOV EA of the given
3601 * object. The caller must ensure nobody else is calling the function
3602 * against the object concurrently. The transaction must be started.
3603 * FLDB service must be running as well; it's used to map FID to the target,
3604 * which is stored in LOV EA.
3606 * \param[in] env execution environment for this thread
3607 * \param[in] lo LOD object
3608 * \param[in] th transaction handle
3610 * \retval 0 if LOV EA is stored successfully
3611 * \retval negative error number on failure
3613 static int lod_generate_and_set_lovea(const struct lu_env *env,
3614 struct lod_object *lo,
3617 struct lod_thread_info *info = lod_env_info(env);
3618 struct dt_object *next = dt_object_child(&lo->ldo_obj);
3619 struct lov_mds_md_v1 *lmm;
3625 if (lo->ldo_comp_cnt == 0) {
3626 lod_striping_free(env, lo);
3627 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
3631 lmm_size = lod_comp_md_size(lo, false);
3632 if (info->lti_ea_store_size < lmm_size) {
3633 rc = lod_ea_store_resize(info, lmm_size);
3637 lmm = info->lti_ea_store;
3639 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
3643 info->lti_buf.lb_buf = lmm;
3644 info->lti_buf.lb_len = lmm_size;
3645 rc = lod_sub_xattr_set(env, next, &info->lti_buf,
3646 XATTR_NAME_LOV, 0, th);
3651 * Delete layout component(s)
3653 * \param[in] env execution environment for this thread
3654 * \param[in] dt object
3655 * \param[in] th transaction handle
3657 * \retval 0 on success
3658 * \retval negative error number on failure
3660 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
3663 struct lod_layout_component *lod_comp;
3664 struct lod_object *lo = lod_dt_obj(dt);
3665 struct dt_object *next = dt_object_child(dt);
3666 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3669 LASSERT(lo->ldo_is_composite);
3670 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
3672 left = lo->ldo_comp_cnt;
3673 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3674 lod_comp = &lo->ldo_comp_entries[i];
3676 if (lod_comp->llc_id != LCME_ID_INVAL)
3680 /* Not instantiated component */
3681 if (lod_comp->llc_stripe == NULL)
3684 LASSERT(lod_comp->llc_stripe_count > 0);
3685 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
3686 struct dt_object *obj = lod_comp->llc_stripe[j];
3690 rc = lod_sub_destroy(env, obj, th);
3694 lu_object_put(env, &obj->do_lu);
3695 lod_comp->llc_stripe[j] = NULL;
3697 OBD_FREE(lod_comp->llc_stripe, sizeof(struct dt_object *) *
3698 lod_comp->llc_stripes_allocated);
3699 lod_comp->llc_stripe = NULL;
3700 OBD_FREE(lod_comp->llc_ost_indices,
3701 sizeof(__u32) * lod_comp->llc_stripes_allocated);
3702 lod_comp->llc_ost_indices = NULL;
3703 lod_comp->llc_stripes_allocated = 0;
3704 lod_obj_set_pool(lo, i, NULL);
3705 if (lod_comp->llc_ostlist.op_array) {
3706 OBD_FREE(lod_comp->llc_ostlist.op_array,
3707 lod_comp->llc_ostlist.op_size);
3708 lod_comp->llc_ostlist.op_array = NULL;
3709 lod_comp->llc_ostlist.op_size = 0;
3713 LASSERTF(left >= 0 && left < lo->ldo_comp_cnt, "left = %d\n", left);
3715 struct lod_layout_component *comp_array;
3717 OBD_ALLOC(comp_array, sizeof(*comp_array) * left);
3718 if (comp_array == NULL)
3719 GOTO(out, rc = -ENOMEM);
3721 memcpy(&comp_array[0], &lo->ldo_comp_entries[0],
3722 sizeof(*comp_array) * left);
3724 OBD_FREE(lo->ldo_comp_entries,
3725 sizeof(*comp_array) * lo->ldo_comp_cnt);
3726 lo->ldo_comp_entries = comp_array;
3727 lo->ldo_comp_cnt = left;
3729 LASSERT(lo->ldo_mirror_count == 1);
3730 lo->ldo_mirrors[0].lme_end = left - 1;
3731 lod_obj_inc_layout_gen(lo);
3733 lod_free_comp_entries(lo);
3736 LASSERT(dt_object_exists(dt));
3737 rc = dt_attr_get(env, next, attr);
3741 if (attr->la_size > 0) {
3743 attr->la_valid = LA_SIZE;
3744 rc = lod_sub_attr_set(env, next, attr, th);
3749 rc = lod_generate_and_set_lovea(env, lo, th);
3753 lod_striping_free(env, lo);
3758 static int lod_get_default_lov_striping(const struct lu_env *env,
3759 struct lod_object *lo,
3760 struct lod_default_striping *lds);
3762 * Implementation of dt_object_operations::do_xattr_set.
3764 * Sets specified extended attribute on the object. Three types of EAs are
3766 * LOV EA - stores striping for a regular file or default striping (when set
3768 * LMV EA - stores a marker for the striped directories
3769 * DMV EA - stores default directory striping
3771 * When striping is applied to a non-striped existing object (this is called
3772 * late striping), then LOD notices the caller wants to turn the object into a
3773 * striped one. The stripe objects are created and appropriate EA is set:
3774 * LOV EA storing all the stripes directly or LMV EA storing just a small header
3775 * with striping configuration.
3777 * \see dt_object_operations::do_xattr_set() in the API description for details.
3779 static int lod_xattr_set(const struct lu_env *env,
3780 struct dt_object *dt, const struct lu_buf *buf,
3781 const char *name, int fl, struct thandle *th)
3783 struct dt_object *next = dt_object_child(dt);
3787 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3788 strcmp(name, XATTR_NAME_LMV) == 0) {
3789 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
3791 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
3792 LMV_HASH_FLAG_MIGRATION)
3793 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3795 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
3800 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3801 strcmp(name, XATTR_NAME_LOV) == 0) {
3802 struct lod_thread_info *info = lod_env_info(env);
3803 struct lod_default_striping *lds = &info->lti_def_striping;
3804 struct lov_user_md_v1 *v1 = buf->lb_buf;
3805 char pool[LOV_MAXPOOLNAME + 1];
3808 /* get existing striping config */
3809 rc = lod_get_default_lov_striping(env, lod_dt_obj(dt), lds);
3813 memset(pool, 0, sizeof(pool));
3814 if (lds->lds_def_striping_set == 1)
3815 lod_layout_get_pool(lds->lds_def_comp_entries,
3816 lds->lds_def_comp_cnt, pool,
3819 is_del = LOVEA_DELETE_VALUES(v1->lmm_stripe_size,
3820 v1->lmm_stripe_count,
3821 v1->lmm_stripe_offset,
3824 /* Retain the pool name if it is not given */
3825 if (v1->lmm_magic == LOV_USER_MAGIC_V1 && pool[0] != '\0' &&
3827 struct lod_thread_info *info = lod_env_info(env);
3828 struct lov_user_md_v3 *v3 = info->lti_ea_store;
3830 memset(v3, 0, sizeof(*v3));
3831 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
3832 v3->lmm_pattern = cpu_to_le32(v1->lmm_pattern);
3833 v3->lmm_stripe_count =
3834 cpu_to_le32(v1->lmm_stripe_count);
3835 v3->lmm_stripe_offset =
3836 cpu_to_le32(v1->lmm_stripe_offset);
3837 v3->lmm_stripe_size = cpu_to_le32(v1->lmm_stripe_size);
3839 strlcpy(v3->lmm_pool_name, pool,
3840 sizeof(v3->lmm_pool_name));
3842 info->lti_buf.lb_buf = v3;
3843 info->lti_buf.lb_len = sizeof(*v3);
3844 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
3847 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name,
3851 if (lds->lds_def_striping_set == 1 &&
3852 lds->lds_def_comp_entries != NULL)
3853 lod_free_def_comp_entries(lds);
3856 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3857 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
3859 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
3862 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3863 (!strcmp(name, XATTR_NAME_LOV) ||
3864 !strncmp(name, XATTR_LUSTRE_LOV,
3865 strlen(XATTR_LUSTRE_LOV)))) {
3866 /* in case of lov EA swap, just set it
3867 * if not, it is a replay so check striping match what we
3868 * already have during req replay, declare_xattr_set()
3869 * defines striping, then create() does the work */
3870 if (fl & LU_XATTR_REPLACE) {
3871 /* free stripes, then update disk */
3872 lod_striping_free(env, lod_dt_obj(dt));
3874 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3875 } else if (dt_object_remote(dt)) {
3876 /* This only happens during migration, see
3877 * mdd_migrate_create(), in which Master MDT will
3878 * create a remote target object, and only set
3879 * (migrating) stripe EA on the remote object,
3880 * and does not need creating each stripes. */
3881 rc = lod_sub_xattr_set(env, next, buf, name,
3883 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
3884 /* delete component(s) */
3885 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
3886 rc = lod_layout_del(env, dt, th);
3889 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
3890 * it's going to create create file with specified
3891 * component(s), the striping must have not being
3892 * cached in this case;
3894 * Otherwise, it's going to add/change component(s) to
3895 * an existing file, the striping must have been cached
3898 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
3899 !strcmp(name, XATTR_NAME_LOV),
3900 !lod_dt_obj(dt)->ldo_comp_cached));
3902 rc = lod_striped_create(env, dt, NULL, NULL, th);
3905 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3906 rc = lod_replace_parent_fid(env, dt, th, false);
3911 /* then all other xattr */
3912 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3918 * Implementation of dt_object_operations::do_declare_xattr_del.
3920 * \see dt_object_operations::do_declare_xattr_del() in the API description
3923 static int lod_declare_xattr_del(const struct lu_env *env,
3924 struct dt_object *dt, const char *name,
3927 struct lod_object *lo = lod_dt_obj(dt);
3932 rc = lod_sub_declare_xattr_del(env, dt_object_child(dt), name, th);
3936 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3939 /* set xattr to each stripes, if needed */
3940 rc = lod_striping_load(env, lo);
3944 if (lo->ldo_dir_stripe_count == 0)
3947 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3948 LASSERT(lo->ldo_stripe[i]);
3949 rc = lod_sub_declare_xattr_del(env, lo->ldo_stripe[i],
3959 * Implementation of dt_object_operations::do_xattr_del.
3961 * If EA storing a regular striping is being deleted, then release
3962 * all the references to the stripe objects in core.
3964 * \see dt_object_operations::do_xattr_del() in the API description for details.
3966 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
3967 const char *name, struct thandle *th)
3969 struct dt_object *next = dt_object_child(dt);
3970 struct lod_object *lo = lod_dt_obj(dt);
3975 if (!strcmp(name, XATTR_NAME_LOV))
3976 lod_striping_free(env, lod_dt_obj(dt));
3978 rc = lod_sub_xattr_del(env, next, name, th);
3979 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3982 if (lo->ldo_dir_stripe_count == 0)
3985 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3986 LASSERT(lo->ldo_stripe[i]);
3988 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3997 * Implementation of dt_object_operations::do_xattr_list.
3999 * \see dt_object_operations::do_xattr_list() in the API description
4002 static int lod_xattr_list(const struct lu_env *env,
4003 struct dt_object *dt, const struct lu_buf *buf)
4005 return dt_xattr_list(env, dt_object_child(dt), buf);
4008 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
4010 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
4014 * Copy OST list from layout provided by user.
4016 * \param[in] lod_comp layout_component to be filled
4017 * \param[in] v3 LOV EA V3 user data
4019 * \retval 0 on success
4020 * \retval negative if failed
4022 int lod_comp_copy_ost_lists(struct lod_layout_component *lod_comp,
4023 struct lov_user_md_v3 *v3)
4029 if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
4030 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
4032 if (lod_comp->llc_ostlist.op_array) {
4033 if (lod_comp->llc_ostlist.op_count ==
4034 v3->lmm_stripe_count)
4036 OBD_FREE(lod_comp->llc_ostlist.op_array,
4037 lod_comp->llc_ostlist.op_size);
4040 /* copy ost list from lmm */
4041 lod_comp->llc_ostlist.op_count = v3->lmm_stripe_count;
4042 lod_comp->llc_ostlist.op_size = v3->lmm_stripe_count * sizeof(__u32);
4043 OBD_ALLOC(lod_comp->llc_ostlist.op_array,
4044 lod_comp->llc_ostlist.op_size);
4045 if (!lod_comp->llc_ostlist.op_array)
4048 for (j = 0; j < v3->lmm_stripe_count; j++) {
4049 lod_comp->llc_ostlist.op_array[j] =
4050 v3->lmm_objects[j].l_ost_idx;
4058 * Get default striping.
4060 * \param[in] env execution environment
4061 * \param[in] lo object
4062 * \param[out] lds default striping
4064 * \retval 0 on success
4065 * \retval negative if failed
4067 static int lod_get_default_lov_striping(const struct lu_env *env,
4068 struct lod_object *lo,
4069 struct lod_default_striping *lds)
4071 struct lod_thread_info *info = lod_env_info(env);
4072 struct lov_user_md_v1 *v1 = NULL;
4073 struct lov_user_md_v3 *v3 = NULL;
4074 struct lov_comp_md_v1 *comp_v1 = NULL;
4081 lds->lds_def_striping_set = 0;
4083 rc = lod_get_lov_ea(env, lo);
4087 if (rc < (typeof(rc))sizeof(struct lov_user_md))
4090 v1 = info->lti_ea_store;
4091 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
4092 lustre_swab_lov_user_md_v1(v1);
4093 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
4094 v3 = (struct lov_user_md_v3 *)v1;
4095 lustre_swab_lov_user_md_v3(v3);
4096 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_SPECIFIC)) {
4097 v3 = (struct lov_user_md_v3 *)v1;
4098 lustre_swab_lov_user_md_v3(v3);
4099 lustre_swab_lov_user_md_objects(v3->lmm_objects,
4100 v3->lmm_stripe_count);
4101 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
4102 comp_v1 = (struct lov_comp_md_v1 *)v1;
4103 lustre_swab_lov_comp_md_v1(comp_v1);
4106 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
4107 v1->lmm_magic != LOV_MAGIC_COMP_V1 &&
4108 v1->lmm_magic != LOV_USER_MAGIC_SPECIFIC)
4111 if (v1->lmm_magic == LOV_MAGIC_COMP_V1) {
4112 comp_v1 = (struct lov_comp_md_v1 *)v1;
4113 comp_cnt = comp_v1->lcm_entry_count;
4116 mirror_cnt = comp_v1->lcm_mirror_count + 1;
4124 /* realloc default comp entries if necessary */
4125 rc = lod_def_striping_comp_resize(lds, comp_cnt);
4129 lds->lds_def_comp_cnt = comp_cnt;
4130 lds->lds_def_striping_is_composite = composite;
4131 lds->lds_def_mirror_cnt = mirror_cnt;
4133 for (i = 0; i < comp_cnt; i++) {
4134 struct lod_layout_component *lod_comp;
4135 struct lu_extent *ext;
4138 lod_comp = &lds->lds_def_comp_entries[i];
4140 * reset lod_comp values, llc_stripes is always NULL in
4141 * the default striping template, llc_pool will be reset
4144 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
4147 v1 = (struct lov_user_md *)((char *)comp_v1 +
4148 comp_v1->lcm_entries[i].lcme_offset);
4149 ext = &comp_v1->lcm_entries[i].lcme_extent;
4150 lod_comp->llc_extent = *ext;
4153 if (v1->lmm_pattern != LOV_PATTERN_RAID0 &&
4154 v1->lmm_pattern != LOV_PATTERN_MDT &&
4155 v1->lmm_pattern != 0) {
4156 lod_free_def_comp_entries(lds);
4160 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d "
4161 "stripe_offset=%d\n",
4162 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
4163 (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
4164 (int)v1->lmm_stripe_offset);
4166 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
4167 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
4168 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
4169 lod_comp->llc_pattern = v1->lmm_pattern;
4172 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
4173 /* XXX: sanity check here */
4174 v3 = (struct lov_user_md_v3 *) v1;
4175 if (v3->lmm_pool_name[0] != '\0')
4176 pool = v3->lmm_pool_name;
4178 lod_set_def_pool(lds, i, pool);
4179 if (v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
4180 v3 = (struct lov_user_md_v3 *)v1;
4181 rc = lod_comp_copy_ost_lists(lod_comp, v3);
4187 lds->lds_def_striping_set = 1;
4192 * Get default directory striping.
4194 * \param[in] env execution environment
4195 * \param[in] lo object
4196 * \param[out] lds default striping
4198 * \retval 0 on success
4199 * \retval negative if failed
4201 static int lod_get_default_lmv_striping(const struct lu_env *env,
4202 struct lod_object *lo,
4203 struct lod_default_striping *lds)
4205 struct lod_thread_info *info = lod_env_info(env);
4206 struct lmv_user_md_v1 *v1 = NULL;
4210 lds->lds_dir_def_striping_set = 0;
4211 rc = lod_get_default_lmv_ea(env, lo);
4215 if (rc < (typeof(rc))sizeof(struct lmv_user_md))
4218 v1 = info->lti_ea_store;
4220 lds->lds_dir_def_stripe_count = le32_to_cpu(v1->lum_stripe_count);
4221 lds->lds_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
4222 lds->lds_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
4223 lds->lds_dir_def_striping_set = 1;
4229 * Get default striping in the object.
4231 * Get object default striping and default directory striping.
4233 * \param[in] env execution environment
4234 * \param[in] lo object
4235 * \param[out] lds default striping
4237 * \retval 0 on success
4238 * \retval negative if failed
4240 static int lod_get_default_striping(const struct lu_env *env,
4241 struct lod_object *lo,
4242 struct lod_default_striping *lds)
4246 rc = lod_get_default_lov_striping(env, lo, lds);
4247 rc1 = lod_get_default_lmv_striping(env, lo, lds);
4248 if (rc == 0 && rc1 < 0)
4255 * Apply default striping on object.
4257 * If object striping pattern is not set, set to the one in default striping.
4258 * The default striping is from parent or fs.
4260 * \param[in] lo new object
4261 * \param[in] lds default striping
4262 * \param[in] mode new object's mode
4264 static void lod_striping_from_default(struct lod_object *lo,
4265 const struct lod_default_striping *lds,
4268 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4269 struct lov_desc *desc = &d->lod_desc;
4272 if (lds->lds_def_striping_set && S_ISREG(mode)) {
4273 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
4274 lds->lds_def_comp_cnt);
4278 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
4279 if (lds->lds_def_mirror_cnt > 1)
4280 lo->ldo_flr_state = LCM_FL_RDONLY;
4282 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4283 struct lod_layout_component *obj_comp =
4284 &lo->ldo_comp_entries[i];
4285 struct lod_layout_component *def_comp =
4286 &lds->lds_def_comp_entries[i];
4288 CDEBUG(D_LAYOUT, "Inherite from default: size:%hu "
4289 "nr:%u offset:%u pattern %#x %s\n",
4290 def_comp->llc_stripe_size,
4291 def_comp->llc_stripe_count,
4292 def_comp->llc_stripe_offset,
4293 def_comp->llc_pattern,
4294 def_comp->llc_pool ?: "");
4296 *obj_comp = *def_comp;
4297 if (def_comp->llc_pool != NULL) {
4298 /* pointer was copied from def_comp */
4299 obj_comp->llc_pool = NULL;
4300 lod_obj_set_pool(lo, i, def_comp->llc_pool);
4304 if (def_comp->llc_ostlist.op_array) {
4305 OBD_ALLOC(obj_comp->llc_ostlist.op_array,
4306 obj_comp->llc_ostlist.op_size);
4307 if (!obj_comp->llc_ostlist.op_array)
4309 memcpy(obj_comp->llc_ostlist.op_array,
4310 def_comp->llc_ostlist.op_array,
4311 obj_comp->llc_ostlist.op_size);
4315 * Don't initialize these fields for plain layout
4316 * (v1/v3) here, they are inherited in the order of
4317 * 'parent' -> 'fs default (root)' -> 'global default
4318 * values for stripe_count & stripe_size'.
4320 * see lod_ah_init().
4322 if (!lo->ldo_is_composite)
4325 lod_adjust_stripe_info(obj_comp, desc);
4327 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
4328 if (lo->ldo_dir_stripe_count == 0)
4329 lo->ldo_dir_stripe_count =
4330 lds->lds_dir_def_stripe_count;
4331 if (lo->ldo_dir_stripe_offset == -1)
4332 lo->ldo_dir_stripe_offset =
4333 lds->lds_dir_def_stripe_offset;
4334 if (lo->ldo_dir_hash_type == 0)
4335 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
4337 CDEBUG(D_LAYOUT, "striping from default dir: count:%hu, "
4338 "offset:%u, hash_type:%u\n",
4339 lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
4340 lo->ldo_dir_hash_type);
4344 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root)
4346 struct lod_layout_component *lod_comp;
4348 if (lo->ldo_comp_cnt == 0)
4351 if (lo->ldo_is_composite)
4354 lod_comp = &lo->ldo_comp_entries[0];
4356 if (lod_comp->llc_stripe_count <= 0 ||
4357 lod_comp->llc_stripe_size <= 0)
4360 if (from_root && (lod_comp->llc_pool == NULL ||
4361 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
4368 * Implementation of dt_object_operations::do_ah_init.
4370 * This method is used to make a decision on the striping configuration for the
4371 * object being created. It can be taken from the \a parent object if it exists,
4372 * or filesystem's default. The resulting configuration (number of stripes,
4373 * stripe size/offset, pool name, etc) is stored in the object itself and will
4374 * be used by the methods like ->doo_declare_create().
4376 * \see dt_object_operations::do_ah_init() in the API description for details.
4378 static void lod_ah_init(const struct lu_env *env,
4379 struct dt_allocation_hint *ah,
4380 struct dt_object *parent,
4381 struct dt_object *child,
4384 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
4385 struct lod_thread_info *info = lod_env_info(env);
4386 struct lod_default_striping *lds = &info->lti_def_striping;
4387 struct dt_object *nextp = NULL;
4388 struct dt_object *nextc;
4389 struct lod_object *lp = NULL;
4390 struct lod_object *lc;
4391 struct lov_desc *desc;
4392 struct lod_layout_component *lod_comp;
4398 if (likely(parent)) {
4399 nextp = dt_object_child(parent);
4400 lp = lod_dt_obj(parent);
4403 nextc = dt_object_child(child);
4404 lc = lod_dt_obj(child);
4406 LASSERT(!lod_obj_is_striped(child));
4407 /* default layout template may have been set on the regular file
4408 * when this is called from mdd_create_data() */
4409 if (S_ISREG(child_mode))
4410 lod_free_comp_entries(lc);
4412 if (!dt_object_exists(nextc))
4413 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
4415 if (S_ISDIR(child_mode)) {
4416 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
4418 /* other default values are 0 */
4419 lc->ldo_dir_stripe_offset = -1;
4421 /* get default striping from parent object */
4422 if (likely(lp != NULL))
4423 lod_get_default_striping(env, lp, lds);
4425 /* set child default striping info, default value is NULL */
4426 if (lds->lds_def_striping_set || lds->lds_dir_def_striping_set)
4427 lc->ldo_def_striping = lds;
4429 /* It should always honour the specified stripes */
4430 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
4431 * will have old magic. In this case, we should ignore the
4432 * stripe count and try to create dir by default stripe.
4434 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
4435 (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
4436 le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
4437 lc->ldo_dir_stripe_count =
4438 le32_to_cpu(lum1->lum_stripe_count);
4439 lc->ldo_dir_stripe_offset =
4440 le32_to_cpu(lum1->lum_stripe_offset);
4441 lc->ldo_dir_hash_type =
4442 le32_to_cpu(lum1->lum_hash_type);
4444 "set dirstripe: count %hu, offset %d, hash %u\n",
4445 lc->ldo_dir_stripe_count,
4446 (int)lc->ldo_dir_stripe_offset,
4447 lc->ldo_dir_hash_type);
4449 /* transfer defaults LMV to new directory */
4450 lod_striping_from_default(lc, lds, child_mode);
4453 /* shrink the stripe_count to the avaible MDT count */
4454 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
4455 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))
4456 lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
4458 /* Directory will be striped only if stripe_count > 1, if
4459 * stripe_count == 1, let's reset stripe_count = 0 to avoid
4460 * create single master stripe and also help to unify the
4461 * stripe handling of directories and files */
4462 if (lc->ldo_dir_stripe_count == 1)
4463 lc->ldo_dir_stripe_count = 0;
4465 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
4466 lc->ldo_dir_stripe_count,
4467 (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
4472 /* child object regular file*/
4474 if (!lod_object_will_be_striped(S_ISREG(child_mode),
4475 lu_object_fid(&child->do_lu)))
4478 /* If object is going to be striped over OSTs, transfer default
4479 * striping information to the child, so that we can use it
4480 * during declaration and creation.
4482 * Try from the parent first.
4484 if (likely(lp != NULL)) {
4485 rc = lod_get_default_lov_striping(env, lp, lds);
4487 lod_striping_from_default(lc, lds, child_mode);
4490 /* Initialize lod_device::lod_md_root object reference */
4491 if (d->lod_md_root == NULL) {
4492 struct dt_object *root;
4493 struct lod_object *lroot;
4495 lu_root_fid(&info->lti_fid);
4496 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
4497 if (!IS_ERR(root)) {
4498 lroot = lod_dt_obj(root);
4500 spin_lock(&d->lod_lock);
4501 if (d->lod_md_root != NULL)
4502 dt_object_put(env, &d->lod_md_root->ldo_obj);
4503 d->lod_md_root = lroot;
4504 spin_unlock(&d->lod_lock);
4508 /* try inherit layout from the root object (fs default) when:
4509 * - parent does not have default layout; or
4510 * - parent has plain(v1/v3) default layout, and some attributes
4511 * are not specified in the default layout;
4513 if (d->lod_md_root != NULL && lod_need_inherit_more(lc, true)) {
4514 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds);
4517 if (lc->ldo_comp_cnt == 0) {
4518 lod_striping_from_default(lc, lds, child_mode);
4519 } else if (!lds->lds_def_striping_is_composite) {
4520 struct lod_layout_component *def_comp;
4522 LASSERT(!lc->ldo_is_composite);
4523 lod_comp = &lc->ldo_comp_entries[0];
4524 def_comp = &lds->lds_def_comp_entries[0];
4526 if (lod_comp->llc_stripe_count <= 0)
4527 lod_comp->llc_stripe_count =
4528 def_comp->llc_stripe_count;
4529 if (lod_comp->llc_stripe_size <= 0)
4530 lod_comp->llc_stripe_size =
4531 def_comp->llc_stripe_size;
4532 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
4533 lod_comp->llc_stripe_offset =
4534 def_comp->llc_stripe_offset;
4535 if (lod_comp->llc_pool == NULL)
4536 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
4541 * fs default striping may not be explicitly set, or historically set
4542 * in config log, use them.
4544 if (lod_need_inherit_more(lc, false)) {
4545 if (lc->ldo_comp_cnt == 0) {
4546 rc = lod_alloc_comp_entries(lc, 0, 1);
4548 /* fail to allocate memory, will create a
4549 * non-striped file. */
4551 lc->ldo_is_composite = 0;
4552 lod_comp = &lc->ldo_comp_entries[0];
4553 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4555 LASSERT(!lc->ldo_is_composite);
4556 lod_comp = &lc->ldo_comp_entries[0];
4557 desc = &d->lod_desc;
4558 lod_adjust_stripe_info(lod_comp, desc);
4564 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
4566 * Size initialization on late striping.
4568 * Propagate the size of a truncated object to a deferred striping.
4569 * This function handles a special case when truncate was done on a
4570 * non-striped object and now while the striping is being created
4571 * we can't lose that size, so we have to propagate it to the stripes
4574 * \param[in] env execution environment
4575 * \param[in] dt object
4576 * \param[in] th transaction handle
4578 * \retval 0 on success
4579 * \retval negative if failed
4581 static int lod_declare_init_size(const struct lu_env *env,
4582 struct dt_object *dt, struct thandle *th)
4584 struct dt_object *next = dt_object_child(dt);
4585 struct lod_object *lo = lod_dt_obj(dt);
4586 struct dt_object **objects = NULL;
4587 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4588 uint64_t size, offs;
4589 int i, rc, stripe, stripe_count = 0, stripe_size = 0;
4590 struct lu_extent size_ext;
4593 if (!lod_obj_is_striped(dt))
4596 rc = dt_attr_get(env, next, attr);
4597 LASSERT(attr->la_valid & LA_SIZE);
4601 size = attr->la_size;
4605 size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
4606 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4607 struct lod_layout_component *lod_comp;
4608 struct lu_extent *extent;
4610 lod_comp = &lo->ldo_comp_entries[i];
4612 if (lod_comp->llc_stripe == NULL)
4615 extent = &lod_comp->llc_extent;
4616 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
4617 if (!lo->ldo_is_composite ||
4618 lu_extent_is_overlapped(extent, &size_ext)) {
4619 objects = lod_comp->llc_stripe;
4620 stripe_count = lod_comp->llc_stripe_count;
4621 stripe_size = lod_comp->llc_stripe_size;
4624 if (stripe_count == 0)
4627 LASSERT(objects != NULL && stripe_size != 0);
4628 /* ll_do_div64(a, b) returns a % b, and a = a / b */
4629 ll_do_div64(size, (__u64)stripe_size);
4630 stripe = ll_do_div64(size, (__u64)stripe_count);
4631 LASSERT(objects[stripe] != NULL);
4633 size = size * stripe_size;
4634 offs = attr->la_size;
4635 size += ll_do_div64(offs, stripe_size);
4637 attr->la_valid = LA_SIZE;
4638 attr->la_size = size;
4640 rc = lod_sub_declare_attr_set(env, objects[stripe],
4649 * Declare creation of striped object.
4651 * The function declares creation stripes for a regular object. The function
4652 * also declares whether the stripes will be created with non-zero size if
4653 * previously size was set non-zero on the master object. If object \a dt is
4654 * not local, then only fully defined striping can be applied in \a lovea.
4655 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
4658 * \param[in] env execution environment
4659 * \param[in] dt object
4660 * \param[in] attr attributes the stripes will be created with
4661 * \param[in] lovea a buffer containing striping description
4662 * \param[in] th transaction handle
4664 * \retval 0 on success
4665 * \retval negative if failed
4667 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
4668 struct lu_attr *attr,
4669 const struct lu_buf *lovea, struct thandle *th)
4671 struct lod_thread_info *info = lod_env_info(env);
4672 struct dt_object *next = dt_object_child(dt);
4673 struct lod_object *lo = lod_dt_obj(dt);
4677 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
4678 GOTO(out, rc = -ENOMEM);
4680 if (!dt_object_remote(next)) {
4681 /* choose OST and generate appropriate objects */
4682 rc = lod_prepare_create(env, lo, attr, lovea, th);
4687 * declare storage for striping data
4689 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
4691 /* LOD can not choose OST objects for remote objects, i.e.
4692 * stripes must be ready before that. Right now, it can only
4693 * happen during migrate, i.e. migrate process needs to create
4694 * remote regular file (mdd_migrate_create), then the migrate
4695 * process will provide stripeEA. */
4696 LASSERT(lovea != NULL);
4697 info->lti_buf = *lovea;
4700 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
4701 XATTR_NAME_LOV, 0, th);
4706 * if striping is created with local object's size > 0,
4707 * we have to propagate this size to specific object
4708 * the case is possible only when local object was created previously
4710 if (dt_object_exists(next))
4711 rc = lod_declare_init_size(env, dt, th);
4714 /* failed to create striping or to set initial size, let's reset
4715 * config so that others don't get confused */
4717 lod_striping_free(env, lo);
4723 * Implementation of dt_object_operations::do_declare_create.
4725 * The method declares creation of a new object. If the object will be striped,
4726 * then helper functions are called to find FIDs for the stripes, declare
4727 * creation of the stripes and declare initialization of the striping
4728 * information to be stored in the master object.
4730 * \see dt_object_operations::do_declare_create() in the API description
4733 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
4734 struct lu_attr *attr,
4735 struct dt_allocation_hint *hint,
4736 struct dt_object_format *dof, struct thandle *th)
4738 struct dt_object *next = dt_object_child(dt);
4739 struct lod_object *lo = lod_dt_obj(dt);
4748 * first of all, we declare creation of local object
4750 rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
4755 * it's lod_ah_init() that has decided the object will be striped
4757 if (dof->dof_type == DFT_REGULAR) {
4758 /* callers don't want stripes */
4759 /* XXX: all tricky interactions with ->ah_make_hint() decided
4760 * to use striping, then ->declare_create() behaving differently
4761 * should be cleaned */
4762 if (dof->u.dof_reg.striped != 0)
4763 rc = lod_declare_striped_create(env, dt, attr,
4765 } else if (dof->dof_type == DFT_DIR) {
4766 struct seq_server_site *ss;
4767 struct lu_buf buf = { NULL };
4768 struct lu_buf *lmu = NULL;
4770 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
4772 /* If the parent has default stripeEA, and client
4773 * did not find it before sending create request,
4774 * then MDT will return -EREMOTE, and client will
4775 * retrieve the default stripeEA and re-create the
4778 * Note: if dah_eadata != NULL, it means creating the
4779 * striped directory with specified stripeEA, then it
4780 * should ignore the default stripeEA */
4781 if (hint != NULL && hint->dah_eadata == NULL) {
4782 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
4783 GOTO(out, rc = -EREMOTE);
4785 if (lo->ldo_dir_stripe_offset == -1) {
4786 /* child and parent should be in the same MDT */
4787 if (hint->dah_parent != NULL &&
4788 dt_object_remote(hint->dah_parent))
4789 GOTO(out, rc = -EREMOTE);
4790 } else if (lo->ldo_dir_stripe_offset !=
4792 struct lod_device *lod;
4793 struct lod_tgt_descs *ltd;
4794 struct lod_tgt_desc *tgt = NULL;
4795 bool found_mdt = false;
4798 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4799 ltd = &lod->lod_mdt_descs;
4800 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
4801 tgt = LTD_TGT(ltd, i);
4802 if (tgt->ltd_index ==
4803 lo->ldo_dir_stripe_offset) {
4809 /* If the MDT indicated by stripe_offset can be
4810 * found, then tell client to resend the create
4811 * request to the correct MDT, otherwise return
4812 * error to client */
4814 GOTO(out, rc = -EREMOTE);
4816 GOTO(out, rc = -EINVAL);
4818 } else if (hint && hint->dah_eadata) {
4820 lmu->lb_buf = (void *)hint->dah_eadata;
4821 lmu->lb_len = hint->dah_eadata_len;
4824 rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
4828 /* failed to create striping or to set initial size, let's reset
4829 * config so that others don't get confused */
4831 lod_striping_free(env, lo);
4836 * Generate component ID for new created component.
4838 * \param[in] lo LOD object
4839 * \param[in] comp_idx index of ldo_comp_entries
4841 * \retval component ID on success
4842 * \retval LCME_ID_INVAL on failure
4844 static __u32 lod_gen_component_id(struct lod_object *lo,
4845 int mirror_id, int comp_idx)
4847 struct lod_layout_component *lod_comp;
4848 __u32 id, start, end;
4851 LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
4853 lod_obj_inc_layout_gen(lo);
4854 id = lo->ldo_layout_gen;
4855 if (likely(id <= SEQ_ID_MAX))
4856 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
4858 /* Layout generation wraps, need to check collisions. */
4859 start = id & SEQ_ID_MASK;
4862 for (id = start; id <= end; id++) {
4863 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4864 lod_comp = &lo->ldo_comp_entries[i];
4865 if (pflr_id(mirror_id, id) == lod_comp->llc_id)
4868 /* Found the ununsed ID */
4869 if (i == lo->ldo_comp_cnt)
4870 RETURN(pflr_id(mirror_id, id));
4872 if (end == LCME_ID_MAX) {
4874 end = min(lo->ldo_layout_gen & LCME_ID_MASK,
4875 (__u32)(LCME_ID_MAX - 1));
4879 RETURN(LCME_ID_INVAL);
4883 * Creation of a striped regular object.
4885 * The function is called to create the stripe objects for a regular
4886 * striped file. This can happen at the initial object creation or
4887 * when the caller asks LOD to do so using ->do_xattr_set() method
4888 * (so called late striping). Notice all the information are already
4889 * prepared in the form of the list of objects (ldo_stripe field).
4890 * This is done during declare phase.
4892 * \param[in] env execution environment
4893 * \param[in] dt object
4894 * \param[in] attr attributes the stripes will be created with
4895 * \param[in] dof format of stripes (see OSD API description)
4896 * \param[in] th transaction handle
4898 * \retval 0 on success
4899 * \retval negative if failed
4901 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
4902 struct lu_attr *attr, struct dt_object_format *dof,
4905 struct lod_layout_component *lod_comp;
4906 struct lod_object *lo = lod_dt_obj(dt);
4911 LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
4913 mirror_id = 0; /* non-flr file's mirror_id is 0 */
4914 if (lo->ldo_mirror_count > 1) {
4915 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4916 lod_comp = &lo->ldo_comp_entries[i];
4917 if (lod_comp->llc_id != LCME_ID_INVAL &&
4918 mirror_id_of(lod_comp->llc_id) > mirror_id)
4919 mirror_id = mirror_id_of(lod_comp->llc_id);
4923 /* create all underlying objects */
4924 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4925 lod_comp = &lo->ldo_comp_entries[i];
4927 if (lod_comp->llc_id == LCME_ID_INVAL) {
4928 /* only the component of FLR layout with more than 1
4929 * mirror has mirror ID in its component ID.
4931 if (lod_comp->llc_extent.e_start == 0 &&
4932 lo->ldo_mirror_count > 1)
4935 lod_comp->llc_id = lod_gen_component_id(lo,
4937 if (lod_comp->llc_id == LCME_ID_INVAL)
4938 GOTO(out, rc = -ERANGE);
4941 if (lod_comp_inited(lod_comp))
4944 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
4945 lod_comp_set_init(lod_comp);
4947 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
4948 lod_comp_set_init(lod_comp);
4950 if (lod_comp->llc_stripe == NULL)
4953 LASSERT(lod_comp->llc_stripe_count);
4954 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4955 struct dt_object *object = lod_comp->llc_stripe[j];
4956 LASSERT(object != NULL);
4957 rc = lod_sub_create(env, object, attr, NULL, dof, th);
4961 lod_comp_set_init(lod_comp);
4964 rc = lod_fill_mirrors(lo);
4968 rc = lod_generate_and_set_lovea(env, lo, th);
4972 lo->ldo_comp_cached = 1;
4976 lod_striping_free(env, lo);
4981 * Implementation of dt_object_operations::do_create.
4983 * If any of preceeding methods (like ->do_declare_create(),
4984 * ->do_ah_init(), etc) chose to create a striped object,
4985 * then this method will create the master and the stripes.
4987 * \see dt_object_operations::do_create() in the API description for details.
4989 static int lod_create(const struct lu_env *env, struct dt_object *dt,
4990 struct lu_attr *attr, struct dt_allocation_hint *hint,
4991 struct dt_object_format *dof, struct thandle *th)
4996 /* create local object */
4997 rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
5001 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
5002 lod_obj_is_striped(dt) && dof->u.dof_reg.striped != 0) {
5003 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
5004 rc = lod_striped_create(env, dt, attr, dof, th);
5011 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
5012 struct dt_object *dt, struct thandle *th,
5013 int comp_idx, int stripe_idx,
5014 struct lod_obj_stripe_cb_data *data)
5016 if (data->locd_declare)
5017 return lod_sub_declare_destroy(env, dt, th);
5018 else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
5019 stripe_idx == cfs_fail_val)
5020 return lod_sub_destroy(env, dt, th);
5026 * Implementation of dt_object_operations::do_declare_destroy.
5028 * If the object is a striped directory, then the function declares reference
5029 * removal from the master object (this is an index) to the stripes and declares
5030 * destroy of all the stripes. In all the cases, it declares an intention to
5031 * destroy the object itself.
5033 * \see dt_object_operations::do_declare_destroy() in the API description
5036 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
5039 struct dt_object *next = dt_object_child(dt);
5040 struct lod_object *lo = lod_dt_obj(dt);
5041 struct lod_thread_info *info = lod_env_info(env);
5042 char *stripe_name = info->lti_key;
5047 * load striping information, notice we don't do this when object
5048 * is being initialized as we don't need this information till
5049 * few specific cases like destroy, chown
5051 rc = lod_striping_load(env, lo);
5055 /* declare destroy for all underlying objects */
5056 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5057 rc = next->do_ops->do_index_try(env, next,
5058 &dt_directory_features);
5062 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5063 rc = lod_sub_declare_ref_del(env, next, th);
5067 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
5068 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
5070 rc = lod_sub_declare_delete(env, next,
5071 (const struct dt_key *)stripe_name, th);
5078 * we declare destroy for the local object
5080 rc = lod_sub_declare_destroy(env, next, th);
5084 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
5085 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
5088 if (!lod_obj_is_striped(dt))
5091 /* declare destroy all striped objects */
5092 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5093 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5094 if (lo->ldo_stripe[i] == NULL)
5097 rc = lod_sub_declare_ref_del(env, lo->ldo_stripe[i],
5100 rc = lod_sub_declare_destroy(env, lo->ldo_stripe[i],
5106 struct lod_obj_stripe_cb_data data = { { 0 } };
5108 data.locd_declare = true;
5109 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
5110 rc = lod_obj_for_each_stripe(env, lo, th, &data);
5117 * Implementation of dt_object_operations::do_destroy.
5119 * If the object is a striped directory, then the function removes references
5120 * from the master object (this is an index) to the stripes and destroys all
5121 * the stripes. In all the cases, the function destroys the object itself.
5123 * \see dt_object_operations::do_destroy() in the API description for details.
5125 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
5128 struct dt_object *next = dt_object_child(dt);
5129 struct lod_object *lo = lod_dt_obj(dt);
5130 struct lod_thread_info *info = lod_env_info(env);
5131 char *stripe_name = info->lti_key;
5136 /* destroy sub-stripe of master object */
5137 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5138 rc = next->do_ops->do_index_try(env, next,
5139 &dt_directory_features);
5143 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5144 rc = lod_sub_ref_del(env, next, th);
5148 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
5149 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
5152 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
5153 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
5154 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
5156 rc = lod_sub_delete(env, next,
5157 (const struct dt_key *)stripe_name, th);
5163 rc = lod_sub_destroy(env, next, th);
5167 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
5168 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
5171 if (!lod_obj_is_striped(dt))
5174 /* destroy all striped objects */
5175 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5176 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5177 if (lo->ldo_stripe[i] == NULL)
5179 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
5180 i == cfs_fail_val) {
5181 dt_write_lock(env, lo->ldo_stripe[i],
5183 rc = lod_sub_ref_del(env, lo->ldo_stripe[i],
5185 dt_write_unlock(env, lo->ldo_stripe[i]);
5189 rc = lod_sub_destroy(env, lo->ldo_stripe[i],
5196 struct lod_obj_stripe_cb_data data = { { 0 } };
5198 data.locd_declare = false;
5199 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
5200 rc = lod_obj_for_each_stripe(env, lo, th, &data);
5207 * Implementation of dt_object_operations::do_declare_ref_add.
5209 * \see dt_object_operations::do_declare_ref_add() in the API description
5212 static int lod_declare_ref_add(const struct lu_env *env,
5213 struct dt_object *dt, struct thandle *th)
5215 return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
5219 * Implementation of dt_object_operations::do_ref_add.
5221 * \see dt_object_operations::do_ref_add() in the API description for details.
5223 static int lod_ref_add(const struct lu_env *env,
5224 struct dt_object *dt, struct thandle *th)
5226 return lod_sub_ref_add(env, dt_object_child(dt), th);
5230 * Implementation of dt_object_operations::do_declare_ref_del.
5232 * \see dt_object_operations::do_declare_ref_del() in the API description
5235 static int lod_declare_ref_del(const struct lu_env *env,
5236 struct dt_object *dt, struct thandle *th)
5238 return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
5242 * Implementation of dt_object_operations::do_ref_del
5244 * \see dt_object_operations::do_ref_del() in the API description for details.
5246 static int lod_ref_del(const struct lu_env *env,
5247 struct dt_object *dt, struct thandle *th)
5249 return lod_sub_ref_del(env, dt_object_child(dt), th);
5253 * Implementation of dt_object_operations::do_object_sync.
5255 * \see dt_object_operations::do_object_sync() in the API description
5258 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
5259 __u64 start, __u64 end)
5261 return dt_object_sync(env, dt_object_child(dt), start, end);
5265 * Implementation of dt_object_operations::do_object_unlock.
5267 * Used to release LDLM lock(s).
5269 * \see dt_object_operations::do_object_unlock() in the API description
5272 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
5273 struct ldlm_enqueue_info *einfo,
5274 union ldlm_policy_data *policy)
5276 struct lod_object *lo = lod_dt_obj(dt);
5277 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
5278 int slave_locks_size;
5282 if (slave_locks == NULL)
5285 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
5286 LASSERT(lo->ldo_dir_stripe_count > 1);
5287 /* Note: for remote lock for single stripe dir, MDT will cancel
5288 * the lock by lockh directly */
5289 LASSERT(!dt_object_remote(dt_object_child(dt)));
5291 /* locks were unlocked in MDT layer */
5292 for (i = 0; i < slave_locks->ha_count; i++)
5293 LASSERT(!lustre_handle_is_used(&slave_locks->ha_handles[i]));
5296 * NB, ha_count may not equal to ldo_dir_stripe_count, because dir
5297 * layout may change, e.g., shrink dir layout after migration.
5299 for (i = 0; i < lo->ldo_dir_stripe_count; i++)
5300 dt_invalidate(env, lo->ldo_stripe[i]);
5302 slave_locks_size = offsetof(typeof(*slave_locks),
5303 ha_handles[slave_locks->ha_count]);
5304 OBD_FREE(slave_locks, slave_locks_size);
5305 einfo->ei_cbdata = NULL;
5311 * Implementation of dt_object_operations::do_object_lock.
5313 * Used to get LDLM lock on the non-striped and striped objects.
5315 * \see dt_object_operations::do_object_lock() in the API description
5318 static int lod_object_lock(const struct lu_env *env,
5319 struct dt_object *dt,
5320 struct lustre_handle *lh,
5321 struct ldlm_enqueue_info *einfo,
5322 union ldlm_policy_data *policy)
5324 struct lod_object *lo = lod_dt_obj(dt);
5325 int slave_locks_size;
5326 struct lustre_handle_array *slave_locks = NULL;
5331 /* remote object lock */
5332 if (!einfo->ei_enq_slave) {
5333 LASSERT(dt_object_remote(dt));
5334 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
5338 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
5341 rc = lod_striping_load(env, lo);
5346 if (lo->ldo_dir_stripe_count <= 1)
5349 slave_locks_size = offsetof(typeof(*slave_locks),
5350 ha_handles[lo->ldo_dir_stripe_count]);
5351 /* Freed in lod_object_unlock */
5352 OBD_ALLOC(slave_locks, slave_locks_size);
5355 slave_locks->ha_count = lo->ldo_dir_stripe_count;
5357 /* striped directory lock */
5358 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5359 struct lustre_handle lockh;
5360 struct ldlm_res_id *res_id;
5362 res_id = &lod_env_info(env)->lti_res_id;
5363 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
5365 einfo->ei_res_id = res_id;
5367 LASSERT(lo->ldo_stripe[i] != NULL);
5368 if (dt_object_remote(lo->ldo_stripe[i])) {
5369 set_bit(i, (void *)slave_locks->ha_map);
5370 rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh,
5373 struct ldlm_namespace *ns = einfo->ei_namespace;
5374 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
5375 ldlm_completion_callback completion = einfo->ei_cb_cp;
5376 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
5378 if (einfo->ei_mode == LCK_PW ||
5379 einfo->ei_mode == LCK_EX)
5380 dlmflags |= LDLM_FL_COS_INCOMPAT;
5382 LASSERT(ns != NULL);
5383 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS,
5384 policy, einfo->ei_mode,
5385 &dlmflags, blocking,
5387 NULL, 0, LVB_T_NONE,
5392 ldlm_lock_decref_and_cancel(
5393 &slave_locks->ha_handles[i],
5395 OBD_FREE(slave_locks, slave_locks_size);
5398 slave_locks->ha_handles[i] = lockh;
5400 einfo->ei_cbdata = slave_locks;
5406 * Implementation of dt_object_operations::do_invalidate.
5408 * \see dt_object_operations::do_invalidate() in the API description for details
5410 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
5412 return dt_invalidate(env, dt_object_child(dt));
5415 static int lod_layout_data_init(struct lod_thread_info *info, __u32 comp_cnt)
5419 /* clear memory region that will be used for layout change */
5420 memset(&info->lti_layout_attr, 0, sizeof(struct lu_attr));
5421 info->lti_count = 0;
5423 if (info->lti_comp_size >= comp_cnt)
5426 if (info->lti_comp_size > 0) {
5427 OBD_FREE(info->lti_comp_idx,
5428 info->lti_comp_size * sizeof(__u32));
5429 info->lti_comp_size = 0;
5432 OBD_ALLOC(info->lti_comp_idx, comp_cnt * sizeof(__u32));
5433 if (!info->lti_comp_idx)
5436 info->lti_comp_size = comp_cnt;
5440 static int lod_declare_instantiate_components(const struct lu_env *env,
5441 struct lod_object *lo, struct thandle *th)
5443 struct lod_thread_info *info = lod_env_info(env);
5448 LASSERT(info->lti_count < lo->ldo_comp_cnt);
5450 for (i = 0; i < info->lti_count; i++) {
5451 rc = lod_qos_prep_create(env, lo, NULL, th,
5452 info->lti_comp_idx[i]);
5458 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5459 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
5460 &info->lti_buf, XATTR_NAME_LOV, 0, th);
5466 static int lod_declare_update_plain(const struct lu_env *env,
5467 struct lod_object *lo, struct layout_intent *layout,
5468 const struct lu_buf *buf, struct thandle *th)
5470 struct lod_thread_info *info = lod_env_info(env);
5471 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5472 struct lod_layout_component *lod_comp;
5473 struct lov_comp_md_v1 *comp_v1 = NULL;
5474 bool replay = false;
5478 LASSERT(lo->ldo_flr_state == LCM_FL_NONE);
5481 * In case the client is passing lovea, which only happens during
5482 * the replay of layout intent write RPC for now, we may need to
5483 * parse the lovea and apply new layout configuration.
5485 if (buf && buf->lb_len) {
5486 struct lov_user_md_v1 *v1 = buf->lb_buf;
5488 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
5489 v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
5490 LOV_MAGIC_COMP_V1)) {
5491 CERROR("%s: the replay buffer of layout extend "
5492 "(magic %#x) does not contain expected "
5493 "composite layout.\n",
5494 lod2obd(d)->obd_name, v1->lmm_magic);
5495 GOTO(out, rc = -EINVAL);
5498 rc = lod_use_defined_striping(env, lo, buf);
5502 rc = lod_get_lov_ea(env, lo);
5505 /* old on-disk EA is stored in info->lti_buf */
5506 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
5509 /* non replay path */
5510 rc = lod_striping_load(env, lo);
5515 /* Make sure defined layout covers the requested write range. */
5516 lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
5517 if (lo->ldo_comp_cnt > 1 &&
5518 lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
5519 lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
5520 CDEBUG(replay ? D_ERROR : D_LAYOUT,
5521 "%s: the defined layout [0, %#llx) does not covers "
5522 "the write range "DEXT"\n",
5523 lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
5524 PEXT(&layout->li_extent));
5525 GOTO(out, rc = -EINVAL);
5528 CDEBUG(D_LAYOUT, "%s: "DFID": instantiate components "DEXT"\n",
5529 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
5530 PEXT(&layout->li_extent));
5533 * Iterate ld->ldo_comp_entries, find the component whose extent under
5534 * the write range and not instantianted.
5536 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5537 lod_comp = &lo->ldo_comp_entries[i];
5539 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
5543 if (lod_comp_inited(lod_comp))
5547 * In replay path, lod_comp is the EA passed by
5548 * client replay buffer, comp_v1 is the pre-recovery
5549 * on-disk EA, we'd sift out those components which
5550 * were init-ed in the on-disk EA.
5552 if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
5557 * this component hasn't instantiated in normal path, or during
5558 * replay it needs replay the instantiation.
5561 /* A released component is being extended */
5562 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
5563 GOTO(out, rc = -EINVAL);
5565 LASSERT(info->lti_comp_idx != NULL);
5566 info->lti_comp_idx[info->lti_count++] = i;
5569 if (info->lti_count == 0)
5572 lod_obj_inc_layout_gen(lo);
5573 rc = lod_declare_instantiate_components(env, lo, th);
5576 lod_striping_free(env, lo);
5580 static inline int lod_comp_index(struct lod_object *lo,
5581 struct lod_layout_component *lod_comp)
5583 LASSERT(lod_comp >= lo->ldo_comp_entries &&
5584 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
5586 return lod_comp - lo->ldo_comp_entries;
5590 * Stale other mirrors by writing extent.
5592 static void lod_stale_components(struct lod_object *lo, int primary,
5593 struct lu_extent *extent)
5595 struct lod_layout_component *pri_comp, *lod_comp;
5598 /* The writing extent decides which components in the primary
5599 * are affected... */
5600 CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
5601 lod_foreach_mirror_comp(pri_comp, lo, primary) {
5602 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
5605 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
5606 lod_comp_index(lo, pri_comp),
5607 PEXT(&pri_comp->llc_extent));
5609 for (i = 0; i < lo->ldo_mirror_count; i++) {
5613 /* ... and then stale other components that are
5614 * overlapping with primary components */
5615 lod_foreach_mirror_comp(lod_comp, lo, i) {
5616 if (!lu_extent_is_overlapped(
5617 &pri_comp->llc_extent,
5618 &lod_comp->llc_extent))
5621 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
5622 i, lod_comp_index(lo, lod_comp));
5624 lod_comp->llc_flags |= LCME_FL_STALE;
5625 lo->ldo_mirrors[i].lme_stale = 1;
5632 * check an OST's availability
5633 * \param[in] env execution environment
5634 * \param[in] lo lod object
5635 * \param[in] dt dt object
5636 * \param[in] index mirror index
5638 * \retval negative if failed
5639 * \retval 1 if \a dt is available
5640 * \retval 0 if \a dt is not available
5642 static inline int lod_check_ost_avail(const struct lu_env *env,
5643 struct lod_object *lo,
5644 struct dt_object *dt, int index)
5646 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5647 struct lod_tgt_desc *ost;
5649 int type = LU_SEQ_RANGE_OST;
5652 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &idx, &type);
5654 CERROR("%s: can't locate "DFID":rc = %d\n",
5655 lod2obd(lod)->obd_name, PFID(lu_object_fid(&dt->do_lu)),
5660 ost = OST_TGT(lod, idx);
5661 if (ost->ltd_statfs.os_state &
5662 (OS_STATE_READONLY | OS_STATE_ENOSPC | OS_STATE_ENOINO |
5663 OS_STATE_NOPRECREATE) ||
5664 ost->ltd_active == 0) {
5665 CDEBUG(D_LAYOUT, DFID ": mirror %d OST%d unavail, rc = %d\n",
5666 PFID(lod_object_fid(lo)), index, idx, rc);
5674 * Pick primary mirror for write
5675 * \param[in] env execution environment
5676 * \param[in] lo object
5677 * \param[in] extent write range
5679 static int lod_primary_pick(const struct lu_env *env, struct lod_object *lo,
5680 struct lu_extent *extent)
5682 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5683 unsigned int seq = 0;
5684 struct lod_layout_component *lod_comp;
5686 int picked = -1, second_pick = -1, third_pick = -1;
5689 if (OBD_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
5690 get_random_bytes(&seq, sizeof(seq));
5691 seq %= lo->ldo_mirror_count;
5695 * Pick a mirror as the primary, and check the availability of OSTs.
5697 * This algo can be revised later after knowing the topology of
5700 lod_qos_statfs_update(env, lod);
5701 for (i = 0; i < lo->ldo_mirror_count; i++) {
5702 bool ost_avail = true;
5703 int index = (i + seq) % lo->ldo_mirror_count;
5705 if (lo->ldo_mirrors[index].lme_stale) {
5706 CDEBUG(D_LAYOUT, DFID": mirror %d stale\n",
5707 PFID(lod_object_fid(lo)), index);
5711 /* 2nd pick is for the primary mirror containing unavail OST */
5712 if (lo->ldo_mirrors[index].lme_primary && second_pick < 0)
5713 second_pick = index;
5715 /* 3rd pick is for non-primary mirror containing unavail OST */
5716 if (second_pick < 0 && third_pick < 0)
5720 * we found a non-primary 1st pick, we'd like to find a
5721 * potential pirmary mirror.
5723 if (picked >= 0 && !lo->ldo_mirrors[index].lme_primary)
5726 /* check the availability of OSTs */
5727 lod_foreach_mirror_comp(lod_comp, lo, index) {
5728 if (!lod_comp_inited(lod_comp) || !lod_comp->llc_stripe)
5731 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
5732 struct dt_object *dt = lod_comp->llc_stripe[j];
5734 rc = lod_check_ost_avail(env, lo, dt, index);
5741 } /* for all dt object in one component */
5744 } /* for all components in a mirror */
5747 * the OSTs where allocated objects locates in the components
5748 * of the mirror are available.
5753 /* this mirror has all OSTs available */
5757 * primary with all OSTs are available, this is the perfect
5760 if (lo->ldo_mirrors[index].lme_primary)
5762 } /* for all mirrors */
5764 /* failed to pick a sound mirror, lower our expectation */
5766 picked = second_pick;
5768 picked = third_pick;
5776 * figure out the components should be instantiated for resync.
5778 static int lod_prepare_resync(const struct lu_env *env, struct lod_object *lo,
5779 struct lu_extent *extent)
5781 struct lod_thread_info *info = lod_env_info(env);
5782 struct lod_layout_component *lod_comp;
5783 unsigned int need_sync = 0;
5787 DFID": instantiate all stale components in "DEXT"\n",
5788 PFID(lod_object_fid(lo)), PEXT(extent));
5791 * instantiate all components within this extent, even non-stale
5794 for (i = 0; i < lo->ldo_mirror_count; i++) {
5795 if (!lo->ldo_mirrors[i].lme_stale)
5798 lod_foreach_mirror_comp(lod_comp, lo, i) {
5799 if (!lu_extent_is_overlapped(extent,
5800 &lod_comp->llc_extent))
5805 if (lod_comp_inited(lod_comp))
5808 CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
5809 i, lod_comp_index(lo, lod_comp));
5810 info->lti_comp_idx[info->lti_count++] =
5811 lod_comp_index(lo, lod_comp);
5815 return need_sync ? 0 : -EALREADY;
5818 static int lod_declare_update_rdonly(const struct lu_env *env,
5819 struct lod_object *lo, struct md_layout_change *mlc,
5822 struct lod_thread_info *info = lod_env_info(env);
5823 struct lu_attr *layout_attr = &info->lti_layout_attr;
5824 struct lod_layout_component *lod_comp;
5825 struct lu_extent extent = { 0 };
5829 LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
5830 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
5831 mlc->mlc_opc == MD_LAYOUT_RESYNC);
5832 LASSERT(lo->ldo_mirror_count > 0);
5834 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
5835 struct layout_intent *layout = mlc->mlc_intent;
5838 extent = layout->li_extent;
5839 CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
5840 PFID(lod_object_fid(lo)), PEXT(&extent));
5842 picked = lod_primary_pick(env, lo, &extent);
5846 CDEBUG(D_LAYOUT, DFID": picked mirror id %u as primary\n",
5847 PFID(lod_object_fid(lo)),
5848 lo->ldo_mirrors[picked].lme_id);
5850 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
5852 * trunc transfers [0, size) in the intent extent, we'd
5853 * stale components overlapping [size, eof).
5855 extent.e_start = extent.e_end;
5856 extent.e_end = OBD_OBJECT_EOF;
5859 /* stale overlapping components from other mirrors */
5860 lod_stale_components(lo, picked, &extent);
5862 /* restore truncate intent extent */
5863 if (layout->li_opc == LAYOUT_INTENT_TRUNC)
5864 extent.e_end = extent.e_start;
5866 /* instantiate components for the picked mirror, start from 0 */
5869 lod_foreach_mirror_comp(lod_comp, lo, picked) {
5870 if (!lu_extent_is_overlapped(&extent,
5871 &lod_comp->llc_extent))
5874 if (lod_comp_inited(lod_comp))
5877 info->lti_comp_idx[info->lti_count++] =
5878 lod_comp_index(lo, lod_comp);
5881 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
5882 } else { /* MD_LAYOUT_RESYNC */
5886 * could contain multiple non-stale mirrors, so we need to
5887 * prep uninited all components assuming any non-stale mirror
5888 * could be picked as the primary mirror.
5890 for (i = 0; i < lo->ldo_mirror_count; i++) {
5891 if (lo->ldo_mirrors[i].lme_stale)
5894 lod_foreach_mirror_comp(lod_comp, lo, i) {
5895 if (!lod_comp_inited(lod_comp))
5898 if (extent.e_end < lod_comp->llc_extent.e_end)
5900 lod_comp->llc_extent.e_end;
5904 rc = lod_prepare_resync(env, lo, &extent);
5907 /* change the file state to SYNC_PENDING */
5908 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
5911 /* Reset the layout version once it's becoming too large.
5912 * This way it can make sure that the layout version is
5913 * monotonously increased in this writing era. */
5914 lod_obj_inc_layout_gen(lo);
5915 if (lo->ldo_layout_gen > (LCME_ID_MAX >> 1)) {
5916 __u32 layout_version;
5918 cfs_get_random_bytes(&layout_version, sizeof(layout_version));
5919 lo->ldo_layout_gen = layout_version & 0xffff;
5922 rc = lod_declare_instantiate_components(env, lo, th);
5926 layout_attr->la_valid = LA_LAYOUT_VERSION;
5927 layout_attr->la_layout_version = 0; /* set current version */
5928 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
5929 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
5930 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
5936 lod_striping_free(env, lo);
5940 static int lod_declare_update_write_pending(const struct lu_env *env,
5941 struct lod_object *lo, struct md_layout_change *mlc,
5944 struct lod_thread_info *info = lod_env_info(env);
5945 struct lu_attr *layout_attr = &info->lti_layout_attr;
5946 struct lod_layout_component *lod_comp;
5947 struct lu_extent extent = { 0 };
5953 LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
5954 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
5955 mlc->mlc_opc == MD_LAYOUT_RESYNC);
5957 /* look for the primary mirror */
5958 for (i = 0; i < lo->ldo_mirror_count; i++) {
5959 if (lo->ldo_mirrors[i].lme_stale)
5962 LASSERTF(primary < 0, DFID " has multiple primary: %u / %u",
5963 PFID(lod_object_fid(lo)),
5964 lo->ldo_mirrors[i].lme_id,
5965 lo->ldo_mirrors[primary].lme_id);
5970 CERROR(DFID ": doesn't have a primary mirror\n",
5971 PFID(lod_object_fid(lo)));
5972 GOTO(out, rc = -ENODATA);
5975 CDEBUG(D_LAYOUT, DFID": found primary %u\n",
5976 PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
5978 LASSERT(!lo->ldo_mirrors[primary].lme_stale);
5980 /* for LAYOUT_WRITE opc, it has to do the following operations:
5981 * 1. stale overlapping componets from stale mirrors;
5982 * 2. instantiate components of the primary mirror;
5983 * 3. transfter layout version to all objects of the primary;
5985 * for LAYOUT_RESYNC opc, it will do:
5986 * 1. instantiate components of all stale mirrors;
5987 * 2. transfer layout version to all objects to close write era. */
5989 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
5990 LASSERT(mlc->mlc_intent != NULL);
5992 extent = mlc->mlc_intent->li_extent;
5994 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
5995 PFID(lod_object_fid(lo)), PEXT(&extent));
5997 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
5999 * trunc transfers [0, size) in the intent extent, we'd
6000 * stale components overlapping [size, eof).
6002 extent.e_start = extent.e_end;
6003 extent.e_end = OBD_OBJECT_EOF;
6005 /* 1. stale overlapping components */
6006 lod_stale_components(lo, primary, &extent);
6008 /* 2. find out the components need instantiating.
6009 * instantiate [0, mlc->mlc_intent->e_end) */
6011 /* restore truncate intent extent */
6012 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC)
6013 extent.e_end = extent.e_start;
6016 lod_foreach_mirror_comp(lod_comp, lo, primary) {
6017 if (!lu_extent_is_overlapped(&extent,
6018 &lod_comp->llc_extent))
6021 if (lod_comp_inited(lod_comp))
6024 CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
6025 primary, lod_comp_index(lo, lod_comp));
6026 info->lti_comp_idx[info->lti_count++] =
6027 lod_comp_index(lo, lod_comp);
6029 } else { /* MD_LAYOUT_RESYNC */
6030 lod_foreach_mirror_comp(lod_comp, lo, primary) {
6031 if (!lod_comp_inited(lod_comp))
6034 extent.e_end = lod_comp->llc_extent.e_end;
6037 rc = lod_prepare_resync(env, lo, &extent);
6040 /* change the file state to SYNC_PENDING */
6041 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
6044 rc = lod_declare_instantiate_components(env, lo, th);
6048 /* 3. transfer layout version to OST objects.
6049 * transfer new layout version to OST objects so that stale writes
6050 * can be denied. It also ends an era of writing by setting
6051 * LU_LAYOUT_RESYNC. Normal client can never use this bit to
6052 * send write RPC; only resync RPCs could do it. */
6053 layout_attr->la_valid = LA_LAYOUT_VERSION;
6054 layout_attr->la_layout_version = 0; /* set current version */
6055 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
6056 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
6057 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
6061 lod_obj_inc_layout_gen(lo);
6064 lod_striping_free(env, lo);
6068 static int lod_declare_update_sync_pending(const struct lu_env *env,
6069 struct lod_object *lo, struct md_layout_change *mlc,
6072 struct lod_thread_info *info = lod_env_info(env);
6073 unsigned sync_components = 0;
6074 unsigned resync_components = 0;
6079 LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
6080 LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
6081 mlc->mlc_opc == MD_LAYOUT_WRITE);
6083 CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
6084 PFID(lod_object_fid(lo)), mlc->mlc_opc);
6086 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
6087 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
6088 PFID(lod_object_fid(lo)));
6090 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
6091 return lod_declare_update_write_pending(env, lo, mlc, th);
6094 /* MD_LAYOUT_RESYNC_DONE */
6096 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6097 struct lod_layout_component *lod_comp;
6100 lod_comp = &lo->ldo_comp_entries[i];
6102 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
6107 for (j = 0; j < mlc->mlc_resync_count; j++) {
6108 if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
6111 mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
6112 lod_comp->llc_flags &= ~LCME_FL_STALE;
6113 resync_components++;
6119 for (i = 0; i < mlc->mlc_resync_count; i++) {
6120 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
6123 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
6124 "or already synced\n", PFID(lod_object_fid(lo)),
6125 mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
6126 GOTO(out, rc = -EINVAL);
6129 if (!sync_components || (mlc->mlc_resync_count && !resync_components)) {
6130 CDEBUG(D_LAYOUT, DFID": no mirror in sync\n",
6131 PFID(lod_object_fid(lo)));
6133 /* tend to return an error code here to prevent
6134 * the MDT from setting SoM attribute */
6135 GOTO(out, rc = -EINVAL);
6138 CDEBUG(D_LAYOUT, DFID": resynced %u/%zu components\n",
6139 PFID(lod_object_fid(lo)),
6140 resync_components, mlc->mlc_resync_count);
6142 lo->ldo_flr_state = LCM_FL_RDONLY;
6143 lod_obj_inc_layout_gen(lo);
6145 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6146 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
6147 &info->lti_buf, XATTR_NAME_LOV, 0, th);
6152 lod_striping_free(env, lo);
6156 static int lod_declare_layout_change(const struct lu_env *env,
6157 struct dt_object *dt, struct md_layout_change *mlc,
6160 struct lod_thread_info *info = lod_env_info(env);
6161 struct lod_object *lo = lod_dt_obj(dt);
6165 if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
6166 dt_object_remote(dt_object_child(dt)))
6169 rc = lod_striping_load(env, lo);
6173 LASSERT(lo->ldo_comp_cnt > 0);
6175 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
6179 switch (lo->ldo_flr_state) {
6181 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
6185 rc = lod_declare_update_rdonly(env, lo, mlc, th);
6187 case LCM_FL_WRITE_PENDING:
6188 rc = lod_declare_update_write_pending(env, lo, mlc, th);
6190 case LCM_FL_SYNC_PENDING:
6191 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
6202 * Instantiate layout component objects which covers the intent write offset.
6204 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
6205 struct md_layout_change *mlc, struct thandle *th)
6207 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
6208 struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
6209 struct lod_object *lo = lod_dt_obj(dt);
6212 rc = lod_striped_create(env, dt, attr, NULL, th);
6213 if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
6214 layout_attr->la_layout_version |= lo->ldo_layout_gen;
6215 rc = lod_attr_set(env, dt, layout_attr, th);
6221 struct dt_object_operations lod_obj_ops = {
6222 .do_read_lock = lod_read_lock,
6223 .do_write_lock = lod_write_lock,
6224 .do_read_unlock = lod_read_unlock,
6225 .do_write_unlock = lod_write_unlock,
6226 .do_write_locked = lod_write_locked,
6227 .do_attr_get = lod_attr_get,
6228 .do_declare_attr_set = lod_declare_attr_set,
6229 .do_attr_set = lod_attr_set,
6230 .do_xattr_get = lod_xattr_get,
6231 .do_declare_xattr_set = lod_declare_xattr_set,
6232 .do_xattr_set = lod_xattr_set,
6233 .do_declare_xattr_del = lod_declare_xattr_del,
6234 .do_xattr_del = lod_xattr_del,
6235 .do_xattr_list = lod_xattr_list,
6236 .do_ah_init = lod_ah_init,
6237 .do_declare_create = lod_declare_create,
6238 .do_create = lod_create,
6239 .do_declare_destroy = lod_declare_destroy,
6240 .do_destroy = lod_destroy,
6241 .do_index_try = lod_index_try,
6242 .do_declare_ref_add = lod_declare_ref_add,
6243 .do_ref_add = lod_ref_add,
6244 .do_declare_ref_del = lod_declare_ref_del,
6245 .do_ref_del = lod_ref_del,
6246 .do_object_sync = lod_object_sync,
6247 .do_object_lock = lod_object_lock,
6248 .do_object_unlock = lod_object_unlock,
6249 .do_invalidate = lod_invalidate,
6250 .do_declare_layout_change = lod_declare_layout_change,
6251 .do_layout_change = lod_layout_change,
6255 * Implementation of dt_body_operations::dbo_read.
6257 * \see dt_body_operations::dbo_read() in the API description for details.
6259 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
6260 struct lu_buf *buf, loff_t *pos)
6262 struct dt_object *next = dt_object_child(dt);
6264 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
6265 S_ISLNK(dt->do_lu.lo_header->loh_attr));
6266 return next->do_body_ops->dbo_read(env, next, buf, pos);
6270 * Implementation of dt_body_operations::dbo_declare_write.
6272 * \see dt_body_operations::dbo_declare_write() in the API description
6275 static ssize_t lod_declare_write(const struct lu_env *env,
6276 struct dt_object *dt,
6277 const struct lu_buf *buf, loff_t pos,
6280 return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
6284 * Implementation of dt_body_operations::dbo_write.
6286 * \see dt_body_operations::dbo_write() in the API description for details.
6288 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
6289 const struct lu_buf *buf, loff_t *pos,
6290 struct thandle *th, int iq)
6292 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
6293 S_ISLNK(dt->do_lu.lo_header->loh_attr));
6294 return lod_sub_write(env, dt_object_child(dt), buf, pos, th, iq);
6297 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
6298 __u64 start, __u64 end, struct thandle *th)
6300 if (dt_object_remote(dt))
6303 return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
6306 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
6307 __u64 start, __u64 end, struct thandle *th)
6309 if (dt_object_remote(dt))
6312 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
6313 return lod_sub_punch(env, dt_object_child(dt), start, end, th);
6317 * different type of files use the same body_ops because object may be created
6318 * in OUT, where there is no chance to set correct body_ops for each type, so
6319 * body_ops themselves will check file type inside, see lod_read/write/punch for
6322 const struct dt_body_operations lod_body_ops = {
6323 .dbo_read = lod_read,
6324 .dbo_declare_write = lod_declare_write,
6325 .dbo_write = lod_write,
6326 .dbo_declare_punch = lod_declare_punch,
6327 .dbo_punch = lod_punch,
6331 * Implementation of lu_object_operations::loo_object_init.
6333 * The function determines the type and the index of the target device using
6334 * sequence of the object's FID. Then passes control down to the
6335 * corresponding device:
6336 * OSD for the local objects, OSP for remote
6338 * \see lu_object_operations::loo_object_init() in the API description
6341 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
6342 const struct lu_object_conf *conf)
6344 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
6345 struct lu_device *cdev = NULL;
6346 struct lu_object *cobj;
6347 struct lod_tgt_descs *ltd = NULL;
6348 struct lod_tgt_desc *tgt;
6350 int type = LU_SEQ_RANGE_ANY;
6354 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
6356 /* Note: Sometimes, it will Return EAGAIN here, see
6357 * ptrlpc_import_delay_req(), which might confuse
6358 * lu_object_find_at() and make it wait there incorrectly.
6359 * so we convert it to EIO here.*/
6366 if (type == LU_SEQ_RANGE_MDT &&
6367 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
6368 cdev = &lod->lod_child->dd_lu_dev;
6369 } else if (type == LU_SEQ_RANGE_MDT) {
6370 ltd = &lod->lod_mdt_descs;
6372 } else if (type == LU_SEQ_RANGE_OST) {
6373 ltd = &lod->lod_ost_descs;
6380 if (ltd->ltd_tgts_size > idx &&
6381 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
6382 tgt = LTD_TGT(ltd, idx);
6384 LASSERT(tgt != NULL);
6385 LASSERT(tgt->ltd_tgt != NULL);
6387 cdev = &(tgt->ltd_tgt->dd_lu_dev);
6389 lod_putref(lod, ltd);
6392 if (unlikely(cdev == NULL))
6395 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
6396 if (unlikely(cobj == NULL))
6399 lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
6401 lu_object_add(lo, cobj);
6408 * Release resources associated with striping.
6410 * If the object is striped (regular or directory), then release
6411 * the stripe objects references and free the ldo_stripe array.
6413 * \param[in] env execution environment
6414 * \param[in] lo object
6416 void lod_striping_free_nolock(const struct lu_env *env, struct lod_object *lo)
6418 struct lod_layout_component *lod_comp;
6421 if (lo->ldo_stripe != NULL) {
6422 LASSERT(lo->ldo_comp_entries == NULL);
6423 LASSERT(lo->ldo_dir_stripes_allocated > 0);
6425 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6426 if (lo->ldo_stripe[i])
6427 dt_object_put(env, lo->ldo_stripe[i]);
6430 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
6431 OBD_FREE(lo->ldo_stripe, j);
6432 lo->ldo_stripe = NULL;
6433 lo->ldo_dir_stripes_allocated = 0;
6434 lo->ldo_dir_stripe_loaded = 0;
6435 lo->ldo_dir_stripe_count = 0;
6436 } else if (lo->ldo_comp_entries != NULL) {
6437 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6438 /* free lod_layout_component::llc_stripe array */
6439 lod_comp = &lo->ldo_comp_entries[i];
6441 if (lod_comp->llc_stripe == NULL)
6443 LASSERT(lod_comp->llc_stripes_allocated != 0);
6444 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
6445 if (lod_comp->llc_stripe[j] != NULL)
6447 &lod_comp->llc_stripe[j]->do_lu);
6449 OBD_FREE(lod_comp->llc_stripe,
6450 sizeof(struct dt_object *) *
6451 lod_comp->llc_stripes_allocated);
6452 lod_comp->llc_stripe = NULL;
6453 OBD_FREE(lod_comp->llc_ost_indices,
6455 lod_comp->llc_stripes_allocated);
6456 lod_comp->llc_ost_indices = NULL;
6457 lod_comp->llc_stripes_allocated = 0;
6459 lod_free_comp_entries(lo);
6460 lo->ldo_comp_cached = 0;
6464 void lod_striping_free(const struct lu_env *env, struct lod_object *lo)
6466 mutex_lock(&lo->ldo_layout_mutex);
6467 lod_striping_free_nolock(env, lo);
6468 mutex_unlock(&lo->ldo_layout_mutex);
6472 * Implementation of lu_object_operations::loo_object_free.
6474 * \see lu_object_operations::loo_object_free() in the API description
6477 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
6479 struct lod_object *lo = lu2lod_obj(o);
6481 /* release all underlying object pinned */
6482 lod_striping_free(env, lo);
6484 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
6488 * Implementation of lu_object_operations::loo_object_release.
6490 * \see lu_object_operations::loo_object_release() in the API description
6493 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
6495 /* XXX: shouldn't we release everything here in case if object
6496 * creation failed before? */
6500 * Implementation of lu_object_operations::loo_object_print.
6502 * \see lu_object_operations::loo_object_print() in the API description
6505 static int lod_object_print(const struct lu_env *env, void *cookie,
6506 lu_printer_t p, const struct lu_object *l)
6508 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
6510 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
6513 struct lu_object_operations lod_lu_obj_ops = {
6514 .loo_object_init = lod_object_init,
6515 .loo_object_free = lod_object_free,
6516 .loo_object_release = lod_object_release,
6517 .loo_object_print = lod_object_print,