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 = snprintf(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 static inline void lod_adjust_stripe_info(struct lod_layout_component *comp,
1079 struct lov_desc *desc)
1081 if (comp->llc_pattern != LOV_PATTERN_MDT) {
1082 if (!comp->llc_stripe_count)
1083 comp->llc_stripe_count =
1084 desc->ld_default_stripe_count;
1086 if (comp->llc_stripe_size <= 0)
1087 comp->llc_stripe_size = desc->ld_default_stripe_size;
1090 int lod_obj_for_each_stripe(const struct lu_env *env, struct lod_object *lo,
1092 struct lod_obj_stripe_cb_data *data)
1094 struct lod_layout_component *lod_comp;
1098 LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
1099 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1100 lod_comp = &lo->ldo_comp_entries[i];
1102 if (lod_comp->llc_stripe == NULL)
1105 /* has stripe but not inited yet, this component has been
1106 * declared to be created, but hasn't created yet.
1108 if (!lod_comp_inited(lod_comp))
1111 if (data->locd_comp_skip_cb &&
1112 data->locd_comp_skip_cb(env, lo, i, data))
1115 if (data->locd_comp_cb) {
1116 rc = data->locd_comp_cb(env, lo, i, data);
1121 /* could used just to do sth about component, not each
1124 if (!data->locd_stripe_cb)
1127 LASSERT(lod_comp->llc_stripe_count > 0);
1128 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
1129 struct dt_object *dt = lod_comp->llc_stripe[j];
1133 rc = data->locd_stripe_cb(env, lo, dt, th, i, j, data);
1141 static bool lod_obj_attr_set_comp_skip_cb(const struct lu_env *env,
1142 struct lod_object *lo, int comp_idx,
1143 struct lod_obj_stripe_cb_data *data)
1145 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
1146 bool skipped = false;
1148 if (!(data->locd_attr->la_valid & LA_LAYOUT_VERSION))
1151 switch (lo->ldo_flr_state) {
1152 case LCM_FL_WRITE_PENDING: {
1155 /* skip stale components */
1156 if (lod_comp->llc_flags & LCME_FL_STALE) {
1161 /* skip valid and overlapping components, therefore any
1162 * attempts to write overlapped components will never succeed
1163 * because client will get EINPROGRESS. */
1164 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1168 if (lo->ldo_comp_entries[i].llc_flags & LCME_FL_STALE)
1171 if (lu_extent_is_overlapped(&lod_comp->llc_extent,
1172 &lo->ldo_comp_entries[i].llc_extent)) {
1180 LASSERTF(0, "impossible: %d\n", lo->ldo_flr_state);
1181 case LCM_FL_SYNC_PENDING:
1185 CDEBUG(D_LAYOUT, DFID": %s to set component %x to version: %u\n",
1186 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
1187 skipped ? "skipped" : "chose", lod_comp->llc_id,
1188 data->locd_attr->la_layout_version);
1194 lod_obj_stripe_attr_set_cb(const struct lu_env *env, struct lod_object *lo,
1195 struct dt_object *dt, struct thandle *th,
1196 int comp_idx, int stripe_idx,
1197 struct lod_obj_stripe_cb_data *data)
1199 if (data->locd_declare)
1200 return lod_sub_declare_attr_set(env, dt, data->locd_attr, th);
1202 if (data->locd_attr->la_valid & LA_LAYOUT_VERSION) {
1203 CDEBUG(D_LAYOUT, DFID": set layout version: %u, comp_idx: %d\n",
1204 PFID(lu_object_fid(&dt->do_lu)),
1205 data->locd_attr->la_layout_version, comp_idx);
1208 return lod_sub_attr_set(env, dt, data->locd_attr, th);
1212 * Implementation of dt_object_operations::do_declare_attr_set.
1214 * If the object is striped, then apply the changes to all the stripes.
1216 * \see dt_object_operations::do_declare_attr_set() in the API description
1219 static int lod_declare_attr_set(const struct lu_env *env,
1220 struct dt_object *dt,
1221 const struct lu_attr *attr,
1224 struct dt_object *next = dt_object_child(dt);
1225 struct lod_object *lo = lod_dt_obj(dt);
1230 * declare setattr on the local object
1232 rc = lod_sub_declare_attr_set(env, next, attr, th);
1236 /* osp_declare_attr_set() ignores all attributes other than
1237 * UID, GID, PROJID, and size, and osp_attr_set() ignores all
1238 * but UID, GID and PROJID. Declaration of size attr setting
1239 * happens through lod_declare_init_size(), and not through
1240 * this function. Therefore we need not load striping unless
1241 * ownership is changing. This should save memory and (we hope)
1242 * speed up rename().
1244 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1245 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1248 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1251 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID | LA_MODE |
1252 LA_ATIME | LA_MTIME | LA_CTIME |
1257 * load striping information, notice we don't do this when object
1258 * is being initialized as we don't need this information till
1259 * few specific cases like destroy, chown
1261 rc = lod_striping_load(env, lo);
1265 if (!lod_obj_is_striped(dt))
1269 * if object is striped declare changes on the stripes
1271 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1272 LASSERT(lo->ldo_stripe);
1273 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1274 if (lo->ldo_stripe[i] == NULL)
1276 if (!dt_object_exists(lo->ldo_stripe[i]))
1278 rc = lod_sub_declare_attr_set(env, lo->ldo_stripe[i],
1284 struct lod_obj_stripe_cb_data data = { { 0 } };
1286 data.locd_attr = attr;
1287 data.locd_declare = true;
1288 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1289 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1295 if (!dt_object_exists(next) || dt_object_remote(next) ||
1296 !S_ISREG(attr->la_mode))
1299 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1300 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
1304 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1305 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1306 struct lod_thread_info *info = lod_env_info(env);
1307 struct lu_buf *buf = &info->lti_buf;
1309 buf->lb_buf = info->lti_ea_store;
1310 buf->lb_len = info->lti_ea_store_size;
1311 rc = lod_sub_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1312 LU_XATTR_REPLACE, th);
1319 * Implementation of dt_object_operations::do_attr_set.
1321 * If the object is striped, then apply the changes to all or subset of
1322 * the stripes depending on the object type and specific attributes.
1324 * \see dt_object_operations::do_attr_set() in the API description for details.
1326 static int lod_attr_set(const struct lu_env *env,
1327 struct dt_object *dt,
1328 const struct lu_attr *attr,
1331 struct dt_object *next = dt_object_child(dt);
1332 struct lod_object *lo = lod_dt_obj(dt);
1337 * apply changes to the local object
1339 rc = lod_sub_attr_set(env, next, attr, th);
1343 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1344 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1347 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1350 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE | LA_PROJID |
1351 LA_ATIME | LA_MTIME | LA_CTIME |
1356 /* FIXME: a tricky case in the code path of mdd_layout_change():
1357 * the in-memory striping information has been freed in lod_xattr_set()
1358 * due to layout change. It has to load stripe here again. It only
1359 * changes flags of layout so declare_attr_set() is still accurate */
1360 rc = lod_striping_load(env, lo);
1364 if (!lod_obj_is_striped(dt))
1368 * if object is striped, apply changes to all the stripes
1370 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1371 LASSERT(lo->ldo_stripe);
1372 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1373 if (unlikely(lo->ldo_stripe[i] == NULL))
1376 if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1379 rc = lod_sub_attr_set(env, lo->ldo_stripe[i], attr, th);
1384 struct lod_obj_stripe_cb_data data = { { 0 } };
1386 data.locd_attr = attr;
1387 data.locd_declare = false;
1388 data.locd_comp_skip_cb = lod_obj_attr_set_comp_skip_cb;
1389 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1390 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1396 if (!dt_object_exists(next) || dt_object_remote(next) ||
1397 !S_ISREG(attr->la_mode))
1400 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1401 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
1405 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1406 struct lod_thread_info *info = lod_env_info(env);
1407 struct lu_buf *buf = &info->lti_buf;
1408 struct ost_id *oi = &info->lti_ostid;
1409 struct lu_fid *fid = &info->lti_fid;
1410 struct lov_mds_md_v1 *lmm;
1411 struct lov_ost_data_v1 *objs;
1414 rc = lod_get_lov_ea(env, lo);
1418 buf->lb_buf = info->lti_ea_store;
1419 buf->lb_len = info->lti_ea_store_size;
1420 lmm = info->lti_ea_store;
1421 magic = le32_to_cpu(lmm->lmm_magic);
1422 if (magic == LOV_MAGIC_COMP_V1) {
1423 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1424 struct lov_comp_md_entry_v1 *lcme =
1425 &lcm->lcm_entries[0];
1427 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1428 magic = le32_to_cpu(lmm->lmm_magic);
1431 if (magic == LOV_MAGIC_V1)
1432 objs = &(lmm->lmm_objects[0]);
1434 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1435 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1436 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1438 fid_to_ostid(fid, oi);
1439 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1441 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1442 LU_XATTR_REPLACE, th);
1443 } else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1444 struct lod_thread_info *info = lod_env_info(env);
1445 struct lu_buf *buf = &info->lti_buf;
1446 struct lov_comp_md_v1 *lcm;
1447 struct lov_comp_md_entry_v1 *lcme;
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;
1456 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
1459 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1460 lcme = &lcm->lcm_entries[0];
1461 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1462 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1464 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1465 LU_XATTR_REPLACE, th);
1472 * Implementation of dt_object_operations::do_xattr_get.
1474 * If LOV EA is requested from the root object and it's not
1475 * found, then return default striping for the filesystem.
1477 * \see dt_object_operations::do_xattr_get() in the API description for details.
1479 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1480 struct lu_buf *buf, const char *name)
1482 struct lod_thread_info *info = lod_env_info(env);
1483 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1488 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1489 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1490 struct lmv_mds_md_v1 *lmv1;
1491 struct lmv_foreign_md *lfm;
1494 if (rc > (typeof(rc))sizeof(*lmv1))
1497 /* short (<= sizeof(struct lmv_mds_md_v1)) foreign LMV case */
1498 /* XXX empty foreign LMV is not allowed */
1499 if (rc <= offsetof(typeof(*lfm), lfm_value))
1500 RETURN(rc = rc > 0 ? -EINVAL : rc);
1502 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1503 CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1505 /* lti_buf is large enough for *lmv1 or a short
1506 * (<= sizeof(struct lmv_mds_md_v1)) foreign LMV
1508 info->lti_buf.lb_buf = info->lti_key;
1509 info->lti_buf.lb_len = sizeof(*lmv1);
1510 rc = dt_xattr_get(env, dt_object_child(dt),
1511 &info->lti_buf, name);
1512 if (unlikely(rc <= offsetof(typeof(*lfm),
1514 RETURN(rc = rc > 0 ? -EINVAL : rc);
1516 lfm = info->lti_buf.lb_buf;
1517 if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN)
1520 if (unlikely(rc != sizeof(*lmv1)))
1521 RETURN(rc = rc > 0 ? -EINVAL : rc);
1523 lmv1 = info->lti_buf.lb_buf;
1524 /* The on-disk LMV EA only contains header, but the
1525 * returned LMV EA size should contain the space for
1526 * the FIDs of all shards of the striped directory. */
1527 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1528 rc = lmv_mds_md_size(
1529 le32_to_cpu(lmv1->lmv_stripe_count),
1533 if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN)
1536 if (rc != sizeof(*lmv1))
1537 RETURN(rc = rc > 0 ? -EINVAL : rc);
1539 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1543 RETURN(rc = rc1 != 0 ? rc1 : rc);
1546 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1550 * XXX: Only used by lfsck
1552 * lod returns default striping on the real root of the device
1553 * this is like the root stores default striping for the whole
1554 * filesystem. historically we've been using a different approach
1555 * and store it in the config.
1557 dt_root_get(env, dev->lod_child, &info->lti_fid);
1558 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1560 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1561 struct lov_user_md *lum = buf->lb_buf;
1562 struct lov_desc *desc = &dev->lod_desc;
1564 if (buf->lb_buf == NULL) {
1566 } else if (buf->lb_len >= sizeof(*lum)) {
1567 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1568 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1569 lmm_oi_set_id(&lum->lmm_oi, 0);
1570 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1571 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1572 lum->lmm_stripe_size = cpu_to_le32(
1573 desc->ld_default_stripe_size);
1574 lum->lmm_stripe_count = cpu_to_le16(
1575 desc->ld_default_stripe_count);
1576 lum->lmm_stripe_offset = cpu_to_le16(
1577 desc->ld_default_stripe_offset);
1590 * Checks that the magic of the stripe is sane.
1592 * \param[in] lod lod device
1593 * \param[in] lum a buffer storing LMV EA to verify
1595 * \retval 0 if the EA is sane
1596 * \retval negative otherwise
1598 static int lod_verify_md_striping(struct lod_device *lod,
1599 const struct lmv_user_md_v1 *lum)
1601 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1602 CERROR("%s: invalid lmv_user_md: magic = %x, "
1603 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1604 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1605 (int)le32_to_cpu(lum->lum_stripe_offset),
1606 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1614 * Initialize LMV EA for a slave.
1616 * Initialize slave's LMV EA from the master's LMV EA.
1618 * \param[in] master_lmv a buffer containing master's EA
1619 * \param[out] slave_lmv a buffer where slave's EA will be stored
1622 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1623 const struct lmv_mds_md_v1 *master_lmv)
1625 *slave_lmv = *master_lmv;
1626 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1632 * Generate LMV EA from the object passed as \a dt. The object must have
1633 * the stripes created and initialized.
1635 * \param[in] env execution environment
1636 * \param[in] dt object
1637 * \param[out] lmv_buf buffer storing generated LMV EA
1639 * \retval 0 on success
1640 * \retval negative if failed
1642 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1643 struct lu_buf *lmv_buf)
1645 struct lod_thread_info *info = lod_env_info(env);
1646 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1647 struct lod_object *lo = lod_dt_obj(dt);
1648 struct lmv_mds_md_v1 *lmm1;
1650 int type = LU_SEQ_RANGE_ANY;
1655 LASSERT(lo->ldo_dir_striped != 0);
1656 LASSERT(lo->ldo_dir_stripe_count > 0);
1657 stripe_count = lo->ldo_dir_stripe_count;
1658 /* Only store the LMV EA heahder on the disk. */
1659 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1660 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1664 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1667 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1668 memset(lmm1, 0, sizeof(*lmm1));
1669 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1670 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1671 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1672 if (lo->ldo_dir_hash_type & LMV_HASH_FLAG_MIGRATION) {
1673 lmm1->lmv_migrate_hash = cpu_to_le32(lo->ldo_dir_migrate_hash);
1674 lmm1->lmv_migrate_offset =
1675 cpu_to_le32(lo->ldo_dir_migrate_offset);
1677 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1682 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1683 lmv_buf->lb_buf = info->lti_ea_store;
1684 lmv_buf->lb_len = sizeof(*lmm1);
1690 * Create in-core represenation for a striped directory.
1692 * Parse the buffer containing LMV EA and instantiate LU objects
1693 * representing the stripe objects. The pointers to the objects are
1694 * stored in ldo_stripe field of \a lo. This function is used when
1695 * we need to access an already created object (i.e. load from a disk).
1697 * \param[in] env execution environment
1698 * \param[in] lo lod object
1699 * \param[in] buf buffer containing LMV EA
1701 * \retval 0 on success
1702 * \retval negative if failed
1704 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1705 const struct lu_buf *buf)
1707 struct lod_thread_info *info = lod_env_info(env);
1708 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1709 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1710 struct dt_object **stripe;
1711 union lmv_mds_md *lmm = buf->lb_buf;
1712 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1713 struct lu_fid *fid = &info->lti_fid;
1718 LASSERT(mutex_is_locked(&lo->ldo_layout_mutex));
1720 /* XXX may be useless as not called for foreign LMV ?? */
1721 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_FOREIGN)
1724 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1725 lo->ldo_dir_slave_stripe = 1;
1729 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1732 if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1735 LASSERT(lo->ldo_stripe == NULL);
1736 OBD_ALLOC(stripe, sizeof(stripe[0]) *
1737 (le32_to_cpu(lmv1->lmv_stripe_count)));
1741 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1742 struct dt_device *tgt_dt;
1743 struct dt_object *dto;
1744 int type = LU_SEQ_RANGE_ANY;
1747 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1748 if (!fid_is_sane(fid)) {
1753 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1757 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1758 tgt_dt = lod->lod_child;
1760 struct lod_tgt_desc *tgt;
1762 tgt = LTD_TGT(ltd, idx);
1764 GOTO(out, rc = -ESTALE);
1765 tgt_dt = tgt->ltd_tgt;
1768 dto = dt_locate_at(env, tgt_dt, fid,
1769 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1772 GOTO(out, rc = PTR_ERR(dto));
1777 lo->ldo_stripe = stripe;
1778 lo->ldo_dir_stripe_count = le32_to_cpu(lmv1->lmv_stripe_count);
1779 lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1781 lod_striping_free_nolock(env, lo);
1787 * Declare create a striped directory.
1789 * Declare creating a striped directory with a given stripe pattern on the
1790 * specified MDTs. A striped directory is represented as a regular directory
1791 * - an index listing all the stripes. The stripes point back to the master
1792 * object with ".." and LinkEA. The master object gets LMV EA which
1793 * identifies it as a striped directory. The function allocates FIDs
1796 * \param[in] env execution environment
1797 * \param[in] dt object
1798 * \param[in] attr attributes to initialize the objects with
1799 * \param[in] dof type of objects to be created
1800 * \param[in] th transaction handle
1802 * \retval 0 on success
1803 * \retval negative if failed
1805 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1806 struct dt_object *dt,
1807 struct lu_attr *attr,
1808 struct dt_object_format *dof,
1811 struct lod_thread_info *info = lod_env_info(env);
1812 struct lu_buf lmv_buf;
1813 struct lu_buf slave_lmv_buf;
1814 struct lmv_mds_md_v1 *lmm;
1815 struct lmv_mds_md_v1 *slave_lmm = NULL;
1816 struct dt_insert_rec *rec = &info->lti_dt_rec;
1817 struct lod_object *lo = lod_dt_obj(dt);
1822 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1825 lmm = lmv_buf.lb_buf;
1827 OBD_ALLOC_PTR(slave_lmm);
1828 if (slave_lmm == NULL)
1829 GOTO(out, rc = -ENOMEM);
1831 lod_prep_slave_lmv_md(slave_lmm, lmm);
1832 slave_lmv_buf.lb_buf = slave_lmm;
1833 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1835 if (!dt_try_as_dir(env, dt_object_child(dt)))
1836 GOTO(out, rc = -EINVAL);
1838 rec->rec_type = S_IFDIR;
1839 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1840 struct dt_object *dto = lo->ldo_stripe[i];
1841 char *stripe_name = info->lti_key;
1842 struct lu_name *sname;
1843 struct linkea_data ldata = { NULL };
1844 struct lu_buf linkea_buf;
1846 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
1850 rc = lod_sub_declare_create(env, dto, attr, NULL, dof, th);
1854 if (!dt_try_as_dir(env, dto))
1855 GOTO(out, rc = -EINVAL);
1857 rc = lod_sub_declare_ref_add(env, dto, th);
1861 rec->rec_fid = lu_object_fid(&dto->do_lu);
1862 rc = lod_sub_declare_insert(env, dto,
1863 (const struct dt_rec *)rec,
1864 (const struct dt_key *)dot, th);
1868 /* master stripe FID will be put to .. */
1869 rec->rec_fid = lu_object_fid(&dt->do_lu);
1870 rc = lod_sub_declare_insert(env, dto,
1871 (const struct dt_rec *)rec,
1872 (const struct dt_key *)dotdot, th);
1876 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1877 cfs_fail_val != i) {
1878 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1880 slave_lmm->lmv_master_mdt_index =
1883 slave_lmm->lmv_master_mdt_index =
1885 rc = lod_sub_declare_xattr_set(env, dto, &slave_lmv_buf,
1886 XATTR_NAME_LMV, 0, th);
1891 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1893 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1894 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1896 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1897 PFID(lu_object_fid(&dto->do_lu)), i);
1899 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1900 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1901 sname, lu_object_fid(&dt->do_lu));
1905 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1906 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1907 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
1908 XATTR_NAME_LINK, 0, th);
1912 rec->rec_fid = lu_object_fid(&dto->do_lu);
1913 rc = lod_sub_declare_insert(env, dt_object_child(dt),
1914 (const struct dt_rec *)rec,
1915 (const struct dt_key *)stripe_name,
1920 rc = lod_sub_declare_ref_add(env, dt_object_child(dt), th);
1925 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt),
1926 &lmv_buf, XATTR_NAME_LMV, 0, th);
1930 if (slave_lmm != NULL)
1931 OBD_FREE_PTR(slave_lmm);
1936 static int lod_prep_md_striped_create(const struct lu_env *env,
1937 struct dt_object *dt,
1938 struct lu_attr *attr,
1939 const struct lmv_user_md_v1 *lum,
1940 struct dt_object_format *dof,
1943 struct lod_thread_info *info = lod_env_info(env);
1944 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1945 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1946 struct lod_object *lo = lod_dt_obj(dt);
1947 struct dt_object **stripe;
1954 bool is_specific = false;
1957 /* The lum has been verifed in lod_verify_md_striping */
1958 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC ||
1959 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC);
1961 stripe_count = lo->ldo_dir_stripe_count;
1963 OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1964 if (idx_array == NULL)
1967 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1969 GOTO(out_free, rc = -ENOMEM);
1971 /* Start index must be the master MDT */
1972 master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1973 idx_array[0] = master_index;
1974 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) {
1976 for (i = 1; i < stripe_count; i++)
1977 idx_array[i] = le32_to_cpu(lum->lum_objects[i].lum_mds);
1980 for (i = 0; i < stripe_count; i++) {
1981 struct lod_tgt_desc *tgt = NULL;
1982 struct dt_object *dto;
1983 struct lu_fid fid = { 0 };
1985 struct lu_object_conf conf = { 0 };
1986 struct dt_device *tgt_dt = NULL;
1988 /* Try to find next avaible target */
1990 for (j = 0; j < lod->lod_remote_mdt_count;
1991 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1992 bool already_allocated = false;
1995 CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
1996 idx, lod->lod_remote_mdt_count + 1, i);
1998 if (likely(!is_specific &&
1999 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
2000 /* check whether the idx already exists
2001 * in current allocated array */
2002 for (k = 0; k < i; k++) {
2003 if (idx_array[k] == idx) {
2004 already_allocated = true;
2009 if (already_allocated)
2013 /* Sigh, this index is not in the bitmap, let's check
2014 * next available target */
2015 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) &&
2016 idx != master_index)
2019 if (idx == master_index) {
2020 /* Allocate the FID locally */
2021 rc = obd_fid_alloc(env, lod->lod_child_exp,
2025 tgt_dt = lod->lod_child;
2029 /* check the status of the OSP */
2030 tgt = LTD_TGT(ltd, idx);
2034 tgt_dt = tgt->ltd_tgt;
2035 rc = dt_statfs(env, tgt_dt, &info->lti_osfs);
2037 /* this OSP doesn't feel well */
2042 rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
2051 /* Can not allocate more stripes */
2052 if (j == lod->lod_remote_mdt_count) {
2053 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
2054 lod2obd(lod)->obd_name, stripe_count, i);
2058 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
2059 idx, i, PFID(&fid));
2061 /* Set the start index for next stripe allocation */
2062 if (!is_specific && i < stripe_count - 1) {
2064 * for large dir test, put all other slaves on one
2065 * remote MDT, otherwise we may save too many local
2066 * slave locks which will exceed RS_MAX_LOCKS.
2068 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)))
2070 idx_array[i + 1] = (idx + 1) %
2071 (lod->lod_remote_mdt_count + 1);
2073 /* tgt_dt and fid must be ready after search avaible OSP
2074 * in the above loop */
2075 LASSERT(tgt_dt != NULL);
2076 LASSERT(fid_is_sane(&fid));
2078 /* fail a remote stripe FID allocation */
2079 if (i && OBD_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_FID))
2082 conf.loc_flags = LOC_F_NEW;
2083 dto = dt_locate_at(env, tgt_dt, &fid,
2084 dt->do_lu.lo_dev->ld_site->ls_top_dev,
2087 GOTO(out_put, rc = PTR_ERR(dto));
2091 lo->ldo_dir_striped = 1;
2092 lo->ldo_stripe = stripe;
2093 lo->ldo_dir_stripe_count = i;
2094 lo->ldo_dir_stripes_allocated = stripe_count;
2096 lo->ldo_dir_stripe_loaded = 1;
2098 if (lo->ldo_dir_stripe_count == 0)
2099 GOTO(out_put, rc = -ENOSPC);
2101 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
2107 for (i = 0; i < stripe_count; i++)
2108 if (stripe[i] != NULL)
2109 dt_object_put(env, stripe[i]);
2110 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
2111 lo->ldo_dir_stripe_count = 0;
2112 lo->ldo_dir_stripes_allocated = 0;
2113 lo->ldo_stripe = NULL;
2117 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
2124 * Alloc cached foreign LMV
2126 * \param[in] lo object
2127 * \param[in] size size of foreign LMV
2129 * \retval 0 on success
2130 * \retval negative if failed
2132 int lod_alloc_foreign_lmv(struct lod_object *lo, size_t size)
2134 OBD_ALLOC_LARGE(lo->ldo_foreign_lmv, size);
2135 if (lo->ldo_foreign_lmv == NULL)
2137 lo->ldo_foreign_lmv_size = size;
2138 lo->ldo_dir_is_foreign = 1;
2144 * Declare create striped md object.
2146 * The function declares intention to create a striped directory. This is a
2147 * wrapper for lod_prep_md_striped_create(). The only additional functionality
2148 * is to verify pattern \a lum_buf is good. Check that function for the details.
2150 * \param[in] env execution environment
2151 * \param[in] dt object
2152 * \param[in] attr attributes to initialize the objects with
2153 * \param[in] lum_buf a pattern specifying the number of stripes and
2155 * \param[in] dof type of objects to be created
2156 * \param[in] th transaction handle
2158 * \retval 0 on success
2159 * \retval negative if failed
2162 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
2163 struct dt_object *dt,
2164 struct lu_attr *attr,
2165 const struct lu_buf *lum_buf,
2166 struct dt_object_format *dof,
2169 struct lod_object *lo = lod_dt_obj(dt);
2170 struct lmv_user_md_v1 *lum = lum_buf->lb_buf;
2174 LASSERT(lum != NULL);
2176 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
2177 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
2178 (int)le32_to_cpu(lum->lum_stripe_offset));
2180 if (lo->ldo_dir_stripe_count == 0) {
2181 if (lo->ldo_dir_is_foreign) {
2182 rc = lod_alloc_foreign_lmv(lo, lum_buf->lb_len);
2185 memcpy(lo->ldo_foreign_lmv, lum, lum_buf->lb_len);
2186 lo->ldo_dir_stripe_loaded = 1;
2191 /* prepare dir striped objects */
2192 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2194 /* failed to create striping, let's reset
2195 * config so that others don't get confused */
2196 lod_striping_free(env, lo);
2204 * Append source stripes after target stripes for migrating directory. NB, we
2205 * only need to declare this, the append is done inside lod_xattr_set_lmv().
2207 * \param[in] env execution environment
2208 * \param[in] dt target object
2209 * \param[in] buf LMV buf which contains source stripe fids
2210 * \param[in] th transaction handle
2212 * \retval 0 on success
2213 * \retval negative if failed
2215 static int lod_dir_declare_layout_add(const struct lu_env *env,
2216 struct dt_object *dt,
2217 const struct lu_buf *buf,
2220 struct lod_thread_info *info = lod_env_info(env);
2221 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
2222 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
2223 struct lod_object *lo = lod_dt_obj(dt);
2224 struct dt_object *next = dt_object_child(dt);
2225 struct dt_object_format *dof = &info->lti_format;
2226 struct lmv_mds_md_v1 *lmv = buf->lb_buf;
2227 struct dt_object **stripe;
2228 __u32 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
2229 struct lu_fid *fid = &info->lti_fid;
2230 struct lod_tgt_desc *tgt;
2231 struct dt_object *dto;
2232 struct dt_device *tgt_dt;
2233 int type = LU_SEQ_RANGE_ANY;
2234 struct dt_insert_rec *rec = &info->lti_dt_rec;
2235 char *stripe_name = info->lti_key;
2236 struct lu_name *sname;
2237 struct linkea_data ldata = { NULL };
2238 struct lu_buf linkea_buf;
2245 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
2248 if (stripe_count == 0)
2251 dof->dof_type = DFT_DIR;
2254 sizeof(*stripe) * (lo->ldo_dir_stripe_count + stripe_count));
2258 for (i = 0; i < lo->ldo_dir_stripe_count; i++)
2259 stripe[i] = lo->ldo_stripe[i];
2261 for (i = 0; i < stripe_count; i++) {
2263 &lmv->lmv_stripe_fids[i]);
2264 if (!fid_is_sane(fid))
2267 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
2271 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
2272 tgt_dt = lod->lod_child;
2274 tgt = LTD_TGT(ltd, idx);
2276 GOTO(out, rc = -ESTALE);
2277 tgt_dt = tgt->ltd_tgt;
2280 dto = dt_locate_at(env, tgt_dt, fid,
2281 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
2284 GOTO(out, rc = PTR_ERR(dto));
2286 stripe[i + lo->ldo_dir_stripe_count] = dto;
2288 if (!dt_try_as_dir(env, dto))
2289 GOTO(out, rc = -ENOTDIR);
2291 rc = lod_sub_declare_ref_add(env, dto, th);
2295 rc = lod_sub_declare_insert(env, dto,
2296 (const struct dt_rec *)rec,
2297 (const struct dt_key *)dot, th);
2301 rc = lod_sub_declare_insert(env, dto,
2302 (const struct dt_rec *)rec,
2303 (const struct dt_key *)dotdot, th);
2307 rc = lod_sub_declare_xattr_set(env, dto, buf,
2308 XATTR_NAME_LMV, 0, th);
2312 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
2313 PFID(lu_object_fid(&dto->do_lu)),
2314 i + lo->ldo_dir_stripe_count);
2316 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2317 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
2318 sname, lu_object_fid(&dt->do_lu));
2322 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
2323 linkea_buf.lb_len = ldata.ld_leh->leh_len;
2324 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
2325 XATTR_NAME_LINK, 0, th);
2329 rc = lod_sub_declare_insert(env, next,
2330 (const struct dt_rec *)rec,
2331 (const struct dt_key *)stripe_name,
2336 rc = lod_sub_declare_ref_add(env, next, th);
2342 OBD_FREE(lo->ldo_stripe,
2343 sizeof(*stripe) * lo->ldo_dir_stripes_allocated);
2344 lo->ldo_stripe = stripe;
2345 lo->ldo_dir_migrate_offset = lo->ldo_dir_stripe_count;
2346 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_hash_type);
2347 lo->ldo_dir_stripe_count += stripe_count;
2348 lo->ldo_dir_stripes_allocated += stripe_count;
2349 lo->ldo_dir_hash_type |= LMV_HASH_FLAG_MIGRATION;
2353 i = lo->ldo_dir_stripe_count;
2354 while (i < lo->ldo_dir_stripe_count + stripe_count && stripe[i])
2355 dt_object_put(env, stripe[i++]);
2358 sizeof(*stripe) * (stripe_count + lo->ldo_dir_stripe_count));
2362 static int lod_dir_declare_layout_delete(const struct lu_env *env,
2363 struct dt_object *dt,
2364 const struct lu_buf *buf,
2367 struct lod_thread_info *info = lod_env_info(env);
2368 struct lod_object *lo = lod_dt_obj(dt);
2369 struct dt_object *next = dt_object_child(dt);
2370 struct lmv_user_md *lmu = buf->lb_buf;
2371 __u32 final_stripe_count;
2372 char *stripe_name = info->lti_key;
2373 struct dt_object *dto;
2380 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
2381 if (final_stripe_count >= lo->ldo_dir_stripe_count)
2384 for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
2385 dto = lo->ldo_stripe[i];
2389 if (!dt_try_as_dir(env, dto))
2392 rc = lod_sub_declare_delete(env, dto,
2393 (const struct dt_key *)dot, th);
2397 rc = lod_sub_declare_ref_del(env, dto, th);
2401 rc = lod_sub_declare_delete(env, dto,
2402 (const struct dt_key *)dotdot, th);
2406 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2407 PFID(lu_object_fid(&dto->do_lu)), i);
2409 rc = lod_sub_declare_delete(env, next,
2410 (const struct dt_key *)stripe_name, th);
2414 rc = lod_sub_declare_ref_del(env, next, th);
2423 * delete stripes from dir master object, the lum_stripe_count in argument is
2424 * the final stripe count, the stripes after that will be deleted, NB, they
2425 * are not destroyed, but deleted from it's parent namespace, this function
2426 * will be called in two places:
2427 * 1. mdd_migrate_create() delete stripes from source, and append them to
2429 * 2. mdd_dir_layout_shrink() delete stripes from source, and destroy them.
2431 static int lod_dir_layout_delete(const struct lu_env *env,
2432 struct dt_object *dt,
2433 const struct lu_buf *buf,
2436 struct lod_thread_info *info = lod_env_info(env);
2437 struct lod_object *lo = lod_dt_obj(dt);
2438 struct dt_object *next = dt_object_child(dt);
2439 struct lmv_user_md *lmu = buf->lb_buf;
2440 __u32 final_stripe_count;
2441 char *stripe_name = info->lti_key;
2442 struct dt_object *dto;
2451 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
2452 if (final_stripe_count >= lo->ldo_dir_stripe_count)
2455 for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
2456 dto = lo->ldo_stripe[i];
2460 rc = lod_sub_delete(env, dto,
2461 (const struct dt_key *)dotdot, th);
2465 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2466 PFID(lu_object_fid(&dto->do_lu)), i);
2468 rc = lod_sub_delete(env, next,
2469 (const struct dt_key *)stripe_name, th);
2473 rc = lod_sub_ref_del(env, next, th);
2478 lod_striping_free(env, lod_dt_obj(dt));
2484 * Implementation of dt_object_operations::do_declare_xattr_set.
2486 * Used with regular (non-striped) objects. Basically it
2487 * initializes the striping information and applies the
2488 * change to all the stripes.
2490 * \see dt_object_operations::do_declare_xattr_set() in the API description
2493 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2494 struct dt_object *dt,
2495 const struct lu_buf *buf,
2496 const char *name, int fl,
2499 struct dt_object *next = dt_object_child(dt);
2500 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2501 struct lod_object *lo = lod_dt_obj(dt);
2506 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2507 struct lmv_user_md_v1 *lum;
2509 LASSERT(buf != NULL && buf->lb_buf != NULL);
2511 rc = lod_verify_md_striping(d, lum);
2514 } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2515 rc = lod_verify_striping(d, lo, buf, false);
2520 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2524 /* Note: Do not set LinkEA on sub-stripes, otherwise
2525 * it will confuse the fid2path process(see mdt_path_current()).
2526 * The linkEA between master and sub-stripes is set in
2527 * lod_xattr_set_lmv(). */
2528 if (strcmp(name, XATTR_NAME_LINK) == 0)
2531 /* set xattr to each stripes, if needed */
2532 rc = lod_striping_load(env, lo);
2536 if (lo->ldo_dir_stripe_count == 0)
2539 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2540 if (!lo->ldo_stripe[i])
2543 if (!dt_object_exists(lo->ldo_stripe[i]))
2546 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2556 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2557 struct lod_object *lo,
2558 struct dt_object *dt, struct thandle *th,
2559 int comp_idx, int stripe_idx,
2560 struct lod_obj_stripe_cb_data *data)
2562 struct lod_thread_info *info = lod_env_info(env);
2563 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
2564 struct filter_fid *ff = &info->lti_ff;
2565 struct lu_buf *buf = &info->lti_buf;
2569 buf->lb_len = sizeof(*ff);
2570 rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2578 * locd_buf is set if it's called by dir migration, which doesn't check
2581 if (data->locd_buf) {
2582 memset(ff, 0, sizeof(*ff));
2583 ff->ff_parent = *(struct lu_fid *)data->locd_buf->lb_buf;
2585 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
2587 if (lu_fid_eq(lod_object_fid(lo), &ff->ff_parent) &&
2588 ff->ff_layout.ol_comp_id == comp->llc_id)
2591 memset(ff, 0, sizeof(*ff));
2592 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2595 /* rewrite filter_fid */
2596 ff->ff_parent.f_ver = stripe_idx;
2597 ff->ff_layout.ol_stripe_size = comp->llc_stripe_size;
2598 ff->ff_layout.ol_stripe_count = comp->llc_stripe_count;
2599 ff->ff_layout.ol_comp_id = comp->llc_id;
2600 ff->ff_layout.ol_comp_start = comp->llc_extent.e_start;
2601 ff->ff_layout.ol_comp_end = comp->llc_extent.e_end;
2602 filter_fid_cpu_to_le(ff, ff, sizeof(*ff));
2604 if (data->locd_declare)
2605 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2606 LU_XATTR_REPLACE, th);
2608 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2609 LU_XATTR_REPLACE, th);
2615 * Reset parent FID on OST object
2617 * Replace parent FID with @dt object FID, which is only called during migration
2618 * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2619 * the FID is changed.
2621 * \param[in] env execution environment
2622 * \param[in] dt dt_object whose stripes's parent FID will be reset
2623 * \parem[in] th thandle
2624 * \param[in] declare if it is declare
2626 * \retval 0 if reset succeeds
2627 * \retval negative errno if reset fails
2629 static int lod_replace_parent_fid(const struct lu_env *env,
2630 struct dt_object *dt,
2631 const struct lu_buf *buf,
2632 struct thandle *th, bool declare)
2634 struct lod_object *lo = lod_dt_obj(dt);
2635 struct lod_thread_info *info = lod_env_info(env);
2636 struct filter_fid *ff;
2637 struct lod_obj_stripe_cb_data data = { { 0 } };
2641 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2643 /* set xattr to each stripes, if needed */
2644 rc = lod_striping_load(env, lo);
2648 if (!lod_obj_is_striped(dt))
2651 if (info->lti_ea_store_size < sizeof(*ff)) {
2652 rc = lod_ea_store_resize(info, sizeof(*ff));
2657 data.locd_declare = declare;
2658 data.locd_stripe_cb = lod_obj_stripe_replace_parent_fid_cb;
2659 data.locd_buf = buf;
2660 rc = lod_obj_for_each_stripe(env, lo, th, &data);
2665 inline __u16 lod_comp_entry_stripe_count(struct lod_object *lo,
2666 struct lod_layout_component *entry,
2669 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2673 else if (lod_comp_inited(entry))
2674 return entry->llc_stripe_count;
2675 else if ((__u16)-1 == entry->llc_stripe_count)
2676 return lod->lod_desc.ld_tgt_count;
2678 return lod_get_stripe_count(lod, lo, entry->llc_stripe_count);
2681 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2683 int magic, size = 0, i;
2684 struct lod_layout_component *comp_entries;
2686 bool is_composite, is_foreign = false;
2689 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2690 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2692 lo->ldo_def_striping->lds_def_striping_is_composite;
2694 comp_cnt = lo->ldo_comp_cnt;
2695 comp_entries = lo->ldo_comp_entries;
2696 is_composite = lo->ldo_is_composite;
2697 is_foreign = lo->ldo_is_foreign;
2701 return lo->ldo_foreign_lov_size;
2703 LASSERT(comp_cnt != 0 && comp_entries != NULL);
2705 size = sizeof(struct lov_comp_md_v1) +
2706 sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2707 LASSERT(size % sizeof(__u64) == 0);
2710 for (i = 0; i < comp_cnt; i++) {
2713 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2714 stripe_count = lod_comp_entry_stripe_count(lo, &comp_entries[i],
2716 if (!is_dir && is_composite)
2717 lod_comp_shrink_stripe_count(&comp_entries[i],
2720 size += lov_user_md_size(stripe_count, magic);
2721 LASSERT(size % sizeof(__u64) == 0);
2727 * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2728 * the xattr value is binary lov_comp_md_v1 which contains component(s)
2731 * \param[in] env execution environment
2732 * \param[in] dt dt_object to add components on
2733 * \param[in] buf buffer contains components to be added
2734 * \parem[in] th thandle
2736 * \retval 0 on success
2737 * \retval negative errno on failure
2739 static int lod_declare_layout_add(const struct lu_env *env,
2740 struct dt_object *dt,
2741 const struct lu_buf *buf,
2744 struct lod_thread_info *info = lod_env_info(env);
2745 struct lod_layout_component *comp_array, *lod_comp, *old_array;
2746 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2747 struct dt_object *next = dt_object_child(dt);
2748 struct lov_desc *desc = &d->lod_desc;
2749 struct lod_object *lo = lod_dt_obj(dt);
2750 struct lov_user_md_v3 *v3;
2751 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2753 int i, rc, array_cnt, old_array_cnt;
2756 LASSERT(lo->ldo_is_composite);
2758 if (lo->ldo_flr_state != LCM_FL_NONE)
2761 rc = lod_verify_striping(d, lo, buf, false);
2765 magic = comp_v1->lcm_magic;
2766 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2767 lustre_swab_lov_comp_md_v1(comp_v1);
2768 magic = comp_v1->lcm_magic;
2771 if (magic != LOV_USER_MAGIC_COMP_V1)
2774 array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2775 OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2776 if (comp_array == NULL)
2779 memcpy(comp_array, lo->ldo_comp_entries,
2780 sizeof(*comp_array) * lo->ldo_comp_cnt);
2782 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2783 struct lov_user_md_v1 *v1;
2784 struct lu_extent *ext;
2786 v1 = (struct lov_user_md *)((char *)comp_v1 +
2787 comp_v1->lcm_entries[i].lcme_offset);
2788 ext = &comp_v1->lcm_entries[i].lcme_extent;
2790 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2791 lod_comp->llc_extent.e_start = ext->e_start;
2792 lod_comp->llc_extent.e_end = ext->e_end;
2793 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2794 lod_comp->llc_flags = comp_v1->lcm_entries[i].lcme_flags;
2796 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2797 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2798 lod_adjust_stripe_info(lod_comp, desc);
2800 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2801 v3 = (struct lov_user_md_v3 *) v1;
2802 if (v3->lmm_pool_name[0] != '\0') {
2803 rc = lod_set_pool(&lod_comp->llc_pool,
2811 old_array = lo->ldo_comp_entries;
2812 old_array_cnt = lo->ldo_comp_cnt;
2814 lo->ldo_comp_entries = comp_array;
2815 lo->ldo_comp_cnt = array_cnt;
2817 /* No need to increase layout generation here, it will be increased
2818 * later when generating component ID for the new components */
2820 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2821 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2822 XATTR_NAME_LOV, 0, th);
2824 lo->ldo_comp_entries = old_array;
2825 lo->ldo_comp_cnt = old_array_cnt;
2829 OBD_FREE(old_array, sizeof(*lod_comp) * old_array_cnt);
2831 LASSERT(lo->ldo_mirror_count == 1);
2832 lo->ldo_mirrors[0].lme_end = array_cnt - 1;
2837 for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2838 lod_comp = &comp_array[i];
2839 if (lod_comp->llc_pool != NULL) {
2840 OBD_FREE(lod_comp->llc_pool,
2841 strlen(lod_comp->llc_pool) + 1);
2842 lod_comp->llc_pool = NULL;
2845 OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2850 * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2851 * the '$field' can only be 'flags' now. The xattr value is binary
2852 * lov_comp_md_v1 which contains the component ID(s) and the value of
2853 * the field to be modified.
2855 * \param[in] env execution environment
2856 * \param[in] dt dt_object to be modified
2857 * \param[in] op operation string, like "set.flags"
2858 * \param[in] buf buffer contains components to be set
2859 * \parem[in] th thandle
2861 * \retval 0 on success
2862 * \retval negative errno on failure
2864 static int lod_declare_layout_set(const struct lu_env *env,
2865 struct dt_object *dt,
2866 char *op, const struct lu_buf *buf,
2869 struct lod_layout_component *lod_comp;
2870 struct lod_thread_info *info = lod_env_info(env);
2871 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2872 struct lod_object *lo = lod_dt_obj(dt);
2873 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2876 bool changed = false;
2879 if (strcmp(op, "set.flags") != 0) {
2880 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2881 lod2obd(d)->obd_name, op);
2885 magic = comp_v1->lcm_magic;
2886 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2887 lustre_swab_lov_comp_md_v1(comp_v1);
2888 magic = comp_v1->lcm_magic;
2891 if (magic != LOV_USER_MAGIC_COMP_V1)
2894 if (comp_v1->lcm_entry_count == 0) {
2895 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2896 lod2obd(d)->obd_name);
2900 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2901 __u32 id = comp_v1->lcm_entries[i].lcme_id;
2902 __u32 flags = comp_v1->lcm_entries[i].lcme_flags;
2903 __u32 mirror_flag = flags & LCME_MIRROR_FLAGS;
2904 bool neg = flags & LCME_FL_NEG;
2906 if (flags & LCME_FL_INIT) {
2908 lod_striping_free(env, lo);
2912 flags &= ~(LCME_MIRROR_FLAGS | LCME_FL_NEG);
2913 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2914 lod_comp = &lo->ldo_comp_entries[j];
2916 /* lfs only put one flag in each entry */
2917 if ((flags && id != lod_comp->llc_id) ||
2918 (mirror_flag && mirror_id_of(id) !=
2919 mirror_id_of(lod_comp->llc_id)))
2924 lod_comp->llc_flags &= ~flags;
2926 lod_comp->llc_flags &= ~mirror_flag;
2929 lod_comp->llc_flags |= flags;
2931 lod_comp->llc_flags |= mirror_flag;
2932 if (mirror_flag & LCME_FL_NOSYNC)
2933 lod_comp->llc_timestamp =
2934 ktime_get_real_seconds();
2942 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2943 lod2obd(d)->obd_name);
2947 lod_obj_inc_layout_gen(lo);
2949 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2950 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), &info->lti_buf,
2951 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
2956 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2957 * and the xattr value is a unique component ID or a special lcme_id.
2959 * \param[in] env execution environment
2960 * \param[in] dt dt_object to be operated on
2961 * \param[in] buf buffer contains component ID or lcme_id
2962 * \parem[in] th thandle
2964 * \retval 0 on success
2965 * \retval negative errno on failure
2967 static int lod_declare_layout_del(const struct lu_env *env,
2968 struct dt_object *dt,
2969 const struct lu_buf *buf,
2972 struct lod_thread_info *info = lod_env_info(env);
2973 struct dt_object *next = dt_object_child(dt);
2974 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2975 struct lod_object *lo = lod_dt_obj(dt);
2976 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2977 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2978 __u32 magic, id, flags, neg_flags = 0;
2982 LASSERT(lo->ldo_is_composite);
2984 if (lo->ldo_flr_state != LCM_FL_NONE)
2987 magic = comp_v1->lcm_magic;
2988 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2989 lustre_swab_lov_comp_md_v1(comp_v1);
2990 magic = comp_v1->lcm_magic;
2993 if (magic != LOV_USER_MAGIC_COMP_V1)
2996 id = comp_v1->lcm_entries[0].lcme_id;
2997 flags = comp_v1->lcm_entries[0].lcme_flags;
2999 if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
3000 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
3001 lod2obd(d)->obd_name, id, flags);
3005 if (id != LCME_ID_INVAL && flags != 0) {
3006 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
3007 lod2obd(d)->obd_name);
3011 if (id == LCME_ID_INVAL && !flags) {
3012 CDEBUG(D_LAYOUT, "%s: no id or flags specified.\n",
3013 lod2obd(d)->obd_name);
3017 if (flags & LCME_FL_NEG) {
3018 neg_flags = flags & ~LCME_FL_NEG;
3022 left = lo->ldo_comp_cnt;
3026 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3027 struct lod_layout_component *lod_comp;
3029 lod_comp = &lo->ldo_comp_entries[i];
3031 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
3033 else if (flags && !(flags & lod_comp->llc_flags))
3035 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
3038 if (left != (i + 1)) {
3039 CDEBUG(D_LAYOUT, "%s: this deletion will create "
3040 "a hole.\n", lod2obd(d)->obd_name);
3045 /* Mark the component as deleted */
3046 lod_comp->llc_id = LCME_ID_INVAL;
3048 /* Not instantiated component */
3049 if (lod_comp->llc_stripe == NULL)
3052 LASSERT(lod_comp->llc_stripe_count > 0);
3053 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
3054 struct dt_object *obj = lod_comp->llc_stripe[j];
3058 rc = lod_sub_declare_destroy(env, obj, th);
3064 LASSERTF(left >= 0, "left = %d\n", left);
3065 if (left == lo->ldo_comp_cnt) {
3066 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
3067 lod2obd(d)->obd_name, id);
3071 memset(attr, 0, sizeof(*attr));
3072 attr->la_valid = LA_SIZE;
3073 rc = lod_sub_declare_attr_set(env, next, attr, th);
3078 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
3079 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
3080 XATTR_NAME_LOV, 0, th);
3082 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
3089 * Declare layout add/set/del operations issued by special xattr names:
3091 * XATTR_LUSTRE_LOV.add add component(s) to existing file
3092 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
3093 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
3095 * \param[in] env execution environment
3096 * \param[in] dt object
3097 * \param[in] name name of xattr
3098 * \param[in] buf lu_buf contains xattr value
3099 * \param[in] th transaction handle
3101 * \retval 0 on success
3102 * \retval negative if failed
3104 static int lod_declare_modify_layout(const struct lu_env *env,
3105 struct dt_object *dt,
3107 const struct lu_buf *buf,
3110 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3111 struct lod_object *lo = lod_dt_obj(dt);
3113 int rc, len = strlen(XATTR_LUSTRE_LOV);
3116 LASSERT(dt_object_exists(dt));
3118 if (strlen(name) <= len || name[len] != '.') {
3119 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
3120 lod2obd(d)->obd_name, name);
3125 rc = lod_striping_load(env, lo);
3129 /* the layout to be modified must be a composite layout */
3130 if (!lo->ldo_is_composite) {
3131 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
3132 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3133 GOTO(unlock, rc = -EINVAL);
3136 op = (char *)name + len;
3137 if (strcmp(op, "add") == 0) {
3138 rc = lod_declare_layout_add(env, dt, buf, th);
3139 } else if (strcmp(op, "del") == 0) {
3140 rc = lod_declare_layout_del(env, dt, buf, th);
3141 } else if (strncmp(op, "set", strlen("set")) == 0) {
3142 rc = lod_declare_layout_set(env, dt, op, buf, th);
3144 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
3145 lod2obd(d)->obd_name, name);
3146 GOTO(unlock, rc = -ENOTSUPP);
3150 lod_striping_free(env, lo);
3156 * Convert a plain file lov_mds_md to a composite layout.
3158 * \param[in,out] info the thread info::lti_ea_store buffer contains little
3159 * endian plain file layout
3161 * \retval 0 on success, <0 on failure
3163 static int lod_layout_convert(struct lod_thread_info *info)
3165 struct lov_mds_md *lmm = info->lti_ea_store;
3166 struct lov_mds_md *lmm_save;
3167 struct lov_comp_md_v1 *lcm;
3168 struct lov_comp_md_entry_v1 *lcme;
3174 /* realloc buffer to a composite layout which contains one component */
3175 blob_size = lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
3176 le32_to_cpu(lmm->lmm_magic));
3177 size = sizeof(*lcm) + sizeof(*lcme) + blob_size;
3179 OBD_ALLOC_LARGE(lmm_save, blob_size);
3181 GOTO(out, rc = -ENOMEM);
3183 memcpy(lmm_save, lmm, blob_size);
3185 if (info->lti_ea_store_size < size) {
3186 rc = lod_ea_store_resize(info, size);
3191 lcm = info->lti_ea_store;
3192 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
3193 lcm->lcm_size = cpu_to_le32(size);
3194 lcm->lcm_layout_gen = cpu_to_le32(le16_to_cpu(
3195 lmm_save->lmm_layout_gen));
3196 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
3197 lcm->lcm_entry_count = cpu_to_le16(1);
3198 lcm->lcm_mirror_count = 0;
3200 lcme = &lcm->lcm_entries[0];
3201 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
3202 lcme->lcme_extent.e_start = 0;
3203 lcme->lcme_extent.e_end = cpu_to_le64(OBD_OBJECT_EOF);
3204 lcme->lcme_offset = cpu_to_le32(sizeof(*lcm) + sizeof(*lcme));
3205 lcme->lcme_size = cpu_to_le32(blob_size);
3207 memcpy((char *)lcm + lcme->lcme_offset, (char *)lmm_save, blob_size);
3212 OBD_FREE_LARGE(lmm_save, blob_size);
3217 * Merge layouts to form a mirrored file.
3219 static int lod_declare_layout_merge(const struct lu_env *env,
3220 struct dt_object *dt, const struct lu_buf *mbuf,
3223 struct lod_thread_info *info = lod_env_info(env);
3224 struct lu_buf *buf = &info->lti_buf;
3225 struct lod_object *lo = lod_dt_obj(dt);
3226 struct lov_comp_md_v1 *lcm;
3227 struct lov_comp_md_v1 *cur_lcm;
3228 struct lov_comp_md_v1 *merge_lcm;
3229 struct lov_comp_md_entry_v1 *lcme;
3232 __u16 cur_entry_count;
3233 __u16 merge_entry_count;
3235 __u16 mirror_id = 0;
3240 merge_lcm = mbuf->lb_buf;
3241 if (mbuf->lb_len < sizeof(*merge_lcm))
3244 /* must be an existing layout from disk */
3245 if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
3248 merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
3250 /* do not allow to merge two mirrored files */
3251 if (le16_to_cpu(merge_lcm->lcm_mirror_count))
3254 /* verify the target buffer */
3255 rc = lod_get_lov_ea(env, lo);
3257 RETURN(rc ? : -ENODATA);
3259 cur_lcm = info->lti_ea_store;
3260 switch (le32_to_cpu(cur_lcm->lcm_magic)) {
3263 rc = lod_layout_convert(info);
3265 case LOV_MAGIC_COMP_V1:
3274 /* info->lti_ea_store could be reallocated in lod_layout_convert() */
3275 cur_lcm = info->lti_ea_store;
3276 cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
3278 /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
3279 mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
3280 if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
3283 /* size of new layout */
3284 size = le32_to_cpu(cur_lcm->lcm_size) +
3285 le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
3287 memset(buf, 0, sizeof(*buf));
3288 lu_buf_alloc(buf, size);
3289 if (buf->lb_buf == NULL)
3293 memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
3295 offset = sizeof(*lcm) +
3296 sizeof(*lcme) * (cur_entry_count + merge_entry_count);
3297 for (i = 0; i < cur_entry_count; i++) {
3298 struct lov_comp_md_entry_v1 *cur_lcme;
3300 lcme = &lcm->lcm_entries[i];
3301 cur_lcme = &cur_lcm->lcm_entries[i];
3303 lcme->lcme_offset = cpu_to_le32(offset);
3304 memcpy((char *)lcm + offset,
3305 (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
3306 le32_to_cpu(lcme->lcme_size));
3308 offset += le32_to_cpu(lcme->lcme_size);
3310 if (mirror_count == 1 &&
3311 mirror_id_of(le32_to_cpu(lcme->lcme_id)) == 0) {
3312 /* Add mirror from a non-flr file, create new mirror ID.
3313 * Otherwise, keep existing mirror's component ID, used
3314 * for mirror extension.
3316 id = pflr_id(1, i + 1);
3317 lcme->lcme_id = cpu_to_le32(id);
3320 id = MAX(le32_to_cpu(lcme->lcme_id), id);
3323 mirror_id = mirror_id_of(id) + 1;
3324 for (i = 0; i < merge_entry_count; i++) {
3325 struct lov_comp_md_entry_v1 *merge_lcme;
3327 merge_lcme = &merge_lcm->lcm_entries[i];
3328 lcme = &lcm->lcm_entries[cur_entry_count + i];
3330 *lcme = *merge_lcme;
3331 lcme->lcme_offset = cpu_to_le32(offset);
3333 id = pflr_id(mirror_id, i + 1);
3334 lcme->lcme_id = cpu_to_le32(id);
3336 memcpy((char *)lcm + offset,
3337 (char *)merge_lcm + le32_to_cpu(merge_lcme->lcme_offset),
3338 le32_to_cpu(lcme->lcme_size));
3340 offset += le32_to_cpu(lcme->lcme_size);
3343 /* fixup layout information */
3344 lod_obj_inc_layout_gen(lo);
3345 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3346 lcm->lcm_size = cpu_to_le32(size);
3347 lcm->lcm_entry_count = cpu_to_le16(cur_entry_count + merge_entry_count);
3348 lcm->lcm_mirror_count = cpu_to_le16(mirror_count);
3349 if ((le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK) == LCM_FL_NONE)
3350 lcm->lcm_flags = cpu_to_le32(LCM_FL_RDONLY);
3352 rc = lod_striping_reload(env, lo, buf);
3356 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), buf,
3357 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3365 * Split layouts, just set the LOVEA with the layout from mbuf.
3367 static int lod_declare_layout_split(const struct lu_env *env,
3368 struct dt_object *dt, const struct lu_buf *mbuf,
3371 struct lod_object *lo = lod_dt_obj(dt);
3372 struct lov_comp_md_v1 *lcm = mbuf->lb_buf;
3376 lod_obj_inc_layout_gen(lo);
3377 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3379 rc = lod_striping_reload(env, lo, mbuf);
3383 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), mbuf,
3384 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3389 * Implementation of dt_object_operations::do_declare_xattr_set.
3391 * \see dt_object_operations::do_declare_xattr_set() in the API description
3394 * the extension to the API:
3395 * - declaring LOVEA requests striping creation
3396 * - LU_XATTR_REPLACE means layout swap
3398 static int lod_declare_xattr_set(const struct lu_env *env,
3399 struct dt_object *dt,
3400 const struct lu_buf *buf,
3401 const char *name, int fl,
3404 struct dt_object *next = dt_object_child(dt);
3405 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3410 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
3411 if ((S_ISREG(mode) || mode == 0) &&
3412 !(fl & (LU_XATTR_REPLACE | LU_XATTR_MERGE | LU_XATTR_SPLIT)) &&
3413 (strcmp(name, XATTR_NAME_LOV) == 0 ||
3414 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
3416 * this is a request to create object's striping.
3418 * allow to declare predefined striping on a new (!mode) object
3419 * which is supposed to be replay of regular file creation
3420 * (when LOV setting is declared)
3422 * LU_XATTR_REPLACE is set to indicate a layout swap
3424 if (dt_object_exists(dt)) {
3425 rc = dt_attr_get(env, next, attr);
3429 memset(attr, 0, sizeof(*attr));
3430 attr->la_valid = LA_TYPE | LA_MODE;
3431 attr->la_mode = S_IFREG;
3433 rc = lod_declare_striped_create(env, dt, attr, buf, th);
3434 } else if (fl & LU_XATTR_MERGE) {
3435 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3436 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3437 rc = lod_declare_layout_merge(env, dt, buf, th);
3438 } else if (fl & LU_XATTR_SPLIT) {
3439 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3440 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3441 rc = lod_declare_layout_split(env, dt, buf, th);
3442 } else if (S_ISREG(mode) &&
3443 strlen(name) > strlen(XATTR_LUSTRE_LOV) + 1 &&
3444 strncmp(name, XATTR_LUSTRE_LOV,
3445 strlen(XATTR_LUSTRE_LOV)) == 0) {
3447 * this is a request to modify object's striping.
3448 * add/set/del component(s).
3450 if (!dt_object_exists(dt))
3453 rc = lod_declare_modify_layout(env, dt, name, buf, th);
3454 } else if (strncmp(name, XATTR_NAME_LMV, strlen(XATTR_NAME_LMV)) == 0 &&
3455 strlen(name) > strlen(XATTR_NAME_LMV) + 1) {
3456 const char *op = name + strlen(XATTR_NAME_LMV) + 1;
3459 if (strcmp(op, "add") == 0)
3460 rc = lod_dir_declare_layout_add(env, dt, buf, th);
3461 else if (strcmp(op, "del") == 0)
3462 rc = lod_dir_declare_layout_delete(env, dt, buf, th);
3463 else if (strcmp(op, "set") == 0)
3464 rc = lod_sub_declare_xattr_set(env, next, buf,
3465 XATTR_NAME_LMV, fl, th);
3468 } else if (S_ISDIR(mode)) {
3469 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
3470 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3471 rc = lod_replace_parent_fid(env, dt, buf, th, true);
3473 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
3480 * Apply xattr changes to the object.
3482 * Applies xattr changes to the object and the stripes if the latter exist.
3484 * \param[in] env execution environment
3485 * \param[in] dt object
3486 * \param[in] buf buffer pointing to the new value of xattr
3487 * \param[in] name name of xattr
3488 * \param[in] fl flags
3489 * \param[in] th transaction handle
3491 * \retval 0 on success
3492 * \retval negative if failed
3494 static int lod_xattr_set_internal(const struct lu_env *env,
3495 struct dt_object *dt,
3496 const struct lu_buf *buf,
3497 const char *name, int fl,
3500 struct dt_object *next = dt_object_child(dt);
3501 struct lod_object *lo = lod_dt_obj(dt);
3506 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3507 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3510 /* Note: Do not set LinkEA on sub-stripes, otherwise
3511 * it will confuse the fid2path process(see mdt_path_current()).
3512 * The linkEA between master and sub-stripes is set in
3513 * lod_xattr_set_lmv(). */
3514 if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
3517 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3518 if (!lo->ldo_stripe[i])
3521 if (!dt_object_exists(lo->ldo_stripe[i]))
3524 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
3534 * Delete an extended attribute.
3536 * Deletes specified xattr from the object and the stripes if the latter exist.
3538 * \param[in] env execution environment
3539 * \param[in] dt object
3540 * \param[in] name name of xattr
3541 * \param[in] th transaction handle
3543 * \retval 0 on success
3544 * \retval negative if failed
3546 static int lod_xattr_del_internal(const struct lu_env *env,
3547 struct dt_object *dt,
3548 const char *name, struct thandle *th)
3550 struct dt_object *next = dt_object_child(dt);
3551 struct lod_object *lo = lod_dt_obj(dt);
3556 rc = lod_sub_xattr_del(env, next, name, th);
3557 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3560 if (lo->ldo_dir_stripe_count == 0)
3563 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3564 LASSERT(lo->ldo_stripe[i]);
3566 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3575 * Set default striping on a directory.
3577 * Sets specified striping on a directory object unless it matches the default
3578 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3579 * EA. This striping will be used when regular file is being created in this
3582 * \param[in] env execution environment
3583 * \param[in] dt the striped object
3584 * \param[in] buf buffer with the striping
3585 * \param[in] name name of EA
3586 * \param[in] fl xattr flag (see OSD API description)
3587 * \param[in] th transaction handle
3589 * \retval 0 on success
3590 * \retval negative if failed
3592 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
3593 struct dt_object *dt,
3594 const struct lu_buf *buf,
3595 const char *name, int fl,
3598 struct lov_user_md_v1 *lum;
3599 struct lov_user_md_v3 *v3 = NULL;
3600 const char *pool_name = NULL;
3605 LASSERT(buf != NULL && buf->lb_buf != NULL);
3608 switch (lum->lmm_magic) {
3609 case LOV_USER_MAGIC_SPECIFIC:
3610 case LOV_USER_MAGIC_V3:
3612 if (v3->lmm_pool_name[0] != '\0')
3613 pool_name = v3->lmm_pool_name;
3615 case LOV_USER_MAGIC_V1:
3616 /* if { size, offset, count } = { 0, -1, 0 } and no pool
3617 * (i.e. all default values specified) then delete default
3618 * striping from dir. */
3620 "set default striping: sz %u # %u offset %d %s %s\n",
3621 (unsigned)lum->lmm_stripe_size,
3622 (unsigned)lum->lmm_stripe_count,
3623 (int)lum->lmm_stripe_offset,
3624 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
3626 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
3627 lum->lmm_stripe_count,
3628 lum->lmm_stripe_offset,
3631 case LOV_USER_MAGIC_COMP_V1:
3635 CERROR("Invalid magic %x\n", lum->lmm_magic);
3640 rc = lod_xattr_del_internal(env, dt, name, th);
3644 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3651 * Set default striping on a directory object.
3653 * Sets specified striping on a directory object unless it matches the default
3654 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3655 * EA. This striping will be used when a new directory is being created in the
3658 * \param[in] env execution environment
3659 * \param[in] dt the striped object
3660 * \param[in] buf buffer with the striping
3661 * \param[in] name name of EA
3662 * \param[in] fl xattr flag (see OSD API description)
3663 * \param[in] th transaction handle
3665 * \retval 0 on success
3666 * \retval negative if failed
3668 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
3669 struct dt_object *dt,
3670 const struct lu_buf *buf,
3671 const char *name, int fl,
3674 struct lmv_user_md_v1 *lum;
3678 LASSERT(buf != NULL && buf->lb_buf != NULL);
3681 CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
3682 le32_to_cpu(lum->lum_stripe_count),
3683 (int)le32_to_cpu(lum->lum_stripe_offset));
3685 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
3686 le32_to_cpu(lum->lum_stripe_offset)) &&
3687 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
3688 rc = lod_xattr_del_internal(env, dt, name, th);
3692 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3701 * Turn directory into a striped directory.
3703 * During replay the client sends the striping created before MDT
3704 * failure, then the layer above LOD sends this defined striping
3705 * using ->do_xattr_set(), so LOD uses this method to replay creation
3706 * of the stripes. Notice the original information for the striping
3707 * (#stripes, FIDs, etc) was transferred in declare path.
3709 * \param[in] env execution environment
3710 * \param[in] dt the striped object
3711 * \param[in] buf not used currently
3712 * \param[in] name not used currently
3713 * \param[in] fl xattr flag (see OSD API description)
3714 * \param[in] th transaction handle
3716 * \retval 0 on success
3717 * \retval negative if failed
3719 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
3720 const struct lu_buf *buf, const char *name,
3721 int fl, struct thandle *th)
3723 struct lod_object *lo = lod_dt_obj(dt);
3724 struct lod_thread_info *info = lod_env_info(env);
3725 struct lu_attr *attr = &info->lti_attr;
3726 struct dt_object_format *dof = &info->lti_format;
3727 struct lu_buf lmv_buf;
3728 struct lu_buf slave_lmv_buf;
3729 struct lmv_mds_md_v1 *lmm;
3730 struct lmv_mds_md_v1 *slave_lmm = NULL;
3731 struct dt_insert_rec *rec = &info->lti_dt_rec;
3736 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3739 /* The stripes are supposed to be allocated in declare phase,
3740 * if there are no stripes being allocated, it will skip */
3741 if (lo->ldo_dir_stripe_count == 0) {
3742 if (lo->ldo_dir_is_foreign) {
3743 rc = lod_sub_xattr_set(env, dt_object_child(dt), buf,
3744 XATTR_NAME_LMV, fl, th);
3751 rc = dt_attr_get(env, dt_object_child(dt), attr);
3755 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
3756 LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
3757 dof->dof_type = DFT_DIR;
3759 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
3762 lmm = lmv_buf.lb_buf;
3764 OBD_ALLOC_PTR(slave_lmm);
3765 if (slave_lmm == NULL)
3768 lod_prep_slave_lmv_md(slave_lmm, lmm);
3769 slave_lmv_buf.lb_buf = slave_lmm;
3770 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
3772 rec->rec_type = S_IFDIR;
3773 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3774 struct dt_object *dto = lo->ldo_stripe[i];
3775 char *stripe_name = info->lti_key;
3776 struct lu_name *sname;
3777 struct linkea_data ldata = { NULL };
3778 struct lu_buf linkea_buf;
3780 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
3784 /* fail a remote stripe creation */
3785 if (i && OBD_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_CREATE))
3788 /* if it's source stripe of migrating directory, don't create */
3789 if (!((lo->ldo_dir_hash_type & LMV_HASH_FLAG_MIGRATION) &&
3790 i >= lo->ldo_dir_migrate_offset)) {
3791 dt_write_lock(env, dto, MOR_TGT_CHILD);
3792 rc = lod_sub_create(env, dto, attr, NULL, dof, th);
3794 dt_write_unlock(env, dto);
3798 rc = lod_sub_ref_add(env, dto, th);
3799 dt_write_unlock(env, dto);
3803 rec->rec_fid = lu_object_fid(&dto->do_lu);
3804 rc = lod_sub_insert(env, dto,
3805 (const struct dt_rec *)rec,
3806 (const struct dt_key *)dot, th);
3811 rec->rec_fid = lu_object_fid(&dt->do_lu);
3812 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
3813 (const struct dt_key *)dotdot, th);
3817 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
3818 cfs_fail_val != i) {
3819 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
3821 slave_lmm->lmv_master_mdt_index =
3824 slave_lmm->lmv_master_mdt_index =
3827 rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
3828 XATTR_NAME_LMV, fl, th);
3833 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
3835 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3836 PFID(lu_object_fid(&dto->do_lu)), i + 1);
3838 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3839 PFID(lu_object_fid(&dto->do_lu)), i);
3841 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
3842 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
3843 sname, lu_object_fid(&dt->do_lu));
3847 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
3848 linkea_buf.lb_len = ldata.ld_leh->leh_len;
3849 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
3850 XATTR_NAME_LINK, 0, th);
3854 rec->rec_fid = lu_object_fid(&dto->do_lu);
3855 rc = lod_sub_insert(env, dt_object_child(dt),
3856 (const struct dt_rec *)rec,
3857 (const struct dt_key *)stripe_name, th);
3861 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
3866 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
3867 rc = lod_sub_xattr_set(env, dt_object_child(dt),
3868 &lmv_buf, XATTR_NAME_LMV, fl, th);
3870 if (slave_lmm != NULL)
3871 OBD_FREE_PTR(slave_lmm);
3877 * Helper function to declare/execute creation of a striped directory
3879 * Called in declare/create object path, prepare striping for a directory
3880 * and prepare defaults data striping for the objects to be created in
3881 * that directory. Notice the function calls "declaration" or "execution"
3882 * methods depending on \a declare param. This is a consequence of the
3883 * current approach while we don't have natural distributed transactions:
3884 * we basically execute non-local updates in the declare phase. So, the
3885 * arguments for the both phases are the same and this is the reason for
3886 * this function to exist.
3888 * \param[in] env execution environment
3889 * \param[in] dt object
3890 * \param[in] attr attributes the stripes will be created with
3891 * \param[in] lmu lmv_user_md if MDT indices are specified
3892 * \param[in] dof format of stripes (see OSD API description)
3893 * \param[in] th transaction handle
3894 * \param[in] declare where to call "declare" or "execute" methods
3896 * \retval 0 on success
3897 * \retval negative if failed
3899 static int lod_dir_striping_create_internal(const struct lu_env *env,
3900 struct dt_object *dt,
3901 struct lu_attr *attr,
3902 const struct lu_buf *lmu,
3903 struct dt_object_format *dof,
3907 struct lod_thread_info *info = lod_env_info(env);
3908 struct lod_object *lo = lod_dt_obj(dt);
3909 const struct lod_default_striping *lds = lo->ldo_def_striping;
3913 LASSERT(ergo(lds != NULL,
3914 lds->lds_def_striping_set ||
3915 lds->lds_dir_def_striping_set));
3917 if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
3918 lo->ldo_dir_stripe_offset)) {
3920 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3921 int stripe_count = lo->ldo_dir_stripe_count;
3923 if (info->lti_ea_store_size < sizeof(*v1)) {
3924 rc = lod_ea_store_resize(info, sizeof(*v1));
3927 v1 = info->lti_ea_store;
3930 memset(v1, 0, sizeof(*v1));
3931 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3932 v1->lum_stripe_count = cpu_to_le32(stripe_count);
3933 v1->lum_stripe_offset =
3934 cpu_to_le32(lo->ldo_dir_stripe_offset);
3936 info->lti_buf.lb_buf = v1;
3937 info->lti_buf.lb_len = sizeof(*v1);
3938 lmu = &info->lti_buf;
3942 rc = lod_declare_xattr_set_lmv(env, dt, attr, lmu, dof,
3945 rc = lod_xattr_set_lmv(env, dt, lmu, XATTR_NAME_LMV, 0,
3950 /* foreign LMV EA case */
3952 struct lmv_foreign_md *lfm = lmu->lb_buf;
3954 if (lfm->lfm_magic == LMV_MAGIC_FOREIGN) {
3955 rc = lod_declare_xattr_set_lmv(env, dt, attr,
3959 if (lo->ldo_dir_is_foreign) {
3960 LASSERT(lo->ldo_foreign_lmv != NULL &&
3961 lo->ldo_foreign_lmv_size > 0);
3962 info->lti_buf.lb_buf = lo->ldo_foreign_lmv;
3963 info->lti_buf.lb_len = lo->ldo_foreign_lmv_size;
3964 lmu = &info->lti_buf;
3965 rc = lod_xattr_set_lmv(env, dt, lmu,
3966 XATTR_NAME_LMV, 0, th);
3971 /* Transfer default LMV striping from the parent */
3972 if (lds != NULL && lds->lds_dir_def_striping_set &&
3973 !LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
3974 lds->lds_dir_def_stripe_offset)) {
3975 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3977 if (info->lti_ea_store_size < sizeof(*v1)) {
3978 rc = lod_ea_store_resize(info, sizeof(*v1));
3981 v1 = info->lti_ea_store;
3984 memset(v1, 0, sizeof(*v1));
3985 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3986 v1->lum_stripe_count =
3987 cpu_to_le32(lds->lds_dir_def_stripe_count);
3988 v1->lum_stripe_offset =
3989 cpu_to_le32(lds->lds_dir_def_stripe_offset);
3991 cpu_to_le32(lds->lds_dir_def_hash_type);
3993 info->lti_buf.lb_buf = v1;
3994 info->lti_buf.lb_len = sizeof(*v1);
3996 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3997 XATTR_NAME_DEFAULT_LMV,
4000 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
4002 XATTR_NAME_DEFAULT_LMV, 0,
4008 /* Transfer default LOV striping from the parent */
4009 if (lds != NULL && lds->lds_def_striping_set &&
4010 lds->lds_def_comp_cnt != 0) {
4011 struct lov_mds_md *lmm;
4012 int lmm_size = lod_comp_md_size(lo, true);
4014 if (info->lti_ea_store_size < lmm_size) {
4015 rc = lod_ea_store_resize(info, lmm_size);
4019 lmm = info->lti_ea_store;
4021 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
4025 info->lti_buf.lb_buf = lmm;
4026 info->lti_buf.lb_len = lmm_size;
4029 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4030 XATTR_NAME_LOV, 0, th);
4032 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4033 XATTR_NAME_LOV, 0, th);
4041 static int lod_declare_dir_striping_create(const struct lu_env *env,
4042 struct dt_object *dt,
4043 struct lu_attr *attr,
4045 struct dt_object_format *dof,
4048 return lod_dir_striping_create_internal(env, dt, attr, lmu, dof, th,
4052 static int lod_dir_striping_create(const struct lu_env *env,
4053 struct dt_object *dt,
4054 struct lu_attr *attr,
4055 struct dt_object_format *dof,
4058 return lod_dir_striping_create_internal(env, dt, attr, NULL, dof, th,
4063 * Make LOV EA for striped object.
4065 * Generate striping information and store it in the LOV EA of the given
4066 * object. The caller must ensure nobody else is calling the function
4067 * against the object concurrently. The transaction must be started.
4068 * FLDB service must be running as well; it's used to map FID to the target,
4069 * which is stored in LOV EA.
4071 * \param[in] env execution environment for this thread
4072 * \param[in] lo LOD object
4073 * \param[in] th transaction handle
4075 * \retval 0 if LOV EA is stored successfully
4076 * \retval negative error number on failure
4078 static int lod_generate_and_set_lovea(const struct lu_env *env,
4079 struct lod_object *lo,
4082 struct lod_thread_info *info = lod_env_info(env);
4083 struct dt_object *next = dt_object_child(&lo->ldo_obj);
4084 struct lov_mds_md_v1 *lmm;
4090 if (lo->ldo_comp_cnt == 0 && !lo->ldo_is_foreign) {
4091 lod_striping_free(env, lo);
4092 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
4096 lmm_size = lod_comp_md_size(lo, false);
4097 if (info->lti_ea_store_size < lmm_size) {
4098 rc = lod_ea_store_resize(info, lmm_size);
4102 lmm = info->lti_ea_store;
4104 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
4108 info->lti_buf.lb_buf = lmm;
4109 info->lti_buf.lb_len = lmm_size;
4110 rc = lod_sub_xattr_set(env, next, &info->lti_buf,
4111 XATTR_NAME_LOV, 0, th);
4116 * Delete layout component(s)
4118 * \param[in] env execution environment for this thread
4119 * \param[in] dt object
4120 * \param[in] th transaction handle
4122 * \retval 0 on success
4123 * \retval negative error number on failure
4125 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
4128 struct lod_layout_component *lod_comp;
4129 struct lod_object *lo = lod_dt_obj(dt);
4130 struct dt_object *next = dt_object_child(dt);
4131 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4134 LASSERT(lo->ldo_is_composite);
4135 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
4137 left = lo->ldo_comp_cnt;
4138 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
4139 lod_comp = &lo->ldo_comp_entries[i];
4141 if (lod_comp->llc_id != LCME_ID_INVAL)
4145 /* Not instantiated component */
4146 if (lod_comp->llc_stripe == NULL)
4149 LASSERT(lod_comp->llc_stripe_count > 0);
4150 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4151 struct dt_object *obj = lod_comp->llc_stripe[j];
4155 rc = lod_sub_destroy(env, obj, th);
4159 lu_object_put(env, &obj->do_lu);
4160 lod_comp->llc_stripe[j] = NULL;
4162 OBD_FREE(lod_comp->llc_stripe, sizeof(struct dt_object *) *
4163 lod_comp->llc_stripes_allocated);
4164 lod_comp->llc_stripe = NULL;
4165 OBD_FREE(lod_comp->llc_ost_indices,
4166 sizeof(__u32) * lod_comp->llc_stripes_allocated);
4167 lod_comp->llc_ost_indices = NULL;
4168 lod_comp->llc_stripes_allocated = 0;
4169 lod_obj_set_pool(lo, i, NULL);
4170 if (lod_comp->llc_ostlist.op_array) {
4171 OBD_FREE(lod_comp->llc_ostlist.op_array,
4172 lod_comp->llc_ostlist.op_size);
4173 lod_comp->llc_ostlist.op_array = NULL;
4174 lod_comp->llc_ostlist.op_size = 0;
4178 LASSERTF(left >= 0 && left < lo->ldo_comp_cnt, "left = %d\n", left);
4180 struct lod_layout_component *comp_array;
4182 OBD_ALLOC(comp_array, sizeof(*comp_array) * left);
4183 if (comp_array == NULL)
4184 GOTO(out, rc = -ENOMEM);
4186 memcpy(&comp_array[0], &lo->ldo_comp_entries[0],
4187 sizeof(*comp_array) * left);
4189 OBD_FREE(lo->ldo_comp_entries,
4190 sizeof(*comp_array) * lo->ldo_comp_cnt);
4191 lo->ldo_comp_entries = comp_array;
4192 lo->ldo_comp_cnt = left;
4194 LASSERT(lo->ldo_mirror_count == 1);
4195 lo->ldo_mirrors[0].lme_end = left - 1;
4196 lod_obj_inc_layout_gen(lo);
4198 lod_free_comp_entries(lo);
4201 LASSERT(dt_object_exists(dt));
4202 rc = dt_attr_get(env, next, attr);
4206 if (attr->la_size > 0) {
4208 attr->la_valid = LA_SIZE;
4209 rc = lod_sub_attr_set(env, next, attr, th);
4214 rc = lod_generate_and_set_lovea(env, lo, th);
4218 lod_striping_free(env, lo);
4223 static int lod_get_default_lov_striping(const struct lu_env *env,
4224 struct lod_object *lo,
4225 struct lod_default_striping *lds);
4227 * Implementation of dt_object_operations::do_xattr_set.
4229 * Sets specified extended attribute on the object. Three types of EAs are
4231 * LOV EA - stores striping for a regular file or default striping (when set
4233 * LMV EA - stores a marker for the striped directories
4234 * DMV EA - stores default directory striping
4236 * When striping is applied to a non-striped existing object (this is called
4237 * late striping), then LOD notices the caller wants to turn the object into a
4238 * striped one. The stripe objects are created and appropriate EA is set:
4239 * LOV EA storing all the stripes directly or LMV EA storing just a small header
4240 * with striping configuration.
4242 * \see dt_object_operations::do_xattr_set() in the API description for details.
4244 static int lod_xattr_set(const struct lu_env *env,
4245 struct dt_object *dt, const struct lu_buf *buf,
4246 const char *name, int fl, struct thandle *th)
4248 struct dt_object *next = dt_object_child(dt);
4252 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4253 strcmp(name, XATTR_NAME_LMV) == 0) {
4254 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
4256 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4257 strncmp(name, XATTR_NAME_LMV, strlen(XATTR_NAME_LMV)) == 0 &&
4258 strlen(name) > strlen(XATTR_NAME_LMV) + 1) {
4259 const char *op = name + strlen(XATTR_NAME_LMV) + 1;
4263 * XATTR_NAME_LMV".add" is never called, but only declared,
4264 * because lod_xattr_set_lmv() will do the addition.
4266 if (strcmp(op, "del") == 0)
4267 rc = lod_dir_layout_delete(env, dt, buf, th);
4268 else if (strcmp(op, "set") == 0)
4269 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LMV,
4273 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4274 strcmp(name, XATTR_NAME_LOV) == 0) {
4275 struct lod_default_striping *lds = lod_lds_buf_get(env);
4276 struct lov_user_md_v1 *v1 = buf->lb_buf;
4277 char pool[LOV_MAXPOOLNAME + 1];
4280 /* get existing striping config */
4281 rc = lod_get_default_lov_striping(env, lod_dt_obj(dt), lds);
4285 memset(pool, 0, sizeof(pool));
4286 if (lds->lds_def_striping_set == 1)
4287 lod_layout_get_pool(lds->lds_def_comp_entries,
4288 lds->lds_def_comp_cnt, pool,
4291 is_del = LOVEA_DELETE_VALUES(v1->lmm_stripe_size,
4292 v1->lmm_stripe_count,
4293 v1->lmm_stripe_offset,
4296 /* Retain the pool name if it is not given */
4297 if (v1->lmm_magic == LOV_USER_MAGIC_V1 && pool[0] != '\0' &&
4299 struct lod_thread_info *info = lod_env_info(env);
4300 struct lov_user_md_v3 *v3 = info->lti_ea_store;
4302 memset(v3, 0, sizeof(*v3));
4303 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
4304 v3->lmm_pattern = cpu_to_le32(v1->lmm_pattern);
4305 v3->lmm_stripe_count =
4306 cpu_to_le32(v1->lmm_stripe_count);
4307 v3->lmm_stripe_offset =
4308 cpu_to_le32(v1->lmm_stripe_offset);
4309 v3->lmm_stripe_size = cpu_to_le32(v1->lmm_stripe_size);
4311 strlcpy(v3->lmm_pool_name, pool,
4312 sizeof(v3->lmm_pool_name));
4314 info->lti_buf.lb_buf = v3;
4315 info->lti_buf.lb_len = sizeof(*v3);
4316 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4319 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name,
4323 if (lds->lds_def_striping_set == 1 &&
4324 lds->lds_def_comp_entries != NULL)
4325 lod_free_def_comp_entries(lds);
4328 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4329 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
4331 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
4334 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4335 (!strcmp(name, XATTR_NAME_LOV) ||
4336 !strncmp(name, XATTR_LUSTRE_LOV,
4337 strlen(XATTR_LUSTRE_LOV)))) {
4338 /* in case of lov EA swap, just set it
4339 * if not, it is a replay so check striping match what we
4340 * already have during req replay, declare_xattr_set()
4341 * defines striping, then create() does the work */
4342 if (fl & LU_XATTR_REPLACE) {
4343 /* free stripes, then update disk */
4344 lod_striping_free(env, lod_dt_obj(dt));
4346 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
4347 } else if (dt_object_remote(dt)) {
4348 /* This only happens during migration, see
4349 * mdd_migrate_create(), in which Master MDT will
4350 * create a remote target object, and only set
4351 * (migrating) stripe EA on the remote object,
4352 * and does not need creating each stripes. */
4353 rc = lod_sub_xattr_set(env, next, buf, name,
4355 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
4356 /* delete component(s) */
4357 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
4358 rc = lod_layout_del(env, dt, th);
4361 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
4362 * it's going to create create file with specified
4363 * component(s), the striping must have not being
4364 * cached in this case;
4366 * Otherwise, it's going to add/change component(s) to
4367 * an existing file, the striping must have been cached
4370 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
4371 !strcmp(name, XATTR_NAME_LOV),
4372 !lod_dt_obj(dt)->ldo_comp_cached));
4374 rc = lod_striped_create(env, dt, NULL, NULL, th);
4377 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
4378 rc = lod_replace_parent_fid(env, dt, buf, th, false);
4383 /* then all other xattr */
4384 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
4390 * Implementation of dt_object_operations::do_declare_xattr_del.
4392 * \see dt_object_operations::do_declare_xattr_del() in the API description
4395 static int lod_declare_xattr_del(const struct lu_env *env,
4396 struct dt_object *dt, const char *name,
4399 struct lod_object *lo = lod_dt_obj(dt);
4400 struct dt_object *next = dt_object_child(dt);
4405 rc = lod_sub_declare_xattr_del(env, next, name, th);
4409 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4412 /* set xattr to each stripes, if needed */
4413 rc = lod_striping_load(env, lo);
4417 if (lo->ldo_dir_stripe_count == 0)
4420 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4421 struct dt_object *dto = lo->ldo_stripe[i];
4426 rc = lod_sub_declare_xattr_del(env, dto, name, th);
4435 * Implementation of dt_object_operations::do_xattr_del.
4437 * If EA storing a regular striping is being deleted, then release
4438 * all the references to the stripe objects in core.
4440 * \see dt_object_operations::do_xattr_del() in the API description for details.
4442 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
4443 const char *name, struct thandle *th)
4445 struct dt_object *next = dt_object_child(dt);
4446 struct lod_object *lo = lod_dt_obj(dt);
4451 if (!strcmp(name, XATTR_NAME_LOV) || !strcmp(name, XATTR_NAME_LMV))
4452 lod_striping_free(env, lod_dt_obj(dt));
4454 rc = lod_sub_xattr_del(env, next, name, th);
4455 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
4458 if (lo->ldo_dir_stripe_count == 0)
4461 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4462 struct dt_object *dto = lo->ldo_stripe[i];
4467 rc = lod_sub_xattr_del(env, dto, name, th);
4476 * Implementation of dt_object_operations::do_xattr_list.
4478 * \see dt_object_operations::do_xattr_list() in the API description
4481 static int lod_xattr_list(const struct lu_env *env,
4482 struct dt_object *dt, const struct lu_buf *buf)
4484 return dt_xattr_list(env, dt_object_child(dt), buf);
4487 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
4489 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
4493 * Copy OST list from layout provided by user.
4495 * \param[in] lod_comp layout_component to be filled
4496 * \param[in] v3 LOV EA V3 user data
4498 * \retval 0 on success
4499 * \retval negative if failed
4501 int lod_comp_copy_ost_lists(struct lod_layout_component *lod_comp,
4502 struct lov_user_md_v3 *v3)
4508 if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
4509 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
4511 if (lod_comp->llc_ostlist.op_array) {
4512 if (lod_comp->llc_ostlist.op_size >=
4513 v3->lmm_stripe_count * sizeof(__u32)) {
4514 lod_comp->llc_ostlist.op_count =
4515 v3->lmm_stripe_count;
4518 OBD_FREE(lod_comp->llc_ostlist.op_array,
4519 lod_comp->llc_ostlist.op_size);
4522 /* copy ost list from lmm */
4523 lod_comp->llc_ostlist.op_count = v3->lmm_stripe_count;
4524 lod_comp->llc_ostlist.op_size = v3->lmm_stripe_count * sizeof(__u32);
4525 OBD_ALLOC(lod_comp->llc_ostlist.op_array,
4526 lod_comp->llc_ostlist.op_size);
4527 if (!lod_comp->llc_ostlist.op_array)
4530 for (j = 0; j < v3->lmm_stripe_count; j++) {
4531 lod_comp->llc_ostlist.op_array[j] =
4532 v3->lmm_objects[j].l_ost_idx;
4540 * Get default striping.
4542 * \param[in] env execution environment
4543 * \param[in] lo object
4544 * \param[out] lds default striping
4546 * \retval 0 on success
4547 * \retval negative if failed
4549 static int lod_get_default_lov_striping(const struct lu_env *env,
4550 struct lod_object *lo,
4551 struct lod_default_striping *lds)
4553 struct lod_thread_info *info = lod_env_info(env);
4554 struct lov_user_md_v1 *v1 = NULL;
4555 struct lov_user_md_v3 *v3 = NULL;
4556 struct lov_comp_md_v1 *comp_v1 = NULL;
4563 lds->lds_def_striping_set = 0;
4565 rc = lod_get_lov_ea(env, lo);
4569 if (rc < (typeof(rc))sizeof(struct lov_user_md))
4572 v1 = info->lti_ea_store;
4573 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
4574 lustre_swab_lov_user_md_v1(v1);
4575 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
4576 v3 = (struct lov_user_md_v3 *)v1;
4577 lustre_swab_lov_user_md_v3(v3);
4578 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_SPECIFIC)) {
4579 v3 = (struct lov_user_md_v3 *)v1;
4580 lustre_swab_lov_user_md_v3(v3);
4581 lustre_swab_lov_user_md_objects(v3->lmm_objects,
4582 v3->lmm_stripe_count);
4583 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
4584 comp_v1 = (struct lov_comp_md_v1 *)v1;
4585 lustre_swab_lov_comp_md_v1(comp_v1);
4588 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
4589 v1->lmm_magic != LOV_MAGIC_COMP_V1 &&
4590 v1->lmm_magic != LOV_USER_MAGIC_SPECIFIC)
4593 if (v1->lmm_magic == LOV_MAGIC_COMP_V1) {
4594 comp_v1 = (struct lov_comp_md_v1 *)v1;
4595 comp_cnt = comp_v1->lcm_entry_count;
4598 mirror_cnt = comp_v1->lcm_mirror_count + 1;
4606 /* realloc default comp entries if necessary */
4607 rc = lod_def_striping_comp_resize(lds, comp_cnt);
4611 lds->lds_def_comp_cnt = comp_cnt;
4612 lds->lds_def_striping_is_composite = composite;
4613 lds->lds_def_mirror_cnt = mirror_cnt;
4615 for (i = 0; i < comp_cnt; i++) {
4616 struct lod_layout_component *lod_comp;
4619 lod_comp = &lds->lds_def_comp_entries[i];
4621 * reset lod_comp values, llc_stripes is always NULL in
4622 * the default striping template, llc_pool will be reset
4625 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
4628 v1 = (struct lov_user_md *)((char *)comp_v1 +
4629 comp_v1->lcm_entries[i].lcme_offset);
4630 lod_comp->llc_extent =
4631 comp_v1->lcm_entries[i].lcme_extent;
4632 /* We only inherit certain flags from the layout */
4633 lod_comp->llc_flags =
4634 comp_v1->lcm_entries[i].lcme_flags &
4635 LCME_TEMPLATE_FLAGS;
4638 if (v1->lmm_pattern != LOV_PATTERN_RAID0 &&
4639 v1->lmm_pattern != LOV_PATTERN_MDT &&
4640 v1->lmm_pattern != 0) {
4641 lod_free_def_comp_entries(lds);
4645 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d "
4646 "stripe_offset=%d\n",
4647 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
4648 (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
4649 (int)v1->lmm_stripe_offset);
4651 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
4652 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
4653 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
4654 lod_comp->llc_pattern = v1->lmm_pattern;
4657 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
4658 /* XXX: sanity check here */
4659 v3 = (struct lov_user_md_v3 *) v1;
4660 if (v3->lmm_pool_name[0] != '\0')
4661 pool = v3->lmm_pool_name;
4663 lod_set_def_pool(lds, i, pool);
4664 if (v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
4665 v3 = (struct lov_user_md_v3 *)v1;
4666 rc = lod_comp_copy_ost_lists(lod_comp, v3);
4669 } else if (lod_comp->llc_ostlist.op_array &&
4670 lod_comp->llc_ostlist.op_count) {
4671 for (j = 0; j < lod_comp->llc_ostlist.op_count; j++)
4672 lod_comp->llc_ostlist.op_array[j] = -1;
4673 lod_comp->llc_ostlist.op_count = 0;
4677 lds->lds_def_striping_set = 1;
4682 * Get default directory striping.
4684 * \param[in] env execution environment
4685 * \param[in] lo object
4686 * \param[out] lds default striping
4688 * \retval 0 on success
4689 * \retval negative if failed
4691 static int lod_get_default_lmv_striping(const struct lu_env *env,
4692 struct lod_object *lo,
4693 struct lod_default_striping *lds)
4695 struct lod_thread_info *info = lod_env_info(env);
4696 struct lmv_user_md_v1 *v1 = NULL;
4700 lds->lds_dir_def_striping_set = 0;
4701 rc = lod_get_default_lmv_ea(env, lo);
4705 if (rc < (typeof(rc))sizeof(struct lmv_user_md))
4708 v1 = info->lti_ea_store;
4710 lds->lds_dir_def_stripe_count = le32_to_cpu(v1->lum_stripe_count);
4711 lds->lds_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
4712 lds->lds_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
4713 lds->lds_dir_def_striping_set = 1;
4719 * Get default striping in the object.
4721 * Get object default striping and default directory striping.
4723 * \param[in] env execution environment
4724 * \param[in] lo object
4725 * \param[out] lds default striping
4727 * \retval 0 on success
4728 * \retval negative if failed
4730 static int lod_get_default_striping(const struct lu_env *env,
4731 struct lod_object *lo,
4732 struct lod_default_striping *lds)
4736 rc = lod_get_default_lov_striping(env, lo, lds);
4737 rc1 = lod_get_default_lmv_striping(env, lo, lds);
4738 if (rc == 0 && rc1 < 0)
4745 * Apply default striping on object.
4747 * If object striping pattern is not set, set to the one in default striping.
4748 * The default striping is from parent or fs.
4750 * \param[in] lo new object
4751 * \param[in] lds default striping
4752 * \param[in] mode new object's mode
4754 static void lod_striping_from_default(struct lod_object *lo,
4755 const struct lod_default_striping *lds,
4758 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4759 struct lov_desc *desc = &d->lod_desc;
4762 if (lds->lds_def_striping_set && S_ISREG(mode)) {
4763 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
4764 lds->lds_def_comp_cnt);
4768 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
4769 if (lds->lds_def_mirror_cnt > 1)
4770 lo->ldo_flr_state = LCM_FL_RDONLY;
4772 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4773 struct lod_layout_component *obj_comp =
4774 &lo->ldo_comp_entries[i];
4775 struct lod_layout_component *def_comp =
4776 &lds->lds_def_comp_entries[i];
4778 CDEBUG(D_LAYOUT, "Inherit from default: flags=%#x "
4779 "size=%hu nr=%u offset=%u pattern=%#x pool=%s\n",
4780 def_comp->llc_flags,
4781 def_comp->llc_stripe_size,
4782 def_comp->llc_stripe_count,
4783 def_comp->llc_stripe_offset,
4784 def_comp->llc_pattern,
4785 def_comp->llc_pool ?: "");
4787 *obj_comp = *def_comp;
4788 if (def_comp->llc_pool != NULL) {
4789 /* pointer was copied from def_comp */
4790 obj_comp->llc_pool = NULL;
4791 lod_obj_set_pool(lo, i, def_comp->llc_pool);
4795 if (def_comp->llc_ostlist.op_array &&
4796 def_comp->llc_ostlist.op_count) {
4797 OBD_ALLOC(obj_comp->llc_ostlist.op_array,
4798 obj_comp->llc_ostlist.op_size);
4799 if (!obj_comp->llc_ostlist.op_array)
4801 memcpy(obj_comp->llc_ostlist.op_array,
4802 def_comp->llc_ostlist.op_array,
4803 obj_comp->llc_ostlist.op_size);
4804 } else if (def_comp->llc_ostlist.op_array) {
4805 obj_comp->llc_ostlist.op_array = NULL;
4809 * Don't initialize these fields for plain layout
4810 * (v1/v3) here, they are inherited in the order of
4811 * 'parent' -> 'fs default (root)' -> 'global default
4812 * values for stripe_count & stripe_size'.
4814 * see lod_ah_init().
4816 if (!lo->ldo_is_composite)
4819 lod_adjust_stripe_info(obj_comp, desc);
4821 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
4822 if (lo->ldo_dir_stripe_count == 0)
4823 lo->ldo_dir_stripe_count =
4824 lds->lds_dir_def_stripe_count;
4825 if (lo->ldo_dir_stripe_offset == -1)
4826 lo->ldo_dir_stripe_offset =
4827 lds->lds_dir_def_stripe_offset;
4828 if (lo->ldo_dir_hash_type == 0)
4829 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
4831 CDEBUG(D_LAYOUT, "striping from default dir: count:%hu, "
4832 "offset:%u, hash_type:%u\n",
4833 lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
4834 lo->ldo_dir_hash_type);
4838 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root)
4840 struct lod_layout_component *lod_comp;
4842 if (lo->ldo_comp_cnt == 0)
4845 if (lo->ldo_is_composite)
4848 lod_comp = &lo->ldo_comp_entries[0];
4850 if (lod_comp->llc_stripe_count <= 0 ||
4851 lod_comp->llc_stripe_size <= 0)
4854 if (from_root && (lod_comp->llc_pool == NULL ||
4855 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
4862 * Implementation of dt_object_operations::do_ah_init.
4864 * This method is used to make a decision on the striping configuration for the
4865 * object being created. It can be taken from the \a parent object if it exists,
4866 * or filesystem's default. The resulting configuration (number of stripes,
4867 * stripe size/offset, pool name, etc) is stored in the object itself and will
4868 * be used by the methods like ->doo_declare_create().
4870 * \see dt_object_operations::do_ah_init() in the API description for details.
4872 static void lod_ah_init(const struct lu_env *env,
4873 struct dt_allocation_hint *ah,
4874 struct dt_object *parent,
4875 struct dt_object *child,
4878 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
4879 struct lod_thread_info *info = lod_env_info(env);
4880 struct lod_default_striping *lds = lod_lds_buf_get(env);
4881 struct dt_object *nextp = NULL;
4882 struct dt_object *nextc;
4883 struct lod_object *lp = NULL;
4884 struct lod_object *lc;
4885 struct lov_desc *desc;
4886 struct lod_layout_component *lod_comp;
4892 if (likely(parent)) {
4893 nextp = dt_object_child(parent);
4894 lp = lod_dt_obj(parent);
4897 nextc = dt_object_child(child);
4898 lc = lod_dt_obj(child);
4900 LASSERT(!lod_obj_is_striped(child));
4901 /* default layout template may have been set on the regular file
4902 * when this is called from mdd_create_data() */
4903 if (S_ISREG(child_mode))
4904 lod_free_comp_entries(lc);
4906 if (!dt_object_exists(nextc))
4907 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
4909 if (S_ISDIR(child_mode)) {
4910 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
4912 /* other default values are 0 */
4913 lc->ldo_dir_stripe_offset = -1;
4915 /* no default striping configuration is needed for
4918 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
4919 le32_to_cpu(lum1->lum_magic) == LMV_MAGIC_FOREIGN) {
4920 lc->ldo_dir_is_foreign = true;
4921 /* keep stripe_count 0 and stripe_offset -1 */
4922 CDEBUG(D_INFO, "no default striping for foreign dir\n");
4927 * If parent object is not root directory,
4928 * then get default striping from parent object.
4930 if (likely(lp != NULL) && !fid_is_root(lod_object_fid(lp)))
4931 lod_get_default_striping(env, lp, lds);
4933 /* set child default striping info, default value is NULL */
4934 if (lds->lds_def_striping_set || lds->lds_dir_def_striping_set)
4935 lc->ldo_def_striping = lds;
4937 /* It should always honour the specified stripes */
4938 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
4939 * will have old magic. In this case, we should ignore the
4940 * stripe count and try to create dir by default stripe.
4942 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
4943 (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
4944 le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
4945 lc->ldo_dir_stripe_count =
4946 le32_to_cpu(lum1->lum_stripe_count);
4947 lc->ldo_dir_stripe_offset =
4948 le32_to_cpu(lum1->lum_stripe_offset);
4949 lc->ldo_dir_hash_type =
4950 le32_to_cpu(lum1->lum_hash_type);
4952 "set dirstripe: count %hu, offset %d, hash %u\n",
4953 lc->ldo_dir_stripe_count,
4954 (int)lc->ldo_dir_stripe_offset,
4955 lc->ldo_dir_hash_type);
4957 /* transfer defaults LMV to new directory */
4958 lod_striping_from_default(lc, lds, child_mode);
4960 /* set count 0 to create normal directory */
4961 if (lc->ldo_dir_stripe_count == 1)
4962 lc->ldo_dir_stripe_count = 0;
4965 /* shrink the stripe_count to the avaible MDT count */
4966 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
4967 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)) {
4968 lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
4969 if (lc->ldo_dir_stripe_count == 1)
4970 lc->ldo_dir_stripe_count = 0;
4973 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
4974 lc->ldo_dir_stripe_count,
4975 (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
4980 /* child object regular file*/
4982 if (!lod_object_will_be_striped(S_ISREG(child_mode),
4983 lu_object_fid(&child->do_lu)))
4986 /* If object is going to be striped over OSTs, transfer default
4987 * striping information to the child, so that we can use it
4988 * during declaration and creation.
4990 * Try from the parent first.
4992 if (likely(lp != NULL)) {
4993 rc = lod_get_default_lov_striping(env, lp, lds);
4995 lod_striping_from_default(lc, lds, child_mode);
4998 /* Initialize lod_device::lod_md_root object reference */
4999 if (d->lod_md_root == NULL) {
5000 struct dt_object *root;
5001 struct lod_object *lroot;
5003 lu_root_fid(&info->lti_fid);
5004 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
5005 if (!IS_ERR(root)) {
5006 lroot = lod_dt_obj(root);
5008 spin_lock(&d->lod_lock);
5009 if (d->lod_md_root != NULL)
5010 dt_object_put(env, &d->lod_md_root->ldo_obj);
5011 d->lod_md_root = lroot;
5012 spin_unlock(&d->lod_lock);
5016 /* try inherit layout from the root object (fs default) when:
5017 * - parent does not have default layout; or
5018 * - parent has plain(v1/v3) default layout, and some attributes
5019 * are not specified in the default layout;
5021 if (d->lod_md_root != NULL && lod_need_inherit_more(lc, true)) {
5022 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds);
5025 if (lc->ldo_comp_cnt == 0) {
5026 lod_striping_from_default(lc, lds, child_mode);
5027 } else if (!lds->lds_def_striping_is_composite) {
5028 struct lod_layout_component *def_comp;
5030 LASSERT(!lc->ldo_is_composite);
5031 lod_comp = &lc->ldo_comp_entries[0];
5032 def_comp = &lds->lds_def_comp_entries[0];
5034 if (lod_comp->llc_stripe_count <= 0)
5035 lod_comp->llc_stripe_count =
5036 def_comp->llc_stripe_count;
5037 if (lod_comp->llc_stripe_size <= 0)
5038 lod_comp->llc_stripe_size =
5039 def_comp->llc_stripe_size;
5040 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT &&
5041 (!lod_comp->llc_pool || !lod_comp->llc_pool[0]))
5042 lod_comp->llc_stripe_offset =
5043 def_comp->llc_stripe_offset;
5044 if (lod_comp->llc_pool == NULL)
5045 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
5050 * fs default striping may not be explicitly set, or historically set
5051 * in config log, use them.
5053 if (lod_need_inherit_more(lc, false)) {
5054 if (lc->ldo_comp_cnt == 0) {
5055 rc = lod_alloc_comp_entries(lc, 0, 1);
5057 /* fail to allocate memory, will create a
5058 * non-striped file. */
5060 lc->ldo_is_composite = 0;
5061 lod_comp = &lc->ldo_comp_entries[0];
5062 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
5064 LASSERT(!lc->ldo_is_composite);
5065 lod_comp = &lc->ldo_comp_entries[0];
5066 desc = &d->lod_desc;
5067 lod_adjust_stripe_info(lod_comp, desc);
5073 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
5075 * Size initialization on late striping.
5077 * Propagate the size of a truncated object to a deferred striping.
5078 * This function handles a special case when truncate was done on a
5079 * non-striped object and now while the striping is being created
5080 * we can't lose that size, so we have to propagate it to the stripes
5083 * \param[in] env execution environment
5084 * \param[in] dt object
5085 * \param[in] th transaction handle
5087 * \retval 0 on success
5088 * \retval negative if failed
5090 static int lod_declare_init_size(const struct lu_env *env,
5091 struct dt_object *dt, struct thandle *th)
5093 struct dt_object *next = dt_object_child(dt);
5094 struct lod_object *lo = lod_dt_obj(dt);
5095 struct dt_object **objects = NULL;
5096 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
5097 uint64_t size, offs;
5098 int i, rc, stripe, stripe_count = 0, stripe_size = 0;
5099 struct lu_extent size_ext;
5102 if (!lod_obj_is_striped(dt))
5105 rc = dt_attr_get(env, next, attr);
5106 LASSERT(attr->la_valid & LA_SIZE);
5110 size = attr->la_size;
5114 size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
5115 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5116 struct lod_layout_component *lod_comp;
5117 struct lu_extent *extent;
5119 lod_comp = &lo->ldo_comp_entries[i];
5121 if (lod_comp->llc_stripe == NULL)
5124 extent = &lod_comp->llc_extent;
5125 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
5126 if (!lo->ldo_is_composite ||
5127 lu_extent_is_overlapped(extent, &size_ext)) {
5128 objects = lod_comp->llc_stripe;
5129 stripe_count = lod_comp->llc_stripe_count;
5130 stripe_size = lod_comp->llc_stripe_size;
5133 if (stripe_count == 0)
5136 LASSERT(objects != NULL && stripe_size != 0);
5137 /* ll_do_div64(a, b) returns a % b, and a = a / b */
5138 ll_do_div64(size, (__u64)stripe_size);
5139 stripe = ll_do_div64(size, (__u64)stripe_count);
5140 LASSERT(objects[stripe] != NULL);
5142 size = size * stripe_size;
5143 offs = attr->la_size;
5144 size += ll_do_div64(offs, stripe_size);
5146 attr->la_valid = LA_SIZE;
5147 attr->la_size = size;
5149 rc = lod_sub_declare_attr_set(env, objects[stripe],
5158 * Declare creation of striped object.
5160 * The function declares creation stripes for a regular object. The function
5161 * also declares whether the stripes will be created with non-zero size if
5162 * previously size was set non-zero on the master object. If object \a dt is
5163 * not local, then only fully defined striping can be applied in \a lovea.
5164 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
5167 * \param[in] env execution environment
5168 * \param[in] dt object
5169 * \param[in] attr attributes the stripes will be created with
5170 * \param[in] lovea a buffer containing striping description
5171 * \param[in] th transaction handle
5173 * \retval 0 on success
5174 * \retval negative if failed
5176 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
5177 struct lu_attr *attr,
5178 const struct lu_buf *lovea, struct thandle *th)
5180 struct lod_thread_info *info = lod_env_info(env);
5181 struct dt_object *next = dt_object_child(dt);
5182 struct lod_object *lo = lod_dt_obj(dt);
5186 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
5187 GOTO(out, rc = -ENOMEM);
5189 if (!dt_object_remote(next)) {
5190 /* choose OST and generate appropriate objects */
5191 rc = lod_prepare_create(env, lo, attr, lovea, th);
5196 * declare storage for striping data
5198 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5200 /* LOD can not choose OST objects for remote objects, i.e.
5201 * stripes must be ready before that. Right now, it can only
5202 * happen during migrate, i.e. migrate process needs to create
5203 * remote regular file (mdd_migrate_create), then the migrate
5204 * process will provide stripeEA. */
5205 LASSERT(lovea != NULL);
5206 info->lti_buf = *lovea;
5209 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
5210 XATTR_NAME_LOV, 0, th);
5215 * if striping is created with local object's size > 0,
5216 * we have to propagate this size to specific object
5217 * the case is possible only when local object was created previously
5219 if (dt_object_exists(next))
5220 rc = lod_declare_init_size(env, dt, th);
5223 /* failed to create striping or to set initial size, let's reset
5224 * config so that others don't get confused */
5226 lod_striping_free(env, lo);
5232 * Implementation of dt_object_operations::do_declare_create.
5234 * The method declares creation of a new object. If the object will be striped,
5235 * then helper functions are called to find FIDs for the stripes, declare
5236 * creation of the stripes and declare initialization of the striping
5237 * information to be stored in the master object.
5239 * \see dt_object_operations::do_declare_create() in the API description
5242 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
5243 struct lu_attr *attr,
5244 struct dt_allocation_hint *hint,
5245 struct dt_object_format *dof, struct thandle *th)
5247 struct dt_object *next = dt_object_child(dt);
5248 struct lod_object *lo = lod_dt_obj(dt);
5257 * first of all, we declare creation of local object
5259 rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
5264 * it's lod_ah_init() that has decided the object will be striped
5266 if (dof->dof_type == DFT_REGULAR) {
5267 /* callers don't want stripes */
5268 /* XXX: all tricky interactions with ->ah_make_hint() decided
5269 * to use striping, then ->declare_create() behaving differently
5270 * should be cleaned */
5271 if (dof->u.dof_reg.striped != 0)
5272 rc = lod_declare_striped_create(env, dt, attr,
5274 } else if (dof->dof_type == DFT_DIR) {
5275 struct seq_server_site *ss;
5276 struct lu_buf buf = { NULL };
5277 struct lu_buf *lmu = NULL;
5279 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
5281 /* If the parent has default stripeEA, and client
5282 * did not find it before sending create request,
5283 * then MDT will return -EREMOTE, and client will
5284 * retrieve the default stripeEA and re-create the
5287 * Note: if dah_eadata != NULL, it means creating the
5288 * striped directory with specified stripeEA, then it
5289 * should ignore the default stripeEA */
5290 if (hint != NULL && hint->dah_eadata == NULL) {
5291 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
5292 GOTO(out, rc = -EREMOTE);
5294 if (lo->ldo_dir_stripe_offset == -1) {
5295 /* child and parent should be in the same MDT */
5296 if (hint->dah_parent != NULL &&
5297 dt_object_remote(hint->dah_parent))
5298 GOTO(out, rc = -EREMOTE);
5299 } else if (lo->ldo_dir_stripe_offset !=
5301 struct lod_device *lod;
5302 struct lod_tgt_descs *ltd;
5303 struct lod_tgt_desc *tgt = NULL;
5304 bool found_mdt = false;
5307 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5308 ltd = &lod->lod_mdt_descs;
5309 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
5310 tgt = LTD_TGT(ltd, i);
5311 if (tgt->ltd_index ==
5312 lo->ldo_dir_stripe_offset) {
5318 /* If the MDT indicated by stripe_offset can be
5319 * found, then tell client to resend the create
5320 * request to the correct MDT, otherwise return
5321 * error to client */
5323 GOTO(out, rc = -EREMOTE);
5325 GOTO(out, rc = -EINVAL);
5327 } else if (hint && hint->dah_eadata) {
5329 lmu->lb_buf = (void *)hint->dah_eadata;
5330 lmu->lb_len = hint->dah_eadata_len;
5333 rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
5337 /* failed to create striping or to set initial size, let's reset
5338 * config so that others don't get confused */
5340 lod_striping_free(env, lo);
5345 * Generate component ID for new created component.
5347 * \param[in] lo LOD object
5348 * \param[in] comp_idx index of ldo_comp_entries
5350 * \retval component ID on success
5351 * \retval LCME_ID_INVAL on failure
5353 static __u32 lod_gen_component_id(struct lod_object *lo,
5354 int mirror_id, int comp_idx)
5356 struct lod_layout_component *lod_comp;
5357 __u32 id, start, end;
5360 LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
5362 lod_obj_inc_layout_gen(lo);
5363 id = lo->ldo_layout_gen;
5364 if (likely(id <= SEQ_ID_MAX))
5365 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
5367 /* Layout generation wraps, need to check collisions. */
5368 start = id & SEQ_ID_MASK;
5371 for (id = start; id <= end; id++) {
5372 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5373 lod_comp = &lo->ldo_comp_entries[i];
5374 if (pflr_id(mirror_id, id) == lod_comp->llc_id)
5377 /* Found the ununsed ID */
5378 if (i == lo->ldo_comp_cnt)
5379 RETURN(pflr_id(mirror_id, id));
5381 if (end == LCME_ID_MAX) {
5383 end = min(lo->ldo_layout_gen & LCME_ID_MASK,
5384 (__u32)(LCME_ID_MAX - 1));
5388 RETURN(LCME_ID_INVAL);
5392 * Creation of a striped regular object.
5394 * The function is called to create the stripe objects for a regular
5395 * striped file. This can happen at the initial object creation or
5396 * when the caller asks LOD to do so using ->do_xattr_set() method
5397 * (so called late striping). Notice all the information are already
5398 * prepared in the form of the list of objects (ldo_stripe field).
5399 * This is done during declare phase.
5401 * \param[in] env execution environment
5402 * \param[in] dt object
5403 * \param[in] attr attributes the stripes will be created with
5404 * \param[in] dof format of stripes (see OSD API description)
5405 * \param[in] th transaction handle
5407 * \retval 0 on success
5408 * \retval negative if failed
5410 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
5411 struct lu_attr *attr, struct dt_object_format *dof,
5414 struct lod_layout_component *lod_comp;
5415 struct lod_object *lo = lod_dt_obj(dt);
5420 LASSERT((lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL) ||
5421 lo->ldo_is_foreign);
5423 mirror_id = 0; /* non-flr file's mirror_id is 0 */
5424 if (lo->ldo_mirror_count > 1) {
5425 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5426 lod_comp = &lo->ldo_comp_entries[i];
5427 if (lod_comp->llc_id != LCME_ID_INVAL &&
5428 mirror_id_of(lod_comp->llc_id) > mirror_id)
5429 mirror_id = mirror_id_of(lod_comp->llc_id);
5433 /* create all underlying objects */
5434 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5435 lod_comp = &lo->ldo_comp_entries[i];
5437 if (lod_comp->llc_id == LCME_ID_INVAL) {
5438 /* only the component of FLR layout with more than 1
5439 * mirror has mirror ID in its component ID.
5441 if (lod_comp->llc_extent.e_start == 0 &&
5442 lo->ldo_mirror_count > 1)
5445 lod_comp->llc_id = lod_gen_component_id(lo,
5447 if (lod_comp->llc_id == LCME_ID_INVAL)
5448 GOTO(out, rc = -ERANGE);
5451 if (lod_comp_inited(lod_comp))
5454 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
5455 lod_comp_set_init(lod_comp);
5457 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
5458 lod_comp_set_init(lod_comp);
5460 if (lod_comp->llc_stripe == NULL)
5463 LASSERT(lod_comp->llc_stripe_count);
5464 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
5465 struct dt_object *object = lod_comp->llc_stripe[j];
5466 LASSERT(object != NULL);
5467 rc = lod_sub_create(env, object, attr, NULL, dof, th);
5471 lod_comp_set_init(lod_comp);
5474 rc = lod_fill_mirrors(lo);
5478 rc = lod_generate_and_set_lovea(env, lo, th);
5482 lo->ldo_comp_cached = 1;
5486 lod_striping_free(env, lo);
5490 static inline bool lod_obj_is_dom(struct dt_object *dt)
5492 struct lod_object *lo = lod_dt_obj(dt);
5494 if (!dt_object_exists(dt_object_child(dt)))
5497 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
5500 if (!lo->ldo_comp_cnt)
5503 return (lov_pattern(lo->ldo_comp_entries[0].llc_pattern) ==
5508 * Implementation of dt_object_operations::do_create.
5510 * If any of preceeding methods (like ->do_declare_create(),
5511 * ->do_ah_init(), etc) chose to create a striped object,
5512 * then this method will create the master and the stripes.
5514 * \see dt_object_operations::do_create() in the API description for details.
5516 static int lod_create(const struct lu_env *env, struct dt_object *dt,
5517 struct lu_attr *attr, struct dt_allocation_hint *hint,
5518 struct dt_object_format *dof, struct thandle *th)
5523 /* create local object */
5524 rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
5528 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
5529 (lod_obj_is_striped(dt) || lod_obj_is_dom(dt)) &&
5530 dof->u.dof_reg.striped != 0) {
5531 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
5532 rc = lod_striped_create(env, dt, attr, dof, th);
5539 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
5540 struct dt_object *dt, struct thandle *th,
5541 int comp_idx, int stripe_idx,
5542 struct lod_obj_stripe_cb_data *data)
5544 if (data->locd_declare)
5545 return lod_sub_declare_destroy(env, dt, th);
5546 else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
5547 stripe_idx == cfs_fail_val)
5548 return lod_sub_destroy(env, dt, th);
5554 * Implementation of dt_object_operations::do_declare_destroy.
5556 * If the object is a striped directory, then the function declares reference
5557 * removal from the master object (this is an index) to the stripes and declares
5558 * destroy of all the stripes. In all the cases, it declares an intention to
5559 * destroy the object itself.
5561 * \see dt_object_operations::do_declare_destroy() in the API description
5564 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
5567 struct dt_object *next = dt_object_child(dt);
5568 struct lod_object *lo = lod_dt_obj(dt);
5569 struct lod_thread_info *info = lod_env_info(env);
5570 struct dt_object *stripe;
5571 char *stripe_name = info->lti_key;
5577 * load striping information, notice we don't do this when object
5578 * is being initialized as we don't need this information till
5579 * few specific cases like destroy, chown
5581 rc = lod_striping_load(env, lo);
5585 /* declare destroy for all underlying objects */
5586 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5587 rc = next->do_ops->do_index_try(env, next,
5588 &dt_directory_features);
5592 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5593 stripe = lo->ldo_stripe[i];
5597 rc = lod_sub_declare_ref_del(env, next, th);
5601 snprintf(stripe_name, sizeof(info->lti_key),
5603 PFID(lu_object_fid(&stripe->do_lu)), i);
5604 rc = lod_sub_declare_delete(env, next,
5605 (const struct dt_key *)stripe_name, th);
5612 * we declare destroy for the local object
5614 rc = lod_sub_declare_destroy(env, next, th);
5618 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
5619 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
5622 if (!lod_obj_is_striped(dt))
5625 /* declare destroy all striped objects */
5626 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5627 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5628 stripe = lo->ldo_stripe[i];
5632 if (!dt_object_exists(stripe))
5635 rc = lod_sub_declare_ref_del(env, stripe, th);
5639 rc = lod_sub_declare_destroy(env, stripe, th);
5644 struct lod_obj_stripe_cb_data data = { { 0 } };
5646 data.locd_declare = true;
5647 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
5648 rc = lod_obj_for_each_stripe(env, lo, th, &data);
5655 * Implementation of dt_object_operations::do_destroy.
5657 * If the object is a striped directory, then the function removes references
5658 * from the master object (this is an index) to the stripes and destroys all
5659 * the stripes. In all the cases, the function destroys the object itself.
5661 * \see dt_object_operations::do_destroy() in the API description for details.
5663 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
5666 struct dt_object *next = dt_object_child(dt);
5667 struct lod_object *lo = lod_dt_obj(dt);
5668 struct lod_thread_info *info = lod_env_info(env);
5669 char *stripe_name = info->lti_key;
5670 struct dt_object *stripe;
5676 /* destroy sub-stripe of master object */
5677 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5678 rc = next->do_ops->do_index_try(env, next,
5679 &dt_directory_features);
5683 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5684 stripe = lo->ldo_stripe[i];
5688 rc = lod_sub_ref_del(env, next, th);
5692 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
5693 PFID(lu_object_fid(&stripe->do_lu)), i);
5695 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
5696 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
5697 PFID(lu_object_fid(&stripe->do_lu)));
5699 rc = lod_sub_delete(env, next,
5700 (const struct dt_key *)stripe_name, th);
5706 rc = lod_sub_destroy(env, next, th);
5710 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
5711 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
5714 if (!lod_obj_is_striped(dt))
5717 /* destroy all striped objects */
5718 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5719 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5720 stripe = lo->ldo_stripe[i];
5724 if (!dt_object_exists(stripe))
5727 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
5728 i == cfs_fail_val) {
5729 dt_write_lock(env, stripe, MOR_TGT_CHILD);
5730 rc = lod_sub_ref_del(env, stripe, th);
5731 dt_write_unlock(env, stripe);
5735 rc = lod_sub_destroy(env, stripe, th);
5741 struct lod_obj_stripe_cb_data data = { { 0 } };
5743 data.locd_declare = false;
5744 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
5745 rc = lod_obj_for_each_stripe(env, lo, th, &data);
5752 * Implementation of dt_object_operations::do_declare_ref_add.
5754 * \see dt_object_operations::do_declare_ref_add() in the API description
5757 static int lod_declare_ref_add(const struct lu_env *env,
5758 struct dt_object *dt, struct thandle *th)
5760 return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
5764 * Implementation of dt_object_operations::do_ref_add.
5766 * \see dt_object_operations::do_ref_add() in the API description for details.
5768 static int lod_ref_add(const struct lu_env *env,
5769 struct dt_object *dt, struct thandle *th)
5771 return lod_sub_ref_add(env, dt_object_child(dt), th);
5775 * Implementation of dt_object_operations::do_declare_ref_del.
5777 * \see dt_object_operations::do_declare_ref_del() in the API description
5780 static int lod_declare_ref_del(const struct lu_env *env,
5781 struct dt_object *dt, struct thandle *th)
5783 return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
5787 * Implementation of dt_object_operations::do_ref_del
5789 * \see dt_object_operations::do_ref_del() in the API description for details.
5791 static int lod_ref_del(const struct lu_env *env,
5792 struct dt_object *dt, struct thandle *th)
5794 return lod_sub_ref_del(env, dt_object_child(dt), th);
5798 * Implementation of dt_object_operations::do_object_sync.
5800 * \see dt_object_operations::do_object_sync() in the API description
5803 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
5804 __u64 start, __u64 end)
5806 return dt_object_sync(env, dt_object_child(dt), start, end);
5810 * Implementation of dt_object_operations::do_object_unlock.
5812 * Used to release LDLM lock(s).
5814 * \see dt_object_operations::do_object_unlock() in the API description
5817 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
5818 struct ldlm_enqueue_info *einfo,
5819 union ldlm_policy_data *policy)
5821 struct lod_object *lo = lod_dt_obj(dt);
5822 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
5823 int slave_locks_size;
5827 if (slave_locks == NULL)
5830 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
5831 /* Note: for remote lock for single stripe dir, MDT will cancel
5832 * the lock by lockh directly */
5833 LASSERT(!dt_object_remote(dt_object_child(dt)));
5835 /* locks were unlocked in MDT layer */
5836 for (i = 0; i < slave_locks->ha_count; i++)
5837 LASSERT(!lustre_handle_is_used(&slave_locks->ha_handles[i]));
5840 * NB, ha_count may not equal to ldo_dir_stripe_count, because dir
5841 * layout may change, e.g., shrink dir layout after migration.
5843 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5844 if (lo->ldo_stripe[i])
5845 dt_invalidate(env, lo->ldo_stripe[i]);
5848 slave_locks_size = offsetof(typeof(*slave_locks),
5849 ha_handles[slave_locks->ha_count]);
5850 OBD_FREE(slave_locks, slave_locks_size);
5851 einfo->ei_cbdata = NULL;
5857 * Implementation of dt_object_operations::do_object_lock.
5859 * Used to get LDLM lock on the non-striped and striped objects.
5861 * \see dt_object_operations::do_object_lock() in the API description
5864 static int lod_object_lock(const struct lu_env *env,
5865 struct dt_object *dt,
5866 struct lustre_handle *lh,
5867 struct ldlm_enqueue_info *einfo,
5868 union ldlm_policy_data *policy)
5870 struct lod_object *lo = lod_dt_obj(dt);
5871 int slave_locks_size;
5872 struct lustre_handle_array *slave_locks = NULL;
5877 /* remote object lock */
5878 if (!einfo->ei_enq_slave) {
5879 LASSERT(dt_object_remote(dt));
5880 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
5884 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
5887 rc = lod_striping_load(env, lo);
5892 if (lo->ldo_dir_stripe_count <= 1)
5895 slave_locks_size = offsetof(typeof(*slave_locks),
5896 ha_handles[lo->ldo_dir_stripe_count]);
5897 /* Freed in lod_object_unlock */
5898 OBD_ALLOC(slave_locks, slave_locks_size);
5901 slave_locks->ha_count = lo->ldo_dir_stripe_count;
5903 /* striped directory lock */
5904 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5905 struct lustre_handle lockh;
5906 struct ldlm_res_id *res_id;
5907 struct dt_object *stripe;
5909 stripe = lo->ldo_stripe[i];
5913 res_id = &lod_env_info(env)->lti_res_id;
5914 fid_build_reg_res_name(lu_object_fid(&stripe->do_lu), res_id);
5915 einfo->ei_res_id = res_id;
5917 if (dt_object_remote(stripe)) {
5918 set_bit(i, (void *)slave_locks->ha_map);
5919 rc = dt_object_lock(env, stripe, &lockh, einfo, policy);
5921 struct ldlm_namespace *ns = einfo->ei_namespace;
5922 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
5923 ldlm_completion_callback completion = einfo->ei_cb_cp;
5924 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
5926 if (einfo->ei_mode == LCK_PW ||
5927 einfo->ei_mode == LCK_EX)
5928 dlmflags |= LDLM_FL_COS_INCOMPAT;
5930 LASSERT(ns != NULL);
5931 rc = ldlm_cli_enqueue_local(env, ns, res_id, LDLM_IBITS,
5932 policy, einfo->ei_mode,
5933 &dlmflags, blocking,
5935 NULL, 0, LVB_T_NONE,
5940 ldlm_lock_decref_and_cancel(
5941 &slave_locks->ha_handles[i],
5943 OBD_FREE(slave_locks, slave_locks_size);
5946 slave_locks->ha_handles[i] = lockh;
5948 einfo->ei_cbdata = slave_locks;
5954 * Implementation of dt_object_operations::do_invalidate.
5956 * \see dt_object_operations::do_invalidate() in the API description for details
5958 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
5960 return dt_invalidate(env, dt_object_child(dt));
5963 static int lod_layout_data_init(struct lod_thread_info *info, __u32 comp_cnt)
5967 /* clear memory region that will be used for layout change */
5968 memset(&info->lti_layout_attr, 0, sizeof(struct lu_attr));
5969 info->lti_count = 0;
5971 if (info->lti_comp_size >= comp_cnt)
5974 if (info->lti_comp_size > 0) {
5975 OBD_FREE(info->lti_comp_idx,
5976 info->lti_comp_size * sizeof(__u32));
5977 info->lti_comp_size = 0;
5980 OBD_ALLOC(info->lti_comp_idx, comp_cnt * sizeof(__u32));
5981 if (!info->lti_comp_idx)
5984 info->lti_comp_size = comp_cnt;
5988 static int lod_declare_instantiate_components(const struct lu_env *env,
5989 struct lod_object *lo, struct thandle *th)
5991 struct lod_thread_info *info = lod_env_info(env);
5996 LASSERT(info->lti_count < lo->ldo_comp_cnt);
5998 for (i = 0; i < info->lti_count; i++) {
5999 rc = lod_qos_prep_create(env, lo, NULL, th,
6000 info->lti_comp_idx[i]);
6006 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6007 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
6008 &info->lti_buf, XATTR_NAME_LOV, 0, th);
6014 static int lod_declare_update_plain(const struct lu_env *env,
6015 struct lod_object *lo, struct layout_intent *layout,
6016 const struct lu_buf *buf, struct thandle *th)
6018 struct lod_thread_info *info = lod_env_info(env);
6019 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6020 struct lod_layout_component *lod_comp;
6021 struct lov_comp_md_v1 *comp_v1 = NULL;
6022 bool replay = false;
6026 LASSERT(lo->ldo_flr_state == LCM_FL_NONE);
6029 * In case the client is passing lovea, which only happens during
6030 * the replay of layout intent write RPC for now, we may need to
6031 * parse the lovea and apply new layout configuration.
6033 if (buf && buf->lb_len) {
6034 struct lov_user_md_v1 *v1 = buf->lb_buf;
6036 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
6037 v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
6038 LOV_MAGIC_COMP_V1)) {
6039 CERROR("%s: the replay buffer of layout extend "
6040 "(magic %#x) does not contain expected "
6041 "composite layout.\n",
6042 lod2obd(d)->obd_name, v1->lmm_magic);
6043 GOTO(out, rc = -EINVAL);
6046 rc = lod_use_defined_striping(env, lo, buf);
6049 lo->ldo_comp_cached = 1;
6051 rc = lod_get_lov_ea(env, lo);
6054 /* old on-disk EA is stored in info->lti_buf */
6055 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
6058 /* non replay path */
6059 rc = lod_striping_load(env, lo);
6064 /* Make sure defined layout covers the requested write range. */
6065 lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
6066 if (lo->ldo_comp_cnt > 1 &&
6067 lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
6068 lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
6069 CDEBUG(replay ? D_ERROR : D_LAYOUT,
6070 "%s: the defined layout [0, %#llx) does not covers "
6071 "the write range "DEXT"\n",
6072 lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
6073 PEXT(&layout->li_extent));
6074 GOTO(out, rc = -EINVAL);
6077 CDEBUG(D_LAYOUT, "%s: "DFID": instantiate components "DEXT"\n",
6078 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
6079 PEXT(&layout->li_extent));
6082 * Iterate ld->ldo_comp_entries, find the component whose extent under
6083 * the write range and not instantianted.
6085 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6086 lod_comp = &lo->ldo_comp_entries[i];
6088 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
6092 if (lod_comp_inited(lod_comp))
6096 * In replay path, lod_comp is the EA passed by
6097 * client replay buffer, comp_v1 is the pre-recovery
6098 * on-disk EA, we'd sift out those components which
6099 * were init-ed in the on-disk EA.
6101 if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
6106 * this component hasn't instantiated in normal path, or during
6107 * replay it needs replay the instantiation.
6110 /* A released component is being extended */
6111 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
6112 GOTO(out, rc = -EINVAL);
6114 LASSERT(info->lti_comp_idx != NULL);
6115 info->lti_comp_idx[info->lti_count++] = i;
6118 if (info->lti_count == 0)
6121 lod_obj_inc_layout_gen(lo);
6122 rc = lod_declare_instantiate_components(env, lo, th);
6125 lod_striping_free(env, lo);
6129 static inline int lod_comp_index(struct lod_object *lo,
6130 struct lod_layout_component *lod_comp)
6132 LASSERT(lod_comp >= lo->ldo_comp_entries &&
6133 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
6135 return lod_comp - lo->ldo_comp_entries;
6139 * Stale other mirrors by writing extent.
6141 static void lod_stale_components(struct lod_object *lo, int primary,
6142 struct lu_extent *extent)
6144 struct lod_layout_component *pri_comp, *lod_comp;
6147 /* The writing extent decides which components in the primary
6148 * are affected... */
6149 CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
6150 lod_foreach_mirror_comp(pri_comp, lo, primary) {
6151 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
6154 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
6155 lod_comp_index(lo, pri_comp),
6156 PEXT(&pri_comp->llc_extent));
6158 for (i = 0; i < lo->ldo_mirror_count; i++) {
6162 /* ... and then stale other components that are
6163 * overlapping with primary components */
6164 lod_foreach_mirror_comp(lod_comp, lo, i) {
6165 if (!lu_extent_is_overlapped(
6166 &pri_comp->llc_extent,
6167 &lod_comp->llc_extent))
6170 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
6171 i, lod_comp_index(lo, lod_comp));
6173 lod_comp->llc_flags |= LCME_FL_STALE;
6174 lo->ldo_mirrors[i].lme_stale = 1;
6181 * check an OST's availability
6182 * \param[in] env execution environment
6183 * \param[in] lo lod object
6184 * \param[in] dt dt object
6185 * \param[in] index mirror index
6187 * \retval negative if failed
6188 * \retval 1 if \a dt is available
6189 * \retval 0 if \a dt is not available
6191 static inline int lod_check_ost_avail(const struct lu_env *env,
6192 struct lod_object *lo,
6193 struct dt_object *dt, int index)
6195 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6196 struct lod_tgt_desc *ost;
6198 int type = LU_SEQ_RANGE_OST;
6201 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &idx, &type);
6203 CERROR("%s: can't locate "DFID":rc = %d\n",
6204 lod2obd(lod)->obd_name, PFID(lu_object_fid(&dt->do_lu)),
6209 ost = OST_TGT(lod, idx);
6210 if (ost->ltd_statfs.os_state &
6211 (OS_STATE_READONLY | OS_STATE_ENOSPC | OS_STATE_ENOINO |
6212 OS_STATE_NOPRECREATE) ||
6213 ost->ltd_active == 0) {
6214 CDEBUG(D_LAYOUT, DFID ": mirror %d OST%d unavail, rc = %d\n",
6215 PFID(lod_object_fid(lo)), index, idx, rc);
6223 * Pick primary mirror for write
6224 * \param[in] env execution environment
6225 * \param[in] lo object
6226 * \param[in] extent write range
6228 static int lod_primary_pick(const struct lu_env *env, struct lod_object *lo,
6229 struct lu_extent *extent)
6231 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6232 unsigned int seq = 0;
6233 struct lod_layout_component *lod_comp;
6235 int picked = -1, second_pick = -1, third_pick = -1;
6238 if (OBD_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
6239 get_random_bytes(&seq, sizeof(seq));
6240 seq %= lo->ldo_mirror_count;
6244 * Pick a mirror as the primary, and check the availability of OSTs.
6246 * This algo can be revised later after knowing the topology of
6249 lod_qos_statfs_update(env, lod);
6250 for (i = 0; i < lo->ldo_mirror_count; i++) {
6251 bool ost_avail = true;
6252 int index = (i + seq) % lo->ldo_mirror_count;
6254 if (lo->ldo_mirrors[index].lme_stale) {
6255 CDEBUG(D_LAYOUT, DFID": mirror %d stale\n",
6256 PFID(lod_object_fid(lo)), index);
6260 /* 2nd pick is for the primary mirror containing unavail OST */
6261 if (lo->ldo_mirrors[index].lme_primary && second_pick < 0)
6262 second_pick = index;
6264 /* 3rd pick is for non-primary mirror containing unavail OST */
6265 if (second_pick < 0 && third_pick < 0)
6269 * we found a non-primary 1st pick, we'd like to find a
6270 * potential pirmary mirror.
6272 if (picked >= 0 && !lo->ldo_mirrors[index].lme_primary)
6275 /* check the availability of OSTs */
6276 lod_foreach_mirror_comp(lod_comp, lo, index) {
6277 if (!lod_comp_inited(lod_comp) || !lod_comp->llc_stripe)
6280 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6281 struct dt_object *dt = lod_comp->llc_stripe[j];
6283 rc = lod_check_ost_avail(env, lo, dt, index);
6290 } /* for all dt object in one component */
6293 } /* for all components in a mirror */
6296 * the OSTs where allocated objects locates in the components
6297 * of the mirror are available.
6302 /* this mirror has all OSTs available */
6306 * primary with all OSTs are available, this is the perfect
6309 if (lo->ldo_mirrors[index].lme_primary)
6311 } /* for all mirrors */
6313 /* failed to pick a sound mirror, lower our expectation */
6315 picked = second_pick;
6317 picked = third_pick;
6324 static int lod_prepare_resync_mirror(const struct lu_env *env,
6325 struct lod_object *lo,
6328 struct lod_thread_info *info = lod_env_info(env);
6329 struct lod_layout_component *lod_comp;
6330 bool neg = !!(MIRROR_ID_NEG & mirror_id);
6333 mirror_id &= ~MIRROR_ID_NEG;
6335 for (i = 0; i < lo->ldo_mirror_count; i++) {
6336 if ((!neg && lo->ldo_mirrors[i].lme_id != mirror_id) ||
6337 (neg && lo->ldo_mirrors[i].lme_id == mirror_id))
6340 lod_foreach_mirror_comp(lod_comp, lo, i) {
6341 if (lod_comp_inited(lod_comp))
6344 info->lti_comp_idx[info->lti_count++] =
6345 lod_comp_index(lo, lod_comp);
6353 * figure out the components should be instantiated for resync.
6355 static int lod_prepare_resync(const struct lu_env *env, struct lod_object *lo,
6356 struct lu_extent *extent)
6358 struct lod_thread_info *info = lod_env_info(env);
6359 struct lod_layout_component *lod_comp;
6360 unsigned int need_sync = 0;
6364 DFID": instantiate all stale components in "DEXT"\n",
6365 PFID(lod_object_fid(lo)), PEXT(extent));
6368 * instantiate all components within this extent, even non-stale
6371 for (i = 0; i < lo->ldo_mirror_count; i++) {
6372 if (!lo->ldo_mirrors[i].lme_stale)
6375 lod_foreach_mirror_comp(lod_comp, lo, i) {
6376 if (!lu_extent_is_overlapped(extent,
6377 &lod_comp->llc_extent))
6382 if (lod_comp_inited(lod_comp))
6385 CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
6386 i, lod_comp_index(lo, lod_comp));
6387 info->lti_comp_idx[info->lti_count++] =
6388 lod_comp_index(lo, lod_comp);
6392 return need_sync ? 0 : -EALREADY;
6395 static int lod_declare_update_rdonly(const struct lu_env *env,
6396 struct lod_object *lo, struct md_layout_change *mlc,
6399 struct lod_thread_info *info = lod_env_info(env);
6400 struct lu_attr *layout_attr = &info->lti_layout_attr;
6401 struct lod_layout_component *lod_comp;
6402 struct lu_extent extent = { 0 };
6406 LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
6407 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
6408 mlc->mlc_opc == MD_LAYOUT_RESYNC);
6409 LASSERT(lo->ldo_mirror_count > 0);
6411 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
6412 struct layout_intent *layout = mlc->mlc_intent;
6415 extent = layout->li_extent;
6416 CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
6417 PFID(lod_object_fid(lo)), PEXT(&extent));
6419 picked = lod_primary_pick(env, lo, &extent);
6423 CDEBUG(D_LAYOUT, DFID": picked mirror id %u as primary\n",
6424 PFID(lod_object_fid(lo)),
6425 lo->ldo_mirrors[picked].lme_id);
6427 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
6429 * trunc transfers [0, size) in the intent extent, we'd
6430 * stale components overlapping [size, eof).
6432 extent.e_start = extent.e_end;
6433 extent.e_end = OBD_OBJECT_EOF;
6436 /* stale overlapping components from other mirrors */
6437 lod_stale_components(lo, picked, &extent);
6439 /* restore truncate intent extent */
6440 if (layout->li_opc == LAYOUT_INTENT_TRUNC)
6441 extent.e_end = extent.e_start;
6443 /* instantiate components for the picked mirror, start from 0 */
6446 lod_foreach_mirror_comp(lod_comp, lo, picked) {
6447 if (!lu_extent_is_overlapped(&extent,
6448 &lod_comp->llc_extent))
6451 if (lod_comp_inited(lod_comp))
6454 info->lti_comp_idx[info->lti_count++] =
6455 lod_comp_index(lo, lod_comp);
6458 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
6459 } else { /* MD_LAYOUT_RESYNC */
6463 * could contain multiple non-stale mirrors, so we need to
6464 * prep uninited all components assuming any non-stale mirror
6465 * could be picked as the primary mirror.
6467 if (mlc->mlc_mirror_id == 0) {
6469 for (i = 0; i < lo->ldo_mirror_count; i++) {
6470 if (lo->ldo_mirrors[i].lme_stale)
6473 lod_foreach_mirror_comp(lod_comp, lo, i) {
6474 if (!lod_comp_inited(lod_comp))
6478 lod_comp->llc_extent.e_end)
6480 lod_comp->llc_extent.e_end;
6483 rc = lod_prepare_resync(env, lo, &extent);
6487 /* mirror write, try to init its all components */
6488 rc = lod_prepare_resync_mirror(env, lo,
6489 mlc->mlc_mirror_id);
6494 /* change the file state to SYNC_PENDING */
6495 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
6498 /* Reset the layout version once it's becoming too large.
6499 * This way it can make sure that the layout version is
6500 * monotonously increased in this writing era. */
6501 lod_obj_inc_layout_gen(lo);
6502 if (lo->ldo_layout_gen > (LCME_ID_MAX >> 1)) {
6503 __u32 layout_version;
6505 cfs_get_random_bytes(&layout_version, sizeof(layout_version));
6506 lo->ldo_layout_gen = layout_version & 0xffff;
6509 rc = lod_declare_instantiate_components(env, lo, th);
6513 layout_attr->la_valid = LA_LAYOUT_VERSION;
6514 layout_attr->la_layout_version = 0; /* set current version */
6515 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
6516 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
6517 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
6523 lod_striping_free(env, lo);
6527 static int lod_declare_update_write_pending(const struct lu_env *env,
6528 struct lod_object *lo, struct md_layout_change *mlc,
6531 struct lod_thread_info *info = lod_env_info(env);
6532 struct lu_attr *layout_attr = &info->lti_layout_attr;
6533 struct lod_layout_component *lod_comp;
6534 struct lu_extent extent = { 0 };
6540 LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
6541 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
6542 mlc->mlc_opc == MD_LAYOUT_RESYNC);
6544 /* look for the primary mirror */
6545 for (i = 0; i < lo->ldo_mirror_count; i++) {
6546 if (lo->ldo_mirrors[i].lme_stale)
6549 LASSERTF(primary < 0, DFID " has multiple primary: %u / %u",
6550 PFID(lod_object_fid(lo)),
6551 lo->ldo_mirrors[i].lme_id,
6552 lo->ldo_mirrors[primary].lme_id);
6557 CERROR(DFID ": doesn't have a primary mirror\n",
6558 PFID(lod_object_fid(lo)));
6559 GOTO(out, rc = -ENODATA);
6562 CDEBUG(D_LAYOUT, DFID": found primary %u\n",
6563 PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
6565 LASSERT(!lo->ldo_mirrors[primary].lme_stale);
6567 /* for LAYOUT_WRITE opc, it has to do the following operations:
6568 * 1. stale overlapping componets from stale mirrors;
6569 * 2. instantiate components of the primary mirror;
6570 * 3. transfter layout version to all objects of the primary;
6572 * for LAYOUT_RESYNC opc, it will do:
6573 * 1. instantiate components of all stale mirrors;
6574 * 2. transfer layout version to all objects to close write era. */
6576 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
6577 LASSERT(mlc->mlc_intent != NULL);
6579 extent = mlc->mlc_intent->li_extent;
6581 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
6582 PFID(lod_object_fid(lo)), PEXT(&extent));
6584 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
6586 * trunc transfers [0, size) in the intent extent, we'd
6587 * stale components overlapping [size, eof).
6589 extent.e_start = extent.e_end;
6590 extent.e_end = OBD_OBJECT_EOF;
6592 /* 1. stale overlapping components */
6593 lod_stale_components(lo, primary, &extent);
6595 /* 2. find out the components need instantiating.
6596 * instantiate [0, mlc->mlc_intent->e_end) */
6598 /* restore truncate intent extent */
6599 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC)
6600 extent.e_end = extent.e_start;
6603 lod_foreach_mirror_comp(lod_comp, lo, primary) {
6604 if (!lu_extent_is_overlapped(&extent,
6605 &lod_comp->llc_extent))
6608 if (lod_comp_inited(lod_comp))
6611 CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
6612 primary, lod_comp_index(lo, lod_comp));
6613 info->lti_comp_idx[info->lti_count++] =
6614 lod_comp_index(lo, lod_comp);
6616 } else { /* MD_LAYOUT_RESYNC */
6617 if (mlc->mlc_mirror_id == 0) {
6619 lod_foreach_mirror_comp(lod_comp, lo, primary) {
6620 if (!lod_comp_inited(lod_comp))
6623 extent.e_end = lod_comp->llc_extent.e_end;
6626 rc = lod_prepare_resync(env, lo, &extent);
6630 /* mirror write, try to init its all components */
6631 rc = lod_prepare_resync_mirror(env, lo,
6632 mlc->mlc_mirror_id);
6637 /* change the file state to SYNC_PENDING */
6638 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
6641 rc = lod_declare_instantiate_components(env, lo, th);
6645 /* 3. transfer layout version to OST objects.
6646 * transfer new layout version to OST objects so that stale writes
6647 * can be denied. It also ends an era of writing by setting
6648 * LU_LAYOUT_RESYNC. Normal client can never use this bit to
6649 * send write RPC; only resync RPCs could do it. */
6650 layout_attr->la_valid = LA_LAYOUT_VERSION;
6651 layout_attr->la_layout_version = 0; /* set current version */
6652 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
6653 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
6654 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
6658 lod_obj_inc_layout_gen(lo);
6661 lod_striping_free(env, lo);
6665 static int lod_declare_update_sync_pending(const struct lu_env *env,
6666 struct lod_object *lo, struct md_layout_change *mlc,
6669 struct lod_thread_info *info = lod_env_info(env);
6670 unsigned sync_components = 0;
6671 unsigned resync_components = 0;
6676 LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
6677 LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
6678 mlc->mlc_opc == MD_LAYOUT_WRITE);
6680 CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
6681 PFID(lod_object_fid(lo)), mlc->mlc_opc);
6683 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
6684 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
6685 PFID(lod_object_fid(lo)));
6687 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
6688 return lod_declare_update_write_pending(env, lo, mlc, th);
6691 /* MD_LAYOUT_RESYNC_DONE */
6693 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6694 struct lod_layout_component *lod_comp;
6697 lod_comp = &lo->ldo_comp_entries[i];
6699 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
6704 for (j = 0; j < mlc->mlc_resync_count; j++) {
6705 if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
6708 mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
6709 lod_comp->llc_flags &= ~LCME_FL_STALE;
6710 resync_components++;
6716 for (i = 0; i < mlc->mlc_resync_count; i++) {
6717 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
6720 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
6721 "or already synced\n", PFID(lod_object_fid(lo)),
6722 mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
6723 GOTO(out, rc = -EINVAL);
6726 if (!sync_components || (mlc->mlc_resync_count && !resync_components)) {
6727 CDEBUG(D_LAYOUT, DFID": no mirror in sync\n",
6728 PFID(lod_object_fid(lo)));
6730 /* tend to return an error code here to prevent
6731 * the MDT from setting SoM attribute */
6732 GOTO(out, rc = -EINVAL);
6735 CDEBUG(D_LAYOUT, DFID": resynced %u/%zu components\n",
6736 PFID(lod_object_fid(lo)),
6737 resync_components, mlc->mlc_resync_count);
6739 lo->ldo_flr_state = LCM_FL_RDONLY;
6740 lod_obj_inc_layout_gen(lo);
6742 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6743 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
6744 &info->lti_buf, XATTR_NAME_LOV, 0, th);
6749 lod_striping_free(env, lo);
6753 static int lod_declare_layout_change(const struct lu_env *env,
6754 struct dt_object *dt, struct md_layout_change *mlc,
6757 struct lod_thread_info *info = lod_env_info(env);
6758 struct lod_object *lo = lod_dt_obj(dt);
6762 if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
6763 dt_object_remote(dt_object_child(dt)))
6766 rc = lod_striping_load(env, lo);
6770 LASSERT(lo->ldo_comp_cnt > 0);
6772 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
6776 switch (lo->ldo_flr_state) {
6778 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
6782 rc = lod_declare_update_rdonly(env, lo, mlc, th);
6784 case LCM_FL_WRITE_PENDING:
6785 rc = lod_declare_update_write_pending(env, lo, mlc, th);
6787 case LCM_FL_SYNC_PENDING:
6788 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
6799 * Instantiate layout component objects which covers the intent write offset.
6801 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
6802 struct md_layout_change *mlc, struct thandle *th)
6804 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
6805 struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
6806 struct lod_object *lo = lod_dt_obj(dt);
6809 rc = lod_striped_create(env, dt, attr, NULL, th);
6810 if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
6811 layout_attr->la_layout_version |= lo->ldo_layout_gen;
6812 rc = lod_attr_set(env, dt, layout_attr, th);
6818 struct dt_object_operations lod_obj_ops = {
6819 .do_read_lock = lod_read_lock,
6820 .do_write_lock = lod_write_lock,
6821 .do_read_unlock = lod_read_unlock,
6822 .do_write_unlock = lod_write_unlock,
6823 .do_write_locked = lod_write_locked,
6824 .do_attr_get = lod_attr_get,
6825 .do_declare_attr_set = lod_declare_attr_set,
6826 .do_attr_set = lod_attr_set,
6827 .do_xattr_get = lod_xattr_get,
6828 .do_declare_xattr_set = lod_declare_xattr_set,
6829 .do_xattr_set = lod_xattr_set,
6830 .do_declare_xattr_del = lod_declare_xattr_del,
6831 .do_xattr_del = lod_xattr_del,
6832 .do_xattr_list = lod_xattr_list,
6833 .do_ah_init = lod_ah_init,
6834 .do_declare_create = lod_declare_create,
6835 .do_create = lod_create,
6836 .do_declare_destroy = lod_declare_destroy,
6837 .do_destroy = lod_destroy,
6838 .do_index_try = lod_index_try,
6839 .do_declare_ref_add = lod_declare_ref_add,
6840 .do_ref_add = lod_ref_add,
6841 .do_declare_ref_del = lod_declare_ref_del,
6842 .do_ref_del = lod_ref_del,
6843 .do_object_sync = lod_object_sync,
6844 .do_object_lock = lod_object_lock,
6845 .do_object_unlock = lod_object_unlock,
6846 .do_invalidate = lod_invalidate,
6847 .do_declare_layout_change = lod_declare_layout_change,
6848 .do_layout_change = lod_layout_change,
6852 * Implementation of dt_body_operations::dbo_read.
6854 * \see dt_body_operations::dbo_read() in the API description for details.
6856 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
6857 struct lu_buf *buf, loff_t *pos)
6859 struct dt_object *next = dt_object_child(dt);
6861 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
6862 S_ISLNK(dt->do_lu.lo_header->loh_attr));
6863 return next->do_body_ops->dbo_read(env, next, buf, pos);
6867 * Implementation of dt_body_operations::dbo_declare_write.
6869 * \see dt_body_operations::dbo_declare_write() in the API description
6872 static ssize_t lod_declare_write(const struct lu_env *env,
6873 struct dt_object *dt,
6874 const struct lu_buf *buf, loff_t pos,
6877 return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
6881 * Implementation of dt_body_operations::dbo_write.
6883 * \see dt_body_operations::dbo_write() in the API description for details.
6885 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
6886 const struct lu_buf *buf, loff_t *pos,
6889 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
6890 S_ISLNK(dt->do_lu.lo_header->loh_attr));
6891 return lod_sub_write(env, dt_object_child(dt), buf, pos, th);
6894 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
6895 __u64 start, __u64 end, struct thandle *th)
6897 if (dt_object_remote(dt))
6900 return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
6903 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
6904 __u64 start, __u64 end, struct thandle *th)
6906 if (dt_object_remote(dt))
6909 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
6910 return lod_sub_punch(env, dt_object_child(dt), start, end, th);
6914 * different type of files use the same body_ops because object may be created
6915 * in OUT, where there is no chance to set correct body_ops for each type, so
6916 * body_ops themselves will check file type inside, see lod_read/write/punch for
6919 const struct dt_body_operations lod_body_ops = {
6920 .dbo_read = lod_read,
6921 .dbo_declare_write = lod_declare_write,
6922 .dbo_write = lod_write,
6923 .dbo_declare_punch = lod_declare_punch,
6924 .dbo_punch = lod_punch,
6928 * Implementation of lu_object_operations::loo_object_init.
6930 * The function determines the type and the index of the target device using
6931 * sequence of the object's FID. Then passes control down to the
6932 * corresponding device:
6933 * OSD for the local objects, OSP for remote
6935 * \see lu_object_operations::loo_object_init() in the API description
6938 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
6939 const struct lu_object_conf *conf)
6941 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
6942 struct lu_device *cdev = NULL;
6943 struct lu_object *cobj;
6944 struct lod_tgt_descs *ltd = NULL;
6945 struct lod_tgt_desc *tgt;
6947 int type = LU_SEQ_RANGE_ANY;
6951 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
6953 /* Note: Sometimes, it will Return EAGAIN here, see
6954 * ptrlpc_import_delay_req(), which might confuse
6955 * lu_object_find_at() and make it wait there incorrectly.
6956 * so we convert it to EIO here.*/
6963 if (type == LU_SEQ_RANGE_MDT &&
6964 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
6965 cdev = &lod->lod_child->dd_lu_dev;
6966 } else if (type == LU_SEQ_RANGE_MDT) {
6967 ltd = &lod->lod_mdt_descs;
6969 } else if (type == LU_SEQ_RANGE_OST) {
6970 ltd = &lod->lod_ost_descs;
6977 if (ltd->ltd_tgts_size > idx &&
6978 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
6979 tgt = LTD_TGT(ltd, idx);
6981 LASSERT(tgt != NULL);
6982 LASSERT(tgt->ltd_tgt != NULL);
6984 cdev = &(tgt->ltd_tgt->dd_lu_dev);
6986 lod_putref(lod, ltd);
6989 if (unlikely(cdev == NULL))
6992 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
6993 if (unlikely(cobj == NULL))
6996 lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
6998 lu_object_add(lo, cobj);
7005 * Alloc cached foreign LOV
7007 * \param[in] lo object
7008 * \param[in] size size of foreign LOV
7010 * \retval 0 on success
7011 * \retval negative if failed
7013 int lod_alloc_foreign_lov(struct lod_object *lo, size_t size)
7015 OBD_ALLOC_LARGE(lo->ldo_foreign_lov, size);
7016 if (lo->ldo_foreign_lov == NULL)
7018 lo->ldo_foreign_lov_size = size;
7019 lo->ldo_is_foreign = 1;
7025 * Free cached foreign LOV
7027 * \param[in] lo object
7029 void lod_free_foreign_lov(struct lod_object *lo)
7031 if (lo->ldo_foreign_lov != NULL)
7032 OBD_FREE_LARGE(lo->ldo_foreign_lov, lo->ldo_foreign_lov_size);
7033 lo->ldo_foreign_lov = NULL;
7034 lo->ldo_foreign_lov_size = 0;
7035 lo->ldo_is_foreign = 0;
7040 * Free cached foreign LMV
7042 * \param[in] lo object
7044 void lod_free_foreign_lmv(struct lod_object *lo)
7046 if (lo->ldo_foreign_lmv != NULL)
7047 OBD_FREE_LARGE(lo->ldo_foreign_lmv, lo->ldo_foreign_lmv_size);
7048 lo->ldo_foreign_lmv = NULL;
7049 lo->ldo_foreign_lmv_size = 0;
7050 lo->ldo_dir_is_foreign = 0;
7055 * Release resources associated with striping.
7057 * If the object is striped (regular or directory), then release
7058 * the stripe objects references and free the ldo_stripe array.
7060 * \param[in] env execution environment
7061 * \param[in] lo object
7063 void lod_striping_free_nolock(const struct lu_env *env, struct lod_object *lo)
7065 struct lod_layout_component *lod_comp;
7068 if (unlikely(lo->ldo_is_foreign)) {
7069 lod_free_foreign_lov(lo);
7070 lo->ldo_comp_cached = 0;
7071 } else if (unlikely(lo->ldo_dir_is_foreign)) {
7072 lod_free_foreign_lmv(lo);
7073 lo->ldo_dir_stripe_loaded = 0;
7074 } else if (lo->ldo_stripe != NULL) {
7075 LASSERT(lo->ldo_comp_entries == NULL);
7076 LASSERT(lo->ldo_dir_stripes_allocated > 0);
7078 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
7079 if (lo->ldo_stripe[i])
7080 dt_object_put(env, lo->ldo_stripe[i]);
7083 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
7084 OBD_FREE(lo->ldo_stripe, j);
7085 lo->ldo_stripe = NULL;
7086 lo->ldo_dir_stripes_allocated = 0;
7087 lo->ldo_dir_stripe_loaded = 0;
7088 lo->ldo_dir_stripe_count = 0;
7089 } else if (lo->ldo_comp_entries != NULL) {
7090 for (i = 0; i < lo->ldo_comp_cnt; i++) {
7091 /* free lod_layout_component::llc_stripe array */
7092 lod_comp = &lo->ldo_comp_entries[i];
7094 if (lod_comp->llc_stripe == NULL)
7096 LASSERT(lod_comp->llc_stripes_allocated != 0);
7097 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
7098 if (lod_comp->llc_stripe[j] != NULL)
7100 &lod_comp->llc_stripe[j]->do_lu);
7102 OBD_FREE(lod_comp->llc_stripe,
7103 sizeof(struct dt_object *) *
7104 lod_comp->llc_stripes_allocated);
7105 lod_comp->llc_stripe = NULL;
7106 OBD_FREE(lod_comp->llc_ost_indices,
7108 lod_comp->llc_stripes_allocated);
7109 lod_comp->llc_ost_indices = NULL;
7110 lod_comp->llc_stripes_allocated = 0;
7112 lod_free_comp_entries(lo);
7113 lo->ldo_comp_cached = 0;
7117 void lod_striping_free(const struct lu_env *env, struct lod_object *lo)
7119 mutex_lock(&lo->ldo_layout_mutex);
7120 lod_striping_free_nolock(env, lo);
7121 mutex_unlock(&lo->ldo_layout_mutex);
7125 * Implementation of lu_object_operations::loo_object_free.
7127 * \see lu_object_operations::loo_object_free() in the API description
7130 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
7132 struct lod_object *lo = lu2lod_obj(o);
7134 /* release all underlying object pinned */
7135 lod_striping_free(env, lo);
7137 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
7141 * Implementation of lu_object_operations::loo_object_release.
7143 * \see lu_object_operations::loo_object_release() in the API description
7146 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
7148 /* XXX: shouldn't we release everything here in case if object
7149 * creation failed before? */
7153 * Implementation of lu_object_operations::loo_object_print.
7155 * \see lu_object_operations::loo_object_print() in the API description
7158 static int lod_object_print(const struct lu_env *env, void *cookie,
7159 lu_printer_t p, const struct lu_object *l)
7161 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
7163 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
7166 struct lu_object_operations lod_lu_obj_ops = {
7167 .loo_object_init = lod_object_init,
7168 .loo_object_free = lod_object_free,
7169 .loo_object_release = lod_object_release,
7170 .loo_object_print = lod_object_print,