4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * lustre/lod/lod_object.c
31 * This file contains implementations of methods for the OSD API
32 * for the Logical Object Device (LOD) layer, which provides a virtual
33 * local OSD object interface to the MDD layer, and abstracts the
34 * addressing of local (OSD) and remote (OSP) objects. The API is
35 * described in the file lustre/include/dt_object.h and in
36 * Documentation/osd-api.txt.
38 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
41 #define DEBUG_SUBSYSTEM S_MDS
43 #include <linux/random.h>
46 #include <obd_class.h>
47 #include <obd_support.h>
49 #include <lustre_fid.h>
50 #include <lustre_linkea.h>
51 #include <lustre_lmv.h>
52 #include <uapi/linux/lustre/lustre_param.h>
53 #include <lustre_swab.h>
54 #include <uapi/linux/lustre/lustre_ver.h>
55 #include <lprocfs_status.h>
56 #include <md_object.h>
58 #include "lod_internal.h"
60 static const char dot[] = ".";
61 static const char dotdot[] = "..";
64 * Implementation of dt_index_operations::dio_lookup
66 * Used with regular (non-striped) objects.
68 * \see dt_index_operations::dio_lookup() in the API description for details.
70 static int lod_lookup(const struct lu_env *env, struct dt_object *dt,
71 struct dt_rec *rec, const struct dt_key *key)
73 struct dt_object *next = dt_object_child(dt);
74 return next->do_index_ops->dio_lookup(env, next, rec, key);
78 * Implementation of dt_index_operations::dio_declare_insert.
80 * Used with regular (non-striped) objects.
82 * \see dt_index_operations::dio_declare_insert() in the API description
85 static int lod_declare_insert(const struct lu_env *env, struct dt_object *dt,
86 const struct dt_rec *rec,
87 const struct dt_key *key, struct thandle *th)
89 return lod_sub_declare_insert(env, dt_object_child(dt), rec, key, th);
93 * Implementation of dt_index_operations::dio_insert.
95 * Used with regular (non-striped) objects
97 * \see dt_index_operations::dio_insert() in the API description for details.
99 static int lod_insert(const struct lu_env *env, struct dt_object *dt,
100 const struct dt_rec *rec, const struct dt_key *key,
103 return lod_sub_insert(env, dt_object_child(dt), rec, key, th);
107 * Implementation of dt_index_operations::dio_declare_delete.
109 * Used with regular (non-striped) objects.
111 * \see dt_index_operations::dio_declare_delete() in the API description
114 static int lod_declare_delete(const struct lu_env *env, struct dt_object *dt,
115 const struct dt_key *key, struct thandle *th)
117 return lod_sub_declare_delete(env, dt_object_child(dt), key, th);
121 * Implementation of dt_index_operations::dio_delete.
123 * Used with regular (non-striped) objects.
125 * \see dt_index_operations::dio_delete() in the API description for details.
127 static int lod_delete(const struct lu_env *env, struct dt_object *dt,
128 const struct dt_key *key, struct thandle *th)
130 return lod_sub_delete(env, dt_object_child(dt), key, th);
134 * Implementation of dt_it_ops::init.
136 * Used with regular (non-striped) objects.
138 * \see dt_it_ops::init() in the API description for details.
140 static struct dt_it *lod_it_init(const struct lu_env *env,
141 struct dt_object *dt, __u32 attr)
143 struct dt_object *next = dt_object_child(dt);
144 struct lod_it *it = &lod_env_info(env)->lti_it;
145 struct dt_it *it_next;
147 it_next = next->do_index_ops->dio_it.init(env, next, attr);
151 /* currently we do not use more than one iterator per thread
152 * so we store it in thread info. if at some point we need
153 * more active iterators in a single thread, we can allocate
155 LASSERT(it->lit_obj == NULL);
157 it->lit_it = it_next;
160 return (struct dt_it *)it;
163 #define LOD_CHECK_IT(env, it) \
165 LASSERT((it)->lit_obj != NULL); \
166 LASSERT((it)->lit_it != NULL); \
170 * Implementation of dt_index_operations::dio_it.fini.
172 * Used with regular (non-striped) objects.
174 * \see dt_index_operations::dio_it.fini() in the API description for details.
176 static void lod_it_fini(const struct lu_env *env, struct dt_it *di)
178 struct lod_it *it = (struct lod_it *)di;
180 LOD_CHECK_IT(env, it);
181 it->lit_obj->do_index_ops->dio_it.fini(env, it->lit_it);
183 /* the iterator not in use any more */
189 * Implementation of dt_it_ops::get.
191 * Used with regular (non-striped) objects.
193 * \see dt_it_ops::get() in the API description for details.
195 static int lod_it_get(const struct lu_env *env, struct dt_it *di,
196 const struct dt_key *key)
198 const struct lod_it *it = (const struct lod_it *)di;
200 LOD_CHECK_IT(env, it);
201 return it->lit_obj->do_index_ops->dio_it.get(env, it->lit_it, key);
205 * Implementation of dt_it_ops::put.
207 * Used with regular (non-striped) objects.
209 * \see dt_it_ops::put() in the API description for details.
211 static void lod_it_put(const struct lu_env *env, struct dt_it *di)
213 struct lod_it *it = (struct lod_it *)di;
215 LOD_CHECK_IT(env, it);
216 return it->lit_obj->do_index_ops->dio_it.put(env, it->lit_it);
220 * Implementation of dt_it_ops::next.
222 * Used with regular (non-striped) objects
224 * \see dt_it_ops::next() in the API description for details.
226 static int lod_it_next(const struct lu_env *env, struct dt_it *di)
228 struct lod_it *it = (struct lod_it *)di;
230 LOD_CHECK_IT(env, it);
231 return it->lit_obj->do_index_ops->dio_it.next(env, it->lit_it);
235 * Implementation of dt_it_ops::key.
237 * Used with regular (non-striped) objects.
239 * \see dt_it_ops::key() in the API description for details.
241 static struct dt_key *lod_it_key(const struct lu_env *env,
242 const struct dt_it *di)
244 const struct lod_it *it = (const struct lod_it *)di;
246 LOD_CHECK_IT(env, it);
247 return it->lit_obj->do_index_ops->dio_it.key(env, it->lit_it);
251 * Implementation of dt_it_ops::key_size.
253 * Used with regular (non-striped) objects.
255 * \see dt_it_ops::key_size() in the API description for details.
257 static int lod_it_key_size(const struct lu_env *env, const struct dt_it *di)
259 struct lod_it *it = (struct lod_it *)di;
261 LOD_CHECK_IT(env, it);
262 return it->lit_obj->do_index_ops->dio_it.key_size(env, it->lit_it);
266 * Implementation of dt_it_ops::rec.
268 * Used with regular (non-striped) objects.
270 * \see dt_it_ops::rec() in the API description for details.
272 static int lod_it_rec(const struct lu_env *env, const struct dt_it *di,
273 struct dt_rec *rec, __u32 attr)
275 const struct lod_it *it = (const struct lod_it *)di;
277 LOD_CHECK_IT(env, it);
278 return it->lit_obj->do_index_ops->dio_it.rec(env, it->lit_it, rec,
283 * Implementation of dt_it_ops::rec_size.
285 * Used with regular (non-striped) objects.
287 * \see dt_it_ops::rec_size() in the API description for details.
289 static int lod_it_rec_size(const struct lu_env *env, const struct dt_it *di,
292 const struct lod_it *it = (const struct lod_it *)di;
294 LOD_CHECK_IT(env, it);
295 return it->lit_obj->do_index_ops->dio_it.rec_size(env, it->lit_it,
300 * Implementation of dt_it_ops::store.
302 * Used with regular (non-striped) objects.
304 * \see dt_it_ops::store() in the API description for details.
306 static __u64 lod_it_store(const struct lu_env *env, const struct dt_it *di)
308 const struct lod_it *it = (const struct lod_it *)di;
310 LOD_CHECK_IT(env, it);
311 return it->lit_obj->do_index_ops->dio_it.store(env, it->lit_it);
315 * Implementation of dt_it_ops::load.
317 * Used with regular (non-striped) objects.
319 * \see dt_it_ops::load() in the API description for details.
321 static int lod_it_load(const struct lu_env *env, const struct dt_it *di,
324 const struct lod_it *it = (const struct lod_it *)di;
326 LOD_CHECK_IT(env, it);
327 return it->lit_obj->do_index_ops->dio_it.load(env, it->lit_it, hash);
331 * Implementation of dt_it_ops::key_rec.
333 * Used with regular (non-striped) objects.
335 * \see dt_it_ops::rec() in the API description for details.
337 static int lod_it_key_rec(const struct lu_env *env, const struct dt_it *di,
340 const struct lod_it *it = (const struct lod_it *)di;
342 LOD_CHECK_IT(env, it);
343 return it->lit_obj->do_index_ops->dio_it.key_rec(env, it->lit_it,
347 static struct dt_index_operations lod_index_ops = {
348 .dio_lookup = lod_lookup,
349 .dio_declare_insert = lod_declare_insert,
350 .dio_insert = lod_insert,
351 .dio_declare_delete = lod_declare_delete,
352 .dio_delete = lod_delete,
360 .key_size = lod_it_key_size,
362 .rec_size = lod_it_rec_size,
363 .store = lod_it_store,
365 .key_rec = lod_it_key_rec,
370 * Implementation of dt_it_ops::init.
372 * Used with striped objects. Internally just initializes the iterator
373 * on the first stripe.
375 * \see dt_it_ops::init() in the API description for details.
377 static struct dt_it *lod_striped_it_init(const struct lu_env *env,
378 struct dt_object *dt, __u32 attr)
380 struct lod_object *lo = lod_dt_obj(dt);
381 struct dt_object *next;
382 struct lod_it *it = &lod_env_info(env)->lti_it;
383 struct dt_it *it_next;
386 LASSERT(lo->ldo_dir_stripe_count > 0);
389 next = lo->ldo_stripe[index];
390 if (next && dt_object_exists(next))
392 } while (++index < lo->ldo_dir_stripe_count);
394 /* no valid stripe */
395 if (!next || !dt_object_exists(next))
396 return ERR_PTR(-ENODEV);
398 LASSERT(next->do_index_ops != NULL);
400 it_next = next->do_index_ops->dio_it.init(env, next, attr);
404 /* currently we do not use more than one iterator per thread
405 * so we store it in thread info. if at some point we need
406 * more active iterators in a single thread, we can allocate
408 LASSERT(it->lit_obj == NULL);
410 it->lit_stripe_index = index;
412 it->lit_it = it_next;
415 return (struct dt_it *)it;
418 #define LOD_CHECK_STRIPED_IT(env, it, lo) \
420 LASSERT((it)->lit_obj != NULL); \
421 LASSERT((it)->lit_it != NULL); \
422 LASSERT((lo)->ldo_dir_stripe_count > 0); \
423 LASSERT((it)->lit_stripe_index < (lo)->ldo_dir_stripe_count); \
427 * Implementation of dt_it_ops::fini.
429 * Used with striped objects.
431 * \see dt_it_ops::fini() in the API description for details.
433 static void lod_striped_it_fini(const struct lu_env *env, struct dt_it *di)
435 struct lod_it *it = (struct lod_it *)di;
436 struct lod_object *lo = lod_dt_obj(it->lit_obj);
437 struct dt_object *next;
439 /* If lit_it == NULL, then it means the sub_it has been finished,
440 * which only happens in failure cases, see lod_striped_it_next() */
441 if (it->lit_it != NULL) {
442 LOD_CHECK_STRIPED_IT(env, it, lo);
444 next = lo->ldo_stripe[it->lit_stripe_index];
446 LASSERT(next->do_index_ops != NULL);
447 next->do_index_ops->dio_it.fini(env, it->lit_it);
451 /* the iterator not in use any more */
454 it->lit_stripe_index = 0;
458 * Implementation of dt_it_ops::get.
460 * Right now it's not used widely, only to reset the iterator to the
461 * initial position. It should be possible to implement a full version
462 * which chooses a correct stripe to be able to position with any key.
464 * \see dt_it_ops::get() in the API description for details.
466 static int lod_striped_it_get(const struct lu_env *env, struct dt_it *di,
467 const struct dt_key *key)
469 const struct lod_it *it = (const struct lod_it *)di;
470 struct lod_object *lo = lod_dt_obj(it->lit_obj);
471 struct dt_object *next;
473 LOD_CHECK_STRIPED_IT(env, it, lo);
475 next = lo->ldo_stripe[it->lit_stripe_index];
476 LASSERT(next != NULL);
477 LASSERT(dt_object_exists(next));
478 LASSERT(next->do_index_ops != NULL);
480 return next->do_index_ops->dio_it.get(env, it->lit_it, key);
484 * Implementation of dt_it_ops::put.
486 * Used with striped objects.
488 * \see dt_it_ops::put() in the API description for details.
490 static void lod_striped_it_put(const struct lu_env *env, struct dt_it *di)
492 struct lod_it *it = (struct lod_it *)di;
493 struct lod_object *lo = lod_dt_obj(it->lit_obj);
494 struct dt_object *next;
497 * If lit_it == NULL, then it means the sub_it has been finished,
498 * which only happens in failure cases, see lod_striped_it_next()
503 LOD_CHECK_STRIPED_IT(env, it, lo);
505 next = lo->ldo_stripe[it->lit_stripe_index];
506 LASSERT(next != NULL);
507 LASSERT(next->do_index_ops != NULL);
509 return next->do_index_ops->dio_it.put(env, it->lit_it);
513 * Implementation of dt_it_ops::next.
515 * Used with striped objects. When the end of the current stripe is
516 * reached, the method takes the next stripe's iterator.
518 * \see dt_it_ops::next() in the API description for details.
520 static int lod_striped_it_next(const struct lu_env *env, struct dt_it *di)
522 struct lod_it *it = (struct lod_it *)di;
523 struct lod_object *lo = lod_dt_obj(it->lit_obj);
524 struct dt_object *next;
525 struct dt_it *it_next;
531 LOD_CHECK_STRIPED_IT(env, it, lo);
533 next = lo->ldo_stripe[it->lit_stripe_index];
534 LASSERT(next != NULL);
535 LASSERT(dt_object_exists(next));
536 LASSERT(next->do_index_ops != NULL);
538 rc = next->do_index_ops->dio_it.next(env, it->lit_it);
542 if (rc == 0 && it->lit_stripe_index == 0)
545 if (rc == 0 && it->lit_stripe_index > 0) {
546 struct lu_dirent *ent;
548 ent = (struct lu_dirent *)lod_env_info(env)->lti_key;
550 rc = next->do_index_ops->dio_it.rec(env, it->lit_it,
551 (struct dt_rec *)ent,
556 /* skip . and .. for slave stripe */
557 if ((strncmp(ent->lde_name, ".",
558 le16_to_cpu(ent->lde_namelen)) == 0 &&
559 le16_to_cpu(ent->lde_namelen) == 1) ||
560 (strncmp(ent->lde_name, "..",
561 le16_to_cpu(ent->lde_namelen)) == 0 &&
562 le16_to_cpu(ent->lde_namelen) == 2))
568 next->do_index_ops->dio_it.put(env, it->lit_it);
569 next->do_index_ops->dio_it.fini(env, it->lit_it);
572 /* go to next stripe */
573 index = it->lit_stripe_index;
574 while (++index < lo->ldo_dir_stripe_count) {
575 next = lo->ldo_stripe[index];
579 if (!dt_object_exists(next))
582 rc = next->do_ops->do_index_try(env, next,
583 &dt_directory_features);
587 LASSERT(next->do_index_ops != NULL);
589 it_next = next->do_index_ops->dio_it.init(env, next,
592 RETURN(PTR_ERR(it_next));
594 rc = next->do_index_ops->dio_it.get(env, it_next,
595 (const struct dt_key *)"");
597 RETURN(rc == 0 ? -EIO : rc);
599 it->lit_it = it_next;
600 it->lit_stripe_index = index;
609 * Implementation of dt_it_ops::key.
611 * Used with striped objects.
613 * \see dt_it_ops::key() in the API description for details.
615 static struct dt_key *lod_striped_it_key(const struct lu_env *env,
616 const struct dt_it *di)
618 const struct lod_it *it = (const struct lod_it *)di;
619 struct lod_object *lo = lod_dt_obj(it->lit_obj);
620 struct dt_object *next;
622 LOD_CHECK_STRIPED_IT(env, it, lo);
624 next = lo->ldo_stripe[it->lit_stripe_index];
625 LASSERT(next != NULL);
626 LASSERT(next->do_index_ops != NULL);
628 return next->do_index_ops->dio_it.key(env, it->lit_it);
632 * Implementation of dt_it_ops::key_size.
634 * Used with striped objects.
636 * \see dt_it_ops::size() in the API description for details.
638 static int lod_striped_it_key_size(const struct lu_env *env,
639 const struct dt_it *di)
641 struct lod_it *it = (struct lod_it *)di;
642 struct lod_object *lo = lod_dt_obj(it->lit_obj);
643 struct dt_object *next;
645 LOD_CHECK_STRIPED_IT(env, it, lo);
647 next = lo->ldo_stripe[it->lit_stripe_index];
648 LASSERT(next != NULL);
649 LASSERT(next->do_index_ops != NULL);
651 return next->do_index_ops->dio_it.key_size(env, it->lit_it);
655 * Implementation of dt_it_ops::rec.
657 * Used with striped objects.
659 * \see dt_it_ops::rec() in the API description for details.
661 static int lod_striped_it_rec(const struct lu_env *env, const struct dt_it *di,
662 struct dt_rec *rec, __u32 attr)
664 const struct lod_it *it = (const struct lod_it *)di;
665 struct lod_object *lo = lod_dt_obj(it->lit_obj);
666 struct dt_object *next;
668 LOD_CHECK_STRIPED_IT(env, it, lo);
670 next = lo->ldo_stripe[it->lit_stripe_index];
671 LASSERT(next != NULL);
672 LASSERT(next->do_index_ops != NULL);
674 return next->do_index_ops->dio_it.rec(env, it->lit_it, rec, attr);
678 * Implementation of dt_it_ops::rec_size.
680 * Used with striped objects.
682 * \see dt_it_ops::rec_size() in the API description for details.
684 static int lod_striped_it_rec_size(const struct lu_env *env,
685 const struct dt_it *di, __u32 attr)
687 struct lod_it *it = (struct lod_it *)di;
688 struct lod_object *lo = lod_dt_obj(it->lit_obj);
689 struct dt_object *next;
691 LOD_CHECK_STRIPED_IT(env, it, lo);
693 next = lo->ldo_stripe[it->lit_stripe_index];
694 LASSERT(next != NULL);
695 LASSERT(next->do_index_ops != NULL);
697 return next->do_index_ops->dio_it.rec_size(env, it->lit_it, attr);
701 * Implementation of dt_it_ops::store.
703 * Used with striped objects.
705 * \see dt_it_ops::store() in the API description for details.
707 static __u64 lod_striped_it_store(const struct lu_env *env,
708 const struct dt_it *di)
710 const struct lod_it *it = (const struct lod_it *)di;
711 struct lod_object *lo = lod_dt_obj(it->lit_obj);
712 struct dt_object *next;
714 LOD_CHECK_STRIPED_IT(env, it, lo);
716 next = lo->ldo_stripe[it->lit_stripe_index];
717 LASSERT(next != NULL);
718 LASSERT(next->do_index_ops != NULL);
720 return next->do_index_ops->dio_it.store(env, it->lit_it);
724 * Implementation of dt_it_ops::load.
726 * Used with striped objects.
728 * \see dt_it_ops::load() in the API description for details.
730 static int lod_striped_it_load(const struct lu_env *env,
731 const struct dt_it *di, __u64 hash)
733 const struct lod_it *it = (const struct lod_it *)di;
734 struct lod_object *lo = lod_dt_obj(it->lit_obj);
735 struct dt_object *next;
737 LOD_CHECK_STRIPED_IT(env, it, lo);
739 next = lo->ldo_stripe[it->lit_stripe_index];
740 LASSERT(next != NULL);
741 LASSERT(next->do_index_ops != NULL);
743 return next->do_index_ops->dio_it.load(env, it->lit_it, hash);
746 static struct dt_index_operations lod_striped_index_ops = {
747 .dio_lookup = lod_lookup,
748 .dio_declare_insert = lod_declare_insert,
749 .dio_insert = lod_insert,
750 .dio_declare_delete = lod_declare_delete,
751 .dio_delete = lod_delete,
753 .init = lod_striped_it_init,
754 .fini = lod_striped_it_fini,
755 .get = lod_striped_it_get,
756 .put = lod_striped_it_put,
757 .next = lod_striped_it_next,
758 .key = lod_striped_it_key,
759 .key_size = lod_striped_it_key_size,
760 .rec = lod_striped_it_rec,
761 .rec_size = lod_striped_it_rec_size,
762 .store = lod_striped_it_store,
763 .load = lod_striped_it_load,
768 * Append the FID for each shard of the striped directory after the
769 * given LMV EA header.
771 * To simplify striped directory and the consistency verification,
772 * we only store the LMV EA header on disk, for both master object
773 * and slave objects. When someone wants to know the whole LMV EA,
774 * such as client readdir(), we can build the entrie LMV EA on the
775 * MDT side (in RAM) via iterating the sub-directory entries that
776 * are contained in the master object of the stripe directory.
778 * For the master object of the striped directroy, the valid name
779 * for each shard is composed of the ${shard_FID}:${shard_idx}.
781 * There may be holes in the LMV EA if some shards' name entries
782 * are corrupted or lost.
784 * \param[in] env pointer to the thread context
785 * \param[in] lo pointer to the master object of the striped directory
786 * \param[in] buf pointer to the lu_buf which will hold the LMV EA
787 * \param[in] resize whether re-allocate the buffer if it is not big enough
789 * \retval positive size of the LMV EA
790 * \retval 0 for nothing to be loaded
791 * \retval negative error number on failure
793 int lod_load_lmv_shards(const struct lu_env *env, struct lod_object *lo,
794 struct lu_buf *buf, bool resize)
796 struct lu_dirent *ent =
797 (struct lu_dirent *)lod_env_info(env)->lti_key;
798 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
799 struct dt_object *obj = dt_object_child(&lo->ldo_obj);
800 struct lmv_mds_md_v1 *lmv1 = buf->lb_buf;
802 const struct dt_it_ops *iops;
804 __u32 magic = le32_to_cpu(lmv1->lmv_magic);
809 if (magic != LMV_MAGIC_V1)
812 stripes = le32_to_cpu(lmv1->lmv_stripe_count);
816 rc = lmv_mds_md_size(stripes, magic);
820 if (buf->lb_len < lmv1_size) {
829 lu_buf_alloc(buf, lmv1_size);
834 memcpy(buf->lb_buf, tbuf.lb_buf, tbuf.lb_len);
837 if (unlikely(!dt_try_as_dir(env, obj)))
840 memset(&lmv1->lmv_stripe_fids[0], 0, stripes * sizeof(struct lu_fid));
841 iops = &obj->do_index_ops->dio_it;
842 it = iops->init(env, obj, LUDA_64BITHASH);
846 rc = iops->load(env, it, 0);
848 rc = iops->next(env, it);
853 char name[FID_LEN + 2] = "";
858 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
864 fid_le_to_cpu(&fid, &ent->lde_fid);
865 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
866 if (ent->lde_name[0] == '.') {
867 if (ent->lde_namelen == 1)
870 if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
874 len = scnprintf(name, sizeof(name),
875 DFID":", PFID(&ent->lde_fid));
876 /* The ent->lde_name is composed of ${FID}:${index} */
877 if (ent->lde_namelen < len + 1 ||
878 memcmp(ent->lde_name, name, len) != 0) {
879 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
880 "%s: invalid shard name %.*s with the FID "DFID
881 " for the striped directory "DFID", %s\n",
882 lod2obd(lod)->obd_name, ent->lde_namelen,
883 ent->lde_name, PFID(&fid),
884 PFID(lu_object_fid(&obj->do_lu)),
885 lod->lod_lmv_failout ? "failout" : "skip");
887 if (lod->lod_lmv_failout)
895 if (ent->lde_name[len] < '0' ||
896 ent->lde_name[len] > '9') {
897 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
898 "%s: invalid shard name %.*s with the "
899 "FID "DFID" for the striped directory "
901 lod2obd(lod)->obd_name, ent->lde_namelen,
902 ent->lde_name, PFID(&fid),
903 PFID(lu_object_fid(&obj->do_lu)),
904 lod->lod_lmv_failout ?
907 if (lod->lod_lmv_failout)
913 index = index * 10 + ent->lde_name[len++] - '0';
914 } while (len < ent->lde_namelen);
916 if (len == ent->lde_namelen) {
917 /* Out of LMV EA range. */
918 if (index >= stripes) {
919 CERROR("%s: the shard %.*s for the striped "
920 "directory "DFID" is out of the known "
921 "LMV EA range [0 - %u], failout\n",
922 lod2obd(lod)->obd_name, ent->lde_namelen,
924 PFID(lu_object_fid(&obj->do_lu)),
930 /* The slot has been occupied. */
931 if (!fid_is_zero(&lmv1->lmv_stripe_fids[index])) {
935 &lmv1->lmv_stripe_fids[index]);
936 CERROR("%s: both the shard "DFID" and "DFID
937 " for the striped directory "DFID
938 " claim the same LMV EA slot at the "
939 "index %d, failout\n",
940 lod2obd(lod)->obd_name,
941 PFID(&fid0), PFID(&fid),
942 PFID(lu_object_fid(&obj->do_lu)), index);
947 /* stored as LE mode */
948 lmv1->lmv_stripe_fids[index] = ent->lde_fid;
951 rc = iops->next(env, it);
958 RETURN(rc > 0 ? lmv_mds_md_size(stripes, magic) : rc);
962 * Implementation of dt_object_operations::do_index_try.
964 * \see dt_object_operations::do_index_try() in the API description for details.
966 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
967 const struct dt_index_features *feat)
969 struct lod_object *lo = lod_dt_obj(dt);
970 struct dt_object *next = dt_object_child(dt);
974 LASSERT(next->do_ops);
975 LASSERT(next->do_ops->do_index_try);
977 rc = lod_striping_load(env, lo);
981 rc = next->do_ops->do_index_try(env, next, feat);
985 if (lo->ldo_dir_stripe_count > 0) {
988 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
989 if (!lo->ldo_stripe[i])
991 if (!dt_object_exists(lo->ldo_stripe[i]))
993 rc = lo->ldo_stripe[i]->do_ops->do_index_try(env,
994 lo->ldo_stripe[i], feat);
998 dt->do_index_ops = &lod_striped_index_ops;
1000 dt->do_index_ops = &lod_index_ops;
1007 * Implementation of dt_object_operations::do_read_lock.
1009 * \see dt_object_operations::do_read_lock() in the API description for details.
1011 static void lod_read_lock(const struct lu_env *env, struct dt_object *dt,
1014 dt_read_lock(env, dt_object_child(dt), role);
1018 * Implementation of dt_object_operations::do_write_lock.
1020 * \see dt_object_operations::do_write_lock() in the API description for
1023 static void lod_write_lock(const struct lu_env *env, struct dt_object *dt,
1026 dt_write_lock(env, dt_object_child(dt), role);
1030 * Implementation of dt_object_operations::do_read_unlock.
1032 * \see dt_object_operations::do_read_unlock() in the API description for
1035 static void lod_read_unlock(const struct lu_env *env, struct dt_object *dt)
1037 dt_read_unlock(env, dt_object_child(dt));
1041 * Implementation of dt_object_operations::do_write_unlock.
1043 * \see dt_object_operations::do_write_unlock() in the API description for
1046 static void lod_write_unlock(const struct lu_env *env, struct dt_object *dt)
1048 dt_write_unlock(env, dt_object_child(dt));
1052 * Implementation of dt_object_operations::do_write_locked.
1054 * \see dt_object_operations::do_write_locked() in the API description for
1057 static int lod_write_locked(const struct lu_env *env, struct dt_object *dt)
1059 return dt_write_locked(env, dt_object_child(dt));
1063 * Implementation of dt_object_operations::do_attr_get.
1065 * \see dt_object_operations::do_attr_get() in the API description for details.
1067 static int lod_attr_get(const struct lu_env *env,
1068 struct dt_object *dt,
1069 struct lu_attr *attr)
1071 /* Note: for striped directory, client will merge attributes
1072 * from all of the sub-stripes see lmv_merge_attr(), and there
1073 * no MDD logic depend on directory nlink/size/time, so we can
1074 * always use master inode nlink and size for now. */
1075 return dt_attr_get(env, dt_object_child(dt), attr);
1078 void lod_adjust_stripe_size(struct lod_layout_component *comp,
1079 __u32 def_stripe_size)
1081 __u64 comp_end = comp->llc_extent.e_end;
1083 /* Choose stripe size if not set. Note that default stripe size can't
1084 * be used as is, because it must be multiplier of given component end.
1085 * - first check if default stripe size can be used
1086 * - if not than select the lowest set bit from component end and use
1087 * that value as stripe size
1089 if (!comp->llc_stripe_size) {
1090 if (comp_end == LUSTRE_EOF || !(comp_end % def_stripe_size))
1091 comp->llc_stripe_size = def_stripe_size;
1093 comp->llc_stripe_size = comp_end & ~(comp_end - 1);
1095 /* check stripe size is multiplier of comp_end */
1096 if (comp_end != LUSTRE_EOF &&
1097 comp_end % comp->llc_stripe_size) {
1098 /* fix that even for defined stripe size but warn
1099 * about the problem, that must not happen
1101 CWARN("Component end %llu is not aligned by the stripe size %u\n",
1102 comp_end, comp->llc_stripe_size);
1104 comp->llc_stripe_size = comp_end & ~(comp_end - 1);
1109 static inline void lod_adjust_stripe_info(struct lod_layout_component *comp,
1110 struct lov_desc *desc,
1113 if (comp->llc_pattern != LOV_PATTERN_MDT) {
1114 if (append_stripes) {
1115 comp->llc_stripe_count = append_stripes;
1116 } else if (!comp->llc_stripe_count) {
1117 comp->llc_stripe_count =
1118 desc->ld_default_stripe_count;
1122 lod_adjust_stripe_size(comp, desc->ld_default_stripe_size);
1125 int lod_obj_for_each_stripe(const struct lu_env *env, struct lod_object *lo,
1127 struct lod_obj_stripe_cb_data *data)
1129 struct lod_layout_component *lod_comp;
1133 LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
1134 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1135 lod_comp = &lo->ldo_comp_entries[i];
1137 if (lod_comp->llc_stripe == NULL)
1140 /* has stripe but not inited yet, this component has been
1141 * declared to be created, but hasn't created yet.
1143 if (!lod_comp_inited(lod_comp))
1146 if (data->locd_comp_skip_cb &&
1147 data->locd_comp_skip_cb(env, lo, i, data))
1150 if (data->locd_comp_cb) {
1151 rc = data->locd_comp_cb(env, lo, i, data);
1156 /* could used just to do sth about component, not each
1159 if (!data->locd_stripe_cb)
1162 LASSERT(lod_comp->llc_stripe_count > 0);
1163 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
1164 struct dt_object *dt = lod_comp->llc_stripe[j];
1168 rc = data->locd_stripe_cb(env, lo, dt, th, i, j, data);
1176 static bool lod_obj_attr_set_comp_skip_cb(const struct lu_env *env,
1177 struct lod_object *lo, int comp_idx,
1178 struct lod_obj_stripe_cb_data *data)
1180 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
1181 bool skipped = false;
1183 if (!(data->locd_attr->la_valid & LA_LAYOUT_VERSION))
1186 switch (lo->ldo_flr_state) {
1187 case LCM_FL_WRITE_PENDING: {
1190 /* skip stale components */
1191 if (lod_comp->llc_flags & LCME_FL_STALE) {
1196 /* skip valid and overlapping components, therefore any
1197 * attempts to write overlapped components will never succeed
1198 * because client will get EINPROGRESS. */
1199 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1203 if (lo->ldo_comp_entries[i].llc_flags & LCME_FL_STALE)
1206 if (lu_extent_is_overlapped(&lod_comp->llc_extent,
1207 &lo->ldo_comp_entries[i].llc_extent)) {
1215 LASSERTF(0, "impossible: %d\n", lo->ldo_flr_state);
1216 case LCM_FL_SYNC_PENDING:
1220 CDEBUG(D_LAYOUT, DFID": %s to set component %x to version: %u\n",
1221 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
1222 skipped ? "skipped" : "chose", lod_comp->llc_id,
1223 data->locd_attr->la_layout_version);
1229 lod_obj_stripe_attr_set_cb(const struct lu_env *env, struct lod_object *lo,
1230 struct dt_object *dt, struct thandle *th,
1231 int comp_idx, int stripe_idx,
1232 struct lod_obj_stripe_cb_data *data)
1234 if (data->locd_declare)
1235 return lod_sub_declare_attr_set(env, dt, data->locd_attr, th);
1237 if (data->locd_attr->la_valid & LA_LAYOUT_VERSION) {
1238 CDEBUG(D_LAYOUT, DFID": set layout version: %u, comp_idx: %d\n",
1239 PFID(lu_object_fid(&dt->do_lu)),
1240 data->locd_attr->la_layout_version, comp_idx);
1243 return lod_sub_attr_set(env, dt, data->locd_attr, th);
1247 * Implementation of dt_object_operations::do_declare_attr_set.
1249 * If the object is striped, then apply the changes to all the stripes.
1251 * \see dt_object_operations::do_declare_attr_set() in the API description
1254 static int lod_declare_attr_set(const struct lu_env *env,
1255 struct dt_object *dt,
1256 const struct lu_attr *attr,
1259 struct dt_object *next = dt_object_child(dt);
1260 struct lod_object *lo = lod_dt_obj(dt);
1265 * declare setattr on the local object
1267 rc = lod_sub_declare_attr_set(env, next, attr, th);
1271 /* osp_declare_attr_set() ignores all attributes other than
1272 * UID, GID, PROJID, and size, and osp_attr_set() ignores all
1273 * but UID, GID and PROJID. Declaration of size attr setting
1274 * happens through lod_declare_init_size(), and not through
1275 * this function. Therefore we need not load striping unless
1276 * ownership is changing. This should save memory and (we hope)
1277 * speed up rename().
1279 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1280 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1283 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1286 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID | LA_MODE |
1287 LA_ATIME | LA_MTIME | LA_CTIME |
1292 * load striping information, notice we don't do this when object
1293 * is being initialized as we don't need this information till
1294 * few specific cases like destroy, chown
1296 rc = lod_striping_load(env, lo);
1300 if (!lod_obj_is_striped(dt))
1304 * if object is striped declare changes on the stripes
1306 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1307 LASSERT(lo->ldo_stripe);
1308 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1309 if (lo->ldo_stripe[i] == NULL)
1311 if (!dt_object_exists(lo->ldo_stripe[i]))
1313 rc = lod_sub_declare_attr_set(env, lo->ldo_stripe[i],
1319 struct lod_obj_stripe_cb_data data = { { 0 } };
1321 data.locd_attr = attr;
1322 data.locd_declare = true;
1323 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1324 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1330 if (!dt_object_exists(next) || dt_object_remote(next) ||
1331 !S_ISREG(attr->la_mode))
1334 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1335 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
1339 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1340 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1341 struct lod_thread_info *info = lod_env_info(env);
1342 struct lu_buf *buf = &info->lti_buf;
1344 buf->lb_buf = info->lti_ea_store;
1345 buf->lb_len = info->lti_ea_store_size;
1346 rc = lod_sub_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1347 LU_XATTR_REPLACE, th);
1354 * Implementation of dt_object_operations::do_attr_set.
1356 * If the object is striped, then apply the changes to all or subset of
1357 * the stripes depending on the object type and specific attributes.
1359 * \see dt_object_operations::do_attr_set() in the API description for details.
1361 static int lod_attr_set(const struct lu_env *env,
1362 struct dt_object *dt,
1363 const struct lu_attr *attr,
1366 struct dt_object *next = dt_object_child(dt);
1367 struct lod_object *lo = lod_dt_obj(dt);
1372 * apply changes to the local object
1374 rc = lod_sub_attr_set(env, next, attr, th);
1378 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1379 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1382 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1385 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE | LA_PROJID |
1386 LA_ATIME | LA_MTIME | LA_CTIME |
1391 /* FIXME: a tricky case in the code path of mdd_layout_change():
1392 * the in-memory striping information has been freed in lod_xattr_set()
1393 * due to layout change. It has to load stripe here again. It only
1394 * changes flags of layout so declare_attr_set() is still accurate */
1395 rc = lod_striping_load(env, lo);
1399 if (!lod_obj_is_striped(dt))
1403 * if object is striped, apply changes to all the stripes
1405 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1406 LASSERT(lo->ldo_stripe);
1407 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1408 if (unlikely(lo->ldo_stripe[i] == NULL))
1411 if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1414 rc = lod_sub_attr_set(env, lo->ldo_stripe[i], attr, th);
1419 struct lod_obj_stripe_cb_data data = { { 0 } };
1421 data.locd_attr = attr;
1422 data.locd_declare = false;
1423 data.locd_comp_skip_cb = lod_obj_attr_set_comp_skip_cb;
1424 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1425 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1431 if (!dt_object_exists(next) || dt_object_remote(next) ||
1432 !S_ISREG(attr->la_mode))
1435 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1436 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
1440 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1441 struct lod_thread_info *info = lod_env_info(env);
1442 struct lu_buf *buf = &info->lti_buf;
1443 struct ost_id *oi = &info->lti_ostid;
1444 struct lu_fid *fid = &info->lti_fid;
1445 struct lov_mds_md_v1 *lmm;
1446 struct lov_ost_data_v1 *objs;
1449 rc = lod_get_lov_ea(env, lo);
1453 buf->lb_buf = info->lti_ea_store;
1454 buf->lb_len = info->lti_ea_store_size;
1455 lmm = info->lti_ea_store;
1456 magic = le32_to_cpu(lmm->lmm_magic);
1457 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
1458 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1459 struct lov_comp_md_entry_v1 *lcme =
1460 &lcm->lcm_entries[0];
1462 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1463 magic = le32_to_cpu(lmm->lmm_magic);
1466 if (magic == LOV_MAGIC_V1)
1467 objs = &(lmm->lmm_objects[0]);
1469 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1470 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1471 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1473 fid_to_ostid(fid, oi);
1474 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1476 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1477 LU_XATTR_REPLACE, th);
1478 } else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1479 struct lod_thread_info *info = lod_env_info(env);
1480 struct lu_buf *buf = &info->lti_buf;
1481 struct lov_comp_md_v1 *lcm;
1482 struct lov_comp_md_entry_v1 *lcme;
1484 rc = lod_get_lov_ea(env, lo);
1488 buf->lb_buf = info->lti_ea_store;
1489 buf->lb_len = info->lti_ea_store_size;
1491 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1 &&
1492 le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_SEL)
1495 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1496 lcme = &lcm->lcm_entries[0];
1497 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1498 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1500 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1501 LU_XATTR_REPLACE, th);
1508 * Implementation of dt_object_operations::do_xattr_get.
1510 * If LOV EA is requested from the root object and it's not
1511 * found, then return default striping for the filesystem.
1513 * \see dt_object_operations::do_xattr_get() in the API description for details.
1515 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1516 struct lu_buf *buf, const char *name)
1518 struct lod_thread_info *info = lod_env_info(env);
1519 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1524 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1525 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1526 struct lmv_mds_md_v1 *lmv1;
1527 struct lmv_foreign_md *lfm;
1530 if (rc > (typeof(rc))sizeof(*lmv1))
1533 /* short (<= sizeof(struct lmv_mds_md_v1)) foreign LMV case */
1534 /* XXX empty foreign LMV is not allowed */
1535 if (rc <= offsetof(typeof(*lfm), lfm_value))
1536 RETURN(rc = rc > 0 ? -EINVAL : rc);
1538 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1539 BUILD_BUG_ON(sizeof(*lmv1) > sizeof(info->lti_key));
1541 /* lti_buf is large enough for *lmv1 or a short
1542 * (<= sizeof(struct lmv_mds_md_v1)) foreign LMV
1544 info->lti_buf.lb_buf = info->lti_key;
1545 info->lti_buf.lb_len = sizeof(*lmv1);
1546 rc = dt_xattr_get(env, dt_object_child(dt),
1547 &info->lti_buf, name);
1548 if (unlikely(rc <= offsetof(typeof(*lfm),
1550 RETURN(rc = rc > 0 ? -EINVAL : rc);
1552 lfm = info->lti_buf.lb_buf;
1553 if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN)
1556 if (unlikely(rc != sizeof(*lmv1)))
1557 RETURN(rc = rc > 0 ? -EINVAL : rc);
1559 lmv1 = info->lti_buf.lb_buf;
1560 /* The on-disk LMV EA only contains header, but the
1561 * returned LMV EA size should contain the space for
1562 * the FIDs of all shards of the striped directory. */
1563 if (lmv_is_sane(lmv1))
1564 rc = lmv_mds_md_size(
1565 le32_to_cpu(lmv1->lmv_stripe_count),
1566 le32_to_cpu(lmv1->lmv_magic));
1569 if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN)
1572 if (rc != sizeof(*lmv1))
1573 RETURN(rc = rc > 0 ? -EINVAL : rc);
1575 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1579 RETURN(rc = rc1 != 0 ? rc1 : rc);
1582 if ((rc > 0) && buf->lb_buf && strcmp(name, XATTR_NAME_LOV) == 0) {
1583 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1585 if (lcm->lcm_magic == cpu_to_le32(LOV_MAGIC_SEL))
1586 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1589 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1593 * XXX: Only used by lfsck
1595 * lod returns default striping on the real root of the device
1596 * this is like the root stores default striping for the whole
1597 * filesystem. historically we've been using a different approach
1598 * and store it in the config.
1600 dt_root_get(env, dev->lod_child, &info->lti_fid);
1601 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1603 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1604 struct lov_user_md *lum = buf->lb_buf;
1605 struct lov_desc *desc = &dev->lod_ost_descs.ltd_lov_desc;
1607 if (buf->lb_buf == NULL) {
1609 } else if (buf->lb_len >= sizeof(*lum)) {
1610 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1611 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1612 lmm_oi_set_id(&lum->lmm_oi, 0);
1613 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1614 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1615 lum->lmm_stripe_size = cpu_to_le32(
1616 desc->ld_default_stripe_size);
1617 lum->lmm_stripe_count = cpu_to_le16(
1618 desc->ld_default_stripe_count);
1619 lum->lmm_stripe_offset = cpu_to_le16(
1620 desc->ld_default_stripe_offset);
1633 * Checks that the magic of the stripe is sane.
1635 * \param[in] lod lod device
1636 * \param[in] lum a buffer storing LMV EA to verify
1638 * \retval 0 if the EA is sane
1639 * \retval negative otherwise
1641 static int lod_verify_md_striping(struct lod_device *lod,
1642 const struct lmv_user_md_v1 *lum)
1644 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1645 CERROR("%s: invalid lmv_user_md: magic = %x, "
1646 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1647 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1648 (int)le32_to_cpu(lum->lum_stripe_offset),
1649 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1657 * Initialize LMV EA for a slave.
1659 * Initialize slave's LMV EA from the master's LMV EA.
1661 * \param[in] master_lmv a buffer containing master's EA
1662 * \param[out] slave_lmv a buffer where slave's EA will be stored
1665 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1666 const struct lmv_mds_md_v1 *master_lmv)
1668 *slave_lmv = *master_lmv;
1669 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1675 * Generate LMV EA from the object passed as \a dt. The object must have
1676 * the stripes created and initialized.
1678 * \param[in] env execution environment
1679 * \param[in] dt object
1680 * \param[out] lmv_buf buffer storing generated LMV EA
1682 * \retval 0 on success
1683 * \retval negative if failed
1685 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1686 struct lu_buf *lmv_buf)
1688 struct lod_thread_info *info = lod_env_info(env);
1689 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1690 struct lod_object *lo = lod_dt_obj(dt);
1691 struct lmv_mds_md_v1 *lmm1;
1693 int type = LU_SEQ_RANGE_ANY;
1698 LASSERT(lo->ldo_dir_striped != 0);
1699 LASSERT(lo->ldo_dir_stripe_count > 0);
1700 stripe_count = lo->ldo_dir_stripe_count;
1701 /* Only store the LMV EA heahder on the disk. */
1702 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1703 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1707 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1710 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1711 memset(lmm1, 0, sizeof(*lmm1));
1712 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1713 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1714 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1715 if (lo->ldo_dir_hash_type & LMV_HASH_FLAG_MIGRATION) {
1716 lmm1->lmv_migrate_hash = cpu_to_le32(lo->ldo_dir_migrate_hash);
1717 lmm1->lmv_migrate_offset =
1718 cpu_to_le32(lo->ldo_dir_migrate_offset);
1720 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1725 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1726 lmv_buf->lb_buf = info->lti_ea_store;
1727 lmv_buf->lb_len = sizeof(*lmm1);
1733 * Create in-core represenation for a striped directory.
1735 * Parse the buffer containing LMV EA and instantiate LU objects
1736 * representing the stripe objects. The pointers to the objects are
1737 * stored in ldo_stripe field of \a lo. This function is used when
1738 * we need to access an already created object (i.e. load from a disk).
1740 * \param[in] env execution environment
1741 * \param[in] lo lod object
1742 * \param[in] buf buffer containing LMV EA
1744 * \retval 0 on success
1745 * \retval negative if failed
1747 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1748 const struct lu_buf *buf)
1750 struct lod_thread_info *info = lod_env_info(env);
1751 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1752 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1753 struct dt_object **stripe;
1754 union lmv_mds_md *lmm = buf->lb_buf;
1755 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1756 struct lu_fid *fid = &info->lti_fid;
1761 LASSERT(mutex_is_locked(&lo->ldo_layout_mutex));
1763 /* XXX may be useless as not called for foreign LMV ?? */
1764 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_FOREIGN)
1767 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1768 lo->ldo_dir_slave_stripe = 1;
1772 if (!lmv_is_sane(lmv1))
1775 LASSERT(lo->ldo_stripe == NULL);
1776 OBD_ALLOC(stripe, sizeof(stripe[0]) *
1777 (le32_to_cpu(lmv1->lmv_stripe_count)));
1781 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1782 struct dt_device *tgt_dt;
1783 struct dt_object *dto;
1784 int type = LU_SEQ_RANGE_ANY;
1787 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1788 if (!fid_is_sane(fid)) {
1793 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1797 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1798 tgt_dt = lod->lod_child;
1800 struct lod_tgt_desc *tgt;
1802 tgt = LTD_TGT(ltd, idx);
1804 GOTO(out, rc = -ESTALE);
1805 tgt_dt = tgt->ltd_tgt;
1808 dto = dt_locate_at(env, tgt_dt, fid,
1809 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1812 GOTO(out, rc = PTR_ERR(dto));
1817 lo->ldo_stripe = stripe;
1818 lo->ldo_dir_stripe_count = le32_to_cpu(lmv1->lmv_stripe_count);
1819 lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1820 lo->ldo_dir_layout_version = le32_to_cpu(lmv1->lmv_layout_version);
1821 lo->ldo_dir_hash_type = le32_to_cpu(lmv1->lmv_hash_type);
1823 lod_striping_free_nolock(env, lo);
1829 * Declare create a striped directory.
1831 * Declare creating a striped directory with a given stripe pattern on the
1832 * specified MDTs. A striped directory is represented as a regular directory
1833 * - an index listing all the stripes. The stripes point back to the master
1834 * object with ".." and LinkEA. The master object gets LMV EA which
1835 * identifies it as a striped directory. The function allocates FIDs
1838 * \param[in] env execution environment
1839 * \param[in] dt object
1840 * \param[in] attr attributes to initialize the objects with
1841 * \param[in] dof type of objects to be created
1842 * \param[in] th transaction handle
1844 * \retval 0 on success
1845 * \retval negative if failed
1847 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1848 struct dt_object *dt,
1849 struct lu_attr *attr,
1850 struct dt_object_format *dof,
1853 struct lod_thread_info *info = lod_env_info(env);
1854 struct lu_buf lmv_buf;
1855 struct lu_buf slave_lmv_buf;
1856 struct lmv_mds_md_v1 *lmm;
1857 struct lmv_mds_md_v1 *slave_lmm = NULL;
1858 struct dt_insert_rec *rec = &info->lti_dt_rec;
1859 struct lod_object *lo = lod_dt_obj(dt);
1864 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1867 lmm = lmv_buf.lb_buf;
1869 OBD_ALLOC_PTR(slave_lmm);
1870 if (slave_lmm == NULL)
1871 GOTO(out, rc = -ENOMEM);
1873 lod_prep_slave_lmv_md(slave_lmm, lmm);
1874 slave_lmv_buf.lb_buf = slave_lmm;
1875 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1877 if (!dt_try_as_dir(env, dt_object_child(dt)))
1878 GOTO(out, rc = -EINVAL);
1880 rec->rec_type = S_IFDIR;
1881 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1882 struct dt_object *dto = lo->ldo_stripe[i];
1883 char *stripe_name = info->lti_key;
1884 struct lu_name *sname;
1885 struct linkea_data ldata = { NULL };
1886 struct lu_buf linkea_buf;
1888 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
1892 rc = lod_sub_declare_create(env, dto, attr, NULL, dof, th);
1896 if (!dt_try_as_dir(env, dto))
1897 GOTO(out, rc = -EINVAL);
1899 rc = lod_sub_declare_ref_add(env, dto, th);
1903 rec->rec_fid = lu_object_fid(&dto->do_lu);
1904 rc = lod_sub_declare_insert(env, dto,
1905 (const struct dt_rec *)rec,
1906 (const struct dt_key *)dot, th);
1910 /* master stripe FID will be put to .. */
1911 rec->rec_fid = lu_object_fid(&dt->do_lu);
1912 rc = lod_sub_declare_insert(env, dto,
1913 (const struct dt_rec *)rec,
1914 (const struct dt_key *)dotdot, th);
1918 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1919 cfs_fail_val != i) {
1920 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1922 slave_lmm->lmv_master_mdt_index =
1925 slave_lmm->lmv_master_mdt_index =
1927 rc = lod_sub_declare_xattr_set(env, dto, &slave_lmv_buf,
1928 XATTR_NAME_LMV, 0, th);
1933 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1935 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1936 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1938 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1939 PFID(lu_object_fid(&dto->do_lu)), i);
1941 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1942 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1943 sname, lu_object_fid(&dt->do_lu));
1947 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1948 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1949 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
1950 XATTR_NAME_LINK, 0, th);
1954 rec->rec_fid = lu_object_fid(&dto->do_lu);
1955 rc = lod_sub_declare_insert(env, dt_object_child(dt),
1956 (const struct dt_rec *)rec,
1957 (const struct dt_key *)stripe_name,
1962 rc = lod_sub_declare_ref_add(env, dt_object_child(dt), th);
1967 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt),
1968 &lmv_buf, XATTR_NAME_LMV, 0, th);
1972 if (slave_lmm != NULL)
1973 OBD_FREE_PTR(slave_lmm);
1979 * Allocate a striping on a predefined set of MDTs.
1981 * Allocates new striping using the MDT index range provided by the data from
1982 * the lum_obejcts contained in the lmv_user_md passed to this method if
1983 * \a is_specific is true; or allocates new layout starting from MDT index in
1984 * lo->ldo_dir_stripe_offset. The exact order of MDTs is not important and
1985 * varies depending on MDT status. The number of stripes needed and stripe
1986 * offset are taken from the object. If that number cannot be met, then the
1987 * function returns an error and then it's the caller's responsibility to
1988 * release the stripes allocated. All the internal structures are protected,
1989 * but no concurrent allocation is allowed on the same objects.
1991 * \param[in] env execution environment for this thread
1992 * \param[in] lo LOD object
1993 * \param[out] stripes striping created
1994 * \param[out] mdt_indices MDT indices of striping created
1995 * \param[in] is_specific true if the MDTs are provided by lum; false if
1996 * only the starting MDT index is provided
1998 * \retval positive stripes allocated, including the first stripe allocated
2000 * \retval negative errno on failure
2002 static int lod_mdt_alloc_specific(const struct lu_env *env,
2003 struct lod_object *lo,
2004 struct dt_object **stripes,
2005 __u32 *mdt_indices, bool is_specific)
2007 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
2008 struct lu_tgt_descs *ltd = &lod->lod_mdt_descs;
2009 struct lu_tgt_desc *tgt = NULL;
2010 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2011 struct dt_device *tgt_dt = NULL;
2012 struct lu_fid fid = { 0 };
2013 struct dt_object *dto;
2015 u32 stripe_count = lo->ldo_dir_stripe_count;
2021 master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
2022 if (stripe_count > 1)
2023 /* Set the start index for the 2nd stripe allocation */
2024 mdt_indices[1] = (mdt_indices[0] + 1) %
2025 (lod->lod_remote_mdt_count + 1);
2027 for (; stripe_idx < stripe_count; stripe_idx++) {
2028 /* Try to find next avaible target */
2029 idx = mdt_indices[stripe_idx];
2030 for (j = 0; j < lod->lod_remote_mdt_count;
2031 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
2032 bool already_allocated = false;
2035 CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
2036 idx, lod->lod_remote_mdt_count + 1, stripe_idx);
2038 if (likely(!is_specific &&
2039 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
2040 /* check whether the idx already exists
2041 * in current allocated array */
2042 for (k = 0; k < stripe_idx; k++) {
2043 if (mdt_indices[k] == idx) {
2044 already_allocated = true;
2049 if (already_allocated)
2053 /* Sigh, this index is not in the bitmap, let's check
2054 * next available target */
2055 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) &&
2056 idx != master_index)
2059 if (idx == master_index) {
2060 /* Allocate the FID locally */
2061 rc = obd_fid_alloc(env, lod->lod_child_exp,
2065 tgt_dt = lod->lod_child;
2069 /* check the status of the OSP */
2070 tgt = LTD_TGT(ltd, idx);
2074 tgt_dt = tgt->ltd_tgt;
2075 if (!tgt->ltd_active)
2076 /* this OSP doesn't feel well */
2079 rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
2086 /* Can not allocate more stripes */
2087 if (j == lod->lod_remote_mdt_count) {
2088 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
2089 lod2obd(lod)->obd_name, stripe_count,
2094 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
2095 idx, stripe_idx, PFID(&fid));
2096 mdt_indices[stripe_idx] = idx;
2097 /* Set the start index for next stripe allocation */
2098 if (!is_specific && stripe_idx < stripe_count - 1) {
2100 * for large dir test, put all other slaves on one
2101 * remote MDT, otherwise we may save too many local
2102 * slave locks which will exceed RS_MAX_LOCKS.
2104 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)))
2106 mdt_indices[stripe_idx + 1] = (idx + 1) %
2107 (lod->lod_remote_mdt_count + 1);
2109 /* tgt_dt and fid must be ready after search avaible OSP
2110 * in the above loop */
2111 LASSERT(tgt_dt != NULL);
2112 LASSERT(fid_is_sane(&fid));
2114 /* fail a remote stripe FID allocation */
2115 if (stripe_idx && OBD_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_FID))
2118 dto = dt_locate_at(env, tgt_dt, &fid,
2119 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
2126 stripes[stripe_idx] = dto;
2132 for (j = 1; j < stripe_idx; j++) {
2133 LASSERT(stripes[j] != NULL);
2134 dt_object_put(env, stripes[j]);
2140 static int lod_prep_md_striped_create(const struct lu_env *env,
2141 struct dt_object *dt,
2142 struct lu_attr *attr,
2143 const struct lmv_user_md_v1 *lum,
2144 struct dt_object_format *dof,
2147 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
2148 struct lod_object *lo = lod_dt_obj(dt);
2149 struct dt_object **stripes;
2150 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2151 struct lu_fid fid = { 0 };
2158 /* The lum has been verifed in lod_verify_md_striping */
2159 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC ||
2160 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC);
2162 stripe_count = lo->ldo_dir_stripe_count;
2164 OBD_ALLOC(stripes, sizeof(stripes[0]) * stripe_count);
2168 /* Allocate the first stripe locally */
2169 rc = obd_fid_alloc(env, lod->lod_child_exp, &fid, NULL);
2173 stripes[0] = dt_locate_at(env, lod->lod_child, &fid,
2174 dt->do_lu.lo_dev->ld_site->ls_top_dev, &conf);
2175 if (IS_ERR(stripes[0]))
2176 GOTO(out, rc = PTR_ERR(stripes[0]));
2178 if (lo->ldo_dir_stripe_offset == LMV_OFFSET_DEFAULT) {
2179 lod_qos_statfs_update(env, lod, &lod->lod_mdt_descs);
2180 rc = lod_mdt_alloc_qos(env, lo, stripes);
2182 rc = lod_mdt_alloc_rr(env, lo, stripes);
2185 bool is_specific = false;
2187 OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
2189 GOTO(out, rc = -ENOMEM);
2191 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) {
2193 for (i = 0; i < stripe_count; i++)
2195 le32_to_cpu(lum->lum_objects[i].lum_mds);
2198 /* stripe 0 is local */
2200 lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
2201 rc = lod_mdt_alloc_specific(env, lo, stripes, idx_array,
2203 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
2211 lo->ldo_dir_striped = 1;
2212 lo->ldo_stripe = stripes;
2213 lo->ldo_dir_stripe_count = rc;
2214 lo->ldo_dir_stripes_allocated = stripe_count;
2216 lo->ldo_dir_stripe_loaded = 1;
2218 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
2220 lod_striping_free(env, lo);
2226 if (!IS_ERR_OR_NULL(stripes[0]))
2227 dt_object_put(env, stripes[0]);
2228 for (i = 1; i < stripe_count; i++)
2229 LASSERT(!stripes[i]);
2230 OBD_FREE(stripes, sizeof(stripes[0]) * stripe_count);
2237 * Alloc cached foreign LMV
2239 * \param[in] lo object
2240 * \param[in] size size of foreign LMV
2242 * \retval 0 on success
2243 * \retval negative if failed
2245 int lod_alloc_foreign_lmv(struct lod_object *lo, size_t size)
2247 OBD_ALLOC_LARGE(lo->ldo_foreign_lmv, size);
2248 if (lo->ldo_foreign_lmv == NULL)
2250 lo->ldo_foreign_lmv_size = size;
2251 lo->ldo_dir_is_foreign = 1;
2257 * Declare create striped md object.
2259 * The function declares intention to create a striped directory. This is a
2260 * wrapper for lod_prep_md_striped_create(). The only additional functionality
2261 * is to verify pattern \a lum_buf is good. Check that function for the details.
2263 * \param[in] env execution environment
2264 * \param[in] dt object
2265 * \param[in] attr attributes to initialize the objects with
2266 * \param[in] lum_buf a pattern specifying the number of stripes and
2268 * \param[in] dof type of objects to be created
2269 * \param[in] th transaction handle
2271 * \retval 0 on success
2272 * \retval negative if failed
2275 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
2276 struct dt_object *dt,
2277 struct lu_attr *attr,
2278 const struct lu_buf *lum_buf,
2279 struct dt_object_format *dof,
2282 struct lod_object *lo = lod_dt_obj(dt);
2283 struct lmv_user_md_v1 *lum = lum_buf->lb_buf;
2287 LASSERT(lum != NULL);
2289 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
2290 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
2291 (int)le32_to_cpu(lum->lum_stripe_offset));
2293 if (lo->ldo_dir_stripe_count == 0) {
2294 if (lo->ldo_dir_is_foreign) {
2295 rc = lod_alloc_foreign_lmv(lo, lum_buf->lb_len);
2298 memcpy(lo->ldo_foreign_lmv, lum, lum_buf->lb_len);
2299 lo->ldo_dir_stripe_loaded = 1;
2304 /* prepare dir striped objects */
2305 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2307 /* failed to create striping, let's reset
2308 * config so that others don't get confused */
2309 lod_striping_free(env, lo);
2317 * Set or replace striped directory layout, and LFSCK may set layout on a plain
2318 * directory, so don't check stripe count.
2320 * \param[in] env execution environment
2321 * \param[in] dt target object
2322 * \param[in] buf LMV buf which contains source stripe fids
2323 * \param[in] fl set or replace
2324 * \param[in] th transaction handle
2326 * \retval 0 on success
2327 * \retval negative if failed
2329 static int lod_dir_layout_set(const struct lu_env *env,
2330 struct dt_object *dt,
2331 const struct lu_buf *buf,
2335 struct dt_object *next = dt_object_child(dt);
2336 struct lod_object *lo = lod_dt_obj(dt);
2337 struct lmv_mds_md_v1 *lmv = buf->lb_buf;
2338 struct lmv_mds_md_v1 *slave_lmv;
2339 struct lu_buf slave_buf;
2345 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LMV, fl, th);
2349 OBD_ALLOC_PTR(slave_lmv);
2353 lod_prep_slave_lmv_md(slave_lmv, lmv);
2354 slave_buf.lb_buf = slave_lmv;
2355 slave_buf.lb_len = sizeof(*slave_lmv);
2357 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2358 if (!lo->ldo_stripe[i])
2361 if (!dt_object_exists(lo->ldo_stripe[i]))
2364 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], &slave_buf,
2365 XATTR_NAME_LMV, fl, th);
2370 lod_striping_free(env, lod_dt_obj(dt));
2371 OBD_FREE_PTR(slave_lmv);
2377 * Implementation of dt_object_operations::do_declare_xattr_set.
2379 * Used with regular (non-striped) objects. Basically it
2380 * initializes the striping information and applies the
2381 * change to all the stripes.
2383 * \see dt_object_operations::do_declare_xattr_set() in the API description
2386 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2387 struct dt_object *dt,
2388 const struct lu_buf *buf,
2389 const char *name, int fl,
2392 struct dt_object *next = dt_object_child(dt);
2393 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2394 struct lod_object *lo = lod_dt_obj(dt);
2399 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2400 struct lmv_user_md_v1 *lum;
2402 LASSERT(buf != NULL && buf->lb_buf != NULL);
2404 rc = lod_verify_md_striping(d, lum);
2407 } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2408 rc = lod_verify_striping(d, lo, buf, false);
2413 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2417 /* Note: Do not set LinkEA on sub-stripes, otherwise
2418 * it will confuse the fid2path process(see mdt_path_current()).
2419 * The linkEA between master and sub-stripes is set in
2420 * lod_xattr_set_lmv(). */
2421 if (strcmp(name, XATTR_NAME_LINK) == 0)
2424 /* set xattr to each stripes, if needed */
2425 rc = lod_striping_load(env, lo);
2429 if (lo->ldo_dir_stripe_count == 0)
2432 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2433 if (!lo->ldo_stripe[i])
2436 if (!dt_object_exists(lo->ldo_stripe[i]))
2439 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2449 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2450 struct lod_object *lo,
2451 struct dt_object *dt, struct thandle *th,
2452 int comp_idx, int stripe_idx,
2453 struct lod_obj_stripe_cb_data *data)
2455 struct lod_thread_info *info = lod_env_info(env);
2456 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
2457 struct filter_fid *ff = &info->lti_ff;
2458 struct lu_buf *buf = &info->lti_buf;
2462 buf->lb_len = sizeof(*ff);
2463 rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2471 * locd_buf is set if it's called by dir migration, which doesn't check
2474 if (data->locd_buf) {
2475 memset(ff, 0, sizeof(*ff));
2476 ff->ff_parent = *(struct lu_fid *)data->locd_buf->lb_buf;
2478 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
2480 if (lu_fid_eq(lod_object_fid(lo), &ff->ff_parent) &&
2481 ff->ff_layout.ol_comp_id == comp->llc_id)
2484 memset(ff, 0, sizeof(*ff));
2485 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2488 /* rewrite filter_fid */
2489 ff->ff_parent.f_ver = stripe_idx;
2490 ff->ff_layout.ol_stripe_size = comp->llc_stripe_size;
2491 ff->ff_layout.ol_stripe_count = comp->llc_stripe_count;
2492 ff->ff_layout.ol_comp_id = comp->llc_id;
2493 ff->ff_layout.ol_comp_start = comp->llc_extent.e_start;
2494 ff->ff_layout.ol_comp_end = comp->llc_extent.e_end;
2495 filter_fid_cpu_to_le(ff, ff, sizeof(*ff));
2497 if (data->locd_declare)
2498 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2499 LU_XATTR_REPLACE, th);
2501 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2502 LU_XATTR_REPLACE, th);
2508 * Reset parent FID on OST object
2510 * Replace parent FID with @dt object FID, which is only called during migration
2511 * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2512 * the FID is changed.
2514 * \param[in] env execution environment
2515 * \param[in] dt dt_object whose stripes's parent FID will be reset
2516 * \parem[in] th thandle
2517 * \param[in] declare if it is declare
2519 * \retval 0 if reset succeeds
2520 * \retval negative errno if reset fails
2522 static int lod_replace_parent_fid(const struct lu_env *env,
2523 struct dt_object *dt,
2524 const struct lu_buf *buf,
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 filter_fid *ff;
2530 struct lod_obj_stripe_cb_data data = { { 0 } };
2534 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2536 /* set xattr to each stripes, if needed */
2537 rc = lod_striping_load(env, lo);
2541 if (!lod_obj_is_striped(dt))
2544 if (info->lti_ea_store_size < sizeof(*ff)) {
2545 rc = lod_ea_store_resize(info, sizeof(*ff));
2550 data.locd_declare = declare;
2551 data.locd_stripe_cb = lod_obj_stripe_replace_parent_fid_cb;
2552 data.locd_buf = buf;
2553 rc = lod_obj_for_each_stripe(env, lo, th, &data);
2558 inline __u16 lod_comp_entry_stripe_count(struct lod_object *lo,
2559 struct lod_layout_component *entry,
2562 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2566 else if (lod_comp_inited(entry))
2567 return entry->llc_stripe_count;
2568 else if ((__u16)-1 == entry->llc_stripe_count)
2569 return lod->lod_ost_count;
2571 return lod_get_stripe_count(lod, lo,
2572 entry->llc_stripe_count, false);
2575 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2577 int magic, size = 0, i;
2578 struct lod_layout_component *comp_entries;
2580 bool is_composite, is_foreign = false;
2583 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2584 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2586 lo->ldo_def_striping->lds_def_striping_is_composite;
2588 comp_cnt = lo->ldo_comp_cnt;
2589 comp_entries = lo->ldo_comp_entries;
2590 is_composite = lo->ldo_is_composite;
2591 is_foreign = lo->ldo_is_foreign;
2595 return lo->ldo_foreign_lov_size;
2597 LASSERT(comp_cnt != 0 && comp_entries != NULL);
2599 size = sizeof(struct lov_comp_md_v1) +
2600 sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2601 LASSERT(size % sizeof(__u64) == 0);
2604 for (i = 0; i < comp_cnt; i++) {
2607 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2608 stripe_count = lod_comp_entry_stripe_count(lo, &comp_entries[i],
2610 if (!is_dir && is_composite)
2611 lod_comp_shrink_stripe_count(&comp_entries[i],
2614 size += lov_user_md_size(stripe_count, magic);
2615 LASSERT(size % sizeof(__u64) == 0);
2621 * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2622 * the xattr value is binary lov_comp_md_v1 which contains component(s)
2625 * \param[in] env execution environment
2626 * \param[in] dt dt_object to add components on
2627 * \param[in] buf buffer contains components to be added
2628 * \parem[in] th thandle
2630 * \retval 0 on success
2631 * \retval negative errno on failure
2633 static int lod_declare_layout_add(const struct lu_env *env,
2634 struct dt_object *dt,
2635 const struct lu_buf *buf,
2638 struct lod_thread_info *info = lod_env_info(env);
2639 struct lod_layout_component *comp_array, *lod_comp, *old_array;
2640 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2641 struct dt_object *next = dt_object_child(dt);
2642 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
2643 struct lod_object *lo = lod_dt_obj(dt);
2644 struct lov_user_md_v3 *v3;
2645 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2647 int i, rc, array_cnt, old_array_cnt;
2650 LASSERT(lo->ldo_is_composite);
2652 if (lo->ldo_flr_state != LCM_FL_NONE)
2655 rc = lod_verify_striping(d, lo, buf, false);
2659 magic = comp_v1->lcm_magic;
2660 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2661 lustre_swab_lov_comp_md_v1(comp_v1);
2662 magic = comp_v1->lcm_magic;
2665 if (magic != LOV_USER_MAGIC_COMP_V1)
2668 array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2669 OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2670 if (comp_array == NULL)
2673 memcpy(comp_array, lo->ldo_comp_entries,
2674 sizeof(*comp_array) * lo->ldo_comp_cnt);
2676 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2677 struct lov_user_md_v1 *v1;
2678 struct lu_extent *ext;
2680 v1 = (struct lov_user_md *)((char *)comp_v1 +
2681 comp_v1->lcm_entries[i].lcme_offset);
2682 ext = &comp_v1->lcm_entries[i].lcme_extent;
2684 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2685 lod_comp->llc_extent.e_start = ext->e_start;
2686 lod_comp->llc_extent.e_end = ext->e_end;
2687 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2688 lod_comp->llc_flags = comp_v1->lcm_entries[i].lcme_flags;
2690 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2691 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2692 lod_adjust_stripe_info(lod_comp, desc, 0);
2694 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2695 v3 = (struct lov_user_md_v3 *) v1;
2696 if (v3->lmm_pool_name[0] != '\0') {
2697 rc = lod_set_pool(&lod_comp->llc_pool,
2705 old_array = lo->ldo_comp_entries;
2706 old_array_cnt = lo->ldo_comp_cnt;
2708 lo->ldo_comp_entries = comp_array;
2709 lo->ldo_comp_cnt = array_cnt;
2711 /* No need to increase layout generation here, it will be increased
2712 * later when generating component ID for the new components */
2714 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2715 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2716 XATTR_NAME_LOV, 0, th);
2718 lo->ldo_comp_entries = old_array;
2719 lo->ldo_comp_cnt = old_array_cnt;
2723 OBD_FREE(old_array, sizeof(*lod_comp) * old_array_cnt);
2725 LASSERT(lo->ldo_mirror_count == 1);
2726 lo->ldo_mirrors[0].lme_end = array_cnt - 1;
2731 for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2732 lod_comp = &comp_array[i];
2733 if (lod_comp->llc_pool != NULL) {
2734 OBD_FREE(lod_comp->llc_pool,
2735 strlen(lod_comp->llc_pool) + 1);
2736 lod_comp->llc_pool = NULL;
2739 OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2744 * lod_last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2745 * @mirror_id: Mirror id to be checked.
2748 * This function checks if a mirror with specified @mirror_id is the last
2749 * non-stale mirror of a LOD object @lo.
2751 * Return: true or false.
2754 bool lod_last_non_stale_mirror(__u16 mirror_id, struct lod_object *lo)
2756 struct lod_layout_component *lod_comp;
2757 bool has_stale_flag;
2760 for (i = 0; i < lo->ldo_mirror_count; i++) {
2761 if (lo->ldo_mirrors[i].lme_id == mirror_id ||
2762 lo->ldo_mirrors[i].lme_stale)
2765 has_stale_flag = false;
2766 lod_foreach_mirror_comp(lod_comp, lo, i) {
2767 if (lod_comp->llc_flags & LCME_FL_STALE) {
2768 has_stale_flag = true;
2772 if (!has_stale_flag)
2780 * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2781 * the '$field' can only be 'flags' now. The xattr value is binary
2782 * lov_comp_md_v1 which contains the component ID(s) and the value of
2783 * the field to be modified.
2784 * Please update allowed_lustre_lov macro if $field groks more values
2787 * \param[in] env execution environment
2788 * \param[in] dt dt_object to be modified
2789 * \param[in] op operation string, like "set.flags"
2790 * \param[in] buf buffer contains components to be set
2791 * \parem[in] th thandle
2793 * \retval 0 on success
2794 * \retval negative errno on failure
2796 static int lod_declare_layout_set(const struct lu_env *env,
2797 struct dt_object *dt,
2798 char *op, const struct lu_buf *buf,
2801 struct lod_layout_component *lod_comp;
2802 struct lod_thread_info *info = lod_env_info(env);
2803 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2804 struct lod_object *lo = lod_dt_obj(dt);
2805 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2808 bool changed = false;
2811 /* Please update allowed_lustre_lov macro if op
2812 * groks more values in the future
2814 if (strcmp(op, "set.flags") != 0) {
2815 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2816 lod2obd(d)->obd_name, op);
2820 magic = comp_v1->lcm_magic;
2821 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2822 lustre_swab_lov_comp_md_v1(comp_v1);
2823 magic = comp_v1->lcm_magic;
2826 if (magic != LOV_USER_MAGIC_COMP_V1)
2829 if (comp_v1->lcm_entry_count == 0) {
2830 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2831 lod2obd(d)->obd_name);
2835 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2836 __u32 id = comp_v1->lcm_entries[i].lcme_id;
2837 __u32 flags = comp_v1->lcm_entries[i].lcme_flags;
2838 __u32 mirror_flag = flags & LCME_MIRROR_FLAGS;
2839 __u16 mirror_id = mirror_id_of(id);
2840 bool neg = flags & LCME_FL_NEG;
2842 if (flags & LCME_FL_INIT) {
2844 lod_striping_free(env, lo);
2848 flags &= ~(LCME_MIRROR_FLAGS | LCME_FL_NEG);
2849 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2850 lod_comp = &lo->ldo_comp_entries[j];
2852 /* lfs only put one flag in each entry */
2853 if ((flags && id != lod_comp->llc_id) ||
2854 (mirror_flag && mirror_id !=
2855 mirror_id_of(lod_comp->llc_id)))
2860 lod_comp->llc_flags &= ~flags;
2862 lod_comp->llc_flags &= ~mirror_flag;
2865 if ((flags & LCME_FL_STALE) &&
2866 lod_last_non_stale_mirror(mirror_id,
2869 lod_comp->llc_flags |= flags;
2872 lod_comp->llc_flags |= mirror_flag;
2873 if (mirror_flag & LCME_FL_NOSYNC)
2874 lod_comp->llc_timestamp =
2875 ktime_get_real_seconds();
2883 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2884 lod2obd(d)->obd_name);
2888 lod_obj_inc_layout_gen(lo);
2890 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2891 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), &info->lti_buf,
2892 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
2897 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2898 * and the xattr value is a unique component ID or a special lcme_id.
2900 * \param[in] env execution environment
2901 * \param[in] dt dt_object to be operated on
2902 * \param[in] buf buffer contains component ID or lcme_id
2903 * \parem[in] th thandle
2905 * \retval 0 on success
2906 * \retval negative errno on failure
2908 static int lod_declare_layout_del(const struct lu_env *env,
2909 struct dt_object *dt,
2910 const struct lu_buf *buf,
2913 struct lod_thread_info *info = lod_env_info(env);
2914 struct dt_object *next = dt_object_child(dt);
2915 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2916 struct lod_object *lo = lod_dt_obj(dt);
2917 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2918 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2919 __u32 magic, id, flags, neg_flags = 0;
2923 LASSERT(lo->ldo_is_composite);
2925 if (lo->ldo_flr_state != LCM_FL_NONE)
2928 magic = comp_v1->lcm_magic;
2929 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2930 lustre_swab_lov_comp_md_v1(comp_v1);
2931 magic = comp_v1->lcm_magic;
2934 if (magic != LOV_USER_MAGIC_COMP_V1)
2937 id = comp_v1->lcm_entries[0].lcme_id;
2938 flags = comp_v1->lcm_entries[0].lcme_flags;
2940 if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
2941 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
2942 lod2obd(d)->obd_name, id, flags);
2946 if (id != LCME_ID_INVAL && flags != 0) {
2947 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
2948 lod2obd(d)->obd_name);
2952 if (id == LCME_ID_INVAL && !flags) {
2953 CDEBUG(D_LAYOUT, "%s: no id or flags specified.\n",
2954 lod2obd(d)->obd_name);
2958 if (flags & LCME_FL_NEG) {
2959 neg_flags = flags & ~LCME_FL_NEG;
2963 left = lo->ldo_comp_cnt;
2967 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
2968 struct lod_layout_component *lod_comp;
2970 lod_comp = &lo->ldo_comp_entries[i];
2972 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
2974 else if (flags && !(flags & lod_comp->llc_flags))
2976 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
2979 if (left != (i + 1)) {
2980 CDEBUG(D_LAYOUT, "%s: this deletion will create "
2981 "a hole.\n", lod2obd(d)->obd_name);
2986 /* Mark the component as deleted */
2987 lod_comp->llc_id = LCME_ID_INVAL;
2989 /* Not instantiated component */
2990 if (lod_comp->llc_stripe == NULL)
2993 LASSERT(lod_comp->llc_stripe_count > 0);
2994 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
2995 struct dt_object *obj = lod_comp->llc_stripe[j];
2999 rc = lod_sub_declare_destroy(env, obj, th);
3005 LASSERTF(left >= 0, "left = %d\n", left);
3006 if (left == lo->ldo_comp_cnt) {
3007 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
3008 lod2obd(d)->obd_name, id);
3012 memset(attr, 0, sizeof(*attr));
3013 attr->la_valid = LA_SIZE;
3014 rc = lod_sub_declare_attr_set(env, next, attr, th);
3019 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
3020 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
3021 XATTR_NAME_LOV, 0, th);
3023 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
3030 * Declare layout add/set/del operations issued by special xattr names:
3032 * XATTR_LUSTRE_LOV.add add component(s) to existing file
3033 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
3034 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
3036 * \param[in] env execution environment
3037 * \param[in] dt object
3038 * \param[in] name name of xattr
3039 * \param[in] buf lu_buf contains xattr value
3040 * \param[in] th transaction handle
3042 * \retval 0 on success
3043 * \retval negative if failed
3045 static int lod_declare_modify_layout(const struct lu_env *env,
3046 struct dt_object *dt,
3048 const struct lu_buf *buf,
3051 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3052 struct lod_object *lo = lod_dt_obj(dt);
3054 int rc, len = strlen(XATTR_LUSTRE_LOV);
3057 LASSERT(dt_object_exists(dt));
3059 if (strlen(name) <= len || name[len] != '.') {
3060 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
3061 lod2obd(d)->obd_name, name);
3066 rc = lod_striping_load(env, lo);
3070 /* the layout to be modified must be a composite layout */
3071 if (!lo->ldo_is_composite) {
3072 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
3073 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3074 GOTO(unlock, rc = -EINVAL);
3077 op = (char *)name + len;
3078 if (strcmp(op, "add") == 0) {
3079 rc = lod_declare_layout_add(env, dt, buf, th);
3080 } else if (strcmp(op, "del") == 0) {
3081 rc = lod_declare_layout_del(env, dt, buf, th);
3082 } else if (strncmp(op, "set", strlen("set")) == 0) {
3083 rc = lod_declare_layout_set(env, dt, op, buf, th);
3085 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
3086 lod2obd(d)->obd_name, name);
3087 GOTO(unlock, rc = -ENOTSUPP);
3091 lod_striping_free(env, lo);
3097 * Convert a plain file lov_mds_md to a composite layout.
3099 * \param[in,out] info the thread info::lti_ea_store buffer contains little
3100 * endian plain file layout
3102 * \retval 0 on success, <0 on failure
3104 static int lod_layout_convert(struct lod_thread_info *info)
3106 struct lov_mds_md *lmm = info->lti_ea_store;
3107 struct lov_mds_md *lmm_save;
3108 struct lov_comp_md_v1 *lcm;
3109 struct lov_comp_md_entry_v1 *lcme;
3115 /* realloc buffer to a composite layout which contains one component */
3116 blob_size = lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
3117 le32_to_cpu(lmm->lmm_magic));
3118 size = sizeof(*lcm) + sizeof(*lcme) + blob_size;
3120 OBD_ALLOC_LARGE(lmm_save, blob_size);
3122 GOTO(out, rc = -ENOMEM);
3124 memcpy(lmm_save, lmm, blob_size);
3126 if (info->lti_ea_store_size < size) {
3127 rc = lod_ea_store_resize(info, size);
3132 lcm = info->lti_ea_store;
3133 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
3134 lcm->lcm_size = cpu_to_le32(size);
3135 lcm->lcm_layout_gen = cpu_to_le32(le16_to_cpu(
3136 lmm_save->lmm_layout_gen));
3137 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
3138 lcm->lcm_entry_count = cpu_to_le16(1);
3139 lcm->lcm_mirror_count = 0;
3141 lcme = &lcm->lcm_entries[0];
3142 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
3143 lcme->lcme_extent.e_start = 0;
3144 lcme->lcme_extent.e_end = cpu_to_le64(OBD_OBJECT_EOF);
3145 lcme->lcme_offset = cpu_to_le32(sizeof(*lcm) + sizeof(*lcme));
3146 lcme->lcme_size = cpu_to_le32(blob_size);
3148 memcpy((char *)lcm + lcme->lcme_offset, (char *)lmm_save, blob_size);
3153 OBD_FREE_LARGE(lmm_save, blob_size);
3158 * Merge layouts to form a mirrored file.
3160 static int lod_declare_layout_merge(const struct lu_env *env,
3161 struct dt_object *dt, const struct lu_buf *mbuf,
3164 struct lod_thread_info *info = lod_env_info(env);
3165 struct lu_buf *buf = &info->lti_buf;
3166 struct lod_object *lo = lod_dt_obj(dt);
3167 struct lov_comp_md_v1 *lcm;
3168 struct lov_comp_md_v1 *cur_lcm;
3169 struct lov_comp_md_v1 *merge_lcm;
3170 struct lov_comp_md_entry_v1 *lcme;
3171 struct lov_mds_md_v1 *lmm;
3174 __u16 cur_entry_count;
3175 __u16 merge_entry_count;
3177 __u16 mirror_id = 0;
3184 merge_lcm = mbuf->lb_buf;
3185 if (mbuf->lb_len < sizeof(*merge_lcm))
3188 /* must be an existing layout from disk */
3189 if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
3192 merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
3194 /* do not allow to merge two mirrored files */
3195 if (le16_to_cpu(merge_lcm->lcm_mirror_count))
3198 /* verify the target buffer */
3199 rc = lod_get_lov_ea(env, lo);
3201 RETURN(rc ? : -ENODATA);
3203 cur_lcm = info->lti_ea_store;
3204 switch (le32_to_cpu(cur_lcm->lcm_magic)) {
3207 rc = lod_layout_convert(info);
3209 case LOV_MAGIC_COMP_V1:
3219 /* info->lti_ea_store could be reallocated in lod_layout_convert() */
3220 cur_lcm = info->lti_ea_store;
3221 cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
3223 /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
3224 mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
3225 if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
3228 /* size of new layout */
3229 size = le32_to_cpu(cur_lcm->lcm_size) +
3230 le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
3232 memset(buf, 0, sizeof(*buf));
3233 lu_buf_alloc(buf, size);
3234 if (buf->lb_buf == NULL)
3238 memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
3240 offset = sizeof(*lcm) +
3241 sizeof(*lcme) * (cur_entry_count + merge_entry_count);
3242 for (i = 0; i < cur_entry_count; i++) {
3243 struct lov_comp_md_entry_v1 *cur_lcme;
3245 lcme = &lcm->lcm_entries[i];
3246 cur_lcme = &cur_lcm->lcm_entries[i];
3248 lcme->lcme_offset = cpu_to_le32(offset);
3249 memcpy((char *)lcm + offset,
3250 (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
3251 le32_to_cpu(lcme->lcme_size));
3253 offset += le32_to_cpu(lcme->lcme_size);
3255 if (mirror_count == 1 &&
3256 mirror_id_of(le32_to_cpu(lcme->lcme_id)) == 0) {
3257 /* Add mirror from a non-flr file, create new mirror ID.
3258 * Otherwise, keep existing mirror's component ID, used
3259 * for mirror extension.
3261 id = pflr_id(1, i + 1);
3262 lcme->lcme_id = cpu_to_le32(id);
3265 id = max(le32_to_cpu(lcme->lcme_id), id);
3268 mirror_id = mirror_id_of(id) + 1;
3270 /* check if first entry in new layout is DOM */
3271 lmm = (struct lov_mds_md_v1 *)((char *)merge_lcm +
3272 merge_lcm->lcm_entries[0].lcme_offset);
3273 merge_has_dom = lov_pattern(le32_to_cpu(lmm->lmm_pattern)) ==
3276 for (i = 0; i < merge_entry_count; i++) {
3277 struct lov_comp_md_entry_v1 *merge_lcme;
3279 merge_lcme = &merge_lcm->lcm_entries[i];
3280 lcme = &lcm->lcm_entries[cur_entry_count + i];
3282 *lcme = *merge_lcme;
3283 lcme->lcme_offset = cpu_to_le32(offset);
3284 if (merge_has_dom && i == 0)
3285 lcme->lcme_flags |= cpu_to_le32(LCME_FL_STALE);
3287 id = pflr_id(mirror_id, i + 1);
3288 lcme->lcme_id = cpu_to_le32(id);
3290 memcpy((char *)lcm + offset,
3291 (char *)merge_lcm + le32_to_cpu(merge_lcme->lcme_offset),
3292 le32_to_cpu(lcme->lcme_size));
3294 offset += le32_to_cpu(lcme->lcme_size);
3297 /* fixup layout information */
3298 lod_obj_inc_layout_gen(lo);
3299 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3300 lcm->lcm_size = cpu_to_le32(size);
3301 lcm->lcm_entry_count = cpu_to_le16(cur_entry_count + merge_entry_count);
3302 lcm->lcm_mirror_count = cpu_to_le16(mirror_count);
3303 if ((le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK) == LCM_FL_NONE)
3304 lcm->lcm_flags = cpu_to_le32(LCM_FL_RDONLY);
3306 rc = lod_striping_reload(env, lo, buf);
3310 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), buf,
3311 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3319 * Split layouts, just set the LOVEA with the layout from mbuf.
3321 static int lod_declare_layout_split(const struct lu_env *env,
3322 struct dt_object *dt, const struct lu_buf *mbuf,
3325 struct lod_object *lo = lod_dt_obj(dt);
3326 struct lov_comp_md_v1 *lcm = mbuf->lb_buf;
3330 lod_obj_inc_layout_gen(lo);
3331 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3333 rc = lod_striping_reload(env, lo, mbuf);
3337 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), mbuf,
3338 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3343 * Implementation of dt_object_operations::do_declare_xattr_set.
3345 * \see dt_object_operations::do_declare_xattr_set() in the API description
3348 * the extension to the API:
3349 * - declaring LOVEA requests striping creation
3350 * - LU_XATTR_REPLACE means layout swap
3352 static int lod_declare_xattr_set(const struct lu_env *env,
3353 struct dt_object *dt,
3354 const struct lu_buf *buf,
3355 const char *name, int fl,
3358 struct dt_object *next = dt_object_child(dt);
3359 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3364 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
3365 if ((S_ISREG(mode) || mode == 0) &&
3366 !(fl & (LU_XATTR_REPLACE | LU_XATTR_MERGE | LU_XATTR_SPLIT)) &&
3367 (strcmp(name, XATTR_NAME_LOV) == 0 ||
3368 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
3370 * this is a request to create object's striping.
3372 * allow to declare predefined striping on a new (!mode) object
3373 * which is supposed to be replay of regular file creation
3374 * (when LOV setting is declared)
3376 * LU_XATTR_REPLACE is set to indicate a layout swap
3378 if (dt_object_exists(dt)) {
3379 rc = dt_attr_get(env, next, attr);
3383 memset(attr, 0, sizeof(*attr));
3384 attr->la_valid = LA_TYPE | LA_MODE;
3385 attr->la_mode = S_IFREG;
3387 rc = lod_declare_striped_create(env, dt, attr, buf, th);
3388 } else if (fl & LU_XATTR_MERGE) {
3389 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3390 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3391 rc = lod_declare_layout_merge(env, dt, buf, th);
3392 } else if (fl & LU_XATTR_SPLIT) {
3393 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3394 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3395 rc = lod_declare_layout_split(env, dt, buf, th);
3396 } else if (S_ISREG(mode) &&
3397 strlen(name) >= sizeof(XATTR_LUSTRE_LOV) + 3 &&
3398 allowed_lustre_lov(name)) {
3400 * this is a request to modify object's striping.
3401 * add/set/del component(s).
3403 if (!dt_object_exists(dt))
3406 rc = lod_declare_modify_layout(env, dt, name, buf, th);
3407 } else if (S_ISDIR(mode)) {
3408 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
3409 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3410 rc = lod_replace_parent_fid(env, dt, buf, th, true);
3412 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
3419 * Apply xattr changes to the object.
3421 * Applies xattr changes to the object and the stripes if the latter exist.
3423 * \param[in] env execution environment
3424 * \param[in] dt object
3425 * \param[in] buf buffer pointing to the new value of xattr
3426 * \param[in] name name of xattr
3427 * \param[in] fl flags
3428 * \param[in] th transaction handle
3430 * \retval 0 on success
3431 * \retval negative if failed
3433 static int lod_xattr_set_internal(const struct lu_env *env,
3434 struct dt_object *dt,
3435 const struct lu_buf *buf,
3436 const char *name, int fl,
3439 struct dt_object *next = dt_object_child(dt);
3440 struct lod_object *lo = lod_dt_obj(dt);
3445 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3446 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3449 /* Note: Do not set LinkEA on sub-stripes, otherwise
3450 * it will confuse the fid2path process(see mdt_path_current()).
3451 * The linkEA between master and sub-stripes is set in
3452 * lod_xattr_set_lmv(). */
3453 if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
3456 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3457 if (!lo->ldo_stripe[i])
3460 if (!dt_object_exists(lo->ldo_stripe[i]))
3463 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
3473 * Delete an extended attribute.
3475 * Deletes specified xattr from the object and the stripes if the latter exist.
3477 * \param[in] env execution environment
3478 * \param[in] dt object
3479 * \param[in] name name of xattr
3480 * \param[in] th transaction handle
3482 * \retval 0 on success
3483 * \retval negative if failed
3485 static int lod_xattr_del_internal(const struct lu_env *env,
3486 struct dt_object *dt,
3487 const char *name, struct thandle *th)
3489 struct dt_object *next = dt_object_child(dt);
3490 struct lod_object *lo = lod_dt_obj(dt);
3495 rc = lod_sub_xattr_del(env, next, name, th);
3496 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3499 if (lo->ldo_dir_stripe_count == 0)
3502 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3503 LASSERT(lo->ldo_stripe[i]);
3505 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3514 * Set default striping on a directory.
3516 * Sets specified striping on a directory object unless it matches the default
3517 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3518 * EA. This striping will be used when regular file is being created in this
3521 * \param[in] env execution environment
3522 * \param[in] dt the striped object
3523 * \param[in] buf buffer with the striping
3524 * \param[in] name name of EA
3525 * \param[in] fl xattr flag (see OSD API description)
3526 * \param[in] th transaction handle
3528 * \retval 0 on success
3529 * \retval negative if failed
3531 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
3532 struct dt_object *dt,
3533 const struct lu_buf *buf,
3534 const char *name, int fl,
3537 struct lov_user_md_v1 *lum;
3538 struct lov_user_md_v3 *v3 = NULL;
3539 const char *pool_name = NULL;
3544 LASSERT(buf != NULL && buf->lb_buf != NULL);
3547 switch (lum->lmm_magic) {
3548 case LOV_USER_MAGIC_SPECIFIC:
3549 case LOV_USER_MAGIC_V3:
3551 if (v3->lmm_pool_name[0] != '\0')
3552 pool_name = v3->lmm_pool_name;
3554 case LOV_USER_MAGIC_V1:
3555 /* if { size, offset, count } = { 0, -1, 0 } and no pool
3556 * (i.e. all default values specified) then delete default
3557 * striping from dir. */
3559 "set default striping: sz %u # %u offset %d %s %s\n",
3560 (unsigned)lum->lmm_stripe_size,
3561 (unsigned)lum->lmm_stripe_count,
3562 (int)lum->lmm_stripe_offset,
3563 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
3565 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
3566 lum->lmm_stripe_count,
3567 lum->lmm_stripe_offset,
3570 case LOV_USER_MAGIC_COMP_V1:
3572 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lum;
3573 struct lov_comp_md_entry_v1 *lcme;
3576 comp_cnt = le16_to_cpu(lcm->lcm_entry_count);
3577 for (i = 0; i < comp_cnt; i++) {
3578 lcme = &lcm->lcm_entries[i];
3579 if (lcme->lcme_flags & cpu_to_le32(LCME_FL_EXTENSION)) {
3580 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_SEL);
3589 CERROR("Invalid magic %x\n", lum->lmm_magic);
3594 rc = lod_xattr_del_internal(env, dt, name, th);
3598 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3605 * Set default striping on a directory object.
3607 * Sets specified striping on a directory object unless it matches the default
3608 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3609 * EA. This striping will be used when a new directory is being created in the
3612 * \param[in] env execution environment
3613 * \param[in] dt the striped object
3614 * \param[in] buf buffer with the striping
3615 * \param[in] name name of EA
3616 * \param[in] fl xattr flag (see OSD API description)
3617 * \param[in] th transaction handle
3619 * \retval 0 on success
3620 * \retval negative if failed
3622 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
3623 struct dt_object *dt,
3624 const struct lu_buf *buf,
3625 const char *name, int fl,
3628 struct lmv_user_md_v1 *lum;
3633 LASSERT(buf != NULL && buf->lb_buf != NULL);
3637 "set default stripe_count # %u stripe_offset %d hash %u\n",
3638 le32_to_cpu(lum->lum_stripe_count),
3639 (int)le32_to_cpu(lum->lum_stripe_offset),
3640 le32_to_cpu(lum->lum_hash_type));
3642 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
3643 le32_to_cpu(lum->lum_stripe_offset)) &&
3644 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
3645 rc = lod_xattr_del_internal(env, dt, name, th);
3649 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3658 * Turn directory into a striped directory.
3660 * During replay the client sends the striping created before MDT
3661 * failure, then the layer above LOD sends this defined striping
3662 * using ->do_xattr_set(), so LOD uses this method to replay creation
3663 * of the stripes. Notice the original information for the striping
3664 * (#stripes, FIDs, etc) was transferred in declare path.
3666 * \param[in] env execution environment
3667 * \param[in] dt the striped object
3668 * \param[in] buf not used currently
3669 * \param[in] name not used currently
3670 * \param[in] fl xattr flag (see OSD API description)
3671 * \param[in] th transaction handle
3673 * \retval 0 on success
3674 * \retval negative if failed
3676 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
3677 const struct lu_buf *buf, const char *name,
3678 int fl, struct thandle *th)
3680 struct lod_object *lo = lod_dt_obj(dt);
3681 struct lod_thread_info *info = lod_env_info(env);
3682 struct lu_attr *attr = &info->lti_attr;
3683 struct dt_object_format *dof = &info->lti_format;
3684 struct lu_buf lmv_buf;
3685 struct lu_buf slave_lmv_buf;
3686 struct lmv_mds_md_v1 *lmm;
3687 struct lmv_mds_md_v1 *slave_lmm = NULL;
3688 struct dt_insert_rec *rec = &info->lti_dt_rec;
3693 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3696 /* The stripes are supposed to be allocated in declare phase,
3697 * if there are no stripes being allocated, it will skip */
3698 if (lo->ldo_dir_stripe_count == 0) {
3699 if (lo->ldo_dir_is_foreign) {
3700 rc = lod_sub_xattr_set(env, dt_object_child(dt), buf,
3701 XATTR_NAME_LMV, fl, th);
3708 rc = dt_attr_get(env, dt_object_child(dt), attr);
3712 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
3713 LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
3714 dof->dof_type = DFT_DIR;
3716 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
3719 lmm = lmv_buf.lb_buf;
3721 OBD_ALLOC_PTR(slave_lmm);
3722 if (slave_lmm == NULL)
3725 lod_prep_slave_lmv_md(slave_lmm, lmm);
3726 slave_lmv_buf.lb_buf = slave_lmm;
3727 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
3729 rec->rec_type = S_IFDIR;
3730 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3731 struct dt_object *dto = lo->ldo_stripe[i];
3732 char *stripe_name = info->lti_key;
3733 struct lu_name *sname;
3734 struct linkea_data ldata = { NULL };
3735 struct lu_buf linkea_buf;
3737 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
3741 /* fail a remote stripe creation */
3742 if (i && OBD_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_CREATE))
3745 /* if it's source stripe of migrating directory, don't create */
3746 if (!((lo->ldo_dir_hash_type & LMV_HASH_FLAG_MIGRATION) &&
3747 i >= lo->ldo_dir_migrate_offset)) {
3748 dt_write_lock(env, dto, DT_TGT_CHILD);
3749 rc = lod_sub_create(env, dto, attr, NULL, dof, th);
3751 dt_write_unlock(env, dto);
3755 rc = lod_sub_ref_add(env, dto, th);
3756 dt_write_unlock(env, dto);
3760 rec->rec_fid = lu_object_fid(&dto->do_lu);
3761 rc = lod_sub_insert(env, dto,
3762 (const struct dt_rec *)rec,
3763 (const struct dt_key *)dot, th);
3768 rec->rec_fid = lu_object_fid(&dt->do_lu);
3769 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
3770 (const struct dt_key *)dotdot, th);
3774 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
3775 cfs_fail_val != i) {
3776 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
3778 slave_lmm->lmv_master_mdt_index =
3781 slave_lmm->lmv_master_mdt_index =
3784 rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
3785 XATTR_NAME_LMV, 0, th);
3790 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
3792 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3793 PFID(lu_object_fid(&dto->do_lu)), i + 1);
3795 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3796 PFID(lu_object_fid(&dto->do_lu)), i);
3798 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
3799 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
3800 sname, lu_object_fid(&dt->do_lu));
3804 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
3805 linkea_buf.lb_len = ldata.ld_leh->leh_len;
3806 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
3807 XATTR_NAME_LINK, 0, th);
3811 rec->rec_fid = lu_object_fid(&dto->do_lu);
3812 rc = lod_sub_insert(env, dt_object_child(dt),
3813 (const struct dt_rec *)rec,
3814 (const struct dt_key *)stripe_name, th);
3818 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
3823 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
3824 rc = lod_sub_xattr_set(env, dt_object_child(dt),
3825 &lmv_buf, XATTR_NAME_LMV, fl, th);
3827 if (slave_lmm != NULL)
3828 OBD_FREE_PTR(slave_lmm);
3834 * Helper function to declare/execute creation of a striped directory
3836 * Called in declare/create object path, prepare striping for a directory
3837 * and prepare defaults data striping for the objects to be created in
3838 * that directory. Notice the function calls "declaration" or "execution"
3839 * methods depending on \a declare param. This is a consequence of the
3840 * current approach while we don't have natural distributed transactions:
3841 * we basically execute non-local updates in the declare phase. So, the
3842 * arguments for the both phases are the same and this is the reason for
3843 * this function to exist.
3845 * \param[in] env execution environment
3846 * \param[in] dt object
3847 * \param[in] attr attributes the stripes will be created with
3848 * \param[in] lmu lmv_user_md if MDT indices are specified
3849 * \param[in] dof format of stripes (see OSD API description)
3850 * \param[in] th transaction handle
3851 * \param[in] declare where to call "declare" or "execute" methods
3853 * \retval 0 on success
3854 * \retval negative if failed
3856 static int lod_dir_striping_create_internal(const struct lu_env *env,
3857 struct dt_object *dt,
3858 struct lu_attr *attr,
3859 const struct lu_buf *lmu,
3860 struct dt_object_format *dof,
3864 struct lod_thread_info *info = lod_env_info(env);
3865 struct lod_object *lo = lod_dt_obj(dt);
3866 const struct lod_default_striping *lds = lo->ldo_def_striping;
3870 LASSERT(ergo(lds != NULL,
3871 lds->lds_def_striping_set ||
3872 lds->lds_dir_def_striping_set));
3874 if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
3875 lo->ldo_dir_stripe_offset)) {
3877 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3878 int stripe_count = lo->ldo_dir_stripe_count;
3880 if (info->lti_ea_store_size < sizeof(*v1)) {
3881 rc = lod_ea_store_resize(info, sizeof(*v1));
3884 v1 = info->lti_ea_store;
3887 memset(v1, 0, sizeof(*v1));
3888 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3889 v1->lum_stripe_count = cpu_to_le32(stripe_count);
3890 v1->lum_stripe_offset =
3891 cpu_to_le32(lo->ldo_dir_stripe_offset);
3893 info->lti_buf.lb_buf = v1;
3894 info->lti_buf.lb_len = sizeof(*v1);
3895 lmu = &info->lti_buf;
3899 rc = lod_declare_xattr_set_lmv(env, dt, attr, lmu, dof,
3902 rc = lod_xattr_set_lmv(env, dt, lmu, XATTR_NAME_LMV, 0,
3907 /* foreign LMV EA case */
3909 struct lmv_foreign_md *lfm = lmu->lb_buf;
3911 if (lfm->lfm_magic == LMV_MAGIC_FOREIGN) {
3912 rc = lod_declare_xattr_set_lmv(env, dt, attr,
3916 if (lo->ldo_dir_is_foreign) {
3917 LASSERT(lo->ldo_foreign_lmv != NULL &&
3918 lo->ldo_foreign_lmv_size > 0);
3919 info->lti_buf.lb_buf = lo->ldo_foreign_lmv;
3920 info->lti_buf.lb_len = lo->ldo_foreign_lmv_size;
3921 lmu = &info->lti_buf;
3922 rc = lod_xattr_set_lmv(env, dt, lmu,
3923 XATTR_NAME_LMV, 0, th);
3928 /* Transfer default LMV striping from the parent */
3929 if (lds != NULL && lds->lds_dir_def_striping_set &&
3930 !(LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
3931 lds->lds_dir_def_stripe_offset) &&
3932 le32_to_cpu(lds->lds_dir_def_hash_type) !=
3933 LMV_HASH_TYPE_UNKNOWN)) {
3934 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3936 if (info->lti_ea_store_size < sizeof(*v1)) {
3937 rc = lod_ea_store_resize(info, sizeof(*v1));
3940 v1 = info->lti_ea_store;
3943 memset(v1, 0, sizeof(*v1));
3944 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3945 v1->lum_stripe_count =
3946 cpu_to_le32(lds->lds_dir_def_stripe_count);
3947 v1->lum_stripe_offset =
3948 cpu_to_le32(lds->lds_dir_def_stripe_offset);
3950 cpu_to_le32(lds->lds_dir_def_hash_type);
3952 info->lti_buf.lb_buf = v1;
3953 info->lti_buf.lb_len = sizeof(*v1);
3955 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3956 XATTR_NAME_DEFAULT_LMV,
3959 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
3961 XATTR_NAME_DEFAULT_LMV, 0,
3967 /* Transfer default LOV striping from the parent */
3968 if (lds != NULL && lds->lds_def_striping_set &&
3969 lds->lds_def_comp_cnt != 0) {
3970 struct lov_mds_md *lmm;
3971 int lmm_size = lod_comp_md_size(lo, true);
3973 if (info->lti_ea_store_size < lmm_size) {
3974 rc = lod_ea_store_resize(info, lmm_size);
3978 lmm = info->lti_ea_store;
3980 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
3984 info->lti_buf.lb_buf = lmm;
3985 info->lti_buf.lb_len = lmm_size;
3988 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3989 XATTR_NAME_LOV, 0, th);
3991 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
3992 XATTR_NAME_LOV, 0, th);
4000 static int lod_declare_dir_striping_create(const struct lu_env *env,
4001 struct dt_object *dt,
4002 struct lu_attr *attr,
4004 struct dt_object_format *dof,
4007 return lod_dir_striping_create_internal(env, dt, attr, lmu, dof, th,
4011 static int lod_dir_striping_create(const struct lu_env *env,
4012 struct dt_object *dt,
4013 struct lu_attr *attr,
4014 struct dt_object_format *dof,
4017 return lod_dir_striping_create_internal(env, dt, attr, NULL, dof, th,
4022 * Make LOV EA for striped object.
4024 * Generate striping information and store it in the LOV EA of the given
4025 * object. The caller must ensure nobody else is calling the function
4026 * against the object concurrently. The transaction must be started.
4027 * FLDB service must be running as well; it's used to map FID to the target,
4028 * which is stored in LOV EA.
4030 * \param[in] env execution environment for this thread
4031 * \param[in] lo LOD object
4032 * \param[in] th transaction handle
4034 * \retval 0 if LOV EA is stored successfully
4035 * \retval negative error number on failure
4037 static int lod_generate_and_set_lovea(const struct lu_env *env,
4038 struct lod_object *lo,
4041 struct lod_thread_info *info = lod_env_info(env);
4042 struct dt_object *next = dt_object_child(&lo->ldo_obj);
4043 struct lov_mds_md_v1 *lmm;
4049 if (lo->ldo_comp_cnt == 0 && !lo->ldo_is_foreign) {
4050 lod_striping_free(env, lo);
4051 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
4055 lmm_size = lod_comp_md_size(lo, false);
4056 if (info->lti_ea_store_size < lmm_size) {
4057 rc = lod_ea_store_resize(info, lmm_size);
4061 lmm = info->lti_ea_store;
4063 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
4067 info->lti_buf.lb_buf = lmm;
4068 info->lti_buf.lb_len = lmm_size;
4069 rc = lod_sub_xattr_set(env, next, &info->lti_buf,
4070 XATTR_NAME_LOV, 0, th);
4074 static __u32 lod_gen_component_id(struct lod_object *lo,
4075 int mirror_id, int comp_idx);
4078 * Repeat an existing component
4080 * Creates a new layout by replicating an existing component. Uses striping
4081 * policy from previous component as a template for the striping for the new
4084 * New component starts with zero length, will be extended (or removed) before
4085 * returning layout to client.
4087 * NB: Reallocates layout components array (lo->ldo_comp_entries), invalidating
4088 * any pre-existing pointers to components. Handle with care.
4090 * \param[in] env execution environment for this thread
4091 * \param[in,out] lo object to update the layout of
4092 * \param[in] index index of component to copy
4094 * \retval 0 on success
4095 * \retval negative errno on error
4097 static int lod_layout_repeat_comp(const struct lu_env *env,
4098 struct lod_object *lo, int index)
4100 struct lod_layout_component *lod_comp;
4101 struct lod_layout_component *new_comp = NULL;
4102 struct lod_layout_component *comp_array;
4103 int rc = 0, i, new_cnt = lo->ldo_comp_cnt + 1;
4108 lod_comp = &lo->ldo_comp_entries[index];
4109 LASSERT(lod_comp_inited(lod_comp) && lod_comp->llc_id != LCME_ID_INVAL);
4111 CDEBUG(D_LAYOUT, "repeating component %d\n", index);
4113 OBD_ALLOC(comp_array, sizeof(*comp_array) * new_cnt);
4114 if (comp_array == NULL)
4115 GOTO(out, rc = -ENOMEM);
4117 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4118 memcpy(&comp_array[i + offset], &lo->ldo_comp_entries[i],
4119 sizeof(*comp_array));
4121 /* Duplicate this component in to the next slot */
4123 new_comp = &comp_array[i + 1];
4124 memcpy(&comp_array[i + 1], &lo->ldo_comp_entries[i],
4125 sizeof(*comp_array));
4126 /* We must now skip this new component when copying */
4131 /* Set up copied component */
4132 new_comp->llc_flags &= ~LCME_FL_INIT;
4133 new_comp->llc_stripe = NULL;
4134 new_comp->llc_stripes_allocated = 0;
4135 new_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4136 /* for uninstantiated components, layout gen stores default stripe
4138 new_comp->llc_layout_gen = lod_comp->llc_stripe_offset;
4139 /* This makes the repeated component zero-length, placed at the end of
4140 * the preceding component */
4141 new_comp->llc_extent.e_start = new_comp->llc_extent.e_end;
4142 new_comp->llc_timestamp = lod_comp->llc_timestamp;
4143 new_comp->llc_pool = NULL;
4145 rc = lod_set_pool(&new_comp->llc_pool, lod_comp->llc_pool);
4149 if (new_comp->llc_ostlist.op_array) {
4150 __u32 *op_array = NULL;
4152 OBD_ALLOC(op_array, new_comp->llc_ostlist.op_size);
4154 GOTO(out, rc = -ENOMEM);
4155 memcpy(op_array, &new_comp->llc_ostlist.op_array,
4156 new_comp->llc_ostlist.op_size);
4157 new_comp->llc_ostlist.op_array = op_array;
4160 OBD_FREE(lo->ldo_comp_entries,
4161 sizeof(*comp_array) * lo->ldo_comp_cnt);
4162 lo->ldo_comp_entries = comp_array;
4163 lo->ldo_comp_cnt = new_cnt;
4165 /* Generate an id for the new component */
4166 mirror_id = mirror_id_of(new_comp->llc_id);
4167 new_comp->llc_id = LCME_ID_INVAL;
4168 new_comp->llc_id = lod_gen_component_id(lo, mirror_id, index + 1);
4169 if (new_comp->llc_id == LCME_ID_INVAL)
4170 GOTO(out, rc = -ERANGE);
4175 OBD_FREE(comp_array, sizeof(*comp_array) * new_cnt);
4180 static int lod_layout_data_init(struct lod_thread_info *info, __u32 comp_cnt)
4184 /* clear memory region that will be used for layout change */
4185 memset(&info->lti_layout_attr, 0, sizeof(struct lu_attr));
4186 info->lti_count = 0;
4188 if (info->lti_comp_size >= comp_cnt)
4191 if (info->lti_comp_size > 0) {
4192 OBD_FREE(info->lti_comp_idx,
4193 info->lti_comp_size * sizeof(__u32));
4194 info->lti_comp_size = 0;
4197 OBD_ALLOC(info->lti_comp_idx, comp_cnt * sizeof(__u32));
4198 if (!info->lti_comp_idx)
4201 info->lti_comp_size = comp_cnt;
4206 * Prepare new layout minus deleted components
4208 * Removes components marked for deletion (LCME_ID_INVAL) by copying to a new
4209 * layout and skipping those components. Removes stripe objects if any exist.
4212 * Reallocates layout components array (lo->ldo_comp_entries), invalidating
4213 * any pre-existing pointers to components.
4215 * Caller is responsible for updating mirror end (ldo_mirror[].lme_end).
4217 * \param[in] env execution environment for this thread
4218 * \param[in,out] lo object to update the layout of
4219 * \param[in] th transaction handle for this operation
4221 * \retval # of components deleted
4222 * \retval negative errno on error
4224 static int lod_layout_del_prep_layout(const struct lu_env *env,
4225 struct lod_object *lo,
4228 struct lod_layout_component *lod_comp;
4229 struct lod_thread_info *info = lod_env_info(env);
4230 int rc = 0, i, j, deleted = 0;
4234 LASSERT(lo->ldo_is_composite);
4235 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
4237 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
4241 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4242 lod_comp = &lo->ldo_comp_entries[i];
4244 if (lod_comp->llc_id != LCME_ID_INVAL) {
4245 /* Build array of things to keep */
4246 info->lti_comp_idx[info->lti_count++] = i;
4250 lod_obj_set_pool(lo, i, NULL);
4251 if (lod_comp->llc_ostlist.op_array) {
4252 OBD_FREE(lod_comp->llc_ostlist.op_array,
4253 lod_comp->llc_ostlist.op_size);
4254 lod_comp->llc_ostlist.op_array = NULL;
4255 lod_comp->llc_ostlist.op_size = 0;
4259 CDEBUG(D_LAYOUT, "deleting comp %d, left %d\n", i,
4260 lo->ldo_comp_cnt - deleted);
4262 /* No striping info for this component */
4263 if (lod_comp->llc_stripe == NULL)
4266 LASSERT(lod_comp->llc_stripe_count > 0);
4267 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4268 struct dt_object *obj = lod_comp->llc_stripe[j];
4273 /* components which are not init have no sub objects
4275 if (lod_comp_inited(lod_comp)) {
4276 rc = lod_sub_destroy(env, obj, th);
4281 lu_object_put(env, &obj->do_lu);
4282 lod_comp->llc_stripe[j] = NULL;
4284 OBD_FREE(lod_comp->llc_stripe, sizeof(*lod_comp->llc_stripe) *
4285 lod_comp->llc_stripes_allocated);
4286 lod_comp->llc_stripe = NULL;
4287 OBD_FREE(lod_comp->llc_ost_indices,
4288 sizeof(__u32) * lod_comp->llc_stripes_allocated);
4289 lod_comp->llc_ost_indices = NULL;
4290 lod_comp->llc_stripes_allocated = 0;
4293 /* info->lti_count has the amount of left components */
4294 LASSERTF(info->lti_count >= 0 && info->lti_count < lo->ldo_comp_cnt,
4295 "left = %d, lo->ldo_comp_cnt %d\n", (int)info->lti_count,
4296 (int)lo->ldo_comp_cnt);
4298 if (info->lti_count > 0) {
4299 struct lod_layout_component *comp_array;
4301 OBD_ALLOC(comp_array, sizeof(*comp_array) * info->lti_count);
4302 if (comp_array == NULL)
4303 GOTO(out, rc = -ENOMEM);
4305 for (i = 0; i < info->lti_count; i++) {
4306 memcpy(&comp_array[i],
4307 &lo->ldo_comp_entries[info->lti_comp_idx[i]],
4308 sizeof(*comp_array));
4311 OBD_FREE(lo->ldo_comp_entries,
4312 sizeof(*comp_array) * lo->ldo_comp_cnt);
4313 lo->ldo_comp_entries = comp_array;
4314 lo->ldo_comp_cnt = info->lti_count;
4316 lod_free_comp_entries(lo);
4321 return rc ? rc : deleted;
4325 * Delete layout component(s)
4327 * This function sets up the layout data in the env and does the setattrs
4328 * required to write out the new layout. The layout itself is modified in
4329 * lod_layout_del_prep_layout.
4331 * \param[in] env execution environment for this thread
4332 * \param[in] dt object
4333 * \param[in] th transaction handle
4335 * \retval 0 on success
4336 * \retval negative error number on failure
4338 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
4341 struct lod_object *lo = lod_dt_obj(dt);
4342 struct dt_object *next = dt_object_child(dt);
4343 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4346 LASSERT(lo->ldo_mirror_count == 1);
4348 rc = lod_layout_del_prep_layout(env, lo, th);
4352 /* Only do this if we didn't delete all components */
4353 if (lo->ldo_comp_cnt > 0) {
4354 lo->ldo_mirrors[0].lme_end = lo->ldo_comp_cnt - 1;
4355 lod_obj_inc_layout_gen(lo);
4358 LASSERT(dt_object_exists(dt));
4359 rc = dt_attr_get(env, next, attr);
4363 if (attr->la_size > 0) {
4365 attr->la_valid = LA_SIZE;
4366 rc = lod_sub_attr_set(env, next, attr, th);
4371 rc = lod_generate_and_set_lovea(env, lo, th);
4375 lod_striping_free(env, lo);
4380 static int lod_get_default_lov_striping(const struct lu_env *env,
4381 struct lod_object *lo,
4382 struct lod_default_striping *lds,
4383 struct dt_allocation_hint *ah);
4385 * Implementation of dt_object_operations::do_xattr_set.
4387 * Sets specified extended attribute on the object. Three types of EAs are
4389 * LOV EA - stores striping for a regular file or default striping (when set
4391 * LMV EA - stores a marker for the striped directories
4392 * DMV EA - stores default directory striping
4394 * When striping is applied to a non-striped existing object (this is called
4395 * late striping), then LOD notices the caller wants to turn the object into a
4396 * striped one. The stripe objects are created and appropriate EA is set:
4397 * LOV EA storing all the stripes directly or LMV EA storing just a small header
4398 * with striping configuration.
4400 * \see dt_object_operations::do_xattr_set() in the API description for details.
4402 static int lod_xattr_set(const struct lu_env *env,
4403 struct dt_object *dt, const struct lu_buf *buf,
4404 const char *name, int fl, struct thandle *th)
4406 struct dt_object *next = dt_object_child(dt);
4411 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4412 !strcmp(name, XATTR_NAME_LMV)) {
4414 case LU_XATTR_CREATE:
4415 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
4418 case LU_XATTR_REPLACE:
4419 rc = lod_dir_layout_set(env, dt, buf, fl, th);
4426 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4427 strcmp(name, XATTR_NAME_LOV) == 0) {
4428 struct lod_default_striping *lds = lod_lds_buf_get(env);
4429 struct lov_user_md_v1 *v1 = buf->lb_buf;
4430 char pool[LOV_MAXPOOLNAME + 1];
4433 /* get existing striping config */
4434 rc = lod_get_default_lov_striping(env, lod_dt_obj(dt), lds,
4439 memset(pool, 0, sizeof(pool));
4440 if (lds->lds_def_striping_set == 1)
4441 lod_layout_get_pool(lds->lds_def_comp_entries,
4442 lds->lds_def_comp_cnt, pool,
4445 is_del = LOVEA_DELETE_VALUES(v1->lmm_stripe_size,
4446 v1->lmm_stripe_count,
4447 v1->lmm_stripe_offset,
4450 /* Retain the pool name if it is not given */
4451 if (v1->lmm_magic == LOV_USER_MAGIC_V1 && pool[0] != '\0' &&
4453 struct lod_thread_info *info = lod_env_info(env);
4454 struct lov_user_md_v3 *v3 = info->lti_ea_store;
4456 memset(v3, 0, sizeof(*v3));
4457 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
4458 v3->lmm_pattern = cpu_to_le32(v1->lmm_pattern);
4459 v3->lmm_stripe_count =
4460 cpu_to_le32(v1->lmm_stripe_count);
4461 v3->lmm_stripe_offset =
4462 cpu_to_le32(v1->lmm_stripe_offset);
4463 v3->lmm_stripe_size = cpu_to_le32(v1->lmm_stripe_size);
4465 strlcpy(v3->lmm_pool_name, pool,
4466 sizeof(v3->lmm_pool_name));
4468 info->lti_buf.lb_buf = v3;
4469 info->lti_buf.lb_len = sizeof(*v3);
4470 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4473 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name,
4477 if (lds->lds_def_striping_set == 1 &&
4478 lds->lds_def_comp_entries != NULL)
4479 lod_free_def_comp_entries(lds);
4482 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4483 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
4485 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
4488 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4489 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4490 strcmp(name, XATTR_LUSTRE_LOV) == 0 ||
4491 allowed_lustre_lov(name))) {
4492 /* in case of lov EA swap, just set it
4493 * if not, it is a replay so check striping match what we
4494 * already have during req replay, declare_xattr_set()
4495 * defines striping, then create() does the work */
4496 if (fl & LU_XATTR_REPLACE) {
4497 /* free stripes, then update disk */
4498 lod_striping_free(env, lod_dt_obj(dt));
4500 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
4501 } else if (dt_object_remote(dt)) {
4502 /* This only happens during migration, see
4503 * mdd_migrate_create(), in which Master MDT will
4504 * create a remote target object, and only set
4505 * (migrating) stripe EA on the remote object,
4506 * and does not need creating each stripes. */
4507 rc = lod_sub_xattr_set(env, next, buf, name,
4509 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
4510 /* delete component(s) */
4511 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
4512 rc = lod_layout_del(env, dt, th);
4515 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
4516 * it's going to create create file with specified
4517 * component(s), the striping must have not being
4518 * cached in this case;
4520 * Otherwise, it's going to add/change component(s) to
4521 * an existing file, the striping must have been cached
4524 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
4525 !strcmp(name, XATTR_NAME_LOV),
4526 !lod_dt_obj(dt)->ldo_comp_cached));
4528 rc = lod_striped_create(env, dt, NULL, NULL, th);
4531 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
4532 rc = lod_replace_parent_fid(env, dt, buf, th, false);
4537 /* then all other xattr */
4538 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
4544 * Implementation of dt_object_operations::do_declare_xattr_del.
4546 * \see dt_object_operations::do_declare_xattr_del() in the API description
4549 static int lod_declare_xattr_del(const struct lu_env *env,
4550 struct dt_object *dt, const char *name,
4553 struct lod_object *lo = lod_dt_obj(dt);
4554 struct dt_object *next = dt_object_child(dt);
4559 rc = lod_sub_declare_xattr_del(env, next, name, th);
4563 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4566 /* NB: don't delete stripe LMV, because when we do this, normally we
4567 * will remove stripes, besides, if directory LMV is corrupt, this will
4568 * prevent deleting its LMV and fixing it (via LFSCK).
4570 if (!strcmp(name, XATTR_NAME_LMV))
4573 rc = lod_striping_load(env, lo);
4577 if (lo->ldo_dir_stripe_count == 0)
4580 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4581 struct dt_object *dto = lo->ldo_stripe[i];
4586 rc = lod_sub_declare_xattr_del(env, dto, name, th);
4595 * Implementation of dt_object_operations::do_xattr_del.
4597 * If EA storing a regular striping is being deleted, then release
4598 * all the references to the stripe objects in core.
4600 * \see dt_object_operations::do_xattr_del() in the API description for details.
4602 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
4603 const char *name, struct thandle *th)
4605 struct dt_object *next = dt_object_child(dt);
4606 struct lod_object *lo = lod_dt_obj(dt);
4611 if (!strcmp(name, XATTR_NAME_LOV) || !strcmp(name, XATTR_NAME_LMV))
4612 lod_striping_free(env, lod_dt_obj(dt));
4614 rc = lod_sub_xattr_del(env, next, name, th);
4615 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
4618 if (!strcmp(name, XATTR_NAME_LMV))
4621 if (lo->ldo_dir_stripe_count == 0)
4624 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4625 struct dt_object *dto = lo->ldo_stripe[i];
4630 rc = lod_sub_xattr_del(env, dto, name, th);
4639 * Implementation of dt_object_operations::do_xattr_list.
4641 * \see dt_object_operations::do_xattr_list() in the API description
4644 static int lod_xattr_list(const struct lu_env *env,
4645 struct dt_object *dt, const struct lu_buf *buf)
4647 return dt_xattr_list(env, dt_object_child(dt), buf);
4650 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
4652 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
4656 * Copy OST list from layout provided by user.
4658 * \param[in] lod_comp layout_component to be filled
4659 * \param[in] v3 LOV EA V3 user data
4661 * \retval 0 on success
4662 * \retval negative if failed
4664 int lod_comp_copy_ost_lists(struct lod_layout_component *lod_comp,
4665 struct lov_user_md_v3 *v3)
4671 if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
4672 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
4674 if (lod_comp->llc_ostlist.op_array) {
4675 if (lod_comp->llc_ostlist.op_size >=
4676 v3->lmm_stripe_count * sizeof(__u32)) {
4677 lod_comp->llc_ostlist.op_count =
4678 v3->lmm_stripe_count;
4681 OBD_FREE(lod_comp->llc_ostlist.op_array,
4682 lod_comp->llc_ostlist.op_size);
4685 /* copy ost list from lmm */
4686 lod_comp->llc_ostlist.op_count = v3->lmm_stripe_count;
4687 lod_comp->llc_ostlist.op_size = v3->lmm_stripe_count * sizeof(__u32);
4688 OBD_ALLOC(lod_comp->llc_ostlist.op_array,
4689 lod_comp->llc_ostlist.op_size);
4690 if (!lod_comp->llc_ostlist.op_array)
4693 for (j = 0; j < v3->lmm_stripe_count; j++) {
4694 lod_comp->llc_ostlist.op_array[j] =
4695 v3->lmm_objects[j].l_ost_idx;
4703 * Get default striping.
4705 * \param[in] env execution environment
4706 * \param[in] lo object
4707 * \param[out] lds default striping
4709 * \retval 0 on success
4710 * \retval negative if failed
4712 static int lod_get_default_lov_striping(const struct lu_env *env,
4713 struct lod_object *lo,
4714 struct lod_default_striping *lds,
4715 struct dt_allocation_hint *ah)
4717 struct lod_thread_info *info = lod_env_info(env);
4718 struct lov_user_md_v1 *v1 = NULL;
4719 struct lov_user_md_v3 *v3 = NULL;
4720 struct lov_comp_md_v1 *comp_v1 = NULL;
4728 rc = lod_get_lov_ea(env, lo);
4732 if (rc < (typeof(rc))sizeof(struct lov_user_md))
4735 v1 = info->lti_ea_store;
4736 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
4737 lustre_swab_lov_user_md_v1(v1);
4738 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
4739 v3 = (struct lov_user_md_v3 *)v1;
4740 lustre_swab_lov_user_md_v3(v3);
4741 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_SPECIFIC)) {
4742 v3 = (struct lov_user_md_v3 *)v1;
4743 lustre_swab_lov_user_md_v3(v3);
4744 lustre_swab_lov_user_md_objects(v3->lmm_objects,
4745 v3->lmm_stripe_count);
4746 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1) ||
4747 v1->lmm_magic == __swab32(LOV_USER_MAGIC_SEL)) {
4748 comp_v1 = (struct lov_comp_md_v1 *)v1;
4749 lustre_swab_lov_comp_md_v1(comp_v1);
4752 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
4753 v1->lmm_magic != LOV_MAGIC_COMP_V1 &&
4754 v1->lmm_magic != LOV_MAGIC_SEL &&
4755 v1->lmm_magic != LOV_USER_MAGIC_SPECIFIC)
4758 if ((v1->lmm_magic == LOV_MAGIC_COMP_V1 ||
4759 v1->lmm_magic == LOV_MAGIC_SEL) &&
4760 !(ah && ah->dah_append_stripes)) {
4761 comp_v1 = (struct lov_comp_md_v1 *)v1;
4762 comp_cnt = comp_v1->lcm_entry_count;
4765 mirror_cnt = comp_v1->lcm_mirror_count + 1;
4773 /* realloc default comp entries if necessary */
4774 rc = lod_def_striping_comp_resize(lds, comp_cnt);
4778 lds->lds_def_comp_cnt = comp_cnt;
4779 lds->lds_def_striping_is_composite = composite;
4780 lds->lds_def_mirror_cnt = mirror_cnt;
4782 for (i = 0; i < comp_cnt; i++) {
4783 struct lod_layout_component *lod_comp;
4786 lod_comp = &lds->lds_def_comp_entries[i];
4788 * reset lod_comp values, llc_stripes is always NULL in
4789 * the default striping template, llc_pool will be reset
4792 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
4795 v1 = (struct lov_user_md *)((char *)comp_v1 +
4796 comp_v1->lcm_entries[i].lcme_offset);
4797 lod_comp->llc_extent =
4798 comp_v1->lcm_entries[i].lcme_extent;
4799 /* We only inherit certain flags from the layout */
4800 lod_comp->llc_flags =
4801 comp_v1->lcm_entries[i].lcme_flags &
4802 LCME_TEMPLATE_FLAGS;
4805 if (!lov_pattern_supported(v1->lmm_pattern) &&
4806 !(v1->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
4807 lod_free_def_comp_entries(lds);
4811 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d stripe_offset=%d append_stripes=%d\n",
4812 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
4813 (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
4814 (int)v1->lmm_stripe_offset,
4815 ah ? ah->dah_append_stripes : 0);
4817 if (ah && ah->dah_append_stripes)
4818 lod_comp->llc_stripe_count = ah->dah_append_stripes;
4820 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
4821 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
4822 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
4823 lod_comp->llc_pattern = v1->lmm_pattern;
4826 if (ah && ah->dah_append_pool && ah->dah_append_pool[0]) {
4827 pool = ah->dah_append_pool;
4828 } else if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
4829 /* XXX: sanity check here */
4830 v3 = (struct lov_user_md_v3 *) v1;
4831 if (v3->lmm_pool_name[0] != '\0')
4832 pool = v3->lmm_pool_name;
4834 lod_set_def_pool(lds, i, pool);
4835 if (v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
4836 v3 = (struct lov_user_md_v3 *)v1;
4837 rc = lod_comp_copy_ost_lists(lod_comp, v3);
4840 } else if (lod_comp->llc_ostlist.op_array &&
4841 lod_comp->llc_ostlist.op_count) {
4842 for (j = 0; j < lod_comp->llc_ostlist.op_count; j++)
4843 lod_comp->llc_ostlist.op_array[j] = -1;
4844 lod_comp->llc_ostlist.op_count = 0;
4848 lds->lds_def_striping_set = 1;
4853 * Get default directory striping.
4855 * \param[in] env execution environment
4856 * \param[in] lo object
4857 * \param[out] lds default striping
4859 * \retval 0 on success
4860 * \retval negative if failed
4862 static int lod_get_default_lmv_striping(const struct lu_env *env,
4863 struct lod_object *lo,
4864 struct lod_default_striping *lds)
4866 struct lmv_user_md *lmu;
4869 lds->lds_dir_def_striping_set = 0;
4871 rc = lod_get_default_lmv_ea(env, lo);
4875 if (rc >= (int)sizeof(*lmu)) {
4876 struct lod_thread_info *info = lod_env_info(env);
4878 lmu = info->lti_ea_store;
4880 lds->lds_dir_def_stripe_count =
4881 le32_to_cpu(lmu->lum_stripe_count);
4882 lds->lds_dir_def_stripe_offset =
4883 le32_to_cpu(lmu->lum_stripe_offset);
4884 lds->lds_dir_def_hash_type =
4885 le32_to_cpu(lmu->lum_hash_type);
4886 lds->lds_dir_def_striping_set = 1;
4893 * Get default striping in the object.
4895 * Get object default striping and default directory striping.
4897 * \param[in] env execution environment
4898 * \param[in] lo object
4899 * \param[out] lds default striping
4901 * \retval 0 on success
4902 * \retval negative if failed
4904 static int lod_get_default_striping(const struct lu_env *env,
4905 struct lod_object *lo,
4906 struct lod_default_striping *lds)
4910 rc = lod_get_default_lov_striping(env, lo, lds, NULL);
4911 rc1 = lod_get_default_lmv_striping(env, lo, lds);
4912 if (rc == 0 && rc1 < 0)
4919 * Apply default striping on object.
4921 * If object striping pattern is not set, set to the one in default striping.
4922 * The default striping is from parent or fs.
4924 * \param[in] lo new object
4925 * \param[in] lds default striping
4926 * \param[in] mode new object's mode
4928 static void lod_striping_from_default(struct lod_object *lo,
4929 const struct lod_default_striping *lds,
4932 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4935 if (lds->lds_def_striping_set && S_ISREG(mode)) {
4936 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
4938 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
4939 lds->lds_def_comp_cnt);
4943 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
4944 if (lds->lds_def_mirror_cnt > 1)
4945 lo->ldo_flr_state = LCM_FL_RDONLY;
4947 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4948 struct lod_layout_component *obj_comp =
4949 &lo->ldo_comp_entries[i];
4950 struct lod_layout_component *def_comp =
4951 &lds->lds_def_comp_entries[i];
4953 CDEBUG(D_LAYOUT, "Inherit from default: flags=%#x "
4954 "size=%hu nr=%u offset=%u pattern=%#x pool=%s\n",
4955 def_comp->llc_flags,
4956 def_comp->llc_stripe_size,
4957 def_comp->llc_stripe_count,
4958 def_comp->llc_stripe_offset,
4959 def_comp->llc_pattern,
4960 def_comp->llc_pool ?: "");
4962 *obj_comp = *def_comp;
4963 if (def_comp->llc_pool != NULL) {
4964 /* pointer was copied from def_comp */
4965 obj_comp->llc_pool = NULL;
4966 lod_obj_set_pool(lo, i, def_comp->llc_pool);
4970 if (def_comp->llc_ostlist.op_array &&
4971 def_comp->llc_ostlist.op_count) {
4972 OBD_ALLOC(obj_comp->llc_ostlist.op_array,
4973 obj_comp->llc_ostlist.op_size);
4974 if (!obj_comp->llc_ostlist.op_array)
4976 memcpy(obj_comp->llc_ostlist.op_array,
4977 def_comp->llc_ostlist.op_array,
4978 obj_comp->llc_ostlist.op_size);
4979 } else if (def_comp->llc_ostlist.op_array) {
4980 obj_comp->llc_ostlist.op_array = NULL;
4984 * Don't initialize these fields for plain layout
4985 * (v1/v3) here, they are inherited in the order of
4986 * 'parent' -> 'fs default (root)' -> 'global default
4987 * values for stripe_count & stripe_size'.
4989 * see lod_ah_init().
4991 if (!lo->ldo_is_composite)
4994 lod_adjust_stripe_info(obj_comp, desc, 0);
4996 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
4997 if (lo->ldo_dir_stripe_count == 0)
4998 lo->ldo_dir_stripe_count =
4999 lds->lds_dir_def_stripe_count;
5000 if (lo->ldo_dir_stripe_offset == -1)
5001 lo->ldo_dir_stripe_offset =
5002 lds->lds_dir_def_stripe_offset;
5003 if (lo->ldo_dir_hash_type == 0)
5004 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
5006 CDEBUG(D_LAYOUT, "striping from default dir: count:%hu, "
5007 "offset:%u, hash_type:%u\n",
5008 lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
5009 lo->ldo_dir_hash_type);
5013 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root,
5016 struct lod_layout_component *lod_comp;
5018 if (lo->ldo_comp_cnt == 0)
5021 if (lo->ldo_is_composite)
5024 lod_comp = &lo->ldo_comp_entries[0];
5026 if (lod_comp->llc_stripe_count <= 0 ||
5027 lod_comp->llc_stripe_size <= 0)
5030 if (from_root && (lod_comp->llc_pool == NULL ||
5031 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
5034 if (append_pool && append_pool[0])
5041 * Implementation of dt_object_operations::do_ah_init.
5043 * This method is used to make a decision on the striping configuration for the
5044 * object being created. It can be taken from the \a parent object if it exists,
5045 * or filesystem's default. The resulting configuration (number of stripes,
5046 * stripe size/offset, pool name, etc) is stored in the object itself and will
5047 * be used by the methods like ->doo_declare_create().
5049 * \see dt_object_operations::do_ah_init() in the API description for details.
5051 static void lod_ah_init(const struct lu_env *env,
5052 struct dt_allocation_hint *ah,
5053 struct dt_object *parent,
5054 struct dt_object *child,
5057 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
5058 struct lod_thread_info *info = lod_env_info(env);
5059 struct lod_default_striping *lds = lod_lds_buf_get(env);
5060 struct dt_object *nextp = NULL;
5061 struct dt_object *nextc;
5062 struct lod_object *lp = NULL;
5063 struct lod_object *lc;
5064 struct lov_desc *desc;
5065 struct lod_layout_component *lod_comp;
5071 if (ah->dah_append_stripes == -1)
5072 ah->dah_append_stripes =
5073 d->lod_ost_descs.ltd_lov_desc.ld_tgt_count;
5075 if (likely(parent)) {
5076 nextp = dt_object_child(parent);
5077 lp = lod_dt_obj(parent);
5080 nextc = dt_object_child(child);
5081 lc = lod_dt_obj(child);
5083 LASSERT(!lod_obj_is_striped(child));
5084 /* default layout template may have been set on the regular file
5085 * when this is called from mdd_create_data() */
5086 if (S_ISREG(child_mode))
5087 lod_free_comp_entries(lc);
5089 if (!dt_object_exists(nextc))
5090 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
5092 if (S_ISDIR(child_mode)) {
5093 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
5095 /* other default values are 0 */
5096 lc->ldo_dir_stripe_offset = -1;
5098 /* no default striping configuration is needed for
5101 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5102 le32_to_cpu(lum1->lum_magic) == LMV_MAGIC_FOREIGN) {
5103 lc->ldo_dir_is_foreign = true;
5104 /* keep stripe_count 0 and stripe_offset -1 */
5105 CDEBUG(D_INFO, "no default striping for foreign dir\n");
5110 * If parent object is not root directory,
5111 * then get default striping from parent object.
5113 if (likely(lp != NULL)) {
5114 lod_get_default_striping(env, lp, lds);
5116 /* inherit default striping except ROOT */
5117 if ((lds->lds_def_striping_set ||
5118 lds->lds_dir_def_striping_set) &&
5119 !fid_is_root(lod_object_fid(lp)))
5120 lc->ldo_def_striping = lds;
5123 /* It should always honour the specified stripes */
5124 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
5125 * will have old magic. In this case, we should ignore the
5126 * stripe count and try to create dir by default stripe.
5128 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5129 (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
5130 le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
5131 lc->ldo_dir_stripe_count =
5132 le32_to_cpu(lum1->lum_stripe_count);
5133 lc->ldo_dir_stripe_offset =
5134 le32_to_cpu(lum1->lum_stripe_offset);
5135 lc->ldo_dir_hash_type =
5136 le32_to_cpu(lum1->lum_hash_type);
5138 "set dirstripe: count %hu, offset %d, hash %u\n",
5139 lc->ldo_dir_stripe_count,
5140 (int)lc->ldo_dir_stripe_offset,
5141 lc->ldo_dir_hash_type);
5143 /* transfer defaults LMV to new directory */
5144 lod_striping_from_default(lc, lds, child_mode);
5146 /* set count 0 to create normal directory */
5147 if (lc->ldo_dir_stripe_count == 1)
5148 lc->ldo_dir_stripe_count = 0;
5151 /* shrink the stripe_count to the avaible MDT count */
5152 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
5153 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)) {
5154 lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
5155 if (lc->ldo_dir_stripe_count == 1)
5156 lc->ldo_dir_stripe_count = 0;
5159 if (!(lc->ldo_dir_hash_type & LMV_HASH_TYPE_MASK))
5160 lc->ldo_dir_hash_type |=
5161 d->lod_mdt_descs.ltd_lmv_desc.ld_pattern;
5163 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
5164 lc->ldo_dir_stripe_count,
5165 (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
5170 /* child object regular file*/
5172 if (!lod_object_will_be_striped(S_ISREG(child_mode),
5173 lu_object_fid(&child->do_lu)))
5176 /* If object is going to be striped over OSTs, transfer default
5177 * striping information to the child, so that we can use it
5178 * during declaration and creation.
5180 * Try from the parent first.
5182 if (likely(lp != NULL)) {
5183 rc = lod_get_default_lov_striping(env, lp, lds, ah);
5185 lod_striping_from_default(lc, lds, child_mode);
5188 /* Initialize lod_device::lod_md_root object reference */
5189 if (d->lod_md_root == NULL) {
5190 struct dt_object *root;
5191 struct lod_object *lroot;
5193 lu_root_fid(&info->lti_fid);
5194 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
5195 if (!IS_ERR(root)) {
5196 lroot = lod_dt_obj(root);
5198 spin_lock(&d->lod_lock);
5199 if (d->lod_md_root != NULL)
5200 dt_object_put(env, &d->lod_md_root->ldo_obj);
5201 d->lod_md_root = lroot;
5202 spin_unlock(&d->lod_lock);
5206 /* try inherit layout from the root object (fs default) when:
5207 * - parent does not have default layout; or
5208 * - parent has plain(v1/v3) default layout, and some attributes
5209 * are not specified in the default layout;
5211 if (d->lod_md_root != NULL &&
5212 lod_need_inherit_more(lc, true, ah->dah_append_pool)) {
5213 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds,
5217 if (lc->ldo_comp_cnt == 0) {
5218 lod_striping_from_default(lc, lds, child_mode);
5219 } else if (!lds->lds_def_striping_is_composite) {
5220 struct lod_layout_component *def_comp;
5222 LASSERT(!lc->ldo_is_composite);
5223 lod_comp = &lc->ldo_comp_entries[0];
5224 def_comp = &lds->lds_def_comp_entries[0];
5226 if (lod_comp->llc_stripe_count <= 0)
5227 lod_comp->llc_stripe_count =
5228 def_comp->llc_stripe_count;
5229 if (lod_comp->llc_stripe_size <= 0)
5230 lod_comp->llc_stripe_size =
5231 def_comp->llc_stripe_size;
5232 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT &&
5233 (!lod_comp->llc_pool || !lod_comp->llc_pool[0]))
5234 lod_comp->llc_stripe_offset =
5235 def_comp->llc_stripe_offset;
5236 if (lod_comp->llc_pool == NULL)
5237 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
5242 * fs default striping may not be explicitly set, or historically set
5243 * in config log, use them.
5245 if (lod_need_inherit_more(lc, false, ah->dah_append_pool)) {
5246 if (lc->ldo_comp_cnt == 0) {
5247 rc = lod_alloc_comp_entries(lc, 0, 1);
5249 /* fail to allocate memory, will create a
5250 * non-striped file. */
5252 lc->ldo_is_composite = 0;
5253 lod_comp = &lc->ldo_comp_entries[0];
5254 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
5256 LASSERT(!lc->ldo_is_composite);
5257 lod_comp = &lc->ldo_comp_entries[0];
5258 desc = &d->lod_ost_descs.ltd_lov_desc;
5259 lod_adjust_stripe_info(lod_comp, desc, ah->dah_append_stripes);
5260 if (ah->dah_append_pool && ah->dah_append_pool[0])
5261 lod_obj_set_pool(lc, 0, ah->dah_append_pool);
5268 * Size initialization on late striping.
5270 * Propagate the size of a truncated object to a deferred striping.
5271 * This function handles a special case when truncate was done on a
5272 * non-striped object and now while the striping is being created
5273 * we can't lose that size, so we have to propagate it to the stripes
5276 * \param[in] env execution environment
5277 * \param[in] dt object
5278 * \param[in] th transaction handle
5280 * \retval 0 on success
5281 * \retval negative if failed
5283 static int lod_declare_init_size(const struct lu_env *env,
5284 struct dt_object *dt, struct thandle *th)
5286 struct dt_object *next = dt_object_child(dt);
5287 struct lod_object *lo = lod_dt_obj(dt);
5288 struct dt_object **objects = NULL;
5289 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
5290 uint64_t size, offs;
5291 int i, rc, stripe, stripe_count = 0, stripe_size = 0;
5292 struct lu_extent size_ext;
5295 if (!lod_obj_is_striped(dt))
5298 rc = dt_attr_get(env, next, attr);
5299 LASSERT(attr->la_valid & LA_SIZE);
5303 size = attr->la_size;
5307 size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
5308 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5309 struct lod_layout_component *lod_comp;
5310 struct lu_extent *extent;
5312 lod_comp = &lo->ldo_comp_entries[i];
5314 if (lod_comp->llc_stripe == NULL)
5317 extent = &lod_comp->llc_extent;
5318 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
5319 if (!lo->ldo_is_composite ||
5320 lu_extent_is_overlapped(extent, &size_ext)) {
5321 objects = lod_comp->llc_stripe;
5322 stripe_count = lod_comp->llc_stripe_count;
5323 stripe_size = lod_comp->llc_stripe_size;
5326 if (stripe_count == 0)
5329 LASSERT(objects != NULL && stripe_size != 0);
5330 do_div(size, stripe_size);
5331 stripe = do_div(size, stripe_count);
5332 LASSERT(objects[stripe] != NULL);
5334 size = size * stripe_size;
5335 offs = attr->la_size;
5336 size += do_div(offs, stripe_size);
5338 attr->la_valid = LA_SIZE;
5339 attr->la_size = size;
5341 rc = lod_sub_declare_attr_set(env, objects[stripe],
5350 * Declare creation of striped object.
5352 * The function declares creation stripes for a regular object. The function
5353 * also declares whether the stripes will be created with non-zero size if
5354 * previously size was set non-zero on the master object. If object \a dt is
5355 * not local, then only fully defined striping can be applied in \a lovea.
5356 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
5359 * \param[in] env execution environment
5360 * \param[in] dt object
5361 * \param[in] attr attributes the stripes will be created with
5362 * \param[in] lovea a buffer containing striping description
5363 * \param[in] th transaction handle
5365 * \retval 0 on success
5366 * \retval negative if failed
5368 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
5369 struct lu_attr *attr,
5370 const struct lu_buf *lovea, struct thandle *th)
5372 struct lod_thread_info *info = lod_env_info(env);
5373 struct dt_object *next = dt_object_child(dt);
5374 struct lod_object *lo = lod_dt_obj(dt);
5378 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
5379 GOTO(out, rc = -ENOMEM);
5381 if (!dt_object_remote(next)) {
5382 /* choose OST and generate appropriate objects */
5383 rc = lod_prepare_create(env, lo, attr, lovea, th);
5388 * declare storage for striping data
5390 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5392 /* LOD can not choose OST objects for remote objects, i.e.
5393 * stripes must be ready before that. Right now, it can only
5394 * happen during migrate, i.e. migrate process needs to create
5395 * remote regular file (mdd_migrate_create), then the migrate
5396 * process will provide stripeEA. */
5397 LASSERT(lovea != NULL);
5398 info->lti_buf = *lovea;
5401 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
5402 XATTR_NAME_LOV, 0, th);
5407 * if striping is created with local object's size > 0,
5408 * we have to propagate this size to specific object
5409 * the case is possible only when local object was created previously
5411 if (dt_object_exists(next))
5412 rc = lod_declare_init_size(env, dt, th);
5415 /* failed to create striping or to set initial size, let's reset
5416 * config so that others don't get confused */
5418 lod_striping_free(env, lo);
5424 * Whether subdirectories under \a dt should be created on MDTs by space QoS
5426 * If LMV_HASH_FLAG_SPACE is set on directory default layout, its subdirectories
5427 * should be created on MDT by space QoS.
5429 * \param[in] env execution environment
5430 * \param[in] dev lu device
5431 * \param[in] dt object
5433 * \retval 1 if directory should create subdir by space usage
5435 * \retval -ev if failed
5437 static inline int dt_object_qos_mkdir(const struct lu_env *env,
5438 struct lu_device *dev,
5439 struct dt_object *dt)
5441 struct lod_thread_info *info = lod_env_info(env);
5442 struct lu_object *obj;
5443 struct lod_object *lo;
5444 struct lmv_user_md *lmu;
5447 obj = lu_object_find_slice(env, dev, lu_object_fid(&dt->do_lu), NULL);
5449 return PTR_ERR(obj);
5451 lo = lu2lod_obj(obj);
5453 rc = lod_get_default_lmv_ea(env, lo);
5454 dt_object_put(env, dt);
5458 if (rc < (int)sizeof(*lmu))
5461 lmu = info->lti_ea_store;
5462 return le32_to_cpu(lmu->lum_stripe_offset) == LMV_OFFSET_DEFAULT;
5466 * Implementation of dt_object_operations::do_declare_create.
5468 * The method declares creation of a new object. If the object will be striped,
5469 * then helper functions are called to find FIDs for the stripes, declare
5470 * creation of the stripes and declare initialization of the striping
5471 * information to be stored in the master object.
5473 * \see dt_object_operations::do_declare_create() in the API description
5476 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
5477 struct lu_attr *attr,
5478 struct dt_allocation_hint *hint,
5479 struct dt_object_format *dof, struct thandle *th)
5481 struct dt_object *next = dt_object_child(dt);
5482 struct lod_object *lo = lod_dt_obj(dt);
5491 * first of all, we declare creation of local object
5493 rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
5498 * it's lod_ah_init() that has decided the object will be striped
5500 if (dof->dof_type == DFT_REGULAR) {
5501 /* callers don't want stripes */
5502 /* XXX: all tricky interactions with ->ah_make_hint() decided
5503 * to use striping, then ->declare_create() behaving differently
5504 * should be cleaned */
5505 if (dof->u.dof_reg.striped != 0)
5506 rc = lod_declare_striped_create(env, dt, attr,
5508 } else if (dof->dof_type == DFT_DIR) {
5509 struct seq_server_site *ss;
5510 struct lu_buf buf = { NULL };
5511 struct lu_buf *lmu = NULL;
5513 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
5515 /* If the parent has default stripeEA, and client
5516 * did not find it before sending create request,
5517 * then MDT will return -EREMOTE, and client will
5518 * retrieve the default stripeEA and re-create the
5521 * Note: if dah_eadata != NULL, it means creating the
5522 * striped directory with specified stripeEA, then it
5523 * should ignore the default stripeEA */
5524 if (hint != NULL && hint->dah_eadata == NULL) {
5525 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
5526 GOTO(out, rc = -EREMOTE);
5528 if (lo->ldo_dir_stripe_offset == LMV_OFFSET_DEFAULT) {
5529 struct lod_default_striping *lds;
5531 lds = lo->ldo_def_striping;
5533 * child and parent should be on the same MDT,
5534 * but if parent has default LMV, and the start
5535 * MDT offset is -1, it's allowed. This check
5536 * is not necessary after 2.12.22 because client
5537 * follows this already, but old client may not.
5539 if (hint->dah_parent &&
5540 dt_object_remote(hint->dah_parent) && lds &&
5541 lds->lds_dir_def_stripe_offset !=
5543 GOTO(out, rc = -EREMOTE);
5544 } else if (lo->ldo_dir_stripe_offset !=
5546 struct lod_device *lod;
5547 struct lu_tgt_desc *mdt = NULL;
5548 bool found_mdt = false;
5550 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5551 lod_foreach_mdt(lod, mdt) {
5552 if (mdt->ltd_index ==
5553 lo->ldo_dir_stripe_offset) {
5559 /* If the MDT indicated by stripe_offset can be
5560 * found, then tell client to resend the create
5561 * request to the correct MDT, otherwise return
5562 * error to client */
5564 GOTO(out, rc = -EREMOTE);
5566 GOTO(out, rc = -EINVAL);
5568 } else if (hint && hint->dah_eadata) {
5570 lmu->lb_buf = (void *)hint->dah_eadata;
5571 lmu->lb_len = hint->dah_eadata_len;
5574 rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
5578 /* failed to create striping or to set initial size, let's reset
5579 * config so that others don't get confused */
5581 lod_striping_free(env, lo);
5586 * Generate component ID for new created component.
5588 * \param[in] lo LOD object
5589 * \param[in] comp_idx index of ldo_comp_entries
5591 * \retval component ID on success
5592 * \retval LCME_ID_INVAL on failure
5594 static __u32 lod_gen_component_id(struct lod_object *lo,
5595 int mirror_id, int comp_idx)
5597 struct lod_layout_component *lod_comp;
5598 __u32 id, start, end;
5601 LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
5603 lod_obj_inc_layout_gen(lo);
5604 id = lo->ldo_layout_gen;
5605 if (likely(id <= SEQ_ID_MAX))
5606 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
5608 /* Layout generation wraps, need to check collisions. */
5609 start = id & SEQ_ID_MASK;
5612 for (id = start; id <= end; id++) {
5613 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5614 lod_comp = &lo->ldo_comp_entries[i];
5615 if (pflr_id(mirror_id, id) == lod_comp->llc_id)
5618 /* Found the ununsed ID */
5619 if (i == lo->ldo_comp_cnt)
5620 RETURN(pflr_id(mirror_id, id));
5622 if (end == LCME_ID_MAX) {
5624 end = min(lo->ldo_layout_gen & LCME_ID_MASK,
5625 (__u32)(LCME_ID_MAX - 1));
5629 RETURN(LCME_ID_INVAL);
5633 * Creation of a striped regular object.
5635 * The function is called to create the stripe objects for a regular
5636 * striped file. This can happen at the initial object creation or
5637 * when the caller asks LOD to do so using ->do_xattr_set() method
5638 * (so called late striping). Notice all the information are already
5639 * prepared in the form of the list of objects (ldo_stripe field).
5640 * This is done during declare phase.
5642 * \param[in] env execution environment
5643 * \param[in] dt object
5644 * \param[in] attr attributes the stripes will be created with
5645 * \param[in] dof format of stripes (see OSD API description)
5646 * \param[in] th transaction handle
5648 * \retval 0 on success
5649 * \retval negative if failed
5651 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
5652 struct lu_attr *attr, struct dt_object_format *dof,
5655 struct lod_layout_component *lod_comp;
5656 struct lod_object *lo = lod_dt_obj(dt);
5661 LASSERT((lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL) ||
5662 lo->ldo_is_foreign);
5664 mirror_id = 0; /* non-flr file's mirror_id is 0 */
5665 if (lo->ldo_mirror_count > 1) {
5666 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5667 lod_comp = &lo->ldo_comp_entries[i];
5668 if (lod_comp->llc_id != LCME_ID_INVAL &&
5669 mirror_id_of(lod_comp->llc_id) > mirror_id)
5670 mirror_id = mirror_id_of(lod_comp->llc_id);
5674 /* create all underlying objects */
5675 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5676 lod_comp = &lo->ldo_comp_entries[i];
5678 if (lod_comp->llc_id == LCME_ID_INVAL) {
5679 /* only the component of FLR layout with more than 1
5680 * mirror has mirror ID in its component ID.
5682 if (lod_comp->llc_extent.e_start == 0 &&
5683 lo->ldo_mirror_count > 1)
5686 lod_comp->llc_id = lod_gen_component_id(lo,
5688 if (lod_comp->llc_id == LCME_ID_INVAL)
5689 GOTO(out, rc = -ERANGE);
5692 if (lod_comp_inited(lod_comp))
5695 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
5696 lod_comp_set_init(lod_comp);
5698 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
5699 lod_comp_set_init(lod_comp);
5701 if (lod_comp->llc_stripe == NULL)
5704 LASSERT(lod_comp->llc_stripe_count);
5705 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
5706 struct dt_object *object = lod_comp->llc_stripe[j];
5707 LASSERT(object != NULL);
5708 rc = lod_sub_create(env, object, attr, NULL, dof, th);
5712 lod_comp_set_init(lod_comp);
5715 rc = lod_fill_mirrors(lo);
5719 rc = lod_generate_and_set_lovea(env, lo, th);
5723 lo->ldo_comp_cached = 1;
5727 lod_striping_free(env, lo);
5731 static inline bool lod_obj_is_dom(struct dt_object *dt)
5733 struct lod_object *lo = lod_dt_obj(dt);
5735 if (!dt_object_exists(dt_object_child(dt)))
5738 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
5741 if (!lo->ldo_comp_cnt)
5744 return (lov_pattern(lo->ldo_comp_entries[0].llc_pattern) ==
5749 * Implementation of dt_object_operations::do_create.
5751 * If any of preceeding methods (like ->do_declare_create(),
5752 * ->do_ah_init(), etc) chose to create a striped object,
5753 * then this method will create the master and the stripes.
5755 * \see dt_object_operations::do_create() in the API description for details.
5757 static int lod_create(const struct lu_env *env, struct dt_object *dt,
5758 struct lu_attr *attr, struct dt_allocation_hint *hint,
5759 struct dt_object_format *dof, struct thandle *th)
5764 /* create local object */
5765 rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
5769 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
5770 (lod_obj_is_striped(dt) || lod_obj_is_dom(dt)) &&
5771 dof->u.dof_reg.striped != 0) {
5772 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
5773 rc = lod_striped_create(env, dt, attr, dof, th);
5780 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
5781 struct dt_object *dt, struct thandle *th,
5782 int comp_idx, int stripe_idx,
5783 struct lod_obj_stripe_cb_data *data)
5785 if (data->locd_declare)
5786 return lod_sub_declare_destroy(env, dt, th);
5787 else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
5788 stripe_idx == cfs_fail_val)
5789 return lod_sub_destroy(env, dt, th);
5795 * Implementation of dt_object_operations::do_declare_destroy.
5797 * If the object is a striped directory, then the function declares reference
5798 * removal from the master object (this is an index) to the stripes and declares
5799 * destroy of all the stripes. In all the cases, it declares an intention to
5800 * destroy the object itself.
5802 * \see dt_object_operations::do_declare_destroy() in the API description
5805 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
5808 struct dt_object *next = dt_object_child(dt);
5809 struct lod_object *lo = lod_dt_obj(dt);
5810 struct lod_thread_info *info = lod_env_info(env);
5811 struct dt_object *stripe;
5812 char *stripe_name = info->lti_key;
5818 * load striping information, notice we don't do this when object
5819 * is being initialized as we don't need this information till
5820 * few specific cases like destroy, chown
5822 rc = lod_striping_load(env, lo);
5826 /* declare destroy for all underlying objects */
5827 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5828 rc = next->do_ops->do_index_try(env, next,
5829 &dt_directory_features);
5833 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5834 stripe = lo->ldo_stripe[i];
5838 rc = lod_sub_declare_ref_del(env, next, th);
5842 snprintf(stripe_name, sizeof(info->lti_key),
5844 PFID(lu_object_fid(&stripe->do_lu)), i);
5845 rc = lod_sub_declare_delete(env, next,
5846 (const struct dt_key *)stripe_name, th);
5853 * we declare destroy for the local object
5855 rc = lod_sub_declare_destroy(env, next, th);
5859 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
5860 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
5863 if (!lod_obj_is_striped(dt))
5866 /* declare destroy all striped objects */
5867 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5868 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5869 stripe = lo->ldo_stripe[i];
5873 if (!dt_object_exists(stripe))
5876 rc = lod_sub_declare_ref_del(env, stripe, th);
5880 rc = lod_sub_declare_destroy(env, stripe, th);
5885 struct lod_obj_stripe_cb_data data = { { 0 } };
5887 data.locd_declare = true;
5888 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
5889 rc = lod_obj_for_each_stripe(env, lo, th, &data);
5896 * Implementation of dt_object_operations::do_destroy.
5898 * If the object is a striped directory, then the function removes references
5899 * from the master object (this is an index) to the stripes and destroys all
5900 * the stripes. In all the cases, the function destroys the object itself.
5902 * \see dt_object_operations::do_destroy() in the API description for details.
5904 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
5907 struct dt_object *next = dt_object_child(dt);
5908 struct lod_object *lo = lod_dt_obj(dt);
5909 struct lod_thread_info *info = lod_env_info(env);
5910 char *stripe_name = info->lti_key;
5911 struct dt_object *stripe;
5917 /* destroy sub-stripe of master object */
5918 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5919 rc = next->do_ops->do_index_try(env, next,
5920 &dt_directory_features);
5924 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5925 stripe = lo->ldo_stripe[i];
5929 rc = lod_sub_ref_del(env, next, th);
5933 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
5934 PFID(lu_object_fid(&stripe->do_lu)), i);
5936 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
5937 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
5938 PFID(lu_object_fid(&stripe->do_lu)));
5940 rc = lod_sub_delete(env, next,
5941 (const struct dt_key *)stripe_name, th);
5947 rc = lod_sub_destroy(env, next, th);
5951 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
5952 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
5955 if (!lod_obj_is_striped(dt))
5958 /* destroy all striped objects */
5959 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5960 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5961 stripe = lo->ldo_stripe[i];
5965 if (!dt_object_exists(stripe))
5968 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
5969 i == cfs_fail_val) {
5970 dt_write_lock(env, stripe, DT_TGT_CHILD);
5971 rc = lod_sub_ref_del(env, stripe, th);
5972 dt_write_unlock(env, stripe);
5976 rc = lod_sub_destroy(env, stripe, th);
5982 struct lod_obj_stripe_cb_data data = { { 0 } };
5984 data.locd_declare = false;
5985 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
5986 rc = lod_obj_for_each_stripe(env, lo, th, &data);
5993 * Implementation of dt_object_operations::do_declare_ref_add.
5995 * \see dt_object_operations::do_declare_ref_add() in the API description
5998 static int lod_declare_ref_add(const struct lu_env *env,
5999 struct dt_object *dt, struct thandle *th)
6001 return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
6005 * Implementation of dt_object_operations::do_ref_add.
6007 * \see dt_object_operations::do_ref_add() in the API description for details.
6009 static int lod_ref_add(const struct lu_env *env,
6010 struct dt_object *dt, struct thandle *th)
6012 return lod_sub_ref_add(env, dt_object_child(dt), th);
6016 * Implementation of dt_object_operations::do_declare_ref_del.
6018 * \see dt_object_operations::do_declare_ref_del() in the API description
6021 static int lod_declare_ref_del(const struct lu_env *env,
6022 struct dt_object *dt, struct thandle *th)
6024 return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
6028 * Implementation of dt_object_operations::do_ref_del
6030 * \see dt_object_operations::do_ref_del() in the API description for details.
6032 static int lod_ref_del(const struct lu_env *env,
6033 struct dt_object *dt, struct thandle *th)
6035 return lod_sub_ref_del(env, dt_object_child(dt), th);
6039 * Implementation of dt_object_operations::do_object_sync.
6041 * \see dt_object_operations::do_object_sync() in the API description
6044 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
6045 __u64 start, __u64 end)
6047 return dt_object_sync(env, dt_object_child(dt), start, end);
6051 * Implementation of dt_object_operations::do_object_unlock.
6053 * Used to release LDLM lock(s).
6055 * \see dt_object_operations::do_object_unlock() in the API description
6058 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
6059 struct ldlm_enqueue_info *einfo,
6060 union ldlm_policy_data *policy)
6062 struct lod_object *lo = lod_dt_obj(dt);
6063 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
6064 int slave_locks_size;
6068 if (slave_locks == NULL)
6071 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
6072 /* Note: for remote lock for single stripe dir, MDT will cancel
6073 * the lock by lockh directly */
6074 LASSERT(!dt_object_remote(dt_object_child(dt)));
6076 /* locks were unlocked in MDT layer */
6077 for (i = 0; i < slave_locks->ha_count; i++)
6078 LASSERT(!lustre_handle_is_used(&slave_locks->ha_handles[i]));
6081 * NB, ha_count may not equal to ldo_dir_stripe_count, because dir
6082 * layout may change, e.g., shrink dir layout after migration.
6084 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6085 if (lo->ldo_stripe[i])
6086 dt_invalidate(env, lo->ldo_stripe[i]);
6089 slave_locks_size = offsetof(typeof(*slave_locks),
6090 ha_handles[slave_locks->ha_count]);
6091 OBD_FREE(slave_locks, slave_locks_size);
6092 einfo->ei_cbdata = NULL;
6098 * Implementation of dt_object_operations::do_object_lock.
6100 * Used to get LDLM lock on the non-striped and striped objects.
6102 * \see dt_object_operations::do_object_lock() in the API description
6105 static int lod_object_lock(const struct lu_env *env,
6106 struct dt_object *dt,
6107 struct lustre_handle *lh,
6108 struct ldlm_enqueue_info *einfo,
6109 union ldlm_policy_data *policy)
6111 struct lod_object *lo = lod_dt_obj(dt);
6112 int slave_locks_size;
6113 struct lustre_handle_array *slave_locks = NULL;
6118 /* remote object lock */
6119 if (!einfo->ei_enq_slave) {
6120 LASSERT(dt_object_remote(dt));
6121 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
6125 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
6128 rc = lod_striping_load(env, lo);
6133 if (lo->ldo_dir_stripe_count <= 1)
6136 slave_locks_size = offsetof(typeof(*slave_locks),
6137 ha_handles[lo->ldo_dir_stripe_count]);
6138 /* Freed in lod_object_unlock */
6139 OBD_ALLOC(slave_locks, slave_locks_size);
6142 slave_locks->ha_count = lo->ldo_dir_stripe_count;
6144 /* striped directory lock */
6145 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6146 struct lustre_handle lockh;
6147 struct ldlm_res_id *res_id;
6148 struct dt_object *stripe;
6150 stripe = lo->ldo_stripe[i];
6154 res_id = &lod_env_info(env)->lti_res_id;
6155 fid_build_reg_res_name(lu_object_fid(&stripe->do_lu), res_id);
6156 einfo->ei_res_id = res_id;
6158 if (dt_object_remote(stripe)) {
6159 set_bit(i, (void *)slave_locks->ha_map);
6160 rc = dt_object_lock(env, stripe, &lockh, einfo, policy);
6162 struct ldlm_namespace *ns = einfo->ei_namespace;
6163 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
6164 ldlm_completion_callback completion = einfo->ei_cb_cp;
6165 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
6167 if (einfo->ei_mode == LCK_PW ||
6168 einfo->ei_mode == LCK_EX)
6169 dlmflags |= LDLM_FL_COS_INCOMPAT;
6171 LASSERT(ns != NULL);
6172 rc = ldlm_cli_enqueue_local(env, ns, res_id, LDLM_IBITS,
6173 policy, einfo->ei_mode,
6174 &dlmflags, blocking,
6176 NULL, 0, LVB_T_NONE,
6181 ldlm_lock_decref_and_cancel(
6182 &slave_locks->ha_handles[i],
6184 OBD_FREE(slave_locks, slave_locks_size);
6187 slave_locks->ha_handles[i] = lockh;
6189 einfo->ei_cbdata = slave_locks;
6195 * Implementation of dt_object_operations::do_invalidate.
6197 * \see dt_object_operations::do_invalidate() in the API description for details
6199 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
6201 return dt_invalidate(env, dt_object_child(dt));
6204 static int lod_declare_instantiate_components(const struct lu_env *env,
6205 struct lod_object *lo, struct thandle *th)
6207 struct lod_thread_info *info = lod_env_info(env);
6212 LASSERT(info->lti_count < lo->ldo_comp_cnt);
6214 for (i = 0; i < info->lti_count; i++) {
6215 rc = lod_qos_prep_create(env, lo, NULL, th,
6216 info->lti_comp_idx[i]);
6222 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6223 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
6224 &info->lti_buf, XATTR_NAME_LOV, 0, th);
6231 * Check OSTs for an existing component for further extension
6233 * Checks if OSTs are still healthy and not out of space. Gets free space
6234 * on OSTs (relative to allocation watermark rmb_low) and compares to
6235 * the proposed new_end for this component.
6237 * Decides whether or not to extend a component on its current OSTs.
6239 * \param[in] env execution environment for this thread
6240 * \param[in] lo object we're checking
6241 * \param[in] index index of this component
6242 * \param[in] extension_size extension size for this component
6243 * \param[in] extent layout extent for requested operation
6244 * \param[in] comp_extent extension component extent
6245 * \param[in] write if this is write operation
6247 * \retval true - OK to extend on current OSTs
6248 * \retval false - do not extend on current OSTs
6250 static bool lod_sel_osts_allowed(const struct lu_env *env,
6251 struct lod_object *lo,
6252 int index, __u64 extension_size,
6253 struct lu_extent *extent,
6254 struct lu_extent *comp_extent, int write)
6256 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[index];
6257 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6258 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
6259 __u64 available = 0;
6266 LASSERT(lod_comp->llc_stripe_count != 0);
6269 (extent->e_start == 0 && extent->e_end == OBD_OBJECT_EOF)) {
6270 /* truncate or append */
6271 size = extension_size;
6273 /* In case of write op, check the real write extent,
6274 * it may be larger than the extension_size */
6275 size = roundup(min(extent->e_end, comp_extent->e_end) -
6276 max(extent->e_start, comp_extent->e_start),
6279 /* extension_size is file level, so we must divide by stripe count to
6280 * compare it to available space on a single OST */
6281 size /= lod_comp->llc_stripe_count;
6283 lod_getref(&lod->lod_ost_descs);
6284 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
6285 int index = lod_comp->llc_ost_indices[i];
6286 struct lod_tgt_desc *ost = OST_TGT(lod, index);
6287 struct obd_statfs_info info = { 0 };
6288 int j, repeated = 0;
6292 /* Get the number of times this OST repeats in this component.
6293 * Note: inter-component repeats are not counted as this is
6294 * considered as a rare case: we try to not repeat OST in other
6295 * components if possible. */
6296 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6297 if (index != lod_comp->llc_ost_indices[j])
6300 /* already handled */
6306 if (j < lod_comp->llc_stripe_count)
6309 if (!cfs_bitmap_check(lod->lod_ost_bitmap, index)) {
6310 CDEBUG(D_LAYOUT, "ost %d no longer present\n", index);
6315 rc = dt_statfs_info(env, ost->ltd_tgt, sfs, &info);
6317 CDEBUG(D_LAYOUT, "statfs failed for ost %d, error %d\n",
6323 if (sfs->os_state & OS_STATE_ENOSPC ||
6324 sfs->os_state & OS_STATE_READONLY ||
6325 sfs->os_state & OS_STATE_DEGRADED) {
6326 CDEBUG(D_LAYOUT, "ost %d is not availble for SEL "
6327 "extension, state %u\n", index, sfs->os_state);
6333 available = sfs->os_bavail * sfs->os_bsize;
6334 /* 'available' is relative to the allocation threshold */
6335 available -= (__u64) info.os_reserved_mb_low << 20;
6337 CDEBUG(D_LAYOUT, "ost %d lowwm: %d highwm: %d, "
6338 "%llu %% blocks available, %llu %% blocks free\n",
6339 index, info.os_reserved_mb_low, info.os_reserved_mb_high,
6340 (100ull * sfs->os_bavail) / sfs->os_blocks,
6341 (100ull * sfs->os_bfree) / sfs->os_blocks);
6343 if (size * repeated > available) {
6345 CDEBUG(D_LAYOUT, "low space on ost %d, available %llu "
6346 "< extension size %llu\n", index, available,
6351 lod_putref(lod, &lod->lod_ost_descs);
6357 * Adjust extents after component removal
6359 * When we remove an extension component, we move the start of the next
6360 * component to match the start of the extension component, so no space is left
6363 * \param[in] env execution environment for this thread
6364 * \param[in] lo object
6365 * \param[in] max_comp layout component
6366 * \param[in] index index of this component
6368 * \retval 0 on success
6369 * \retval negative errno on error
6371 static void lod_sel_adjust_extents(const struct lu_env *env,
6372 struct lod_object *lo,
6373 int max_comp, int index)
6375 struct lod_layout_component *lod_comp = NULL;
6376 struct lod_layout_component *next = NULL;
6377 struct lod_layout_component *prev = NULL;
6378 __u64 new_start = 0;
6382 /* Extension space component */
6383 lod_comp = &lo->ldo_comp_entries[index];
6384 next = &lo->ldo_comp_entries[index + 1];
6385 prev = &lo->ldo_comp_entries[index - 1];
6387 LASSERT(lod_comp != NULL && prev != NULL && next != NULL);
6388 LASSERT(lod_comp->llc_flags & LCME_FL_EXTENSION);
6390 /* Previous is being removed */
6391 if (prev && prev->llc_id == LCME_ID_INVAL)
6392 new_start = prev->llc_extent.e_start;
6394 new_start = lod_comp->llc_extent.e_start;
6396 for (i = index + 1; i < max_comp; i++) {
6397 lod_comp = &lo->ldo_comp_entries[i];
6399 start = lod_comp->llc_extent.e_start;
6400 lod_comp->llc_extent.e_start = new_start;
6402 /* We only move zero length extendable components */
6403 if (!(start == lod_comp->llc_extent.e_end))
6406 LASSERT(!(lod_comp->llc_flags & LCME_FL_INIT));
6408 lod_comp->llc_extent.e_end = new_start;
6412 /* Calculate the proposed 'new end' for a component we're extending */
6413 static __u64 lod_extension_new_end(__u64 extension_size, __u64 extent_end,
6414 __u32 stripe_size, __u64 component_end,
6415 __u64 extension_end)
6419 LASSERT(extension_size != 0 && stripe_size != 0);
6421 /* Round up to extension size */
6422 if (extent_end == OBD_OBJECT_EOF) {
6423 new_end = OBD_OBJECT_EOF;
6425 /* Add at least extension_size to the previous component_end,
6426 * covering the req layout extent */
6427 new_end = max(extent_end - component_end, extension_size);
6428 new_end = roundup(new_end, extension_size);
6429 new_end += component_end;
6431 /* Component end must be min stripe size aligned */
6432 if (new_end % stripe_size) {
6433 CDEBUG(D_LAYOUT, "new component end is not aligned "
6434 "by the stripe size %u: [%llu, %llu) ext size "
6435 "%llu new end %llu, aligning\n",
6436 stripe_size, component_end, extent_end,
6437 extension_size, new_end);
6438 new_end = roundup(new_end, stripe_size);
6442 if (new_end < extent_end)
6443 new_end = OBD_OBJECT_EOF;
6446 /* Don't extend past the end of the extension component */
6447 if (new_end > extension_end)
6448 new_end = extension_end;
6453 /* As lod_sel_handler() could be re-entered for the same component several
6454 * times, this is the data for the next call. Fields could be changed to
6455 * component indexes when needed, (e.g. if there is no need to instantiate
6456 * all the previous components up to the current position) to tell the caller
6457 * where to start over from. */
6464 * Process extent updates for a particular layout component
6466 * Handle layout updates for a particular extension space component touched by
6467 * a layout update operation. Core function of self-extending PFL feature.
6469 * In general, this function processes exactly *one* stage of an extension
6470 * operation, modifying the layout accordingly, then returns to the caller.
6471 * The caller is responsible for restarting processing with the new layout,
6472 * which may repeatedly return to this function until the extension updates
6475 * This function does one of a few things to the layout:
6476 * 1. Extends the component before the current extension space component to
6477 * allow it to accomodate the requested operation (if space/policy permit that
6478 * component to continue on its current OSTs)
6480 * 2. If extension of the existing component fails, we do one of two things:
6481 * a. If there is a component after the extension space, we remove the
6482 * extension space component, move the start of the next component down
6483 * accordingly, then notify the caller to restart processing w/the new
6485 * b. If there is no following component, we try repeating the current
6486 * component, creating a new component using the current one as a
6487 * template (keeping its stripe properties but not specific striping),
6488 * and try assigning striping for this component. If there is sufficient
6489 * free space on the OSTs chosen for this component, it is instantiated
6490 * and i/o continues there.
6492 * If there is not sufficient space on the new OSTs, we remove this new
6493 * component & extend the current component.
6495 * Note further that uninited components followed by extension space can be zero
6496 * length meaning that we will try to extend them before initializing them, and
6497 * if that fails, they will be removed without initialization.
6499 * 3. If we extend to/beyond the end of an extension space component, that
6500 * component is exhausted (all of its range has been given to real components),
6501 * so we remove it and restart processing.
6503 * \param[in] env execution environment for this thread
6504 * \param[in,out] lo object to update the layout of
6505 * \param[in] extent layout extent for requested operation, update
6506 * layout to fit this operation
6507 * \param[in] th transaction handle for this operation
6508 * \param[in,out] max_comp the highest comp for the portion of the layout
6509 * we are operating on (For FLR, the chosen
6510 * replica). Updated because we may remove
6512 * \param[in] index index of the extension space component we're
6514 * \param[in] write if this is write op
6515 * \param[in,out] force if the extension is to be forced; set here
6516 to force it on the 2nd call for the same
6519 * \retval 0 on success
6520 * \retval negative errno on error
6522 static int lod_sel_handler(const struct lu_env *env,
6523 struct lod_object *lo,
6524 struct lu_extent *extent,
6525 struct thandle *th, int *max_comp,
6526 int index, int write,
6527 struct sel_data *sd)
6529 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6530 struct lod_thread_info *info = lod_env_info(env);
6531 struct lod_layout_component *lod_comp;
6532 struct lod_layout_component *prev;
6533 struct lod_layout_component *next = NULL;
6534 __u64 extension_size;
6541 /* First component cannot be extension space */
6543 CERROR("%s: "DFID" first component cannot be extension space\n",
6544 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6548 lod_comp = &lo->ldo_comp_entries[index];
6549 prev = &lo->ldo_comp_entries[index - 1];
6550 if ((index + 1) < *max_comp)
6551 next = &lo->ldo_comp_entries[index + 1];
6553 /* extension size uses the stripe size field as KiB */
6554 extension_size = lod_comp->llc_stripe_size * SEL_UNIT_SIZE;
6556 CDEBUG(D_LAYOUT, "prev start %llu, extension start %llu, extension end"
6557 " %llu, extension size %llu\n", prev->llc_extent.e_start,
6558 lod_comp->llc_extent.e_start, lod_comp->llc_extent.e_end,
6561 /* Two extension space components cannot be adjacent & extension space
6562 * components cannot be init */
6563 if ((prev->llc_flags & LCME_FL_EXTENSION) ||
6564 !(ergo(next, !(next->llc_flags & LCME_FL_EXTENSION))) ||
6565 lod_comp_inited(lod_comp)) {
6566 CERROR("%s: "DFID" invalid extension space components\n",
6567 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6571 if (!prev->llc_stripe) {
6572 CDEBUG(D_LAYOUT, "Previous component not inited\n");
6573 info->lti_count = 1;
6574 info->lti_comp_idx[0] = index - 1;
6575 rc = lod_declare_instantiate_components(env, lo, th);
6576 /* ENOSPC tells us we can't use this component. If there is
6577 * a next or we are repeating, we either spill over (next) or
6578 * extend the original comp (repeat). Otherwise, return the
6579 * error to the user. */
6580 if (rc == -ENOSPC && (next || sd->sd_repeat))
6586 if (sd->sd_force == 0 && rc == 0)
6587 rc = !lod_sel_osts_allowed(env, lo, index - 1,
6588 extension_size, extent,
6589 &lod_comp->llc_extent, write);
6591 repeated = !!(sd->sd_repeat);
6595 /* Extend previous component */
6597 new_end = lod_extension_new_end(extension_size, extent->e_end,
6598 prev->llc_stripe_size,
6599 prev->llc_extent.e_end,
6600 lod_comp->llc_extent.e_end);
6602 CDEBUG(D_LAYOUT, "new end %llu\n", new_end);
6603 lod_comp->llc_extent.e_start = new_end;
6604 prev->llc_extent.e_end = new_end;
6606 if (prev->llc_extent.e_end == lod_comp->llc_extent.e_end) {
6607 CDEBUG(D_LAYOUT, "Extension component exhausted\n");
6608 lod_comp->llc_id = LCME_ID_INVAL;
6612 /* rc == 1, failed to extend current component */
6615 /* Normal 'spillover' case - Remove the extension
6616 * space component & bring down the start of the next
6618 lod_comp->llc_id = LCME_ID_INVAL;
6620 if (!(prev->llc_flags & LCME_FL_INIT)) {
6621 prev->llc_id = LCME_ID_INVAL;
6624 lod_sel_adjust_extents(env, lo, *max_comp, index);
6625 } else if (lod_comp_inited(prev)) {
6626 /* If there is no next, and the previous component is
6627 * INIT'ed, try repeating the previous component. */
6628 LASSERT(repeated == 0);
6629 rc = lod_layout_repeat_comp(env, lo, index - 1);
6633 /* The previous component is a repeated component.
6634 * Record this so we don't keep trying to repeat it. */
6637 /* If the previous component is not INIT'ed, this may
6638 * be a component we have just instantiated but failed
6639 * to extend. Or even a repeated component we failed
6640 * to prepare a striping for. Do not repeat but instead
6641 * remove the repeated component & force the extention
6642 * of the original one */
6645 prev->llc_id = LCME_ID_INVAL;
6652 rc = lod_layout_del_prep_layout(env, lo, NULL);
6655 LASSERTF(-rc == change,
6656 "number deleted %d != requested %d\n", -rc,
6659 *max_comp = *max_comp + change;
6661 /* lod_del_prep_layout reallocates ldo_comp_entries, so we must
6662 * refresh these pointers before using them */
6663 lod_comp = &lo->ldo_comp_entries[index];
6664 prev = &lo->ldo_comp_entries[index - 1];
6665 CDEBUG(D_LAYOUT, "After extent updates: prev start %llu, current start "
6666 "%llu, current end %llu max_comp %d ldo_comp_cnt %d\n",
6667 prev->llc_extent.e_start, lod_comp->llc_extent.e_start,
6668 lod_comp->llc_extent.e_end, *max_comp, lo->ldo_comp_cnt);
6670 /* Layout changed successfully */
6675 * Declare layout extent updates
6677 * Handles extensions. Identifies extension components touched by current
6678 * operation and passes them to processing function.
6680 * Restarts with updated layouts from the processing function until the current
6681 * operation no longer touches an extension space component.
6683 * \param[in] env execution environment for this thread
6684 * \param[in,out] lo object to update the layout of
6685 * \param[in] extent layout extent for requested operation, update layout to
6686 * fit this operation
6687 * \param[in] th transaction handle for this operation
6688 * \param[in] pick identifies chosen mirror for FLR layouts
6689 * \param[in] write if this is write op
6691 * \retval 1 on layout changed, 0 on no change
6692 * \retval negative errno on error
6694 static int lod_declare_update_extents(const struct lu_env *env,
6695 struct lod_object *lo, struct lu_extent *extent,
6696 struct thandle *th, int pick, int write)
6698 struct lod_thread_info *info = lod_env_info(env);
6699 struct lod_layout_component *lod_comp;
6700 bool layout_changed = false;
6701 struct sel_data sd = { 0 };
6709 /* This makes us work on the components of the chosen mirror */
6710 start_index = lo->ldo_mirrors[pick].lme_start;
6711 max_comp = lo->ldo_mirrors[pick].lme_end + 1;
6712 if (lo->ldo_flr_state == LCM_FL_NONE)
6713 LASSERT(start_index == 0 && max_comp == lo->ldo_comp_cnt);
6715 CDEBUG(D_LAYOUT, "extent->e_start %llu, extent->e_end %llu\n",
6716 extent->e_start, extent->e_end);
6717 for (i = start_index; i < max_comp; i++) {
6718 lod_comp = &lo->ldo_comp_entries[i];
6720 /* We've passed all components of interest */
6721 if (lod_comp->llc_extent.e_start >= extent->e_end)
6724 if (lod_comp->llc_flags & LCME_FL_EXTENSION) {
6725 layout_changed = true;
6726 rc = lod_sel_handler(env, lo, extent, th, &max_comp,
6731 /* Nothing has changed behind the prev one */
6737 /* We may have added or removed components. If so, we must update the
6738 * start & ends of all the mirrors after the current one, and the end
6739 * of the current mirror. */
6740 change = max_comp - 1 - lo->ldo_mirrors[pick].lme_end;
6742 lo->ldo_mirrors[pick].lme_end += change;
6743 for (i = pick + 1; i < lo->ldo_mirror_count; i++) {
6744 lo->ldo_mirrors[i].lme_start += change;
6745 lo->ldo_mirrors[i].lme_end += change;
6751 /* The amount of components has changed, adjust the lti_comp_idx */
6752 rc2 = lod_layout_data_init(info, lo->ldo_comp_cnt);
6754 return rc < 0 ? rc : rc2 < 0 ? rc2 : layout_changed;
6757 /* If striping is already instantiated or INIT'ed DOM? */
6758 static bool lod_is_instantiation_needed(struct lod_layout_component *comp)
6760 return !(((lov_pattern(comp->llc_pattern) == LOV_PATTERN_MDT) &&
6761 lod_comp_inited(comp)) || comp->llc_stripe);
6765 * Declare layout update for a non-FLR layout.
6767 * \param[in] env execution environment for this thread
6768 * \param[in,out] lo object to update the layout of
6769 * \param[in] layout layout intent for requested operation, "update" is
6770 * a process of reacting to this
6771 * \param[in] buf buffer containing lov ea (see comment on usage inline)
6772 * \param[in] th transaction handle for this operation
6774 * \retval 0 on success
6775 * \retval negative errno on error
6777 static int lod_declare_update_plain(const struct lu_env *env,
6778 struct lod_object *lo, struct layout_intent *layout,
6779 const struct lu_buf *buf, struct thandle *th)
6781 struct lod_thread_info *info = lod_env_info(env);
6782 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6783 struct lod_layout_component *lod_comp;
6784 struct lov_comp_md_v1 *comp_v1 = NULL;
6785 bool layout_changed = false;
6786 bool replay = false;
6790 LASSERT(lo->ldo_flr_state == LCM_FL_NONE);
6793 * In case the client is passing lovea, which only happens during
6794 * the replay of layout intent write RPC for now, we may need to
6795 * parse the lovea and apply new layout configuration.
6797 if (buf && buf->lb_len) {
6798 struct lov_user_md_v1 *v1 = buf->lb_buf;
6800 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
6801 v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
6802 LOV_MAGIC_COMP_V1)) {
6803 CERROR("%s: the replay buffer of layout extend "
6804 "(magic %#x) does not contain expected "
6805 "composite layout.\n",
6806 lod2obd(d)->obd_name, v1->lmm_magic);
6807 GOTO(out, rc = -EINVAL);
6810 rc = lod_use_defined_striping(env, lo, buf);
6813 lo->ldo_comp_cached = 1;
6815 rc = lod_get_lov_ea(env, lo);
6818 /* old on-disk EA is stored in info->lti_buf */
6819 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
6821 layout_changed = true;
6823 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
6827 /* non replay path */
6828 rc = lod_striping_load(env, lo);
6833 /* Make sure defined layout covers the requested write range. */
6834 lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
6835 if (lo->ldo_comp_cnt > 1 &&
6836 lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
6837 lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
6838 CDEBUG_LIMIT(replay ? D_ERROR : D_LAYOUT,
6839 "%s: the defined layout [0, %#llx) does not "
6840 "covers the write range "DEXT"\n",
6841 lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
6842 PEXT(&layout->li_extent));
6843 GOTO(out, rc = -EINVAL);
6846 CDEBUG(D_LAYOUT, "%s: "DFID": update components "DEXT"\n",
6847 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
6848 PEXT(&layout->li_extent));
6851 rc = lod_declare_update_extents(env, lo, &layout->li_extent,
6852 th, 0, layout->li_opc == LAYOUT_INTENT_WRITE);
6856 layout_changed = true;
6860 * Iterate ld->ldo_comp_entries, find the component whose extent under
6861 * the write range and not instantianted.
6863 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6864 lod_comp = &lo->ldo_comp_entries[i];
6866 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
6870 /* If striping is instantiated or INIT'ed DOM skip */
6871 if (!lod_is_instantiation_needed(lod_comp))
6875 * In replay path, lod_comp is the EA passed by
6876 * client replay buffer, comp_v1 is the pre-recovery
6877 * on-disk EA, we'd sift out those components which
6878 * were init-ed in the on-disk EA.
6880 if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
6885 * this component hasn't instantiated in normal path, or during
6886 * replay it needs replay the instantiation.
6889 /* A released component is being extended */
6890 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
6891 GOTO(out, rc = -EINVAL);
6893 LASSERT(info->lti_comp_idx != NULL);
6894 info->lti_comp_idx[info->lti_count++] = i;
6895 layout_changed = true;
6898 if (!layout_changed)
6901 lod_obj_inc_layout_gen(lo);
6902 rc = lod_declare_instantiate_components(env, lo, th);
6906 lod_striping_free(env, lo);
6910 static inline int lod_comp_index(struct lod_object *lo,
6911 struct lod_layout_component *lod_comp)
6913 LASSERT(lod_comp >= lo->ldo_comp_entries &&
6914 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
6916 return lod_comp - lo->ldo_comp_entries;
6920 * Stale other mirrors by writing extent.
6922 static int lod_stale_components(const struct lu_env *env, struct lod_object *lo,
6923 int primary, struct lu_extent *extent,
6926 struct lod_layout_component *pri_comp, *lod_comp;
6927 struct lu_extent pri_extent;
6932 /* The writing extent decides which components in the primary
6933 * are affected... */
6934 CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
6937 lod_foreach_mirror_comp(pri_comp, lo, primary) {
6938 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
6941 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
6942 lod_comp_index(lo, pri_comp),
6943 PEXT(&pri_comp->llc_extent));
6945 pri_extent.e_start = pri_comp->llc_extent.e_start;
6946 pri_extent.e_end = pri_comp->llc_extent.e_end;
6948 for (i = 0; i < lo->ldo_mirror_count; i++) {
6951 rc = lod_declare_update_extents(env, lo, &pri_extent,
6953 /* if update_extents changed the layout, it may have
6954 * reallocated the component array, so start over to
6955 * avoid using stale pointers */
6961 /* ... and then stale other components that are
6962 * overlapping with primary components */
6963 lod_foreach_mirror_comp(lod_comp, lo, i) {
6964 if (!lu_extent_is_overlapped(
6966 &lod_comp->llc_extent))
6969 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
6970 i, lod_comp_index(lo, lod_comp));
6972 lod_comp->llc_flags |= LCME_FL_STALE;
6973 lo->ldo_mirrors[i].lme_stale = 1;
6982 * check an OST's availability
6983 * \param[in] env execution environment
6984 * \param[in] lo lod object
6985 * \param[in] dt dt object
6986 * \param[in] index mirror index
6988 * \retval negative if failed
6989 * \retval 1 if \a dt is available
6990 * \retval 0 if \a dt is not available
6992 static inline int lod_check_ost_avail(const struct lu_env *env,
6993 struct lod_object *lo,
6994 struct dt_object *dt, int index)
6996 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6997 struct lod_tgt_desc *ost;
6999 int type = LU_SEQ_RANGE_OST;
7002 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &idx, &type);
7004 CERROR("%s: can't locate "DFID":rc = %d\n",
7005 lod2obd(lod)->obd_name, PFID(lu_object_fid(&dt->do_lu)),
7010 ost = OST_TGT(lod, idx);
7011 if (ost->ltd_statfs.os_state &
7012 (OS_STATE_READONLY | OS_STATE_ENOSPC | OS_STATE_ENOINO |
7013 OS_STATE_NOPRECREATE) ||
7014 ost->ltd_active == 0) {
7015 CDEBUG(D_LAYOUT, DFID ": mirror %d OST%d unavail, rc = %d\n",
7016 PFID(lod_object_fid(lo)), index, idx, rc);
7024 * Pick primary mirror for write
7025 * \param[in] env execution environment
7026 * \param[in] lo object
7027 * \param[in] extent write range
7029 static int lod_primary_pick(const struct lu_env *env, struct lod_object *lo,
7030 struct lu_extent *extent)
7032 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7033 unsigned int seq = 0;
7034 struct lod_layout_component *lod_comp;
7036 int picked = -1, second_pick = -1, third_pick = -1;
7039 if (OBD_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
7040 get_random_bytes(&seq, sizeof(seq));
7041 seq %= lo->ldo_mirror_count;
7045 * Pick a mirror as the primary, and check the availability of OSTs.
7047 * This algo can be revised later after knowing the topology of
7050 lod_qos_statfs_update(env, lod, &lod->lod_ost_descs);
7051 for (i = 0; i < lo->ldo_mirror_count; i++) {
7052 bool ost_avail = true;
7053 int index = (i + seq) % lo->ldo_mirror_count;
7055 if (lo->ldo_mirrors[index].lme_stale) {
7056 CDEBUG(D_LAYOUT, DFID": mirror %d stale\n",
7057 PFID(lod_object_fid(lo)), index);
7061 /* 2nd pick is for the primary mirror containing unavail OST */
7062 if (lo->ldo_mirrors[index].lme_primary && second_pick < 0)
7063 second_pick = index;
7065 /* 3rd pick is for non-primary mirror containing unavail OST */
7066 if (second_pick < 0 && third_pick < 0)
7070 * we found a non-primary 1st pick, we'd like to find a
7071 * potential pirmary mirror.
7073 if (picked >= 0 && !lo->ldo_mirrors[index].lme_primary)
7076 /* check the availability of OSTs */
7077 lod_foreach_mirror_comp(lod_comp, lo, index) {
7078 if (!lod_comp_inited(lod_comp) || !lod_comp->llc_stripe)
7081 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
7082 struct dt_object *dt = lod_comp->llc_stripe[j];
7084 rc = lod_check_ost_avail(env, lo, dt, index);
7091 } /* for all dt object in one component */
7094 } /* for all components in a mirror */
7097 * the OSTs where allocated objects locates in the components
7098 * of the mirror are available.
7103 /* this mirror has all OSTs available */
7107 * primary with all OSTs are available, this is the perfect
7110 if (lo->ldo_mirrors[index].lme_primary)
7112 } /* for all mirrors */
7114 /* failed to pick a sound mirror, lower our expectation */
7116 picked = second_pick;
7118 picked = third_pick;
7125 static int lod_prepare_resync_mirror(const struct lu_env *env,
7126 struct lod_object *lo,
7129 struct lod_thread_info *info = lod_env_info(env);
7130 struct lod_layout_component *lod_comp;
7131 bool neg = !!(MIRROR_ID_NEG & mirror_id);
7134 mirror_id &= ~MIRROR_ID_NEG;
7136 for (i = 0; i < lo->ldo_mirror_count; i++) {
7137 if ((!neg && lo->ldo_mirrors[i].lme_id != mirror_id) ||
7138 (neg && lo->ldo_mirrors[i].lme_id == mirror_id))
7141 lod_foreach_mirror_comp(lod_comp, lo, i) {
7142 if (lod_comp_inited(lod_comp))
7145 info->lti_comp_idx[info->lti_count++] =
7146 lod_comp_index(lo, lod_comp);
7154 * figure out the components should be instantiated for resync.
7156 static int lod_prepare_resync(const struct lu_env *env, struct lod_object *lo,
7157 struct lu_extent *extent)
7159 struct lod_thread_info *info = lod_env_info(env);
7160 struct lod_layout_component *lod_comp;
7161 unsigned int need_sync = 0;
7165 DFID": instantiate all stale components in "DEXT"\n",
7166 PFID(lod_object_fid(lo)), PEXT(extent));
7169 * instantiate all components within this extent, even non-stale
7172 for (i = 0; i < lo->ldo_mirror_count; i++) {
7173 if (!lo->ldo_mirrors[i].lme_stale)
7176 lod_foreach_mirror_comp(lod_comp, lo, i) {
7177 if (!lu_extent_is_overlapped(extent,
7178 &lod_comp->llc_extent))
7183 if (lod_comp_inited(lod_comp))
7186 CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
7187 i, lod_comp_index(lo, lod_comp));
7188 info->lti_comp_idx[info->lti_count++] =
7189 lod_comp_index(lo, lod_comp);
7193 return need_sync ? 0 : -EALREADY;
7196 static int lod_declare_update_rdonly(const struct lu_env *env,
7197 struct lod_object *lo, struct md_layout_change *mlc,
7200 struct lod_thread_info *info = lod_env_info(env);
7201 struct lu_attr *layout_attr = &info->lti_layout_attr;
7202 struct lod_layout_component *lod_comp;
7203 struct lu_extent extent = { 0 };
7207 LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
7208 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7209 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7210 LASSERT(lo->ldo_mirror_count > 0);
7212 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7213 struct layout_intent *layout = mlc->mlc_intent;
7214 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7217 extent = layout->li_extent;
7218 CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
7219 PFID(lod_object_fid(lo)), PEXT(&extent));
7221 picked = lod_primary_pick(env, lo, &extent);
7225 CDEBUG(D_LAYOUT, DFID": picked mirror id %u as primary\n",
7226 PFID(lod_object_fid(lo)),
7227 lo->ldo_mirrors[picked].lme_id);
7229 /* Update extents of primary before staling */
7230 rc = lod_declare_update_extents(env, lo, &extent, th, picked,
7235 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
7237 * trunc transfers [0, size) in the intent extent, we'd
7238 * stale components overlapping [size, eof).
7240 extent.e_start = extent.e_end;
7241 extent.e_end = OBD_OBJECT_EOF;
7244 /* stale overlapping components from other mirrors */
7245 rc = lod_stale_components(env, lo, picked, &extent, th);
7249 /* restore truncate intent extent */
7250 if (layout->li_opc == LAYOUT_INTENT_TRUNC)
7251 extent.e_end = extent.e_start;
7253 /* instantiate components for the picked mirror, start from 0 */
7256 lod_foreach_mirror_comp(lod_comp, lo, picked) {
7257 if (!lu_extent_is_overlapped(&extent,
7258 &lod_comp->llc_extent))
7261 if (!lod_is_instantiation_needed(lod_comp))
7264 info->lti_comp_idx[info->lti_count++] =
7265 lod_comp_index(lo, lod_comp);
7268 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7269 } else { /* MD_LAYOUT_RESYNC */
7273 * could contain multiple non-stale mirrors, so we need to
7274 * prep uninited all components assuming any non-stale mirror
7275 * could be picked as the primary mirror.
7277 if (mlc->mlc_mirror_id == 0) {
7279 for (i = 0; i < lo->ldo_mirror_count; i++) {
7280 if (lo->ldo_mirrors[i].lme_stale)
7283 lod_foreach_mirror_comp(lod_comp, lo, i) {
7284 if (!lod_comp_inited(lod_comp))
7288 lod_comp->llc_extent.e_end)
7290 lod_comp->llc_extent.e_end;
7293 rc = lod_prepare_resync(env, lo, &extent);
7297 /* mirror write, try to init its all components */
7298 rc = lod_prepare_resync_mirror(env, lo,
7299 mlc->mlc_mirror_id);
7304 /* change the file state to SYNC_PENDING */
7305 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7308 /* Reset the layout version once it's becoming too large.
7309 * This way it can make sure that the layout version is
7310 * monotonously increased in this writing era. */
7311 lod_obj_inc_layout_gen(lo);
7312 if (lo->ldo_layout_gen > (LCME_ID_MAX >> 1)) {
7313 __u32 layout_version;
7315 get_random_bytes(&layout_version, sizeof(layout_version));
7316 lo->ldo_layout_gen = layout_version & 0xffff;
7319 rc = lod_declare_instantiate_components(env, lo, th);
7323 layout_attr->la_valid = LA_LAYOUT_VERSION;
7324 layout_attr->la_layout_version = 0; /* set current version */
7325 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7326 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7327 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7333 lod_striping_free(env, lo);
7337 static int lod_declare_update_write_pending(const struct lu_env *env,
7338 struct lod_object *lo, struct md_layout_change *mlc,
7341 struct lod_thread_info *info = lod_env_info(env);
7342 struct lu_attr *layout_attr = &info->lti_layout_attr;
7343 struct lod_layout_component *lod_comp;
7344 struct lu_extent extent = { 0 };
7350 LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
7351 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7352 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7354 /* look for the primary mirror */
7355 for (i = 0; i < lo->ldo_mirror_count; i++) {
7356 if (lo->ldo_mirrors[i].lme_stale)
7359 LASSERTF(primary < 0, DFID " has multiple primary: %u / %u",
7360 PFID(lod_object_fid(lo)),
7361 lo->ldo_mirrors[i].lme_id,
7362 lo->ldo_mirrors[primary].lme_id);
7367 CERROR(DFID ": doesn't have a primary mirror\n",
7368 PFID(lod_object_fid(lo)));
7369 GOTO(out, rc = -ENODATA);
7372 CDEBUG(D_LAYOUT, DFID": found primary %u\n",
7373 PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
7375 LASSERT(!lo->ldo_mirrors[primary].lme_stale);
7377 /* for LAYOUT_WRITE opc, it has to do the following operations:
7378 * 1. stale overlapping componets from stale mirrors;
7379 * 2. instantiate components of the primary mirror;
7380 * 3. transfter layout version to all objects of the primary;
7382 * for LAYOUT_RESYNC opc, it will do:
7383 * 1. instantiate components of all stale mirrors;
7384 * 2. transfer layout version to all objects to close write era. */
7386 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7387 struct layout_intent *layout = mlc->mlc_intent;
7388 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7390 LASSERT(mlc->mlc_intent != NULL);
7392 extent = mlc->mlc_intent->li_extent;
7394 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
7395 PFID(lod_object_fid(lo)), PEXT(&extent));
7397 /* 1. Update extents of primary before staling */
7398 rc = lod_declare_update_extents(env, lo, &extent, th, primary,
7403 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
7405 * trunc transfers [0, size) in the intent extent, we'd
7406 * stale components overlapping [size, eof).
7408 extent.e_start = extent.e_end;
7409 extent.e_end = OBD_OBJECT_EOF;
7412 /* 2. stale overlapping components */
7413 rc = lod_stale_components(env, lo, primary, &extent, th);
7417 /* 3. find the components which need instantiating.
7418 * instantiate [0, mlc->mlc_intent->e_end) */
7420 /* restore truncate intent extent */
7421 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC)
7422 extent.e_end = extent.e_start;
7425 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7426 if (!lu_extent_is_overlapped(&extent,
7427 &lod_comp->llc_extent))
7430 if (!lod_is_instantiation_needed(lod_comp))
7433 CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
7434 primary, lod_comp_index(lo, lod_comp));
7435 info->lti_comp_idx[info->lti_count++] =
7436 lod_comp_index(lo, lod_comp);
7438 } else { /* MD_LAYOUT_RESYNC */
7439 if (mlc->mlc_mirror_id == 0) {
7441 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7442 if (!lod_comp_inited(lod_comp))
7445 extent.e_end = lod_comp->llc_extent.e_end;
7448 rc = lod_prepare_resync(env, lo, &extent);
7452 /* mirror write, try to init its all components */
7453 rc = lod_prepare_resync_mirror(env, lo,
7454 mlc->mlc_mirror_id);
7459 /* change the file state to SYNC_PENDING */
7460 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7463 rc = lod_declare_instantiate_components(env, lo, th);
7467 /* 3. transfer layout version to OST objects.
7468 * transfer new layout version to OST objects so that stale writes
7469 * can be denied. It also ends an era of writing by setting
7470 * LU_LAYOUT_RESYNC. Normal client can never use this bit to
7471 * send write RPC; only resync RPCs could do it. */
7472 layout_attr->la_valid = LA_LAYOUT_VERSION;
7473 layout_attr->la_layout_version = 0; /* set current version */
7474 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7475 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7476 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7480 lod_obj_inc_layout_gen(lo);
7483 lod_striping_free(env, lo);
7487 static int lod_declare_update_sync_pending(const struct lu_env *env,
7488 struct lod_object *lo, struct md_layout_change *mlc,
7491 struct lod_thread_info *info = lod_env_info(env);
7492 unsigned sync_components = 0;
7493 unsigned resync_components = 0;
7498 LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
7499 LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
7500 mlc->mlc_opc == MD_LAYOUT_WRITE);
7502 CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
7503 PFID(lod_object_fid(lo)), mlc->mlc_opc);
7505 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7506 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
7507 PFID(lod_object_fid(lo)));
7509 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7510 return lod_declare_update_write_pending(env, lo, mlc, th);
7513 /* MD_LAYOUT_RESYNC_DONE */
7515 for (i = 0; i < lo->ldo_comp_cnt; i++) {
7516 struct lod_layout_component *lod_comp;
7519 lod_comp = &lo->ldo_comp_entries[i];
7521 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
7526 for (j = 0; j < mlc->mlc_resync_count; j++) {
7527 if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
7530 mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
7531 lod_comp->llc_flags &= ~LCME_FL_STALE;
7532 resync_components++;
7538 for (i = 0; i < mlc->mlc_resync_count; i++) {
7539 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
7542 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
7543 "or already synced\n", PFID(lod_object_fid(lo)),
7544 mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
7545 GOTO(out, rc = -EINVAL);
7548 if (!sync_components || (mlc->mlc_resync_count && !resync_components)) {
7549 CDEBUG(D_LAYOUT, DFID": no mirror in sync\n",
7550 PFID(lod_object_fid(lo)));
7552 /* tend to return an error code here to prevent
7553 * the MDT from setting SoM attribute */
7554 GOTO(out, rc = -EINVAL);
7557 CDEBUG(D_LAYOUT, DFID": synced %u resynced %u/%zu components\n",
7558 PFID(lod_object_fid(lo)),
7559 sync_components, resync_components, mlc->mlc_resync_count);
7561 lo->ldo_flr_state = LCM_FL_RDONLY;
7562 lod_obj_inc_layout_gen(lo);
7564 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
7565 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
7566 &info->lti_buf, XATTR_NAME_LOV, 0, th);
7571 lod_striping_free(env, lo);
7575 typedef int (*mlc_handler)(const struct lu_env *env, struct dt_object *dt,
7576 const struct md_layout_change *mlc,
7577 struct thandle *th);
7580 * Attach stripes after target's for migrating directory. NB, we
7581 * only need to declare this, the actual work is done inside
7582 * lod_xattr_set_lmv().
7584 * \param[in] env execution environment
7585 * \param[in] dt target object
7586 * \param[in] mlc layout change data
7587 * \param[in] th transaction handle
7589 * \retval 0 on success
7590 * \retval negative if failed
7592 static int lod_dir_declare_layout_attach(const struct lu_env *env,
7593 struct dt_object *dt,
7594 const struct md_layout_change *mlc,
7597 struct lod_thread_info *info = lod_env_info(env);
7598 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
7599 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
7600 struct lod_object *lo = lod_dt_obj(dt);
7601 struct dt_object *next = dt_object_child(dt);
7602 struct dt_object_format *dof = &info->lti_format;
7603 struct lmv_mds_md_v1 *lmv = mlc->mlc_buf.lb_buf;
7604 struct dt_object **stripes;
7605 __u32 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
7606 struct lu_fid *fid = &info->lti_fid;
7607 struct lod_tgt_desc *tgt;
7608 struct dt_object *dto;
7609 struct dt_device *tgt_dt;
7610 int type = LU_SEQ_RANGE_ANY;
7611 struct dt_insert_rec *rec = &info->lti_dt_rec;
7612 char *stripe_name = info->lti_key;
7613 struct lu_name *sname;
7614 struct linkea_data ldata = { NULL };
7615 struct lu_buf linkea_buf;
7622 if (!lmv_is_sane(lmv))
7625 if (!dt_try_as_dir(env, dt))
7628 dof->dof_type = DFT_DIR;
7631 sizeof(*stripes) * (lo->ldo_dir_stripe_count + stripe_count));
7635 for (i = 0; i < lo->ldo_dir_stripe_count; i++)
7636 stripes[i] = lo->ldo_stripe[i];
7638 rec->rec_type = S_IFDIR;
7640 for (i = 0; i < stripe_count; i++) {
7642 &lmv->lmv_stripe_fids[i]);
7643 if (!fid_is_sane(fid))
7646 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
7650 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
7651 tgt_dt = lod->lod_child;
7653 tgt = LTD_TGT(ltd, idx);
7655 GOTO(out, rc = -ESTALE);
7656 tgt_dt = tgt->ltd_tgt;
7659 dto = dt_locate_at(env, tgt_dt, fid,
7660 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
7663 GOTO(out, rc = PTR_ERR(dto));
7665 stripes[i + lo->ldo_dir_stripe_count] = dto;
7667 if (!dt_try_as_dir(env, dto))
7668 GOTO(out, rc = -ENOTDIR);
7670 rc = lod_sub_declare_ref_add(env, dto, th);
7674 rec->rec_fid = lu_object_fid(&dto->do_lu);
7675 rc = lod_sub_declare_insert(env, dto,
7676 (const struct dt_rec *)rec,
7677 (const struct dt_key *)dot, th);
7681 rc = lod_sub_declare_insert(env, dto,
7682 (const struct dt_rec *)rec,
7683 (const struct dt_key *)dotdot, th);
7687 rc = lod_sub_declare_xattr_set(env, dto, &mlc->mlc_buf,
7688 XATTR_NAME_LMV, 0, th);
7692 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
7693 PFID(lu_object_fid(&dto->do_lu)),
7694 i + lo->ldo_dir_stripe_count);
7696 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
7697 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
7698 sname, lu_object_fid(&dt->do_lu));
7702 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
7703 linkea_buf.lb_len = ldata.ld_leh->leh_len;
7704 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
7705 XATTR_NAME_LINK, 0, th);
7709 rc = lod_sub_declare_insert(env, next,
7710 (const struct dt_rec *)rec,
7711 (const struct dt_key *)stripe_name,
7716 rc = lod_sub_declare_ref_add(env, next, th);
7722 OBD_FREE(lo->ldo_stripe,
7723 sizeof(*stripes) * lo->ldo_dir_stripes_allocated);
7724 lo->ldo_stripe = stripes;
7725 lo->ldo_dir_migrate_offset = lo->ldo_dir_stripe_count;
7726 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_hash_type);
7727 lo->ldo_dir_stripe_count += stripe_count;
7728 lo->ldo_dir_stripes_allocated += stripe_count;
7729 lo->ldo_dir_hash_type |= LMV_HASH_FLAG_MIGRATION;
7733 i = lo->ldo_dir_stripe_count;
7734 while (i < lo->ldo_dir_stripe_count + stripe_count && stripes[i])
7735 dt_object_put(env, stripes[i++]);
7738 sizeof(*stripes) * (stripe_count + lo->ldo_dir_stripe_count));
7742 static int lod_dir_declare_layout_detach(const struct lu_env *env,
7743 struct dt_object *dt,
7744 const struct md_layout_change *unused,
7747 struct lod_thread_info *info = lod_env_info(env);
7748 struct lod_object *lo = lod_dt_obj(dt);
7749 struct dt_object *next = dt_object_child(dt);
7750 char *stripe_name = info->lti_key;
7751 struct dt_object *dto;
7755 if (!dt_try_as_dir(env, dt))
7758 if (!lo->ldo_dir_stripe_count)
7759 return lod_sub_declare_delete(env, next,
7760 (const struct dt_key *)dotdot, th);
7762 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
7763 dto = lo->ldo_stripe[i];
7767 if (!dt_try_as_dir(env, dto))
7770 rc = lod_sub_declare_delete(env, dto,
7771 (const struct dt_key *)dotdot, th);
7775 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
7776 PFID(lu_object_fid(&dto->do_lu)), i);
7778 rc = lod_sub_declare_delete(env, next,
7779 (const struct dt_key *)stripe_name, th);
7783 rc = lod_sub_declare_ref_del(env, next, th);
7791 static int dt_dir_is_empty(const struct lu_env *env,
7792 struct dt_object *obj)
7795 const struct dt_it_ops *iops;
7800 if (!dt_try_as_dir(env, obj))
7803 iops = &obj->do_index_ops->dio_it;
7804 it = iops->init(env, obj, LUDA_64BITHASH);
7806 RETURN(PTR_ERR(it));
7808 rc = iops->get(env, it, (const struct dt_key *)"");
7812 for (rc = 0, i = 0; rc == 0 && i < 3; ++i)
7813 rc = iops->next(env, it);
7819 /* Huh? Index contains no zero key? */
7824 iops->fini(env, it);
7829 static int lod_dir_declare_layout_shrink(const struct lu_env *env,
7830 struct dt_object *dt,
7831 const struct md_layout_change *mlc,
7834 struct lod_thread_info *info = lod_env_info(env);
7835 struct lod_object *lo = lod_dt_obj(dt);
7836 struct dt_object *next = dt_object_child(dt);
7837 struct lmv_user_md *lmu = mlc->mlc_buf.lb_buf;
7838 __u32 final_stripe_count;
7839 char *stripe_name = info->lti_key;
7840 struct lu_buf *lmv_buf = &info->lti_buf;
7841 struct dt_object *dto;
7847 if (!dt_try_as_dir(env, dt))
7850 /* shouldn't be called on plain directory */
7851 LASSERT(lo->ldo_dir_stripe_count);
7853 lmv_buf->lb_buf = &info->lti_lmv.lmv_md_v1;
7854 lmv_buf->lb_len = sizeof(info->lti_lmv.lmv_md_v1);
7856 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
7857 LASSERT(final_stripe_count &&
7858 final_stripe_count < lo->ldo_dir_stripe_count);
7860 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
7861 dto = lo->ldo_stripe[i];
7865 if (i < final_stripe_count) {
7866 if (final_stripe_count == 1)
7869 rc = lod_sub_declare_xattr_set(env, dto, lmv_buf,
7871 LU_XATTR_REPLACE, th);
7878 rc = dt_dir_is_empty(env, dto);
7882 rc = lod_sub_declare_ref_del(env, dto, th);
7886 rc = lod_sub_declare_destroy(env, dto, th);
7890 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
7891 PFID(lu_object_fid(&dto->do_lu)), i);
7893 rc = lod_sub_declare_delete(env, next,
7894 (const struct dt_key *)stripe_name, th);
7898 rc = lod_sub_declare_ref_del(env, next, th);
7903 rc = lod_sub_declare_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV,
7904 LU_XATTR_REPLACE, th);
7909 * detach all stripes from dir master object, NB, stripes are not destroyed, but
7910 * deleted from it's parent namespace, this function is called in two places:
7911 * 1. mdd_migrate_mdt() detach stripes from source, and attach them to
7913 * 2. mdd_dir_layout_update() detach stripe before turning 1-stripe directory to
7914 * a plain directory.
7916 * \param[in] env execution environment
7917 * \param[in] dt target object
7918 * \param[in] mlc layout change data
7919 * \param[in] th transaction handle
7921 * \retval 0 on success
7922 * \retval negative if failed
7924 static int lod_dir_layout_detach(const struct lu_env *env,
7925 struct dt_object *dt,
7926 const struct md_layout_change *mlc,
7929 struct lod_thread_info *info = lod_env_info(env);
7930 struct lod_object *lo = lod_dt_obj(dt);
7931 struct dt_object *next = dt_object_child(dt);
7932 char *stripe_name = info->lti_key;
7933 struct dt_object *dto;
7939 if (!lo->ldo_dir_stripe_count) {
7940 /* plain directory delete .. */
7941 rc = lod_sub_delete(env, next,
7942 (const struct dt_key *)dotdot, th);
7946 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
7947 dto = lo->ldo_stripe[i];
7951 rc = lod_sub_delete(env, dto,
7952 (const struct dt_key *)dotdot, th);
7956 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
7957 PFID(lu_object_fid(&dto->do_lu)), i);
7959 rc = lod_sub_delete(env, next,
7960 (const struct dt_key *)stripe_name, th);
7964 rc = lod_sub_ref_del(env, next, th);
7969 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
7970 dto = lo->ldo_stripe[i];
7972 dt_object_put(env, dto);
7974 OBD_FREE(lo->ldo_stripe,
7975 sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated);
7976 lo->ldo_stripe = NULL;
7977 lo->ldo_dir_stripes_allocated = 0;
7978 lo->ldo_dir_stripe_count = 0;
7983 static int lod_dir_layout_shrink(const struct lu_env *env,
7984 struct dt_object *dt,
7985 const struct md_layout_change *mlc,
7988 struct lod_thread_info *info = lod_env_info(env);
7989 struct lod_object *lo = lod_dt_obj(dt);
7990 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7991 struct dt_object *next = dt_object_child(dt);
7992 struct lmv_user_md *lmu = mlc->mlc_buf.lb_buf;
7993 __u32 final_stripe_count;
7994 char *stripe_name = info->lti_key;
7995 struct dt_object *dto;
7996 struct lu_buf *lmv_buf = &info->lti_buf;
7997 struct lmv_mds_md_v1 *lmv = &info->lti_lmv.lmv_md_v1;
7999 int type = LU_SEQ_RANGE_ANY;
8005 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
8007 lmv_buf->lb_buf = lmv;
8008 lmv_buf->lb_len = sizeof(*lmv);
8009 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
8010 lmv->lmv_stripe_count = cpu_to_le32(final_stripe_count);
8011 lmv->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type) &
8012 cpu_to_le32(LMV_HASH_TYPE_MASK);
8013 lmv->lmv_layout_version =
8014 cpu_to_le32(lo->ldo_dir_layout_version + 1);
8016 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8017 dto = lo->ldo_stripe[i];
8021 if (i < final_stripe_count) {
8022 /* if only one stripe left, no need to update
8023 * LMV because this stripe will replace master
8024 * object and act as a plain directory.
8026 if (final_stripe_count == 1)
8030 rc = lod_fld_lookup(env, lod,
8031 lu_object_fid(&dto->do_lu),
8036 lmv->lmv_master_mdt_index = cpu_to_le32(mdtidx);
8037 rc = lod_sub_xattr_set(env, dto, lmv_buf,
8039 LU_XATTR_REPLACE, th);
8046 dt_write_lock(env, dto, DT_TGT_CHILD);
8047 rc = lod_sub_ref_del(env, dto, th);
8048 dt_write_unlock(env, dto);
8052 rc = lod_sub_destroy(env, dto, th);
8056 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8057 PFID(lu_object_fid(&dto->do_lu)), i);
8059 rc = lod_sub_delete(env, next,
8060 (const struct dt_key *)stripe_name, th);
8064 rc = lod_sub_ref_del(env, next, th);
8069 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &mdtidx,
8074 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
8075 lmv->lmv_master_mdt_index = cpu_to_le32(mdtidx);
8076 rc = lod_sub_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV,
8077 LU_XATTR_REPLACE, th);
8081 for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
8082 dto = lo->ldo_stripe[i];
8084 dt_object_put(env, dto);
8086 lo->ldo_dir_stripe_count = final_stripe_count;
8091 static mlc_handler dir_mlc_declare_ops[MD_LAYOUT_MAX] = {
8092 [MD_LAYOUT_ATTACH] = lod_dir_declare_layout_attach,
8093 [MD_LAYOUT_DETACH] = lod_dir_declare_layout_detach,
8094 [MD_LAYOUT_SHRINK] = lod_dir_declare_layout_shrink,
8097 static mlc_handler dir_mlc_ops[MD_LAYOUT_MAX] = {
8098 [MD_LAYOUT_DETACH] = lod_dir_layout_detach,
8099 [MD_LAYOUT_SHRINK] = lod_dir_layout_shrink,
8102 static int lod_declare_layout_change(const struct lu_env *env,
8103 struct dt_object *dt, struct md_layout_change *mlc,
8106 struct lod_thread_info *info = lod_env_info(env);
8107 struct lod_object *lo = lod_dt_obj(dt);
8112 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
8113 LASSERT(dir_mlc_declare_ops[mlc->mlc_opc]);
8114 rc = dir_mlc_declare_ops[mlc->mlc_opc](env, dt, mlc, th);
8118 if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
8119 dt_object_remote(dt_object_child(dt)))
8122 rc = lod_striping_load(env, lo);
8126 LASSERT(lo->ldo_comp_cnt > 0);
8128 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
8132 switch (lo->ldo_flr_state) {
8134 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
8138 rc = lod_declare_update_rdonly(env, lo, mlc, th);
8140 case LCM_FL_WRITE_PENDING:
8141 rc = lod_declare_update_write_pending(env, lo, mlc, th);
8143 case LCM_FL_SYNC_PENDING:
8144 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
8155 * Instantiate layout component objects which covers the intent write offset.
8157 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
8158 struct md_layout_change *mlc, struct thandle *th)
8160 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
8161 struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
8162 struct lod_object *lo = lod_dt_obj(dt);
8167 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
8168 LASSERT(dir_mlc_ops[mlc->mlc_opc]);
8169 rc = dir_mlc_ops[mlc->mlc_opc](env, dt, mlc, th);
8173 rc = lod_striped_create(env, dt, attr, NULL, th);
8174 if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
8175 layout_attr->la_layout_version |= lo->ldo_layout_gen;
8176 rc = lod_attr_set(env, dt, layout_attr, th);
8182 struct dt_object_operations lod_obj_ops = {
8183 .do_read_lock = lod_read_lock,
8184 .do_write_lock = lod_write_lock,
8185 .do_read_unlock = lod_read_unlock,
8186 .do_write_unlock = lod_write_unlock,
8187 .do_write_locked = lod_write_locked,
8188 .do_attr_get = lod_attr_get,
8189 .do_declare_attr_set = lod_declare_attr_set,
8190 .do_attr_set = lod_attr_set,
8191 .do_xattr_get = lod_xattr_get,
8192 .do_declare_xattr_set = lod_declare_xattr_set,
8193 .do_xattr_set = lod_xattr_set,
8194 .do_declare_xattr_del = lod_declare_xattr_del,
8195 .do_xattr_del = lod_xattr_del,
8196 .do_xattr_list = lod_xattr_list,
8197 .do_ah_init = lod_ah_init,
8198 .do_declare_create = lod_declare_create,
8199 .do_create = lod_create,
8200 .do_declare_destroy = lod_declare_destroy,
8201 .do_destroy = lod_destroy,
8202 .do_index_try = lod_index_try,
8203 .do_declare_ref_add = lod_declare_ref_add,
8204 .do_ref_add = lod_ref_add,
8205 .do_declare_ref_del = lod_declare_ref_del,
8206 .do_ref_del = lod_ref_del,
8207 .do_object_sync = lod_object_sync,
8208 .do_object_lock = lod_object_lock,
8209 .do_object_unlock = lod_object_unlock,
8210 .do_invalidate = lod_invalidate,
8211 .do_declare_layout_change = lod_declare_layout_change,
8212 .do_layout_change = lod_layout_change,
8216 * Implementation of dt_body_operations::dbo_read.
8218 * \see dt_body_operations::dbo_read() in the API description for details.
8220 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
8221 struct lu_buf *buf, loff_t *pos)
8223 struct dt_object *next = dt_object_child(dt);
8225 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
8226 S_ISLNK(dt->do_lu.lo_header->loh_attr));
8227 return next->do_body_ops->dbo_read(env, next, buf, pos);
8231 * Implementation of dt_body_operations::dbo_declare_write.
8233 * \see dt_body_operations::dbo_declare_write() in the API description
8236 static ssize_t lod_declare_write(const struct lu_env *env,
8237 struct dt_object *dt,
8238 const struct lu_buf *buf, loff_t pos,
8241 return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
8245 * Implementation of dt_body_operations::dbo_write.
8247 * \see dt_body_operations::dbo_write() in the API description for details.
8249 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
8250 const struct lu_buf *buf, loff_t *pos,
8253 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
8254 S_ISLNK(dt->do_lu.lo_header->loh_attr));
8255 return lod_sub_write(env, dt_object_child(dt), buf, pos, th);
8258 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
8259 __u64 start, __u64 end, struct thandle *th)
8261 if (dt_object_remote(dt))
8264 return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
8267 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
8268 __u64 start, __u64 end, struct thandle *th)
8270 if (dt_object_remote(dt))
8273 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
8274 return lod_sub_punch(env, dt_object_child(dt), start, end, th);
8278 * different type of files use the same body_ops because object may be created
8279 * in OUT, where there is no chance to set correct body_ops for each type, so
8280 * body_ops themselves will check file type inside, see lod_read/write/punch for
8283 const struct dt_body_operations lod_body_ops = {
8284 .dbo_read = lod_read,
8285 .dbo_declare_write = lod_declare_write,
8286 .dbo_write = lod_write,
8287 .dbo_declare_punch = lod_declare_punch,
8288 .dbo_punch = lod_punch,
8292 * Implementation of lu_object_operations::loo_object_init.
8294 * The function determines the type and the index of the target device using
8295 * sequence of the object's FID. Then passes control down to the
8296 * corresponding device:
8297 * OSD for the local objects, OSP for remote
8299 * \see lu_object_operations::loo_object_init() in the API description
8302 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
8303 const struct lu_object_conf *conf)
8305 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
8306 struct lu_device *cdev = NULL;
8307 struct lu_object *cobj;
8308 struct lod_tgt_descs *ltd = NULL;
8309 struct lod_tgt_desc *tgt;
8311 int type = LU_SEQ_RANGE_ANY;
8315 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
8319 if (type == LU_SEQ_RANGE_MDT &&
8320 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
8321 cdev = &lod->lod_child->dd_lu_dev;
8322 } else if (type == LU_SEQ_RANGE_MDT) {
8323 ltd = &lod->lod_mdt_descs;
8325 } else if (type == LU_SEQ_RANGE_OST) {
8326 ltd = &lod->lod_ost_descs;
8333 if (ltd->ltd_tgts_size > idx &&
8334 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
8335 tgt = LTD_TGT(ltd, idx);
8337 LASSERT(tgt != NULL);
8338 LASSERT(tgt->ltd_tgt != NULL);
8340 cdev = &(tgt->ltd_tgt->dd_lu_dev);
8342 lod_putref(lod, ltd);
8345 if (unlikely(cdev == NULL))
8348 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
8349 if (unlikely(cobj == NULL))
8352 lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
8354 lu_object_add(lo, cobj);
8361 * Alloc cached foreign LOV
8363 * \param[in] lo object
8364 * \param[in] size size of foreign LOV
8366 * \retval 0 on success
8367 * \retval negative if failed
8369 int lod_alloc_foreign_lov(struct lod_object *lo, size_t size)
8371 OBD_ALLOC_LARGE(lo->ldo_foreign_lov, size);
8372 if (lo->ldo_foreign_lov == NULL)
8374 lo->ldo_foreign_lov_size = size;
8375 lo->ldo_is_foreign = 1;
8381 * Free cached foreign LOV
8383 * \param[in] lo object
8385 void lod_free_foreign_lov(struct lod_object *lo)
8387 if (lo->ldo_foreign_lov != NULL)
8388 OBD_FREE_LARGE(lo->ldo_foreign_lov, lo->ldo_foreign_lov_size);
8389 lo->ldo_foreign_lov = NULL;
8390 lo->ldo_foreign_lov_size = 0;
8391 lo->ldo_is_foreign = 0;
8396 * Free cached foreign LMV
8398 * \param[in] lo object
8400 void lod_free_foreign_lmv(struct lod_object *lo)
8402 if (lo->ldo_foreign_lmv != NULL)
8403 OBD_FREE_LARGE(lo->ldo_foreign_lmv, lo->ldo_foreign_lmv_size);
8404 lo->ldo_foreign_lmv = NULL;
8405 lo->ldo_foreign_lmv_size = 0;
8406 lo->ldo_dir_is_foreign = 0;
8411 * Release resources associated with striping.
8413 * If the object is striped (regular or directory), then release
8414 * the stripe objects references and free the ldo_stripe array.
8416 * \param[in] env execution environment
8417 * \param[in] lo object
8419 void lod_striping_free_nolock(const struct lu_env *env, struct lod_object *lo)
8421 struct lod_layout_component *lod_comp;
8424 if (unlikely(lo->ldo_is_foreign)) {
8425 lod_free_foreign_lov(lo);
8426 lo->ldo_comp_cached = 0;
8427 } else if (unlikely(lo->ldo_dir_is_foreign)) {
8428 lod_free_foreign_lmv(lo);
8429 lo->ldo_dir_stripe_loaded = 0;
8430 } else if (lo->ldo_stripe != NULL) {
8431 LASSERT(lo->ldo_comp_entries == NULL);
8432 LASSERT(lo->ldo_dir_stripes_allocated > 0);
8434 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8435 if (lo->ldo_stripe[i])
8436 dt_object_put(env, lo->ldo_stripe[i]);
8439 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
8440 OBD_FREE(lo->ldo_stripe, j);
8441 lo->ldo_stripe = NULL;
8442 lo->ldo_dir_stripes_allocated = 0;
8443 lo->ldo_dir_stripe_loaded = 0;
8444 lo->ldo_dir_stripe_count = 0;
8445 } else if (lo->ldo_comp_entries != NULL) {
8446 for (i = 0; i < lo->ldo_comp_cnt; i++) {
8447 /* free lod_layout_component::llc_stripe array */
8448 lod_comp = &lo->ldo_comp_entries[i];
8450 if (lod_comp->llc_stripe == NULL)
8452 LASSERT(lod_comp->llc_stripes_allocated != 0);
8453 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
8454 if (lod_comp->llc_stripe[j] != NULL)
8456 &lod_comp->llc_stripe[j]->do_lu);
8458 OBD_FREE(lod_comp->llc_stripe,
8459 sizeof(struct dt_object *) *
8460 lod_comp->llc_stripes_allocated);
8461 lod_comp->llc_stripe = NULL;
8462 OBD_FREE(lod_comp->llc_ost_indices,
8464 lod_comp->llc_stripes_allocated);
8465 lod_comp->llc_ost_indices = NULL;
8466 lod_comp->llc_stripes_allocated = 0;
8468 lod_free_comp_entries(lo);
8469 lo->ldo_comp_cached = 0;
8473 void lod_striping_free(const struct lu_env *env, struct lod_object *lo)
8475 mutex_lock(&lo->ldo_layout_mutex);
8476 lod_striping_free_nolock(env, lo);
8477 mutex_unlock(&lo->ldo_layout_mutex);
8481 * Implementation of lu_object_operations::loo_object_free.
8483 * \see lu_object_operations::loo_object_free() in the API description
8486 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
8488 struct lod_object *lo = lu2lod_obj(o);
8490 /* release all underlying object pinned */
8491 lod_striping_free(env, lo);
8493 /* lo doesn't contain a lu_object_header, so we don't need call_rcu */
8494 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
8498 * Implementation of lu_object_operations::loo_object_release.
8500 * \see lu_object_operations::loo_object_release() in the API description
8503 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
8505 /* XXX: shouldn't we release everything here in case if object
8506 * creation failed before? */
8510 * Implementation of lu_object_operations::loo_object_print.
8512 * \see lu_object_operations::loo_object_print() in the API description
8515 static int lod_object_print(const struct lu_env *env, void *cookie,
8516 lu_printer_t p, const struct lu_object *l)
8518 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
8520 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
8523 struct lu_object_operations lod_lu_obj_ops = {
8524 .loo_object_init = lod_object_init,
8525 .loo_object_free = lod_object_free,
8526 .loo_object_release = lod_object_release,
8527 .loo_object_print = lod_object_print,