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_load_striping_locked(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 LASSERT(lod_comp->llc_stripe_count > 0);
1089 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
1090 struct dt_object *dt = lod_comp->llc_stripe[j];
1094 rc = data->locd_stripe_cb(env, lo, dt, th, i, j, data);
1102 static bool lod_obj_attr_set_comp_skip_cb(const struct lu_env *env,
1103 struct lod_object *lo, int comp_idx,
1104 struct lod_obj_stripe_cb_data *data)
1106 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
1107 bool skipped = false;
1109 if (!(data->locd_attr->la_valid & LA_LAYOUT_VERSION))
1112 switch (lo->ldo_flr_state) {
1113 case LCM_FL_WRITE_PENDING: {
1116 /* skip stale components */
1117 if (lod_comp->llc_flags & LCME_FL_STALE) {
1122 /* skip valid and overlapping components, therefore any
1123 * attempts to write overlapped components will never succeed
1124 * because client will get EINPROGRESS. */
1125 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1129 if (lo->ldo_comp_entries[i].llc_flags & LCME_FL_STALE)
1132 if (lu_extent_is_overlapped(&lod_comp->llc_extent,
1133 &lo->ldo_comp_entries[i].llc_extent)) {
1141 LASSERTF(0, "impossible: %d\n", lo->ldo_flr_state);
1142 case LCM_FL_SYNC_PENDING:
1146 CDEBUG(D_LAYOUT, DFID": %s to set component %x to version: %u\n",
1147 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
1148 skipped ? "skipped" : "chose", lod_comp->llc_id,
1149 data->locd_attr->la_layout_version);
1155 lod_obj_stripe_attr_set_cb(const struct lu_env *env, struct lod_object *lo,
1156 struct dt_object *dt, struct thandle *th,
1157 int comp_idx, int stripe_idx,
1158 struct lod_obj_stripe_cb_data *data)
1160 if (data->locd_declare)
1161 return lod_sub_declare_attr_set(env, dt, data->locd_attr, th);
1163 if (data->locd_attr->la_valid & LA_LAYOUT_VERSION) {
1164 CDEBUG(D_LAYOUT, DFID": set layout version: %u, comp_idx: %d\n",
1165 PFID(lu_object_fid(&dt->do_lu)),
1166 data->locd_attr->la_layout_version, comp_idx);
1169 return lod_sub_attr_set(env, dt, data->locd_attr, th);
1173 * Implementation of dt_object_operations::do_declare_attr_set.
1175 * If the object is striped, then apply the changes to all the stripes.
1177 * \see dt_object_operations::do_declare_attr_set() in the API description
1180 static int lod_declare_attr_set(const struct lu_env *env,
1181 struct dt_object *dt,
1182 const struct lu_attr *attr,
1185 struct dt_object *next = dt_object_child(dt);
1186 struct lod_object *lo = lod_dt_obj(dt);
1191 * declare setattr on the local object
1193 rc = lod_sub_declare_attr_set(env, next, attr, th);
1197 /* osp_declare_attr_set() ignores all attributes other than
1198 * UID, GID, PROJID, and size, and osp_attr_set() ignores all
1199 * but UID, GID and PROJID. Declaration of size attr setting
1200 * happens through lod_declare_init_size(), and not through
1201 * this function. Therefore we need not load striping unless
1202 * ownership is changing. This should save memory and (we hope)
1203 * speed up rename().
1205 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1206 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1209 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1212 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID | LA_MODE |
1213 LA_ATIME | LA_MTIME | LA_CTIME |
1218 * load striping information, notice we don't do this when object
1219 * is being initialized as we don't need this information till
1220 * few specific cases like destroy, chown
1222 rc = lod_load_striping(env, lo);
1226 if (!lod_obj_is_striped(dt))
1230 * if object is striped declare changes on the stripes
1232 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1233 LASSERT(lo->ldo_stripe);
1234 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1235 if (lo->ldo_stripe[i] == NULL)
1237 rc = lod_sub_declare_attr_set(env, lo->ldo_stripe[i],
1243 struct lod_obj_stripe_cb_data data = { { 0 } };
1245 data.locd_attr = attr;
1246 data.locd_declare = true;
1247 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1248 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1254 if (!dt_object_exists(next) || dt_object_remote(next) ||
1255 !S_ISREG(attr->la_mode))
1258 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1259 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
1263 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1264 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1265 struct lod_thread_info *info = lod_env_info(env);
1266 struct lu_buf *buf = &info->lti_buf;
1268 buf->lb_buf = info->lti_ea_store;
1269 buf->lb_len = info->lti_ea_store_size;
1270 rc = lod_sub_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1271 LU_XATTR_REPLACE, th);
1278 * Implementation of dt_object_operations::do_attr_set.
1280 * If the object is striped, then apply the changes to all or subset of
1281 * the stripes depending on the object type and specific attributes.
1283 * \see dt_object_operations::do_attr_set() in the API description for details.
1285 static int lod_attr_set(const struct lu_env *env,
1286 struct dt_object *dt,
1287 const struct lu_attr *attr,
1290 struct dt_object *next = dt_object_child(dt);
1291 struct lod_object *lo = lod_dt_obj(dt);
1296 * apply changes to the local object
1298 rc = lod_sub_attr_set(env, next, attr, th);
1302 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1303 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1306 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1309 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE | LA_PROJID |
1310 LA_ATIME | LA_MTIME | LA_CTIME |
1315 /* FIXME: a tricky case in the code path of mdd_layout_change():
1316 * the in-memory striping information has been freed in lod_xattr_set()
1317 * due to layout change. It has to load stripe here again. It only
1318 * changes flags of layout so declare_attr_set() is still accurate */
1319 rc = lod_load_striping_locked(env, lo);
1323 if (!lod_obj_is_striped(dt))
1327 * if object is striped, apply changes to all the stripes
1329 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1330 LASSERT(lo->ldo_stripe);
1331 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1332 if (unlikely(lo->ldo_stripe[i] == NULL))
1335 if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1338 rc = lod_sub_attr_set(env, lo->ldo_stripe[i], attr, th);
1343 struct lod_obj_stripe_cb_data data = { { 0 } };
1345 data.locd_attr = attr;
1346 data.locd_declare = false;
1347 data.locd_comp_skip_cb = lod_obj_attr_set_comp_skip_cb;
1348 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1349 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1355 if (!dt_object_exists(next) || dt_object_remote(next) ||
1356 !S_ISREG(attr->la_mode))
1359 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1360 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
1364 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1365 struct lod_thread_info *info = lod_env_info(env);
1366 struct lu_buf *buf = &info->lti_buf;
1367 struct ost_id *oi = &info->lti_ostid;
1368 struct lu_fid *fid = &info->lti_fid;
1369 struct lov_mds_md_v1 *lmm;
1370 struct lov_ost_data_v1 *objs;
1373 rc = lod_get_lov_ea(env, lo);
1377 buf->lb_buf = info->lti_ea_store;
1378 buf->lb_len = info->lti_ea_store_size;
1379 lmm = info->lti_ea_store;
1380 magic = le32_to_cpu(lmm->lmm_magic);
1381 if (magic == LOV_MAGIC_COMP_V1) {
1382 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1383 struct lov_comp_md_entry_v1 *lcme =
1384 &lcm->lcm_entries[0];
1386 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1387 magic = le32_to_cpu(lmm->lmm_magic);
1390 if (magic == LOV_MAGIC_V1)
1391 objs = &(lmm->lmm_objects[0]);
1393 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1394 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1395 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1397 fid_to_ostid(fid, oi);
1398 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1400 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1401 LU_XATTR_REPLACE, th);
1402 } else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1403 struct lod_thread_info *info = lod_env_info(env);
1404 struct lu_buf *buf = &info->lti_buf;
1405 struct lov_comp_md_v1 *lcm;
1406 struct lov_comp_md_entry_v1 *lcme;
1408 rc = lod_get_lov_ea(env, lo);
1412 buf->lb_buf = info->lti_ea_store;
1413 buf->lb_len = info->lti_ea_store_size;
1415 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
1418 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1419 lcme = &lcm->lcm_entries[0];
1420 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1421 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1423 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1424 LU_XATTR_REPLACE, th);
1431 * Implementation of dt_object_operations::do_xattr_get.
1433 * If LOV EA is requested from the root object and it's not
1434 * found, then return default striping for the filesystem.
1436 * \see dt_object_operations::do_xattr_get() in the API description for details.
1438 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1439 struct lu_buf *buf, const char *name)
1441 struct lod_thread_info *info = lod_env_info(env);
1442 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1447 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1448 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1449 struct lmv_mds_md_v1 *lmv1;
1452 if (rc > (typeof(rc))sizeof(*lmv1))
1455 if (rc < (typeof(rc))sizeof(*lmv1))
1456 RETURN(rc = rc > 0 ? -EINVAL : rc);
1458 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1459 CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1461 info->lti_buf.lb_buf = info->lti_key;
1462 info->lti_buf.lb_len = sizeof(*lmv1);
1463 rc = dt_xattr_get(env, dt_object_child(dt),
1464 &info->lti_buf, name);
1465 if (unlikely(rc != sizeof(*lmv1)))
1466 RETURN(rc = rc > 0 ? -EINVAL : rc);
1468 lmv1 = info->lti_buf.lb_buf;
1469 /* The on-disk LMV EA only contains header, but the
1470 * returned LMV EA size should contain the space for
1471 * the FIDs of all shards of the striped directory. */
1472 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1473 rc = lmv_mds_md_size(
1474 le32_to_cpu(lmv1->lmv_stripe_count),
1477 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1481 RETURN(rc = rc1 != 0 ? rc1 : rc);
1484 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1488 * XXX: Only used by lfsck
1490 * lod returns default striping on the real root of the device
1491 * this is like the root stores default striping for the whole
1492 * filesystem. historically we've been using a different approach
1493 * and store it in the config.
1495 dt_root_get(env, dev->lod_child, &info->lti_fid);
1496 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1498 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1499 struct lov_user_md *lum = buf->lb_buf;
1500 struct lov_desc *desc = &dev->lod_desc;
1502 if (buf->lb_buf == NULL) {
1504 } else if (buf->lb_len >= sizeof(*lum)) {
1505 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1506 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1507 lmm_oi_set_id(&lum->lmm_oi, 0);
1508 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1509 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1510 lum->lmm_stripe_size = cpu_to_le32(
1511 desc->ld_default_stripe_size);
1512 lum->lmm_stripe_count = cpu_to_le16(
1513 desc->ld_default_stripe_count);
1514 lum->lmm_stripe_offset = cpu_to_le16(
1515 desc->ld_default_stripe_offset);
1528 * Checks that the magic of the stripe is sane.
1530 * \param[in] lod lod device
1531 * \param[in] lum a buffer storing LMV EA to verify
1533 * \retval 0 if the EA is sane
1534 * \retval negative otherwise
1536 static int lod_verify_md_striping(struct lod_device *lod,
1537 const struct lmv_user_md_v1 *lum)
1539 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1540 CERROR("%s: invalid lmv_user_md: magic = %x, "
1541 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1542 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1543 (int)le32_to_cpu(lum->lum_stripe_offset),
1544 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1552 * Initialize LMV EA for a slave.
1554 * Initialize slave's LMV EA from the master's LMV EA.
1556 * \param[in] master_lmv a buffer containing master's EA
1557 * \param[out] slave_lmv a buffer where slave's EA will be stored
1560 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1561 const struct lmv_mds_md_v1 *master_lmv)
1563 *slave_lmv = *master_lmv;
1564 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1570 * Generate LMV EA from the object passed as \a dt. The object must have
1571 * the stripes created and initialized.
1573 * \param[in] env execution environment
1574 * \param[in] dt object
1575 * \param[out] lmv_buf buffer storing generated LMV EA
1577 * \retval 0 on success
1578 * \retval negative if failed
1580 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1581 struct lu_buf *lmv_buf)
1583 struct lod_thread_info *info = lod_env_info(env);
1584 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1585 struct lod_object *lo = lod_dt_obj(dt);
1586 struct lmv_mds_md_v1 *lmm1;
1588 int type = LU_SEQ_RANGE_ANY;
1593 LASSERT(lo->ldo_dir_striped != 0);
1594 LASSERT(lo->ldo_dir_stripe_count > 0);
1595 stripe_count = lo->ldo_dir_stripe_count;
1596 /* Only store the LMV EA heahder on the disk. */
1597 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1598 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1602 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1605 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1606 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1607 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1608 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1609 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1614 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1615 lmv_buf->lb_buf = info->lti_ea_store;
1616 lmv_buf->lb_len = sizeof(*lmm1);
1622 * Create in-core represenation for a striped directory.
1624 * Parse the buffer containing LMV EA and instantiate LU objects
1625 * representing the stripe objects. The pointers to the objects are
1626 * stored in ldo_stripe field of \a lo. This function is used when
1627 * we need to access an already created object (i.e. load from a disk).
1629 * \param[in] env execution environment
1630 * \param[in] lo lod object
1631 * \param[in] buf buffer containing LMV EA
1633 * \retval 0 on success
1634 * \retval negative if failed
1636 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1637 const struct lu_buf *buf)
1639 struct lod_thread_info *info = lod_env_info(env);
1640 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1641 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1642 struct dt_object **stripe;
1643 union lmv_mds_md *lmm = buf->lb_buf;
1644 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1645 struct lu_fid *fid = &info->lti_fid;
1650 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1653 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1654 lo->ldo_dir_slave_stripe = 1;
1658 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1661 if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1664 LASSERT(lo->ldo_stripe == NULL);
1665 OBD_ALLOC(stripe, sizeof(stripe[0]) *
1666 (le32_to_cpu(lmv1->lmv_stripe_count)));
1670 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1671 struct dt_device *tgt_dt;
1672 struct dt_object *dto;
1673 int type = LU_SEQ_RANGE_ANY;
1676 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1677 if (!fid_is_sane(fid))
1678 GOTO(out, rc = -ESTALE);
1680 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1684 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1685 tgt_dt = lod->lod_child;
1687 struct lod_tgt_desc *tgt;
1689 tgt = LTD_TGT(ltd, idx);
1691 GOTO(out, rc = -ESTALE);
1692 tgt_dt = tgt->ltd_tgt;
1695 dto = dt_locate_at(env, tgt_dt, fid,
1696 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1699 GOTO(out, rc = PTR_ERR(dto));
1704 lo->ldo_stripe = stripe;
1705 lo->ldo_dir_stripe_count = le32_to_cpu(lmv1->lmv_stripe_count);
1706 lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1708 lod_object_free_striping(env, lo);
1714 * Declare create a striped directory.
1716 * Declare creating a striped directory with a given stripe pattern on the
1717 * specified MDTs. A striped directory is represented as a regular directory
1718 * - an index listing all the stripes. The stripes point back to the master
1719 * object with ".." and LinkEA. The master object gets LMV EA which
1720 * identifies it as a striped directory. The function allocates FIDs
1723 * \param[in] env execution environment
1724 * \param[in] dt object
1725 * \param[in] attr attributes to initialize the objects with
1726 * \param[in] dof type of objects to be created
1727 * \param[in] th transaction handle
1729 * \retval 0 on success
1730 * \retval negative if failed
1732 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1733 struct dt_object *dt,
1734 struct lu_attr *attr,
1735 struct dt_object_format *dof,
1738 struct lod_thread_info *info = lod_env_info(env);
1739 struct lu_buf lmv_buf;
1740 struct lu_buf slave_lmv_buf;
1741 struct lmv_mds_md_v1 *lmm;
1742 struct lmv_mds_md_v1 *slave_lmm = NULL;
1743 struct dt_insert_rec *rec = &info->lti_dt_rec;
1744 struct lod_object *lo = lod_dt_obj(dt);
1749 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1752 lmm = lmv_buf.lb_buf;
1754 OBD_ALLOC_PTR(slave_lmm);
1755 if (slave_lmm == NULL)
1756 GOTO(out, rc = -ENOMEM);
1758 lod_prep_slave_lmv_md(slave_lmm, lmm);
1759 slave_lmv_buf.lb_buf = slave_lmm;
1760 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1762 if (!dt_try_as_dir(env, dt_object_child(dt)))
1763 GOTO(out, rc = -EINVAL);
1765 rec->rec_type = S_IFDIR;
1766 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1767 struct dt_object *dto = lo->ldo_stripe[i];
1768 char *stripe_name = info->lti_key;
1769 struct lu_name *sname;
1770 struct linkea_data ldata = { NULL };
1771 struct lu_buf linkea_buf;
1773 rc = lod_sub_declare_create(env, dto, attr, NULL, dof, th);
1777 if (!dt_try_as_dir(env, dto))
1778 GOTO(out, rc = -EINVAL);
1780 rc = lod_sub_declare_ref_add(env, dto, th);
1784 rec->rec_fid = lu_object_fid(&dto->do_lu);
1785 rc = lod_sub_declare_insert(env, dto,
1786 (const struct dt_rec *)rec,
1787 (const struct dt_key *)dot, th);
1791 /* master stripe FID will be put to .. */
1792 rec->rec_fid = lu_object_fid(&dt->do_lu);
1793 rc = lod_sub_declare_insert(env, dto,
1794 (const struct dt_rec *)rec,
1795 (const struct dt_key *)dotdot, th);
1799 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1800 cfs_fail_val != i) {
1801 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1803 slave_lmm->lmv_master_mdt_index =
1806 slave_lmm->lmv_master_mdt_index =
1808 rc = lod_sub_declare_xattr_set(env, dto, &slave_lmv_buf,
1809 XATTR_NAME_LMV, 0, th);
1814 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1816 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1817 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1819 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1820 PFID(lu_object_fid(&dto->do_lu)), i);
1822 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1823 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1824 sname, lu_object_fid(&dt->do_lu));
1828 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1829 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1830 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
1831 XATTR_NAME_LINK, 0, th);
1835 rec->rec_fid = lu_object_fid(&dto->do_lu);
1836 rc = lod_sub_declare_insert(env, dt_object_child(dt),
1837 (const struct dt_rec *)rec,
1838 (const struct dt_key *)stripe_name,
1843 rc = lod_sub_declare_ref_add(env, dt_object_child(dt), th);
1848 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt),
1849 &lmv_buf, XATTR_NAME_LMV, 0, th);
1853 if (slave_lmm != NULL)
1854 OBD_FREE_PTR(slave_lmm);
1859 static int lod_prep_md_striped_create(const struct lu_env *env,
1860 struct dt_object *dt,
1861 struct lu_attr *attr,
1862 const struct lmv_user_md_v1 *lum,
1863 struct dt_object_format *dof,
1866 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1867 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1868 struct lod_object *lo = lod_dt_obj(dt);
1869 struct dt_object **stripe;
1876 bool is_specific = false;
1879 /* The lum has been verifed in lod_verify_md_striping */
1880 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC ||
1881 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC);
1882 LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1884 stripe_count = le32_to_cpu(lum->lum_stripe_count);
1886 OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1887 if (idx_array == NULL)
1890 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1892 GOTO(out_free, rc = -ENOMEM);
1894 /* Start index must be the master MDT */
1895 master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1896 idx_array[0] = master_index;
1897 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) {
1899 for (i = 1; i < stripe_count; i++)
1900 idx_array[i] = le32_to_cpu(lum->lum_objects[i].lum_mds);
1903 for (i = 0; i < stripe_count; i++) {
1904 struct lod_tgt_desc *tgt = NULL;
1905 struct dt_object *dto;
1906 struct lu_fid fid = { 0 };
1908 struct lu_object_conf conf = { 0 };
1909 struct dt_device *tgt_dt = NULL;
1911 /* Try to find next avaible target */
1913 for (j = 0; j < lod->lod_remote_mdt_count;
1914 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1915 bool already_allocated = false;
1918 CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
1919 idx, lod->lod_remote_mdt_count + 1, i);
1921 if (likely(!is_specific &&
1922 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
1923 /* check whether the idx already exists
1924 * in current allocated array */
1925 for (k = 0; k < i; k++) {
1926 if (idx_array[k] == idx) {
1927 already_allocated = true;
1932 if (already_allocated)
1936 /* Sigh, this index is not in the bitmap, let's check
1937 * next available target */
1938 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) &&
1939 idx != master_index)
1942 if (idx == master_index) {
1943 /* Allocate the FID locally */
1944 rc = obd_fid_alloc(env, lod->lod_child_exp,
1948 tgt_dt = lod->lod_child;
1952 /* check the status of the OSP */
1953 tgt = LTD_TGT(ltd, idx);
1957 tgt_dt = tgt->ltd_tgt;
1958 rc = dt_statfs(env, tgt_dt, NULL);
1960 /* this OSP doesn't feel well */
1965 rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1974 /* Can not allocate more stripes */
1975 if (j == lod->lod_remote_mdt_count) {
1976 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1977 lod2obd(lod)->obd_name, stripe_count, i);
1981 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
1982 idx, i, PFID(&fid));
1984 /* Set the start index for next stripe allocation */
1985 if (!is_specific && i < stripe_count - 1)
1986 idx_array[i + 1] = (idx + 1) %
1987 (lod->lod_remote_mdt_count + 1);
1988 /* tgt_dt and fid must be ready after search avaible OSP
1989 * in the above loop */
1990 LASSERT(tgt_dt != NULL);
1991 LASSERT(fid_is_sane(&fid));
1992 conf.loc_flags = LOC_F_NEW;
1993 dto = dt_locate_at(env, tgt_dt, &fid,
1994 dt->do_lu.lo_dev->ld_site->ls_top_dev,
1997 GOTO(out_put, rc = PTR_ERR(dto));
2001 lo->ldo_dir_stripe_loaded = 1;
2002 lo->ldo_dir_striped = 1;
2003 lo->ldo_stripe = stripe;
2004 lo->ldo_dir_stripe_count = i;
2005 lo->ldo_dir_stripes_allocated = stripe_count;
2007 if (lo->ldo_dir_stripe_count == 0)
2008 GOTO(out_put, rc = -ENOSPC);
2010 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
2016 for (i = 0; i < stripe_count; i++)
2017 if (stripe[i] != NULL)
2018 dt_object_put(env, stripe[i]);
2019 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
2020 lo->ldo_dir_stripe_count = 0;
2021 lo->ldo_dir_stripes_allocated = 0;
2022 lo->ldo_stripe = NULL;
2026 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
2032 * Declare create striped md object.
2034 * The function declares intention to create a striped directory. This is a
2035 * wrapper for lod_prep_md_striped_create(). The only additional functionality
2036 * is to verify pattern \a lum_buf is good. Check that function for the details.
2038 * \param[in] env execution environment
2039 * \param[in] dt object
2040 * \param[in] attr attributes to initialize the objects with
2041 * \param[in] lum_buf a pattern specifying the number of stripes and
2043 * \param[in] dof type of objects to be created
2044 * \param[in] th transaction handle
2046 * \retval 0 on success
2047 * \retval negative if failed
2050 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
2051 struct dt_object *dt,
2052 struct lu_attr *attr,
2053 const struct lu_buf *lum_buf,
2054 struct dt_object_format *dof,
2057 struct lod_object *lo = lod_dt_obj(dt);
2058 struct lmv_user_md_v1 *lum = lum_buf->lb_buf;
2062 LASSERT(lum != NULL);
2064 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
2065 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
2066 (int)le32_to_cpu(lum->lum_stripe_offset));
2068 if (lo->ldo_dir_stripe_count == 0)
2071 /* prepare dir striped objects */
2072 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2074 /* failed to create striping, let's reset
2075 * config so that others don't get confused */
2076 lod_object_free_striping(env, lo);
2084 * Implementation of dt_object_operations::do_declare_xattr_set.
2086 * Used with regular (non-striped) objects. Basically it
2087 * initializes the striping information and applies the
2088 * change to all the stripes.
2090 * \see dt_object_operations::do_declare_xattr_set() in the API description
2093 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2094 struct dt_object *dt,
2095 const struct lu_buf *buf,
2096 const char *name, int fl,
2099 struct dt_object *next = dt_object_child(dt);
2100 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2101 struct lod_object *lo = lod_dt_obj(dt);
2106 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2107 struct lmv_user_md_v1 *lum;
2109 LASSERT(buf != NULL && buf->lb_buf != NULL);
2111 rc = lod_verify_md_striping(d, lum);
2114 } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2115 rc = lod_verify_striping(d, lo, buf, false);
2120 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2124 /* Note: Do not set LinkEA on sub-stripes, otherwise
2125 * it will confuse the fid2path process(see mdt_path_current()).
2126 * The linkEA between master and sub-stripes is set in
2127 * lod_xattr_set_lmv(). */
2128 if (strcmp(name, XATTR_NAME_LINK) == 0)
2131 /* set xattr to each stripes, if needed */
2132 rc = lod_load_striping(env, lo);
2136 if (lo->ldo_dir_stripe_count == 0)
2139 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2140 LASSERT(lo->ldo_stripe[i]);
2142 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2152 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2153 struct lod_object *lo,
2154 struct dt_object *dt, struct thandle *th,
2155 int comp_idx, int stripe_idx,
2156 struct lod_obj_stripe_cb_data *data)
2158 struct lod_thread_info *info = lod_env_info(env);
2159 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
2160 struct filter_fid *ff = &info->lti_ff;
2161 struct lu_buf *buf = &info->lti_buf;
2165 buf->lb_len = sizeof(*ff);
2166 rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2173 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
2174 if (lu_fid_eq(lu_object_fid(&lo->ldo_obj.do_lu), &ff->ff_parent) &&
2175 ff->ff_layout.ol_comp_id == comp->llc_id)
2178 /* rewrite filter_fid */
2179 memset(ff, 0, sizeof(*ff));
2180 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2181 ff->ff_parent.f_ver = stripe_idx;
2182 ff->ff_layout.ol_stripe_size = comp->llc_stripe_size;
2183 ff->ff_layout.ol_stripe_count = comp->llc_stripe_count;
2184 ff->ff_layout.ol_comp_id = comp->llc_id;
2185 ff->ff_layout.ol_comp_start = comp->llc_extent.e_start;
2186 ff->ff_layout.ol_comp_end = comp->llc_extent.e_end;
2187 filter_fid_cpu_to_le(ff, ff, sizeof(*ff));
2189 if (data->locd_declare)
2190 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2191 LU_XATTR_REPLACE, th);
2193 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2194 LU_XATTR_REPLACE, th);
2200 * Reset parent FID on OST object
2202 * Replace parent FID with @dt object FID, which is only called during migration
2203 * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2204 * the FID is changed.
2206 * \param[in] env execution environment
2207 * \param[in] dt dt_object whose stripes's parent FID will be reset
2208 * \parem[in] th thandle
2209 * \param[in] declare if it is declare
2211 * \retval 0 if reset succeeds
2212 * \retval negative errno if reset fails
2214 static int lod_replace_parent_fid(const struct lu_env *env,
2215 struct dt_object *dt,
2216 struct thandle *th, bool declare)
2218 struct lod_object *lo = lod_dt_obj(dt);
2219 struct lod_thread_info *info = lod_env_info(env);
2220 struct lu_buf *buf = &info->lti_buf;
2221 struct filter_fid *ff;
2222 struct lod_obj_stripe_cb_data data = { { 0 } };
2226 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2228 /* set xattr to each stripes, if needed */
2229 rc = lod_load_striping(env, lo);
2233 if (!lod_obj_is_striped(dt))
2236 if (info->lti_ea_store_size < sizeof(*ff)) {
2237 rc = lod_ea_store_resize(info, sizeof(*ff));
2242 buf->lb_buf = info->lti_ea_store;
2243 buf->lb_len = info->lti_ea_store_size;
2245 data.locd_declare = declare;
2246 data.locd_stripe_cb = lod_obj_stripe_replace_parent_fid_cb;
2247 rc = lod_obj_for_each_stripe(env, lo, th, &data);
2252 inline __u16 lod_comp_entry_stripe_count(struct lod_object *lo,
2253 struct lod_layout_component *entry,
2256 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2260 else if (lod_comp_inited(entry))
2261 return entry->llc_stripe_count;
2262 else if ((__u16)-1 == entry->llc_stripe_count)
2263 return lod->lod_desc.ld_tgt_count;
2265 return lod_get_stripe_count(lod, lo, entry->llc_stripe_count);
2268 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2270 int magic, size = 0, i;
2271 struct lod_layout_component *comp_entries;
2276 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2277 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2279 lo->ldo_def_striping->lds_def_striping_is_composite;
2281 comp_cnt = lo->ldo_comp_cnt;
2282 comp_entries = lo->ldo_comp_entries;
2283 is_composite = lo->ldo_is_composite;
2287 LASSERT(comp_cnt != 0 && comp_entries != NULL);
2289 size = sizeof(struct lov_comp_md_v1) +
2290 sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2291 LASSERT(size % sizeof(__u64) == 0);
2294 for (i = 0; i < comp_cnt; i++) {
2297 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2298 stripe_count = lod_comp_entry_stripe_count(lo, &comp_entries[i],
2300 if (!is_dir && is_composite)
2301 lod_comp_shrink_stripe_count(&comp_entries[i],
2304 size += lov_user_md_size(stripe_count, magic);
2305 LASSERT(size % sizeof(__u64) == 0);
2311 * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2312 * the xattr value is binary lov_comp_md_v1 which contains component(s)
2315 * \param[in] env execution environment
2316 * \param[in] dt dt_object to add components on
2317 * \param[in] buf buffer contains components to be added
2318 * \parem[in] th thandle
2320 * \retval 0 on success
2321 * \retval negative errno on failure
2323 static int lod_declare_layout_add(const struct lu_env *env,
2324 struct dt_object *dt,
2325 const struct lu_buf *buf,
2328 struct lod_thread_info *info = lod_env_info(env);
2329 struct lod_layout_component *comp_array, *lod_comp, *old_array;
2330 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2331 struct dt_object *next = dt_object_child(dt);
2332 struct lov_desc *desc = &d->lod_desc;
2333 struct lod_object *lo = lod_dt_obj(dt);
2334 struct lov_user_md_v3 *v3;
2335 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2337 int i, rc, array_cnt, old_array_cnt;
2340 LASSERT(lo->ldo_is_composite);
2342 if (lo->ldo_flr_state != LCM_FL_NOT_FLR)
2345 rc = lod_verify_striping(d, lo, buf, false);
2349 magic = comp_v1->lcm_magic;
2350 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2351 lustre_swab_lov_comp_md_v1(comp_v1);
2352 magic = comp_v1->lcm_magic;
2355 if (magic != LOV_USER_MAGIC_COMP_V1)
2358 array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2359 OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2360 if (comp_array == NULL)
2363 memcpy(comp_array, lo->ldo_comp_entries,
2364 sizeof(*comp_array) * lo->ldo_comp_cnt);
2366 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2367 struct lov_user_md_v1 *v1;
2368 struct lu_extent *ext;
2370 v1 = (struct lov_user_md *)((char *)comp_v1 +
2371 comp_v1->lcm_entries[i].lcme_offset);
2372 ext = &comp_v1->lcm_entries[i].lcme_extent;
2374 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2375 lod_comp->llc_extent.e_start = ext->e_start;
2376 lod_comp->llc_extent.e_end = ext->e_end;
2377 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2378 lod_comp->llc_flags = comp_v1->lcm_entries[i].lcme_flags;
2380 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2381 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2382 lod_adjust_stripe_info(lod_comp, desc);
2384 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2385 v3 = (struct lov_user_md_v3 *) v1;
2386 if (v3->lmm_pool_name[0] != '\0') {
2387 rc = lod_set_pool(&lod_comp->llc_pool,
2395 old_array = lo->ldo_comp_entries;
2396 old_array_cnt = lo->ldo_comp_cnt;
2398 lo->ldo_comp_entries = comp_array;
2399 lo->ldo_comp_cnt = array_cnt;
2401 /* No need to increase layout generation here, it will be increased
2402 * later when generating component ID for the new components */
2404 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2405 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2406 XATTR_NAME_LOV, 0, th);
2408 lo->ldo_comp_entries = old_array;
2409 lo->ldo_comp_cnt = old_array_cnt;
2413 OBD_FREE(old_array, sizeof(*lod_comp) * old_array_cnt);
2415 LASSERT(lo->ldo_mirror_count == 1);
2416 lo->ldo_mirrors[0].lme_end = array_cnt - 1;
2421 for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2422 lod_comp = &comp_array[i];
2423 if (lod_comp->llc_pool != NULL) {
2424 OBD_FREE(lod_comp->llc_pool,
2425 strlen(lod_comp->llc_pool) + 1);
2426 lod_comp->llc_pool = NULL;
2429 OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2434 * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2435 * the '$field' can only be 'flags' now. The xattr value is binary
2436 * lov_comp_md_v1 which contains the component ID(s) and the value of
2437 * the field to be modified.
2439 * \param[in] env execution environment
2440 * \param[in] dt dt_object to be modified
2441 * \param[in] op operation string, like "set.flags"
2442 * \param[in] buf buffer contains components to be set
2443 * \parem[in] th thandle
2445 * \retval 0 on success
2446 * \retval negative errno on failure
2448 static int lod_declare_layout_set(const struct lu_env *env,
2449 struct dt_object *dt,
2450 char *op, const struct lu_buf *buf,
2453 struct lod_layout_component *lod_comp;
2454 struct lod_thread_info *info = lod_env_info(env);
2455 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2456 struct lod_object *lo = lod_dt_obj(dt);
2457 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2460 bool changed = false;
2463 if (strcmp(op, "set.flags") != 0) {
2464 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2465 lod2obd(d)->obd_name, op);
2469 magic = comp_v1->lcm_magic;
2470 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2471 lustre_swab_lov_comp_md_v1(comp_v1);
2472 magic = comp_v1->lcm_magic;
2475 if (magic != LOV_USER_MAGIC_COMP_V1)
2478 if (comp_v1->lcm_entry_count == 0) {
2479 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2480 lod2obd(d)->obd_name);
2484 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2485 __u32 id = comp_v1->lcm_entries[i].lcme_id;
2486 __u32 flags = comp_v1->lcm_entries[i].lcme_flags;
2488 if (flags & LCME_FL_INIT) {
2490 lod_object_free_striping(env, lo);
2494 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2495 lod_comp = &lo->ldo_comp_entries[j];
2496 if (id != lod_comp->llc_id)
2499 if (flags & LCME_FL_NEG) {
2500 flags &= ~LCME_FL_NEG;
2501 lod_comp->llc_flags &= ~flags;
2503 lod_comp->llc_flags |= flags;
2510 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2511 lod2obd(d)->obd_name);
2515 lod_obj_inc_layout_gen(lo);
2517 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2518 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), &info->lti_buf,
2519 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
2524 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2525 * and the xattr value is a unique component ID or a special lcme_id.
2527 * \param[in] env execution environment
2528 * \param[in] dt dt_object to be operated on
2529 * \param[in] buf buffer contains component ID or lcme_id
2530 * \parem[in] th thandle
2532 * \retval 0 on success
2533 * \retval negative errno on failure
2535 static int lod_declare_layout_del(const struct lu_env *env,
2536 struct dt_object *dt,
2537 const struct lu_buf *buf,
2540 struct lod_thread_info *info = lod_env_info(env);
2541 struct dt_object *next = dt_object_child(dt);
2542 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2543 struct lod_object *lo = lod_dt_obj(dt);
2544 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2545 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2546 __u32 magic, id, flags, neg_flags = 0;
2550 LASSERT(lo->ldo_is_composite);
2552 if (lo->ldo_flr_state != LCM_FL_NOT_FLR)
2555 magic = comp_v1->lcm_magic;
2556 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2557 lustre_swab_lov_comp_md_v1(comp_v1);
2558 magic = comp_v1->lcm_magic;
2561 if (magic != LOV_USER_MAGIC_COMP_V1)
2564 id = comp_v1->lcm_entries[0].lcme_id;
2565 flags = comp_v1->lcm_entries[0].lcme_flags;
2567 if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
2568 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
2569 lod2obd(d)->obd_name, id, flags);
2573 if (id != LCME_ID_INVAL && flags != 0) {
2574 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
2575 lod2obd(d)->obd_name);
2579 if (id == LCME_ID_INVAL && !flags) {
2580 CDEBUG(D_LAYOUT, "%s: no id or flags specified.\n",
2581 lod2obd(d)->obd_name);
2585 if (flags & LCME_FL_NEG) {
2586 neg_flags = flags & ~LCME_FL_NEG;
2590 left = lo->ldo_comp_cnt;
2594 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
2595 struct lod_layout_component *lod_comp;
2597 lod_comp = &lo->ldo_comp_entries[i];
2599 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
2601 else if (flags && !(flags & lod_comp->llc_flags))
2603 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
2606 if (left != (i + 1)) {
2607 CDEBUG(D_LAYOUT, "%s: this deletion will create "
2608 "a hole.\n", lod2obd(d)->obd_name);
2613 /* Mark the component as deleted */
2614 lod_comp->llc_id = LCME_ID_INVAL;
2616 /* Not instantiated component */
2617 if (lod_comp->llc_stripe == NULL)
2620 LASSERT(lod_comp->llc_stripe_count > 0);
2621 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
2622 struct dt_object *obj = lod_comp->llc_stripe[j];
2626 rc = lod_sub_declare_destroy(env, obj, th);
2632 LASSERTF(left >= 0, "left = %d\n", left);
2633 if (left == lo->ldo_comp_cnt) {
2634 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
2635 lod2obd(d)->obd_name, id);
2639 memset(attr, 0, sizeof(*attr));
2640 attr->la_valid = LA_SIZE;
2641 rc = lod_sub_declare_attr_set(env, next, attr, th);
2646 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2647 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2648 XATTR_NAME_LOV, 0, th);
2650 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
2657 * Declare layout add/set/del operations issued by special xattr names:
2659 * XATTR_LUSTRE_LOV.add add component(s) to existing file
2660 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
2661 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
2663 * \param[in] env execution environment
2664 * \param[in] dt object
2665 * \param[in] name name of xattr
2666 * \param[in] buf lu_buf contains xattr value
2667 * \param[in] th transaction handle
2669 * \retval 0 on success
2670 * \retval negative if failed
2672 static int lod_declare_modify_layout(const struct lu_env *env,
2673 struct dt_object *dt,
2675 const struct lu_buf *buf,
2678 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2679 struct lod_object *lo = lod_dt_obj(dt);
2680 struct dt_object *next = dt_object_child(&lo->ldo_obj);
2682 int rc, len = strlen(XATTR_LUSTRE_LOV);
2685 LASSERT(dt_object_exists(dt));
2687 if (strlen(name) <= len || name[len] != '.') {
2688 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
2689 lod2obd(d)->obd_name, name);
2694 dt_write_lock(env, next, 0);
2695 rc = lod_load_striping_locked(env, lo);
2699 /* the layout to be modified must be a composite layout */
2700 if (!lo->ldo_is_composite) {
2701 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
2702 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
2703 GOTO(unlock, rc = -EINVAL);
2706 op = (char *)name + len;
2707 if (strcmp(op, "add") == 0) {
2708 rc = lod_declare_layout_add(env, dt, buf, th);
2709 } else if (strcmp(op, "del") == 0) {
2710 rc = lod_declare_layout_del(env, dt, buf, th);
2711 } else if (strncmp(op, "set", strlen("set")) == 0) {
2712 rc = lod_declare_layout_set(env, dt, op, buf, th);
2714 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
2715 lod2obd(d)->obd_name, name);
2716 GOTO(unlock, rc = -ENOTSUPP);
2720 lod_object_free_striping(env, lo);
2721 dt_write_unlock(env, next);
2727 * Convert a plain file lov_mds_md to a composite layout.
2729 * \param[in,out] info the thread info::lti_ea_store buffer contains little
2730 * endian plain file layout
2732 * \retval 0 on success, <0 on failure
2734 static int lod_layout_convert(struct lod_thread_info *info)
2736 struct lov_mds_md *lmm = info->lti_ea_store;
2737 struct lov_mds_md *lmm_save;
2738 struct lov_comp_md_v1 *lcm;
2739 struct lov_comp_md_entry_v1 *lcme;
2745 /* realloc buffer to a composite layout which contains one component */
2746 blob_size = lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
2747 le32_to_cpu(lmm->lmm_magic));
2748 size = sizeof(*lcm) + sizeof(*lcme) + blob_size;
2750 OBD_ALLOC_LARGE(lmm_save, blob_size);
2752 GOTO(out, rc = -ENOMEM);
2754 memcpy(lmm_save, lmm, blob_size);
2756 if (info->lti_ea_store_size < size) {
2757 rc = lod_ea_store_resize(info, size);
2762 lcm = info->lti_ea_store;
2763 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
2764 lcm->lcm_size = cpu_to_le32(size);
2765 lcm->lcm_layout_gen = cpu_to_le32(le16_to_cpu(
2766 lmm_save->lmm_layout_gen));
2767 lcm->lcm_flags = cpu_to_le16(LCM_FL_NOT_FLR);
2768 lcm->lcm_entry_count = cpu_to_le16(1);
2769 lcm->lcm_mirror_count = 0;
2771 lcme = &lcm->lcm_entries[0];
2772 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
2773 lcme->lcme_extent.e_start = 0;
2774 lcme->lcme_extent.e_end = cpu_to_le64(OBD_OBJECT_EOF);
2775 lcme->lcme_offset = cpu_to_le32(sizeof(*lcm) + sizeof(*lcme));
2776 lcme->lcme_size = cpu_to_le32(blob_size);
2778 memcpy((char *)lcm + lcme->lcme_offset, (char *)lmm_save, blob_size);
2783 OBD_FREE_LARGE(lmm_save, blob_size);
2788 * Merge layouts to form a mirrored file.
2790 static int lod_declare_layout_merge(const struct lu_env *env,
2791 struct dt_object *dt, const struct lu_buf *mbuf,
2794 struct lod_thread_info *info = lod_env_info(env);
2795 struct lu_buf *buf = &info->lti_buf;
2796 struct lod_object *lo = lod_dt_obj(dt);
2797 struct lov_comp_md_v1 *lcm;
2798 struct lov_comp_md_v1 *cur_lcm;
2799 struct lov_comp_md_v1 *merge_lcm;
2800 struct lov_comp_md_entry_v1 *lcme;
2803 __u16 cur_entry_count;
2804 __u16 merge_entry_count;
2806 __u16 mirror_id = 0;
2811 merge_lcm = mbuf->lb_buf;
2812 if (mbuf->lb_len < sizeof(*merge_lcm))
2815 /* must be an existing layout from disk */
2816 if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
2819 merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
2821 /* do not allow to merge two mirrored files */
2822 if (le16_to_cpu(merge_lcm->lcm_mirror_count))
2825 /* verify the target buffer */
2826 rc = lod_get_lov_ea(env, lo);
2828 RETURN(rc ? : -ENODATA);
2830 cur_lcm = info->lti_ea_store;
2831 switch (le32_to_cpu(cur_lcm->lcm_magic)) {
2834 rc = lod_layout_convert(info);
2836 case LOV_MAGIC_COMP_V1:
2845 /* info->lti_ea_store could be reallocated in lod_layout_convert() */
2846 cur_lcm = info->lti_ea_store;
2847 cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
2849 /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
2850 mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
2851 if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
2854 /* size of new layout */
2855 size = le32_to_cpu(cur_lcm->lcm_size) +
2856 le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
2858 memset(buf, 0, sizeof(*buf));
2859 lu_buf_alloc(buf, size);
2860 if (buf->lb_buf == NULL)
2864 memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
2866 offset = sizeof(*lcm) +
2867 sizeof(*lcme) * (cur_entry_count + merge_entry_count);
2868 for (i = 0; i < cur_entry_count; i++) {
2869 struct lov_comp_md_entry_v1 *cur_lcme;
2871 lcme = &lcm->lcm_entries[i];
2872 cur_lcme = &cur_lcm->lcm_entries[i];
2874 lcme->lcme_offset = cpu_to_le32(offset);
2875 memcpy((char *)lcm + offset,
2876 (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
2877 le32_to_cpu(lcme->lcme_size));
2879 offset += le32_to_cpu(lcme->lcme_size);
2881 if (mirror_count == 1) {
2882 /* new mirrored file, create new mirror ID */
2883 id = pflr_id(1, i + 1);
2884 lcme->lcme_id = cpu_to_le32(id);
2887 id = MAX(le32_to_cpu(lcme->lcme_id), id);
2890 mirror_id = mirror_id_of(id) + 1;
2891 for (i = 0; i < merge_entry_count; i++) {
2892 struct lov_comp_md_entry_v1 *merge_lcme;
2894 merge_lcme = &merge_lcm->lcm_entries[i];
2895 lcme = &lcm->lcm_entries[cur_entry_count + i];
2897 *lcme = *merge_lcme;
2898 lcme->lcme_offset = cpu_to_le32(offset);
2900 id = pflr_id(mirror_id, i + 1);
2901 lcme->lcme_id = cpu_to_le32(id);
2903 memcpy((char *)lcm + offset,
2904 (char *)merge_lcm + le32_to_cpu(merge_lcme->lcme_offset),
2905 le32_to_cpu(lcme->lcme_size));
2907 offset += le32_to_cpu(lcme->lcme_size);
2910 /* fixup layout information */
2911 lod_obj_inc_layout_gen(lo);
2912 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
2913 lcm->lcm_size = cpu_to_le32(size);
2914 lcm->lcm_entry_count = cpu_to_le16(cur_entry_count + merge_entry_count);
2915 lcm->lcm_mirror_count = cpu_to_le16(mirror_count);
2916 if ((le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK) == LCM_FL_NOT_FLR)
2917 lcm->lcm_flags = cpu_to_le32(LCM_FL_RDONLY);
2919 LASSERT(dt_write_locked(env, dt_object_child(dt)));
2920 lod_object_free_striping(env, lo);
2921 rc = lod_parse_striping(env, lo, buf);
2925 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), buf,
2926 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
2934 * Implementation of dt_object_operations::do_declare_xattr_set.
2936 * \see dt_object_operations::do_declare_xattr_set() in the API description
2939 * the extension to the API:
2940 * - declaring LOVEA requests striping creation
2941 * - LU_XATTR_REPLACE means layout swap
2943 static int lod_declare_xattr_set(const struct lu_env *env,
2944 struct dt_object *dt,
2945 const struct lu_buf *buf,
2946 const char *name, int fl,
2949 struct dt_object *next = dt_object_child(dt);
2950 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2955 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2956 if ((S_ISREG(mode) || mode == 0) &&
2957 !(fl & (LU_XATTR_REPLACE | LU_XATTR_MERGE)) &&
2958 (strcmp(name, XATTR_NAME_LOV) == 0 ||
2959 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
2961 * this is a request to create object's striping.
2963 * allow to declare predefined striping on a new (!mode) object
2964 * which is supposed to be replay of regular file creation
2965 * (when LOV setting is declared)
2967 * LU_XATTR_REPLACE is set to indicate a layout swap
2969 if (dt_object_exists(dt)) {
2970 rc = dt_attr_get(env, next, attr);
2974 memset(attr, 0, sizeof(*attr));
2975 attr->la_valid = LA_TYPE | LA_MODE;
2976 attr->la_mode = S_IFREG;
2978 rc = lod_declare_striped_create(env, dt, attr, buf, th);
2979 } else if (fl & LU_XATTR_MERGE) {
2980 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
2981 strcmp(name, XATTR_LUSTRE_LOV) == 0);
2982 rc = lod_declare_layout_merge(env, dt, buf, th);
2983 } else if (S_ISREG(mode) &&
2984 strlen(name) > strlen(XATTR_LUSTRE_LOV) + 1 &&
2985 strncmp(name, XATTR_LUSTRE_LOV,
2986 strlen(XATTR_LUSTRE_LOV)) == 0) {
2988 * this is a request to modify object's striping.
2989 * add/set/del component(s).
2991 if (!dt_object_exists(dt))
2994 rc = lod_declare_modify_layout(env, dt, name, buf, th);
2995 } else if (S_ISDIR(mode)) {
2996 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2997 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
2998 rc = lod_replace_parent_fid(env, dt, th, true);
3000 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
3007 * Apply xattr changes to the object.
3009 * Applies xattr changes to the object and the stripes if the latter exist.
3011 * \param[in] env execution environment
3012 * \param[in] dt object
3013 * \param[in] buf buffer pointing to the new value of xattr
3014 * \param[in] name name of xattr
3015 * \param[in] fl flags
3016 * \param[in] th transaction handle
3018 * \retval 0 on success
3019 * \retval negative if failed
3021 static int lod_xattr_set_internal(const struct lu_env *env,
3022 struct dt_object *dt,
3023 const struct lu_buf *buf,
3024 const char *name, int fl,
3027 struct dt_object *next = dt_object_child(dt);
3028 struct lod_object *lo = lod_dt_obj(dt);
3033 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3034 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3037 /* Note: Do not set LinkEA on sub-stripes, otherwise
3038 * it will confuse the fid2path process(see mdt_path_current()).
3039 * The linkEA between master and sub-stripes is set in
3040 * lod_xattr_set_lmv(). */
3041 if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
3044 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3045 LASSERT(lo->ldo_stripe[i]);
3047 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
3057 * Delete an extended attribute.
3059 * Deletes specified xattr from the object and the stripes if the latter exist.
3061 * \param[in] env execution environment
3062 * \param[in] dt object
3063 * \param[in] name name of xattr
3064 * \param[in] th transaction handle
3066 * \retval 0 on success
3067 * \retval negative if failed
3069 static int lod_xattr_del_internal(const struct lu_env *env,
3070 struct dt_object *dt,
3071 const char *name, struct thandle *th)
3073 struct dt_object *next = dt_object_child(dt);
3074 struct lod_object *lo = lod_dt_obj(dt);
3079 rc = lod_sub_xattr_del(env, next, name, th);
3080 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3083 if (lo->ldo_dir_stripe_count == 0)
3086 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3087 LASSERT(lo->ldo_stripe[i]);
3089 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3098 * Set default striping on a directory.
3100 * Sets specified striping on a directory object unless it matches the default
3101 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3102 * EA. This striping will be used when regular file is being created in this
3105 * \param[in] env execution environment
3106 * \param[in] dt the striped object
3107 * \param[in] buf buffer with the striping
3108 * \param[in] name name of EA
3109 * \param[in] fl xattr flag (see OSD API description)
3110 * \param[in] th transaction handle
3112 * \retval 0 on success
3113 * \retval negative if failed
3115 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
3116 struct dt_object *dt,
3117 const struct lu_buf *buf,
3118 const char *name, int fl,
3121 struct lov_user_md_v1 *lum;
3122 struct lov_user_md_v3 *v3 = NULL;
3123 const char *pool_name = NULL;
3128 LASSERT(buf != NULL && buf->lb_buf != NULL);
3131 switch (lum->lmm_magic) {
3132 case LOV_USER_MAGIC_V3:
3134 if (v3->lmm_pool_name[0] != '\0')
3135 pool_name = v3->lmm_pool_name;
3137 case LOV_USER_MAGIC_V1:
3138 /* if { size, offset, count } = { 0, -1, 0 } and no pool
3139 * (i.e. all default values specified) then delete default
3140 * striping from dir. */
3142 "set default striping: sz %u # %u offset %d %s %s\n",
3143 (unsigned)lum->lmm_stripe_size,
3144 (unsigned)lum->lmm_stripe_count,
3145 (int)lum->lmm_stripe_offset,
3146 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
3148 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
3149 lum->lmm_stripe_count,
3150 lum->lmm_stripe_offset,
3153 case LOV_USER_MAGIC_COMP_V1:
3157 CERROR("Invalid magic %x\n", lum->lmm_magic);
3162 rc = lod_xattr_del_internal(env, dt, name, th);
3166 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3173 * Set default striping on a directory object.
3175 * Sets specified striping on a directory object unless it matches the default
3176 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3177 * EA. This striping will be used when a new directory is being created in the
3180 * \param[in] env execution environment
3181 * \param[in] dt the striped object
3182 * \param[in] buf buffer with the striping
3183 * \param[in] name name of EA
3184 * \param[in] fl xattr flag (see OSD API description)
3185 * \param[in] th transaction handle
3187 * \retval 0 on success
3188 * \retval negative if failed
3190 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
3191 struct dt_object *dt,
3192 const struct lu_buf *buf,
3193 const char *name, int fl,
3196 struct lmv_user_md_v1 *lum;
3200 LASSERT(buf != NULL && buf->lb_buf != NULL);
3203 CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
3204 le32_to_cpu(lum->lum_stripe_count),
3205 (int)le32_to_cpu(lum->lum_stripe_offset));
3207 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
3208 le32_to_cpu(lum->lum_stripe_offset)) &&
3209 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
3210 rc = lod_xattr_del_internal(env, dt, name, th);
3214 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3223 * Turn directory into a striped directory.
3225 * During replay the client sends the striping created before MDT
3226 * failure, then the layer above LOD sends this defined striping
3227 * using ->do_xattr_set(), so LOD uses this method to replay creation
3228 * of the stripes. Notice the original information for the striping
3229 * (#stripes, FIDs, etc) was transferred in declare path.
3231 * \param[in] env execution environment
3232 * \param[in] dt the striped object
3233 * \param[in] buf not used currently
3234 * \param[in] name not used currently
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_lmv(const struct lu_env *env, struct dt_object *dt,
3242 const struct lu_buf *buf, const char *name,
3243 int fl, struct thandle *th)
3245 struct lod_object *lo = lod_dt_obj(dt);
3246 struct lod_thread_info *info = lod_env_info(env);
3247 struct lu_attr *attr = &info->lti_attr;
3248 struct dt_object_format *dof = &info->lti_format;
3249 struct lu_buf lmv_buf;
3250 struct lu_buf slave_lmv_buf;
3251 struct lmv_mds_md_v1 *lmm;
3252 struct lmv_mds_md_v1 *slave_lmm = NULL;
3253 struct dt_insert_rec *rec = &info->lti_dt_rec;
3258 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3261 /* The stripes are supposed to be allocated in declare phase,
3262 * if there are no stripes being allocated, it will skip */
3263 if (lo->ldo_dir_stripe_count == 0)
3266 rc = dt_attr_get(env, dt_object_child(dt), attr);
3270 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
3271 LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
3272 dof->dof_type = DFT_DIR;
3274 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
3277 lmm = lmv_buf.lb_buf;
3279 OBD_ALLOC_PTR(slave_lmm);
3280 if (slave_lmm == NULL)
3283 lod_prep_slave_lmv_md(slave_lmm, lmm);
3284 slave_lmv_buf.lb_buf = slave_lmm;
3285 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
3287 rec->rec_type = S_IFDIR;
3288 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3289 struct dt_object *dto;
3290 char *stripe_name = info->lti_key;
3291 struct lu_name *sname;
3292 struct linkea_data ldata = { NULL };
3293 struct lu_buf linkea_buf;
3295 dto = lo->ldo_stripe[i];
3297 dt_write_lock(env, dto, MOR_TGT_CHILD);
3298 rc = lod_sub_create(env, dto, attr, NULL, dof, th);
3300 dt_write_unlock(env, dto);
3304 rc = lod_sub_ref_add(env, dto, th);
3305 dt_write_unlock(env, dto);
3309 rec->rec_fid = lu_object_fid(&dto->do_lu);
3310 rc = lod_sub_insert(env, dto, (const struct dt_rec *)rec,
3311 (const struct dt_key *)dot, th, 0);
3315 rec->rec_fid = lu_object_fid(&dt->do_lu);
3316 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
3317 (const struct dt_key *)dotdot, th, 0);
3321 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
3322 cfs_fail_val != i) {
3323 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
3325 slave_lmm->lmv_master_mdt_index =
3328 slave_lmm->lmv_master_mdt_index =
3331 rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
3332 XATTR_NAME_LMV, fl, th);
3337 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
3339 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3340 PFID(lu_object_fid(&dto->do_lu)), i + 1);
3342 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3343 PFID(lu_object_fid(&dto->do_lu)), i);
3345 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
3346 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
3347 sname, lu_object_fid(&dt->do_lu));
3351 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
3352 linkea_buf.lb_len = ldata.ld_leh->leh_len;
3353 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
3354 XATTR_NAME_LINK, 0, th);
3358 rec->rec_fid = lu_object_fid(&dto->do_lu);
3359 rc = lod_sub_insert(env, dt_object_child(dt),
3360 (const struct dt_rec *)rec,
3361 (const struct dt_key *)stripe_name, th, 0);
3365 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
3370 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
3371 rc = lod_sub_xattr_set(env, dt_object_child(dt),
3372 &lmv_buf, XATTR_NAME_LMV, fl, th);
3374 if (slave_lmm != NULL)
3375 OBD_FREE_PTR(slave_lmm);
3381 * Helper function to declare/execute creation of a striped directory
3383 * Called in declare/create object path, prepare striping for a directory
3384 * and prepare defaults data striping for the objects to be created in
3385 * that directory. Notice the function calls "declaration" or "execution"
3386 * methods depending on \a declare param. This is a consequence of the
3387 * current approach while we don't have natural distributed transactions:
3388 * we basically execute non-local updates in the declare phase. So, the
3389 * arguments for the both phases are the same and this is the reason for
3390 * this function to exist.
3392 * \param[in] env execution environment
3393 * \param[in] dt object
3394 * \param[in] attr attributes the stripes will be created with
3395 * \param[in] lmu lmv_user_md if MDT indices are specified
3396 * \param[in] dof format of stripes (see OSD API description)
3397 * \param[in] th transaction handle
3398 * \param[in] declare where to call "declare" or "execute" methods
3400 * \retval 0 on success
3401 * \retval negative if failed
3403 static int lod_dir_striping_create_internal(const struct lu_env *env,
3404 struct dt_object *dt,
3405 struct lu_attr *attr,
3406 const struct lu_buf *lmu,
3407 struct dt_object_format *dof,
3411 struct lod_thread_info *info = lod_env_info(env);
3412 struct lod_object *lo = lod_dt_obj(dt);
3413 const struct lod_default_striping *lds = lo->ldo_def_striping;
3417 LASSERT(ergo(lds != NULL,
3418 lds->lds_def_striping_set ||
3419 lds->lds_dir_def_striping_set));
3421 if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
3422 lo->ldo_dir_stripe_offset)) {
3424 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3425 int stripe_count = lo->ldo_dir_stripe_count;
3427 if (info->lti_ea_store_size < sizeof(*v1)) {
3428 rc = lod_ea_store_resize(info, sizeof(*v1));
3431 v1 = info->lti_ea_store;
3434 memset(v1, 0, sizeof(*v1));
3435 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3436 v1->lum_stripe_count = cpu_to_le32(stripe_count);
3437 v1->lum_stripe_offset =
3438 cpu_to_le32(lo->ldo_dir_stripe_offset);
3440 info->lti_buf.lb_buf = v1;
3441 info->lti_buf.lb_len = sizeof(*v1);
3442 lmu = &info->lti_buf;
3446 rc = lod_declare_xattr_set_lmv(env, dt, attr, lmu, dof,
3449 rc = lod_xattr_set_lmv(env, dt, lmu, XATTR_NAME_LMV, 0,
3455 /* Transfer default LMV striping from the parent */
3456 if (lds != NULL && lds->lds_dir_def_striping_set &&
3457 !LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
3458 lds->lds_dir_def_stripe_offset)) {
3459 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3461 if (info->lti_ea_store_size < sizeof(*v1)) {
3462 rc = lod_ea_store_resize(info, sizeof(*v1));
3465 v1 = info->lti_ea_store;
3468 memset(v1, 0, sizeof(*v1));
3469 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3470 v1->lum_stripe_count =
3471 cpu_to_le32(lds->lds_dir_def_stripe_count);
3472 v1->lum_stripe_offset =
3473 cpu_to_le32(lds->lds_dir_def_stripe_offset);
3475 cpu_to_le32(lds->lds_dir_def_hash_type);
3477 info->lti_buf.lb_buf = v1;
3478 info->lti_buf.lb_len = sizeof(*v1);
3480 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3481 XATTR_NAME_DEFAULT_LMV,
3484 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
3486 XATTR_NAME_DEFAULT_LMV, 0,
3492 /* Transfer default LOV striping from the parent */
3493 if (lds != NULL && lds->lds_def_striping_set &&
3494 lds->lds_def_comp_cnt != 0) {
3495 struct lov_mds_md *lmm;
3496 int lmm_size = lod_comp_md_size(lo, true);
3498 if (info->lti_ea_store_size < lmm_size) {
3499 rc = lod_ea_store_resize(info, lmm_size);
3503 lmm = info->lti_ea_store;
3505 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
3509 info->lti_buf.lb_buf = lmm;
3510 info->lti_buf.lb_len = lmm_size;
3513 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3514 XATTR_NAME_LOV, 0, th);
3516 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
3517 XATTR_NAME_LOV, 0, th);
3525 static int lod_declare_dir_striping_create(const struct lu_env *env,
3526 struct dt_object *dt,
3527 struct lu_attr *attr,
3529 struct dt_object_format *dof,
3532 return lod_dir_striping_create_internal(env, dt, attr, lmu, dof, th,
3536 static int lod_dir_striping_create(const struct lu_env *env,
3537 struct dt_object *dt,
3538 struct lu_attr *attr,
3539 struct dt_object_format *dof,
3542 return lod_dir_striping_create_internal(env, dt, attr, NULL, dof, th,
3547 * Make LOV EA for striped object.
3549 * Generate striping information and store it in the LOV EA of the given
3550 * object. The caller must ensure nobody else is calling the function
3551 * against the object concurrently. The transaction must be started.
3552 * FLDB service must be running as well; it's used to map FID to the target,
3553 * which is stored in LOV EA.
3555 * \param[in] env execution environment for this thread
3556 * \param[in] lo LOD object
3557 * \param[in] th transaction handle
3559 * \retval 0 if LOV EA is stored successfully
3560 * \retval negative error number on failure
3562 static int lod_generate_and_set_lovea(const struct lu_env *env,
3563 struct lod_object *lo,
3566 struct lod_thread_info *info = lod_env_info(env);
3567 struct dt_object *next = dt_object_child(&lo->ldo_obj);
3568 struct lov_mds_md_v1 *lmm;
3574 if (lo->ldo_comp_cnt == 0) {
3575 lod_object_free_striping(env, lo);
3576 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
3580 lmm_size = lod_comp_md_size(lo, false);
3581 if (info->lti_ea_store_size < lmm_size) {
3582 rc = lod_ea_store_resize(info, lmm_size);
3586 lmm = info->lti_ea_store;
3588 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
3592 info->lti_buf.lb_buf = lmm;
3593 info->lti_buf.lb_len = lmm_size;
3594 rc = lod_sub_xattr_set(env, next, &info->lti_buf,
3595 XATTR_NAME_LOV, 0, th);
3600 * Delete layout component(s)
3602 * \param[in] env execution environment for this thread
3603 * \param[in] dt object
3604 * \param[in] th transaction handle
3606 * \retval 0 on success
3607 * \retval negative error number on failure
3609 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
3612 struct lod_layout_component *lod_comp;
3613 struct lod_object *lo = lod_dt_obj(dt);
3614 struct dt_object *next = dt_object_child(dt);
3615 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3618 LASSERT(lo->ldo_is_composite);
3619 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
3621 left = lo->ldo_comp_cnt;
3622 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3623 lod_comp = &lo->ldo_comp_entries[i];
3625 if (lod_comp->llc_id != LCME_ID_INVAL)
3629 /* Not instantiated component */
3630 if (lod_comp->llc_stripe == NULL)
3633 LASSERT(lod_comp->llc_stripe_count > 0);
3634 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
3635 struct dt_object *obj = lod_comp->llc_stripe[j];
3639 rc = lod_sub_destroy(env, obj, th);
3643 lu_object_put(env, &obj->do_lu);
3644 lod_comp->llc_stripe[j] = NULL;
3646 OBD_FREE(lod_comp->llc_stripe, sizeof(struct dt_object *) *
3647 lod_comp->llc_stripes_allocated);
3648 lod_comp->llc_stripe = NULL;
3649 lod_comp->llc_stripes_allocated = 0;
3650 lod_obj_set_pool(lo, i, NULL);
3651 if (lod_comp->llc_ostlist.op_array) {
3652 OBD_FREE(lod_comp->llc_ostlist.op_array,
3653 lod_comp->llc_ostlist.op_size);
3654 lod_comp->llc_ostlist.op_array = NULL;
3655 lod_comp->llc_ostlist.op_size = 0;
3659 LASSERTF(left >= 0 && left < lo->ldo_comp_cnt, "left = %d\n", left);
3661 struct lod_layout_component *comp_array;
3663 OBD_ALLOC(comp_array, sizeof(*comp_array) * left);
3664 if (comp_array == NULL)
3665 GOTO(out, rc = -ENOMEM);
3667 memcpy(&comp_array[0], &lo->ldo_comp_entries[0],
3668 sizeof(*comp_array) * left);
3670 OBD_FREE(lo->ldo_comp_entries,
3671 sizeof(*comp_array) * lo->ldo_comp_cnt);
3672 lo->ldo_comp_entries = comp_array;
3673 lo->ldo_comp_cnt = left;
3675 LASSERT(lo->ldo_mirror_count == 1);
3676 lo->ldo_mirrors[0].lme_end = left - 1;
3677 lod_obj_inc_layout_gen(lo);
3679 lod_free_comp_entries(lo);
3682 LASSERT(dt_object_exists(dt));
3683 rc = dt_attr_get(env, next, attr);
3687 if (attr->la_size > 0) {
3689 attr->la_valid = LA_SIZE;
3690 rc = lod_sub_attr_set(env, next, attr, th);
3695 rc = lod_generate_and_set_lovea(env, lo, th);
3699 lod_object_free_striping(env, lo);
3704 static int lod_get_default_lov_striping(const struct lu_env *env,
3705 struct lod_object *lo,
3706 struct lod_default_striping *lds);
3708 * Implementation of dt_object_operations::do_xattr_set.
3710 * Sets specified extended attribute on the object. Three types of EAs are
3712 * LOV EA - stores striping for a regular file or default striping (when set
3714 * LMV EA - stores a marker for the striped directories
3715 * DMV EA - stores default directory striping
3717 * When striping is applied to a non-striped existing object (this is called
3718 * late striping), then LOD notices the caller wants to turn the object into a
3719 * striped one. The stripe objects are created and appropriate EA is set:
3720 * LOV EA storing all the stripes directly or LMV EA storing just a small header
3721 * with striping configuration.
3723 * \see dt_object_operations::do_xattr_set() in the API description for details.
3725 static int lod_xattr_set(const struct lu_env *env,
3726 struct dt_object *dt, const struct lu_buf *buf,
3727 const char *name, int fl, struct thandle *th)
3729 struct dt_object *next = dt_object_child(dt);
3733 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3734 strcmp(name, XATTR_NAME_LMV) == 0) {
3735 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
3737 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
3738 LMV_HASH_FLAG_MIGRATION)
3739 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3741 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
3746 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3747 strcmp(name, XATTR_NAME_LOV) == 0) {
3748 struct lod_thread_info *info = lod_env_info(env);
3749 struct lod_default_striping *lds = &info->lti_def_striping;
3750 struct lov_user_md_v1 *v1 = buf->lb_buf;
3751 char pool[LOV_MAXPOOLNAME + 1];
3753 /* get existing striping config */
3754 rc = lod_get_default_lov_striping(env, lod_dt_obj(dt), lds);
3758 memset(pool, 0, sizeof(pool));
3759 if (lds->lds_def_striping_set == 1)
3760 lod_layout_get_pool(lds->lds_def_comp_entries,
3761 lds->lds_def_comp_cnt, pool,
3764 /* Retain the pool name if it is not given */
3765 if (v1->lmm_magic == LOV_USER_MAGIC_V1 && pool[0] != '\0') {
3766 struct lod_thread_info *info = lod_env_info(env);
3767 struct lov_user_md_v3 *v3 = info->lti_ea_store;
3769 memset(v3, 0, sizeof(*v3));
3770 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
3771 v3->lmm_pattern = cpu_to_le32(v1->lmm_pattern);
3772 v3->lmm_stripe_count =
3773 cpu_to_le32(v1->lmm_stripe_count);
3774 v3->lmm_stripe_offset =
3775 cpu_to_le32(v1->lmm_stripe_offset);
3776 v3->lmm_stripe_size = cpu_to_le32(v1->lmm_stripe_size);
3778 strlcpy(v3->lmm_pool_name, pool,
3779 sizeof(v3->lmm_pool_name));
3781 info->lti_buf.lb_buf = v3;
3782 info->lti_buf.lb_len = sizeof(*v3);
3783 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
3786 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name,
3790 if (lds->lds_def_striping_set == 1 &&
3791 lds->lds_def_comp_entries != NULL)
3792 lod_free_def_comp_entries(lds);
3795 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3796 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
3798 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
3801 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3802 (!strcmp(name, XATTR_NAME_LOV) ||
3803 !strncmp(name, XATTR_LUSTRE_LOV,
3804 strlen(XATTR_LUSTRE_LOV)))) {
3805 /* in case of lov EA swap, just set it
3806 * if not, it is a replay so check striping match what we
3807 * already have during req replay, declare_xattr_set()
3808 * defines striping, then create() does the work */
3809 if (fl & LU_XATTR_REPLACE) {
3810 /* free stripes, then update disk */
3811 lod_object_free_striping(env, lod_dt_obj(dt));
3813 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3814 } else if (dt_object_remote(dt)) {
3815 /* This only happens during migration, see
3816 * mdd_migrate_create(), in which Master MDT will
3817 * create a remote target object, and only set
3818 * (migrating) stripe EA on the remote object,
3819 * and does not need creating each stripes. */
3820 rc = lod_sub_xattr_set(env, next, buf, name,
3822 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
3823 /* delete component(s) */
3824 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
3825 rc = lod_layout_del(env, dt, th);
3828 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
3829 * it's going to create create file with specified
3830 * component(s), the striping must have not being
3831 * cached in this case;
3833 * Otherwise, it's going to add/change component(s) to
3834 * an existing file, the striping must have been cached
3837 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
3838 !strcmp(name, XATTR_NAME_LOV),
3839 !lod_dt_obj(dt)->ldo_comp_cached));
3841 rc = lod_striped_create(env, dt, NULL, NULL, th);
3844 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3845 rc = lod_replace_parent_fid(env, dt, th, false);
3850 /* then all other xattr */
3851 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3857 * Implementation of dt_object_operations::do_declare_xattr_del.
3859 * \see dt_object_operations::do_declare_xattr_del() in the API description
3862 static int lod_declare_xattr_del(const struct lu_env *env,
3863 struct dt_object *dt, const char *name,
3866 struct lod_object *lo = lod_dt_obj(dt);
3871 rc = lod_sub_declare_xattr_del(env, dt_object_child(dt), name, th);
3875 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3878 /* set xattr to each stripes, if needed */
3879 rc = lod_load_striping(env, lo);
3883 if (lo->ldo_dir_stripe_count == 0)
3886 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3887 LASSERT(lo->ldo_stripe[i]);
3888 rc = lod_sub_declare_xattr_del(env, lo->ldo_stripe[i],
3898 * Implementation of dt_object_operations::do_xattr_del.
3900 * If EA storing a regular striping is being deleted, then release
3901 * all the references to the stripe objects in core.
3903 * \see dt_object_operations::do_xattr_del() in the API description for details.
3905 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
3906 const char *name, struct thandle *th)
3908 struct dt_object *next = dt_object_child(dt);
3909 struct lod_object *lo = lod_dt_obj(dt);
3914 if (!strcmp(name, XATTR_NAME_LOV))
3915 lod_object_free_striping(env, lod_dt_obj(dt));
3917 rc = lod_sub_xattr_del(env, next, name, th);
3918 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3921 if (lo->ldo_dir_stripe_count == 0)
3924 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3925 LASSERT(lo->ldo_stripe[i]);
3927 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3936 * Implementation of dt_object_operations::do_xattr_list.
3938 * \see dt_object_operations::do_xattr_list() in the API description
3941 static int lod_xattr_list(const struct lu_env *env,
3942 struct dt_object *dt, const struct lu_buf *buf)
3944 return dt_xattr_list(env, dt_object_child(dt), buf);
3947 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
3949 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
3954 * Get default striping.
3956 * \param[in] env execution environment
3957 * \param[in] lo object
3958 * \param[out] lds default striping
3960 * \retval 0 on success
3961 * \retval negative if failed
3963 static int lod_get_default_lov_striping(const struct lu_env *env,
3964 struct lod_object *lo,
3965 struct lod_default_striping *lds)
3967 struct lod_thread_info *info = lod_env_info(env);
3968 struct lov_user_md_v1 *v1 = NULL;
3969 struct lov_user_md_v3 *v3 = NULL;
3970 struct lov_comp_md_v1 *comp_v1 = NULL;
3977 lds->lds_def_striping_set = 0;
3979 rc = lod_get_lov_ea(env, lo);
3983 if (rc < (typeof(rc))sizeof(struct lov_user_md))
3986 v1 = info->lti_ea_store;
3987 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
3988 lustre_swab_lov_user_md_v1(v1);
3989 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
3990 v3 = (struct lov_user_md_v3 *)v1;
3991 lustre_swab_lov_user_md_v3(v3);
3992 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3993 comp_v1 = (struct lov_comp_md_v1 *)v1;
3994 lustre_swab_lov_comp_md_v1(comp_v1);
3997 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
3998 v1->lmm_magic != LOV_MAGIC_COMP_V1)
4001 if (v1->lmm_magic == LOV_MAGIC_COMP_V1) {
4002 comp_v1 = (struct lov_comp_md_v1 *)v1;
4003 comp_cnt = comp_v1->lcm_entry_count;
4006 mirror_cnt = comp_v1->lcm_mirror_count + 1;
4014 /* realloc default comp entries if necessary */
4015 rc = lod_def_striping_comp_resize(lds, comp_cnt);
4019 lds->lds_def_comp_cnt = comp_cnt;
4020 lds->lds_def_striping_is_composite = composite;
4021 lds->lds_def_mirror_cnt = mirror_cnt;
4023 for (i = 0; i < comp_cnt; i++) {
4024 struct lod_layout_component *lod_comp;
4025 struct lu_extent *ext;
4028 lod_comp = &lds->lds_def_comp_entries[i];
4030 * reset lod_comp values, llc_stripes is always NULL in
4031 * the default striping template, llc_pool will be reset
4034 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
4037 v1 = (struct lov_user_md *)((char *)comp_v1 +
4038 comp_v1->lcm_entries[i].lcme_offset);
4039 ext = &comp_v1->lcm_entries[i].lcme_extent;
4040 lod_comp->llc_extent = *ext;
4043 if (v1->lmm_pattern != LOV_PATTERN_RAID0 &&
4044 v1->lmm_pattern != LOV_PATTERN_MDT &&
4045 v1->lmm_pattern != 0) {
4046 lod_free_def_comp_entries(lds);
4050 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d "
4051 "stripe_offset=%d\n",
4052 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
4053 (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
4054 (int)v1->lmm_stripe_offset);
4056 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
4057 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
4058 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
4059 lod_comp->llc_pattern = v1->lmm_pattern;
4062 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
4063 /* XXX: sanity check here */
4064 v3 = (struct lov_user_md_v3 *) v1;
4065 if (v3->lmm_pool_name[0] != '\0')
4066 pool = v3->lmm_pool_name;
4068 lod_set_def_pool(lds, i, pool);
4071 lds->lds_def_striping_set = 1;
4076 * Get default directory striping.
4078 * \param[in] env execution environment
4079 * \param[in] lo object
4080 * \param[out] lds default striping
4082 * \retval 0 on success
4083 * \retval negative if failed
4085 static int lod_get_default_lmv_striping(const struct lu_env *env,
4086 struct lod_object *lo,
4087 struct lod_default_striping *lds)
4089 struct lod_thread_info *info = lod_env_info(env);
4090 struct lmv_user_md_v1 *v1 = NULL;
4094 lds->lds_dir_def_striping_set = 0;
4095 rc = lod_get_default_lmv_ea(env, lo);
4099 if (rc < (typeof(rc))sizeof(struct lmv_user_md))
4102 v1 = info->lti_ea_store;
4104 lds->lds_dir_def_stripe_count = le32_to_cpu(v1->lum_stripe_count);
4105 lds->lds_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
4106 lds->lds_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
4107 lds->lds_dir_def_striping_set = 1;
4113 * Get default striping in the object.
4115 * Get object default striping and default directory striping.
4117 * \param[in] env execution environment
4118 * \param[in] lo object
4119 * \param[out] lds default striping
4121 * \retval 0 on success
4122 * \retval negative if failed
4124 static int lod_get_default_striping(const struct lu_env *env,
4125 struct lod_object *lo,
4126 struct lod_default_striping *lds)
4130 rc = lod_get_default_lov_striping(env, lo, lds);
4131 rc1 = lod_get_default_lmv_striping(env, lo, lds);
4132 if (rc == 0 && rc1 < 0)
4139 * Apply default striping on object.
4141 * If object striping pattern is not set, set to the one in default striping.
4142 * The default striping is from parent or fs.
4144 * \param[in] lo new object
4145 * \param[in] lds default striping
4146 * \param[in] mode new object's mode
4148 static void lod_striping_from_default(struct lod_object *lo,
4149 const struct lod_default_striping *lds,
4152 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4153 struct lov_desc *desc = &d->lod_desc;
4156 if (lds->lds_def_striping_set && S_ISREG(mode)) {
4157 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
4158 lds->lds_def_comp_cnt);
4162 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
4163 if (lds->lds_def_mirror_cnt > 1)
4164 lo->ldo_flr_state = LCM_FL_RDONLY;
4166 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4167 struct lod_layout_component *obj_comp =
4168 &lo->ldo_comp_entries[i];
4169 struct lod_layout_component *def_comp =
4170 &lds->lds_def_comp_entries[i];
4172 CDEBUG(D_LAYOUT, "Inherite from default: size:%hu "
4173 "nr:%u offset:%u pattern %#x %s\n",
4174 def_comp->llc_stripe_size,
4175 def_comp->llc_stripe_count,
4176 def_comp->llc_stripe_offset,
4177 def_comp->llc_pattern,
4178 def_comp->llc_pool ?: "");
4180 *obj_comp = *def_comp;
4181 if (def_comp->llc_pool != NULL) {
4182 /* pointer was copied from def_comp */
4183 obj_comp->llc_pool = NULL;
4184 lod_obj_set_pool(lo, i, def_comp->llc_pool);
4188 * Don't initialize these fields for plain layout
4189 * (v1/v3) here, they are inherited in the order of
4190 * 'parent' -> 'fs default (root)' -> 'global default
4191 * values for stripe_count & stripe_size'.
4193 * see lod_ah_init().
4195 if (!lo->ldo_is_composite)
4198 lod_adjust_stripe_info(obj_comp, desc);
4200 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
4201 if (lo->ldo_dir_stripe_count == 0)
4202 lo->ldo_dir_stripe_count =
4203 lds->lds_dir_def_stripe_count;
4204 if (lo->ldo_dir_stripe_offset == -1)
4205 lo->ldo_dir_stripe_offset =
4206 lds->lds_dir_def_stripe_offset;
4207 if (lo->ldo_dir_hash_type == 0)
4208 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
4210 CDEBUG(D_LAYOUT, "striping from default dir: count:%hu, "
4211 "offset:%u, hash_type:%u\n",
4212 lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
4213 lo->ldo_dir_hash_type);
4217 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root)
4219 struct lod_layout_component *lod_comp;
4221 if (lo->ldo_comp_cnt == 0)
4224 if (lo->ldo_is_composite)
4227 lod_comp = &lo->ldo_comp_entries[0];
4229 if (lod_comp->llc_stripe_count <= 0 ||
4230 lod_comp->llc_stripe_size <= 0)
4233 if (from_root && (lod_comp->llc_pool == NULL ||
4234 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
4241 * Implementation of dt_object_operations::do_ah_init.
4243 * This method is used to make a decision on the striping configuration for the
4244 * object being created. It can be taken from the \a parent object if it exists,
4245 * or filesystem's default. The resulting configuration (number of stripes,
4246 * stripe size/offset, pool name, etc) is stored in the object itself and will
4247 * be used by the methods like ->doo_declare_create().
4249 * \see dt_object_operations::do_ah_init() in the API description for details.
4251 static void lod_ah_init(const struct lu_env *env,
4252 struct dt_allocation_hint *ah,
4253 struct dt_object *parent,
4254 struct dt_object *child,
4257 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
4258 struct lod_thread_info *info = lod_env_info(env);
4259 struct lod_default_striping *lds = &info->lti_def_striping;
4260 struct dt_object *nextp = NULL;
4261 struct dt_object *nextc;
4262 struct lod_object *lp = NULL;
4263 struct lod_object *lc;
4264 struct lov_desc *desc;
4265 struct lod_layout_component *lod_comp;
4271 if (likely(parent)) {
4272 nextp = dt_object_child(parent);
4273 lp = lod_dt_obj(parent);
4276 nextc = dt_object_child(child);
4277 lc = lod_dt_obj(child);
4279 LASSERT(!lod_obj_is_striped(child));
4280 /* default layout template may have been set on the regular file
4281 * when this is called from mdd_create_data() */
4282 if (S_ISREG(child_mode))
4283 lod_free_comp_entries(lc);
4285 if (!dt_object_exists(nextc))
4286 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
4288 if (S_ISDIR(child_mode)) {
4289 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
4291 /* other default values are 0 */
4292 lc->ldo_dir_stripe_offset = -1;
4294 /* get default striping from parent object */
4295 if (likely(lp != NULL))
4296 lod_get_default_striping(env, lp, lds);
4298 /* set child default striping info, default value is NULL */
4299 if (lds->lds_def_striping_set || lds->lds_dir_def_striping_set)
4300 lc->ldo_def_striping = lds;
4302 /* It should always honour the specified stripes */
4303 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
4304 * will have old magic. In this case, we should ignore the
4305 * stripe count and try to create dir by default stripe.
4307 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
4308 (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
4309 le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
4310 lc->ldo_dir_stripe_count =
4311 le32_to_cpu(lum1->lum_stripe_count);
4312 lc->ldo_dir_stripe_offset =
4313 le32_to_cpu(lum1->lum_stripe_offset);
4314 lc->ldo_dir_hash_type =
4315 le32_to_cpu(lum1->lum_hash_type);
4317 "set dirstripe: count %hu, offset %d, hash %u\n",
4318 lc->ldo_dir_stripe_count,
4319 (int)lc->ldo_dir_stripe_offset,
4320 lc->ldo_dir_hash_type);
4322 /* transfer defaults LMV to new directory */
4323 lod_striping_from_default(lc, lds, child_mode);
4326 /* shrink the stripe_count to the avaible MDT count */
4327 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
4328 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))
4329 lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
4331 /* Directory will be striped only if stripe_count > 1, if
4332 * stripe_count == 1, let's reset stripe_count = 0 to avoid
4333 * create single master stripe and also help to unify the
4334 * stripe handling of directories and files */
4335 if (lc->ldo_dir_stripe_count == 1)
4336 lc->ldo_dir_stripe_count = 0;
4338 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
4339 lc->ldo_dir_stripe_count,
4340 (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
4345 /* child object regular file*/
4347 if (!lod_object_will_be_striped(S_ISREG(child_mode),
4348 lu_object_fid(&child->do_lu)))
4351 /* If object is going to be striped over OSTs, transfer default
4352 * striping information to the child, so that we can use it
4353 * during declaration and creation.
4355 * Try from the parent first.
4357 if (likely(lp != NULL)) {
4358 rc = lod_get_default_lov_striping(env, lp, lds);
4360 lod_striping_from_default(lc, lds, child_mode);
4363 /* Initialize lod_device::lod_md_root object reference */
4364 if (d->lod_md_root == NULL) {
4365 struct dt_object *root;
4366 struct lod_object *lroot;
4368 lu_root_fid(&info->lti_fid);
4369 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
4370 if (!IS_ERR(root)) {
4371 lroot = lod_dt_obj(root);
4373 spin_lock(&d->lod_lock);
4374 if (d->lod_md_root != NULL)
4375 dt_object_put(env, &d->lod_md_root->ldo_obj);
4376 d->lod_md_root = lroot;
4377 spin_unlock(&d->lod_lock);
4381 /* try inherit layout from the root object (fs default) when:
4382 * - parent does not have default layout; or
4383 * - parent has plain(v1/v3) default layout, and some attributes
4384 * are not specified in the default layout;
4386 if (d->lod_md_root != NULL && lod_need_inherit_more(lc, true)) {
4387 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds);
4390 if (lc->ldo_comp_cnt == 0) {
4391 lod_striping_from_default(lc, lds, child_mode);
4392 } else if (!lds->lds_def_striping_is_composite) {
4393 struct lod_layout_component *def_comp;
4395 LASSERT(!lc->ldo_is_composite);
4396 lod_comp = &lc->ldo_comp_entries[0];
4397 def_comp = &lds->lds_def_comp_entries[0];
4399 if (lod_comp->llc_stripe_count <= 0)
4400 lod_comp->llc_stripe_count =
4401 def_comp->llc_stripe_count;
4402 if (lod_comp->llc_stripe_size <= 0)
4403 lod_comp->llc_stripe_size =
4404 def_comp->llc_stripe_size;
4405 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
4406 lod_comp->llc_stripe_offset =
4407 def_comp->llc_stripe_offset;
4408 if (lod_comp->llc_pool == NULL)
4409 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
4414 * fs default striping may not be explicitly set, or historically set
4415 * in config log, use them.
4417 if (lod_need_inherit_more(lc, false)) {
4418 if (lc->ldo_comp_cnt == 0) {
4419 rc = lod_alloc_comp_entries(lc, 0, 1);
4421 /* fail to allocate memory, will create a
4422 * non-striped file. */
4424 lc->ldo_is_composite = 0;
4425 lod_comp = &lc->ldo_comp_entries[0];
4426 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4428 LASSERT(!lc->ldo_is_composite);
4429 lod_comp = &lc->ldo_comp_entries[0];
4430 desc = &d->lod_desc;
4431 lod_adjust_stripe_info(lod_comp, desc);
4437 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
4439 * Size initialization on late striping.
4441 * Propagate the size of a truncated object to a deferred striping.
4442 * This function handles a special case when truncate was done on a
4443 * non-striped object and now while the striping is being created
4444 * we can't lose that size, so we have to propagate it to the stripes
4447 * \param[in] env execution environment
4448 * \param[in] dt object
4449 * \param[in] th transaction handle
4451 * \retval 0 on success
4452 * \retval negative if failed
4454 static int lod_declare_init_size(const struct lu_env *env,
4455 struct dt_object *dt, struct thandle *th)
4457 struct dt_object *next = dt_object_child(dt);
4458 struct lod_object *lo = lod_dt_obj(dt);
4459 struct dt_object **objects = NULL;
4460 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4461 uint64_t size, offs;
4462 int i, rc, stripe, stripe_count = 0, stripe_size = 0;
4463 struct lu_extent size_ext;
4466 if (!lod_obj_is_striped(dt))
4469 rc = dt_attr_get(env, next, attr);
4470 LASSERT(attr->la_valid & LA_SIZE);
4474 size = attr->la_size;
4478 size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
4479 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4480 struct lod_layout_component *lod_comp;
4481 struct lu_extent *extent;
4483 lod_comp = &lo->ldo_comp_entries[i];
4485 if (lod_comp->llc_stripe == NULL)
4488 extent = &lod_comp->llc_extent;
4489 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
4490 if (!lo->ldo_is_composite ||
4491 lu_extent_is_overlapped(extent, &size_ext)) {
4492 objects = lod_comp->llc_stripe;
4493 stripe_count = lod_comp->llc_stripe_count;
4494 stripe_size = lod_comp->llc_stripe_size;
4497 if (stripe_count == 0)
4500 LASSERT(objects != NULL && stripe_size != 0);
4501 /* ll_do_div64(a, b) returns a % b, and a = a / b */
4502 ll_do_div64(size, (__u64)stripe_size);
4503 stripe = ll_do_div64(size, (__u64)stripe_count);
4504 LASSERT(objects[stripe] != NULL);
4506 size = size * stripe_size;
4507 offs = attr->la_size;
4508 size += ll_do_div64(offs, stripe_size);
4510 attr->la_valid = LA_SIZE;
4511 attr->la_size = size;
4513 rc = lod_sub_declare_attr_set(env, objects[stripe],
4522 * Declare creation of striped object.
4524 * The function declares creation stripes for a regular object. The function
4525 * also declares whether the stripes will be created with non-zero size if
4526 * previously size was set non-zero on the master object. If object \a dt is
4527 * not local, then only fully defined striping can be applied in \a lovea.
4528 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
4531 * \param[in] env execution environment
4532 * \param[in] dt object
4533 * \param[in] attr attributes the stripes will be created with
4534 * \param[in] lovea a buffer containing striping description
4535 * \param[in] th transaction handle
4537 * \retval 0 on success
4538 * \retval negative if failed
4540 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
4541 struct lu_attr *attr,
4542 const struct lu_buf *lovea, struct thandle *th)
4544 struct lod_thread_info *info = lod_env_info(env);
4545 struct dt_object *next = dt_object_child(dt);
4546 struct lod_object *lo = lod_dt_obj(dt);
4550 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
4551 GOTO(out, rc = -ENOMEM);
4553 if (!dt_object_remote(next)) {
4554 /* choose OST and generate appropriate objects */
4555 rc = lod_prepare_create(env, lo, attr, lovea, th);
4560 * declare storage for striping data
4562 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
4564 /* LOD can not choose OST objects for remote objects, i.e.
4565 * stripes must be ready before that. Right now, it can only
4566 * happen during migrate, i.e. migrate process needs to create
4567 * remote regular file (mdd_migrate_create), then the migrate
4568 * process will provide stripeEA. */
4569 LASSERT(lovea != NULL);
4570 info->lti_buf = *lovea;
4573 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
4574 XATTR_NAME_LOV, 0, th);
4579 * if striping is created with local object's size > 0,
4580 * we have to propagate this size to specific object
4581 * the case is possible only when local object was created previously
4583 if (dt_object_exists(next))
4584 rc = lod_declare_init_size(env, dt, th);
4587 /* failed to create striping or to set initial size, let's reset
4588 * config so that others don't get confused */
4590 lod_object_free_striping(env, lo);
4596 * Implementation of dt_object_operations::do_declare_create.
4598 * The method declares creation of a new object. If the object will be striped,
4599 * then helper functions are called to find FIDs for the stripes, declare
4600 * creation of the stripes and declare initialization of the striping
4601 * information to be stored in the master object.
4603 * \see dt_object_operations::do_declare_create() in the API description
4606 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
4607 struct lu_attr *attr,
4608 struct dt_allocation_hint *hint,
4609 struct dt_object_format *dof, struct thandle *th)
4611 struct dt_object *next = dt_object_child(dt);
4612 struct lod_object *lo = lod_dt_obj(dt);
4621 * first of all, we declare creation of local object
4623 rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
4628 * it's lod_ah_init() that has decided the object will be striped
4630 if (dof->dof_type == DFT_REGULAR) {
4631 /* callers don't want stripes */
4632 /* XXX: all tricky interactions with ->ah_make_hint() decided
4633 * to use striping, then ->declare_create() behaving differently
4634 * should be cleaned */
4635 if (dof->u.dof_reg.striped != 0)
4636 rc = lod_declare_striped_create(env, dt, attr,
4638 } else if (dof->dof_type == DFT_DIR) {
4639 struct seq_server_site *ss;
4640 struct lu_buf buf = { NULL };
4641 struct lu_buf *lmu = NULL;
4643 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
4645 /* If the parent has default stripeEA, and client
4646 * did not find it before sending create request,
4647 * then MDT will return -EREMOTE, and client will
4648 * retrieve the default stripeEA and re-create the
4651 * Note: if dah_eadata != NULL, it means creating the
4652 * striped directory with specified stripeEA, then it
4653 * should ignore the default stripeEA */
4654 if (hint != NULL && hint->dah_eadata == NULL) {
4655 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
4656 GOTO(out, rc = -EREMOTE);
4658 if (lo->ldo_dir_stripe_offset == -1) {
4659 /* child and parent should be in the same MDT */
4660 if (hint->dah_parent != NULL &&
4661 dt_object_remote(hint->dah_parent))
4662 GOTO(out, rc = -EREMOTE);
4663 } else if (lo->ldo_dir_stripe_offset !=
4665 struct lod_device *lod;
4666 struct lod_tgt_descs *ltd;
4667 struct lod_tgt_desc *tgt = NULL;
4668 bool found_mdt = false;
4671 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4672 ltd = &lod->lod_mdt_descs;
4673 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
4674 tgt = LTD_TGT(ltd, i);
4675 if (tgt->ltd_index ==
4676 lo->ldo_dir_stripe_offset) {
4682 /* If the MDT indicated by stripe_offset can be
4683 * found, then tell client to resend the create
4684 * request to the correct MDT, otherwise return
4685 * error to client */
4687 GOTO(out, rc = -EREMOTE);
4689 GOTO(out, rc = -EINVAL);
4691 } else if (hint && hint->dah_eadata) {
4693 lmu->lb_buf = (void *)hint->dah_eadata;
4694 lmu->lb_len = hint->dah_eadata_len;
4697 rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
4701 /* failed to create striping or to set initial size, let's reset
4702 * config so that others don't get confused */
4704 lod_object_free_striping(env, lo);
4709 * Generate component ID for new created component.
4711 * \param[in] lo LOD object
4712 * \param[in] comp_idx index of ldo_comp_entries
4714 * \retval component ID on success
4715 * \retval LCME_ID_INVAL on failure
4717 static __u32 lod_gen_component_id(struct lod_object *lo,
4718 int mirror_id, int comp_idx)
4720 struct lod_layout_component *lod_comp;
4721 __u32 id, start, end;
4724 LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
4726 lod_obj_inc_layout_gen(lo);
4727 id = lo->ldo_layout_gen;
4728 if (likely(id <= SEQ_ID_MAX))
4729 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
4731 /* Layout generation wraps, need to check collisions. */
4732 start = id & SEQ_ID_MASK;
4735 for (id = start; id <= end; id++) {
4736 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4737 lod_comp = &lo->ldo_comp_entries[i];
4738 if (pflr_id(mirror_id, id) == lod_comp->llc_id)
4741 /* Found the ununsed ID */
4742 if (i == lo->ldo_comp_cnt)
4743 RETURN(pflr_id(mirror_id, id));
4745 if (end == LCME_ID_MAX) {
4747 end = min(lo->ldo_layout_gen & LCME_ID_MASK,
4748 (__u32)(LCME_ID_MAX - 1));
4752 RETURN(LCME_ID_INVAL);
4756 * Creation of a striped regular object.
4758 * The function is called to create the stripe objects for a regular
4759 * striped file. This can happen at the initial object creation or
4760 * when the caller asks LOD to do so using ->do_xattr_set() method
4761 * (so called late striping). Notice all the information are already
4762 * prepared in the form of the list of objects (ldo_stripe field).
4763 * This is done during declare phase.
4765 * \param[in] env execution environment
4766 * \param[in] dt object
4767 * \param[in] attr attributes the stripes will be created with
4768 * \param[in] dof format of stripes (see OSD API description)
4769 * \param[in] th transaction handle
4771 * \retval 0 on success
4772 * \retval negative if failed
4774 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
4775 struct lu_attr *attr, struct dt_object_format *dof,
4778 struct lod_layout_component *lod_comp;
4779 struct lod_object *lo = lod_dt_obj(dt);
4784 LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
4786 mirror_id = lo->ldo_mirror_count > 1 ? 1 : 0;
4788 /* create all underlying objects */
4789 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4790 lod_comp = &lo->ldo_comp_entries[i];
4792 if (lod_comp->llc_extent.e_start == 0 && i > 0) /* new mirror */
4795 if (lod_comp->llc_id == LCME_ID_INVAL) {
4796 lod_comp->llc_id = lod_gen_component_id(lo,
4798 if (lod_comp->llc_id == LCME_ID_INVAL)
4799 GOTO(out, rc = -ERANGE);
4802 if (lod_comp_inited(lod_comp))
4805 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
4806 lod_comp_set_init(lod_comp);
4808 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
4809 lod_comp_set_init(lod_comp);
4811 if (lod_comp->llc_stripe == NULL)
4814 LASSERT(lod_comp->llc_stripe_count);
4815 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4816 struct dt_object *object = lod_comp->llc_stripe[j];
4817 LASSERT(object != NULL);
4818 rc = lod_sub_create(env, object, attr, NULL, dof, th);
4822 lod_comp_set_init(lod_comp);
4825 rc = lod_fill_mirrors(lo);
4829 rc = lod_generate_and_set_lovea(env, lo, th);
4833 lo->ldo_comp_cached = 1;
4837 lod_object_free_striping(env, lo);
4842 * Implementation of dt_object_operations::do_create.
4844 * If any of preceeding methods (like ->do_declare_create(),
4845 * ->do_ah_init(), etc) chose to create a striped object,
4846 * then this method will create the master and the stripes.
4848 * \see dt_object_operations::do_create() in the API description for details.
4850 static int lod_create(const struct lu_env *env, struct dt_object *dt,
4851 struct lu_attr *attr, struct dt_allocation_hint *hint,
4852 struct dt_object_format *dof, struct thandle *th)
4857 /* create local object */
4858 rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
4862 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4863 lod_obj_is_striped(dt) && dof->u.dof_reg.striped != 0) {
4864 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
4865 rc = lod_striped_create(env, dt, attr, dof, th);
4872 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
4873 struct dt_object *dt, struct thandle *th,
4874 int comp_idx, int stripe_idx,
4875 struct lod_obj_stripe_cb_data *data)
4877 if (data->locd_declare)
4878 return lod_sub_declare_destroy(env, dt, th);
4879 else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4880 stripe_idx == cfs_fail_val)
4881 return lod_sub_destroy(env, dt, th);
4887 * Implementation of dt_object_operations::do_declare_destroy.
4889 * If the object is a striped directory, then the function declares reference
4890 * removal from the master object (this is an index) to the stripes and declares
4891 * destroy of all the stripes. In all the cases, it declares an intention to
4892 * destroy the object itself.
4894 * \see dt_object_operations::do_declare_destroy() in the API description
4897 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
4900 struct dt_object *next = dt_object_child(dt);
4901 struct lod_object *lo = lod_dt_obj(dt);
4902 struct lod_thread_info *info = lod_env_info(env);
4903 char *stripe_name = info->lti_key;
4908 * load striping information, notice we don't do this when object
4909 * is being initialized as we don't need this information till
4910 * few specific cases like destroy, chown
4912 rc = lod_load_striping(env, lo);
4916 /* declare destroy for all underlying objects */
4917 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4918 rc = next->do_ops->do_index_try(env, next,
4919 &dt_directory_features);
4923 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4924 rc = lod_sub_declare_ref_del(env, next, th);
4928 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4929 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4931 rc = lod_sub_declare_delete(env, next,
4932 (const struct dt_key *)stripe_name, th);
4939 * we declare destroy for the local object
4941 rc = lod_sub_declare_destroy(env, next, th);
4945 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4946 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4949 if (!lod_obj_is_striped(dt))
4952 /* declare destroy all striped objects */
4953 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4954 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4955 if (lo->ldo_stripe[i] == NULL)
4958 rc = lod_sub_declare_ref_del(env, lo->ldo_stripe[i],
4961 rc = lod_sub_declare_destroy(env, lo->ldo_stripe[i],
4967 struct lod_obj_stripe_cb_data data = { { 0 } };
4969 data.locd_declare = true;
4970 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
4971 rc = lod_obj_for_each_stripe(env, lo, th, &data);
4978 * Implementation of dt_object_operations::do_destroy.
4980 * If the object is a striped directory, then the function removes references
4981 * from the master object (this is an index) to the stripes and destroys all
4982 * the stripes. In all the cases, the function destroys the object itself.
4984 * \see dt_object_operations::do_destroy() in the API description for details.
4986 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
4989 struct dt_object *next = dt_object_child(dt);
4990 struct lod_object *lo = lod_dt_obj(dt);
4991 struct lod_thread_info *info = lod_env_info(env);
4992 char *stripe_name = info->lti_key;
4997 /* destroy sub-stripe of master object */
4998 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4999 rc = next->do_ops->do_index_try(env, next,
5000 &dt_directory_features);
5004 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5005 rc = lod_sub_ref_del(env, next, th);
5009 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
5010 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
5013 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
5014 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
5015 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
5017 rc = lod_sub_delete(env, next,
5018 (const struct dt_key *)stripe_name, th);
5024 rc = lod_sub_destroy(env, next, th);
5028 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
5029 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
5032 if (!lod_obj_is_striped(dt))
5035 /* destroy all striped objects */
5036 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5037 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5038 if (lo->ldo_stripe[i] == NULL)
5040 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
5041 i == cfs_fail_val) {
5042 dt_write_lock(env, lo->ldo_stripe[i],
5044 rc = lod_sub_ref_del(env, lo->ldo_stripe[i],
5046 dt_write_unlock(env, lo->ldo_stripe[i]);
5050 rc = lod_sub_destroy(env, lo->ldo_stripe[i],
5057 struct lod_obj_stripe_cb_data data = { { 0 } };
5059 data.locd_declare = false;
5060 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
5061 rc = lod_obj_for_each_stripe(env, lo, th, &data);
5068 * Implementation of dt_object_operations::do_declare_ref_add.
5070 * \see dt_object_operations::do_declare_ref_add() in the API description
5073 static int lod_declare_ref_add(const struct lu_env *env,
5074 struct dt_object *dt, struct thandle *th)
5076 return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
5080 * Implementation of dt_object_operations::do_ref_add.
5082 * \see dt_object_operations::do_ref_add() in the API description for details.
5084 static int lod_ref_add(const struct lu_env *env,
5085 struct dt_object *dt, struct thandle *th)
5087 return lod_sub_ref_add(env, dt_object_child(dt), th);
5091 * Implementation of dt_object_operations::do_declare_ref_del.
5093 * \see dt_object_operations::do_declare_ref_del() in the API description
5096 static int lod_declare_ref_del(const struct lu_env *env,
5097 struct dt_object *dt, struct thandle *th)
5099 return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
5103 * Implementation of dt_object_operations::do_ref_del
5105 * \see dt_object_operations::do_ref_del() in the API description for details.
5107 static int lod_ref_del(const struct lu_env *env,
5108 struct dt_object *dt, struct thandle *th)
5110 return lod_sub_ref_del(env, dt_object_child(dt), th);
5114 * Implementation of dt_object_operations::do_object_sync.
5116 * \see dt_object_operations::do_object_sync() in the API description
5119 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
5120 __u64 start, __u64 end)
5122 return dt_object_sync(env, dt_object_child(dt), start, end);
5126 * Release LDLM locks on the stripes of a striped directory.
5128 * Iterates over all the locks taken on the stripe objects and
5131 * \param[in] env execution environment
5132 * \param[in] dt striped object
5133 * \param[in] einfo lock description
5134 * \param[in] policy data describing requested lock
5136 * \retval 0 on success
5137 * \retval negative if failed
5139 static int lod_object_unlock_internal(const struct lu_env *env,
5140 struct dt_object *dt,
5141 struct ldlm_enqueue_info *einfo,
5142 union ldlm_policy_data *policy)
5144 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
5149 if (slave_locks == NULL)
5152 for (i = 1; i < slave_locks->count; i++) {
5153 if (lustre_handle_is_used(&slave_locks->handles[i]))
5154 ldlm_lock_decref_and_cancel(&slave_locks->handles[i],
5162 * Implementation of dt_object_operations::do_object_unlock.
5164 * Used to release LDLM lock(s).
5166 * \see dt_object_operations::do_object_unlock() in the API description
5169 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
5170 struct ldlm_enqueue_info *einfo,
5171 union ldlm_policy_data *policy)
5173 struct lod_object *lo = lod_dt_obj(dt);
5174 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
5175 int slave_locks_size;
5179 if (slave_locks == NULL)
5182 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
5183 LASSERT(lo->ldo_dir_stripe_count > 1);
5184 /* Note: for remote lock for single stripe dir, MDT will cancel
5185 * the lock by lockh directly */
5186 LASSERT(!dt_object_remote(dt_object_child(dt)));
5188 /* locks were unlocked in MDT layer */
5189 for (i = 1; i < slave_locks->count; i++) {
5190 LASSERT(!lustre_handle_is_used(&slave_locks->handles[i]));
5191 dt_invalidate(env, lo->ldo_stripe[i]);
5194 slave_locks_size = sizeof(*slave_locks) + slave_locks->count *
5195 sizeof(slave_locks->handles[0]);
5196 OBD_FREE(slave_locks, slave_locks_size);
5197 einfo->ei_cbdata = NULL;
5203 * Implementation of dt_object_operations::do_object_lock.
5205 * Used to get LDLM lock on the non-striped and striped objects.
5207 * \see dt_object_operations::do_object_lock() in the API description
5210 static int lod_object_lock(const struct lu_env *env,
5211 struct dt_object *dt,
5212 struct lustre_handle *lh,
5213 struct ldlm_enqueue_info *einfo,
5214 union ldlm_policy_data *policy)
5216 struct lod_object *lo = lod_dt_obj(dt);
5219 int slave_locks_size;
5220 struct lustre_handle_array *slave_locks = NULL;
5223 /* remote object lock */
5224 if (!einfo->ei_enq_slave) {
5225 LASSERT(dt_object_remote(dt));
5226 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
5230 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
5231 GOTO(out, rc = -ENOTDIR);
5233 rc = lod_load_striping(env, lo);
5238 if (lo->ldo_dir_stripe_count <= 1) {
5240 * NB, ei_cbdata stores pointer to slave locks, if no locks
5241 * taken, make sure it's set to NULL, otherwise MDT will try to
5244 einfo->ei_cbdata = NULL;
5248 slave_locks_size = sizeof(*slave_locks) + lo->ldo_dir_stripe_count *
5249 sizeof(slave_locks->handles[0]);
5250 /* Freed in lod_object_unlock */
5251 OBD_ALLOC(slave_locks, slave_locks_size);
5252 if (slave_locks == NULL)
5253 GOTO(out, rc = -ENOMEM);
5254 slave_locks->count = lo->ldo_dir_stripe_count;
5256 /* striped directory lock */
5257 for (i = 1; i < lo->ldo_dir_stripe_count; i++) {
5258 struct lustre_handle lockh;
5259 struct ldlm_res_id *res_id;
5261 res_id = &lod_env_info(env)->lti_res_id;
5262 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
5264 einfo->ei_res_id = res_id;
5266 LASSERT(lo->ldo_stripe[i] != NULL);
5267 if (likely(dt_object_remote(lo->ldo_stripe[i]))) {
5268 rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh,
5271 struct ldlm_namespace *ns = einfo->ei_namespace;
5272 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
5273 ldlm_completion_callback completion = einfo->ei_cb_cp;
5274 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
5276 if (einfo->ei_mode == LCK_PW ||
5277 einfo->ei_mode == LCK_EX)
5278 dlmflags |= LDLM_FL_COS_INCOMPAT;
5280 /* This only happens if there are mulitple stripes
5281 * on the master MDT, i.e. except stripe0, there are
5282 * other stripes on the Master MDT as well, Only
5283 * happens in the test case right now. */
5284 LASSERT(ns != NULL);
5285 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS,
5286 policy, einfo->ei_mode,
5287 &dlmflags, blocking,
5289 NULL, 0, LVB_T_NONE,
5294 slave_locks->handles[i] = lockh;
5296 einfo->ei_cbdata = slave_locks;
5298 if (rc != 0 && slave_locks != NULL) {
5299 lod_object_unlock_internal(env, dt, einfo, policy);
5300 OBD_FREE(slave_locks, slave_locks_size);
5305 einfo->ei_cbdata = NULL;
5310 * Implementation of dt_object_operations::do_invalidate.
5312 * \see dt_object_operations::do_invalidate() in the API description for details
5314 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
5316 return dt_invalidate(env, dt_object_child(dt));
5319 static int lod_layout_data_init(struct lod_thread_info *info, __u32 comp_cnt)
5323 /* clear memory region that will be used for layout change */
5324 memset(&info->lti_layout_attr, 0, sizeof(struct lu_attr));
5325 info->lti_count = 0;
5327 if (info->lti_comp_size >= comp_cnt)
5330 if (info->lti_comp_size > 0) {
5331 OBD_FREE(info->lti_comp_idx,
5332 info->lti_comp_size * sizeof(__u32));
5333 info->lti_comp_size = 0;
5336 OBD_ALLOC(info->lti_comp_idx, comp_cnt * sizeof(__u32));
5337 if (!info->lti_comp_idx)
5340 info->lti_comp_size = comp_cnt;
5344 static int lod_declare_instantiate_components(const struct lu_env *env,
5345 struct lod_object *lo, struct thandle *th)
5347 struct lod_thread_info *info = lod_env_info(env);
5348 struct ost_pool *inuse = &info->lti_inuse_osts;
5353 LASSERT(info->lti_count < lo->ldo_comp_cnt);
5354 if (info->lti_count > 0) {
5355 /* Prepare inuse array for composite file */
5356 rc = lod_prepare_inuse(env, lo);
5361 for (i = 0; i < info->lti_count; i++) {
5362 rc = lod_qos_prep_create(env, lo, NULL, th,
5363 info->lti_comp_idx[i], inuse);
5369 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5370 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
5371 &info->lti_buf, XATTR_NAME_LOV, 0, th);
5377 static int lod_declare_update_plain(const struct lu_env *env,
5378 struct lod_object *lo, struct layout_intent *layout,
5379 const struct lu_buf *buf, struct thandle *th)
5381 struct lod_thread_info *info = lod_env_info(env);
5382 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5383 struct lod_layout_component *lod_comp;
5384 struct lov_comp_md_v1 *comp_v1 = NULL;
5385 bool replay = false;
5389 LASSERT(lo->ldo_flr_state == LCM_FL_NOT_FLR);
5392 * In case the client is passing lovea, which only happens during
5393 * the replay of layout intent write RPC for now, we may need to
5394 * parse the lovea and apply new layout configuration.
5396 if (buf && buf->lb_len) {
5397 struct lov_user_md_v1 *v1 = buf->lb_buf;
5399 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
5400 v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
5401 LOV_MAGIC_COMP_V1)) {
5402 CERROR("%s: the replay buffer of layout extend "
5403 "(magic %#x) does not contain expected "
5404 "composite layout.\n",
5405 lod2obd(d)->obd_name, v1->lmm_magic);
5406 GOTO(out, rc = -EINVAL);
5409 lod_object_free_striping(env, lo);
5410 rc = lod_use_defined_striping(env, lo, buf);
5414 rc = lod_get_lov_ea(env, lo);
5417 /* old on-disk EA is stored in info->lti_buf */
5418 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
5421 /* non replay path */
5422 rc = lod_load_striping_locked(env, lo);
5427 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
5429 * trunc transfers [size, eof) in the intent extent, while
5430 * we'd instantiated components covers [0, size).
5432 layout->li_extent.e_end = layout->li_extent.e_start;
5433 layout->li_extent.e_start = 0;
5436 /* Make sure defined layout covers the requested write range. */
5437 lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
5438 if (lo->ldo_comp_cnt > 1 &&
5439 lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
5440 lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
5441 CDEBUG(replay ? D_ERROR : D_LAYOUT,
5442 "%s: the defined layout [0, %#llx) does not covers "
5443 "the write range "DEXT"\n",
5444 lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
5445 PEXT(&layout->li_extent));
5446 GOTO(out, rc = -EINVAL);
5449 CDEBUG(D_LAYOUT, "%s: "DFID": instantiate components "DEXT"\n",
5450 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
5451 PEXT(&layout->li_extent));
5454 * Iterate ld->ldo_comp_entries, find the component whose extent under
5455 * the write range and not instantianted.
5457 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5458 lod_comp = &lo->ldo_comp_entries[i];
5460 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
5464 if (lod_comp_inited(lod_comp))
5468 * In replay path, lod_comp is the EA passed by
5469 * client replay buffer, comp_v1 is the pre-recovery
5470 * on-disk EA, we'd sift out those components which
5471 * were init-ed in the on-disk EA.
5473 if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
5478 * this component hasn't instantiated in normal path, or during
5479 * replay it needs replay the instantiation.
5482 /* A released component is being extended */
5483 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
5484 GOTO(out, rc = -EINVAL);
5486 LASSERT(info->lti_comp_idx != NULL);
5487 info->lti_comp_idx[info->lti_count++] = i;
5490 if (info->lti_count == 0)
5493 lod_obj_inc_layout_gen(lo);
5494 rc = lod_declare_instantiate_components(env, lo, th);
5497 lod_object_free_striping(env, lo);
5501 #define lod_foreach_mirror_comp(comp, lo, mirror_idx) \
5502 for (comp = &lo->ldo_comp_entries[lo->ldo_mirrors[mirror_idx].lme_start]; \
5503 comp <= &lo->ldo_comp_entries[lo->ldo_mirrors[mirror_idx].lme_end]; \
5506 static inline int lod_comp_index(struct lod_object *lo,
5507 struct lod_layout_component *lod_comp)
5509 LASSERT(lod_comp >= lo->ldo_comp_entries &&
5510 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
5512 return lod_comp - lo->ldo_comp_entries;
5516 * Stale other mirrors by writing extent.
5518 static void lod_stale_components(struct lod_object *lo, int primary,
5519 struct lu_extent *extent)
5521 struct lod_layout_component *pri_comp, *lod_comp;
5524 /* The writing extent decides which components in the primary
5525 * are affected... */
5526 CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
5527 lod_foreach_mirror_comp(pri_comp, lo, primary) {
5528 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
5531 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
5532 lod_comp_index(lo, pri_comp),
5533 PEXT(&pri_comp->llc_extent));
5535 for (i = 0; i < lo->ldo_mirror_count; i++) {
5539 /* ... and then stale other components that are
5540 * overlapping with primary components */
5541 lod_foreach_mirror_comp(lod_comp, lo, i) {
5542 if (!lu_extent_is_overlapped(
5543 &pri_comp->llc_extent,
5544 &lod_comp->llc_extent))
5547 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
5548 i, lod_comp_index(lo, lod_comp));
5550 lod_comp->llc_flags |= LCME_FL_STALE;
5551 lo->ldo_mirrors[i].lme_stale = 1;
5557 static int lod_declare_update_rdonly(const struct lu_env *env,
5558 struct lod_object *lo, struct md_layout_change *mlc,
5561 struct lod_thread_info *info = lod_env_info(env);
5562 struct lu_attr *layout_attr = &info->lti_layout_attr;
5563 struct lod_layout_component *lod_comp;
5564 struct layout_intent *layout = mlc->mlc_intent;
5565 struct lu_extent extent = layout->li_extent;
5566 unsigned int seq = 0;
5572 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE);
5573 LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
5574 LASSERT(lo->ldo_mirror_count > 0);
5576 CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
5577 PFID(lod_object_fid(lo)), PEXT(&extent));
5579 if (OBD_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
5580 get_random_bytes(&seq, sizeof(seq));
5581 seq %= lo->ldo_mirror_count;
5585 * Pick a mirror as the primary.
5586 * Now it only picks the first mirror that has primary flag set and
5587 * doesn't have any stale components. This algo should be revised
5588 * later after knowing the topology of cluster or the availability of
5591 for (picked = -1, i = 0; i < lo->ldo_mirror_count; i++) {
5592 int index = (i + seq) % lo->ldo_mirror_count;
5594 if (!lo->ldo_mirrors[index].lme_stale) {
5595 if (lo->ldo_mirrors[index].lme_primary) {
5604 if (picked < 0) /* failed to pick a primary */
5607 CDEBUG(D_LAYOUT, DFID": picked mirror %u as primary\n",
5608 PFID(lod_object_fid(lo)), lo->ldo_mirrors[picked].lme_id);
5610 /* stale overlapping components from other mirrors */
5611 lod_stale_components(lo, picked, &extent);
5613 /* instantiate components for the picked mirror, start from 0 */
5614 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
5616 * trunc transfers [size, eof) in the intent extent, we'd
5617 * stale components overlapping [size, eof), while we'd
5618 * instantiated components covers [0, size).
5620 extent.e_end = extent.e_start;
5624 lod_foreach_mirror_comp(lod_comp, lo, picked) {
5625 if (!lu_extent_is_overlapped(&extent,
5626 &lod_comp->llc_extent))
5629 if (lod_comp_inited(lod_comp))
5632 CDEBUG(D_LAYOUT, "instantiate: %u / %u\n",
5633 i, lod_comp_index(lo, lod_comp));
5635 info->lti_comp_idx[info->lti_count++] =
5636 lod_comp_index(lo, lod_comp);
5639 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
5641 /* Reset the layout version once it's becoming too large.
5642 * This way it can make sure that the layout version is
5643 * monotonously increased in this writing era. */
5644 lod_obj_inc_layout_gen(lo);
5645 if (lo->ldo_layout_gen > (LCME_ID_MAX >> 1)) {
5646 __u32 layout_version;
5648 cfs_get_random_bytes(&layout_version, sizeof(layout_version));
5649 lo->ldo_layout_gen = layout_version & 0xffff;
5652 rc = lod_declare_instantiate_components(env, lo, th);
5656 layout_attr->la_valid = LA_LAYOUT_VERSION;
5657 layout_attr->la_layout_version = 0; /* set current version */
5658 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
5664 lod_object_free_striping(env, lo);
5668 static int lod_declare_update_write_pending(const struct lu_env *env,
5669 struct lod_object *lo, struct md_layout_change *mlc,
5672 struct lod_thread_info *info = lod_env_info(env);
5673 struct lu_attr *layout_attr = &info->lti_layout_attr;
5674 struct lod_layout_component *lod_comp;
5675 struct lu_extent extent = { 0 };
5681 LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
5682 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
5683 mlc->mlc_opc == MD_LAYOUT_RESYNC);
5685 /* look for the primary mirror */
5686 for (i = 0; i < lo->ldo_mirror_count; i++) {
5687 if (lo->ldo_mirrors[i].lme_stale)
5690 LASSERTF(primary < 0, DFID " has multiple primary: %u / %u",
5691 PFID(lod_object_fid(lo)),
5692 lo->ldo_mirrors[i].lme_id,
5693 lo->ldo_mirrors[primary].lme_id);
5698 CERROR(DFID ": doesn't have a primary mirror\n",
5699 PFID(lod_object_fid(lo)));
5700 GOTO(out, rc = -ENODATA);
5703 CDEBUG(D_LAYOUT, DFID": found primary %u\n",
5704 PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
5706 LASSERT(!lo->ldo_mirrors[primary].lme_stale);
5708 /* for LAYOUT_WRITE opc, it has to do the following operations:
5709 * 1. stale overlapping componets from stale mirrors;
5710 * 2. instantiate components of the primary mirror;
5711 * 3. transfter layout version to all objects of the primary;
5713 * for LAYOUT_RESYNC opc, it will do:
5714 * 1. instantiate components of all stale mirrors;
5715 * 2. transfer layout version to all objects to close write era. */
5717 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
5718 LASSERT(mlc->mlc_intent != NULL);
5720 extent = mlc->mlc_intent->li_extent;
5722 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
5723 PFID(lod_object_fid(lo)), PEXT(&extent));
5725 /* 1. stale overlapping components */
5726 lod_stale_components(lo, primary, &extent);
5728 /* 2. find out the components need instantiating.
5729 * instantiate [0, mlc->mlc_intent->e_end) */
5730 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
5732 * trunc transfers [size, eof) in the intent extent,
5733 * we'd stale components overlapping [size, eof),
5734 * while we'd instantiated components covers [0, size).
5736 extent.e_end = extent.e_start;
5740 lod_foreach_mirror_comp(lod_comp, lo, primary) {
5741 if (!lu_extent_is_overlapped(&extent,
5742 &lod_comp->llc_extent))
5745 if (lod_comp_inited(lod_comp))
5748 CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
5749 primary, lod_comp_index(lo, lod_comp));
5750 info->lti_comp_idx[info->lti_count++] =
5751 lod_comp_index(lo, lod_comp);
5753 } else { /* MD_LAYOUT_RESYNC */
5754 /* figure out the components that have been instantiated in
5755 * in primary to decide what components should be instantiated
5756 * in stale mirrors */
5757 lod_foreach_mirror_comp(lod_comp, lo, primary) {
5758 if (!lod_comp_inited(lod_comp))
5761 extent.e_end = lod_comp->llc_extent.e_end;
5765 DFID": instantiate all stale components in "DEXT"\n",
5766 PFID(lod_object_fid(lo)), PEXT(&extent));
5768 /* 1. instantiate all components within this extent, even
5769 * non-stale components so that it won't need to instantiate
5770 * those components for mirror truncate later. */
5771 for (i = 0; i < lo->ldo_mirror_count; i++) {
5775 LASSERTF(lo->ldo_mirrors[i].lme_stale,
5776 "both %d and %d are primary\n", i, primary);
5778 lod_foreach_mirror_comp(lod_comp, lo, i) {
5779 if (!lu_extent_is_overlapped(&extent,
5780 &lod_comp->llc_extent))
5783 if (lod_comp_inited(lod_comp))
5786 CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
5787 i, lod_comp_index(lo, lod_comp));
5789 info->lti_comp_idx[info->lti_count++] =
5790 lod_comp_index(lo, lod_comp);
5794 /* change the file state to SYNC_PENDING */
5795 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
5798 rc = lod_declare_instantiate_components(env, lo, th);
5802 /* 3. transfer layout version to OST objects.
5803 * transfer new layout version to OST objects so that stale writes
5804 * can be denied. It also ends an era of writing by setting
5805 * LU_LAYOUT_RESYNC. Normal client can never use this bit to
5806 * send write RPC; only resync RPCs could do it. */
5807 layout_attr->la_valid = LA_LAYOUT_VERSION;
5808 layout_attr->la_layout_version = 0; /* set current version */
5809 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
5810 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
5811 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
5815 lod_obj_inc_layout_gen(lo);
5818 lod_object_free_striping(env, lo);
5822 static int lod_declare_update_sync_pending(const struct lu_env *env,
5823 struct lod_object *lo, struct md_layout_change *mlc,
5826 struct lod_thread_info *info = lod_env_info(env);
5827 unsigned sync_components = 0;
5828 unsigned resync_components = 0;
5833 LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
5834 LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
5835 mlc->mlc_opc == MD_LAYOUT_WRITE);
5837 CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
5838 PFID(lod_object_fid(lo)), mlc->mlc_opc);
5840 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
5841 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
5842 PFID(lod_object_fid(lo)));
5844 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
5845 return lod_declare_update_write_pending(env, lo, mlc, th);
5848 /* MD_LAYOUT_RESYNC_DONE */
5850 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5851 struct lod_layout_component *lod_comp;
5854 lod_comp = &lo->ldo_comp_entries[i];
5856 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
5861 for (j = 0; j < mlc->mlc_resync_count; j++) {
5862 if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
5865 mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
5866 lod_comp->llc_flags &= ~LCME_FL_STALE;
5867 resync_components++;
5873 for (i = 0; i < mlc->mlc_resync_count; i++) {
5874 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
5877 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
5878 "or already synced\n", PFID(lod_object_fid(lo)),
5879 mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
5880 GOTO(out, rc = -EINVAL);
5883 if (!sync_components || !resync_components) {
5884 CDEBUG(D_LAYOUT, DFID": no mirror in sync or resync\n",
5885 PFID(lod_object_fid(lo)));
5887 /* tend to return an error code here to prevent
5888 * the MDT from setting SoM attribute */
5889 GOTO(out, rc = -EINVAL);
5892 CDEBUG(D_LAYOUT, DFID": resynced %u/%zu components\n",
5893 PFID(lod_object_fid(lo)),
5894 resync_components, mlc->mlc_resync_count);
5896 lo->ldo_flr_state = LCM_FL_RDONLY;
5897 lod_obj_inc_layout_gen(lo);
5899 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5900 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
5901 &info->lti_buf, XATTR_NAME_LOV, 0, th);
5906 lod_object_free_striping(env, lo);
5910 static int lod_declare_layout_change(const struct lu_env *env,
5911 struct dt_object *dt, struct md_layout_change *mlc,
5914 struct lod_thread_info *info = lod_env_info(env);
5915 struct lod_object *lo = lod_dt_obj(dt);
5919 if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
5920 dt_object_remote(dt_object_child(dt)))
5923 lod_write_lock(env, dt, 0);
5924 rc = lod_load_striping_locked(env, lo);
5928 LASSERT(lo->ldo_comp_cnt > 0);
5930 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
5934 switch (lo->ldo_flr_state) {
5935 case LCM_FL_NOT_FLR:
5936 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
5940 rc = lod_declare_update_rdonly(env, lo, mlc, th);
5942 case LCM_FL_WRITE_PENDING:
5943 rc = lod_declare_update_write_pending(env, lo, mlc, th);
5945 case LCM_FL_SYNC_PENDING:
5946 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
5953 dt_write_unlock(env, dt);
5958 * Instantiate layout component objects which covers the intent write offset.
5960 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
5961 struct md_layout_change *mlc, struct thandle *th)
5963 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
5964 struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
5965 struct lod_object *lo = lod_dt_obj(dt);
5968 rc = lod_striped_create(env, dt, attr, NULL, th);
5969 if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
5970 layout_attr->la_layout_version |= lo->ldo_layout_gen;
5971 rc = lod_attr_set(env, dt, layout_attr, th);
5977 struct dt_object_operations lod_obj_ops = {
5978 .do_read_lock = lod_read_lock,
5979 .do_write_lock = lod_write_lock,
5980 .do_read_unlock = lod_read_unlock,
5981 .do_write_unlock = lod_write_unlock,
5982 .do_write_locked = lod_write_locked,
5983 .do_attr_get = lod_attr_get,
5984 .do_declare_attr_set = lod_declare_attr_set,
5985 .do_attr_set = lod_attr_set,
5986 .do_xattr_get = lod_xattr_get,
5987 .do_declare_xattr_set = lod_declare_xattr_set,
5988 .do_xattr_set = lod_xattr_set,
5989 .do_declare_xattr_del = lod_declare_xattr_del,
5990 .do_xattr_del = lod_xattr_del,
5991 .do_xattr_list = lod_xattr_list,
5992 .do_ah_init = lod_ah_init,
5993 .do_declare_create = lod_declare_create,
5994 .do_create = lod_create,
5995 .do_declare_destroy = lod_declare_destroy,
5996 .do_destroy = lod_destroy,
5997 .do_index_try = lod_index_try,
5998 .do_declare_ref_add = lod_declare_ref_add,
5999 .do_ref_add = lod_ref_add,
6000 .do_declare_ref_del = lod_declare_ref_del,
6001 .do_ref_del = lod_ref_del,
6002 .do_object_sync = lod_object_sync,
6003 .do_object_lock = lod_object_lock,
6004 .do_object_unlock = lod_object_unlock,
6005 .do_invalidate = lod_invalidate,
6006 .do_declare_layout_change = lod_declare_layout_change,
6007 .do_layout_change = lod_layout_change,
6011 * Implementation of dt_body_operations::dbo_read.
6013 * \see dt_body_operations::dbo_read() in the API description for details.
6015 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
6016 struct lu_buf *buf, loff_t *pos)
6018 struct dt_object *next = dt_object_child(dt);
6020 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
6021 S_ISLNK(dt->do_lu.lo_header->loh_attr));
6022 return next->do_body_ops->dbo_read(env, next, buf, pos);
6026 * Implementation of dt_body_operations::dbo_declare_write.
6028 * \see dt_body_operations::dbo_declare_write() in the API description
6031 static ssize_t lod_declare_write(const struct lu_env *env,
6032 struct dt_object *dt,
6033 const struct lu_buf *buf, loff_t pos,
6036 return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
6040 * Implementation of dt_body_operations::dbo_write.
6042 * \see dt_body_operations::dbo_write() in the API description for details.
6044 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
6045 const struct lu_buf *buf, loff_t *pos,
6046 struct thandle *th, int iq)
6048 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
6049 S_ISLNK(dt->do_lu.lo_header->loh_attr));
6050 return lod_sub_write(env, dt_object_child(dt), buf, pos, th, iq);
6053 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
6054 __u64 start, __u64 end, struct thandle *th)
6056 if (dt_object_remote(dt))
6059 return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
6062 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
6063 __u64 start, __u64 end, struct thandle *th)
6065 if (dt_object_remote(dt))
6068 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
6069 return lod_sub_punch(env, dt_object_child(dt), start, end, th);
6073 * different type of files use the same body_ops because object may be created
6074 * in OUT, where there is no chance to set correct body_ops for each type, so
6075 * body_ops themselves will check file type inside, see lod_read/write/punch for
6078 const struct dt_body_operations lod_body_ops = {
6079 .dbo_read = lod_read,
6080 .dbo_declare_write = lod_declare_write,
6081 .dbo_write = lod_write,
6082 .dbo_declare_punch = lod_declare_punch,
6083 .dbo_punch = lod_punch,
6087 * Implementation of lu_object_operations::loo_object_init.
6089 * The function determines the type and the index of the target device using
6090 * sequence of the object's FID. Then passes control down to the
6091 * corresponding device:
6092 * OSD for the local objects, OSP for remote
6094 * \see lu_object_operations::loo_object_init() in the API description
6097 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
6098 const struct lu_object_conf *conf)
6100 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
6101 struct lu_device *cdev = NULL;
6102 struct lu_object *cobj;
6103 struct lod_tgt_descs *ltd = NULL;
6104 struct lod_tgt_desc *tgt;
6106 int type = LU_SEQ_RANGE_ANY;
6110 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
6112 /* Note: Sometimes, it will Return EAGAIN here, see
6113 * ptrlpc_import_delay_req(), which might confuse
6114 * lu_object_find_at() and make it wait there incorrectly.
6115 * so we convert it to EIO here.*/
6122 if (type == LU_SEQ_RANGE_MDT &&
6123 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
6124 cdev = &lod->lod_child->dd_lu_dev;
6125 } else if (type == LU_SEQ_RANGE_MDT) {
6126 ltd = &lod->lod_mdt_descs;
6128 } else if (type == LU_SEQ_RANGE_OST) {
6129 ltd = &lod->lod_ost_descs;
6136 if (ltd->ltd_tgts_size > idx &&
6137 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
6138 tgt = LTD_TGT(ltd, idx);
6140 LASSERT(tgt != NULL);
6141 LASSERT(tgt->ltd_tgt != NULL);
6143 cdev = &(tgt->ltd_tgt->dd_lu_dev);
6145 lod_putref(lod, ltd);
6148 if (unlikely(cdev == NULL))
6151 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
6152 if (unlikely(cobj == NULL))
6155 lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
6157 lu_object_add(lo, cobj);
6164 * Release resources associated with striping.
6166 * If the object is striped (regular or directory), then release
6167 * the stripe objects references and free the ldo_stripe array.
6169 * \param[in] env execution environment
6170 * \param[in] lo object
6172 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
6174 struct lod_layout_component *lod_comp;
6177 if (lo->ldo_stripe != NULL) {
6178 LASSERT(lo->ldo_comp_entries == NULL);
6179 LASSERT(lo->ldo_dir_stripes_allocated > 0);
6181 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6182 if (lo->ldo_stripe[i])
6183 dt_object_put(env, lo->ldo_stripe[i]);
6186 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
6187 OBD_FREE(lo->ldo_stripe, j);
6188 lo->ldo_stripe = NULL;
6189 lo->ldo_dir_stripes_allocated = 0;
6190 lo->ldo_dir_stripe_loaded = 0;
6191 lo->ldo_dir_stripe_count = 0;
6192 } else if (lo->ldo_comp_entries != NULL) {
6193 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6194 /* free lod_layout_component::llc_stripe array */
6195 lod_comp = &lo->ldo_comp_entries[i];
6197 if (lod_comp->llc_stripe == NULL)
6199 LASSERT(lod_comp->llc_stripes_allocated != 0);
6200 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
6201 if (lod_comp->llc_stripe[j] != NULL)
6203 &lod_comp->llc_stripe[j]->do_lu);
6205 OBD_FREE(lod_comp->llc_stripe,
6206 sizeof(struct dt_object *) *
6207 lod_comp->llc_stripes_allocated);
6208 lod_comp->llc_stripe = NULL;
6209 lod_comp->llc_stripes_allocated = 0;
6211 lod_free_comp_entries(lo);
6212 lo->ldo_comp_cached = 0;
6217 * Implementation of lu_object_operations::loo_object_free.
6219 * \see lu_object_operations::loo_object_free() in the API description
6222 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
6224 struct lod_object *lo = lu2lod_obj(o);
6226 /* release all underlying object pinned */
6227 lod_object_free_striping(env, lo);
6229 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
6233 * Implementation of lu_object_operations::loo_object_release.
6235 * \see lu_object_operations::loo_object_release() in the API description
6238 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
6240 /* XXX: shouldn't we release everything here in case if object
6241 * creation failed before? */
6245 * Implementation of lu_object_operations::loo_object_print.
6247 * \see lu_object_operations::loo_object_print() in the API description
6250 static int lod_object_print(const struct lu_env *env, void *cookie,
6251 lu_printer_t p, const struct lu_object *l)
6253 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
6255 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
6258 struct lu_object_operations lod_lu_obj_ops = {
6259 .loo_object_init = lod_object_init,
6260 .loo_object_free = lod_object_free,
6261 .loo_object_release = lod_object_release,
6262 .loo_object_print = lod_object_print,