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 || magic == LOV_MAGIC_SEL) {
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 &&
1457 le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_SEL)
1460 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1461 lcme = &lcm->lcm_entries[0];
1462 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1463 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1465 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1466 LU_XATTR_REPLACE, th);
1473 * Implementation of dt_object_operations::do_xattr_get.
1475 * If LOV EA is requested from the root object and it's not
1476 * found, then return default striping for the filesystem.
1478 * \see dt_object_operations::do_xattr_get() in the API description for details.
1480 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1481 struct lu_buf *buf, const char *name)
1483 struct lod_thread_info *info = lod_env_info(env);
1484 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1489 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1490 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1491 struct lmv_mds_md_v1 *lmv1;
1492 struct lmv_foreign_md *lfm;
1495 if (rc > (typeof(rc))sizeof(*lmv1))
1498 /* short (<= sizeof(struct lmv_mds_md_v1)) foreign LMV case */
1499 /* XXX empty foreign LMV is not allowed */
1500 if (rc <= offsetof(typeof(*lfm), lfm_value))
1501 RETURN(rc = rc > 0 ? -EINVAL : rc);
1503 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1504 CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1506 /* lti_buf is large enough for *lmv1 or a short
1507 * (<= sizeof(struct lmv_mds_md_v1)) foreign LMV
1509 info->lti_buf.lb_buf = info->lti_key;
1510 info->lti_buf.lb_len = sizeof(*lmv1);
1511 rc = dt_xattr_get(env, dt_object_child(dt),
1512 &info->lti_buf, name);
1513 if (unlikely(rc <= offsetof(typeof(*lfm),
1515 RETURN(rc = rc > 0 ? -EINVAL : rc);
1517 lfm = info->lti_buf.lb_buf;
1518 if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN)
1521 if (unlikely(rc != sizeof(*lmv1)))
1522 RETURN(rc = rc > 0 ? -EINVAL : rc);
1524 lmv1 = info->lti_buf.lb_buf;
1525 /* The on-disk LMV EA only contains header, but the
1526 * returned LMV EA size should contain the space for
1527 * the FIDs of all shards of the striped directory. */
1528 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1529 rc = lmv_mds_md_size(
1530 le32_to_cpu(lmv1->lmv_stripe_count),
1534 if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN)
1537 if (rc != sizeof(*lmv1))
1538 RETURN(rc = rc > 0 ? -EINVAL : rc);
1540 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1544 RETURN(rc = rc1 != 0 ? rc1 : rc);
1547 if ((rc > 0) && buf->lb_buf && strcmp(name, XATTR_NAME_LOV) == 0) {
1548 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1550 if (lcm->lcm_magic == cpu_to_le32(LOV_MAGIC_SEL))
1551 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1554 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1558 * XXX: Only used by lfsck
1560 * lod returns default striping on the real root of the device
1561 * this is like the root stores default striping for the whole
1562 * filesystem. historically we've been using a different approach
1563 * and store it in the config.
1565 dt_root_get(env, dev->lod_child, &info->lti_fid);
1566 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1568 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1569 struct lov_user_md *lum = buf->lb_buf;
1570 struct lov_desc *desc = &dev->lod_desc;
1572 if (buf->lb_buf == NULL) {
1574 } else if (buf->lb_len >= sizeof(*lum)) {
1575 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1576 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1577 lmm_oi_set_id(&lum->lmm_oi, 0);
1578 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1579 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1580 lum->lmm_stripe_size = cpu_to_le32(
1581 desc->ld_default_stripe_size);
1582 lum->lmm_stripe_count = cpu_to_le16(
1583 desc->ld_default_stripe_count);
1584 lum->lmm_stripe_offset = cpu_to_le16(
1585 desc->ld_default_stripe_offset);
1598 * Checks that the magic of the stripe is sane.
1600 * \param[in] lod lod device
1601 * \param[in] lum a buffer storing LMV EA to verify
1603 * \retval 0 if the EA is sane
1604 * \retval negative otherwise
1606 static int lod_verify_md_striping(struct lod_device *lod,
1607 const struct lmv_user_md_v1 *lum)
1609 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1610 CERROR("%s: invalid lmv_user_md: magic = %x, "
1611 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1612 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1613 (int)le32_to_cpu(lum->lum_stripe_offset),
1614 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1622 * Initialize LMV EA for a slave.
1624 * Initialize slave's LMV EA from the master's LMV EA.
1626 * \param[in] master_lmv a buffer containing master's EA
1627 * \param[out] slave_lmv a buffer where slave's EA will be stored
1630 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1631 const struct lmv_mds_md_v1 *master_lmv)
1633 *slave_lmv = *master_lmv;
1634 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1640 * Generate LMV EA from the object passed as \a dt. The object must have
1641 * the stripes created and initialized.
1643 * \param[in] env execution environment
1644 * \param[in] dt object
1645 * \param[out] lmv_buf buffer storing generated LMV EA
1647 * \retval 0 on success
1648 * \retval negative if failed
1650 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1651 struct lu_buf *lmv_buf)
1653 struct lod_thread_info *info = lod_env_info(env);
1654 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1655 struct lod_object *lo = lod_dt_obj(dt);
1656 struct lmv_mds_md_v1 *lmm1;
1658 int type = LU_SEQ_RANGE_ANY;
1663 LASSERT(lo->ldo_dir_striped != 0);
1664 LASSERT(lo->ldo_dir_stripe_count > 0);
1665 stripe_count = lo->ldo_dir_stripe_count;
1666 /* Only store the LMV EA heahder on the disk. */
1667 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1668 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1672 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1675 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1676 memset(lmm1, 0, sizeof(*lmm1));
1677 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1678 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1679 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1680 if (lo->ldo_dir_hash_type & LMV_HASH_FLAG_MIGRATION) {
1681 lmm1->lmv_migrate_hash = cpu_to_le32(lo->ldo_dir_migrate_hash);
1682 lmm1->lmv_migrate_offset =
1683 cpu_to_le32(lo->ldo_dir_migrate_offset);
1685 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1690 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1691 lmv_buf->lb_buf = info->lti_ea_store;
1692 lmv_buf->lb_len = sizeof(*lmm1);
1698 * Create in-core represenation for a striped directory.
1700 * Parse the buffer containing LMV EA and instantiate LU objects
1701 * representing the stripe objects. The pointers to the objects are
1702 * stored in ldo_stripe field of \a lo. This function is used when
1703 * we need to access an already created object (i.e. load from a disk).
1705 * \param[in] env execution environment
1706 * \param[in] lo lod object
1707 * \param[in] buf buffer containing LMV EA
1709 * \retval 0 on success
1710 * \retval negative if failed
1712 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1713 const struct lu_buf *buf)
1715 struct lod_thread_info *info = lod_env_info(env);
1716 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1717 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1718 struct dt_object **stripe;
1719 union lmv_mds_md *lmm = buf->lb_buf;
1720 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1721 struct lu_fid *fid = &info->lti_fid;
1726 LASSERT(mutex_is_locked(&lo->ldo_layout_mutex));
1728 /* XXX may be useless as not called for foreign LMV ?? */
1729 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_FOREIGN)
1732 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1733 lo->ldo_dir_slave_stripe = 1;
1737 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1740 if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1743 LASSERT(lo->ldo_stripe == NULL);
1744 OBD_ALLOC(stripe, sizeof(stripe[0]) *
1745 (le32_to_cpu(lmv1->lmv_stripe_count)));
1749 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1750 struct dt_device *tgt_dt;
1751 struct dt_object *dto;
1752 int type = LU_SEQ_RANGE_ANY;
1755 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1756 if (!fid_is_sane(fid)) {
1761 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1765 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1766 tgt_dt = lod->lod_child;
1768 struct lod_tgt_desc *tgt;
1770 tgt = LTD_TGT(ltd, idx);
1772 GOTO(out, rc = -ESTALE);
1773 tgt_dt = tgt->ltd_tgt;
1776 dto = dt_locate_at(env, tgt_dt, fid,
1777 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1780 GOTO(out, rc = PTR_ERR(dto));
1785 lo->ldo_stripe = stripe;
1786 lo->ldo_dir_stripe_count = le32_to_cpu(lmv1->lmv_stripe_count);
1787 lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1789 lod_striping_free_nolock(env, lo);
1795 * Declare create a striped directory.
1797 * Declare creating a striped directory with a given stripe pattern on the
1798 * specified MDTs. A striped directory is represented as a regular directory
1799 * - an index listing all the stripes. The stripes point back to the master
1800 * object with ".." and LinkEA. The master object gets LMV EA which
1801 * identifies it as a striped directory. The function allocates FIDs
1804 * \param[in] env execution environment
1805 * \param[in] dt object
1806 * \param[in] attr attributes to initialize the objects with
1807 * \param[in] dof type of objects to be created
1808 * \param[in] th transaction handle
1810 * \retval 0 on success
1811 * \retval negative if failed
1813 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1814 struct dt_object *dt,
1815 struct lu_attr *attr,
1816 struct dt_object_format *dof,
1819 struct lod_thread_info *info = lod_env_info(env);
1820 struct lu_buf lmv_buf;
1821 struct lu_buf slave_lmv_buf;
1822 struct lmv_mds_md_v1 *lmm;
1823 struct lmv_mds_md_v1 *slave_lmm = NULL;
1824 struct dt_insert_rec *rec = &info->lti_dt_rec;
1825 struct lod_object *lo = lod_dt_obj(dt);
1830 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1833 lmm = lmv_buf.lb_buf;
1835 OBD_ALLOC_PTR(slave_lmm);
1836 if (slave_lmm == NULL)
1837 GOTO(out, rc = -ENOMEM);
1839 lod_prep_slave_lmv_md(slave_lmm, lmm);
1840 slave_lmv_buf.lb_buf = slave_lmm;
1841 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1843 if (!dt_try_as_dir(env, dt_object_child(dt)))
1844 GOTO(out, rc = -EINVAL);
1846 rec->rec_type = S_IFDIR;
1847 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1848 struct dt_object *dto = lo->ldo_stripe[i];
1849 char *stripe_name = info->lti_key;
1850 struct lu_name *sname;
1851 struct linkea_data ldata = { NULL };
1852 struct lu_buf linkea_buf;
1854 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
1858 rc = lod_sub_declare_create(env, dto, attr, NULL, dof, th);
1862 if (!dt_try_as_dir(env, dto))
1863 GOTO(out, rc = -EINVAL);
1865 rc = lod_sub_declare_ref_add(env, dto, th);
1869 rec->rec_fid = lu_object_fid(&dto->do_lu);
1870 rc = lod_sub_declare_insert(env, dto,
1871 (const struct dt_rec *)rec,
1872 (const struct dt_key *)dot, th);
1876 /* master stripe FID will be put to .. */
1877 rec->rec_fid = lu_object_fid(&dt->do_lu);
1878 rc = lod_sub_declare_insert(env, dto,
1879 (const struct dt_rec *)rec,
1880 (const struct dt_key *)dotdot, th);
1884 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1885 cfs_fail_val != i) {
1886 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1888 slave_lmm->lmv_master_mdt_index =
1891 slave_lmm->lmv_master_mdt_index =
1893 rc = lod_sub_declare_xattr_set(env, dto, &slave_lmv_buf,
1894 XATTR_NAME_LMV, 0, th);
1899 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1901 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1902 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1904 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1905 PFID(lu_object_fid(&dto->do_lu)), i);
1907 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1908 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1909 sname, lu_object_fid(&dt->do_lu));
1913 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1914 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1915 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
1916 XATTR_NAME_LINK, 0, th);
1920 rec->rec_fid = lu_object_fid(&dto->do_lu);
1921 rc = lod_sub_declare_insert(env, dt_object_child(dt),
1922 (const struct dt_rec *)rec,
1923 (const struct dt_key *)stripe_name,
1928 rc = lod_sub_declare_ref_add(env, dt_object_child(dt), th);
1933 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt),
1934 &lmv_buf, XATTR_NAME_LMV, 0, th);
1938 if (slave_lmm != NULL)
1939 OBD_FREE_PTR(slave_lmm);
1944 static int lod_prep_md_striped_create(const struct lu_env *env,
1945 struct dt_object *dt,
1946 struct lu_attr *attr,
1947 const struct lmv_user_md_v1 *lum,
1948 struct dt_object_format *dof,
1951 struct lod_thread_info *info = lod_env_info(env);
1952 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1953 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1954 struct lod_object *lo = lod_dt_obj(dt);
1955 struct dt_object **stripe;
1962 bool is_specific = false;
1965 /* The lum has been verifed in lod_verify_md_striping */
1966 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC ||
1967 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC);
1969 stripe_count = lo->ldo_dir_stripe_count;
1971 OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1972 if (idx_array == NULL)
1975 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1977 GOTO(out_free, rc = -ENOMEM);
1979 /* Start index must be the master MDT */
1980 master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1981 idx_array[0] = master_index;
1982 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) {
1984 for (i = 1; i < stripe_count; i++)
1985 idx_array[i] = le32_to_cpu(lum->lum_objects[i].lum_mds);
1988 for (i = 0; i < stripe_count; i++) {
1989 struct lod_tgt_desc *tgt = NULL;
1990 struct dt_object *dto;
1991 struct lu_fid fid = { 0 };
1993 struct lu_object_conf conf = { 0 };
1994 struct dt_device *tgt_dt = NULL;
1996 /* Try to find next avaible target */
1998 for (j = 0; j < lod->lod_remote_mdt_count;
1999 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
2000 bool already_allocated = false;
2003 CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
2004 idx, lod->lod_remote_mdt_count + 1, i);
2006 if (likely(!is_specific &&
2007 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
2008 /* check whether the idx already exists
2009 * in current allocated array */
2010 for (k = 0; k < i; k++) {
2011 if (idx_array[k] == idx) {
2012 already_allocated = true;
2017 if (already_allocated)
2021 /* Sigh, this index is not in the bitmap, let's check
2022 * next available target */
2023 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) &&
2024 idx != master_index)
2027 if (idx == master_index) {
2028 /* Allocate the FID locally */
2029 rc = obd_fid_alloc(env, lod->lod_child_exp,
2033 tgt_dt = lod->lod_child;
2037 /* check the status of the OSP */
2038 tgt = LTD_TGT(ltd, idx);
2042 tgt_dt = tgt->ltd_tgt;
2043 rc = dt_statfs(env, tgt_dt, &info->lti_osfs);
2045 /* this OSP doesn't feel well */
2050 rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
2059 /* Can not allocate more stripes */
2060 if (j == lod->lod_remote_mdt_count) {
2061 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
2062 lod2obd(lod)->obd_name, stripe_count, i);
2066 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
2067 idx, i, PFID(&fid));
2069 /* Set the start index for next stripe allocation */
2070 if (!is_specific && i < stripe_count - 1) {
2072 * for large dir test, put all other slaves on one
2073 * remote MDT, otherwise we may save too many local
2074 * slave locks which will exceed RS_MAX_LOCKS.
2076 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)))
2078 idx_array[i + 1] = (idx + 1) %
2079 (lod->lod_remote_mdt_count + 1);
2081 /* tgt_dt and fid must be ready after search avaible OSP
2082 * in the above loop */
2083 LASSERT(tgt_dt != NULL);
2084 LASSERT(fid_is_sane(&fid));
2086 /* fail a remote stripe FID allocation */
2087 if (i && OBD_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_FID))
2090 conf.loc_flags = LOC_F_NEW;
2091 dto = dt_locate_at(env, tgt_dt, &fid,
2092 dt->do_lu.lo_dev->ld_site->ls_top_dev,
2095 GOTO(out_put, rc = PTR_ERR(dto));
2099 lo->ldo_dir_striped = 1;
2100 lo->ldo_stripe = stripe;
2101 lo->ldo_dir_stripe_count = i;
2102 lo->ldo_dir_stripes_allocated = stripe_count;
2104 lo->ldo_dir_stripe_loaded = 1;
2106 if (lo->ldo_dir_stripe_count == 0)
2107 GOTO(out_put, rc = -ENOSPC);
2109 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
2115 for (i = 0; i < stripe_count; i++)
2116 if (stripe[i] != NULL)
2117 dt_object_put(env, stripe[i]);
2118 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
2119 lo->ldo_dir_stripe_count = 0;
2120 lo->ldo_dir_stripes_allocated = 0;
2121 lo->ldo_stripe = NULL;
2125 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
2132 * Alloc cached foreign LMV
2134 * \param[in] lo object
2135 * \param[in] size size of foreign LMV
2137 * \retval 0 on success
2138 * \retval negative if failed
2140 int lod_alloc_foreign_lmv(struct lod_object *lo, size_t size)
2142 OBD_ALLOC_LARGE(lo->ldo_foreign_lmv, size);
2143 if (lo->ldo_foreign_lmv == NULL)
2145 lo->ldo_foreign_lmv_size = size;
2146 lo->ldo_dir_is_foreign = 1;
2152 * Declare create striped md object.
2154 * The function declares intention to create a striped directory. This is a
2155 * wrapper for lod_prep_md_striped_create(). The only additional functionality
2156 * is to verify pattern \a lum_buf is good. Check that function for the details.
2158 * \param[in] env execution environment
2159 * \param[in] dt object
2160 * \param[in] attr attributes to initialize the objects with
2161 * \param[in] lum_buf a pattern specifying the number of stripes and
2163 * \param[in] dof type of objects to be created
2164 * \param[in] th transaction handle
2166 * \retval 0 on success
2167 * \retval negative if failed
2170 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
2171 struct dt_object *dt,
2172 struct lu_attr *attr,
2173 const struct lu_buf *lum_buf,
2174 struct dt_object_format *dof,
2177 struct lod_object *lo = lod_dt_obj(dt);
2178 struct lmv_user_md_v1 *lum = lum_buf->lb_buf;
2182 LASSERT(lum != NULL);
2184 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
2185 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
2186 (int)le32_to_cpu(lum->lum_stripe_offset));
2188 if (lo->ldo_dir_stripe_count == 0) {
2189 if (lo->ldo_dir_is_foreign) {
2190 rc = lod_alloc_foreign_lmv(lo, lum_buf->lb_len);
2193 memcpy(lo->ldo_foreign_lmv, lum, lum_buf->lb_len);
2194 lo->ldo_dir_stripe_loaded = 1;
2199 /* prepare dir striped objects */
2200 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2202 /* failed to create striping, let's reset
2203 * config so that others don't get confused */
2204 lod_striping_free(env, lo);
2212 * Append source stripes after target stripes for migrating directory. NB, we
2213 * only need to declare this, the append is done inside lod_xattr_set_lmv().
2215 * \param[in] env execution environment
2216 * \param[in] dt target object
2217 * \param[in] buf LMV buf which contains source stripe fids
2218 * \param[in] th transaction handle
2220 * \retval 0 on success
2221 * \retval negative if failed
2223 static int lod_dir_declare_layout_add(const struct lu_env *env,
2224 struct dt_object *dt,
2225 const struct lu_buf *buf,
2228 struct lod_thread_info *info = lod_env_info(env);
2229 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
2230 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
2231 struct lod_object *lo = lod_dt_obj(dt);
2232 struct dt_object *next = dt_object_child(dt);
2233 struct dt_object_format *dof = &info->lti_format;
2234 struct lmv_mds_md_v1 *lmv = buf->lb_buf;
2235 struct dt_object **stripe;
2236 __u32 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
2237 struct lu_fid *fid = &info->lti_fid;
2238 struct lod_tgt_desc *tgt;
2239 struct dt_object *dto;
2240 struct dt_device *tgt_dt;
2241 int type = LU_SEQ_RANGE_ANY;
2242 struct dt_insert_rec *rec = &info->lti_dt_rec;
2243 char *stripe_name = info->lti_key;
2244 struct lu_name *sname;
2245 struct linkea_data ldata = { NULL };
2246 struct lu_buf linkea_buf;
2253 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
2256 if (stripe_count == 0)
2259 dof->dof_type = DFT_DIR;
2262 sizeof(*stripe) * (lo->ldo_dir_stripe_count + stripe_count));
2266 for (i = 0; i < lo->ldo_dir_stripe_count; i++)
2267 stripe[i] = lo->ldo_stripe[i];
2269 for (i = 0; i < stripe_count; i++) {
2271 &lmv->lmv_stripe_fids[i]);
2272 if (!fid_is_sane(fid))
2275 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
2279 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
2280 tgt_dt = lod->lod_child;
2282 tgt = LTD_TGT(ltd, idx);
2284 GOTO(out, rc = -ESTALE);
2285 tgt_dt = tgt->ltd_tgt;
2288 dto = dt_locate_at(env, tgt_dt, fid,
2289 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
2292 GOTO(out, rc = PTR_ERR(dto));
2294 stripe[i + lo->ldo_dir_stripe_count] = dto;
2296 if (!dt_try_as_dir(env, dto))
2297 GOTO(out, rc = -ENOTDIR);
2299 rc = lod_sub_declare_ref_add(env, dto, th);
2303 rc = lod_sub_declare_insert(env, dto,
2304 (const struct dt_rec *)rec,
2305 (const struct dt_key *)dot, th);
2309 rc = lod_sub_declare_insert(env, dto,
2310 (const struct dt_rec *)rec,
2311 (const struct dt_key *)dotdot, th);
2315 rc = lod_sub_declare_xattr_set(env, dto, buf,
2316 XATTR_NAME_LMV, 0, th);
2320 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
2321 PFID(lu_object_fid(&dto->do_lu)),
2322 i + lo->ldo_dir_stripe_count);
2324 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2325 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
2326 sname, lu_object_fid(&dt->do_lu));
2330 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
2331 linkea_buf.lb_len = ldata.ld_leh->leh_len;
2332 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
2333 XATTR_NAME_LINK, 0, th);
2337 rc = lod_sub_declare_insert(env, next,
2338 (const struct dt_rec *)rec,
2339 (const struct dt_key *)stripe_name,
2344 rc = lod_sub_declare_ref_add(env, next, th);
2350 OBD_FREE(lo->ldo_stripe,
2351 sizeof(*stripe) * lo->ldo_dir_stripes_allocated);
2352 lo->ldo_stripe = stripe;
2353 lo->ldo_dir_migrate_offset = lo->ldo_dir_stripe_count;
2354 lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_hash_type);
2355 lo->ldo_dir_stripe_count += stripe_count;
2356 lo->ldo_dir_stripes_allocated += stripe_count;
2357 lo->ldo_dir_hash_type |= LMV_HASH_FLAG_MIGRATION;
2361 i = lo->ldo_dir_stripe_count;
2362 while (i < lo->ldo_dir_stripe_count + stripe_count && stripe[i])
2363 dt_object_put(env, stripe[i++]);
2366 sizeof(*stripe) * (stripe_count + lo->ldo_dir_stripe_count));
2370 static int lod_dir_declare_layout_delete(const struct lu_env *env,
2371 struct dt_object *dt,
2372 const struct lu_buf *buf,
2375 struct lod_thread_info *info = lod_env_info(env);
2376 struct lod_object *lo = lod_dt_obj(dt);
2377 struct dt_object *next = dt_object_child(dt);
2378 struct lmv_user_md *lmu = buf->lb_buf;
2379 __u32 final_stripe_count;
2380 char *stripe_name = info->lti_key;
2381 struct dt_object *dto;
2388 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
2389 if (final_stripe_count >= lo->ldo_dir_stripe_count)
2392 for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
2393 dto = lo->ldo_stripe[i];
2397 if (!dt_try_as_dir(env, dto))
2400 rc = lod_sub_declare_delete(env, dto,
2401 (const struct dt_key *)dot, th);
2405 rc = lod_sub_declare_ref_del(env, dto, th);
2409 rc = lod_sub_declare_delete(env, dto,
2410 (const struct dt_key *)dotdot, th);
2414 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2415 PFID(lu_object_fid(&dto->do_lu)), i);
2417 rc = lod_sub_declare_delete(env, next,
2418 (const struct dt_key *)stripe_name, th);
2422 rc = lod_sub_declare_ref_del(env, next, th);
2431 * delete stripes from dir master object, the lum_stripe_count in argument is
2432 * the final stripe count, the stripes after that will be deleted, NB, they
2433 * are not destroyed, but deleted from it's parent namespace, this function
2434 * will be called in two places:
2435 * 1. mdd_migrate_create() delete stripes from source, and append them to
2437 * 2. mdd_dir_layout_shrink() delete stripes from source, and destroy them.
2439 static int lod_dir_layout_delete(const struct lu_env *env,
2440 struct dt_object *dt,
2441 const struct lu_buf *buf,
2444 struct lod_thread_info *info = lod_env_info(env);
2445 struct lod_object *lo = lod_dt_obj(dt);
2446 struct dt_object *next = dt_object_child(dt);
2447 struct lmv_user_md *lmu = buf->lb_buf;
2448 __u32 final_stripe_count;
2449 char *stripe_name = info->lti_key;
2450 struct dt_object *dto;
2459 final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
2460 if (final_stripe_count >= lo->ldo_dir_stripe_count)
2463 for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
2464 dto = lo->ldo_stripe[i];
2468 rc = lod_sub_delete(env, dto,
2469 (const struct dt_key *)dotdot, th);
2473 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2474 PFID(lu_object_fid(&dto->do_lu)), i);
2476 rc = lod_sub_delete(env, next,
2477 (const struct dt_key *)stripe_name, th);
2481 rc = lod_sub_ref_del(env, next, th);
2486 lod_striping_free(env, lod_dt_obj(dt));
2492 * Implementation of dt_object_operations::do_declare_xattr_set.
2494 * Used with regular (non-striped) objects. Basically it
2495 * initializes the striping information and applies the
2496 * change to all the stripes.
2498 * \see dt_object_operations::do_declare_xattr_set() in the API description
2501 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2502 struct dt_object *dt,
2503 const struct lu_buf *buf,
2504 const char *name, int fl,
2507 struct dt_object *next = dt_object_child(dt);
2508 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2509 struct lod_object *lo = lod_dt_obj(dt);
2514 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2515 struct lmv_user_md_v1 *lum;
2517 LASSERT(buf != NULL && buf->lb_buf != NULL);
2519 rc = lod_verify_md_striping(d, lum);
2522 } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2523 rc = lod_verify_striping(d, lo, buf, false);
2528 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2532 /* Note: Do not set LinkEA on sub-stripes, otherwise
2533 * it will confuse the fid2path process(see mdt_path_current()).
2534 * The linkEA between master and sub-stripes is set in
2535 * lod_xattr_set_lmv(). */
2536 if (strcmp(name, XATTR_NAME_LINK) == 0)
2539 /* set xattr to each stripes, if needed */
2540 rc = lod_striping_load(env, lo);
2544 if (lo->ldo_dir_stripe_count == 0)
2547 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2548 if (!lo->ldo_stripe[i])
2551 if (!dt_object_exists(lo->ldo_stripe[i]))
2554 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2564 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2565 struct lod_object *lo,
2566 struct dt_object *dt, struct thandle *th,
2567 int comp_idx, int stripe_idx,
2568 struct lod_obj_stripe_cb_data *data)
2570 struct lod_thread_info *info = lod_env_info(env);
2571 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
2572 struct filter_fid *ff = &info->lti_ff;
2573 struct lu_buf *buf = &info->lti_buf;
2577 buf->lb_len = sizeof(*ff);
2578 rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2586 * locd_buf is set if it's called by dir migration, which doesn't check
2589 if (data->locd_buf) {
2590 memset(ff, 0, sizeof(*ff));
2591 ff->ff_parent = *(struct lu_fid *)data->locd_buf->lb_buf;
2593 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
2595 if (lu_fid_eq(lod_object_fid(lo), &ff->ff_parent) &&
2596 ff->ff_layout.ol_comp_id == comp->llc_id)
2599 memset(ff, 0, sizeof(*ff));
2600 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2603 /* rewrite filter_fid */
2604 ff->ff_parent.f_ver = stripe_idx;
2605 ff->ff_layout.ol_stripe_size = comp->llc_stripe_size;
2606 ff->ff_layout.ol_stripe_count = comp->llc_stripe_count;
2607 ff->ff_layout.ol_comp_id = comp->llc_id;
2608 ff->ff_layout.ol_comp_start = comp->llc_extent.e_start;
2609 ff->ff_layout.ol_comp_end = comp->llc_extent.e_end;
2610 filter_fid_cpu_to_le(ff, ff, sizeof(*ff));
2612 if (data->locd_declare)
2613 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2614 LU_XATTR_REPLACE, th);
2616 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2617 LU_XATTR_REPLACE, th);
2623 * Reset parent FID on OST object
2625 * Replace parent FID with @dt object FID, which is only called during migration
2626 * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2627 * the FID is changed.
2629 * \param[in] env execution environment
2630 * \param[in] dt dt_object whose stripes's parent FID will be reset
2631 * \parem[in] th thandle
2632 * \param[in] declare if it is declare
2634 * \retval 0 if reset succeeds
2635 * \retval negative errno if reset fails
2637 static int lod_replace_parent_fid(const struct lu_env *env,
2638 struct dt_object *dt,
2639 const struct lu_buf *buf,
2640 struct thandle *th, bool declare)
2642 struct lod_object *lo = lod_dt_obj(dt);
2643 struct lod_thread_info *info = lod_env_info(env);
2644 struct filter_fid *ff;
2645 struct lod_obj_stripe_cb_data data = { { 0 } };
2649 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2651 /* set xattr to each stripes, if needed */
2652 rc = lod_striping_load(env, lo);
2656 if (!lod_obj_is_striped(dt))
2659 if (info->lti_ea_store_size < sizeof(*ff)) {
2660 rc = lod_ea_store_resize(info, sizeof(*ff));
2665 data.locd_declare = declare;
2666 data.locd_stripe_cb = lod_obj_stripe_replace_parent_fid_cb;
2667 data.locd_buf = buf;
2668 rc = lod_obj_for_each_stripe(env, lo, th, &data);
2673 inline __u16 lod_comp_entry_stripe_count(struct lod_object *lo,
2674 struct lod_layout_component *entry,
2677 struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2681 else if (lod_comp_inited(entry))
2682 return entry->llc_stripe_count;
2683 else if ((__u16)-1 == entry->llc_stripe_count)
2684 return lod->lod_desc.ld_tgt_count;
2686 return lod_get_stripe_count(lod, lo,
2687 entry->llc_stripe_count, false);
2690 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2692 int magic, size = 0, i;
2693 struct lod_layout_component *comp_entries;
2695 bool is_composite, is_foreign = false;
2698 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2699 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2701 lo->ldo_def_striping->lds_def_striping_is_composite;
2703 comp_cnt = lo->ldo_comp_cnt;
2704 comp_entries = lo->ldo_comp_entries;
2705 is_composite = lo->ldo_is_composite;
2706 is_foreign = lo->ldo_is_foreign;
2710 return lo->ldo_foreign_lov_size;
2712 LASSERT(comp_cnt != 0 && comp_entries != NULL);
2714 size = sizeof(struct lov_comp_md_v1) +
2715 sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2716 LASSERT(size % sizeof(__u64) == 0);
2719 for (i = 0; i < comp_cnt; i++) {
2722 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2723 stripe_count = lod_comp_entry_stripe_count(lo, &comp_entries[i],
2725 if (!is_dir && is_composite)
2726 lod_comp_shrink_stripe_count(&comp_entries[i],
2729 size += lov_user_md_size(stripe_count, magic);
2730 LASSERT(size % sizeof(__u64) == 0);
2736 * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2737 * the xattr value is binary lov_comp_md_v1 which contains component(s)
2740 * \param[in] env execution environment
2741 * \param[in] dt dt_object to add components on
2742 * \param[in] buf buffer contains components to be added
2743 * \parem[in] th thandle
2745 * \retval 0 on success
2746 * \retval negative errno on failure
2748 static int lod_declare_layout_add(const struct lu_env *env,
2749 struct dt_object *dt,
2750 const struct lu_buf *buf,
2753 struct lod_thread_info *info = lod_env_info(env);
2754 struct lod_layout_component *comp_array, *lod_comp, *old_array;
2755 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2756 struct dt_object *next = dt_object_child(dt);
2757 struct lov_desc *desc = &d->lod_desc;
2758 struct lod_object *lo = lod_dt_obj(dt);
2759 struct lov_user_md_v3 *v3;
2760 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2762 int i, rc, array_cnt, old_array_cnt;
2765 LASSERT(lo->ldo_is_composite);
2767 if (lo->ldo_flr_state != LCM_FL_NONE)
2770 rc = lod_verify_striping(d, lo, buf, false);
2774 magic = comp_v1->lcm_magic;
2775 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2776 lustre_swab_lov_comp_md_v1(comp_v1);
2777 magic = comp_v1->lcm_magic;
2780 if (magic != LOV_USER_MAGIC_COMP_V1)
2783 array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2784 OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2785 if (comp_array == NULL)
2788 memcpy(comp_array, lo->ldo_comp_entries,
2789 sizeof(*comp_array) * lo->ldo_comp_cnt);
2791 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2792 struct lov_user_md_v1 *v1;
2793 struct lu_extent *ext;
2795 v1 = (struct lov_user_md *)((char *)comp_v1 +
2796 comp_v1->lcm_entries[i].lcme_offset);
2797 ext = &comp_v1->lcm_entries[i].lcme_extent;
2799 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2800 lod_comp->llc_extent.e_start = ext->e_start;
2801 lod_comp->llc_extent.e_end = ext->e_end;
2802 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2803 lod_comp->llc_flags = comp_v1->lcm_entries[i].lcme_flags;
2805 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2806 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2807 lod_adjust_stripe_info(lod_comp, desc);
2809 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2810 v3 = (struct lov_user_md_v3 *) v1;
2811 if (v3->lmm_pool_name[0] != '\0') {
2812 rc = lod_set_pool(&lod_comp->llc_pool,
2820 old_array = lo->ldo_comp_entries;
2821 old_array_cnt = lo->ldo_comp_cnt;
2823 lo->ldo_comp_entries = comp_array;
2824 lo->ldo_comp_cnt = array_cnt;
2826 /* No need to increase layout generation here, it will be increased
2827 * later when generating component ID for the new components */
2829 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2830 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2831 XATTR_NAME_LOV, 0, th);
2833 lo->ldo_comp_entries = old_array;
2834 lo->ldo_comp_cnt = old_array_cnt;
2838 OBD_FREE(old_array, sizeof(*lod_comp) * old_array_cnt);
2840 LASSERT(lo->ldo_mirror_count == 1);
2841 lo->ldo_mirrors[0].lme_end = array_cnt - 1;
2846 for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2847 lod_comp = &comp_array[i];
2848 if (lod_comp->llc_pool != NULL) {
2849 OBD_FREE(lod_comp->llc_pool,
2850 strlen(lod_comp->llc_pool) + 1);
2851 lod_comp->llc_pool = NULL;
2854 OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2859 * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2860 * the '$field' can only be 'flags' now. The xattr value is binary
2861 * lov_comp_md_v1 which contains the component ID(s) and the value of
2862 * the field to be modified.
2864 * \param[in] env execution environment
2865 * \param[in] dt dt_object to be modified
2866 * \param[in] op operation string, like "set.flags"
2867 * \param[in] buf buffer contains components to be set
2868 * \parem[in] th thandle
2870 * \retval 0 on success
2871 * \retval negative errno on failure
2873 static int lod_declare_layout_set(const struct lu_env *env,
2874 struct dt_object *dt,
2875 char *op, const struct lu_buf *buf,
2878 struct lod_layout_component *lod_comp;
2879 struct lod_thread_info *info = lod_env_info(env);
2880 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2881 struct lod_object *lo = lod_dt_obj(dt);
2882 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2885 bool changed = false;
2888 if (strcmp(op, "set.flags") != 0) {
2889 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2890 lod2obd(d)->obd_name, op);
2894 magic = comp_v1->lcm_magic;
2895 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2896 lustre_swab_lov_comp_md_v1(comp_v1);
2897 magic = comp_v1->lcm_magic;
2900 if (magic != LOV_USER_MAGIC_COMP_V1)
2903 if (comp_v1->lcm_entry_count == 0) {
2904 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2905 lod2obd(d)->obd_name);
2909 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2910 __u32 id = comp_v1->lcm_entries[i].lcme_id;
2911 __u32 flags = comp_v1->lcm_entries[i].lcme_flags;
2912 __u32 mirror_flag = flags & LCME_MIRROR_FLAGS;
2913 bool neg = flags & LCME_FL_NEG;
2915 if (flags & LCME_FL_INIT) {
2917 lod_striping_free(env, lo);
2921 flags &= ~(LCME_MIRROR_FLAGS | LCME_FL_NEG);
2922 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2923 lod_comp = &lo->ldo_comp_entries[j];
2925 /* lfs only put one flag in each entry */
2926 if ((flags && id != lod_comp->llc_id) ||
2927 (mirror_flag && mirror_id_of(id) !=
2928 mirror_id_of(lod_comp->llc_id)))
2933 lod_comp->llc_flags &= ~flags;
2935 lod_comp->llc_flags &= ~mirror_flag;
2938 lod_comp->llc_flags |= flags;
2940 lod_comp->llc_flags |= mirror_flag;
2941 if (mirror_flag & LCME_FL_NOSYNC)
2942 lod_comp->llc_timestamp =
2943 ktime_get_real_seconds();
2951 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2952 lod2obd(d)->obd_name);
2956 lod_obj_inc_layout_gen(lo);
2958 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2959 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), &info->lti_buf,
2960 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
2965 * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2966 * and the xattr value is a unique component ID or a special lcme_id.
2968 * \param[in] env execution environment
2969 * \param[in] dt dt_object to be operated on
2970 * \param[in] buf buffer contains component ID or lcme_id
2971 * \parem[in] th thandle
2973 * \retval 0 on success
2974 * \retval negative errno on failure
2976 static int lod_declare_layout_del(const struct lu_env *env,
2977 struct dt_object *dt,
2978 const struct lu_buf *buf,
2981 struct lod_thread_info *info = lod_env_info(env);
2982 struct dt_object *next = dt_object_child(dt);
2983 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2984 struct lod_object *lo = lod_dt_obj(dt);
2985 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2986 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2987 __u32 magic, id, flags, neg_flags = 0;
2991 LASSERT(lo->ldo_is_composite);
2993 if (lo->ldo_flr_state != LCM_FL_NONE)
2996 magic = comp_v1->lcm_magic;
2997 if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2998 lustre_swab_lov_comp_md_v1(comp_v1);
2999 magic = comp_v1->lcm_magic;
3002 if (magic != LOV_USER_MAGIC_COMP_V1)
3005 id = comp_v1->lcm_entries[0].lcme_id;
3006 flags = comp_v1->lcm_entries[0].lcme_flags;
3008 if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
3009 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
3010 lod2obd(d)->obd_name, id, flags);
3014 if (id != LCME_ID_INVAL && flags != 0) {
3015 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
3016 lod2obd(d)->obd_name);
3020 if (id == LCME_ID_INVAL && !flags) {
3021 CDEBUG(D_LAYOUT, "%s: no id or flags specified.\n",
3022 lod2obd(d)->obd_name);
3026 if (flags & LCME_FL_NEG) {
3027 neg_flags = flags & ~LCME_FL_NEG;
3031 left = lo->ldo_comp_cnt;
3035 for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3036 struct lod_layout_component *lod_comp;
3038 lod_comp = &lo->ldo_comp_entries[i];
3040 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
3042 else if (flags && !(flags & lod_comp->llc_flags))
3044 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
3047 if (left != (i + 1)) {
3048 CDEBUG(D_LAYOUT, "%s: this deletion will create "
3049 "a hole.\n", lod2obd(d)->obd_name);
3054 /* Mark the component as deleted */
3055 lod_comp->llc_id = LCME_ID_INVAL;
3057 /* Not instantiated component */
3058 if (lod_comp->llc_stripe == NULL)
3061 LASSERT(lod_comp->llc_stripe_count > 0);
3062 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
3063 struct dt_object *obj = lod_comp->llc_stripe[j];
3067 rc = lod_sub_declare_destroy(env, obj, th);
3073 LASSERTF(left >= 0, "left = %d\n", left);
3074 if (left == lo->ldo_comp_cnt) {
3075 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
3076 lod2obd(d)->obd_name, id);
3080 memset(attr, 0, sizeof(*attr));
3081 attr->la_valid = LA_SIZE;
3082 rc = lod_sub_declare_attr_set(env, next, attr, th);
3087 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
3088 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
3089 XATTR_NAME_LOV, 0, th);
3091 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
3098 * Declare layout add/set/del operations issued by special xattr names:
3100 * XATTR_LUSTRE_LOV.add add component(s) to existing file
3101 * XATTR_LUSTRE_LOV.del delete component(s) from existing file
3102 * XATTR_LUSTRE_LOV.set.$field set specified field of certain component(s)
3104 * \param[in] env execution environment
3105 * \param[in] dt object
3106 * \param[in] name name of xattr
3107 * \param[in] buf lu_buf contains xattr value
3108 * \param[in] th transaction handle
3110 * \retval 0 on success
3111 * \retval negative if failed
3113 static int lod_declare_modify_layout(const struct lu_env *env,
3114 struct dt_object *dt,
3116 const struct lu_buf *buf,
3119 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3120 struct lod_object *lo = lod_dt_obj(dt);
3122 int rc, len = strlen(XATTR_LUSTRE_LOV);
3125 LASSERT(dt_object_exists(dt));
3127 if (strlen(name) <= len || name[len] != '.') {
3128 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
3129 lod2obd(d)->obd_name, name);
3134 rc = lod_striping_load(env, lo);
3138 /* the layout to be modified must be a composite layout */
3139 if (!lo->ldo_is_composite) {
3140 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
3141 lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3142 GOTO(unlock, rc = -EINVAL);
3145 op = (char *)name + len;
3146 if (strcmp(op, "add") == 0) {
3147 rc = lod_declare_layout_add(env, dt, buf, th);
3148 } else if (strcmp(op, "del") == 0) {
3149 rc = lod_declare_layout_del(env, dt, buf, th);
3150 } else if (strncmp(op, "set", strlen("set")) == 0) {
3151 rc = lod_declare_layout_set(env, dt, op, buf, th);
3153 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
3154 lod2obd(d)->obd_name, name);
3155 GOTO(unlock, rc = -ENOTSUPP);
3159 lod_striping_free(env, lo);
3165 * Convert a plain file lov_mds_md to a composite layout.
3167 * \param[in,out] info the thread info::lti_ea_store buffer contains little
3168 * endian plain file layout
3170 * \retval 0 on success, <0 on failure
3172 static int lod_layout_convert(struct lod_thread_info *info)
3174 struct lov_mds_md *lmm = info->lti_ea_store;
3175 struct lov_mds_md *lmm_save;
3176 struct lov_comp_md_v1 *lcm;
3177 struct lov_comp_md_entry_v1 *lcme;
3183 /* realloc buffer to a composite layout which contains one component */
3184 blob_size = lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
3185 le32_to_cpu(lmm->lmm_magic));
3186 size = sizeof(*lcm) + sizeof(*lcme) + blob_size;
3188 OBD_ALLOC_LARGE(lmm_save, blob_size);
3190 GOTO(out, rc = -ENOMEM);
3192 memcpy(lmm_save, lmm, blob_size);
3194 if (info->lti_ea_store_size < size) {
3195 rc = lod_ea_store_resize(info, size);
3200 lcm = info->lti_ea_store;
3201 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
3202 lcm->lcm_size = cpu_to_le32(size);
3203 lcm->lcm_layout_gen = cpu_to_le32(le16_to_cpu(
3204 lmm_save->lmm_layout_gen));
3205 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
3206 lcm->lcm_entry_count = cpu_to_le16(1);
3207 lcm->lcm_mirror_count = 0;
3209 lcme = &lcm->lcm_entries[0];
3210 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
3211 lcme->lcme_extent.e_start = 0;
3212 lcme->lcme_extent.e_end = cpu_to_le64(OBD_OBJECT_EOF);
3213 lcme->lcme_offset = cpu_to_le32(sizeof(*lcm) + sizeof(*lcme));
3214 lcme->lcme_size = cpu_to_le32(blob_size);
3216 memcpy((char *)lcm + lcme->lcme_offset, (char *)lmm_save, blob_size);
3221 OBD_FREE_LARGE(lmm_save, blob_size);
3226 * Merge layouts to form a mirrored file.
3228 static int lod_declare_layout_merge(const struct lu_env *env,
3229 struct dt_object *dt, const struct lu_buf *mbuf,
3232 struct lod_thread_info *info = lod_env_info(env);
3233 struct lu_buf *buf = &info->lti_buf;
3234 struct lod_object *lo = lod_dt_obj(dt);
3235 struct lov_comp_md_v1 *lcm;
3236 struct lov_comp_md_v1 *cur_lcm;
3237 struct lov_comp_md_v1 *merge_lcm;
3238 struct lov_comp_md_entry_v1 *lcme;
3241 __u16 cur_entry_count;
3242 __u16 merge_entry_count;
3244 __u16 mirror_id = 0;
3249 merge_lcm = mbuf->lb_buf;
3250 if (mbuf->lb_len < sizeof(*merge_lcm))
3253 /* must be an existing layout from disk */
3254 if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
3257 merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
3259 /* do not allow to merge two mirrored files */
3260 if (le16_to_cpu(merge_lcm->lcm_mirror_count))
3263 /* verify the target buffer */
3264 rc = lod_get_lov_ea(env, lo);
3266 RETURN(rc ? : -ENODATA);
3268 cur_lcm = info->lti_ea_store;
3269 switch (le32_to_cpu(cur_lcm->lcm_magic)) {
3272 rc = lod_layout_convert(info);
3274 case LOV_MAGIC_COMP_V1:
3284 /* info->lti_ea_store could be reallocated in lod_layout_convert() */
3285 cur_lcm = info->lti_ea_store;
3286 cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
3288 /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
3289 mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
3290 if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
3293 /* size of new layout */
3294 size = le32_to_cpu(cur_lcm->lcm_size) +
3295 le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
3297 memset(buf, 0, sizeof(*buf));
3298 lu_buf_alloc(buf, size);
3299 if (buf->lb_buf == NULL)
3303 memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
3305 offset = sizeof(*lcm) +
3306 sizeof(*lcme) * (cur_entry_count + merge_entry_count);
3307 for (i = 0; i < cur_entry_count; i++) {
3308 struct lov_comp_md_entry_v1 *cur_lcme;
3310 lcme = &lcm->lcm_entries[i];
3311 cur_lcme = &cur_lcm->lcm_entries[i];
3313 lcme->lcme_offset = cpu_to_le32(offset);
3314 memcpy((char *)lcm + offset,
3315 (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
3316 le32_to_cpu(lcme->lcme_size));
3318 offset += le32_to_cpu(lcme->lcme_size);
3320 if (mirror_count == 1 &&
3321 mirror_id_of(le32_to_cpu(lcme->lcme_id)) == 0) {
3322 /* Add mirror from a non-flr file, create new mirror ID.
3323 * Otherwise, keep existing mirror's component ID, used
3324 * for mirror extension.
3326 id = pflr_id(1, i + 1);
3327 lcme->lcme_id = cpu_to_le32(id);
3330 id = MAX(le32_to_cpu(lcme->lcme_id), id);
3333 mirror_id = mirror_id_of(id) + 1;
3334 for (i = 0; i < merge_entry_count; i++) {
3335 struct lov_comp_md_entry_v1 *merge_lcme;
3337 merge_lcme = &merge_lcm->lcm_entries[i];
3338 lcme = &lcm->lcm_entries[cur_entry_count + i];
3340 *lcme = *merge_lcme;
3341 lcme->lcme_offset = cpu_to_le32(offset);
3343 id = pflr_id(mirror_id, i + 1);
3344 lcme->lcme_id = cpu_to_le32(id);
3346 memcpy((char *)lcm + offset,
3347 (char *)merge_lcm + le32_to_cpu(merge_lcme->lcme_offset),
3348 le32_to_cpu(lcme->lcme_size));
3350 offset += le32_to_cpu(lcme->lcme_size);
3353 /* fixup layout information */
3354 lod_obj_inc_layout_gen(lo);
3355 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3356 lcm->lcm_size = cpu_to_le32(size);
3357 lcm->lcm_entry_count = cpu_to_le16(cur_entry_count + merge_entry_count);
3358 lcm->lcm_mirror_count = cpu_to_le16(mirror_count);
3359 if ((le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK) == LCM_FL_NONE)
3360 lcm->lcm_flags = cpu_to_le32(LCM_FL_RDONLY);
3362 rc = lod_striping_reload(env, lo, buf);
3366 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), buf,
3367 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3375 * Split layouts, just set the LOVEA with the layout from mbuf.
3377 static int lod_declare_layout_split(const struct lu_env *env,
3378 struct dt_object *dt, const struct lu_buf *mbuf,
3381 struct lod_object *lo = lod_dt_obj(dt);
3382 struct lov_comp_md_v1 *lcm = mbuf->lb_buf;
3386 lod_obj_inc_layout_gen(lo);
3387 lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3389 rc = lod_striping_reload(env, lo, mbuf);
3393 rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), mbuf,
3394 XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3399 * Implementation of dt_object_operations::do_declare_xattr_set.
3401 * \see dt_object_operations::do_declare_xattr_set() in the API description
3404 * the extension to the API:
3405 * - declaring LOVEA requests striping creation
3406 * - LU_XATTR_REPLACE means layout swap
3408 static int lod_declare_xattr_set(const struct lu_env *env,
3409 struct dt_object *dt,
3410 const struct lu_buf *buf,
3411 const char *name, int fl,
3414 struct dt_object *next = dt_object_child(dt);
3415 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3420 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
3421 if ((S_ISREG(mode) || mode == 0) &&
3422 !(fl & (LU_XATTR_REPLACE | LU_XATTR_MERGE | LU_XATTR_SPLIT)) &&
3423 (strcmp(name, XATTR_NAME_LOV) == 0 ||
3424 strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
3426 * this is a request to create object's striping.
3428 * allow to declare predefined striping on a new (!mode) object
3429 * which is supposed to be replay of regular file creation
3430 * (when LOV setting is declared)
3432 * LU_XATTR_REPLACE is set to indicate a layout swap
3434 if (dt_object_exists(dt)) {
3435 rc = dt_attr_get(env, next, attr);
3439 memset(attr, 0, sizeof(*attr));
3440 attr->la_valid = LA_TYPE | LA_MODE;
3441 attr->la_mode = S_IFREG;
3443 rc = lod_declare_striped_create(env, dt, attr, buf, th);
3444 } else if (fl & LU_XATTR_MERGE) {
3445 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3446 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3447 rc = lod_declare_layout_merge(env, dt, buf, th);
3448 } else if (fl & LU_XATTR_SPLIT) {
3449 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3450 strcmp(name, XATTR_LUSTRE_LOV) == 0);
3451 rc = lod_declare_layout_split(env, dt, buf, th);
3452 } else if (S_ISREG(mode) &&
3453 strlen(name) > strlen(XATTR_LUSTRE_LOV) + 1 &&
3454 strncmp(name, XATTR_LUSTRE_LOV,
3455 strlen(XATTR_LUSTRE_LOV)) == 0) {
3457 * this is a request to modify object's striping.
3458 * add/set/del component(s).
3460 if (!dt_object_exists(dt))
3463 rc = lod_declare_modify_layout(env, dt, name, buf, th);
3464 } else if (strncmp(name, XATTR_NAME_LMV, strlen(XATTR_NAME_LMV)) == 0 &&
3465 strlen(name) > strlen(XATTR_NAME_LMV) + 1) {
3466 const char *op = name + strlen(XATTR_NAME_LMV) + 1;
3469 if (strcmp(op, "add") == 0)
3470 rc = lod_dir_declare_layout_add(env, dt, buf, th);
3471 else if (strcmp(op, "del") == 0)
3472 rc = lod_dir_declare_layout_delete(env, dt, buf, th);
3473 else if (strcmp(op, "set") == 0)
3474 rc = lod_sub_declare_xattr_set(env, next, buf,
3475 XATTR_NAME_LMV, fl, th);
3478 } else if (S_ISDIR(mode)) {
3479 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
3480 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3481 rc = lod_replace_parent_fid(env, dt, buf, th, true);
3483 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
3490 * Apply xattr changes to the object.
3492 * Applies xattr changes to the object and the stripes if the latter exist.
3494 * \param[in] env execution environment
3495 * \param[in] dt object
3496 * \param[in] buf buffer pointing to the new value of xattr
3497 * \param[in] name name of xattr
3498 * \param[in] fl flags
3499 * \param[in] th transaction handle
3501 * \retval 0 on success
3502 * \retval negative if failed
3504 static int lod_xattr_set_internal(const struct lu_env *env,
3505 struct dt_object *dt,
3506 const struct lu_buf *buf,
3507 const char *name, int fl,
3510 struct dt_object *next = dt_object_child(dt);
3511 struct lod_object *lo = lod_dt_obj(dt);
3516 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3517 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3520 /* Note: Do not set LinkEA on sub-stripes, otherwise
3521 * it will confuse the fid2path process(see mdt_path_current()).
3522 * The linkEA between master and sub-stripes is set in
3523 * lod_xattr_set_lmv(). */
3524 if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
3527 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3528 if (!lo->ldo_stripe[i])
3531 if (!dt_object_exists(lo->ldo_stripe[i]))
3534 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
3544 * Delete an extended attribute.
3546 * Deletes specified xattr from the object and the stripes if the latter exist.
3548 * \param[in] env execution environment
3549 * \param[in] dt object
3550 * \param[in] name name of xattr
3551 * \param[in] th transaction handle
3553 * \retval 0 on success
3554 * \retval negative if failed
3556 static int lod_xattr_del_internal(const struct lu_env *env,
3557 struct dt_object *dt,
3558 const char *name, struct thandle *th)
3560 struct dt_object *next = dt_object_child(dt);
3561 struct lod_object *lo = lod_dt_obj(dt);
3566 rc = lod_sub_xattr_del(env, next, name, th);
3567 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3570 if (lo->ldo_dir_stripe_count == 0)
3573 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3574 LASSERT(lo->ldo_stripe[i]);
3576 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3585 * Set default striping on a directory.
3587 * Sets specified striping on a directory object unless it matches the default
3588 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3589 * EA. This striping will be used when regular file is being created in this
3592 * \param[in] env execution environment
3593 * \param[in] dt the striped object
3594 * \param[in] buf buffer with the striping
3595 * \param[in] name name of EA
3596 * \param[in] fl xattr flag (see OSD API description)
3597 * \param[in] th transaction handle
3599 * \retval 0 on success
3600 * \retval negative if failed
3602 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
3603 struct dt_object *dt,
3604 const struct lu_buf *buf,
3605 const char *name, int fl,
3608 struct lov_user_md_v1 *lum;
3609 struct lov_user_md_v3 *v3 = NULL;
3610 const char *pool_name = NULL;
3615 LASSERT(buf != NULL && buf->lb_buf != NULL);
3618 switch (lum->lmm_magic) {
3619 case LOV_USER_MAGIC_SPECIFIC:
3620 case LOV_USER_MAGIC_V3:
3622 if (v3->lmm_pool_name[0] != '\0')
3623 pool_name = v3->lmm_pool_name;
3625 case LOV_USER_MAGIC_V1:
3626 /* if { size, offset, count } = { 0, -1, 0 } and no pool
3627 * (i.e. all default values specified) then delete default
3628 * striping from dir. */
3630 "set default striping: sz %u # %u offset %d %s %s\n",
3631 (unsigned)lum->lmm_stripe_size,
3632 (unsigned)lum->lmm_stripe_count,
3633 (int)lum->lmm_stripe_offset,
3634 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
3636 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
3637 lum->lmm_stripe_count,
3638 lum->lmm_stripe_offset,
3641 case LOV_USER_MAGIC_COMP_V1:
3643 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lum;
3644 struct lov_comp_md_entry_v1 *lcme;
3647 comp_cnt = le16_to_cpu(lcm->lcm_entry_count);
3648 for (i = 0; i < comp_cnt; i++) {
3649 lcme = &lcm->lcm_entries[i];
3650 if (lcme->lcme_flags & cpu_to_le32(LCME_FL_EXTENSION)) {
3651 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_SEL);
3660 CERROR("Invalid magic %x\n", lum->lmm_magic);
3665 rc = lod_xattr_del_internal(env, dt, name, th);
3669 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3676 * Set default striping on a directory object.
3678 * Sets specified striping on a directory object unless it matches the default
3679 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3680 * EA. This striping will be used when a new directory is being created in the
3683 * \param[in] env execution environment
3684 * \param[in] dt the striped object
3685 * \param[in] buf buffer with the striping
3686 * \param[in] name name of EA
3687 * \param[in] fl xattr flag (see OSD API description)
3688 * \param[in] th transaction handle
3690 * \retval 0 on success
3691 * \retval negative if failed
3693 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
3694 struct dt_object *dt,
3695 const struct lu_buf *buf,
3696 const char *name, int fl,
3699 struct lmv_user_md_v1 *lum;
3704 LASSERT(buf != NULL && buf->lb_buf != NULL);
3708 "set default stripe_count # %u stripe_offset %d hash %u\n",
3709 le32_to_cpu(lum->lum_stripe_count),
3710 (int)le32_to_cpu(lum->lum_stripe_offset),
3711 le32_to_cpu(lum->lum_hash_type));
3713 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
3714 le32_to_cpu(lum->lum_stripe_offset)) &&
3715 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC &&
3716 !(le32_to_cpu(lum->lum_hash_type) & LMV_HASH_FLAG_SPACE)) {
3717 rc = lod_xattr_del_internal(env, dt, name, th);
3721 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3730 * Turn directory into a striped directory.
3732 * During replay the client sends the striping created before MDT
3733 * failure, then the layer above LOD sends this defined striping
3734 * using ->do_xattr_set(), so LOD uses this method to replay creation
3735 * of the stripes. Notice the original information for the striping
3736 * (#stripes, FIDs, etc) was transferred in declare path.
3738 * \param[in] env execution environment
3739 * \param[in] dt the striped object
3740 * \param[in] buf not used currently
3741 * \param[in] name not used currently
3742 * \param[in] fl xattr flag (see OSD API description)
3743 * \param[in] th transaction handle
3745 * \retval 0 on success
3746 * \retval negative if failed
3748 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
3749 const struct lu_buf *buf, const char *name,
3750 int fl, struct thandle *th)
3752 struct lod_object *lo = lod_dt_obj(dt);
3753 struct lod_thread_info *info = lod_env_info(env);
3754 struct lu_attr *attr = &info->lti_attr;
3755 struct dt_object_format *dof = &info->lti_format;
3756 struct lu_buf lmv_buf;
3757 struct lu_buf slave_lmv_buf;
3758 struct lmv_mds_md_v1 *lmm;
3759 struct lmv_mds_md_v1 *slave_lmm = NULL;
3760 struct dt_insert_rec *rec = &info->lti_dt_rec;
3765 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3768 /* The stripes are supposed to be allocated in declare phase,
3769 * if there are no stripes being allocated, it will skip */
3770 if (lo->ldo_dir_stripe_count == 0) {
3771 if (lo->ldo_dir_is_foreign) {
3772 rc = lod_sub_xattr_set(env, dt_object_child(dt), buf,
3773 XATTR_NAME_LMV, fl, th);
3780 rc = dt_attr_get(env, dt_object_child(dt), attr);
3784 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
3785 LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
3786 dof->dof_type = DFT_DIR;
3788 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
3791 lmm = lmv_buf.lb_buf;
3793 OBD_ALLOC_PTR(slave_lmm);
3794 if (slave_lmm == NULL)
3797 lod_prep_slave_lmv_md(slave_lmm, lmm);
3798 slave_lmv_buf.lb_buf = slave_lmm;
3799 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
3801 rec->rec_type = S_IFDIR;
3802 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3803 struct dt_object *dto = lo->ldo_stripe[i];
3804 char *stripe_name = info->lti_key;
3805 struct lu_name *sname;
3806 struct linkea_data ldata = { NULL };
3807 struct lu_buf linkea_buf;
3809 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
3813 /* fail a remote stripe creation */
3814 if (i && OBD_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_CREATE))
3817 /* if it's source stripe of migrating directory, don't create */
3818 if (!((lo->ldo_dir_hash_type & LMV_HASH_FLAG_MIGRATION) &&
3819 i >= lo->ldo_dir_migrate_offset)) {
3820 dt_write_lock(env, dto, DT_TGT_CHILD);
3821 rc = lod_sub_create(env, dto, attr, NULL, dof, th);
3823 dt_write_unlock(env, dto);
3827 rc = lod_sub_ref_add(env, dto, th);
3828 dt_write_unlock(env, dto);
3832 rec->rec_fid = lu_object_fid(&dto->do_lu);
3833 rc = lod_sub_insert(env, dto,
3834 (const struct dt_rec *)rec,
3835 (const struct dt_key *)dot, th);
3840 rec->rec_fid = lu_object_fid(&dt->do_lu);
3841 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
3842 (const struct dt_key *)dotdot, th);
3846 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
3847 cfs_fail_val != i) {
3848 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
3850 slave_lmm->lmv_master_mdt_index =
3853 slave_lmm->lmv_master_mdt_index =
3856 rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
3857 XATTR_NAME_LMV, fl, th);
3862 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
3864 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3865 PFID(lu_object_fid(&dto->do_lu)), i + 1);
3867 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3868 PFID(lu_object_fid(&dto->do_lu)), i);
3870 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
3871 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
3872 sname, lu_object_fid(&dt->do_lu));
3876 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
3877 linkea_buf.lb_len = ldata.ld_leh->leh_len;
3878 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
3879 XATTR_NAME_LINK, 0, th);
3883 rec->rec_fid = lu_object_fid(&dto->do_lu);
3884 rc = lod_sub_insert(env, dt_object_child(dt),
3885 (const struct dt_rec *)rec,
3886 (const struct dt_key *)stripe_name, th);
3890 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
3895 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
3896 rc = lod_sub_xattr_set(env, dt_object_child(dt),
3897 &lmv_buf, XATTR_NAME_LMV, fl, th);
3899 if (slave_lmm != NULL)
3900 OBD_FREE_PTR(slave_lmm);
3906 * Helper function to declare/execute creation of a striped directory
3908 * Called in declare/create object path, prepare striping for a directory
3909 * and prepare defaults data striping for the objects to be created in
3910 * that directory. Notice the function calls "declaration" or "execution"
3911 * methods depending on \a declare param. This is a consequence of the
3912 * current approach while we don't have natural distributed transactions:
3913 * we basically execute non-local updates in the declare phase. So, the
3914 * arguments for the both phases are the same and this is the reason for
3915 * this function to exist.
3917 * \param[in] env execution environment
3918 * \param[in] dt object
3919 * \param[in] attr attributes the stripes will be created with
3920 * \param[in] lmu lmv_user_md if MDT indices are specified
3921 * \param[in] dof format of stripes (see OSD API description)
3922 * \param[in] th transaction handle
3923 * \param[in] declare where to call "declare" or "execute" methods
3925 * \retval 0 on success
3926 * \retval negative if failed
3928 static int lod_dir_striping_create_internal(const struct lu_env *env,
3929 struct dt_object *dt,
3930 struct lu_attr *attr,
3931 const struct lu_buf *lmu,
3932 struct dt_object_format *dof,
3936 struct lod_thread_info *info = lod_env_info(env);
3937 struct lod_object *lo = lod_dt_obj(dt);
3938 const struct lod_default_striping *lds = lo->ldo_def_striping;
3942 LASSERT(ergo(lds != NULL,
3943 lds->lds_def_striping_set ||
3944 lds->lds_dir_def_striping_set));
3946 if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
3947 lo->ldo_dir_stripe_offset)) {
3949 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3950 int stripe_count = lo->ldo_dir_stripe_count;
3952 if (info->lti_ea_store_size < sizeof(*v1)) {
3953 rc = lod_ea_store_resize(info, sizeof(*v1));
3956 v1 = info->lti_ea_store;
3959 memset(v1, 0, sizeof(*v1));
3960 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3961 v1->lum_stripe_count = cpu_to_le32(stripe_count);
3962 v1->lum_stripe_offset =
3963 cpu_to_le32(lo->ldo_dir_stripe_offset);
3965 info->lti_buf.lb_buf = v1;
3966 info->lti_buf.lb_len = sizeof(*v1);
3967 lmu = &info->lti_buf;
3971 rc = lod_declare_xattr_set_lmv(env, dt, attr, lmu, dof,
3974 rc = lod_xattr_set_lmv(env, dt, lmu, XATTR_NAME_LMV, 0,
3979 /* foreign LMV EA case */
3981 struct lmv_foreign_md *lfm = lmu->lb_buf;
3983 if (lfm->lfm_magic == LMV_MAGIC_FOREIGN) {
3984 rc = lod_declare_xattr_set_lmv(env, dt, attr,
3988 if (lo->ldo_dir_is_foreign) {
3989 LASSERT(lo->ldo_foreign_lmv != NULL &&
3990 lo->ldo_foreign_lmv_size > 0);
3991 info->lti_buf.lb_buf = lo->ldo_foreign_lmv;
3992 info->lti_buf.lb_len = lo->ldo_foreign_lmv_size;
3993 lmu = &info->lti_buf;
3994 rc = lod_xattr_set_lmv(env, dt, lmu,
3995 XATTR_NAME_LMV, 0, th);
4000 /* Transfer default LMV striping from the parent */
4001 if (lds != NULL && lds->lds_dir_def_striping_set &&
4002 !(LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
4003 lds->lds_dir_def_stripe_offset) &&
4004 le32_to_cpu(lds->lds_dir_def_hash_type) !=
4005 LMV_HASH_TYPE_UNKNOWN)) {
4006 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
4008 if (info->lti_ea_store_size < sizeof(*v1)) {
4009 rc = lod_ea_store_resize(info, sizeof(*v1));
4012 v1 = info->lti_ea_store;
4015 memset(v1, 0, sizeof(*v1));
4016 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
4017 v1->lum_stripe_count =
4018 cpu_to_le32(lds->lds_dir_def_stripe_count);
4019 v1->lum_stripe_offset =
4020 cpu_to_le32(lds->lds_dir_def_stripe_offset);
4022 cpu_to_le32(lds->lds_dir_def_hash_type);
4024 info->lti_buf.lb_buf = v1;
4025 info->lti_buf.lb_len = sizeof(*v1);
4027 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4028 XATTR_NAME_DEFAULT_LMV,
4031 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
4033 XATTR_NAME_DEFAULT_LMV, 0,
4039 /* Transfer default LOV striping from the parent */
4040 if (lds != NULL && lds->lds_def_striping_set &&
4041 lds->lds_def_comp_cnt != 0) {
4042 struct lov_mds_md *lmm;
4043 int lmm_size = lod_comp_md_size(lo, true);
4045 if (info->lti_ea_store_size < lmm_size) {
4046 rc = lod_ea_store_resize(info, lmm_size);
4050 lmm = info->lti_ea_store;
4052 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
4056 info->lti_buf.lb_buf = lmm;
4057 info->lti_buf.lb_len = lmm_size;
4060 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4061 XATTR_NAME_LOV, 0, th);
4063 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4064 XATTR_NAME_LOV, 0, th);
4072 static int lod_declare_dir_striping_create(const struct lu_env *env,
4073 struct dt_object *dt,
4074 struct lu_attr *attr,
4076 struct dt_object_format *dof,
4079 return lod_dir_striping_create_internal(env, dt, attr, lmu, dof, th,
4083 static int lod_dir_striping_create(const struct lu_env *env,
4084 struct dt_object *dt,
4085 struct lu_attr *attr,
4086 struct dt_object_format *dof,
4089 return lod_dir_striping_create_internal(env, dt, attr, NULL, dof, th,
4094 * Make LOV EA for striped object.
4096 * Generate striping information and store it in the LOV EA of the given
4097 * object. The caller must ensure nobody else is calling the function
4098 * against the object concurrently. The transaction must be started.
4099 * FLDB service must be running as well; it's used to map FID to the target,
4100 * which is stored in LOV EA.
4102 * \param[in] env execution environment for this thread
4103 * \param[in] lo LOD object
4104 * \param[in] th transaction handle
4106 * \retval 0 if LOV EA is stored successfully
4107 * \retval negative error number on failure
4109 static int lod_generate_and_set_lovea(const struct lu_env *env,
4110 struct lod_object *lo,
4113 struct lod_thread_info *info = lod_env_info(env);
4114 struct dt_object *next = dt_object_child(&lo->ldo_obj);
4115 struct lov_mds_md_v1 *lmm;
4121 if (lo->ldo_comp_cnt == 0 && !lo->ldo_is_foreign) {
4122 lod_striping_free(env, lo);
4123 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
4127 lmm_size = lod_comp_md_size(lo, false);
4128 if (info->lti_ea_store_size < lmm_size) {
4129 rc = lod_ea_store_resize(info, lmm_size);
4133 lmm = info->lti_ea_store;
4135 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
4139 info->lti_buf.lb_buf = lmm;
4140 info->lti_buf.lb_len = lmm_size;
4141 rc = lod_sub_xattr_set(env, next, &info->lti_buf,
4142 XATTR_NAME_LOV, 0, th);
4146 static __u32 lod_gen_component_id(struct lod_object *lo,
4147 int mirror_id, int comp_idx);
4150 * Repeat an existing component
4152 * Creates a new layout by replicating an existing component. Uses striping
4153 * policy from previous component as a template for the striping for the new
4156 * New component starts with zero length, will be extended (or removed) before
4157 * returning layout to client.
4159 * NB: Reallocates layout components array (lo->ldo_comp_entries), invalidating
4160 * any pre-existing pointers to components. Handle with care.
4162 * \param[in] env execution environment for this thread
4163 * \param[in,out] lo object to update the layout of
4164 * \param[in] index index of component to copy
4166 * \retval 0 on success
4167 * \retval negative errno on error
4169 static int lod_layout_repeat_comp(const struct lu_env *env,
4170 struct lod_object *lo, int index)
4172 struct lod_layout_component *lod_comp;
4173 struct lod_layout_component *new_comp = NULL;
4174 struct lod_layout_component *comp_array;
4175 int rc = 0, i, new_cnt = lo->ldo_comp_cnt + 1;
4180 lod_comp = &lo->ldo_comp_entries[index];
4181 LASSERT(lod_comp_inited(lod_comp) && lod_comp->llc_id != LCME_ID_INVAL);
4183 CDEBUG(D_LAYOUT, "repeating component %d\n", index);
4185 OBD_ALLOC(comp_array, sizeof(*comp_array) * new_cnt);
4186 if (comp_array == NULL)
4187 GOTO(out, rc = -ENOMEM);
4189 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4190 memcpy(&comp_array[i + offset], &lo->ldo_comp_entries[i],
4191 sizeof(*comp_array));
4193 /* Duplicate this component in to the next slot */
4195 new_comp = &comp_array[i + 1];
4196 memcpy(&comp_array[i + 1], &lo->ldo_comp_entries[i],
4197 sizeof(*comp_array));
4198 /* We must now skip this new component when copying */
4203 /* Set up copied component */
4204 new_comp->llc_flags &= ~LCME_FL_INIT;
4205 new_comp->llc_stripe = NULL;
4206 new_comp->llc_stripes_allocated = 0;
4207 new_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4208 /* for uninstantiated components, layout gen stores default stripe
4210 new_comp->llc_layout_gen = lod_comp->llc_stripe_offset;
4211 /* This makes the repeated component zero-length, placed at the end of
4212 * the preceding component */
4213 new_comp->llc_extent.e_start = new_comp->llc_extent.e_end;
4214 new_comp->llc_timestamp = lod_comp->llc_timestamp;
4215 new_comp->llc_pool = NULL;
4217 rc = lod_set_pool(&new_comp->llc_pool, lod_comp->llc_pool);
4221 if (new_comp->llc_ostlist.op_array) {
4222 __u32 *op_array = NULL;
4224 OBD_ALLOC(op_array, new_comp->llc_ostlist.op_size);
4226 GOTO(out, rc = -ENOMEM);
4227 memcpy(op_array, &new_comp->llc_ostlist.op_array,
4228 new_comp->llc_ostlist.op_size);
4229 new_comp->llc_ostlist.op_array = op_array;
4232 OBD_FREE(lo->ldo_comp_entries,
4233 sizeof(*comp_array) * lo->ldo_comp_cnt);
4234 lo->ldo_comp_entries = comp_array;
4235 lo->ldo_comp_cnt = new_cnt;
4237 /* Generate an id for the new component */
4238 mirror_id = mirror_id_of(new_comp->llc_id);
4239 new_comp->llc_id = LCME_ID_INVAL;
4240 new_comp->llc_id = lod_gen_component_id(lo, mirror_id, index + 1);
4241 if (new_comp->llc_id == LCME_ID_INVAL)
4242 GOTO(out, rc = -ERANGE);
4247 OBD_FREE(comp_array, sizeof(*comp_array) * new_cnt);
4252 static int lod_layout_data_init(struct lod_thread_info *info, __u32 comp_cnt)
4256 /* clear memory region that will be used for layout change */
4257 memset(&info->lti_layout_attr, 0, sizeof(struct lu_attr));
4258 info->lti_count = 0;
4260 if (info->lti_comp_size >= comp_cnt)
4263 if (info->lti_comp_size > 0) {
4264 OBD_FREE(info->lti_comp_idx,
4265 info->lti_comp_size * sizeof(__u32));
4266 info->lti_comp_size = 0;
4269 OBD_ALLOC(info->lti_comp_idx, comp_cnt * sizeof(__u32));
4270 if (!info->lti_comp_idx)
4273 info->lti_comp_size = comp_cnt;
4278 * Prepare new layout minus deleted components
4280 * Removes components marked for deletion (LCME_ID_INVAL) by copying to a new
4281 * layout and skipping those components. Removes stripe objects if any exist.
4284 * Reallocates layout components array (lo->ldo_comp_entries), invalidating
4285 * any pre-existing pointers to components.
4287 * Caller is responsible for updating mirror end (ldo_mirror[].lme_end).
4289 * \param[in] env execution environment for this thread
4290 * \param[in,out] lo object to update the layout of
4291 * \param[in] th transaction handle for this operation
4293 * \retval # of components deleted
4294 * \retval negative errno on error
4296 static int lod_layout_del_prep_layout(const struct lu_env *env,
4297 struct lod_object *lo,
4300 struct lod_layout_component *lod_comp;
4301 struct lod_thread_info *info = lod_env_info(env);
4302 int rc = 0, i, j, deleted = 0;
4306 LASSERT(lo->ldo_is_composite);
4307 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
4309 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
4313 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4314 lod_comp = &lo->ldo_comp_entries[i];
4316 if (lod_comp->llc_id != LCME_ID_INVAL) {
4317 /* Build array of things to keep */
4318 info->lti_comp_idx[info->lti_count++] = i;
4322 lod_obj_set_pool(lo, i, NULL);
4323 if (lod_comp->llc_ostlist.op_array) {
4324 OBD_FREE(lod_comp->llc_ostlist.op_array,
4325 lod_comp->llc_ostlist.op_size);
4326 lod_comp->llc_ostlist.op_array = NULL;
4327 lod_comp->llc_ostlist.op_size = 0;
4331 CDEBUG(D_LAYOUT, "deleting comp %d, left %d\n", i,
4332 lo->ldo_comp_cnt - deleted);
4334 /* No striping info for this component */
4335 if (lod_comp->llc_stripe == NULL)
4338 LASSERT(lod_comp->llc_stripe_count > 0);
4339 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4340 struct dt_object *obj = lod_comp->llc_stripe[j];
4345 /* components which are not init have no sub objects
4347 if (lod_comp_inited(lod_comp)) {
4348 rc = lod_sub_destroy(env, obj, th);
4353 lu_object_put(env, &obj->do_lu);
4354 lod_comp->llc_stripe[j] = NULL;
4356 OBD_FREE(lod_comp->llc_stripe, sizeof(struct dt_object *) *
4357 lod_comp->llc_stripes_allocated);
4358 lod_comp->llc_stripe = NULL;
4359 OBD_FREE(lod_comp->llc_ost_indices,
4360 sizeof(__u32) * lod_comp->llc_stripes_allocated);
4361 lod_comp->llc_ost_indices = NULL;
4362 lod_comp->llc_stripes_allocated = 0;
4365 /* info->lti_count has the amount of left components */
4366 LASSERTF(info->lti_count >= 0 && info->lti_count < lo->ldo_comp_cnt,
4367 "left = %d, lo->ldo_comp_cnt %d\n", (int)info->lti_count,
4368 (int)lo->ldo_comp_cnt);
4370 if (info->lti_count > 0) {
4371 struct lod_layout_component *comp_array;
4373 OBD_ALLOC(comp_array, sizeof(*comp_array) * info->lti_count);
4374 if (comp_array == NULL)
4375 GOTO(out, rc = -ENOMEM);
4377 for (i = 0; i < info->lti_count; i++) {
4378 memcpy(&comp_array[i],
4379 &lo->ldo_comp_entries[info->lti_comp_idx[i]],
4380 sizeof(*comp_array));
4383 OBD_FREE(lo->ldo_comp_entries,
4384 sizeof(*comp_array) * lo->ldo_comp_cnt);
4385 lo->ldo_comp_entries = comp_array;
4386 lo->ldo_comp_cnt = info->lti_count;
4388 lod_free_comp_entries(lo);
4393 return rc ? rc : deleted;
4397 * Delete layout component(s)
4399 * This function sets up the layout data in the env and does the setattrs
4400 * required to write out the new layout. The layout itself is modified in
4401 * lod_layout_del_prep_layout.
4403 * \param[in] env execution environment for this thread
4404 * \param[in] dt object
4405 * \param[in] th transaction handle
4407 * \retval 0 on success
4408 * \retval negative error number on failure
4410 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
4413 struct lod_object *lo = lod_dt_obj(dt);
4414 struct dt_object *next = dt_object_child(dt);
4415 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4418 LASSERT(lo->ldo_mirror_count == 1);
4420 rc = lod_layout_del_prep_layout(env, lo, th);
4424 /* Only do this if we didn't delete all components */
4425 if (lo->ldo_comp_cnt > 0) {
4426 lo->ldo_mirrors[0].lme_end = lo->ldo_comp_cnt - 1;
4427 lod_obj_inc_layout_gen(lo);
4430 LASSERT(dt_object_exists(dt));
4431 rc = dt_attr_get(env, next, attr);
4435 if (attr->la_size > 0) {
4437 attr->la_valid = LA_SIZE;
4438 rc = lod_sub_attr_set(env, next, attr, th);
4443 rc = lod_generate_and_set_lovea(env, lo, th);
4447 lod_striping_free(env, lo);
4452 static int lod_get_default_lov_striping(const struct lu_env *env,
4453 struct lod_object *lo,
4454 struct lod_default_striping *lds);
4456 * Implementation of dt_object_operations::do_xattr_set.
4458 * Sets specified extended attribute on the object. Three types of EAs are
4460 * LOV EA - stores striping for a regular file or default striping (when set
4462 * LMV EA - stores a marker for the striped directories
4463 * DMV EA - stores default directory striping
4465 * When striping is applied to a non-striped existing object (this is called
4466 * late striping), then LOD notices the caller wants to turn the object into a
4467 * striped one. The stripe objects are created and appropriate EA is set:
4468 * LOV EA storing all the stripes directly or LMV EA storing just a small header
4469 * with striping configuration.
4471 * \see dt_object_operations::do_xattr_set() in the API description for details.
4473 static int lod_xattr_set(const struct lu_env *env,
4474 struct dt_object *dt, const struct lu_buf *buf,
4475 const char *name, int fl, struct thandle *th)
4477 struct dt_object *next = dt_object_child(dt);
4481 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4482 strcmp(name, XATTR_NAME_LMV) == 0) {
4483 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
4485 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4486 strncmp(name, XATTR_NAME_LMV, strlen(XATTR_NAME_LMV)) == 0 &&
4487 strlen(name) > strlen(XATTR_NAME_LMV) + 1) {
4488 const char *op = name + strlen(XATTR_NAME_LMV) + 1;
4492 * XATTR_NAME_LMV".add" is never called, but only declared,
4493 * because lod_xattr_set_lmv() will do the addition.
4495 if (strcmp(op, "del") == 0)
4496 rc = lod_dir_layout_delete(env, dt, buf, th);
4497 else if (strcmp(op, "set") == 0)
4498 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LMV,
4502 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4503 strcmp(name, XATTR_NAME_LOV) == 0) {
4504 struct lod_default_striping *lds = lod_lds_buf_get(env);
4505 struct lov_user_md_v1 *v1 = buf->lb_buf;
4506 char pool[LOV_MAXPOOLNAME + 1];
4509 /* get existing striping config */
4510 rc = lod_get_default_lov_striping(env, lod_dt_obj(dt), lds);
4514 memset(pool, 0, sizeof(pool));
4515 if (lds->lds_def_striping_set == 1)
4516 lod_layout_get_pool(lds->lds_def_comp_entries,
4517 lds->lds_def_comp_cnt, pool,
4520 is_del = LOVEA_DELETE_VALUES(v1->lmm_stripe_size,
4521 v1->lmm_stripe_count,
4522 v1->lmm_stripe_offset,
4525 /* Retain the pool name if it is not given */
4526 if (v1->lmm_magic == LOV_USER_MAGIC_V1 && pool[0] != '\0' &&
4528 struct lod_thread_info *info = lod_env_info(env);
4529 struct lov_user_md_v3 *v3 = info->lti_ea_store;
4531 memset(v3, 0, sizeof(*v3));
4532 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
4533 v3->lmm_pattern = cpu_to_le32(v1->lmm_pattern);
4534 v3->lmm_stripe_count =
4535 cpu_to_le32(v1->lmm_stripe_count);
4536 v3->lmm_stripe_offset =
4537 cpu_to_le32(v1->lmm_stripe_offset);
4538 v3->lmm_stripe_size = cpu_to_le32(v1->lmm_stripe_size);
4540 strlcpy(v3->lmm_pool_name, pool,
4541 sizeof(v3->lmm_pool_name));
4543 info->lti_buf.lb_buf = v3;
4544 info->lti_buf.lb_len = sizeof(*v3);
4545 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4548 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name,
4552 if (lds->lds_def_striping_set == 1 &&
4553 lds->lds_def_comp_entries != NULL)
4554 lod_free_def_comp_entries(lds);
4557 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4558 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
4560 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
4563 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4564 (!strcmp(name, XATTR_NAME_LOV) ||
4565 !strncmp(name, XATTR_LUSTRE_LOV,
4566 strlen(XATTR_LUSTRE_LOV)))) {
4567 /* in case of lov EA swap, just set it
4568 * if not, it is a replay so check striping match what we
4569 * already have during req replay, declare_xattr_set()
4570 * defines striping, then create() does the work */
4571 if (fl & LU_XATTR_REPLACE) {
4572 /* free stripes, then update disk */
4573 lod_striping_free(env, lod_dt_obj(dt));
4575 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
4576 } else if (dt_object_remote(dt)) {
4577 /* This only happens during migration, see
4578 * mdd_migrate_create(), in which Master MDT will
4579 * create a remote target object, and only set
4580 * (migrating) stripe EA on the remote object,
4581 * and does not need creating each stripes. */
4582 rc = lod_sub_xattr_set(env, next, buf, name,
4584 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
4585 /* delete component(s) */
4586 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
4587 rc = lod_layout_del(env, dt, th);
4590 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
4591 * it's going to create create file with specified
4592 * component(s), the striping must have not being
4593 * cached in this case;
4595 * Otherwise, it's going to add/change component(s) to
4596 * an existing file, the striping must have been cached
4599 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
4600 !strcmp(name, XATTR_NAME_LOV),
4601 !lod_dt_obj(dt)->ldo_comp_cached));
4603 rc = lod_striped_create(env, dt, NULL, NULL, th);
4606 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
4607 rc = lod_replace_parent_fid(env, dt, buf, th, false);
4612 /* then all other xattr */
4613 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
4619 * Implementation of dt_object_operations::do_declare_xattr_del.
4621 * \see dt_object_operations::do_declare_xattr_del() in the API description
4624 static int lod_declare_xattr_del(const struct lu_env *env,
4625 struct dt_object *dt, const char *name,
4628 struct lod_object *lo = lod_dt_obj(dt);
4629 struct dt_object *next = dt_object_child(dt);
4634 rc = lod_sub_declare_xattr_del(env, next, name, th);
4638 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4641 /* NB: don't delete stripe LMV, because when we do this, normally we
4642 * will remove stripes, besides, if directory LMV is corrupt, this will
4643 * prevent deleting its LMV and fixing it (via LFSCK).
4645 if (!strcmp(name, XATTR_NAME_LMV))
4648 rc = lod_striping_load(env, lo);
4652 if (lo->ldo_dir_stripe_count == 0)
4655 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4656 struct dt_object *dto = lo->ldo_stripe[i];
4661 rc = lod_sub_declare_xattr_del(env, dto, name, th);
4670 * Implementation of dt_object_operations::do_xattr_del.
4672 * If EA storing a regular striping is being deleted, then release
4673 * all the references to the stripe objects in core.
4675 * \see dt_object_operations::do_xattr_del() in the API description for details.
4677 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
4678 const char *name, struct thandle *th)
4680 struct dt_object *next = dt_object_child(dt);
4681 struct lod_object *lo = lod_dt_obj(dt);
4686 if (!strcmp(name, XATTR_NAME_LOV) || !strcmp(name, XATTR_NAME_LMV))
4687 lod_striping_free(env, lod_dt_obj(dt));
4689 rc = lod_sub_xattr_del(env, next, name, th);
4690 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
4693 if (!strcmp(name, XATTR_NAME_LMV))
4696 if (lo->ldo_dir_stripe_count == 0)
4699 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4700 struct dt_object *dto = lo->ldo_stripe[i];
4705 rc = lod_sub_xattr_del(env, dto, name, th);
4714 * Implementation of dt_object_operations::do_xattr_list.
4716 * \see dt_object_operations::do_xattr_list() in the API description
4719 static int lod_xattr_list(const struct lu_env *env,
4720 struct dt_object *dt, const struct lu_buf *buf)
4722 return dt_xattr_list(env, dt_object_child(dt), buf);
4725 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
4727 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
4731 * Copy OST list from layout provided by user.
4733 * \param[in] lod_comp layout_component to be filled
4734 * \param[in] v3 LOV EA V3 user data
4736 * \retval 0 on success
4737 * \retval negative if failed
4739 int lod_comp_copy_ost_lists(struct lod_layout_component *lod_comp,
4740 struct lov_user_md_v3 *v3)
4746 if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
4747 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
4749 if (lod_comp->llc_ostlist.op_array) {
4750 if (lod_comp->llc_ostlist.op_size >=
4751 v3->lmm_stripe_count * sizeof(__u32)) {
4752 lod_comp->llc_ostlist.op_count =
4753 v3->lmm_stripe_count;
4756 OBD_FREE(lod_comp->llc_ostlist.op_array,
4757 lod_comp->llc_ostlist.op_size);
4760 /* copy ost list from lmm */
4761 lod_comp->llc_ostlist.op_count = v3->lmm_stripe_count;
4762 lod_comp->llc_ostlist.op_size = v3->lmm_stripe_count * sizeof(__u32);
4763 OBD_ALLOC(lod_comp->llc_ostlist.op_array,
4764 lod_comp->llc_ostlist.op_size);
4765 if (!lod_comp->llc_ostlist.op_array)
4768 for (j = 0; j < v3->lmm_stripe_count; j++) {
4769 lod_comp->llc_ostlist.op_array[j] =
4770 v3->lmm_objects[j].l_ost_idx;
4778 * Get default striping.
4780 * \param[in] env execution environment
4781 * \param[in] lo object
4782 * \param[out] lds default striping
4784 * \retval 0 on success
4785 * \retval negative if failed
4787 static int lod_get_default_lov_striping(const struct lu_env *env,
4788 struct lod_object *lo,
4789 struct lod_default_striping *lds)
4791 struct lod_thread_info *info = lod_env_info(env);
4792 struct lov_user_md_v1 *v1 = NULL;
4793 struct lov_user_md_v3 *v3 = NULL;
4794 struct lov_comp_md_v1 *comp_v1 = NULL;
4802 rc = lod_get_lov_ea(env, lo);
4806 if (rc < (typeof(rc))sizeof(struct lov_user_md))
4809 v1 = info->lti_ea_store;
4810 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
4811 lustre_swab_lov_user_md_v1(v1);
4812 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
4813 v3 = (struct lov_user_md_v3 *)v1;
4814 lustre_swab_lov_user_md_v3(v3);
4815 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_SPECIFIC)) {
4816 v3 = (struct lov_user_md_v3 *)v1;
4817 lustre_swab_lov_user_md_v3(v3);
4818 lustre_swab_lov_user_md_objects(v3->lmm_objects,
4819 v3->lmm_stripe_count);
4820 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1) ||
4821 v1->lmm_magic == __swab32(LOV_USER_MAGIC_SEL)) {
4822 comp_v1 = (struct lov_comp_md_v1 *)v1;
4823 lustre_swab_lov_comp_md_v1(comp_v1);
4826 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
4827 v1->lmm_magic != LOV_MAGIC_COMP_V1 &&
4828 v1->lmm_magic != LOV_MAGIC_SEL &&
4829 v1->lmm_magic != LOV_USER_MAGIC_SPECIFIC)
4832 if (v1->lmm_magic == LOV_MAGIC_COMP_V1 ||
4833 v1->lmm_magic == LOV_MAGIC_SEL) {
4834 comp_v1 = (struct lov_comp_md_v1 *)v1;
4835 comp_cnt = comp_v1->lcm_entry_count;
4838 mirror_cnt = comp_v1->lcm_mirror_count + 1;
4846 /* realloc default comp entries if necessary */
4847 rc = lod_def_striping_comp_resize(lds, comp_cnt);
4851 lds->lds_def_comp_cnt = comp_cnt;
4852 lds->lds_def_striping_is_composite = composite;
4853 lds->lds_def_mirror_cnt = mirror_cnt;
4855 for (i = 0; i < comp_cnt; i++) {
4856 struct lod_layout_component *lod_comp;
4859 lod_comp = &lds->lds_def_comp_entries[i];
4861 * reset lod_comp values, llc_stripes is always NULL in
4862 * the default striping template, llc_pool will be reset
4865 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
4868 v1 = (struct lov_user_md *)((char *)comp_v1 +
4869 comp_v1->lcm_entries[i].lcme_offset);
4870 lod_comp->llc_extent =
4871 comp_v1->lcm_entries[i].lcme_extent;
4872 /* We only inherit certain flags from the layout */
4873 lod_comp->llc_flags =
4874 comp_v1->lcm_entries[i].lcme_flags &
4875 LCME_TEMPLATE_FLAGS;
4878 if (!lov_pattern_supported(v1->lmm_pattern) &&
4879 !(v1->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
4880 lod_free_def_comp_entries(lds);
4884 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d "
4885 "stripe_offset=%d\n",
4886 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
4887 (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
4888 (int)v1->lmm_stripe_offset);
4890 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
4891 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
4892 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
4893 lod_comp->llc_pattern = v1->lmm_pattern;
4896 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
4897 /* XXX: sanity check here */
4898 v3 = (struct lov_user_md_v3 *) v1;
4899 if (v3->lmm_pool_name[0] != '\0')
4900 pool = v3->lmm_pool_name;
4902 lod_set_def_pool(lds, i, pool);
4903 if (v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
4904 v3 = (struct lov_user_md_v3 *)v1;
4905 rc = lod_comp_copy_ost_lists(lod_comp, v3);
4908 } else if (lod_comp->llc_ostlist.op_array &&
4909 lod_comp->llc_ostlist.op_count) {
4910 for (j = 0; j < lod_comp->llc_ostlist.op_count; j++)
4911 lod_comp->llc_ostlist.op_array[j] = -1;
4912 lod_comp->llc_ostlist.op_count = 0;
4916 lds->lds_def_striping_set = 1;
4921 * Get default directory striping.
4923 * \param[in] env execution environment
4924 * \param[in] lo object
4925 * \param[out] lds default striping
4927 * \retval 0 on success
4928 * \retval negative if failed
4930 static int lod_get_default_lmv_striping(const struct lu_env *env,
4931 struct lod_object *lo,
4932 struct lod_default_striping *lds)
4934 struct lmv_user_md *lmu;
4937 lds->lds_dir_def_striping_set = 0;
4939 rc = lod_get_default_lmv_ea(env, lo);
4943 if (rc >= (int)sizeof(*lmu)) {
4944 struct lod_thread_info *info = lod_env_info(env);
4946 lmu = info->lti_ea_store;
4948 lds->lds_dir_def_stripe_count =
4949 le32_to_cpu(lmu->lum_stripe_count);
4950 lds->lds_dir_def_stripe_offset =
4951 le32_to_cpu(lmu->lum_stripe_offset);
4952 lds->lds_dir_def_hash_type =
4953 le32_to_cpu(lmu->lum_hash_type);
4954 lds->lds_dir_def_striping_set = 1;
4961 * Get default striping in the object.
4963 * Get object default striping and default directory striping.
4965 * \param[in] env execution environment
4966 * \param[in] lo object
4967 * \param[out] lds default striping
4969 * \retval 0 on success
4970 * \retval negative if failed
4972 static int lod_get_default_striping(const struct lu_env *env,
4973 struct lod_object *lo,
4974 struct lod_default_striping *lds)
4978 rc = lod_get_default_lov_striping(env, lo, lds);
4979 rc1 = lod_get_default_lmv_striping(env, lo, lds);
4980 if (rc == 0 && rc1 < 0)
4987 * Apply default striping on object.
4989 * If object striping pattern is not set, set to the one in default striping.
4990 * The default striping is from parent or fs.
4992 * \param[in] lo new object
4993 * \param[in] lds default striping
4994 * \param[in] mode new object's mode
4996 static void lod_striping_from_default(struct lod_object *lo,
4997 const struct lod_default_striping *lds,
5000 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5001 struct lov_desc *desc = &d->lod_desc;
5004 if (lds->lds_def_striping_set && S_ISREG(mode)) {
5005 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
5006 lds->lds_def_comp_cnt);
5010 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
5011 if (lds->lds_def_mirror_cnt > 1)
5012 lo->ldo_flr_state = LCM_FL_RDONLY;
5014 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5015 struct lod_layout_component *obj_comp =
5016 &lo->ldo_comp_entries[i];
5017 struct lod_layout_component *def_comp =
5018 &lds->lds_def_comp_entries[i];
5020 CDEBUG(D_LAYOUT, "Inherit from default: flags=%#x "
5021 "size=%hu nr=%u offset=%u pattern=%#x pool=%s\n",
5022 def_comp->llc_flags,
5023 def_comp->llc_stripe_size,
5024 def_comp->llc_stripe_count,
5025 def_comp->llc_stripe_offset,
5026 def_comp->llc_pattern,
5027 def_comp->llc_pool ?: "");
5029 *obj_comp = *def_comp;
5030 if (def_comp->llc_pool != NULL) {
5031 /* pointer was copied from def_comp */
5032 obj_comp->llc_pool = NULL;
5033 lod_obj_set_pool(lo, i, def_comp->llc_pool);
5037 if (def_comp->llc_ostlist.op_array &&
5038 def_comp->llc_ostlist.op_count) {
5039 OBD_ALLOC(obj_comp->llc_ostlist.op_array,
5040 obj_comp->llc_ostlist.op_size);
5041 if (!obj_comp->llc_ostlist.op_array)
5043 memcpy(obj_comp->llc_ostlist.op_array,
5044 def_comp->llc_ostlist.op_array,
5045 obj_comp->llc_ostlist.op_size);
5046 } else if (def_comp->llc_ostlist.op_array) {
5047 obj_comp->llc_ostlist.op_array = NULL;
5051 * Don't initialize these fields for plain layout
5052 * (v1/v3) here, they are inherited in the order of
5053 * 'parent' -> 'fs default (root)' -> 'global default
5054 * values for stripe_count & stripe_size'.
5056 * see lod_ah_init().
5058 if (!lo->ldo_is_composite)
5061 lod_adjust_stripe_info(obj_comp, desc);
5063 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
5064 if (lo->ldo_dir_stripe_count == 0)
5065 lo->ldo_dir_stripe_count =
5066 lds->lds_dir_def_stripe_count;
5067 if (lo->ldo_dir_stripe_offset == -1)
5068 lo->ldo_dir_stripe_offset =
5069 lds->lds_dir_def_stripe_offset;
5070 if (lo->ldo_dir_hash_type == 0)
5071 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type &
5072 ~LMV_HASH_FLAG_SPACE;
5074 CDEBUG(D_LAYOUT, "striping from default dir: count:%hu, "
5075 "offset:%u, hash_type:%u\n",
5076 lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
5077 lo->ldo_dir_hash_type);
5081 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root)
5083 struct lod_layout_component *lod_comp;
5085 if (lo->ldo_comp_cnt == 0)
5088 if (lo->ldo_is_composite)
5091 lod_comp = &lo->ldo_comp_entries[0];
5093 if (lod_comp->llc_stripe_count <= 0 ||
5094 lod_comp->llc_stripe_size <= 0)
5097 if (from_root && (lod_comp->llc_pool == NULL ||
5098 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
5105 * Implementation of dt_object_operations::do_ah_init.
5107 * This method is used to make a decision on the striping configuration for the
5108 * object being created. It can be taken from the \a parent object if it exists,
5109 * or filesystem's default. The resulting configuration (number of stripes,
5110 * stripe size/offset, pool name, etc) is stored in the object itself and will
5111 * be used by the methods like ->doo_declare_create().
5113 * \see dt_object_operations::do_ah_init() in the API description for details.
5115 static void lod_ah_init(const struct lu_env *env,
5116 struct dt_allocation_hint *ah,
5117 struct dt_object *parent,
5118 struct dt_object *child,
5121 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
5122 struct lod_thread_info *info = lod_env_info(env);
5123 struct lod_default_striping *lds = lod_lds_buf_get(env);
5124 struct dt_object *nextp = NULL;
5125 struct dt_object *nextc;
5126 struct lod_object *lp = NULL;
5127 struct lod_object *lc;
5128 struct lov_desc *desc;
5129 struct lod_layout_component *lod_comp;
5135 if (likely(parent)) {
5136 nextp = dt_object_child(parent);
5137 lp = lod_dt_obj(parent);
5140 nextc = dt_object_child(child);
5141 lc = lod_dt_obj(child);
5143 LASSERT(!lod_obj_is_striped(child));
5144 /* default layout template may have been set on the regular file
5145 * when this is called from mdd_create_data() */
5146 if (S_ISREG(child_mode))
5147 lod_free_comp_entries(lc);
5149 if (!dt_object_exists(nextc))
5150 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
5152 if (S_ISDIR(child_mode)) {
5153 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
5155 /* other default values are 0 */
5156 lc->ldo_dir_stripe_offset = -1;
5158 /* no default striping configuration is needed for
5161 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5162 le32_to_cpu(lum1->lum_magic) == LMV_MAGIC_FOREIGN) {
5163 lc->ldo_dir_is_foreign = true;
5164 /* keep stripe_count 0 and stripe_offset -1 */
5165 CDEBUG(D_INFO, "no default striping for foreign dir\n");
5170 * If parent object is not root directory,
5171 * then get default striping from parent object.
5173 if (likely(lp != NULL) && !fid_is_root(lod_object_fid(lp)))
5174 lod_get_default_striping(env, lp, lds);
5176 /* set child default striping info, default value is NULL */
5177 if (lds->lds_def_striping_set || lds->lds_dir_def_striping_set)
5178 lc->ldo_def_striping = lds;
5180 /* It should always honour the specified stripes */
5181 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
5182 * will have old magic. In this case, we should ignore the
5183 * stripe count and try to create dir by default stripe.
5185 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5186 (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
5187 le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
5188 lc->ldo_dir_stripe_count =
5189 le32_to_cpu(lum1->lum_stripe_count);
5190 lc->ldo_dir_stripe_offset =
5191 le32_to_cpu(lum1->lum_stripe_offset);
5192 lc->ldo_dir_hash_type =
5193 le32_to_cpu(lum1->lum_hash_type);
5195 "set dirstripe: count %hu, offset %d, hash %u\n",
5196 lc->ldo_dir_stripe_count,
5197 (int)lc->ldo_dir_stripe_offset,
5198 lc->ldo_dir_hash_type);
5200 /* transfer defaults LMV to new directory */
5201 lod_striping_from_default(lc, lds, child_mode);
5203 /* set count 0 to create normal directory */
5204 if (lc->ldo_dir_stripe_count == 1)
5205 lc->ldo_dir_stripe_count = 0;
5208 /* shrink the stripe_count to the avaible MDT count */
5209 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
5210 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)) {
5211 lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
5212 if (lc->ldo_dir_stripe_count == 1)
5213 lc->ldo_dir_stripe_count = 0;
5216 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
5217 lc->ldo_dir_stripe_count,
5218 (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
5223 /* child object regular file*/
5225 if (!lod_object_will_be_striped(S_ISREG(child_mode),
5226 lu_object_fid(&child->do_lu)))
5229 /* If object is going to be striped over OSTs, transfer default
5230 * striping information to the child, so that we can use it
5231 * during declaration and creation.
5233 * Try from the parent first.
5235 if (likely(lp != NULL)) {
5236 rc = lod_get_default_lov_striping(env, lp, lds);
5238 lod_striping_from_default(lc, lds, child_mode);
5241 /* Initialize lod_device::lod_md_root object reference */
5242 if (d->lod_md_root == NULL) {
5243 struct dt_object *root;
5244 struct lod_object *lroot;
5246 lu_root_fid(&info->lti_fid);
5247 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
5248 if (!IS_ERR(root)) {
5249 lroot = lod_dt_obj(root);
5251 spin_lock(&d->lod_lock);
5252 if (d->lod_md_root != NULL)
5253 dt_object_put(env, &d->lod_md_root->ldo_obj);
5254 d->lod_md_root = lroot;
5255 spin_unlock(&d->lod_lock);
5259 /* try inherit layout from the root object (fs default) when:
5260 * - parent does not have default layout; or
5261 * - parent has plain(v1/v3) default layout, and some attributes
5262 * are not specified in the default layout;
5264 if (d->lod_md_root != NULL && lod_need_inherit_more(lc, true)) {
5265 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds);
5268 if (lc->ldo_comp_cnt == 0) {
5269 lod_striping_from_default(lc, lds, child_mode);
5270 } else if (!lds->lds_def_striping_is_composite) {
5271 struct lod_layout_component *def_comp;
5273 LASSERT(!lc->ldo_is_composite);
5274 lod_comp = &lc->ldo_comp_entries[0];
5275 def_comp = &lds->lds_def_comp_entries[0];
5277 if (lod_comp->llc_stripe_count <= 0)
5278 lod_comp->llc_stripe_count =
5279 def_comp->llc_stripe_count;
5280 if (lod_comp->llc_stripe_size <= 0)
5281 lod_comp->llc_stripe_size =
5282 def_comp->llc_stripe_size;
5283 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT &&
5284 (!lod_comp->llc_pool || !lod_comp->llc_pool[0]))
5285 lod_comp->llc_stripe_offset =
5286 def_comp->llc_stripe_offset;
5287 if (lod_comp->llc_pool == NULL)
5288 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
5293 * fs default striping may not be explicitly set, or historically set
5294 * in config log, use them.
5296 if (lod_need_inherit_more(lc, false)) {
5297 if (lc->ldo_comp_cnt == 0) {
5298 rc = lod_alloc_comp_entries(lc, 0, 1);
5300 /* fail to allocate memory, will create a
5301 * non-striped file. */
5303 lc->ldo_is_composite = 0;
5304 lod_comp = &lc->ldo_comp_entries[0];
5305 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
5307 LASSERT(!lc->ldo_is_composite);
5308 lod_comp = &lc->ldo_comp_entries[0];
5309 desc = &d->lod_desc;
5310 lod_adjust_stripe_info(lod_comp, desc);
5316 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
5318 * Size initialization on late striping.
5320 * Propagate the size of a truncated object to a deferred striping.
5321 * This function handles a special case when truncate was done on a
5322 * non-striped object and now while the striping is being created
5323 * we can't lose that size, so we have to propagate it to the stripes
5326 * \param[in] env execution environment
5327 * \param[in] dt object
5328 * \param[in] th transaction handle
5330 * \retval 0 on success
5331 * \retval negative if failed
5333 static int lod_declare_init_size(const struct lu_env *env,
5334 struct dt_object *dt, struct thandle *th)
5336 struct dt_object *next = dt_object_child(dt);
5337 struct lod_object *lo = lod_dt_obj(dt);
5338 struct dt_object **objects = NULL;
5339 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
5340 uint64_t size, offs;
5341 int i, rc, stripe, stripe_count = 0, stripe_size = 0;
5342 struct lu_extent size_ext;
5345 if (!lod_obj_is_striped(dt))
5348 rc = dt_attr_get(env, next, attr);
5349 LASSERT(attr->la_valid & LA_SIZE);
5353 size = attr->la_size;
5357 size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
5358 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5359 struct lod_layout_component *lod_comp;
5360 struct lu_extent *extent;
5362 lod_comp = &lo->ldo_comp_entries[i];
5364 if (lod_comp->llc_stripe == NULL)
5367 extent = &lod_comp->llc_extent;
5368 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
5369 if (!lo->ldo_is_composite ||
5370 lu_extent_is_overlapped(extent, &size_ext)) {
5371 objects = lod_comp->llc_stripe;
5372 stripe_count = lod_comp->llc_stripe_count;
5373 stripe_size = lod_comp->llc_stripe_size;
5376 if (stripe_count == 0)
5379 LASSERT(objects != NULL && stripe_size != 0);
5380 /* ll_do_div64(a, b) returns a % b, and a = a / b */
5381 ll_do_div64(size, (__u64)stripe_size);
5382 stripe = ll_do_div64(size, (__u64)stripe_count);
5383 LASSERT(objects[stripe] != NULL);
5385 size = size * stripe_size;
5386 offs = attr->la_size;
5387 size += ll_do_div64(offs, stripe_size);
5389 attr->la_valid = LA_SIZE;
5390 attr->la_size = size;
5392 rc = lod_sub_declare_attr_set(env, objects[stripe],
5401 * Declare creation of striped object.
5403 * The function declares creation stripes for a regular object. The function
5404 * also declares whether the stripes will be created with non-zero size if
5405 * previously size was set non-zero on the master object. If object \a dt is
5406 * not local, then only fully defined striping can be applied in \a lovea.
5407 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
5410 * \param[in] env execution environment
5411 * \param[in] dt object
5412 * \param[in] attr attributes the stripes will be created with
5413 * \param[in] lovea a buffer containing striping description
5414 * \param[in] th transaction handle
5416 * \retval 0 on success
5417 * \retval negative if failed
5419 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
5420 struct lu_attr *attr,
5421 const struct lu_buf *lovea, struct thandle *th)
5423 struct lod_thread_info *info = lod_env_info(env);
5424 struct dt_object *next = dt_object_child(dt);
5425 struct lod_object *lo = lod_dt_obj(dt);
5429 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
5430 GOTO(out, rc = -ENOMEM);
5432 if (!dt_object_remote(next)) {
5433 /* choose OST and generate appropriate objects */
5434 rc = lod_prepare_create(env, lo, attr, lovea, th);
5439 * declare storage for striping data
5441 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5443 /* LOD can not choose OST objects for remote objects, i.e.
5444 * stripes must be ready before that. Right now, it can only
5445 * happen during migrate, i.e. migrate process needs to create
5446 * remote regular file (mdd_migrate_create), then the migrate
5447 * process will provide stripeEA. */
5448 LASSERT(lovea != NULL);
5449 info->lti_buf = *lovea;
5452 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
5453 XATTR_NAME_LOV, 0, th);
5458 * if striping is created with local object's size > 0,
5459 * we have to propagate this size to specific object
5460 * the case is possible only when local object was created previously
5462 if (dt_object_exists(next))
5463 rc = lod_declare_init_size(env, dt, th);
5466 /* failed to create striping or to set initial size, let's reset
5467 * config so that others don't get confused */
5469 lod_striping_free(env, lo);
5475 * Whether subdirectories under \a dt should be created on MDTs by space QoS
5477 * If LMV_HASH_FLAG_SPACE is set on directory default layout, its subdirectories
5478 * should be created on MDT by space QoS.
5480 * \param[in] env execution environment
5481 * \param[in] dev lu device
5482 * \param[in] dt object
5484 * \retval 1 if directory should create subdir by space usage
5486 * \retval -ev if failed
5488 static inline int dt_object_qos_mkdir(const struct lu_env *env,
5489 struct lu_device *dev,
5490 struct dt_object *dt)
5492 struct lod_thread_info *info = lod_env_info(env);
5493 struct lu_object *obj;
5494 struct lod_object *lo;
5495 struct lmv_user_md *lmu;
5498 obj = lu_object_find_slice(env, dev, lu_object_fid(&dt->do_lu), NULL);
5500 return PTR_ERR(obj);
5502 lo = lu2lod_obj(obj);
5504 rc = lod_get_default_lmv_ea(env, lo);
5505 dt_object_put(env, dt);
5509 if (rc < (int)sizeof(*lmu))
5512 lmu = info->lti_ea_store;
5513 return !!(le32_to_cpu(lmu->lum_hash_type) & LMV_HASH_FLAG_SPACE);
5517 * Implementation of dt_object_operations::do_declare_create.
5519 * The method declares creation of a new object. If the object will be striped,
5520 * then helper functions are called to find FIDs for the stripes, declare
5521 * creation of the stripes and declare initialization of the striping
5522 * information to be stored in the master object.
5524 * \see dt_object_operations::do_declare_create() in the API description
5527 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
5528 struct lu_attr *attr,
5529 struct dt_allocation_hint *hint,
5530 struct dt_object_format *dof, struct thandle *th)
5532 struct dt_object *next = dt_object_child(dt);
5533 struct lod_object *lo = lod_dt_obj(dt);
5542 * first of all, we declare creation of local object
5544 rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
5549 * it's lod_ah_init() that has decided the object will be striped
5551 if (dof->dof_type == DFT_REGULAR) {
5552 /* callers don't want stripes */
5553 /* XXX: all tricky interactions with ->ah_make_hint() decided
5554 * to use striping, then ->declare_create() behaving differently
5555 * should be cleaned */
5556 if (dof->u.dof_reg.striped != 0)
5557 rc = lod_declare_striped_create(env, dt, attr,
5559 } else if (dof->dof_type == DFT_DIR) {
5560 struct seq_server_site *ss;
5561 struct lu_buf buf = { NULL };
5562 struct lu_buf *lmu = NULL;
5564 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
5566 /* If the parent has default stripeEA, and client
5567 * did not find it before sending create request,
5568 * then MDT will return -EREMOTE, and client will
5569 * retrieve the default stripeEA and re-create the
5572 * Note: if dah_eadata != NULL, it means creating the
5573 * striped directory with specified stripeEA, then it
5574 * should ignore the default stripeEA */
5575 if (hint != NULL && hint->dah_eadata == NULL) {
5576 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
5577 GOTO(out, rc = -EREMOTE);
5579 if (lo->ldo_dir_stripe_offset == -1) {
5581 * child and parent should be in the same MDT,
5582 * but if parent has plain layout, it's allowed.
5584 if (hint->dah_parent &&
5585 dt_object_remote(hint->dah_parent)) {
5586 rc = dt_object_qos_mkdir(env,
5587 lo->ldo_obj.do_lu.lo_dev,
5590 GOTO(out, rc ? rc : -EREMOTE);
5592 } else if (lo->ldo_dir_stripe_offset !=
5594 struct lod_device *lod;
5595 struct lod_tgt_descs *ltd;
5596 struct lod_tgt_desc *tgt = NULL;
5597 bool found_mdt = false;
5600 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5601 ltd = &lod->lod_mdt_descs;
5602 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
5603 tgt = LTD_TGT(ltd, i);
5604 if (tgt->ltd_index ==
5605 lo->ldo_dir_stripe_offset) {
5611 /* If the MDT indicated by stripe_offset can be
5612 * found, then tell client to resend the create
5613 * request to the correct MDT, otherwise return
5614 * error to client */
5616 GOTO(out, rc = -EREMOTE);
5618 GOTO(out, rc = -EINVAL);
5620 } else if (hint && hint->dah_eadata) {
5622 lmu->lb_buf = (void *)hint->dah_eadata;
5623 lmu->lb_len = hint->dah_eadata_len;
5626 rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
5630 /* failed to create striping or to set initial size, let's reset
5631 * config so that others don't get confused */
5633 lod_striping_free(env, lo);
5638 * Generate component ID for new created component.
5640 * \param[in] lo LOD object
5641 * \param[in] comp_idx index of ldo_comp_entries
5643 * \retval component ID on success
5644 * \retval LCME_ID_INVAL on failure
5646 static __u32 lod_gen_component_id(struct lod_object *lo,
5647 int mirror_id, int comp_idx)
5649 struct lod_layout_component *lod_comp;
5650 __u32 id, start, end;
5653 LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
5655 lod_obj_inc_layout_gen(lo);
5656 id = lo->ldo_layout_gen;
5657 if (likely(id <= SEQ_ID_MAX))
5658 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
5660 /* Layout generation wraps, need to check collisions. */
5661 start = id & SEQ_ID_MASK;
5664 for (id = start; id <= end; id++) {
5665 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5666 lod_comp = &lo->ldo_comp_entries[i];
5667 if (pflr_id(mirror_id, id) == lod_comp->llc_id)
5670 /* Found the ununsed ID */
5671 if (i == lo->ldo_comp_cnt)
5672 RETURN(pflr_id(mirror_id, id));
5674 if (end == LCME_ID_MAX) {
5676 end = min(lo->ldo_layout_gen & LCME_ID_MASK,
5677 (__u32)(LCME_ID_MAX - 1));
5681 RETURN(LCME_ID_INVAL);
5685 * Creation of a striped regular object.
5687 * The function is called to create the stripe objects for a regular
5688 * striped file. This can happen at the initial object creation or
5689 * when the caller asks LOD to do so using ->do_xattr_set() method
5690 * (so called late striping). Notice all the information are already
5691 * prepared in the form of the list of objects (ldo_stripe field).
5692 * This is done during declare phase.
5694 * \param[in] env execution environment
5695 * \param[in] dt object
5696 * \param[in] attr attributes the stripes will be created with
5697 * \param[in] dof format of stripes (see OSD API description)
5698 * \param[in] th transaction handle
5700 * \retval 0 on success
5701 * \retval negative if failed
5703 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
5704 struct lu_attr *attr, struct dt_object_format *dof,
5707 struct lod_layout_component *lod_comp;
5708 struct lod_object *lo = lod_dt_obj(dt);
5713 LASSERT((lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL) ||
5714 lo->ldo_is_foreign);
5716 mirror_id = 0; /* non-flr file's mirror_id is 0 */
5717 if (lo->ldo_mirror_count > 1) {
5718 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5719 lod_comp = &lo->ldo_comp_entries[i];
5720 if (lod_comp->llc_id != LCME_ID_INVAL &&
5721 mirror_id_of(lod_comp->llc_id) > mirror_id)
5722 mirror_id = mirror_id_of(lod_comp->llc_id);
5726 /* create all underlying objects */
5727 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5728 lod_comp = &lo->ldo_comp_entries[i];
5730 if (lod_comp->llc_id == LCME_ID_INVAL) {
5731 /* only the component of FLR layout with more than 1
5732 * mirror has mirror ID in its component ID.
5734 if (lod_comp->llc_extent.e_start == 0 &&
5735 lo->ldo_mirror_count > 1)
5738 lod_comp->llc_id = lod_gen_component_id(lo,
5740 if (lod_comp->llc_id == LCME_ID_INVAL)
5741 GOTO(out, rc = -ERANGE);
5744 if (lod_comp_inited(lod_comp))
5747 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
5748 lod_comp_set_init(lod_comp);
5750 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
5751 lod_comp_set_init(lod_comp);
5753 if (lod_comp->llc_stripe == NULL)
5756 LASSERT(lod_comp->llc_stripe_count);
5757 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
5758 struct dt_object *object = lod_comp->llc_stripe[j];
5759 LASSERT(object != NULL);
5760 rc = lod_sub_create(env, object, attr, NULL, dof, th);
5764 lod_comp_set_init(lod_comp);
5767 rc = lod_fill_mirrors(lo);
5771 rc = lod_generate_and_set_lovea(env, lo, th);
5775 lo->ldo_comp_cached = 1;
5779 lod_striping_free(env, lo);
5783 static inline bool lod_obj_is_dom(struct dt_object *dt)
5785 struct lod_object *lo = lod_dt_obj(dt);
5787 if (!dt_object_exists(dt_object_child(dt)))
5790 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
5793 if (!lo->ldo_comp_cnt)
5796 return (lov_pattern(lo->ldo_comp_entries[0].llc_pattern) ==
5801 * Implementation of dt_object_operations::do_create.
5803 * If any of preceeding methods (like ->do_declare_create(),
5804 * ->do_ah_init(), etc) chose to create a striped object,
5805 * then this method will create the master and the stripes.
5807 * \see dt_object_operations::do_create() in the API description for details.
5809 static int lod_create(const struct lu_env *env, struct dt_object *dt,
5810 struct lu_attr *attr, struct dt_allocation_hint *hint,
5811 struct dt_object_format *dof, struct thandle *th)
5816 /* create local object */
5817 rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
5821 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
5822 (lod_obj_is_striped(dt) || lod_obj_is_dom(dt)) &&
5823 dof->u.dof_reg.striped != 0) {
5824 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
5825 rc = lod_striped_create(env, dt, attr, dof, th);
5832 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
5833 struct dt_object *dt, struct thandle *th,
5834 int comp_idx, int stripe_idx,
5835 struct lod_obj_stripe_cb_data *data)
5837 if (data->locd_declare)
5838 return lod_sub_declare_destroy(env, dt, th);
5839 else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
5840 stripe_idx == cfs_fail_val)
5841 return lod_sub_destroy(env, dt, th);
5847 * Implementation of dt_object_operations::do_declare_destroy.
5849 * If the object is a striped directory, then the function declares reference
5850 * removal from the master object (this is an index) to the stripes and declares
5851 * destroy of all the stripes. In all the cases, it declares an intention to
5852 * destroy the object itself.
5854 * \see dt_object_operations::do_declare_destroy() in the API description
5857 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
5860 struct dt_object *next = dt_object_child(dt);
5861 struct lod_object *lo = lod_dt_obj(dt);
5862 struct lod_thread_info *info = lod_env_info(env);
5863 struct dt_object *stripe;
5864 char *stripe_name = info->lti_key;
5870 * load striping information, notice we don't do this when object
5871 * is being initialized as we don't need this information till
5872 * few specific cases like destroy, chown
5874 rc = lod_striping_load(env, lo);
5878 /* declare destroy for all underlying objects */
5879 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5880 rc = next->do_ops->do_index_try(env, next,
5881 &dt_directory_features);
5885 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5886 stripe = lo->ldo_stripe[i];
5890 rc = lod_sub_declare_ref_del(env, next, th);
5894 snprintf(stripe_name, sizeof(info->lti_key),
5896 PFID(lu_object_fid(&stripe->do_lu)), i);
5897 rc = lod_sub_declare_delete(env, next,
5898 (const struct dt_key *)stripe_name, th);
5905 * we declare destroy for the local object
5907 rc = lod_sub_declare_destroy(env, next, th);
5911 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
5912 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
5915 if (!lod_obj_is_striped(dt))
5918 /* declare destroy all striped objects */
5919 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5920 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5921 stripe = lo->ldo_stripe[i];
5925 if (!dt_object_exists(stripe))
5928 rc = lod_sub_declare_ref_del(env, stripe, th);
5932 rc = lod_sub_declare_destroy(env, stripe, th);
5937 struct lod_obj_stripe_cb_data data = { { 0 } };
5939 data.locd_declare = true;
5940 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
5941 rc = lod_obj_for_each_stripe(env, lo, th, &data);
5948 * Implementation of dt_object_operations::do_destroy.
5950 * If the object is a striped directory, then the function removes references
5951 * from the master object (this is an index) to the stripes and destroys all
5952 * the stripes. In all the cases, the function destroys the object itself.
5954 * \see dt_object_operations::do_destroy() in the API description for details.
5956 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
5959 struct dt_object *next = dt_object_child(dt);
5960 struct lod_object *lo = lod_dt_obj(dt);
5961 struct lod_thread_info *info = lod_env_info(env);
5962 char *stripe_name = info->lti_key;
5963 struct dt_object *stripe;
5969 /* destroy sub-stripe of master object */
5970 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5971 rc = next->do_ops->do_index_try(env, next,
5972 &dt_directory_features);
5976 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5977 stripe = lo->ldo_stripe[i];
5981 rc = lod_sub_ref_del(env, next, th);
5985 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
5986 PFID(lu_object_fid(&stripe->do_lu)), i);
5988 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
5989 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
5990 PFID(lu_object_fid(&stripe->do_lu)));
5992 rc = lod_sub_delete(env, next,
5993 (const struct dt_key *)stripe_name, th);
5999 rc = lod_sub_destroy(env, next, th);
6003 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
6004 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
6007 if (!lod_obj_is_striped(dt))
6010 /* destroy all striped objects */
6011 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6012 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6013 stripe = lo->ldo_stripe[i];
6017 if (!dt_object_exists(stripe))
6020 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
6021 i == cfs_fail_val) {
6022 dt_write_lock(env, stripe, DT_TGT_CHILD);
6023 rc = lod_sub_ref_del(env, stripe, th);
6024 dt_write_unlock(env, stripe);
6028 rc = lod_sub_destroy(env, stripe, th);
6034 struct lod_obj_stripe_cb_data data = { { 0 } };
6036 data.locd_declare = false;
6037 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6038 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6045 * Implementation of dt_object_operations::do_declare_ref_add.
6047 * \see dt_object_operations::do_declare_ref_add() in the API description
6050 static int lod_declare_ref_add(const struct lu_env *env,
6051 struct dt_object *dt, struct thandle *th)
6053 return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
6057 * Implementation of dt_object_operations::do_ref_add.
6059 * \see dt_object_operations::do_ref_add() in the API description for details.
6061 static int lod_ref_add(const struct lu_env *env,
6062 struct dt_object *dt, struct thandle *th)
6064 return lod_sub_ref_add(env, dt_object_child(dt), th);
6068 * Implementation of dt_object_operations::do_declare_ref_del.
6070 * \see dt_object_operations::do_declare_ref_del() in the API description
6073 static int lod_declare_ref_del(const struct lu_env *env,
6074 struct dt_object *dt, struct thandle *th)
6076 return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
6080 * Implementation of dt_object_operations::do_ref_del
6082 * \see dt_object_operations::do_ref_del() in the API description for details.
6084 static int lod_ref_del(const struct lu_env *env,
6085 struct dt_object *dt, struct thandle *th)
6087 return lod_sub_ref_del(env, dt_object_child(dt), th);
6091 * Implementation of dt_object_operations::do_object_sync.
6093 * \see dt_object_operations::do_object_sync() in the API description
6096 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
6097 __u64 start, __u64 end)
6099 return dt_object_sync(env, dt_object_child(dt), start, end);
6103 * Implementation of dt_object_operations::do_object_unlock.
6105 * Used to release LDLM lock(s).
6107 * \see dt_object_operations::do_object_unlock() in the API description
6110 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
6111 struct ldlm_enqueue_info *einfo,
6112 union ldlm_policy_data *policy)
6114 struct lod_object *lo = lod_dt_obj(dt);
6115 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
6116 int slave_locks_size;
6120 if (slave_locks == NULL)
6123 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
6124 /* Note: for remote lock for single stripe dir, MDT will cancel
6125 * the lock by lockh directly */
6126 LASSERT(!dt_object_remote(dt_object_child(dt)));
6128 /* locks were unlocked in MDT layer */
6129 for (i = 0; i < slave_locks->ha_count; i++)
6130 LASSERT(!lustre_handle_is_used(&slave_locks->ha_handles[i]));
6133 * NB, ha_count may not equal to ldo_dir_stripe_count, because dir
6134 * layout may change, e.g., shrink dir layout after migration.
6136 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6137 if (lo->ldo_stripe[i])
6138 dt_invalidate(env, lo->ldo_stripe[i]);
6141 slave_locks_size = offsetof(typeof(*slave_locks),
6142 ha_handles[slave_locks->ha_count]);
6143 OBD_FREE(slave_locks, slave_locks_size);
6144 einfo->ei_cbdata = NULL;
6150 * Implementation of dt_object_operations::do_object_lock.
6152 * Used to get LDLM lock on the non-striped and striped objects.
6154 * \see dt_object_operations::do_object_lock() in the API description
6157 static int lod_object_lock(const struct lu_env *env,
6158 struct dt_object *dt,
6159 struct lustre_handle *lh,
6160 struct ldlm_enqueue_info *einfo,
6161 union ldlm_policy_data *policy)
6163 struct lod_object *lo = lod_dt_obj(dt);
6164 int slave_locks_size;
6165 struct lustre_handle_array *slave_locks = NULL;
6170 /* remote object lock */
6171 if (!einfo->ei_enq_slave) {
6172 LASSERT(dt_object_remote(dt));
6173 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
6177 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
6180 rc = lod_striping_load(env, lo);
6185 if (lo->ldo_dir_stripe_count <= 1)
6188 slave_locks_size = offsetof(typeof(*slave_locks),
6189 ha_handles[lo->ldo_dir_stripe_count]);
6190 /* Freed in lod_object_unlock */
6191 OBD_ALLOC(slave_locks, slave_locks_size);
6194 slave_locks->ha_count = lo->ldo_dir_stripe_count;
6196 /* striped directory lock */
6197 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6198 struct lustre_handle lockh;
6199 struct ldlm_res_id *res_id;
6200 struct dt_object *stripe;
6202 stripe = lo->ldo_stripe[i];
6206 res_id = &lod_env_info(env)->lti_res_id;
6207 fid_build_reg_res_name(lu_object_fid(&stripe->do_lu), res_id);
6208 einfo->ei_res_id = res_id;
6210 if (dt_object_remote(stripe)) {
6211 set_bit(i, (void *)slave_locks->ha_map);
6212 rc = dt_object_lock(env, stripe, &lockh, einfo, policy);
6214 struct ldlm_namespace *ns = einfo->ei_namespace;
6215 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
6216 ldlm_completion_callback completion = einfo->ei_cb_cp;
6217 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
6219 if (einfo->ei_mode == LCK_PW ||
6220 einfo->ei_mode == LCK_EX)
6221 dlmflags |= LDLM_FL_COS_INCOMPAT;
6223 LASSERT(ns != NULL);
6224 rc = ldlm_cli_enqueue_local(env, ns, res_id, LDLM_IBITS,
6225 policy, einfo->ei_mode,
6226 &dlmflags, blocking,
6228 NULL, 0, LVB_T_NONE,
6233 ldlm_lock_decref_and_cancel(
6234 &slave_locks->ha_handles[i],
6236 OBD_FREE(slave_locks, slave_locks_size);
6239 slave_locks->ha_handles[i] = lockh;
6241 einfo->ei_cbdata = slave_locks;
6247 * Implementation of dt_object_operations::do_invalidate.
6249 * \see dt_object_operations::do_invalidate() in the API description for details
6251 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
6253 return dt_invalidate(env, dt_object_child(dt));
6256 static int lod_declare_instantiate_components(const struct lu_env *env,
6257 struct lod_object *lo, struct thandle *th)
6259 struct lod_thread_info *info = lod_env_info(env);
6264 LASSERT(info->lti_count < lo->ldo_comp_cnt);
6266 for (i = 0; i < info->lti_count; i++) {
6267 rc = lod_qos_prep_create(env, lo, NULL, th,
6268 info->lti_comp_idx[i]);
6274 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6275 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
6276 &info->lti_buf, XATTR_NAME_LOV, 0, th);
6283 * Check OSTs for an existing component for further extension
6285 * Checks if OSTs are still healthy and not out of space. Gets free space
6286 * on OSTs (relative to allocation watermark rmb_low) and compares to
6287 * the proposed new_end for this component.
6289 * Decides whether or not to extend a component on its current OSTs.
6291 * \param[in] env execution environment for this thread
6292 * \param[in] lo object we're checking
6293 * \param[in] index index of this component
6294 * \param[in] extension_size extension size for this component
6295 * \param[in] extent layout extent for requested operation
6296 * \param[in] comp_extent extension component extent
6297 * \param[in] write if this is write operation
6299 * \retval true - OK to extend on current OSTs
6300 * \retval false - do not extend on current OSTs
6302 static bool lod_sel_osts_allowed(const struct lu_env *env,
6303 struct lod_object *lo,
6304 int index, __u64 extension_size,
6305 struct lu_extent *extent,
6306 struct lu_extent *comp_extent, int write)
6308 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[index];
6309 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6310 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
6311 __u64 available = 0;
6318 LASSERT(lod_comp->llc_stripe_count != 0);
6321 (extent->e_start == 0 && extent->e_end == OBD_OBJECT_EOF)) {
6322 /* truncate or append */
6323 size = extension_size;
6325 /* In case of write op, check the real write extent,
6326 * it may be larger than the extension_size */
6327 size = roundup(min(extent->e_end, comp_extent->e_end) -
6328 max(extent->e_start, comp_extent->e_start),
6331 /* extension_size is file level, so we must divide by stripe count to
6332 * compare it to available space on a single OST */
6333 size /= lod_comp->llc_stripe_count;
6335 lod_getref(&lod->lod_ost_descs);
6336 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
6337 int index = lod_comp->llc_ost_indices[i];
6338 struct lod_tgt_desc *ost = OST_TGT(lod, index);
6339 struct obd_statfs_info info = { 0 };
6340 int j, repeated = 0;
6344 /* Get the number of times this OST repeats in this component.
6345 * Note: inter-component repeats are not counted as this is
6346 * considered as a rare case: we try to not repeat OST in other
6347 * components if possible. */
6348 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6349 if (index != lod_comp->llc_ost_indices[j])
6352 /* already handled */
6358 if (j < lod_comp->llc_stripe_count)
6361 if (!cfs_bitmap_check(lod->lod_ost_bitmap, index)) {
6362 CDEBUG(D_LAYOUT, "ost %d no longer present\n", index);
6367 rc = dt_statfs_info(env, ost->ltd_ost, sfs, &info);
6369 CDEBUG(D_LAYOUT, "statfs failed for ost %d, error %d\n",
6375 if (sfs->os_state & OS_STATE_ENOSPC ||
6376 sfs->os_state & OS_STATE_READONLY ||
6377 sfs->os_state & OS_STATE_DEGRADED) {
6378 CDEBUG(D_LAYOUT, "ost %d is not availble for SEL "
6379 "extension, state %u\n", index, sfs->os_state);
6385 available = sfs->os_bavail * sfs->os_bsize;
6386 /* 'available' is relative to the allocation threshold */
6387 available -= (__u64) info.os_reserved_mb_low << 20;
6389 CDEBUG(D_LAYOUT, "ost %d lowwm: %d highwm: %d, "
6390 "%llu %% blocks available, %llu %% blocks free\n",
6391 index, info.os_reserved_mb_low, info.os_reserved_mb_high,
6392 (100ull * sfs->os_bavail) / sfs->os_blocks,
6393 (100ull * sfs->os_bfree) / sfs->os_blocks);
6395 if (size * repeated > available) {
6397 CDEBUG(D_LAYOUT, "low space on ost %d, available %llu "
6398 "< extension size %llu\n", index, available,
6403 lod_putref(lod, &lod->lod_ost_descs);
6409 * Adjust extents after component removal
6411 * When we remove an extension component, we move the start of the next
6412 * component to match the start of the extension component, so no space is left
6415 * \param[in] env execution environment for this thread
6416 * \param[in] lo object
6417 * \param[in] max_comp layout component
6418 * \param[in] index index of this component
6420 * \retval 0 on success
6421 * \retval negative errno on error
6423 static void lod_sel_adjust_extents(const struct lu_env *env,
6424 struct lod_object *lo,
6425 int max_comp, int index)
6427 struct lod_layout_component *lod_comp = NULL;
6428 struct lod_layout_component *next = NULL;
6429 struct lod_layout_component *prev = NULL;
6430 __u64 new_start = 0;
6434 /* Extension space component */
6435 lod_comp = &lo->ldo_comp_entries[index];
6436 next = &lo->ldo_comp_entries[index + 1];
6437 prev = &lo->ldo_comp_entries[index - 1];
6439 LASSERT(lod_comp != NULL && prev != NULL && next != NULL);
6440 LASSERT(lod_comp->llc_flags & LCME_FL_EXTENSION);
6442 /* Previous is being removed */
6443 if (prev && prev->llc_id == LCME_ID_INVAL)
6444 new_start = prev->llc_extent.e_start;
6446 new_start = lod_comp->llc_extent.e_start;
6448 for (i = index + 1; i < max_comp; i++) {
6449 lod_comp = &lo->ldo_comp_entries[i];
6451 start = lod_comp->llc_extent.e_start;
6452 lod_comp->llc_extent.e_start = new_start;
6454 /* We only move zero length extendable components */
6455 if (!(start == lod_comp->llc_extent.e_end))
6458 LASSERT(!(lod_comp->llc_flags & LCME_FL_INIT));
6460 lod_comp->llc_extent.e_end = new_start;
6464 /* Calculate the proposed 'new end' for a component we're extending */
6465 static __u64 lod_extension_new_end(__u64 extension_size, __u64 extent_end,
6466 __u32 stripe_size, __u64 component_end,
6467 __u64 extension_end)
6471 LASSERT(extension_size != 0 && stripe_size != 0);
6473 /* Round up to extension size */
6474 if (extent_end == OBD_OBJECT_EOF) {
6475 new_end = OBD_OBJECT_EOF;
6477 /* Add at least extension_size to the previous component_end,
6478 * covering the req layout extent */
6479 new_end = max(extent_end - component_end, extension_size);
6480 new_end = roundup(new_end, extension_size);
6481 new_end += component_end;
6483 /* Component end must be min stripe size aligned */
6484 if (new_end % stripe_size) {
6485 CDEBUG(D_LAYOUT, "new component end is not aligned "
6486 "by the stripe size %u: [%llu, %llu) ext size "
6487 "%llu new end %llu, aligning\n",
6488 stripe_size, component_end, extent_end,
6489 extension_size, new_end);
6490 new_end = roundup(new_end, stripe_size);
6494 if (new_end < extent_end)
6495 new_end = OBD_OBJECT_EOF;
6498 /* Don't extend past the end of the extension component */
6499 if (new_end > extension_end)
6500 new_end = extension_end;
6505 /* As lod_sel_handler() could be re-entered for the same component several
6506 * times, this is the data for the next call. Fields could be changed to
6507 * component indexes when needed, (e.g. if there is no need to instantiate
6508 * all the previous components up to the current position) to tell the caller
6509 * where to start over from. */
6516 * Process extent updates for a particular layout component
6518 * Handle layout updates for a particular extension space component touched by
6519 * a layout update operation. Core function of self-extending PFL feature.
6521 * In general, this function processes exactly *one* stage of an extension
6522 * operation, modifying the layout accordingly, then returns to the caller.
6523 * The caller is responsible for restarting processing with the new layout,
6524 * which may repeatedly return to this function until the extension updates
6527 * This function does one of a few things to the layout:
6528 * 1. Extends the component before the current extension space component to
6529 * allow it to accomodate the requested operation (if space/policy permit that
6530 * component to continue on its current OSTs)
6532 * 2. If extension of the existing component fails, we do one of two things:
6533 * a. If there is a component after the extension space, we remove the
6534 * extension space component, move the start of the next component down
6535 * accordingly, then notify the caller to restart processing w/the new
6537 * b. If there is no following component, we try repeating the current
6538 * component, creating a new component using the current one as a
6539 * template (keeping its stripe properties but not specific striping),
6540 * and try assigning striping for this component. If there is sufficient
6541 * free space on the OSTs chosen for this component, it is instantiated
6542 * and i/o continues there.
6544 * If there is not sufficient space on the new OSTs, we remove this new
6545 * component & extend the current component.
6547 * Note further that uninited components followed by extension space can be zero
6548 * length meaning that we will try to extend them before initializing them, and
6549 * if that fails, they will be removed without initialization.
6551 * 3. If we extend to/beyond the end of an extension space component, that
6552 * component is exhausted (all of its range has been given to real components),
6553 * so we remove it and restart processing.
6555 * \param[in] env execution environment for this thread
6556 * \param[in,out] lo object to update the layout of
6557 * \param[in] extent layout extent for requested operation, update
6558 * layout to fit this operation
6559 * \param[in] th transaction handle for this operation
6560 * \param[in,out] max_comp the highest comp for the portion of the layout
6561 * we are operating on (For FLR, the chosen
6562 * replica). Updated because we may remove
6564 * \param[in] index index of the extension space component we're
6566 * \param[in] write if this is write op
6567 * \param[in,out] force if the extension is to be forced; set here
6568 to force it on the 2nd call for the same
6571 * \retval 0 on success
6572 * \retval negative errno on error
6574 static int lod_sel_handler(const struct lu_env *env,
6575 struct lod_object *lo,
6576 struct lu_extent *extent,
6577 struct thandle *th, int *max_comp,
6578 int index, int write,
6579 struct sel_data *sd)
6581 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6582 struct lod_thread_info *info = lod_env_info(env);
6583 struct lod_layout_component *lod_comp;
6584 struct lod_layout_component *prev;
6585 struct lod_layout_component *next = NULL;
6586 __u64 extension_size;
6593 /* First component cannot be extension space */
6595 CERROR("%s: "DFID" first component cannot be extension space\n",
6596 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6600 lod_comp = &lo->ldo_comp_entries[index];
6601 prev = &lo->ldo_comp_entries[index - 1];
6602 if ((index + 1) < *max_comp)
6603 next = &lo->ldo_comp_entries[index + 1];
6605 /* extension size uses the stripe size field as KiB */
6606 extension_size = lod_comp->llc_stripe_size * SEL_UNIT_SIZE;
6608 CDEBUG(D_LAYOUT, "prev start %llu, extension start %llu, extension end"
6609 " %llu, extension size %llu\n", prev->llc_extent.e_start,
6610 lod_comp->llc_extent.e_start, lod_comp->llc_extent.e_end,
6613 /* Two extension space components cannot be adjacent & extension space
6614 * components cannot be init */
6615 if ((prev->llc_flags & LCME_FL_EXTENSION) ||
6616 !(ergo(next, !(next->llc_flags & LCME_FL_EXTENSION))) ||
6617 lod_comp_inited(lod_comp)) {
6618 CERROR("%s: "DFID" invalid extension space components\n",
6619 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6623 if (!prev->llc_stripe) {
6624 CDEBUG(D_LAYOUT, "Previous component not inited\n");
6625 info->lti_count = 1;
6626 info->lti_comp_idx[0] = index - 1;
6627 rc = lod_declare_instantiate_components(env, lo, th);
6628 /* ENOSPC tells us we can't use this component. If there is
6629 * a next or we are repeating, we either spill over (next) or
6630 * extend the original comp (repeat). Otherwise, return the
6631 * error to the user. */
6632 if (rc == -ENOSPC && (next || sd->sd_repeat))
6638 if (sd->sd_force == 0 && rc == 0)
6639 rc = !lod_sel_osts_allowed(env, lo, index - 1,
6640 extension_size, extent,
6641 &lod_comp->llc_extent, write);
6643 repeated = !!(sd->sd_repeat);
6647 /* Extend previous component */
6649 new_end = lod_extension_new_end(extension_size, extent->e_end,
6650 prev->llc_stripe_size,
6651 prev->llc_extent.e_end,
6652 lod_comp->llc_extent.e_end);
6654 CDEBUG(D_LAYOUT, "new end %llu\n", new_end);
6655 lod_comp->llc_extent.e_start = new_end;
6656 prev->llc_extent.e_end = new_end;
6658 if (prev->llc_extent.e_end == lod_comp->llc_extent.e_end) {
6659 CDEBUG(D_LAYOUT, "Extension component exhausted\n");
6660 lod_comp->llc_id = LCME_ID_INVAL;
6664 /* rc == 1, failed to extend current component */
6667 /* Normal 'spillover' case - Remove the extension
6668 * space component & bring down the start of the next
6670 lod_comp->llc_id = LCME_ID_INVAL;
6672 if (!(prev->llc_flags & LCME_FL_INIT)) {
6673 prev->llc_id = LCME_ID_INVAL;
6676 lod_sel_adjust_extents(env, lo, *max_comp, index);
6677 } else if (lod_comp_inited(prev)) {
6678 /* If there is no next, and the previous component is
6679 * INIT'ed, try repeating the previous component. */
6680 LASSERT(repeated == 0);
6681 rc = lod_layout_repeat_comp(env, lo, index - 1);
6685 /* The previous component is a repeated component.
6686 * Record this so we don't keep trying to repeat it. */
6689 /* If the previous component is not INIT'ed, this may
6690 * be a component we have just instantiated but failed
6691 * to extend. Or even a repeated component we failed
6692 * to prepare a striping for. Do not repeat but instead
6693 * remove the repeated component & force the extention
6694 * of the original one */
6697 prev->llc_id = LCME_ID_INVAL;
6704 rc = lod_layout_del_prep_layout(env, lo, NULL);
6707 LASSERTF(-rc == change,
6708 "number deleted %d != requested %d\n", -rc,
6711 *max_comp = *max_comp + change;
6713 /* lod_del_prep_layout reallocates ldo_comp_entries, so we must
6714 * refresh these pointers before using them */
6715 lod_comp = &lo->ldo_comp_entries[index];
6716 prev = &lo->ldo_comp_entries[index - 1];
6717 CDEBUG(D_LAYOUT, "After extent updates: prev start %llu, current start "
6718 "%llu, current end %llu max_comp %d ldo_comp_cnt %d\n",
6719 prev->llc_extent.e_start, lod_comp->llc_extent.e_start,
6720 lod_comp->llc_extent.e_end, *max_comp, lo->ldo_comp_cnt);
6722 /* Layout changed successfully */
6727 * Declare layout extent updates
6729 * Handles extensions. Identifies extension components touched by current
6730 * operation and passes them to processing function.
6732 * Restarts with updated layouts from the processing function until the current
6733 * operation no longer touches an extension space component.
6735 * \param[in] env execution environment for this thread
6736 * \param[in,out] lo object to update the layout of
6737 * \param[in] extent layout extent for requested operation, update layout to
6738 * fit this operation
6739 * \param[in] th transaction handle for this operation
6740 * \param[in] pick identifies chosen mirror for FLR layouts
6741 * \param[in] write if this is write op
6743 * \retval 1 on layout changed, 0 on no change
6744 * \retval negative errno on error
6746 static int lod_declare_update_extents(const struct lu_env *env,
6747 struct lod_object *lo, struct lu_extent *extent,
6748 struct thandle *th, int pick, int write)
6750 struct lod_thread_info *info = lod_env_info(env);
6751 struct lod_layout_component *lod_comp;
6752 bool layout_changed = false;
6753 struct sel_data sd = { 0 };
6761 /* This makes us work on the components of the chosen mirror */
6762 start_index = lo->ldo_mirrors[pick].lme_start;
6763 max_comp = lo->ldo_mirrors[pick].lme_end + 1;
6764 if (lo->ldo_flr_state == LCM_FL_NONE)
6765 LASSERT(start_index == 0 && max_comp == lo->ldo_comp_cnt);
6767 CDEBUG(D_LAYOUT, "extent->e_start %llu, extent->e_end %llu\n",
6768 extent->e_start, extent->e_end);
6769 for (i = start_index; i < max_comp; i++) {
6770 lod_comp = &lo->ldo_comp_entries[i];
6772 /* We've passed all components of interest */
6773 if (lod_comp->llc_extent.e_start >= extent->e_end)
6776 if (lod_comp->llc_flags & LCME_FL_EXTENSION) {
6777 layout_changed = true;
6778 rc = lod_sel_handler(env, lo, extent, th, &max_comp,
6783 /* Nothing has changed behind the prev one */
6789 /* We may have added or removed components. If so, we must update the
6790 * start & ends of all the mirrors after the current one, and the end
6791 * of the current mirror. */
6792 change = max_comp - 1 - lo->ldo_mirrors[pick].lme_end;
6794 lo->ldo_mirrors[pick].lme_end += change;
6795 for (i = pick + 1; i < lo->ldo_mirror_count; i++) {
6796 lo->ldo_mirrors[i].lme_start += change;
6797 lo->ldo_mirrors[i].lme_end += change;
6803 /* The amount of components has changed, adjust the lti_comp_idx */
6804 rc2 = lod_layout_data_init(info, lo->ldo_comp_cnt);
6806 return rc < 0 ? rc : rc2 < 0 ? rc2 : layout_changed;
6809 /* If striping is already instantiated or INIT'ed DOM? */
6810 static bool lod_is_instantiation_needed(struct lod_layout_component *comp)
6812 return !(((lov_pattern(comp->llc_pattern) == LOV_PATTERN_MDT) &&
6813 lod_comp_inited(comp)) || comp->llc_stripe);
6817 * Declare layout update for a non-FLR layout.
6819 * \param[in] env execution environment for this thread
6820 * \param[in,out] lo object to update the layout of
6821 * \param[in] layout layout intent for requested operation, "update" is
6822 * a process of reacting to this
6823 * \param[in] buf buffer containing lov ea (see comment on usage inline)
6824 * \param[in] th transaction handle for this operation
6826 * \retval 0 on success
6827 * \retval negative errno on error
6829 static int lod_declare_update_plain(const struct lu_env *env,
6830 struct lod_object *lo, struct layout_intent *layout,
6831 const struct lu_buf *buf, struct thandle *th)
6833 struct lod_thread_info *info = lod_env_info(env);
6834 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6835 struct lod_layout_component *lod_comp;
6836 struct lov_comp_md_v1 *comp_v1 = NULL;
6837 bool layout_changed = false;
6838 bool replay = false;
6842 LASSERT(lo->ldo_flr_state == LCM_FL_NONE);
6845 * In case the client is passing lovea, which only happens during
6846 * the replay of layout intent write RPC for now, we may need to
6847 * parse the lovea and apply new layout configuration.
6849 if (buf && buf->lb_len) {
6850 struct lov_user_md_v1 *v1 = buf->lb_buf;
6852 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
6853 v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
6854 LOV_MAGIC_COMP_V1)) {
6855 CERROR("%s: the replay buffer of layout extend "
6856 "(magic %#x) does not contain expected "
6857 "composite layout.\n",
6858 lod2obd(d)->obd_name, v1->lmm_magic);
6859 GOTO(out, rc = -EINVAL);
6862 rc = lod_use_defined_striping(env, lo, buf);
6865 lo->ldo_comp_cached = 1;
6867 rc = lod_get_lov_ea(env, lo);
6870 /* old on-disk EA is stored in info->lti_buf */
6871 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
6873 layout_changed = true;
6875 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
6879 /* non replay path */
6880 rc = lod_striping_load(env, lo);
6885 /* Make sure defined layout covers the requested write range. */
6886 lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
6887 if (lo->ldo_comp_cnt > 1 &&
6888 lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
6889 lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
6890 CDEBUG_LIMIT(replay ? D_ERROR : D_LAYOUT,
6891 "%s: the defined layout [0, %#llx) does not "
6892 "covers the write range "DEXT"\n",
6893 lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
6894 PEXT(&layout->li_extent));
6895 GOTO(out, rc = -EINVAL);
6898 CDEBUG(D_LAYOUT, "%s: "DFID": update components "DEXT"\n",
6899 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
6900 PEXT(&layout->li_extent));
6903 rc = lod_declare_update_extents(env, lo, &layout->li_extent,
6904 th, 0, layout->li_opc == LAYOUT_INTENT_WRITE);
6908 layout_changed = true;
6912 * Iterate ld->ldo_comp_entries, find the component whose extent under
6913 * the write range and not instantianted.
6915 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6916 lod_comp = &lo->ldo_comp_entries[i];
6918 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
6922 /* If striping is instantiated or INIT'ed DOM skip */
6923 if (!lod_is_instantiation_needed(lod_comp))
6927 * In replay path, lod_comp is the EA passed by
6928 * client replay buffer, comp_v1 is the pre-recovery
6929 * on-disk EA, we'd sift out those components which
6930 * were init-ed in the on-disk EA.
6932 if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
6937 * this component hasn't instantiated in normal path, or during
6938 * replay it needs replay the instantiation.
6941 /* A released component is being extended */
6942 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
6943 GOTO(out, rc = -EINVAL);
6945 LASSERT(info->lti_comp_idx != NULL);
6946 info->lti_comp_idx[info->lti_count++] = i;
6947 layout_changed = true;
6950 if (!layout_changed)
6953 lod_obj_inc_layout_gen(lo);
6954 rc = lod_declare_instantiate_components(env, lo, th);
6958 lod_striping_free(env, lo);
6962 static inline int lod_comp_index(struct lod_object *lo,
6963 struct lod_layout_component *lod_comp)
6965 LASSERT(lod_comp >= lo->ldo_comp_entries &&
6966 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
6968 return lod_comp - lo->ldo_comp_entries;
6972 * Stale other mirrors by writing extent.
6974 static int lod_stale_components(const struct lu_env *env, struct lod_object *lo,
6975 int primary, struct lu_extent *extent,
6978 struct lod_layout_component *pri_comp, *lod_comp;
6979 struct lu_extent pri_extent;
6984 /* The writing extent decides which components in the primary
6985 * are affected... */
6986 CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
6989 lod_foreach_mirror_comp(pri_comp, lo, primary) {
6990 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
6993 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
6994 lod_comp_index(lo, pri_comp),
6995 PEXT(&pri_comp->llc_extent));
6997 pri_extent.e_start = pri_comp->llc_extent.e_start;
6998 pri_extent.e_end = pri_comp->llc_extent.e_end;
7000 for (i = 0; i < lo->ldo_mirror_count; i++) {
7003 rc = lod_declare_update_extents(env, lo, &pri_extent,
7005 /* if update_extents changed the layout, it may have
7006 * reallocated the component array, so start over to
7007 * avoid using stale pointers */
7013 /* ... and then stale other components that are
7014 * overlapping with primary components */
7015 lod_foreach_mirror_comp(lod_comp, lo, i) {
7016 if (!lu_extent_is_overlapped(
7018 &lod_comp->llc_extent))
7021 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
7022 i, lod_comp_index(lo, lod_comp));
7024 lod_comp->llc_flags |= LCME_FL_STALE;
7025 lo->ldo_mirrors[i].lme_stale = 1;
7034 * check an OST's availability
7035 * \param[in] env execution environment
7036 * \param[in] lo lod object
7037 * \param[in] dt dt object
7038 * \param[in] index mirror index
7040 * \retval negative if failed
7041 * \retval 1 if \a dt is available
7042 * \retval 0 if \a dt is not available
7044 static inline int lod_check_ost_avail(const struct lu_env *env,
7045 struct lod_object *lo,
7046 struct dt_object *dt, int index)
7048 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7049 struct lod_tgt_desc *ost;
7051 int type = LU_SEQ_RANGE_OST;
7054 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &idx, &type);
7056 CERROR("%s: can't locate "DFID":rc = %d\n",
7057 lod2obd(lod)->obd_name, PFID(lu_object_fid(&dt->do_lu)),
7062 ost = OST_TGT(lod, idx);
7063 if (ost->ltd_statfs.os_state &
7064 (OS_STATE_READONLY | OS_STATE_ENOSPC | OS_STATE_ENOINO |
7065 OS_STATE_NOPRECREATE) ||
7066 ost->ltd_active == 0) {
7067 CDEBUG(D_LAYOUT, DFID ": mirror %d OST%d unavail, rc = %d\n",
7068 PFID(lod_object_fid(lo)), index, idx, rc);
7076 * Pick primary mirror for write
7077 * \param[in] env execution environment
7078 * \param[in] lo object
7079 * \param[in] extent write range
7081 static int lod_primary_pick(const struct lu_env *env, struct lod_object *lo,
7082 struct lu_extent *extent)
7084 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7085 unsigned int seq = 0;
7086 struct lod_layout_component *lod_comp;
7088 int picked = -1, second_pick = -1, third_pick = -1;
7091 if (OBD_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
7092 get_random_bytes(&seq, sizeof(seq));
7093 seq %= lo->ldo_mirror_count;
7097 * Pick a mirror as the primary, and check the availability of OSTs.
7099 * This algo can be revised later after knowing the topology of
7102 lod_qos_statfs_update(env, lod);
7103 for (i = 0; i < lo->ldo_mirror_count; i++) {
7104 bool ost_avail = true;
7105 int index = (i + seq) % lo->ldo_mirror_count;
7107 if (lo->ldo_mirrors[index].lme_stale) {
7108 CDEBUG(D_LAYOUT, DFID": mirror %d stale\n",
7109 PFID(lod_object_fid(lo)), index);
7113 /* 2nd pick is for the primary mirror containing unavail OST */
7114 if (lo->ldo_mirrors[index].lme_primary && second_pick < 0)
7115 second_pick = index;
7117 /* 3rd pick is for non-primary mirror containing unavail OST */
7118 if (second_pick < 0 && third_pick < 0)
7122 * we found a non-primary 1st pick, we'd like to find a
7123 * potential pirmary mirror.
7125 if (picked >= 0 && !lo->ldo_mirrors[index].lme_primary)
7128 /* check the availability of OSTs */
7129 lod_foreach_mirror_comp(lod_comp, lo, index) {
7130 if (!lod_comp_inited(lod_comp) || !lod_comp->llc_stripe)
7133 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
7134 struct dt_object *dt = lod_comp->llc_stripe[j];
7136 rc = lod_check_ost_avail(env, lo, dt, index);
7143 } /* for all dt object in one component */
7146 } /* for all components in a mirror */
7149 * the OSTs where allocated objects locates in the components
7150 * of the mirror are available.
7155 /* this mirror has all OSTs available */
7159 * primary with all OSTs are available, this is the perfect
7162 if (lo->ldo_mirrors[index].lme_primary)
7164 } /* for all mirrors */
7166 /* failed to pick a sound mirror, lower our expectation */
7168 picked = second_pick;
7170 picked = third_pick;
7177 static int lod_prepare_resync_mirror(const struct lu_env *env,
7178 struct lod_object *lo,
7181 struct lod_thread_info *info = lod_env_info(env);
7182 struct lod_layout_component *lod_comp;
7183 bool neg = !!(MIRROR_ID_NEG & mirror_id);
7186 mirror_id &= ~MIRROR_ID_NEG;
7188 for (i = 0; i < lo->ldo_mirror_count; i++) {
7189 if ((!neg && lo->ldo_mirrors[i].lme_id != mirror_id) ||
7190 (neg && lo->ldo_mirrors[i].lme_id == mirror_id))
7193 lod_foreach_mirror_comp(lod_comp, lo, i) {
7194 if (lod_comp_inited(lod_comp))
7197 info->lti_comp_idx[info->lti_count++] =
7198 lod_comp_index(lo, lod_comp);
7206 * figure out the components should be instantiated for resync.
7208 static int lod_prepare_resync(const struct lu_env *env, struct lod_object *lo,
7209 struct lu_extent *extent)
7211 struct lod_thread_info *info = lod_env_info(env);
7212 struct lod_layout_component *lod_comp;
7213 unsigned int need_sync = 0;
7217 DFID": instantiate all stale components in "DEXT"\n",
7218 PFID(lod_object_fid(lo)), PEXT(extent));
7221 * instantiate all components within this extent, even non-stale
7224 for (i = 0; i < lo->ldo_mirror_count; i++) {
7225 if (!lo->ldo_mirrors[i].lme_stale)
7228 lod_foreach_mirror_comp(lod_comp, lo, i) {
7229 if (!lu_extent_is_overlapped(extent,
7230 &lod_comp->llc_extent))
7235 if (lod_comp_inited(lod_comp))
7238 CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
7239 i, lod_comp_index(lo, lod_comp));
7240 info->lti_comp_idx[info->lti_count++] =
7241 lod_comp_index(lo, lod_comp);
7245 return need_sync ? 0 : -EALREADY;
7248 static int lod_declare_update_rdonly(const struct lu_env *env,
7249 struct lod_object *lo, struct md_layout_change *mlc,
7252 struct lod_thread_info *info = lod_env_info(env);
7253 struct lu_attr *layout_attr = &info->lti_layout_attr;
7254 struct lod_layout_component *lod_comp;
7255 struct lu_extent extent = { 0 };
7259 LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
7260 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7261 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7262 LASSERT(lo->ldo_mirror_count > 0);
7264 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7265 struct layout_intent *layout = mlc->mlc_intent;
7266 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7269 extent = layout->li_extent;
7270 CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
7271 PFID(lod_object_fid(lo)), PEXT(&extent));
7273 picked = lod_primary_pick(env, lo, &extent);
7277 CDEBUG(D_LAYOUT, DFID": picked mirror id %u as primary\n",
7278 PFID(lod_object_fid(lo)),
7279 lo->ldo_mirrors[picked].lme_id);
7281 /* Update extents of primary before staling */
7282 rc = lod_declare_update_extents(env, lo, &extent, th, picked,
7287 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
7289 * trunc transfers [0, size) in the intent extent, we'd
7290 * stale components overlapping [size, eof).
7292 extent.e_start = extent.e_end;
7293 extent.e_end = OBD_OBJECT_EOF;
7296 /* stale overlapping components from other mirrors */
7297 rc = lod_stale_components(env, lo, picked, &extent, th);
7301 /* restore truncate intent extent */
7302 if (layout->li_opc == LAYOUT_INTENT_TRUNC)
7303 extent.e_end = extent.e_start;
7305 /* instantiate components for the picked mirror, start from 0 */
7308 lod_foreach_mirror_comp(lod_comp, lo, picked) {
7309 if (!lu_extent_is_overlapped(&extent,
7310 &lod_comp->llc_extent))
7313 if (!lod_is_instantiation_needed(lod_comp))
7316 info->lti_comp_idx[info->lti_count++] =
7317 lod_comp_index(lo, lod_comp);
7320 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7321 } else { /* MD_LAYOUT_RESYNC */
7325 * could contain multiple non-stale mirrors, so we need to
7326 * prep uninited all components assuming any non-stale mirror
7327 * could be picked as the primary mirror.
7329 if (mlc->mlc_mirror_id == 0) {
7331 for (i = 0; i < lo->ldo_mirror_count; i++) {
7332 if (lo->ldo_mirrors[i].lme_stale)
7335 lod_foreach_mirror_comp(lod_comp, lo, i) {
7336 if (!lod_comp_inited(lod_comp))
7340 lod_comp->llc_extent.e_end)
7342 lod_comp->llc_extent.e_end;
7345 rc = lod_prepare_resync(env, lo, &extent);
7349 /* mirror write, try to init its all components */
7350 rc = lod_prepare_resync_mirror(env, lo,
7351 mlc->mlc_mirror_id);
7356 /* change the file state to SYNC_PENDING */
7357 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7360 /* Reset the layout version once it's becoming too large.
7361 * This way it can make sure that the layout version is
7362 * monotonously increased in this writing era. */
7363 lod_obj_inc_layout_gen(lo);
7364 if (lo->ldo_layout_gen > (LCME_ID_MAX >> 1)) {
7365 __u32 layout_version;
7367 get_random_bytes(&layout_version, sizeof(layout_version));
7368 lo->ldo_layout_gen = layout_version & 0xffff;
7371 rc = lod_declare_instantiate_components(env, lo, th);
7375 layout_attr->la_valid = LA_LAYOUT_VERSION;
7376 layout_attr->la_layout_version = 0; /* set current version */
7377 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7378 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7379 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7385 lod_striping_free(env, lo);
7389 static int lod_declare_update_write_pending(const struct lu_env *env,
7390 struct lod_object *lo, struct md_layout_change *mlc,
7393 struct lod_thread_info *info = lod_env_info(env);
7394 struct lu_attr *layout_attr = &info->lti_layout_attr;
7395 struct lod_layout_component *lod_comp;
7396 struct lu_extent extent = { 0 };
7402 LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
7403 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7404 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7406 /* look for the primary mirror */
7407 for (i = 0; i < lo->ldo_mirror_count; i++) {
7408 if (lo->ldo_mirrors[i].lme_stale)
7411 LASSERTF(primary < 0, DFID " has multiple primary: %u / %u",
7412 PFID(lod_object_fid(lo)),
7413 lo->ldo_mirrors[i].lme_id,
7414 lo->ldo_mirrors[primary].lme_id);
7419 CERROR(DFID ": doesn't have a primary mirror\n",
7420 PFID(lod_object_fid(lo)));
7421 GOTO(out, rc = -ENODATA);
7424 CDEBUG(D_LAYOUT, DFID": found primary %u\n",
7425 PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
7427 LASSERT(!lo->ldo_mirrors[primary].lme_stale);
7429 /* for LAYOUT_WRITE opc, it has to do the following operations:
7430 * 1. stale overlapping componets from stale mirrors;
7431 * 2. instantiate components of the primary mirror;
7432 * 3. transfter layout version to all objects of the primary;
7434 * for LAYOUT_RESYNC opc, it will do:
7435 * 1. instantiate components of all stale mirrors;
7436 * 2. transfer layout version to all objects to close write era. */
7438 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7439 struct layout_intent *layout = mlc->mlc_intent;
7440 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7442 LASSERT(mlc->mlc_intent != NULL);
7444 extent = mlc->mlc_intent->li_extent;
7446 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
7447 PFID(lod_object_fid(lo)), PEXT(&extent));
7449 /* 1. Update extents of primary before staling */
7450 rc = lod_declare_update_extents(env, lo, &extent, th, primary,
7455 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
7457 * trunc transfers [0, size) in the intent extent, we'd
7458 * stale components overlapping [size, eof).
7460 extent.e_start = extent.e_end;
7461 extent.e_end = OBD_OBJECT_EOF;
7464 /* 2. stale overlapping components */
7465 rc = lod_stale_components(env, lo, primary, &extent, th);
7469 /* 3. find the components which need instantiating.
7470 * instantiate [0, mlc->mlc_intent->e_end) */
7472 /* restore truncate intent extent */
7473 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC)
7474 extent.e_end = extent.e_start;
7477 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7478 if (!lu_extent_is_overlapped(&extent,
7479 &lod_comp->llc_extent))
7482 if (!lod_is_instantiation_needed(lod_comp))
7485 CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
7486 primary, lod_comp_index(lo, lod_comp));
7487 info->lti_comp_idx[info->lti_count++] =
7488 lod_comp_index(lo, lod_comp);
7490 } else { /* MD_LAYOUT_RESYNC */
7491 if (mlc->mlc_mirror_id == 0) {
7493 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7494 if (!lod_comp_inited(lod_comp))
7497 extent.e_end = lod_comp->llc_extent.e_end;
7500 rc = lod_prepare_resync(env, lo, &extent);
7504 /* mirror write, try to init its all components */
7505 rc = lod_prepare_resync_mirror(env, lo,
7506 mlc->mlc_mirror_id);
7511 /* change the file state to SYNC_PENDING */
7512 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7515 rc = lod_declare_instantiate_components(env, lo, th);
7519 /* 3. transfer layout version to OST objects.
7520 * transfer new layout version to OST objects so that stale writes
7521 * can be denied. It also ends an era of writing by setting
7522 * LU_LAYOUT_RESYNC. Normal client can never use this bit to
7523 * send write RPC; only resync RPCs could do it. */
7524 layout_attr->la_valid = LA_LAYOUT_VERSION;
7525 layout_attr->la_layout_version = 0; /* set current version */
7526 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7527 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7528 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7532 lod_obj_inc_layout_gen(lo);
7535 lod_striping_free(env, lo);
7539 static int lod_declare_update_sync_pending(const struct lu_env *env,
7540 struct lod_object *lo, struct md_layout_change *mlc,
7543 struct lod_thread_info *info = lod_env_info(env);
7544 unsigned sync_components = 0;
7545 unsigned resync_components = 0;
7550 LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
7551 LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
7552 mlc->mlc_opc == MD_LAYOUT_WRITE);
7554 CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
7555 PFID(lod_object_fid(lo)), mlc->mlc_opc);
7557 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7558 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
7559 PFID(lod_object_fid(lo)));
7561 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7562 return lod_declare_update_write_pending(env, lo, mlc, th);
7565 /* MD_LAYOUT_RESYNC_DONE */
7567 for (i = 0; i < lo->ldo_comp_cnt; i++) {
7568 struct lod_layout_component *lod_comp;
7571 lod_comp = &lo->ldo_comp_entries[i];
7573 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
7578 for (j = 0; j < mlc->mlc_resync_count; j++) {
7579 if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
7582 mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
7583 lod_comp->llc_flags &= ~LCME_FL_STALE;
7584 resync_components++;
7590 for (i = 0; i < mlc->mlc_resync_count; i++) {
7591 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
7594 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
7595 "or already synced\n", PFID(lod_object_fid(lo)),
7596 mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
7597 GOTO(out, rc = -EINVAL);
7600 if (!sync_components || (mlc->mlc_resync_count && !resync_components)) {
7601 CDEBUG(D_LAYOUT, DFID": no mirror in sync\n",
7602 PFID(lod_object_fid(lo)));
7604 /* tend to return an error code here to prevent
7605 * the MDT from setting SoM attribute */
7606 GOTO(out, rc = -EINVAL);
7609 CDEBUG(D_LAYOUT, DFID": synced %u resynced %u/%zu components\n",
7610 PFID(lod_object_fid(lo)),
7611 sync_components, resync_components, mlc->mlc_resync_count);
7613 lo->ldo_flr_state = LCM_FL_RDONLY;
7614 lod_obj_inc_layout_gen(lo);
7616 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
7617 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
7618 &info->lti_buf, XATTR_NAME_LOV, 0, th);
7623 lod_striping_free(env, lo);
7627 static int lod_declare_layout_change(const struct lu_env *env,
7628 struct dt_object *dt, struct md_layout_change *mlc,
7631 struct lod_thread_info *info = lod_env_info(env);
7632 struct lod_object *lo = lod_dt_obj(dt);
7636 if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
7637 dt_object_remote(dt_object_child(dt)))
7640 rc = lod_striping_load(env, lo);
7644 LASSERT(lo->ldo_comp_cnt > 0);
7646 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
7650 switch (lo->ldo_flr_state) {
7652 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
7656 rc = lod_declare_update_rdonly(env, lo, mlc, th);
7658 case LCM_FL_WRITE_PENDING:
7659 rc = lod_declare_update_write_pending(env, lo, mlc, th);
7661 case LCM_FL_SYNC_PENDING:
7662 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
7673 * Instantiate layout component objects which covers the intent write offset.
7675 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
7676 struct md_layout_change *mlc, struct thandle *th)
7678 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
7679 struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
7680 struct lod_object *lo = lod_dt_obj(dt);
7683 rc = lod_striped_create(env, dt, attr, NULL, th);
7684 if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
7685 layout_attr->la_layout_version |= lo->ldo_layout_gen;
7686 rc = lod_attr_set(env, dt, layout_attr, th);
7692 struct dt_object_operations lod_obj_ops = {
7693 .do_read_lock = lod_read_lock,
7694 .do_write_lock = lod_write_lock,
7695 .do_read_unlock = lod_read_unlock,
7696 .do_write_unlock = lod_write_unlock,
7697 .do_write_locked = lod_write_locked,
7698 .do_attr_get = lod_attr_get,
7699 .do_declare_attr_set = lod_declare_attr_set,
7700 .do_attr_set = lod_attr_set,
7701 .do_xattr_get = lod_xattr_get,
7702 .do_declare_xattr_set = lod_declare_xattr_set,
7703 .do_xattr_set = lod_xattr_set,
7704 .do_declare_xattr_del = lod_declare_xattr_del,
7705 .do_xattr_del = lod_xattr_del,
7706 .do_xattr_list = lod_xattr_list,
7707 .do_ah_init = lod_ah_init,
7708 .do_declare_create = lod_declare_create,
7709 .do_create = lod_create,
7710 .do_declare_destroy = lod_declare_destroy,
7711 .do_destroy = lod_destroy,
7712 .do_index_try = lod_index_try,
7713 .do_declare_ref_add = lod_declare_ref_add,
7714 .do_ref_add = lod_ref_add,
7715 .do_declare_ref_del = lod_declare_ref_del,
7716 .do_ref_del = lod_ref_del,
7717 .do_object_sync = lod_object_sync,
7718 .do_object_lock = lod_object_lock,
7719 .do_object_unlock = lod_object_unlock,
7720 .do_invalidate = lod_invalidate,
7721 .do_declare_layout_change = lod_declare_layout_change,
7722 .do_layout_change = lod_layout_change,
7726 * Implementation of dt_body_operations::dbo_read.
7728 * \see dt_body_operations::dbo_read() in the API description for details.
7730 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
7731 struct lu_buf *buf, loff_t *pos)
7733 struct dt_object *next = dt_object_child(dt);
7735 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
7736 S_ISLNK(dt->do_lu.lo_header->loh_attr));
7737 return next->do_body_ops->dbo_read(env, next, buf, pos);
7741 * Implementation of dt_body_operations::dbo_declare_write.
7743 * \see dt_body_operations::dbo_declare_write() in the API description
7746 static ssize_t lod_declare_write(const struct lu_env *env,
7747 struct dt_object *dt,
7748 const struct lu_buf *buf, loff_t pos,
7751 return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
7755 * Implementation of dt_body_operations::dbo_write.
7757 * \see dt_body_operations::dbo_write() in the API description for details.
7759 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
7760 const struct lu_buf *buf, loff_t *pos,
7763 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
7764 S_ISLNK(dt->do_lu.lo_header->loh_attr));
7765 return lod_sub_write(env, dt_object_child(dt), buf, pos, th);
7768 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
7769 __u64 start, __u64 end, struct thandle *th)
7771 if (dt_object_remote(dt))
7774 return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
7777 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
7778 __u64 start, __u64 end, struct thandle *th)
7780 if (dt_object_remote(dt))
7783 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
7784 return lod_sub_punch(env, dt_object_child(dt), start, end, th);
7788 * different type of files use the same body_ops because object may be created
7789 * in OUT, where there is no chance to set correct body_ops for each type, so
7790 * body_ops themselves will check file type inside, see lod_read/write/punch for
7793 const struct dt_body_operations lod_body_ops = {
7794 .dbo_read = lod_read,
7795 .dbo_declare_write = lod_declare_write,
7796 .dbo_write = lod_write,
7797 .dbo_declare_punch = lod_declare_punch,
7798 .dbo_punch = lod_punch,
7802 * Implementation of lu_object_operations::loo_object_init.
7804 * The function determines the type and the index of the target device using
7805 * sequence of the object's FID. Then passes control down to the
7806 * corresponding device:
7807 * OSD for the local objects, OSP for remote
7809 * \see lu_object_operations::loo_object_init() in the API description
7812 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
7813 const struct lu_object_conf *conf)
7815 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
7816 struct lu_device *cdev = NULL;
7817 struct lu_object *cobj;
7818 struct lod_tgt_descs *ltd = NULL;
7819 struct lod_tgt_desc *tgt;
7821 int type = LU_SEQ_RANGE_ANY;
7825 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
7829 if (type == LU_SEQ_RANGE_MDT &&
7830 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
7831 cdev = &lod->lod_child->dd_lu_dev;
7832 } else if (type == LU_SEQ_RANGE_MDT) {
7833 ltd = &lod->lod_mdt_descs;
7835 } else if (type == LU_SEQ_RANGE_OST) {
7836 ltd = &lod->lod_ost_descs;
7843 if (ltd->ltd_tgts_size > idx &&
7844 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
7845 tgt = LTD_TGT(ltd, idx);
7847 LASSERT(tgt != NULL);
7848 LASSERT(tgt->ltd_tgt != NULL);
7850 cdev = &(tgt->ltd_tgt->dd_lu_dev);
7852 lod_putref(lod, ltd);
7855 if (unlikely(cdev == NULL))
7858 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
7859 if (unlikely(cobj == NULL))
7862 lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
7864 lu_object_add(lo, cobj);
7871 * Alloc cached foreign LOV
7873 * \param[in] lo object
7874 * \param[in] size size of foreign LOV
7876 * \retval 0 on success
7877 * \retval negative if failed
7879 int lod_alloc_foreign_lov(struct lod_object *lo, size_t size)
7881 OBD_ALLOC_LARGE(lo->ldo_foreign_lov, size);
7882 if (lo->ldo_foreign_lov == NULL)
7884 lo->ldo_foreign_lov_size = size;
7885 lo->ldo_is_foreign = 1;
7891 * Free cached foreign LOV
7893 * \param[in] lo object
7895 void lod_free_foreign_lov(struct lod_object *lo)
7897 if (lo->ldo_foreign_lov != NULL)
7898 OBD_FREE_LARGE(lo->ldo_foreign_lov, lo->ldo_foreign_lov_size);
7899 lo->ldo_foreign_lov = NULL;
7900 lo->ldo_foreign_lov_size = 0;
7901 lo->ldo_is_foreign = 0;
7906 * Free cached foreign LMV
7908 * \param[in] lo object
7910 void lod_free_foreign_lmv(struct lod_object *lo)
7912 if (lo->ldo_foreign_lmv != NULL)
7913 OBD_FREE_LARGE(lo->ldo_foreign_lmv, lo->ldo_foreign_lmv_size);
7914 lo->ldo_foreign_lmv = NULL;
7915 lo->ldo_foreign_lmv_size = 0;
7916 lo->ldo_dir_is_foreign = 0;
7921 * Release resources associated with striping.
7923 * If the object is striped (regular or directory), then release
7924 * the stripe objects references and free the ldo_stripe array.
7926 * \param[in] env execution environment
7927 * \param[in] lo object
7929 void lod_striping_free_nolock(const struct lu_env *env, struct lod_object *lo)
7931 struct lod_layout_component *lod_comp;
7934 if (unlikely(lo->ldo_is_foreign)) {
7935 lod_free_foreign_lov(lo);
7936 lo->ldo_comp_cached = 0;
7937 } else if (unlikely(lo->ldo_dir_is_foreign)) {
7938 lod_free_foreign_lmv(lo);
7939 lo->ldo_dir_stripe_loaded = 0;
7940 } else if (lo->ldo_stripe != NULL) {
7941 LASSERT(lo->ldo_comp_entries == NULL);
7942 LASSERT(lo->ldo_dir_stripes_allocated > 0);
7944 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
7945 if (lo->ldo_stripe[i])
7946 dt_object_put(env, lo->ldo_stripe[i]);
7949 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
7950 OBD_FREE(lo->ldo_stripe, j);
7951 lo->ldo_stripe = NULL;
7952 lo->ldo_dir_stripes_allocated = 0;
7953 lo->ldo_dir_stripe_loaded = 0;
7954 lo->ldo_dir_stripe_count = 0;
7955 } else if (lo->ldo_comp_entries != NULL) {
7956 for (i = 0; i < lo->ldo_comp_cnt; i++) {
7957 /* free lod_layout_component::llc_stripe array */
7958 lod_comp = &lo->ldo_comp_entries[i];
7960 if (lod_comp->llc_stripe == NULL)
7962 LASSERT(lod_comp->llc_stripes_allocated != 0);
7963 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
7964 if (lod_comp->llc_stripe[j] != NULL)
7966 &lod_comp->llc_stripe[j]->do_lu);
7968 OBD_FREE(lod_comp->llc_stripe,
7969 sizeof(struct dt_object *) *
7970 lod_comp->llc_stripes_allocated);
7971 lod_comp->llc_stripe = NULL;
7972 OBD_FREE(lod_comp->llc_ost_indices,
7974 lod_comp->llc_stripes_allocated);
7975 lod_comp->llc_ost_indices = NULL;
7976 lod_comp->llc_stripes_allocated = 0;
7978 lod_free_comp_entries(lo);
7979 lo->ldo_comp_cached = 0;
7983 void lod_striping_free(const struct lu_env *env, struct lod_object *lo)
7985 mutex_lock(&lo->ldo_layout_mutex);
7986 lod_striping_free_nolock(env, lo);
7987 mutex_unlock(&lo->ldo_layout_mutex);
7991 * Implementation of lu_object_operations::loo_object_free.
7993 * \see lu_object_operations::loo_object_free() in the API description
7996 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
7998 struct lod_object *lo = lu2lod_obj(o);
8000 /* release all underlying object pinned */
8001 lod_striping_free(env, lo);
8003 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
8007 * Implementation of lu_object_operations::loo_object_release.
8009 * \see lu_object_operations::loo_object_release() in the API description
8012 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
8014 /* XXX: shouldn't we release everything here in case if object
8015 * creation failed before? */
8019 * Implementation of lu_object_operations::loo_object_print.
8021 * \see lu_object_operations::loo_object_print() in the API description
8024 static int lod_object_print(const struct lu_env *env, void *cookie,
8025 lu_printer_t p, const struct lu_object *l)
8027 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
8029 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
8032 struct lu_object_operations lod_lu_obj_ops = {
8033 .loo_object_init = lod_object_init,
8034 .loo_object_free = lod_object_free,
8035 .loo_object_release = lod_object_release,
8036 .loo_object_print = lod_object_print,