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 (magic != LMV_MAGIC_V1)
782 stripes = le32_to_cpu(lmv1->lmv_stripe_count);
786 rc = lmv_mds_md_size(stripes, magic);
790 if (buf->lb_len < lmv1_size) {
799 lu_buf_alloc(buf, lmv1_size);
804 memcpy(buf->lb_buf, tbuf.lb_buf, tbuf.lb_len);
807 if (unlikely(!dt_try_as_dir(env, obj)))
810 memset(&lmv1->lmv_stripe_fids[0], 0, stripes * sizeof(struct lu_fid));
811 iops = &obj->do_index_ops->dio_it;
812 it = iops->init(env, obj, LUDA_64BITHASH);
816 rc = iops->load(env, it, 0);
818 rc = iops->next(env, it);
823 char name[FID_LEN + 2] = "";
828 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
834 fid_le_to_cpu(&fid, &ent->lde_fid);
835 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
836 if (ent->lde_name[0] == '.') {
837 if (ent->lde_namelen == 1)
840 if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
844 len = snprintf(name, sizeof(name),
845 DFID":", PFID(&ent->lde_fid));
846 /* The ent->lde_name is composed of ${FID}:${index} */
847 if (ent->lde_namelen < len + 1 ||
848 memcmp(ent->lde_name, name, len) != 0) {
849 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
850 "%s: invalid shard name %.*s with the FID "DFID
851 " for the striped directory "DFID", %s\n",
852 lod2obd(lod)->obd_name, ent->lde_namelen,
853 ent->lde_name, PFID(&fid),
854 PFID(lu_object_fid(&obj->do_lu)),
855 lod->lod_lmv_failout ? "failout" : "skip");
857 if (lod->lod_lmv_failout)
865 if (ent->lde_name[len] < '0' ||
866 ent->lde_name[len] > '9') {
867 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
868 "%s: invalid shard name %.*s with the "
869 "FID "DFID" for the striped directory "
871 lod2obd(lod)->obd_name, ent->lde_namelen,
872 ent->lde_name, PFID(&fid),
873 PFID(lu_object_fid(&obj->do_lu)),
874 lod->lod_lmv_failout ?
877 if (lod->lod_lmv_failout)
883 index = index * 10 + ent->lde_name[len++] - '0';
884 } while (len < ent->lde_namelen);
886 if (len == ent->lde_namelen) {
887 /* Out of LMV EA range. */
888 if (index >= stripes) {
889 CERROR("%s: the shard %.*s for the striped "
890 "directory "DFID" is out of the known "
891 "LMV EA range [0 - %u], failout\n",
892 lod2obd(lod)->obd_name, ent->lde_namelen,
894 PFID(lu_object_fid(&obj->do_lu)),
900 /* The slot has been occupied. */
901 if (!fid_is_zero(&lmv1->lmv_stripe_fids[index])) {
905 &lmv1->lmv_stripe_fids[index]);
906 CERROR("%s: both the shard "DFID" and "DFID
907 " for the striped directory "DFID
908 " claim the same LMV EA slot at the "
909 "index %d, failout\n",
910 lod2obd(lod)->obd_name,
911 PFID(&fid0), PFID(&fid),
912 PFID(lu_object_fid(&obj->do_lu)), index);
917 /* stored as LE mode */
918 lmv1->lmv_stripe_fids[index] = ent->lde_fid;
921 rc = iops->next(env, it);
928 RETURN(rc > 0 ? lmv_mds_md_size(stripes, magic) : rc);
932 * Implementation of dt_object_operations::do_index_try.
934 * \see dt_object_operations::do_index_try() in the API description for details.
936 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
937 const struct dt_index_features *feat)
939 struct lod_object *lo = lod_dt_obj(dt);
940 struct dt_object *next = dt_object_child(dt);
944 LASSERT(next->do_ops);
945 LASSERT(next->do_ops->do_index_try);
947 rc = lod_striping_load(env, lo);
951 rc = next->do_ops->do_index_try(env, next, feat);
955 if (lo->ldo_dir_stripe_count > 0) {
958 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
959 if (dt_object_exists(lo->ldo_stripe[i]) == 0)
961 rc = lo->ldo_stripe[i]->do_ops->do_index_try(env,
962 lo->ldo_stripe[i], feat);
966 dt->do_index_ops = &lod_striped_index_ops;
968 dt->do_index_ops = &lod_index_ops;
975 * Implementation of dt_object_operations::do_read_lock.
977 * \see dt_object_operations::do_read_lock() in the API description for details.
979 static void lod_read_lock(const struct lu_env *env, struct dt_object *dt,
982 dt_read_lock(env, dt_object_child(dt), role);
986 * Implementation of dt_object_operations::do_write_lock.
988 * \see dt_object_operations::do_write_lock() in the API description for
991 static void lod_write_lock(const struct lu_env *env, struct dt_object *dt,
994 dt_write_lock(env, dt_object_child(dt), role);
998 * Implementation of dt_object_operations::do_read_unlock.
1000 * \see dt_object_operations::do_read_unlock() in the API description for
1003 static void lod_read_unlock(const struct lu_env *env, struct dt_object *dt)
1005 dt_read_unlock(env, dt_object_child(dt));
1009 * Implementation of dt_object_operations::do_write_unlock.
1011 * \see dt_object_operations::do_write_unlock() in the API description for
1014 static void lod_write_unlock(const struct lu_env *env, struct dt_object *dt)
1016 dt_write_unlock(env, dt_object_child(dt));
1020 * Implementation of dt_object_operations::do_write_locked.
1022 * \see dt_object_operations::do_write_locked() in the API description for
1025 static int lod_write_locked(const struct lu_env *env, struct dt_object *dt)
1027 return dt_write_locked(env, dt_object_child(dt));
1031 * Implementation of dt_object_operations::do_attr_get.
1033 * \see dt_object_operations::do_attr_get() in the API description for details.
1035 static int lod_attr_get(const struct lu_env *env,
1036 struct dt_object *dt,
1037 struct lu_attr *attr)
1039 /* Note: for striped directory, client will merge attributes
1040 * from all of the sub-stripes see lmv_merge_attr(), and there
1041 * no MDD logic depend on directory nlink/size/time, so we can
1042 * always use master inode nlink and size for now. */
1043 return dt_attr_get(env, dt_object_child(dt), attr);
1046 static inline void lod_adjust_stripe_info(struct lod_layout_component *comp,
1047 struct lov_desc *desc)
1049 if (comp->llc_pattern != LOV_PATTERN_MDT) {
1050 if (!comp->llc_stripe_count)
1051 comp->llc_stripe_count =
1052 desc->ld_default_stripe_count;
1054 if (comp->llc_stripe_size <= 0)
1055 comp->llc_stripe_size = desc->ld_default_stripe_size;
1058 int lod_obj_for_each_stripe(const struct lu_env *env, struct lod_object *lo,
1060 struct lod_obj_stripe_cb_data *data)
1062 struct lod_layout_component *lod_comp;
1066 LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
1067 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1068 lod_comp = &lo->ldo_comp_entries[i];
1070 if (lod_comp->llc_stripe == NULL)
1073 /* has stripe but not inited yet, this component has been
1074 * declared to be created, but hasn't created yet.
1076 if (!lod_comp_inited(lod_comp))
1079 if (data->locd_comp_skip_cb &&
1080 data->locd_comp_skip_cb(env, lo, i, data))
1083 if (data->locd_comp_cb) {
1084 rc = data->locd_comp_cb(env, lo, i, data);
1089 /* could used just to do sth about component, not each
1092 if (!data->locd_stripe_cb)
1095 LASSERT(lod_comp->llc_stripe_count > 0);
1096 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
1097 struct dt_object *dt = lod_comp->llc_stripe[j];
1101 rc = data->locd_stripe_cb(env, lo, dt, th, i, j, data);
1109 static bool lod_obj_attr_set_comp_skip_cb(const struct lu_env *env,
1110 struct lod_object *lo, int comp_idx,
1111 struct lod_obj_stripe_cb_data *data)
1113 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
1114 bool skipped = false;
1116 if (!(data->locd_attr->la_valid & LA_LAYOUT_VERSION))
1119 switch (lo->ldo_flr_state) {
1120 case LCM_FL_WRITE_PENDING: {
1123 /* skip stale components */
1124 if (lod_comp->llc_flags & LCME_FL_STALE) {
1129 /* skip valid and overlapping components, therefore any
1130 * attempts to write overlapped components will never succeed
1131 * because client will get EINPROGRESS. */
1132 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1136 if (lo->ldo_comp_entries[i].llc_flags & LCME_FL_STALE)
1139 if (lu_extent_is_overlapped(&lod_comp->llc_extent,
1140 &lo->ldo_comp_entries[i].llc_extent)) {
1148 LASSERTF(0, "impossible: %d\n", lo->ldo_flr_state);
1149 case LCM_FL_SYNC_PENDING:
1153 CDEBUG(D_LAYOUT, DFID": %s to set component %x to version: %u\n",
1154 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
1155 skipped ? "skipped" : "chose", lod_comp->llc_id,
1156 data->locd_attr->la_layout_version);
1162 lod_obj_stripe_attr_set_cb(const struct lu_env *env, struct lod_object *lo,
1163 struct dt_object *dt, struct thandle *th,
1164 int comp_idx, int stripe_idx,
1165 struct lod_obj_stripe_cb_data *data)
1167 if (data->locd_declare)
1168 return lod_sub_declare_attr_set(env, dt, data->locd_attr, th);
1170 if (data->locd_attr->la_valid & LA_LAYOUT_VERSION) {
1171 CDEBUG(D_LAYOUT, DFID": set layout version: %u, comp_idx: %d\n",
1172 PFID(lu_object_fid(&dt->do_lu)),
1173 data->locd_attr->la_layout_version, comp_idx);
1176 return lod_sub_attr_set(env, dt, data->locd_attr, th);
1180 * Implementation of dt_object_operations::do_declare_attr_set.
1182 * If the object is striped, then apply the changes to all the stripes.
1184 * \see dt_object_operations::do_declare_attr_set() in the API description
1187 static int lod_declare_attr_set(const struct lu_env *env,
1188 struct dt_object *dt,
1189 const struct lu_attr *attr,
1192 struct dt_object *next = dt_object_child(dt);
1193 struct lod_object *lo = lod_dt_obj(dt);
1198 * declare setattr on the local object
1200 rc = lod_sub_declare_attr_set(env, next, attr, th);
1204 /* osp_declare_attr_set() ignores all attributes other than
1205 * UID, GID, PROJID, and size, and osp_attr_set() ignores all
1206 * but UID, GID and PROJID. Declaration of size attr setting
1207 * happens through lod_declare_init_size(), and not through
1208 * this function. Therefore we need not load striping unless
1209 * ownership is changing. This should save memory and (we hope)
1210 * speed up rename().
1212 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1213 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1216 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1219 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID | LA_MODE |
1220 LA_ATIME | LA_MTIME | LA_CTIME |
1225 * load striping information, notice we don't do this when object
1226 * is being initialized as we don't need this information till
1227 * few specific cases like destroy, chown
1229 rc = lod_striping_load(env, lo);
1233 if (!lod_obj_is_striped(dt))
1237 * if object is striped declare changes on the stripes
1239 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1240 LASSERT(lo->ldo_stripe);
1241 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1242 if (lo->ldo_stripe[i] == NULL)
1244 rc = lod_sub_declare_attr_set(env, lo->ldo_stripe[i],
1250 struct lod_obj_stripe_cb_data data = { { 0 } };
1252 data.locd_attr = attr;
1253 data.locd_declare = true;
1254 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1255 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1261 if (!dt_object_exists(next) || dt_object_remote(next) ||
1262 !S_ISREG(attr->la_mode))
1265 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1266 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
1270 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1271 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1272 struct lod_thread_info *info = lod_env_info(env);
1273 struct lu_buf *buf = &info->lti_buf;
1275 buf->lb_buf = info->lti_ea_store;
1276 buf->lb_len = info->lti_ea_store_size;
1277 rc = lod_sub_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1278 LU_XATTR_REPLACE, th);
1285 * Implementation of dt_object_operations::do_attr_set.
1287 * If the object is striped, then apply the changes to all or subset of
1288 * the stripes depending on the object type and specific attributes.
1290 * \see dt_object_operations::do_attr_set() in the API description for details.
1292 static int lod_attr_set(const struct lu_env *env,
1293 struct dt_object *dt,
1294 const struct lu_attr *attr,
1297 struct dt_object *next = dt_object_child(dt);
1298 struct lod_object *lo = lod_dt_obj(dt);
1303 * apply changes to the local object
1305 rc = lod_sub_attr_set(env, next, attr, th);
1309 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1310 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1313 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1316 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE | LA_PROJID |
1317 LA_ATIME | LA_MTIME | LA_CTIME |
1322 /* FIXME: a tricky case in the code path of mdd_layout_change():
1323 * the in-memory striping information has been freed in lod_xattr_set()
1324 * due to layout change. It has to load stripe here again. It only
1325 * changes flags of layout so declare_attr_set() is still accurate */
1326 rc = lod_striping_load(env, lo);
1330 if (!lod_obj_is_striped(dt))
1334 * if object is striped, apply changes to all the stripes
1336 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1337 LASSERT(lo->ldo_stripe);
1338 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1339 if (unlikely(lo->ldo_stripe[i] == NULL))
1342 if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1345 rc = lod_sub_attr_set(env, lo->ldo_stripe[i], attr, th);
1350 struct lod_obj_stripe_cb_data data = { { 0 } };
1352 data.locd_attr = attr;
1353 data.locd_declare = false;
1354 data.locd_comp_skip_cb = lod_obj_attr_set_comp_skip_cb;
1355 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1356 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1362 if (!dt_object_exists(next) || dt_object_remote(next) ||
1363 !S_ISREG(attr->la_mode))
1366 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1367 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
1371 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1372 struct lod_thread_info *info = lod_env_info(env);
1373 struct lu_buf *buf = &info->lti_buf;
1374 struct ost_id *oi = &info->lti_ostid;
1375 struct lu_fid *fid = &info->lti_fid;
1376 struct lov_mds_md_v1 *lmm;
1377 struct lov_ost_data_v1 *objs;
1380 rc = lod_get_lov_ea(env, lo);
1384 buf->lb_buf = info->lti_ea_store;
1385 buf->lb_len = info->lti_ea_store_size;
1386 lmm = info->lti_ea_store;
1387 magic = le32_to_cpu(lmm->lmm_magic);
1388 if (magic == LOV_MAGIC_COMP_V1) {
1389 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1390 struct lov_comp_md_entry_v1 *lcme =
1391 &lcm->lcm_entries[0];
1393 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1394 magic = le32_to_cpu(lmm->lmm_magic);
1397 if (magic == LOV_MAGIC_V1)
1398 objs = &(lmm->lmm_objects[0]);
1400 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1401 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1402 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1404 fid_to_ostid(fid, oi);
1405 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1407 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1408 LU_XATTR_REPLACE, th);
1409 } else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1410 struct lod_thread_info *info = lod_env_info(env);
1411 struct lu_buf *buf = &info->lti_buf;
1412 struct lov_comp_md_v1 *lcm;
1413 struct lov_comp_md_entry_v1 *lcme;
1415 rc = lod_get_lov_ea(env, lo);
1419 buf->lb_buf = info->lti_ea_store;
1420 buf->lb_len = info->lti_ea_store_size;
1422 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
1425 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1426 lcme = &lcm->lcm_entries[0];
1427 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1428 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1430 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1431 LU_XATTR_REPLACE, th);
1438 * Implementation of dt_object_operations::do_xattr_get.
1440 * If LOV EA is requested from the root object and it's not
1441 * found, then return default striping for the filesystem.
1443 * \see dt_object_operations::do_xattr_get() in the API description for details.
1445 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1446 struct lu_buf *buf, const char *name)
1448 struct lod_thread_info *info = lod_env_info(env);
1449 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1454 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1455 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1456 struct lmv_mds_md_v1 *lmv1;
1459 if (rc > (typeof(rc))sizeof(*lmv1))
1462 if (rc < (typeof(rc))sizeof(*lmv1))
1463 RETURN(rc = rc > 0 ? -EINVAL : rc);
1465 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1466 CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1468 info->lti_buf.lb_buf = info->lti_key;
1469 info->lti_buf.lb_len = sizeof(*lmv1);
1470 rc = dt_xattr_get(env, dt_object_child(dt),
1471 &info->lti_buf, name);
1472 if (unlikely(rc != sizeof(*lmv1)))
1473 RETURN(rc = rc > 0 ? -EINVAL : rc);
1475 lmv1 = info->lti_buf.lb_buf;
1476 /* The on-disk LMV EA only contains header, but the
1477 * returned LMV EA size should contain the space for
1478 * the FIDs of all shards of the striped directory. */
1479 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1480 rc = lmv_mds_md_size(
1481 le32_to_cpu(lmv1->lmv_stripe_count),
1484 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1488 RETURN(rc = rc1 != 0 ? rc1 : rc);
1491 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1495 * XXX: Only used by lfsck
1497 * lod returns default striping on the real root of the device
1498 * this is like the root stores default striping for the whole
1499 * filesystem. historically we've been using a different approach
1500 * and store it in the config.
1502 dt_root_get(env, dev->lod_child, &info->lti_fid);
1503 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1505 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1506 struct lov_user_md *lum = buf->lb_buf;
1507 struct lov_desc *desc = &dev->lod_desc;
1509 if (buf->lb_buf == NULL) {
1511 } else if (buf->lb_len >= sizeof(*lum)) {
1512 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1513 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1514 lmm_oi_set_id(&lum->lmm_oi, 0);
1515 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1516 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1517 lum->lmm_stripe_size = cpu_to_le32(
1518 desc->ld_default_stripe_size);
1519 lum->lmm_stripe_count = cpu_to_le16(
1520 desc->ld_default_stripe_count);
1521 lum->lmm_stripe_offset = cpu_to_le16(
1522 desc->ld_default_stripe_offset);
1535 * Checks that the magic of the stripe is sane.
1537 * \param[in] lod lod device
1538 * \param[in] lum a buffer storing LMV EA to verify
1540 * \retval 0 if the EA is sane
1541 * \retval negative otherwise
1543 static int lod_verify_md_striping(struct lod_device *lod,
1544 const struct lmv_user_md_v1 *lum)
1546 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1547 CERROR("%s: invalid lmv_user_md: magic = %x, "
1548 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1549 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1550 (int)le32_to_cpu(lum->lum_stripe_offset),
1551 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1559 * Initialize LMV EA for a slave.
1561 * Initialize slave's LMV EA from the master's LMV EA.
1563 * \param[in] master_lmv a buffer containing master's EA
1564 * \param[out] slave_lmv a buffer where slave's EA will be stored
1567 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1568 const struct lmv_mds_md_v1 *master_lmv)
1570 *slave_lmv = *master_lmv;
1571 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1577 * Generate LMV EA from the object passed as \a dt. The object must have
1578 * the stripes created and initialized.
1580 * \param[in] env execution environment
1581 * \param[in] dt object
1582 * \param[out] lmv_buf buffer storing generated LMV EA
1584 * \retval 0 on success
1585 * \retval negative if failed
1587 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1588 struct lu_buf *lmv_buf)
1590 struct lod_thread_info *info = lod_env_info(env);
1591 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1592 struct lod_object *lo = lod_dt_obj(dt);
1593 struct lmv_mds_md_v1 *lmm1;
1595 int type = LU_SEQ_RANGE_ANY;
1600 LASSERT(lo->ldo_dir_striped != 0);
1601 LASSERT(lo->ldo_dir_stripe_count > 0);
1602 stripe_count = lo->ldo_dir_stripe_count;
1603 /* Only store the LMV EA heahder on the disk. */
1604 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1605 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1609 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1612 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1613 memset(lmm1, 0, sizeof(*lmm1));
1614 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1615 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1616 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1617 if (lo->ldo_dir_hash_type & LMV_HASH_FLAG_MIGRATION) {
1618 lmm1->lmv_migrate_hash = cpu_to_le32(lo->ldo_dir_migrate_hash);
1619 lmm1->lmv_migrate_offset =
1620 cpu_to_le32(lo->ldo_dir_migrate_offset);
1622 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1627 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1628 lmv_buf->lb_buf = info->lti_ea_store;
1629 lmv_buf->lb_len = sizeof(*lmm1);
1635 * Create in-core represenation for a striped directory.
1637 * Parse the buffer containing LMV EA and instantiate LU objects
1638 * representing the stripe objects. The pointers to the objects are
1639 * stored in ldo_stripe field of \a lo. This function is used when
1640 * we need to access an already created object (i.e. load from a disk).
1642 * \param[in] env execution environment
1643 * \param[in] lo lod object
1644 * \param[in] buf buffer containing LMV EA
1646 * \retval 0 on success
1647 * \retval negative if failed
1649 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1650 const struct lu_buf *buf)
1652 struct lod_thread_info *info = lod_env_info(env);
1653 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1654 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1655 struct dt_object **stripe;
1656 union lmv_mds_md *lmm = buf->lb_buf;
1657 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1658 struct lu_fid *fid = &info->lti_fid;
1663 LASSERT(mutex_is_locked(&lo->ldo_layout_mutex));
1665 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1668 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1669 lo->ldo_dir_slave_stripe = 1;
1673 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1676 if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1679 LASSERT(lo->ldo_stripe == NULL);
1680 OBD_ALLOC(stripe, sizeof(stripe[0]) *
1681 (le32_to_cpu(lmv1->lmv_stripe_count)));
1685 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1686 struct dt_device *tgt_dt;
1687 struct dt_object *dto;
1688 int type = LU_SEQ_RANGE_ANY;
1691 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1692 if (!fid_is_sane(fid))
1693 GOTO(out, rc = -ESTALE);
1695 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1699 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1700 tgt_dt = lod->lod_child;
1702 struct lod_tgt_desc *tgt;
1704 tgt = LTD_TGT(ltd, idx);
1706 GOTO(out, rc = -ESTALE);
1707 tgt_dt = tgt->ltd_tgt;
1710 dto = dt_locate_at(env, tgt_dt, fid,
1711 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1714 GOTO(out, rc = PTR_ERR(dto));
1719 lo->ldo_stripe = stripe;
1720 lo->ldo_dir_stripe_count = le32_to_cpu(lmv1->lmv_stripe_count);
1721 lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1723 lod_striping_free_nolock(env, lo);
1729 * Declare create a striped directory.
1731 * Declare creating a striped directory with a given stripe pattern on the
1732 * specified MDTs. A striped directory is represented as a regular directory
1733 * - an index listing all the stripes. The stripes point back to the master
1734 * object with ".." and LinkEA. The master object gets LMV EA which
1735 * identifies it as a striped directory. The function allocates FIDs
1738 * \param[in] env execution environment
1739 * \param[in] dt object
1740 * \param[in] attr attributes to initialize the objects with
1741 * \param[in] dof type of objects to be created
1742 * \param[in] th transaction handle
1744 * \retval 0 on success
1745 * \retval negative if failed
1747 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1748 struct dt_object *dt,
1749 struct lu_attr *attr,
1750 struct dt_object_format *dof,
1753 struct lod_thread_info *info = lod_env_info(env);
1754 struct lu_buf lmv_buf;
1755 struct lu_buf slave_lmv_buf;
1756 struct lmv_mds_md_v1 *lmm;
1757 struct lmv_mds_md_v1 *slave_lmm = NULL;
1758 struct dt_insert_rec *rec = &info->lti_dt_rec;
1759 struct lod_object *lo = lod_dt_obj(dt);
1764 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1767 lmm = lmv_buf.lb_buf;
1769 OBD_ALLOC_PTR(slave_lmm);
1770 if (slave_lmm == NULL)
1771 GOTO(out, rc = -ENOMEM);
1773 lod_prep_slave_lmv_md(slave_lmm, lmm);
1774 slave_lmv_buf.lb_buf = slave_lmm;
1775 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1777 if (!dt_try_as_dir(env, dt_object_child(dt)))
1778 GOTO(out, rc = -EINVAL);
1780 rec->rec_type = S_IFDIR;
1781 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1782 struct dt_object *dto = lo->ldo_stripe[i];
1783 char *stripe_name = info->lti_key;
1784 struct lu_name *sname;
1785 struct linkea_data ldata = { NULL };
1786 struct lu_buf linkea_buf;
1788 rc = lod_sub_declare_create(env, dto, attr, NULL, dof, th);
1792 if (!dt_try_as_dir(env, dto))
1793 GOTO(out, rc = -EINVAL);
1795 rc = lod_sub_declare_ref_add(env, dto, th);
1799 rec->rec_fid = lu_object_fid(&dto->do_lu);
1800 rc = lod_sub_declare_insert(env, dto,
1801 (const struct dt_rec *)rec,
1802 (const struct dt_key *)dot, th);
1806 /* master stripe FID will be put to .. */
1807 rec->rec_fid = lu_object_fid(&dt->do_lu);
1808 rc = lod_sub_declare_insert(env, dto,
1809 (const struct dt_rec *)rec,
1810 (const struct dt_key *)dotdot, th);
1814 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1815 cfs_fail_val != i) {
1816 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1818 slave_lmm->lmv_master_mdt_index =
1821 slave_lmm->lmv_master_mdt_index =
1823 rc = lod_sub_declare_xattr_set(env, dto, &slave_lmv_buf,
1824 XATTR_NAME_LMV, 0, th);
1829 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1831 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1832 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1834 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1835 PFID(lu_object_fid(&dto->do_lu)), i);
1837 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1838 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1839 sname, lu_object_fid(&dt->do_lu));
1843 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1844 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1845 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
1846 XATTR_NAME_LINK, 0, th);
1850 rec->rec_fid = lu_object_fid(&dto->do_lu);
1851 rc = lod_sub_declare_insert(env, dt_object_child(dt),
1852 (const struct dt_rec *)rec,
1853 (const struct dt_key *)stripe_name,
1858 rc = lod_sub_declare_ref_add(env, dt_object_child(dt), th);
1863 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt),
1864 &lmv_buf, XATTR_NAME_LMV, 0, th);
1868 if (slave_lmm != NULL)
1869 OBD_FREE_PTR(slave_lmm);
1874 static int lod_prep_md_striped_create(const struct lu_env *env,
1875 struct dt_object *dt,
1876 struct lu_attr *attr,
1877 const struct lmv_user_md_v1 *lum,
1878 struct dt_object_format *dof,
1881 struct lod_thread_info *info = lod_env_info(env);
1882 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1883 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1884 struct lod_object *lo = lod_dt_obj(dt);
1885 struct dt_object **stripe;
1892 bool is_specific = false;
1895 /* The lum has been verifed in lod_verify_md_striping */
1896 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC ||
1897 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC);
1898 LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1900 stripe_count = le32_to_cpu(lum->lum_stripe_count);
1902 OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1903 if (idx_array == NULL)
1906 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1908 GOTO(out_free, rc = -ENOMEM);
1910 /* Start index must be the master MDT */
1911 master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1912 idx_array[0] = master_index;
1913 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) {
1915 for (i = 1; i < stripe_count; i++)
1916 idx_array[i] = le32_to_cpu(lum->lum_objects[i].lum_mds);
1919 for (i = 0; i < stripe_count; i++) {
1920 struct lod_tgt_desc *tgt = NULL;
1921 struct dt_object *dto;
1922 struct lu_fid fid = { 0 };
1924 struct lu_object_conf conf = { 0 };
1925 struct dt_device *tgt_dt = NULL;
1927 /* Try to find next avaible target */
1929 for (j = 0; j < lod->lod_remote_mdt_count;
1930 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1931 bool already_allocated = false;
1934 CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
1935 idx, lod->lod_remote_mdt_count + 1, i);
1937 if (likely(!is_specific &&
1938 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
1939 /* check whether the idx already exists
1940 * in current allocated array */
1941 for (k = 0; k < i; k++) {
1942 if (idx_array[k] == idx) {
1943 already_allocated = true;
1948 if (already_allocated)
1952 /* Sigh, this index is not in the bitmap, let's check
1953 * next available target */
1954 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) &&
1955 idx != master_index)
1958 if (idx == master_index) {
1959 /* Allocate the FID locally */
1960 rc = obd_fid_alloc(env, lod->lod_child_exp,
1964 tgt_dt = lod->lod_child;
1968 /* check the status of the OSP */
1969 tgt = LTD_TGT(ltd, idx);
1973 tgt_dt = tgt->ltd_tgt;
1974 rc = dt_statfs(env, tgt_dt, &info->lti_osfs);
1976 /* this OSP doesn't feel well */
1981 rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1990 /* Can not allocate more stripes */
1991 if (j == lod->lod_remote_mdt_count) {
1992 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1993 lod2obd(lod)->obd_name, stripe_count, i);
1997 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
1998 idx, i, PFID(&fid));
2000 /* Set the start index for next stripe allocation */
2001 if (!is_specific && i < stripe_count - 1) {
2003 * for large dir test, put all other slaves on one
2004 * remote MDT, otherwise we may save too many local
2005 * slave locks which will exceed RS_MAX_LOCKS.
2007 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)))
2009 idx_array[i + 1] = (idx + 1) %
2010 (lod->lod_remote_mdt_count + 1);
2012 /* tgt_dt and fid must be ready after search avaible OSP
2013 * in the above loop */
2014 LASSERT(tgt_dt != NULL);
2015 LASSERT(fid_is_sane(&fid));
2016 conf.loc_flags = LOC_F_NEW;
2017 dto = dt_locate_at(env, tgt_dt, &fid,
2018 dt->do_lu.lo_dev->ld_site->ls_top_dev,
2021 GOTO(out_put, rc = PTR_ERR(dto));
2025 lo->ldo_dir_striped = 1;
2026 lo->ldo_stripe = stripe;
2027 lo->ldo_dir_stripe_count = i;
2028 lo->ldo_dir_stripes_allocated = stripe_count;
2030 lo->ldo_dir_stripe_loaded = 1;
2032 if (lo->ldo_dir_stripe_count == 0)
2033 GOTO(out_put, rc = -ENOSPC);
2035 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
2041 for (i = 0; i < stripe_count; i++)
2042 if (stripe[i] != NULL)
2043 dt_object_put(env, stripe[i]);
2044 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
2045 lo->ldo_dir_stripe_count = 0;
2046 lo->ldo_dir_stripes_allocated = 0;
2047 lo->ldo_stripe = NULL;
2051 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
2057 * Declare create striped md object.
2059 * The function declares intention to create a striped directory. This is a
2060 * wrapper for lod_prep_md_striped_create(). The only additional functionality
2061 * is to verify pattern \a lum_buf is good. Check that function for the details.
2063 * \param[in] env execution environment
2064 * \param[in] dt object
2065 * \param[in] attr attributes to initialize the objects with
2066 * \param[in] lum_buf a pattern specifying the number of stripes and
2068 * \param[in] dof type of objects to be created
2069 * \param[in] th transaction handle
2071 * \retval 0 on success
2072 * \retval negative if failed
2075 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
2076 struct dt_object *dt,
2077 struct lu_attr *attr,
2078 const struct lu_buf *lum_buf,
2079 struct dt_object_format *dof,
2082 struct lod_object *lo = lod_dt_obj(dt);
2083 struct lmv_user_md_v1 *lum = lum_buf->lb_buf;
2087 LASSERT(lum != NULL);
2089 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
2090 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
2091 (int)le32_to_cpu(lum->lum_stripe_offset));
2093 if (lo->ldo_dir_stripe_count == 0)
2096 /* prepare dir striped objects */
2097 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2099 /* failed to create striping, let's reset
2100 * config so that others don't get confused */
2101 lod_striping_free(env, lo);
2109 * Append source stripes after target stripes for migrating directory. NB, we
2110 * only need to declare this, the append is done inside lod_xattr_set_lmv().
2112 * \param[in] env execution environment
2113 * \param[in] dt target object
2114 * \param[in] buf LMV buf which contains source stripe fids
2115 * \param[in] th transaction handle
2117 * \retval 0 on success
2118 * \retval negative if failed
2120 static int lod_dir_declare_layout_add(const struct lu_env *env,
2121 struct dt_object *dt,
2122 const struct lu_buf *buf,
2125 struct lod_thread_info *info = lod_env_info(env);
2126 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
2127 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
2128 struct lod_object *lo = lod_dt_obj(dt);
2129 struct dt_object *next = dt_object_child(dt);
2130 struct dt_object_format *dof = &info->lti_format;
2131 struct lmv_mds_md_v1 *lmv = buf->lb_buf;
2132 struct dt_object **stripe;
2133 __u32 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
2134 struct lu_fid *fid = &info->lti_fid;
2135 struct lod_tgt_desc *tgt;
2136 struct dt_object *dto;
2137 struct dt_device *tgt_dt;
2138 int type = LU_SEQ_RANGE_ANY;
2139 struct dt_insert_rec *rec = &info->lti_dt_rec;
2140 char *stripe_name = info->lti_key;
2141 struct lu_name *sname;
2142 struct linkea_data ldata = { NULL };
2143 struct lu_buf linkea_buf;
2150 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
2153 if (stripe_count == 0)
2156 dof->dof_type = DFT_DIR;
2159 sizeof(*stripe) * (lo->ldo_dir_stripe_count + stripe_count));
2163 for (i = 0; i < lo->ldo_dir_stripe_count; i++)
2164 stripe[i] = lo->ldo_stripe[i];
2166 for (i = 0; i < stripe_count; i++) {
2168 &lmv->lmv_stripe_fids[i]);
2169 if (!fid_is_sane(fid))
2170 GOTO(out, rc = -ESTALE);
2172 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
2176 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
2177 tgt_dt = lod->lod_child;
2179 tgt = LTD_TGT(ltd, idx);
2181 GOTO(out, rc = -ESTALE);
2182 tgt_dt = tgt->ltd_tgt;
2185 dto = dt_locate_at(env, tgt_dt, fid,
2186 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
2189 GOTO(out, rc = PTR_ERR(dto));
2191 stripe[i + lo->ldo_dir_stripe_count] = dto;
2193 if (!dt_try_as_dir(env, dto))
2194 GOTO(out, rc = -ENOTDIR);
2196 rc = lod_sub_declare_ref_add(env, dto, th);
2200 rc = lod_sub_declare_insert(env, dto,
2201 (const struct dt_rec *)rec,
2202 (const struct dt_key *)dot, th);
2206 rc = lod_sub_declare_insert(env, dto,
2207 (const struct dt_rec *)rec,
2208 (const struct dt_key *)dotdot, th);
2212 rc = lod_sub_declare_xattr_set(env, dto, buf,
2213 XATTR_NAME_LMV, 0, th);
2217 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
2218 PFID(lu_object_fid(&dto->do_lu)),
2219 i + lo->ldo_dir_stripe_count);
2221 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2222 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
2223 sname, lu_object_fid(&dt->do_lu));
2227 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
2228 linkea_buf.lb_len = ldata.ld_leh->leh_len;
2229 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
2230 XATTR_NAME_LINK, 0, th);
2234 rc = lod_sub_declare_insert(env, next,
2235 (const struct dt_rec *)rec,
2236 (const struct dt_key *)stripe_name,
2241 rc = lod_sub_declare_ref_add(env, next, th);
2247 OBD_FREE(lo->ldo_stripe,
2248 sizeof(*stripe) * lo->ldo_dir_stripes_allocated);
2249 lo->ldo_stripe = stripe;
2250 lo->ldo_dir_migrate_offset = lo->ldo_dir_stripe_count;
2251 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_hash_type);
2252 lo->ldo_dir_stripe_count += stripe_count;
2253 lo->ldo_dir_stripes_allocated += stripe_count;
2254 lo->ldo_dir_hash_type |= LMV_HASH_FLAG_MIGRATION;
2258 i = lo->ldo_dir_stripe_count;
2259 while (i < lo->ldo_dir_stripe_count + stripe_count && stripe[i])
2260 dt_object_put(env, stripe[i++]);
2263 sizeof(*stripe) * (stripe_count + lo->ldo_dir_stripe_count));
2267 static int lod_dir_declare_layout_delete(const struct lu_env *env,
2268 struct dt_object *dt,
2269 const struct lu_buf *buf,
2272 struct lod_thread_info *info = lod_env_info(env);
2273 struct lod_object *lo = lod_dt_obj(dt);
2274 struct dt_object *next = dt_object_child(dt);
2275 struct lmv_user_md *lmu = buf->lb_buf;
2276 __u32 final_stripe_count;
2277 char *stripe_name = info->lti_key;
2278 struct dt_object *dto;
2285 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
2286 if (final_stripe_count >= lo->ldo_dir_stripe_count)
2289 for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
2290 dto = lo->ldo_stripe[i];
2293 if (!dt_try_as_dir(env, dto))
2296 rc = lod_sub_declare_delete(env, dto,
2297 (const struct dt_key *)dot, th);
2301 rc = lod_sub_declare_ref_del(env, dto, th);
2305 rc = lod_sub_declare_delete(env, dto,
2306 (const struct dt_key *)dotdot, th);
2310 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2311 PFID(lu_object_fid(&dto->do_lu)), i);
2313 rc = lod_sub_declare_delete(env, next,
2314 (const struct dt_key *)stripe_name, th);
2318 rc = lod_sub_declare_ref_del(env, next, th);
2327 * delete stripes from dir master object, the lum_stripe_count in argument is
2328 * the final stripe count, the stripes after that will be deleted, NB, they
2329 * are not destroyed, but deleted from it's parent namespace, this function
2330 * will be called in two places:
2331 * 1. mdd_migrate_create() delete stripes from source, and append them to
2333 * 2. mdd_dir_layout_shrink() delete stripes from source, and destroy them.
2335 static int lod_dir_layout_delete(const struct lu_env *env,
2336 struct dt_object *dt,
2337 const struct lu_buf *buf,
2340 struct lod_thread_info *info = lod_env_info(env);
2341 struct lod_object *lo = lod_dt_obj(dt);
2342 struct dt_object *next = dt_object_child(dt);
2343 struct lmv_user_md *lmu = buf->lb_buf;
2344 __u32 final_stripe_count;
2345 char *stripe_name = info->lti_key;
2346 struct dt_object *dto;
2355 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
2356 if (final_stripe_count >= lo->ldo_dir_stripe_count)
2359 for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
2360 dto = lo->ldo_stripe[i];
2363 rc = lod_sub_delete(env, dto,
2364 (const struct dt_key *)dotdot, th);
2368 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2369 PFID(lu_object_fid(&dto->do_lu)), i);
2371 rc = lod_sub_delete(env, next,
2372 (const struct dt_key *)stripe_name, th);
2376 rc = lod_sub_ref_del(env, next, th);
2381 lod_striping_free(env, lod_dt_obj(dt));
2387 * Implementation of dt_object_operations::do_declare_xattr_set.
2389 * Used with regular (non-striped) objects. Basically it
2390 * initializes the striping information and applies the
2391 * change to all the stripes.
2393 * \see dt_object_operations::do_declare_xattr_set() in the API description
2396 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2397 struct dt_object *dt,
2398 const struct lu_buf *buf,
2399 const char *name, int fl,
2402 struct dt_object *next = dt_object_child(dt);
2403 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2404 struct lod_object *lo = lod_dt_obj(dt);
2409 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2410 struct lmv_user_md_v1 *lum;
2412 LASSERT(buf != NULL && buf->lb_buf != NULL);
2414 rc = lod_verify_md_striping(d, lum);
2417 } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2418 rc = lod_verify_striping(d, lo, buf, false);
2423 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2427 /* Note: Do not set LinkEA on sub-stripes, otherwise
2428 * it will confuse the fid2path process(see mdt_path_current()).
2429 * The linkEA between master and sub-stripes is set in
2430 * lod_xattr_set_lmv(). */
2431 if (strcmp(name, XATTR_NAME_LINK) == 0)
2434 /* set xattr to each stripes, if needed */
2435 rc = lod_striping_load(env, lo);
2439 if (lo->ldo_dir_stripe_count == 0)
2442 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2443 LASSERT(lo->ldo_stripe[i]);
2445 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2455 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2456 struct lod_object *lo,
2457 struct dt_object *dt, struct thandle *th,
2458 int comp_idx, int stripe_idx,
2459 struct lod_obj_stripe_cb_data *data)
2461 struct lod_thread_info *info = lod_env_info(env);
2462 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
2463 struct filter_fid *ff = &info->lti_ff;
2464 struct lu_buf *buf = &info->lti_buf;
2468 buf->lb_len = sizeof(*ff);
2469 rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2476 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
2479 * mdd_declare_migrate_create() declares this via source object because
2480 * target is not ready yet, so declare anyway.
2482 if (!data->locd_declare &&
2483 lu_fid_eq(lu_object_fid(&lo->ldo_obj.do_lu), &ff->ff_parent) &&
2484 ff->ff_layout.ol_comp_id == comp->llc_id)
2487 /* rewrite filter_fid */
2488 memset(ff, 0, sizeof(*ff));
2489 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2490 ff->ff_parent.f_ver = stripe_idx;
2491 ff->ff_layout.ol_stripe_size = comp->llc_stripe_size;
2492 ff->ff_layout.ol_stripe_count = comp->llc_stripe_count;
2493 ff->ff_layout.ol_comp_id = comp->llc_id;
2494 ff->ff_layout.ol_comp_start = comp->llc_extent.e_start;
2495 ff->ff_layout.ol_comp_end = comp->llc_extent.e_end;
2496 filter_fid_cpu_to_le(ff, ff, sizeof(*ff));
2498 if (data->locd_declare)
2499 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2500 LU_XATTR_REPLACE, th);
2502 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2503 LU_XATTR_REPLACE, th);
2509 * Reset parent FID on OST object
2511 * Replace parent FID with @dt object FID, which is only called during migration
2512 * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2513 * the FID is changed.
2515 * \param[in] env execution environment
2516 * \param[in] dt dt_object whose stripes's parent FID will be reset
2517 * \parem[in] th thandle
2518 * \param[in] declare if it is declare
2520 * \retval 0 if reset succeeds
2521 * \retval negative errno if reset fails
2523 static int lod_replace_parent_fid(const struct lu_env *env,
2524 struct dt_object *dt,
2525 struct thandle *th, bool declare)
2527 struct lod_object *lo = lod_dt_obj(dt);
2528 struct lod_thread_info *info = lod_env_info(env);
2529 struct lu_buf *buf = &info->lti_buf;
2530 struct filter_fid *ff;
2531 struct lod_obj_stripe_cb_data data = { { 0 } };
2535 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2537 /* set xattr to each stripes, if needed */
2538 rc = lod_striping_load(env, lo);
2542 if (!lod_obj_is_striped(dt))
2545 if (info->lti_ea_store_size < sizeof(*ff)) {
2546 rc = lod_ea_store_resize(info, sizeof(*ff));
2551 buf->lb_buf = info->lti_ea_store;
2552 buf->lb_len = info->lti_ea_store_size;
2554 data.locd_declare = declare;
2555 data.locd_stripe_cb = lod_obj_stripe_replace_parent_fid_cb;
2556 rc = lod_obj_for_each_stripe(env, lo, th, &data);
2561 inline __u16 lod_comp_entry_stripe_count(struct lod_object *lo,
2562 struct lod_layout_component *entry,
2565 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2569 else if (lod_comp_inited(entry))
2570 return entry->llc_stripe_count;
2571 else if ((__u16)-1 == entry->llc_stripe_count)
2572 return lod->lod_desc.ld_tgt_count;
2574 return lod_get_stripe_count(lod, lo, entry->llc_stripe_count);
2577 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2579 int magic, size = 0, i;
2580 struct lod_layout_component *comp_entries;
2585 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2586 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2588 lo->ldo_def_striping->lds_def_striping_is_composite;
2590 comp_cnt = lo->ldo_comp_cnt;
2591 comp_entries = lo->ldo_comp_entries;
2592 is_composite = lo->ldo_is_composite;
2596 LASSERT(comp_cnt != 0 && comp_entries != NULL);
2598 size = sizeof(struct lov_comp_md_v1) +
2599 sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2600 LASSERT(size % sizeof(__u64) == 0);
2603 for (i = 0; i < comp_cnt; i++) {
2606 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2607 stripe_count = lod_comp_entry_stripe_count(lo, &comp_entries[i],
2609 if (!is_dir && is_composite)
2610 lod_comp_shrink_stripe_count(&comp_entries[i],
2613 size += lov_user_md_size(stripe_count, magic);
2614 LASSERT(size % sizeof(__u64) == 0);
2620 * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2621 * the xattr value is binary lov_comp_md_v1 which contains component(s)
2624 * \param[in] env execution environment
2625 * \param[in] dt dt_object to add components on
2626 * \param[in] buf buffer contains components to be added
2627 * \parem[in] th thandle
2629 * \retval 0 on success
2630 * \retval negative errno on failure
2632 static int lod_declare_layout_add(const struct lu_env *env,
2633 struct dt_object *dt,
2634 const struct lu_buf *buf,
2637 struct lod_thread_info *info = lod_env_info(env);
2638 struct lod_layout_component *comp_array, *lod_comp, *old_array;
2639 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2640 struct dt_object *next = dt_object_child(dt);
2641 struct lov_desc *desc = &d->lod_desc;
2642 struct lod_object *lo = lod_dt_obj(dt);
2643 struct lov_user_md_v3 *v3;
2644 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2646 int i, rc, array_cnt, old_array_cnt;
2649 LASSERT(lo->ldo_is_composite);
2651 if (lo->ldo_flr_state != LCM_FL_NONE)
2654 rc = lod_verify_striping(d, lo, buf, false);
2658 magic = comp_v1->lcm_magic;
2659 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2660 lustre_swab_lov_comp_md_v1(comp_v1);
2661 magic = comp_v1->lcm_magic;
2664 if (magic != LOV_USER_MAGIC_COMP_V1)
2667 array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2668 OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2669 if (comp_array == NULL)
2672 memcpy(comp_array, lo->ldo_comp_entries,
2673 sizeof(*comp_array) * lo->ldo_comp_cnt);
2675 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2676 struct lov_user_md_v1 *v1;
2677 struct lu_extent *ext;
2679 v1 = (struct lov_user_md *)((char *)comp_v1 +
2680 comp_v1->lcm_entries[i].lcme_offset);
2681 ext = &comp_v1->lcm_entries[i].lcme_extent;
2683 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2684 lod_comp->llc_extent.e_start = ext->e_start;
2685 lod_comp->llc_extent.e_end = ext->e_end;
2686 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2687 lod_comp->llc_flags = comp_v1->lcm_entries[i].lcme_flags;
2689 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2690 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2691 lod_adjust_stripe_info(lod_comp, desc);
2693 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2694 v3 = (struct lov_user_md_v3 *) v1;
2695 if (v3->lmm_pool_name[0] != '\0') {
2696 rc = lod_set_pool(&lod_comp->llc_pool,
2704 old_array = lo->ldo_comp_entries;
2705 old_array_cnt = lo->ldo_comp_cnt;
2707 lo->ldo_comp_entries = comp_array;
2708 lo->ldo_comp_cnt = array_cnt;
2710 /* No need to increase layout generation here, it will be increased
2711 * later when generating component ID for the new components */
2713 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2714 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2715 XATTR_NAME_LOV, 0, th);
2717 lo->ldo_comp_entries = old_array;
2718 lo->ldo_comp_cnt = old_array_cnt;
2722 OBD_FREE(old_array, sizeof(*lod_comp) * old_array_cnt);
2724 LASSERT(lo->ldo_mirror_count == 1);
2725 lo->ldo_mirrors[0].lme_end = array_cnt - 1;
2730 for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2731 lod_comp = &comp_array[i];
2732 if (lod_comp->llc_pool != NULL) {
2733 OBD_FREE(lod_comp->llc_pool,
2734 strlen(lod_comp->llc_pool) + 1);
2735 lod_comp->llc_pool = NULL;
2738 OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2743 * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2744 * the '$field' can only be 'flags' now. The xattr value is binary
2745 * lov_comp_md_v1 which contains the component ID(s) and the value of
2746 * the field to be modified.
2748 * \param[in] env execution environment
2749 * \param[in] dt dt_object to be modified
2750 * \param[in] op operation string, like "set.flags"
2751 * \param[in] buf buffer contains components to be set
2752 * \parem[in] th thandle
2754 * \retval 0 on success
2755 * \retval negative errno on failure
2757 static int lod_declare_layout_set(const struct lu_env *env,
2758 struct dt_object *dt,
2759 char *op, const struct lu_buf *buf,
2762 struct lod_layout_component *lod_comp;
2763 struct lod_thread_info *info = lod_env_info(env);
2764 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2765 struct lod_object *lo = lod_dt_obj(dt);
2766 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2769 bool changed = false;
2772 if (strcmp(op, "set.flags") != 0) {
2773 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2774 lod2obd(d)->obd_name, op);
2778 magic = comp_v1->lcm_magic;
2779 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2780 lustre_swab_lov_comp_md_v1(comp_v1);
2781 magic = comp_v1->lcm_magic;
2784 if (magic != LOV_USER_MAGIC_COMP_V1)
2787 if (comp_v1->lcm_entry_count == 0) {
2788 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2789 lod2obd(d)->obd_name);
2793 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2794 __u32 id = comp_v1->lcm_entries[i].lcme_id;
2795 __u32 flags = comp_v1->lcm_entries[i].lcme_flags;
2797 if (flags & LCME_FL_INIT) {
2799 lod_striping_free(env, lo);
2803 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2804 lod_comp = &lo->ldo_comp_entries[j];
2805 if (id != lod_comp->llc_id)
2808 if (flags & LCME_FL_NEG) {
2809 flags &= ~LCME_FL_NEG;
2810 lod_comp->llc_flags &= ~flags;
2812 lod_comp->llc_flags |= flags;
2819 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2820 lod2obd(d)->obd_name);
2824 lod_obj_inc_layout_gen(lo);
2826 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2827 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), &info->lti_buf,
2828 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
2833 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2834 * and the xattr value is a unique component ID or a special lcme_id.
2836 * \param[in] env execution environment
2837 * \param[in] dt dt_object to be operated on
2838 * \param[in] buf buffer contains component ID or lcme_id
2839 * \parem[in] th thandle
2841 * \retval 0 on success
2842 * \retval negative errno on failure
2844 static int lod_declare_layout_del(const struct lu_env *env,
2845 struct dt_object *dt,
2846 const struct lu_buf *buf,
2849 struct lod_thread_info *info = lod_env_info(env);
2850 struct dt_object *next = dt_object_child(dt);
2851 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2852 struct lod_object *lo = lod_dt_obj(dt);
2853 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2854 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2855 __u32 magic, id, flags, neg_flags = 0;
2859 LASSERT(lo->ldo_is_composite);
2861 if (lo->ldo_flr_state != LCM_FL_NONE)
2864 magic = comp_v1->lcm_magic;
2865 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2866 lustre_swab_lov_comp_md_v1(comp_v1);
2867 magic = comp_v1->lcm_magic;
2870 if (magic != LOV_USER_MAGIC_COMP_V1)
2873 id = comp_v1->lcm_entries[0].lcme_id;
2874 flags = comp_v1->lcm_entries[0].lcme_flags;
2876 if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
2877 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
2878 lod2obd(d)->obd_name, id, flags);
2882 if (id != LCME_ID_INVAL && flags != 0) {
2883 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
2884 lod2obd(d)->obd_name);
2888 if (id == LCME_ID_INVAL && !flags) {
2889 CDEBUG(D_LAYOUT, "%s: no id or flags specified.\n",
2890 lod2obd(d)->obd_name);
2894 if (flags & LCME_FL_NEG) {
2895 neg_flags = flags & ~LCME_FL_NEG;
2899 left = lo->ldo_comp_cnt;
2903 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
2904 struct lod_layout_component *lod_comp;
2906 lod_comp = &lo->ldo_comp_entries[i];
2908 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
2910 else if (flags && !(flags & lod_comp->llc_flags))
2912 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
2915 if (left != (i + 1)) {
2916 CDEBUG(D_LAYOUT, "%s: this deletion will create "
2917 "a hole.\n", lod2obd(d)->obd_name);
2922 /* Mark the component as deleted */
2923 lod_comp->llc_id = LCME_ID_INVAL;
2925 /* Not instantiated component */
2926 if (lod_comp->llc_stripe == NULL)
2929 LASSERT(lod_comp->llc_stripe_count > 0);
2930 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
2931 struct dt_object *obj = lod_comp->llc_stripe[j];
2935 rc = lod_sub_declare_destroy(env, obj, th);
2941 LASSERTF(left >= 0, "left = %d\n", left);
2942 if (left == lo->ldo_comp_cnt) {
2943 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
2944 lod2obd(d)->obd_name, id);
2948 memset(attr, 0, sizeof(*attr));
2949 attr->la_valid = LA_SIZE;
2950 rc = lod_sub_declare_attr_set(env, next, attr, th);
2955 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2956 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2957 XATTR_NAME_LOV, 0, th);
2959 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
2966 * Declare layout add/set/del operations issued by special xattr names:
2968 * XATTR_LUSTRE_LOV.add add component(s) to existing file
2969 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
2970 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
2972 * \param[in] env execution environment
2973 * \param[in] dt object
2974 * \param[in] name name of xattr
2975 * \param[in] buf lu_buf contains xattr value
2976 * \param[in] th transaction handle
2978 * \retval 0 on success
2979 * \retval negative if failed
2981 static int lod_declare_modify_layout(const struct lu_env *env,
2982 struct dt_object *dt,
2984 const struct lu_buf *buf,
2987 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2988 struct lod_object *lo = lod_dt_obj(dt);
2990 int rc, len = strlen(XATTR_LUSTRE_LOV);
2993 LASSERT(dt_object_exists(dt));
2995 if (strlen(name) <= len || name[len] != '.') {
2996 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
2997 lod2obd(d)->obd_name, name);
3002 rc = lod_striping_load(env, lo);
3006 /* the layout to be modified must be a composite layout */
3007 if (!lo->ldo_is_composite) {
3008 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
3009 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3010 GOTO(unlock, rc = -EINVAL);
3013 op = (char *)name + len;
3014 if (strcmp(op, "add") == 0) {
3015 rc = lod_declare_layout_add(env, dt, buf, th);
3016 } else if (strcmp(op, "del") == 0) {
3017 rc = lod_declare_layout_del(env, dt, buf, th);
3018 } else if (strncmp(op, "set", strlen("set")) == 0) {
3019 rc = lod_declare_layout_set(env, dt, op, buf, th);
3021 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
3022 lod2obd(d)->obd_name, name);
3023 GOTO(unlock, rc = -ENOTSUPP);
3027 lod_striping_free(env, lo);
3033 * Convert a plain file lov_mds_md to a composite layout.
3035 * \param[in,out] info the thread info::lti_ea_store buffer contains little
3036 * endian plain file layout
3038 * \retval 0 on success, <0 on failure
3040 static int lod_layout_convert(struct lod_thread_info *info)
3042 struct lov_mds_md *lmm = info->lti_ea_store;
3043 struct lov_mds_md *lmm_save;
3044 struct lov_comp_md_v1 *lcm;
3045 struct lov_comp_md_entry_v1 *lcme;
3051 /* realloc buffer to a composite layout which contains one component */
3052 blob_size = lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
3053 le32_to_cpu(lmm->lmm_magic));
3054 size = sizeof(*lcm) + sizeof(*lcme) + blob_size;
3056 OBD_ALLOC_LARGE(lmm_save, blob_size);
3058 GOTO(out, rc = -ENOMEM);
3060 memcpy(lmm_save, lmm, blob_size);
3062 if (info->lti_ea_store_size < size) {
3063 rc = lod_ea_store_resize(info, size);
3068 lcm = info->lti_ea_store;
3069 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
3070 lcm->lcm_size = cpu_to_le32(size);
3071 lcm->lcm_layout_gen = cpu_to_le32(le16_to_cpu(
3072 lmm_save->lmm_layout_gen));
3073 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
3074 lcm->lcm_entry_count = cpu_to_le16(1);
3075 lcm->lcm_mirror_count = 0;
3077 lcme = &lcm->lcm_entries[0];
3078 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
3079 lcme->lcme_extent.e_start = 0;
3080 lcme->lcme_extent.e_end = cpu_to_le64(OBD_OBJECT_EOF);
3081 lcme->lcme_offset = cpu_to_le32(sizeof(*lcm) + sizeof(*lcme));
3082 lcme->lcme_size = cpu_to_le32(blob_size);
3084 memcpy((char *)lcm + lcme->lcme_offset, (char *)lmm_save, blob_size);
3089 OBD_FREE_LARGE(lmm_save, blob_size);
3094 * Merge layouts to form a mirrored file.
3096 static int lod_declare_layout_merge(const struct lu_env *env,
3097 struct dt_object *dt, const struct lu_buf *mbuf,
3100 struct lod_thread_info *info = lod_env_info(env);
3101 struct lu_buf *buf = &info->lti_buf;
3102 struct lod_object *lo = lod_dt_obj(dt);
3103 struct lov_comp_md_v1 *lcm;
3104 struct lov_comp_md_v1 *cur_lcm;
3105 struct lov_comp_md_v1 *merge_lcm;
3106 struct lov_comp_md_entry_v1 *lcme;
3109 __u16 cur_entry_count;
3110 __u16 merge_entry_count;
3112 __u16 mirror_id = 0;
3117 merge_lcm = mbuf->lb_buf;
3118 if (mbuf->lb_len < sizeof(*merge_lcm))
3121 /* must be an existing layout from disk */
3122 if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
3125 merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
3127 /* do not allow to merge two mirrored files */
3128 if (le16_to_cpu(merge_lcm->lcm_mirror_count))
3131 /* verify the target buffer */
3132 rc = lod_get_lov_ea(env, lo);
3134 RETURN(rc ? : -ENODATA);
3136 cur_lcm = info->lti_ea_store;
3137 switch (le32_to_cpu(cur_lcm->lcm_magic)) {
3140 rc = lod_layout_convert(info);
3142 case LOV_MAGIC_COMP_V1:
3151 /* info->lti_ea_store could be reallocated in lod_layout_convert() */
3152 cur_lcm = info->lti_ea_store;
3153 cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
3155 /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
3156 mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
3157 if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
3160 /* size of new layout */
3161 size = le32_to_cpu(cur_lcm->lcm_size) +
3162 le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
3164 memset(buf, 0, sizeof(*buf));
3165 lu_buf_alloc(buf, size);
3166 if (buf->lb_buf == NULL)
3170 memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
3172 offset = sizeof(*lcm) +
3173 sizeof(*lcme) * (cur_entry_count + merge_entry_count);
3174 for (i = 0; i < cur_entry_count; i++) {
3175 struct lov_comp_md_entry_v1 *cur_lcme;
3177 lcme = &lcm->lcm_entries[i];
3178 cur_lcme = &cur_lcm->lcm_entries[i];
3180 lcme->lcme_offset = cpu_to_le32(offset);
3181 memcpy((char *)lcm + offset,
3182 (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
3183 le32_to_cpu(lcme->lcme_size));
3185 offset += le32_to_cpu(lcme->lcme_size);
3187 if (mirror_count == 1 &&
3188 mirror_id_of(le32_to_cpu(lcme->lcme_id)) == 0) {
3189 /* Add mirror from a non-flr file, create new mirror ID.
3190 * Otherwise, keep existing mirror's component ID, used
3191 * for mirror extension.
3193 id = pflr_id(1, i + 1);
3194 lcme->lcme_id = cpu_to_le32(id);
3197 id = MAX(le32_to_cpu(lcme->lcme_id), id);
3200 mirror_id = mirror_id_of(id) + 1;
3201 for (i = 0; i < merge_entry_count; i++) {
3202 struct lov_comp_md_entry_v1 *merge_lcme;
3204 merge_lcme = &merge_lcm->lcm_entries[i];
3205 lcme = &lcm->lcm_entries[cur_entry_count + i];
3207 *lcme = *merge_lcme;
3208 lcme->lcme_offset = cpu_to_le32(offset);
3210 id = pflr_id(mirror_id, i + 1);
3211 lcme->lcme_id = cpu_to_le32(id);
3213 memcpy((char *)lcm + offset,
3214 (char *)merge_lcm + le32_to_cpu(merge_lcme->lcme_offset),
3215 le32_to_cpu(lcme->lcme_size));
3217 offset += le32_to_cpu(lcme->lcme_size);
3220 /* fixup layout information */
3221 lod_obj_inc_layout_gen(lo);
3222 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3223 lcm->lcm_size = cpu_to_le32(size);
3224 lcm->lcm_entry_count = cpu_to_le16(cur_entry_count + merge_entry_count);
3225 lcm->lcm_mirror_count = cpu_to_le16(mirror_count);
3226 if ((le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK) == LCM_FL_NONE)
3227 lcm->lcm_flags = cpu_to_le32(LCM_FL_RDONLY);
3229 rc = lod_striping_reload(env, lo, buf);
3233 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), buf,
3234 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3242 * Split layouts, just set the LOVEA with the layout from mbuf.
3244 static int lod_declare_layout_split(const struct lu_env *env,
3245 struct dt_object *dt, const struct lu_buf *mbuf,
3248 struct lod_object *lo = lod_dt_obj(dt);
3249 struct lov_comp_md_v1 *lcm = mbuf->lb_buf;
3253 lod_obj_inc_layout_gen(lo);
3254 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3256 rc = lod_striping_reload(env, lo, mbuf);
3260 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), mbuf,
3261 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3266 * Implementation of dt_object_operations::do_declare_xattr_set.
3268 * \see dt_object_operations::do_declare_xattr_set() in the API description
3271 * the extension to the API:
3272 * - declaring LOVEA requests striping creation
3273 * - LU_XATTR_REPLACE means layout swap
3275 static int lod_declare_xattr_set(const struct lu_env *env,
3276 struct dt_object *dt,
3277 const struct lu_buf *buf,
3278 const char *name, int fl,
3281 struct dt_object *next = dt_object_child(dt);
3282 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3287 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
3288 if ((S_ISREG(mode) || mode == 0) &&
3289 !(fl & (LU_XATTR_REPLACE | LU_XATTR_MERGE | LU_XATTR_SPLIT)) &&
3290 (strcmp(name, XATTR_NAME_LOV) == 0 ||
3291 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
3293 * this is a request to create object's striping.
3295 * allow to declare predefined striping on a new (!mode) object
3296 * which is supposed to be replay of regular file creation
3297 * (when LOV setting is declared)
3299 * LU_XATTR_REPLACE is set to indicate a layout swap
3301 if (dt_object_exists(dt)) {
3302 rc = dt_attr_get(env, next, attr);
3306 memset(attr, 0, sizeof(*attr));
3307 attr->la_valid = LA_TYPE | LA_MODE;
3308 attr->la_mode = S_IFREG;
3310 rc = lod_declare_striped_create(env, dt, attr, buf, th);
3311 } else if (fl & LU_XATTR_MERGE) {
3312 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3313 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3314 rc = lod_declare_layout_merge(env, dt, buf, th);
3315 } else if (fl & LU_XATTR_SPLIT) {
3316 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3317 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3318 rc = lod_declare_layout_split(env, dt, buf, th);
3319 } else if (S_ISREG(mode) &&
3320 strlen(name) > strlen(XATTR_LUSTRE_LOV) + 1 &&
3321 strncmp(name, XATTR_LUSTRE_LOV,
3322 strlen(XATTR_LUSTRE_LOV)) == 0) {
3324 * this is a request to modify object's striping.
3325 * add/set/del component(s).
3327 if (!dt_object_exists(dt))
3330 rc = lod_declare_modify_layout(env, dt, name, buf, th);
3331 } else if (strncmp(name, XATTR_NAME_LMV, strlen(XATTR_NAME_LMV)) == 0 &&
3332 strlen(name) > strlen(XATTR_NAME_LMV) + 1) {
3333 const char *op = name + strlen(XATTR_NAME_LMV) + 1;
3336 if (strcmp(op, "add") == 0)
3337 rc = lod_dir_declare_layout_add(env, dt, buf, th);
3338 else if (strcmp(op, "del") == 0)
3339 rc = lod_dir_declare_layout_delete(env, dt, buf, th);
3342 } else if (S_ISDIR(mode)) {
3343 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
3344 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3345 rc = lod_replace_parent_fid(env, dt, th, true);
3347 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
3354 * Apply xattr changes to the object.
3356 * Applies xattr changes to the object and the stripes if the latter exist.
3358 * \param[in] env execution environment
3359 * \param[in] dt object
3360 * \param[in] buf buffer pointing to the new value of xattr
3361 * \param[in] name name of xattr
3362 * \param[in] fl flags
3363 * \param[in] th transaction handle
3365 * \retval 0 on success
3366 * \retval negative if failed
3368 static int lod_xattr_set_internal(const struct lu_env *env,
3369 struct dt_object *dt,
3370 const struct lu_buf *buf,
3371 const char *name, int fl,
3374 struct dt_object *next = dt_object_child(dt);
3375 struct lod_object *lo = lod_dt_obj(dt);
3380 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3381 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3384 /* Note: Do not set LinkEA on sub-stripes, otherwise
3385 * it will confuse the fid2path process(see mdt_path_current()).
3386 * The linkEA between master and sub-stripes is set in
3387 * lod_xattr_set_lmv(). */
3388 if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
3391 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3392 LASSERT(lo->ldo_stripe[i]);
3394 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
3404 * Delete an extended attribute.
3406 * Deletes specified xattr from the object and the stripes if the latter exist.
3408 * \param[in] env execution environment
3409 * \param[in] dt object
3410 * \param[in] name name of xattr
3411 * \param[in] th transaction handle
3413 * \retval 0 on success
3414 * \retval negative if failed
3416 static int lod_xattr_del_internal(const struct lu_env *env,
3417 struct dt_object *dt,
3418 const char *name, struct thandle *th)
3420 struct dt_object *next = dt_object_child(dt);
3421 struct lod_object *lo = lod_dt_obj(dt);
3426 rc = lod_sub_xattr_del(env, next, name, th);
3427 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3430 if (lo->ldo_dir_stripe_count == 0)
3433 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3434 LASSERT(lo->ldo_stripe[i]);
3436 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3445 * Set default striping on a directory.
3447 * Sets specified striping on a directory object unless it matches the default
3448 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3449 * EA. This striping will be used when regular file is being created in this
3452 * \param[in] env execution environment
3453 * \param[in] dt the striped object
3454 * \param[in] buf buffer with the striping
3455 * \param[in] name name of EA
3456 * \param[in] fl xattr flag (see OSD API description)
3457 * \param[in] th transaction handle
3459 * \retval 0 on success
3460 * \retval negative if failed
3462 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
3463 struct dt_object *dt,
3464 const struct lu_buf *buf,
3465 const char *name, int fl,
3468 struct lov_user_md_v1 *lum;
3469 struct lov_user_md_v3 *v3 = NULL;
3470 const char *pool_name = NULL;
3475 LASSERT(buf != NULL && buf->lb_buf != NULL);
3478 switch (lum->lmm_magic) {
3479 case LOV_USER_MAGIC_SPECIFIC:
3480 case LOV_USER_MAGIC_V3:
3482 if (v3->lmm_pool_name[0] != '\0')
3483 pool_name = v3->lmm_pool_name;
3485 case LOV_USER_MAGIC_V1:
3486 /* if { size, offset, count } = { 0, -1, 0 } and no pool
3487 * (i.e. all default values specified) then delete default
3488 * striping from dir. */
3490 "set default striping: sz %u # %u offset %d %s %s\n",
3491 (unsigned)lum->lmm_stripe_size,
3492 (unsigned)lum->lmm_stripe_count,
3493 (int)lum->lmm_stripe_offset,
3494 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
3496 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
3497 lum->lmm_stripe_count,
3498 lum->lmm_stripe_offset,
3501 case LOV_USER_MAGIC_COMP_V1:
3505 CERROR("Invalid magic %x\n", lum->lmm_magic);
3510 rc = lod_xattr_del_internal(env, dt, name, th);
3514 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3521 * Set default striping on a directory object.
3523 * Sets specified striping on a directory object unless it matches the default
3524 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3525 * EA. This striping will be used when a new directory is being created in the
3528 * \param[in] env execution environment
3529 * \param[in] dt the striped object
3530 * \param[in] buf buffer with the striping
3531 * \param[in] name name of EA
3532 * \param[in] fl xattr flag (see OSD API description)
3533 * \param[in] th transaction handle
3535 * \retval 0 on success
3536 * \retval negative if failed
3538 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
3539 struct dt_object *dt,
3540 const struct lu_buf *buf,
3541 const char *name, int fl,
3544 struct lmv_user_md_v1 *lum;
3548 LASSERT(buf != NULL && buf->lb_buf != NULL);
3551 CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
3552 le32_to_cpu(lum->lum_stripe_count),
3553 (int)le32_to_cpu(lum->lum_stripe_offset));
3555 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
3556 le32_to_cpu(lum->lum_stripe_offset)) &&
3557 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
3558 rc = lod_xattr_del_internal(env, dt, name, th);
3562 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3571 * Turn directory into a striped directory.
3573 * During replay the client sends the striping created before MDT
3574 * failure, then the layer above LOD sends this defined striping
3575 * using ->do_xattr_set(), so LOD uses this method to replay creation
3576 * of the stripes. Notice the original information for the striping
3577 * (#stripes, FIDs, etc) was transferred in declare path.
3579 * \param[in] env execution environment
3580 * \param[in] dt the striped object
3581 * \param[in] buf not used currently
3582 * \param[in] name not used currently
3583 * \param[in] fl xattr flag (see OSD API description)
3584 * \param[in] th transaction handle
3586 * \retval 0 on success
3587 * \retval negative if failed
3589 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
3590 const struct lu_buf *buf, const char *name,
3591 int fl, struct thandle *th)
3593 struct lod_object *lo = lod_dt_obj(dt);
3594 struct lod_thread_info *info = lod_env_info(env);
3595 struct lu_attr *attr = &info->lti_attr;
3596 struct dt_object_format *dof = &info->lti_format;
3597 struct lu_buf lmv_buf;
3598 struct lu_buf slave_lmv_buf;
3599 struct lmv_mds_md_v1 *lmm;
3600 struct lmv_mds_md_v1 *slave_lmm = NULL;
3601 struct dt_insert_rec *rec = &info->lti_dt_rec;
3606 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3609 /* The stripes are supposed to be allocated in declare phase,
3610 * if there are no stripes being allocated, it will skip */
3611 if (lo->ldo_dir_stripe_count == 0)
3614 rc = dt_attr_get(env, dt_object_child(dt), attr);
3618 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
3619 LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
3620 dof->dof_type = DFT_DIR;
3622 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
3625 lmm = lmv_buf.lb_buf;
3627 OBD_ALLOC_PTR(slave_lmm);
3628 if (slave_lmm == NULL)
3631 lod_prep_slave_lmv_md(slave_lmm, lmm);
3632 slave_lmv_buf.lb_buf = slave_lmm;
3633 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
3635 rec->rec_type = S_IFDIR;
3636 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3637 struct dt_object *dto = lo->ldo_stripe[i];
3638 char *stripe_name = info->lti_key;
3639 struct lu_name *sname;
3640 struct linkea_data ldata = { NULL };
3641 struct lu_buf linkea_buf;
3643 /* if it's source stripe of migrating directory, don't create */
3644 if (!((lo->ldo_dir_hash_type & LMV_HASH_FLAG_MIGRATION) &&
3645 i >= lo->ldo_dir_migrate_offset)) {
3646 dt_write_lock(env, dto, MOR_TGT_CHILD);
3647 rc = lod_sub_create(env, dto, attr, NULL, dof, th);
3649 dt_write_unlock(env, dto);
3653 rc = lod_sub_ref_add(env, dto, th);
3654 dt_write_unlock(env, dto);
3658 rec->rec_fid = lu_object_fid(&dto->do_lu);
3659 rc = lod_sub_insert(env, dto,
3660 (const struct dt_rec *)rec,
3661 (const struct dt_key *)dot, th, 0);
3666 rec->rec_fid = lu_object_fid(&dt->do_lu);
3667 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
3668 (const struct dt_key *)dotdot, th, 0);
3672 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
3673 cfs_fail_val != i) {
3674 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
3676 slave_lmm->lmv_master_mdt_index =
3679 slave_lmm->lmv_master_mdt_index =
3682 rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
3683 XATTR_NAME_LMV, fl, th);
3688 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
3690 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3691 PFID(lu_object_fid(&dto->do_lu)), i + 1);
3693 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3694 PFID(lu_object_fid(&dto->do_lu)), i);
3696 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
3697 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
3698 sname, lu_object_fid(&dt->do_lu));
3702 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
3703 linkea_buf.lb_len = ldata.ld_leh->leh_len;
3704 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
3705 XATTR_NAME_LINK, 0, th);
3709 rec->rec_fid = lu_object_fid(&dto->do_lu);
3710 rc = lod_sub_insert(env, dt_object_child(dt),
3711 (const struct dt_rec *)rec,
3712 (const struct dt_key *)stripe_name, th, 0);
3716 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
3721 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
3722 rc = lod_sub_xattr_set(env, dt_object_child(dt),
3723 &lmv_buf, XATTR_NAME_LMV, fl, th);
3725 if (slave_lmm != NULL)
3726 OBD_FREE_PTR(slave_lmm);
3732 * Helper function to declare/execute creation of a striped directory
3734 * Called in declare/create object path, prepare striping for a directory
3735 * and prepare defaults data striping for the objects to be created in
3736 * that directory. Notice the function calls "declaration" or "execution"
3737 * methods depending on \a declare param. This is a consequence of the
3738 * current approach while we don't have natural distributed transactions:
3739 * we basically execute non-local updates in the declare phase. So, the
3740 * arguments for the both phases are the same and this is the reason for
3741 * this function to exist.
3743 * \param[in] env execution environment
3744 * \param[in] dt object
3745 * \param[in] attr attributes the stripes will be created with
3746 * \param[in] lmu lmv_user_md if MDT indices are specified
3747 * \param[in] dof format of stripes (see OSD API description)
3748 * \param[in] th transaction handle
3749 * \param[in] declare where to call "declare" or "execute" methods
3751 * \retval 0 on success
3752 * \retval negative if failed
3754 static int lod_dir_striping_create_internal(const struct lu_env *env,
3755 struct dt_object *dt,
3756 struct lu_attr *attr,
3757 const struct lu_buf *lmu,
3758 struct dt_object_format *dof,
3762 struct lod_thread_info *info = lod_env_info(env);
3763 struct lod_object *lo = lod_dt_obj(dt);
3764 const struct lod_default_striping *lds = lo->ldo_def_striping;
3768 LASSERT(ergo(lds != NULL,
3769 lds->lds_def_striping_set ||
3770 lds->lds_dir_def_striping_set));
3772 if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
3773 lo->ldo_dir_stripe_offset)) {
3775 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3776 int stripe_count = lo->ldo_dir_stripe_count;
3778 if (info->lti_ea_store_size < sizeof(*v1)) {
3779 rc = lod_ea_store_resize(info, sizeof(*v1));
3782 v1 = info->lti_ea_store;
3785 memset(v1, 0, sizeof(*v1));
3786 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3787 v1->lum_stripe_count = cpu_to_le32(stripe_count);
3788 v1->lum_stripe_offset =
3789 cpu_to_le32(lo->ldo_dir_stripe_offset);
3791 info->lti_buf.lb_buf = v1;
3792 info->lti_buf.lb_len = sizeof(*v1);
3793 lmu = &info->lti_buf;
3797 rc = lod_declare_xattr_set_lmv(env, dt, attr, lmu, dof,
3800 rc = lod_xattr_set_lmv(env, dt, lmu, XATTR_NAME_LMV, 0,
3806 /* Transfer default LMV striping from the parent */
3807 if (lds != NULL && lds->lds_dir_def_striping_set &&
3808 !LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
3809 lds->lds_dir_def_stripe_offset)) {
3810 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3812 if (info->lti_ea_store_size < sizeof(*v1)) {
3813 rc = lod_ea_store_resize(info, sizeof(*v1));
3816 v1 = info->lti_ea_store;
3819 memset(v1, 0, sizeof(*v1));
3820 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3821 v1->lum_stripe_count =
3822 cpu_to_le32(lds->lds_dir_def_stripe_count);
3823 v1->lum_stripe_offset =
3824 cpu_to_le32(lds->lds_dir_def_stripe_offset);
3826 cpu_to_le32(lds->lds_dir_def_hash_type);
3828 info->lti_buf.lb_buf = v1;
3829 info->lti_buf.lb_len = sizeof(*v1);
3831 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3832 XATTR_NAME_DEFAULT_LMV,
3835 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
3837 XATTR_NAME_DEFAULT_LMV, 0,
3843 /* Transfer default LOV striping from the parent */
3844 if (lds != NULL && lds->lds_def_striping_set &&
3845 lds->lds_def_comp_cnt != 0) {
3846 struct lov_mds_md *lmm;
3847 int lmm_size = lod_comp_md_size(lo, true);
3849 if (info->lti_ea_store_size < lmm_size) {
3850 rc = lod_ea_store_resize(info, lmm_size);
3854 lmm = info->lti_ea_store;
3856 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
3860 info->lti_buf.lb_buf = lmm;
3861 info->lti_buf.lb_len = lmm_size;
3864 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3865 XATTR_NAME_LOV, 0, th);
3867 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
3868 XATTR_NAME_LOV, 0, th);
3876 static int lod_declare_dir_striping_create(const struct lu_env *env,
3877 struct dt_object *dt,
3878 struct lu_attr *attr,
3880 struct dt_object_format *dof,
3883 return lod_dir_striping_create_internal(env, dt, attr, lmu, dof, th,
3887 static int lod_dir_striping_create(const struct lu_env *env,
3888 struct dt_object *dt,
3889 struct lu_attr *attr,
3890 struct dt_object_format *dof,
3893 return lod_dir_striping_create_internal(env, dt, attr, NULL, dof, th,
3898 * Make LOV EA for striped object.
3900 * Generate striping information and store it in the LOV EA of the given
3901 * object. The caller must ensure nobody else is calling the function
3902 * against the object concurrently. The transaction must be started.
3903 * FLDB service must be running as well; it's used to map FID to the target,
3904 * which is stored in LOV EA.
3906 * \param[in] env execution environment for this thread
3907 * \param[in] lo LOD object
3908 * \param[in] th transaction handle
3910 * \retval 0 if LOV EA is stored successfully
3911 * \retval negative error number on failure
3913 static int lod_generate_and_set_lovea(const struct lu_env *env,
3914 struct lod_object *lo,
3917 struct lod_thread_info *info = lod_env_info(env);
3918 struct dt_object *next = dt_object_child(&lo->ldo_obj);
3919 struct lov_mds_md_v1 *lmm;
3925 if (lo->ldo_comp_cnt == 0) {
3926 lod_striping_free(env, lo);
3927 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
3931 lmm_size = lod_comp_md_size(lo, false);
3932 if (info->lti_ea_store_size < lmm_size) {
3933 rc = lod_ea_store_resize(info, lmm_size);
3937 lmm = info->lti_ea_store;
3939 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
3943 info->lti_buf.lb_buf = lmm;
3944 info->lti_buf.lb_len = lmm_size;
3945 rc = lod_sub_xattr_set(env, next, &info->lti_buf,
3946 XATTR_NAME_LOV, 0, th);
3951 * Delete layout component(s)
3953 * \param[in] env execution environment for this thread
3954 * \param[in] dt object
3955 * \param[in] th transaction handle
3957 * \retval 0 on success
3958 * \retval negative error number on failure
3960 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
3963 struct lod_layout_component *lod_comp;
3964 struct lod_object *lo = lod_dt_obj(dt);
3965 struct dt_object *next = dt_object_child(dt);
3966 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3969 LASSERT(lo->ldo_is_composite);
3970 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
3972 left = lo->ldo_comp_cnt;
3973 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3974 lod_comp = &lo->ldo_comp_entries[i];
3976 if (lod_comp->llc_id != LCME_ID_INVAL)
3980 /* Not instantiated component */
3981 if (lod_comp->llc_stripe == NULL)
3984 LASSERT(lod_comp->llc_stripe_count > 0);
3985 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
3986 struct dt_object *obj = lod_comp->llc_stripe[j];
3990 rc = lod_sub_destroy(env, obj, th);
3994 lu_object_put(env, &obj->do_lu);
3995 lod_comp->llc_stripe[j] = NULL;
3997 OBD_FREE(lod_comp->llc_stripe, sizeof(struct dt_object *) *
3998 lod_comp->llc_stripes_allocated);
3999 lod_comp->llc_stripe = NULL;
4000 OBD_FREE(lod_comp->llc_ost_indices,
4001 sizeof(__u32) * lod_comp->llc_stripes_allocated);
4002 lod_comp->llc_ost_indices = NULL;
4003 lod_comp->llc_stripes_allocated = 0;
4004 lod_obj_set_pool(lo, i, NULL);
4005 if (lod_comp->llc_ostlist.op_array) {
4006 OBD_FREE(lod_comp->llc_ostlist.op_array,
4007 lod_comp->llc_ostlist.op_size);
4008 lod_comp->llc_ostlist.op_array = NULL;
4009 lod_comp->llc_ostlist.op_size = 0;
4013 LASSERTF(left >= 0 && left < lo->ldo_comp_cnt, "left = %d\n", left);
4015 struct lod_layout_component *comp_array;
4017 OBD_ALLOC(comp_array, sizeof(*comp_array) * left);
4018 if (comp_array == NULL)
4019 GOTO(out, rc = -ENOMEM);
4021 memcpy(&comp_array[0], &lo->ldo_comp_entries[0],
4022 sizeof(*comp_array) * left);
4024 OBD_FREE(lo->ldo_comp_entries,
4025 sizeof(*comp_array) * lo->ldo_comp_cnt);
4026 lo->ldo_comp_entries = comp_array;
4027 lo->ldo_comp_cnt = left;
4029 LASSERT(lo->ldo_mirror_count == 1);
4030 lo->ldo_mirrors[0].lme_end = left - 1;
4031 lod_obj_inc_layout_gen(lo);
4033 lod_free_comp_entries(lo);
4036 LASSERT(dt_object_exists(dt));
4037 rc = dt_attr_get(env, next, attr);
4041 if (attr->la_size > 0) {
4043 attr->la_valid = LA_SIZE;
4044 rc = lod_sub_attr_set(env, next, attr, th);
4049 rc = lod_generate_and_set_lovea(env, lo, th);
4053 lod_striping_free(env, lo);
4058 static int lod_get_default_lov_striping(const struct lu_env *env,
4059 struct lod_object *lo,
4060 struct lod_default_striping *lds);
4062 * Implementation of dt_object_operations::do_xattr_set.
4064 * Sets specified extended attribute on the object. Three types of EAs are
4066 * LOV EA - stores striping for a regular file or default striping (when set
4068 * LMV EA - stores a marker for the striped directories
4069 * DMV EA - stores default directory striping
4071 * When striping is applied to a non-striped existing object (this is called
4072 * late striping), then LOD notices the caller wants to turn the object into a
4073 * striped one. The stripe objects are created and appropriate EA is set:
4074 * LOV EA storing all the stripes directly or LMV EA storing just a small header
4075 * with striping configuration.
4077 * \see dt_object_operations::do_xattr_set() in the API description for details.
4079 static int lod_xattr_set(const struct lu_env *env,
4080 struct dt_object *dt, const struct lu_buf *buf,
4081 const char *name, int fl, struct thandle *th)
4083 struct dt_object *next = dt_object_child(dt);
4087 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4088 strcmp(name, XATTR_NAME_LMV) == 0) {
4089 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
4091 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4092 strncmp(name, XATTR_NAME_LMV, strlen(XATTR_NAME_LMV)) == 0 &&
4093 strlen(name) > strlen(XATTR_NAME_LMV) + 1) {
4094 const char *op = name + strlen(XATTR_NAME_LMV) + 1;
4097 if (strcmp(op, "del") == 0)
4098 rc = lod_dir_layout_delete(env, dt, buf, th);
4100 * XATTR_NAME_LMV".add" is never called, but only declared,
4101 * because lod_xattr_set_lmv() will do the addition.
4105 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4106 strcmp(name, XATTR_NAME_LOV) == 0) {
4107 struct lod_thread_info *info = lod_env_info(env);
4108 struct lod_default_striping *lds = &info->lti_def_striping;
4109 struct lov_user_md_v1 *v1 = buf->lb_buf;
4110 char pool[LOV_MAXPOOLNAME + 1];
4113 /* get existing striping config */
4114 rc = lod_get_default_lov_striping(env, lod_dt_obj(dt), lds);
4118 memset(pool, 0, sizeof(pool));
4119 if (lds->lds_def_striping_set == 1)
4120 lod_layout_get_pool(lds->lds_def_comp_entries,
4121 lds->lds_def_comp_cnt, pool,
4124 is_del = LOVEA_DELETE_VALUES(v1->lmm_stripe_size,
4125 v1->lmm_stripe_count,
4126 v1->lmm_stripe_offset,
4129 /* Retain the pool name if it is not given */
4130 if (v1->lmm_magic == LOV_USER_MAGIC_V1 && pool[0] != '\0' &&
4132 struct lod_thread_info *info = lod_env_info(env);
4133 struct lov_user_md_v3 *v3 = info->lti_ea_store;
4135 memset(v3, 0, sizeof(*v3));
4136 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
4137 v3->lmm_pattern = cpu_to_le32(v1->lmm_pattern);
4138 v3->lmm_stripe_count =
4139 cpu_to_le32(v1->lmm_stripe_count);
4140 v3->lmm_stripe_offset =
4141 cpu_to_le32(v1->lmm_stripe_offset);
4142 v3->lmm_stripe_size = cpu_to_le32(v1->lmm_stripe_size);
4144 strlcpy(v3->lmm_pool_name, pool,
4145 sizeof(v3->lmm_pool_name));
4147 info->lti_buf.lb_buf = v3;
4148 info->lti_buf.lb_len = sizeof(*v3);
4149 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4152 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name,
4156 if (lds->lds_def_striping_set == 1 &&
4157 lds->lds_def_comp_entries != NULL)
4158 lod_free_def_comp_entries(lds);
4161 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4162 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
4164 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
4167 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4168 (!strcmp(name, XATTR_NAME_LOV) ||
4169 !strncmp(name, XATTR_LUSTRE_LOV,
4170 strlen(XATTR_LUSTRE_LOV)))) {
4171 /* in case of lov EA swap, just set it
4172 * if not, it is a replay so check striping match what we
4173 * already have during req replay, declare_xattr_set()
4174 * defines striping, then create() does the work */
4175 if (fl & LU_XATTR_REPLACE) {
4176 /* free stripes, then update disk */
4177 lod_striping_free(env, lod_dt_obj(dt));
4179 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
4180 } else if (dt_object_remote(dt)) {
4181 /* This only happens during migration, see
4182 * mdd_migrate_create(), in which Master MDT will
4183 * create a remote target object, and only set
4184 * (migrating) stripe EA on the remote object,
4185 * and does not need creating each stripes. */
4186 rc = lod_sub_xattr_set(env, next, buf, name,
4188 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
4189 /* delete component(s) */
4190 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
4191 rc = lod_layout_del(env, dt, th);
4194 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
4195 * it's going to create create file with specified
4196 * component(s), the striping must have not being
4197 * cached in this case;
4199 * Otherwise, it's going to add/change component(s) to
4200 * an existing file, the striping must have been cached
4203 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
4204 !strcmp(name, XATTR_NAME_LOV),
4205 !lod_dt_obj(dt)->ldo_comp_cached));
4207 rc = lod_striped_create(env, dt, NULL, NULL, th);
4210 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
4211 rc = lod_replace_parent_fid(env, dt, th, false);
4216 /* then all other xattr */
4217 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
4223 * Implementation of dt_object_operations::do_declare_xattr_del.
4225 * \see dt_object_operations::do_declare_xattr_del() in the API description
4228 static int lod_declare_xattr_del(const struct lu_env *env,
4229 struct dt_object *dt, const char *name,
4232 struct lod_object *lo = lod_dt_obj(dt);
4233 struct dt_object *next = dt_object_child(dt);
4238 rc = lod_sub_declare_xattr_del(env, next, name, th);
4242 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4245 /* set xattr to each stripes, if needed */
4246 rc = lod_striping_load(env, lo);
4250 if (lo->ldo_dir_stripe_count == 0)
4253 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4254 struct dt_object *dto = lo->ldo_stripe[i];
4257 rc = lod_sub_declare_xattr_del(env, dto, name, th);
4266 * Implementation of dt_object_operations::do_xattr_del.
4268 * If EA storing a regular striping is being deleted, then release
4269 * all the references to the stripe objects in core.
4271 * \see dt_object_operations::do_xattr_del() in the API description for details.
4273 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
4274 const char *name, struct thandle *th)
4276 struct dt_object *next = dt_object_child(dt);
4277 struct lod_object *lo = lod_dt_obj(dt);
4282 if (!strcmp(name, XATTR_NAME_LOV) || !strcmp(name, XATTR_NAME_LMV))
4283 lod_striping_free(env, lod_dt_obj(dt));
4285 rc = lod_sub_xattr_del(env, next, name, th);
4286 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
4289 if (lo->ldo_dir_stripe_count == 0)
4292 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4293 struct dt_object *dto = lo->ldo_stripe[i];
4297 rc = lod_sub_xattr_del(env, dto, name, th);
4306 * Implementation of dt_object_operations::do_xattr_list.
4308 * \see dt_object_operations::do_xattr_list() in the API description
4311 static int lod_xattr_list(const struct lu_env *env,
4312 struct dt_object *dt, const struct lu_buf *buf)
4314 return dt_xattr_list(env, dt_object_child(dt), buf);
4317 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
4319 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
4323 * Copy OST list from layout provided by user.
4325 * \param[in] lod_comp layout_component to be filled
4326 * \param[in] v3 LOV EA V3 user data
4328 * \retval 0 on success
4329 * \retval negative if failed
4331 int lod_comp_copy_ost_lists(struct lod_layout_component *lod_comp,
4332 struct lov_user_md_v3 *v3)
4338 if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
4339 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
4341 if (lod_comp->llc_ostlist.op_array) {
4342 if (lod_comp->llc_ostlist.op_size >=
4343 v3->lmm_stripe_count * sizeof(__u32)) {
4344 lod_comp->llc_ostlist.op_count =
4345 v3->lmm_stripe_count;
4348 OBD_FREE(lod_comp->llc_ostlist.op_array,
4349 lod_comp->llc_ostlist.op_size);
4352 /* copy ost list from lmm */
4353 lod_comp->llc_ostlist.op_count = v3->lmm_stripe_count;
4354 lod_comp->llc_ostlist.op_size = v3->lmm_stripe_count * sizeof(__u32);
4355 OBD_ALLOC(lod_comp->llc_ostlist.op_array,
4356 lod_comp->llc_ostlist.op_size);
4357 if (!lod_comp->llc_ostlist.op_array)
4360 for (j = 0; j < v3->lmm_stripe_count; j++) {
4361 lod_comp->llc_ostlist.op_array[j] =
4362 v3->lmm_objects[j].l_ost_idx;
4370 * Get default striping.
4372 * \param[in] env execution environment
4373 * \param[in] lo object
4374 * \param[out] lds default striping
4376 * \retval 0 on success
4377 * \retval negative if failed
4379 static int lod_get_default_lov_striping(const struct lu_env *env,
4380 struct lod_object *lo,
4381 struct lod_default_striping *lds)
4383 struct lod_thread_info *info = lod_env_info(env);
4384 struct lov_user_md_v1 *v1 = NULL;
4385 struct lov_user_md_v3 *v3 = NULL;
4386 struct lov_comp_md_v1 *comp_v1 = NULL;
4393 lds->lds_def_striping_set = 0;
4395 rc = lod_get_lov_ea(env, lo);
4399 if (rc < (typeof(rc))sizeof(struct lov_user_md))
4402 v1 = info->lti_ea_store;
4403 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
4404 lustre_swab_lov_user_md_v1(v1);
4405 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
4406 v3 = (struct lov_user_md_v3 *)v1;
4407 lustre_swab_lov_user_md_v3(v3);
4408 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_SPECIFIC)) {
4409 v3 = (struct lov_user_md_v3 *)v1;
4410 lustre_swab_lov_user_md_v3(v3);
4411 lustre_swab_lov_user_md_objects(v3->lmm_objects,
4412 v3->lmm_stripe_count);
4413 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
4414 comp_v1 = (struct lov_comp_md_v1 *)v1;
4415 lustre_swab_lov_comp_md_v1(comp_v1);
4418 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
4419 v1->lmm_magic != LOV_MAGIC_COMP_V1 &&
4420 v1->lmm_magic != LOV_USER_MAGIC_SPECIFIC)
4423 if (v1->lmm_magic == LOV_MAGIC_COMP_V1) {
4424 comp_v1 = (struct lov_comp_md_v1 *)v1;
4425 comp_cnt = comp_v1->lcm_entry_count;
4428 mirror_cnt = comp_v1->lcm_mirror_count + 1;
4436 /* realloc default comp entries if necessary */
4437 rc = lod_def_striping_comp_resize(lds, comp_cnt);
4441 lds->lds_def_comp_cnt = comp_cnt;
4442 lds->lds_def_striping_is_composite = composite;
4443 lds->lds_def_mirror_cnt = mirror_cnt;
4445 for (i = 0; i < comp_cnt; i++) {
4446 struct lod_layout_component *lod_comp;
4447 struct lu_extent *ext;
4450 lod_comp = &lds->lds_def_comp_entries[i];
4452 * reset lod_comp values, llc_stripes is always NULL in
4453 * the default striping template, llc_pool will be reset
4456 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
4459 v1 = (struct lov_user_md *)((char *)comp_v1 +
4460 comp_v1->lcm_entries[i].lcme_offset);
4461 ext = &comp_v1->lcm_entries[i].lcme_extent;
4462 lod_comp->llc_extent = *ext;
4465 if (v1->lmm_pattern != LOV_PATTERN_RAID0 &&
4466 v1->lmm_pattern != LOV_PATTERN_MDT &&
4467 v1->lmm_pattern != 0) {
4468 lod_free_def_comp_entries(lds);
4472 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d "
4473 "stripe_offset=%d\n",
4474 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
4475 (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
4476 (int)v1->lmm_stripe_offset);
4478 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
4479 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
4480 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
4481 lod_comp->llc_pattern = v1->lmm_pattern;
4484 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
4485 /* XXX: sanity check here */
4486 v3 = (struct lov_user_md_v3 *) v1;
4487 if (v3->lmm_pool_name[0] != '\0')
4488 pool = v3->lmm_pool_name;
4490 lod_set_def_pool(lds, i, pool);
4491 if (v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
4492 v3 = (struct lov_user_md_v3 *)v1;
4493 rc = lod_comp_copy_ost_lists(lod_comp, v3);
4496 } else if (lod_comp->llc_ostlist.op_array &&
4497 lod_comp->llc_ostlist.op_count) {
4498 for (j = 0; j < lod_comp->llc_ostlist.op_count; j++)
4499 lod_comp->llc_ostlist.op_array[j] = -1;
4500 lod_comp->llc_ostlist.op_count = 0;
4504 lds->lds_def_striping_set = 1;
4509 * Get default directory striping.
4511 * \param[in] env execution environment
4512 * \param[in] lo object
4513 * \param[out] lds default striping
4515 * \retval 0 on success
4516 * \retval negative if failed
4518 static int lod_get_default_lmv_striping(const struct lu_env *env,
4519 struct lod_object *lo,
4520 struct lod_default_striping *lds)
4522 struct lod_thread_info *info = lod_env_info(env);
4523 struct lmv_user_md_v1 *v1 = NULL;
4527 lds->lds_dir_def_striping_set = 0;
4528 rc = lod_get_default_lmv_ea(env, lo);
4532 if (rc < (typeof(rc))sizeof(struct lmv_user_md))
4535 v1 = info->lti_ea_store;
4537 lds->lds_dir_def_stripe_count = le32_to_cpu(v1->lum_stripe_count);
4538 lds->lds_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
4539 lds->lds_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
4540 lds->lds_dir_def_striping_set = 1;
4546 * Get default striping in the object.
4548 * Get object default striping and default directory striping.
4550 * \param[in] env execution environment
4551 * \param[in] lo object
4552 * \param[out] lds default striping
4554 * \retval 0 on success
4555 * \retval negative if failed
4557 static int lod_get_default_striping(const struct lu_env *env,
4558 struct lod_object *lo,
4559 struct lod_default_striping *lds)
4563 rc = lod_get_default_lov_striping(env, lo, lds);
4564 rc1 = lod_get_default_lmv_striping(env, lo, lds);
4565 if (rc == 0 && rc1 < 0)
4572 * Apply default striping on object.
4574 * If object striping pattern is not set, set to the one in default striping.
4575 * The default striping is from parent or fs.
4577 * \param[in] lo new object
4578 * \param[in] lds default striping
4579 * \param[in] mode new object's mode
4581 static void lod_striping_from_default(struct lod_object *lo,
4582 const struct lod_default_striping *lds,
4585 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4586 struct lov_desc *desc = &d->lod_desc;
4589 if (lds->lds_def_striping_set && S_ISREG(mode)) {
4590 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
4591 lds->lds_def_comp_cnt);
4595 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
4596 if (lds->lds_def_mirror_cnt > 1)
4597 lo->ldo_flr_state = LCM_FL_RDONLY;
4599 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4600 struct lod_layout_component *obj_comp =
4601 &lo->ldo_comp_entries[i];
4602 struct lod_layout_component *def_comp =
4603 &lds->lds_def_comp_entries[i];
4605 CDEBUG(D_LAYOUT, "Inherite from default: size:%hu "
4606 "nr:%u offset:%u pattern %#x %s\n",
4607 def_comp->llc_stripe_size,
4608 def_comp->llc_stripe_count,
4609 def_comp->llc_stripe_offset,
4610 def_comp->llc_pattern,
4611 def_comp->llc_pool ?: "");
4613 *obj_comp = *def_comp;
4614 if (def_comp->llc_pool != NULL) {
4615 /* pointer was copied from def_comp */
4616 obj_comp->llc_pool = NULL;
4617 lod_obj_set_pool(lo, i, def_comp->llc_pool);
4621 if (def_comp->llc_ostlist.op_array &&
4622 def_comp->llc_ostlist.op_count) {
4623 OBD_ALLOC(obj_comp->llc_ostlist.op_array,
4624 obj_comp->llc_ostlist.op_size);
4625 if (!obj_comp->llc_ostlist.op_array)
4627 memcpy(obj_comp->llc_ostlist.op_array,
4628 def_comp->llc_ostlist.op_array,
4629 obj_comp->llc_ostlist.op_size);
4630 } else if (def_comp->llc_ostlist.op_array) {
4631 obj_comp->llc_ostlist.op_array = NULL;
4635 * Don't initialize these fields for plain layout
4636 * (v1/v3) here, they are inherited in the order of
4637 * 'parent' -> 'fs default (root)' -> 'global default
4638 * values for stripe_count & stripe_size'.
4640 * see lod_ah_init().
4642 if (!lo->ldo_is_composite)
4645 lod_adjust_stripe_info(obj_comp, desc);
4647 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
4648 if (lo->ldo_dir_stripe_count == 0)
4649 lo->ldo_dir_stripe_count =
4650 lds->lds_dir_def_stripe_count;
4651 if (lo->ldo_dir_stripe_offset == -1)
4652 lo->ldo_dir_stripe_offset =
4653 lds->lds_dir_def_stripe_offset;
4654 if (lo->ldo_dir_hash_type == 0)
4655 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
4657 CDEBUG(D_LAYOUT, "striping from default dir: count:%hu, "
4658 "offset:%u, hash_type:%u\n",
4659 lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
4660 lo->ldo_dir_hash_type);
4664 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root)
4666 struct lod_layout_component *lod_comp;
4668 if (lo->ldo_comp_cnt == 0)
4671 if (lo->ldo_is_composite)
4674 lod_comp = &lo->ldo_comp_entries[0];
4676 if (lod_comp->llc_stripe_count <= 0 ||
4677 lod_comp->llc_stripe_size <= 0)
4680 if (from_root && (lod_comp->llc_pool == NULL ||
4681 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
4688 * Implementation of dt_object_operations::do_ah_init.
4690 * This method is used to make a decision on the striping configuration for the
4691 * object being created. It can be taken from the \a parent object if it exists,
4692 * or filesystem's default. The resulting configuration (number of stripes,
4693 * stripe size/offset, pool name, etc) is stored in the object itself and will
4694 * be used by the methods like ->doo_declare_create().
4696 * \see dt_object_operations::do_ah_init() in the API description for details.
4698 static void lod_ah_init(const struct lu_env *env,
4699 struct dt_allocation_hint *ah,
4700 struct dt_object *parent,
4701 struct dt_object *child,
4704 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
4705 struct lod_thread_info *info = lod_env_info(env);
4706 struct lod_default_striping *lds = &info->lti_def_striping;
4707 struct dt_object *nextp = NULL;
4708 struct dt_object *nextc;
4709 struct lod_object *lp = NULL;
4710 struct lod_object *lc;
4711 struct lov_desc *desc;
4712 struct lod_layout_component *lod_comp;
4718 if (likely(parent)) {
4719 nextp = dt_object_child(parent);
4720 lp = lod_dt_obj(parent);
4723 nextc = dt_object_child(child);
4724 lc = lod_dt_obj(child);
4726 LASSERT(!lod_obj_is_striped(child));
4727 /* default layout template may have been set on the regular file
4728 * when this is called from mdd_create_data() */
4729 if (S_ISREG(child_mode))
4730 lod_free_comp_entries(lc);
4732 if (!dt_object_exists(nextc))
4733 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
4735 if (S_ISDIR(child_mode)) {
4736 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
4738 /* other default values are 0 */
4739 lc->ldo_dir_stripe_offset = -1;
4741 /* get default striping from parent object */
4742 if (likely(lp != NULL))
4743 lod_get_default_striping(env, lp, lds);
4745 /* set child default striping info, default value is NULL */
4746 if (lds->lds_def_striping_set || lds->lds_dir_def_striping_set)
4747 lc->ldo_def_striping = lds;
4749 /* It should always honour the specified stripes */
4750 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
4751 * will have old magic. In this case, we should ignore the
4752 * stripe count and try to create dir by default stripe.
4754 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
4755 (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
4756 le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
4757 lc->ldo_dir_stripe_count =
4758 le32_to_cpu(lum1->lum_stripe_count);
4759 lc->ldo_dir_stripe_offset =
4760 le32_to_cpu(lum1->lum_stripe_offset);
4761 lc->ldo_dir_hash_type =
4762 le32_to_cpu(lum1->lum_hash_type);
4764 "set dirstripe: count %hu, offset %d, hash %u\n",
4765 lc->ldo_dir_stripe_count,
4766 (int)lc->ldo_dir_stripe_offset,
4767 lc->ldo_dir_hash_type);
4769 /* transfer defaults LMV to new directory */
4770 lod_striping_from_default(lc, lds, child_mode);
4772 /* set count 0 to create normal directory */
4773 if (lc->ldo_dir_stripe_count == 1)
4774 lc->ldo_dir_stripe_count = 0;
4777 /* shrink the stripe_count to the avaible MDT count */
4778 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
4779 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)) {
4780 lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
4781 if (lc->ldo_dir_stripe_count == 1)
4782 lc->ldo_dir_stripe_count = 0;
4785 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
4786 lc->ldo_dir_stripe_count,
4787 (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
4792 /* child object regular file*/
4794 if (!lod_object_will_be_striped(S_ISREG(child_mode),
4795 lu_object_fid(&child->do_lu)))
4798 /* If object is going to be striped over OSTs, transfer default
4799 * striping information to the child, so that we can use it
4800 * during declaration and creation.
4802 * Try from the parent first.
4804 if (likely(lp != NULL)) {
4805 rc = lod_get_default_lov_striping(env, lp, lds);
4807 lod_striping_from_default(lc, lds, child_mode);
4810 /* Initialize lod_device::lod_md_root object reference */
4811 if (d->lod_md_root == NULL) {
4812 struct dt_object *root;
4813 struct lod_object *lroot;
4815 lu_root_fid(&info->lti_fid);
4816 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
4817 if (!IS_ERR(root)) {
4818 lroot = lod_dt_obj(root);
4820 spin_lock(&d->lod_lock);
4821 if (d->lod_md_root != NULL)
4822 dt_object_put(env, &d->lod_md_root->ldo_obj);
4823 d->lod_md_root = lroot;
4824 spin_unlock(&d->lod_lock);
4828 /* try inherit layout from the root object (fs default) when:
4829 * - parent does not have default layout; or
4830 * - parent has plain(v1/v3) default layout, and some attributes
4831 * are not specified in the default layout;
4833 if (d->lod_md_root != NULL && lod_need_inherit_more(lc, true)) {
4834 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds);
4837 if (lc->ldo_comp_cnt == 0) {
4838 lod_striping_from_default(lc, lds, child_mode);
4839 } else if (!lds->lds_def_striping_is_composite) {
4840 struct lod_layout_component *def_comp;
4842 LASSERT(!lc->ldo_is_composite);
4843 lod_comp = &lc->ldo_comp_entries[0];
4844 def_comp = &lds->lds_def_comp_entries[0];
4846 if (lod_comp->llc_stripe_count <= 0)
4847 lod_comp->llc_stripe_count =
4848 def_comp->llc_stripe_count;
4849 if (lod_comp->llc_stripe_size <= 0)
4850 lod_comp->llc_stripe_size =
4851 def_comp->llc_stripe_size;
4852 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
4853 lod_comp->llc_stripe_offset =
4854 def_comp->llc_stripe_offset;
4855 if (lod_comp->llc_pool == NULL)
4856 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
4861 * fs default striping may not be explicitly set, or historically set
4862 * in config log, use them.
4864 if (lod_need_inherit_more(lc, false)) {
4865 if (lc->ldo_comp_cnt == 0) {
4866 rc = lod_alloc_comp_entries(lc, 0, 1);
4868 /* fail to allocate memory, will create a
4869 * non-striped file. */
4871 lc->ldo_is_composite = 0;
4872 lod_comp = &lc->ldo_comp_entries[0];
4873 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4875 LASSERT(!lc->ldo_is_composite);
4876 lod_comp = &lc->ldo_comp_entries[0];
4877 desc = &d->lod_desc;
4878 lod_adjust_stripe_info(lod_comp, desc);
4884 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
4886 * Size initialization on late striping.
4888 * Propagate the size of a truncated object to a deferred striping.
4889 * This function handles a special case when truncate was done on a
4890 * non-striped object and now while the striping is being created
4891 * we can't lose that size, so we have to propagate it to the stripes
4894 * \param[in] env execution environment
4895 * \param[in] dt object
4896 * \param[in] th transaction handle
4898 * \retval 0 on success
4899 * \retval negative if failed
4901 static int lod_declare_init_size(const struct lu_env *env,
4902 struct dt_object *dt, struct thandle *th)
4904 struct dt_object *next = dt_object_child(dt);
4905 struct lod_object *lo = lod_dt_obj(dt);
4906 struct dt_object **objects = NULL;
4907 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4908 uint64_t size, offs;
4909 int i, rc, stripe, stripe_count = 0, stripe_size = 0;
4910 struct lu_extent size_ext;
4913 if (!lod_obj_is_striped(dt))
4916 rc = dt_attr_get(env, next, attr);
4917 LASSERT(attr->la_valid & LA_SIZE);
4921 size = attr->la_size;
4925 size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
4926 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4927 struct lod_layout_component *lod_comp;
4928 struct lu_extent *extent;
4930 lod_comp = &lo->ldo_comp_entries[i];
4932 if (lod_comp->llc_stripe == NULL)
4935 extent = &lod_comp->llc_extent;
4936 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
4937 if (!lo->ldo_is_composite ||
4938 lu_extent_is_overlapped(extent, &size_ext)) {
4939 objects = lod_comp->llc_stripe;
4940 stripe_count = lod_comp->llc_stripe_count;
4941 stripe_size = lod_comp->llc_stripe_size;
4944 if (stripe_count == 0)
4947 LASSERT(objects != NULL && stripe_size != 0);
4948 /* ll_do_div64(a, b) returns a % b, and a = a / b */
4949 ll_do_div64(size, (__u64)stripe_size);
4950 stripe = ll_do_div64(size, (__u64)stripe_count);
4951 LASSERT(objects[stripe] != NULL);
4953 size = size * stripe_size;
4954 offs = attr->la_size;
4955 size += ll_do_div64(offs, stripe_size);
4957 attr->la_valid = LA_SIZE;
4958 attr->la_size = size;
4960 rc = lod_sub_declare_attr_set(env, objects[stripe],
4969 * Declare creation of striped object.
4971 * The function declares creation stripes for a regular object. The function
4972 * also declares whether the stripes will be created with non-zero size if
4973 * previously size was set non-zero on the master object. If object \a dt is
4974 * not local, then only fully defined striping can be applied in \a lovea.
4975 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
4978 * \param[in] env execution environment
4979 * \param[in] dt object
4980 * \param[in] attr attributes the stripes will be created with
4981 * \param[in] lovea a buffer containing striping description
4982 * \param[in] th transaction handle
4984 * \retval 0 on success
4985 * \retval negative if failed
4987 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
4988 struct lu_attr *attr,
4989 const struct lu_buf *lovea, struct thandle *th)
4991 struct lod_thread_info *info = lod_env_info(env);
4992 struct dt_object *next = dt_object_child(dt);
4993 struct lod_object *lo = lod_dt_obj(dt);
4997 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
4998 GOTO(out, rc = -ENOMEM);
5000 if (!dt_object_remote(next)) {
5001 /* choose OST and generate appropriate objects */
5002 rc = lod_prepare_create(env, lo, attr, lovea, th);
5007 * declare storage for striping data
5009 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5011 /* LOD can not choose OST objects for remote objects, i.e.
5012 * stripes must be ready before that. Right now, it can only
5013 * happen during migrate, i.e. migrate process needs to create
5014 * remote regular file (mdd_migrate_create), then the migrate
5015 * process will provide stripeEA. */
5016 LASSERT(lovea != NULL);
5017 info->lti_buf = *lovea;
5020 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
5021 XATTR_NAME_LOV, 0, th);
5026 * if striping is created with local object's size > 0,
5027 * we have to propagate this size to specific object
5028 * the case is possible only when local object was created previously
5030 if (dt_object_exists(next))
5031 rc = lod_declare_init_size(env, dt, th);
5034 /* failed to create striping or to set initial size, let's reset
5035 * config so that others don't get confused */
5037 lod_striping_free(env, lo);
5043 * Implementation of dt_object_operations::do_declare_create.
5045 * The method declares creation of a new object. If the object will be striped,
5046 * then helper functions are called to find FIDs for the stripes, declare
5047 * creation of the stripes and declare initialization of the striping
5048 * information to be stored in the master object.
5050 * \see dt_object_operations::do_declare_create() in the API description
5053 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
5054 struct lu_attr *attr,
5055 struct dt_allocation_hint *hint,
5056 struct dt_object_format *dof, struct thandle *th)
5058 struct dt_object *next = dt_object_child(dt);
5059 struct lod_object *lo = lod_dt_obj(dt);
5068 * first of all, we declare creation of local object
5070 rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
5075 * it's lod_ah_init() that has decided the object will be striped
5077 if (dof->dof_type == DFT_REGULAR) {
5078 /* callers don't want stripes */
5079 /* XXX: all tricky interactions with ->ah_make_hint() decided
5080 * to use striping, then ->declare_create() behaving differently
5081 * should be cleaned */
5082 if (dof->u.dof_reg.striped != 0)
5083 rc = lod_declare_striped_create(env, dt, attr,
5085 } else if (dof->dof_type == DFT_DIR) {
5086 struct seq_server_site *ss;
5087 struct lu_buf buf = { NULL };
5088 struct lu_buf *lmu = NULL;
5090 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
5092 /* If the parent has default stripeEA, and client
5093 * did not find it before sending create request,
5094 * then MDT will return -EREMOTE, and client will
5095 * retrieve the default stripeEA and re-create the
5098 * Note: if dah_eadata != NULL, it means creating the
5099 * striped directory with specified stripeEA, then it
5100 * should ignore the default stripeEA */
5101 if (hint != NULL && hint->dah_eadata == NULL) {
5102 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
5103 GOTO(out, rc = -EREMOTE);
5105 if (lo->ldo_dir_stripe_offset == -1) {
5106 /* child and parent should be in the same MDT */
5107 if (hint->dah_parent != NULL &&
5108 dt_object_remote(hint->dah_parent))
5109 GOTO(out, rc = -EREMOTE);
5110 } else if (lo->ldo_dir_stripe_offset !=
5112 struct lod_device *lod;
5113 struct lod_tgt_descs *ltd;
5114 struct lod_tgt_desc *tgt = NULL;
5115 bool found_mdt = false;
5118 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5119 ltd = &lod->lod_mdt_descs;
5120 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
5121 tgt = LTD_TGT(ltd, i);
5122 if (tgt->ltd_index ==
5123 lo->ldo_dir_stripe_offset) {
5129 /* If the MDT indicated by stripe_offset can be
5130 * found, then tell client to resend the create
5131 * request to the correct MDT, otherwise return
5132 * error to client */
5134 GOTO(out, rc = -EREMOTE);
5136 GOTO(out, rc = -EINVAL);
5138 } else if (hint && hint->dah_eadata) {
5140 lmu->lb_buf = (void *)hint->dah_eadata;
5141 lmu->lb_len = hint->dah_eadata_len;
5144 rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
5148 /* failed to create striping or to set initial size, let's reset
5149 * config so that others don't get confused */
5151 lod_striping_free(env, lo);
5156 * Generate component ID for new created component.
5158 * \param[in] lo LOD object
5159 * \param[in] comp_idx index of ldo_comp_entries
5161 * \retval component ID on success
5162 * \retval LCME_ID_INVAL on failure
5164 static __u32 lod_gen_component_id(struct lod_object *lo,
5165 int mirror_id, int comp_idx)
5167 struct lod_layout_component *lod_comp;
5168 __u32 id, start, end;
5171 LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
5173 lod_obj_inc_layout_gen(lo);
5174 id = lo->ldo_layout_gen;
5175 if (likely(id <= SEQ_ID_MAX))
5176 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
5178 /* Layout generation wraps, need to check collisions. */
5179 start = id & SEQ_ID_MASK;
5182 for (id = start; id <= end; id++) {
5183 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5184 lod_comp = &lo->ldo_comp_entries[i];
5185 if (pflr_id(mirror_id, id) == lod_comp->llc_id)
5188 /* Found the ununsed ID */
5189 if (i == lo->ldo_comp_cnt)
5190 RETURN(pflr_id(mirror_id, id));
5192 if (end == LCME_ID_MAX) {
5194 end = min(lo->ldo_layout_gen & LCME_ID_MASK,
5195 (__u32)(LCME_ID_MAX - 1));
5199 RETURN(LCME_ID_INVAL);
5203 * Creation of a striped regular object.
5205 * The function is called to create the stripe objects for a regular
5206 * striped file. This can happen at the initial object creation or
5207 * when the caller asks LOD to do so using ->do_xattr_set() method
5208 * (so called late striping). Notice all the information are already
5209 * prepared in the form of the list of objects (ldo_stripe field).
5210 * This is done during declare phase.
5212 * \param[in] env execution environment
5213 * \param[in] dt object
5214 * \param[in] attr attributes the stripes will be created with
5215 * \param[in] dof format of stripes (see OSD API description)
5216 * \param[in] th transaction handle
5218 * \retval 0 on success
5219 * \retval negative if failed
5221 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
5222 struct lu_attr *attr, struct dt_object_format *dof,
5225 struct lod_layout_component *lod_comp;
5226 struct lod_object *lo = lod_dt_obj(dt);
5231 LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
5233 mirror_id = 0; /* non-flr file's mirror_id is 0 */
5234 if (lo->ldo_mirror_count > 1) {
5235 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5236 lod_comp = &lo->ldo_comp_entries[i];
5237 if (lod_comp->llc_id != LCME_ID_INVAL &&
5238 mirror_id_of(lod_comp->llc_id) > mirror_id)
5239 mirror_id = mirror_id_of(lod_comp->llc_id);
5243 /* create all underlying objects */
5244 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5245 lod_comp = &lo->ldo_comp_entries[i];
5247 if (lod_comp->llc_id == LCME_ID_INVAL) {
5248 /* only the component of FLR layout with more than 1
5249 * mirror has mirror ID in its component ID.
5251 if (lod_comp->llc_extent.e_start == 0 &&
5252 lo->ldo_mirror_count > 1)
5255 lod_comp->llc_id = lod_gen_component_id(lo,
5257 if (lod_comp->llc_id == LCME_ID_INVAL)
5258 GOTO(out, rc = -ERANGE);
5261 if (lod_comp_inited(lod_comp))
5264 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
5265 lod_comp_set_init(lod_comp);
5267 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
5268 lod_comp_set_init(lod_comp);
5270 if (lod_comp->llc_stripe == NULL)
5273 LASSERT(lod_comp->llc_stripe_count);
5274 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
5275 struct dt_object *object = lod_comp->llc_stripe[j];
5276 LASSERT(object != NULL);
5277 rc = lod_sub_create(env, object, attr, NULL, dof, th);
5281 lod_comp_set_init(lod_comp);
5284 rc = lod_fill_mirrors(lo);
5288 rc = lod_generate_and_set_lovea(env, lo, th);
5292 lo->ldo_comp_cached = 1;
5296 lod_striping_free(env, lo);
5301 * Implementation of dt_object_operations::do_create.
5303 * If any of preceeding methods (like ->do_declare_create(),
5304 * ->do_ah_init(), etc) chose to create a striped object,
5305 * then this method will create the master and the stripes.
5307 * \see dt_object_operations::do_create() in the API description for details.
5309 static int lod_create(const struct lu_env *env, struct dt_object *dt,
5310 struct lu_attr *attr, struct dt_allocation_hint *hint,
5311 struct dt_object_format *dof, struct thandle *th)
5316 /* create local object */
5317 rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
5321 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
5322 lod_obj_is_striped(dt) && dof->u.dof_reg.striped != 0) {
5323 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
5324 rc = lod_striped_create(env, dt, attr, dof, th);
5331 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
5332 struct dt_object *dt, struct thandle *th,
5333 int comp_idx, int stripe_idx,
5334 struct lod_obj_stripe_cb_data *data)
5336 if (data->locd_declare)
5337 return lod_sub_declare_destroy(env, dt, th);
5338 else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
5339 stripe_idx == cfs_fail_val)
5340 return lod_sub_destroy(env, dt, th);
5346 * Implementation of dt_object_operations::do_declare_destroy.
5348 * If the object is a striped directory, then the function declares reference
5349 * removal from the master object (this is an index) to the stripes and declares
5350 * destroy of all the stripes. In all the cases, it declares an intention to
5351 * destroy the object itself.
5353 * \see dt_object_operations::do_declare_destroy() in the API description
5356 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
5359 struct dt_object *next = dt_object_child(dt);
5360 struct lod_object *lo = lod_dt_obj(dt);
5361 struct lod_thread_info *info = lod_env_info(env);
5362 char *stripe_name = info->lti_key;
5367 * load striping information, notice we don't do this when object
5368 * is being initialized as we don't need this information till
5369 * few specific cases like destroy, chown
5371 rc = lod_striping_load(env, lo);
5375 /* declare destroy for all underlying objects */
5376 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5377 rc = next->do_ops->do_index_try(env, next,
5378 &dt_directory_features);
5382 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5383 rc = lod_sub_declare_ref_del(env, next, th);
5387 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
5388 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
5390 rc = lod_sub_declare_delete(env, next,
5391 (const struct dt_key *)stripe_name, th);
5398 * we declare destroy for the local object
5400 rc = lod_sub_declare_destroy(env, next, th);
5404 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
5405 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
5408 if (!lod_obj_is_striped(dt))
5411 /* declare destroy all striped objects */
5412 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5413 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5414 if (lo->ldo_stripe[i] == NULL)
5417 rc = lod_sub_declare_ref_del(env, lo->ldo_stripe[i],
5420 rc = lod_sub_declare_destroy(env, lo->ldo_stripe[i],
5426 struct lod_obj_stripe_cb_data data = { { 0 } };
5428 data.locd_declare = true;
5429 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
5430 rc = lod_obj_for_each_stripe(env, lo, th, &data);
5437 * Implementation of dt_object_operations::do_destroy.
5439 * If the object is a striped directory, then the function removes references
5440 * from the master object (this is an index) to the stripes and destroys all
5441 * the stripes. In all the cases, the function destroys the object itself.
5443 * \see dt_object_operations::do_destroy() in the API description for details.
5445 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
5448 struct dt_object *next = dt_object_child(dt);
5449 struct lod_object *lo = lod_dt_obj(dt);
5450 struct lod_thread_info *info = lod_env_info(env);
5451 char *stripe_name = info->lti_key;
5456 /* destroy sub-stripe of master object */
5457 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5458 rc = next->do_ops->do_index_try(env, next,
5459 &dt_directory_features);
5463 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5464 rc = lod_sub_ref_del(env, next, th);
5468 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
5469 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
5472 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
5473 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
5474 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
5476 rc = lod_sub_delete(env, next,
5477 (const struct dt_key *)stripe_name, th);
5483 rc = lod_sub_destroy(env, next, th);
5487 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
5488 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
5491 if (!lod_obj_is_striped(dt))
5494 /* destroy all striped objects */
5495 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5496 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5497 if (lo->ldo_stripe[i] == NULL)
5499 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
5500 i == cfs_fail_val) {
5501 dt_write_lock(env, lo->ldo_stripe[i],
5503 rc = lod_sub_ref_del(env, lo->ldo_stripe[i],
5505 dt_write_unlock(env, lo->ldo_stripe[i]);
5509 rc = lod_sub_destroy(env, lo->ldo_stripe[i],
5516 struct lod_obj_stripe_cb_data data = { { 0 } };
5518 data.locd_declare = false;
5519 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
5520 rc = lod_obj_for_each_stripe(env, lo, th, &data);
5527 * Implementation of dt_object_operations::do_declare_ref_add.
5529 * \see dt_object_operations::do_declare_ref_add() in the API description
5532 static int lod_declare_ref_add(const struct lu_env *env,
5533 struct dt_object *dt, struct thandle *th)
5535 return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
5539 * Implementation of dt_object_operations::do_ref_add.
5541 * \see dt_object_operations::do_ref_add() in the API description for details.
5543 static int lod_ref_add(const struct lu_env *env,
5544 struct dt_object *dt, struct thandle *th)
5546 return lod_sub_ref_add(env, dt_object_child(dt), th);
5550 * Implementation of dt_object_operations::do_declare_ref_del.
5552 * \see dt_object_operations::do_declare_ref_del() in the API description
5555 static int lod_declare_ref_del(const struct lu_env *env,
5556 struct dt_object *dt, struct thandle *th)
5558 return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
5562 * Implementation of dt_object_operations::do_ref_del
5564 * \see dt_object_operations::do_ref_del() in the API description for details.
5566 static int lod_ref_del(const struct lu_env *env,
5567 struct dt_object *dt, struct thandle *th)
5569 return lod_sub_ref_del(env, dt_object_child(dt), th);
5573 * Implementation of dt_object_operations::do_object_sync.
5575 * \see dt_object_operations::do_object_sync() in the API description
5578 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
5579 __u64 start, __u64 end)
5581 return dt_object_sync(env, dt_object_child(dt), start, end);
5585 * Implementation of dt_object_operations::do_object_unlock.
5587 * Used to release LDLM lock(s).
5589 * \see dt_object_operations::do_object_unlock() in the API description
5592 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
5593 struct ldlm_enqueue_info *einfo,
5594 union ldlm_policy_data *policy)
5596 struct lod_object *lo = lod_dt_obj(dt);
5597 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
5598 int slave_locks_size;
5602 if (slave_locks == NULL)
5605 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
5606 /* Note: for remote lock for single stripe dir, MDT will cancel
5607 * the lock by lockh directly */
5608 LASSERT(!dt_object_remote(dt_object_child(dt)));
5610 /* locks were unlocked in MDT layer */
5611 for (i = 0; i < slave_locks->ha_count; i++)
5612 LASSERT(!lustre_handle_is_used(&slave_locks->ha_handles[i]));
5615 * NB, ha_count may not equal to ldo_dir_stripe_count, because dir
5616 * layout may change, e.g., shrink dir layout after migration.
5618 for (i = 0; i < lo->ldo_dir_stripe_count; i++)
5619 dt_invalidate(env, lo->ldo_stripe[i]);
5621 slave_locks_size = offsetof(typeof(*slave_locks),
5622 ha_handles[slave_locks->ha_count]);
5623 OBD_FREE(slave_locks, slave_locks_size);
5624 einfo->ei_cbdata = NULL;
5630 * Implementation of dt_object_operations::do_object_lock.
5632 * Used to get LDLM lock on the non-striped and striped objects.
5634 * \see dt_object_operations::do_object_lock() in the API description
5637 static int lod_object_lock(const struct lu_env *env,
5638 struct dt_object *dt,
5639 struct lustre_handle *lh,
5640 struct ldlm_enqueue_info *einfo,
5641 union ldlm_policy_data *policy)
5643 struct lod_object *lo = lod_dt_obj(dt);
5644 int slave_locks_size;
5645 struct lustre_handle_array *slave_locks = NULL;
5650 /* remote object lock */
5651 if (!einfo->ei_enq_slave) {
5652 LASSERT(dt_object_remote(dt));
5653 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
5657 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
5660 rc = lod_striping_load(env, lo);
5665 if (lo->ldo_dir_stripe_count <= 1)
5668 slave_locks_size = offsetof(typeof(*slave_locks),
5669 ha_handles[lo->ldo_dir_stripe_count]);
5670 /* Freed in lod_object_unlock */
5671 OBD_ALLOC(slave_locks, slave_locks_size);
5674 slave_locks->ha_count = lo->ldo_dir_stripe_count;
5676 /* striped directory lock */
5677 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5678 struct lustre_handle lockh;
5679 struct ldlm_res_id *res_id;
5681 res_id = &lod_env_info(env)->lti_res_id;
5682 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
5684 einfo->ei_res_id = res_id;
5686 LASSERT(lo->ldo_stripe[i] != NULL);
5687 if (dt_object_remote(lo->ldo_stripe[i])) {
5688 set_bit(i, (void *)slave_locks->ha_map);
5689 rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh,
5692 struct ldlm_namespace *ns = einfo->ei_namespace;
5693 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
5694 ldlm_completion_callback completion = einfo->ei_cb_cp;
5695 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
5697 if (einfo->ei_mode == LCK_PW ||
5698 einfo->ei_mode == LCK_EX)
5699 dlmflags |= LDLM_FL_COS_INCOMPAT;
5701 LASSERT(ns != NULL);
5702 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS,
5703 policy, einfo->ei_mode,
5704 &dlmflags, blocking,
5706 NULL, 0, LVB_T_NONE,
5711 ldlm_lock_decref_and_cancel(
5712 &slave_locks->ha_handles[i],
5714 OBD_FREE(slave_locks, slave_locks_size);
5717 slave_locks->ha_handles[i] = lockh;
5719 einfo->ei_cbdata = slave_locks;
5725 * Implementation of dt_object_operations::do_invalidate.
5727 * \see dt_object_operations::do_invalidate() in the API description for details
5729 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
5731 return dt_invalidate(env, dt_object_child(dt));
5734 static int lod_layout_data_init(struct lod_thread_info *info, __u32 comp_cnt)
5738 /* clear memory region that will be used for layout change */
5739 memset(&info->lti_layout_attr, 0, sizeof(struct lu_attr));
5740 info->lti_count = 0;
5742 if (info->lti_comp_size >= comp_cnt)
5745 if (info->lti_comp_size > 0) {
5746 OBD_FREE(info->lti_comp_idx,
5747 info->lti_comp_size * sizeof(__u32));
5748 info->lti_comp_size = 0;
5751 OBD_ALLOC(info->lti_comp_idx, comp_cnt * sizeof(__u32));
5752 if (!info->lti_comp_idx)
5755 info->lti_comp_size = comp_cnt;
5759 static int lod_declare_instantiate_components(const struct lu_env *env,
5760 struct lod_object *lo, struct thandle *th)
5762 struct lod_thread_info *info = lod_env_info(env);
5767 LASSERT(info->lti_count < lo->ldo_comp_cnt);
5769 for (i = 0; i < info->lti_count; i++) {
5770 rc = lod_qos_prep_create(env, lo, NULL, th,
5771 info->lti_comp_idx[i]);
5777 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5778 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
5779 &info->lti_buf, XATTR_NAME_LOV, 0, th);
5785 static int lod_declare_update_plain(const struct lu_env *env,
5786 struct lod_object *lo, struct layout_intent *layout,
5787 const struct lu_buf *buf, struct thandle *th)
5789 struct lod_thread_info *info = lod_env_info(env);
5790 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5791 struct lod_layout_component *lod_comp;
5792 struct lov_comp_md_v1 *comp_v1 = NULL;
5793 bool replay = false;
5797 LASSERT(lo->ldo_flr_state == LCM_FL_NONE);
5800 * In case the client is passing lovea, which only happens during
5801 * the replay of layout intent write RPC for now, we may need to
5802 * parse the lovea and apply new layout configuration.
5804 if (buf && buf->lb_len) {
5805 struct lov_user_md_v1 *v1 = buf->lb_buf;
5807 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
5808 v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
5809 LOV_MAGIC_COMP_V1)) {
5810 CERROR("%s: the replay buffer of layout extend "
5811 "(magic %#x) does not contain expected "
5812 "composite layout.\n",
5813 lod2obd(d)->obd_name, v1->lmm_magic);
5814 GOTO(out, rc = -EINVAL);
5817 rc = lod_use_defined_striping(env, lo, buf);
5820 lo->ldo_comp_cached = 1;
5822 rc = lod_get_lov_ea(env, lo);
5825 /* old on-disk EA is stored in info->lti_buf */
5826 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
5829 /* non replay path */
5830 rc = lod_striping_load(env, lo);
5835 /* Make sure defined layout covers the requested write range. */
5836 lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
5837 if (lo->ldo_comp_cnt > 1 &&
5838 lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
5839 lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
5840 CDEBUG(replay ? D_ERROR : D_LAYOUT,
5841 "%s: the defined layout [0, %#llx) does not covers "
5842 "the write range "DEXT"\n",
5843 lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
5844 PEXT(&layout->li_extent));
5845 GOTO(out, rc = -EINVAL);
5848 CDEBUG(D_LAYOUT, "%s: "DFID": instantiate components "DEXT"\n",
5849 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
5850 PEXT(&layout->li_extent));
5853 * Iterate ld->ldo_comp_entries, find the component whose extent under
5854 * the write range and not instantianted.
5856 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5857 lod_comp = &lo->ldo_comp_entries[i];
5859 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
5863 if (lod_comp_inited(lod_comp))
5867 * In replay path, lod_comp is the EA passed by
5868 * client replay buffer, comp_v1 is the pre-recovery
5869 * on-disk EA, we'd sift out those components which
5870 * were init-ed in the on-disk EA.
5872 if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
5877 * this component hasn't instantiated in normal path, or during
5878 * replay it needs replay the instantiation.
5881 /* A released component is being extended */
5882 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
5883 GOTO(out, rc = -EINVAL);
5885 LASSERT(info->lti_comp_idx != NULL);
5886 info->lti_comp_idx[info->lti_count++] = i;
5889 if (info->lti_count == 0)
5892 lod_obj_inc_layout_gen(lo);
5893 rc = lod_declare_instantiate_components(env, lo, th);
5896 lod_striping_free(env, lo);
5900 static inline int lod_comp_index(struct lod_object *lo,
5901 struct lod_layout_component *lod_comp)
5903 LASSERT(lod_comp >= lo->ldo_comp_entries &&
5904 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
5906 return lod_comp - lo->ldo_comp_entries;
5910 * Stale other mirrors by writing extent.
5912 static void lod_stale_components(struct lod_object *lo, int primary,
5913 struct lu_extent *extent)
5915 struct lod_layout_component *pri_comp, *lod_comp;
5918 /* The writing extent decides which components in the primary
5919 * are affected... */
5920 CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
5921 lod_foreach_mirror_comp(pri_comp, lo, primary) {
5922 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
5925 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
5926 lod_comp_index(lo, pri_comp),
5927 PEXT(&pri_comp->llc_extent));
5929 for (i = 0; i < lo->ldo_mirror_count; i++) {
5933 /* ... and then stale other components that are
5934 * overlapping with primary components */
5935 lod_foreach_mirror_comp(lod_comp, lo, i) {
5936 if (!lu_extent_is_overlapped(
5937 &pri_comp->llc_extent,
5938 &lod_comp->llc_extent))
5941 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
5942 i, lod_comp_index(lo, lod_comp));
5944 lod_comp->llc_flags |= LCME_FL_STALE;
5945 lo->ldo_mirrors[i].lme_stale = 1;
5952 * check an OST's availability
5953 * \param[in] env execution environment
5954 * \param[in] lo lod object
5955 * \param[in] dt dt object
5956 * \param[in] index mirror index
5958 * \retval negative if failed
5959 * \retval 1 if \a dt is available
5960 * \retval 0 if \a dt is not available
5962 static inline int lod_check_ost_avail(const struct lu_env *env,
5963 struct lod_object *lo,
5964 struct dt_object *dt, int index)
5966 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5967 struct lod_tgt_desc *ost;
5969 int type = LU_SEQ_RANGE_OST;
5972 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &idx, &type);
5974 CERROR("%s: can't locate "DFID":rc = %d\n",
5975 lod2obd(lod)->obd_name, PFID(lu_object_fid(&dt->do_lu)),
5980 ost = OST_TGT(lod, idx);
5981 if (ost->ltd_statfs.os_state &
5982 (OS_STATE_READONLY | OS_STATE_ENOSPC | OS_STATE_ENOINO |
5983 OS_STATE_NOPRECREATE) ||
5984 ost->ltd_active == 0) {
5985 CDEBUG(D_LAYOUT, DFID ": mirror %d OST%d unavail, rc = %d\n",
5986 PFID(lod_object_fid(lo)), index, idx, rc);
5994 * Pick primary mirror for write
5995 * \param[in] env execution environment
5996 * \param[in] lo object
5997 * \param[in] extent write range
5999 static int lod_primary_pick(const struct lu_env *env, struct lod_object *lo,
6000 struct lu_extent *extent)
6002 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6003 unsigned int seq = 0;
6004 struct lod_layout_component *lod_comp;
6006 int picked = -1, second_pick = -1, third_pick = -1;
6009 if (OBD_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
6010 get_random_bytes(&seq, sizeof(seq));
6011 seq %= lo->ldo_mirror_count;
6015 * Pick a mirror as the primary, and check the availability of OSTs.
6017 * This algo can be revised later after knowing the topology of
6020 lod_qos_statfs_update(env, lod);
6021 for (i = 0; i < lo->ldo_mirror_count; i++) {
6022 bool ost_avail = true;
6023 int index = (i + seq) % lo->ldo_mirror_count;
6025 if (lo->ldo_mirrors[index].lme_stale) {
6026 CDEBUG(D_LAYOUT, DFID": mirror %d stale\n",
6027 PFID(lod_object_fid(lo)), index);
6031 /* 2nd pick is for the primary mirror containing unavail OST */
6032 if (lo->ldo_mirrors[index].lme_primary && second_pick < 0)
6033 second_pick = index;
6035 /* 3rd pick is for non-primary mirror containing unavail OST */
6036 if (second_pick < 0 && third_pick < 0)
6040 * we found a non-primary 1st pick, we'd like to find a
6041 * potential pirmary mirror.
6043 if (picked >= 0 && !lo->ldo_mirrors[index].lme_primary)
6046 /* check the availability of OSTs */
6047 lod_foreach_mirror_comp(lod_comp, lo, index) {
6048 if (!lod_comp_inited(lod_comp) || !lod_comp->llc_stripe)
6051 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6052 struct dt_object *dt = lod_comp->llc_stripe[j];
6054 rc = lod_check_ost_avail(env, lo, dt, index);
6061 } /* for all dt object in one component */
6064 } /* for all components in a mirror */
6067 * the OSTs where allocated objects locates in the components
6068 * of the mirror are available.
6073 /* this mirror has all OSTs available */
6077 * primary with all OSTs are available, this is the perfect
6080 if (lo->ldo_mirrors[index].lme_primary)
6082 } /* for all mirrors */
6084 /* failed to pick a sound mirror, lower our expectation */
6086 picked = second_pick;
6088 picked = third_pick;
6096 * figure out the components should be instantiated for resync.
6098 static int lod_prepare_resync(const struct lu_env *env, struct lod_object *lo,
6099 struct lu_extent *extent)
6101 struct lod_thread_info *info = lod_env_info(env);
6102 struct lod_layout_component *lod_comp;
6103 unsigned int need_sync = 0;
6107 DFID": instantiate all stale components in "DEXT"\n",
6108 PFID(lod_object_fid(lo)), PEXT(extent));
6111 * instantiate all components within this extent, even non-stale
6114 for (i = 0; i < lo->ldo_mirror_count; i++) {
6115 if (!lo->ldo_mirrors[i].lme_stale)
6118 lod_foreach_mirror_comp(lod_comp, lo, i) {
6119 if (!lu_extent_is_overlapped(extent,
6120 &lod_comp->llc_extent))
6125 if (lod_comp_inited(lod_comp))
6128 CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
6129 i, lod_comp_index(lo, lod_comp));
6130 info->lti_comp_idx[info->lti_count++] =
6131 lod_comp_index(lo, lod_comp);
6135 return need_sync ? 0 : -EALREADY;
6138 static int lod_declare_update_rdonly(const struct lu_env *env,
6139 struct lod_object *lo, struct md_layout_change *mlc,
6142 struct lod_thread_info *info = lod_env_info(env);
6143 struct lu_attr *layout_attr = &info->lti_layout_attr;
6144 struct lod_layout_component *lod_comp;
6145 struct lu_extent extent = { 0 };
6149 LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
6150 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
6151 mlc->mlc_opc == MD_LAYOUT_RESYNC);
6152 LASSERT(lo->ldo_mirror_count > 0);
6154 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
6155 struct layout_intent *layout = mlc->mlc_intent;
6158 extent = layout->li_extent;
6159 CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
6160 PFID(lod_object_fid(lo)), PEXT(&extent));
6162 picked = lod_primary_pick(env, lo, &extent);
6166 CDEBUG(D_LAYOUT, DFID": picked mirror id %u as primary\n",
6167 PFID(lod_object_fid(lo)),
6168 lo->ldo_mirrors[picked].lme_id);
6170 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
6172 * trunc transfers [0, size) in the intent extent, we'd
6173 * stale components overlapping [size, eof).
6175 extent.e_start = extent.e_end;
6176 extent.e_end = OBD_OBJECT_EOF;
6179 /* stale overlapping components from other mirrors */
6180 lod_stale_components(lo, picked, &extent);
6182 /* restore truncate intent extent */
6183 if (layout->li_opc == LAYOUT_INTENT_TRUNC)
6184 extent.e_end = extent.e_start;
6186 /* instantiate components for the picked mirror, start from 0 */
6189 lod_foreach_mirror_comp(lod_comp, lo, picked) {
6190 if (!lu_extent_is_overlapped(&extent,
6191 &lod_comp->llc_extent))
6194 if (lod_comp_inited(lod_comp))
6197 info->lti_comp_idx[info->lti_count++] =
6198 lod_comp_index(lo, lod_comp);
6201 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
6202 } else { /* MD_LAYOUT_RESYNC */
6206 * could contain multiple non-stale mirrors, so we need to
6207 * prep uninited all components assuming any non-stale mirror
6208 * could be picked as the primary mirror.
6210 for (i = 0; i < lo->ldo_mirror_count; i++) {
6211 if (lo->ldo_mirrors[i].lme_stale)
6214 lod_foreach_mirror_comp(lod_comp, lo, i) {
6215 if (!lod_comp_inited(lod_comp))
6218 if (extent.e_end < lod_comp->llc_extent.e_end)
6220 lod_comp->llc_extent.e_end;
6224 rc = lod_prepare_resync(env, lo, &extent);
6227 /* change the file state to SYNC_PENDING */
6228 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
6231 /* Reset the layout version once it's becoming too large.
6232 * This way it can make sure that the layout version is
6233 * monotonously increased in this writing era. */
6234 lod_obj_inc_layout_gen(lo);
6235 if (lo->ldo_layout_gen > (LCME_ID_MAX >> 1)) {
6236 __u32 layout_version;
6238 cfs_get_random_bytes(&layout_version, sizeof(layout_version));
6239 lo->ldo_layout_gen = layout_version & 0xffff;
6242 rc = lod_declare_instantiate_components(env, lo, th);
6246 layout_attr->la_valid = LA_LAYOUT_VERSION;
6247 layout_attr->la_layout_version = 0; /* set current version */
6248 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
6249 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
6250 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
6256 lod_striping_free(env, lo);
6260 static int lod_declare_update_write_pending(const struct lu_env *env,
6261 struct lod_object *lo, struct md_layout_change *mlc,
6264 struct lod_thread_info *info = lod_env_info(env);
6265 struct lu_attr *layout_attr = &info->lti_layout_attr;
6266 struct lod_layout_component *lod_comp;
6267 struct lu_extent extent = { 0 };
6273 LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
6274 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
6275 mlc->mlc_opc == MD_LAYOUT_RESYNC);
6277 /* look for the primary mirror */
6278 for (i = 0; i < lo->ldo_mirror_count; i++) {
6279 if (lo->ldo_mirrors[i].lme_stale)
6282 LASSERTF(primary < 0, DFID " has multiple primary: %u / %u",
6283 PFID(lod_object_fid(lo)),
6284 lo->ldo_mirrors[i].lme_id,
6285 lo->ldo_mirrors[primary].lme_id);
6290 CERROR(DFID ": doesn't have a primary mirror\n",
6291 PFID(lod_object_fid(lo)));
6292 GOTO(out, rc = -ENODATA);
6295 CDEBUG(D_LAYOUT, DFID": found primary %u\n",
6296 PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
6298 LASSERT(!lo->ldo_mirrors[primary].lme_stale);
6300 /* for LAYOUT_WRITE opc, it has to do the following operations:
6301 * 1. stale overlapping componets from stale mirrors;
6302 * 2. instantiate components of the primary mirror;
6303 * 3. transfter layout version to all objects of the primary;
6305 * for LAYOUT_RESYNC opc, it will do:
6306 * 1. instantiate components of all stale mirrors;
6307 * 2. transfer layout version to all objects to close write era. */
6309 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
6310 LASSERT(mlc->mlc_intent != NULL);
6312 extent = mlc->mlc_intent->li_extent;
6314 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
6315 PFID(lod_object_fid(lo)), PEXT(&extent));
6317 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
6319 * trunc transfers [0, size) in the intent extent, we'd
6320 * stale components overlapping [size, eof).
6322 extent.e_start = extent.e_end;
6323 extent.e_end = OBD_OBJECT_EOF;
6325 /* 1. stale overlapping components */
6326 lod_stale_components(lo, primary, &extent);
6328 /* 2. find out the components need instantiating.
6329 * instantiate [0, mlc->mlc_intent->e_end) */
6331 /* restore truncate intent extent */
6332 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC)
6333 extent.e_end = extent.e_start;
6336 lod_foreach_mirror_comp(lod_comp, lo, primary) {
6337 if (!lu_extent_is_overlapped(&extent,
6338 &lod_comp->llc_extent))
6341 if (lod_comp_inited(lod_comp))
6344 CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
6345 primary, lod_comp_index(lo, lod_comp));
6346 info->lti_comp_idx[info->lti_count++] =
6347 lod_comp_index(lo, lod_comp);
6349 } else { /* MD_LAYOUT_RESYNC */
6350 lod_foreach_mirror_comp(lod_comp, lo, primary) {
6351 if (!lod_comp_inited(lod_comp))
6354 extent.e_end = lod_comp->llc_extent.e_end;
6357 rc = lod_prepare_resync(env, lo, &extent);
6360 /* change the file state to SYNC_PENDING */
6361 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
6364 rc = lod_declare_instantiate_components(env, lo, th);
6368 /* 3. transfer layout version to OST objects.
6369 * transfer new layout version to OST objects so that stale writes
6370 * can be denied. It also ends an era of writing by setting
6371 * LU_LAYOUT_RESYNC. Normal client can never use this bit to
6372 * send write RPC; only resync RPCs could do it. */
6373 layout_attr->la_valid = LA_LAYOUT_VERSION;
6374 layout_attr->la_layout_version = 0; /* set current version */
6375 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
6376 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
6377 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
6381 lod_obj_inc_layout_gen(lo);
6384 lod_striping_free(env, lo);
6388 static int lod_declare_update_sync_pending(const struct lu_env *env,
6389 struct lod_object *lo, struct md_layout_change *mlc,
6392 struct lod_thread_info *info = lod_env_info(env);
6393 unsigned sync_components = 0;
6394 unsigned resync_components = 0;
6399 LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
6400 LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
6401 mlc->mlc_opc == MD_LAYOUT_WRITE);
6403 CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
6404 PFID(lod_object_fid(lo)), mlc->mlc_opc);
6406 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
6407 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
6408 PFID(lod_object_fid(lo)));
6410 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
6411 return lod_declare_update_write_pending(env, lo, mlc, th);
6414 /* MD_LAYOUT_RESYNC_DONE */
6416 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6417 struct lod_layout_component *lod_comp;
6420 lod_comp = &lo->ldo_comp_entries[i];
6422 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
6427 for (j = 0; j < mlc->mlc_resync_count; j++) {
6428 if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
6431 mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
6432 lod_comp->llc_flags &= ~LCME_FL_STALE;
6433 resync_components++;
6439 for (i = 0; i < mlc->mlc_resync_count; i++) {
6440 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
6443 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
6444 "or already synced\n", PFID(lod_object_fid(lo)),
6445 mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
6446 GOTO(out, rc = -EINVAL);
6449 if (!sync_components || (mlc->mlc_resync_count && !resync_components)) {
6450 CDEBUG(D_LAYOUT, DFID": no mirror in sync\n",
6451 PFID(lod_object_fid(lo)));
6453 /* tend to return an error code here to prevent
6454 * the MDT from setting SoM attribute */
6455 GOTO(out, rc = -EINVAL);
6458 CDEBUG(D_LAYOUT, DFID": resynced %u/%zu components\n",
6459 PFID(lod_object_fid(lo)),
6460 resync_components, mlc->mlc_resync_count);
6462 lo->ldo_flr_state = LCM_FL_RDONLY;
6463 lod_obj_inc_layout_gen(lo);
6465 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6466 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
6467 &info->lti_buf, XATTR_NAME_LOV, 0, th);
6472 lod_striping_free(env, lo);
6476 static int lod_declare_layout_change(const struct lu_env *env,
6477 struct dt_object *dt, struct md_layout_change *mlc,
6480 struct lod_thread_info *info = lod_env_info(env);
6481 struct lod_object *lo = lod_dt_obj(dt);
6485 if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
6486 dt_object_remote(dt_object_child(dt)))
6489 rc = lod_striping_load(env, lo);
6493 LASSERT(lo->ldo_comp_cnt > 0);
6495 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
6499 switch (lo->ldo_flr_state) {
6501 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
6505 rc = lod_declare_update_rdonly(env, lo, mlc, th);
6507 case LCM_FL_WRITE_PENDING:
6508 rc = lod_declare_update_write_pending(env, lo, mlc, th);
6510 case LCM_FL_SYNC_PENDING:
6511 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
6522 * Instantiate layout component objects which covers the intent write offset.
6524 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
6525 struct md_layout_change *mlc, struct thandle *th)
6527 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
6528 struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
6529 struct lod_object *lo = lod_dt_obj(dt);
6532 rc = lod_striped_create(env, dt, attr, NULL, th);
6533 if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
6534 layout_attr->la_layout_version |= lo->ldo_layout_gen;
6535 rc = lod_attr_set(env, dt, layout_attr, th);
6541 struct dt_object_operations lod_obj_ops = {
6542 .do_read_lock = lod_read_lock,
6543 .do_write_lock = lod_write_lock,
6544 .do_read_unlock = lod_read_unlock,
6545 .do_write_unlock = lod_write_unlock,
6546 .do_write_locked = lod_write_locked,
6547 .do_attr_get = lod_attr_get,
6548 .do_declare_attr_set = lod_declare_attr_set,
6549 .do_attr_set = lod_attr_set,
6550 .do_xattr_get = lod_xattr_get,
6551 .do_declare_xattr_set = lod_declare_xattr_set,
6552 .do_xattr_set = lod_xattr_set,
6553 .do_declare_xattr_del = lod_declare_xattr_del,
6554 .do_xattr_del = lod_xattr_del,
6555 .do_xattr_list = lod_xattr_list,
6556 .do_ah_init = lod_ah_init,
6557 .do_declare_create = lod_declare_create,
6558 .do_create = lod_create,
6559 .do_declare_destroy = lod_declare_destroy,
6560 .do_destroy = lod_destroy,
6561 .do_index_try = lod_index_try,
6562 .do_declare_ref_add = lod_declare_ref_add,
6563 .do_ref_add = lod_ref_add,
6564 .do_declare_ref_del = lod_declare_ref_del,
6565 .do_ref_del = lod_ref_del,
6566 .do_object_sync = lod_object_sync,
6567 .do_object_lock = lod_object_lock,
6568 .do_object_unlock = lod_object_unlock,
6569 .do_invalidate = lod_invalidate,
6570 .do_declare_layout_change = lod_declare_layout_change,
6571 .do_layout_change = lod_layout_change,
6575 * Implementation of dt_body_operations::dbo_read.
6577 * \see dt_body_operations::dbo_read() in the API description for details.
6579 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
6580 struct lu_buf *buf, loff_t *pos)
6582 struct dt_object *next = dt_object_child(dt);
6584 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
6585 S_ISLNK(dt->do_lu.lo_header->loh_attr));
6586 return next->do_body_ops->dbo_read(env, next, buf, pos);
6590 * Implementation of dt_body_operations::dbo_declare_write.
6592 * \see dt_body_operations::dbo_declare_write() in the API description
6595 static ssize_t lod_declare_write(const struct lu_env *env,
6596 struct dt_object *dt,
6597 const struct lu_buf *buf, loff_t pos,
6600 return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
6604 * Implementation of dt_body_operations::dbo_write.
6606 * \see dt_body_operations::dbo_write() in the API description for details.
6608 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
6609 const struct lu_buf *buf, loff_t *pos,
6610 struct thandle *th, int iq)
6612 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
6613 S_ISLNK(dt->do_lu.lo_header->loh_attr));
6614 return lod_sub_write(env, dt_object_child(dt), buf, pos, th, iq);
6617 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
6618 __u64 start, __u64 end, struct thandle *th)
6620 if (dt_object_remote(dt))
6623 return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
6626 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
6627 __u64 start, __u64 end, struct thandle *th)
6629 if (dt_object_remote(dt))
6632 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
6633 return lod_sub_punch(env, dt_object_child(dt), start, end, th);
6637 * different type of files use the same body_ops because object may be created
6638 * in OUT, where there is no chance to set correct body_ops for each type, so
6639 * body_ops themselves will check file type inside, see lod_read/write/punch for
6642 const struct dt_body_operations lod_body_ops = {
6643 .dbo_read = lod_read,
6644 .dbo_declare_write = lod_declare_write,
6645 .dbo_write = lod_write,
6646 .dbo_declare_punch = lod_declare_punch,
6647 .dbo_punch = lod_punch,
6651 * Implementation of lu_object_operations::loo_object_init.
6653 * The function determines the type and the index of the target device using
6654 * sequence of the object's FID. Then passes control down to the
6655 * corresponding device:
6656 * OSD for the local objects, OSP for remote
6658 * \see lu_object_operations::loo_object_init() in the API description
6661 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
6662 const struct lu_object_conf *conf)
6664 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
6665 struct lu_device *cdev = NULL;
6666 struct lu_object *cobj;
6667 struct lod_tgt_descs *ltd = NULL;
6668 struct lod_tgt_desc *tgt;
6670 int type = LU_SEQ_RANGE_ANY;
6674 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
6676 /* Note: Sometimes, it will Return EAGAIN here, see
6677 * ptrlpc_import_delay_req(), which might confuse
6678 * lu_object_find_at() and make it wait there incorrectly.
6679 * so we convert it to EIO here.*/
6686 if (type == LU_SEQ_RANGE_MDT &&
6687 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
6688 cdev = &lod->lod_child->dd_lu_dev;
6689 } else if (type == LU_SEQ_RANGE_MDT) {
6690 ltd = &lod->lod_mdt_descs;
6692 } else if (type == LU_SEQ_RANGE_OST) {
6693 ltd = &lod->lod_ost_descs;
6700 if (ltd->ltd_tgts_size > idx &&
6701 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
6702 tgt = LTD_TGT(ltd, idx);
6704 LASSERT(tgt != NULL);
6705 LASSERT(tgt->ltd_tgt != NULL);
6707 cdev = &(tgt->ltd_tgt->dd_lu_dev);
6709 lod_putref(lod, ltd);
6712 if (unlikely(cdev == NULL))
6715 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
6716 if (unlikely(cobj == NULL))
6719 lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
6721 lu_object_add(lo, cobj);
6728 * Release resources associated with striping.
6730 * If the object is striped (regular or directory), then release
6731 * the stripe objects references and free the ldo_stripe array.
6733 * \param[in] env execution environment
6734 * \param[in] lo object
6736 void lod_striping_free_nolock(const struct lu_env *env, struct lod_object *lo)
6738 struct lod_layout_component *lod_comp;
6741 if (lo->ldo_stripe != NULL) {
6742 LASSERT(lo->ldo_comp_entries == NULL);
6743 LASSERT(lo->ldo_dir_stripes_allocated > 0);
6745 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6746 if (lo->ldo_stripe[i])
6747 dt_object_put(env, lo->ldo_stripe[i]);
6750 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
6751 OBD_FREE(lo->ldo_stripe, j);
6752 lo->ldo_stripe = NULL;
6753 lo->ldo_dir_stripes_allocated = 0;
6754 lo->ldo_dir_stripe_loaded = 0;
6755 lo->ldo_dir_stripe_count = 0;
6756 } else if (lo->ldo_comp_entries != NULL) {
6757 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6758 /* free lod_layout_component::llc_stripe array */
6759 lod_comp = &lo->ldo_comp_entries[i];
6761 if (lod_comp->llc_stripe == NULL)
6763 LASSERT(lod_comp->llc_stripes_allocated != 0);
6764 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
6765 if (lod_comp->llc_stripe[j] != NULL)
6767 &lod_comp->llc_stripe[j]->do_lu);
6769 OBD_FREE(lod_comp->llc_stripe,
6770 sizeof(struct dt_object *) *
6771 lod_comp->llc_stripes_allocated);
6772 lod_comp->llc_stripe = NULL;
6773 OBD_FREE(lod_comp->llc_ost_indices,
6775 lod_comp->llc_stripes_allocated);
6776 lod_comp->llc_ost_indices = NULL;
6777 lod_comp->llc_stripes_allocated = 0;
6779 lod_free_comp_entries(lo);
6780 lo->ldo_comp_cached = 0;
6784 void lod_striping_free(const struct lu_env *env, struct lod_object *lo)
6786 mutex_lock(&lo->ldo_layout_mutex);
6787 lod_striping_free_nolock(env, lo);
6788 mutex_unlock(&lo->ldo_layout_mutex);
6792 * Implementation of lu_object_operations::loo_object_free.
6794 * \see lu_object_operations::loo_object_free() in the API description
6797 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
6799 struct lod_object *lo = lu2lod_obj(o);
6801 /* release all underlying object pinned */
6802 lod_striping_free(env, lo);
6804 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
6808 * Implementation of lu_object_operations::loo_object_release.
6810 * \see lu_object_operations::loo_object_release() in the API description
6813 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
6815 /* XXX: shouldn't we release everything here in case if object
6816 * creation failed before? */
6820 * Implementation of lu_object_operations::loo_object_print.
6822 * \see lu_object_operations::loo_object_print() in the API description
6825 static int lod_object_print(const struct lu_env *env, void *cookie,
6826 lu_printer_t p, const struct lu_object *l)
6828 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
6830 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
6833 struct lu_object_operations lod_lu_obj_ops = {
6834 .loo_object_init = lod_object_init,
6835 .loo_object_free = lod_object_free,
6836 .loo_object_release = lod_object_release,
6837 .loo_object_print = lod_object_print,