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, NULL);
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, MOR_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 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
4310 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4311 lod_comp = &lo->ldo_comp_entries[i];
4313 if (lod_comp->llc_id != LCME_ID_INVAL) {
4314 /* Build array of things to keep */
4315 info->lti_comp_idx[info->lti_count++] = i;
4319 lod_obj_set_pool(lo, i, NULL);
4320 if (lod_comp->llc_ostlist.op_array) {
4321 OBD_FREE(lod_comp->llc_ostlist.op_array,
4322 lod_comp->llc_ostlist.op_size);
4323 lod_comp->llc_ostlist.op_array = NULL;
4324 lod_comp->llc_ostlist.op_size = 0;
4328 CDEBUG(D_LAYOUT, "deleting comp %d, left %d\n", i,
4329 lo->ldo_comp_cnt - deleted);
4331 /* No striping info for this component */
4332 if (lod_comp->llc_stripe == NULL)
4335 LASSERT(lod_comp->llc_stripe_count > 0);
4336 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4337 struct dt_object *obj = lod_comp->llc_stripe[j];
4342 /* components which are not init have no sub objects
4344 if (lod_comp_inited(lod_comp)) {
4345 rc = lod_sub_destroy(env, obj, th);
4350 lu_object_put(env, &obj->do_lu);
4351 lod_comp->llc_stripe[j] = NULL;
4353 OBD_FREE(lod_comp->llc_stripe, sizeof(struct dt_object *) *
4354 lod_comp->llc_stripes_allocated);
4355 lod_comp->llc_stripe = NULL;
4356 OBD_FREE(lod_comp->llc_ost_indices,
4357 sizeof(__u32) * lod_comp->llc_stripes_allocated);
4358 lod_comp->llc_ost_indices = NULL;
4359 lod_comp->llc_stripes_allocated = 0;
4362 /* info->lti_count has the amount of left components */
4363 LASSERTF(info->lti_count >= 0 && info->lti_count < lo->ldo_comp_cnt,
4364 "left = %d, lo->ldo_comp_cnt %d\n", (int)info->lti_count,
4365 (int)lo->ldo_comp_cnt);
4367 if (info->lti_count > 0) {
4368 struct lod_layout_component *comp_array;
4370 OBD_ALLOC(comp_array, sizeof(*comp_array) * info->lti_count);
4371 if (comp_array == NULL)
4372 GOTO(out, rc = -ENOMEM);
4374 for (i = 0; i < info->lti_count; i++) {
4375 memcpy(&comp_array[i],
4376 &lo->ldo_comp_entries[info->lti_comp_idx[i]],
4377 sizeof(*comp_array));
4380 OBD_FREE(lo->ldo_comp_entries,
4381 sizeof(*comp_array) * lo->ldo_comp_cnt);
4382 lo->ldo_comp_entries = comp_array;
4383 lo->ldo_comp_cnt = info->lti_count;
4385 lod_free_comp_entries(lo);
4390 return rc ? rc : deleted;
4394 * Delete layout component(s)
4396 * This function sets up the layout data in the env and does the setattrs
4397 * required to write out the new layout. The layout itself is modified in
4398 * lod_layout_del_prep_layout.
4400 * \param[in] env execution environment for this thread
4401 * \param[in] dt object
4402 * \param[in] th transaction handle
4404 * \retval 0 on success
4405 * \retval negative error number on failure
4407 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
4410 struct lod_object *lo = lod_dt_obj(dt);
4411 struct dt_object *next = dt_object_child(dt);
4412 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4415 LASSERT(lo->ldo_is_composite);
4416 LASSERT(lo->ldo_mirror_count == 1);
4417 LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
4419 rc = lod_layout_del_prep_layout(env, lo, th);
4423 /* Only do this if we didn't delete all components */
4424 if (lo->ldo_comp_cnt > 0) {
4425 lo->ldo_mirrors[0].lme_end = lo->ldo_comp_cnt - 1;
4426 lod_obj_inc_layout_gen(lo);
4429 LASSERT(dt_object_exists(dt));
4430 rc = dt_attr_get(env, next, attr);
4434 if (attr->la_size > 0) {
4436 attr->la_valid = LA_SIZE;
4437 rc = lod_sub_attr_set(env, next, attr, th);
4442 rc = lod_generate_and_set_lovea(env, lo, th);
4446 lod_striping_free(env, lo);
4451 static int lod_get_default_lov_striping(const struct lu_env *env,
4452 struct lod_object *lo,
4453 struct lod_default_striping *lds);
4455 * Implementation of dt_object_operations::do_xattr_set.
4457 * Sets specified extended attribute on the object. Three types of EAs are
4459 * LOV EA - stores striping for a regular file or default striping (when set
4461 * LMV EA - stores a marker for the striped directories
4462 * DMV EA - stores default directory striping
4464 * When striping is applied to a non-striped existing object (this is called
4465 * late striping), then LOD notices the caller wants to turn the object into a
4466 * striped one. The stripe objects are created and appropriate EA is set:
4467 * LOV EA storing all the stripes directly or LMV EA storing just a small header
4468 * with striping configuration.
4470 * \see dt_object_operations::do_xattr_set() in the API description for details.
4472 static int lod_xattr_set(const struct lu_env *env,
4473 struct dt_object *dt, const struct lu_buf *buf,
4474 const char *name, int fl, struct thandle *th)
4476 struct dt_object *next = dt_object_child(dt);
4480 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4481 strcmp(name, XATTR_NAME_LMV) == 0) {
4482 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
4484 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4485 strncmp(name, XATTR_NAME_LMV, strlen(XATTR_NAME_LMV)) == 0 &&
4486 strlen(name) > strlen(XATTR_NAME_LMV) + 1) {
4487 const char *op = name + strlen(XATTR_NAME_LMV) + 1;
4491 * XATTR_NAME_LMV".add" is never called, but only declared,
4492 * because lod_xattr_set_lmv() will do the addition.
4494 if (strcmp(op, "del") == 0)
4495 rc = lod_dir_layout_delete(env, dt, buf, th);
4496 else if (strcmp(op, "set") == 0)
4497 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LMV,
4501 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4502 strcmp(name, XATTR_NAME_LOV) == 0) {
4503 struct lod_default_striping *lds = lod_lds_buf_get(env);
4504 struct lov_user_md_v1 *v1 = buf->lb_buf;
4505 char pool[LOV_MAXPOOLNAME + 1];
4508 /* get existing striping config */
4509 rc = lod_get_default_lov_striping(env, lod_dt_obj(dt), lds);
4513 memset(pool, 0, sizeof(pool));
4514 if (lds->lds_def_striping_set == 1)
4515 lod_layout_get_pool(lds->lds_def_comp_entries,
4516 lds->lds_def_comp_cnt, pool,
4519 is_del = LOVEA_DELETE_VALUES(v1->lmm_stripe_size,
4520 v1->lmm_stripe_count,
4521 v1->lmm_stripe_offset,
4524 /* Retain the pool name if it is not given */
4525 if (v1->lmm_magic == LOV_USER_MAGIC_V1 && pool[0] != '\0' &&
4527 struct lod_thread_info *info = lod_env_info(env);
4528 struct lov_user_md_v3 *v3 = info->lti_ea_store;
4530 memset(v3, 0, sizeof(*v3));
4531 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
4532 v3->lmm_pattern = cpu_to_le32(v1->lmm_pattern);
4533 v3->lmm_stripe_count =
4534 cpu_to_le32(v1->lmm_stripe_count);
4535 v3->lmm_stripe_offset =
4536 cpu_to_le32(v1->lmm_stripe_offset);
4537 v3->lmm_stripe_size = cpu_to_le32(v1->lmm_stripe_size);
4539 strlcpy(v3->lmm_pool_name, pool,
4540 sizeof(v3->lmm_pool_name));
4542 info->lti_buf.lb_buf = v3;
4543 info->lti_buf.lb_len = sizeof(*v3);
4544 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4547 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name,
4551 if (lds->lds_def_striping_set == 1 &&
4552 lds->lds_def_comp_entries != NULL)
4553 lod_free_def_comp_entries(lds);
4556 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4557 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
4559 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
4562 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4563 (!strcmp(name, XATTR_NAME_LOV) ||
4564 !strncmp(name, XATTR_LUSTRE_LOV,
4565 strlen(XATTR_LUSTRE_LOV)))) {
4566 /* in case of lov EA swap, just set it
4567 * if not, it is a replay so check striping match what we
4568 * already have during req replay, declare_xattr_set()
4569 * defines striping, then create() does the work */
4570 if (fl & LU_XATTR_REPLACE) {
4571 /* free stripes, then update disk */
4572 lod_striping_free(env, lod_dt_obj(dt));
4574 rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
4575 } else if (dt_object_remote(dt)) {
4576 /* This only happens during migration, see
4577 * mdd_migrate_create(), in which Master MDT will
4578 * create a remote target object, and only set
4579 * (migrating) stripe EA on the remote object,
4580 * and does not need creating each stripes. */
4581 rc = lod_sub_xattr_set(env, next, buf, name,
4583 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
4584 /* delete component(s) */
4585 LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
4586 rc = lod_layout_del(env, dt, th);
4589 * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
4590 * it's going to create create file with specified
4591 * component(s), the striping must have not being
4592 * cached in this case;
4594 * Otherwise, it's going to add/change component(s) to
4595 * an existing file, the striping must have been cached
4598 LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
4599 !strcmp(name, XATTR_NAME_LOV),
4600 !lod_dt_obj(dt)->ldo_comp_cached));
4602 rc = lod_striped_create(env, dt, NULL, NULL, th);
4605 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
4606 rc = lod_replace_parent_fid(env, dt, buf, th, false);
4611 /* then all other xattr */
4612 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
4618 * Implementation of dt_object_operations::do_declare_xattr_del.
4620 * \see dt_object_operations::do_declare_xattr_del() in the API description
4623 static int lod_declare_xattr_del(const struct lu_env *env,
4624 struct dt_object *dt, const char *name,
4627 struct lod_object *lo = lod_dt_obj(dt);
4628 struct dt_object *next = dt_object_child(dt);
4633 rc = lod_sub_declare_xattr_del(env, next, name, th);
4637 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4640 /* set xattr to each stripes, if needed */
4641 rc = lod_striping_load(env, lo);
4645 if (lo->ldo_dir_stripe_count == 0)
4648 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4649 struct dt_object *dto = lo->ldo_stripe[i];
4654 rc = lod_sub_declare_xattr_del(env, dto, name, th);
4663 * Implementation of dt_object_operations::do_xattr_del.
4665 * If EA storing a regular striping is being deleted, then release
4666 * all the references to the stripe objects in core.
4668 * \see dt_object_operations::do_xattr_del() in the API description for details.
4670 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
4671 const char *name, struct thandle *th)
4673 struct dt_object *next = dt_object_child(dt);
4674 struct lod_object *lo = lod_dt_obj(dt);
4679 if (!strcmp(name, XATTR_NAME_LOV) || !strcmp(name, XATTR_NAME_LMV))
4680 lod_striping_free(env, lod_dt_obj(dt));
4682 rc = lod_sub_xattr_del(env, next, name, th);
4683 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
4686 if (lo->ldo_dir_stripe_count == 0)
4689 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4690 struct dt_object *dto = lo->ldo_stripe[i];
4695 rc = lod_sub_xattr_del(env, dto, name, th);
4704 * Implementation of dt_object_operations::do_xattr_list.
4706 * \see dt_object_operations::do_xattr_list() in the API description
4709 static int lod_xattr_list(const struct lu_env *env,
4710 struct dt_object *dt, const struct lu_buf *buf)
4712 return dt_xattr_list(env, dt_object_child(dt), buf);
4715 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
4717 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
4721 * Copy OST list from layout provided by user.
4723 * \param[in] lod_comp layout_component to be filled
4724 * \param[in] v3 LOV EA V3 user data
4726 * \retval 0 on success
4727 * \retval negative if failed
4729 int lod_comp_copy_ost_lists(struct lod_layout_component *lod_comp,
4730 struct lov_user_md_v3 *v3)
4736 if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
4737 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
4739 if (lod_comp->llc_ostlist.op_array) {
4740 if (lod_comp->llc_ostlist.op_size >=
4741 v3->lmm_stripe_count * sizeof(__u32)) {
4742 lod_comp->llc_ostlist.op_count =
4743 v3->lmm_stripe_count;
4746 OBD_FREE(lod_comp->llc_ostlist.op_array,
4747 lod_comp->llc_ostlist.op_size);
4750 /* copy ost list from lmm */
4751 lod_comp->llc_ostlist.op_count = v3->lmm_stripe_count;
4752 lod_comp->llc_ostlist.op_size = v3->lmm_stripe_count * sizeof(__u32);
4753 OBD_ALLOC(lod_comp->llc_ostlist.op_array,
4754 lod_comp->llc_ostlist.op_size);
4755 if (!lod_comp->llc_ostlist.op_array)
4758 for (j = 0; j < v3->lmm_stripe_count; j++) {
4759 lod_comp->llc_ostlist.op_array[j] =
4760 v3->lmm_objects[j].l_ost_idx;
4768 * Get default striping.
4770 * \param[in] env execution environment
4771 * \param[in] lo object
4772 * \param[out] lds default striping
4774 * \retval 0 on success
4775 * \retval negative if failed
4777 static int lod_get_default_lov_striping(const struct lu_env *env,
4778 struct lod_object *lo,
4779 struct lod_default_striping *lds)
4781 struct lod_thread_info *info = lod_env_info(env);
4782 struct lov_user_md_v1 *v1 = NULL;
4783 struct lov_user_md_v3 *v3 = NULL;
4784 struct lov_comp_md_v1 *comp_v1 = NULL;
4792 rc = lod_get_lov_ea(env, lo);
4796 if (rc < (typeof(rc))sizeof(struct lov_user_md))
4799 v1 = info->lti_ea_store;
4800 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
4801 lustre_swab_lov_user_md_v1(v1);
4802 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
4803 v3 = (struct lov_user_md_v3 *)v1;
4804 lustre_swab_lov_user_md_v3(v3);
4805 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_SPECIFIC)) {
4806 v3 = (struct lov_user_md_v3 *)v1;
4807 lustre_swab_lov_user_md_v3(v3);
4808 lustre_swab_lov_user_md_objects(v3->lmm_objects,
4809 v3->lmm_stripe_count);
4810 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1) ||
4811 v1->lmm_magic == __swab32(LOV_USER_MAGIC_SEL)) {
4812 comp_v1 = (struct lov_comp_md_v1 *)v1;
4813 lustre_swab_lov_comp_md_v1(comp_v1);
4816 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
4817 v1->lmm_magic != LOV_MAGIC_COMP_V1 &&
4818 v1->lmm_magic != LOV_MAGIC_SEL &&
4819 v1->lmm_magic != LOV_USER_MAGIC_SPECIFIC)
4822 if (v1->lmm_magic == LOV_MAGIC_COMP_V1 ||
4823 v1->lmm_magic == LOV_MAGIC_SEL) {
4824 comp_v1 = (struct lov_comp_md_v1 *)v1;
4825 comp_cnt = comp_v1->lcm_entry_count;
4828 mirror_cnt = comp_v1->lcm_mirror_count + 1;
4836 /* realloc default comp entries if necessary */
4837 rc = lod_def_striping_comp_resize(lds, comp_cnt);
4841 lds->lds_def_comp_cnt = comp_cnt;
4842 lds->lds_def_striping_is_composite = composite;
4843 lds->lds_def_mirror_cnt = mirror_cnt;
4845 for (i = 0; i < comp_cnt; i++) {
4846 struct lod_layout_component *lod_comp;
4849 lod_comp = &lds->lds_def_comp_entries[i];
4851 * reset lod_comp values, llc_stripes is always NULL in
4852 * the default striping template, llc_pool will be reset
4855 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
4858 v1 = (struct lov_user_md *)((char *)comp_v1 +
4859 comp_v1->lcm_entries[i].lcme_offset);
4860 lod_comp->llc_extent =
4861 comp_v1->lcm_entries[i].lcme_extent;
4862 /* We only inherit certain flags from the layout */
4863 lod_comp->llc_flags =
4864 comp_v1->lcm_entries[i].lcme_flags &
4865 LCME_TEMPLATE_FLAGS;
4868 if (!lov_pattern_supported(v1->lmm_pattern) &&
4869 !(v1->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
4870 lod_free_def_comp_entries(lds);
4874 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d "
4875 "stripe_offset=%d\n",
4876 PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
4877 (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
4878 (int)v1->lmm_stripe_offset);
4880 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
4881 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
4882 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
4883 lod_comp->llc_pattern = v1->lmm_pattern;
4886 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
4887 /* XXX: sanity check here */
4888 v3 = (struct lov_user_md_v3 *) v1;
4889 if (v3->lmm_pool_name[0] != '\0')
4890 pool = v3->lmm_pool_name;
4892 lod_set_def_pool(lds, i, pool);
4893 if (v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
4894 v3 = (struct lov_user_md_v3 *)v1;
4895 rc = lod_comp_copy_ost_lists(lod_comp, v3);
4898 } else if (lod_comp->llc_ostlist.op_array &&
4899 lod_comp->llc_ostlist.op_count) {
4900 for (j = 0; j < lod_comp->llc_ostlist.op_count; j++)
4901 lod_comp->llc_ostlist.op_array[j] = -1;
4902 lod_comp->llc_ostlist.op_count = 0;
4906 lds->lds_def_striping_set = 1;
4911 * Get default directory striping.
4913 * \param[in] env execution environment
4914 * \param[in] lo object
4915 * \param[out] lds default striping
4917 * \retval 0 on success
4918 * \retval negative if failed
4920 static int lod_get_default_lmv_striping(const struct lu_env *env,
4921 struct lod_object *lo,
4922 struct lod_default_striping *lds)
4924 struct lmv_user_md *lmu;
4927 lds->lds_dir_def_striping_set = 0;
4929 rc = lod_get_default_lmv_ea(env, lo);
4933 if (rc >= (int)sizeof(*lmu)) {
4934 struct lod_thread_info *info = lod_env_info(env);
4936 lmu = info->lti_ea_store;
4938 lds->lds_dir_def_stripe_count =
4939 le32_to_cpu(lmu->lum_stripe_count);
4940 lds->lds_dir_def_stripe_offset =
4941 le32_to_cpu(lmu->lum_stripe_offset);
4942 lds->lds_dir_def_hash_type =
4943 le32_to_cpu(lmu->lum_hash_type);
4944 lds->lds_dir_def_striping_set = 1;
4951 * Get default striping in the object.
4953 * Get object default striping and default directory striping.
4955 * \param[in] env execution environment
4956 * \param[in] lo object
4957 * \param[out] lds default striping
4959 * \retval 0 on success
4960 * \retval negative if failed
4962 static int lod_get_default_striping(const struct lu_env *env,
4963 struct lod_object *lo,
4964 struct lod_default_striping *lds)
4968 rc = lod_get_default_lov_striping(env, lo, lds);
4969 rc1 = lod_get_default_lmv_striping(env, lo, lds);
4970 if (rc == 0 && rc1 < 0)
4977 * Apply default striping on object.
4979 * If object striping pattern is not set, set to the one in default striping.
4980 * The default striping is from parent or fs.
4982 * \param[in] lo new object
4983 * \param[in] lds default striping
4984 * \param[in] mode new object's mode
4986 static void lod_striping_from_default(struct lod_object *lo,
4987 const struct lod_default_striping *lds,
4990 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4991 struct lov_desc *desc = &d->lod_desc;
4994 if (lds->lds_def_striping_set && S_ISREG(mode)) {
4995 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
4996 lds->lds_def_comp_cnt);
5000 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
5001 if (lds->lds_def_mirror_cnt > 1)
5002 lo->ldo_flr_state = LCM_FL_RDONLY;
5004 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5005 struct lod_layout_component *obj_comp =
5006 &lo->ldo_comp_entries[i];
5007 struct lod_layout_component *def_comp =
5008 &lds->lds_def_comp_entries[i];
5010 CDEBUG(D_LAYOUT, "Inherit from default: flags=%#x "
5011 "size=%hu nr=%u offset=%u pattern=%#x pool=%s\n",
5012 def_comp->llc_flags,
5013 def_comp->llc_stripe_size,
5014 def_comp->llc_stripe_count,
5015 def_comp->llc_stripe_offset,
5016 def_comp->llc_pattern,
5017 def_comp->llc_pool ?: "");
5019 *obj_comp = *def_comp;
5020 if (def_comp->llc_pool != NULL) {
5021 /* pointer was copied from def_comp */
5022 obj_comp->llc_pool = NULL;
5023 lod_obj_set_pool(lo, i, def_comp->llc_pool);
5027 if (def_comp->llc_ostlist.op_array &&
5028 def_comp->llc_ostlist.op_count) {
5029 OBD_ALLOC(obj_comp->llc_ostlist.op_array,
5030 obj_comp->llc_ostlist.op_size);
5031 if (!obj_comp->llc_ostlist.op_array)
5033 memcpy(obj_comp->llc_ostlist.op_array,
5034 def_comp->llc_ostlist.op_array,
5035 obj_comp->llc_ostlist.op_size);
5036 } else if (def_comp->llc_ostlist.op_array) {
5037 obj_comp->llc_ostlist.op_array = NULL;
5041 * Don't initialize these fields for plain layout
5042 * (v1/v3) here, they are inherited in the order of
5043 * 'parent' -> 'fs default (root)' -> 'global default
5044 * values for stripe_count & stripe_size'.
5046 * see lod_ah_init().
5048 if (!lo->ldo_is_composite)
5051 lod_adjust_stripe_info(obj_comp, desc);
5053 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
5054 if (lo->ldo_dir_stripe_count == 0)
5055 lo->ldo_dir_stripe_count =
5056 lds->lds_dir_def_stripe_count;
5057 if (lo->ldo_dir_stripe_offset == -1)
5058 lo->ldo_dir_stripe_offset =
5059 lds->lds_dir_def_stripe_offset;
5060 if (lo->ldo_dir_hash_type == 0)
5061 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type &
5062 ~LMV_HASH_FLAG_SPACE;
5064 CDEBUG(D_LAYOUT, "striping from default dir: count:%hu, "
5065 "offset:%u, hash_type:%u\n",
5066 lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
5067 lo->ldo_dir_hash_type);
5071 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root)
5073 struct lod_layout_component *lod_comp;
5075 if (lo->ldo_comp_cnt == 0)
5078 if (lo->ldo_is_composite)
5081 lod_comp = &lo->ldo_comp_entries[0];
5083 if (lod_comp->llc_stripe_count <= 0 ||
5084 lod_comp->llc_stripe_size <= 0)
5087 if (from_root && (lod_comp->llc_pool == NULL ||
5088 lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
5095 * Implementation of dt_object_operations::do_ah_init.
5097 * This method is used to make a decision on the striping configuration for the
5098 * object being created. It can be taken from the \a parent object if it exists,
5099 * or filesystem's default. The resulting configuration (number of stripes,
5100 * stripe size/offset, pool name, etc) is stored in the object itself and will
5101 * be used by the methods like ->doo_declare_create().
5103 * \see dt_object_operations::do_ah_init() in the API description for details.
5105 static void lod_ah_init(const struct lu_env *env,
5106 struct dt_allocation_hint *ah,
5107 struct dt_object *parent,
5108 struct dt_object *child,
5111 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
5112 struct lod_thread_info *info = lod_env_info(env);
5113 struct lod_default_striping *lds = lod_lds_buf_get(env);
5114 struct dt_object *nextp = NULL;
5115 struct dt_object *nextc;
5116 struct lod_object *lp = NULL;
5117 struct lod_object *lc;
5118 struct lov_desc *desc;
5119 struct lod_layout_component *lod_comp;
5125 if (likely(parent)) {
5126 nextp = dt_object_child(parent);
5127 lp = lod_dt_obj(parent);
5130 nextc = dt_object_child(child);
5131 lc = lod_dt_obj(child);
5133 LASSERT(!lod_obj_is_striped(child));
5134 /* default layout template may have been set on the regular file
5135 * when this is called from mdd_create_data() */
5136 if (S_ISREG(child_mode))
5137 lod_free_comp_entries(lc);
5139 if (!dt_object_exists(nextc))
5140 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
5142 if (S_ISDIR(child_mode)) {
5143 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
5145 /* other default values are 0 */
5146 lc->ldo_dir_stripe_offset = -1;
5148 /* no default striping configuration is needed for
5151 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5152 le32_to_cpu(lum1->lum_magic) == LMV_MAGIC_FOREIGN) {
5153 lc->ldo_dir_is_foreign = true;
5154 /* keep stripe_count 0 and stripe_offset -1 */
5155 CDEBUG(D_INFO, "no default striping for foreign dir\n");
5160 * If parent object is not root directory,
5161 * then get default striping from parent object.
5163 if (likely(lp != NULL) && !fid_is_root(lod_object_fid(lp)))
5164 lod_get_default_striping(env, lp, lds);
5166 /* set child default striping info, default value is NULL */
5167 if (lds->lds_def_striping_set || lds->lds_dir_def_striping_set)
5168 lc->ldo_def_striping = lds;
5170 /* It should always honour the specified stripes */
5171 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
5172 * will have old magic. In this case, we should ignore the
5173 * stripe count and try to create dir by default stripe.
5175 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5176 (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
5177 le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
5178 lc->ldo_dir_stripe_count =
5179 le32_to_cpu(lum1->lum_stripe_count);
5180 lc->ldo_dir_stripe_offset =
5181 le32_to_cpu(lum1->lum_stripe_offset);
5182 lc->ldo_dir_hash_type =
5183 le32_to_cpu(lum1->lum_hash_type);
5185 "set dirstripe: count %hu, offset %d, hash %u\n",
5186 lc->ldo_dir_stripe_count,
5187 (int)lc->ldo_dir_stripe_offset,
5188 lc->ldo_dir_hash_type);
5190 /* transfer defaults LMV to new directory */
5191 lod_striping_from_default(lc, lds, child_mode);
5193 /* set count 0 to create normal directory */
5194 if (lc->ldo_dir_stripe_count == 1)
5195 lc->ldo_dir_stripe_count = 0;
5198 /* shrink the stripe_count to the avaible MDT count */
5199 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
5200 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)) {
5201 lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
5202 if (lc->ldo_dir_stripe_count == 1)
5203 lc->ldo_dir_stripe_count = 0;
5206 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
5207 lc->ldo_dir_stripe_count,
5208 (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
5213 /* child object regular file*/
5215 if (!lod_object_will_be_striped(S_ISREG(child_mode),
5216 lu_object_fid(&child->do_lu)))
5219 /* If object is going to be striped over OSTs, transfer default
5220 * striping information to the child, so that we can use it
5221 * during declaration and creation.
5223 * Try from the parent first.
5225 if (likely(lp != NULL)) {
5226 rc = lod_get_default_lov_striping(env, lp, lds);
5228 lod_striping_from_default(lc, lds, child_mode);
5231 /* Initialize lod_device::lod_md_root object reference */
5232 if (d->lod_md_root == NULL) {
5233 struct dt_object *root;
5234 struct lod_object *lroot;
5236 lu_root_fid(&info->lti_fid);
5237 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
5238 if (!IS_ERR(root)) {
5239 lroot = lod_dt_obj(root);
5241 spin_lock(&d->lod_lock);
5242 if (d->lod_md_root != NULL)
5243 dt_object_put(env, &d->lod_md_root->ldo_obj);
5244 d->lod_md_root = lroot;
5245 spin_unlock(&d->lod_lock);
5249 /* try inherit layout from the root object (fs default) when:
5250 * - parent does not have default layout; or
5251 * - parent has plain(v1/v3) default layout, and some attributes
5252 * are not specified in the default layout;
5254 if (d->lod_md_root != NULL && lod_need_inherit_more(lc, true)) {
5255 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds);
5258 if (lc->ldo_comp_cnt == 0) {
5259 lod_striping_from_default(lc, lds, child_mode);
5260 } else if (!lds->lds_def_striping_is_composite) {
5261 struct lod_layout_component *def_comp;
5263 LASSERT(!lc->ldo_is_composite);
5264 lod_comp = &lc->ldo_comp_entries[0];
5265 def_comp = &lds->lds_def_comp_entries[0];
5267 if (lod_comp->llc_stripe_count <= 0)
5268 lod_comp->llc_stripe_count =
5269 def_comp->llc_stripe_count;
5270 if (lod_comp->llc_stripe_size <= 0)
5271 lod_comp->llc_stripe_size =
5272 def_comp->llc_stripe_size;
5273 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT &&
5274 (!lod_comp->llc_pool || !lod_comp->llc_pool[0]))
5275 lod_comp->llc_stripe_offset =
5276 def_comp->llc_stripe_offset;
5277 if (lod_comp->llc_pool == NULL)
5278 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
5283 * fs default striping may not be explicitly set, or historically set
5284 * in config log, use them.
5286 if (lod_need_inherit_more(lc, false)) {
5287 if (lc->ldo_comp_cnt == 0) {
5288 rc = lod_alloc_comp_entries(lc, 0, 1);
5290 /* fail to allocate memory, will create a
5291 * non-striped file. */
5293 lc->ldo_is_composite = 0;
5294 lod_comp = &lc->ldo_comp_entries[0];
5295 lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
5297 LASSERT(!lc->ldo_is_composite);
5298 lod_comp = &lc->ldo_comp_entries[0];
5299 desc = &d->lod_desc;
5300 lod_adjust_stripe_info(lod_comp, desc);
5306 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
5308 * Size initialization on late striping.
5310 * Propagate the size of a truncated object to a deferred striping.
5311 * This function handles a special case when truncate was done on a
5312 * non-striped object and now while the striping is being created
5313 * we can't lose that size, so we have to propagate it to the stripes
5316 * \param[in] env execution environment
5317 * \param[in] dt object
5318 * \param[in] th transaction handle
5320 * \retval 0 on success
5321 * \retval negative if failed
5323 static int lod_declare_init_size(const struct lu_env *env,
5324 struct dt_object *dt, struct thandle *th)
5326 struct dt_object *next = dt_object_child(dt);
5327 struct lod_object *lo = lod_dt_obj(dt);
5328 struct dt_object **objects = NULL;
5329 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
5330 uint64_t size, offs;
5331 int i, rc, stripe, stripe_count = 0, stripe_size = 0;
5332 struct lu_extent size_ext;
5335 if (!lod_obj_is_striped(dt))
5338 rc = dt_attr_get(env, next, attr);
5339 LASSERT(attr->la_valid & LA_SIZE);
5343 size = attr->la_size;
5347 size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
5348 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5349 struct lod_layout_component *lod_comp;
5350 struct lu_extent *extent;
5352 lod_comp = &lo->ldo_comp_entries[i];
5354 if (lod_comp->llc_stripe == NULL)
5357 extent = &lod_comp->llc_extent;
5358 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
5359 if (!lo->ldo_is_composite ||
5360 lu_extent_is_overlapped(extent, &size_ext)) {
5361 objects = lod_comp->llc_stripe;
5362 stripe_count = lod_comp->llc_stripe_count;
5363 stripe_size = lod_comp->llc_stripe_size;
5366 if (stripe_count == 0)
5369 LASSERT(objects != NULL && stripe_size != 0);
5370 /* ll_do_div64(a, b) returns a % b, and a = a / b */
5371 ll_do_div64(size, (__u64)stripe_size);
5372 stripe = ll_do_div64(size, (__u64)stripe_count);
5373 LASSERT(objects[stripe] != NULL);
5375 size = size * stripe_size;
5376 offs = attr->la_size;
5377 size += ll_do_div64(offs, stripe_size);
5379 attr->la_valid = LA_SIZE;
5380 attr->la_size = size;
5382 rc = lod_sub_declare_attr_set(env, objects[stripe],
5391 * Declare creation of striped object.
5393 * The function declares creation stripes for a regular object. The function
5394 * also declares whether the stripes will be created with non-zero size if
5395 * previously size was set non-zero on the master object. If object \a dt is
5396 * not local, then only fully defined striping can be applied in \a lovea.
5397 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
5400 * \param[in] env execution environment
5401 * \param[in] dt object
5402 * \param[in] attr attributes the stripes will be created with
5403 * \param[in] lovea a buffer containing striping description
5404 * \param[in] th transaction handle
5406 * \retval 0 on success
5407 * \retval negative if failed
5409 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
5410 struct lu_attr *attr,
5411 const struct lu_buf *lovea, struct thandle *th)
5413 struct lod_thread_info *info = lod_env_info(env);
5414 struct dt_object *next = dt_object_child(dt);
5415 struct lod_object *lo = lod_dt_obj(dt);
5419 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
5420 GOTO(out, rc = -ENOMEM);
5422 if (!dt_object_remote(next)) {
5423 /* choose OST and generate appropriate objects */
5424 rc = lod_prepare_create(env, lo, attr, lovea, th);
5429 * declare storage for striping data
5431 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5433 /* LOD can not choose OST objects for remote objects, i.e.
5434 * stripes must be ready before that. Right now, it can only
5435 * happen during migrate, i.e. migrate process needs to create
5436 * remote regular file (mdd_migrate_create), then the migrate
5437 * process will provide stripeEA. */
5438 LASSERT(lovea != NULL);
5439 info->lti_buf = *lovea;
5442 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
5443 XATTR_NAME_LOV, 0, th);
5448 * if striping is created with local object's size > 0,
5449 * we have to propagate this size to specific object
5450 * the case is possible only when local object was created previously
5452 if (dt_object_exists(next))
5453 rc = lod_declare_init_size(env, dt, th);
5456 /* failed to create striping or to set initial size, let's reset
5457 * config so that others don't get confused */
5459 lod_striping_free(env, lo);
5465 * Whether subdirectories under \a dt should be created on MDTs by space QoS
5467 * If LMV_HASH_FLAG_SPACE is set on directory default layout, its subdirectories
5468 * should be created on MDT by space QoS.
5470 * \param[in] env execution environment
5471 * \param[in] dev lu device
5472 * \param[in] dt object
5474 * \retval 1 if directory should create subdir by space usage
5476 * \retval -ev if failed
5478 static inline int dt_object_qos_mkdir(const struct lu_env *env,
5479 struct lu_device *dev,
5480 struct dt_object *dt)
5482 struct lod_thread_info *info = lod_env_info(env);
5483 struct lu_object *obj;
5484 struct lod_object *lo;
5485 struct lmv_user_md *lmu;
5488 obj = lu_object_find_slice(env, dev, lu_object_fid(&dt->do_lu), NULL);
5490 return PTR_ERR(obj);
5492 lo = lu2lod_obj(obj);
5494 rc = lod_get_default_lmv_ea(env, lo);
5495 dt_object_put(env, dt);
5499 if (rc < (int)sizeof(*lmu))
5502 lmu = info->lti_ea_store;
5503 return !!(le32_to_cpu(lmu->lum_hash_type) & LMV_HASH_FLAG_SPACE);
5507 * Implementation of dt_object_operations::do_declare_create.
5509 * The method declares creation of a new object. If the object will be striped,
5510 * then helper functions are called to find FIDs for the stripes, declare
5511 * creation of the stripes and declare initialization of the striping
5512 * information to be stored in the master object.
5514 * \see dt_object_operations::do_declare_create() in the API description
5517 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
5518 struct lu_attr *attr,
5519 struct dt_allocation_hint *hint,
5520 struct dt_object_format *dof, struct thandle *th)
5522 struct dt_object *next = dt_object_child(dt);
5523 struct lod_object *lo = lod_dt_obj(dt);
5532 * first of all, we declare creation of local object
5534 rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
5539 * it's lod_ah_init() that has decided the object will be striped
5541 if (dof->dof_type == DFT_REGULAR) {
5542 /* callers don't want stripes */
5543 /* XXX: all tricky interactions with ->ah_make_hint() decided
5544 * to use striping, then ->declare_create() behaving differently
5545 * should be cleaned */
5546 if (dof->u.dof_reg.striped != 0)
5547 rc = lod_declare_striped_create(env, dt, attr,
5549 } else if (dof->dof_type == DFT_DIR) {
5550 struct seq_server_site *ss;
5551 struct lu_buf buf = { NULL };
5552 struct lu_buf *lmu = NULL;
5554 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
5556 /* If the parent has default stripeEA, and client
5557 * did not find it before sending create request,
5558 * then MDT will return -EREMOTE, and client will
5559 * retrieve the default stripeEA and re-create the
5562 * Note: if dah_eadata != NULL, it means creating the
5563 * striped directory with specified stripeEA, then it
5564 * should ignore the default stripeEA */
5565 if (hint != NULL && hint->dah_eadata == NULL) {
5566 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
5567 GOTO(out, rc = -EREMOTE);
5569 if (lo->ldo_dir_stripe_offset == -1) {
5571 * child and parent should be in the same MDT,
5572 * but if parent has plain layout, it's allowed.
5574 if (hint->dah_parent &&
5575 dt_object_remote(hint->dah_parent)) {
5576 rc = dt_object_qos_mkdir(env,
5577 lo->ldo_obj.do_lu.lo_dev,
5580 GOTO(out, rc ? rc : -EREMOTE);
5582 } else if (lo->ldo_dir_stripe_offset !=
5584 struct lod_device *lod;
5585 struct lod_tgt_descs *ltd;
5586 struct lod_tgt_desc *tgt = NULL;
5587 bool found_mdt = false;
5590 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5591 ltd = &lod->lod_mdt_descs;
5592 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
5593 tgt = LTD_TGT(ltd, i);
5594 if (tgt->ltd_index ==
5595 lo->ldo_dir_stripe_offset) {
5601 /* If the MDT indicated by stripe_offset can be
5602 * found, then tell client to resend the create
5603 * request to the correct MDT, otherwise return
5604 * error to client */
5606 GOTO(out, rc = -EREMOTE);
5608 GOTO(out, rc = -EINVAL);
5610 } else if (hint && hint->dah_eadata) {
5612 lmu->lb_buf = (void *)hint->dah_eadata;
5613 lmu->lb_len = hint->dah_eadata_len;
5616 rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
5620 /* failed to create striping or to set initial size, let's reset
5621 * config so that others don't get confused */
5623 lod_striping_free(env, lo);
5628 * Generate component ID for new created component.
5630 * \param[in] lo LOD object
5631 * \param[in] comp_idx index of ldo_comp_entries
5633 * \retval component ID on success
5634 * \retval LCME_ID_INVAL on failure
5636 static __u32 lod_gen_component_id(struct lod_object *lo,
5637 int mirror_id, int comp_idx)
5639 struct lod_layout_component *lod_comp;
5640 __u32 id, start, end;
5643 LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
5645 lod_obj_inc_layout_gen(lo);
5646 id = lo->ldo_layout_gen;
5647 if (likely(id <= SEQ_ID_MAX))
5648 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
5650 /* Layout generation wraps, need to check collisions. */
5651 start = id & SEQ_ID_MASK;
5654 for (id = start; id <= end; id++) {
5655 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5656 lod_comp = &lo->ldo_comp_entries[i];
5657 if (pflr_id(mirror_id, id) == lod_comp->llc_id)
5660 /* Found the ununsed ID */
5661 if (i == lo->ldo_comp_cnt)
5662 RETURN(pflr_id(mirror_id, id));
5664 if (end == LCME_ID_MAX) {
5666 end = min(lo->ldo_layout_gen & LCME_ID_MASK,
5667 (__u32)(LCME_ID_MAX - 1));
5671 RETURN(LCME_ID_INVAL);
5675 * Creation of a striped regular object.
5677 * The function is called to create the stripe objects for a regular
5678 * striped file. This can happen at the initial object creation or
5679 * when the caller asks LOD to do so using ->do_xattr_set() method
5680 * (so called late striping). Notice all the information are already
5681 * prepared in the form of the list of objects (ldo_stripe field).
5682 * This is done during declare phase.
5684 * \param[in] env execution environment
5685 * \param[in] dt object
5686 * \param[in] attr attributes the stripes will be created with
5687 * \param[in] dof format of stripes (see OSD API description)
5688 * \param[in] th transaction handle
5690 * \retval 0 on success
5691 * \retval negative if failed
5693 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
5694 struct lu_attr *attr, struct dt_object_format *dof,
5697 struct lod_layout_component *lod_comp;
5698 struct lod_object *lo = lod_dt_obj(dt);
5703 LASSERT((lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL) ||
5704 lo->ldo_is_foreign);
5706 mirror_id = 0; /* non-flr file's mirror_id is 0 */
5707 if (lo->ldo_mirror_count > 1) {
5708 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5709 lod_comp = &lo->ldo_comp_entries[i];
5710 if (lod_comp->llc_id != LCME_ID_INVAL &&
5711 mirror_id_of(lod_comp->llc_id) > mirror_id)
5712 mirror_id = mirror_id_of(lod_comp->llc_id);
5716 /* create all underlying objects */
5717 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5718 lod_comp = &lo->ldo_comp_entries[i];
5720 if (lod_comp->llc_id == LCME_ID_INVAL) {
5721 /* only the component of FLR layout with more than 1
5722 * mirror has mirror ID in its component ID.
5724 if (lod_comp->llc_extent.e_start == 0 &&
5725 lo->ldo_mirror_count > 1)
5728 lod_comp->llc_id = lod_gen_component_id(lo,
5730 if (lod_comp->llc_id == LCME_ID_INVAL)
5731 GOTO(out, rc = -ERANGE);
5734 if (lod_comp_inited(lod_comp))
5737 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
5738 lod_comp_set_init(lod_comp);
5740 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
5741 lod_comp_set_init(lod_comp);
5743 if (lod_comp->llc_stripe == NULL)
5746 LASSERT(lod_comp->llc_stripe_count);
5747 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
5748 struct dt_object *object = lod_comp->llc_stripe[j];
5749 LASSERT(object != NULL);
5750 rc = lod_sub_create(env, object, attr, NULL, dof, th);
5754 lod_comp_set_init(lod_comp);
5757 rc = lod_fill_mirrors(lo);
5761 rc = lod_generate_and_set_lovea(env, lo, th);
5765 lo->ldo_comp_cached = 1;
5769 lod_striping_free(env, lo);
5773 static inline bool lod_obj_is_dom(struct dt_object *dt)
5775 struct lod_object *lo = lod_dt_obj(dt);
5777 if (!dt_object_exists(dt_object_child(dt)))
5780 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
5783 if (!lo->ldo_comp_cnt)
5786 return (lov_pattern(lo->ldo_comp_entries[0].llc_pattern) ==
5791 * Implementation of dt_object_operations::do_create.
5793 * If any of preceeding methods (like ->do_declare_create(),
5794 * ->do_ah_init(), etc) chose to create a striped object,
5795 * then this method will create the master and the stripes.
5797 * \see dt_object_operations::do_create() in the API description for details.
5799 static int lod_create(const struct lu_env *env, struct dt_object *dt,
5800 struct lu_attr *attr, struct dt_allocation_hint *hint,
5801 struct dt_object_format *dof, struct thandle *th)
5806 /* create local object */
5807 rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
5811 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
5812 (lod_obj_is_striped(dt) || lod_obj_is_dom(dt)) &&
5813 dof->u.dof_reg.striped != 0) {
5814 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
5815 rc = lod_striped_create(env, dt, attr, dof, th);
5822 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
5823 struct dt_object *dt, struct thandle *th,
5824 int comp_idx, int stripe_idx,
5825 struct lod_obj_stripe_cb_data *data)
5827 if (data->locd_declare)
5828 return lod_sub_declare_destroy(env, dt, th);
5829 else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
5830 stripe_idx == cfs_fail_val)
5831 return lod_sub_destroy(env, dt, th);
5837 * Implementation of dt_object_operations::do_declare_destroy.
5839 * If the object is a striped directory, then the function declares reference
5840 * removal from the master object (this is an index) to the stripes and declares
5841 * destroy of all the stripes. In all the cases, it declares an intention to
5842 * destroy the object itself.
5844 * \see dt_object_operations::do_declare_destroy() in the API description
5847 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
5850 struct dt_object *next = dt_object_child(dt);
5851 struct lod_object *lo = lod_dt_obj(dt);
5852 struct lod_thread_info *info = lod_env_info(env);
5853 struct dt_object *stripe;
5854 char *stripe_name = info->lti_key;
5860 * load striping information, notice we don't do this when object
5861 * is being initialized as we don't need this information till
5862 * few specific cases like destroy, chown
5864 rc = lod_striping_load(env, lo);
5868 /* declare destroy for all underlying objects */
5869 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5870 rc = next->do_ops->do_index_try(env, next,
5871 &dt_directory_features);
5875 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5876 stripe = lo->ldo_stripe[i];
5880 rc = lod_sub_declare_ref_del(env, next, th);
5884 snprintf(stripe_name, sizeof(info->lti_key),
5886 PFID(lu_object_fid(&stripe->do_lu)), i);
5887 rc = lod_sub_declare_delete(env, next,
5888 (const struct dt_key *)stripe_name, th);
5895 * we declare destroy for the local object
5897 rc = lod_sub_declare_destroy(env, next, th);
5901 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
5902 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
5905 if (!lod_obj_is_striped(dt))
5908 /* declare destroy all striped objects */
5909 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5910 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5911 stripe = lo->ldo_stripe[i];
5915 if (!dt_object_exists(stripe))
5918 rc = lod_sub_declare_ref_del(env, stripe, th);
5922 rc = lod_sub_declare_destroy(env, stripe, th);
5927 struct lod_obj_stripe_cb_data data = { { 0 } };
5929 data.locd_declare = true;
5930 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
5931 rc = lod_obj_for_each_stripe(env, lo, th, &data);
5938 * Implementation of dt_object_operations::do_destroy.
5940 * If the object is a striped directory, then the function removes references
5941 * from the master object (this is an index) to the stripes and destroys all
5942 * the stripes. In all the cases, the function destroys the object itself.
5944 * \see dt_object_operations::do_destroy() in the API description for details.
5946 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
5949 struct dt_object *next = dt_object_child(dt);
5950 struct lod_object *lo = lod_dt_obj(dt);
5951 struct lod_thread_info *info = lod_env_info(env);
5952 char *stripe_name = info->lti_key;
5953 struct dt_object *stripe;
5959 /* destroy sub-stripe of master object */
5960 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5961 rc = next->do_ops->do_index_try(env, next,
5962 &dt_directory_features);
5966 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5967 stripe = lo->ldo_stripe[i];
5971 rc = lod_sub_ref_del(env, next, th);
5975 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
5976 PFID(lu_object_fid(&stripe->do_lu)), i);
5978 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
5979 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
5980 PFID(lu_object_fid(&stripe->do_lu)));
5982 rc = lod_sub_delete(env, next,
5983 (const struct dt_key *)stripe_name, th);
5989 rc = lod_sub_destroy(env, next, th);
5993 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
5994 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
5997 if (!lod_obj_is_striped(dt))
6000 /* destroy all striped objects */
6001 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6002 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6003 stripe = lo->ldo_stripe[i];
6007 if (!dt_object_exists(stripe))
6010 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
6011 i == cfs_fail_val) {
6012 dt_write_lock(env, stripe, MOR_TGT_CHILD);
6013 rc = lod_sub_ref_del(env, stripe, th);
6014 dt_write_unlock(env, stripe);
6018 rc = lod_sub_destroy(env, stripe, th);
6024 struct lod_obj_stripe_cb_data data = { { 0 } };
6026 data.locd_declare = false;
6027 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6028 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6035 * Implementation of dt_object_operations::do_declare_ref_add.
6037 * \see dt_object_operations::do_declare_ref_add() in the API description
6040 static int lod_declare_ref_add(const struct lu_env *env,
6041 struct dt_object *dt, struct thandle *th)
6043 return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
6047 * Implementation of dt_object_operations::do_ref_add.
6049 * \see dt_object_operations::do_ref_add() in the API description for details.
6051 static int lod_ref_add(const struct lu_env *env,
6052 struct dt_object *dt, struct thandle *th)
6054 return lod_sub_ref_add(env, dt_object_child(dt), th);
6058 * Implementation of dt_object_operations::do_declare_ref_del.
6060 * \see dt_object_operations::do_declare_ref_del() in the API description
6063 static int lod_declare_ref_del(const struct lu_env *env,
6064 struct dt_object *dt, struct thandle *th)
6066 return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
6070 * Implementation of dt_object_operations::do_ref_del
6072 * \see dt_object_operations::do_ref_del() in the API description for details.
6074 static int lod_ref_del(const struct lu_env *env,
6075 struct dt_object *dt, struct thandle *th)
6077 return lod_sub_ref_del(env, dt_object_child(dt), th);
6081 * Implementation of dt_object_operations::do_object_sync.
6083 * \see dt_object_operations::do_object_sync() in the API description
6086 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
6087 __u64 start, __u64 end)
6089 return dt_object_sync(env, dt_object_child(dt), start, end);
6093 * Implementation of dt_object_operations::do_object_unlock.
6095 * Used to release LDLM lock(s).
6097 * \see dt_object_operations::do_object_unlock() in the API description
6100 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
6101 struct ldlm_enqueue_info *einfo,
6102 union ldlm_policy_data *policy)
6104 struct lod_object *lo = lod_dt_obj(dt);
6105 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
6106 int slave_locks_size;
6110 if (slave_locks == NULL)
6113 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
6114 /* Note: for remote lock for single stripe dir, MDT will cancel
6115 * the lock by lockh directly */
6116 LASSERT(!dt_object_remote(dt_object_child(dt)));
6118 /* locks were unlocked in MDT layer */
6119 for (i = 0; i < slave_locks->ha_count; i++)
6120 LASSERT(!lustre_handle_is_used(&slave_locks->ha_handles[i]));
6123 * NB, ha_count may not equal to ldo_dir_stripe_count, because dir
6124 * layout may change, e.g., shrink dir layout after migration.
6126 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6127 if (lo->ldo_stripe[i])
6128 dt_invalidate(env, lo->ldo_stripe[i]);
6131 slave_locks_size = offsetof(typeof(*slave_locks),
6132 ha_handles[slave_locks->ha_count]);
6133 OBD_FREE(slave_locks, slave_locks_size);
6134 einfo->ei_cbdata = NULL;
6140 * Implementation of dt_object_operations::do_object_lock.
6142 * Used to get LDLM lock on the non-striped and striped objects.
6144 * \see dt_object_operations::do_object_lock() in the API description
6147 static int lod_object_lock(const struct lu_env *env,
6148 struct dt_object *dt,
6149 struct lustre_handle *lh,
6150 struct ldlm_enqueue_info *einfo,
6151 union ldlm_policy_data *policy)
6153 struct lod_object *lo = lod_dt_obj(dt);
6154 int slave_locks_size;
6155 struct lustre_handle_array *slave_locks = NULL;
6160 /* remote object lock */
6161 if (!einfo->ei_enq_slave) {
6162 LASSERT(dt_object_remote(dt));
6163 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
6167 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
6170 rc = lod_striping_load(env, lo);
6175 if (lo->ldo_dir_stripe_count <= 1)
6178 slave_locks_size = offsetof(typeof(*slave_locks),
6179 ha_handles[lo->ldo_dir_stripe_count]);
6180 /* Freed in lod_object_unlock */
6181 OBD_ALLOC(slave_locks, slave_locks_size);
6184 slave_locks->ha_count = lo->ldo_dir_stripe_count;
6186 /* striped directory lock */
6187 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6188 struct lustre_handle lockh;
6189 struct ldlm_res_id *res_id;
6190 struct dt_object *stripe;
6192 stripe = lo->ldo_stripe[i];
6196 res_id = &lod_env_info(env)->lti_res_id;
6197 fid_build_reg_res_name(lu_object_fid(&stripe->do_lu), res_id);
6198 einfo->ei_res_id = res_id;
6200 if (dt_object_remote(stripe)) {
6201 set_bit(i, (void *)slave_locks->ha_map);
6202 rc = dt_object_lock(env, stripe, &lockh, einfo, policy);
6204 struct ldlm_namespace *ns = einfo->ei_namespace;
6205 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
6206 ldlm_completion_callback completion = einfo->ei_cb_cp;
6207 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
6209 if (einfo->ei_mode == LCK_PW ||
6210 einfo->ei_mode == LCK_EX)
6211 dlmflags |= LDLM_FL_COS_INCOMPAT;
6213 LASSERT(ns != NULL);
6214 rc = ldlm_cli_enqueue_local(env, ns, res_id, LDLM_IBITS,
6215 policy, einfo->ei_mode,
6216 &dlmflags, blocking,
6218 NULL, 0, LVB_T_NONE,
6223 ldlm_lock_decref_and_cancel(
6224 &slave_locks->ha_handles[i],
6226 OBD_FREE(slave_locks, slave_locks_size);
6229 slave_locks->ha_handles[i] = lockh;
6231 einfo->ei_cbdata = slave_locks;
6237 * Implementation of dt_object_operations::do_invalidate.
6239 * \see dt_object_operations::do_invalidate() in the API description for details
6241 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
6243 return dt_invalidate(env, dt_object_child(dt));
6246 static int lod_declare_instantiate_components(const struct lu_env *env,
6247 struct lod_object *lo, struct thandle *th)
6249 struct lod_thread_info *info = lod_env_info(env);
6254 LASSERT(info->lti_count < lo->ldo_comp_cnt);
6256 for (i = 0; i < info->lti_count; i++) {
6257 rc = lod_qos_prep_create(env, lo, NULL, th,
6258 info->lti_comp_idx[i]);
6264 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6265 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
6266 &info->lti_buf, XATTR_NAME_LOV, 0, th);
6273 * Check OSTs for an existing component for further extension
6275 * Checks if OSTs are still healthy and not out of space. Gets free space
6276 * on OSTs (relative to allocation watermark rmb_low) and compares to
6277 * the proposed new_end for this component.
6279 * Decides whether or not to extend a component on its current OSTs.
6281 * \param[in] env execution environment for this thread
6282 * \param[in] lo object we're checking
6283 * \param[in] index index of this component
6284 * \param[in] extension_size extension size for this component
6285 * \param[in] extent layout extent for requested operation
6286 * \param[in] comp_extent extension component extent
6287 * \param[in] write if this is write operation
6289 * \retval true - OK to extend on current OSTs
6290 * \retval false - do not extend on current OSTs
6292 static bool lod_sel_osts_allowed(const struct lu_env *env,
6293 struct lod_object *lo,
6294 int index, __u64 extension_size,
6295 struct lu_extent *extent,
6296 struct lu_extent *comp_extent, int write)
6298 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[index];
6299 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6300 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
6301 __u64 available = 0;
6308 LASSERT(lod_comp->llc_stripe_count != 0);
6311 (extent->e_start == 0 && extent->e_end == OBD_OBJECT_EOF)) {
6312 /* truncate or append */
6313 size = extension_size;
6315 /* In case of write op, check the real write extent,
6316 * it may be larger than the extension_size */
6317 size = roundup(min(extent->e_end, comp_extent->e_end) -
6318 max(extent->e_start, comp_extent->e_start),
6321 /* extension_size is file level, so we must divide by stripe count to
6322 * compare it to available space on a single OST */
6323 size /= lod_comp->llc_stripe_count;
6325 lod_getref(&lod->lod_ost_descs);
6326 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
6327 int index = lod_comp->llc_ost_indices[i];
6328 struct lod_tgt_desc *ost = OST_TGT(lod, index);
6329 struct obd_statfs_info info = { 0 };
6330 int j, repeated = 0;
6334 /* Get the number of times this OST repeats in this component.
6335 * Note: inter-component repeats are not counted as this is
6336 * considered as a rare case: we try to not repeat OST in other
6337 * components if possible. */
6338 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6339 if (index != lod_comp->llc_ost_indices[j])
6342 /* already handled */
6348 if (j < lod_comp->llc_stripe_count)
6351 if (!cfs_bitmap_check(lod->lod_ost_bitmap, index)) {
6352 CDEBUG(D_LAYOUT, "ost %d no longer present\n", index);
6357 rc = dt_statfs(env, ost->ltd_ost, sfs, &info);
6359 CDEBUG(D_LAYOUT, "statfs failed for ost %d, error %d\n",
6365 if (sfs->os_state & OS_STATE_ENOSPC ||
6366 sfs->os_state & OS_STATE_READONLY ||
6367 sfs->os_state & OS_STATE_DEGRADED) {
6368 CDEBUG(D_LAYOUT, "ost %d is not availble for SEL "
6369 "extension, state %u\n", index, sfs->os_state);
6375 available = sfs->os_bavail * sfs->os_bsize;
6376 /* 'available' is relative to the allocation threshold */
6377 available -= (__u64) info.os_reserved_mb_low << 20;
6379 CDEBUG(D_LAYOUT, "ost %d lowwm: %d highwm: %d, "
6380 "%llu %% blocks available, %llu %% blocks free\n",
6381 index, info.os_reserved_mb_low, info.os_reserved_mb_high,
6382 (100ull * sfs->os_bavail) / sfs->os_blocks,
6383 (100ull * sfs->os_bfree) / sfs->os_blocks);
6385 if (size * repeated > available) {
6387 CDEBUG(D_LAYOUT, "low space on ost %d, available %llu "
6388 "< extension size %llu\n", index, available,
6393 lod_putref(lod, &lod->lod_ost_descs);
6399 * Adjust extents after component removal
6401 * When we remove an extension component, we move the start of the next
6402 * component to match the start of the extension component, so no space is left
6405 * \param[in] env execution environment for this thread
6406 * \param[in] lo object
6407 * \param[in] max_comp layout component
6408 * \param[in] index index of this component
6410 * \retval 0 on success
6411 * \retval negative errno on error
6413 static void lod_sel_adjust_extents(const struct lu_env *env,
6414 struct lod_object *lo,
6415 int max_comp, int index)
6417 struct lod_layout_component *lod_comp = NULL;
6418 struct lod_layout_component *next = NULL;
6419 struct lod_layout_component *prev = NULL;
6420 __u64 new_start = 0;
6424 /* Extension space component */
6425 lod_comp = &lo->ldo_comp_entries[index];
6426 next = &lo->ldo_comp_entries[index + 1];
6427 prev = &lo->ldo_comp_entries[index - 1];
6429 LASSERT(lod_comp != NULL && prev != NULL && next != NULL);
6430 LASSERT(lod_comp->llc_flags & LCME_FL_EXTENSION);
6432 /* Previous is being removed */
6433 if (prev && prev->llc_id == LCME_ID_INVAL)
6434 new_start = prev->llc_extent.e_start;
6436 new_start = lod_comp->llc_extent.e_start;
6438 for (i = index + 1; i < max_comp; i++) {
6439 lod_comp = &lo->ldo_comp_entries[i];
6441 start = lod_comp->llc_extent.e_start;
6442 lod_comp->llc_extent.e_start = new_start;
6444 /* We only move zero length extendable components */
6445 if (!(start == lod_comp->llc_extent.e_end))
6448 LASSERT(!(lod_comp->llc_flags & LCME_FL_INIT));
6450 lod_comp->llc_extent.e_end = new_start;
6454 /* Calculate the proposed 'new end' for a component we're extending */
6455 static __u64 lod_extension_new_end(__u64 extension_size, __u64 extent_end,
6456 __u32 stripe_size, __u64 component_end,
6457 __u64 extension_end)
6461 LASSERT(extension_size != 0 && stripe_size != 0);
6463 /* Round up to extension size */
6464 if (extent_end == OBD_OBJECT_EOF) {
6465 new_end = OBD_OBJECT_EOF;
6467 /* Add at least extension_size to the previous component_end,
6468 * covering the req layout extent */
6469 new_end = max(extent_end - component_end, extension_size);
6470 new_end = roundup(new_end, extension_size);
6471 new_end += component_end;
6473 /* Component end must be min stripe size aligned */
6474 if (new_end % stripe_size) {
6475 CDEBUG(D_LAYOUT, "new component end is not aligned "
6476 "by the stripe size %u: [%llu, %llu) ext size "
6477 "%llu new end %llu, aligning\n",
6478 stripe_size, component_end, extent_end,
6479 extension_size, new_end);
6480 new_end = roundup(new_end, stripe_size);
6484 if (new_end < extent_end)
6485 new_end = OBD_OBJECT_EOF;
6488 /* Don't extend past the end of the extension component */
6489 if (new_end > extension_end)
6490 new_end = extension_end;
6495 /* As lod_sel_handler() could be re-entered for the same component several
6496 * times, this is the data for the next call. Fields could be changed to
6497 * component indexes when needed, (e.g. if there is no need to instantiate
6498 * all the previous components up to the current position) to tell the caller
6499 * where to start over from. */
6506 * Process extent updates for a particular layout component
6508 * Handle layout updates for a particular extension space component touched by
6509 * a layout update operation. Core function of self-extending PFL feature.
6511 * In general, this function processes exactly *one* stage of an extension
6512 * operation, modifying the layout accordingly, then returns to the caller.
6513 * The caller is responsible for restarting processing with the new layout,
6514 * which may repeatedly return to this function until the extension updates
6517 * This function does one of a few things to the layout:
6518 * 1. Extends the component before the current extension space component to
6519 * allow it to accomodate the requested operation (if space/policy permit that
6520 * component to continue on its current OSTs)
6522 * 2. If extension of the existing component fails, we do one of two things:
6523 * a. If there is a component after the extension space, we remove the
6524 * extension space component, move the start of the next component down
6525 * accordingly, then notify the caller to restart processing w/the new
6527 * b. If there is no following component, we try repeating the current
6528 * component, creating a new component using the current one as a
6529 * template (keeping its stripe properties but not specific striping),
6530 * and try assigning striping for this component. If there is sufficient
6531 * free space on the OSTs chosen for this component, it is instantiated
6532 * and i/o continues there.
6534 * If there is not sufficient space on the new OSTs, we remove this new
6535 * component & extend the current component.
6537 * Note further that uninited components followed by extension space can be zero
6538 * length meaning that we will try to extend them before initializing them, and
6539 * if that fails, they will be removed without initialization.
6541 * 3. If we extend to/beyond the end of an extension space component, that
6542 * component is exhausted (all of its range has been given to real components),
6543 * so we remove it and restart processing.
6545 * \param[in] env execution environment for this thread
6546 * \param[in,out] lo object to update the layout of
6547 * \param[in] extent layout extent for requested operation, update
6548 * layout to fit this operation
6549 * \param[in] th transaction handle for this operation
6550 * \param[in,out] max_comp the highest comp for the portion of the layout
6551 * we are operating on (For FLR, the chosen
6552 * replica). Updated because we may remove
6554 * \param[in] index index of the extension space component we're
6556 * \param[in] write if this is write op
6557 * \param[in,out] force if the extension is to be forced; set here
6558 to force it on the 2nd call for the same
6561 * \retval 0 on success
6562 * \retval negative errno on error
6564 static int lod_sel_handler(const struct lu_env *env,
6565 struct lod_object *lo,
6566 struct lu_extent *extent,
6567 struct thandle *th, int *max_comp,
6568 int index, int write,
6569 struct sel_data *sd)
6571 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6572 struct lod_thread_info *info = lod_env_info(env);
6573 struct lod_layout_component *lod_comp;
6574 struct lod_layout_component *prev;
6575 struct lod_layout_component *next = NULL;
6576 __u64 extension_size;
6583 /* First component cannot be extension space */
6585 CERROR("%s: "DFID" first component cannot be extension space\n",
6586 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6590 lod_comp = &lo->ldo_comp_entries[index];
6591 prev = &lo->ldo_comp_entries[index - 1];
6592 if ((index + 1) < *max_comp)
6593 next = &lo->ldo_comp_entries[index + 1];
6595 /* extension size uses the stripe size field as KiB */
6596 extension_size = lod_comp->llc_stripe_size * SEL_UNIT_SIZE;
6598 CDEBUG(D_LAYOUT, "prev start %llu, extension start %llu, extension end"
6599 " %llu, extension size %llu\n", prev->llc_extent.e_start,
6600 lod_comp->llc_extent.e_start, lod_comp->llc_extent.e_end,
6603 /* Two extension space components cannot be adjacent & extension space
6604 * components cannot be init */
6605 if ((prev->llc_flags & LCME_FL_EXTENSION) ||
6606 !(ergo(next, !(next->llc_flags & LCME_FL_EXTENSION))) ||
6607 lod_comp_inited(lod_comp)) {
6608 CERROR("%s: "DFID" invalid extension space components\n",
6609 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
6613 if (!prev->llc_stripe) {
6614 CDEBUG(D_LAYOUT, "Previous component not inited\n");
6615 info->lti_count = 1;
6616 info->lti_comp_idx[0] = index - 1;
6617 rc = lod_declare_instantiate_components(env, lo, th);
6618 /* ENOSPC tells us we can't use this component. If there is
6619 * a next or we are repeating, we either spill over (next) or
6620 * extend the original comp (repeat). Otherwise, return the
6621 * error to the user. */
6622 if (rc == -ENOSPC && (next || sd->sd_repeat))
6628 if (sd->sd_force == 0 && rc == 0)
6629 rc = !lod_sel_osts_allowed(env, lo, index - 1,
6630 extension_size, extent,
6631 &lod_comp->llc_extent, write);
6633 repeated = !!(sd->sd_repeat);
6637 /* Extend previous component */
6639 new_end = lod_extension_new_end(extension_size, extent->e_end,
6640 prev->llc_stripe_size,
6641 prev->llc_extent.e_end,
6642 lod_comp->llc_extent.e_end);
6644 CDEBUG(D_LAYOUT, "new end %llu\n", new_end);
6645 lod_comp->llc_extent.e_start = new_end;
6646 prev->llc_extent.e_end = new_end;
6648 if (prev->llc_extent.e_end == lod_comp->llc_extent.e_end) {
6649 CDEBUG(D_LAYOUT, "Extension component exhausted\n");
6650 lod_comp->llc_id = LCME_ID_INVAL;
6654 /* rc == 1, failed to extend current component */
6657 /* Normal 'spillover' case - Remove the extension
6658 * space component & bring down the start of the next
6660 lod_comp->llc_id = LCME_ID_INVAL;
6662 if (!(prev->llc_flags & LCME_FL_INIT)) {
6663 prev->llc_id = LCME_ID_INVAL;
6666 lod_sel_adjust_extents(env, lo, *max_comp, index);
6667 } else if (lod_comp_inited(prev)) {
6668 /* If there is no next, and the previous component is
6669 * INIT'ed, try repeating the previous component. */
6670 LASSERT(repeated == 0);
6671 rc = lod_layout_repeat_comp(env, lo, index - 1);
6675 /* The previous component is a repeated component.
6676 * Record this so we don't keep trying to repeat it. */
6679 /* If the previous component is not INIT'ed, this may
6680 * be a component we have just instantiated but failed
6681 * to extend. Or even a repeated component we failed
6682 * to prepare a striping for. Do not repeat but instead
6683 * remove the repeated component & force the extention
6684 * of the original one */
6687 prev->llc_id = LCME_ID_INVAL;
6694 rc = lod_layout_del_prep_layout(env, lo, NULL);
6697 LASSERTF(-rc == change,
6698 "number deleted %d != requested %d\n", -rc,
6701 *max_comp = *max_comp + change;
6703 /* lod_del_prep_layout reallocates ldo_comp_entries, so we must
6704 * refresh these pointers before using them */
6705 lod_comp = &lo->ldo_comp_entries[index];
6706 prev = &lo->ldo_comp_entries[index - 1];
6707 CDEBUG(D_LAYOUT, "After extent updates: prev start %llu, current start "
6708 "%llu, current end %llu max_comp %d ldo_comp_cnt %d\n",
6709 prev->llc_extent.e_start, lod_comp->llc_extent.e_start,
6710 lod_comp->llc_extent.e_end, *max_comp, lo->ldo_comp_cnt);
6712 /* Layout changed successfully */
6717 * Declare layout extent updates
6719 * Handles extensions. Identifies extension components touched by current
6720 * operation and passes them to processing function.
6722 * Restarts with updated layouts from the processing function until the current
6723 * operation no longer touches an extension space component.
6725 * \param[in] env execution environment for this thread
6726 * \param[in,out] lo object to update the layout of
6727 * \param[in] extent layout extent for requested operation, update layout to
6728 * fit this operation
6729 * \param[in] th transaction handle for this operation
6730 * \param[in] pick identifies chosen mirror for FLR layouts
6731 * \param[in] write if this is write op
6733 * \retval 1 on layout changed, 0 on no change
6734 * \retval negative errno on error
6736 static int lod_declare_update_extents(const struct lu_env *env,
6737 struct lod_object *lo, struct lu_extent *extent,
6738 struct thandle *th, int pick, int write)
6740 struct lod_thread_info *info = lod_env_info(env);
6741 struct lod_layout_component *lod_comp;
6742 bool layout_changed = false;
6743 struct sel_data sd = { 0 };
6751 /* This makes us work on the components of the chosen mirror */
6752 start_index = lo->ldo_mirrors[pick].lme_start;
6753 max_comp = lo->ldo_mirrors[pick].lme_end + 1;
6754 if (lo->ldo_flr_state == LCM_FL_NONE)
6755 LASSERT(start_index == 0 && max_comp == lo->ldo_comp_cnt);
6757 CDEBUG(D_LAYOUT, "extent->e_start %llu, extent->e_end %llu\n",
6758 extent->e_start, extent->e_end);
6759 for (i = start_index; i < max_comp; i++) {
6760 lod_comp = &lo->ldo_comp_entries[i];
6762 /* We've passed all components of interest */
6763 if (lod_comp->llc_extent.e_start >= extent->e_end)
6766 if (lod_comp->llc_flags & LCME_FL_EXTENSION) {
6767 layout_changed = true;
6768 rc = lod_sel_handler(env, lo, extent, th, &max_comp,
6773 /* Nothing has changed behind the prev one */
6779 /* We may have added or removed components. If so, we must update the
6780 * start & ends of all the mirrors after the current one, and the end
6781 * of the current mirror. */
6782 change = max_comp - 1 - lo->ldo_mirrors[pick].lme_end;
6784 lo->ldo_mirrors[pick].lme_end += change;
6785 for (i = pick + 1; i < lo->ldo_mirror_count; i++) {
6786 lo->ldo_mirrors[i].lme_start += change;
6787 lo->ldo_mirrors[i].lme_end += change;
6793 /* The amount of components has changed, adjust the lti_comp_idx */
6794 rc2 = lod_layout_data_init(info, lo->ldo_comp_cnt);
6796 return rc < 0 ? rc : rc2 < 0 ? rc2 : layout_changed;
6799 /* If striping is already instantiated or INIT'ed DOM? */
6800 static bool lod_is_instantiation_needed(struct lod_layout_component *comp)
6802 return !(((lov_pattern(comp->llc_pattern) == LOV_PATTERN_MDT) &&
6803 lod_comp_inited(comp)) || comp->llc_stripe);
6807 * Declare layout update for a non-FLR layout.
6809 * \param[in] env execution environment for this thread
6810 * \param[in,out] lo object to update the layout of
6811 * \param[in] layout layout intent for requested operation, "update" is
6812 * a process of reacting to this
6813 * \param[in] buf buffer containing lov ea (see comment on usage inline)
6814 * \param[in] th transaction handle for this operation
6816 * \retval 0 on success
6817 * \retval negative errno on error
6819 static int lod_declare_update_plain(const struct lu_env *env,
6820 struct lod_object *lo, struct layout_intent *layout,
6821 const struct lu_buf *buf, struct thandle *th)
6823 struct lod_thread_info *info = lod_env_info(env);
6824 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6825 struct lod_layout_component *lod_comp;
6826 struct lov_comp_md_v1 *comp_v1 = NULL;
6827 bool layout_changed = false;
6828 bool replay = false;
6832 LASSERT(lo->ldo_flr_state == LCM_FL_NONE);
6835 * In case the client is passing lovea, which only happens during
6836 * the replay of layout intent write RPC for now, we may need to
6837 * parse the lovea and apply new layout configuration.
6839 if (buf && buf->lb_len) {
6840 struct lov_user_md_v1 *v1 = buf->lb_buf;
6842 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
6843 v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
6844 LOV_MAGIC_COMP_V1)) {
6845 CERROR("%s: the replay buffer of layout extend "
6846 "(magic %#x) does not contain expected "
6847 "composite layout.\n",
6848 lod2obd(d)->obd_name, v1->lmm_magic);
6849 GOTO(out, rc = -EINVAL);
6852 rc = lod_use_defined_striping(env, lo, buf);
6855 lo->ldo_comp_cached = 1;
6857 rc = lod_get_lov_ea(env, lo);
6860 /* old on-disk EA is stored in info->lti_buf */
6861 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
6863 layout_changed = true;
6865 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
6869 /* non replay path */
6870 rc = lod_striping_load(env, lo);
6875 /* Make sure defined layout covers the requested write range. */
6876 lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
6877 if (lo->ldo_comp_cnt > 1 &&
6878 lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
6879 lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
6880 CDEBUG_LIMIT(replay ? D_ERROR : D_LAYOUT,
6881 "%s: the defined layout [0, %#llx) does not "
6882 "covers the write range "DEXT"\n",
6883 lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
6884 PEXT(&layout->li_extent));
6885 GOTO(out, rc = -EINVAL);
6888 CDEBUG(D_LAYOUT, "%s: "DFID": update components "DEXT"\n",
6889 lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
6890 PEXT(&layout->li_extent));
6893 rc = lod_declare_update_extents(env, lo, &layout->li_extent,
6894 th, 0, layout->li_opc == LAYOUT_INTENT_WRITE);
6898 layout_changed = true;
6902 * Iterate ld->ldo_comp_entries, find the component whose extent under
6903 * the write range and not instantianted.
6905 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6906 lod_comp = &lo->ldo_comp_entries[i];
6908 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
6912 /* If striping is instantiated or INIT'ed DOM skip */
6913 if (!lod_is_instantiation_needed(lod_comp))
6917 * In replay path, lod_comp is the EA passed by
6918 * client replay buffer, comp_v1 is the pre-recovery
6919 * on-disk EA, we'd sift out those components which
6920 * were init-ed in the on-disk EA.
6922 if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
6927 * this component hasn't instantiated in normal path, or during
6928 * replay it needs replay the instantiation.
6931 /* A released component is being extended */
6932 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
6933 GOTO(out, rc = -EINVAL);
6935 LASSERT(info->lti_comp_idx != NULL);
6936 info->lti_comp_idx[info->lti_count++] = i;
6937 layout_changed = true;
6940 if (!layout_changed)
6943 lod_obj_inc_layout_gen(lo);
6944 rc = lod_declare_instantiate_components(env, lo, th);
6948 lod_striping_free(env, lo);
6952 static inline int lod_comp_index(struct lod_object *lo,
6953 struct lod_layout_component *lod_comp)
6955 LASSERT(lod_comp >= lo->ldo_comp_entries &&
6956 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
6958 return lod_comp - lo->ldo_comp_entries;
6962 * Stale other mirrors by writing extent.
6964 static int lod_stale_components(const struct lu_env *env, struct lod_object *lo,
6965 int primary, struct lu_extent *extent,
6968 struct lod_layout_component *pri_comp, *lod_comp;
6969 struct lu_extent pri_extent;
6974 /* The writing extent decides which components in the primary
6975 * are affected... */
6976 CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
6979 lod_foreach_mirror_comp(pri_comp, lo, primary) {
6980 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
6983 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
6984 lod_comp_index(lo, pri_comp),
6985 PEXT(&pri_comp->llc_extent));
6987 pri_extent.e_start = pri_comp->llc_extent.e_start;
6988 pri_extent.e_end = pri_comp->llc_extent.e_end;
6990 for (i = 0; i < lo->ldo_mirror_count; i++) {
6993 rc = lod_declare_update_extents(env, lo, &pri_extent,
6995 /* if update_extents changed the layout, it may have
6996 * reallocated the component array, so start over to
6997 * avoid using stale pointers */
7003 /* ... and then stale other components that are
7004 * overlapping with primary components */
7005 lod_foreach_mirror_comp(lod_comp, lo, i) {
7006 if (!lu_extent_is_overlapped(
7008 &lod_comp->llc_extent))
7011 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
7012 i, lod_comp_index(lo, lod_comp));
7014 lod_comp->llc_flags |= LCME_FL_STALE;
7015 lo->ldo_mirrors[i].lme_stale = 1;
7024 * check an OST's availability
7025 * \param[in] env execution environment
7026 * \param[in] lo lod object
7027 * \param[in] dt dt object
7028 * \param[in] index mirror index
7030 * \retval negative if failed
7031 * \retval 1 if \a dt is available
7032 * \retval 0 if \a dt is not available
7034 static inline int lod_check_ost_avail(const struct lu_env *env,
7035 struct lod_object *lo,
7036 struct dt_object *dt, int index)
7038 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7039 struct lod_tgt_desc *ost;
7041 int type = LU_SEQ_RANGE_OST;
7044 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &idx, &type);
7046 CERROR("%s: can't locate "DFID":rc = %d\n",
7047 lod2obd(lod)->obd_name, PFID(lu_object_fid(&dt->do_lu)),
7052 ost = OST_TGT(lod, idx);
7053 if (ost->ltd_statfs.os_state &
7054 (OS_STATE_READONLY | OS_STATE_ENOSPC | OS_STATE_ENOINO |
7055 OS_STATE_NOPRECREATE) ||
7056 ost->ltd_active == 0) {
7057 CDEBUG(D_LAYOUT, DFID ": mirror %d OST%d unavail, rc = %d\n",
7058 PFID(lod_object_fid(lo)), index, idx, rc);
7066 * Pick primary mirror for write
7067 * \param[in] env execution environment
7068 * \param[in] lo object
7069 * \param[in] extent write range
7071 static int lod_primary_pick(const struct lu_env *env, struct lod_object *lo,
7072 struct lu_extent *extent)
7074 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7075 unsigned int seq = 0;
7076 struct lod_layout_component *lod_comp;
7078 int picked = -1, second_pick = -1, third_pick = -1;
7081 if (OBD_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
7082 get_random_bytes(&seq, sizeof(seq));
7083 seq %= lo->ldo_mirror_count;
7087 * Pick a mirror as the primary, and check the availability of OSTs.
7089 * This algo can be revised later after knowing the topology of
7092 lod_qos_statfs_update(env, lod);
7093 for (i = 0; i < lo->ldo_mirror_count; i++) {
7094 bool ost_avail = true;
7095 int index = (i + seq) % lo->ldo_mirror_count;
7097 if (lo->ldo_mirrors[index].lme_stale) {
7098 CDEBUG(D_LAYOUT, DFID": mirror %d stale\n",
7099 PFID(lod_object_fid(lo)), index);
7103 /* 2nd pick is for the primary mirror containing unavail OST */
7104 if (lo->ldo_mirrors[index].lme_primary && second_pick < 0)
7105 second_pick = index;
7107 /* 3rd pick is for non-primary mirror containing unavail OST */
7108 if (second_pick < 0 && third_pick < 0)
7112 * we found a non-primary 1st pick, we'd like to find a
7113 * potential pirmary mirror.
7115 if (picked >= 0 && !lo->ldo_mirrors[index].lme_primary)
7118 /* check the availability of OSTs */
7119 lod_foreach_mirror_comp(lod_comp, lo, index) {
7120 if (!lod_comp_inited(lod_comp) || !lod_comp->llc_stripe)
7123 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
7124 struct dt_object *dt = lod_comp->llc_stripe[j];
7126 rc = lod_check_ost_avail(env, lo, dt, index);
7133 } /* for all dt object in one component */
7136 } /* for all components in a mirror */
7139 * the OSTs where allocated objects locates in the components
7140 * of the mirror are available.
7145 /* this mirror has all OSTs available */
7149 * primary with all OSTs are available, this is the perfect
7152 if (lo->ldo_mirrors[index].lme_primary)
7154 } /* for all mirrors */
7156 /* failed to pick a sound mirror, lower our expectation */
7158 picked = second_pick;
7160 picked = third_pick;
7167 static int lod_prepare_resync_mirror(const struct lu_env *env,
7168 struct lod_object *lo,
7171 struct lod_thread_info *info = lod_env_info(env);
7172 struct lod_layout_component *lod_comp;
7173 bool neg = !!(MIRROR_ID_NEG & mirror_id);
7176 mirror_id &= ~MIRROR_ID_NEG;
7178 for (i = 0; i < lo->ldo_mirror_count; i++) {
7179 if ((!neg && lo->ldo_mirrors[i].lme_id != mirror_id) ||
7180 (neg && lo->ldo_mirrors[i].lme_id == mirror_id))
7183 lod_foreach_mirror_comp(lod_comp, lo, i) {
7184 if (lod_comp_inited(lod_comp))
7187 info->lti_comp_idx[info->lti_count++] =
7188 lod_comp_index(lo, lod_comp);
7196 * figure out the components should be instantiated for resync.
7198 static int lod_prepare_resync(const struct lu_env *env, struct lod_object *lo,
7199 struct lu_extent *extent)
7201 struct lod_thread_info *info = lod_env_info(env);
7202 struct lod_layout_component *lod_comp;
7203 unsigned int need_sync = 0;
7207 DFID": instantiate all stale components in "DEXT"\n",
7208 PFID(lod_object_fid(lo)), PEXT(extent));
7211 * instantiate all components within this extent, even non-stale
7214 for (i = 0; i < lo->ldo_mirror_count; i++) {
7215 if (!lo->ldo_mirrors[i].lme_stale)
7218 lod_foreach_mirror_comp(lod_comp, lo, i) {
7219 if (!lu_extent_is_overlapped(extent,
7220 &lod_comp->llc_extent))
7225 if (lod_comp_inited(lod_comp))
7228 CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
7229 i, lod_comp_index(lo, lod_comp));
7230 info->lti_comp_idx[info->lti_count++] =
7231 lod_comp_index(lo, lod_comp);
7235 return need_sync ? 0 : -EALREADY;
7238 static int lod_declare_update_rdonly(const struct lu_env *env,
7239 struct lod_object *lo, struct md_layout_change *mlc,
7242 struct lod_thread_info *info = lod_env_info(env);
7243 struct lu_attr *layout_attr = &info->lti_layout_attr;
7244 struct lod_layout_component *lod_comp;
7245 struct lu_extent extent = { 0 };
7249 LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
7250 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7251 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7252 LASSERT(lo->ldo_mirror_count > 0);
7254 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7255 struct layout_intent *layout = mlc->mlc_intent;
7256 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7259 extent = layout->li_extent;
7260 CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
7261 PFID(lod_object_fid(lo)), PEXT(&extent));
7263 picked = lod_primary_pick(env, lo, &extent);
7267 CDEBUG(D_LAYOUT, DFID": picked mirror id %u as primary\n",
7268 PFID(lod_object_fid(lo)),
7269 lo->ldo_mirrors[picked].lme_id);
7271 /* Update extents of primary before staling */
7272 rc = lod_declare_update_extents(env, lo, &extent, th, picked,
7277 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
7279 * trunc transfers [0, size) in the intent extent, we'd
7280 * stale components overlapping [size, eof).
7282 extent.e_start = extent.e_end;
7283 extent.e_end = OBD_OBJECT_EOF;
7286 /* stale overlapping components from other mirrors */
7287 rc = lod_stale_components(env, lo, picked, &extent, th);
7291 /* restore truncate intent extent */
7292 if (layout->li_opc == LAYOUT_INTENT_TRUNC)
7293 extent.e_end = extent.e_start;
7295 /* instantiate components for the picked mirror, start from 0 */
7298 lod_foreach_mirror_comp(lod_comp, lo, picked) {
7299 if (!lu_extent_is_overlapped(&extent,
7300 &lod_comp->llc_extent))
7303 if (!lod_is_instantiation_needed(lod_comp))
7306 info->lti_comp_idx[info->lti_count++] =
7307 lod_comp_index(lo, lod_comp);
7310 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7311 } else { /* MD_LAYOUT_RESYNC */
7315 * could contain multiple non-stale mirrors, so we need to
7316 * prep uninited all components assuming any non-stale mirror
7317 * could be picked as the primary mirror.
7319 if (mlc->mlc_mirror_id == 0) {
7321 for (i = 0; i < lo->ldo_mirror_count; i++) {
7322 if (lo->ldo_mirrors[i].lme_stale)
7325 lod_foreach_mirror_comp(lod_comp, lo, i) {
7326 if (!lod_comp_inited(lod_comp))
7330 lod_comp->llc_extent.e_end)
7332 lod_comp->llc_extent.e_end;
7335 rc = lod_prepare_resync(env, lo, &extent);
7339 /* mirror write, try to init its all components */
7340 rc = lod_prepare_resync_mirror(env, lo,
7341 mlc->mlc_mirror_id);
7346 /* change the file state to SYNC_PENDING */
7347 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7350 /* Reset the layout version once it's becoming too large.
7351 * This way it can make sure that the layout version is
7352 * monotonously increased in this writing era. */
7353 lod_obj_inc_layout_gen(lo);
7354 if (lo->ldo_layout_gen > (LCME_ID_MAX >> 1)) {
7355 __u32 layout_version;
7357 get_random_bytes(&layout_version, sizeof(layout_version));
7358 lo->ldo_layout_gen = layout_version & 0xffff;
7361 rc = lod_declare_instantiate_components(env, lo, th);
7365 layout_attr->la_valid = LA_LAYOUT_VERSION;
7366 layout_attr->la_layout_version = 0; /* set current version */
7367 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7368 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7369 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7375 lod_striping_free(env, lo);
7379 static int lod_declare_update_write_pending(const struct lu_env *env,
7380 struct lod_object *lo, struct md_layout_change *mlc,
7383 struct lod_thread_info *info = lod_env_info(env);
7384 struct lu_attr *layout_attr = &info->lti_layout_attr;
7385 struct lod_layout_component *lod_comp;
7386 struct lu_extent extent = { 0 };
7392 LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
7393 LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7394 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7396 /* look for the primary mirror */
7397 for (i = 0; i < lo->ldo_mirror_count; i++) {
7398 if (lo->ldo_mirrors[i].lme_stale)
7401 LASSERTF(primary < 0, DFID " has multiple primary: %u / %u",
7402 PFID(lod_object_fid(lo)),
7403 lo->ldo_mirrors[i].lme_id,
7404 lo->ldo_mirrors[primary].lme_id);
7409 CERROR(DFID ": doesn't have a primary mirror\n",
7410 PFID(lod_object_fid(lo)));
7411 GOTO(out, rc = -ENODATA);
7414 CDEBUG(D_LAYOUT, DFID": found primary %u\n",
7415 PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
7417 LASSERT(!lo->ldo_mirrors[primary].lme_stale);
7419 /* for LAYOUT_WRITE opc, it has to do the following operations:
7420 * 1. stale overlapping componets from stale mirrors;
7421 * 2. instantiate components of the primary mirror;
7422 * 3. transfter layout version to all objects of the primary;
7424 * for LAYOUT_RESYNC opc, it will do:
7425 * 1. instantiate components of all stale mirrors;
7426 * 2. transfer layout version to all objects to close write era. */
7428 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7429 struct layout_intent *layout = mlc->mlc_intent;
7430 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7432 LASSERT(mlc->mlc_intent != NULL);
7434 extent = mlc->mlc_intent->li_extent;
7436 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
7437 PFID(lod_object_fid(lo)), PEXT(&extent));
7439 /* 1. Update extents of primary before staling */
7440 rc = lod_declare_update_extents(env, lo, &extent, th, primary,
7445 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
7447 * trunc transfers [0, size) in the intent extent, we'd
7448 * stale components overlapping [size, eof).
7450 extent.e_start = extent.e_end;
7451 extent.e_end = OBD_OBJECT_EOF;
7454 /* 2. stale overlapping components */
7455 rc = lod_stale_components(env, lo, primary, &extent, th);
7459 /* 3. find the components which need instantiating.
7460 * instantiate [0, mlc->mlc_intent->e_end) */
7462 /* restore truncate intent extent */
7463 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC)
7464 extent.e_end = extent.e_start;
7467 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7468 if (!lu_extent_is_overlapped(&extent,
7469 &lod_comp->llc_extent))
7472 if (!lod_is_instantiation_needed(lod_comp))
7475 CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
7476 primary, lod_comp_index(lo, lod_comp));
7477 info->lti_comp_idx[info->lti_count++] =
7478 lod_comp_index(lo, lod_comp);
7480 } else { /* MD_LAYOUT_RESYNC */
7481 if (mlc->mlc_mirror_id == 0) {
7483 lod_foreach_mirror_comp(lod_comp, lo, primary) {
7484 if (!lod_comp_inited(lod_comp))
7487 extent.e_end = lod_comp->llc_extent.e_end;
7490 rc = lod_prepare_resync(env, lo, &extent);
7494 /* mirror write, try to init its all components */
7495 rc = lod_prepare_resync_mirror(env, lo,
7496 mlc->mlc_mirror_id);
7501 /* change the file state to SYNC_PENDING */
7502 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7505 rc = lod_declare_instantiate_components(env, lo, th);
7509 /* 3. transfer layout version to OST objects.
7510 * transfer new layout version to OST objects so that stale writes
7511 * can be denied. It also ends an era of writing by setting
7512 * LU_LAYOUT_RESYNC. Normal client can never use this bit to
7513 * send write RPC; only resync RPCs could do it. */
7514 layout_attr->la_valid = LA_LAYOUT_VERSION;
7515 layout_attr->la_layout_version = 0; /* set current version */
7516 if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7517 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7518 rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7522 lod_obj_inc_layout_gen(lo);
7525 lod_striping_free(env, lo);
7529 static int lod_declare_update_sync_pending(const struct lu_env *env,
7530 struct lod_object *lo, struct md_layout_change *mlc,
7533 struct lod_thread_info *info = lod_env_info(env);
7534 unsigned sync_components = 0;
7535 unsigned resync_components = 0;
7540 LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
7541 LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
7542 mlc->mlc_opc == MD_LAYOUT_WRITE);
7544 CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
7545 PFID(lod_object_fid(lo)), mlc->mlc_opc);
7547 if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7548 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
7549 PFID(lod_object_fid(lo)));
7551 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7552 return lod_declare_update_write_pending(env, lo, mlc, th);
7555 /* MD_LAYOUT_RESYNC_DONE */
7557 for (i = 0; i < lo->ldo_comp_cnt; i++) {
7558 struct lod_layout_component *lod_comp;
7561 lod_comp = &lo->ldo_comp_entries[i];
7563 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
7568 for (j = 0; j < mlc->mlc_resync_count; j++) {
7569 if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
7572 mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
7573 lod_comp->llc_flags &= ~LCME_FL_STALE;
7574 resync_components++;
7580 for (i = 0; i < mlc->mlc_resync_count; i++) {
7581 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
7584 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
7585 "or already synced\n", PFID(lod_object_fid(lo)),
7586 mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
7587 GOTO(out, rc = -EINVAL);
7590 if (!sync_components || (mlc->mlc_resync_count && !resync_components)) {
7591 CDEBUG(D_LAYOUT, DFID": no mirror in sync\n",
7592 PFID(lod_object_fid(lo)));
7594 /* tend to return an error code here to prevent
7595 * the MDT from setting SoM attribute */
7596 GOTO(out, rc = -EINVAL);
7599 CDEBUG(D_LAYOUT, DFID": synced %u resynced %u/%zu components\n",
7600 PFID(lod_object_fid(lo)),
7601 sync_components, resync_components, mlc->mlc_resync_count);
7603 lo->ldo_flr_state = LCM_FL_RDONLY;
7604 lod_obj_inc_layout_gen(lo);
7606 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
7607 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
7608 &info->lti_buf, XATTR_NAME_LOV, 0, th);
7613 lod_striping_free(env, lo);
7617 static int lod_declare_layout_change(const struct lu_env *env,
7618 struct dt_object *dt, struct md_layout_change *mlc,
7621 struct lod_thread_info *info = lod_env_info(env);
7622 struct lod_object *lo = lod_dt_obj(dt);
7626 if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
7627 dt_object_remote(dt_object_child(dt)))
7630 rc = lod_striping_load(env, lo);
7634 LASSERT(lo->ldo_comp_cnt > 0);
7636 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
7640 switch (lo->ldo_flr_state) {
7642 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
7646 rc = lod_declare_update_rdonly(env, lo, mlc, th);
7648 case LCM_FL_WRITE_PENDING:
7649 rc = lod_declare_update_write_pending(env, lo, mlc, th);
7651 case LCM_FL_SYNC_PENDING:
7652 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
7663 * Instantiate layout component objects which covers the intent write offset.
7665 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
7666 struct md_layout_change *mlc, struct thandle *th)
7668 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
7669 struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
7670 struct lod_object *lo = lod_dt_obj(dt);
7673 rc = lod_striped_create(env, dt, attr, NULL, th);
7674 if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
7675 layout_attr->la_layout_version |= lo->ldo_layout_gen;
7676 rc = lod_attr_set(env, dt, layout_attr, th);
7682 struct dt_object_operations lod_obj_ops = {
7683 .do_read_lock = lod_read_lock,
7684 .do_write_lock = lod_write_lock,
7685 .do_read_unlock = lod_read_unlock,
7686 .do_write_unlock = lod_write_unlock,
7687 .do_write_locked = lod_write_locked,
7688 .do_attr_get = lod_attr_get,
7689 .do_declare_attr_set = lod_declare_attr_set,
7690 .do_attr_set = lod_attr_set,
7691 .do_xattr_get = lod_xattr_get,
7692 .do_declare_xattr_set = lod_declare_xattr_set,
7693 .do_xattr_set = lod_xattr_set,
7694 .do_declare_xattr_del = lod_declare_xattr_del,
7695 .do_xattr_del = lod_xattr_del,
7696 .do_xattr_list = lod_xattr_list,
7697 .do_ah_init = lod_ah_init,
7698 .do_declare_create = lod_declare_create,
7699 .do_create = lod_create,
7700 .do_declare_destroy = lod_declare_destroy,
7701 .do_destroy = lod_destroy,
7702 .do_index_try = lod_index_try,
7703 .do_declare_ref_add = lod_declare_ref_add,
7704 .do_ref_add = lod_ref_add,
7705 .do_declare_ref_del = lod_declare_ref_del,
7706 .do_ref_del = lod_ref_del,
7707 .do_object_sync = lod_object_sync,
7708 .do_object_lock = lod_object_lock,
7709 .do_object_unlock = lod_object_unlock,
7710 .do_invalidate = lod_invalidate,
7711 .do_declare_layout_change = lod_declare_layout_change,
7712 .do_layout_change = lod_layout_change,
7716 * Implementation of dt_body_operations::dbo_read.
7718 * \see dt_body_operations::dbo_read() in the API description for details.
7720 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
7721 struct lu_buf *buf, loff_t *pos)
7723 struct dt_object *next = dt_object_child(dt);
7725 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
7726 S_ISLNK(dt->do_lu.lo_header->loh_attr));
7727 return next->do_body_ops->dbo_read(env, next, buf, pos);
7731 * Implementation of dt_body_operations::dbo_declare_write.
7733 * \see dt_body_operations::dbo_declare_write() in the API description
7736 static ssize_t lod_declare_write(const struct lu_env *env,
7737 struct dt_object *dt,
7738 const struct lu_buf *buf, loff_t pos,
7741 return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
7745 * Implementation of dt_body_operations::dbo_write.
7747 * \see dt_body_operations::dbo_write() in the API description for details.
7749 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
7750 const struct lu_buf *buf, loff_t *pos,
7753 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
7754 S_ISLNK(dt->do_lu.lo_header->loh_attr));
7755 return lod_sub_write(env, dt_object_child(dt), buf, pos, th);
7758 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
7759 __u64 start, __u64 end, struct thandle *th)
7761 if (dt_object_remote(dt))
7764 return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
7767 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
7768 __u64 start, __u64 end, struct thandle *th)
7770 if (dt_object_remote(dt))
7773 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
7774 return lod_sub_punch(env, dt_object_child(dt), start, end, th);
7778 * different type of files use the same body_ops because object may be created
7779 * in OUT, where there is no chance to set correct body_ops for each type, so
7780 * body_ops themselves will check file type inside, see lod_read/write/punch for
7783 const struct dt_body_operations lod_body_ops = {
7784 .dbo_read = lod_read,
7785 .dbo_declare_write = lod_declare_write,
7786 .dbo_write = lod_write,
7787 .dbo_declare_punch = lod_declare_punch,
7788 .dbo_punch = lod_punch,
7792 * Implementation of lu_object_operations::loo_object_init.
7794 * The function determines the type and the index of the target device using
7795 * sequence of the object's FID. Then passes control down to the
7796 * corresponding device:
7797 * OSD for the local objects, OSP for remote
7799 * \see lu_object_operations::loo_object_init() in the API description
7802 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
7803 const struct lu_object_conf *conf)
7805 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
7806 struct lu_device *cdev = NULL;
7807 struct lu_object *cobj;
7808 struct lod_tgt_descs *ltd = NULL;
7809 struct lod_tgt_desc *tgt;
7811 int type = LU_SEQ_RANGE_ANY;
7815 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
7819 if (type == LU_SEQ_RANGE_MDT &&
7820 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
7821 cdev = &lod->lod_child->dd_lu_dev;
7822 } else if (type == LU_SEQ_RANGE_MDT) {
7823 ltd = &lod->lod_mdt_descs;
7825 } else if (type == LU_SEQ_RANGE_OST) {
7826 ltd = &lod->lod_ost_descs;
7833 if (ltd->ltd_tgts_size > idx &&
7834 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
7835 tgt = LTD_TGT(ltd, idx);
7837 LASSERT(tgt != NULL);
7838 LASSERT(tgt->ltd_tgt != NULL);
7840 cdev = &(tgt->ltd_tgt->dd_lu_dev);
7842 lod_putref(lod, ltd);
7845 if (unlikely(cdev == NULL))
7848 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
7849 if (unlikely(cobj == NULL))
7852 lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
7854 lu_object_add(lo, cobj);
7861 * Alloc cached foreign LOV
7863 * \param[in] lo object
7864 * \param[in] size size of foreign LOV
7866 * \retval 0 on success
7867 * \retval negative if failed
7869 int lod_alloc_foreign_lov(struct lod_object *lo, size_t size)
7871 OBD_ALLOC_LARGE(lo->ldo_foreign_lov, size);
7872 if (lo->ldo_foreign_lov == NULL)
7874 lo->ldo_foreign_lov_size = size;
7875 lo->ldo_is_foreign = 1;
7881 * Free cached foreign LOV
7883 * \param[in] lo object
7885 void lod_free_foreign_lov(struct lod_object *lo)
7887 if (lo->ldo_foreign_lov != NULL)
7888 OBD_FREE_LARGE(lo->ldo_foreign_lov, lo->ldo_foreign_lov_size);
7889 lo->ldo_foreign_lov = NULL;
7890 lo->ldo_foreign_lov_size = 0;
7891 lo->ldo_is_foreign = 0;
7896 * Free cached foreign LMV
7898 * \param[in] lo object
7900 void lod_free_foreign_lmv(struct lod_object *lo)
7902 if (lo->ldo_foreign_lmv != NULL)
7903 OBD_FREE_LARGE(lo->ldo_foreign_lmv, lo->ldo_foreign_lmv_size);
7904 lo->ldo_foreign_lmv = NULL;
7905 lo->ldo_foreign_lmv_size = 0;
7906 lo->ldo_dir_is_foreign = 0;
7911 * Release resources associated with striping.
7913 * If the object is striped (regular or directory), then release
7914 * the stripe objects references and free the ldo_stripe array.
7916 * \param[in] env execution environment
7917 * \param[in] lo object
7919 void lod_striping_free_nolock(const struct lu_env *env, struct lod_object *lo)
7921 struct lod_layout_component *lod_comp;
7924 if (unlikely(lo->ldo_is_foreign)) {
7925 lod_free_foreign_lov(lo);
7926 lo->ldo_comp_cached = 0;
7927 } else if (unlikely(lo->ldo_dir_is_foreign)) {
7928 lod_free_foreign_lmv(lo);
7929 lo->ldo_dir_stripe_loaded = 0;
7930 } else if (lo->ldo_stripe != NULL) {
7931 LASSERT(lo->ldo_comp_entries == NULL);
7932 LASSERT(lo->ldo_dir_stripes_allocated > 0);
7934 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
7935 if (lo->ldo_stripe[i])
7936 dt_object_put(env, lo->ldo_stripe[i]);
7939 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
7940 OBD_FREE(lo->ldo_stripe, j);
7941 lo->ldo_stripe = NULL;
7942 lo->ldo_dir_stripes_allocated = 0;
7943 lo->ldo_dir_stripe_loaded = 0;
7944 lo->ldo_dir_stripe_count = 0;
7945 } else if (lo->ldo_comp_entries != NULL) {
7946 for (i = 0; i < lo->ldo_comp_cnt; i++) {
7947 /* free lod_layout_component::llc_stripe array */
7948 lod_comp = &lo->ldo_comp_entries[i];
7950 if (lod_comp->llc_stripe == NULL)
7952 LASSERT(lod_comp->llc_stripes_allocated != 0);
7953 for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
7954 if (lod_comp->llc_stripe[j] != NULL)
7956 &lod_comp->llc_stripe[j]->do_lu);
7958 OBD_FREE(lod_comp->llc_stripe,
7959 sizeof(struct dt_object *) *
7960 lod_comp->llc_stripes_allocated);
7961 lod_comp->llc_stripe = NULL;
7962 OBD_FREE(lod_comp->llc_ost_indices,
7964 lod_comp->llc_stripes_allocated);
7965 lod_comp->llc_ost_indices = NULL;
7966 lod_comp->llc_stripes_allocated = 0;
7968 lod_free_comp_entries(lo);
7969 lo->ldo_comp_cached = 0;
7973 void lod_striping_free(const struct lu_env *env, struct lod_object *lo)
7975 mutex_lock(&lo->ldo_layout_mutex);
7976 lod_striping_free_nolock(env, lo);
7977 mutex_unlock(&lo->ldo_layout_mutex);
7981 * Implementation of lu_object_operations::loo_object_free.
7983 * \see lu_object_operations::loo_object_free() in the API description
7986 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
7988 struct lod_object *lo = lu2lod_obj(o);
7990 /* release all underlying object pinned */
7991 lod_striping_free(env, lo);
7993 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
7997 * Implementation of lu_object_operations::loo_object_release.
7999 * \see lu_object_operations::loo_object_release() in the API description
8002 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
8004 /* XXX: shouldn't we release everything here in case if object
8005 * creation failed before? */
8009 * Implementation of lu_object_operations::loo_object_print.
8011 * \see lu_object_operations::loo_object_print() in the API description
8014 static int lod_object_print(const struct lu_env *env, void *cookie,
8015 lu_printer_t p, const struct lu_object *l)
8017 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
8019 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
8022 struct lu_object_operations lod_lu_obj_ops = {
8023 .loo_object_init = lod_object_init,
8024 .loo_object_free = lod_object_free,
8025 .loo_object_release = lod_object_release,
8026 .loo_object_print = lod_object_print,