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 tgt_dt = lod->lod_child;
2062 rc = dt_fid_alloc(env, tgt_dt, &fid, NULL,
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 = dt_fid_alloc(env, tgt_dt, &fid, NULL, 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 = dt_fid_alloc(env, lod->lod_child, &fid, NULL, 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, 1, stripe_count);
2182 rc = lod_mdt_alloc_rr(env, lo, stripes, 1,
2186 bool is_specific = false;
2188 OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
2190 GOTO(out, rc = -ENOMEM);
2192 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) {
2194 for (i = 0; i < stripe_count; i++)
2196 le32_to_cpu(lum->lum_objects[i].lum_mds);
2199 /* stripe 0 is local */
2201 lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
2202 rc = lod_mdt_alloc_specific(env, lo, stripes, idx_array,
2204 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
2212 lo->ldo_dir_striped = 1;
2213 lo->ldo_stripe = stripes;
2214 lo->ldo_dir_stripe_count = rc;
2215 lo->ldo_dir_stripes_allocated = stripe_count;
2217 lo->ldo_dir_stripe_loaded = 1;
2219 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
2221 lod_striping_free(env, lo);
2227 if (!IS_ERR_OR_NULL(stripes[0]))
2228 dt_object_put(env, stripes[0]);
2229 for (i = 1; i < stripe_count; i++)
2230 LASSERT(!stripes[i]);
2231 OBD_FREE(stripes, sizeof(stripes[0]) * stripe_count);
2238 * Alloc cached foreign LMV
2240 * \param[in] lo object
2241 * \param[in] size size of foreign LMV
2243 * \retval 0 on success
2244 * \retval negative if failed
2246 int lod_alloc_foreign_lmv(struct lod_object *lo, size_t size)
2248 OBD_ALLOC_LARGE(lo->ldo_foreign_lmv, size);
2249 if (lo->ldo_foreign_lmv == NULL)
2251 lo->ldo_foreign_lmv_size = size;
2252 lo->ldo_dir_is_foreign = 1;
2258 * Declare create striped md object.
2260 * The function declares intention to create a striped directory. This is a
2261 * wrapper for lod_prep_md_striped_create(). The only additional functionality
2262 * is to verify pattern \a lum_buf is good. Check that function for the details.
2264 * \param[in] env execution environment
2265 * \param[in] dt object
2266 * \param[in] attr attributes to initialize the objects with
2267 * \param[in] lum_buf a pattern specifying the number of stripes and
2269 * \param[in] dof type of objects to be created
2270 * \param[in] th transaction handle
2272 * \retval 0 on success
2273 * \retval negative if failed
2276 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
2277 struct dt_object *dt,
2278 struct lu_attr *attr,
2279 const struct lu_buf *lum_buf,
2280 struct dt_object_format *dof,
2283 struct lod_object *lo = lod_dt_obj(dt);
2284 struct lmv_user_md_v1 *lum = lum_buf->lb_buf;
2288 LASSERT(lum != NULL);
2290 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
2291 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
2292 (int)le32_to_cpu(lum->lum_stripe_offset));
2294 if (lo->ldo_dir_stripe_count == 0) {
2295 if (lo->ldo_dir_is_foreign) {
2296 rc = lod_alloc_foreign_lmv(lo, lum_buf->lb_len);
2299 memcpy(lo->ldo_foreign_lmv, lum, lum_buf->lb_len);
2300 lo->ldo_dir_stripe_loaded = 1;
2305 /* prepare dir striped objects */
2306 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2308 /* failed to create striping, let's reset
2309 * config so that others don't get confused */
2310 lod_striping_free(env, lo);
2318 * Set or replace striped directory layout, and LFSCK may set layout on a plain
2319 * directory, so don't check stripe count.
2321 * \param[in] env execution environment
2322 * \param[in] dt target object
2323 * \param[in] buf LMV buf which contains source stripe fids
2324 * \param[in] fl set or replace
2325 * \param[in] th transaction handle
2327 * \retval 0 on success
2328 * \retval negative if failed
2330 static int lod_dir_layout_set(const struct lu_env *env,
2331 struct dt_object *dt,
2332 const struct lu_buf *buf,
2336 struct dt_object *next = dt_object_child(dt);
2337 struct lod_object *lo = lod_dt_obj(dt);
2338 struct lmv_mds_md_v1 *lmv = buf->lb_buf;
2339 struct lmv_mds_md_v1 *slave_lmv;
2340 struct lu_buf slave_buf;
2346 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LMV, fl, th);
2350 OBD_ALLOC_PTR(slave_lmv);
2354 lod_prep_slave_lmv_md(slave_lmv, lmv);
2355 slave_buf.lb_buf = slave_lmv;
2356 slave_buf.lb_len = sizeof(*slave_lmv);
2358 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2359 if (!lo->ldo_stripe[i])
2362 if (!dt_object_exists(lo->ldo_stripe[i]))
2365 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], &slave_buf,
2366 XATTR_NAME_LMV, fl, th);
2371 lod_striping_free(env, lod_dt_obj(dt));
2372 OBD_FREE_PTR(slave_lmv);
2378 * Implementation of dt_object_operations::do_declare_xattr_set.
2380 * Used with regular (non-striped) objects. Basically it
2381 * initializes the striping information and applies the
2382 * change to all the stripes.
2384 * \see dt_object_operations::do_declare_xattr_set() in the API description
2387 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2388 struct dt_object *dt,
2389 const struct lu_buf *buf,
2390 const char *name, int fl,
2393 struct dt_object *next = dt_object_child(dt);
2394 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2395 struct lod_object *lo = lod_dt_obj(dt);
2400 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2401 struct lmv_user_md_v1 *lum;
2403 LASSERT(buf != NULL && buf->lb_buf != NULL);
2405 rc = lod_verify_md_striping(d, lum);
2408 } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2409 rc = lod_verify_striping(env, d, lo, buf, false);
2414 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2418 /* Note: Do not set LinkEA on sub-stripes, otherwise
2419 * it will confuse the fid2path process(see mdt_path_current()).
2420 * The linkEA between master and sub-stripes is set in
2421 * lod_xattr_set_lmv(). */
2422 if (strcmp(name, XATTR_NAME_LINK) == 0)
2425 /* set xattr to each stripes, if needed */
2426 rc = lod_striping_load(env, lo);
2430 if (lo->ldo_dir_stripe_count == 0)
2433 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2434 if (!lo->ldo_stripe[i])
2437 if (!dt_object_exists(lo->ldo_stripe[i]))
2440 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2450 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2451 struct lod_object *lo,
2452 struct dt_object *dt, struct thandle *th,
2453 int comp_idx, int stripe_idx,
2454 struct lod_obj_stripe_cb_data *data)
2456 struct lod_thread_info *info = lod_env_info(env);
2457 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
2458 struct filter_fid *ff = &info->lti_ff;
2459 struct lu_buf *buf = &info->lti_buf;
2463 buf->lb_len = sizeof(*ff);
2464 rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2472 * locd_buf is set if it's called by dir migration, which doesn't check
2475 if (data->locd_buf) {
2476 memset(ff, 0, sizeof(*ff));
2477 ff->ff_parent = *(struct lu_fid *)data->locd_buf->lb_buf;
2479 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
2481 if (lu_fid_eq(lod_object_fid(lo), &ff->ff_parent) &&
2482 ff->ff_layout.ol_comp_id == comp->llc_id)
2485 memset(ff, 0, sizeof(*ff));
2486 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2489 /* rewrite filter_fid */
2490 ff->ff_parent.f_ver = stripe_idx;
2491 ff->ff_layout.ol_stripe_size = comp->llc_stripe_size;
2492 ff->ff_layout.ol_stripe_count = comp->llc_stripe_count;
2493 ff->ff_layout.ol_comp_id = comp->llc_id;
2494 ff->ff_layout.ol_comp_start = comp->llc_extent.e_start;
2495 ff->ff_layout.ol_comp_end = comp->llc_extent.e_end;
2496 filter_fid_cpu_to_le(ff, ff, sizeof(*ff));
2498 if (data->locd_declare)
2499 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2500 LU_XATTR_REPLACE, th);
2502 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2503 LU_XATTR_REPLACE, th);
2509 * Reset parent FID on OST object
2511 * Replace parent FID with @dt object FID, which is only called during migration
2512 * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2513 * the FID is changed.
2515 * \param[in] env execution environment
2516 * \param[in] dt dt_object whose stripes's parent FID will be reset
2517 * \parem[in] th thandle
2518 * \param[in] declare if it is declare
2520 * \retval 0 if reset succeeds
2521 * \retval negative errno if reset fails
2523 static int lod_replace_parent_fid(const struct lu_env *env,
2524 struct dt_object *dt,
2525 const struct lu_buf *buf,
2526 struct thandle *th, bool declare)
2528 struct lod_object *lo = lod_dt_obj(dt);
2529 struct lod_thread_info *info = lod_env_info(env);
2530 struct filter_fid *ff;
2531 struct lod_obj_stripe_cb_data data = { { 0 } };
2535 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2537 /* set xattr to each stripes, if needed */
2538 rc = lod_striping_load(env, lo);
2542 if (!lod_obj_is_striped(dt))
2545 if (info->lti_ea_store_size < sizeof(*ff)) {
2546 rc = lod_ea_store_resize(info, sizeof(*ff));
2551 data.locd_declare = declare;
2552 data.locd_stripe_cb = lod_obj_stripe_replace_parent_fid_cb;
2553 data.locd_buf = buf;
2554 rc = lod_obj_for_each_stripe(env, lo, th, &data);
2559 inline __u16 lod_comp_entry_stripe_count(struct lod_object *lo,
2560 struct lod_layout_component *entry,
2563 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2567 else if (lod_comp_inited(entry))
2568 return entry->llc_stripe_count;
2569 else if ((__u16)-1 == entry->llc_stripe_count)
2570 return lod->lod_ost_count;
2572 return lod_get_stripe_count(lod, lo,
2573 entry->llc_stripe_count, false);
2576 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2578 int magic, size = 0, i;
2579 struct lod_layout_component *comp_entries;
2581 bool is_composite, is_foreign = false;
2584 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2585 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2587 lo->ldo_def_striping->lds_def_striping_is_composite;
2589 comp_cnt = lo->ldo_comp_cnt;
2590 comp_entries = lo->ldo_comp_entries;
2591 is_composite = lo->ldo_is_composite;
2592 is_foreign = lo->ldo_is_foreign;
2596 return lo->ldo_foreign_lov_size;
2598 LASSERT(comp_cnt != 0 && comp_entries != NULL);
2600 size = sizeof(struct lov_comp_md_v1) +
2601 sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2602 LASSERT(size % sizeof(__u64) == 0);
2605 for (i = 0; i < comp_cnt; i++) {
2608 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2609 stripe_count = lod_comp_entry_stripe_count(lo, &comp_entries[i],
2611 if (!is_dir && is_composite)
2612 lod_comp_shrink_stripe_count(&comp_entries[i],
2615 size += lov_user_md_size(stripe_count, magic);
2616 LASSERT(size % sizeof(__u64) == 0);
2622 * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2623 * the xattr value is binary lov_comp_md_v1 which contains component(s)
2626 * \param[in] env execution environment
2627 * \param[in] dt dt_object to add components on
2628 * \param[in] buf buffer contains components to be added
2629 * \parem[in] th thandle
2631 * \retval 0 on success
2632 * \retval negative errno on failure
2634 static int lod_declare_layout_add(const struct lu_env *env,
2635 struct dt_object *dt,
2636 const struct lu_buf *buf,
2639 struct lod_thread_info *info = lod_env_info(env);
2640 struct lod_layout_component *comp_array, *lod_comp, *old_array;
2641 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2642 struct dt_object *next = dt_object_child(dt);
2643 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
2644 struct lod_object *lo = lod_dt_obj(dt);
2645 struct lov_user_md_v3 *v3;
2646 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2648 int i, rc, array_cnt, old_array_cnt;
2651 LASSERT(lo->ldo_is_composite);
2653 if (lo->ldo_flr_state != LCM_FL_NONE)
2656 rc = lod_verify_striping(env, d, lo, buf, false);
2660 magic = comp_v1->lcm_magic;
2661 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2662 lustre_swab_lov_comp_md_v1(comp_v1);
2663 magic = comp_v1->lcm_magic;
2666 if (magic != LOV_USER_MAGIC_COMP_V1)
2669 array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2670 OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2671 if (comp_array == NULL)
2674 memcpy(comp_array, lo->ldo_comp_entries,
2675 sizeof(*comp_array) * lo->ldo_comp_cnt);
2677 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2678 struct lov_user_md_v1 *v1;
2679 struct lu_extent *ext;
2681 v1 = (struct lov_user_md *)((char *)comp_v1 +
2682 comp_v1->lcm_entries[i].lcme_offset);
2683 ext = &comp_v1->lcm_entries[i].lcme_extent;
2685 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2686 lod_comp->llc_extent.e_start = ext->e_start;
2687 lod_comp->llc_extent.e_end = ext->e_end;
2688 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2689 lod_comp->llc_flags = comp_v1->lcm_entries[i].lcme_flags;
2691 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2692 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2693 lod_adjust_stripe_info(lod_comp, desc, 0);
2695 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2696 v3 = (struct lov_user_md_v3 *) v1;
2697 if (v3->lmm_pool_name[0] != '\0') {
2698 rc = lod_set_pool(&lod_comp->llc_pool,
2706 old_array = lo->ldo_comp_entries;
2707 old_array_cnt = lo->ldo_comp_cnt;
2709 lo->ldo_comp_entries = comp_array;
2710 lo->ldo_comp_cnt = array_cnt;
2712 /* No need to increase layout generation here, it will be increased
2713 * later when generating component ID for the new components */
2715 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2716 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2717 XATTR_NAME_LOV, 0, th);
2719 lo->ldo_comp_entries = old_array;
2720 lo->ldo_comp_cnt = old_array_cnt;
2724 OBD_FREE(old_array, sizeof(*lod_comp) * old_array_cnt);
2726 LASSERT(lo->ldo_mirror_count == 1);
2727 lo->ldo_mirrors[0].lme_end = array_cnt - 1;
2732 for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2733 lod_comp = &comp_array[i];
2734 if (lod_comp->llc_pool != NULL) {
2735 OBD_FREE(lod_comp->llc_pool,
2736 strlen(lod_comp->llc_pool) + 1);
2737 lod_comp->llc_pool = NULL;
2740 OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2745 * lod_last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2746 * @mirror_id: Mirror id to be checked.
2749 * This function checks if a mirror with specified @mirror_id is the last
2750 * non-stale mirror of a LOD object @lo.
2752 * Return: true or false.
2755 bool lod_last_non_stale_mirror(__u16 mirror_id, struct lod_object *lo)
2757 struct lod_layout_component *lod_comp;
2758 bool has_stale_flag;
2761 for (i = 0; i < lo->ldo_mirror_count; i++) {
2762 if (lo->ldo_mirrors[i].lme_id == mirror_id ||
2763 lo->ldo_mirrors[i].lme_stale)
2766 has_stale_flag = false;
2767 lod_foreach_mirror_comp(lod_comp, lo, i) {
2768 if (lod_comp->llc_flags & LCME_FL_STALE) {
2769 has_stale_flag = true;
2773 if (!has_stale_flag)
2781 * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2782 * the '$field' can only be 'flags' now. The xattr value is binary
2783 * lov_comp_md_v1 which contains the component ID(s) and the value of
2784 * the field to be modified.
2785 * Please update allowed_lustre_lov macro if $field groks more values
2788 * \param[in] env execution environment
2789 * \param[in] dt dt_object to be modified
2790 * \param[in] op operation string, like "set.flags"
2791 * \param[in] buf buffer contains components to be set
2792 * \parem[in] th thandle
2794 * \retval 0 on success
2795 * \retval negative errno on failure
2797 static int lod_declare_layout_set(const struct lu_env *env,
2798 struct dt_object *dt,
2799 char *op, const struct lu_buf *buf,
2802 struct lod_layout_component *lod_comp;
2803 struct lod_thread_info *info = lod_env_info(env);
2804 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2805 struct lod_object *lo = lod_dt_obj(dt);
2806 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2809 bool changed = false;
2812 /* Please update allowed_lustre_lov macro if op
2813 * groks more values in the future
2815 if (strcmp(op, "set.flags") != 0) {
2816 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2817 lod2obd(d)->obd_name, op);
2821 magic = comp_v1->lcm_magic;
2822 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2823 lustre_swab_lov_comp_md_v1(comp_v1);
2824 magic = comp_v1->lcm_magic;
2827 if (magic != LOV_USER_MAGIC_COMP_V1)
2830 if (comp_v1->lcm_entry_count == 0) {
2831 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2832 lod2obd(d)->obd_name);
2836 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2837 __u32 id = comp_v1->lcm_entries[i].lcme_id;
2838 __u32 flags = comp_v1->lcm_entries[i].lcme_flags;
2839 __u32 mirror_flag = flags & LCME_MIRROR_FLAGS;
2840 __u16 mirror_id = mirror_id_of(id);
2841 bool neg = flags & LCME_FL_NEG;
2843 if (flags & LCME_FL_INIT) {
2845 lod_striping_free(env, lo);
2849 flags &= ~(LCME_MIRROR_FLAGS | LCME_FL_NEG);
2850 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2851 lod_comp = &lo->ldo_comp_entries[j];
2853 /* lfs only put one flag in each entry */
2854 if ((flags && id != lod_comp->llc_id) ||
2855 (mirror_flag && mirror_id !=
2856 mirror_id_of(lod_comp->llc_id)))
2861 lod_comp->llc_flags &= ~flags;
2863 lod_comp->llc_flags &= ~mirror_flag;
2866 if ((flags & LCME_FL_STALE) &&
2867 lod_last_non_stale_mirror(mirror_id,
2870 lod_comp->llc_flags |= flags;
2873 lod_comp->llc_flags |= mirror_flag;
2874 if (mirror_flag & LCME_FL_NOSYNC)
2875 lod_comp->llc_timestamp =
2876 ktime_get_real_seconds();
2884 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2885 lod2obd(d)->obd_name);
2889 lod_obj_inc_layout_gen(lo);
2891 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2892 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), &info->lti_buf,
2893 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
2898 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2899 * and the xattr value is a unique component ID or a special lcme_id.
2901 * \param[in] env execution environment
2902 * \param[in] dt dt_object to be operated on
2903 * \param[in] buf buffer contains component ID or lcme_id
2904 * \parem[in] th thandle
2906 * \retval 0 on success
2907 * \retval negative errno on failure
2909 static int lod_declare_layout_del(const struct lu_env *env,
2910 struct dt_object *dt,
2911 const struct lu_buf *buf,
2914 struct lod_thread_info *info = lod_env_info(env);
2915 struct dt_object *next = dt_object_child(dt);
2916 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2917 struct lod_object *lo = lod_dt_obj(dt);
2918 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2919 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2920 __u32 magic, id, flags, neg_flags = 0;
2924 LASSERT(lo->ldo_is_composite);
2926 if (lo->ldo_flr_state != LCM_FL_NONE)
2929 magic = comp_v1->lcm_magic;
2930 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2931 lustre_swab_lov_comp_md_v1(comp_v1);
2932 magic = comp_v1->lcm_magic;
2935 if (magic != LOV_USER_MAGIC_COMP_V1)
2938 id = comp_v1->lcm_entries[0].lcme_id;
2939 flags = comp_v1->lcm_entries[0].lcme_flags;
2941 if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
2942 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
2943 lod2obd(d)->obd_name, id, flags);
2947 if (id != LCME_ID_INVAL && flags != 0) {
2948 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
2949 lod2obd(d)->obd_name);
2953 if (id == LCME_ID_INVAL && !flags) {
2954 CDEBUG(D_LAYOUT, "%s: no id or flags specified.\n",
2955 lod2obd(d)->obd_name);
2959 if (flags & LCME_FL_NEG) {
2960 neg_flags = flags & ~LCME_FL_NEG;
2964 left = lo->ldo_comp_cnt;
2968 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
2969 struct lod_layout_component *lod_comp;
2971 lod_comp = &lo->ldo_comp_entries[i];
2973 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
2975 else if (flags && !(flags & lod_comp->llc_flags))
2977 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
2980 if (left != (i + 1)) {
2981 CDEBUG(D_LAYOUT, "%s: this deletion will create "
2982 "a hole.\n", lod2obd(d)->obd_name);
2987 /* Mark the component as deleted */
2988 lod_comp->llc_id = LCME_ID_INVAL;
2990 /* Not instantiated component */
2991 if (lod_comp->llc_stripe == NULL)
2994 LASSERT(lod_comp->llc_stripe_count > 0);
2995 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
2996 struct dt_object *obj = lod_comp->llc_stripe[j];
3000 rc = lod_sub_declare_destroy(env, obj, th);
3006 LASSERTF(left >= 0, "left = %d\n", left);
3007 if (left == lo->ldo_comp_cnt) {
3008 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
3009 lod2obd(d)->obd_name, id);
3013 memset(attr, 0, sizeof(*attr));
3014 attr->la_valid = LA_SIZE;
3015 rc = lod_sub_declare_attr_set(env, next, attr, th);
3020 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
3021 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
3022 XATTR_NAME_LOV, 0, th);
3024 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
3031 * Declare layout add/set/del operations issued by special xattr names:
3033 * XATTR_LUSTRE_LOV.add add component(s) to existing file
3034 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
3035 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
3037 * \param[in] env execution environment
3038 * \param[in] dt object
3039 * \param[in] name name of xattr
3040 * \param[in] buf lu_buf contains xattr value
3041 * \param[in] th transaction handle
3043 * \retval 0 on success
3044 * \retval negative if failed
3046 static int lod_declare_modify_layout(const struct lu_env *env,
3047 struct dt_object *dt,
3049 const struct lu_buf *buf,
3052 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3053 struct lod_object *lo = lod_dt_obj(dt);
3055 int rc, len = strlen(XATTR_LUSTRE_LOV);
3058 LASSERT(dt_object_exists(dt));
3060 if (strlen(name) <= len || name[len] != '.') {
3061 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
3062 lod2obd(d)->obd_name, name);
3067 rc = lod_striping_load(env, lo);
3071 /* the layout to be modified must be a composite layout */
3072 if (!lo->ldo_is_composite) {
3073 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
3074 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3075 GOTO(unlock, rc = -EINVAL);
3078 op = (char *)name + len;
3079 if (strcmp(op, "add") == 0) {
3080 rc = lod_declare_layout_add(env, dt, buf, th);
3081 } else if (strcmp(op, "del") == 0) {
3082 rc = lod_declare_layout_del(env, dt, buf, th);
3083 } else if (strncmp(op, "set", strlen("set")) == 0) {
3084 rc = lod_declare_layout_set(env, dt, op, buf, th);
3086 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
3087 lod2obd(d)->obd_name, name);
3088 GOTO(unlock, rc = -ENOTSUPP);
3092 lod_striping_free(env, lo);
3098 * Convert a plain file lov_mds_md to a composite layout.
3100 * \param[in,out] info the thread info::lti_ea_store buffer contains little
3101 * endian plain file layout
3103 * \retval 0 on success, <0 on failure
3105 static int lod_layout_convert(struct lod_thread_info *info)
3107 struct lov_mds_md *lmm = info->lti_ea_store;
3108 struct lov_mds_md *lmm_save;
3109 struct lov_comp_md_v1 *lcm;
3110 struct lov_comp_md_entry_v1 *lcme;
3116 /* realloc buffer to a composite layout which contains one component */
3117 blob_size = lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
3118 le32_to_cpu(lmm->lmm_magic));
3119 size = sizeof(*lcm) + sizeof(*lcme) + blob_size;
3121 OBD_ALLOC_LARGE(lmm_save, blob_size);
3123 GOTO(out, rc = -ENOMEM);
3125 memcpy(lmm_save, lmm, blob_size);
3127 if (info->lti_ea_store_size < size) {
3128 rc = lod_ea_store_resize(info, size);
3133 lcm = info->lti_ea_store;
3134 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
3135 lcm->lcm_size = cpu_to_le32(size);
3136 lcm->lcm_layout_gen = cpu_to_le32(le16_to_cpu(
3137 lmm_save->lmm_layout_gen));
3138 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
3139 lcm->lcm_entry_count = cpu_to_le16(1);
3140 lcm->lcm_mirror_count = 0;
3142 lcme = &lcm->lcm_entries[0];
3143 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
3144 lcme->lcme_extent.e_start = 0;
3145 lcme->lcme_extent.e_end = cpu_to_le64(OBD_OBJECT_EOF);
3146 lcme->lcme_offset = cpu_to_le32(sizeof(*lcm) + sizeof(*lcme));
3147 lcme->lcme_size = cpu_to_le32(blob_size);
3149 memcpy((char *)lcm + lcme->lcme_offset, (char *)lmm_save, blob_size);
3154 OBD_FREE_LARGE(lmm_save, blob_size);
3159 * Merge layouts to form a mirrored file.
3161 static int lod_declare_layout_merge(const struct lu_env *env,
3162 struct dt_object *dt, const struct lu_buf *mbuf,
3165 struct lod_thread_info *info = lod_env_info(env);
3166 struct lu_buf *buf = &info->lti_buf;
3167 struct lod_object *lo = lod_dt_obj(dt);
3168 struct lov_comp_md_v1 *lcm;
3169 struct lov_comp_md_v1 *cur_lcm;
3170 struct lov_comp_md_v1 *merge_lcm;
3171 struct lov_comp_md_entry_v1 *lcme;
3172 struct lov_mds_md_v1 *lmm;
3175 __u16 cur_entry_count;
3176 __u16 merge_entry_count;
3178 __u16 mirror_id = 0;
3185 merge_lcm = mbuf->lb_buf;
3186 if (mbuf->lb_len < sizeof(*merge_lcm))
3189 /* must be an existing layout from disk */
3190 if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
3193 merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
3195 /* do not allow to merge two mirrored files */
3196 if (le16_to_cpu(merge_lcm->lcm_mirror_count))
3199 /* verify the target buffer */
3200 rc = lod_get_lov_ea(env, lo);
3202 RETURN(rc ? : -ENODATA);
3204 cur_lcm = info->lti_ea_store;
3205 switch (le32_to_cpu(cur_lcm->lcm_magic)) {
3208 rc = lod_layout_convert(info);
3210 case LOV_MAGIC_COMP_V1:
3220 /* info->lti_ea_store could be reallocated in lod_layout_convert() */
3221 cur_lcm = info->lti_ea_store;
3222 cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
3224 /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
3225 mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
3226 if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
3229 /* size of new layout */
3230 size = le32_to_cpu(cur_lcm->lcm_size) +
3231 le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
3233 memset(buf, 0, sizeof(*buf));
3234 lu_buf_alloc(buf, size);
3235 if (buf->lb_buf == NULL)
3239 memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
3241 offset = sizeof(*lcm) +
3242 sizeof(*lcme) * (cur_entry_count + merge_entry_count);
3243 for (i = 0; i < cur_entry_count; i++) {
3244 struct lov_comp_md_entry_v1 *cur_lcme;
3246 lcme = &lcm->lcm_entries[i];
3247 cur_lcme = &cur_lcm->lcm_entries[i];
3249 lcme->lcme_offset = cpu_to_le32(offset);
3250 memcpy((char *)lcm + offset,
3251 (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
3252 le32_to_cpu(lcme->lcme_size));
3254 offset += le32_to_cpu(lcme->lcme_size);
3256 if (mirror_count == 1 &&
3257 mirror_id_of(le32_to_cpu(lcme->lcme_id)) == 0) {
3258 /* Add mirror from a non-flr file, create new mirror ID.
3259 * Otherwise, keep existing mirror's component ID, used
3260 * for mirror extension.
3262 id = pflr_id(1, i + 1);
3263 lcme->lcme_id = cpu_to_le32(id);
3266 id = max(le32_to_cpu(lcme->lcme_id), id);
3269 mirror_id = mirror_id_of(id) + 1;
3271 /* check if first entry in new layout is DOM */
3272 lmm = (struct lov_mds_md_v1 *)((char *)merge_lcm +
3273 merge_lcm->lcm_entries[0].lcme_offset);
3274 merge_has_dom = lov_pattern(le32_to_cpu(lmm->lmm_pattern)) ==
3277 for (i = 0; i < merge_entry_count; i++) {
3278 struct lov_comp_md_entry_v1 *merge_lcme;
3280 merge_lcme = &merge_lcm->lcm_entries[i];
3281 lcme = &lcm->lcm_entries[cur_entry_count + i];
3283 *lcme = *merge_lcme;
3284 lcme->lcme_offset = cpu_to_le32(offset);
3285 if (merge_has_dom && i == 0)
3286 lcme->lcme_flags |= cpu_to_le32(LCME_FL_STALE);
3288 id = pflr_id(mirror_id, i + 1);
3289 lcme->lcme_id = cpu_to_le32(id);
3291 memcpy((char *)lcm + offset,
3292 (char *)merge_lcm + le32_to_cpu(merge_lcme->lcme_offset),
3293 le32_to_cpu(lcme->lcme_size));
3295 offset += le32_to_cpu(lcme->lcme_size);
3298 /* fixup layout information */
3299 lod_obj_inc_layout_gen(lo);
3300 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3301 lcm->lcm_size = cpu_to_le32(size);
3302 lcm->lcm_entry_count = cpu_to_le16(cur_entry_count + merge_entry_count);
3303 lcm->lcm_mirror_count = cpu_to_le16(mirror_count);
3304 if ((le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK) == LCM_FL_NONE)
3305 lcm->lcm_flags = cpu_to_le32(LCM_FL_RDONLY);
3307 rc = lod_striping_reload(env, lo, buf);
3311 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), buf,
3312 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3320 * Split layouts, just set the LOVEA with the layout from mbuf.
3322 static int lod_declare_layout_split(const struct lu_env *env,
3323 struct dt_object *dt, const struct lu_buf *mbuf,
3326 struct lod_object *lo = lod_dt_obj(dt);
3327 struct lov_comp_md_v1 *lcm = mbuf->lb_buf;
3331 lod_obj_inc_layout_gen(lo);
3332 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3334 rc = lod_striping_reload(env, lo, mbuf);
3338 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), mbuf,
3339 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3344 * Implementation of dt_object_operations::do_declare_xattr_set.
3346 * \see dt_object_operations::do_declare_xattr_set() in the API description
3349 * the extension to the API:
3350 * - declaring LOVEA requests striping creation
3351 * - LU_XATTR_REPLACE means layout swap
3353 static int lod_declare_xattr_set(const struct lu_env *env,
3354 struct dt_object *dt,
3355 const struct lu_buf *buf,
3356 const char *name, int fl,
3359 struct dt_object *next = dt_object_child(dt);
3360 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3365 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
3366 if ((S_ISREG(mode) || mode == 0) &&
3367 !(fl & (LU_XATTR_REPLACE | LU_XATTR_MERGE | LU_XATTR_SPLIT)) &&
3368 (strcmp(name, XATTR_NAME_LOV) == 0 ||
3369 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
3371 * this is a request to create object's striping.
3373 * allow to declare predefined striping on a new (!mode) object
3374 * which is supposed to be replay of regular file creation
3375 * (when LOV setting is declared)
3377 * LU_XATTR_REPLACE is set to indicate a layout swap
3379 if (dt_object_exists(dt)) {
3380 rc = dt_attr_get(env, next, attr);
3384 memset(attr, 0, sizeof(*attr));
3385 attr->la_valid = LA_TYPE | LA_MODE;
3386 attr->la_mode = S_IFREG;
3388 rc = lod_declare_striped_create(env, dt, attr, buf, th);
3389 } else if (fl & LU_XATTR_MERGE) {
3390 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3391 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3392 rc = lod_declare_layout_merge(env, dt, buf, th);
3393 } else if (fl & LU_XATTR_SPLIT) {
3394 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3395 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3396 rc = lod_declare_layout_split(env, dt, buf, th);
3397 } else if (S_ISREG(mode) &&
3398 strlen(name) >= sizeof(XATTR_LUSTRE_LOV) + 3 &&
3399 allowed_lustre_lov(name)) {
3401 * this is a request to modify object's striping.
3402 * add/set/del component(s).
3404 if (!dt_object_exists(dt))
3407 rc = lod_declare_modify_layout(env, dt, name, buf, th);
3408 } else if (S_ISDIR(mode)) {
3409 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
3410 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3411 rc = lod_replace_parent_fid(env, dt, buf, th, true);
3413 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
3420 * Apply xattr changes to the object.
3422 * Applies xattr changes to the object and the stripes if the latter exist.
3424 * \param[in] env execution environment
3425 * \param[in] dt object
3426 * \param[in] buf buffer pointing to the new value of xattr
3427 * \param[in] name name of xattr
3428 * \param[in] fl flags
3429 * \param[in] th transaction handle
3431 * \retval 0 on success
3432 * \retval negative if failed
3434 static int lod_xattr_set_internal(const struct lu_env *env,
3435 struct dt_object *dt,
3436 const struct lu_buf *buf,
3437 const char *name, int fl,
3440 struct dt_object *next = dt_object_child(dt);
3441 struct lod_object *lo = lod_dt_obj(dt);
3446 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3447 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3450 /* Note: Do not set LinkEA on sub-stripes, otherwise
3451 * it will confuse the fid2path process(see mdt_path_current()).
3452 * The linkEA between master and sub-stripes is set in
3453 * lod_xattr_set_lmv(). */
3454 if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
3457 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3458 if (!lo->ldo_stripe[i])
3461 if (!dt_object_exists(lo->ldo_stripe[i]))
3464 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
3474 * Delete an extended attribute.
3476 * Deletes specified xattr from the object and the stripes if the latter exist.
3478 * \param[in] env execution environment
3479 * \param[in] dt object
3480 * \param[in] name name of xattr
3481 * \param[in] th transaction handle
3483 * \retval 0 on success
3484 * \retval negative if failed
3486 static int lod_xattr_del_internal(const struct lu_env *env,
3487 struct dt_object *dt,
3488 const char *name, struct thandle *th)
3490 struct dt_object *next = dt_object_child(dt);
3491 struct lod_object *lo = lod_dt_obj(dt);
3496 rc = lod_sub_xattr_del(env, next, name, th);
3497 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3500 if (lo->ldo_dir_stripe_count == 0)
3503 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3504 LASSERT(lo->ldo_stripe[i]);
3506 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3515 * Set default striping on a directory.
3517 * Sets specified striping on a directory object unless it matches the default
3518 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3519 * EA. This striping will be used when regular file is being created in this
3522 * \param[in] env execution environment
3523 * \param[in] dt the striped object
3524 * \param[in] buf buffer with the striping
3525 * \param[in] name name of EA
3526 * \param[in] fl xattr flag (see OSD API description)
3527 * \param[in] th transaction handle
3529 * \retval 0 on success
3530 * \retval negative if failed
3532 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
3533 struct dt_object *dt,
3534 const struct lu_buf *buf,
3535 const char *name, int fl,
3538 struct lov_user_md_v1 *lum;
3539 struct lov_user_md_v3 *v3 = NULL;
3540 const char *pool_name = NULL;
3545 LASSERT(buf != NULL && buf->lb_buf != NULL);
3548 switch (lum->lmm_magic) {
3549 case LOV_USER_MAGIC_SPECIFIC:
3550 case LOV_USER_MAGIC_V3:
3552 if (v3->lmm_pool_name[0] != '\0')
3553 pool_name = v3->lmm_pool_name;
3555 case LOV_USER_MAGIC_V1:
3556 /* if { size, offset, count } = { 0, -1, 0 } and no pool
3557 * (i.e. all default values specified) then delete default
3558 * striping from dir. */
3560 "set default striping: sz %u # %u offset %d %s %s\n",
3561 (unsigned)lum->lmm_stripe_size,
3562 (unsigned)lum->lmm_stripe_count,
3563 (int)lum->lmm_stripe_offset,
3564 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
3566 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
3567 lum->lmm_stripe_count,
3568 lum->lmm_stripe_offset,
3571 case LOV_USER_MAGIC_COMP_V1:
3573 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lum;
3574 struct lov_comp_md_entry_v1 *lcme;
3577 comp_cnt = le16_to_cpu(lcm->lcm_entry_count);
3578 for (i = 0; i < comp_cnt; i++) {
3579 lcme = &lcm->lcm_entries[i];
3580 if (lcme->lcme_flags & cpu_to_le32(LCME_FL_EXTENSION)) {
3581 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_SEL);
3590 CERROR("Invalid magic %x\n", lum->lmm_magic);
3595 rc = lod_xattr_del_internal(env, dt, name, th);
3599 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3606 * Set default striping on a directory object.
3608 * Sets specified striping on a directory object unless it matches the default
3609 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3610 * EA. This striping will be used when a new directory is being created in the
3613 * \param[in] env execution environment
3614 * \param[in] dt the striped object
3615 * \param[in] buf buffer with the striping
3616 * \param[in] name name of EA
3617 * \param[in] fl xattr flag (see OSD API description)
3618 * \param[in] th transaction handle
3620 * \retval 0 on success
3621 * \retval negative if failed
3623 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
3624 struct dt_object *dt,
3625 const struct lu_buf *buf,
3626 const char *name, int fl,
3629 struct lmv_user_md_v1 *lum;
3634 LASSERT(buf != NULL && buf->lb_buf != NULL);
3638 "set default stripe_count # %u stripe_offset %d hash %u\n",
3639 le32_to_cpu(lum->lum_stripe_count),
3640 (int)le32_to_cpu(lum->lum_stripe_offset),
3641 le32_to_cpu(lum->lum_hash_type));
3643 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
3644 le32_to_cpu(lum->lum_stripe_offset)) &&
3645 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
3646 rc = lod_xattr_del_internal(env, dt, name, th);
3650 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3659 * Turn directory into a striped directory.
3661 * During replay the client sends the striping created before MDT
3662 * failure, then the layer above LOD sends this defined striping
3663 * using ->do_xattr_set(), so LOD uses this method to replay creation
3664 * of the stripes. Notice the original information for the striping
3665 * (#stripes, FIDs, etc) was transferred in declare path.
3667 * \param[in] env execution environment
3668 * \param[in] dt the striped object
3669 * \param[in] buf not used currently
3670 * \param[in] name not used currently
3671 * \param[in] fl xattr flag (see OSD API description)
3672 * \param[in] th transaction handle
3674 * \retval 0 on success
3675 * \retval negative if failed
3677 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
3678 const struct lu_buf *buf, const char *name,
3679 int fl, struct thandle *th)
3681 struct lod_object *lo = lod_dt_obj(dt);
3682 struct lod_thread_info *info = lod_env_info(env);
3683 struct lu_attr *attr = &info->lti_attr;
3684 struct dt_object_format *dof = &info->lti_format;
3685 struct lu_buf lmv_buf;
3686 struct lu_buf slave_lmv_buf;
3687 struct lmv_mds_md_v1 *lmm;
3688 struct lmv_mds_md_v1 *slave_lmm = NULL;
3689 struct dt_insert_rec *rec = &info->lti_dt_rec;
3694 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3697 /* The stripes are supposed to be allocated in declare phase,
3698 * if there are no stripes being allocated, it will skip */
3699 if (lo->ldo_dir_stripe_count == 0) {
3700 if (lo->ldo_dir_is_foreign) {
3701 rc = lod_sub_xattr_set(env, dt_object_child(dt), buf,
3702 XATTR_NAME_LMV, fl, th);
3709 rc = dt_attr_get(env, dt_object_child(dt), attr);
3713 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
3714 LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
3715 dof->dof_type = DFT_DIR;
3717 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
3720 lmm = lmv_buf.lb_buf;
3722 OBD_ALLOC_PTR(slave_lmm);
3723 if (slave_lmm == NULL)
3726 lod_prep_slave_lmv_md(slave_lmm, lmm);
3727 slave_lmv_buf.lb_buf = slave_lmm;
3728 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
3730 rec->rec_type = S_IFDIR;
3731 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3732 struct dt_object *dto = lo->ldo_stripe[i];
3733 char *stripe_name = info->lti_key;
3734 struct lu_name *sname;
3735 struct linkea_data ldata = { NULL };
3736 struct lu_buf linkea_buf;
3738 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
3742 /* fail a remote stripe creation */
3743 if (i && OBD_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_CREATE))
3746 /* if it's source stripe of migrating directory, don't create */
3747 if (!((lo->ldo_dir_hash_type & LMV_HASH_FLAG_MIGRATION) &&
3748 i >= lo->ldo_dir_migrate_offset)) {
3749 dt_write_lock(env, dto, DT_TGT_CHILD);
3750 rc = lod_sub_create(env, dto, attr, NULL, dof, th);
3752 dt_write_unlock(env, dto);
3756 rc = lod_sub_ref_add(env, dto, th);
3757 dt_write_unlock(env, dto);
3761 rec->rec_fid = lu_object_fid(&dto->do_lu);
3762 rc = lod_sub_insert(env, dto,
3763 (const struct dt_rec *)rec,
3764 (const struct dt_key *)dot, th);
3769 rec->rec_fid = lu_object_fid(&dt->do_lu);
3770 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
3771 (const struct dt_key *)dotdot, th);
3775 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
3776 cfs_fail_val != i) {
3777 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
3779 slave_lmm->lmv_master_mdt_index =
3782 slave_lmm->lmv_master_mdt_index =
3785 rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
3786 XATTR_NAME_LMV, 0, th);
3791 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
3793 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3794 PFID(lu_object_fid(&dto->do_lu)), i + 1);
3796 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3797 PFID(lu_object_fid(&dto->do_lu)), i);
3799 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
3800 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
3801 sname, lu_object_fid(&dt->do_lu));
3805 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
3806 linkea_buf.lb_len = ldata.ld_leh->leh_len;
3807 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
3808 XATTR_NAME_LINK, 0, th);
3812 rec->rec_fid = lu_object_fid(&dto->do_lu);
3813 rc = lod_sub_insert(env, dt_object_child(dt),
3814 (const struct dt_rec *)rec,
3815 (const struct dt_key *)stripe_name, th);
3819 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
3824 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
3825 rc = lod_sub_xattr_set(env, dt_object_child(dt),
3826 &lmv_buf, XATTR_NAME_LMV, fl, th);
3828 if (slave_lmm != NULL)
3829 OBD_FREE_PTR(slave_lmm);
3835 * Helper function to declare/execute creation of a striped directory
3837 * Called in declare/create object path, prepare striping for a directory
3838 * and prepare defaults data striping for the objects to be created in
3839 * that directory. Notice the function calls "declaration" or "execution"
3840 * methods depending on \a declare param. This is a consequence of the
3841 * current approach while we don't have natural distributed transactions:
3842 * we basically execute non-local updates in the declare phase. So, the
3843 * arguments for the both phases are the same and this is the reason for
3844 * this function to exist.
3846 * \param[in] env execution environment
3847 * \param[in] dt object
3848 * \param[in] attr attributes the stripes will be created with
3849 * \param[in] lmu lmv_user_md if MDT indices are specified
3850 * \param[in] dof format of stripes (see OSD API description)
3851 * \param[in] th transaction handle
3852 * \param[in] declare where to call "declare" or "execute" methods
3854 * \retval 0 on success
3855 * \retval negative if failed
3857 static int lod_dir_striping_create_internal(const struct lu_env *env,
3858 struct dt_object *dt,
3859 struct lu_attr *attr,
3860 const struct lu_buf *lmu,
3861 struct dt_object_format *dof,
3865 struct lod_thread_info *info = lod_env_info(env);
3866 struct lod_object *lo = lod_dt_obj(dt);
3867 const struct lod_default_striping *lds = lo->ldo_def_striping;
3871 LASSERT(ergo(lds != NULL,
3872 lds->lds_def_striping_set ||
3873 lds->lds_dir_def_striping_set));
3875 if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
3876 lo->ldo_dir_stripe_offset)) {
3878 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3879 int stripe_count = lo->ldo_dir_stripe_count;
3881 if (info->lti_ea_store_size < sizeof(*v1)) {
3882 rc = lod_ea_store_resize(info, sizeof(*v1));
3885 v1 = info->lti_ea_store;
3888 memset(v1, 0, sizeof(*v1));
3889 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3890 v1->lum_stripe_count = cpu_to_le32(stripe_count);
3891 v1->lum_stripe_offset =
3892 cpu_to_le32(lo->ldo_dir_stripe_offset);
3894 info->lti_buf.lb_buf = v1;
3895 info->lti_buf.lb_len = sizeof(*v1);
3896 lmu = &info->lti_buf;
3900 rc = lod_declare_xattr_set_lmv(env, dt, attr, lmu, dof,
3903 rc = lod_xattr_set_lmv(env, dt, lmu, XATTR_NAME_LMV, 0,
3908 /* foreign LMV EA case */
3910 struct lmv_foreign_md *lfm = lmu->lb_buf;
3912 if (lfm->lfm_magic == LMV_MAGIC_FOREIGN) {
3913 rc = lod_declare_xattr_set_lmv(env, dt, attr,
3917 if (lo->ldo_dir_is_foreign) {
3918 LASSERT(lo->ldo_foreign_lmv != NULL &&
3919 lo->ldo_foreign_lmv_size > 0);
3920 info->lti_buf.lb_buf = lo->ldo_foreign_lmv;
3921 info->lti_buf.lb_len = lo->ldo_foreign_lmv_size;
3922 lmu = &info->lti_buf;
3923 rc = lod_xattr_set_lmv(env, dt, lmu,
3924 XATTR_NAME_LMV, 0, th);
3929 /* Transfer default LMV striping from the parent */
3930 if (lds != NULL && lds->lds_dir_def_striping_set &&
3931 !(LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
3932 lds->lds_dir_def_stripe_offset) &&
3933 le32_to_cpu(lds->lds_dir_def_hash_type) !=
3934 LMV_HASH_TYPE_UNKNOWN)) {
3935 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3937 if (info->lti_ea_store_size < sizeof(*v1)) {
3938 rc = lod_ea_store_resize(info, sizeof(*v1));
3941 v1 = info->lti_ea_store;
3944 memset(v1, 0, sizeof(*v1));
3945 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3946 v1->lum_stripe_count =
3947 cpu_to_le32(lds->lds_dir_def_stripe_count);
3948 v1->lum_stripe_offset =
3949 cpu_to_le32(lds->lds_dir_def_stripe_offset);
3951 cpu_to_le32(lds->lds_dir_def_hash_type);
3953 info->lti_buf.lb_buf = v1;
3954 info->lti_buf.lb_len = sizeof(*v1);
3956 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3957 XATTR_NAME_DEFAULT_LMV,
3960 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
3962 XATTR_NAME_DEFAULT_LMV, 0,
3968 /* Transfer default LOV striping from the parent */
3969 if (lds != NULL && lds->lds_def_striping_set &&
3970 lds->lds_def_comp_cnt != 0) {
3971 struct lov_mds_md *lmm;
3972 int lmm_size = lod_comp_md_size(lo, true);
3974 if (info->lti_ea_store_size < lmm_size) {
3975 rc = lod_ea_store_resize(info, lmm_size);
3979 lmm = info->lti_ea_store;
3981 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
3985 info->lti_buf.lb_buf = lmm;
3986 info->lti_buf.lb_len = lmm_size;
3989 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3990 XATTR_NAME_LOV, 0, th);
3992 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
3993 XATTR_NAME_LOV, 0, th);
4001 static int lod_declare_dir_striping_create(const struct lu_env *env,
4002 struct dt_object *dt,
4003 struct lu_attr *attr,
4005 struct dt_object_format *dof,
4008 return lod_dir_striping_create_internal(env, dt, attr, lmu, dof, th,
4012 static int lod_dir_striping_create(const struct lu_env *env,
4013 struct dt_object *dt,
4014 struct lu_attr *attr,
4015 struct dt_object_format *dof,
4018 return lod_dir_striping_create_internal(env, dt, attr, NULL, dof, th,
4023 * Make LOV EA for striped object.
4025 * Generate striping information and store it in the LOV EA of the given
4026 * object. The caller must ensure nobody else is calling the function
4027 * against the object concurrently. The transaction must be started.
4028 * FLDB service must be running as well; it's used to map FID to the target,
4029 * which is stored in LOV EA.
4031 * \param[in] env execution environment for this thread
4032 * \param[in] lo LOD object
4033 * \param[in] th transaction handle
4035 * \retval 0 if LOV EA is stored successfully
4036 * \retval negative error number on failure
4038 static int lod_generate_and_set_lovea(const struct lu_env *env,
4039 struct lod_object *lo,
4042 struct lod_thread_info *info = lod_env_info(env);
4043 struct dt_object *next = dt_object_child(&lo->ldo_obj);
4044 struct lov_mds_md_v1 *lmm;
4050 if (lo->ldo_comp_cnt == 0 && !lo->ldo_is_foreign) {
4051 lod_striping_free(env, lo);
4052 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
4056 lmm_size = lod_comp_md_size(lo, false);
4057 if (info->lti_ea_store_size < lmm_size) {
4058 rc = lod_ea_store_resize(info, lmm_size);
4062 lmm = info->lti_ea_store;
4064 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
4068 info->lti_buf.lb_buf = lmm;
4069 info->lti_buf.lb_len = lmm_size;
4070 rc = lod_sub_xattr_set(env, next, &info->lti_buf,
4071 XATTR_NAME_LOV, 0, th);
4075 static __u32 lod_gen_component_id(struct lod_object *lo,
4076 int mirror_id, int comp_idx);
4079 * Repeat an existing component
4081 * Creates a new layout by replicating an existing component. Uses striping
4082 * policy from previous component as a template for the striping for the new
4085 * New component starts with zero length, will be extended (or removed) before
4086 * returning layout to client.
4088 * NB: Reallocates layout components array (lo->ldo_comp_entries), invalidating
4089 * any pre-existing pointers to components. Handle with care.
4091 * \param[in] env execution environment for this thread
4092 * \param[in,out] lo object to update the layout of
4093 * \param[in] index index of component to copy
4095 * \retval 0 on success
4096 * \retval negative errno on error
4098 static int lod_layout_repeat_comp(const struct lu_env *env,
4099 struct lod_object *lo, int index)
4101 struct lod_layout_component *lod_comp;
4102 struct lod_layout_component *new_comp = NULL;
4103 struct lod_layout_component *comp_array;
4104 int rc = 0, i, new_cnt = lo->ldo_comp_cnt + 1;
4109 lod_comp = &lo->ldo_comp_entries[index];
4110 LASSERT(lod_comp_inited(lod_comp) && lod_comp->llc_id != LCME_ID_INVAL);
4112 CDEBUG(D_LAYOUT, "repeating component %d\n", index);
4114 OBD_ALLOC(comp_array, sizeof(*comp_array) * new_cnt);
4115 if (comp_array == NULL)
4116 GOTO(out, rc = -ENOMEM);
4118 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4119 memcpy(&comp_array[i + offset], &lo->ldo_comp_entries[i],
4120 sizeof(*comp_array));
4122 /* Duplicate this component in to the next slot */
4124 new_comp = &comp_array[i + 1];
4125 memcpy(&comp_array[i + 1], &lo->ldo_comp_entries[i],
4126 sizeof(*comp_array));
4127 /* We must now skip this new component when copying */
4132 /* Set up copied component */
4133 new_comp->llc_flags &= ~LCME_FL_INIT;
4134 new_comp->llc_stripe = NULL;
4135 new_comp->llc_stripes_allocated = 0;
4136 new_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4137 /* for uninstantiated components, layout gen stores default stripe
4139 new_comp->llc_layout_gen = lod_comp->llc_stripe_offset;
4140 /* This makes the repeated component zero-length, placed at the end of
4141 * the preceding component */
4142 new_comp->llc_extent.e_start = new_comp->llc_extent.e_end;
4143 new_comp->llc_timestamp = lod_comp->llc_timestamp;
4144 new_comp->llc_pool = NULL;
4146 rc = lod_set_pool(&new_comp->llc_pool, lod_comp->llc_pool);
4150 if (new_comp->llc_ostlist.op_array) {
4151 __u32 *op_array = NULL;
4153 OBD_ALLOC(op_array, new_comp->llc_ostlist.op_size);
4155 GOTO(out, rc = -ENOMEM);
4156 memcpy(op_array, &new_comp->llc_ostlist.op_array,
4157 new_comp->llc_ostlist.op_size);
4158 new_comp->llc_ostlist.op_array = op_array;
4161 OBD_FREE(lo->ldo_comp_entries,
4162 sizeof(*comp_array) * lo->ldo_comp_cnt);
4163 lo->ldo_comp_entries = comp_array;
4164 lo->ldo_comp_cnt = new_cnt;
4166 /* Generate an id for the new component */
4167 mirror_id = mirror_id_of(new_comp->llc_id);
4168 new_comp->llc_id = LCME_ID_INVAL;
4169 new_comp->llc_id = lod_gen_component_id(lo, mirror_id, index + 1);
4170 if (new_comp->llc_id == LCME_ID_INVAL)
4171 GOTO(out, rc = -ERANGE);
4176 OBD_FREE(comp_array, sizeof(*comp_array) * new_cnt);
4181 static int lod_layout_data_init(struct lod_thread_info *info, __u32 comp_cnt)
4185 /* clear memory region that will be used for layout change */
4186 memset(&info->lti_layout_attr, 0, sizeof(struct lu_attr));
4187 info->lti_count = 0;
4189 if (info->lti_comp_size >= comp_cnt)
4192 if (info->lti_comp_size > 0) {
4193 OBD_FREE(info->lti_comp_idx,
4194 info->lti_comp_size * sizeof(__u32));
4195 info->lti_comp_size = 0;
4198 OBD_ALLOC(info->lti_comp_idx, comp_cnt * sizeof(__u32));
4199 if (!info->lti_comp_idx)
4202 info->lti_comp_size = comp_cnt;
4207 * Prepare new layout minus deleted components
4209 * Removes components marked for deletion (LCME_ID_INVAL) by copying to a new
4210 * layout and skipping those components. Removes stripe objects if any exist.
4213 * Reallocates layout components array (lo->ldo_comp_entries), invalidating
4214 * any pre-existing pointers to components.
4216 * Caller is responsible for updating mirror end (ldo_mirror[].lme_end).
4218 * \param[in] env execution environment for this thread
4219 * \param[in,out] lo object to update the layout of
4220 * \param[in] th transaction handle for this operation
4222 * \retval # of components deleted
4223 * \retval negative errno on error
4225 static int lod_layout_del_prep_layout(const struct lu_env *env,
4226 struct lod_object *lo,
4229 struct lod_layout_component *lod_comp;
4230 struct lod_thread_info *info = lod_env_info(env);
4231 int rc = 0, i, j, deleted = 0;
4235 LASSERT(lo->ldo_is_composite);
4236 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
4238 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
4242 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4243 lod_comp = &lo->ldo_comp_entries[i];
4245 if (lod_comp->llc_id != LCME_ID_INVAL) {
4246 /* Build array of things to keep */
4247 info->lti_comp_idx[info->lti_count++] = i;
4251 lod_obj_set_pool(lo, i, NULL);
4252 if (lod_comp->llc_ostlist.op_array) {
4253 OBD_FREE(lod_comp->llc_ostlist.op_array,
4254 lod_comp->llc_ostlist.op_size);
4255 lod_comp->llc_ostlist.op_array = NULL;
4256 lod_comp->llc_ostlist.op_size = 0;
4260 CDEBUG(D_LAYOUT, "deleting comp %d, left %d\n", i,
4261 lo->ldo_comp_cnt - deleted);
4263 /* No striping info for this component */
4264 if (lod_comp->llc_stripe == NULL)
4267 LASSERT(lod_comp->llc_stripe_count > 0);
4268 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4269 struct dt_object *obj = lod_comp->llc_stripe[j];
4274 /* components which are not init have no sub objects
4276 if (lod_comp_inited(lod_comp)) {
4277 rc = lod_sub_destroy(env, obj, th);
4282 lu_object_put(env, &obj->do_lu);
4283 lod_comp->llc_stripe[j] = NULL;
4285 OBD_FREE(lod_comp->llc_stripe, sizeof(*lod_comp->llc_stripe) *
4286 lod_comp->llc_stripes_allocated);
4287 lod_comp->llc_stripe = NULL;
4288 OBD_FREE(lod_comp->llc_ost_indices,
4289 sizeof(__u32) * lod_comp->llc_stripes_allocated);
4290 lod_comp->llc_ost_indices = NULL;
4291 lod_comp->llc_stripes_allocated = 0;
4294 /* info->lti_count has the amount of left components */
4295 LASSERTF(info->lti_count >= 0 && info->lti_count < lo->ldo_comp_cnt,
4296 "left = %d, lo->ldo_comp_cnt %d\n", (int)info->lti_count,
4297 (int)lo->ldo_comp_cnt);
4299 if (info->lti_count > 0) {
4300 struct lod_layout_component *comp_array;
4302 OBD_ALLOC(comp_array, sizeof(*comp_array) * info->lti_count);
4303 if (comp_array == NULL)
4304 GOTO(out, rc = -ENOMEM);
4306 for (i = 0; i < info->lti_count; i++) {
4307 memcpy(&comp_array[i],
4308 &lo->ldo_comp_entries[info->lti_comp_idx[i]],
4309 sizeof(*comp_array));
4312 OBD_FREE(lo->ldo_comp_entries,
4313 sizeof(*comp_array) * lo->ldo_comp_cnt);
4314 lo->ldo_comp_entries = comp_array;
4315 lo->ldo_comp_cnt = info->lti_count;
4317 lod_free_comp_entries(lo);
4322 return rc ? rc : deleted;
4326 * Delete layout component(s)
4328 * This function sets up the layout data in the env and does the setattrs
4329 * required to write out the new layout. The layout itself is modified in
4330 * lod_layout_del_prep_layout.
4332 * \param[in] env execution environment for this thread
4333 * \param[in] dt object
4334 * \param[in] th transaction handle
4336 * \retval 0 on success
4337 * \retval negative error number on failure
4339 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
4342 struct lod_object *lo = lod_dt_obj(dt);
4343 struct dt_object *next = dt_object_child(dt);
4344 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4347 LASSERT(lo->ldo_mirror_count == 1);
4349 rc = lod_layout_del_prep_layout(env, lo, th);
4353 /* Only do this if we didn't delete all components */
4354 if (lo->ldo_comp_cnt > 0) {
4355 lo->ldo_mirrors[0].lme_end = lo->ldo_comp_cnt - 1;
4356 lod_obj_inc_layout_gen(lo);
4359 LASSERT(dt_object_exists(dt));
4360 rc = dt_attr_get(env, next, attr);
4364 if (attr->la_size > 0) {
4366 attr->la_valid = LA_SIZE;
4367 rc = lod_sub_attr_set(env, next, attr, th);
4372 rc = lod_generate_and_set_lovea(env, lo, th);
4376 lod_striping_free(env, lo);
4381 static int lod_get_default_lov_striping(const struct lu_env *env,
4382 struct lod_object *lo,
4383 struct lod_default_striping *lds,
4384 struct dt_allocation_hint *ah);
4386 * Implementation of dt_object_operations::do_xattr_set.
4388 * Sets specified extended attribute on the object. Three types of EAs are
4390 * LOV EA - stores striping for a regular file or default striping (when set
4392 * LMV EA - stores a marker for the striped directories
4393 * DMV EA - stores default directory striping
4395 * When striping is applied to a non-striped existing object (this is called
4396 * late striping), then LOD notices the caller wants to turn the object into a
4397 * striped one. The stripe objects are created and appropriate EA is set:
4398 * LOV EA storing all the stripes directly or LMV EA storing just a small header
4399 * with striping configuration.
4401 * \see dt_object_operations::do_xattr_set() in the API description for details.
4403 static int lod_xattr_set(const struct lu_env *env,
4404 struct dt_object *dt, const struct lu_buf *buf,
4405 const char *name, int fl, struct thandle *th)
4407 struct dt_object *next = dt_object_child(dt);
4412 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4413 !strcmp(name, XATTR_NAME_LMV)) {
4415 case LU_XATTR_CREATE:
4416 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
4419 case LU_XATTR_REPLACE:
4420 rc = lod_dir_layout_set(env, dt, buf, fl, th);
4427 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4428 strcmp(name, XATTR_NAME_LOV) == 0) {
4429 struct lod_default_striping *lds = lod_lds_buf_get(env);
4430 struct lov_user_md_v1 *v1 = buf->lb_buf;
4431 char pool[LOV_MAXPOOLNAME + 1];
4434 /* get existing striping config */
4435 rc = lod_get_default_lov_striping(env, lod_dt_obj(dt), lds,
4440 memset(pool, 0, sizeof(pool));
4441 if (lds->lds_def_striping_set == 1)
4442 lod_layout_get_pool(lds->lds_def_comp_entries,
4443 lds->lds_def_comp_cnt, pool,
4446 is_del = LOVEA_DELETE_VALUES(v1->lmm_stripe_size,
4447 v1->lmm_stripe_count,
4448 v1->lmm_stripe_offset,
4451 /* Retain the pool name if it is not given */
4452 if (v1->lmm_magic == LOV_USER_MAGIC_V1 && pool[0] != '\0' &&
4454 struct lod_thread_info *info = lod_env_info(env);
4455 struct lov_user_md_v3 *v3 = info->lti_ea_store;
4457 memset(v3, 0, sizeof(*v3));
4458 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
4459 v3->lmm_pattern = cpu_to_le32(v1->lmm_pattern);
4460 v3->lmm_stripe_count =
4461 cpu_to_le32(v1->lmm_stripe_count);
4462 v3->lmm_stripe_offset =
4463 cpu_to_le32(v1->lmm_stripe_offset);
4464 v3->lmm_stripe_size = cpu_to_le32(v1->lmm_stripe_size);
4466 strlcpy(v3->lmm_pool_name, pool,
4467 sizeof(v3->lmm_pool_name));
4469 info->lti_buf.lb_buf = v3;
4470 info->lti_buf.lb_len = sizeof(*v3);
4471 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4474 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name,
4478 if (lds->lds_def_striping_set == 1 &&
4479 lds->lds_def_comp_entries != NULL)
4480 lod_free_def_comp_entries(lds);
4483 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4484 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
4486 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
4489 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4490 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4491 strcmp(name, XATTR_LUSTRE_LOV) == 0 ||
4492 allowed_lustre_lov(name))) {
4493 /* in case of lov EA swap, just set it
4494 * if not, it is a replay so check striping match what we
4495 * already have during req replay, declare_xattr_set()
4496 * defines striping, then create() does the work */
4497 if (fl & LU_XATTR_REPLACE) {
4498 /* free stripes, then update disk */
4499 lod_striping_free(env, lod_dt_obj(dt));
4501 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
4502 } else if (dt_object_remote(dt)) {
4503 /* This only happens during migration, see
4504 * mdd_migrate_create(), in which Master MDT will
4505 * create a remote target object, and only set
4506 * (migrating) stripe EA on the remote object,
4507 * and does not need creating each stripes. */
4508 rc = lod_sub_xattr_set(env, next, buf, name,
4510 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
4511 /* delete component(s) */
4512 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
4513 rc = lod_layout_del(env, dt, th);
4516 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
4517 * it's going to create create file with specified
4518 * component(s), the striping must have not being
4519 * cached in this case;
4521 * Otherwise, it's going to add/change component(s) to
4522 * an existing file, the striping must have been cached
4525 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
4526 !strcmp(name, XATTR_NAME_LOV),
4527 !lod_dt_obj(dt)->ldo_comp_cached));
4529 rc = lod_striped_create(env, dt, NULL, NULL, th);
4532 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
4533 rc = lod_replace_parent_fid(env, dt, buf, th, false);
4538 /* then all other xattr */
4539 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
4545 * Implementation of dt_object_operations::do_declare_xattr_del.
4547 * \see dt_object_operations::do_declare_xattr_del() in the API description
4550 static int lod_declare_xattr_del(const struct lu_env *env,
4551 struct dt_object *dt, const char *name,
4554 struct lod_object *lo = lod_dt_obj(dt);
4555 struct dt_object *next = dt_object_child(dt);
4560 rc = lod_sub_declare_xattr_del(env, next, name, th);
4564 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4567 /* NB: don't delete stripe LMV, because when we do this, normally we
4568 * will remove stripes, besides, if directory LMV is corrupt, this will
4569 * prevent deleting its LMV and fixing it (via LFSCK).
4571 if (!strcmp(name, XATTR_NAME_LMV))
4574 rc = lod_striping_load(env, lo);
4578 if (lo->ldo_dir_stripe_count == 0)
4581 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4582 struct dt_object *dto = lo->ldo_stripe[i];
4587 rc = lod_sub_declare_xattr_del(env, dto, name, th);
4596 * Implementation of dt_object_operations::do_xattr_del.
4598 * If EA storing a regular striping is being deleted, then release
4599 * all the references to the stripe objects in core.
4601 * \see dt_object_operations::do_xattr_del() in the API description for details.
4603 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
4604 const char *name, struct thandle *th)
4606 struct dt_object *next = dt_object_child(dt);
4607 struct lod_object *lo = lod_dt_obj(dt);
4612 if (!strcmp(name, XATTR_NAME_LOV) || !strcmp(name, XATTR_NAME_LMV))
4613 lod_striping_free(env, lod_dt_obj(dt));
4615 rc = lod_sub_xattr_del(env, next, name, th);
4616 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
4619 if (!strcmp(name, XATTR_NAME_LMV))
4622 if (lo->ldo_dir_stripe_count == 0)
4625 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4626 struct dt_object *dto = lo->ldo_stripe[i];
4631 rc = lod_sub_xattr_del(env, dto, name, th);
4640 * Implementation of dt_object_operations::do_xattr_list.
4642 * \see dt_object_operations::do_xattr_list() in the API description
4645 static int lod_xattr_list(const struct lu_env *env,
4646 struct dt_object *dt, const struct lu_buf *buf)
4648 return dt_xattr_list(env, dt_object_child(dt), buf);
4651 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
4653 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
4657 * Copy OST list from layout provided by user.
4659 * \param[in] lod_comp layout_component to be filled
4660 * \param[in] v3 LOV EA V3 user data
4662 * \retval 0 on success
4663 * \retval negative if failed
4665 int lod_comp_copy_ost_lists(struct lod_layout_component *lod_comp,
4666 struct lov_user_md_v3 *v3)
4672 if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
4673 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
4675 if (lod_comp->llc_ostlist.op_array) {
4676 if (lod_comp->llc_ostlist.op_size >=
4677 v3->lmm_stripe_count * sizeof(__u32)) {
4678 lod_comp->llc_ostlist.op_count =
4679 v3->lmm_stripe_count;
4682 OBD_FREE(lod_comp->llc_ostlist.op_array,
4683 lod_comp->llc_ostlist.op_size);
4686 /* copy ost list from lmm */
4687 lod_comp->llc_ostlist.op_count = v3->lmm_stripe_count;
4688 lod_comp->llc_ostlist.op_size = v3->lmm_stripe_count * sizeof(__u32);
4689 OBD_ALLOC(lod_comp->llc_ostlist.op_array,
4690 lod_comp->llc_ostlist.op_size);
4691 if (!lod_comp->llc_ostlist.op_array)
4694 for (j = 0; j < v3->lmm_stripe_count; j++) {
4695 lod_comp->llc_ostlist.op_array[j] =
4696 v3->lmm_objects[j].l_ost_idx;
4704 * Get default striping.
4706 * \param[in] env execution environment
4707 * \param[in] lo object
4708 * \param[out] lds default striping
4710 * \retval 0 on success
4711 * \retval negative if failed
4713 static int lod_get_default_lov_striping(const struct lu_env *env,
4714 struct lod_object *lo,
4715 struct lod_default_striping *lds,
4716 struct dt_allocation_hint *ah)
4718 struct lod_thread_info *info = lod_env_info(env);
4719 struct lov_user_md_v1 *v1 = NULL;
4720 struct lov_user_md_v3 *v3 = NULL;
4721 struct lov_comp_md_v1 *comp_v1 = NULL;
4729 rc = lod_get_lov_ea(env, lo);
4733 if (rc < (typeof(rc))sizeof(struct lov_user_md))
4736 v1 = info->lti_ea_store;
4737 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
4738 lustre_swab_lov_user_md_v1(v1);
4739 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
4740 v3 = (struct lov_user_md_v3 *)v1;
4741 lustre_swab_lov_user_md_v3(v3);
4742 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_SPECIFIC)) {
4743 v3 = (struct lov_user_md_v3 *)v1;
4744 lustre_swab_lov_user_md_v3(v3);
4745 lustre_swab_lov_user_md_objects(v3->lmm_objects,
4746 v3->lmm_stripe_count);
4747 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1) ||
4748 v1->lmm_magic == __swab32(LOV_USER_MAGIC_SEL)) {
4749 comp_v1 = (struct lov_comp_md_v1 *)v1;
4750 lustre_swab_lov_comp_md_v1(comp_v1);
4753 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
4754 v1->lmm_magic != LOV_MAGIC_COMP_V1 &&
4755 v1->lmm_magic != LOV_MAGIC_SEL &&
4756 v1->lmm_magic != LOV_USER_MAGIC_SPECIFIC)
4759 if ((v1->lmm_magic == LOV_MAGIC_COMP_V1 ||
4760 v1->lmm_magic == LOV_MAGIC_SEL) &&
4761 !(ah && ah->dah_append_stripes)) {
4762 comp_v1 = (struct lov_comp_md_v1 *)v1;
4763 comp_cnt = comp_v1->lcm_entry_count;
4766 mirror_cnt = comp_v1->lcm_mirror_count + 1;
4774 /* realloc default comp entries if necessary */
4775 rc = lod_def_striping_comp_resize(lds, comp_cnt);
4779 lds->lds_def_comp_cnt = comp_cnt;
4780 lds->lds_def_striping_is_composite = composite;
4781 lds->lds_def_mirror_cnt = mirror_cnt;
4783 for (i = 0; i < comp_cnt; i++) {
4784 struct lod_layout_component *lod_comp;
4787 lod_comp = &lds->lds_def_comp_entries[i];
4789 * reset lod_comp values, llc_stripes is always NULL in
4790 * the default striping template, llc_pool will be reset
4793 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
4796 v1 = (struct lov_user_md *)((char *)comp_v1 +
4797 comp_v1->lcm_entries[i].lcme_offset);
4798 lod_comp->llc_extent =
4799 comp_v1->lcm_entries[i].lcme_extent;
4800 /* We only inherit certain flags from the layout */
4801 lod_comp->llc_flags =
4802 comp_v1->lcm_entries[i].lcme_flags &
4803 LCME_TEMPLATE_FLAGS;
4806 if (!lov_pattern_supported(v1->lmm_pattern) &&
4807 !(v1->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
4808 lod_free_def_comp_entries(lds);
4812 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d stripe_offset=%d append_stripes=%d\n",
4813 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
4814 (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
4815 (int)v1->lmm_stripe_offset,
4816 ah ? ah->dah_append_stripes : 0);
4818 if (ah && ah->dah_append_stripes)
4819 lod_comp->llc_stripe_count = ah->dah_append_stripes;
4821 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
4822 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
4823 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
4824 lod_comp->llc_pattern = v1->lmm_pattern;
4827 if (ah && ah->dah_append_pool && ah->dah_append_pool[0]) {
4828 pool = ah->dah_append_pool;
4829 } else if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
4830 /* XXX: sanity check here */
4831 v3 = (struct lov_user_md_v3 *) v1;
4832 if (v3->lmm_pool_name[0] != '\0')
4833 pool = v3->lmm_pool_name;
4835 lod_set_def_pool(lds, i, pool);
4836 if (v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
4837 v3 = (struct lov_user_md_v3 *)v1;
4838 rc = lod_comp_copy_ost_lists(lod_comp, v3);
4841 } else if (lod_comp->llc_ostlist.op_array &&
4842 lod_comp->llc_ostlist.op_count) {
4843 for (j = 0; j < lod_comp->llc_ostlist.op_count; j++)
4844 lod_comp->llc_ostlist.op_array[j] = -1;
4845 lod_comp->llc_ostlist.op_count = 0;
4849 lds->lds_def_striping_set = 1;
4854 * Get default directory striping.
4856 * \param[in] env execution environment
4857 * \param[in] lo object
4858 * \param[out] lds default striping
4860 * \retval 0 on success
4861 * \retval negative if failed
4863 static int lod_get_default_lmv_striping(const struct lu_env *env,
4864 struct lod_object *lo,
4865 struct lod_default_striping *lds)
4867 struct lmv_user_md *lmu;
4870 lds->lds_dir_def_striping_set = 0;
4872 rc = lod_get_default_lmv_ea(env, lo);
4876 if (rc >= (int)sizeof(*lmu)) {
4877 struct lod_thread_info *info = lod_env_info(env);
4879 lmu = info->lti_ea_store;
4881 lds->lds_dir_def_stripe_count =
4882 le32_to_cpu(lmu->lum_stripe_count);
4883 lds->lds_dir_def_stripe_offset =
4884 le32_to_cpu(lmu->lum_stripe_offset);
4885 lds->lds_dir_def_hash_type =
4886 le32_to_cpu(lmu->lum_hash_type);
4887 lds->lds_dir_def_striping_set = 1;
4894 * Get default striping in the object.
4896 * Get object default striping and default directory striping.
4898 * \param[in] env execution environment
4899 * \param[in] lo object
4900 * \param[out] lds default striping
4902 * \retval 0 on success
4903 * \retval negative if failed
4905 static int lod_get_default_striping(const struct lu_env *env,
4906 struct lod_object *lo,
4907 struct lod_default_striping *lds)
4911 rc = lod_get_default_lov_striping(env, lo, lds, NULL);
4912 rc1 = lod_get_default_lmv_striping(env, lo, lds);
4913 if (rc == 0 && rc1 < 0)
4920 * Apply default striping on object.
4922 * If object striping pattern is not set, set to the one in default striping.
4923 * The default striping is from parent or fs.
4925 * \param[in] lo new object
4926 * \param[in] lds default striping
4927 * \param[in] mode new object's mode
4929 static void lod_striping_from_default(struct lod_object *lo,
4930 const struct lod_default_striping *lds,
4933 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4936 if (lds->lds_def_striping_set && S_ISREG(mode)) {
4937 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
4939 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
4940 lds->lds_def_comp_cnt);
4944 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
4945 if (lds->lds_def_mirror_cnt > 1)
4946 lo->ldo_flr_state = LCM_FL_RDONLY;
4948 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4949 struct lod_layout_component *obj_comp =
4950 &lo->ldo_comp_entries[i];
4951 struct lod_layout_component *def_comp =
4952 &lds->lds_def_comp_entries[i];
4954 CDEBUG(D_LAYOUT, "Inherit from default: flags=%#x "
4955 "size=%hu nr=%u offset=%u pattern=%#x pool=%s\n",
4956 def_comp->llc_flags,
4957 def_comp->llc_stripe_size,
4958 def_comp->llc_stripe_count,
4959 def_comp->llc_stripe_offset,
4960 def_comp->llc_pattern,
4961 def_comp->llc_pool ?: "");
4963 *obj_comp = *def_comp;
4964 if (def_comp->llc_pool != NULL) {
4965 /* pointer was copied from def_comp */
4966 obj_comp->llc_pool = NULL;
4967 lod_obj_set_pool(lo, i, def_comp->llc_pool);
4971 if (def_comp->llc_ostlist.op_array &&
4972 def_comp->llc_ostlist.op_count) {
4973 OBD_ALLOC(obj_comp->llc_ostlist.op_array,
4974 obj_comp->llc_ostlist.op_size);
4975 if (!obj_comp->llc_ostlist.op_array)
4977 memcpy(obj_comp->llc_ostlist.op_array,
4978 def_comp->llc_ostlist.op_array,
4979 obj_comp->llc_ostlist.op_size);
4980 } else if (def_comp->llc_ostlist.op_array) {
4981 obj_comp->llc_ostlist.op_array = NULL;
4985 * Don't initialize these fields for plain layout
4986 * (v1/v3) here, they are inherited in the order of
4987 * 'parent' -> 'fs default (root)' -> 'global default
4988 * values for stripe_count & stripe_size'.
4990 * see lod_ah_init().
4992 if (!lo->ldo_is_composite)
4995 lod_adjust_stripe_info(obj_comp, desc, 0);
4997 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
4998 if (lo->ldo_dir_stripe_count == 0)
4999 lo->ldo_dir_stripe_count =
5000 lds->lds_dir_def_stripe_count;
5001 if (lo->ldo_dir_stripe_offset == -1)
5002 lo->ldo_dir_stripe_offset =
5003 lds->lds_dir_def_stripe_offset;
5004 if (lo->ldo_dir_hash_type == 0)
5005 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
5007 CDEBUG(D_LAYOUT, "striping from default dir: count:%hu, "
5008 "offset:%u, hash_type:%u\n",
5009 lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
5010 lo->ldo_dir_hash_type);
5014 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root,
5017 struct lod_layout_component *lod_comp;
5019 if (lo->ldo_comp_cnt == 0)
5022 if (lo->ldo_is_composite)
5025 lod_comp = &lo->ldo_comp_entries[0];
5027 if (lod_comp->llc_stripe_count <= 0 ||
5028 lod_comp->llc_stripe_size <= 0)
5031 if (from_root && (lod_comp->llc_pool == NULL ||
5032 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
5035 if (append_pool && append_pool[0])
5042 * Implementation of dt_object_operations::do_ah_init.
5044 * This method is used to make a decision on the striping configuration for the
5045 * object being created. It can be taken from the \a parent object if it exists,
5046 * or filesystem's default. The resulting configuration (number of stripes,
5047 * stripe size/offset, pool name, etc) is stored in the object itself and will
5048 * be used by the methods like ->doo_declare_create().
5050 * \see dt_object_operations::do_ah_init() in the API description for details.
5052 static void lod_ah_init(const struct lu_env *env,
5053 struct dt_allocation_hint *ah,
5054 struct dt_object *parent,
5055 struct dt_object *child,
5058 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
5059 struct lod_thread_info *info = lod_env_info(env);
5060 struct lod_default_striping *lds = lod_lds_buf_get(env);
5061 struct dt_object *nextp = NULL;
5062 struct dt_object *nextc;
5063 struct lod_object *lp = NULL;
5064 struct lod_object *lc;
5065 struct lov_desc *desc;
5066 struct lod_layout_component *lod_comp;
5072 if (ah->dah_append_stripes == -1)
5073 ah->dah_append_stripes =
5074 d->lod_ost_descs.ltd_lov_desc.ld_tgt_count;
5076 if (likely(parent)) {
5077 nextp = dt_object_child(parent);
5078 lp = lod_dt_obj(parent);
5081 nextc = dt_object_child(child);
5082 lc = lod_dt_obj(child);
5084 LASSERT(!lod_obj_is_striped(child));
5085 /* default layout template may have been set on the regular file
5086 * when this is called from mdd_create_data() */
5087 if (S_ISREG(child_mode))
5088 lod_free_comp_entries(lc);
5090 if (!dt_object_exists(nextc))
5091 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
5093 if (S_ISDIR(child_mode)) {
5094 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
5096 /* other default values are 0 */
5097 lc->ldo_dir_stripe_offset = -1;
5099 /* no default striping configuration is needed for
5102 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5103 le32_to_cpu(lum1->lum_magic) == LMV_MAGIC_FOREIGN) {
5104 lc->ldo_dir_is_foreign = true;
5105 /* keep stripe_count 0 and stripe_offset -1 */
5106 CDEBUG(D_INFO, "no default striping for foreign dir\n");
5111 * If parent object is not root directory,
5112 * then get default striping from parent object.
5114 if (likely(lp != NULL)) {
5115 lod_get_default_striping(env, lp, lds);
5117 /* inherit default striping except ROOT */
5118 if ((lds->lds_def_striping_set ||
5119 lds->lds_dir_def_striping_set) &&
5120 !fid_is_root(lod_object_fid(lp)))
5121 lc->ldo_def_striping = lds;
5124 /* It should always honour the specified stripes */
5125 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
5126 * will have old magic. In this case, we should ignore the
5127 * stripe count and try to create dir by default stripe.
5129 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5130 (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
5131 le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
5132 lc->ldo_dir_stripe_count =
5133 le32_to_cpu(lum1->lum_stripe_count);
5134 lc->ldo_dir_stripe_offset =
5135 le32_to_cpu(lum1->lum_stripe_offset);
5136 lc->ldo_dir_hash_type =
5137 le32_to_cpu(lum1->lum_hash_type);
5139 "set dirstripe: count %hu, offset %d, hash %u\n",
5140 lc->ldo_dir_stripe_count,
5141 (int)lc->ldo_dir_stripe_offset,
5142 lc->ldo_dir_hash_type);
5144 /* transfer defaults LMV to new directory */
5145 lod_striping_from_default(lc, lds, child_mode);
5147 /* set count 0 to create normal directory */
5148 if (lc->ldo_dir_stripe_count == 1)
5149 lc->ldo_dir_stripe_count = 0;
5152 /* shrink the stripe_count to the avaible MDT count */
5153 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
5154 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)) {
5155 lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
5156 if (lc->ldo_dir_stripe_count == 1)
5157 lc->ldo_dir_stripe_count = 0;
5160 if (!(lc->ldo_dir_hash_type & LMV_HASH_TYPE_MASK))
5161 lc->ldo_dir_hash_type |=
5162 d->lod_mdt_descs.ltd_lmv_desc.ld_pattern;
5164 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
5165 lc->ldo_dir_stripe_count,
5166 (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
5171 /* child object regular file*/
5173 if (!lod_object_will_be_striped(S_ISREG(child_mode),
5174 lu_object_fid(&child->do_lu)))
5177 /* If object is going to be striped over OSTs, transfer default
5178 * striping information to the child, so that we can use it
5179 * during declaration and creation.
5181 * Try from the parent first.
5183 if (likely(lp != NULL)) {
5184 rc = lod_get_default_lov_striping(env, lp, lds, ah);
5186 lod_striping_from_default(lc, lds, child_mode);
5189 /* Initialize lod_device::lod_md_root object reference */
5190 if (d->lod_md_root == NULL) {
5191 struct dt_object *root;
5192 struct lod_object *lroot;
5194 lu_root_fid(&info->lti_fid);
5195 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
5196 if (!IS_ERR(root)) {
5197 lroot = lod_dt_obj(root);
5199 spin_lock(&d->lod_lock);
5200 if (d->lod_md_root != NULL)
5201 dt_object_put(env, &d->lod_md_root->ldo_obj);
5202 d->lod_md_root = lroot;
5203 spin_unlock(&d->lod_lock);
5207 /* try inherit layout from the root object (fs default) when:
5208 * - parent does not have default layout; or
5209 * - parent has plain(v1/v3) default layout, and some attributes
5210 * are not specified in the default layout;
5212 if (d->lod_md_root != NULL &&
5213 lod_need_inherit_more(lc, true, ah->dah_append_pool)) {
5214 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds,
5218 if (lc->ldo_comp_cnt == 0) {
5219 lod_striping_from_default(lc, lds, child_mode);
5220 } else if (!lds->lds_def_striping_is_composite) {
5221 struct lod_layout_component *def_comp;
5223 LASSERT(!lc->ldo_is_composite);
5224 lod_comp = &lc->ldo_comp_entries[0];
5225 def_comp = &lds->lds_def_comp_entries[0];
5227 if (lod_comp->llc_stripe_count <= 0)
5228 lod_comp->llc_stripe_count =
5229 def_comp->llc_stripe_count;
5230 if (lod_comp->llc_stripe_size <= 0)
5231 lod_comp->llc_stripe_size =
5232 def_comp->llc_stripe_size;
5233 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT &&
5234 (!lod_comp->llc_pool || !lod_comp->llc_pool[0]))
5235 lod_comp->llc_stripe_offset =
5236 def_comp->llc_stripe_offset;
5237 if (lod_comp->llc_pool == NULL)
5238 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
5243 * fs default striping may not be explicitly set, or historically set
5244 * in config log, use them.
5246 if (lod_need_inherit_more(lc, false, ah->dah_append_pool)) {
5247 if (lc->ldo_comp_cnt == 0) {
5248 rc = lod_alloc_comp_entries(lc, 0, 1);
5250 /* fail to allocate memory, will create a
5251 * non-striped file. */
5253 lc->ldo_is_composite = 0;
5254 lod_comp = &lc->ldo_comp_entries[0];
5255 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
5257 LASSERT(!lc->ldo_is_composite);
5258 lod_comp = &lc->ldo_comp_entries[0];
5259 desc = &d->lod_ost_descs.ltd_lov_desc;
5260 lod_adjust_stripe_info(lod_comp, desc, ah->dah_append_stripes);
5261 if (ah->dah_append_pool && ah->dah_append_pool[0])
5262 lod_obj_set_pool(lc, 0, ah->dah_append_pool);
5269 * Size initialization on late striping.
5271 * Propagate the size of a truncated object to a deferred striping.
5272 * This function handles a special case when truncate was done on a
5273 * non-striped object and now while the striping is being created
5274 * we can't lose that size, so we have to propagate it to the stripes
5277 * \param[in] env execution environment
5278 * \param[in] dt object
5279 * \param[in] th transaction handle
5281 * \retval 0 on success
5282 * \retval negative if failed
5284 static int lod_declare_init_size(const struct lu_env *env,
5285 struct dt_object *dt, struct thandle *th)
5287 struct dt_object *next = dt_object_child(dt);
5288 struct lod_object *lo = lod_dt_obj(dt);
5289 struct dt_object **objects = NULL;
5290 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
5291 uint64_t size, offs;
5292 int i, rc, stripe, stripe_count = 0, stripe_size = 0;
5293 struct lu_extent size_ext;
5296 if (!lod_obj_is_striped(dt))
5299 rc = dt_attr_get(env, next, attr);
5300 LASSERT(attr->la_valid & LA_SIZE);
5304 size = attr->la_size;
5308 size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
5309 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5310 struct lod_layout_component *lod_comp;
5311 struct lu_extent *extent;
5313 lod_comp = &lo->ldo_comp_entries[i];
5315 if (lod_comp->llc_stripe == NULL)
5318 extent = &lod_comp->llc_extent;
5319 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
5320 if (!lo->ldo_is_composite ||
5321 lu_extent_is_overlapped(extent, &size_ext)) {
5322 objects = lod_comp->llc_stripe;
5323 stripe_count = lod_comp->llc_stripe_count;
5324 stripe_size = lod_comp->llc_stripe_size;
5327 if (stripe_count == 0)
5330 LASSERT(objects != NULL && stripe_size != 0);
5331 do_div(size, stripe_size);
5332 stripe = do_div(size, stripe_count);
5333 LASSERT(objects[stripe] != NULL);
5335 size = size * stripe_size;
5336 offs = attr->la_size;
5337 size += do_div(offs, stripe_size);
5339 attr->la_valid = LA_SIZE;
5340 attr->la_size = size;
5342 rc = lod_sub_declare_attr_set(env, objects[stripe],
5351 * Declare creation of striped object.
5353 * The function declares creation stripes for a regular object. The function
5354 * also declares whether the stripes will be created with non-zero size if
5355 * previously size was set non-zero on the master object. If object \a dt is
5356 * not local, then only fully defined striping can be applied in \a lovea.
5357 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
5360 * \param[in] env execution environment
5361 * \param[in] dt object
5362 * \param[in] attr attributes the stripes will be created with
5363 * \param[in] lovea a buffer containing striping description
5364 * \param[in] th transaction handle
5366 * \retval 0 on success
5367 * \retval negative if failed
5369 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
5370 struct lu_attr *attr,
5371 const struct lu_buf *lovea, struct thandle *th)
5373 struct lod_thread_info *info = lod_env_info(env);
5374 struct dt_object *next = dt_object_child(dt);
5375 struct lod_object *lo = lod_dt_obj(dt);
5379 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
5380 GOTO(out, rc = -ENOMEM);
5382 if (!dt_object_remote(next)) {
5383 /* choose OST and generate appropriate objects */
5384 rc = lod_prepare_create(env, lo, attr, lovea, th);
5389 * declare storage for striping data
5391 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5393 /* LOD can not choose OST objects for remote objects, i.e.
5394 * stripes must be ready before that. Right now, it can only
5395 * happen during migrate, i.e. migrate process needs to create
5396 * remote regular file (mdd_migrate_create), then the migrate
5397 * process will provide stripeEA. */
5398 LASSERT(lovea != NULL);
5399 info->lti_buf = *lovea;
5402 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
5403 XATTR_NAME_LOV, 0, th);
5408 * if striping is created with local object's size > 0,
5409 * we have to propagate this size to specific object
5410 * the case is possible only when local object was created previously
5412 if (dt_object_exists(next))
5413 rc = lod_declare_init_size(env, dt, th);
5416 /* failed to create striping or to set initial size, let's reset
5417 * config so that others don't get confused */
5419 lod_striping_free(env, lo);
5425 * Whether subdirectories under \a dt should be created on MDTs by space QoS
5427 * If LMV_HASH_FLAG_SPACE is set on directory default layout, its subdirectories
5428 * should be created on MDT by space QoS.
5430 * \param[in] env execution environment
5431 * \param[in] dev lu device
5432 * \param[in] dt object
5434 * \retval 1 if directory should create subdir by space usage
5436 * \retval -ev if failed
5438 static inline int dt_object_qos_mkdir(const struct lu_env *env,
5439 struct lu_device *dev,
5440 struct dt_object *dt)
5442 struct lod_thread_info *info = lod_env_info(env);
5443 struct lu_object *obj;
5444 struct lod_object *lo;
5445 struct lmv_user_md *lmu;
5448 obj = lu_object_find_slice(env, dev, lu_object_fid(&dt->do_lu), NULL);
5450 return PTR_ERR(obj);
5452 lo = lu2lod_obj(obj);
5454 rc = lod_get_default_lmv_ea(env, lo);
5455 dt_object_put(env, dt);
5459 if (rc < (int)sizeof(*lmu))
5462 lmu = info->lti_ea_store;
5463 return le32_to_cpu(lmu->lum_stripe_offset) == LMV_OFFSET_DEFAULT;
5467 * Implementation of dt_object_operations::do_declare_create.
5469 * The method declares creation of a new object. If the object will be striped,
5470 * then helper functions are called to find FIDs for the stripes, declare
5471 * creation of the stripes and declare initialization of the striping
5472 * information to be stored in the master object.
5474 * \see dt_object_operations::do_declare_create() in the API description
5477 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
5478 struct lu_attr *attr,
5479 struct dt_allocation_hint *hint,
5480 struct dt_object_format *dof, struct thandle *th)
5482 struct dt_object *next = dt_object_child(dt);
5483 struct lod_object *lo = lod_dt_obj(dt);
5492 * first of all, we declare creation of local object
5494 rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
5499 * it's lod_ah_init() that has decided the object will be striped
5501 if (dof->dof_type == DFT_REGULAR) {
5502 /* callers don't want stripes */
5503 /* XXX: all tricky interactions with ->ah_make_hint() decided
5504 * to use striping, then ->declare_create() behaving differently
5505 * should be cleaned */
5506 if (dof->u.dof_reg.striped != 0)
5507 rc = lod_declare_striped_create(env, dt, attr,
5509 } else if (dof->dof_type == DFT_DIR) {
5510 struct seq_server_site *ss;
5511 struct lu_buf buf = { NULL };
5512 struct lu_buf *lmu = NULL;
5514 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
5516 /* If the parent has default stripeEA, and client
5517 * did not find it before sending create request,
5518 * then MDT will return -EREMOTE, and client will
5519 * retrieve the default stripeEA and re-create the
5522 * Note: if dah_eadata != NULL, it means creating the
5523 * striped directory with specified stripeEA, then it
5524 * should ignore the default stripeEA */
5525 if (hint != NULL && hint->dah_eadata == NULL) {
5526 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
5527 GOTO(out, rc = -EREMOTE);
5529 if (lo->ldo_dir_stripe_offset == LMV_OFFSET_DEFAULT) {
5530 struct lod_default_striping *lds;
5532 lds = lo->ldo_def_striping;
5534 * child and parent should be on the same MDT,
5535 * but if parent has default LMV, and the start
5536 * MDT offset is -1, it's allowed. This check
5537 * is not necessary after 2.12.22 because client
5538 * follows this already, but old client may not.
5540 if (hint->dah_parent &&
5541 dt_object_remote(hint->dah_parent) && lds &&
5542 lds->lds_dir_def_stripe_offset !=
5544 GOTO(out, rc = -EREMOTE);
5545 } else if (lo->ldo_dir_stripe_offset !=
5547 struct lod_device *lod;
5548 struct lu_tgt_desc *mdt = NULL;
5549 bool found_mdt = false;
5551 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5552 lod_foreach_mdt(lod, mdt) {
5553 if (mdt->ltd_index ==
5554 lo->ldo_dir_stripe_offset) {
5560 /* If the MDT indicated by stripe_offset can be
5561 * found, then tell client to resend the create
5562 * request to the correct MDT, otherwise return
5563 * error to client */
5565 GOTO(out, rc = -EREMOTE);
5567 GOTO(out, rc = -EINVAL);
5569 } else if (hint && hint->dah_eadata) {
5571 lmu->lb_buf = (void *)hint->dah_eadata;
5572 lmu->lb_len = hint->dah_eadata_len;
5575 rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
5579 /* failed to create striping or to set initial size, let's reset
5580 * config so that others don't get confused */
5582 lod_striping_free(env, lo);
5587 * Generate component ID for new created component.
5589 * \param[in] lo LOD object
5590 * \param[in] comp_idx index of ldo_comp_entries
5592 * \retval component ID on success
5593 * \retval LCME_ID_INVAL on failure
5595 static __u32 lod_gen_component_id(struct lod_object *lo,
5596 int mirror_id, int comp_idx)
5598 struct lod_layout_component *lod_comp;
5599 __u32 id, start, end;
5602 LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
5604 lod_obj_inc_layout_gen(lo);
5605 id = lo->ldo_layout_gen;
5606 if (likely(id <= SEQ_ID_MAX))
5607 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
5609 /* Layout generation wraps, need to check collisions. */
5610 start = id & SEQ_ID_MASK;
5613 for (id = start; id <= end; id++) {
5614 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5615 lod_comp = &lo->ldo_comp_entries[i];
5616 if (pflr_id(mirror_id, id) == lod_comp->llc_id)
5619 /* Found the ununsed ID */
5620 if (i == lo->ldo_comp_cnt)
5621 RETURN(pflr_id(mirror_id, id));
5623 if (end == LCME_ID_MAX) {
5625 end = min(lo->ldo_layout_gen & LCME_ID_MASK,
5626 (__u32)(LCME_ID_MAX - 1));
5630 RETURN(LCME_ID_INVAL);
5634 * Creation of a striped regular object.
5636 * The function is called to create the stripe objects for a regular
5637 * striped file. This can happen at the initial object creation or
5638 * when the caller asks LOD to do so using ->do_xattr_set() method
5639 * (so called late striping). Notice all the information are already
5640 * prepared in the form of the list of objects (ldo_stripe field).
5641 * This is done during declare phase.
5643 * \param[in] env execution environment
5644 * \param[in] dt object
5645 * \param[in] attr attributes the stripes will be created with
5646 * \param[in] dof format of stripes (see OSD API description)
5647 * \param[in] th transaction handle
5649 * \retval 0 on success
5650 * \retval negative if failed
5652 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
5653 struct lu_attr *attr, struct dt_object_format *dof,
5656 struct lod_layout_component *lod_comp;
5657 struct lod_object *lo = lod_dt_obj(dt);
5662 LASSERT((lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL) ||
5663 lo->ldo_is_foreign);
5665 mirror_id = 0; /* non-flr file's mirror_id is 0 */
5666 if (lo->ldo_mirror_count > 1) {
5667 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5668 lod_comp = &lo->ldo_comp_entries[i];
5669 if (lod_comp->llc_id != LCME_ID_INVAL &&
5670 mirror_id_of(lod_comp->llc_id) > mirror_id)
5671 mirror_id = mirror_id_of(lod_comp->llc_id);
5675 /* create all underlying objects */
5676 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5677 lod_comp = &lo->ldo_comp_entries[i];
5679 if (lod_comp->llc_id == LCME_ID_INVAL) {
5680 /* only the component of FLR layout with more than 1
5681 * mirror has mirror ID in its component ID.
5683 if (lod_comp->llc_extent.e_start == 0 &&
5684 lo->ldo_mirror_count > 1)
5687 lod_comp->llc_id = lod_gen_component_id(lo,
5689 if (lod_comp->llc_id == LCME_ID_INVAL)
5690 GOTO(out, rc = -ERANGE);
5693 if (lod_comp_inited(lod_comp))
5696 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
5697 lod_comp_set_init(lod_comp);
5699 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
5700 lod_comp_set_init(lod_comp);
5702 if (lod_comp->llc_stripe == NULL)
5705 LASSERT(lod_comp->llc_stripe_count);
5706 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
5707 struct dt_object *object = lod_comp->llc_stripe[j];
5708 LASSERT(object != NULL);
5709 rc = lod_sub_create(env, object, attr, NULL, dof, th);
5713 lod_comp_set_init(lod_comp);
5716 rc = lod_fill_mirrors(lo);
5720 rc = lod_generate_and_set_lovea(env, lo, th);
5724 lo->ldo_comp_cached = 1;
5728 lod_striping_free(env, lo);
5732 static inline bool lod_obj_is_dom(struct dt_object *dt)
5734 struct lod_object *lo = lod_dt_obj(dt);
5736 if (!dt_object_exists(dt_object_child(dt)))
5739 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
5742 if (!lo->ldo_comp_cnt)
5745 return (lov_pattern(lo->ldo_comp_entries[0].llc_pattern) ==
5750 * Implementation of dt_object_operations::do_create.
5752 * If any of preceeding methods (like ->do_declare_create(),
5753 * ->do_ah_init(), etc) chose to create a striped object,
5754 * then this method will create the master and the stripes.
5756 * \see dt_object_operations::do_create() in the API description for details.
5758 static int lod_create(const struct lu_env *env, struct dt_object *dt,
5759 struct lu_attr *attr, struct dt_allocation_hint *hint,
5760 struct dt_object_format *dof, struct thandle *th)
5765 /* create local object */
5766 rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
5770 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
5771 (lod_obj_is_striped(dt) || lod_obj_is_dom(dt)) &&
5772 dof->u.dof_reg.striped != 0) {
5773 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
5774 rc = lod_striped_create(env, dt, attr, dof, th);
5781 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
5782 struct dt_object *dt, struct thandle *th,
5783 int comp_idx, int stripe_idx,
5784 struct lod_obj_stripe_cb_data *data)
5786 if (data->locd_declare)
5787 return lod_sub_declare_destroy(env, dt, th);
5788 else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
5789 stripe_idx == cfs_fail_val)
5790 return lod_sub_destroy(env, dt, th);
5796 * Implementation of dt_object_operations::do_declare_destroy.
5798 * If the object is a striped directory, then the function declares reference
5799 * removal from the master object (this is an index) to the stripes and declares
5800 * destroy of all the stripes. In all the cases, it declares an intention to
5801 * destroy the object itself.
5803 * \see dt_object_operations::do_declare_destroy() in the API description
5806 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
5809 struct dt_object *next = dt_object_child(dt);
5810 struct lod_object *lo = lod_dt_obj(dt);
5811 struct lod_thread_info *info = lod_env_info(env);
5812 struct dt_object *stripe;
5813 char *stripe_name = info->lti_key;
5819 * load striping information, notice we don't do this when object
5820 * is being initialized as we don't need this information till
5821 * few specific cases like destroy, chown
5823 rc = lod_striping_load(env, lo);
5827 /* declare destroy for all underlying objects */
5828 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5829 rc = next->do_ops->do_index_try(env, next,
5830 &dt_directory_features);
5834 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5835 stripe = lo->ldo_stripe[i];
5839 rc = lod_sub_declare_ref_del(env, next, th);
5843 snprintf(stripe_name, sizeof(info->lti_key),
5845 PFID(lu_object_fid(&stripe->do_lu)), i);
5846 rc = lod_sub_declare_delete(env, next,
5847 (const struct dt_key *)stripe_name, th);
5854 * we declare destroy for the local object
5856 rc = lod_sub_declare_destroy(env, next, th);
5860 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
5861 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
5864 if (!lod_obj_is_striped(dt))
5867 /* declare destroy all striped objects */
5868 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5869 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5870 stripe = lo->ldo_stripe[i];
5874 if (!dt_object_exists(stripe))
5877 rc = lod_sub_declare_ref_del(env, stripe, th);
5881 rc = lod_sub_declare_destroy(env, stripe, th);
5886 struct lod_obj_stripe_cb_data data = { { 0 } };
5888 data.locd_declare = true;
5889 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
5890 rc = lod_obj_for_each_stripe(env, lo, th, &data);
5897 * Implementation of dt_object_operations::do_destroy.
5899 * If the object is a striped directory, then the function removes references
5900 * from the master object (this is an index) to the stripes and destroys all
5901 * the stripes. In all the cases, the function destroys the object itself.
5903 * \see dt_object_operations::do_destroy() in the API description for details.
5905 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
5908 struct dt_object *next = dt_object_child(dt);
5909 struct lod_object *lo = lod_dt_obj(dt);
5910 struct lod_thread_info *info = lod_env_info(env);
5911 char *stripe_name = info->lti_key;
5912 struct dt_object *stripe;
5918 /* destroy sub-stripe of master object */
5919 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5920 rc = next->do_ops->do_index_try(env, next,
5921 &dt_directory_features);
5925 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5926 stripe = lo->ldo_stripe[i];
5930 rc = lod_sub_ref_del(env, next, th);
5934 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
5935 PFID(lu_object_fid(&stripe->do_lu)), i);
5937 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
5938 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
5939 PFID(lu_object_fid(&stripe->do_lu)));
5941 rc = lod_sub_delete(env, next,
5942 (const struct dt_key *)stripe_name, th);
5948 rc = lod_sub_destroy(env, next, th);
5952 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
5953 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
5956 if (!lod_obj_is_striped(dt))
5959 /* destroy all striped objects */
5960 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5961 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5962 stripe = lo->ldo_stripe[i];
5966 if (!dt_object_exists(stripe))
5969 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
5970 i == cfs_fail_val) {
5971 dt_write_lock(env, stripe, DT_TGT_CHILD);
5972 rc = lod_sub_ref_del(env, stripe, th);
5973 dt_write_unlock(env, stripe);
5977 rc = lod_sub_destroy(env, stripe, th);
5983 struct lod_obj_stripe_cb_data data = { { 0 } };
5985 data.locd_declare = false;
5986 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
5987 rc = lod_obj_for_each_stripe(env, lo, th, &data);
5994 * Implementation of dt_object_operations::do_declare_ref_add.
5996 * \see dt_object_operations::do_declare_ref_add() in the API description
5999 static int lod_declare_ref_add(const struct lu_env *env,
6000 struct dt_object *dt, struct thandle *th)
6002 return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
6006 * Implementation of dt_object_operations::do_ref_add.
6008 * \see dt_object_operations::do_ref_add() in the API description for details.
6010 static int lod_ref_add(const struct lu_env *env,
6011 struct dt_object *dt, struct thandle *th)
6013 return lod_sub_ref_add(env, dt_object_child(dt), th);
6017 * Implementation of dt_object_operations::do_declare_ref_del.
6019 * \see dt_object_operations::do_declare_ref_del() in the API description
6022 static int lod_declare_ref_del(const struct lu_env *env,
6023 struct dt_object *dt, struct thandle *th)
6025 return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
6029 * Implementation of dt_object_operations::do_ref_del
6031 * \see dt_object_operations::do_ref_del() in the API description for details.
6033 static int lod_ref_del(const struct lu_env *env,
6034 struct dt_object *dt, struct thandle *th)
6036 return lod_sub_ref_del(env, dt_object_child(dt), th);
6040 * Implementation of dt_object_operations::do_object_sync.
6042 * \see dt_object_operations::do_object_sync() in the API description
6045 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
6046 __u64 start, __u64 end)
6048 return dt_object_sync(env, dt_object_child(dt), start, end);
6052 * Implementation of dt_object_operations::do_object_unlock.
6054 * Used to release LDLM lock(s).
6056 * \see dt_object_operations::do_object_unlock() in the API description
6059 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
6060 struct ldlm_enqueue_info *einfo,
6061 union ldlm_policy_data *policy)
6063 struct lod_object *lo = lod_dt_obj(dt);
6064 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
6065 int slave_locks_size;
6069 if (slave_locks == NULL)
6072 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
6073 /* Note: for remote lock for single stripe dir, MDT will cancel
6074 * the lock by lockh directly */
6075 LASSERT(!dt_object_remote(dt_object_child(dt)));
6077 /* locks were unlocked in MDT layer */
6078 for (i = 0; i < slave_locks->ha_count; i++)
6079 LASSERT(!lustre_handle_is_used(&slave_locks->ha_handles[i]));
6082 * NB, ha_count may not equal to ldo_dir_stripe_count, because dir
6083 * layout may change, e.g., shrink dir layout after migration.
6085 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6086 if (lo->ldo_stripe[i])
6087 dt_invalidate(env, lo->ldo_stripe[i]);
6090 slave_locks_size = offsetof(typeof(*slave_locks),
6091 ha_handles[slave_locks->ha_count]);
6092 OBD_FREE(slave_locks, slave_locks_size);
6093 einfo->ei_cbdata = NULL;
6099 * Implementation of dt_object_operations::do_object_lock.
6101 * Used to get LDLM lock on the non-striped and striped objects.
6103 * \see dt_object_operations::do_object_lock() in the API description
6106 static int lod_object_lock(const struct lu_env *env,
6107 struct dt_object *dt,
6108 struct lustre_handle *lh,
6109 struct ldlm_enqueue_info *einfo,
6110 union ldlm_policy_data *policy)
6112 struct lod_object *lo = lod_dt_obj(dt);
6113 int slave_locks_size;
6114 struct lustre_handle_array *slave_locks = NULL;
6119 /* remote object lock */
6120 if (!einfo->ei_enq_slave) {
6121 LASSERT(dt_object_remote(dt));
6122 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
6126 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
6129 rc = lod_striping_load(env, lo);
6134 if (lo->ldo_dir_stripe_count <= 1)
6137 slave_locks_size = offsetof(typeof(*slave_locks),
6138 ha_handles[lo->ldo_dir_stripe_count]);
6139 /* Freed in lod_object_unlock */
6140 OBD_ALLOC(slave_locks, slave_locks_size);
6143 slave_locks->ha_count = lo->ldo_dir_stripe_count;
6145 /* striped directory lock */
6146 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6147 struct lustre_handle lockh;
6148 struct ldlm_res_id *res_id;
6149 struct dt_object *stripe;
6151 stripe = lo->ldo_stripe[i];
6155 res_id = &lod_env_info(env)->lti_res_id;
6156 fid_build_reg_res_name(lu_object_fid(&stripe->do_lu), res_id);
6157 einfo->ei_res_id = res_id;
6159 if (dt_object_remote(stripe)) {
6160 set_bit(i, (void *)slave_locks->ha_map);
6161 rc = dt_object_lock(env, stripe, &lockh, einfo, policy);
6163 struct ldlm_namespace *ns = einfo->ei_namespace;
6164 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
6165 ldlm_completion_callback completion = einfo->ei_cb_cp;
6166 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
6168 if (einfo->ei_mode == LCK_PW ||
6169 einfo->ei_mode == LCK_EX)
6170 dlmflags |= LDLM_FL_COS_INCOMPAT;
6172 LASSERT(ns != NULL);
6173 rc = ldlm_cli_enqueue_local(env, ns, res_id, LDLM_IBITS,
6174 policy, einfo->ei_mode,
6175 &dlmflags, blocking,
6177 NULL, 0, LVB_T_NONE,
6182 ldlm_lock_decref_and_cancel(
6183 &slave_locks->ha_handles[i],
6185 OBD_FREE(slave_locks, slave_locks_size);
6188 slave_locks->ha_handles[i] = lockh;
6190 einfo->ei_cbdata = slave_locks;
6196 * Implementation of dt_object_operations::do_invalidate.
6198 * \see dt_object_operations::do_invalidate() in the API description for details
6200 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
6202 return dt_invalidate(env, dt_object_child(dt));
6205 static int lod_declare_instantiate_components(const struct lu_env *env,
6206 struct lod_object *lo, struct thandle *th)
6208 struct lod_thread_info *info = lod_env_info(env);
6213 LASSERT(info->lti_count < lo->ldo_comp_cnt);
6215 for (i = 0; i < info->lti_count; i++) {
6216 rc = lod_qos_prep_create(env, lo, NULL, th,
6217 info->lti_comp_idx[i]);
6223 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6224 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
6225 &info->lti_buf, XATTR_NAME_LOV, 0, th);
6232 * Check OSTs for an existing component for further extension
6234 * Checks if OSTs are still healthy and not out of space. Gets free space
6235 * on OSTs (relative to allocation watermark rmb_low) and compares to
6236 * the proposed new_end for this component.
6238 * Decides whether or not to extend a component on its current OSTs.
6240 * \param[in] env execution environment for this thread
6241 * \param[in] lo object we're checking
6242 * \param[in] index index of this component
6243 * \param[in] extension_size extension size for this component
6244 * \param[in] extent layout extent for requested operation
6245 * \param[in] comp_extent extension component extent
6246 * \param[in] write if this is write operation
6248 * \retval true - OK to extend on current OSTs
6249 * \retval false - do not extend on current OSTs
6251 static bool lod_sel_osts_allowed(const struct lu_env *env,
6252 struct lod_object *lo,
6253 int index, __u64 extension_size,
6254 struct lu_extent *extent,
6255 struct lu_extent *comp_extent, int write)
6257 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[index];
6258 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6259 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
6260 __u64 available = 0;
6267 LASSERT(lod_comp->llc_stripe_count != 0);
6270 (extent->e_start == 0 && extent->e_end == OBD_OBJECT_EOF)) {
6271 /* truncate or append */
6272 size = extension_size;
6274 /* In case of write op, check the real write extent,
6275 * it may be larger than the extension_size */
6276 size = roundup(min(extent->e_end, comp_extent->e_end) -
6277 max(extent->e_start, comp_extent->e_start),
6280 /* extension_size is file level, so we must divide by stripe count to
6281 * compare it to available space on a single OST */
6282 size /= lod_comp->llc_stripe_count;
6284 lod_getref(&lod->lod_ost_descs);
6285 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
6286 int index = lod_comp->llc_ost_indices[i];
6287 struct lod_tgt_desc *ost = OST_TGT(lod, index);
6288 struct obd_statfs_info info = { 0 };
6289 int j, repeated = 0;
6293 /* Get the number of times this OST repeats in this component.
6294 * Note: inter-component repeats are not counted as this is
6295 * considered as a rare case: we try to not repeat OST in other
6296 * components if possible. */
6297 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6298 if (index != lod_comp->llc_ost_indices[j])
6301 /* already handled */
6307 if (j < lod_comp->llc_stripe_count)
6310 if (!cfs_bitmap_check(lod->lod_ost_bitmap, index)) {
6311 CDEBUG(D_LAYOUT, "ost %d no longer present\n", index);
6316 rc = dt_statfs_info(env, ost->ltd_tgt, sfs, &info);
6318 CDEBUG(D_LAYOUT, "statfs failed for ost %d, error %d\n",
6324 if (sfs->os_state & OS_STATE_ENOSPC ||
6325 sfs->os_state & OS_STATE_READONLY ||
6326 sfs->os_state & OS_STATE_DEGRADED) {
6327 CDEBUG(D_LAYOUT, "ost %d is not availble for SEL "
6328 "extension, state %u\n", index, sfs->os_state);
6334 available = sfs->os_bavail * sfs->os_bsize;
6335 /* 'available' is relative to the allocation threshold */
6336 available -= (__u64) info.os_reserved_mb_low << 20;
6338 CDEBUG(D_LAYOUT, "ost %d lowwm: %d highwm: %d, "
6339 "%llu %% blocks available, %llu %% blocks free\n",
6340 index, info.os_reserved_mb_low, info.os_reserved_mb_high,
6341 (100ull * sfs->os_bavail) / sfs->os_blocks,
6342 (100ull * sfs->os_bfree) / sfs->os_blocks);
6344 if (size * repeated > available) {
6346 CDEBUG(D_LAYOUT, "low space on ost %d, available %llu "
6347 "< extension size %llu\n", index, available,
6352 lod_putref(lod, &lod->lod_ost_descs);
6358 * Adjust extents after component removal
6360 * When we remove an extension component, we move the start of the next
6361 * component to match the start of the extension component, so no space is left
6364 * \param[in] env execution environment for this thread
6365 * \param[in] lo object
6366 * \param[in] max_comp layout component
6367 * \param[in] index index of this component
6369 * \retval 0 on success
6370 * \retval negative errno on error
6372 static void lod_sel_adjust_extents(const struct lu_env *env,
6373 struct lod_object *lo,
6374 int max_comp, int index)
6376 struct lod_layout_component *lod_comp = NULL;
6377 struct lod_layout_component *next = NULL;
6378 struct lod_layout_component *prev = NULL;
6379 __u64 new_start = 0;
6383 /* Extension space component */
6384 lod_comp = &lo->ldo_comp_entries[index];
6385 next = &lo->ldo_comp_entries[index + 1];
6386 prev = &lo->ldo_comp_entries[index - 1];
6388 LASSERT(lod_comp != NULL && prev != NULL && next != NULL);
6389 LASSERT(lod_comp->llc_flags & LCME_FL_EXTENSION);
6391 /* Previous is being removed */
6392 if (prev && prev->llc_id == LCME_ID_INVAL)
6393 new_start = prev->llc_extent.e_start;
6395 new_start = lod_comp->llc_extent.e_start;
6397 for (i = index + 1; i < max_comp; i++) {
6398 lod_comp = &lo->ldo_comp_entries[i];
6400 start = lod_comp->llc_extent.e_start;
6401 lod_comp->llc_extent.e_start = new_start;
6403 /* We only move zero length extendable components */
6404 if (!(start == lod_comp->llc_extent.e_end))
6407 LASSERT(!(lod_comp->llc_flags & LCME_FL_INIT));
6409 lod_comp->llc_extent.e_end = new_start;
6413 /* Calculate the proposed 'new end' for a component we're extending */
6414 static __u64 lod_extension_new_end(__u64 extension_size, __u64 extent_end,
6415 __u32 stripe_size, __u64 component_end,
6416 __u64 extension_end)
6420 LASSERT(extension_size != 0 && stripe_size != 0);
6422 /* Round up to extension size */
6423 if (extent_end == OBD_OBJECT_EOF) {
6424 new_end = OBD_OBJECT_EOF;
6426 /* Add at least extension_size to the previous component_end,
6427 * covering the req layout extent */
6428 new_end = max(extent_end - component_end, extension_size);
6429 new_end = roundup(new_end, extension_size);
6430 new_end += component_end;
6432 /* Component end must be min stripe size aligned */
6433 if (new_end % stripe_size) {
6434 CDEBUG(D_LAYOUT, "new component end is not aligned "
6435 "by the stripe size %u: [%llu, %llu) ext size "
6436 "%llu new end %llu, aligning\n",
6437 stripe_size, component_end, extent_end,
6438 extension_size, new_end);
6439 new_end = roundup(new_end, stripe_size);
6443 if (new_end < extent_end)
6444 new_end = OBD_OBJECT_EOF;
6447 /* Don't extend past the end of the extension component */
6448 if (new_end > extension_end)
6449 new_end = extension_end;
6454 /* As lod_sel_handler() could be re-entered for the same component several
6455 * times, this is the data for the next call. Fields could be changed to
6456 * component indexes when needed, (e.g. if there is no need to instantiate
6457 * all the previous components up to the current position) to tell the caller
6458 * where to start over from. */
6465 * Process extent updates for a particular layout component
6467 * Handle layout updates for a particular extension space component touched by
6468 * a layout update operation. Core function of self-extending PFL feature.
6470 * In general, this function processes exactly *one* stage of an extension
6471 * operation, modifying the layout accordingly, then returns to the caller.
6472 * The caller is responsible for restarting processing with the new layout,
6473 * which may repeatedly return to this function until the extension updates
6476 * This function does one of a few things to the layout:
6477 * 1. Extends the component before the current extension space component to
6478 * allow it to accomodate the requested operation (if space/policy permit that
6479 * component to continue on its current OSTs)
6481 * 2. If extension of the existing component fails, we do one of two things:
6482 * a. If there is a component after the extension space, we remove the
6483 * extension space component, move the start of the next component down
6484 * accordingly, then notify the caller to restart processing w/the new
6486 * b. If there is no following component, we try repeating the current
6487 * component, creating a new component using the current one as a
6488 * template (keeping its stripe properties but not specific striping),
6489 * and try assigning striping for this component. If there is sufficient
6490 * free space on the OSTs chosen for this component, it is instantiated
6491 * and i/o continues there.
6493 * If there is not sufficient space on the new OSTs, we remove this new
6494 * component & extend the current component.
6496 * Note further that uninited components followed by extension space can be zero
6497 * length meaning that we will try to extend them before initializing them, and
6498 * if that fails, they will be removed without initialization.
6500 * 3. If we extend to/beyond the end of an extension space component, that
6501 * component is exhausted (all of its range has been given to real components),
6502 * so we remove it and restart processing.
6504 * \param[in] env execution environment for this thread
6505 * \param[in,out] lo object to update the layout of
6506 * \param[in] extent layout extent for requested operation, update
6507 * layout to fit this operation
6508 * \param[in] th transaction handle for this operation
6509 * \param[in,out] max_comp the highest comp for the portion of the layout
6510 * we are operating on (For FLR, the chosen
6511 * replica). Updated because we may remove
6513 * \param[in] index index of the extension space component we're
6515 * \param[in] write if this is write op
6516 * \param[in,out] force if the extension is to be forced; set here
6517 to force it on the 2nd call for the same
6520 * \retval 0 on success
6521 * \retval negative errno on error
6523 static int lod_sel_handler(const struct lu_env *env,
6524 struct lod_object *lo,
6525 struct lu_extent *extent,
6526 struct thandle *th, int *max_comp,
6527 int index, int write,
6528 struct sel_data *sd)
6530 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6531 struct lod_thread_info *info = lod_env_info(env);
6532 struct lod_layout_component *lod_comp;
6533 struct lod_layout_component *prev;
6534 struct lod_layout_component *next = NULL;
6535 __u64 extension_size;
6542 /* First component cannot be extension space */
6544 CERROR("%s: "DFID" first component cannot be extension space\n",
6545 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6549 lod_comp = &lo->ldo_comp_entries[index];
6550 prev = &lo->ldo_comp_entries[index - 1];
6551 if ((index + 1) < *max_comp)
6552 next = &lo->ldo_comp_entries[index + 1];
6554 /* extension size uses the stripe size field as KiB */
6555 extension_size = lod_comp->llc_stripe_size * SEL_UNIT_SIZE;
6557 CDEBUG(D_LAYOUT, "prev start %llu, extension start %llu, extension end"
6558 " %llu, extension size %llu\n", prev->llc_extent.e_start,
6559 lod_comp->llc_extent.e_start, lod_comp->llc_extent.e_end,
6562 /* Two extension space components cannot be adjacent & extension space
6563 * components cannot be init */
6564 if ((prev->llc_flags & LCME_FL_EXTENSION) ||
6565 !(ergo(next, !(next->llc_flags & LCME_FL_EXTENSION))) ||
6566 lod_comp_inited(lod_comp)) {
6567 CERROR("%s: "DFID" invalid extension space components\n",
6568 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6572 if (!prev->llc_stripe) {
6573 CDEBUG(D_LAYOUT, "Previous component not inited\n");
6574 info->lti_count = 1;
6575 info->lti_comp_idx[0] = index - 1;
6576 rc = lod_declare_instantiate_components(env, lo, th);
6577 /* ENOSPC tells us we can't use this component. If there is
6578 * a next or we are repeating, we either spill over (next) or
6579 * extend the original comp (repeat). Otherwise, return the
6580 * error to the user. */
6581 if (rc == -ENOSPC && (next || sd->sd_repeat))
6587 if (sd->sd_force == 0 && rc == 0)
6588 rc = !lod_sel_osts_allowed(env, lo, index - 1,
6589 extension_size, extent,
6590 &lod_comp->llc_extent, write);
6592 repeated = !!(sd->sd_repeat);
6596 /* Extend previous component */
6598 new_end = lod_extension_new_end(extension_size, extent->e_end,
6599 prev->llc_stripe_size,
6600 prev->llc_extent.e_end,
6601 lod_comp->llc_extent.e_end);
6603 CDEBUG(D_LAYOUT, "new end %llu\n", new_end);
6604 lod_comp->llc_extent.e_start = new_end;
6605 prev->llc_extent.e_end = new_end;
6607 if (prev->llc_extent.e_end == lod_comp->llc_extent.e_end) {
6608 CDEBUG(D_LAYOUT, "Extension component exhausted\n");
6609 lod_comp->llc_id = LCME_ID_INVAL;
6613 /* rc == 1, failed to extend current component */
6616 /* Normal 'spillover' case - Remove the extension
6617 * space component & bring down the start of the next
6619 lod_comp->llc_id = LCME_ID_INVAL;
6621 if (!(prev->llc_flags & LCME_FL_INIT)) {
6622 prev->llc_id = LCME_ID_INVAL;
6625 lod_sel_adjust_extents(env, lo, *max_comp, index);
6626 } else if (lod_comp_inited(prev)) {
6627 /* If there is no next, and the previous component is
6628 * INIT'ed, try repeating the previous component. */
6629 LASSERT(repeated == 0);
6630 rc = lod_layout_repeat_comp(env, lo, index - 1);
6634 /* The previous component is a repeated component.
6635 * Record this so we don't keep trying to repeat it. */
6638 /* If the previous component is not INIT'ed, this may
6639 * be a component we have just instantiated but failed
6640 * to extend. Or even a repeated component we failed
6641 * to prepare a striping for. Do not repeat but instead
6642 * remove the repeated component & force the extention
6643 * of the original one */
6646 prev->llc_id = LCME_ID_INVAL;
6653 rc = lod_layout_del_prep_layout(env, lo, NULL);
6656 LASSERTF(-rc == change,
6657 "number deleted %d != requested %d\n", -rc,
6660 *max_comp = *max_comp + change;
6662 /* lod_del_prep_layout reallocates ldo_comp_entries, so we must
6663 * refresh these pointers before using them */
6664 lod_comp = &lo->ldo_comp_entries[index];
6665 prev = &lo->ldo_comp_entries[index - 1];
6666 CDEBUG(D_LAYOUT, "After extent updates: prev start %llu, current start "
6667 "%llu, current end %llu max_comp %d ldo_comp_cnt %d\n",
6668 prev->llc_extent.e_start, lod_comp->llc_extent.e_start,
6669 lod_comp->llc_extent.e_end, *max_comp, lo->ldo_comp_cnt);
6671 /* Layout changed successfully */
6676 * Declare layout extent updates
6678 * Handles extensions. Identifies extension components touched by current
6679 * operation and passes them to processing function.
6681 * Restarts with updated layouts from the processing function until the current
6682 * operation no longer touches an extension space component.
6684 * \param[in] env execution environment for this thread
6685 * \param[in,out] lo object to update the layout of
6686 * \param[in] extent layout extent for requested operation, update layout to
6687 * fit this operation
6688 * \param[in] th transaction handle for this operation
6689 * \param[in] pick identifies chosen mirror for FLR layouts
6690 * \param[in] write if this is write op
6692 * \retval 1 on layout changed, 0 on no change
6693 * \retval negative errno on error
6695 static int lod_declare_update_extents(const struct lu_env *env,
6696 struct lod_object *lo, struct lu_extent *extent,
6697 struct thandle *th, int pick, int write)
6699 struct lod_thread_info *info = lod_env_info(env);
6700 struct lod_layout_component *lod_comp;
6701 bool layout_changed = false;
6702 struct sel_data sd = { 0 };
6710 /* This makes us work on the components of the chosen mirror */
6711 start_index = lo->ldo_mirrors[pick].lme_start;
6712 max_comp = lo->ldo_mirrors[pick].lme_end + 1;
6713 if (lo->ldo_flr_state == LCM_FL_NONE)
6714 LASSERT(start_index == 0 && max_comp == lo->ldo_comp_cnt);
6716 CDEBUG(D_LAYOUT, "extent->e_start %llu, extent->e_end %llu\n",
6717 extent->e_start, extent->e_end);
6718 for (i = start_index; i < max_comp; i++) {
6719 lod_comp = &lo->ldo_comp_entries[i];
6721 /* We've passed all components of interest */
6722 if (lod_comp->llc_extent.e_start >= extent->e_end)
6725 if (lod_comp->llc_flags & LCME_FL_EXTENSION) {
6726 layout_changed = true;
6727 rc = lod_sel_handler(env, lo, extent, th, &max_comp,
6732 /* Nothing has changed behind the prev one */
6738 /* We may have added or removed components. If so, we must update the
6739 * start & ends of all the mirrors after the current one, and the end
6740 * of the current mirror. */
6741 change = max_comp - 1 - lo->ldo_mirrors[pick].lme_end;
6743 lo->ldo_mirrors[pick].lme_end += change;
6744 for (i = pick + 1; i < lo->ldo_mirror_count; i++) {
6745 lo->ldo_mirrors[i].lme_start += change;
6746 lo->ldo_mirrors[i].lme_end += change;
6752 /* The amount of components has changed, adjust the lti_comp_idx */
6753 rc2 = lod_layout_data_init(info, lo->ldo_comp_cnt);
6755 return rc < 0 ? rc : rc2 < 0 ? rc2 : layout_changed;
6758 /* If striping is already instantiated or INIT'ed DOM? */
6759 static bool lod_is_instantiation_needed(struct lod_layout_component *comp)
6761 return !(((lov_pattern(comp->llc_pattern) == LOV_PATTERN_MDT) &&
6762 lod_comp_inited(comp)) || comp->llc_stripe);
6766 * Declare layout update for a non-FLR layout.
6768 * \param[in] env execution environment for this thread
6769 * \param[in,out] lo object to update the layout of
6770 * \param[in] layout layout intent for requested operation, "update" is
6771 * a process of reacting to this
6772 * \param[in] buf buffer containing lov ea (see comment on usage inline)
6773 * \param[in] th transaction handle for this operation
6775 * \retval 0 on success
6776 * \retval negative errno on error
6778 static int lod_declare_update_plain(const struct lu_env *env,
6779 struct lod_object *lo, struct layout_intent *layout,
6780 const struct lu_buf *buf, struct thandle *th)
6782 struct lod_thread_info *info = lod_env_info(env);
6783 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6784 struct lod_layout_component *lod_comp;
6785 struct lov_comp_md_v1 *comp_v1 = NULL;
6786 bool layout_changed = false;
6787 bool replay = false;
6791 LASSERT(lo->ldo_flr_state == LCM_FL_NONE);
6794 * In case the client is passing lovea, which only happens during
6795 * the replay of layout intent write RPC for now, we may need to
6796 * parse the lovea and apply new layout configuration.
6798 if (buf && buf->lb_len) {
6799 struct lov_user_md_v1 *v1 = buf->lb_buf;
6801 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
6802 v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
6803 LOV_MAGIC_COMP_V1)) {
6804 CERROR("%s: the replay buffer of layout extend "
6805 "(magic %#x) does not contain expected "
6806 "composite layout.\n",
6807 lod2obd(d)->obd_name, v1->lmm_magic);
6808 GOTO(out, rc = -EINVAL);
6811 rc = lod_use_defined_striping(env, lo, buf);
6814 lo->ldo_comp_cached = 1;
6816 rc = lod_get_lov_ea(env, lo);
6819 /* old on-disk EA is stored in info->lti_buf */
6820 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
6822 layout_changed = true;
6824 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
6828 /* non replay path */
6829 rc = lod_striping_load(env, lo);
6834 /* Make sure defined layout covers the requested write range. */
6835 lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
6836 if (lo->ldo_comp_cnt > 1 &&
6837 lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
6838 lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
6839 CDEBUG_LIMIT(replay ? D_ERROR : D_LAYOUT,
6840 "%s: the defined layout [0, %#llx) does not "
6841 "covers the write range "DEXT"\n",
6842 lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
6843 PEXT(&layout->li_extent));
6844 GOTO(out, rc = -EINVAL);
6847 CDEBUG(D_LAYOUT, "%s: "DFID": update components "DEXT"\n",
6848 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
6849 PEXT(&layout->li_extent));
6852 rc = lod_declare_update_extents(env, lo, &layout->li_extent,
6853 th, 0, layout->li_opc == LAYOUT_INTENT_WRITE);
6857 layout_changed = true;
6861 * Iterate ld->ldo_comp_entries, find the component whose extent under
6862 * the write range and not instantianted.
6864 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6865 lod_comp = &lo->ldo_comp_entries[i];
6867 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
6871 /* If striping is instantiated or INIT'ed DOM skip */
6872 if (!lod_is_instantiation_needed(lod_comp))
6876 * In replay path, lod_comp is the EA passed by
6877 * client replay buffer, comp_v1 is the pre-recovery
6878 * on-disk EA, we'd sift out those components which
6879 * were init-ed in the on-disk EA.
6881 if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
6886 * this component hasn't instantiated in normal path, or during
6887 * replay it needs replay the instantiation.
6890 /* A released component is being extended */
6891 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
6892 GOTO(out, rc = -EINVAL);
6894 LASSERT(info->lti_comp_idx != NULL);
6895 info->lti_comp_idx[info->lti_count++] = i;
6896 layout_changed = true;
6899 if (!layout_changed)
6902 lod_obj_inc_layout_gen(lo);
6903 rc = lod_declare_instantiate_components(env, lo, th);
6907 lod_striping_free(env, lo);
6911 static inline int lod_comp_index(struct lod_object *lo,
6912 struct lod_layout_component *lod_comp)
6914 LASSERT(lod_comp >= lo->ldo_comp_entries &&
6915 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
6917 return lod_comp - lo->ldo_comp_entries;
6921 * Stale other mirrors by writing extent.
6923 static int lod_stale_components(const struct lu_env *env, struct lod_object *lo,
6924 int primary, struct lu_extent *extent,
6927 struct lod_layout_component *pri_comp, *lod_comp;
6928 struct lu_extent pri_extent;
6933 /* The writing extent decides which components in the primary
6934 * are affected... */
6935 CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
6938 lod_foreach_mirror_comp(pri_comp, lo, primary) {
6939 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
6942 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
6943 lod_comp_index(lo, pri_comp),
6944 PEXT(&pri_comp->llc_extent));
6946 pri_extent.e_start = pri_comp->llc_extent.e_start;
6947 pri_extent.e_end = pri_comp->llc_extent.e_end;
6949 for (i = 0; i < lo->ldo_mirror_count; i++) {
6952 rc = lod_declare_update_extents(env, lo, &pri_extent,
6954 /* if update_extents changed the layout, it may have
6955 * reallocated the component array, so start over to
6956 * avoid using stale pointers */
6962 /* ... and then stale other components that are
6963 * overlapping with primary components */
6964 lod_foreach_mirror_comp(lod_comp, lo, i) {
6965 if (!lu_extent_is_overlapped(
6967 &lod_comp->llc_extent))
6970 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
6971 i, lod_comp_index(lo, lod_comp));
6973 lod_comp->llc_flags |= LCME_FL_STALE;
6974 lo->ldo_mirrors[i].lme_stale = 1;
6983 * check an OST's availability
6984 * \param[in] env execution environment
6985 * \param[in] lo lod object
6986 * \param[in] dt dt object
6987 * \param[in] index mirror index
6989 * \retval negative if failed
6990 * \retval 1 if \a dt is available
6991 * \retval 0 if \a dt is not available
6993 static inline int lod_check_ost_avail(const struct lu_env *env,
6994 struct lod_object *lo,
6995 struct dt_object *dt, int index)
6997 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6998 struct lod_tgt_desc *ost;
7000 int type = LU_SEQ_RANGE_OST;
7003 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &idx, &type);
7005 CERROR("%s: can't locate "DFID":rc = %d\n",
7006 lod2obd(lod)->obd_name, PFID(lu_object_fid(&dt->do_lu)),
7011 ost = OST_TGT(lod, idx);
7012 if (ost->ltd_statfs.os_state &
7013 (OS_STATE_READONLY | OS_STATE_ENOSPC | OS_STATE_ENOINO |
7014 OS_STATE_NOPRECREATE) ||
7015 ost->ltd_active == 0) {
7016 CDEBUG(D_LAYOUT, DFID ": mirror %d OST%d unavail, rc = %d\n",
7017 PFID(lod_object_fid(lo)), index, idx, rc);
7025 * Pick primary mirror for write
7026 * \param[in] env execution environment
7027 * \param[in] lo object
7028 * \param[in] extent write range
7030 static int lod_primary_pick(const struct lu_env *env, struct lod_object *lo,
7031 struct lu_extent *extent)
7033 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7034 unsigned int seq = 0;
7035 struct lod_layout_component *lod_comp;
7037 int picked = -1, second_pick = -1, third_pick = -1;
7040 if (OBD_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
7041 get_random_bytes(&seq, sizeof(seq));
7042 seq %= lo->ldo_mirror_count;
7046 * Pick a mirror as the primary, and check the availability of OSTs.
7048 * This algo can be revised later after knowing the topology of
7051 lod_qos_statfs_update(env, lod, &lod->lod_ost_descs);
7052 for (i = 0; i < lo->ldo_mirror_count; i++) {
7053 bool ost_avail = true;
7054 int index = (i + seq) % lo->ldo_mirror_count;
7056 if (lo->ldo_mirrors[index].lme_stale) {
7057 CDEBUG(D_LAYOUT, DFID": mirror %d stale\n",
7058 PFID(lod_object_fid(lo)), index);
7062 /* 2nd pick is for the primary mirror containing unavail OST */
7063 if (lo->ldo_mirrors[index].lme_primary && second_pick < 0)
7064 second_pick = index;
7066 /* 3rd pick is for non-primary mirror containing unavail OST */
7067 if (second_pick < 0 && third_pick < 0)
7071 * we found a non-primary 1st pick, we'd like to find a
7072 * potential pirmary mirror.
7074 if (picked >= 0 && !lo->ldo_mirrors[index].lme_primary)
7077 /* check the availability of OSTs */
7078 lod_foreach_mirror_comp(lod_comp, lo, index) {
7079 if (!lod_comp_inited(lod_comp) || !lod_comp->llc_stripe)
7082 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
7083 struct dt_object *dt = lod_comp->llc_stripe[j];
7085 rc = lod_check_ost_avail(env, lo, dt, index);
7092 } /* for all dt object in one component */
7095 } /* for all components in a mirror */
7098 * the OSTs where allocated objects locates in the components
7099 * of the mirror are available.
7104 /* this mirror has all OSTs available */
7108 * primary with all OSTs are available, this is the perfect
7111 if (lo->ldo_mirrors[index].lme_primary)
7113 } /* for all mirrors */
7115 /* failed to pick a sound mirror, lower our expectation */
7117 picked = second_pick;
7119 picked = third_pick;
7126 static int lod_prepare_resync_mirror(const struct lu_env *env,
7127 struct lod_object *lo,
7130 struct lod_thread_info *info = lod_env_info(env);
7131 struct lod_layout_component *lod_comp;
7132 bool neg = !!(MIRROR_ID_NEG & mirror_id);
7135 mirror_id &= ~MIRROR_ID_NEG;
7137 for (i = 0; i < lo->ldo_mirror_count; i++) {
7138 if ((!neg && lo->ldo_mirrors[i].lme_id != mirror_id) ||
7139 (neg && lo->ldo_mirrors[i].lme_id == mirror_id))
7142 lod_foreach_mirror_comp(lod_comp, lo, i) {
7143 if (lod_comp_inited(lod_comp))
7146 info->lti_comp_idx[info->lti_count++] =
7147 lod_comp_index(lo, lod_comp);
7155 * figure out the components should be instantiated for resync.
7157 static int lod_prepare_resync(const struct lu_env *env, struct lod_object *lo,
7158 struct lu_extent *extent)
7160 struct lod_thread_info *info = lod_env_info(env);
7161 struct lod_layout_component *lod_comp;
7162 unsigned int need_sync = 0;
7166 DFID": instantiate all stale components in "DEXT"\n",
7167 PFID(lod_object_fid(lo)), PEXT(extent));
7170 * instantiate all components within this extent, even non-stale
7173 for (i = 0; i < lo->ldo_mirror_count; i++) {
7174 if (!lo->ldo_mirrors[i].lme_stale)
7177 lod_foreach_mirror_comp(lod_comp, lo, i) {
7178 if (!lu_extent_is_overlapped(extent,
7179 &lod_comp->llc_extent))
7184 if (lod_comp_inited(lod_comp))
7187 CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
7188 i, lod_comp_index(lo, lod_comp));
7189 info->lti_comp_idx[info->lti_count++] =
7190 lod_comp_index(lo, lod_comp);
7194 return need_sync ? 0 : -EALREADY;
7197 static int lod_declare_update_rdonly(const struct lu_env *env,
7198 struct lod_object *lo, struct md_layout_change *mlc,
7201 struct lod_thread_info *info = lod_env_info(env);
7202 struct lu_attr *layout_attr = &info->lti_layout_attr;
7203 struct lod_layout_component *lod_comp;
7204 struct lu_extent extent = { 0 };
7208 LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
7209 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7210 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7211 LASSERT(lo->ldo_mirror_count > 0);
7213 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7214 struct layout_intent *layout = mlc->mlc_intent;
7215 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7218 extent = layout->li_extent;
7219 CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
7220 PFID(lod_object_fid(lo)), PEXT(&extent));
7222 picked = lod_primary_pick(env, lo, &extent);
7226 CDEBUG(D_LAYOUT, DFID": picked mirror id %u as primary\n",
7227 PFID(lod_object_fid(lo)),
7228 lo->ldo_mirrors[picked].lme_id);
7230 /* Update extents of primary before staling */
7231 rc = lod_declare_update_extents(env, lo, &extent, th, picked,
7236 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
7238 * trunc transfers [0, size) in the intent extent, we'd
7239 * stale components overlapping [size, eof).
7241 extent.e_start = extent.e_end;
7242 extent.e_end = OBD_OBJECT_EOF;
7245 /* stale overlapping components from other mirrors */
7246 rc = lod_stale_components(env, lo, picked, &extent, th);
7250 /* restore truncate intent extent */
7251 if (layout->li_opc == LAYOUT_INTENT_TRUNC)
7252 extent.e_end = extent.e_start;
7254 /* instantiate components for the picked mirror, start from 0 */
7257 lod_foreach_mirror_comp(lod_comp, lo, picked) {
7258 if (!lu_extent_is_overlapped(&extent,
7259 &lod_comp->llc_extent))
7262 if (!lod_is_instantiation_needed(lod_comp))
7265 info->lti_comp_idx[info->lti_count++] =
7266 lod_comp_index(lo, lod_comp);
7269 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7270 } else { /* MD_LAYOUT_RESYNC */
7274 * could contain multiple non-stale mirrors, so we need to
7275 * prep uninited all components assuming any non-stale mirror
7276 * could be picked as the primary mirror.
7278 if (mlc->mlc_mirror_id == 0) {
7280 for (i = 0; i < lo->ldo_mirror_count; i++) {
7281 if (lo->ldo_mirrors[i].lme_stale)
7284 lod_foreach_mirror_comp(lod_comp, lo, i) {
7285 if (!lod_comp_inited(lod_comp))
7289 lod_comp->llc_extent.e_end)
7291 lod_comp->llc_extent.e_end;
7294 rc = lod_prepare_resync(env, lo, &extent);
7298 /* mirror write, try to init its all components */
7299 rc = lod_prepare_resync_mirror(env, lo,
7300 mlc->mlc_mirror_id);
7305 /* change the file state to SYNC_PENDING */
7306 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7309 /* Reset the layout version once it's becoming too large.
7310 * This way it can make sure that the layout version is
7311 * monotonously increased in this writing era. */
7312 lod_obj_inc_layout_gen(lo);
7313 if (lo->ldo_layout_gen > (LCME_ID_MAX >> 1)) {
7314 __u32 layout_version;
7316 get_random_bytes(&layout_version, sizeof(layout_version));
7317 lo->ldo_layout_gen = layout_version & 0xffff;
7320 rc = lod_declare_instantiate_components(env, lo, th);
7324 layout_attr->la_valid = LA_LAYOUT_VERSION;
7325 layout_attr->la_layout_version = 0; /* set current version */
7326 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7327 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7328 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7334 lod_striping_free(env, lo);
7338 static int lod_declare_update_write_pending(const struct lu_env *env,
7339 struct lod_object *lo, struct md_layout_change *mlc,
7342 struct lod_thread_info *info = lod_env_info(env);
7343 struct lu_attr *layout_attr = &info->lti_layout_attr;
7344 struct lod_layout_component *lod_comp;
7345 struct lu_extent extent = { 0 };
7351 LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
7352 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7353 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7355 /* look for the primary mirror */
7356 for (i = 0; i < lo->ldo_mirror_count; i++) {
7357 if (lo->ldo_mirrors[i].lme_stale)
7360 LASSERTF(primary < 0, DFID " has multiple primary: %u / %u",
7361 PFID(lod_object_fid(lo)),
7362 lo->ldo_mirrors[i].lme_id,
7363 lo->ldo_mirrors[primary].lme_id);
7368 CERROR(DFID ": doesn't have a primary mirror\n",
7369 PFID(lod_object_fid(lo)));
7370 GOTO(out, rc = -ENODATA);
7373 CDEBUG(D_LAYOUT, DFID": found primary %u\n",
7374 PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
7376 LASSERT(!lo->ldo_mirrors[primary].lme_stale);
7378 /* for LAYOUT_WRITE opc, it has to do the following operations:
7379 * 1. stale overlapping componets from stale mirrors;
7380 * 2. instantiate components of the primary mirror;
7381 * 3. transfter layout version to all objects of the primary;
7383 * for LAYOUT_RESYNC opc, it will do:
7384 * 1. instantiate components of all stale mirrors;
7385 * 2. transfer layout version to all objects to close write era. */
7387 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7388 struct layout_intent *layout = mlc->mlc_intent;
7389 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7391 LASSERT(mlc->mlc_intent != NULL);
7393 extent = mlc->mlc_intent->li_extent;
7395 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
7396 PFID(lod_object_fid(lo)), PEXT(&extent));
7398 /* 1. Update extents of primary before staling */
7399 rc = lod_declare_update_extents(env, lo, &extent, th, primary,
7404 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
7406 * trunc transfers [0, size) in the intent extent, we'd
7407 * stale components overlapping [size, eof).
7409 extent.e_start = extent.e_end;
7410 extent.e_end = OBD_OBJECT_EOF;
7413 /* 2. stale overlapping components */
7414 rc = lod_stale_components(env, lo, primary, &extent, th);
7418 /* 3. find the components which need instantiating.
7419 * instantiate [0, mlc->mlc_intent->e_end) */
7421 /* restore truncate intent extent */
7422 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC)
7423 extent.e_end = extent.e_start;
7426 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7427 if (!lu_extent_is_overlapped(&extent,
7428 &lod_comp->llc_extent))
7431 if (!lod_is_instantiation_needed(lod_comp))
7434 CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
7435 primary, lod_comp_index(lo, lod_comp));
7436 info->lti_comp_idx[info->lti_count++] =
7437 lod_comp_index(lo, lod_comp);
7439 } else { /* MD_LAYOUT_RESYNC */
7440 if (mlc->mlc_mirror_id == 0) {
7442 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7443 if (!lod_comp_inited(lod_comp))
7446 extent.e_end = lod_comp->llc_extent.e_end;
7449 rc = lod_prepare_resync(env, lo, &extent);
7453 /* mirror write, try to init its all components */
7454 rc = lod_prepare_resync_mirror(env, lo,
7455 mlc->mlc_mirror_id);
7460 /* change the file state to SYNC_PENDING */
7461 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7464 rc = lod_declare_instantiate_components(env, lo, th);
7468 /* 3. transfer layout version to OST objects.
7469 * transfer new layout version to OST objects so that stale writes
7470 * can be denied. It also ends an era of writing by setting
7471 * LU_LAYOUT_RESYNC. Normal client can never use this bit to
7472 * send write RPC; only resync RPCs could do it. */
7473 layout_attr->la_valid = LA_LAYOUT_VERSION;
7474 layout_attr->la_layout_version = 0; /* set current version */
7475 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7476 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7477 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7481 lod_obj_inc_layout_gen(lo);
7484 lod_striping_free(env, lo);
7488 static int lod_declare_update_sync_pending(const struct lu_env *env,
7489 struct lod_object *lo, struct md_layout_change *mlc,
7492 struct lod_thread_info *info = lod_env_info(env);
7493 unsigned sync_components = 0;
7494 unsigned resync_components = 0;
7499 LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
7500 LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
7501 mlc->mlc_opc == MD_LAYOUT_WRITE);
7503 CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
7504 PFID(lod_object_fid(lo)), mlc->mlc_opc);
7506 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7507 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
7508 PFID(lod_object_fid(lo)));
7510 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7511 return lod_declare_update_write_pending(env, lo, mlc, th);
7514 /* MD_LAYOUT_RESYNC_DONE */
7516 for (i = 0; i < lo->ldo_comp_cnt; i++) {
7517 struct lod_layout_component *lod_comp;
7520 lod_comp = &lo->ldo_comp_entries[i];
7522 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
7527 for (j = 0; j < mlc->mlc_resync_count; j++) {
7528 if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
7531 mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
7532 lod_comp->llc_flags &= ~LCME_FL_STALE;
7533 resync_components++;
7539 for (i = 0; i < mlc->mlc_resync_count; i++) {
7540 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
7543 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
7544 "or already synced\n", PFID(lod_object_fid(lo)),
7545 mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
7546 GOTO(out, rc = -EINVAL);
7549 if (!sync_components || (mlc->mlc_resync_count && !resync_components)) {
7550 CDEBUG(D_LAYOUT, DFID": no mirror in sync\n",
7551 PFID(lod_object_fid(lo)));
7553 /* tend to return an error code here to prevent
7554 * the MDT from setting SoM attribute */
7555 GOTO(out, rc = -EINVAL);
7558 CDEBUG(D_LAYOUT, DFID": synced %u resynced %u/%zu components\n",
7559 PFID(lod_object_fid(lo)),
7560 sync_components, resync_components, mlc->mlc_resync_count);
7562 lo->ldo_flr_state = LCM_FL_RDONLY;
7563 lod_obj_inc_layout_gen(lo);
7565 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
7566 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
7567 &info->lti_buf, XATTR_NAME_LOV, 0, th);
7572 lod_striping_free(env, lo);
7576 typedef int (*mlc_handler)(const struct lu_env *env, struct dt_object *dt,
7577 const struct md_layout_change *mlc,
7578 struct thandle *th);
7581 * Attach stripes after target's for migrating directory. NB, we
7582 * only need to declare this, the actual work is done inside
7583 * lod_xattr_set_lmv().
7585 * \param[in] env execution environment
7586 * \param[in] dt target object
7587 * \param[in] mlc layout change data
7588 * \param[in] th transaction handle
7590 * \retval 0 on success
7591 * \retval negative if failed
7593 static int lod_dir_declare_layout_attach(const struct lu_env *env,
7594 struct dt_object *dt,
7595 const struct md_layout_change *mlc,
7598 struct lod_thread_info *info = lod_env_info(env);
7599 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
7600 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
7601 struct lod_object *lo = lod_dt_obj(dt);
7602 struct dt_object *next = dt_object_child(dt);
7603 struct dt_object_format *dof = &info->lti_format;
7604 struct lmv_mds_md_v1 *lmv = mlc->mlc_buf.lb_buf;
7605 struct dt_object **stripes;
7606 __u32 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
7607 struct lu_fid *fid = &info->lti_fid;
7608 struct lod_tgt_desc *tgt;
7609 struct dt_object *dto;
7610 struct dt_device *tgt_dt;
7611 int type = LU_SEQ_RANGE_ANY;
7612 struct dt_insert_rec *rec = &info->lti_dt_rec;
7613 char *stripe_name = info->lti_key;
7614 struct lu_name *sname;
7615 struct linkea_data ldata = { NULL };
7616 struct lu_buf linkea_buf;
7623 if (!lmv_is_sane(lmv))
7626 if (!dt_try_as_dir(env, dt))
7629 dof->dof_type = DFT_DIR;
7632 sizeof(*stripes) * (lo->ldo_dir_stripe_count + stripe_count));
7636 for (i = 0; i < lo->ldo_dir_stripe_count; i++)
7637 stripes[i] = lo->ldo_stripe[i];
7639 rec->rec_type = S_IFDIR;
7641 for (i = 0; i < stripe_count; i++) {
7643 &lmv->lmv_stripe_fids[i]);
7644 if (!fid_is_sane(fid))
7647 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
7651 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
7652 tgt_dt = lod->lod_child;
7654 tgt = LTD_TGT(ltd, idx);
7656 GOTO(out, rc = -ESTALE);
7657 tgt_dt = tgt->ltd_tgt;
7660 dto = dt_locate_at(env, tgt_dt, fid,
7661 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
7664 GOTO(out, rc = PTR_ERR(dto));
7666 stripes[i + lo->ldo_dir_stripe_count] = dto;
7668 if (!dt_try_as_dir(env, dto))
7669 GOTO(out, rc = -ENOTDIR);
7671 rc = lod_sub_declare_ref_add(env, dto, th);
7675 rec->rec_fid = lu_object_fid(&dto->do_lu);
7676 rc = lod_sub_declare_insert(env, dto,
7677 (const struct dt_rec *)rec,
7678 (const struct dt_key *)dot, th);
7682 rc = lod_sub_declare_insert(env, dto,
7683 (const struct dt_rec *)rec,
7684 (const struct dt_key *)dotdot, th);
7688 rc = lod_sub_declare_xattr_set(env, dto, &mlc->mlc_buf,
7689 XATTR_NAME_LMV, 0, th);
7693 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
7694 PFID(lu_object_fid(&dto->do_lu)),
7695 i + lo->ldo_dir_stripe_count);
7697 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
7698 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
7699 sname, lu_object_fid(&dt->do_lu));
7703 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
7704 linkea_buf.lb_len = ldata.ld_leh->leh_len;
7705 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
7706 XATTR_NAME_LINK, 0, th);
7710 rc = lod_sub_declare_insert(env, next,
7711 (const struct dt_rec *)rec,
7712 (const struct dt_key *)stripe_name,
7717 rc = lod_sub_declare_ref_add(env, next, th);
7723 OBD_FREE(lo->ldo_stripe,
7724 sizeof(*stripes) * lo->ldo_dir_stripes_allocated);
7725 lo->ldo_stripe = stripes;
7726 lo->ldo_dir_migrate_offset = lo->ldo_dir_stripe_count;
7727 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_hash_type);
7728 lo->ldo_dir_stripe_count += stripe_count;
7729 lo->ldo_dir_stripes_allocated += stripe_count;
7730 lo->ldo_dir_hash_type |= LMV_HASH_FLAG_MIGRATION;
7734 i = lo->ldo_dir_stripe_count;
7735 while (i < lo->ldo_dir_stripe_count + stripe_count && stripes[i])
7736 dt_object_put(env, stripes[i++]);
7739 sizeof(*stripes) * (stripe_count + lo->ldo_dir_stripe_count));
7743 static int lod_dir_declare_layout_detach(const struct lu_env *env,
7744 struct dt_object *dt,
7745 const struct md_layout_change *unused,
7748 struct lod_thread_info *info = lod_env_info(env);
7749 struct lod_object *lo = lod_dt_obj(dt);
7750 struct dt_object *next = dt_object_child(dt);
7751 char *stripe_name = info->lti_key;
7752 struct dt_object *dto;
7756 if (!dt_try_as_dir(env, dt))
7759 if (!lo->ldo_dir_stripe_count)
7760 return lod_sub_declare_delete(env, next,
7761 (const struct dt_key *)dotdot, th);
7763 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
7764 dto = lo->ldo_stripe[i];
7768 if (!dt_try_as_dir(env, dto))
7771 rc = lod_sub_declare_delete(env, dto,
7772 (const struct dt_key *)dotdot, th);
7776 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
7777 PFID(lu_object_fid(&dto->do_lu)), i);
7779 rc = lod_sub_declare_delete(env, next,
7780 (const struct dt_key *)stripe_name, th);
7784 rc = lod_sub_declare_ref_del(env, next, th);
7792 static int dt_dir_is_empty(const struct lu_env *env,
7793 struct dt_object *obj)
7796 const struct dt_it_ops *iops;
7801 if (!dt_try_as_dir(env, obj))
7804 iops = &obj->do_index_ops->dio_it;
7805 it = iops->init(env, obj, LUDA_64BITHASH);
7807 RETURN(PTR_ERR(it));
7809 rc = iops->get(env, it, (const struct dt_key *)"");
7813 for (rc = 0, i = 0; rc == 0 && i < 3; ++i)
7814 rc = iops->next(env, it);
7820 /* Huh? Index contains no zero key? */
7825 iops->fini(env, it);
7830 static int lod_dir_declare_layout_shrink(const struct lu_env *env,
7831 struct dt_object *dt,
7832 const struct md_layout_change *mlc,
7835 struct lod_thread_info *info = lod_env_info(env);
7836 struct lod_object *lo = lod_dt_obj(dt);
7837 struct dt_object *next = dt_object_child(dt);
7838 struct lmv_user_md *lmu = mlc->mlc_buf.lb_buf;
7839 __u32 final_stripe_count;
7840 char *stripe_name = info->lti_key;
7841 struct lu_buf *lmv_buf = &info->lti_buf;
7842 struct dt_object *dto;
7848 if (!dt_try_as_dir(env, dt))
7851 /* shouldn't be called on plain directory */
7852 LASSERT(lo->ldo_dir_stripe_count);
7854 lmv_buf->lb_buf = &info->lti_lmv.lmv_md_v1;
7855 lmv_buf->lb_len = sizeof(info->lti_lmv.lmv_md_v1);
7857 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
7858 LASSERT(final_stripe_count &&
7859 final_stripe_count < lo->ldo_dir_stripe_count);
7861 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
7862 dto = lo->ldo_stripe[i];
7866 if (i < final_stripe_count) {
7867 if (final_stripe_count == 1)
7870 rc = lod_sub_declare_xattr_set(env, dto, lmv_buf,
7872 LU_XATTR_REPLACE, th);
7879 rc = dt_dir_is_empty(env, dto);
7883 rc = lod_sub_declare_ref_del(env, dto, th);
7887 rc = lod_sub_declare_destroy(env, dto, th);
7891 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
7892 PFID(lu_object_fid(&dto->do_lu)), i);
7894 rc = lod_sub_declare_delete(env, next,
7895 (const struct dt_key *)stripe_name, th);
7899 rc = lod_sub_declare_ref_del(env, next, th);
7904 rc = lod_sub_declare_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV,
7905 LU_XATTR_REPLACE, th);
7910 * detach all stripes from dir master object, NB, stripes are not destroyed, but
7911 * deleted from it's parent namespace, this function is called in two places:
7912 * 1. mdd_migrate_mdt() detach stripes from source, and attach them to
7914 * 2. mdd_dir_layout_update() detach stripe before turning 1-stripe directory to
7915 * a plain directory.
7917 * \param[in] env execution environment
7918 * \param[in] dt target object
7919 * \param[in] mlc layout change data
7920 * \param[in] th transaction handle
7922 * \retval 0 on success
7923 * \retval negative if failed
7925 static int lod_dir_layout_detach(const struct lu_env *env,
7926 struct dt_object *dt,
7927 const struct md_layout_change *mlc,
7930 struct lod_thread_info *info = lod_env_info(env);
7931 struct lod_object *lo = lod_dt_obj(dt);
7932 struct dt_object *next = dt_object_child(dt);
7933 char *stripe_name = info->lti_key;
7934 struct dt_object *dto;
7940 if (!lo->ldo_dir_stripe_count) {
7941 /* plain directory delete .. */
7942 rc = lod_sub_delete(env, next,
7943 (const struct dt_key *)dotdot, th);
7947 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
7948 dto = lo->ldo_stripe[i];
7952 rc = lod_sub_delete(env, dto,
7953 (const struct dt_key *)dotdot, th);
7957 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
7958 PFID(lu_object_fid(&dto->do_lu)), i);
7960 rc = lod_sub_delete(env, next,
7961 (const struct dt_key *)stripe_name, th);
7965 rc = lod_sub_ref_del(env, next, th);
7970 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
7971 dto = lo->ldo_stripe[i];
7973 dt_object_put(env, dto);
7975 OBD_FREE(lo->ldo_stripe,
7976 sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated);
7977 lo->ldo_stripe = NULL;
7978 lo->ldo_dir_stripes_allocated = 0;
7979 lo->ldo_dir_stripe_count = 0;
7984 static int lod_dir_layout_shrink(const struct lu_env *env,
7985 struct dt_object *dt,
7986 const struct md_layout_change *mlc,
7989 struct lod_thread_info *info = lod_env_info(env);
7990 struct lod_object *lo = lod_dt_obj(dt);
7991 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7992 struct dt_object *next = dt_object_child(dt);
7993 struct lmv_user_md *lmu = mlc->mlc_buf.lb_buf;
7994 __u32 final_stripe_count;
7995 char *stripe_name = info->lti_key;
7996 struct dt_object *dto;
7997 struct lu_buf *lmv_buf = &info->lti_buf;
7998 struct lmv_mds_md_v1 *lmv = &info->lti_lmv.lmv_md_v1;
8000 int type = LU_SEQ_RANGE_ANY;
8006 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
8008 lmv_buf->lb_buf = lmv;
8009 lmv_buf->lb_len = sizeof(*lmv);
8010 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
8011 lmv->lmv_stripe_count = cpu_to_le32(final_stripe_count);
8012 lmv->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type) &
8013 cpu_to_le32(LMV_HASH_TYPE_MASK);
8014 lmv->lmv_layout_version =
8015 cpu_to_le32(lo->ldo_dir_layout_version + 1);
8017 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8018 dto = lo->ldo_stripe[i];
8022 if (i < final_stripe_count) {
8023 /* if only one stripe left, no need to update
8024 * LMV because this stripe will replace master
8025 * object and act as a plain directory.
8027 if (final_stripe_count == 1)
8031 rc = lod_fld_lookup(env, lod,
8032 lu_object_fid(&dto->do_lu),
8037 lmv->lmv_master_mdt_index = cpu_to_le32(mdtidx);
8038 rc = lod_sub_xattr_set(env, dto, lmv_buf,
8040 LU_XATTR_REPLACE, th);
8047 dt_write_lock(env, dto, DT_TGT_CHILD);
8048 rc = lod_sub_ref_del(env, dto, th);
8049 dt_write_unlock(env, dto);
8053 rc = lod_sub_destroy(env, dto, th);
8057 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8058 PFID(lu_object_fid(&dto->do_lu)), i);
8060 rc = lod_sub_delete(env, next,
8061 (const struct dt_key *)stripe_name, th);
8065 rc = lod_sub_ref_del(env, next, th);
8070 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &mdtidx,
8075 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
8076 lmv->lmv_master_mdt_index = cpu_to_le32(mdtidx);
8077 rc = lod_sub_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV,
8078 LU_XATTR_REPLACE, th);
8082 for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
8083 dto = lo->ldo_stripe[i];
8085 dt_object_put(env, dto);
8087 lo->ldo_dir_stripe_count = final_stripe_count;
8092 static mlc_handler dir_mlc_declare_ops[MD_LAYOUT_MAX] = {
8093 [MD_LAYOUT_ATTACH] = lod_dir_declare_layout_attach,
8094 [MD_LAYOUT_DETACH] = lod_dir_declare_layout_detach,
8095 [MD_LAYOUT_SHRINK] = lod_dir_declare_layout_shrink,
8098 static mlc_handler dir_mlc_ops[MD_LAYOUT_MAX] = {
8099 [MD_LAYOUT_DETACH] = lod_dir_layout_detach,
8100 [MD_LAYOUT_SHRINK] = lod_dir_layout_shrink,
8103 static int lod_declare_layout_change(const struct lu_env *env,
8104 struct dt_object *dt, struct md_layout_change *mlc,
8107 struct lod_thread_info *info = lod_env_info(env);
8108 struct lod_object *lo = lod_dt_obj(dt);
8113 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
8114 LASSERT(dir_mlc_declare_ops[mlc->mlc_opc]);
8115 rc = dir_mlc_declare_ops[mlc->mlc_opc](env, dt, mlc, th);
8119 if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
8120 dt_object_remote(dt_object_child(dt)))
8123 rc = lod_striping_load(env, lo);
8127 LASSERT(lo->ldo_comp_cnt > 0);
8129 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
8133 switch (lo->ldo_flr_state) {
8135 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
8139 rc = lod_declare_update_rdonly(env, lo, mlc, th);
8141 case LCM_FL_WRITE_PENDING:
8142 rc = lod_declare_update_write_pending(env, lo, mlc, th);
8144 case LCM_FL_SYNC_PENDING:
8145 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
8156 * Instantiate layout component objects which covers the intent write offset.
8158 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
8159 struct md_layout_change *mlc, struct thandle *th)
8161 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
8162 struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
8163 struct lod_object *lo = lod_dt_obj(dt);
8168 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
8169 LASSERT(dir_mlc_ops[mlc->mlc_opc]);
8170 rc = dir_mlc_ops[mlc->mlc_opc](env, dt, mlc, th);
8174 rc = lod_striped_create(env, dt, attr, NULL, th);
8175 if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
8176 layout_attr->la_layout_version |= lo->ldo_layout_gen;
8177 rc = lod_attr_set(env, dt, layout_attr, th);
8183 struct dt_object_operations lod_obj_ops = {
8184 .do_read_lock = lod_read_lock,
8185 .do_write_lock = lod_write_lock,
8186 .do_read_unlock = lod_read_unlock,
8187 .do_write_unlock = lod_write_unlock,
8188 .do_write_locked = lod_write_locked,
8189 .do_attr_get = lod_attr_get,
8190 .do_declare_attr_set = lod_declare_attr_set,
8191 .do_attr_set = lod_attr_set,
8192 .do_xattr_get = lod_xattr_get,
8193 .do_declare_xattr_set = lod_declare_xattr_set,
8194 .do_xattr_set = lod_xattr_set,
8195 .do_declare_xattr_del = lod_declare_xattr_del,
8196 .do_xattr_del = lod_xattr_del,
8197 .do_xattr_list = lod_xattr_list,
8198 .do_ah_init = lod_ah_init,
8199 .do_declare_create = lod_declare_create,
8200 .do_create = lod_create,
8201 .do_declare_destroy = lod_declare_destroy,
8202 .do_destroy = lod_destroy,
8203 .do_index_try = lod_index_try,
8204 .do_declare_ref_add = lod_declare_ref_add,
8205 .do_ref_add = lod_ref_add,
8206 .do_declare_ref_del = lod_declare_ref_del,
8207 .do_ref_del = lod_ref_del,
8208 .do_object_sync = lod_object_sync,
8209 .do_object_lock = lod_object_lock,
8210 .do_object_unlock = lod_object_unlock,
8211 .do_invalidate = lod_invalidate,
8212 .do_declare_layout_change = lod_declare_layout_change,
8213 .do_layout_change = lod_layout_change,
8217 * Implementation of dt_body_operations::dbo_read.
8219 * \see dt_body_operations::dbo_read() in the API description for details.
8221 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
8222 struct lu_buf *buf, loff_t *pos)
8224 struct dt_object *next = dt_object_child(dt);
8226 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
8227 S_ISLNK(dt->do_lu.lo_header->loh_attr));
8228 return next->do_body_ops->dbo_read(env, next, buf, pos);
8232 * Implementation of dt_body_operations::dbo_declare_write.
8234 * \see dt_body_operations::dbo_declare_write() in the API description
8237 static ssize_t lod_declare_write(const struct lu_env *env,
8238 struct dt_object *dt,
8239 const struct lu_buf *buf, loff_t pos,
8242 return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
8246 * Implementation of dt_body_operations::dbo_write.
8248 * \see dt_body_operations::dbo_write() in the API description for details.
8250 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
8251 const struct lu_buf *buf, loff_t *pos,
8254 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
8255 S_ISLNK(dt->do_lu.lo_header->loh_attr));
8256 return lod_sub_write(env, dt_object_child(dt), buf, pos, th);
8259 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
8260 __u64 start, __u64 end, struct thandle *th)
8262 if (dt_object_remote(dt))
8265 return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
8268 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
8269 __u64 start, __u64 end, struct thandle *th)
8271 if (dt_object_remote(dt))
8274 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
8275 return lod_sub_punch(env, dt_object_child(dt), start, end, th);
8279 * different type of files use the same body_ops because object may be created
8280 * in OUT, where there is no chance to set correct body_ops for each type, so
8281 * body_ops themselves will check file type inside, see lod_read/write/punch for
8284 const struct dt_body_operations lod_body_ops = {
8285 .dbo_read = lod_read,
8286 .dbo_declare_write = lod_declare_write,
8287 .dbo_write = lod_write,
8288 .dbo_declare_punch = lod_declare_punch,
8289 .dbo_punch = lod_punch,
8293 * Implementation of lu_object_operations::loo_object_init.
8295 * The function determines the type and the index of the target device using
8296 * sequence of the object's FID. Then passes control down to the
8297 * corresponding device:
8298 * OSD for the local objects, OSP for remote
8300 * \see lu_object_operations::loo_object_init() in the API description
8303 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
8304 const struct lu_object_conf *conf)
8306 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
8307 struct lu_device *cdev = NULL;
8308 struct lu_object *cobj;
8309 struct lod_tgt_descs *ltd = NULL;
8310 struct lod_tgt_desc *tgt;
8312 int type = LU_SEQ_RANGE_ANY;
8316 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
8320 if (type == LU_SEQ_RANGE_MDT &&
8321 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
8322 cdev = &lod->lod_child->dd_lu_dev;
8323 } else if (type == LU_SEQ_RANGE_MDT) {
8324 ltd = &lod->lod_mdt_descs;
8326 } else if (type == LU_SEQ_RANGE_OST) {
8327 ltd = &lod->lod_ost_descs;
8334 if (ltd->ltd_tgts_size > idx &&
8335 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
8336 tgt = LTD_TGT(ltd, idx);
8338 LASSERT(tgt != NULL);
8339 LASSERT(tgt->ltd_tgt != NULL);
8341 cdev = &(tgt->ltd_tgt->dd_lu_dev);
8343 lod_putref(lod, ltd);
8346 if (unlikely(cdev == NULL))
8349 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
8350 if (unlikely(cobj == NULL))
8353 lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
8355 lu_object_add(lo, cobj);
8362 * Alloc cached foreign LOV
8364 * \param[in] lo object
8365 * \param[in] size size of foreign LOV
8367 * \retval 0 on success
8368 * \retval negative if failed
8370 int lod_alloc_foreign_lov(struct lod_object *lo, size_t size)
8372 OBD_ALLOC_LARGE(lo->ldo_foreign_lov, size);
8373 if (lo->ldo_foreign_lov == NULL)
8375 lo->ldo_foreign_lov_size = size;
8376 lo->ldo_is_foreign = 1;
8382 * Free cached foreign LOV
8384 * \param[in] lo object
8386 void lod_free_foreign_lov(struct lod_object *lo)
8388 if (lo->ldo_foreign_lov != NULL)
8389 OBD_FREE_LARGE(lo->ldo_foreign_lov, lo->ldo_foreign_lov_size);
8390 lo->ldo_foreign_lov = NULL;
8391 lo->ldo_foreign_lov_size = 0;
8392 lo->ldo_is_foreign = 0;
8397 * Free cached foreign LMV
8399 * \param[in] lo object
8401 void lod_free_foreign_lmv(struct lod_object *lo)
8403 if (lo->ldo_foreign_lmv != NULL)
8404 OBD_FREE_LARGE(lo->ldo_foreign_lmv, lo->ldo_foreign_lmv_size);
8405 lo->ldo_foreign_lmv = NULL;
8406 lo->ldo_foreign_lmv_size = 0;
8407 lo->ldo_dir_is_foreign = 0;
8412 * Release resources associated with striping.
8414 * If the object is striped (regular or directory), then release
8415 * the stripe objects references and free the ldo_stripe array.
8417 * \param[in] env execution environment
8418 * \param[in] lo object
8420 void lod_striping_free_nolock(const struct lu_env *env, struct lod_object *lo)
8422 struct lod_layout_component *lod_comp;
8425 if (unlikely(lo->ldo_is_foreign)) {
8426 lod_free_foreign_lov(lo);
8427 lo->ldo_comp_cached = 0;
8428 } else if (unlikely(lo->ldo_dir_is_foreign)) {
8429 lod_free_foreign_lmv(lo);
8430 lo->ldo_dir_stripe_loaded = 0;
8431 } else if (lo->ldo_stripe != NULL) {
8432 LASSERT(lo->ldo_comp_entries == NULL);
8433 LASSERT(lo->ldo_dir_stripes_allocated > 0);
8435 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8436 if (lo->ldo_stripe[i])
8437 dt_object_put(env, lo->ldo_stripe[i]);
8440 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
8441 OBD_FREE(lo->ldo_stripe, j);
8442 lo->ldo_stripe = NULL;
8443 lo->ldo_dir_stripes_allocated = 0;
8444 lo->ldo_dir_stripe_loaded = 0;
8445 lo->ldo_dir_stripe_count = 0;
8446 } else if (lo->ldo_comp_entries != NULL) {
8447 for (i = 0; i < lo->ldo_comp_cnt; i++) {
8448 /* free lod_layout_component::llc_stripe array */
8449 lod_comp = &lo->ldo_comp_entries[i];
8451 if (lod_comp->llc_stripe == NULL)
8453 LASSERT(lod_comp->llc_stripes_allocated != 0);
8454 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
8455 if (lod_comp->llc_stripe[j] != NULL)
8457 &lod_comp->llc_stripe[j]->do_lu);
8459 OBD_FREE(lod_comp->llc_stripe,
8460 sizeof(struct dt_object *) *
8461 lod_comp->llc_stripes_allocated);
8462 lod_comp->llc_stripe = NULL;
8463 OBD_FREE(lod_comp->llc_ost_indices,
8465 lod_comp->llc_stripes_allocated);
8466 lod_comp->llc_ost_indices = NULL;
8467 lod_comp->llc_stripes_allocated = 0;
8469 lod_free_comp_entries(lo);
8470 lo->ldo_comp_cached = 0;
8474 void lod_striping_free(const struct lu_env *env, struct lod_object *lo)
8476 mutex_lock(&lo->ldo_layout_mutex);
8477 lod_striping_free_nolock(env, lo);
8478 mutex_unlock(&lo->ldo_layout_mutex);
8482 * Implementation of lu_object_operations::loo_object_free.
8484 * \see lu_object_operations::loo_object_free() in the API description
8487 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
8489 struct lod_object *lo = lu2lod_obj(o);
8491 /* release all underlying object pinned */
8492 lod_striping_free(env, lo);
8494 /* lo doesn't contain a lu_object_header, so we don't need call_rcu */
8495 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
8499 * Implementation of lu_object_operations::loo_object_release.
8501 * \see lu_object_operations::loo_object_release() in the API description
8504 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
8506 /* XXX: shouldn't we release everything here in case if object
8507 * creation failed before? */
8511 * Implementation of lu_object_operations::loo_object_print.
8513 * \see lu_object_operations::loo_object_print() in the API description
8516 static int lod_object_print(const struct lu_env *env, void *cookie,
8517 lu_printer_t p, const struct lu_object *l)
8519 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
8521 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
8524 struct lu_object_operations lod_lu_obj_ops = {
8525 .loo_object_init = lod_object_init,
8526 .loo_object_free = lod_object_free,
8527 .loo_object_release = lod_object_release,
8528 .loo_object_print = lod_object_print,