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 int lod_obj_for_each_stripe(const struct lu_env *env, struct lod_object *lo,
1053 struct lod_obj_stripe_cb_data *data)
1055 struct lod_layout_component *lod_comp;
1059 LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
1060 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1061 lod_comp = &lo->ldo_comp_entries[i];
1063 if (lod_comp->llc_stripe == NULL)
1066 /* has stripe but not inited yet, this component has been
1067 * declared to be created, but hasn't created yet.
1069 if (!lod_comp_inited(lod_comp))
1072 if (data->locd_comp_skip_cb &&
1073 data->locd_comp_skip_cb(env, lo, i, data))
1076 LASSERT(lod_comp->llc_stripe_count > 0);
1077 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
1078 struct dt_object *dt = lod_comp->llc_stripe[j];
1082 rc = data->locd_stripe_cb(env, lo, dt, th, i, j, data);
1090 static bool lod_obj_attr_set_comp_skip_cb(const struct lu_env *env,
1091 struct lod_object *lo, int comp_idx,
1092 struct lod_obj_stripe_cb_data *data)
1094 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
1095 bool skipped = false;
1097 if (!(data->locd_attr->la_valid & LA_LAYOUT_VERSION))
1100 switch (lo->ldo_flr_state) {
1101 case LCM_FL_WRITE_PENDING: {
1104 /* skip stale components */
1105 if (lod_comp->llc_flags & LCME_FL_STALE) {
1110 /* skip valid and overlapping components, therefore any
1111 * attempts to write overlapped components will never succeed
1112 * because client will get EINPROGRESS. */
1113 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1117 if (lo->ldo_comp_entries[i].llc_flags & LCME_FL_STALE)
1120 if (lu_extent_is_overlapped(&lod_comp->llc_extent,
1121 &lo->ldo_comp_entries[i].llc_extent)) {
1129 LASSERTF(0, "impossible: %d\n", lo->ldo_flr_state);
1130 case LCM_FL_SYNC_PENDING:
1134 CDEBUG(D_LAYOUT, DFID": %s to set component %x to version: %u\n",
1135 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
1136 skipped ? "skipped" : "chose", lod_comp->llc_id,
1137 data->locd_attr->la_layout_version);
1143 lod_obj_stripe_attr_set_cb(const struct lu_env *env, struct lod_object *lo,
1144 struct dt_object *dt, struct thandle *th,
1145 int comp_idx, int stripe_idx,
1146 struct lod_obj_stripe_cb_data *data)
1148 if (data->locd_declare)
1149 return lod_sub_declare_attr_set(env, dt, data->locd_attr, th);
1151 if (data->locd_attr->la_valid & LA_LAYOUT_VERSION) {
1152 CDEBUG(D_LAYOUT, DFID": set layout version: %u, comp_idx: %d\n",
1153 PFID(lu_object_fid(&dt->do_lu)),
1154 data->locd_attr->la_layout_version, comp_idx);
1157 return lod_sub_attr_set(env, dt, data->locd_attr, th);
1161 * Implementation of dt_object_operations::do_declare_attr_set.
1163 * If the object is striped, then apply the changes to all the stripes.
1165 * \see dt_object_operations::do_declare_attr_set() in the API description
1168 static int lod_declare_attr_set(const struct lu_env *env,
1169 struct dt_object *dt,
1170 const struct lu_attr *attr,
1173 struct dt_object *next = dt_object_child(dt);
1174 struct lod_object *lo = lod_dt_obj(dt);
1179 * declare setattr on the local object
1181 rc = lod_sub_declare_attr_set(env, next, attr, th);
1185 /* osp_declare_attr_set() ignores all attributes other than
1186 * UID, GID, PROJID, and size, and osp_attr_set() ignores all
1187 * but UID, GID and PROJID. Declaration of size attr setting
1188 * happens through lod_declare_init_size(), and not through
1189 * this function. Therefore we need not load striping unless
1190 * ownership is changing. This should save memory and (we hope)
1191 * speed up rename().
1193 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1194 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1197 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1200 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID | LA_MODE |
1201 LA_ATIME | LA_MTIME | LA_CTIME |
1206 * load striping information, notice we don't do this when object
1207 * is being initialized as we don't need this information till
1208 * few specific cases like destroy, chown
1210 rc = lod_load_striping(env, lo);
1214 if (!lod_obj_is_striped(dt))
1218 * if object is striped declare changes on the stripes
1220 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1221 LASSERT(lo->ldo_stripe);
1222 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1223 if (lo->ldo_stripe[i] == NULL)
1225 rc = lod_sub_declare_attr_set(env, lo->ldo_stripe[i],
1231 struct lod_obj_stripe_cb_data data = { { 0 } };
1233 data.locd_attr = attr;
1234 data.locd_declare = true;
1235 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1236 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1242 if (!dt_object_exists(next) || dt_object_remote(next) ||
1243 !S_ISREG(attr->la_mode))
1246 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1247 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
1251 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1252 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1253 struct lod_thread_info *info = lod_env_info(env);
1254 struct lu_buf *buf = &info->lti_buf;
1256 buf->lb_buf = info->lti_ea_store;
1257 buf->lb_len = info->lti_ea_store_size;
1258 rc = lod_sub_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1259 LU_XATTR_REPLACE, th);
1266 * Implementation of dt_object_operations::do_attr_set.
1268 * If the object is striped, then apply the changes to all or subset of
1269 * the stripes depending on the object type and specific attributes.
1271 * \see dt_object_operations::do_attr_set() in the API description for details.
1273 static int lod_attr_set(const struct lu_env *env,
1274 struct dt_object *dt,
1275 const struct lu_attr *attr,
1278 struct dt_object *next = dt_object_child(dt);
1279 struct lod_object *lo = lod_dt_obj(dt);
1284 * apply changes to the local object
1286 rc = lod_sub_attr_set(env, next, attr, th);
1290 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1291 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1294 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1297 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE | LA_PROJID |
1298 LA_ATIME | LA_MTIME | LA_CTIME |
1303 /* FIXME: a tricky case in the code path of mdd_layout_change():
1304 * the in-memory striping information has been freed in lod_xattr_set()
1305 * due to layout change. It has to load stripe here again. It only
1306 * changes flags of layout so declare_attr_set() is still accurate */
1307 rc = lod_load_striping_locked(env, lo);
1311 if (!lod_obj_is_striped(dt))
1315 * if object is striped, apply changes to all the stripes
1317 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1318 LASSERT(lo->ldo_stripe);
1319 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1320 if (unlikely(lo->ldo_stripe[i] == NULL))
1323 if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1326 rc = lod_sub_attr_set(env, lo->ldo_stripe[i], attr, th);
1331 struct lod_obj_stripe_cb_data data = { { 0 } };
1333 data.locd_attr = attr;
1334 data.locd_declare = false;
1335 data.locd_comp_skip_cb = lod_obj_attr_set_comp_skip_cb;
1336 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1337 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1343 if (!dt_object_exists(next) || dt_object_remote(next) ||
1344 !S_ISREG(attr->la_mode))
1347 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1348 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
1352 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1353 struct lod_thread_info *info = lod_env_info(env);
1354 struct lu_buf *buf = &info->lti_buf;
1355 struct ost_id *oi = &info->lti_ostid;
1356 struct lu_fid *fid = &info->lti_fid;
1357 struct lov_mds_md_v1 *lmm;
1358 struct lov_ost_data_v1 *objs;
1361 rc = lod_get_lov_ea(env, lo);
1365 buf->lb_buf = info->lti_ea_store;
1366 buf->lb_len = info->lti_ea_store_size;
1367 lmm = info->lti_ea_store;
1368 magic = le32_to_cpu(lmm->lmm_magic);
1369 if (magic == LOV_MAGIC_COMP_V1) {
1370 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1371 struct lov_comp_md_entry_v1 *lcme =
1372 &lcm->lcm_entries[0];
1374 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1375 magic = le32_to_cpu(lmm->lmm_magic);
1378 if (magic == LOV_MAGIC_V1)
1379 objs = &(lmm->lmm_objects[0]);
1381 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1382 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1383 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1385 fid_to_ostid(fid, oi);
1386 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1388 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1389 LU_XATTR_REPLACE, th);
1390 } else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1391 struct lod_thread_info *info = lod_env_info(env);
1392 struct lu_buf *buf = &info->lti_buf;
1393 struct lov_comp_md_v1 *lcm;
1394 struct lov_comp_md_entry_v1 *lcme;
1396 rc = lod_get_lov_ea(env, lo);
1400 buf->lb_buf = info->lti_ea_store;
1401 buf->lb_len = info->lti_ea_store_size;
1403 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
1406 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1407 lcme = &lcm->lcm_entries[0];
1408 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1409 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1411 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1412 LU_XATTR_REPLACE, th);
1419 * Implementation of dt_object_operations::do_xattr_get.
1421 * If LOV EA is requested from the root object and it's not
1422 * found, then return default striping for the filesystem.
1424 * \see dt_object_operations::do_xattr_get() in the API description for details.
1426 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1427 struct lu_buf *buf, const char *name)
1429 struct lod_thread_info *info = lod_env_info(env);
1430 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1435 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1436 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1437 struct lmv_mds_md_v1 *lmv1;
1440 if (rc > (typeof(rc))sizeof(*lmv1))
1443 if (rc < (typeof(rc))sizeof(*lmv1))
1444 RETURN(rc = rc > 0 ? -EINVAL : rc);
1446 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1447 CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1449 info->lti_buf.lb_buf = info->lti_key;
1450 info->lti_buf.lb_len = sizeof(*lmv1);
1451 rc = dt_xattr_get(env, dt_object_child(dt),
1452 &info->lti_buf, name);
1453 if (unlikely(rc != sizeof(*lmv1)))
1454 RETURN(rc = rc > 0 ? -EINVAL : rc);
1456 lmv1 = info->lti_buf.lb_buf;
1457 /* The on-disk LMV EA only contains header, but the
1458 * returned LMV EA size should contain the space for
1459 * the FIDs of all shards of the striped directory. */
1460 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1461 rc = lmv_mds_md_size(
1462 le32_to_cpu(lmv1->lmv_stripe_count),
1465 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1469 RETURN(rc = rc1 != 0 ? rc1 : rc);
1472 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1476 * XXX: Only used by lfsck
1478 * lod returns default striping on the real root of the device
1479 * this is like the root stores default striping for the whole
1480 * filesystem. historically we've been using a different approach
1481 * and store it in the config.
1483 dt_root_get(env, dev->lod_child, &info->lti_fid);
1484 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1486 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1487 struct lov_user_md *lum = buf->lb_buf;
1488 struct lov_desc *desc = &dev->lod_desc;
1490 if (buf->lb_buf == NULL) {
1492 } else if (buf->lb_len >= sizeof(*lum)) {
1493 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1494 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1495 lmm_oi_set_id(&lum->lmm_oi, 0);
1496 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1497 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1498 lum->lmm_stripe_size = cpu_to_le32(
1499 desc->ld_default_stripe_size);
1500 lum->lmm_stripe_count = cpu_to_le16(
1501 desc->ld_default_stripe_count);
1502 lum->lmm_stripe_offset = cpu_to_le16(
1503 desc->ld_default_stripe_offset);
1516 * Checks that the magic of the stripe is sane.
1518 * \param[in] lod lod device
1519 * \param[in] lum a buffer storing LMV EA to verify
1521 * \retval 0 if the EA is sane
1522 * \retval negative otherwise
1524 static int lod_verify_md_striping(struct lod_device *lod,
1525 const struct lmv_user_md_v1 *lum)
1527 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1528 CERROR("%s: invalid lmv_user_md: magic = %x, "
1529 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1530 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1531 (int)le32_to_cpu(lum->lum_stripe_offset),
1532 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1540 * Initialize LMV EA for a slave.
1542 * Initialize slave's LMV EA from the master's LMV EA.
1544 * \param[in] master_lmv a buffer containing master's EA
1545 * \param[out] slave_lmv a buffer where slave's EA will be stored
1548 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1549 const struct lmv_mds_md_v1 *master_lmv)
1551 *slave_lmv = *master_lmv;
1552 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1558 * Generate LMV EA from the object passed as \a dt. The object must have
1559 * the stripes created and initialized.
1561 * \param[in] env execution environment
1562 * \param[in] dt object
1563 * \param[out] lmv_buf buffer storing generated LMV EA
1565 * \retval 0 on success
1566 * \retval negative if failed
1568 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1569 struct lu_buf *lmv_buf)
1571 struct lod_thread_info *info = lod_env_info(env);
1572 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1573 struct lod_object *lo = lod_dt_obj(dt);
1574 struct lmv_mds_md_v1 *lmm1;
1576 int type = LU_SEQ_RANGE_ANY;
1581 LASSERT(lo->ldo_dir_striped != 0);
1582 LASSERT(lo->ldo_dir_stripe_count > 0);
1583 stripe_count = lo->ldo_dir_stripe_count;
1584 /* Only store the LMV EA heahder on the disk. */
1585 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1586 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1590 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1593 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1594 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1595 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1596 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1597 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1602 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1603 lmv_buf->lb_buf = info->lti_ea_store;
1604 lmv_buf->lb_len = sizeof(*lmm1);
1610 * Create in-core represenation for a striped directory.
1612 * Parse the buffer containing LMV EA and instantiate LU objects
1613 * representing the stripe objects. The pointers to the objects are
1614 * stored in ldo_stripe field of \a lo. This function is used when
1615 * we need to access an already created object (i.e. load from a disk).
1617 * \param[in] env execution environment
1618 * \param[in] lo lod object
1619 * \param[in] buf buffer containing LMV EA
1621 * \retval 0 on success
1622 * \retval negative if failed
1624 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1625 const struct lu_buf *buf)
1627 struct lod_thread_info *info = lod_env_info(env);
1628 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1629 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1630 struct dt_object **stripe;
1631 union lmv_mds_md *lmm = buf->lb_buf;
1632 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1633 struct lu_fid *fid = &info->lti_fid;
1638 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1641 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1642 lo->ldo_dir_slave_stripe = 1;
1646 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1649 if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1652 LASSERT(lo->ldo_stripe == NULL);
1653 OBD_ALLOC(stripe, sizeof(stripe[0]) *
1654 (le32_to_cpu(lmv1->lmv_stripe_count)));
1658 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1659 struct dt_device *tgt_dt;
1660 struct dt_object *dto;
1661 int type = LU_SEQ_RANGE_ANY;
1664 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1665 if (!fid_is_sane(fid))
1666 GOTO(out, rc = -ESTALE);
1668 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1672 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1673 tgt_dt = lod->lod_child;
1675 struct lod_tgt_desc *tgt;
1677 tgt = LTD_TGT(ltd, idx);
1679 GOTO(out, rc = -ESTALE);
1680 tgt_dt = tgt->ltd_tgt;
1683 dto = dt_locate_at(env, tgt_dt, fid,
1684 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1687 GOTO(out, rc = PTR_ERR(dto));
1692 lo->ldo_stripe = stripe;
1693 lo->ldo_dir_stripe_count = le32_to_cpu(lmv1->lmv_stripe_count);
1694 lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1696 lod_object_free_striping(env, lo);
1702 * Declare create a striped directory.
1704 * Declare creating a striped directory with a given stripe pattern on the
1705 * specified MDTs. A striped directory is represented as a regular directory
1706 * - an index listing all the stripes. The stripes point back to the master
1707 * object with ".." and LinkEA. The master object gets LMV EA which
1708 * identifies it as a striped directory. The function allocates FIDs
1711 * \param[in] env execution environment
1712 * \param[in] dt object
1713 * \param[in] attr attributes to initialize the objects with
1714 * \param[in] dof type of objects to be created
1715 * \param[in] th transaction handle
1717 * \retval 0 on success
1718 * \retval negative if failed
1720 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1721 struct dt_object *dt,
1722 struct lu_attr *attr,
1723 struct dt_object_format *dof,
1726 struct lod_thread_info *info = lod_env_info(env);
1727 struct lu_buf lmv_buf;
1728 struct lu_buf slave_lmv_buf;
1729 struct lmv_mds_md_v1 *lmm;
1730 struct lmv_mds_md_v1 *slave_lmm = NULL;
1731 struct dt_insert_rec *rec = &info->lti_dt_rec;
1732 struct lod_object *lo = lod_dt_obj(dt);
1737 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1740 lmm = lmv_buf.lb_buf;
1742 OBD_ALLOC_PTR(slave_lmm);
1743 if (slave_lmm == NULL)
1744 GOTO(out, rc = -ENOMEM);
1746 lod_prep_slave_lmv_md(slave_lmm, lmm);
1747 slave_lmv_buf.lb_buf = slave_lmm;
1748 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1750 if (!dt_try_as_dir(env, dt_object_child(dt)))
1751 GOTO(out, rc = -EINVAL);
1753 rec->rec_type = S_IFDIR;
1754 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1755 struct dt_object *dto = lo->ldo_stripe[i];
1756 char *stripe_name = info->lti_key;
1757 struct lu_name *sname;
1758 struct linkea_data ldata = { NULL };
1759 struct lu_buf linkea_buf;
1761 rc = lod_sub_declare_create(env, dto, attr, NULL, dof, th);
1765 if (!dt_try_as_dir(env, dto))
1766 GOTO(out, rc = -EINVAL);
1768 rc = lod_sub_declare_ref_add(env, dto, th);
1772 rec->rec_fid = lu_object_fid(&dto->do_lu);
1773 rc = lod_sub_declare_insert(env, dto,
1774 (const struct dt_rec *)rec,
1775 (const struct dt_key *)dot, th);
1779 /* master stripe FID will be put to .. */
1780 rec->rec_fid = lu_object_fid(&dt->do_lu);
1781 rc = lod_sub_declare_insert(env, dto,
1782 (const struct dt_rec *)rec,
1783 (const struct dt_key *)dotdot, th);
1787 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1788 cfs_fail_val != i) {
1789 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1791 slave_lmm->lmv_master_mdt_index =
1794 slave_lmm->lmv_master_mdt_index =
1796 rc = lod_sub_declare_xattr_set(env, dto, &slave_lmv_buf,
1797 XATTR_NAME_LMV, 0, th);
1802 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1804 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1805 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1807 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1808 PFID(lu_object_fid(&dto->do_lu)), i);
1810 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1811 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1812 sname, lu_object_fid(&dt->do_lu));
1816 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1817 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1818 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
1819 XATTR_NAME_LINK, 0, th);
1823 rec->rec_fid = lu_object_fid(&dto->do_lu);
1824 rc = lod_sub_declare_insert(env, dt_object_child(dt),
1825 (const struct dt_rec *)rec,
1826 (const struct dt_key *)stripe_name,
1831 rc = lod_sub_declare_ref_add(env, dt_object_child(dt), th);
1836 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt),
1837 &lmv_buf, XATTR_NAME_LMV, 0, th);
1841 if (slave_lmm != NULL)
1842 OBD_FREE_PTR(slave_lmm);
1847 static int lod_prep_md_striped_create(const struct lu_env *env,
1848 struct dt_object *dt,
1849 struct lu_attr *attr,
1850 const struct lmv_user_md_v1 *lum,
1851 struct dt_object_format *dof,
1854 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1855 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1856 struct lod_object *lo = lod_dt_obj(dt);
1857 struct dt_object **stripe;
1864 bool is_specific = false;
1867 /* The lum has been verifed in lod_verify_md_striping */
1868 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC ||
1869 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC);
1870 LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1872 stripe_count = le32_to_cpu(lum->lum_stripe_count);
1874 OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1875 if (idx_array == NULL)
1878 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1880 GOTO(out_free, rc = -ENOMEM);
1882 /* Start index must be the master MDT */
1883 master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1884 idx_array[0] = master_index;
1885 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) {
1887 for (i = 1; i < stripe_count; i++)
1888 idx_array[i] = le32_to_cpu(lum->lum_objects[i].lum_mds);
1891 for (i = 0; i < stripe_count; i++) {
1892 struct lod_tgt_desc *tgt = NULL;
1893 struct dt_object *dto;
1894 struct lu_fid fid = { 0 };
1896 struct lu_object_conf conf = { 0 };
1897 struct dt_device *tgt_dt = NULL;
1899 /* Try to find next avaible target */
1901 for (j = 0; j < lod->lod_remote_mdt_count;
1902 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1903 bool already_allocated = false;
1906 CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
1907 idx, lod->lod_remote_mdt_count + 1, i);
1909 if (likely(!is_specific &&
1910 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
1911 /* check whether the idx already exists
1912 * in current allocated array */
1913 for (k = 0; k < i; k++) {
1914 if (idx_array[k] == idx) {
1915 already_allocated = true;
1920 if (already_allocated)
1924 /* Sigh, this index is not in the bitmap, let's check
1925 * next available target */
1926 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) &&
1927 idx != master_index)
1930 if (idx == master_index) {
1931 /* Allocate the FID locally */
1932 rc = obd_fid_alloc(env, lod->lod_child_exp,
1936 tgt_dt = lod->lod_child;
1940 /* check the status of the OSP */
1941 tgt = LTD_TGT(ltd, idx);
1945 tgt_dt = tgt->ltd_tgt;
1946 rc = dt_statfs(env, tgt_dt, NULL);
1948 /* this OSP doesn't feel well */
1953 rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1962 /* Can not allocate more stripes */
1963 if (j == lod->lod_remote_mdt_count) {
1964 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1965 lod2obd(lod)->obd_name, stripe_count, i);
1969 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
1970 idx, i, PFID(&fid));
1972 /* Set the start index for next stripe allocation */
1973 if (!is_specific && i < stripe_count - 1)
1974 idx_array[i + 1] = (idx + 1) %
1975 (lod->lod_remote_mdt_count + 1);
1976 /* tgt_dt and fid must be ready after search avaible OSP
1977 * in the above loop */
1978 LASSERT(tgt_dt != NULL);
1979 LASSERT(fid_is_sane(&fid));
1980 conf.loc_flags = LOC_F_NEW;
1981 dto = dt_locate_at(env, tgt_dt, &fid,
1982 dt->do_lu.lo_dev->ld_site->ls_top_dev,
1985 GOTO(out_put, rc = PTR_ERR(dto));
1989 lo->ldo_dir_stripe_loaded = 1;
1990 lo->ldo_dir_striped = 1;
1991 lo->ldo_stripe = stripe;
1992 lo->ldo_dir_stripe_count = i;
1993 lo->ldo_dir_stripes_allocated = stripe_count;
1995 if (lo->ldo_dir_stripe_count == 0)
1996 GOTO(out_put, rc = -ENOSPC);
1998 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
2004 for (i = 0; i < stripe_count; i++)
2005 if (stripe[i] != NULL)
2006 dt_object_put(env, stripe[i]);
2007 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
2008 lo->ldo_dir_stripe_count = 0;
2009 lo->ldo_dir_stripes_allocated = 0;
2010 lo->ldo_stripe = NULL;
2014 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
2020 * Declare create striped md object.
2022 * The function declares intention to create a striped directory. This is a
2023 * wrapper for lod_prep_md_striped_create(). The only additional functionality
2024 * is to verify pattern \a lum_buf is good. Check that function for the details.
2026 * \param[in] env execution environment
2027 * \param[in] dt object
2028 * \param[in] attr attributes to initialize the objects with
2029 * \param[in] lum_buf a pattern specifying the number of stripes and
2031 * \param[in] dof type of objects to be created
2032 * \param[in] th transaction handle
2034 * \retval 0 on success
2035 * \retval negative if failed
2038 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
2039 struct dt_object *dt,
2040 struct lu_attr *attr,
2041 const struct lu_buf *lum_buf,
2042 struct dt_object_format *dof,
2045 struct lod_object *lo = lod_dt_obj(dt);
2046 struct lmv_user_md_v1 *lum = lum_buf->lb_buf;
2050 LASSERT(lum != NULL);
2052 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
2053 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
2054 (int)le32_to_cpu(lum->lum_stripe_offset));
2056 if (lo->ldo_dir_stripe_count == 0)
2059 /* prepare dir striped objects */
2060 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2062 /* failed to create striping, let's reset
2063 * config so that others don't get confused */
2064 lod_object_free_striping(env, lo);
2072 * Implementation of dt_object_operations::do_declare_xattr_set.
2074 * Used with regular (non-striped) objects. Basically it
2075 * initializes the striping information and applies the
2076 * change to all the stripes.
2078 * \see dt_object_operations::do_declare_xattr_set() in the API description
2081 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2082 struct dt_object *dt,
2083 const struct lu_buf *buf,
2084 const char *name, int fl,
2087 struct dt_object *next = dt_object_child(dt);
2088 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2089 struct lod_object *lo = lod_dt_obj(dt);
2094 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2095 struct lmv_user_md_v1 *lum;
2097 LASSERT(buf != NULL && buf->lb_buf != NULL);
2099 rc = lod_verify_md_striping(d, lum);
2102 } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2103 rc = lod_verify_striping(d, lo, buf, false);
2108 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2112 /* Note: Do not set LinkEA on sub-stripes, otherwise
2113 * it will confuse the fid2path process(see mdt_path_current()).
2114 * The linkEA between master and sub-stripes is set in
2115 * lod_xattr_set_lmv(). */
2116 if (strcmp(name, XATTR_NAME_LINK) == 0)
2119 /* set xattr to each stripes, if needed */
2120 rc = lod_load_striping(env, lo);
2124 if (lo->ldo_dir_stripe_count == 0)
2127 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2128 LASSERT(lo->ldo_stripe[i]);
2130 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2140 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2141 struct lod_object *lo,
2142 struct dt_object *dt, struct thandle *th,
2143 int comp_idx, int stripe_idx,
2144 struct lod_obj_stripe_cb_data *data)
2146 struct lod_thread_info *info = lod_env_info(env);
2147 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
2148 struct filter_fid *ff = &info->lti_ff;
2149 struct lu_buf *buf = &info->lti_buf;
2153 buf->lb_len = sizeof(*ff);
2154 rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2161 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
2162 if (lu_fid_eq(lu_object_fid(&lo->ldo_obj.do_lu), &ff->ff_parent) &&
2163 ff->ff_layout.ol_comp_id == comp->llc_id)
2166 /* rewrite filter_fid */
2167 memset(ff, 0, sizeof(*ff));
2168 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2169 ff->ff_parent.f_ver = stripe_idx;
2170 ff->ff_layout.ol_stripe_size = comp->llc_stripe_size;
2171 ff->ff_layout.ol_stripe_count = comp->llc_stripe_count;
2172 ff->ff_layout.ol_comp_id = comp->llc_id;
2173 ff->ff_layout.ol_comp_start = comp->llc_extent.e_start;
2174 ff->ff_layout.ol_comp_end = comp->llc_extent.e_end;
2175 filter_fid_cpu_to_le(ff, ff, sizeof(*ff));
2177 if (data->locd_declare)
2178 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2179 LU_XATTR_REPLACE, th);
2181 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2182 LU_XATTR_REPLACE, th);
2188 * Reset parent FID on OST object
2190 * Replace parent FID with @dt object FID, which is only called during migration
2191 * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2192 * the FID is changed.
2194 * \param[in] env execution environment
2195 * \param[in] dt dt_object whose stripes's parent FID will be reset
2196 * \parem[in] th thandle
2197 * \param[in] declare if it is declare
2199 * \retval 0 if reset succeeds
2200 * \retval negative errno if reset fails
2202 static int lod_replace_parent_fid(const struct lu_env *env,
2203 struct dt_object *dt,
2204 struct thandle *th, bool declare)
2206 struct lod_object *lo = lod_dt_obj(dt);
2207 struct lod_thread_info *info = lod_env_info(env);
2208 struct lu_buf *buf = &info->lti_buf;
2209 struct filter_fid *ff;
2210 struct lod_obj_stripe_cb_data data = { { 0 } };
2214 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2216 /* set xattr to each stripes, if needed */
2217 rc = lod_load_striping(env, lo);
2221 if (!lod_obj_is_striped(dt))
2224 if (info->lti_ea_store_size < sizeof(*ff)) {
2225 rc = lod_ea_store_resize(info, sizeof(*ff));
2230 buf->lb_buf = info->lti_ea_store;
2231 buf->lb_len = info->lti_ea_store_size;
2233 data.locd_declare = declare;
2234 data.locd_stripe_cb = lod_obj_stripe_replace_parent_fid_cb;
2235 rc = lod_obj_for_each_stripe(env, lo, th, &data);
2240 inline __u16 lod_comp_entry_stripe_count(struct lod_object *lo,
2241 struct lod_layout_component *entry,
2244 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2248 else if (lod_comp_inited(entry))
2249 return entry->llc_stripe_count;
2250 else if ((__u16)-1 == entry->llc_stripe_count)
2251 return lod->lod_desc.ld_tgt_count;
2253 return lod_get_stripe_count(lod, lo, entry->llc_stripe_count);
2256 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2258 int magic, size = 0, i;
2259 struct lod_layout_component *comp_entries;
2264 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2265 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2267 lo->ldo_def_striping->lds_def_striping_is_composite;
2269 comp_cnt = lo->ldo_comp_cnt;
2270 comp_entries = lo->ldo_comp_entries;
2271 is_composite = lo->ldo_is_composite;
2275 LASSERT(comp_cnt != 0 && comp_entries != NULL);
2277 size = sizeof(struct lov_comp_md_v1) +
2278 sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2279 LASSERT(size % sizeof(__u64) == 0);
2282 for (i = 0; i < comp_cnt; i++) {
2285 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2286 stripe_count = lod_comp_entry_stripe_count(lo, &comp_entries[i],
2288 if (!is_dir && is_composite)
2289 lod_comp_shrink_stripe_count(&comp_entries[i],
2292 size += lov_user_md_size(stripe_count, magic);
2293 LASSERT(size % sizeof(__u64) == 0);
2299 * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2300 * the xattr value is binary lov_comp_md_v1 which contains component(s)
2303 * \param[in] env execution environment
2304 * \param[in] dt dt_object to add components on
2305 * \param[in] buf buffer contains components to be added
2306 * \parem[in] th thandle
2308 * \retval 0 on success
2309 * \retval negative errno on failure
2311 static int lod_declare_layout_add(const struct lu_env *env,
2312 struct dt_object *dt,
2313 const struct lu_buf *buf,
2316 struct lod_thread_info *info = lod_env_info(env);
2317 struct lod_layout_component *comp_array, *lod_comp, *old_array;
2318 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2319 struct dt_object *next = dt_object_child(dt);
2320 struct lov_desc *desc = &d->lod_desc;
2321 struct lod_object *lo = lod_dt_obj(dt);
2322 struct lov_user_md_v3 *v3;
2323 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2325 int i, rc, array_cnt, old_array_cnt;
2328 LASSERT(lo->ldo_is_composite);
2330 if (lo->ldo_flr_state != LCM_FL_NOT_FLR)
2333 rc = lod_verify_striping(d, lo, buf, false);
2337 magic = comp_v1->lcm_magic;
2338 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2339 lustre_swab_lov_comp_md_v1(comp_v1);
2340 magic = comp_v1->lcm_magic;
2343 if (magic != LOV_USER_MAGIC_COMP_V1)
2346 array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2347 OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2348 if (comp_array == NULL)
2351 memcpy(comp_array, lo->ldo_comp_entries,
2352 sizeof(*comp_array) * lo->ldo_comp_cnt);
2354 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2355 struct lov_user_md_v1 *v1;
2356 struct lu_extent *ext;
2358 v1 = (struct lov_user_md *)((char *)comp_v1 +
2359 comp_v1->lcm_entries[i].lcme_offset);
2360 ext = &comp_v1->lcm_entries[i].lcme_extent;
2362 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2363 lod_comp->llc_extent.e_start = ext->e_start;
2364 lod_comp->llc_extent.e_end = ext->e_end;
2365 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2366 lod_comp->llc_flags = comp_v1->lcm_entries[i].lcme_flags;
2368 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2369 if (!lod_comp->llc_stripe_count ||
2370 lod_comp->llc_stripe_count == (__u16)-1)
2371 lod_comp->llc_stripe_count =
2372 desc->ld_default_stripe_count;
2373 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2374 if (!lod_comp->llc_stripe_size)
2375 lod_comp->llc_stripe_size =
2376 desc->ld_default_stripe_size;
2378 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2379 v3 = (struct lov_user_md_v3 *) v1;
2380 if (v3->lmm_pool_name[0] != '\0') {
2381 rc = lod_set_pool(&lod_comp->llc_pool,
2389 old_array = lo->ldo_comp_entries;
2390 old_array_cnt = lo->ldo_comp_cnt;
2392 lo->ldo_comp_entries = comp_array;
2393 lo->ldo_comp_cnt = array_cnt;
2395 /* No need to increase layout generation here, it will be increased
2396 * later when generating component ID for the new components */
2398 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2399 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2400 XATTR_NAME_LOV, 0, th);
2402 lo->ldo_comp_entries = old_array;
2403 lo->ldo_comp_cnt = old_array_cnt;
2407 OBD_FREE(old_array, sizeof(*lod_comp) * old_array_cnt);
2409 LASSERT(lo->ldo_mirror_count == 1);
2410 lo->ldo_mirrors[0].lme_end = array_cnt - 1;
2415 for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2416 lod_comp = &comp_array[i];
2417 if (lod_comp->llc_pool != NULL) {
2418 OBD_FREE(lod_comp->llc_pool,
2419 strlen(lod_comp->llc_pool) + 1);
2420 lod_comp->llc_pool = NULL;
2423 OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2428 * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2429 * the '$field' can only be 'flags' now. The xattr value is binary
2430 * lov_comp_md_v1 which contains the component ID(s) and the value of
2431 * the field to be modified.
2433 * \param[in] env execution environment
2434 * \param[in] dt dt_object to be modified
2435 * \param[in] op operation string, like "set.flags"
2436 * \param[in] buf buffer contains components to be set
2437 * \parem[in] th thandle
2439 * \retval 0 on success
2440 * \retval negative errno on failure
2442 static int lod_declare_layout_set(const struct lu_env *env,
2443 struct dt_object *dt,
2444 char *op, const struct lu_buf *buf,
2447 struct lod_layout_component *lod_comp;
2448 struct lod_thread_info *info = lod_env_info(env);
2449 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2450 struct lod_object *lo = lod_dt_obj(dt);
2451 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2454 bool changed = false;
2457 if (strcmp(op, "set.flags") != 0) {
2458 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2459 lod2obd(d)->obd_name, op);
2463 magic = comp_v1->lcm_magic;
2464 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2465 lustre_swab_lov_comp_md_v1(comp_v1);
2466 magic = comp_v1->lcm_magic;
2469 if (magic != LOV_USER_MAGIC_COMP_V1)
2472 if (comp_v1->lcm_entry_count == 0) {
2473 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2474 lod2obd(d)->obd_name);
2478 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2479 id = comp_v1->lcm_entries[i].lcme_id;
2481 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2482 lod_comp = &lo->ldo_comp_entries[j];
2483 if (id == lod_comp->llc_id || id == LCME_ID_ALL) {
2484 lod_comp->llc_flags =
2485 comp_v1->lcm_entries[i].lcme_flags;
2492 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2493 lod2obd(d)->obd_name);
2497 lod_obj_inc_layout_gen(lo);
2499 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2500 rc = lod_sub_declare_xattr_set(env, dt, &info->lti_buf,
2501 XATTR_NAME_LOV, 0, th);
2506 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2507 * and the xattr value is a unique component ID or a special lcme_id.
2509 * \param[in] env execution environment
2510 * \param[in] dt dt_object to be operated on
2511 * \param[in] buf buffer contains component ID or lcme_id
2512 * \parem[in] th thandle
2514 * \retval 0 on success
2515 * \retval negative errno on failure
2517 static int lod_declare_layout_del(const struct lu_env *env,
2518 struct dt_object *dt,
2519 const struct lu_buf *buf,
2522 struct lod_thread_info *info = lod_env_info(env);
2523 struct dt_object *next = dt_object_child(dt);
2524 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2525 struct lod_object *lo = lod_dt_obj(dt);
2526 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2527 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2528 __u32 magic, id, flags, neg_flags = 0;
2532 LASSERT(lo->ldo_is_composite);
2534 if (lo->ldo_flr_state != LCM_FL_NOT_FLR)
2537 magic = comp_v1->lcm_magic;
2538 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2539 lustre_swab_lov_comp_md_v1(comp_v1);
2540 magic = comp_v1->lcm_magic;
2543 if (magic != LOV_USER_MAGIC_COMP_V1)
2546 id = comp_v1->lcm_entries[0].lcme_id;
2547 flags = comp_v1->lcm_entries[0].lcme_flags;
2549 if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
2550 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
2551 lod2obd(d)->obd_name, id, flags);
2555 if (id != LCME_ID_INVAL && flags != 0) {
2556 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
2557 lod2obd(d)->obd_name);
2561 if (flags & LCME_FL_NEG) {
2562 neg_flags = flags & ~LCME_FL_NEG;
2566 left = lo->ldo_comp_cnt;
2570 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
2571 struct lod_layout_component *lod_comp;
2573 lod_comp = &lo->ldo_comp_entries[i];
2575 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
2577 else if (flags && !(flags & lod_comp->llc_flags))
2579 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
2582 if (left != (i + 1)) {
2583 CDEBUG(D_LAYOUT, "%s: this deletion will create "
2584 "a hole.\n", lod2obd(d)->obd_name);
2589 /* Mark the component as deleted */
2590 lod_comp->llc_id = LCME_ID_INVAL;
2592 /* Not instantiated component */
2593 if (lod_comp->llc_stripe == NULL)
2596 LASSERT(lod_comp->llc_stripe_count > 0);
2597 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
2598 struct dt_object *obj = lod_comp->llc_stripe[j];
2602 rc = lod_sub_declare_destroy(env, obj, th);
2608 LASSERTF(left >= 0, "left = %d\n", left);
2609 if (left == lo->ldo_comp_cnt) {
2610 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
2611 lod2obd(d)->obd_name, id);
2615 memset(attr, 0, sizeof(*attr));
2616 attr->la_valid = LA_SIZE;
2617 rc = lod_sub_declare_attr_set(env, next, attr, th);
2622 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2623 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2624 XATTR_NAME_LOV, 0, th);
2626 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
2633 * Declare layout add/set/del operations issued by special xattr names:
2635 * XATTR_LUSTRE_LOV.add add component(s) to existing file
2636 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
2637 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
2639 * \param[in] env execution environment
2640 * \param[in] dt object
2641 * \param[in] name name of xattr
2642 * \param[in] buf lu_buf contains xattr value
2643 * \param[in] th transaction handle
2645 * \retval 0 on success
2646 * \retval negative if failed
2648 static int lod_declare_modify_layout(const struct lu_env *env,
2649 struct dt_object *dt,
2651 const struct lu_buf *buf,
2654 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2655 struct lod_object *lo = lod_dt_obj(dt);
2656 struct dt_object *next = dt_object_child(&lo->ldo_obj);
2658 int rc, len = strlen(XATTR_LUSTRE_LOV);
2661 LASSERT(dt_object_exists(dt));
2663 if (strlen(name) <= len || name[len] != '.') {
2664 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
2665 lod2obd(d)->obd_name, name);
2670 dt_write_lock(env, next, 0);
2671 rc = lod_load_striping_locked(env, lo);
2675 /* the layout to be modified must be a composite layout */
2676 if (!lo->ldo_is_composite) {
2677 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
2678 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
2679 GOTO(unlock, rc = -EINVAL);
2682 op = (char *)name + len;
2683 if (strcmp(op, "add") == 0) {
2684 rc = lod_declare_layout_add(env, dt, buf, th);
2685 } else if (strcmp(op, "del") == 0) {
2686 rc = lod_declare_layout_del(env, dt, buf, th);
2687 } else if (strncmp(op, "set", strlen("set")) == 0) {
2688 rc = lod_declare_layout_set(env, dt, op, buf, th);
2690 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
2691 lod2obd(d)->obd_name, name);
2692 GOTO(unlock, rc = -ENOTSUPP);
2696 lod_object_free_striping(env, lo);
2697 dt_write_unlock(env, next);
2703 * Convert a plain file lov_mds_md to a composite layout.
2705 * \param[in,out] info the thread info::lti_ea_store buffer contains little
2706 * endian plain file layout
2708 * \retval 0 on success, <0 on failure
2710 static int lod_layout_convert(struct lod_thread_info *info)
2712 struct lov_mds_md *lmm = info->lti_ea_store;
2713 struct lov_mds_md *lmm_save;
2714 struct lov_comp_md_v1 *lcm;
2715 struct lov_comp_md_entry_v1 *lcme;
2721 /* realloc buffer to a composite layout which contains one component */
2722 blob_size = lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
2723 le32_to_cpu(lmm->lmm_magic));
2724 size = sizeof(*lcm) + sizeof(*lcme) + blob_size;
2726 OBD_ALLOC_LARGE(lmm_save, blob_size);
2728 GOTO(out, rc = -ENOMEM);
2730 memcpy(lmm_save, lmm, blob_size);
2732 if (info->lti_ea_store_size < size) {
2733 rc = lod_ea_store_resize(info, size);
2738 lcm = info->lti_ea_store;
2739 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
2740 lcm->lcm_size = cpu_to_le32(size);
2741 lcm->lcm_layout_gen = cpu_to_le32(le16_to_cpu(
2742 lmm_save->lmm_layout_gen));
2743 lcm->lcm_flags = cpu_to_le16(LCM_FL_NOT_FLR);
2744 lcm->lcm_entry_count = cpu_to_le16(1);
2745 lcm->lcm_mirror_count = 0;
2747 lcme = &lcm->lcm_entries[0];
2748 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
2749 lcme->lcme_extent.e_start = 0;
2750 lcme->lcme_extent.e_end = cpu_to_le64(OBD_OBJECT_EOF);
2751 lcme->lcme_offset = cpu_to_le32(sizeof(*lcm) + sizeof(*lcme));
2752 lcme->lcme_size = cpu_to_le32(blob_size);
2754 memcpy((char *)lcm + lcme->lcme_offset, (char *)lmm_save, blob_size);
2759 OBD_FREE_LARGE(lmm_save, blob_size);
2764 * Merge layouts to form a mirrored file.
2766 static int lod_declare_layout_merge(const struct lu_env *env,
2767 struct dt_object *dt, const struct lu_buf *mbuf,
2770 struct lod_thread_info *info = lod_env_info(env);
2771 struct lu_buf *buf = &info->lti_buf;
2772 struct lod_object *lo = lod_dt_obj(dt);
2773 struct lov_comp_md_v1 *lcm;
2774 struct lov_comp_md_v1 *cur_lcm;
2775 struct lov_comp_md_v1 *merge_lcm;
2776 struct lov_comp_md_entry_v1 *lcme;
2779 __u16 cur_entry_count;
2780 __u16 merge_entry_count;
2782 __u16 mirror_id = 0;
2787 merge_lcm = mbuf->lb_buf;
2788 if (mbuf->lb_len < sizeof(*merge_lcm))
2791 /* must be an existing layout from disk */
2792 if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
2795 merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
2797 /* do not allow to merge two mirrored files */
2798 if (le16_to_cpu(merge_lcm->lcm_mirror_count))
2801 /* verify the target buffer */
2802 rc = lod_get_lov_ea(env, lo);
2804 RETURN(rc ? : -ENODATA);
2806 cur_lcm = info->lti_ea_store;
2807 switch (le32_to_cpu(cur_lcm->lcm_magic)) {
2810 rc = lod_layout_convert(info);
2812 case LOV_MAGIC_COMP_V1:
2821 /* info->lti_ea_store could be reallocated in lod_layout_convert() */
2822 cur_lcm = info->lti_ea_store;
2823 cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
2825 /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
2826 mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
2827 if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
2830 /* size of new layout */
2831 size = le32_to_cpu(cur_lcm->lcm_size) +
2832 le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
2834 memset(buf, 0, sizeof(*buf));
2835 lu_buf_alloc(buf, size);
2836 if (buf->lb_buf == NULL)
2840 memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
2842 offset = sizeof(*lcm) +
2843 sizeof(*lcme) * (cur_entry_count + merge_entry_count);
2844 for (i = 0; i < cur_entry_count; i++) {
2845 struct lov_comp_md_entry_v1 *cur_lcme;
2847 lcme = &lcm->lcm_entries[i];
2848 cur_lcme = &cur_lcm->lcm_entries[i];
2850 lcme->lcme_offset = cpu_to_le32(offset);
2851 memcpy((char *)lcm + offset,
2852 (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
2853 le32_to_cpu(lcme->lcme_size));
2855 offset += le32_to_cpu(lcme->lcme_size);
2857 if (mirror_count == 1) {
2858 /* new mirrored file, create new mirror ID */
2859 id = pflr_id(1, i + 1);
2860 lcme->lcme_id = cpu_to_le32(id);
2863 id = MAX(le32_to_cpu(lcme->lcme_id), id);
2866 mirror_id = mirror_id_of(id) + 1;
2867 for (i = 0; i < merge_entry_count; i++) {
2868 struct lov_comp_md_entry_v1 *merge_lcme;
2870 merge_lcme = &merge_lcm->lcm_entries[i];
2871 lcme = &lcm->lcm_entries[cur_entry_count + i];
2873 *lcme = *merge_lcme;
2874 lcme->lcme_offset = cpu_to_le32(offset);
2876 id = pflr_id(mirror_id, i + 1);
2877 lcme->lcme_id = cpu_to_le32(id);
2879 memcpy((char *)lcm + offset,
2880 (char *)merge_lcm + le32_to_cpu(merge_lcme->lcme_offset),
2881 le32_to_cpu(lcme->lcme_size));
2883 offset += le32_to_cpu(lcme->lcme_size);
2886 /* fixup layout information */
2887 lod_obj_inc_layout_gen(lo);
2888 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
2889 lcm->lcm_size = cpu_to_le32(size);
2890 lcm->lcm_entry_count = cpu_to_le16(cur_entry_count + merge_entry_count);
2891 lcm->lcm_mirror_count = cpu_to_le16(mirror_count);
2892 if ((le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK) == LCM_FL_NOT_FLR)
2893 lcm->lcm_flags = cpu_to_le32(LCM_FL_RDONLY);
2895 LASSERT(dt_write_locked(env, dt_object_child(dt)));
2896 lod_object_free_striping(env, lo);
2897 rc = lod_parse_striping(env, lo, buf);
2901 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), buf,
2902 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
2910 * Implementation of dt_object_operations::do_declare_xattr_set.
2912 * \see dt_object_operations::do_declare_xattr_set() in the API description
2915 * the extension to the API:
2916 * - declaring LOVEA requests striping creation
2917 * - LU_XATTR_REPLACE means layout swap
2919 static int lod_declare_xattr_set(const struct lu_env *env,
2920 struct dt_object *dt,
2921 const struct lu_buf *buf,
2922 const char *name, int fl,
2925 struct dt_object *next = dt_object_child(dt);
2926 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2931 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2932 if ((S_ISREG(mode) || mode == 0) &&
2933 !(fl & (LU_XATTR_REPLACE | LU_XATTR_MERGE)) &&
2934 (strcmp(name, XATTR_NAME_LOV) == 0 ||
2935 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
2937 * this is a request to create object's striping.
2939 * allow to declare predefined striping on a new (!mode) object
2940 * which is supposed to be replay of regular file creation
2941 * (when LOV setting is declared)
2943 * LU_XATTR_REPLACE is set to indicate a layout swap
2945 if (dt_object_exists(dt)) {
2946 rc = dt_attr_get(env, next, attr);
2950 memset(attr, 0, sizeof(*attr));
2951 attr->la_valid = LA_TYPE | LA_MODE;
2952 attr->la_mode = S_IFREG;
2954 rc = lod_declare_striped_create(env, dt, attr, buf, th);
2955 } else if (fl & LU_XATTR_MERGE) {
2956 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
2957 strcmp(name, XATTR_LUSTRE_LOV) == 0);
2958 rc = lod_declare_layout_merge(env, dt, buf, th);
2959 } else if (S_ISREG(mode) &&
2960 strlen(name) > strlen(XATTR_LUSTRE_LOV) + 1 &&
2961 strncmp(name, XATTR_LUSTRE_LOV,
2962 strlen(XATTR_LUSTRE_LOV)) == 0) {
2964 * this is a request to modify object's striping.
2965 * add/set/del component(s).
2967 if (!dt_object_exists(dt))
2970 rc = lod_declare_modify_layout(env, dt, name, buf, th);
2971 } else if (S_ISDIR(mode)) {
2972 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2973 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
2974 rc = lod_replace_parent_fid(env, dt, th, true);
2976 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2983 * Apply xattr changes to the object.
2985 * Applies xattr changes to the object and the stripes if the latter exist.
2987 * \param[in] env execution environment
2988 * \param[in] dt object
2989 * \param[in] buf buffer pointing to the new value of xattr
2990 * \param[in] name name of xattr
2991 * \param[in] fl flags
2992 * \param[in] th transaction handle
2994 * \retval 0 on success
2995 * \retval negative if failed
2997 static int lod_xattr_set_internal(const struct lu_env *env,
2998 struct dt_object *dt,
2999 const struct lu_buf *buf,
3000 const char *name, int fl,
3003 struct dt_object *next = dt_object_child(dt);
3004 struct lod_object *lo = lod_dt_obj(dt);
3009 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3010 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3013 /* Note: Do not set LinkEA on sub-stripes, otherwise
3014 * it will confuse the fid2path process(see mdt_path_current()).
3015 * The linkEA between master and sub-stripes is set in
3016 * lod_xattr_set_lmv(). */
3017 if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
3020 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3021 LASSERT(lo->ldo_stripe[i]);
3023 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
3033 * Delete an extended attribute.
3035 * Deletes specified xattr from the object and the stripes if the latter exist.
3037 * \param[in] env execution environment
3038 * \param[in] dt object
3039 * \param[in] name name of xattr
3040 * \param[in] th transaction handle
3042 * \retval 0 on success
3043 * \retval negative if failed
3045 static int lod_xattr_del_internal(const struct lu_env *env,
3046 struct dt_object *dt,
3047 const char *name, struct thandle *th)
3049 struct dt_object *next = dt_object_child(dt);
3050 struct lod_object *lo = lod_dt_obj(dt);
3055 rc = lod_sub_xattr_del(env, next, name, th);
3056 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3059 if (lo->ldo_dir_stripe_count == 0)
3062 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3063 LASSERT(lo->ldo_stripe[i]);
3065 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3074 * Set default striping on a directory.
3076 * Sets specified striping on a directory object unless it matches the default
3077 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3078 * EA. This striping will be used when regular file is being created in this
3081 * \param[in] env execution environment
3082 * \param[in] dt the striped object
3083 * \param[in] buf buffer with the striping
3084 * \param[in] name name of EA
3085 * \param[in] fl xattr flag (see OSD API description)
3086 * \param[in] th transaction handle
3088 * \retval 0 on success
3089 * \retval negative if failed
3091 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
3092 struct dt_object *dt,
3093 const struct lu_buf *buf,
3094 const char *name, int fl,
3097 struct lov_user_md_v1 *lum;
3098 struct lov_user_md_v3 *v3 = NULL;
3099 const char *pool_name = NULL;
3104 LASSERT(buf != NULL && buf->lb_buf != NULL);
3107 switch (lum->lmm_magic) {
3108 case LOV_USER_MAGIC_V3:
3110 if (v3->lmm_pool_name[0] != '\0')
3111 pool_name = v3->lmm_pool_name;
3113 case LOV_USER_MAGIC_V1:
3114 /* if { size, offset, count } = { 0, -1, 0 } and no pool
3115 * (i.e. all default values specified) then delete default
3116 * striping from dir. */
3118 "set default striping: sz %u # %u offset %d %s %s\n",
3119 (unsigned)lum->lmm_stripe_size,
3120 (unsigned)lum->lmm_stripe_count,
3121 (int)lum->lmm_stripe_offset,
3122 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
3124 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
3125 lum->lmm_stripe_count,
3126 lum->lmm_stripe_offset,
3129 case LOV_USER_MAGIC_COMP_V1:
3133 CERROR("Invalid magic %x\n", lum->lmm_magic);
3138 rc = lod_xattr_del_internal(env, dt, name, th);
3142 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3149 * Set default striping on a directory object.
3151 * Sets specified striping on a directory object unless it matches the default
3152 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3153 * EA. This striping will be used when a new directory is being created in the
3156 * \param[in] env execution environment
3157 * \param[in] dt the striped object
3158 * \param[in] buf buffer with the striping
3159 * \param[in] name name of EA
3160 * \param[in] fl xattr flag (see OSD API description)
3161 * \param[in] th transaction handle
3163 * \retval 0 on success
3164 * \retval negative if failed
3166 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
3167 struct dt_object *dt,
3168 const struct lu_buf *buf,
3169 const char *name, int fl,
3172 struct lmv_user_md_v1 *lum;
3176 LASSERT(buf != NULL && buf->lb_buf != NULL);
3179 CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
3180 le32_to_cpu(lum->lum_stripe_count),
3181 (int)le32_to_cpu(lum->lum_stripe_offset));
3183 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
3184 le32_to_cpu(lum->lum_stripe_offset)) &&
3185 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
3186 rc = lod_xattr_del_internal(env, dt, name, th);
3190 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3199 * Turn directory into a striped directory.
3201 * During replay the client sends the striping created before MDT
3202 * failure, then the layer above LOD sends this defined striping
3203 * using ->do_xattr_set(), so LOD uses this method to replay creation
3204 * of the stripes. Notice the original information for the striping
3205 * (#stripes, FIDs, etc) was transferred in declare path.
3207 * \param[in] env execution environment
3208 * \param[in] dt the striped object
3209 * \param[in] buf not used currently
3210 * \param[in] name not used currently
3211 * \param[in] fl xattr flag (see OSD API description)
3212 * \param[in] th transaction handle
3214 * \retval 0 on success
3215 * \retval negative if failed
3217 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
3218 const struct lu_buf *buf, const char *name,
3219 int fl, struct thandle *th)
3221 struct lod_object *lo = lod_dt_obj(dt);
3222 struct lod_thread_info *info = lod_env_info(env);
3223 struct lu_attr *attr = &info->lti_attr;
3224 struct dt_object_format *dof = &info->lti_format;
3225 struct lu_buf lmv_buf;
3226 struct lu_buf slave_lmv_buf;
3227 struct lmv_mds_md_v1 *lmm;
3228 struct lmv_mds_md_v1 *slave_lmm = NULL;
3229 struct dt_insert_rec *rec = &info->lti_dt_rec;
3234 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3237 /* The stripes are supposed to be allocated in declare phase,
3238 * if there are no stripes being allocated, it will skip */
3239 if (lo->ldo_dir_stripe_count == 0)
3242 rc = dt_attr_get(env, dt_object_child(dt), attr);
3246 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
3247 LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
3248 dof->dof_type = DFT_DIR;
3250 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
3253 lmm = lmv_buf.lb_buf;
3255 OBD_ALLOC_PTR(slave_lmm);
3256 if (slave_lmm == NULL)
3259 lod_prep_slave_lmv_md(slave_lmm, lmm);
3260 slave_lmv_buf.lb_buf = slave_lmm;
3261 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
3263 rec->rec_type = S_IFDIR;
3264 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3265 struct dt_object *dto;
3266 char *stripe_name = info->lti_key;
3267 struct lu_name *sname;
3268 struct linkea_data ldata = { NULL };
3269 struct lu_buf linkea_buf;
3271 dto = lo->ldo_stripe[i];
3273 dt_write_lock(env, dto, MOR_TGT_CHILD);
3274 rc = lod_sub_create(env, dto, attr, NULL, dof, th);
3276 dt_write_unlock(env, dto);
3280 rc = lod_sub_ref_add(env, dto, th);
3281 dt_write_unlock(env, dto);
3285 rec->rec_fid = lu_object_fid(&dto->do_lu);
3286 rc = lod_sub_insert(env, dto, (const struct dt_rec *)rec,
3287 (const struct dt_key *)dot, th, 0);
3291 rec->rec_fid = lu_object_fid(&dt->do_lu);
3292 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
3293 (const struct dt_key *)dotdot, th, 0);
3297 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
3298 cfs_fail_val != i) {
3299 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
3301 slave_lmm->lmv_master_mdt_index =
3304 slave_lmm->lmv_master_mdt_index =
3307 rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
3308 XATTR_NAME_LMV, fl, th);
3313 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
3315 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3316 PFID(lu_object_fid(&dto->do_lu)), i + 1);
3318 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3319 PFID(lu_object_fid(&dto->do_lu)), i);
3321 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
3322 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
3323 sname, lu_object_fid(&dt->do_lu));
3327 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
3328 linkea_buf.lb_len = ldata.ld_leh->leh_len;
3329 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
3330 XATTR_NAME_LINK, 0, th);
3334 rec->rec_fid = lu_object_fid(&dto->do_lu);
3335 rc = lod_sub_insert(env, dt_object_child(dt),
3336 (const struct dt_rec *)rec,
3337 (const struct dt_key *)stripe_name, th, 0);
3341 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
3346 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
3347 rc = lod_sub_xattr_set(env, dt_object_child(dt),
3348 &lmv_buf, XATTR_NAME_LMV, fl, th);
3350 if (slave_lmm != NULL)
3351 OBD_FREE_PTR(slave_lmm);
3357 * Helper function to declare/execute creation of a striped directory
3359 * Called in declare/create object path, prepare striping for a directory
3360 * and prepare defaults data striping for the objects to be created in
3361 * that directory. Notice the function calls "declaration" or "execution"
3362 * methods depending on \a declare param. This is a consequence of the
3363 * current approach while we don't have natural distributed transactions:
3364 * we basically execute non-local updates in the declare phase. So, the
3365 * arguments for the both phases are the same and this is the reason for
3366 * this function to exist.
3368 * \param[in] env execution environment
3369 * \param[in] dt object
3370 * \param[in] attr attributes the stripes will be created with
3371 * \param[in] lmu lmv_user_md if MDT indices are specified
3372 * \param[in] dof format of stripes (see OSD API description)
3373 * \param[in] th transaction handle
3374 * \param[in] declare where to call "declare" or "execute" methods
3376 * \retval 0 on success
3377 * \retval negative if failed
3379 static int lod_dir_striping_create_internal(const struct lu_env *env,
3380 struct dt_object *dt,
3381 struct lu_attr *attr,
3382 const struct lu_buf *lmu,
3383 struct dt_object_format *dof,
3387 struct lod_thread_info *info = lod_env_info(env);
3388 struct lod_object *lo = lod_dt_obj(dt);
3389 const struct lod_default_striping *lds = lo->ldo_def_striping;
3393 LASSERT(ergo(lds != NULL,
3394 lds->lds_def_striping_set ||
3395 lds->lds_dir_def_striping_set));
3397 if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
3398 lo->ldo_dir_stripe_offset)) {
3400 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3401 int stripe_count = lo->ldo_dir_stripe_count;
3403 if (info->lti_ea_store_size < sizeof(*v1)) {
3404 rc = lod_ea_store_resize(info, sizeof(*v1));
3407 v1 = info->lti_ea_store;
3410 memset(v1, 0, sizeof(*v1));
3411 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3412 v1->lum_stripe_count = cpu_to_le32(stripe_count);
3413 v1->lum_stripe_offset =
3414 cpu_to_le32(lo->ldo_dir_stripe_offset);
3416 info->lti_buf.lb_buf = v1;
3417 info->lti_buf.lb_len = sizeof(*v1);
3418 lmu = &info->lti_buf;
3422 rc = lod_declare_xattr_set_lmv(env, dt, attr, lmu, dof,
3425 rc = lod_xattr_set_lmv(env, dt, lmu, XATTR_NAME_LMV, 0,
3431 /* Transfer default LMV striping from the parent */
3432 if (lds != NULL && lds->lds_dir_def_striping_set &&
3433 !LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
3434 lds->lds_dir_def_stripe_offset)) {
3435 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3437 if (info->lti_ea_store_size < sizeof(*v1)) {
3438 rc = lod_ea_store_resize(info, sizeof(*v1));
3441 v1 = info->lti_ea_store;
3444 memset(v1, 0, sizeof(*v1));
3445 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3446 v1->lum_stripe_count =
3447 cpu_to_le32(lds->lds_dir_def_stripe_count);
3448 v1->lum_stripe_offset =
3449 cpu_to_le32(lds->lds_dir_def_stripe_offset);
3451 cpu_to_le32(lds->lds_dir_def_hash_type);
3453 info->lti_buf.lb_buf = v1;
3454 info->lti_buf.lb_len = sizeof(*v1);
3456 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3457 XATTR_NAME_DEFAULT_LMV,
3460 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
3462 XATTR_NAME_DEFAULT_LMV, 0,
3468 /* Transfer default LOV striping from the parent */
3469 if (lds != NULL && lds->lds_def_striping_set &&
3470 lds->lds_def_comp_cnt != 0) {
3471 struct lov_mds_md *lmm;
3472 int lmm_size = lod_comp_md_size(lo, true);
3474 if (info->lti_ea_store_size < lmm_size) {
3475 rc = lod_ea_store_resize(info, lmm_size);
3479 lmm = info->lti_ea_store;
3481 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
3485 info->lti_buf.lb_buf = lmm;
3486 info->lti_buf.lb_len = lmm_size;
3489 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3490 XATTR_NAME_LOV, 0, th);
3492 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
3493 XATTR_NAME_LOV, 0, th);
3501 static int lod_declare_dir_striping_create(const struct lu_env *env,
3502 struct dt_object *dt,
3503 struct lu_attr *attr,
3505 struct dt_object_format *dof,
3508 return lod_dir_striping_create_internal(env, dt, attr, lmu, dof, th,
3512 static int lod_dir_striping_create(const struct lu_env *env,
3513 struct dt_object *dt,
3514 struct lu_attr *attr,
3515 struct dt_object_format *dof,
3518 return lod_dir_striping_create_internal(env, dt, attr, NULL, dof, th,
3523 * Make LOV EA for striped object.
3525 * Generate striping information and store it in the LOV EA of the given
3526 * object. The caller must ensure nobody else is calling the function
3527 * against the object concurrently. The transaction must be started.
3528 * FLDB service must be running as well; it's used to map FID to the target,
3529 * which is stored in LOV EA.
3531 * \param[in] env execution environment for this thread
3532 * \param[in] lo LOD object
3533 * \param[in] th transaction handle
3535 * \retval 0 if LOV EA is stored successfully
3536 * \retval negative error number on failure
3538 static int lod_generate_and_set_lovea(const struct lu_env *env,
3539 struct lod_object *lo,
3542 struct lod_thread_info *info = lod_env_info(env);
3543 struct dt_object *next = dt_object_child(&lo->ldo_obj);
3544 struct lov_mds_md_v1 *lmm;
3550 if (lo->ldo_comp_cnt == 0) {
3551 lod_object_free_striping(env, lo);
3552 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
3556 lmm_size = lod_comp_md_size(lo, false);
3557 if (info->lti_ea_store_size < lmm_size) {
3558 rc = lod_ea_store_resize(info, lmm_size);
3562 lmm = info->lti_ea_store;
3564 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
3568 info->lti_buf.lb_buf = lmm;
3569 info->lti_buf.lb_len = lmm_size;
3570 rc = lod_sub_xattr_set(env, next, &info->lti_buf,
3571 XATTR_NAME_LOV, 0, th);
3576 * Delete layout component(s)
3578 * \param[in] env execution environment for this thread
3579 * \param[in] dt object
3580 * \param[in] th transaction handle
3582 * \retval 0 on success
3583 * \retval negative error number on failure
3585 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
3588 struct lod_layout_component *lod_comp;
3589 struct lod_object *lo = lod_dt_obj(dt);
3590 struct dt_object *next = dt_object_child(dt);
3591 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3594 LASSERT(lo->ldo_is_composite);
3595 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
3597 left = lo->ldo_comp_cnt;
3598 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3599 lod_comp = &lo->ldo_comp_entries[i];
3601 if (lod_comp->llc_id != LCME_ID_INVAL)
3605 /* Not instantiated component */
3606 if (lod_comp->llc_stripe == NULL)
3609 LASSERT(lod_comp->llc_stripe_count > 0);
3610 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
3611 struct dt_object *obj = lod_comp->llc_stripe[j];
3615 rc = lod_sub_destroy(env, obj, th);
3619 lu_object_put(env, &obj->do_lu);
3620 lod_comp->llc_stripe[j] = NULL;
3622 OBD_FREE(lod_comp->llc_stripe, sizeof(struct dt_object *) *
3623 lod_comp->llc_stripes_allocated);
3624 lod_comp->llc_stripe = NULL;
3625 lod_comp->llc_stripes_allocated = 0;
3626 lod_obj_set_pool(lo, i, NULL);
3627 if (lod_comp->llc_ostlist.op_array) {
3628 OBD_FREE(lod_comp->llc_ostlist.op_array,
3629 lod_comp->llc_ostlist.op_size);
3630 lod_comp->llc_ostlist.op_array = NULL;
3631 lod_comp->llc_ostlist.op_size = 0;
3635 LASSERTF(left >= 0 && left < lo->ldo_comp_cnt, "left = %d\n", left);
3637 struct lod_layout_component *comp_array;
3639 OBD_ALLOC(comp_array, sizeof(*comp_array) * left);
3640 if (comp_array == NULL)
3641 GOTO(out, rc = -ENOMEM);
3643 memcpy(&comp_array[0], &lo->ldo_comp_entries[0],
3644 sizeof(*comp_array) * left);
3646 OBD_FREE(lo->ldo_comp_entries,
3647 sizeof(*comp_array) * lo->ldo_comp_cnt);
3648 lo->ldo_comp_entries = comp_array;
3649 lo->ldo_comp_cnt = left;
3651 LASSERT(lo->ldo_mirror_count == 1);
3652 lo->ldo_mirrors[0].lme_end = left - 1;
3653 lod_obj_inc_layout_gen(lo);
3655 lod_free_comp_entries(lo);
3658 LASSERT(dt_object_exists(dt));
3659 rc = dt_attr_get(env, next, attr);
3663 if (attr->la_size > 0) {
3665 attr->la_valid = LA_SIZE;
3666 rc = lod_sub_attr_set(env, next, attr, th);
3671 rc = lod_generate_and_set_lovea(env, lo, th);
3675 lod_object_free_striping(env, lo);
3680 * Implementation of dt_object_operations::do_xattr_set.
3682 * Sets specified extended attribute on the object. Three types of EAs are
3684 * LOV EA - stores striping for a regular file or default striping (when set
3686 * LMV EA - stores a marker for the striped directories
3687 * DMV EA - stores default directory striping
3689 * When striping is applied to a non-striped existing object (this is called
3690 * late striping), then LOD notices the caller wants to turn the object into a
3691 * striped one. The stripe objects are created and appropriate EA is set:
3692 * LOV EA storing all the stripes directly or LMV EA storing just a small header
3693 * with striping configuration.
3695 * \see dt_object_operations::do_xattr_set() in the API description for details.
3697 static int lod_xattr_set(const struct lu_env *env,
3698 struct dt_object *dt, const struct lu_buf *buf,
3699 const char *name, int fl, struct thandle *th)
3701 struct dt_object *next = dt_object_child(dt);
3705 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3706 strcmp(name, XATTR_NAME_LMV) == 0) {
3707 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
3709 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
3710 LMV_HASH_FLAG_MIGRATION)
3711 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3713 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
3718 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3719 strcmp(name, XATTR_NAME_LOV) == 0) {
3721 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
3723 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3724 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
3726 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
3729 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3730 (!strcmp(name, XATTR_NAME_LOV) ||
3731 !strncmp(name, XATTR_LUSTRE_LOV,
3732 strlen(XATTR_LUSTRE_LOV)))) {
3733 /* in case of lov EA swap, just set it
3734 * if not, it is a replay so check striping match what we
3735 * already have during req replay, declare_xattr_set()
3736 * defines striping, then create() does the work */
3737 if (fl & LU_XATTR_REPLACE) {
3738 /* free stripes, then update disk */
3739 lod_object_free_striping(env, lod_dt_obj(dt));
3741 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3742 } else if (dt_object_remote(dt)) {
3743 /* This only happens during migration, see
3744 * mdd_migrate_create(), in which Master MDT will
3745 * create a remote target object, and only set
3746 * (migrating) stripe EA on the remote object,
3747 * and does not need creating each stripes. */
3748 rc = lod_sub_xattr_set(env, next, buf, name,
3750 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
3751 /* delete component(s) */
3752 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
3753 rc = lod_layout_del(env, dt, th);
3756 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
3757 * it's going to create create file with specified
3758 * component(s), the striping must have not being
3759 * cached in this case;
3761 * Otherwise, it's going to add/change component(s) to
3762 * an existing file, the striping must have been cached
3765 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
3766 !strcmp(name, XATTR_NAME_LOV),
3767 !lod_dt_obj(dt)->ldo_comp_cached));
3769 rc = lod_striped_create(env, dt, NULL, NULL, th);
3772 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3773 rc = lod_replace_parent_fid(env, dt, th, false);
3778 /* then all other xattr */
3779 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3785 * Implementation of dt_object_operations::do_declare_xattr_del.
3787 * \see dt_object_operations::do_declare_xattr_del() in the API description
3790 static int lod_declare_xattr_del(const struct lu_env *env,
3791 struct dt_object *dt, const char *name,
3794 struct lod_object *lo = lod_dt_obj(dt);
3799 rc = lod_sub_declare_xattr_del(env, dt_object_child(dt), name, th);
3803 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3806 /* set xattr to each stripes, if needed */
3807 rc = lod_load_striping(env, lo);
3811 if (lo->ldo_dir_stripe_count == 0)
3814 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3815 LASSERT(lo->ldo_stripe[i]);
3816 rc = lod_sub_declare_xattr_del(env, lo->ldo_stripe[i],
3826 * Implementation of dt_object_operations::do_xattr_del.
3828 * If EA storing a regular striping is being deleted, then release
3829 * all the references to the stripe objects in core.
3831 * \see dt_object_operations::do_xattr_del() in the API description for details.
3833 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
3834 const char *name, struct thandle *th)
3836 struct dt_object *next = dt_object_child(dt);
3837 struct lod_object *lo = lod_dt_obj(dt);
3842 if (!strcmp(name, XATTR_NAME_LOV))
3843 lod_object_free_striping(env, lod_dt_obj(dt));
3845 rc = lod_sub_xattr_del(env, next, name, th);
3846 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3849 if (lo->ldo_dir_stripe_count == 0)
3852 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3853 LASSERT(lo->ldo_stripe[i]);
3855 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3864 * Implementation of dt_object_operations::do_xattr_list.
3866 * \see dt_object_operations::do_xattr_list() in the API description
3869 static int lod_xattr_list(const struct lu_env *env,
3870 struct dt_object *dt, const struct lu_buf *buf)
3872 return dt_xattr_list(env, dt_object_child(dt), buf);
3875 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
3877 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
3882 * Get default striping.
3884 * \param[in] env execution environment
3885 * \param[in] lo object
3886 * \param[out] lds default striping
3888 * \retval 0 on success
3889 * \retval negative if failed
3891 static int lod_get_default_lov_striping(const struct lu_env *env,
3892 struct lod_object *lo,
3893 struct lod_default_striping *lds)
3895 struct lod_thread_info *info = lod_env_info(env);
3896 struct lov_user_md_v1 *v1 = NULL;
3897 struct lov_user_md_v3 *v3 = NULL;
3898 struct lov_comp_md_v1 *comp_v1 = NULL;
3905 lds->lds_def_striping_set = 0;
3907 rc = lod_get_lov_ea(env, lo);
3911 if (rc < (typeof(rc))sizeof(struct lov_user_md))
3914 v1 = info->lti_ea_store;
3915 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
3916 lustre_swab_lov_user_md_v1(v1);
3917 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
3918 v3 = (struct lov_user_md_v3 *)v1;
3919 lustre_swab_lov_user_md_v3(v3);
3920 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3921 comp_v1 = (struct lov_comp_md_v1 *)v1;
3922 lustre_swab_lov_comp_md_v1(comp_v1);
3925 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
3926 v1->lmm_magic != LOV_MAGIC_COMP_V1)
3929 if (v1->lmm_magic == LOV_MAGIC_COMP_V1) {
3930 comp_v1 = (struct lov_comp_md_v1 *)v1;
3931 comp_cnt = comp_v1->lcm_entry_count;
3934 mirror_cnt = comp_v1->lcm_mirror_count + 1;
3942 /* realloc default comp entries if necessary */
3943 rc = lod_def_striping_comp_resize(lds, comp_cnt);
3947 lds->lds_def_comp_cnt = comp_cnt;
3948 lds->lds_def_striping_is_composite = composite;
3949 lds->lds_def_mirror_cnt = mirror_cnt;
3951 for (i = 0; i < comp_cnt; i++) {
3952 struct lod_layout_component *lod_comp;
3953 struct lu_extent *ext;
3956 lod_comp = &lds->lds_def_comp_entries[i];
3958 * reset lod_comp values, llc_stripes is always NULL in
3959 * the default striping template, llc_pool will be reset
3962 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
3965 v1 = (struct lov_user_md *)((char *)comp_v1 +
3966 comp_v1->lcm_entries[i].lcme_offset);
3967 ext = &comp_v1->lcm_entries[i].lcme_extent;
3968 lod_comp->llc_extent = *ext;
3971 if (v1->lmm_pattern != LOV_PATTERN_RAID0 &&
3972 v1->lmm_pattern != LOV_PATTERN_MDT &&
3973 v1->lmm_pattern != 0) {
3974 lod_free_def_comp_entries(lds);
3978 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d "
3979 "stripe_offset=%d\n",
3980 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
3981 (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
3982 (int)v1->lmm_stripe_offset);
3984 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
3985 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
3986 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
3987 lod_comp->llc_pattern = v1->lmm_pattern;
3990 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
3991 /* XXX: sanity check here */
3992 v3 = (struct lov_user_md_v3 *) v1;
3993 if (v3->lmm_pool_name[0] != '\0')
3994 pool = v3->lmm_pool_name;
3996 lod_set_def_pool(lds, i, pool);
3999 lds->lds_def_striping_set = 1;
4004 * Get default directory striping.
4006 * \param[in] env execution environment
4007 * \param[in] lo object
4008 * \param[out] lds default striping
4010 * \retval 0 on success
4011 * \retval negative if failed
4013 static int lod_get_default_lmv_striping(const struct lu_env *env,
4014 struct lod_object *lo,
4015 struct lod_default_striping *lds)
4017 struct lod_thread_info *info = lod_env_info(env);
4018 struct lmv_user_md_v1 *v1 = NULL;
4022 lds->lds_dir_def_striping_set = 0;
4023 rc = lod_get_default_lmv_ea(env, lo);
4027 if (rc < (typeof(rc))sizeof(struct lmv_user_md))
4030 v1 = info->lti_ea_store;
4032 lds->lds_dir_def_stripe_count = le32_to_cpu(v1->lum_stripe_count);
4033 lds->lds_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
4034 lds->lds_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
4035 lds->lds_dir_def_striping_set = 1;
4041 * Get default striping in the object.
4043 * Get object default striping and default directory striping.
4045 * \param[in] env execution environment
4046 * \param[in] lo object
4047 * \param[out] lds default striping
4049 * \retval 0 on success
4050 * \retval negative if failed
4052 static int lod_get_default_striping(const struct lu_env *env,
4053 struct lod_object *lo,
4054 struct lod_default_striping *lds)
4058 rc = lod_get_default_lov_striping(env, lo, lds);
4059 rc1 = lod_get_default_lmv_striping(env, lo, lds);
4060 if (rc == 0 && rc1 < 0)
4067 * Apply default striping on object.
4069 * If object striping pattern is not set, set to the one in default striping.
4070 * The default striping is from parent or fs.
4072 * \param[in] lo new object
4073 * \param[in] lds default striping
4074 * \param[in] mode new object's mode
4076 static void lod_striping_from_default(struct lod_object *lo,
4077 const struct lod_default_striping *lds,
4080 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4081 struct lov_desc *desc = &d->lod_desc;
4084 if (lds->lds_def_striping_set && S_ISREG(mode)) {
4085 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
4086 lds->lds_def_comp_cnt);
4090 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
4091 if (lds->lds_def_mirror_cnt > 1)
4092 lo->ldo_flr_state = LCM_FL_RDONLY;
4094 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4095 struct lod_layout_component *obj_comp =
4096 &lo->ldo_comp_entries[i];
4097 struct lod_layout_component *def_comp =
4098 &lds->lds_def_comp_entries[i];
4100 CDEBUG(D_LAYOUT, "Inherite from default: size:%hu "
4101 "nr:%u offset:%u pattern %#x %s\n",
4102 def_comp->llc_stripe_size,
4103 def_comp->llc_stripe_count,
4104 def_comp->llc_stripe_offset,
4105 def_comp->llc_pattern,
4106 def_comp->llc_pool ?: "");
4108 *obj_comp = *def_comp;
4109 if (def_comp->llc_pool != NULL) {
4110 /* pointer was copied from def_comp */
4111 obj_comp->llc_pool = NULL;
4112 lod_obj_set_pool(lo, i, def_comp->llc_pool);
4116 * Don't initialize these fields for plain layout
4117 * (v1/v3) here, they are inherited in the order of
4118 * 'parent' -> 'fs default (root)' -> 'global default
4119 * values for stripe_count & stripe_size'.
4121 * see lod_ah_init().
4123 if (!lo->ldo_is_composite)
4126 if (obj_comp->llc_stripe_count <= 0 &&
4127 obj_comp->llc_pattern != LOV_PATTERN_MDT)
4128 obj_comp->llc_stripe_count =
4129 desc->ld_default_stripe_count;
4130 if (obj_comp->llc_stripe_size <= 0)
4131 obj_comp->llc_stripe_size =
4132 desc->ld_default_stripe_size;
4134 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
4135 if (lo->ldo_dir_stripe_count == 0)
4136 lo->ldo_dir_stripe_count =
4137 lds->lds_dir_def_stripe_count;
4138 if (lo->ldo_dir_stripe_offset == -1)
4139 lo->ldo_dir_stripe_offset =
4140 lds->lds_dir_def_stripe_offset;
4141 if (lo->ldo_dir_hash_type == 0)
4142 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
4144 CDEBUG(D_LAYOUT, "striping from default dir: count:%hu, "
4145 "offset:%u, hash_type:%u\n",
4146 lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
4147 lo->ldo_dir_hash_type);
4151 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root)
4153 struct lod_layout_component *lod_comp;
4155 if (lo->ldo_comp_cnt == 0)
4158 if (lo->ldo_is_composite)
4161 lod_comp = &lo->ldo_comp_entries[0];
4163 if (lod_comp->llc_stripe_count <= 0 ||
4164 lod_comp->llc_stripe_size <= 0)
4167 if (from_root && (lod_comp->llc_pool == NULL ||
4168 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
4175 * Implementation of dt_object_operations::do_ah_init.
4177 * This method is used to make a decision on the striping configuration for the
4178 * object being created. It can be taken from the \a parent object if it exists,
4179 * or filesystem's default. The resulting configuration (number of stripes,
4180 * stripe size/offset, pool name, etc) is stored in the object itself and will
4181 * be used by the methods like ->doo_declare_create().
4183 * \see dt_object_operations::do_ah_init() in the API description for details.
4185 static void lod_ah_init(const struct lu_env *env,
4186 struct dt_allocation_hint *ah,
4187 struct dt_object *parent,
4188 struct dt_object *child,
4191 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
4192 struct lod_thread_info *info = lod_env_info(env);
4193 struct lod_default_striping *lds = &info->lti_def_striping;
4194 struct dt_object *nextp = NULL;
4195 struct dt_object *nextc;
4196 struct lod_object *lp = NULL;
4197 struct lod_object *lc;
4198 struct lov_desc *desc;
4199 struct lod_layout_component *lod_comp;
4205 if (likely(parent)) {
4206 nextp = dt_object_child(parent);
4207 lp = lod_dt_obj(parent);
4210 nextc = dt_object_child(child);
4211 lc = lod_dt_obj(child);
4213 LASSERT(!lod_obj_is_striped(child));
4214 /* default layout template may have been set on the regular file
4215 * when this is called from mdd_create_data() */
4216 if (S_ISREG(child_mode))
4217 lod_free_comp_entries(lc);
4219 if (!dt_object_exists(nextc))
4220 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
4222 if (S_ISDIR(child_mode)) {
4223 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
4225 /* other default values are 0 */
4226 lc->ldo_dir_stripe_offset = -1;
4228 /* get default striping from parent object */
4229 if (likely(lp != NULL))
4230 lod_get_default_striping(env, lp, lds);
4232 /* set child default striping info, default value is NULL */
4233 if (lds->lds_def_striping_set || lds->lds_dir_def_striping_set)
4234 lc->ldo_def_striping = lds;
4236 /* It should always honour the specified stripes */
4237 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
4238 * will have old magic. In this case, we should ignore the
4239 * stripe count and try to create dir by default stripe.
4241 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
4242 (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
4243 le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
4244 lc->ldo_dir_stripe_count =
4245 le32_to_cpu(lum1->lum_stripe_count);
4246 lc->ldo_dir_stripe_offset =
4247 le32_to_cpu(lum1->lum_stripe_offset);
4248 lc->ldo_dir_hash_type =
4249 le32_to_cpu(lum1->lum_hash_type);
4251 "set dirstripe: count %hu, offset %d, hash %u\n",
4252 lc->ldo_dir_stripe_count,
4253 (int)lc->ldo_dir_stripe_offset,
4254 lc->ldo_dir_hash_type);
4256 /* transfer defaults LMV to new directory */
4257 lod_striping_from_default(lc, lds, child_mode);
4260 /* shrink the stripe_count to the avaible MDT count */
4261 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
4262 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))
4263 lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
4265 /* Directory will be striped only if stripe_count > 1, if
4266 * stripe_count == 1, let's reset stripe_count = 0 to avoid
4267 * create single master stripe and also help to unify the
4268 * stripe handling of directories and files */
4269 if (lc->ldo_dir_stripe_count == 1)
4270 lc->ldo_dir_stripe_count = 0;
4272 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
4273 lc->ldo_dir_stripe_count,
4274 (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
4279 /* child object regular file*/
4281 if (!lod_object_will_be_striped(S_ISREG(child_mode),
4282 lu_object_fid(&child->do_lu)))
4285 /* If object is going to be striped over OSTs, transfer default
4286 * striping information to the child, so that we can use it
4287 * during declaration and creation.
4289 * Try from the parent first.
4291 if (likely(lp != NULL)) {
4292 rc = lod_get_default_lov_striping(env, lp, lds);
4294 lod_striping_from_default(lc, lds, child_mode);
4297 /* Initialize lod_device::lod_md_root object reference */
4298 if (d->lod_md_root == NULL) {
4299 struct dt_object *root;
4300 struct lod_object *lroot;
4302 lu_root_fid(&info->lti_fid);
4303 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
4304 if (!IS_ERR(root)) {
4305 lroot = lod_dt_obj(root);
4307 spin_lock(&d->lod_lock);
4308 if (d->lod_md_root != NULL)
4309 dt_object_put(env, &d->lod_md_root->ldo_obj);
4310 d->lod_md_root = lroot;
4311 spin_unlock(&d->lod_lock);
4315 /* try inherit layout from the root object (fs default) when:
4316 * - parent does not have default layout; or
4317 * - parent has plain(v1/v3) default layout, and some attributes
4318 * are not specified in the default layout;
4320 if (d->lod_md_root != NULL && lod_need_inherit_more(lc, true)) {
4321 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds);
4324 if (lc->ldo_comp_cnt == 0) {
4325 lod_striping_from_default(lc, lds, child_mode);
4326 } else if (!lds->lds_def_striping_is_composite) {
4327 struct lod_layout_component *def_comp;
4329 LASSERT(!lc->ldo_is_composite);
4330 lod_comp = &lc->ldo_comp_entries[0];
4331 def_comp = &lds->lds_def_comp_entries[0];
4333 if (lod_comp->llc_stripe_count <= 0)
4334 lod_comp->llc_stripe_count =
4335 def_comp->llc_stripe_count;
4336 if (lod_comp->llc_stripe_size <= 0)
4337 lod_comp->llc_stripe_size =
4338 def_comp->llc_stripe_size;
4339 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
4340 lod_comp->llc_stripe_offset =
4341 def_comp->llc_stripe_offset;
4342 if (lod_comp->llc_pool == NULL)
4343 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
4348 * fs default striping may not be explicitly set, or historically set
4349 * in config log, use them.
4351 if (lod_need_inherit_more(lc, false)) {
4352 if (lc->ldo_comp_cnt == 0) {
4353 rc = lod_alloc_comp_entries(lc, 0, 1);
4355 /* fail to allocate memory, will create a
4356 * non-striped file. */
4358 lc->ldo_is_composite = 0;
4359 lod_comp = &lc->ldo_comp_entries[0];
4360 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4362 LASSERT(!lc->ldo_is_composite);
4363 lod_comp = &lc->ldo_comp_entries[0];
4364 desc = &d->lod_desc;
4365 if (lod_comp->llc_stripe_count <= 0)
4366 lod_comp->llc_stripe_count =
4367 desc->ld_default_stripe_count;
4368 if (lod_comp->llc_stripe_size <= 0)
4369 lod_comp->llc_stripe_size =
4370 desc->ld_default_stripe_size;
4376 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
4378 * Size initialization on late striping.
4380 * Propagate the size of a truncated object to a deferred striping.
4381 * This function handles a special case when truncate was done on a
4382 * non-striped object and now while the striping is being created
4383 * we can't lose that size, so we have to propagate it to the stripes
4386 * \param[in] env execution environment
4387 * \param[in] dt object
4388 * \param[in] th transaction handle
4390 * \retval 0 on success
4391 * \retval negative if failed
4393 static int lod_declare_init_size(const struct lu_env *env,
4394 struct dt_object *dt, struct thandle *th)
4396 struct dt_object *next = dt_object_child(dt);
4397 struct lod_object *lo = lod_dt_obj(dt);
4398 struct dt_object **objects = NULL;
4399 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4400 uint64_t size, offs;
4401 int i, rc, stripe, stripe_count = 0, stripe_size = 0;
4402 struct lu_extent size_ext;
4405 if (!lod_obj_is_striped(dt))
4408 rc = dt_attr_get(env, next, attr);
4409 LASSERT(attr->la_valid & LA_SIZE);
4413 size = attr->la_size;
4417 size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
4418 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4419 struct lod_layout_component *lod_comp;
4420 struct lu_extent *extent;
4422 lod_comp = &lo->ldo_comp_entries[i];
4424 if (lod_comp->llc_stripe == NULL)
4427 extent = &lod_comp->llc_extent;
4428 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
4429 if (!lo->ldo_is_composite ||
4430 lu_extent_is_overlapped(extent, &size_ext)) {
4431 objects = lod_comp->llc_stripe;
4432 stripe_count = lod_comp->llc_stripe_count;
4433 stripe_size = lod_comp->llc_stripe_size;
4436 if (stripe_count == 0)
4439 LASSERT(objects != NULL && stripe_size != 0);
4440 /* ll_do_div64(a, b) returns a % b, and a = a / b */
4441 ll_do_div64(size, (__u64)stripe_size);
4442 stripe = ll_do_div64(size, (__u64)stripe_count);
4443 LASSERT(objects[stripe] != NULL);
4445 size = size * stripe_size;
4446 offs = attr->la_size;
4447 size += ll_do_div64(offs, stripe_size);
4449 attr->la_valid = LA_SIZE;
4450 attr->la_size = size;
4452 rc = lod_sub_declare_attr_set(env, objects[stripe],
4461 * Declare creation of striped object.
4463 * The function declares creation stripes for a regular object. The function
4464 * also declares whether the stripes will be created with non-zero size if
4465 * previously size was set non-zero on the master object. If object \a dt is
4466 * not local, then only fully defined striping can be applied in \a lovea.
4467 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
4470 * \param[in] env execution environment
4471 * \param[in] dt object
4472 * \param[in] attr attributes the stripes will be created with
4473 * \param[in] lovea a buffer containing striping description
4474 * \param[in] th transaction handle
4476 * \retval 0 on success
4477 * \retval negative if failed
4479 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
4480 struct lu_attr *attr,
4481 const struct lu_buf *lovea, struct thandle *th)
4483 struct lod_thread_info *info = lod_env_info(env);
4484 struct dt_object *next = dt_object_child(dt);
4485 struct lod_object *lo = lod_dt_obj(dt);
4489 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
4490 GOTO(out, rc = -ENOMEM);
4492 if (!dt_object_remote(next)) {
4493 /* choose OST and generate appropriate objects */
4494 rc = lod_prepare_create(env, lo, attr, lovea, th);
4499 * declare storage for striping data
4501 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
4503 /* LOD can not choose OST objects for remote objects, i.e.
4504 * stripes must be ready before that. Right now, it can only
4505 * happen during migrate, i.e. migrate process needs to create
4506 * remote regular file (mdd_migrate_create), then the migrate
4507 * process will provide stripeEA. */
4508 LASSERT(lovea != NULL);
4509 info->lti_buf = *lovea;
4512 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
4513 XATTR_NAME_LOV, 0, th);
4518 * if striping is created with local object's size > 0,
4519 * we have to propagate this size to specific object
4520 * the case is possible only when local object was created previously
4522 if (dt_object_exists(next))
4523 rc = lod_declare_init_size(env, dt, th);
4526 /* failed to create striping or to set initial size, let's reset
4527 * config so that others don't get confused */
4529 lod_object_free_striping(env, lo);
4535 * Implementation of dt_object_operations::do_declare_create.
4537 * The method declares creation of a new object. If the object will be striped,
4538 * then helper functions are called to find FIDs for the stripes, declare
4539 * creation of the stripes and declare initialization of the striping
4540 * information to be stored in the master object.
4542 * \see dt_object_operations::do_declare_create() in the API description
4545 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
4546 struct lu_attr *attr,
4547 struct dt_allocation_hint *hint,
4548 struct dt_object_format *dof, struct thandle *th)
4550 struct dt_object *next = dt_object_child(dt);
4551 struct lod_object *lo = lod_dt_obj(dt);
4560 * first of all, we declare creation of local object
4562 rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
4567 * it's lod_ah_init() that has decided the object will be striped
4569 if (dof->dof_type == DFT_REGULAR) {
4570 /* callers don't want stripes */
4571 /* XXX: all tricky interactions with ->ah_make_hint() decided
4572 * to use striping, then ->declare_create() behaving differently
4573 * should be cleaned */
4574 if (dof->u.dof_reg.striped != 0)
4575 rc = lod_declare_striped_create(env, dt, attr,
4577 } else if (dof->dof_type == DFT_DIR) {
4578 struct seq_server_site *ss;
4579 struct lu_buf buf = { NULL };
4580 struct lu_buf *lmu = NULL;
4582 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
4584 /* If the parent has default stripeEA, and client
4585 * did not find it before sending create request,
4586 * then MDT will return -EREMOTE, and client will
4587 * retrieve the default stripeEA and re-create the
4590 * Note: if dah_eadata != NULL, it means creating the
4591 * striped directory with specified stripeEA, then it
4592 * should ignore the default stripeEA */
4593 if (hint != NULL && hint->dah_eadata == NULL) {
4594 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
4595 GOTO(out, rc = -EREMOTE);
4597 if (lo->ldo_dir_stripe_offset == -1) {
4598 /* child and parent should be in the same MDT */
4599 if (hint->dah_parent != NULL &&
4600 dt_object_remote(hint->dah_parent))
4601 GOTO(out, rc = -EREMOTE);
4602 } else if (lo->ldo_dir_stripe_offset !=
4604 struct lod_device *lod;
4605 struct lod_tgt_descs *ltd;
4606 struct lod_tgt_desc *tgt = NULL;
4607 bool found_mdt = false;
4610 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4611 ltd = &lod->lod_mdt_descs;
4612 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
4613 tgt = LTD_TGT(ltd, i);
4614 if (tgt->ltd_index ==
4615 lo->ldo_dir_stripe_offset) {
4621 /* If the MDT indicated by stripe_offset can be
4622 * found, then tell client to resend the create
4623 * request to the correct MDT, otherwise return
4624 * error to client */
4626 GOTO(out, rc = -EREMOTE);
4628 GOTO(out, rc = -EINVAL);
4630 } else if (hint && hint->dah_eadata) {
4632 lmu->lb_buf = (void *)hint->dah_eadata;
4633 lmu->lb_len = hint->dah_eadata_len;
4636 rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
4640 /* failed to create striping or to set initial size, let's reset
4641 * config so that others don't get confused */
4643 lod_object_free_striping(env, lo);
4648 * Generate component ID for new created component.
4650 * \param[in] lo LOD object
4651 * \param[in] comp_idx index of ldo_comp_entries
4653 * \retval component ID on success
4654 * \retval LCME_ID_INVAL on failure
4656 static __u32 lod_gen_component_id(struct lod_object *lo,
4657 int mirror_id, int comp_idx)
4659 struct lod_layout_component *lod_comp;
4660 __u32 id, start, end;
4663 LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
4665 lod_obj_inc_layout_gen(lo);
4666 id = lo->ldo_layout_gen;
4667 if (likely(id <= SEQ_ID_MAX))
4668 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
4670 /* Layout generation wraps, need to check collisions. */
4671 start = id & SEQ_ID_MASK;
4674 for (id = start; id <= end; id++) {
4675 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4676 lod_comp = &lo->ldo_comp_entries[i];
4677 if (pflr_id(mirror_id, id) == lod_comp->llc_id)
4680 /* Found the ununsed ID */
4681 if (i == lo->ldo_comp_cnt)
4682 RETURN(pflr_id(mirror_id, id));
4684 if (end == LCME_ID_MAX) {
4686 end = min(lo->ldo_layout_gen & LCME_ID_MASK,
4687 (__u32)(LCME_ID_MAX - 1));
4691 RETURN(LCME_ID_INVAL);
4695 * Creation of a striped regular object.
4697 * The function is called to create the stripe objects for a regular
4698 * striped file. This can happen at the initial object creation or
4699 * when the caller asks LOD to do so using ->do_xattr_set() method
4700 * (so called late striping). Notice all the information are already
4701 * prepared in the form of the list of objects (ldo_stripe field).
4702 * This is done during declare phase.
4704 * \param[in] env execution environment
4705 * \param[in] dt object
4706 * \param[in] attr attributes the stripes will be created with
4707 * \param[in] dof format of stripes (see OSD API description)
4708 * \param[in] th transaction handle
4710 * \retval 0 on success
4711 * \retval negative if failed
4713 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
4714 struct lu_attr *attr, struct dt_object_format *dof,
4717 struct lod_layout_component *lod_comp;
4718 struct lod_object *lo = lod_dt_obj(dt);
4723 LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
4725 mirror_id = lo->ldo_mirror_count > 1 ? 1 : 0;
4727 /* create all underlying objects */
4728 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4729 lod_comp = &lo->ldo_comp_entries[i];
4731 if (lod_comp->llc_extent.e_start == 0 && i > 0) /* new mirror */
4734 if (lod_comp->llc_id == LCME_ID_INVAL) {
4735 lod_comp->llc_id = lod_gen_component_id(lo,
4737 if (lod_comp->llc_id == LCME_ID_INVAL)
4738 GOTO(out, rc = -ERANGE);
4741 if (lod_comp_inited(lod_comp))
4744 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
4745 lod_comp_set_init(lod_comp);
4747 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
4748 lod_comp_set_init(lod_comp);
4750 if (lod_comp->llc_stripe == NULL)
4753 LASSERT(lod_comp->llc_stripe_count);
4754 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4755 struct dt_object *object = lod_comp->llc_stripe[j];
4756 LASSERT(object != NULL);
4757 rc = lod_sub_create(env, object, attr, NULL, dof, th);
4761 lod_comp_set_init(lod_comp);
4764 rc = lod_fill_mirrors(lo);
4768 rc = lod_generate_and_set_lovea(env, lo, th);
4772 lo->ldo_comp_cached = 1;
4776 lod_object_free_striping(env, lo);
4781 * Implementation of dt_object_operations::do_create.
4783 * If any of preceeding methods (like ->do_declare_create(),
4784 * ->do_ah_init(), etc) chose to create a striped object,
4785 * then this method will create the master and the stripes.
4787 * \see dt_object_operations::do_create() in the API description for details.
4789 static int lod_create(const struct lu_env *env, struct dt_object *dt,
4790 struct lu_attr *attr, struct dt_allocation_hint *hint,
4791 struct dt_object_format *dof, struct thandle *th)
4796 /* create local object */
4797 rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
4801 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4802 lod_obj_is_striped(dt) && dof->u.dof_reg.striped != 0) {
4803 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
4804 rc = lod_striped_create(env, dt, attr, dof, th);
4811 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
4812 struct dt_object *dt, struct thandle *th,
4813 int comp_idx, int stripe_idx,
4814 struct lod_obj_stripe_cb_data *data)
4816 if (data->locd_declare)
4817 return lod_sub_declare_destroy(env, dt, th);
4818 else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4819 stripe_idx == cfs_fail_val)
4820 return lod_sub_destroy(env, dt, th);
4826 * Implementation of dt_object_operations::do_declare_destroy.
4828 * If the object is a striped directory, then the function declares reference
4829 * removal from the master object (this is an index) to the stripes and declares
4830 * destroy of all the stripes. In all the cases, it declares an intention to
4831 * destroy the object itself.
4833 * \see dt_object_operations::do_declare_destroy() in the API description
4836 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
4839 struct dt_object *next = dt_object_child(dt);
4840 struct lod_object *lo = lod_dt_obj(dt);
4841 struct lod_thread_info *info = lod_env_info(env);
4842 char *stripe_name = info->lti_key;
4847 * load striping information, notice we don't do this when object
4848 * is being initialized as we don't need this information till
4849 * few specific cases like destroy, chown
4851 rc = lod_load_striping(env, lo);
4855 /* declare destroy for all underlying objects */
4856 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4857 rc = next->do_ops->do_index_try(env, next,
4858 &dt_directory_features);
4862 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4863 rc = lod_sub_declare_ref_del(env, next, th);
4867 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4868 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4870 rc = lod_sub_declare_delete(env, next,
4871 (const struct dt_key *)stripe_name, th);
4878 * we declare destroy for the local object
4880 rc = lod_sub_declare_destroy(env, next, th);
4884 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4885 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4888 if (!lod_obj_is_striped(dt))
4891 /* declare destroy all striped objects */
4892 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4893 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4894 if (lo->ldo_stripe[i] == NULL)
4897 rc = lod_sub_declare_ref_del(env, lo->ldo_stripe[i],
4900 rc = lod_sub_declare_destroy(env, lo->ldo_stripe[i],
4906 struct lod_obj_stripe_cb_data data = { { 0 } };
4908 data.locd_declare = true;
4909 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
4910 rc = lod_obj_for_each_stripe(env, lo, th, &data);
4917 * Implementation of dt_object_operations::do_destroy.
4919 * If the object is a striped directory, then the function removes references
4920 * from the master object (this is an index) to the stripes and destroys all
4921 * the stripes. In all the cases, the function destroys the object itself.
4923 * \see dt_object_operations::do_destroy() in the API description for details.
4925 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
4928 struct dt_object *next = dt_object_child(dt);
4929 struct lod_object *lo = lod_dt_obj(dt);
4930 struct lod_thread_info *info = lod_env_info(env);
4931 char *stripe_name = info->lti_key;
4936 /* destroy sub-stripe of master object */
4937 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4938 rc = next->do_ops->do_index_try(env, next,
4939 &dt_directory_features);
4943 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4944 rc = lod_sub_ref_del(env, next, th);
4948 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4949 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4952 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
4953 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
4954 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
4956 rc = lod_sub_delete(env, next,
4957 (const struct dt_key *)stripe_name, th);
4963 rc = lod_sub_destroy(env, next, th);
4967 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4968 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4971 if (!lod_obj_is_striped(dt))
4974 /* destroy all striped objects */
4975 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4976 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4977 if (lo->ldo_stripe[i] == NULL)
4979 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4980 i == cfs_fail_val) {
4981 dt_write_lock(env, lo->ldo_stripe[i],
4983 rc = lod_sub_ref_del(env, lo->ldo_stripe[i],
4985 dt_write_unlock(env, lo->ldo_stripe[i]);
4989 rc = lod_sub_destroy(env, lo->ldo_stripe[i],
4996 struct lod_obj_stripe_cb_data data = { { 0 } };
4998 data.locd_declare = false;
4999 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
5000 rc = lod_obj_for_each_stripe(env, lo, th, &data);
5007 * Implementation of dt_object_operations::do_declare_ref_add.
5009 * \see dt_object_operations::do_declare_ref_add() in the API description
5012 static int lod_declare_ref_add(const struct lu_env *env,
5013 struct dt_object *dt, struct thandle *th)
5015 return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
5019 * Implementation of dt_object_operations::do_ref_add.
5021 * \see dt_object_operations::do_ref_add() in the API description for details.
5023 static int lod_ref_add(const struct lu_env *env,
5024 struct dt_object *dt, struct thandle *th)
5026 return lod_sub_ref_add(env, dt_object_child(dt), th);
5030 * Implementation of dt_object_operations::do_declare_ref_del.
5032 * \see dt_object_operations::do_declare_ref_del() in the API description
5035 static int lod_declare_ref_del(const struct lu_env *env,
5036 struct dt_object *dt, struct thandle *th)
5038 return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
5042 * Implementation of dt_object_operations::do_ref_del
5044 * \see dt_object_operations::do_ref_del() in the API description for details.
5046 static int lod_ref_del(const struct lu_env *env,
5047 struct dt_object *dt, struct thandle *th)
5049 return lod_sub_ref_del(env, dt_object_child(dt), th);
5053 * Implementation of dt_object_operations::do_object_sync.
5055 * \see dt_object_operations::do_object_sync() in the API description
5058 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
5059 __u64 start, __u64 end)
5061 return dt_object_sync(env, dt_object_child(dt), start, end);
5065 * Release LDLM locks on the stripes of a striped directory.
5067 * Iterates over all the locks taken on the stripe objects and
5070 * \param[in] env execution environment
5071 * \param[in] dt striped object
5072 * \param[in] einfo lock description
5073 * \param[in] policy data describing requested lock
5075 * \retval 0 on success
5076 * \retval negative if failed
5078 static int lod_object_unlock_internal(const struct lu_env *env,
5079 struct dt_object *dt,
5080 struct ldlm_enqueue_info *einfo,
5081 union ldlm_policy_data *policy)
5083 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
5088 if (slave_locks == NULL)
5091 for (i = 1; i < slave_locks->count; i++) {
5092 if (lustre_handle_is_used(&slave_locks->handles[i]))
5093 ldlm_lock_decref_and_cancel(&slave_locks->handles[i],
5101 * Implementation of dt_object_operations::do_object_unlock.
5103 * Used to release LDLM lock(s).
5105 * \see dt_object_operations::do_object_unlock() in the API description
5108 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
5109 struct ldlm_enqueue_info *einfo,
5110 union ldlm_policy_data *policy)
5112 struct lod_object *lo = lod_dt_obj(dt);
5113 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
5114 int slave_locks_size;
5118 if (slave_locks == NULL)
5121 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
5122 LASSERT(lo->ldo_dir_stripe_count > 1);
5123 /* Note: for remote lock for single stripe dir, MDT will cancel
5124 * the lock by lockh directly */
5125 LASSERT(!dt_object_remote(dt_object_child(dt)));
5127 /* locks were unlocked in MDT layer */
5128 for (i = 1; i < slave_locks->count; i++) {
5129 LASSERT(!lustre_handle_is_used(&slave_locks->handles[i]));
5130 dt_invalidate(env, lo->ldo_stripe[i]);
5133 slave_locks_size = sizeof(*slave_locks) + slave_locks->count *
5134 sizeof(slave_locks->handles[0]);
5135 OBD_FREE(slave_locks, slave_locks_size);
5136 einfo->ei_cbdata = NULL;
5142 * Implementation of dt_object_operations::do_object_lock.
5144 * Used to get LDLM lock on the non-striped and striped objects.
5146 * \see dt_object_operations::do_object_lock() in the API description
5149 static int lod_object_lock(const struct lu_env *env,
5150 struct dt_object *dt,
5151 struct lustre_handle *lh,
5152 struct ldlm_enqueue_info *einfo,
5153 union ldlm_policy_data *policy)
5155 struct lod_object *lo = lod_dt_obj(dt);
5158 int slave_locks_size;
5159 struct lustre_handle_array *slave_locks = NULL;
5162 /* remote object lock */
5163 if (!einfo->ei_enq_slave) {
5164 LASSERT(dt_object_remote(dt));
5165 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
5169 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
5170 GOTO(out, rc = -ENOTDIR);
5172 rc = lod_load_striping(env, lo);
5177 if (lo->ldo_dir_stripe_count <= 1) {
5179 * NB, ei_cbdata stores pointer to slave locks, if no locks
5180 * taken, make sure it's set to NULL, otherwise MDT will try to
5183 einfo->ei_cbdata = NULL;
5187 slave_locks_size = sizeof(*slave_locks) + lo->ldo_dir_stripe_count *
5188 sizeof(slave_locks->handles[0]);
5189 /* Freed in lod_object_unlock */
5190 OBD_ALLOC(slave_locks, slave_locks_size);
5191 if (slave_locks == NULL)
5192 GOTO(out, rc = -ENOMEM);
5193 slave_locks->count = lo->ldo_dir_stripe_count;
5195 /* striped directory lock */
5196 for (i = 1; i < lo->ldo_dir_stripe_count; i++) {
5197 struct lustre_handle lockh;
5198 struct ldlm_res_id *res_id;
5200 res_id = &lod_env_info(env)->lti_res_id;
5201 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
5203 einfo->ei_res_id = res_id;
5205 LASSERT(lo->ldo_stripe[i] != NULL);
5206 if (likely(dt_object_remote(lo->ldo_stripe[i]))) {
5207 rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh,
5210 struct ldlm_namespace *ns = einfo->ei_namespace;
5211 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
5212 ldlm_completion_callback completion = einfo->ei_cb_cp;
5213 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
5215 if (einfo->ei_mode == LCK_PW ||
5216 einfo->ei_mode == LCK_EX)
5217 dlmflags |= LDLM_FL_COS_INCOMPAT;
5219 /* This only happens if there are mulitple stripes
5220 * on the master MDT, i.e. except stripe0, there are
5221 * other stripes on the Master MDT as well, Only
5222 * happens in the test case right now. */
5223 LASSERT(ns != NULL);
5224 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS,
5225 policy, einfo->ei_mode,
5226 &dlmflags, blocking,
5228 NULL, 0, LVB_T_NONE,
5233 slave_locks->handles[i] = lockh;
5235 einfo->ei_cbdata = slave_locks;
5237 if (rc != 0 && slave_locks != NULL) {
5238 lod_object_unlock_internal(env, dt, einfo, policy);
5239 OBD_FREE(slave_locks, slave_locks_size);
5244 einfo->ei_cbdata = NULL;
5249 * Implementation of dt_object_operations::do_invalidate.
5251 * \see dt_object_operations::do_invalidate() in the API description for details
5253 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
5255 return dt_invalidate(env, dt_object_child(dt));
5258 static int lod_layout_data_init(struct lod_thread_info *info, __u32 comp_cnt)
5262 /* clear memory region that will be used for layout change */
5263 memset(&info->lti_layout_attr, 0, sizeof(struct lu_attr));
5264 info->lti_count = 0;
5266 if (info->lti_comp_size >= comp_cnt)
5269 if (info->lti_comp_size > 0) {
5270 OBD_FREE(info->lti_comp_idx,
5271 info->lti_comp_size * sizeof(__u32));
5272 info->lti_comp_size = 0;
5275 OBD_ALLOC(info->lti_comp_idx, comp_cnt * sizeof(__u32));
5276 if (!info->lti_comp_idx)
5279 info->lti_comp_size = comp_cnt;
5283 static int lod_declare_instantiate_components(const struct lu_env *env,
5284 struct lod_object *lo, struct thandle *th)
5286 struct lod_thread_info *info = lod_env_info(env);
5287 struct ost_pool *inuse = &info->lti_inuse_osts;
5292 LASSERT(info->lti_count < lo->ldo_comp_cnt);
5293 if (info->lti_count > 0) {
5294 /* Prepare inuse array for composite file */
5295 rc = lod_prepare_inuse(env, lo);
5300 for (i = 0; i < info->lti_count; i++) {
5301 rc = lod_qos_prep_create(env, lo, NULL, th,
5302 info->lti_comp_idx[i], inuse);
5308 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5309 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
5310 &info->lti_buf, XATTR_NAME_LOV, 0, th);
5316 static int lod_declare_update_plain(const struct lu_env *env,
5317 struct lod_object *lo, struct layout_intent *layout,
5318 const struct lu_buf *buf, struct thandle *th)
5320 struct lod_thread_info *info = lod_env_info(env);
5321 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5322 struct lod_layout_component *lod_comp;
5323 struct lov_comp_md_v1 *comp_v1 = NULL;
5324 bool replay = false;
5328 LASSERT(lo->ldo_flr_state == LCM_FL_NOT_FLR);
5331 * In case the client is passing lovea, which only happens during
5332 * the replay of layout intent write RPC for now, we may need to
5333 * parse the lovea and apply new layout configuration.
5335 if (buf && buf->lb_len) {
5336 struct lov_user_md_v1 *v1 = buf->lb_buf;
5338 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
5339 v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
5340 LOV_MAGIC_COMP_V1)) {
5341 CERROR("%s: the replay buffer of layout extend "
5342 "(magic %#x) does not contain expected "
5343 "composite layout.\n",
5344 lod2obd(d)->obd_name, v1->lmm_magic);
5345 GOTO(out, rc = -EINVAL);
5348 lod_object_free_striping(env, lo);
5349 rc = lod_use_defined_striping(env, lo, buf);
5353 rc = lod_get_lov_ea(env, lo);
5356 /* old on-disk EA is stored in info->lti_buf */
5357 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
5360 /* non replay path */
5361 rc = lod_load_striping_locked(env, lo);
5366 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
5368 * trunc transfers [size, eof) in the intent extent, while
5369 * we'd instantiated components covers [0, size).
5371 layout->li_extent.e_end = layout->li_extent.e_start;
5372 layout->li_extent.e_start = 0;
5375 /* Make sure defined layout covers the requested write range. */
5376 lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
5377 if (lo->ldo_comp_cnt > 1 &&
5378 lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
5379 lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
5380 CDEBUG(replay ? D_ERROR : D_LAYOUT,
5381 "%s: the defined layout [0, %#llx) does not covers "
5382 "the write range "DEXT"\n",
5383 lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
5384 PEXT(&layout->li_extent));
5385 GOTO(out, rc = -EINVAL);
5388 CDEBUG(D_LAYOUT, "%s: "DFID": instantiate components "DEXT"\n",
5389 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
5390 PEXT(&layout->li_extent));
5393 * Iterate ld->ldo_comp_entries, find the component whose extent under
5394 * the write range and not instantianted.
5396 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5397 lod_comp = &lo->ldo_comp_entries[i];
5399 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
5403 if (lod_comp_inited(lod_comp))
5407 * In replay path, lod_comp is the EA passed by
5408 * client replay buffer, comp_v1 is the pre-recovery
5409 * on-disk EA, we'd sift out those components which
5410 * were init-ed in the on-disk EA.
5412 if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
5417 * this component hasn't instantiated in normal path, or during
5418 * replay it needs replay the instantiation.
5421 /* A released component is being extended */
5422 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
5423 GOTO(out, rc = -EINVAL);
5425 LASSERT(info->lti_comp_idx != NULL);
5426 info->lti_comp_idx[info->lti_count++] = i;
5429 if (info->lti_count == 0)
5432 lod_obj_inc_layout_gen(lo);
5433 rc = lod_declare_instantiate_components(env, lo, th);
5436 lod_object_free_striping(env, lo);
5440 #define lod_foreach_mirror_comp(comp, lo, mirror_idx) \
5441 for (comp = &lo->ldo_comp_entries[lo->ldo_mirrors[mirror_idx].lme_start]; \
5442 comp <= &lo->ldo_comp_entries[lo->ldo_mirrors[mirror_idx].lme_end]; \
5445 static inline int lod_comp_index(struct lod_object *lo,
5446 struct lod_layout_component *lod_comp)
5448 LASSERT(lod_comp >= lo->ldo_comp_entries &&
5449 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
5451 return lod_comp - lo->ldo_comp_entries;
5455 * Stale other mirrors by writing extent.
5457 static void lod_stale_components(struct lod_object *lo, int primary,
5458 struct lu_extent *extent)
5460 struct lod_layout_component *pri_comp, *lod_comp;
5463 /* The writing extent decides which components in the primary
5464 * are affected... */
5465 CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
5466 lod_foreach_mirror_comp(pri_comp, lo, primary) {
5467 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
5470 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
5471 lod_comp_index(lo, pri_comp),
5472 PEXT(&pri_comp->llc_extent));
5474 for (i = 0; i < lo->ldo_mirror_count; i++) {
5478 /* ... and then stale other components that are
5479 * overlapping with primary components */
5480 lod_foreach_mirror_comp(lod_comp, lo, i) {
5481 if (!lu_extent_is_overlapped(
5482 &pri_comp->llc_extent,
5483 &lod_comp->llc_extent))
5486 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
5487 i, lod_comp_index(lo, lod_comp));
5489 lod_comp->llc_flags |= LCME_FL_STALE;
5490 lo->ldo_mirrors[i].lme_stale = 1;
5496 static int lod_declare_update_rdonly(const struct lu_env *env,
5497 struct lod_object *lo, struct md_layout_change *mlc,
5500 struct lod_thread_info *info = lod_env_info(env);
5501 struct lu_attr *layout_attr = &info->lti_layout_attr;
5502 struct lod_layout_component *lod_comp;
5503 struct layout_intent *layout = mlc->mlc_intent;
5504 struct lu_extent extent = layout->li_extent;
5505 unsigned int seq = 0;
5511 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE);
5512 LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
5513 LASSERT(lo->ldo_mirror_count > 0);
5515 CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
5516 PFID(lod_object_fid(lo)), PEXT(&extent));
5518 if (OBD_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
5519 get_random_bytes(&seq, sizeof(seq));
5520 seq %= lo->ldo_mirror_count;
5524 * Pick a mirror as the primary.
5525 * Now it only picks the first mirror, this algo can be
5526 * revised later after knowing the topology of cluster or
5527 * the availability of OSTs.
5529 for (picked = -1, i = 0; i < lo->ldo_mirror_count; i++) {
5530 int index = (i + seq) % lo->ldo_mirror_count;
5532 if (!lo->ldo_mirrors[index].lme_stale) {
5537 if (picked < 0) /* failed to pick a primary */
5540 CDEBUG(D_LAYOUT, DFID": picked mirror %u as primary\n",
5541 PFID(lod_object_fid(lo)), lo->ldo_mirrors[picked].lme_id);
5543 /* stale overlapping components from other mirrors */
5544 lod_stale_components(lo, picked, &extent);
5546 /* instantiate components for the picked mirror, start from 0 */
5547 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
5549 * trunc transfers [size, eof) in the intent extent, we'd
5550 * stale components overlapping [size, eof), while we'd
5551 * instantiated components covers [0, size).
5553 extent.e_end = extent.e_start;
5557 lod_foreach_mirror_comp(lod_comp, lo, picked) {
5558 if (!lu_extent_is_overlapped(&extent,
5559 &lod_comp->llc_extent))
5562 if (lod_comp_inited(lod_comp))
5565 CDEBUG(D_LAYOUT, "instantiate: %u / %u\n",
5566 i, lod_comp_index(lo, lod_comp));
5568 info->lti_comp_idx[info->lti_count++] =
5569 lod_comp_index(lo, lod_comp);
5572 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
5574 /* Reset the layout version once it's becoming too large.
5575 * This way it can make sure that the layout version is
5576 * monotonously increased in this writing era. */
5577 lod_obj_inc_layout_gen(lo);
5578 if (lo->ldo_layout_gen > (LCME_ID_MAX >> 1)) {
5579 __u32 layout_version;
5581 cfs_get_random_bytes(&layout_version, sizeof(layout_version));
5582 lo->ldo_layout_gen = layout_version & 0xffff;
5585 rc = lod_declare_instantiate_components(env, lo, th);
5589 layout_attr->la_valid = LA_LAYOUT_VERSION;
5590 layout_attr->la_layout_version = 0; /* set current version */
5591 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
5597 lod_object_free_striping(env, lo);
5601 static int lod_declare_update_write_pending(const struct lu_env *env,
5602 struct lod_object *lo, struct md_layout_change *mlc,
5605 struct lod_thread_info *info = lod_env_info(env);
5606 struct lu_attr *layout_attr = &info->lti_layout_attr;
5607 struct lod_layout_component *lod_comp;
5608 struct lu_extent extent = { 0 };
5614 LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
5615 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
5616 mlc->mlc_opc == MD_LAYOUT_RESYNC);
5618 /* look for the primary mirror */
5619 for (i = 0; i < lo->ldo_mirror_count; i++) {
5620 if (lo->ldo_mirrors[i].lme_stale)
5623 LASSERTF(primary < 0, DFID " has multiple primary: %u / %u",
5624 PFID(lod_object_fid(lo)),
5625 lo->ldo_mirrors[i].lme_id,
5626 lo->ldo_mirrors[primary].lme_id);
5631 CERROR(DFID ": doesn't have a primary mirror\n",
5632 PFID(lod_object_fid(lo)));
5633 GOTO(out, rc = -ENODATA);
5636 CDEBUG(D_LAYOUT, DFID": found primary %u\n",
5637 PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
5639 LASSERT(!lo->ldo_mirrors[primary].lme_stale);
5641 /* for LAYOUT_WRITE opc, it has to do the following operations:
5642 * 1. stale overlapping componets from stale mirrors;
5643 * 2. instantiate components of the primary mirror;
5644 * 3. transfter layout version to all objects of the primary;
5646 * for LAYOUT_RESYNC opc, it will do:
5647 * 1. instantiate components of all stale mirrors;
5648 * 2. transfer layout version to all objects to close write era. */
5650 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
5651 LASSERT(mlc->mlc_intent != NULL);
5653 extent = mlc->mlc_intent->li_extent;
5655 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
5656 PFID(lod_object_fid(lo)), PEXT(&extent));
5658 /* 1. stale overlapping components */
5659 lod_stale_components(lo, primary, &extent);
5661 /* 2. find out the components need instantiating.
5662 * instantiate [0, mlc->mlc_intent->e_end) */
5663 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
5665 * trunc transfers [size, eof) in the intent extent,
5666 * we'd stale components overlapping [size, eof),
5667 * while we'd instantiated components covers [0, size).
5669 extent.e_end = extent.e_start;
5673 lod_foreach_mirror_comp(lod_comp, lo, primary) {
5674 if (!lu_extent_is_overlapped(&extent,
5675 &lod_comp->llc_extent))
5678 if (lod_comp_inited(lod_comp))
5681 CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
5682 primary, lod_comp_index(lo, lod_comp));
5683 info->lti_comp_idx[info->lti_count++] =
5684 lod_comp_index(lo, lod_comp);
5686 } else { /* MD_LAYOUT_RESYNC */
5687 /* figure out the components that have been instantiated in
5688 * in primary to decide what components should be instantiated
5689 * in stale mirrors */
5690 lod_foreach_mirror_comp(lod_comp, lo, primary) {
5691 if (!lod_comp_inited(lod_comp))
5694 extent.e_end = lod_comp->llc_extent.e_end;
5698 DFID": instantiate all stale components in "DEXT"\n",
5699 PFID(lod_object_fid(lo)), PEXT(&extent));
5701 /* 1. instantiate all components within this extent, even
5702 * non-stale components so that it won't need to instantiate
5703 * those components for mirror truncate later. */
5704 for (i = 0; i < lo->ldo_mirror_count; i++) {
5708 LASSERTF(lo->ldo_mirrors[i].lme_stale,
5709 "both %d and %d are primary\n", i, primary);
5711 lod_foreach_mirror_comp(lod_comp, lo, i) {
5712 if (!lu_extent_is_overlapped(&extent,
5713 &lod_comp->llc_extent))
5716 if (lod_comp_inited(lod_comp))
5719 CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
5720 i, lod_comp_index(lo, lod_comp));
5722 info->lti_comp_idx[info->lti_count++] =
5723 lod_comp_index(lo, lod_comp);
5727 /* change the file state to SYNC_PENDING */
5728 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
5731 rc = lod_declare_instantiate_components(env, lo, th);
5735 /* 3. transfer layout version to OST objects.
5736 * transfer new layout version to OST objects so that stale writes
5737 * can be denied. It also ends an era of writing by setting
5738 * LU_LAYOUT_RESYNC. Normal client can never use this bit to
5739 * send write RPC; only resync RPCs could do it. */
5740 layout_attr->la_valid = LA_LAYOUT_VERSION;
5741 layout_attr->la_layout_version = 0; /* set current version */
5742 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
5743 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
5744 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
5748 lod_obj_inc_layout_gen(lo);
5751 lod_object_free_striping(env, lo);
5755 static int lod_declare_update_sync_pending(const struct lu_env *env,
5756 struct lod_object *lo, struct md_layout_change *mlc,
5759 struct lod_thread_info *info = lod_env_info(env);
5760 unsigned sync_components = 0;
5761 unsigned resync_components = 0;
5766 LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
5767 LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
5768 mlc->mlc_opc == MD_LAYOUT_WRITE);
5770 CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
5771 PFID(lod_object_fid(lo)), mlc->mlc_opc);
5773 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
5774 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
5775 PFID(lod_object_fid(lo)));
5777 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
5778 return lod_declare_update_write_pending(env, lo, mlc, th);
5781 /* MD_LAYOUT_RESYNC_DONE */
5783 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5784 struct lod_layout_component *lod_comp;
5787 lod_comp = &lo->ldo_comp_entries[i];
5789 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
5794 for (j = 0; j < mlc->mlc_resync_count; j++) {
5795 if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
5798 mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
5799 lod_comp->llc_flags &= ~LCME_FL_STALE;
5800 resync_components++;
5806 for (i = 0; i < mlc->mlc_resync_count; i++) {
5807 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
5810 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
5811 "or already synced\n", PFID(lod_object_fid(lo)),
5812 mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
5813 GOTO(out, rc = -EINVAL);
5816 if (!sync_components || !resync_components) {
5817 CDEBUG(D_LAYOUT, DFID": no mirror in sync or resync\n",
5818 PFID(lod_object_fid(lo)));
5820 /* tend to return an error code here to prevent
5821 * the MDT from setting SoM attribute */
5822 GOTO(out, rc = -EINVAL);
5825 CDEBUG(D_LAYOUT, DFID": resynced %u/%zu components\n",
5826 PFID(lod_object_fid(lo)),
5827 resync_components, mlc->mlc_resync_count);
5829 lo->ldo_flr_state = LCM_FL_RDONLY;
5830 lod_obj_inc_layout_gen(lo);
5832 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5833 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
5834 &info->lti_buf, XATTR_NAME_LOV, 0, th);
5839 lod_object_free_striping(env, lo);
5843 static int lod_declare_layout_change(const struct lu_env *env,
5844 struct dt_object *dt, struct md_layout_change *mlc,
5847 struct lod_thread_info *info = lod_env_info(env);
5848 struct lod_object *lo = lod_dt_obj(dt);
5852 if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
5853 dt_object_remote(dt_object_child(dt)))
5856 lod_write_lock(env, dt, 0);
5857 rc = lod_load_striping_locked(env, lo);
5861 LASSERT(lo->ldo_comp_cnt > 0);
5863 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
5867 switch (lo->ldo_flr_state) {
5868 case LCM_FL_NOT_FLR:
5869 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
5873 rc = lod_declare_update_rdonly(env, lo, mlc, th);
5875 case LCM_FL_WRITE_PENDING:
5876 rc = lod_declare_update_write_pending(env, lo, mlc, th);
5878 case LCM_FL_SYNC_PENDING:
5879 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
5886 dt_write_unlock(env, dt);
5891 * Instantiate layout component objects which covers the intent write offset.
5893 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
5894 struct md_layout_change *mlc, struct thandle *th)
5896 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
5897 struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
5898 struct lod_object *lo = lod_dt_obj(dt);
5901 rc = lod_striped_create(env, dt, attr, NULL, th);
5902 if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
5903 layout_attr->la_layout_version |= lo->ldo_layout_gen;
5904 rc = lod_attr_set(env, dt, layout_attr, th);
5910 struct dt_object_operations lod_obj_ops = {
5911 .do_read_lock = lod_read_lock,
5912 .do_write_lock = lod_write_lock,
5913 .do_read_unlock = lod_read_unlock,
5914 .do_write_unlock = lod_write_unlock,
5915 .do_write_locked = lod_write_locked,
5916 .do_attr_get = lod_attr_get,
5917 .do_declare_attr_set = lod_declare_attr_set,
5918 .do_attr_set = lod_attr_set,
5919 .do_xattr_get = lod_xattr_get,
5920 .do_declare_xattr_set = lod_declare_xattr_set,
5921 .do_xattr_set = lod_xattr_set,
5922 .do_declare_xattr_del = lod_declare_xattr_del,
5923 .do_xattr_del = lod_xattr_del,
5924 .do_xattr_list = lod_xattr_list,
5925 .do_ah_init = lod_ah_init,
5926 .do_declare_create = lod_declare_create,
5927 .do_create = lod_create,
5928 .do_declare_destroy = lod_declare_destroy,
5929 .do_destroy = lod_destroy,
5930 .do_index_try = lod_index_try,
5931 .do_declare_ref_add = lod_declare_ref_add,
5932 .do_ref_add = lod_ref_add,
5933 .do_declare_ref_del = lod_declare_ref_del,
5934 .do_ref_del = lod_ref_del,
5935 .do_object_sync = lod_object_sync,
5936 .do_object_lock = lod_object_lock,
5937 .do_object_unlock = lod_object_unlock,
5938 .do_invalidate = lod_invalidate,
5939 .do_declare_layout_change = lod_declare_layout_change,
5940 .do_layout_change = lod_layout_change,
5944 * Implementation of dt_body_operations::dbo_read.
5946 * \see dt_body_operations::dbo_read() in the API description for details.
5948 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
5949 struct lu_buf *buf, loff_t *pos)
5951 struct dt_object *next = dt_object_child(dt);
5953 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
5954 S_ISLNK(dt->do_lu.lo_header->loh_attr));
5955 return next->do_body_ops->dbo_read(env, next, buf, pos);
5959 * Implementation of dt_body_operations::dbo_declare_write.
5961 * \see dt_body_operations::dbo_declare_write() in the API description
5964 static ssize_t lod_declare_write(const struct lu_env *env,
5965 struct dt_object *dt,
5966 const struct lu_buf *buf, loff_t pos,
5969 return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
5973 * Implementation of dt_body_operations::dbo_write.
5975 * \see dt_body_operations::dbo_write() in the API description for details.
5977 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
5978 const struct lu_buf *buf, loff_t *pos,
5979 struct thandle *th, int iq)
5981 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
5982 S_ISLNK(dt->do_lu.lo_header->loh_attr));
5983 return lod_sub_write(env, dt_object_child(dt), buf, pos, th, iq);
5986 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
5987 __u64 start, __u64 end, struct thandle *th)
5989 if (dt_object_remote(dt))
5992 return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
5995 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
5996 __u64 start, __u64 end, struct thandle *th)
5998 if (dt_object_remote(dt))
6001 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
6002 return lod_sub_punch(env, dt_object_child(dt), start, end, th);
6006 * different type of files use the same body_ops because object may be created
6007 * in OUT, where there is no chance to set correct body_ops for each type, so
6008 * body_ops themselves will check file type inside, see lod_read/write/punch for
6011 const struct dt_body_operations lod_body_ops = {
6012 .dbo_read = lod_read,
6013 .dbo_declare_write = lod_declare_write,
6014 .dbo_write = lod_write,
6015 .dbo_declare_punch = lod_declare_punch,
6016 .dbo_punch = lod_punch,
6020 * Implementation of lu_object_operations::loo_object_init.
6022 * The function determines the type and the index of the target device using
6023 * sequence of the object's FID. Then passes control down to the
6024 * corresponding device:
6025 * OSD for the local objects, OSP for remote
6027 * \see lu_object_operations::loo_object_init() in the API description
6030 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
6031 const struct lu_object_conf *conf)
6033 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
6034 struct lu_device *cdev = NULL;
6035 struct lu_object *cobj;
6036 struct lod_tgt_descs *ltd = NULL;
6037 struct lod_tgt_desc *tgt;
6039 int type = LU_SEQ_RANGE_ANY;
6043 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
6045 /* Note: Sometimes, it will Return EAGAIN here, see
6046 * ptrlpc_import_delay_req(), which might confuse
6047 * lu_object_find_at() and make it wait there incorrectly.
6048 * so we convert it to EIO here.*/
6055 if (type == LU_SEQ_RANGE_MDT &&
6056 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
6057 cdev = &lod->lod_child->dd_lu_dev;
6058 } else if (type == LU_SEQ_RANGE_MDT) {
6059 ltd = &lod->lod_mdt_descs;
6061 } else if (type == LU_SEQ_RANGE_OST) {
6062 ltd = &lod->lod_ost_descs;
6069 if (ltd->ltd_tgts_size > idx &&
6070 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
6071 tgt = LTD_TGT(ltd, idx);
6073 LASSERT(tgt != NULL);
6074 LASSERT(tgt->ltd_tgt != NULL);
6076 cdev = &(tgt->ltd_tgt->dd_lu_dev);
6078 lod_putref(lod, ltd);
6081 if (unlikely(cdev == NULL))
6084 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
6085 if (unlikely(cobj == NULL))
6088 lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
6090 lu_object_add(lo, cobj);
6097 * Release resources associated with striping.
6099 * If the object is striped (regular or directory), then release
6100 * the stripe objects references and free the ldo_stripe array.
6102 * \param[in] env execution environment
6103 * \param[in] lo object
6105 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
6107 struct lod_layout_component *lod_comp;
6110 if (lo->ldo_stripe != NULL) {
6111 LASSERT(lo->ldo_comp_entries == NULL);
6112 LASSERT(lo->ldo_dir_stripes_allocated > 0);
6114 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6115 if (lo->ldo_stripe[i])
6116 dt_object_put(env, lo->ldo_stripe[i]);
6119 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
6120 OBD_FREE(lo->ldo_stripe, j);
6121 lo->ldo_stripe = NULL;
6122 lo->ldo_dir_stripes_allocated = 0;
6123 lo->ldo_dir_stripe_loaded = 0;
6124 lo->ldo_dir_stripe_count = 0;
6125 } else if (lo->ldo_comp_entries != NULL) {
6126 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6127 /* free lod_layout_component::llc_stripe array */
6128 lod_comp = &lo->ldo_comp_entries[i];
6130 if (lod_comp->llc_stripe == NULL)
6132 LASSERT(lod_comp->llc_stripes_allocated != 0);
6133 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
6134 if (lod_comp->llc_stripe[j] != NULL)
6136 &lod_comp->llc_stripe[j]->do_lu);
6138 OBD_FREE(lod_comp->llc_stripe,
6139 sizeof(struct dt_object *) *
6140 lod_comp->llc_stripes_allocated);
6141 lod_comp->llc_stripe = NULL;
6142 lod_comp->llc_stripes_allocated = 0;
6144 lod_free_comp_entries(lo);
6145 lo->ldo_comp_cached = 0;
6150 * Implementation of lu_object_operations::loo_object_free.
6152 * \see lu_object_operations::loo_object_free() in the API description
6155 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
6157 struct lod_object *lo = lu2lod_obj(o);
6159 /* release all underlying object pinned */
6160 lod_object_free_striping(env, lo);
6162 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
6166 * Implementation of lu_object_operations::loo_object_release.
6168 * \see lu_object_operations::loo_object_release() in the API description
6171 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
6173 /* XXX: shouldn't we release everything here in case if object
6174 * creation failed before? */
6178 * Implementation of lu_object_operations::loo_object_print.
6180 * \see lu_object_operations::loo_object_print() in the API description
6183 static int lod_object_print(const struct lu_env *env, void *cookie,
6184 lu_printer_t p, const struct lu_object *l)
6186 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
6188 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
6191 struct lu_object_operations lod_lu_obj_ops = {
6192 .loo_object_init = lod_object_init,
6193 .loo_object_free = lod_object_free,
6194 .loo_object_release = lod_object_release,
6195 .loo_object_print = lod_object_print,