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, 2013, 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 * lustre/doc/osd-api.txt.
38 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
41 #define DEBUG_SUBSYSTEM S_MDS
44 #include <obd_class.h>
45 #include <lustre_ver.h>
46 #include <obd_support.h>
47 #include <lprocfs_status.h>
49 #include <lustre_fid.h>
50 #include <lustre_param.h>
51 #include <lustre_fid.h>
52 #include <lustre_lmv.h>
53 #include <md_object.h>
54 #include <lustre_linkea.h>
56 #include "lod_internal.h"
58 static const char dot[] = ".";
59 static const char dotdot[] = "..";
61 extern struct kmem_cache *lod_object_kmem;
62 static const struct dt_body_operations lod_body_lnk_ops;
65 * Implementation of dt_index_operations::dio_lookup
67 * Used with regular (non-striped) objects.
69 * \see dt_index_operations::dio_lookup() in the API description for details.
71 static int lod_index_lookup(const struct lu_env *env, struct dt_object *dt,
72 struct dt_rec *rec, const struct dt_key *key,
73 struct lustre_capa *capa)
75 struct dt_object *next = dt_object_child(dt);
76 return next->do_index_ops->dio_lookup(env, next, rec, key, capa);
80 * Implementation of dt_index_operations::dio_declare_insert.
82 * Used with regular (non-striped) objects.
84 * \see dt_index_operations::dio_declare_insert() in the API description
87 static int lod_declare_index_insert(const struct lu_env *env,
89 const struct dt_rec *rec,
90 const struct dt_key *key,
91 struct thandle *handle)
93 return dt_declare_insert(env, dt_object_child(dt), rec, key, handle);
97 * Implementation of dt_index_operations::dio_insert.
99 * Used with regular (non-striped) objects
101 * \see dt_index_operations::dio_insert() in the API description for details.
103 static int lod_index_insert(const struct lu_env *env,
104 struct dt_object *dt,
105 const struct dt_rec *rec,
106 const struct dt_key *key,
108 struct lustre_capa *capa,
111 return dt_insert(env, dt_object_child(dt), rec, key, th, capa, ign);
115 * Implementation of dt_index_operations::dio_declare_delete.
117 * Used with regular (non-striped) objects.
119 * \see dt_index_operations::dio_declare_delete() in the API description
122 static int lod_declare_index_delete(const struct lu_env *env,
123 struct dt_object *dt,
124 const struct dt_key *key,
127 return dt_declare_delete(env, dt_object_child(dt), key, th);
131 * Implementation of dt_index_operations::dio_delete.
133 * Used with regular (non-striped) objects.
135 * \see dt_index_operations::dio_delete() in the API description for details.
137 static int lod_index_delete(const struct lu_env *env,
138 struct dt_object *dt,
139 const struct dt_key *key,
141 struct lustre_capa *capa)
143 return dt_delete(env, dt_object_child(dt), key, th, capa);
147 * Implementation of dt_it_ops::init.
149 * Used with regular (non-striped) objects.
151 * \see dt_it_ops::init() in the API description for details.
153 static struct dt_it *lod_it_init(const struct lu_env *env,
154 struct dt_object *dt, __u32 attr,
155 struct lustre_capa *capa)
157 struct dt_object *next = dt_object_child(dt);
158 struct lod_it *it = &lod_env_info(env)->lti_it;
159 struct dt_it *it_next;
162 it_next = next->do_index_ops->dio_it.init(env, next, attr, capa);
166 /* currently we do not use more than one iterator per thread
167 * so we store it in thread info. if at some point we need
168 * more active iterators in a single thread, we can allocate
170 LASSERT(it->lit_obj == NULL);
172 it->lit_it = it_next;
175 return (struct dt_it *)it;
178 #define LOD_CHECK_IT(env, it) \
180 LASSERT((it)->lit_obj != NULL); \
181 LASSERT((it)->lit_it != NULL); \
185 * Implementation of dt_index_operations::dio_it.fini.
187 * Used with regular (non-striped) objects.
189 * \see dt_index_operations::dio_it.fini() in the API description for details.
191 void lod_it_fini(const struct lu_env *env, struct dt_it *di)
193 struct lod_it *it = (struct lod_it *)di;
195 LOD_CHECK_IT(env, it);
196 it->lit_obj->do_index_ops->dio_it.fini(env, it->lit_it);
198 /* the iterator not in use any more */
204 * Implementation of dt_it_ops::get.
206 * Used with regular (non-striped) objects.
208 * \see dt_it_ops::get() in the API description for details.
210 int lod_it_get(const struct lu_env *env, struct dt_it *di,
211 const struct dt_key *key)
213 const struct lod_it *it = (const struct lod_it *)di;
215 LOD_CHECK_IT(env, it);
216 return it->lit_obj->do_index_ops->dio_it.get(env, it->lit_it, key);
220 * Implementation of dt_it_ops::put.
222 * Used with regular (non-striped) objects.
224 * \see dt_it_ops::put() in the API description for details.
226 void lod_it_put(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.put(env, it->lit_it);
235 * Implementation of dt_it_ops::next.
237 * Used with regular (non-striped) objects
239 * \see dt_it_ops::next() in the API description for details.
241 int lod_it_next(const struct lu_env *env, struct dt_it *di)
243 struct lod_it *it = (struct lod_it *)di;
245 LOD_CHECK_IT(env, it);
246 return it->lit_obj->do_index_ops->dio_it.next(env, it->lit_it);
250 * Implementation of dt_it_ops::key.
252 * Used with regular (non-striped) objects.
254 * \see dt_it_ops::key() in the API description for details.
256 struct dt_key *lod_it_key(const struct lu_env *env, const struct dt_it *di)
258 const struct lod_it *it = (const struct lod_it *)di;
260 LOD_CHECK_IT(env, it);
261 return it->lit_obj->do_index_ops->dio_it.key(env, it->lit_it);
265 * Implementation of dt_it_ops::key_size.
267 * Used with regular (non-striped) objects.
269 * \see dt_it_ops::key_size() in the API description for details.
271 int lod_it_key_size(const struct lu_env *env, const struct dt_it *di)
273 struct lod_it *it = (struct lod_it *)di;
275 LOD_CHECK_IT(env, it);
276 return it->lit_obj->do_index_ops->dio_it.key_size(env, it->lit_it);
280 * Implementation of dt_it_ops::rec.
282 * Used with regular (non-striped) objects.
284 * \see dt_it_ops::rec() in the API description for details.
286 int lod_it_rec(const struct lu_env *env, const struct dt_it *di,
287 struct dt_rec *rec, __u32 attr)
289 const struct lod_it *it = (const struct lod_it *)di;
291 LOD_CHECK_IT(env, it);
292 return it->lit_obj->do_index_ops->dio_it.rec(env, it->lit_it, rec,
297 * Implementation of dt_it_ops::rec_size.
299 * Used with regular (non-striped) objects.
301 * \see dt_it_ops::rec_size() in the API description for details.
303 int lod_it_rec_size(const struct lu_env *env, const struct dt_it *di,
306 const struct lod_it *it = (const struct lod_it *)di;
308 LOD_CHECK_IT(env, it);
309 return it->lit_obj->do_index_ops->dio_it.rec_size(env, it->lit_it,
314 * Implementation of dt_it_ops::store.
316 * Used with regular (non-striped) objects.
318 * \see dt_it_ops::store() in the API description for details.
320 __u64 lod_it_store(const struct lu_env *env, const struct dt_it *di)
322 const struct lod_it *it = (const struct lod_it *)di;
324 LOD_CHECK_IT(env, it);
325 return it->lit_obj->do_index_ops->dio_it.store(env, it->lit_it);
329 * Implementation of dt_it_ops::load.
331 * Used with regular (non-striped) objects.
333 * \see dt_it_ops::load() in the API description for details.
335 int lod_it_load(const struct lu_env *env, const struct dt_it *di, __u64 hash)
337 const struct lod_it *it = (const struct lod_it *)di;
339 LOD_CHECK_IT(env, it);
340 return it->lit_obj->do_index_ops->dio_it.load(env, it->lit_it, hash);
344 * Implementation of dt_it_ops::key_rec.
346 * Used with regular (non-striped) objects.
348 * \see dt_it_ops::rec() in the API description for details.
350 int lod_it_key_rec(const struct lu_env *env, const struct dt_it *di,
353 const struct lod_it *it = (const struct lod_it *)di;
355 LOD_CHECK_IT(env, it);
356 return it->lit_obj->do_index_ops->dio_it.key_rec(env, it->lit_it,
360 static struct dt_index_operations lod_index_ops = {
361 .dio_lookup = lod_index_lookup,
362 .dio_declare_insert = lod_declare_index_insert,
363 .dio_insert = lod_index_insert,
364 .dio_declare_delete = lod_declare_index_delete,
365 .dio_delete = lod_index_delete,
373 .key_size = lod_it_key_size,
375 .rec_size = lod_it_rec_size,
376 .store = lod_it_store,
378 .key_rec = lod_it_key_rec,
383 * Implementation of dt_it_ops::init.
385 * Used with striped objects. Internally just initializes the iterator
386 * on the first stripe.
388 * \see dt_it_ops::init() in the API description for details.
390 static struct dt_it *lod_striped_it_init(const struct lu_env *env,
391 struct dt_object *dt, __u32 attr,
392 struct lustre_capa *capa)
394 struct lod_object *lo = lod_dt_obj(dt);
395 struct dt_object *next;
396 struct lod_it *it = &lod_env_info(env)->lti_it;
397 struct dt_it *it_next;
400 LASSERT(lo->ldo_stripenr > 0);
401 next = lo->ldo_stripe[0];
402 LASSERT(next != NULL);
403 LASSERT(next->do_index_ops != NULL);
405 it_next = next->do_index_ops->dio_it.init(env, next, attr, capa);
409 /* currently we do not use more than one iterator per thread
410 * so we store it in thread info. if at some point we need
411 * more active iterators in a single thread, we can allocate
413 LASSERT(it->lit_obj == NULL);
415 it->lit_stripe_index = 0;
417 it->lit_it = it_next;
420 return (struct dt_it *)it;
423 #define LOD_CHECK_STRIPED_IT(env, it, lo) \
425 LASSERT((it)->lit_obj != NULL); \
426 LASSERT((it)->lit_it != NULL); \
427 LASSERT((lo)->ldo_stripenr > 0); \
428 LASSERT((it)->lit_stripe_index < (lo)->ldo_stripenr); \
432 * Implementation of dt_it_ops::fini.
434 * Used with striped objects.
436 * \see dt_it_ops::fini() in the API description for details.
438 static void lod_striped_it_fini(const struct lu_env *env, struct dt_it *di)
440 struct lod_it *it = (struct lod_it *)di;
441 struct lod_object *lo = lod_dt_obj(it->lit_obj);
442 struct dt_object *next;
444 LOD_CHECK_STRIPED_IT(env, it, lo);
446 next = lo->ldo_stripe[it->lit_stripe_index];
447 LASSERT(next != NULL);
448 LASSERT(next->do_index_ops != NULL);
450 next->do_index_ops->dio_it.fini(env, it->lit_it);
452 /* the iterator not in use any more */
455 it->lit_stripe_index = 0;
459 * Implementation of dt_it_ops::get.
461 * Right now it's not used widely, only to reset the iterator to the
462 * initial position. It should be possible to implement a full version
463 * which chooses a correct stripe to be able to position with any key.
465 * \see dt_it_ops::get() in the API description for details.
467 static int lod_striped_it_get(const struct lu_env *env, struct dt_it *di,
468 const struct dt_key *key)
470 const struct lod_it *it = (const struct lod_it *)di;
471 struct lod_object *lo = lod_dt_obj(it->lit_obj);
472 struct dt_object *next;
475 LOD_CHECK_STRIPED_IT(env, it, lo);
477 next = lo->ldo_stripe[it->lit_stripe_index];
478 LASSERT(next != NULL);
479 LASSERT(next->do_index_ops != NULL);
481 return next->do_index_ops->dio_it.get(env, it->lit_it, key);
485 * Implementation of dt_it_ops::put.
487 * Used with striped objects.
489 * \see dt_it_ops::put() in the API description for details.
491 static void lod_striped_it_put(const struct lu_env *env, struct dt_it *di)
493 struct lod_it *it = (struct lod_it *)di;
494 struct lod_object *lo = lod_dt_obj(it->lit_obj);
495 struct dt_object *next;
497 LOD_CHECK_STRIPED_IT(env, it, lo);
499 next = lo->ldo_stripe[it->lit_stripe_index];
500 LASSERT(next != NULL);
501 LASSERT(next->do_index_ops != NULL);
503 return next->do_index_ops->dio_it.put(env, it->lit_it);
507 * Implementation of dt_it_ops::next.
509 * Used with striped objects. When the end of the current stripe is
510 * reached, the method takes the next stripe's iterator.
512 * \see dt_it_ops::next() in the API description for details.
514 static int lod_striped_it_next(const struct lu_env *env, struct dt_it *di)
516 struct lod_it *it = (struct lod_it *)di;
517 struct lod_object *lo = lod_dt_obj(it->lit_obj);
518 struct dt_object *next;
519 struct dt_it *it_next;
523 LOD_CHECK_STRIPED_IT(env, it, lo);
525 next = lo->ldo_stripe[it->lit_stripe_index];
526 LASSERT(next != NULL);
527 LASSERT(next->do_index_ops != NULL);
529 rc = next->do_index_ops->dio_it.next(env, it->lit_it);
533 if (rc == 0 && it->lit_stripe_index == 0)
536 if (rc == 0 && it->lit_stripe_index > 0) {
537 struct lu_dirent *ent;
539 ent = (struct lu_dirent *)lod_env_info(env)->lti_key;
541 rc = next->do_index_ops->dio_it.rec(env, it->lit_it,
542 (struct dt_rec *)ent,
547 /* skip . and .. for slave stripe */
548 if ((strncmp(ent->lde_name, ".",
549 le16_to_cpu(ent->lde_namelen)) == 0 &&
550 le16_to_cpu(ent->lde_namelen) == 1) ||
551 (strncmp(ent->lde_name, "..",
552 le16_to_cpu(ent->lde_namelen)) == 0 &&
553 le16_to_cpu(ent->lde_namelen) == 2))
559 /* go to next stripe */
560 if (it->lit_stripe_index + 1 >= lo->ldo_stripenr)
563 it->lit_stripe_index++;
565 next->do_index_ops->dio_it.put(env, it->lit_it);
566 next->do_index_ops->dio_it.fini(env, it->lit_it);
568 rc = next->do_ops->do_index_try(env, next, &dt_directory_features);
572 next = lo->ldo_stripe[it->lit_stripe_index];
573 LASSERT(next != NULL);
574 LASSERT(next->do_index_ops != NULL);
576 it_next = next->do_index_ops->dio_it.init(env, next, it->lit_attr,
578 if (!IS_ERR(it_next)) {
579 it->lit_it = it_next;
582 rc = PTR_ERR(it_next);
589 * Implementation of dt_it_ops::key.
591 * Used with striped objects.
593 * \see dt_it_ops::key() in the API description for details.
595 static struct dt_key *lod_striped_it_key(const struct lu_env *env,
596 const struct dt_it *di)
598 const struct lod_it *it = (const struct lod_it *)di;
599 struct lod_object *lo = lod_dt_obj(it->lit_obj);
600 struct dt_object *next;
602 LOD_CHECK_STRIPED_IT(env, it, lo);
604 next = lo->ldo_stripe[it->lit_stripe_index];
605 LASSERT(next != NULL);
606 LASSERT(next->do_index_ops != NULL);
608 return next->do_index_ops->dio_it.key(env, it->lit_it);
612 * Implementation of dt_it_ops::key_size.
614 * Used with striped objects.
616 * \see dt_it_ops::size() in the API description for details.
618 static int lod_striped_it_key_size(const struct lu_env *env,
619 const struct dt_it *di)
621 struct lod_it *it = (struct lod_it *)di;
622 struct lod_object *lo = lod_dt_obj(it->lit_obj);
623 struct dt_object *next;
625 LOD_CHECK_STRIPED_IT(env, it, lo);
627 next = lo->ldo_stripe[it->lit_stripe_index];
628 LASSERT(next != NULL);
629 LASSERT(next->do_index_ops != NULL);
631 return next->do_index_ops->dio_it.key_size(env, it->lit_it);
635 * Implementation of dt_it_ops::rec.
637 * Used with striped objects.
639 * \see dt_it_ops::rec() in the API description for details.
641 static int lod_striped_it_rec(const struct lu_env *env, const struct dt_it *di,
642 struct dt_rec *rec, __u32 attr)
644 const struct lod_it *it = (const struct lod_it *)di;
645 struct lod_object *lo = lod_dt_obj(it->lit_obj);
646 struct dt_object *next;
648 LOD_CHECK_STRIPED_IT(env, it, lo);
650 next = lo->ldo_stripe[it->lit_stripe_index];
651 LASSERT(next != NULL);
652 LASSERT(next->do_index_ops != NULL);
654 return next->do_index_ops->dio_it.rec(env, it->lit_it, rec, attr);
658 * Implementation of dt_it_ops::rec_size.
660 * Used with striped objects.
662 * \see dt_it_ops::rec_size() in the API description for details.
664 static int lod_striped_it_rec_size(const struct lu_env *env,
665 const struct dt_it *di, __u32 attr)
667 struct lod_it *it = (struct lod_it *)di;
668 struct lod_object *lo = lod_dt_obj(it->lit_obj);
669 struct dt_object *next;
671 LOD_CHECK_STRIPED_IT(env, it, lo);
673 next = lo->ldo_stripe[it->lit_stripe_index];
674 LASSERT(next != NULL);
675 LASSERT(next->do_index_ops != NULL);
677 return next->do_index_ops->dio_it.rec_size(env, it->lit_it, attr);
681 * Implementation of dt_it_ops::store.
683 * Used with striped objects.
685 * \see dt_it_ops::store() in the API description for details.
687 static __u64 lod_striped_it_store(const struct lu_env *env,
688 const struct dt_it *di)
690 const struct lod_it *it = (const struct lod_it *)di;
691 struct lod_object *lo = lod_dt_obj(it->lit_obj);
692 struct dt_object *next;
694 LOD_CHECK_STRIPED_IT(env, it, lo);
696 next = lo->ldo_stripe[it->lit_stripe_index];
697 LASSERT(next != NULL);
698 LASSERT(next->do_index_ops != NULL);
700 return next->do_index_ops->dio_it.store(env, it->lit_it);
704 * Implementation of dt_it_ops::load.
706 * Used with striped objects.
708 * \see dt_it_ops::load() in the API description for details.
710 static int lod_striped_it_load(const struct lu_env *env,
711 const struct dt_it *di, __u64 hash)
713 const struct lod_it *it = (const struct lod_it *)di;
714 struct lod_object *lo = lod_dt_obj(it->lit_obj);
715 struct dt_object *next;
717 LOD_CHECK_STRIPED_IT(env, it, lo);
719 next = lo->ldo_stripe[it->lit_stripe_index];
720 LASSERT(next != NULL);
721 LASSERT(next->do_index_ops != NULL);
723 return next->do_index_ops->dio_it.load(env, it->lit_it, hash);
726 static struct dt_index_operations lod_striped_index_ops = {
727 .dio_lookup = lod_index_lookup,
728 .dio_declare_insert = lod_declare_index_insert,
729 .dio_insert = lod_index_insert,
730 .dio_declare_delete = lod_declare_index_delete,
731 .dio_delete = lod_index_delete,
733 .init = lod_striped_it_init,
734 .fini = lod_striped_it_fini,
735 .get = lod_striped_it_get,
736 .put = lod_striped_it_put,
737 .next = lod_striped_it_next,
738 .key = lod_striped_it_key,
739 .key_size = lod_striped_it_key_size,
740 .rec = lod_striped_it_rec,
741 .rec_size = lod_striped_it_rec_size,
742 .store = lod_striped_it_store,
743 .load = lod_striped_it_load,
748 * Append the FID for each shard of the striped directory after the
749 * given LMV EA header.
751 * To simplify striped directory and the consistency verification,
752 * we only store the LMV EA header on disk, for both master object
753 * and slave objects. When someone wants to know the whole LMV EA,
754 * such as client readdir(), we can build the entrie LMV EA on the
755 * MDT side (in RAM) via iterating the sub-directory entries that
756 * are contained in the master object of the stripe directory.
758 * For the master object of the striped directroy, the valid name
759 * for each shard is composed of the ${shard_FID}:${shard_idx}.
761 * There may be holes in the LMV EA if some shards' name entries
762 * are corrupted or lost.
764 * \param[in] env pointer to the thread context
765 * \param[in] lo pointer to the master object of the striped directory
766 * \param[in] buf pointer to the lu_buf which will hold the LMV EA
767 * \param[in] resize whether re-allocate the buffer if it is not big enough
769 * \retval positive size of the LMV EA
770 * \retval 0 for nothing to be loaded
771 * \retval negative error number on failure
773 int lod_load_lmv_shards(const struct lu_env *env, struct lod_object *lo,
774 struct lu_buf *buf, bool resize)
776 struct lu_dirent *ent =
777 (struct lu_dirent *)lod_env_info(env)->lti_key;
778 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
779 struct dt_object *obj = dt_object_child(&lo->ldo_obj);
780 struct lmv_mds_md_v1 *lmv1 = buf->lb_buf;
782 const struct dt_it_ops *iops;
784 __u32 magic = le32_to_cpu(lmv1->lmv_magic);
789 /* If it is not a striped directory, then load nothing. */
790 if (magic != LMV_MAGIC_V1)
793 /* If it is in migration (or failure), then load nothing. */
794 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
797 stripes = le32_to_cpu(lmv1->lmv_stripe_count);
801 rc = lmv_mds_md_size(stripes, magic);
805 if (buf->lb_len < lmv1_size) {
814 lu_buf_alloc(buf, lmv1_size);
819 memcpy(buf->lb_buf, tbuf.lb_buf, tbuf.lb_len);
822 if (unlikely(!dt_try_as_dir(env, obj)))
825 memset(&lmv1->lmv_stripe_fids[0], 0, stripes * sizeof(struct lu_fid));
826 iops = &obj->do_index_ops->dio_it;
827 it = iops->init(env, obj, LUDA_64BITHASH, BYPASS_CAPA);
831 rc = iops->load(env, it, 0);
833 rc = iops->next(env, it);
838 char name[FID_LEN + 2] = "";
843 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
849 fid_le_to_cpu(&fid, &ent->lde_fid);
850 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
851 if (ent->lde_name[0] == '.') {
852 if (ent->lde_namelen == 1)
855 if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
859 len = snprintf(name, FID_LEN + 1, DFID":", PFID(&ent->lde_fid));
860 /* The ent->lde_name is composed of ${FID}:${index} */
861 if (ent->lde_namelen < len + 1 ||
862 memcmp(ent->lde_name, name, len) != 0) {
863 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
864 "%s: invalid shard name %.*s with the FID "DFID
865 " for the striped directory "DFID", %s\n",
866 lod2obd(lod)->obd_name, ent->lde_namelen,
867 ent->lde_name, PFID(&fid),
868 PFID(lu_object_fid(&obj->do_lu)),
869 lod->lod_lmv_failout ? "failout" : "skip");
871 if (lod->lod_lmv_failout)
879 if (ent->lde_name[len] < '0' ||
880 ent->lde_name[len] > '9') {
881 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
882 "%s: invalid shard name %.*s with the "
883 "FID "DFID" for the striped directory "
885 lod2obd(lod)->obd_name, ent->lde_namelen,
886 ent->lde_name, PFID(&fid),
887 PFID(lu_object_fid(&obj->do_lu)),
888 lod->lod_lmv_failout ?
891 if (lod->lod_lmv_failout)
897 index = index * 10 + ent->lde_name[len++] - '0';
898 } while (len < ent->lde_namelen);
900 if (len == ent->lde_namelen) {
901 /* Out of LMV EA range. */
902 if (index >= stripes) {
903 CERROR("%s: the shard %.*s for the striped "
904 "directory "DFID" is out of the known "
905 "LMV EA range [0 - %u], failout\n",
906 lod2obd(lod)->obd_name, ent->lde_namelen,
908 PFID(lu_object_fid(&obj->do_lu)),
914 /* The slot has been occupied. */
915 if (!fid_is_zero(&lmv1->lmv_stripe_fids[index])) {
919 &lmv1->lmv_stripe_fids[index]);
920 CERROR("%s: both the shard "DFID" and "DFID
921 " for the striped directory "DFID
922 " claim the same LMV EA slot at the "
923 "index %d, failout\n",
924 lod2obd(lod)->obd_name,
925 PFID(&fid0), PFID(&fid),
926 PFID(lu_object_fid(&obj->do_lu)), index);
931 /* stored as LE mode */
932 lmv1->lmv_stripe_fids[index] = ent->lde_fid;
935 rc = iops->next(env, it);
942 RETURN(rc > 0 ? lmv_mds_md_size(stripes, magic) : rc);
946 * Implementation of dt_object_operations::do_index_try.
948 * \see dt_object_operations::do_index_try() in the API description for details.
950 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
951 const struct dt_index_features *feat)
953 struct lod_object *lo = lod_dt_obj(dt);
954 struct dt_object *next = dt_object_child(dt);
958 LASSERT(next->do_ops);
959 LASSERT(next->do_ops->do_index_try);
961 rc = lod_load_striping_locked(env, lo);
965 rc = next->do_ops->do_index_try(env, next, feat);
969 if (lo->ldo_stripenr > 0) {
972 for (i = 0; i < lo->ldo_stripenr; i++) {
973 if (dt_object_exists(lo->ldo_stripe[i]) == 0)
975 rc = lo->ldo_stripe[i]->do_ops->do_index_try(env,
976 lo->ldo_stripe[i], feat);
980 dt->do_index_ops = &lod_striped_index_ops;
982 dt->do_index_ops = &lod_index_ops;
989 * Implementation of dt_object_operations::do_read_lock.
991 * \see dt_object_operations::do_read_lock() in the API description for details.
993 static void lod_object_read_lock(const struct lu_env *env,
994 struct dt_object *dt, unsigned role)
996 dt_read_lock(env, dt_object_child(dt), role);
1000 * Implementation of dt_object_operations::do_write_lock.
1002 * \see dt_object_operations::do_write_lock() in the API description for
1005 static void lod_object_write_lock(const struct lu_env *env,
1006 struct dt_object *dt, unsigned role)
1008 dt_write_lock(env, dt_object_child(dt), role);
1012 * Implementation of dt_object_operations::do_read_unlock.
1014 * \see dt_object_operations::do_read_unlock() in the API description for
1017 static void lod_object_read_unlock(const struct lu_env *env,
1018 struct dt_object *dt)
1020 dt_read_unlock(env, dt_object_child(dt));
1024 * Implementation of dt_object_operations::do_write_unlock.
1026 * \see dt_object_operations::do_write_unlock() in the API description for
1029 static void lod_object_write_unlock(const struct lu_env *env,
1030 struct dt_object *dt)
1032 dt_write_unlock(env, dt_object_child(dt));
1036 * Implementation of dt_object_operations::do_write_locked.
1038 * \see dt_object_operations::do_write_locked() in the API description for
1041 static int lod_object_write_locked(const struct lu_env *env,
1042 struct dt_object *dt)
1044 return dt_write_locked(env, dt_object_child(dt));
1048 * Implementation of dt_object_operations::do_attr_get.
1050 * \see dt_object_operations::do_attr_get() in the API description for details.
1052 static int lod_attr_get(const struct lu_env *env,
1053 struct dt_object *dt,
1054 struct lu_attr *attr,
1055 struct lustre_capa *capa)
1057 /* Note: for striped directory, client will merge attributes
1058 * from all of the sub-stripes see lmv_merge_attr(), and there
1059 * no MDD logic depend on directory nlink/size/time, so we can
1060 * always use master inode nlink and size for now. */
1061 return dt_attr_get(env, dt_object_child(dt), attr, capa);
1065 * Mark all of the striped directory sub-stripes dead.
1067 * When a striped object is a subject to removal, we have
1068 * to mark all the stripes to prevent further access to
1069 * them (e.g. create a new file in those). So we mark
1070 * all the stripes with LMV_HASH_FLAG_DEAD. The function
1071 * can be used to declare the changes and to apply them.
1072 * If the object isn't striped, then just return success.
1074 * \param[in] env execution environment
1075 * \param[in] dt the striped object
1076 * \param[in] handle transaction handle
1077 * \param[in] declare whether to declare the change or apply
1079 * \retval 0 on success
1080 * \retval negative if failed
1082 static int lod_mark_dead_object(const struct lu_env *env,
1083 struct dt_object *dt,
1084 struct thandle *handle,
1087 struct lod_object *lo = lod_dt_obj(dt);
1088 struct lmv_mds_md_v1 *lmv;
1089 __u32 dead_hash_type;
1095 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
1098 rc = lod_load_striping_locked(env, lo);
1102 if (lo->ldo_stripenr == 0)
1105 rc = lod_get_lmv_ea(env, lo);
1109 lmv = lod_env_info(env)->lti_ea_store;
1110 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1111 dead_hash_type = le32_to_cpu(lmv->lmv_hash_type) | LMV_HASH_FLAG_DEAD;
1112 lmv->lmv_hash_type = cpu_to_le32(dead_hash_type);
1113 for (i = 0; i < lo->ldo_stripenr; i++) {
1116 lmv->lmv_master_mdt_index = i;
1118 buf.lb_len = sizeof(*lmv);
1120 rc = dt_declare_xattr_set(env, lo->ldo_stripe[i], &buf,
1122 LU_XATTR_REPLACE, handle);
1124 rc = dt_xattr_set(env, lo->ldo_stripe[i], &buf,
1125 XATTR_NAME_LMV, LU_XATTR_REPLACE,
1126 handle, BYPASS_CAPA);
1136 * Implementation of dt_object_operations::do_declare_attr_set.
1138 * If the object is striped, then apply the changes to all the stripes.
1140 * \see dt_object_operations::do_declare_attr_set() in the API description
1143 static int lod_declare_attr_set(const struct lu_env *env,
1144 struct dt_object *dt,
1145 const struct lu_attr *attr,
1146 struct thandle *handle)
1148 struct dt_object *next = dt_object_child(dt);
1149 struct lod_object *lo = lod_dt_obj(dt);
1153 /* Set dead object on all other stripes */
1154 if (attr->la_valid & LA_FLAGS && !(attr->la_valid & ~LA_FLAGS) &&
1155 attr->la_flags & LUSTRE_SLAVE_DEAD_FL) {
1156 rc = lod_mark_dead_object(env, dt, handle, true);
1161 * declare setattr on the local object
1163 rc = dt_declare_attr_set(env, next, attr, handle);
1167 /* osp_declare_attr_set() ignores all attributes other than
1168 * UID, GID, and size, and osp_attr_set() ignores all but UID
1169 * and GID. Declaration of size attr setting happens through
1170 * lod_declare_init_size(), and not through this function.
1171 * Therefore we need not load striping unless ownership is
1172 * changing. This should save memory and (we hope) speed up
1174 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1175 if (!(attr->la_valid & (LA_UID | LA_GID)))
1178 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1181 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE |
1182 LA_ATIME | LA_MTIME | LA_CTIME)))
1186 * load striping information, notice we don't do this when object
1187 * is being initialized as we don't need this information till
1188 * few specific cases like destroy, chown
1190 rc = lod_load_striping(env, lo);
1194 if (lo->ldo_stripenr == 0)
1198 * if object is striped declare changes on the stripes
1200 LASSERT(lo->ldo_stripe);
1201 for (i = 0; i < lo->ldo_stripenr; i++) {
1202 if (likely(lo->ldo_stripe[i] != NULL)) {
1203 rc = dt_declare_attr_set(env, lo->ldo_stripe[i], attr,
1206 CERROR("failed declaration: %d\n", rc);
1212 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE) &&
1213 dt_object_exists(next) != 0 &&
1214 dt_object_remote(next) == 0)
1215 dt_declare_xattr_del(env, next, XATTR_NAME_LOV, handle);
1217 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) &&
1218 dt_object_exists(next) &&
1219 dt_object_remote(next) == 0 && S_ISREG(attr->la_mode)) {
1220 struct lod_thread_info *info = lod_env_info(env);
1221 struct lu_buf *buf = &info->lti_buf;
1223 buf->lb_buf = info->lti_ea_store;
1224 buf->lb_len = info->lti_ea_store_size;
1225 dt_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1226 LU_XATTR_REPLACE, handle);
1233 * Implementation of dt_object_operations::do_attr_set.
1235 * If the object is striped, then apply the changes to all or subset of
1236 * the stripes depending on the object type and specific attributes.
1238 * \see dt_object_operations::do_attr_set() in the API description for details.
1240 static int lod_attr_set(const struct lu_env *env,
1241 struct dt_object *dt,
1242 const struct lu_attr *attr,
1243 struct thandle *handle,
1244 struct lustre_capa *capa)
1246 struct dt_object *next = dt_object_child(dt);
1247 struct lod_object *lo = lod_dt_obj(dt);
1251 /* Set dead object on all other stripes */
1252 if (attr->la_valid & LA_FLAGS && !(attr->la_valid & ~LA_FLAGS) &&
1253 attr->la_flags & LUSTRE_SLAVE_DEAD_FL) {
1254 rc = lod_mark_dead_object(env, dt, handle, false);
1259 * apply changes to the local object
1261 rc = dt_attr_set(env, next, attr, handle, capa);
1265 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1266 if (!(attr->la_valid & (LA_UID | LA_GID)))
1269 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1272 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE |
1273 LA_ATIME | LA_MTIME | LA_CTIME)))
1277 if (lo->ldo_stripenr == 0)
1281 * if object is striped, apply changes to all the stripes
1283 LASSERT(lo->ldo_stripe);
1284 for (i = 0; i < lo->ldo_stripenr; i++) {
1285 if (unlikely(lo->ldo_stripe[i] == NULL))
1287 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
1288 (dt_object_exists(lo->ldo_stripe[i]) == 0))
1291 rc = dt_attr_set(env, lo->ldo_stripe[i], attr, handle, capa);
1293 CERROR("failed declaration: %d\n", rc);
1298 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE) &&
1299 dt_object_exists(next) != 0 &&
1300 dt_object_remote(next) == 0)
1301 dt_xattr_del(env, next, XATTR_NAME_LOV, handle, BYPASS_CAPA);
1303 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) &&
1304 dt_object_exists(next) &&
1305 dt_object_remote(next) == 0 && S_ISREG(attr->la_mode)) {
1306 struct lod_thread_info *info = lod_env_info(env);
1307 struct lu_buf *buf = &info->lti_buf;
1308 struct ost_id *oi = &info->lti_ostid;
1309 struct lu_fid *fid = &info->lti_fid;
1310 struct lov_mds_md_v1 *lmm;
1311 struct lov_ost_data_v1 *objs;
1315 rc1 = lod_get_lov_ea(env, lo);
1319 buf->lb_buf = info->lti_ea_store;
1320 buf->lb_len = info->lti_ea_store_size;
1321 lmm = info->lti_ea_store;
1322 magic = le32_to_cpu(lmm->lmm_magic);
1323 if (magic == LOV_MAGIC_V1)
1324 objs = &(lmm->lmm_objects[0]);
1326 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1327 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1328 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1330 fid_to_ostid(fid, oi);
1331 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1332 dt_xattr_set(env, next, buf, XATTR_NAME_LOV,
1333 LU_XATTR_REPLACE, handle, BYPASS_CAPA);
1340 * Implementation of dt_object_operations::do_xattr_get.
1342 * If LOV EA is requested from the root object and it's not
1343 * found, then return default striping for the filesystem.
1345 * \see dt_object_operations::do_xattr_get() in the API description for details.
1347 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1348 struct lu_buf *buf, const char *name,
1349 struct lustre_capa *capa)
1351 struct lod_thread_info *info = lod_env_info(env);
1352 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1356 rc = dt_xattr_get(env, dt_object_child(dt), buf, name, capa);
1357 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1358 struct lmv_mds_md_v1 *lmv1;
1361 if (rc > (typeof(rc))sizeof(*lmv1))
1364 if (rc < (typeof(rc))sizeof(*lmv1))
1365 RETURN(rc = rc > 0 ? -EINVAL : rc);
1367 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1368 CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1370 info->lti_buf.lb_buf = info->lti_key;
1371 info->lti_buf.lb_len = sizeof(*lmv1);
1372 rc = dt_xattr_get(env, dt_object_child(dt),
1373 &info->lti_buf, name, capa);
1374 if (unlikely(rc != sizeof(*lmv1)))
1375 RETURN(rc = rc > 0 ? -EINVAL : rc);
1377 lmv1 = info->lti_buf.lb_buf;
1378 /* The on-disk LMV EA only contains header, but the
1379 * returned LMV EA size should contain the space for
1380 * the FIDs of all shards of the striped directory. */
1381 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1382 rc = lmv_mds_md_size(
1383 le32_to_cpu(lmv1->lmv_stripe_count),
1386 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1390 RETURN(rc = rc1 != 0 ? rc1 : rc);
1393 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1397 * lod returns default striping on the real root of the device
1398 * this is like the root stores default striping for the whole
1399 * filesystem. historically we've been using a different approach
1400 * and store it in the config.
1402 dt_root_get(env, dev->lod_child, &info->lti_fid);
1403 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1405 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1406 struct lov_user_md *lum = buf->lb_buf;
1407 struct lov_desc *desc = &dev->lod_desc;
1409 if (buf->lb_buf == NULL) {
1411 } else if (buf->lb_len >= sizeof(*lum)) {
1412 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1413 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1414 lmm_oi_set_id(&lum->lmm_oi, 0);
1415 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1416 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1417 lum->lmm_stripe_size = cpu_to_le32(
1418 desc->ld_default_stripe_size);
1419 lum->lmm_stripe_count = cpu_to_le16(
1420 desc->ld_default_stripe_count);
1421 lum->lmm_stripe_offset = cpu_to_le16(
1422 desc->ld_default_stripe_offset);
1435 * Checks that the magic and the number of the stripes are sane.
1437 * \param[in] lod lod device
1438 * \param[in] lum a buffer storing LMV EA to verify
1440 * \retval 0 if the EA is sane
1441 * \retval negative otherwise
1443 static int lod_verify_md_striping(struct lod_device *lod,
1444 const struct lmv_user_md_v1 *lum)
1449 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC))
1450 GOTO(out, rc = -EINVAL);
1452 if (unlikely(le32_to_cpu(lum->lum_stripe_count) == 0))
1453 GOTO(out, rc = -EINVAL);
1456 CERROR("%s: invalid lmv_user_md: magic = %x, "
1457 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1458 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1459 (int)le32_to_cpu(lum->lum_stripe_offset),
1460 le32_to_cpu(lum->lum_stripe_count), rc);
1465 * Initialize LMV EA for a slave.
1467 * Initialize slave's LMV EA from the master's LMV EA.
1469 * \param[in] master_lmv a buffer containing master's EA
1470 * \param[out] slave_lmv a buffer where slave's EA will be stored
1473 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1474 const struct lmv_mds_md_v1 *master_lmv)
1476 *slave_lmv = *master_lmv;
1477 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1483 * Generate LMV EA from the object passed as \a dt. The object must have
1484 * the stripes created and initialized.
1486 * \param[in] env execution environment
1487 * \param[in] dt object
1488 * \param[out] lmv_buf buffer storing generated LMV EA
1490 * \retval 0 on success
1491 * \retval negative if failed
1493 int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1494 struct lu_buf *lmv_buf)
1496 struct lod_thread_info *info = lod_env_info(env);
1497 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1498 struct lod_object *lo = lod_dt_obj(dt);
1499 struct lmv_mds_md_v1 *lmm1;
1501 int type = LU_SEQ_RANGE_ANY;
1506 LASSERT(lo->ldo_dir_striped != 0);
1507 LASSERT(lo->ldo_stripenr > 0);
1508 stripe_count = lo->ldo_stripenr;
1509 /* Only store the LMV EA heahder on the disk. */
1510 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1511 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1515 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1518 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1519 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1520 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1521 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1522 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1527 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1528 lmv_buf->lb_buf = info->lti_ea_store;
1529 lmv_buf->lb_len = sizeof(*lmm1);
1530 lo->ldo_dir_striping_cached = 1;
1536 * Create in-core represenation for a striped directory.
1538 * Parse the buffer containing LMV EA and instantiate LU objects
1539 * representing the stripe objects. The pointers to the objects are
1540 * stored in ldo_stripe field of \a lo. This function is used when
1541 * we need to access an already created object (i.e. load from a disk).
1543 * \param[in] env execution environment
1544 * \param[in] lo lod object
1545 * \param[in] buf buffer containing LMV EA
1547 * \retval 0 on success
1548 * \retval negative if failed
1550 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1551 const struct lu_buf *buf)
1553 struct lod_thread_info *info = lod_env_info(env);
1554 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1555 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1556 struct dt_object **stripe;
1557 union lmv_mds_md *lmm = buf->lb_buf;
1558 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1559 struct lu_fid *fid = &info->lti_fid;
1564 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1567 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1568 lo->ldo_dir_slave_stripe = 1;
1572 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1575 if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1578 LASSERT(lo->ldo_stripe == NULL);
1579 OBD_ALLOC(stripe, sizeof(stripe[0]) *
1580 (le32_to_cpu(lmv1->lmv_stripe_count)));
1584 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1585 struct dt_device *tgt_dt;
1586 struct dt_object *dto;
1587 int type = LU_SEQ_RANGE_ANY;
1590 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1591 if (!fid_is_sane(fid))
1592 GOTO(out, rc = -ESTALE);
1594 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1598 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1599 tgt_dt = lod->lod_child;
1601 struct lod_tgt_desc *tgt;
1603 tgt = LTD_TGT(ltd, idx);
1605 GOTO(out, rc = -ESTALE);
1606 tgt_dt = tgt->ltd_tgt;
1609 dto = dt_locate_at(env, tgt_dt, fid,
1610 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1613 GOTO(out, rc = PTR_ERR(dto));
1618 lo->ldo_stripe = stripe;
1619 lo->ldo_stripenr = le32_to_cpu(lmv1->lmv_stripe_count);
1620 lo->ldo_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1622 lod_object_free_striping(env, lo);
1628 * Create a striped directory.
1630 * Create a striped directory with a given stripe pattern on the specified MDTs.
1631 * A striped directory is represented as a regular directory - an index listing
1632 * all the stripes. The stripes point back to the master object with ".." and
1633 * LinkEA. The master object gets LMV EA which identifies it as a striped
1634 * directory. The function allocates FIDs for all the stripes.
1636 * \param[in] env execution environment
1637 * \param[in] dt object
1638 * \param[in] attr attributes to initialize the objects with
1639 * \param[in] lum a pattern specifying the number of stripes and
1641 * \param[in] dof type of objects to be created
1642 * \param[in] th transaction handle
1644 * \retval 0 on success
1645 * \retval negative if failed
1647 static int lod_prep_md_striped_create(const struct lu_env *env,
1648 struct dt_object *dt,
1649 struct lu_attr *attr,
1650 const struct lmv_user_md_v1 *lum,
1651 struct dt_object_format *dof,
1654 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1655 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1656 struct lod_object *lo = lod_dt_obj(dt);
1657 struct lod_thread_info *info = lod_env_info(env);
1658 struct dt_object **stripe;
1659 struct lu_buf lmv_buf;
1660 struct lu_buf slave_lmv_buf;
1661 struct lmv_mds_md_v1 *lmm;
1662 struct lmv_mds_md_v1 *slave_lmm = NULL;
1663 struct dt_insert_rec *rec = &info->lti_dt_rec;
1671 /* The lum has been verifed in lod_verify_md_striping */
1672 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
1673 LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1675 stripe_count = le32_to_cpu(lum->lum_stripe_count);
1677 /* shrink the stripe_count to the avaible MDT count */
1678 if (stripe_count > lod->lod_remote_mdt_count + 1)
1679 stripe_count = lod->lod_remote_mdt_count + 1;
1681 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1685 OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1686 if (idx_array == NULL)
1687 GOTO(out_free, rc = -ENOMEM);
1689 for (i = 0; i < stripe_count; i++) {
1690 struct lod_tgt_desc *tgt = NULL;
1691 struct dt_object *dto;
1692 struct lu_fid fid = { 0 };
1694 struct lu_object_conf conf = { 0 };
1695 struct dt_device *tgt_dt = NULL;
1698 /* Right now, master stripe and master object are
1699 * on the same MDT */
1700 idx = le32_to_cpu(lum->lum_stripe_offset);
1701 rc = obd_fid_alloc(env, lod->lod_child_exp, &fid,
1705 tgt_dt = lod->lod_child;
1709 idx = (idx_array[i - 1] + 1) % (lod->lod_remote_mdt_count + 1);
1711 for (j = 0; j < lod->lod_remote_mdt_count;
1712 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1713 bool already_allocated = false;
1716 CDEBUG(D_INFO, "try idx %d, mdt cnt %u,"
1717 " allocated %u, last allocated %d\n", idx,
1718 lod->lod_remote_mdt_count, i, idx_array[i - 1]);
1720 /* Find next available target */
1721 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx))
1724 /* check whether the idx already exists
1725 * in current allocated array */
1726 for (k = 0; k < i; k++) {
1727 if (idx_array[k] == idx) {
1728 already_allocated = true;
1733 if (already_allocated)
1736 /* check the status of the OSP */
1737 tgt = LTD_TGT(ltd, idx);
1741 tgt_dt = tgt->ltd_tgt;
1742 rc = dt_statfs(env, tgt_dt, NULL);
1744 /* this OSP doesn't feel well */
1749 rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1758 /* Can not allocate more stripes */
1759 if (j == lod->lod_remote_mdt_count) {
1760 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1761 lod2obd(lod)->obd_name, stripe_count, i - 1);
1765 CDEBUG(D_INFO, "idx %d, mdt cnt %u,"
1766 " allocated %u, last allocated %d\n", idx,
1767 lod->lod_remote_mdt_count, i, idx_array[i - 1]);
1770 /* tgt_dt and fid must be ready after search avaible OSP
1771 * in the above loop */
1772 LASSERT(tgt_dt != NULL);
1773 LASSERT(fid_is_sane(&fid));
1774 conf.loc_flags = LOC_F_NEW;
1775 dto = dt_locate_at(env, tgt_dt, &fid,
1776 dt->do_lu.lo_dev->ld_site->ls_top_dev,
1779 GOTO(out_put, rc = PTR_ERR(dto));
1784 lo->ldo_dir_striped = 1;
1785 lo->ldo_stripe = stripe;
1786 lo->ldo_stripenr = i;
1787 lo->ldo_stripes_allocated = stripe_count;
1789 if (lo->ldo_stripenr == 0)
1790 GOTO(out_put, rc = -ENOSPC);
1792 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1795 lmm = lmv_buf.lb_buf;
1797 OBD_ALLOC_PTR(slave_lmm);
1798 if (slave_lmm == NULL)
1799 GOTO(out_put, rc = -ENOMEM);
1801 lod_prep_slave_lmv_md(slave_lmm, lmm);
1802 slave_lmv_buf.lb_buf = slave_lmm;
1803 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1805 if (!dt_try_as_dir(env, dt_object_child(dt)))
1806 GOTO(out_put, rc = -EINVAL);
1808 rec->rec_type = S_IFDIR;
1809 for (i = 0; i < lo->ldo_stripenr; i++) {
1810 struct dt_object *dto = stripe[i];
1811 char *stripe_name = info->lti_key;
1812 struct lu_name *sname;
1813 struct linkea_data ldata = { 0 };
1814 struct lu_buf linkea_buf;
1816 rc = dt_declare_create(env, dto, attr, NULL, dof, th);
1820 if (!dt_try_as_dir(env, dto))
1821 GOTO(out_put, rc = -EINVAL);
1823 rec->rec_fid = lu_object_fid(&dto->do_lu);
1824 rc = dt_declare_insert(env, dto, (const struct dt_rec *)rec,
1825 (const struct dt_key *)dot, th);
1829 /* master stripe FID will be put to .. */
1830 rec->rec_fid = lu_object_fid(&dt->do_lu);
1831 rc = dt_declare_insert(env, dto, (const struct dt_rec *)rec,
1832 (const struct dt_key *)dotdot, th);
1836 /* probably nothing to inherite */
1837 if (lo->ldo_striping_cached &&
1838 !LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size,
1839 lo->ldo_def_stripenr,
1840 lo->ldo_def_stripe_offset)) {
1841 struct lov_user_md_v3 *v3;
1843 /* sigh, lti_ea_store has been used for lmv_buf,
1844 * so we have to allocate buffer for default
1848 GOTO(out_put, rc = -ENOMEM);
1850 memset(v3, 0, sizeof(*v3));
1851 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
1852 v3->lmm_stripe_count =
1853 cpu_to_le16(lo->ldo_def_stripenr);
1854 v3->lmm_stripe_offset =
1855 cpu_to_le16(lo->ldo_def_stripe_offset);
1856 v3->lmm_stripe_size =
1857 cpu_to_le32(lo->ldo_def_stripe_size);
1858 if (lo->ldo_pool != NULL)
1859 strlcpy(v3->lmm_pool_name, lo->ldo_pool,
1860 sizeof(v3->lmm_pool_name));
1862 info->lti_buf.lb_buf = v3;
1863 info->lti_buf.lb_len = sizeof(*v3);
1864 rc = dt_declare_xattr_set(env, dto,
1873 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1874 cfs_fail_val != i) {
1875 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1877 slave_lmm->lmv_master_mdt_index =
1880 slave_lmm->lmv_master_mdt_index =
1882 rc = dt_declare_xattr_set(env, dto, &slave_lmv_buf,
1883 XATTR_NAME_LMV, 0, th);
1888 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1890 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1891 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1893 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1894 PFID(lu_object_fid(&dto->do_lu)), i);
1896 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1897 rc = linkea_data_new(&ldata, &info->lti_linkea_buf);
1901 rc = linkea_add_buf(&ldata, sname, lu_object_fid(&dt->do_lu));
1905 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1906 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1907 rc = dt_declare_xattr_set(env, dto, &linkea_buf,
1908 XATTR_NAME_LINK, 0, th);
1912 rec->rec_fid = lu_object_fid(&dto->do_lu);
1913 rc = dt_declare_insert(env, dt_object_child(dt),
1914 (const struct dt_rec *)rec,
1915 (const struct dt_key *)stripe_name, th);
1919 rc = dt_declare_ref_add(env, dt_object_child(dt), th);
1924 rc = dt_declare_xattr_set(env, dt_object_child(dt), &lmv_buf,
1925 XATTR_NAME_LMV, 0, th);
1931 for (i = 0; i < stripe_count; i++)
1932 if (stripe[i] != NULL)
1933 lu_object_put(env, &stripe[i]->do_lu);
1934 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
1935 lo->ldo_stripenr = 0;
1936 lo->ldo_stripes_allocated = 0;
1937 lo->ldo_stripe = NULL;
1941 if (idx_array != NULL)
1942 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
1943 if (slave_lmm != NULL)
1944 OBD_FREE_PTR(slave_lmm);
1950 * Declare create striped md object.
1952 * The function declares intention to create a striped directory. This is a
1953 * wrapper for lod_prep_md_striped_create(). The only additional functionality
1954 * is to verify pattern \a lum_buf is good. Check that function for the details.
1956 * \param[in] env execution environment
1957 * \param[in] dt object
1958 * \param[in] attr attributes to initialize the objects with
1959 * \param[in] lum_buf a pattern specifying the number of stripes and
1961 * \param[in] dof type of objects to be created
1962 * \param[in] th transaction handle
1964 * \retval 0 on success
1965 * \retval negative if failed
1968 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
1969 struct dt_object *dt,
1970 struct lu_attr *attr,
1971 const struct lu_buf *lum_buf,
1972 struct dt_object_format *dof,
1975 struct lod_object *lo = lod_dt_obj(dt);
1976 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1977 struct lmv_user_md_v1 *lum;
1981 lum = lum_buf->lb_buf;
1982 LASSERT(lum != NULL);
1984 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
1985 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
1986 (int)le32_to_cpu(lum->lum_stripe_offset));
1988 if (le32_to_cpu(lum->lum_stripe_count) == 0)
1991 rc = lod_verify_md_striping(lod, lum);
1995 /* prepare dir striped objects */
1996 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
1998 /* failed to create striping, let's reset
1999 * config so that others don't get confused */
2000 lod_object_free_striping(env, lo);
2009 * Implementation of dt_object_operations::do_declare_xattr_set.
2011 * Used with regular (non-striped) objects. Basically it
2012 * initializes the striping information and applies the
2013 * change to all the stripes.
2015 * \see dt_object_operations::do_declare_xattr_set() in the API description
2018 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2019 struct dt_object *dt,
2020 const struct lu_buf *buf,
2021 const char *name, int fl,
2024 struct dt_object *next = dt_object_child(dt);
2025 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2026 struct lod_object *lo = lod_dt_obj(dt);
2031 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2032 struct lmv_user_md_v1 *lum;
2034 LASSERT(buf != NULL && buf->lb_buf != NULL);
2036 rc = lod_verify_md_striping(d, lum);
2041 rc = dt_declare_xattr_set(env, next, buf, name, fl, th);
2045 /* set xattr to each stripes, if needed */
2046 rc = lod_load_striping(env, lo);
2050 /* Note: Do not set LinkEA on sub-stripes, otherwise
2051 * it will confuse the fid2path process(see mdt_path_current()).
2052 * The linkEA between master and sub-stripes is set in
2053 * lod_xattr_set_lmv(). */
2054 if (lo->ldo_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2057 for (i = 0; i < lo->ldo_stripenr; i++) {
2058 LASSERT(lo->ldo_stripe[i]);
2059 rc = dt_declare_xattr_set(env, lo->ldo_stripe[i], buf,
2069 * Implementation of dt_object_operations::do_declare_xattr_set.
2071 * \see dt_object_operations::do_declare_xattr_set() in the API description
2074 * the extension to the API:
2075 * - declaring LOVEA requests striping creation
2076 * - LU_XATTR_REPLACE means layout swap
2078 static int lod_declare_xattr_set(const struct lu_env *env,
2079 struct dt_object *dt,
2080 const struct lu_buf *buf,
2081 const char *name, int fl,
2084 struct dt_object *next = dt_object_child(dt);
2085 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2091 * allow to declare predefined striping on a new (!mode) object
2092 * which is supposed to be replay of regular file creation
2093 * (when LOV setting is declared)
2094 * LU_XATTR_REPLACE is set to indicate a layout swap
2096 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2097 if ((S_ISREG(mode) || mode == 0) && strcmp(name, XATTR_NAME_LOV) == 0 &&
2098 !(fl & LU_XATTR_REPLACE)) {
2100 * this is a request to manipulate object's striping
2102 if (dt_object_exists(dt)) {
2103 rc = dt_attr_get(env, next, attr, BYPASS_CAPA);
2107 memset(attr, 0, sizeof(*attr));
2108 attr->la_valid = LA_TYPE | LA_MODE;
2109 attr->la_mode = S_IFREG;
2111 rc = lod_declare_striped_object(env, dt, attr, buf, th);
2112 } else if (S_ISDIR(mode)) {
2113 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2115 rc = dt_declare_xattr_set(env, next, buf, name, fl, th);
2122 * Resets cached default striping in the object.
2124 * \param[in] lo object
2126 static void lod_lov_stripe_cache_clear(struct lod_object *lo)
2128 lo->ldo_striping_cached = 0;
2129 lo->ldo_def_striping_set = 0;
2130 lod_object_set_pool(lo, NULL);
2131 lo->ldo_def_stripe_size = 0;
2132 lo->ldo_def_stripenr = 0;
2133 if (lo->ldo_dir_stripe != NULL)
2134 lo->ldo_dir_striping_cached = 0;
2138 * Apply xattr changes to the object.
2140 * Applies xattr changes to the object and the stripes if the latter exist.
2142 * \param[in] env execution environment
2143 * \param[in] dt object
2144 * \param[in] buf buffer pointing to the new value of xattr
2145 * \param[in] name name of xattr
2146 * \param[in] fl flags
2147 * \param[in] th transaction handle
2148 * \param[in] capa not used currently
2150 * \retval 0 on success
2151 * \retval negative if failed
2153 static int lod_xattr_set_internal(const struct lu_env *env,
2154 struct dt_object *dt,
2155 const struct lu_buf *buf,
2156 const char *name, int fl, struct thandle *th,
2157 struct lustre_capa *capa)
2159 struct dt_object *next = dt_object_child(dt);
2160 struct lod_object *lo = lod_dt_obj(dt);
2165 rc = dt_xattr_set(env, next, buf, name, fl, th, capa);
2166 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2169 /* Note: Do not set LinkEA on sub-stripes, otherwise
2170 * it will confuse the fid2path process(see mdt_path_current()).
2171 * The linkEA between master and sub-stripes is set in
2172 * lod_xattr_set_lmv(). */
2173 if (lo->ldo_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2176 for (i = 0; i < lo->ldo_stripenr; i++) {
2177 LASSERT(lo->ldo_stripe[i]);
2178 rc = dt_xattr_set(env, lo->ldo_stripe[i], buf, name, fl, th,
2188 * Delete an extended attribute.
2190 * Deletes specified xattr from the object and the stripes if the latter exist.
2192 * \param[in] env execution environment
2193 * \param[in] dt object
2194 * \param[in] name name of xattr
2195 * \param[in] th transaction handle
2196 * \param[in] capa not used currently
2198 * \retval 0 on success
2199 * \retval negative if failed
2201 static int lod_xattr_del_internal(const struct lu_env *env,
2202 struct dt_object *dt,
2203 const char *name, struct thandle *th,
2204 struct lustre_capa *capa)
2206 struct dt_object *next = dt_object_child(dt);
2207 struct lod_object *lo = lod_dt_obj(dt);
2212 rc = dt_xattr_del(env, next, name, th, capa);
2213 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2216 if (lo->ldo_stripenr == 0)
2219 for (i = 0; i < lo->ldo_stripenr; i++) {
2220 LASSERT(lo->ldo_stripe[i]);
2221 rc = dt_xattr_del(env, lo->ldo_stripe[i], name, th,
2231 * Set default striping on a directory.
2233 * Sets specified striping on a directory object unless it matches the default
2234 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2235 * EA. This striping will be used when regular file is being created in this
2238 * \param[in] env execution environment
2239 * \param[in] dt the striped object
2240 * \param[in] buf buffer with the striping
2241 * \param[in] name name of EA
2242 * \param[in] fl xattr flag (see OSD API description)
2243 * \param[in] th transaction handle
2244 * \param[in] capa not used
2246 * \retval 0 on success
2247 * \retval negative if failed
2249 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
2250 struct dt_object *dt,
2251 const struct lu_buf *buf,
2252 const char *name, int fl,
2254 struct lustre_capa *capa)
2256 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2257 struct lod_object *l = lod_dt_obj(dt);
2258 struct lov_user_md_v1 *lum;
2259 struct lov_user_md_v3 *v3 = NULL;
2263 /* If it is striped dir, we should clear the stripe cache for
2264 * slave stripe as well, but there are no effective way to
2265 * notify the LOD on the slave MDT, so we do not cache stripe
2266 * information for slave stripe for now. XXX*/
2267 lod_lov_stripe_cache_clear(l);
2268 LASSERT(buf != NULL && buf->lb_buf != NULL);
2271 rc = lod_verify_striping(d, buf, false);
2275 if (lum->lmm_magic == LOV_USER_MAGIC_V3)
2278 /* if { size, offset, count } = { 0, -1, 0 } and no pool
2279 * (i.e. all default values specified) then delete default
2280 * striping from dir. */
2282 "set default striping: sz %u # %u offset %d %s %s\n",
2283 (unsigned)lum->lmm_stripe_size,
2284 (unsigned)lum->lmm_stripe_count,
2285 (int)lum->lmm_stripe_offset,
2286 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
2288 if (LOVEA_DELETE_VALUES((lum->lmm_stripe_size),
2289 (lum->lmm_stripe_count),
2290 (lum->lmm_stripe_offset)) &&
2291 lum->lmm_magic == LOV_USER_MAGIC_V1) {
2292 rc = lod_xattr_del_internal(env, dt, name, th, capa);
2296 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th, capa);
2303 * Set default striping on a directory object.
2305 * Sets specified striping on a directory object unless it matches the default
2306 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2307 * EA. This striping will be used when a new directory is being created in the
2310 * \param[in] env execution environment
2311 * \param[in] dt the striped object
2312 * \param[in] buf buffer with the striping
2313 * \param[in] name name of EA
2314 * \param[in] fl xattr flag (see OSD API description)
2315 * \param[in] th transaction handle
2316 * \param[in] capa not used
2318 * \retval 0 on success
2319 * \retval negative if failed
2321 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
2322 struct dt_object *dt,
2323 const struct lu_buf *buf,
2324 const char *name, int fl,
2326 struct lustre_capa *capa)
2328 struct lod_object *l = lod_dt_obj(dt);
2329 struct lmv_user_md_v1 *lum;
2333 LASSERT(buf != NULL && buf->lb_buf != NULL);
2336 CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
2337 le32_to_cpu(lum->lum_stripe_count),
2338 (int)le32_to_cpu(lum->lum_stripe_offset));
2340 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
2341 le32_to_cpu(lum->lum_stripe_offset)) &&
2342 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
2343 rc = lod_xattr_del_internal(env, dt, name, th, capa);
2347 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th, capa);
2352 /* Update default stripe cache */
2353 if (l->ldo_dir_stripe == NULL) {
2354 OBD_ALLOC_PTR(l->ldo_dir_stripe);
2355 if (l->ldo_dir_stripe == NULL)
2359 l->ldo_dir_striping_cached = 0;
2360 l->ldo_dir_def_striping_set = 1;
2361 l->ldo_dir_def_stripenr = le32_to_cpu(lum->lum_stripe_count);
2367 * Turn directory into a striped directory.
2369 * During replay the client sends the striping created before MDT
2370 * failure, then the layer above LOD sends this defined striping
2371 * using ->do_xattr_set(), so LOD uses this method to replay creation
2372 * of the stripes. Notice the original information for the striping
2373 * (#stripes, FIDs, etc) was transfered in declare path.
2375 * \param[in] env execution environment
2376 * \param[in] dt the striped object
2377 * \param[in] buf not used currently
2378 * \param[in] name not used currently
2379 * \param[in] fl xattr flag (see OSD API description)
2380 * \param[in] th transaction handle
2381 * \param[in] capa not used
2383 * \retval 0 on success
2384 * \retval negative if failed
2386 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
2387 const struct lu_buf *buf, const char *name,
2388 int fl, struct thandle *th,
2389 struct lustre_capa *capa)
2391 struct lod_object *lo = lod_dt_obj(dt);
2392 struct lod_thread_info *info = lod_env_info(env);
2393 struct lu_attr *attr = &info->lti_attr;
2394 struct dt_object_format *dof = &info->lti_format;
2395 struct lu_buf lmv_buf;
2396 struct lu_buf slave_lmv_buf;
2397 struct lmv_mds_md_v1 *lmm;
2398 struct lmv_mds_md_v1 *slave_lmm = NULL;
2399 struct dt_insert_rec *rec = &info->lti_dt_rec;
2404 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2407 /* The stripes are supposed to be allocated in declare phase,
2408 * if there are no stripes being allocated, it will skip */
2409 if (lo->ldo_stripenr == 0)
2412 rc = dt_attr_get(env, dt_object_child(dt), attr, BYPASS_CAPA);
2416 attr->la_valid = LA_TYPE | LA_MODE;
2417 dof->dof_type = DFT_DIR;
2419 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
2422 lmm = lmv_buf.lb_buf;
2424 OBD_ALLOC_PTR(slave_lmm);
2425 if (slave_lmm == NULL)
2428 lod_prep_slave_lmv_md(slave_lmm, lmm);
2429 slave_lmv_buf.lb_buf = slave_lmm;
2430 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
2432 rec->rec_type = S_IFDIR;
2433 for (i = 0; i < lo->ldo_stripenr; i++) {
2434 struct dt_object *dto;
2435 char *stripe_name = info->lti_key;
2436 struct lu_name *sname;
2437 struct linkea_data ldata = { 0 };
2438 struct lu_buf linkea_buf;
2440 dto = lo->ldo_stripe[i];
2441 dt_write_lock(env, dto, MOR_TGT_CHILD);
2442 rc = dt_create(env, dto, attr, NULL, dof, th);
2443 dt_write_unlock(env, dto);
2447 rec->rec_fid = lu_object_fid(&dto->do_lu);
2448 rc = dt_insert(env, dto, (const struct dt_rec *)rec,
2449 (const struct dt_key *)dot, th, capa, 0);
2453 rec->rec_fid = lu_object_fid(&dt->do_lu);
2454 rc = dt_insert(env, dto, (struct dt_rec *)rec,
2455 (const struct dt_key *)dotdot, th, capa, 0);
2459 if (lo->ldo_striping_cached &&
2460 !LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size,
2461 lo->ldo_def_stripenr,
2462 lo->ldo_def_stripe_offset)) {
2463 struct lov_user_md_v3 *v3;
2465 /* sigh, lti_ea_store has been used for lmv_buf,
2466 * so we have to allocate buffer for default
2472 memset(v3, 0, sizeof(*v3));
2473 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
2474 v3->lmm_stripe_count =
2475 cpu_to_le16(lo->ldo_def_stripenr);
2476 v3->lmm_stripe_offset =
2477 cpu_to_le16(lo->ldo_def_stripe_offset);
2478 v3->lmm_stripe_size =
2479 cpu_to_le32(lo->ldo_def_stripe_size);
2480 if (lo->ldo_pool != NULL)
2481 strlcpy(v3->lmm_pool_name, lo->ldo_pool,
2482 sizeof(v3->lmm_pool_name));
2484 info->lti_buf.lb_buf = v3;
2485 info->lti_buf.lb_len = sizeof(*v3);
2486 rc = dt_xattr_set(env, dto, &info->lti_buf,
2487 XATTR_NAME_LOV, 0, th, capa);
2493 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
2494 cfs_fail_val != i) {
2495 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
2497 slave_lmm->lmv_master_mdt_index =
2500 slave_lmm->lmv_master_mdt_index =
2502 rc = dt_xattr_set(env, dto, &slave_lmv_buf,
2503 XATTR_NAME_LMV, fl, th, capa);
2508 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
2510 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2511 PFID(lu_object_fid(&dto->do_lu)), i + 1);
2513 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2514 PFID(lu_object_fid(&dto->do_lu)), i);
2516 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2517 rc = linkea_data_new(&ldata, &info->lti_linkea_buf);
2521 rc = linkea_add_buf(&ldata, sname, lu_object_fid(&dt->do_lu));
2525 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
2526 linkea_buf.lb_len = ldata.ld_leh->leh_len;
2527 rc = dt_xattr_set(env, dto, &linkea_buf, XATTR_NAME_LINK,
2528 0, th, BYPASS_CAPA);
2532 rec->rec_fid = lu_object_fid(&dto->do_lu);
2533 rc = dt_insert(env, dt_object_child(dt),
2534 (const struct dt_rec *)rec,
2535 (const struct dt_key *)stripe_name, th, capa, 0);
2539 rc = dt_ref_add(env, dt_object_child(dt), th);
2544 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
2545 rc = dt_xattr_set(env, dt_object_child(dt), &lmv_buf,
2546 XATTR_NAME_LMV, fl, th, capa);
2549 if (slave_lmm != NULL)
2550 OBD_FREE_PTR(slave_lmm);
2556 * Helper function to declare/execute creation of a striped directory
2558 * Called in declare/create object path, prepare striping for a directory
2559 * and prepare defaults data striping for the objects to be created in
2560 * that directory. Notice the function calls "declaration" or "execution"
2561 * methods depending on \a declare param. This is a consequence of the
2562 * current approach while we don't have natural distributed transactions:
2563 * we basically execute non-local updates in the declare phase. So, the
2564 * arguments for the both phases are the same and this is the reason for
2565 * this function to exist.
2567 * \param[in] env execution environment
2568 * \param[in] dt object
2569 * \param[in] attr attributes the stripes will be created with
2570 * \param[in] dof format of stripes (see OSD API description)
2571 * \param[in] th transaction handle
2572 * \param[in] declare where to call "declare" or "execute" methods
2574 * \retval 0 on success
2575 * \retval negative if failed
2577 int lod_dir_striping_create_internal(const struct lu_env *env,
2578 struct dt_object *dt,
2579 struct lu_attr *attr,
2580 struct dt_object_format *dof,
2584 struct lod_thread_info *info = lod_env_info(env);
2585 struct lod_object *lo = lod_dt_obj(dt);
2589 if (!LMVEA_DELETE_VALUES(lo->ldo_stripenr,
2590 lo->ldo_dir_stripe_offset)) {
2591 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
2592 int stripe_count = lo->ldo_stripenr;
2594 if (info->lti_ea_store_size < sizeof(*v1)) {
2595 rc = lod_ea_store_resize(info, sizeof(*v1));
2598 v1 = info->lti_ea_store;
2601 memset(v1, 0, sizeof(*v1));
2602 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
2603 v1->lum_stripe_count = cpu_to_le32(stripe_count);
2604 v1->lum_stripe_offset =
2605 cpu_to_le32(lo->ldo_dir_stripe_offset);
2607 info->lti_buf.lb_buf = v1;
2608 info->lti_buf.lb_len = sizeof(*v1);
2611 rc = lod_declare_xattr_set_lmv(env, dt, attr,
2612 &info->lti_buf, dof, th);
2614 rc = lod_xattr_set_lmv(env, dt, &info->lti_buf,
2615 XATTR_NAME_LMV, 0, th,
2621 /* Transfer default LMV striping from the parent */
2622 if (lo->ldo_dir_striping_cached &&
2623 !LMVEA_DELETE_VALUES(lo->ldo_dir_def_stripenr,
2624 lo->ldo_dir_def_stripe_offset)) {
2625 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
2626 int def_stripe_count = lo->ldo_dir_def_stripenr;
2628 if (info->lti_ea_store_size < sizeof(*v1)) {
2629 rc = lod_ea_store_resize(info, sizeof(*v1));
2632 v1 = info->lti_ea_store;
2635 memset(v1, 0, sizeof(*v1));
2636 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
2637 v1->lum_stripe_count = cpu_to_le32(def_stripe_count);
2638 v1->lum_stripe_offset =
2639 cpu_to_le32(lo->ldo_dir_def_stripe_offset);
2641 cpu_to_le32(lo->ldo_dir_def_hash_type);
2643 info->lti_buf.lb_buf = v1;
2644 info->lti_buf.lb_len = sizeof(*v1);
2646 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
2647 XATTR_NAME_DEFAULT_LMV,
2650 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
2652 XATTR_NAME_DEFAULT_LMV, 0,
2658 /* Transfer default LOV striping from the parent */
2659 if (lo->ldo_striping_cached &&
2660 !LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size,
2661 lo->ldo_def_stripenr,
2662 lo->ldo_def_stripe_offset)) {
2663 struct lov_user_md_v3 *v3 = info->lti_ea_store;
2665 if (info->lti_ea_store_size < sizeof(*v3)) {
2666 rc = lod_ea_store_resize(info, sizeof(*v3));
2669 v3 = info->lti_ea_store;
2672 memset(v3, 0, sizeof(*v3));
2673 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
2674 v3->lmm_stripe_count = cpu_to_le16(lo->ldo_def_stripenr);
2675 v3->lmm_stripe_offset = cpu_to_le16(lo->ldo_def_stripe_offset);
2676 v3->lmm_stripe_size = cpu_to_le32(lo->ldo_def_stripe_size);
2677 if (lo->ldo_pool != NULL)
2678 strlcpy(v3->lmm_pool_name, lo->ldo_pool,
2679 sizeof(v3->lmm_pool_name));
2681 info->lti_buf.lb_buf = v3;
2682 info->lti_buf.lb_len = sizeof(*v3);
2685 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
2686 XATTR_NAME_LOV, 0, th);
2688 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
2689 XATTR_NAME_LOV, 0, th,
2698 static int lod_declare_dir_striping_create(const struct lu_env *env,
2699 struct dt_object *dt,
2700 struct lu_attr *attr,
2701 struct dt_object_format *dof,
2704 return lod_dir_striping_create_internal(env, dt, attr, dof, th, true);
2707 static int lod_dir_striping_create(const struct lu_env *env,
2708 struct dt_object *dt,
2709 struct lu_attr *attr,
2710 struct dt_object_format *dof,
2713 return lod_dir_striping_create_internal(env, dt, attr, dof, th, false);
2717 * Implementation of dt_object_operations::do_xattr_set.
2719 * Sets specified extended attribute on the object. Three types of EAs are
2721 * LOV EA - stores striping for a regular file or default striping (when set
2723 * LMV EA - stores a marker for the striped directories
2724 * DMV EA - stores default directory striping
2726 * When striping is applied to a non-striped existing object (this is called
2727 * late striping), then LOD notices the caller wants to turn the object into a
2728 * striped one. The stripe objects are created and appropriate EA is set:
2729 * LOV EA storing all the stripes directly or LMV EA storing just a small header
2730 * with striping configuration.
2732 * \see dt_object_operations::do_xattr_set() in the API description for details.
2734 static int lod_xattr_set(const struct lu_env *env,
2735 struct dt_object *dt, const struct lu_buf *buf,
2736 const char *name, int fl, struct thandle *th,
2737 struct lustre_capa *capa)
2739 struct dt_object *next = dt_object_child(dt);
2743 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2744 strcmp(name, XATTR_NAME_LMV) == 0) {
2745 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
2747 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
2748 LMV_HASH_FLAG_MIGRATION)
2749 rc = dt_xattr_set(env, next, buf, name, fl, th, capa);
2751 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
2756 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2757 strcmp(name, XATTR_NAME_LOV) == 0) {
2759 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th, capa);
2761 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2762 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2764 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
2767 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
2768 !strcmp(name, XATTR_NAME_LOV)) {
2769 /* in case of lov EA swap, just set it
2770 * if not, it is a replay so check striping match what we
2771 * already have during req replay, declare_xattr_set()
2772 * defines striping, then create() does the work
2774 if (fl & LU_XATTR_REPLACE) {
2775 /* free stripes, then update disk */
2776 lod_object_free_striping(env, lod_dt_obj(dt));
2777 rc = dt_xattr_set(env, next, buf, name, fl, th, capa);
2779 rc = lod_striping_create(env, dt, NULL, NULL, th);
2784 /* then all other xattr */
2785 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th, capa);
2791 * Implementation of dt_object_operations::do_declare_xattr_del.
2793 * \see dt_object_operations::do_declare_xattr_del() in the API description
2796 static int lod_declare_xattr_del(const struct lu_env *env,
2797 struct dt_object *dt, const char *name,
2800 return dt_declare_xattr_del(env, dt_object_child(dt), name, th);
2804 * Implementation of dt_object_operations::do_xattr_del.
2806 * If EA storing a regular striping is being deleted, then release
2807 * all the references to the stripe objects in core.
2809 * \see dt_object_operations::do_xattr_del() in the API description for details.
2811 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
2812 const char *name, struct thandle *th,
2813 struct lustre_capa *capa)
2815 if (!strcmp(name, XATTR_NAME_LOV))
2816 lod_object_free_striping(env, lod_dt_obj(dt));
2817 return dt_xattr_del(env, dt_object_child(dt), name, th, capa);
2821 * Implementation of dt_object_operations::do_xattr_list.
2823 * \see dt_object_operations::do_xattr_list() in the API description
2826 static int lod_xattr_list(const struct lu_env *env,
2827 struct dt_object *dt, struct lu_buf *buf,
2828 struct lustre_capa *capa)
2830 return dt_xattr_list(env, dt_object_child(dt), buf, capa);
2834 * Initialize a pool the object belongs to.
2836 * When a striped object is being created, striping configuration
2837 * may demand the stripes are allocated on a limited set of the
2838 * targets. These limited sets are known as "pools". So we copy
2839 * a pool name into the object and later actual creation methods
2840 * (like lod_object_create()) will use this information to allocate
2841 * the stripes properly.
2843 * \param[in] o object
2844 * \param[in] pool pool name
2846 int lod_object_set_pool(struct lod_object *o, char *pool)
2851 len = strlen(o->ldo_pool);
2852 OBD_FREE(o->ldo_pool, len + 1);
2857 OBD_ALLOC(o->ldo_pool, len + 1);
2858 if (o->ldo_pool == NULL)
2860 strcpy(o->ldo_pool, pool);
2865 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
2867 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
2872 * Cache default regular striping in the object.
2874 * To improve performance of striped regular object creation we cache
2875 * default LOV striping (if it exists) in the parent directory object.
2877 * \param[in] env execution environment
2878 * \param[in] lp object
2880 * \retval 0 on success
2881 * \retval negative if failed
2883 static int lod_cache_parent_lov_striping(const struct lu_env *env,
2884 struct lod_object *lp)
2886 struct lod_thread_info *info = lod_env_info(env);
2887 struct lov_user_md_v1 *v1 = NULL;
2888 struct lov_user_md_v3 *v3 = NULL;
2892 /* called from MDD without parent being write locked,
2894 dt_write_lock(env, dt_object_child(&lp->ldo_obj), 0);
2895 rc = lod_get_lov_ea(env, lp);
2899 if (rc < (typeof(rc))sizeof(struct lov_user_md)) {
2900 /* don't lookup for non-existing or invalid striping */
2901 lp->ldo_def_striping_set = 0;
2902 lp->ldo_striping_cached = 1;
2903 lp->ldo_def_stripe_size = 0;
2904 lp->ldo_def_stripenr = 0;
2905 lp->ldo_def_stripe_offset = (typeof(v1->lmm_stripe_offset))(-1);
2906 GOTO(unlock, rc = 0);
2910 v1 = info->lti_ea_store;
2911 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
2912 lustre_swab_lov_user_md_v1(v1);
2913 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
2914 v3 = (struct lov_user_md_v3 *)v1;
2915 lustre_swab_lov_user_md_v3(v3);
2918 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1)
2919 GOTO(unlock, rc = 0);
2921 if (v1->lmm_pattern != LOV_PATTERN_RAID0 && v1->lmm_pattern != 0)
2922 GOTO(unlock, rc = 0);
2924 CDEBUG(D_INFO, DFID" stripe_count=%d stripe_size=%d stripe_offset=%d\n",
2925 PFID(lu_object_fid(&lp->ldo_obj.do_lu)),
2926 (int)v1->lmm_stripe_count,
2927 (int)v1->lmm_stripe_size, (int)v1->lmm_stripe_offset);
2929 lp->ldo_def_stripenr = v1->lmm_stripe_count;
2930 lp->ldo_def_stripe_size = v1->lmm_stripe_size;
2931 lp->ldo_def_stripe_offset = v1->lmm_stripe_offset;
2932 lp->ldo_striping_cached = 1;
2933 lp->ldo_def_striping_set = 1;
2934 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2935 /* XXX: sanity check here */
2936 v3 = (struct lov_user_md_v3 *) v1;
2937 if (v3->lmm_pool_name[0])
2938 lod_object_set_pool(lp, v3->lmm_pool_name);
2942 dt_write_unlock(env, dt_object_child(&lp->ldo_obj));
2948 * Cache default directory striping in the object.
2950 * To improve performance of striped directory creation we cache default
2951 * directory striping (if it exists) in the parent directory object.
2953 * \param[in] env execution environment
2954 * \param[in] lp object
2956 * \retval 0 on success
2957 * \retval negative if failed
2959 static int lod_cache_parent_lmv_striping(const struct lu_env *env,
2960 struct lod_object *lp)
2962 struct lod_thread_info *info = lod_env_info(env);
2963 struct lmv_user_md_v1 *v1 = NULL;
2967 /* called from MDD without parent being write locked,
2969 dt_write_lock(env, dt_object_child(&lp->ldo_obj), 0);
2970 rc = lod_get_default_lmv_ea(env, lp);
2974 if (rc < (typeof(rc))sizeof(struct lmv_user_md)) {
2975 /* don't lookup for non-existing or invalid striping */
2976 lp->ldo_dir_def_striping_set = 0;
2977 lp->ldo_dir_striping_cached = 1;
2978 lp->ldo_dir_def_stripenr = 0;
2979 lp->ldo_dir_def_stripe_offset =
2980 (typeof(v1->lum_stripe_offset))(-1);
2981 lp->ldo_dir_def_hash_type = LMV_HASH_TYPE_FNV_1A_64;
2982 GOTO(unlock, rc = 0);
2986 v1 = info->lti_ea_store;
2988 lp->ldo_dir_def_stripenr = le32_to_cpu(v1->lum_stripe_count);
2989 lp->ldo_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
2990 lp->ldo_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
2991 lp->ldo_dir_def_striping_set = 1;
2992 lp->ldo_dir_striping_cached = 1;
2996 dt_write_unlock(env, dt_object_child(&lp->ldo_obj));
3001 * Cache default striping in the object.
3003 * To improve performance of striped object creation we cache default striping
3004 * (if it exists) in the parent directory object. We always cache default
3005 * striping for the regular files (stored in LOV EA) and we cache default
3006 * striping for the directories if requested by \a child_mode (when a new
3007 * directory is being created).
3009 * \param[in] env execution environment
3010 * \param[in] lp object
3011 * \param[in] child_mode new object's mode
3013 * \retval 0 on success
3014 * \retval negative if failed
3016 static int lod_cache_parent_striping(const struct lu_env *env,
3017 struct lod_object *lp,
3023 rc = lod_load_striping(env, lp);
3027 if (!lp->ldo_striping_cached) {
3028 /* we haven't tried to get default striping for
3029 * the directory yet, let's cache it in the object */
3030 rc = lod_cache_parent_lov_striping(env, lp);
3035 if (S_ISDIR(child_mode) && !lp->ldo_dir_striping_cached)
3036 rc = lod_cache_parent_lmv_striping(env, lp);
3042 * Implementation of dt_object_operations::do_ah_init.
3044 * This method is used to make a decision on the striping configuration for the
3045 * object being created. It can be taken from the \a parent object if it exists,
3046 * or filesystem's default. The resulting configuration (number of stripes,
3047 * stripe size/offset, pool name, etc) is stored in the object itself and will
3048 * be used by the methods like ->doo_declare_create().
3050 * \see dt_object_operations::do_ah_init() in the API description for details.
3052 static void lod_ah_init(const struct lu_env *env,
3053 struct dt_allocation_hint *ah,
3054 struct dt_object *parent,
3055 struct dt_object *child,
3058 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
3059 struct dt_object *nextp = NULL;
3060 struct dt_object *nextc;
3061 struct lod_object *lp = NULL;
3062 struct lod_object *lc;
3063 struct lov_desc *desc;
3069 if (likely(parent)) {
3070 nextp = dt_object_child(parent);
3071 lp = lod_dt_obj(parent);
3072 rc = lod_load_striping(env, lp);
3077 nextc = dt_object_child(child);
3078 lc = lod_dt_obj(child);
3080 LASSERT(lc->ldo_stripenr == 0);
3081 LASSERT(lc->ldo_stripe == NULL);
3084 * local object may want some hints
3085 * in case of late striping creation, ->ah_init()
3086 * can be called with local object existing
3088 if (!dt_object_exists(nextc) || dt_object_remote(nextc)) {
3089 struct dt_object *obj;
3091 obj = (nextp != NULL && dt_object_remote(nextp)) ? NULL : nextp;
3092 nextc->do_ops->do_ah_init(env, ah, obj, nextc, child_mode);
3095 if (S_ISDIR(child_mode)) {
3096 if (lc->ldo_dir_stripe == NULL) {
3097 OBD_ALLOC_PTR(lc->ldo_dir_stripe);
3098 if (lc->ldo_dir_stripe == NULL)
3102 if (lp->ldo_dir_stripe == NULL) {
3103 OBD_ALLOC_PTR(lp->ldo_dir_stripe);
3104 if (lp->ldo_dir_stripe == NULL)
3108 rc = lod_cache_parent_striping(env, lp, child_mode);
3112 /* transfer defaults to new directory */
3113 if (lp->ldo_striping_cached) {
3115 lod_object_set_pool(lc, lp->ldo_pool);
3116 lc->ldo_def_stripenr = lp->ldo_def_stripenr;
3117 lc->ldo_def_stripe_size = lp->ldo_def_stripe_size;
3118 lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
3119 lc->ldo_striping_cached = 1;
3120 lc->ldo_def_striping_set = 1;
3121 CDEBUG(D_OTHER, "inherite EA sz:%d off:%d nr:%d\n",
3122 (int)lc->ldo_def_stripe_size,
3123 (int)lc->ldo_def_stripe_offset,
3124 (int)lc->ldo_def_stripenr);
3127 /* transfer dir defaults to new directory */
3128 if (lp->ldo_dir_striping_cached) {
3129 lc->ldo_dir_def_stripenr = lp->ldo_dir_def_stripenr;
3130 lc->ldo_dir_def_stripe_offset =
3131 lp->ldo_dir_def_stripe_offset;
3132 lc->ldo_dir_def_hash_type =
3133 lp->ldo_dir_def_hash_type;
3134 lc->ldo_dir_striping_cached = 1;
3135 lc->ldo_dir_def_striping_set = 1;
3136 CDEBUG(D_INFO, "inherit default EA nr:%d off:%d t%u\n",
3137 (int)lc->ldo_dir_def_stripenr,
3138 (int)lc->ldo_dir_def_stripe_offset,
3139 lc->ldo_dir_def_hash_type);
3142 /* It should always honour the specified stripes */
3143 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0) {
3144 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
3146 rc = lod_verify_md_striping(d, lum1);
3148 le32_to_cpu(lum1->lum_stripe_count) > 1) {
3149 /* Directory will be striped only if
3150 * stripe_count > 1 */
3152 le32_to_cpu(lum1->lum_stripe_count);
3153 lc->ldo_dir_stripe_offset =
3154 le32_to_cpu(lum1->lum_stripe_offset);
3155 lc->ldo_dir_hash_type =
3156 le32_to_cpu(lum1->lum_hash_type);
3157 CDEBUG(D_INFO, "set stripe EA nr:%hu off:%d\n",
3159 (int)lc->ldo_dir_stripe_offset);
3161 /* then check whether there is default stripes from parent */
3162 } else if (lp->ldo_dir_def_striping_set) {
3163 /* If there are default dir stripe from parent */
3164 lc->ldo_stripenr = lp->ldo_dir_def_stripenr;
3165 lc->ldo_dir_stripe_offset =
3166 lp->ldo_dir_def_stripe_offset;
3167 lc->ldo_dir_hash_type =
3168 lp->ldo_dir_def_hash_type;
3169 CDEBUG(D_INFO, "inherit EA nr:%hu off:%d\n",
3171 (int)lc->ldo_dir_stripe_offset);
3173 /* set default stripe for this directory */
3174 lc->ldo_stripenr = 0;
3175 lc->ldo_dir_stripe_offset = -1;
3178 CDEBUG(D_INFO, "final striping count:%hu, offset:%d\n",
3179 lc->ldo_stripenr, (int)lc->ldo_dir_stripe_offset);
3185 * if object is going to be striped over OSTs, transfer default
3186 * striping information to the child, so that we can use it
3187 * during declaration and creation
3189 if (!lod_object_will_be_striped(S_ISREG(child_mode),
3190 lu_object_fid(&child->do_lu)))
3193 * try from the parent
3195 if (likely(parent)) {
3196 lod_cache_parent_striping(env, lp, child_mode);
3198 lc->ldo_def_stripe_offset = LOV_OFFSET_DEFAULT;
3200 if (lp->ldo_def_striping_set) {
3202 lod_object_set_pool(lc, lp->ldo_pool);
3203 lc->ldo_stripenr = lp->ldo_def_stripenr;
3204 lc->ldo_stripe_size = lp->ldo_def_stripe_size;
3205 lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
3206 CDEBUG(D_OTHER, "striping from parent: #%d, sz %d %s\n",
3207 lc->ldo_stripenr, lc->ldo_stripe_size,
3208 lp->ldo_pool ? lp->ldo_pool : "");
3213 * if the parent doesn't provide with specific pattern, grab fs-wide one
3215 desc = &d->lod_desc;
3216 if (lc->ldo_stripenr == 0)
3217 lc->ldo_stripenr = desc->ld_default_stripe_count;
3218 if (lc->ldo_stripe_size == 0)
3219 lc->ldo_stripe_size = desc->ld_default_stripe_size;
3220 CDEBUG(D_OTHER, "final striping: # %d stripes, sz %d from %s\n",
3221 lc->ldo_stripenr, lc->ldo_stripe_size,
3222 lc->ldo_pool ? lc->ldo_pool : "");
3225 /* we do not cache stripe information for slave stripe, see
3226 * lod_xattr_set_lov_on_dir */
3227 if (lp != NULL && lp->ldo_dir_slave_stripe)
3228 lod_lov_stripe_cache_clear(lp);
3233 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
3235 * Size initialization on late striping.
3237 * Propagate the size of a truncated object to a deferred striping.
3238 * This function handles a special case when truncate was done on a
3239 * non-striped object and now while the striping is being created
3240 * we can't lose that size, so we have to propagate it to the stripes
3243 * \param[in] env execution environment
3244 * \param[in] dt object
3245 * \param[in] th transaction handle
3247 * \retval 0 on success
3248 * \retval negative if failed
3250 static int lod_declare_init_size(const struct lu_env *env,
3251 struct dt_object *dt, struct thandle *th)
3253 struct dt_object *next = dt_object_child(dt);
3254 struct lod_object *lo = lod_dt_obj(dt);
3255 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3256 uint64_t size, offs;
3260 /* XXX: we support the simplest (RAID0) striping so far */
3261 LASSERT(lo->ldo_stripe || lo->ldo_stripenr == 0);
3262 LASSERT(lo->ldo_stripe_size > 0);
3264 rc = dt_attr_get(env, next, attr, BYPASS_CAPA);
3265 LASSERT(attr->la_valid & LA_SIZE);
3269 size = attr->la_size;
3273 /* ll_do_div64(a, b) returns a % b, and a = a / b */
3274 ll_do_div64(size, (__u64) lo->ldo_stripe_size);
3275 stripe = ll_do_div64(size, (__u64) lo->ldo_stripenr);
3277 size = size * lo->ldo_stripe_size;
3278 offs = attr->la_size;
3279 size += ll_do_div64(offs, lo->ldo_stripe_size);
3281 attr->la_valid = LA_SIZE;
3282 attr->la_size = size;
3284 rc = dt_declare_attr_set(env, lo->ldo_stripe[stripe], attr, th);
3290 * Declare creation of striped object.
3292 * The function declares creation stripes for a regular object. The function
3293 * also declares whether the stripes will be created with non-zero size if
3294 * previously size was set non-zero on the master object. If object \a dt is
3295 * not local, then only fully defined striping can be applied in \a lovea.
3296 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
3299 * \param[in] env execution environment
3300 * \param[in] dt object
3301 * \param[in] attr attributes the stripes will be created with
3302 * \param[in] lovea a buffer containing striping description
3303 * \param[in] th transaction handle
3305 * \retval 0 on success
3306 * \retval negative if failed
3308 int lod_declare_striped_object(const struct lu_env *env, struct dt_object *dt,
3309 struct lu_attr *attr,
3310 const struct lu_buf *lovea, struct thandle *th)
3312 struct lod_thread_info *info = lod_env_info(env);
3313 struct dt_object *next = dt_object_child(dt);
3314 struct lod_object *lo = lod_dt_obj(dt);
3318 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO)) {
3319 /* failed to create striping, let's reset
3320 * config so that others don't get confused */
3321 lod_object_free_striping(env, lo);
3322 GOTO(out, rc = -ENOMEM);
3325 if (!dt_object_remote(next)) {
3326 /* choose OST and generate appropriate objects */
3327 rc = lod_qos_prep_create(env, lo, attr, lovea, th);
3329 /* failed to create striping, let's reset
3330 * config so that others don't get confused */
3331 lod_object_free_striping(env, lo);
3336 * declare storage for striping data
3338 info->lti_buf.lb_len = lov_mds_md_size(lo->ldo_stripenr,
3339 lo->ldo_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1);
3341 /* LOD can not choose OST objects for remote objects, i.e.
3342 * stripes must be ready before that. Right now, it can only
3343 * happen during migrate, i.e. migrate process needs to create
3344 * remote regular file (mdd_migrate_create), then the migrate
3345 * process will provide stripeEA. */
3346 LASSERT(lovea != NULL);
3347 info->lti_buf = *lovea;
3350 rc = dt_declare_xattr_set(env, next, &info->lti_buf,
3351 XATTR_NAME_LOV, 0, th);
3356 * if striping is created with local object's size > 0,
3357 * we have to propagate this size to specific object
3358 * the case is possible only when local object was created previously
3360 if (dt_object_exists(next))
3361 rc = lod_declare_init_size(env, dt, th);
3368 * Implementation of dt_object_operations::do_declare_create.
3370 * The method declares creation of a new object. If the object will be striped,
3371 * then helper functions are called to find FIDs for the stripes, declare
3372 * creation of the stripes and declare initialization of the striping
3373 * information to be stored in the master object.
3375 * \see dt_object_operations::do_declare_create() in the API description
3378 static int lod_declare_object_create(const struct lu_env *env,
3379 struct dt_object *dt,
3380 struct lu_attr *attr,
3381 struct dt_allocation_hint *hint,
3382 struct dt_object_format *dof,
3385 struct dt_object *next = dt_object_child(dt);
3386 struct lod_object *lo = lod_dt_obj(dt);
3395 * first of all, we declare creation of local object
3397 rc = dt_declare_create(env, next, attr, hint, dof, th);
3401 if (dof->dof_type == DFT_SYM)
3402 dt->do_body_ops = &lod_body_lnk_ops;
3405 * it's lod_ah_init() who has decided the object will striped
3407 if (dof->dof_type == DFT_REGULAR) {
3408 /* callers don't want stripes */
3409 /* XXX: all tricky interactions with ->ah_make_hint() decided
3410 * to use striping, then ->declare_create() behaving differently
3411 * should be cleaned */
3412 if (dof->u.dof_reg.striped == 0)
3413 lo->ldo_stripenr = 0;
3414 if (lo->ldo_stripenr > 0)
3415 rc = lod_declare_striped_object(env, dt, attr,
3417 } else if (dof->dof_type == DFT_DIR) {
3418 /* Orphan object (like migrating object) does not have
3419 * lod_dir_stripe, see lod_ah_init */
3420 if (lo->ldo_dir_stripe != NULL)
3421 rc = lod_declare_dir_striping_create(env, dt, attr,
3429 * Creation of a striped regular object.
3431 * The function is called to create the stripe objects for a regular
3432 * striped file. This can happen at the initial object creation or
3433 * when the caller asks LOD to do so using ->do_xattr_set() method
3434 * (so called late striping). Notice all the information are already
3435 * prepared in the form of the list of objects (ldo_stripe field).
3436 * This is done during declare phase.
3438 * \param[in] env execution environment
3439 * \param[in] dt object
3440 * \param[in] attr attributes the stripes will be created with
3441 * \param[in] dof format of stripes (see OSD API description)
3442 * \param[in] th transaction handle
3444 * \retval 0 on success
3445 * \retval negative if failed
3447 int lod_striping_create(const struct lu_env *env, struct dt_object *dt,
3448 struct lu_attr *attr, struct dt_object_format *dof,
3451 struct lod_object *lo = lod_dt_obj(dt);
3455 LASSERT(lo->ldo_striping_cached == 0);
3457 /* create all underlying objects */
3458 for (i = 0; i < lo->ldo_stripenr; i++) {
3459 LASSERT(lo->ldo_stripe[i]);
3460 rc = dt_create(env, lo->ldo_stripe[i], attr, NULL, dof, th);
3466 rc = lod_generate_and_set_lovea(env, lo, th);
3472 * Implementation of dt_object_operations::do_create.
3474 * If any of preceeding methods (like ->do_declare_create(),
3475 * ->do_ah_init(), etc) chose to create a striped object,
3476 * then this method will create the master and the stripes.
3478 * \see dt_object_operations::do_create() in the API description for details.
3480 static int lod_object_create(const struct lu_env *env, struct dt_object *dt,
3481 struct lu_attr *attr,
3482 struct dt_allocation_hint *hint,
3483 struct dt_object_format *dof, struct thandle *th)
3485 struct dt_object *next = dt_object_child(dt);
3486 struct lod_object *lo = lod_dt_obj(dt);
3490 /* create local object */
3491 rc = dt_create(env, next, attr, hint, dof, th);
3495 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3496 lo->ldo_stripe && dof->u.dof_reg.striped != 0)
3497 rc = lod_striping_create(env, dt, attr, dof, th);
3503 * Implementation of dt_object_operations::do_declare_destroy.
3505 * If the object is a striped directory, then the function declares reference
3506 * removal from the master object (this is an index) to the stripes and declares
3507 * destroy of all the stripes. In all the cases, it declares an intention to
3508 * destroy the object itself.
3510 * \see dt_object_operations::do_declare_destroy() in the API description
3513 static int lod_declare_object_destroy(const struct lu_env *env,
3514 struct dt_object *dt,
3517 struct dt_object *next = dt_object_child(dt);
3518 struct lod_object *lo = lod_dt_obj(dt);
3519 struct lod_thread_info *info = lod_env_info(env);
3520 char *stripe_name = info->lti_key;
3525 * load striping information, notice we don't do this when object
3526 * is being initialized as we don't need this information till
3527 * few specific cases like destroy, chown
3529 rc = lod_load_striping(env, lo);
3533 /* declare destroy for all underlying objects */
3534 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3535 rc = next->do_ops->do_index_try(env, next,
3536 &dt_directory_features);
3540 for (i = 0; i < lo->ldo_stripenr; i++) {
3541 rc = dt_declare_ref_del(env, next, th);
3544 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3545 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
3547 rc = dt_declare_delete(env, next,
3548 (const struct dt_key *)stripe_name, th);
3554 * we declare destroy for the local object
3556 rc = dt_declare_destroy(env, next, th);
3560 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ))
3563 /* declare destroy all striped objects */
3564 for (i = 0; i < lo->ldo_stripenr; i++) {
3565 if (likely(lo->ldo_stripe[i] != NULL)) {
3566 rc = dt_declare_destroy(env, lo->ldo_stripe[i], th);
3576 * Implementation of dt_object_operations::do_destroy.
3578 * If the object is a striped directory, then the function removes references
3579 * from the master object (this is an index) to the stripes and destroys all
3580 * the stripes. In all the cases, the function destroys the object itself.
3582 * \see dt_object_operations::do_destroy() in the API description for details.
3584 static int lod_object_destroy(const struct lu_env *env,
3585 struct dt_object *dt, struct thandle *th)
3587 struct dt_object *next = dt_object_child(dt);
3588 struct lod_object *lo = lod_dt_obj(dt);
3589 struct lod_thread_info *info = lod_env_info(env);
3590 char *stripe_name = info->lti_key;
3595 /* destroy sub-stripe of master object */
3596 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3597 rc = next->do_ops->do_index_try(env, next,
3598 &dt_directory_features);
3602 for (i = 0; i < lo->ldo_stripenr; i++) {
3603 rc = dt_ref_del(env, next, th);
3607 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3608 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
3611 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
3612 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
3613 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
3615 rc = dt_delete(env, next,
3616 (const struct dt_key *)stripe_name,
3622 rc = dt_destroy(env, next, th);
3626 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ))
3629 /* destroy all striped objects */
3630 for (i = 0; i < lo->ldo_stripenr; i++) {
3631 if (likely(lo->ldo_stripe[i] != NULL) &&
3632 (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
3633 i == cfs_fail_val)) {
3634 rc = dt_destroy(env, lo->ldo_stripe[i], th);
3644 * Implementation of dt_object_operations::do_declare_ref_add.
3646 * \see dt_object_operations::do_declare_ref_add() in the API description
3649 static int lod_declare_ref_add(const struct lu_env *env,
3650 struct dt_object *dt, struct thandle *th)
3652 return dt_declare_ref_add(env, dt_object_child(dt), th);
3656 * Implementation of dt_object_operations::do_ref_add.
3658 * \see dt_object_operations::do_ref_add() in the API description for details.
3660 static int lod_ref_add(const struct lu_env *env,
3661 struct dt_object *dt, struct thandle *th)
3663 return dt_ref_add(env, dt_object_child(dt), th);
3667 * Implementation of dt_object_operations::do_declare_ref_del.
3669 * \see dt_object_operations::do_declare_ref_del() in the API description
3672 static int lod_declare_ref_del(const struct lu_env *env,
3673 struct dt_object *dt, struct thandle *th)
3675 return dt_declare_ref_del(env, dt_object_child(dt), th);
3679 * Implementation of dt_object_operations::do_ref_del
3681 * \see dt_object_operations::do_ref_del() in the API description for details.
3683 static int lod_ref_del(const struct lu_env *env,
3684 struct dt_object *dt, struct thandle *th)
3686 return dt_ref_del(env, dt_object_child(dt), th);
3690 * Implementation of dt_object_operations::do_capa_get.
3692 * \see dt_object_operations::do_capa_get() in the API description for details.
3694 static struct obd_capa *lod_capa_get(const struct lu_env *env,
3695 struct dt_object *dt,
3696 struct lustre_capa *old, __u64 opc)
3698 return dt_capa_get(env, dt_object_child(dt), old, opc);
3702 * Implementation of dt_object_operations::do_object_sync.
3704 * \see dt_object_operations::do_object_sync() in the API description
3707 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
3708 __u64 start, __u64 end)
3710 return dt_object_sync(env, dt_object_child(dt), start, end);
3713 struct lod_slave_locks {
3715 struct lustre_handle lsl_handle[0];
3719 * Release LDLM locks on the stripes of a striped directory.
3721 * Iterates over all the locks taken on the stripe objects and
3722 * release them using ->do_object_unlock() method.
3724 * \param[in] env execution environment
3725 * \param[in] dt striped object
3726 * \param[in] einfo lock description
3727 * \param[in] policy data describing requested lock
3729 * \retval 0 on success
3730 * \retval negative if failed
3732 static int lod_object_unlock_internal(const struct lu_env *env,
3733 struct dt_object *dt,
3734 struct ldlm_enqueue_info *einfo,
3735 ldlm_policy_data_t *policy)
3737 struct lod_object *lo = lod_dt_obj(dt);
3738 struct lod_slave_locks *slave_locks = einfo->ei_cbdata;
3743 if (slave_locks == NULL)
3746 for (i = 1; i < slave_locks->lsl_lock_count; i++) {
3747 if (lustre_handle_is_used(&slave_locks->lsl_handle[i])) {
3750 einfo->ei_cbdata = &slave_locks->lsl_handle[i];
3751 rc1 = dt_object_unlock(env, lo->ldo_stripe[i], einfo,
3754 rc = rc == 0 ? rc1 : rc;
3762 * Implementation of dt_object_operations::do_object_unlock.
3764 * Used to release LDLM lock(s).
3766 * \see dt_object_operations::do_object_unlock() in the API description
3769 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
3770 struct ldlm_enqueue_info *einfo,
3771 union ldlm_policy_data *policy)
3773 struct lod_object *lo = lod_dt_obj(dt);
3774 struct lod_slave_locks *slave_locks = einfo->ei_cbdata;
3775 int slave_locks_size;
3779 if (slave_locks == NULL)
3782 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3785 rc = lod_load_striping(env, lo);
3789 /* Note: for remote lock for single stripe dir, MDT will cancel
3790 * the lock by lockh directly */
3791 if (lo->ldo_stripenr <= 1 && dt_object_remote(dt_object_child(dt)))
3794 /* Only cancel slave lock for striped dir */
3795 rc = lod_object_unlock_internal(env, dt, einfo, policy);
3797 slave_locks_size = sizeof(*slave_locks) + slave_locks->lsl_lock_count *
3798 sizeof(slave_locks->lsl_handle[0]);
3799 OBD_FREE(slave_locks, slave_locks_size);
3800 einfo->ei_cbdata = NULL;
3806 * Implementation of dt_object_operations::do_object_lock.
3808 * Used to get LDLM lock on the non-striped and striped objects.
3810 * \see dt_object_operations::do_object_lock() in the API description
3813 static int lod_object_lock(const struct lu_env *env,
3814 struct dt_object *dt,
3815 struct lustre_handle *lh,
3816 struct ldlm_enqueue_info *einfo,
3817 union ldlm_policy_data *policy)
3819 struct lod_object *lo = lod_dt_obj(dt);
3822 int slave_locks_size;
3823 struct lod_slave_locks *slave_locks = NULL;
3826 /* remote object lock */
3827 if (!einfo->ei_enq_slave) {
3828 LASSERT(dt_object_remote(dt));
3829 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
3833 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3836 rc = lod_load_striping(env, lo);
3841 if (lo->ldo_stripenr <= 1)
3844 slave_locks_size = sizeof(*slave_locks) + lo->ldo_stripenr *
3845 sizeof(slave_locks->lsl_handle[0]);
3846 /* Freed in lod_object_unlock */
3847 OBD_ALLOC(slave_locks, slave_locks_size);
3848 if (slave_locks == NULL)
3850 slave_locks->lsl_lock_count = lo->ldo_stripenr;
3852 /* striped directory lock */
3853 for (i = 1; i < lo->ldo_stripenr; i++) {
3854 struct lustre_handle lockh;
3855 struct ldlm_res_id *res_id;
3857 res_id = &lod_env_info(env)->lti_res_id;
3858 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
3860 einfo->ei_res_id = res_id;
3862 LASSERT(lo->ldo_stripe[i]);
3863 rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh, einfo,
3867 slave_locks->lsl_handle[i] = lockh;
3870 einfo->ei_cbdata = slave_locks;
3873 if (rc != 0 && slave_locks != NULL) {
3874 einfo->ei_cbdata = slave_locks;
3875 lod_object_unlock_internal(env, dt, einfo, policy);
3876 OBD_FREE(slave_locks, slave_locks_size);
3877 einfo->ei_cbdata = NULL;
3883 struct dt_object_operations lod_obj_ops = {
3884 .do_read_lock = lod_object_read_lock,
3885 .do_write_lock = lod_object_write_lock,
3886 .do_read_unlock = lod_object_read_unlock,
3887 .do_write_unlock = lod_object_write_unlock,
3888 .do_write_locked = lod_object_write_locked,
3889 .do_attr_get = lod_attr_get,
3890 .do_declare_attr_set = lod_declare_attr_set,
3891 .do_attr_set = lod_attr_set,
3892 .do_xattr_get = lod_xattr_get,
3893 .do_declare_xattr_set = lod_declare_xattr_set,
3894 .do_xattr_set = lod_xattr_set,
3895 .do_declare_xattr_del = lod_declare_xattr_del,
3896 .do_xattr_del = lod_xattr_del,
3897 .do_xattr_list = lod_xattr_list,
3898 .do_ah_init = lod_ah_init,
3899 .do_declare_create = lod_declare_object_create,
3900 .do_create = lod_object_create,
3901 .do_declare_destroy = lod_declare_object_destroy,
3902 .do_destroy = lod_object_destroy,
3903 .do_index_try = lod_index_try,
3904 .do_declare_ref_add = lod_declare_ref_add,
3905 .do_ref_add = lod_ref_add,
3906 .do_declare_ref_del = lod_declare_ref_del,
3907 .do_ref_del = lod_ref_del,
3908 .do_capa_get = lod_capa_get,
3909 .do_object_sync = lod_object_sync,
3910 .do_object_lock = lod_object_lock,
3911 .do_object_unlock = lod_object_unlock,
3915 * Implementation of dt_body_operations::dbo_read.
3917 * \see dt_body_operations::dbo_read() in the API description for details.
3919 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
3920 struct lu_buf *buf, loff_t *pos,
3921 struct lustre_capa *capa)
3923 struct dt_object *next = dt_object_child(dt);
3924 return next->do_body_ops->dbo_read(env, next, buf, pos, capa);
3928 * Implementation of dt_body_operations::dbo_declare_write.
3930 * \see dt_body_operations::dbo_declare_write() in the API description
3933 static ssize_t lod_declare_write(const struct lu_env *env,
3934 struct dt_object *dt,
3935 const struct lu_buf *buf, loff_t pos,
3938 return dt_declare_record_write(env, dt_object_child(dt),
3943 * Implementation of dt_body_operations::dbo_write.
3945 * \see dt_body_operations::dbo_write() in the API description for details.
3947 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
3948 const struct lu_buf *buf, loff_t *pos,
3949 struct thandle *th, struct lustre_capa *capa, int iq)
3951 struct dt_object *next = dt_object_child(dt);
3953 return next->do_body_ops->dbo_write(env, next, buf, pos, th, capa, iq);
3956 static const struct dt_body_operations lod_body_lnk_ops = {
3957 .dbo_read = lod_read,
3958 .dbo_declare_write = lod_declare_write,
3959 .dbo_write = lod_write
3963 * Implementation of lu_object_operations::loo_object_init.
3965 * The function determines the type and the index of the target device using
3966 * sequence of the object's FID. Then passes control down to the
3967 * corresponding device:
3968 * OSD for the local objects, OSP for remote
3970 * \see lu_object_operations::loo_object_init() in the API description
3973 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
3974 const struct lu_object_conf *conf)
3976 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
3977 struct lu_device *cdev = NULL;
3978 struct lu_object *cobj;
3979 struct lod_tgt_descs *ltd = NULL;
3980 struct lod_tgt_desc *tgt;
3982 int type = LU_SEQ_RANGE_ANY;
3986 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
3988 /* Note: Sometimes, it will Return EAGAIN here, see
3989 * ptrlpc_import_delay_req(), which might confuse
3990 * lu_object_find_at() and make it wait there incorrectly.
3991 * so we convert it to EIO here.*/
3998 if (type == LU_SEQ_RANGE_MDT &&
3999 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
4000 cdev = &lod->lod_child->dd_lu_dev;
4001 } else if (type == LU_SEQ_RANGE_MDT) {
4002 ltd = &lod->lod_mdt_descs;
4004 } else if (type == LU_SEQ_RANGE_OST) {
4005 ltd = &lod->lod_ost_descs;
4012 if (ltd->ltd_tgts_size > idx &&
4013 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
4014 tgt = LTD_TGT(ltd, idx);
4016 LASSERT(tgt != NULL);
4017 LASSERT(tgt->ltd_tgt != NULL);
4019 cdev = &(tgt->ltd_tgt->dd_lu_dev);
4021 lod_putref(lod, ltd);
4024 if (unlikely(cdev == NULL))
4027 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
4028 if (unlikely(cobj == NULL))
4031 lu_object_add(lo, cobj);
4038 * Release resources associated with striping.
4040 * If the object is striped (regular or directory), then release
4041 * the stripe objects references and free the ldo_stripe array.
4043 * \param[in] env execution environment
4044 * \param[in] lo object
4046 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
4050 if (lo->ldo_dir_stripe != NULL) {
4051 OBD_FREE_PTR(lo->ldo_dir_stripe);
4052 lo->ldo_dir_stripe = NULL;
4055 if (lo->ldo_stripe) {
4056 LASSERT(lo->ldo_stripes_allocated > 0);
4058 for (i = 0; i < lo->ldo_stripenr; i++) {
4059 if (lo->ldo_stripe[i])
4060 lu_object_put(env, &lo->ldo_stripe[i]->do_lu);
4063 i = sizeof(struct dt_object *) * lo->ldo_stripes_allocated;
4064 OBD_FREE(lo->ldo_stripe, i);
4065 lo->ldo_stripe = NULL;
4066 lo->ldo_stripes_allocated = 0;
4068 lo->ldo_stripenr = 0;
4069 lo->ldo_pattern = 0;
4073 * Implementation of lu_object_operations::loo_object_start.
4075 * \see lu_object_operations::loo_object_start() in the API description
4078 static int lod_object_start(const struct lu_env *env, struct lu_object *o)
4080 if (S_ISLNK(o->lo_header->loh_attr & S_IFMT))
4081 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_lnk_ops;
4086 * Implementation of lu_object_operations::loo_object_free.
4088 * \see lu_object_operations::loo_object_free() in the API description
4091 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
4093 struct lod_object *mo = lu2lod_obj(o);
4096 * release all underlying object pinned
4099 lod_object_free_striping(env, mo);
4101 lod_object_set_pool(mo, NULL);
4104 OBD_SLAB_FREE_PTR(mo, lod_object_kmem);
4108 * Implementation of lu_object_operations::loo_object_release.
4110 * \see lu_object_operations::loo_object_release() in the API description
4113 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
4115 /* XXX: shouldn't we release everything here in case if object
4116 * creation failed before? */
4120 * Implementation of lu_object_operations::loo_object_print.
4122 * \see lu_object_operations::loo_object_print() in the API description
4125 static int lod_object_print(const struct lu_env *env, void *cookie,
4126 lu_printer_t p, const struct lu_object *l)
4128 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
4130 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
4133 struct lu_object_operations lod_lu_obj_ops = {
4134 .loo_object_init = lod_object_init,
4135 .loo_object_start = lod_object_start,
4136 .loo_object_free = lod_object_free,
4137 .loo_object_release = lod_object_release,
4138 .loo_object_print = lod_object_print,