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, 2014, 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 static const struct dt_body_operations lod_body_lnk_ops;
62 static const struct dt_body_operations lod_body_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)
74 struct dt_object *next = dt_object_child(dt);
75 return next->do_index_ops->dio_lookup(env, next, rec, key);
79 * Implementation of dt_index_operations::dio_declare_insert.
81 * Used with regular (non-striped) objects.
83 * \see dt_index_operations::dio_declare_insert() in the API description
86 static int lod_declare_index_insert(const struct lu_env *env,
88 const struct dt_rec *rec,
89 const struct dt_key *key,
92 return lod_sub_object_declare_insert(env, dt_object_child(dt),
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,
110 return lod_sub_object_index_insert(env, dt_object_child(dt), rec, key,
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 lod_sub_object_declare_delete(env, dt_object_child(dt), key,
132 * Implementation of dt_index_operations::dio_delete.
134 * Used with regular (non-striped) objects.
136 * \see dt_index_operations::dio_delete() in the API description for details.
138 static int lod_index_delete(const struct lu_env *env,
139 struct dt_object *dt,
140 const struct dt_key *key,
143 return lod_sub_object_delete(env, dt_object_child(dt), key, th);
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)
156 struct dt_object *next = dt_object_child(dt);
157 struct lod_it *it = &lod_env_info(env)->lti_it;
158 struct dt_it *it_next;
160 it_next = next->do_index_ops->dio_it.init(env, next, attr);
164 /* currently we do not use more than one iterator per thread
165 * so we store it in thread info. if at some point we need
166 * more active iterators in a single thread, we can allocate
168 LASSERT(it->lit_obj == NULL);
170 it->lit_it = it_next;
173 return (struct dt_it *)it;
176 #define LOD_CHECK_IT(env, it) \
178 LASSERT((it)->lit_obj != NULL); \
179 LASSERT((it)->lit_it != NULL); \
183 * Implementation of dt_index_operations::dio_it.fini.
185 * Used with regular (non-striped) objects.
187 * \see dt_index_operations::dio_it.fini() in the API description for details.
189 static void lod_it_fini(const struct lu_env *env, struct dt_it *di)
191 struct lod_it *it = (struct lod_it *)di;
193 LOD_CHECK_IT(env, it);
194 it->lit_obj->do_index_ops->dio_it.fini(env, it->lit_it);
196 /* the iterator not in use any more */
202 * Implementation of dt_it_ops::get.
204 * Used with regular (non-striped) objects.
206 * \see dt_it_ops::get() in the API description for details.
208 static int lod_it_get(const struct lu_env *env, struct dt_it *di,
209 const struct dt_key *key)
211 const struct lod_it *it = (const struct lod_it *)di;
213 LOD_CHECK_IT(env, it);
214 return it->lit_obj->do_index_ops->dio_it.get(env, it->lit_it, key);
218 * Implementation of dt_it_ops::put.
220 * Used with regular (non-striped) objects.
222 * \see dt_it_ops::put() in the API description for details.
224 static void lod_it_put(const struct lu_env *env, struct dt_it *di)
226 struct lod_it *it = (struct lod_it *)di;
228 LOD_CHECK_IT(env, it);
229 return it->lit_obj->do_index_ops->dio_it.put(env, it->lit_it);
233 * Implementation of dt_it_ops::next.
235 * Used with regular (non-striped) objects
237 * \see dt_it_ops::next() in the API description for details.
239 static int lod_it_next(const struct lu_env *env, struct dt_it *di)
241 struct lod_it *it = (struct lod_it *)di;
243 LOD_CHECK_IT(env, it);
244 return it->lit_obj->do_index_ops->dio_it.next(env, it->lit_it);
248 * Implementation of dt_it_ops::key.
250 * Used with regular (non-striped) objects.
252 * \see dt_it_ops::key() in the API description for details.
254 static struct dt_key *lod_it_key(const struct lu_env *env,
255 const struct dt_it *di)
257 const struct lod_it *it = (const struct lod_it *)di;
259 LOD_CHECK_IT(env, it);
260 return it->lit_obj->do_index_ops->dio_it.key(env, it->lit_it);
264 * Implementation of dt_it_ops::key_size.
266 * Used with regular (non-striped) objects.
268 * \see dt_it_ops::key_size() in the API description for details.
270 static int lod_it_key_size(const struct lu_env *env, const struct dt_it *di)
272 struct lod_it *it = (struct lod_it *)di;
274 LOD_CHECK_IT(env, it);
275 return it->lit_obj->do_index_ops->dio_it.key_size(env, it->lit_it);
279 * Implementation of dt_it_ops::rec.
281 * Used with regular (non-striped) objects.
283 * \see dt_it_ops::rec() in the API description for details.
285 static int lod_it_rec(const struct lu_env *env, const struct dt_it *di,
286 struct dt_rec *rec, __u32 attr)
288 const struct lod_it *it = (const struct lod_it *)di;
290 LOD_CHECK_IT(env, it);
291 return it->lit_obj->do_index_ops->dio_it.rec(env, it->lit_it, rec,
296 * Implementation of dt_it_ops::rec_size.
298 * Used with regular (non-striped) objects.
300 * \see dt_it_ops::rec_size() in the API description for details.
302 static int lod_it_rec_size(const struct lu_env *env, const struct dt_it *di,
305 const struct lod_it *it = (const struct lod_it *)di;
307 LOD_CHECK_IT(env, it);
308 return it->lit_obj->do_index_ops->dio_it.rec_size(env, it->lit_it,
313 * Implementation of dt_it_ops::store.
315 * Used with regular (non-striped) objects.
317 * \see dt_it_ops::store() in the API description for details.
319 static __u64 lod_it_store(const struct lu_env *env, const struct dt_it *di)
321 const struct lod_it *it = (const struct lod_it *)di;
323 LOD_CHECK_IT(env, it);
324 return it->lit_obj->do_index_ops->dio_it.store(env, it->lit_it);
328 * Implementation of dt_it_ops::load.
330 * Used with regular (non-striped) objects.
332 * \see dt_it_ops::load() in the API description for details.
334 static int lod_it_load(const struct lu_env *env, const struct dt_it *di,
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 static 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)
393 struct lod_object *lo = lod_dt_obj(dt);
394 struct dt_object *next;
395 struct lod_it *it = &lod_env_info(env)->lti_it;
396 struct dt_it *it_next;
399 LASSERT(lo->ldo_stripenr > 0);
400 next = lo->ldo_stripe[0];
401 LASSERT(next != NULL);
402 LASSERT(next->do_index_ops != NULL);
404 it_next = next->do_index_ops->dio_it.init(env, next, attr);
408 /* currently we do not use more than one iterator per thread
409 * so we store it in thread info. if at some point we need
410 * more active iterators in a single thread, we can allocate
412 LASSERT(it->lit_obj == NULL);
414 it->lit_stripe_index = 0;
416 it->lit_it = it_next;
419 return (struct dt_it *)it;
422 #define LOD_CHECK_STRIPED_IT(env, it, lo) \
424 LASSERT((it)->lit_obj != NULL); \
425 LASSERT((it)->lit_it != NULL); \
426 LASSERT((lo)->ldo_stripenr > 0); \
427 LASSERT((it)->lit_stripe_index < (lo)->ldo_stripenr); \
431 * Implementation of dt_it_ops::fini.
433 * Used with striped objects.
435 * \see dt_it_ops::fini() in the API description for details.
437 static void lod_striped_it_fini(const struct lu_env *env, struct dt_it *di)
439 struct lod_it *it = (struct lod_it *)di;
440 struct lod_object *lo = lod_dt_obj(it->lit_obj);
441 struct dt_object *next;
443 /* If lit_it == NULL, then it means the sub_it has been finished,
444 * which only happens in failure cases, see lod_striped_it_next() */
445 if (it->lit_it != NULL) {
446 LOD_CHECK_STRIPED_IT(env, it, lo);
448 next = lo->ldo_stripe[it->lit_stripe_index];
449 LASSERT(next != NULL);
450 LASSERT(next->do_index_ops != NULL);
452 next->do_index_ops->dio_it.fini(env, it->lit_it);
455 /* the iterator not in use any more */
458 it->lit_stripe_index = 0;
462 * Implementation of dt_it_ops::get.
464 * Right now it's not used widely, only to reset the iterator to the
465 * initial position. It should be possible to implement a full version
466 * which chooses a correct stripe to be able to position with any key.
468 * \see dt_it_ops::get() in the API description for details.
470 static int lod_striped_it_get(const struct lu_env *env, struct dt_it *di,
471 const struct dt_key *key)
473 const struct lod_it *it = (const struct lod_it *)di;
474 struct lod_object *lo = lod_dt_obj(it->lit_obj);
475 struct dt_object *next;
478 LOD_CHECK_STRIPED_IT(env, it, lo);
480 next = lo->ldo_stripe[it->lit_stripe_index];
481 LASSERT(next != NULL);
482 LASSERT(next->do_index_ops != NULL);
484 return next->do_index_ops->dio_it.get(env, it->lit_it, key);
488 * Implementation of dt_it_ops::put.
490 * Used with striped objects.
492 * \see dt_it_ops::put() in the API description for details.
494 static void lod_striped_it_put(const struct lu_env *env, struct dt_it *di)
496 struct lod_it *it = (struct lod_it *)di;
497 struct lod_object *lo = lod_dt_obj(it->lit_obj);
498 struct dt_object *next;
500 LOD_CHECK_STRIPED_IT(env, it, lo);
502 next = lo->ldo_stripe[it->lit_stripe_index];
503 LASSERT(next != NULL);
504 LASSERT(next->do_index_ops != NULL);
506 return next->do_index_ops->dio_it.put(env, it->lit_it);
510 * Implementation of dt_it_ops::next.
512 * Used with striped objects. When the end of the current stripe is
513 * reached, the method takes the next stripe's iterator.
515 * \see dt_it_ops::next() in the API description for details.
517 static int lod_striped_it_next(const struct lu_env *env, struct dt_it *di)
519 struct lod_it *it = (struct lod_it *)di;
520 struct lod_object *lo = lod_dt_obj(it->lit_obj);
521 struct dt_object *next;
522 struct dt_it *it_next;
526 LOD_CHECK_STRIPED_IT(env, it, lo);
528 next = lo->ldo_stripe[it->lit_stripe_index];
529 LASSERT(next != NULL);
530 LASSERT(next->do_index_ops != NULL);
532 rc = next->do_index_ops->dio_it.next(env, it->lit_it);
536 if (rc == 0 && it->lit_stripe_index == 0)
539 if (rc == 0 && it->lit_stripe_index > 0) {
540 struct lu_dirent *ent;
542 ent = (struct lu_dirent *)lod_env_info(env)->lti_key;
544 rc = next->do_index_ops->dio_it.rec(env, it->lit_it,
545 (struct dt_rec *)ent,
550 /* skip . and .. for slave stripe */
551 if ((strncmp(ent->lde_name, ".",
552 le16_to_cpu(ent->lde_namelen)) == 0 &&
553 le16_to_cpu(ent->lde_namelen) == 1) ||
554 (strncmp(ent->lde_name, "..",
555 le16_to_cpu(ent->lde_namelen)) == 0 &&
556 le16_to_cpu(ent->lde_namelen) == 2))
562 /* go to next stripe */
563 if (it->lit_stripe_index + 1 >= lo->ldo_stripenr)
566 it->lit_stripe_index++;
568 next->do_index_ops->dio_it.put(env, it->lit_it);
569 next->do_index_ops->dio_it.fini(env, it->lit_it);
572 next = lo->ldo_stripe[it->lit_stripe_index];
573 LASSERT(next != NULL);
574 LASSERT(next->do_index_ops != NULL);
576 rc = next->do_ops->do_index_try(env, next, &dt_directory_features);
580 it_next = next->do_index_ops->dio_it.init(env, next, it->lit_attr);
581 if (!IS_ERR(it_next)) {
582 it->lit_it = it_next;
585 rc = PTR_ERR(it_next);
592 * Implementation of dt_it_ops::key.
594 * Used with striped objects.
596 * \see dt_it_ops::key() in the API description for details.
598 static struct dt_key *lod_striped_it_key(const struct lu_env *env,
599 const struct dt_it *di)
601 const struct lod_it *it = (const struct lod_it *)di;
602 struct lod_object *lo = lod_dt_obj(it->lit_obj);
603 struct dt_object *next;
605 LOD_CHECK_STRIPED_IT(env, it, lo);
607 next = lo->ldo_stripe[it->lit_stripe_index];
608 LASSERT(next != NULL);
609 LASSERT(next->do_index_ops != NULL);
611 return next->do_index_ops->dio_it.key(env, it->lit_it);
615 * Implementation of dt_it_ops::key_size.
617 * Used with striped objects.
619 * \see dt_it_ops::size() in the API description for details.
621 static int lod_striped_it_key_size(const struct lu_env *env,
622 const struct dt_it *di)
624 struct lod_it *it = (struct lod_it *)di;
625 struct lod_object *lo = lod_dt_obj(it->lit_obj);
626 struct dt_object *next;
628 LOD_CHECK_STRIPED_IT(env, it, lo);
630 next = lo->ldo_stripe[it->lit_stripe_index];
631 LASSERT(next != NULL);
632 LASSERT(next->do_index_ops != NULL);
634 return next->do_index_ops->dio_it.key_size(env, it->lit_it);
638 * Implementation of dt_it_ops::rec.
640 * Used with striped objects.
642 * \see dt_it_ops::rec() in the API description for details.
644 static int lod_striped_it_rec(const struct lu_env *env, const struct dt_it *di,
645 struct dt_rec *rec, __u32 attr)
647 const struct lod_it *it = (const struct lod_it *)di;
648 struct lod_object *lo = lod_dt_obj(it->lit_obj);
649 struct dt_object *next;
651 LOD_CHECK_STRIPED_IT(env, it, lo);
653 next = lo->ldo_stripe[it->lit_stripe_index];
654 LASSERT(next != NULL);
655 LASSERT(next->do_index_ops != NULL);
657 return next->do_index_ops->dio_it.rec(env, it->lit_it, rec, attr);
661 * Implementation of dt_it_ops::rec_size.
663 * Used with striped objects.
665 * \see dt_it_ops::rec_size() in the API description for details.
667 static int lod_striped_it_rec_size(const struct lu_env *env,
668 const struct dt_it *di, __u32 attr)
670 struct lod_it *it = (struct lod_it *)di;
671 struct lod_object *lo = lod_dt_obj(it->lit_obj);
672 struct dt_object *next;
674 LOD_CHECK_STRIPED_IT(env, it, lo);
676 next = lo->ldo_stripe[it->lit_stripe_index];
677 LASSERT(next != NULL);
678 LASSERT(next->do_index_ops != NULL);
680 return next->do_index_ops->dio_it.rec_size(env, it->lit_it, attr);
684 * Implementation of dt_it_ops::store.
686 * Used with striped objects.
688 * \see dt_it_ops::store() in the API description for details.
690 static __u64 lod_striped_it_store(const struct lu_env *env,
691 const struct dt_it *di)
693 const struct lod_it *it = (const struct lod_it *)di;
694 struct lod_object *lo = lod_dt_obj(it->lit_obj);
695 struct dt_object *next;
697 LOD_CHECK_STRIPED_IT(env, it, lo);
699 next = lo->ldo_stripe[it->lit_stripe_index];
700 LASSERT(next != NULL);
701 LASSERT(next->do_index_ops != NULL);
703 return next->do_index_ops->dio_it.store(env, it->lit_it);
707 * Implementation of dt_it_ops::load.
709 * Used with striped objects.
711 * \see dt_it_ops::load() in the API description for details.
713 static int lod_striped_it_load(const struct lu_env *env,
714 const struct dt_it *di, __u64 hash)
716 const struct lod_it *it = (const struct lod_it *)di;
717 struct lod_object *lo = lod_dt_obj(it->lit_obj);
718 struct dt_object *next;
720 LOD_CHECK_STRIPED_IT(env, it, lo);
722 next = lo->ldo_stripe[it->lit_stripe_index];
723 LASSERT(next != NULL);
724 LASSERT(next->do_index_ops != NULL);
726 return next->do_index_ops->dio_it.load(env, it->lit_it, hash);
729 static struct dt_index_operations lod_striped_index_ops = {
730 .dio_lookup = lod_index_lookup,
731 .dio_declare_insert = lod_declare_index_insert,
732 .dio_insert = lod_index_insert,
733 .dio_declare_delete = lod_declare_index_delete,
734 .dio_delete = lod_index_delete,
736 .init = lod_striped_it_init,
737 .fini = lod_striped_it_fini,
738 .get = lod_striped_it_get,
739 .put = lod_striped_it_put,
740 .next = lod_striped_it_next,
741 .key = lod_striped_it_key,
742 .key_size = lod_striped_it_key_size,
743 .rec = lod_striped_it_rec,
744 .rec_size = lod_striped_it_rec_size,
745 .store = lod_striped_it_store,
746 .load = lod_striped_it_load,
751 * Append the FID for each shard of the striped directory after the
752 * given LMV EA header.
754 * To simplify striped directory and the consistency verification,
755 * we only store the LMV EA header on disk, for both master object
756 * and slave objects. When someone wants to know the whole LMV EA,
757 * such as client readdir(), we can build the entrie LMV EA on the
758 * MDT side (in RAM) via iterating the sub-directory entries that
759 * are contained in the master object of the stripe directory.
761 * For the master object of the striped directroy, the valid name
762 * for each shard is composed of the ${shard_FID}:${shard_idx}.
764 * There may be holes in the LMV EA if some shards' name entries
765 * are corrupted or lost.
767 * \param[in] env pointer to the thread context
768 * \param[in] lo pointer to the master object of the striped directory
769 * \param[in] buf pointer to the lu_buf which will hold the LMV EA
770 * \param[in] resize whether re-allocate the buffer if it is not big enough
772 * \retval positive size of the LMV EA
773 * \retval 0 for nothing to be loaded
774 * \retval negative error number on failure
776 int lod_load_lmv_shards(const struct lu_env *env, struct lod_object *lo,
777 struct lu_buf *buf, bool resize)
779 struct lu_dirent *ent =
780 (struct lu_dirent *)lod_env_info(env)->lti_key;
781 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
782 struct dt_object *obj = dt_object_child(&lo->ldo_obj);
783 struct lmv_mds_md_v1 *lmv1 = buf->lb_buf;
785 const struct dt_it_ops *iops;
787 __u32 magic = le32_to_cpu(lmv1->lmv_magic);
792 /* If it is not a striped directory, then load nothing. */
793 if (magic != LMV_MAGIC_V1)
796 /* If it is in migration (or failure), then load nothing. */
797 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
800 stripes = le32_to_cpu(lmv1->lmv_stripe_count);
804 rc = lmv_mds_md_size(stripes, magic);
808 if (buf->lb_len < lmv1_size) {
817 lu_buf_alloc(buf, lmv1_size);
822 memcpy(buf->lb_buf, tbuf.lb_buf, tbuf.lb_len);
825 if (unlikely(!dt_try_as_dir(env, obj)))
828 memset(&lmv1->lmv_stripe_fids[0], 0, stripes * sizeof(struct lu_fid));
829 iops = &obj->do_index_ops->dio_it;
830 it = iops->init(env, obj, LUDA_64BITHASH);
834 rc = iops->load(env, it, 0);
836 rc = iops->next(env, it);
841 char name[FID_LEN + 2] = "";
846 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
852 fid_le_to_cpu(&fid, &ent->lde_fid);
853 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
854 if (ent->lde_name[0] == '.') {
855 if (ent->lde_namelen == 1)
858 if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
862 len = snprintf(name, sizeof(name),
863 DFID":", PFID(&ent->lde_fid));
864 /* The ent->lde_name is composed of ${FID}:${index} */
865 if (ent->lde_namelen < len + 1 ||
866 memcmp(ent->lde_name, name, len) != 0) {
867 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
868 "%s: invalid shard name %.*s with the FID "DFID
869 " for the striped directory "DFID", %s\n",
870 lod2obd(lod)->obd_name, ent->lde_namelen,
871 ent->lde_name, PFID(&fid),
872 PFID(lu_object_fid(&obj->do_lu)),
873 lod->lod_lmv_failout ? "failout" : "skip");
875 if (lod->lod_lmv_failout)
883 if (ent->lde_name[len] < '0' ||
884 ent->lde_name[len] > '9') {
885 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
886 "%s: invalid shard name %.*s with the "
887 "FID "DFID" for the striped directory "
889 lod2obd(lod)->obd_name, ent->lde_namelen,
890 ent->lde_name, PFID(&fid),
891 PFID(lu_object_fid(&obj->do_lu)),
892 lod->lod_lmv_failout ?
895 if (lod->lod_lmv_failout)
901 index = index * 10 + ent->lde_name[len++] - '0';
902 } while (len < ent->lde_namelen);
904 if (len == ent->lde_namelen) {
905 /* Out of LMV EA range. */
906 if (index >= stripes) {
907 CERROR("%s: the shard %.*s for the striped "
908 "directory "DFID" is out of the known "
909 "LMV EA range [0 - %u], failout\n",
910 lod2obd(lod)->obd_name, ent->lde_namelen,
912 PFID(lu_object_fid(&obj->do_lu)),
918 /* The slot has been occupied. */
919 if (!fid_is_zero(&lmv1->lmv_stripe_fids[index])) {
923 &lmv1->lmv_stripe_fids[index]);
924 CERROR("%s: both the shard "DFID" and "DFID
925 " for the striped directory "DFID
926 " claim the same LMV EA slot at the "
927 "index %d, failout\n",
928 lod2obd(lod)->obd_name,
929 PFID(&fid0), PFID(&fid),
930 PFID(lu_object_fid(&obj->do_lu)), index);
935 /* stored as LE mode */
936 lmv1->lmv_stripe_fids[index] = ent->lde_fid;
939 rc = iops->next(env, it);
946 RETURN(rc > 0 ? lmv_mds_md_size(stripes, magic) : rc);
950 * Implementation of dt_object_operations::do_index_try.
952 * \see dt_object_operations::do_index_try() in the API description for details.
954 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
955 const struct dt_index_features *feat)
957 struct lod_object *lo = lod_dt_obj(dt);
958 struct dt_object *next = dt_object_child(dt);
962 LASSERT(next->do_ops);
963 LASSERT(next->do_ops->do_index_try);
965 rc = lod_load_striping_locked(env, lo);
969 rc = next->do_ops->do_index_try(env, next, feat);
973 if (lo->ldo_stripenr > 0) {
976 for (i = 0; i < lo->ldo_stripenr; i++) {
977 if (dt_object_exists(lo->ldo_stripe[i]) == 0)
979 rc = lo->ldo_stripe[i]->do_ops->do_index_try(env,
980 lo->ldo_stripe[i], feat);
984 dt->do_index_ops = &lod_striped_index_ops;
986 dt->do_index_ops = &lod_index_ops;
993 * Implementation of dt_object_operations::do_read_lock.
995 * \see dt_object_operations::do_read_lock() in the API description for details.
997 static void lod_object_read_lock(const struct lu_env *env,
998 struct dt_object *dt, unsigned role)
1000 dt_read_lock(env, dt_object_child(dt), role);
1004 * Implementation of dt_object_operations::do_write_lock.
1006 * \see dt_object_operations::do_write_lock() in the API description for
1009 static void lod_object_write_lock(const struct lu_env *env,
1010 struct dt_object *dt, unsigned role)
1012 dt_write_lock(env, dt_object_child(dt), role);
1016 * Implementation of dt_object_operations::do_read_unlock.
1018 * \see dt_object_operations::do_read_unlock() in the API description for
1021 static void lod_object_read_unlock(const struct lu_env *env,
1022 struct dt_object *dt)
1024 dt_read_unlock(env, dt_object_child(dt));
1028 * Implementation of dt_object_operations::do_write_unlock.
1030 * \see dt_object_operations::do_write_unlock() in the API description for
1033 static void lod_object_write_unlock(const struct lu_env *env,
1034 struct dt_object *dt)
1036 dt_write_unlock(env, dt_object_child(dt));
1040 * Implementation of dt_object_operations::do_write_locked.
1042 * \see dt_object_operations::do_write_locked() in the API description for
1045 static int lod_object_write_locked(const struct lu_env *env,
1046 struct dt_object *dt)
1048 return dt_write_locked(env, dt_object_child(dt));
1052 * Implementation of dt_object_operations::do_attr_get.
1054 * \see dt_object_operations::do_attr_get() in the API description for details.
1056 static int lod_attr_get(const struct lu_env *env,
1057 struct dt_object *dt,
1058 struct lu_attr *attr)
1060 /* Note: for striped directory, client will merge attributes
1061 * from all of the sub-stripes see lmv_merge_attr(), and there
1062 * no MDD logic depend on directory nlink/size/time, so we can
1063 * always use master inode nlink and size for now. */
1064 return dt_attr_get(env, dt_object_child(dt), attr);
1068 * Mark all of the striped directory sub-stripes dead.
1070 * When a striped object is a subject to removal, we have
1071 * to mark all the stripes to prevent further access to
1072 * them (e.g. create a new file in those). So we mark
1073 * all the stripes with LMV_HASH_FLAG_DEAD. The function
1074 * can be used to declare the changes and to apply them.
1075 * If the object isn't striped, then just return success.
1077 * \param[in] env execution environment
1078 * \param[in] dt the striped object
1079 * \param[in] handle transaction handle
1080 * \param[in] declare whether to declare the change or apply
1082 * \retval 0 on success
1083 * \retval negative if failed
1085 static int lod_mark_dead_object(const struct lu_env *env,
1086 struct dt_object *dt,
1090 struct lod_object *lo = lod_dt_obj(dt);
1091 struct lmv_mds_md_v1 *lmv;
1092 __u32 dead_hash_type;
1098 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
1101 rc = lod_load_striping_locked(env, lo);
1105 if (lo->ldo_stripenr == 0)
1108 rc = lod_get_lmv_ea(env, lo);
1112 lmv = lod_env_info(env)->lti_ea_store;
1113 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1114 dead_hash_type = le32_to_cpu(lmv->lmv_hash_type) | LMV_HASH_FLAG_DEAD;
1115 lmv->lmv_hash_type = cpu_to_le32(dead_hash_type);
1116 for (i = 0; i < lo->ldo_stripenr; i++) {
1119 lmv->lmv_master_mdt_index = i;
1121 buf.lb_len = sizeof(*lmv);
1123 rc = lod_sub_object_declare_xattr_set(env,
1124 lo->ldo_stripe[i], &buf,
1126 LU_XATTR_REPLACE, th);
1128 rc = lod_sub_object_xattr_set(env, lo->ldo_stripe[i],
1129 &buf, XATTR_NAME_LMV,
1130 LU_XATTR_REPLACE, th);
1140 * Implementation of dt_object_operations::do_declare_attr_set.
1142 * If the object is striped, then apply the changes to all the stripes.
1144 * \see dt_object_operations::do_declare_attr_set() in the API description
1147 static int lod_declare_attr_set(const struct lu_env *env,
1148 struct dt_object *dt,
1149 const struct lu_attr *attr,
1152 struct dt_object *next = dt_object_child(dt);
1153 struct lod_object *lo = lod_dt_obj(dt);
1157 /* Set dead object on all other stripes */
1158 if (attr->la_valid & LA_FLAGS && !(attr->la_valid & ~LA_FLAGS) &&
1159 attr->la_flags & LUSTRE_SLAVE_DEAD_FL) {
1160 rc = lod_mark_dead_object(env, dt, th, true);
1165 * declare setattr on the local object
1167 rc = lod_sub_object_declare_attr_set(env, next, attr, th);
1171 /* osp_declare_attr_set() ignores all attributes other than
1172 * UID, GID, and size, and osp_attr_set() ignores all but UID
1173 * and GID. Declaration of size attr setting happens through
1174 * lod_declare_init_size(), and not through this function.
1175 * Therefore we need not load striping unless ownership is
1176 * changing. This should save memory and (we hope) speed up
1178 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1179 if (!(attr->la_valid & (LA_UID | LA_GID)))
1182 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1185 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE |
1186 LA_ATIME | LA_MTIME | LA_CTIME |
1191 * load striping information, notice we don't do this when object
1192 * is being initialized as we don't need this information till
1193 * few specific cases like destroy, chown
1195 rc = lod_load_striping(env, lo);
1199 if (lo->ldo_stripenr == 0)
1203 * if object is striped declare changes on the stripes
1205 LASSERT(lo->ldo_stripe);
1206 for (i = 0; i < lo->ldo_stripenr; i++) {
1207 if (lo->ldo_stripe[i] == NULL)
1209 rc = lod_sub_object_declare_attr_set(env,
1210 lo->ldo_stripe[i], attr,
1216 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE) &&
1217 dt_object_exists(next) != 0 &&
1218 dt_object_remote(next) == 0)
1219 lod_sub_object_declare_xattr_del(env, next,
1220 XATTR_NAME_LOV, th);
1222 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) &&
1223 dt_object_exists(next) &&
1224 dt_object_remote(next) == 0 && S_ISREG(attr->la_mode)) {
1225 struct lod_thread_info *info = lod_env_info(env);
1226 struct lu_buf *buf = &info->lti_buf;
1228 buf->lb_buf = info->lti_ea_store;
1229 buf->lb_len = info->lti_ea_store_size;
1230 lod_sub_object_declare_xattr_set(env, next, buf,
1232 LU_XATTR_REPLACE, th);
1239 * Implementation of dt_object_operations::do_attr_set.
1241 * If the object is striped, then apply the changes to all or subset of
1242 * the stripes depending on the object type and specific attributes.
1244 * \see dt_object_operations::do_attr_set() in the API description for details.
1246 static int lod_attr_set(const struct lu_env *env,
1247 struct dt_object *dt,
1248 const struct lu_attr *attr,
1251 struct dt_object *next = dt_object_child(dt);
1252 struct lod_object *lo = lod_dt_obj(dt);
1256 /* Set dead object on all other stripes */
1257 if (attr->la_valid & LA_FLAGS && !(attr->la_valid & ~LA_FLAGS) &&
1258 attr->la_flags & LUSTRE_SLAVE_DEAD_FL) {
1259 rc = lod_mark_dead_object(env, dt, th, false);
1264 * apply changes to the local object
1266 rc = lod_sub_object_attr_set(env, next, attr, th);
1270 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1271 if (!(attr->la_valid & (LA_UID | LA_GID)))
1274 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1277 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE |
1278 LA_ATIME | LA_MTIME | LA_CTIME |
1283 if (lo->ldo_stripenr == 0)
1287 * if object is striped, apply changes to all the stripes
1289 LASSERT(lo->ldo_stripe);
1290 for (i = 0; i < lo->ldo_stripenr; i++) {
1291 if (unlikely(lo->ldo_stripe[i] == NULL))
1294 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
1295 (dt_object_exists(lo->ldo_stripe[i]) == 0))
1298 rc = lod_sub_object_attr_set(env, lo->ldo_stripe[i], attr, th);
1303 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE) &&
1304 dt_object_exists(next) != 0 &&
1305 dt_object_remote(next) == 0)
1306 rc = lod_sub_object_xattr_del(env, next, XATTR_NAME_LOV, th);
1308 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) &&
1309 dt_object_exists(next) &&
1310 dt_object_remote(next) == 0 && S_ISREG(attr->la_mode)) {
1311 struct lod_thread_info *info = lod_env_info(env);
1312 struct lu_buf *buf = &info->lti_buf;
1313 struct ost_id *oi = &info->lti_ostid;
1314 struct lu_fid *fid = &info->lti_fid;
1315 struct lov_mds_md_v1 *lmm;
1316 struct lov_ost_data_v1 *objs;
1320 rc1 = lod_get_lov_ea(env, lo);
1324 buf->lb_buf = info->lti_ea_store;
1325 buf->lb_len = info->lti_ea_store_size;
1326 lmm = info->lti_ea_store;
1327 magic = le32_to_cpu(lmm->lmm_magic);
1328 if (magic == LOV_MAGIC_V1)
1329 objs = &(lmm->lmm_objects[0]);
1331 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1332 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1333 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1335 fid_to_ostid(fid, oi);
1336 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1338 rc = lod_sub_object_xattr_set(env, next, buf, XATTR_NAME_LOV,
1339 LU_XATTR_REPLACE, th);
1346 * Implementation of dt_object_operations::do_xattr_get.
1348 * If LOV EA is requested from the root object and it's not
1349 * found, then return default striping for the filesystem.
1351 * \see dt_object_operations::do_xattr_get() in the API description for details.
1353 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1354 struct lu_buf *buf, const char *name)
1356 struct lod_thread_info *info = lod_env_info(env);
1357 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1361 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1362 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1363 struct lmv_mds_md_v1 *lmv1;
1366 if (rc > (typeof(rc))sizeof(*lmv1))
1369 if (rc < (typeof(rc))sizeof(*lmv1))
1370 RETURN(rc = rc > 0 ? -EINVAL : rc);
1372 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1373 CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1375 info->lti_buf.lb_buf = info->lti_key;
1376 info->lti_buf.lb_len = sizeof(*lmv1);
1377 rc = dt_xattr_get(env, dt_object_child(dt),
1378 &info->lti_buf, name);
1379 if (unlikely(rc != sizeof(*lmv1)))
1380 RETURN(rc = rc > 0 ? -EINVAL : rc);
1382 lmv1 = info->lti_buf.lb_buf;
1383 /* The on-disk LMV EA only contains header, but the
1384 * returned LMV EA size should contain the space for
1385 * the FIDs of all shards of the striped directory. */
1386 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1387 rc = lmv_mds_md_size(
1388 le32_to_cpu(lmv1->lmv_stripe_count),
1391 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1395 RETURN(rc = rc1 != 0 ? rc1 : rc);
1398 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1402 * lod returns default striping on the real root of the device
1403 * this is like the root stores default striping for the whole
1404 * filesystem. historically we've been using a different approach
1405 * and store it in the config.
1407 dt_root_get(env, dev->lod_child, &info->lti_fid);
1408 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1410 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1411 struct lov_user_md *lum = buf->lb_buf;
1412 struct lov_desc *desc = &dev->lod_desc;
1414 if (buf->lb_buf == NULL) {
1416 } else if (buf->lb_len >= sizeof(*lum)) {
1417 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1418 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1419 lmm_oi_set_id(&lum->lmm_oi, 0);
1420 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1421 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1422 lum->lmm_stripe_size = cpu_to_le32(
1423 desc->ld_default_stripe_size);
1424 lum->lmm_stripe_count = cpu_to_le16(
1425 desc->ld_default_stripe_count);
1426 lum->lmm_stripe_offset = cpu_to_le16(
1427 desc->ld_default_stripe_offset);
1440 * Checks that the magic of the stripe is sane.
1442 * \param[in] lod lod device
1443 * \param[in] lum a buffer storing LMV EA to verify
1445 * \retval 0 if the EA is sane
1446 * \retval negative otherwise
1448 static int lod_verify_md_striping(struct lod_device *lod,
1449 const struct lmv_user_md_v1 *lum)
1451 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1452 CERROR("%s: invalid lmv_user_md: magic = %x, "
1453 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1454 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1455 (int)le32_to_cpu(lum->lum_stripe_offset),
1456 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1464 * Initialize LMV EA for a slave.
1466 * Initialize slave's LMV EA from the master's LMV EA.
1468 * \param[in] master_lmv a buffer containing master's EA
1469 * \param[out] slave_lmv a buffer where slave's EA will be stored
1472 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1473 const struct lmv_mds_md_v1 *master_lmv)
1475 *slave_lmv = *master_lmv;
1476 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1482 * Generate LMV EA from the object passed as \a dt. The object must have
1483 * the stripes created and initialized.
1485 * \param[in] env execution environment
1486 * \param[in] dt object
1487 * \param[out] lmv_buf buffer storing generated LMV EA
1489 * \retval 0 on success
1490 * \retval negative if failed
1492 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1493 struct lu_buf *lmv_buf)
1495 struct lod_thread_info *info = lod_env_info(env);
1496 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1497 struct lod_object *lo = lod_dt_obj(dt);
1498 struct lmv_mds_md_v1 *lmm1;
1500 int type = LU_SEQ_RANGE_ANY;
1505 LASSERT(lo->ldo_dir_striped != 0);
1506 LASSERT(lo->ldo_stripenr > 0);
1507 stripe_count = lo->ldo_stripenr;
1508 /* Only store the LMV EA heahder on the disk. */
1509 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1510 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1514 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1517 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1518 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1519 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1520 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1521 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1526 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1527 lmv_buf->lb_buf = info->lti_ea_store;
1528 lmv_buf->lb_len = sizeof(*lmm1);
1534 * Create in-core represenation for a striped directory.
1536 * Parse the buffer containing LMV EA and instantiate LU objects
1537 * representing the stripe objects. The pointers to the objects are
1538 * stored in ldo_stripe field of \a lo. This function is used when
1539 * we need to access an already created object (i.e. load from a disk).
1541 * \param[in] env execution environment
1542 * \param[in] lo lod object
1543 * \param[in] buf buffer containing LMV EA
1545 * \retval 0 on success
1546 * \retval negative if failed
1548 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1549 const struct lu_buf *buf)
1551 struct lod_thread_info *info = lod_env_info(env);
1552 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1553 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1554 struct dt_object **stripe;
1555 union lmv_mds_md *lmm = buf->lb_buf;
1556 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1557 struct lu_fid *fid = &info->lti_fid;
1562 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1565 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1566 lo->ldo_dir_slave_stripe = 1;
1570 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1573 if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1576 LASSERT(lo->ldo_stripe == NULL);
1577 OBD_ALLOC(stripe, sizeof(stripe[0]) *
1578 (le32_to_cpu(lmv1->lmv_stripe_count)));
1582 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1583 struct dt_device *tgt_dt;
1584 struct dt_object *dto;
1585 int type = LU_SEQ_RANGE_ANY;
1588 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1589 if (!fid_is_sane(fid))
1590 GOTO(out, rc = -ESTALE);
1592 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1596 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1597 tgt_dt = lod->lod_child;
1599 struct lod_tgt_desc *tgt;
1601 tgt = LTD_TGT(ltd, idx);
1603 GOTO(out, rc = -ESTALE);
1604 tgt_dt = tgt->ltd_tgt;
1607 dto = dt_locate_at(env, tgt_dt, fid,
1608 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1611 GOTO(out, rc = PTR_ERR(dto));
1616 lo->ldo_stripe = stripe;
1617 lo->ldo_stripenr = le32_to_cpu(lmv1->lmv_stripe_count);
1618 lo->ldo_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1620 lod_object_free_striping(env, lo);
1626 * Declare create a striped directory.
1628 * Declare creating a striped directory with a given stripe pattern on the
1629 * specified MDTs. A striped directory is represented as a regular directory
1630 * - an index listing all the stripes. The stripes point back to the master
1631 * object with ".." and LinkEA. The master object gets LMV EA which
1632 * identifies it as a striped directory. The function allocates FIDs
1635 * \param[in] env execution environment
1636 * \param[in] dt object
1637 * \param[in] attr attributes to initialize the objects with
1638 * \param[in] dof type of objects to be created
1639 * \param[in] th transaction handle
1641 * \retval 0 on success
1642 * \retval negative if failed
1644 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1645 struct dt_object *dt,
1646 struct lu_attr *attr,
1647 struct dt_object_format *dof,
1650 struct lod_thread_info *info = lod_env_info(env);
1651 struct lu_buf lmv_buf;
1652 struct lu_buf slave_lmv_buf;
1653 struct lmv_mds_md_v1 *lmm;
1654 struct lmv_mds_md_v1 *slave_lmm = NULL;
1655 struct dt_insert_rec *rec = &info->lti_dt_rec;
1656 struct lod_object *lo = lod_dt_obj(dt);
1661 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1664 lmm = lmv_buf.lb_buf;
1666 OBD_ALLOC_PTR(slave_lmm);
1667 if (slave_lmm == NULL)
1668 GOTO(out, rc = -ENOMEM);
1670 lod_prep_slave_lmv_md(slave_lmm, lmm);
1671 slave_lmv_buf.lb_buf = slave_lmm;
1672 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1674 if (!dt_try_as_dir(env, dt_object_child(dt)))
1675 GOTO(out, rc = -EINVAL);
1677 rec->rec_type = S_IFDIR;
1678 for (i = 0; i < lo->ldo_stripenr; i++) {
1679 struct dt_object *dto = lo->ldo_stripe[i];
1680 char *stripe_name = info->lti_key;
1681 struct lu_name *sname;
1682 struct linkea_data ldata = { NULL };
1683 struct lu_buf linkea_buf;
1685 rc = lod_sub_object_declare_create(env, dto, attr, NULL,
1690 if (!dt_try_as_dir(env, dto))
1691 GOTO(out, rc = -EINVAL);
1693 rc = lod_sub_object_declare_ref_add(env, dto, th);
1697 rec->rec_fid = lu_object_fid(&dto->do_lu);
1698 rc = lod_sub_object_declare_insert(env, dto,
1699 (const struct dt_rec *)rec,
1700 (const struct dt_key *)dot, th);
1704 /* master stripe FID will be put to .. */
1705 rec->rec_fid = lu_object_fid(&dt->do_lu);
1706 rc = lod_sub_object_declare_insert(env, dto,
1707 (const struct dt_rec *)rec,
1708 (const struct dt_key *)dotdot,
1713 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1714 cfs_fail_val != i) {
1715 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1717 slave_lmm->lmv_master_mdt_index =
1720 slave_lmm->lmv_master_mdt_index =
1722 rc = lod_sub_object_declare_xattr_set(env, dto,
1723 &slave_lmv_buf, XATTR_NAME_LMV, 0, th);
1728 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1730 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1731 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1733 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1734 PFID(lu_object_fid(&dto->do_lu)), i);
1736 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1737 rc = linkea_data_new(&ldata, &info->lti_linkea_buf);
1741 rc = linkea_add_buf(&ldata, sname, lu_object_fid(&dt->do_lu));
1745 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1746 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1747 rc = lod_sub_object_declare_xattr_set(env, dto, &linkea_buf,
1748 XATTR_NAME_LINK, 0, th);
1752 rec->rec_fid = lu_object_fid(&dto->do_lu);
1753 rc = lod_sub_object_declare_insert(env, dt_object_child(dt),
1754 (const struct dt_rec *)rec,
1755 (const struct dt_key *)stripe_name,
1760 rc = lod_sub_object_declare_ref_add(env, dt_object_child(dt),
1766 rc = lod_sub_object_declare_xattr_set(env, dt_object_child(dt),
1767 &lmv_buf, XATTR_NAME_LMV, 0, th);
1771 if (slave_lmm != NULL)
1772 OBD_FREE_PTR(slave_lmm);
1777 static int lod_prep_md_striped_create(const struct lu_env *env,
1778 struct dt_object *dt,
1779 struct lu_attr *attr,
1780 const struct lmv_user_md_v1 *lum,
1781 struct dt_object_format *dof,
1784 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1785 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1786 struct lod_object *lo = lod_dt_obj(dt);
1787 struct dt_object **stripe;
1796 /* The lum has been verifed in lod_verify_md_striping */
1797 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
1798 LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1800 stripe_count = le32_to_cpu(lum->lum_stripe_count);
1802 /* shrink the stripe_count to the avaible MDT count */
1803 if (stripe_count > lod->lod_remote_mdt_count + 1 &&
1804 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))
1805 stripe_count = lod->lod_remote_mdt_count + 1;
1807 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1811 OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1812 if (idx_array == NULL)
1813 GOTO(out_free, rc = -ENOMEM);
1815 /* Start index will be the master MDT */
1816 master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1817 idx_array[0] = master_index;
1818 for (i = 0; i < stripe_count; i++) {
1819 struct lod_tgt_desc *tgt = NULL;
1820 struct dt_object *dto;
1821 struct lu_fid fid = { 0 };
1823 struct lu_object_conf conf = { 0 };
1824 struct dt_device *tgt_dt = NULL;
1826 /* Try to find next avaible target */
1828 for (j = 0; j < lod->lod_remote_mdt_count;
1829 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1830 bool already_allocated = false;
1833 CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
1834 idx, lod->lod_remote_mdt_count + 1, i);
1835 if (idx == master_index) {
1836 /* Allocate the FID locally */
1837 rc = obd_fid_alloc(env, lod->lod_child_exp,
1841 tgt_dt = lod->lod_child;
1845 /* Find next available target */
1846 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx))
1849 if (likely(!OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
1850 /* check whether the idx already exists
1851 * in current allocated array */
1852 for (k = 0; k < i; k++) {
1853 if (idx_array[k] == idx) {
1854 already_allocated = true;
1859 if (already_allocated)
1863 /* check the status of the OSP */
1864 tgt = LTD_TGT(ltd, idx);
1868 tgt_dt = tgt->ltd_tgt;
1869 rc = dt_statfs(env, tgt_dt, NULL);
1871 /* this OSP doesn't feel well */
1876 rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1885 /* Can not allocate more stripes */
1886 if (j == lod->lod_remote_mdt_count) {
1887 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1888 lod2obd(lod)->obd_name, stripe_count, i - 1);
1892 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
1893 idx, i, PFID(&fid));
1895 /* Set the start index for next stripe allocation */
1896 if (i < stripe_count - 1)
1897 idx_array[i + 1] = (idx + 1) %
1898 (lod->lod_remote_mdt_count + 1);
1899 /* tgt_dt and fid must be ready after search avaible OSP
1900 * in the above loop */
1901 LASSERT(tgt_dt != NULL);
1902 LASSERT(fid_is_sane(&fid));
1903 conf.loc_flags = LOC_F_NEW;
1904 dto = dt_locate_at(env, tgt_dt, &fid,
1905 dt->do_lu.lo_dev->ld_site->ls_top_dev,
1908 GOTO(out_put, rc = PTR_ERR(dto));
1912 lo->ldo_dir_striped = 1;
1913 lo->ldo_stripe = stripe;
1914 lo->ldo_stripenr = i;
1915 lo->ldo_stripes_allocated = stripe_count;
1917 if (lo->ldo_stripenr == 0)
1918 GOTO(out_put, rc = -ENOSPC);
1920 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
1926 for (i = 0; i < stripe_count; i++)
1927 if (stripe[i] != NULL)
1928 lu_object_put(env, &stripe[i]->do_lu);
1929 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
1930 lo->ldo_stripenr = 0;
1931 lo->ldo_stripes_allocated = 0;
1932 lo->ldo_stripe = NULL;
1936 if (idx_array != NULL)
1937 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
1943 * Declare create striped md object.
1945 * The function declares intention to create a striped directory. This is a
1946 * wrapper for lod_prep_md_striped_create(). The only additional functionality
1947 * is to verify pattern \a lum_buf is good. Check that function for the details.
1949 * \param[in] env execution environment
1950 * \param[in] dt object
1951 * \param[in] attr attributes to initialize the objects with
1952 * \param[in] lum_buf a pattern specifying the number of stripes and
1954 * \param[in] dof type of objects to be created
1955 * \param[in] th transaction handle
1957 * \retval 0 on success
1958 * \retval negative if failed
1961 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
1962 struct dt_object *dt,
1963 struct lu_attr *attr,
1964 const struct lu_buf *lum_buf,
1965 struct dt_object_format *dof,
1968 struct lod_object *lo = lod_dt_obj(dt);
1969 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1970 struct lmv_user_md_v1 *lum;
1974 lum = lum_buf->lb_buf;
1975 LASSERT(lum != NULL);
1977 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
1978 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
1979 (int)le32_to_cpu(lum->lum_stripe_offset));
1981 if (le32_to_cpu(lum->lum_stripe_count) == 0)
1984 rc = lod_verify_md_striping(lod, lum);
1988 /* prepare dir striped objects */
1989 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
1991 /* failed to create striping, let's reset
1992 * config so that others don't get confused */
1993 lod_object_free_striping(env, lo);
2002 * Implementation of dt_object_operations::do_declare_xattr_set.
2004 * Used with regular (non-striped) objects. Basically it
2005 * initializes the striping information and applies the
2006 * change to all the stripes.
2008 * \see dt_object_operations::do_declare_xattr_set() in the API description
2011 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2012 struct dt_object *dt,
2013 const struct lu_buf *buf,
2014 const char *name, int fl,
2017 struct dt_object *next = dt_object_child(dt);
2018 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2019 struct lod_object *lo = lod_dt_obj(dt);
2024 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2025 struct lmv_user_md_v1 *lum;
2027 LASSERT(buf != NULL && buf->lb_buf != NULL);
2029 rc = lod_verify_md_striping(d, lum);
2034 rc = lod_sub_object_declare_xattr_set(env, next, buf, name, fl, th);
2038 /* set xattr to each stripes, if needed */
2039 rc = lod_load_striping(env, lo);
2043 /* Note: Do not set LinkEA on sub-stripes, otherwise
2044 * it will confuse the fid2path process(see mdt_path_current()).
2045 * The linkEA between master and sub-stripes is set in
2046 * lod_xattr_set_lmv(). */
2047 if (lo->ldo_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2050 for (i = 0; i < lo->ldo_stripenr; i++) {
2051 LASSERT(lo->ldo_stripe[i]);
2053 rc = lod_sub_object_declare_xattr_set(env, lo->ldo_stripe[i],
2063 * Implementation of dt_object_operations::do_declare_xattr_set.
2065 * \see dt_object_operations::do_declare_xattr_set() in the API description
2068 * the extension to the API:
2069 * - declaring LOVEA requests striping creation
2070 * - LU_XATTR_REPLACE means layout swap
2072 static int lod_declare_xattr_set(const struct lu_env *env,
2073 struct dt_object *dt,
2074 const struct lu_buf *buf,
2075 const char *name, int fl,
2078 struct dt_object *next = dt_object_child(dt);
2079 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2085 * allow to declare predefined striping on a new (!mode) object
2086 * which is supposed to be replay of regular file creation
2087 * (when LOV setting is declared)
2088 * LU_XATTR_REPLACE is set to indicate a layout swap
2090 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2091 if ((S_ISREG(mode) || mode == 0) && strcmp(name, XATTR_NAME_LOV) == 0 &&
2092 !(fl & LU_XATTR_REPLACE)) {
2094 * this is a request to manipulate object's striping
2096 if (dt_object_exists(dt)) {
2097 rc = dt_attr_get(env, next, attr);
2101 memset(attr, 0, sizeof(*attr));
2102 attr->la_valid = LA_TYPE | LA_MODE;
2103 attr->la_mode = S_IFREG;
2105 rc = lod_declare_striped_object(env, dt, attr, buf, th);
2106 } else if (S_ISDIR(mode)) {
2107 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2109 rc = lod_sub_object_declare_xattr_set(env, next, buf, name,
2117 * Resets cached default striping in the object.
2119 * \param[in] lo object
2121 static void lod_lov_stripe_cache_clear(struct lod_object *lo)
2123 lo->ldo_def_striping_set = 0;
2124 lo->ldo_def_striping_cached = 0;
2125 lod_object_set_pool(lo, NULL);
2126 lo->ldo_def_stripe_size = 0;
2127 lo->ldo_def_stripenr = 0;
2128 if (lo->ldo_dir_stripe != NULL)
2129 lo->ldo_dir_def_striping_cached = 0;
2133 * Apply xattr changes to the object.
2135 * Applies xattr changes to the object and the stripes if the latter exist.
2137 * \param[in] env execution environment
2138 * \param[in] dt object
2139 * \param[in] buf buffer pointing to the new value of xattr
2140 * \param[in] name name of xattr
2141 * \param[in] fl flags
2142 * \param[in] th transaction handle
2144 * \retval 0 on success
2145 * \retval negative if failed
2147 static int lod_xattr_set_internal(const struct lu_env *env,
2148 struct dt_object *dt,
2149 const struct lu_buf *buf,
2150 const char *name, int fl,
2153 struct dt_object *next = dt_object_child(dt);
2154 struct lod_object *lo = lod_dt_obj(dt);
2159 rc = lod_sub_object_xattr_set(env, next, buf, name, fl, th);
2160 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2163 /* Note: Do not set LinkEA on sub-stripes, otherwise
2164 * it will confuse the fid2path process(see mdt_path_current()).
2165 * The linkEA between master and sub-stripes is set in
2166 * lod_xattr_set_lmv(). */
2167 if (lo->ldo_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2170 for (i = 0; i < lo->ldo_stripenr; i++) {
2171 LASSERT(lo->ldo_stripe[i]);
2173 rc = lod_sub_object_xattr_set(env, lo->ldo_stripe[i], buf, name,
2183 * Delete an extended attribute.
2185 * Deletes specified xattr from the object and the stripes if the latter exist.
2187 * \param[in] env execution environment
2188 * \param[in] dt object
2189 * \param[in] name name of xattr
2190 * \param[in] th transaction handle
2192 * \retval 0 on success
2193 * \retval negative if failed
2195 static int lod_xattr_del_internal(const struct lu_env *env,
2196 struct dt_object *dt,
2197 const char *name, struct thandle *th)
2199 struct dt_object *next = dt_object_child(dt);
2200 struct lod_object *lo = lod_dt_obj(dt);
2205 rc = lod_sub_object_xattr_del(env, next, name, th);
2206 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2209 if (lo->ldo_stripenr == 0)
2212 for (i = 0; i < lo->ldo_stripenr; i++) {
2213 LASSERT(lo->ldo_stripe[i]);
2215 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name,
2225 * Set default striping on a directory.
2227 * Sets specified striping on a directory object unless it matches the default
2228 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2229 * EA. This striping will be used when regular file is being created in this
2232 * \param[in] env execution environment
2233 * \param[in] dt the striped object
2234 * \param[in] buf buffer with the striping
2235 * \param[in] name name of EA
2236 * \param[in] fl xattr flag (see OSD API description)
2237 * \param[in] th transaction handle
2239 * \retval 0 on success
2240 * \retval negative if failed
2242 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
2243 struct dt_object *dt,
2244 const struct lu_buf *buf,
2245 const char *name, int fl,
2248 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2249 struct lod_object *l = lod_dt_obj(dt);
2250 struct lov_user_md_v1 *lum;
2251 struct lov_user_md_v3 *v3 = NULL;
2252 const char *pool_name = NULL;
2256 /* If it is striped dir, we should clear the stripe cache for
2257 * slave stripe as well, but there are no effective way to
2258 * notify the LOD on the slave MDT, so we do not cache stripe
2259 * information for slave stripe for now. XXX*/
2260 lod_lov_stripe_cache_clear(l);
2261 LASSERT(buf != NULL && buf->lb_buf != NULL);
2264 rc = lod_verify_striping(d, buf, false);
2268 if (lum->lmm_magic == LOV_USER_MAGIC_V3) {
2270 if (v3->lmm_pool_name[0] != '\0')
2271 pool_name = v3->lmm_pool_name;
2274 /* if { size, offset, count } = { 0, -1, 0 } and no pool
2275 * (i.e. all default values specified) then delete default
2276 * striping from dir. */
2278 "set default striping: sz %u # %u offset %d %s %s\n",
2279 (unsigned)lum->lmm_stripe_size,
2280 (unsigned)lum->lmm_stripe_count,
2281 (int)lum->lmm_stripe_offset,
2282 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
2284 if (LOVEA_DELETE_VALUES(lum->lmm_stripe_size, lum->lmm_stripe_count,
2285 lum->lmm_stripe_offset, pool_name)) {
2286 rc = lod_xattr_del_internal(env, dt, name, th);
2290 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2297 * Set default striping on a directory object.
2299 * Sets specified striping on a directory object unless it matches the default
2300 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2301 * EA. This striping will be used when a new directory is being created in the
2304 * \param[in] env execution environment
2305 * \param[in] dt the striped object
2306 * \param[in] buf buffer with the striping
2307 * \param[in] name name of EA
2308 * \param[in] fl xattr flag (see OSD API description)
2309 * \param[in] th transaction handle
2311 * \retval 0 on success
2312 * \retval negative if failed
2314 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
2315 struct dt_object *dt,
2316 const struct lu_buf *buf,
2317 const char *name, int fl,
2320 struct lod_object *l = lod_dt_obj(dt);
2321 struct lmv_user_md_v1 *lum;
2325 LASSERT(buf != NULL && buf->lb_buf != NULL);
2328 CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
2329 le32_to_cpu(lum->lum_stripe_count),
2330 (int)le32_to_cpu(lum->lum_stripe_offset));
2332 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
2333 le32_to_cpu(lum->lum_stripe_offset)) &&
2334 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
2335 rc = lod_xattr_del_internal(env, dt, name, th);
2339 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2344 /* Update default stripe cache */
2345 if (l->ldo_dir_stripe == NULL) {
2346 OBD_ALLOC_PTR(l->ldo_dir_stripe);
2347 if (l->ldo_dir_stripe == NULL)
2351 l->ldo_dir_def_striping_cached = 0;
2356 * Turn directory into a striped directory.
2358 * During replay the client sends the striping created before MDT
2359 * failure, then the layer above LOD sends this defined striping
2360 * using ->do_xattr_set(), so LOD uses this method to replay creation
2361 * of the stripes. Notice the original information for the striping
2362 * (#stripes, FIDs, etc) was transferred in declare path.
2364 * \param[in] env execution environment
2365 * \param[in] dt the striped object
2366 * \param[in] buf not used currently
2367 * \param[in] name not used currently
2368 * \param[in] fl xattr flag (see OSD API description)
2369 * \param[in] th transaction handle
2371 * \retval 0 on success
2372 * \retval negative if failed
2374 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
2375 const struct lu_buf *buf, const char *name,
2376 int fl, struct thandle *th)
2378 struct lod_object *lo = lod_dt_obj(dt);
2379 struct lod_thread_info *info = lod_env_info(env);
2380 struct lu_attr *attr = &info->lti_attr;
2381 struct dt_object_format *dof = &info->lti_format;
2382 struct lu_buf lmv_buf;
2383 struct lu_buf slave_lmv_buf;
2384 struct lmv_mds_md_v1 *lmm;
2385 struct lmv_mds_md_v1 *slave_lmm = NULL;
2386 struct dt_insert_rec *rec = &info->lti_dt_rec;
2391 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2394 /* The stripes are supposed to be allocated in declare phase,
2395 * if there are no stripes being allocated, it will skip */
2396 if (lo->ldo_stripenr == 0)
2399 rc = dt_attr_get(env, dt_object_child(dt), attr);
2403 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
2404 LA_MODE | LA_UID | LA_GID | LA_TYPE;
2405 dof->dof_type = DFT_DIR;
2407 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
2410 lmm = lmv_buf.lb_buf;
2412 OBD_ALLOC_PTR(slave_lmm);
2413 if (slave_lmm == NULL)
2416 lod_prep_slave_lmv_md(slave_lmm, lmm);
2417 slave_lmv_buf.lb_buf = slave_lmm;
2418 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
2420 rec->rec_type = S_IFDIR;
2421 for (i = 0; i < lo->ldo_stripenr; i++) {
2422 struct dt_object *dto;
2423 char *stripe_name = info->lti_key;
2424 struct lu_name *sname;
2425 struct linkea_data ldata = { NULL };
2426 struct lu_buf linkea_buf;
2428 dto = lo->ldo_stripe[i];
2430 dt_write_lock(env, dto, MOR_TGT_CHILD);
2431 rc = lod_sub_object_create(env, dto, attr, NULL, dof,
2434 dt_write_unlock(env, dto);
2438 rc = lod_sub_object_ref_add(env, dto, th);
2439 dt_write_unlock(env, dto);
2443 rec->rec_fid = lu_object_fid(&dto->do_lu);
2444 rc = lod_sub_object_index_insert(env, dto,
2445 (const struct dt_rec *)rec,
2446 (const struct dt_key *)dot, th, 0);
2450 rec->rec_fid = lu_object_fid(&dt->do_lu);
2451 rc = lod_sub_object_index_insert(env, dto, (struct dt_rec *)rec,
2452 (const struct dt_key *)dotdot, th, 0);
2456 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
2457 cfs_fail_val != i) {
2458 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
2460 slave_lmm->lmv_master_mdt_index =
2463 slave_lmm->lmv_master_mdt_index =
2466 rc = lod_sub_object_xattr_set(env, dto, &slave_lmv_buf,
2467 XATTR_NAME_LMV, fl, th);
2472 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
2474 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2475 PFID(lu_object_fid(&dto->do_lu)), i + 1);
2477 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2478 PFID(lu_object_fid(&dto->do_lu)), i);
2480 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2481 rc = linkea_data_new(&ldata, &info->lti_linkea_buf);
2485 rc = linkea_add_buf(&ldata, sname, lu_object_fid(&dt->do_lu));
2489 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
2490 linkea_buf.lb_len = ldata.ld_leh->leh_len;
2491 rc = lod_sub_object_xattr_set(env, dto, &linkea_buf,
2492 XATTR_NAME_LINK, 0, th);
2496 rec->rec_fid = lu_object_fid(&dto->do_lu);
2497 rc = lod_sub_object_index_insert(env, dt_object_child(dt),
2498 (const struct dt_rec *)rec,
2499 (const struct dt_key *)stripe_name, th, 0);
2503 rc = lod_sub_object_ref_add(env, dt_object_child(dt), th);
2508 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
2509 rc = lod_sub_object_xattr_set(env, dt_object_child(dt),
2510 &lmv_buf, XATTR_NAME_LMV, fl, th);
2512 if (slave_lmm != NULL)
2513 OBD_FREE_PTR(slave_lmm);
2519 * Helper function to declare/execute creation of a striped directory
2521 * Called in declare/create object path, prepare striping for a directory
2522 * and prepare defaults data striping for the objects to be created in
2523 * that directory. Notice the function calls "declaration" or "execution"
2524 * methods depending on \a declare param. This is a consequence of the
2525 * current approach while we don't have natural distributed transactions:
2526 * we basically execute non-local updates in the declare phase. So, the
2527 * arguments for the both phases are the same and this is the reason for
2528 * this function to exist.
2530 * \param[in] env execution environment
2531 * \param[in] dt object
2532 * \param[in] attr attributes the stripes will be created with
2533 * \param[in] dof format of stripes (see OSD API description)
2534 * \param[in] th transaction handle
2535 * \param[in] declare where to call "declare" or "execute" methods
2537 * \retval 0 on success
2538 * \retval negative if failed
2540 static int lod_dir_striping_create_internal(const struct lu_env *env,
2541 struct dt_object *dt,
2542 struct lu_attr *attr,
2543 struct dt_object_format *dof,
2547 struct lod_thread_info *info = lod_env_info(env);
2548 struct lod_object *lo = lod_dt_obj(dt);
2552 if (!LMVEA_DELETE_VALUES(lo->ldo_stripenr,
2553 lo->ldo_dir_stripe_offset)) {
2554 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
2555 int stripe_count = lo->ldo_stripenr;
2557 if (info->lti_ea_store_size < sizeof(*v1)) {
2558 rc = lod_ea_store_resize(info, sizeof(*v1));
2561 v1 = info->lti_ea_store;
2564 memset(v1, 0, sizeof(*v1));
2565 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
2566 v1->lum_stripe_count = cpu_to_le32(stripe_count);
2567 v1->lum_stripe_offset =
2568 cpu_to_le32(lo->ldo_dir_stripe_offset);
2570 info->lti_buf.lb_buf = v1;
2571 info->lti_buf.lb_len = sizeof(*v1);
2574 rc = lod_declare_xattr_set_lmv(env, dt, attr,
2575 &info->lti_buf, dof, th);
2577 rc = lod_xattr_set_lmv(env, dt, &info->lti_buf,
2578 XATTR_NAME_LMV, 0, th);
2583 /* Transfer default LMV striping from the parent */
2584 if (lo->ldo_dir_def_striping_set &&
2585 !LMVEA_DELETE_VALUES(lo->ldo_dir_def_stripenr,
2586 lo->ldo_dir_def_stripe_offset)) {
2587 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
2588 int def_stripe_count = lo->ldo_dir_def_stripenr;
2590 if (info->lti_ea_store_size < sizeof(*v1)) {
2591 rc = lod_ea_store_resize(info, sizeof(*v1));
2594 v1 = info->lti_ea_store;
2597 memset(v1, 0, sizeof(*v1));
2598 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
2599 v1->lum_stripe_count = cpu_to_le32(def_stripe_count);
2600 v1->lum_stripe_offset =
2601 cpu_to_le32(lo->ldo_dir_def_stripe_offset);
2603 cpu_to_le32(lo->ldo_dir_def_hash_type);
2605 info->lti_buf.lb_buf = v1;
2606 info->lti_buf.lb_len = sizeof(*v1);
2608 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
2609 XATTR_NAME_DEFAULT_LMV,
2612 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
2614 XATTR_NAME_DEFAULT_LMV, 0,
2620 /* Transfer default LOV striping from the parent */
2621 if (lo->ldo_def_striping_set &&
2622 !LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size,
2623 lo->ldo_def_stripenr,
2624 lo->ldo_def_stripe_offset,
2626 struct lov_user_md_v3 *v3 = info->lti_ea_store;
2628 if (info->lti_ea_store_size < sizeof(*v3)) {
2629 rc = lod_ea_store_resize(info, sizeof(*v3));
2632 v3 = info->lti_ea_store;
2635 memset(v3, 0, sizeof(*v3));
2636 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
2637 v3->lmm_stripe_count = cpu_to_le16(lo->ldo_def_stripenr);
2638 v3->lmm_stripe_offset = cpu_to_le16(lo->ldo_def_stripe_offset);
2639 v3->lmm_stripe_size = cpu_to_le32(lo->ldo_def_stripe_size);
2640 if (lo->ldo_pool != NULL)
2641 strlcpy(v3->lmm_pool_name, lo->ldo_pool,
2642 sizeof(v3->lmm_pool_name));
2644 info->lti_buf.lb_buf = v3;
2645 info->lti_buf.lb_len = sizeof(*v3);
2648 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
2649 XATTR_NAME_LOV, 0, th);
2651 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
2652 XATTR_NAME_LOV, 0, th);
2660 static int lod_declare_dir_striping_create(const struct lu_env *env,
2661 struct dt_object *dt,
2662 struct lu_attr *attr,
2663 struct dt_object_format *dof,
2666 return lod_dir_striping_create_internal(env, dt, attr, dof, th, true);
2669 static int lod_dir_striping_create(const struct lu_env *env,
2670 struct dt_object *dt,
2671 struct lu_attr *attr,
2672 struct dt_object_format *dof,
2675 struct lod_object *lo = lod_dt_obj(dt);
2678 rc = lod_dir_striping_create_internal(env, dt, attr, dof, th, false);
2680 lo->ldo_striping_cached = 1;
2686 * Implementation of dt_object_operations::do_xattr_set.
2688 * Sets specified extended attribute on the object. Three types of EAs are
2690 * LOV EA - stores striping for a regular file or default striping (when set
2692 * LMV EA - stores a marker for the striped directories
2693 * DMV EA - stores default directory striping
2695 * When striping is applied to a non-striped existing object (this is called
2696 * late striping), then LOD notices the caller wants to turn the object into a
2697 * striped one. The stripe objects are created and appropriate EA is set:
2698 * LOV EA storing all the stripes directly or LMV EA storing just a small header
2699 * with striping configuration.
2701 * \see dt_object_operations::do_xattr_set() in the API description for details.
2703 static int lod_xattr_set(const struct lu_env *env,
2704 struct dt_object *dt, const struct lu_buf *buf,
2705 const char *name, int fl, struct thandle *th)
2707 struct dt_object *next = dt_object_child(dt);
2711 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2712 strcmp(name, XATTR_NAME_LMV) == 0) {
2713 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
2715 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
2716 LMV_HASH_FLAG_MIGRATION)
2717 rc = lod_sub_object_xattr_set(env, next, buf, name, fl,
2720 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
2725 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2726 strcmp(name, XATTR_NAME_LOV) == 0) {
2728 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
2730 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2731 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2733 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
2736 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
2737 !strcmp(name, XATTR_NAME_LOV)) {
2738 /* in case of lov EA swap, just set it
2739 * if not, it is a replay so check striping match what we
2740 * already have during req replay, declare_xattr_set()
2741 * defines striping, then create() does the work */
2742 if (fl & LU_XATTR_REPLACE) {
2743 /* free stripes, then update disk */
2744 lod_object_free_striping(env, lod_dt_obj(dt));
2746 rc = lod_sub_object_xattr_set(env, next, buf, name,
2748 } else if (dt_object_remote(dt)) {
2749 /* This only happens during migration, see
2750 * mdd_migrate_create(), in which Master MDT will
2751 * create a remote target object, and only set
2752 * (migrating) stripe EA on the remote object,
2753 * and does not need creating each stripes. */
2754 rc = lod_sub_object_xattr_set(env, next, buf, name,
2757 rc = lod_striping_create(env, dt, NULL, NULL, th);
2762 /* then all other xattr */
2763 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2769 * Implementation of dt_object_operations::do_declare_xattr_del.
2771 * \see dt_object_operations::do_declare_xattr_del() in the API description
2774 static int lod_declare_xattr_del(const struct lu_env *env,
2775 struct dt_object *dt, const char *name,
2778 struct lod_object *lo = lod_dt_obj(dt);
2783 rc = lod_sub_object_declare_xattr_del(env, dt_object_child(dt),
2788 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2791 /* set xattr to each stripes, if needed */
2792 rc = lod_load_striping(env, lo);
2796 if (lo->ldo_stripenr == 0)
2799 for (i = 0; i < lo->ldo_stripenr; i++) {
2800 LASSERT(lo->ldo_stripe[i]);
2801 rc = lod_sub_object_declare_xattr_del(env, lo->ldo_stripe[i],
2811 * Implementation of dt_object_operations::do_xattr_del.
2813 * If EA storing a regular striping is being deleted, then release
2814 * all the references to the stripe objects in core.
2816 * \see dt_object_operations::do_xattr_del() in the API description for details.
2818 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
2819 const char *name, struct thandle *th)
2821 struct dt_object *next = dt_object_child(dt);
2822 struct lod_object *lo = lod_dt_obj(dt);
2827 if (!strcmp(name, XATTR_NAME_LOV))
2828 lod_object_free_striping(env, lod_dt_obj(dt));
2830 rc = lod_sub_object_xattr_del(env, next, name, th);
2831 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2834 if (lo->ldo_stripenr == 0)
2837 for (i = 0; i < lo->ldo_stripenr; i++) {
2838 LASSERT(lo->ldo_stripe[i]);
2840 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name, th);
2849 * Implementation of dt_object_operations::do_xattr_list.
2851 * \see dt_object_operations::do_xattr_list() in the API description
2854 static int lod_xattr_list(const struct lu_env *env,
2855 struct dt_object *dt, const struct lu_buf *buf)
2857 return dt_xattr_list(env, dt_object_child(dt), buf);
2861 * Initialize a pool the object belongs to.
2863 * When a striped object is being created, striping configuration
2864 * may demand the stripes are allocated on a limited set of the
2865 * targets. These limited sets are known as "pools". So we copy
2866 * a pool name into the object and later actual creation methods
2867 * (like lod_object_create()) will use this information to allocate
2868 * the stripes properly.
2870 * \param[in] o object
2871 * \param[in] pool pool name
2873 int lod_object_set_pool(struct lod_object *o, char *pool)
2878 len = strlen(o->ldo_pool);
2879 OBD_FREE(o->ldo_pool, len + 1);
2884 OBD_ALLOC(o->ldo_pool, len + 1);
2885 if (o->ldo_pool == NULL)
2887 strcpy(o->ldo_pool, pool);
2892 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
2894 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
2899 * Cache default regular striping in the object.
2901 * To improve performance of striped regular object creation we cache
2902 * default LOV striping (if it exists) in the parent directory object.
2904 * \param[in] env execution environment
2905 * \param[in] lp object
2907 * \retval 0 on success
2908 * \retval negative if failed
2910 static int lod_cache_parent_lov_striping(const struct lu_env *env,
2911 struct lod_object *lp)
2913 struct lod_thread_info *info = lod_env_info(env);
2914 struct lov_user_md_v1 *v1 = NULL;
2915 struct lov_user_md_v3 *v3 = NULL;
2919 /* called from MDD without parent being write locked,
2921 dt_write_lock(env, dt_object_child(&lp->ldo_obj), 0);
2922 rc = lod_get_lov_ea(env, lp);
2926 if (rc < (typeof(rc))sizeof(struct lov_user_md)) {
2927 /* don't lookup for non-existing or invalid striping */
2928 lp->ldo_def_striping_set = 0;
2929 lp->ldo_def_striping_cached = 1;
2930 lp->ldo_def_stripe_size = 0;
2931 lp->ldo_def_stripenr = 0;
2932 lp->ldo_def_stripe_offset = (typeof(v1->lmm_stripe_offset))(-1);
2933 GOTO(unlock, rc = 0);
2937 v1 = info->lti_ea_store;
2938 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
2939 lustre_swab_lov_user_md_v1(v1);
2940 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
2941 v3 = (struct lov_user_md_v3 *)v1;
2942 lustre_swab_lov_user_md_v3(v3);
2945 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1)
2946 GOTO(unlock, rc = 0);
2948 if (v1->lmm_pattern != LOV_PATTERN_RAID0 && v1->lmm_pattern != 0)
2949 GOTO(unlock, rc = 0);
2951 CDEBUG(D_INFO, DFID" stripe_count=%d stripe_size=%d stripe_offset=%d\n",
2952 PFID(lu_object_fid(&lp->ldo_obj.do_lu)),
2953 (int)v1->lmm_stripe_count,
2954 (int)v1->lmm_stripe_size, (int)v1->lmm_stripe_offset);
2956 lp->ldo_def_stripenr = v1->lmm_stripe_count;
2957 lp->ldo_def_stripe_size = v1->lmm_stripe_size;
2958 lp->ldo_def_stripe_offset = v1->lmm_stripe_offset;
2959 lp->ldo_def_striping_cached = 1;
2960 lp->ldo_def_striping_set = 1;
2961 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2962 /* XXX: sanity check here */
2963 v3 = (struct lov_user_md_v3 *) v1;
2964 if (v3->lmm_pool_name[0])
2965 lod_object_set_pool(lp, v3->lmm_pool_name);
2969 dt_write_unlock(env, dt_object_child(&lp->ldo_obj));
2975 * Cache default directory striping in the object.
2977 * To improve performance of striped directory creation we cache default
2978 * directory striping (if it exists) in the parent directory object.
2980 * \param[in] env execution environment
2981 * \param[in] lp object
2983 * \retval 0 on success
2984 * \retval negative if failed
2986 static int lod_cache_parent_lmv_striping(const struct lu_env *env,
2987 struct lod_object *lp)
2989 struct lod_thread_info *info = lod_env_info(env);
2990 struct lmv_user_md_v1 *v1 = NULL;
2994 /* called from MDD without parent being write locked,
2996 dt_write_lock(env, dt_object_child(&lp->ldo_obj), 0);
2997 rc = lod_get_default_lmv_ea(env, lp);
3001 if (rc < (typeof(rc))sizeof(struct lmv_user_md)) {
3002 /* don't lookup for non-existing or invalid striping */
3003 lp->ldo_dir_def_striping_set = 0;
3004 lp->ldo_dir_def_striping_cached = 1;
3005 lp->ldo_dir_def_stripenr = 0;
3006 lp->ldo_dir_def_stripe_offset =
3007 (typeof(v1->lum_stripe_offset))(-1);
3008 lp->ldo_dir_def_hash_type = LMV_HASH_TYPE_FNV_1A_64;
3009 GOTO(unlock, rc = 0);
3013 v1 = info->lti_ea_store;
3015 lp->ldo_dir_def_stripenr = le32_to_cpu(v1->lum_stripe_count);
3016 lp->ldo_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
3017 lp->ldo_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
3018 lp->ldo_dir_def_striping_set = 1;
3019 lp->ldo_dir_def_striping_cached = 1;
3023 dt_write_unlock(env, dt_object_child(&lp->ldo_obj));
3028 * Cache default striping in the object.
3030 * To improve performance of striped object creation we cache default striping
3031 * (if it exists) in the parent directory object. We always cache default
3032 * striping for the regular files (stored in LOV EA) and we cache default
3033 * striping for the directories if requested by \a child_mode (when a new
3034 * directory is being created).
3036 * \param[in] env execution environment
3037 * \param[in] lp object
3038 * \param[in] child_mode new object's mode
3040 * \retval 0 on success
3041 * \retval negative if failed
3043 static int lod_cache_parent_striping(const struct lu_env *env,
3044 struct lod_object *lp,
3050 if (!lp->ldo_def_striping_cached) {
3051 /* we haven't tried to get default striping for
3052 * the directory yet, let's cache it in the object */
3053 rc = lod_cache_parent_lov_striping(env, lp);
3058 /* If the parent is on the remote MDT, we should always
3059 * try to refresh the default stripeEA cache, because we
3060 * do not cache default striping information for remote
3062 if (S_ISDIR(child_mode) && (!lp->ldo_dir_def_striping_cached ||
3063 dt_object_remote(&lp->ldo_obj)))
3064 rc = lod_cache_parent_lmv_striping(env, lp);
3070 * Implementation of dt_object_operations::do_ah_init.
3072 * This method is used to make a decision on the striping configuration for the
3073 * object being created. It can be taken from the \a parent object if it exists,
3074 * or filesystem's default. The resulting configuration (number of stripes,
3075 * stripe size/offset, pool name, etc) is stored in the object itself and will
3076 * be used by the methods like ->doo_declare_create().
3078 * \see dt_object_operations::do_ah_init() in the API description for details.
3080 static void lod_ah_init(const struct lu_env *env,
3081 struct dt_allocation_hint *ah,
3082 struct dt_object *parent,
3083 struct dt_object *child,
3086 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
3087 struct dt_object *nextp = NULL;
3088 struct dt_object *nextc;
3089 struct lod_object *lp = NULL;
3090 struct lod_object *lc;
3091 struct lov_desc *desc;
3097 if (likely(parent)) {
3098 nextp = dt_object_child(parent);
3099 lp = lod_dt_obj(parent);
3100 rc = lod_load_striping(env, lp);
3105 nextc = dt_object_child(child);
3106 lc = lod_dt_obj(child);
3108 LASSERT(lc->ldo_stripenr == 0);
3109 LASSERT(lc->ldo_stripe == NULL);
3111 if (!dt_object_exists(nextc))
3112 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
3114 if (S_ISDIR(child_mode)) {
3115 if (lc->ldo_dir_stripe == NULL) {
3116 OBD_ALLOC_PTR(lc->ldo_dir_stripe);
3117 if (lc->ldo_dir_stripe == NULL)
3121 LASSERT(lp != NULL);
3122 if (lp->ldo_dir_stripe == NULL) {
3123 OBD_ALLOC_PTR(lp->ldo_dir_stripe);
3124 if (lp->ldo_dir_stripe == NULL)
3128 rc = lod_cache_parent_striping(env, lp, child_mode);
3132 /* transfer defaults to new directory */
3133 if (lp->ldo_def_striping_set) {
3135 lod_object_set_pool(lc, lp->ldo_pool);
3136 lc->ldo_def_stripenr = lp->ldo_def_stripenr;
3137 lc->ldo_def_stripe_size = lp->ldo_def_stripe_size;
3138 lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
3139 lc->ldo_def_striping_set = 1;
3140 lc->ldo_def_striping_cached = 1;
3141 CDEBUG(D_OTHER, "inherite EA sz:%d off:%d nr:%d\n",
3142 (int)lc->ldo_def_stripe_size,
3143 (int)lc->ldo_def_stripe_offset,
3144 (int)lc->ldo_def_stripenr);
3147 /* transfer dir defaults to new directory */
3148 if (lp->ldo_dir_def_striping_set) {
3149 lc->ldo_dir_def_stripenr = lp->ldo_dir_def_stripenr;
3150 lc->ldo_dir_def_stripe_offset =
3151 lp->ldo_dir_def_stripe_offset;
3152 lc->ldo_dir_def_hash_type =
3153 lp->ldo_dir_def_hash_type;
3154 lc->ldo_dir_def_striping_set = 1;
3155 lc->ldo_dir_def_striping_cached = 1;
3156 CDEBUG(D_INFO, "inherit default EA nr:%d off:%d t%u\n",
3157 (int)lc->ldo_dir_def_stripenr,
3158 (int)lc->ldo_dir_def_stripe_offset,
3159 lc->ldo_dir_def_hash_type);
3162 /* It should always honour the specified stripes */
3163 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0) {
3164 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
3166 rc = lod_verify_md_striping(d, lum1);
3168 le32_to_cpu(lum1->lum_stripe_count) > 1) {
3169 /* Directory will be striped only if
3170 * stripe_count > 1 */
3172 le32_to_cpu(lum1->lum_stripe_count);
3173 lc->ldo_dir_stripe_offset =
3174 le32_to_cpu(lum1->lum_stripe_offset);
3175 lc->ldo_dir_hash_type =
3176 le32_to_cpu(lum1->lum_hash_type);
3177 CDEBUG(D_INFO, "set stripe EA nr:%hu off:%d\n",
3179 (int)lc->ldo_dir_stripe_offset);
3181 /* then check whether there is default stripes from parent */
3182 } else if (lp->ldo_dir_def_striping_set) {
3183 /* If there are default dir stripe from parent */
3184 lc->ldo_stripenr = lp->ldo_dir_def_stripenr;
3185 lc->ldo_dir_stripe_offset =
3186 lp->ldo_dir_def_stripe_offset;
3187 lc->ldo_dir_hash_type =
3188 lp->ldo_dir_def_hash_type;
3189 CDEBUG(D_INFO, "inherit EA nr:%hu off:%d\n",
3191 (int)lc->ldo_dir_stripe_offset);
3193 /* set default stripe for this directory */
3194 lc->ldo_stripenr = 0;
3195 lc->ldo_dir_stripe_offset = -1;
3198 CDEBUG(D_INFO, "final striping count:%hu, offset:%d\n",
3199 lc->ldo_stripenr, (int)lc->ldo_dir_stripe_offset);
3205 * if object is going to be striped over OSTs, transfer default
3206 * striping information to the child, so that we can use it
3207 * during declaration and creation
3209 if (!lod_object_will_be_striped(S_ISREG(child_mode),
3210 lu_object_fid(&child->do_lu)))
3213 * try from the parent
3215 if (likely(parent)) {
3216 lod_cache_parent_striping(env, lp, child_mode);
3218 lc->ldo_def_stripe_offset = LOV_OFFSET_DEFAULT;
3220 if (lp->ldo_def_striping_set) {
3222 lod_object_set_pool(lc, lp->ldo_pool);
3223 lc->ldo_stripenr = lp->ldo_def_stripenr;
3224 lc->ldo_stripe_size = lp->ldo_def_stripe_size;
3225 lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
3226 CDEBUG(D_OTHER, "striping from parent: #%d, sz %d %s\n",
3227 lc->ldo_stripenr, lc->ldo_stripe_size,
3228 lp->ldo_pool ? lp->ldo_pool : "");
3233 * if the parent doesn't provide with specific pattern, grab fs-wide one
3235 desc = &d->lod_desc;
3236 if (lc->ldo_stripenr == 0)
3237 lc->ldo_stripenr = desc->ld_default_stripe_count;
3238 if (lc->ldo_stripe_size == 0)
3239 lc->ldo_stripe_size = desc->ld_default_stripe_size;
3240 CDEBUG(D_OTHER, "final striping: # %d stripes, sz %d from %s\n",
3241 lc->ldo_stripenr, lc->ldo_stripe_size,
3242 lc->ldo_pool ? lc->ldo_pool : "");
3245 /* we do not cache stripe information for slave stripe, see
3246 * lod_xattr_set_lov_on_dir */
3247 if (lp != NULL && lp->ldo_dir_slave_stripe)
3248 lod_lov_stripe_cache_clear(lp);
3253 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
3255 * Size initialization on late striping.
3257 * Propagate the size of a truncated object to a deferred striping.
3258 * This function handles a special case when truncate was done on a
3259 * non-striped object and now while the striping is being created
3260 * we can't lose that size, so we have to propagate it to the stripes
3263 * \param[in] env execution environment
3264 * \param[in] dt object
3265 * \param[in] th transaction handle
3267 * \retval 0 on success
3268 * \retval negative if failed
3270 static int lod_declare_init_size(const struct lu_env *env,
3271 struct dt_object *dt, struct thandle *th)
3273 struct dt_object *next = dt_object_child(dt);
3274 struct lod_object *lo = lod_dt_obj(dt);
3275 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3276 uint64_t size, offs;
3280 /* XXX: we support the simplest (RAID0) striping so far */
3281 LASSERT(lo->ldo_stripe || lo->ldo_stripenr == 0);
3282 LASSERT(lo->ldo_stripe_size > 0);
3284 rc = dt_attr_get(env, next, attr);
3285 LASSERT(attr->la_valid & LA_SIZE);
3289 size = attr->la_size;
3293 /* ll_do_div64(a, b) returns a % b, and a = a / b */
3294 ll_do_div64(size, (__u64) lo->ldo_stripe_size);
3295 stripe = ll_do_div64(size, (__u64) lo->ldo_stripenr);
3297 size = size * lo->ldo_stripe_size;
3298 offs = attr->la_size;
3299 size += ll_do_div64(offs, lo->ldo_stripe_size);
3301 attr->la_valid = LA_SIZE;
3302 attr->la_size = size;
3304 rc = lod_sub_object_declare_attr_set(env, lo->ldo_stripe[stripe], attr,
3311 * Declare creation of striped object.
3313 * The function declares creation stripes for a regular object. The function
3314 * also declares whether the stripes will be created with non-zero size if
3315 * previously size was set non-zero on the master object. If object \a dt is
3316 * not local, then only fully defined striping can be applied in \a lovea.
3317 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
3320 * \param[in] env execution environment
3321 * \param[in] dt object
3322 * \param[in] attr attributes the stripes will be created with
3323 * \param[in] lovea a buffer containing striping description
3324 * \param[in] th transaction handle
3326 * \retval 0 on success
3327 * \retval negative if failed
3329 int lod_declare_striped_object(const struct lu_env *env, struct dt_object *dt,
3330 struct lu_attr *attr,
3331 const struct lu_buf *lovea, struct thandle *th)
3333 struct lod_thread_info *info = lod_env_info(env);
3334 struct dt_object *next = dt_object_child(dt);
3335 struct lod_object *lo = lod_dt_obj(dt);
3339 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO)) {
3340 /* failed to create striping, let's reset
3341 * config so that others don't get confused */
3342 lod_object_free_striping(env, lo);
3343 GOTO(out, rc = -ENOMEM);
3346 if (!dt_object_remote(next)) {
3347 /* choose OST and generate appropriate objects */
3348 rc = lod_qos_prep_create(env, lo, attr, lovea, th);
3350 /* failed to create striping, let's reset
3351 * config so that others don't get confused */
3352 lod_object_free_striping(env, lo);
3357 * declare storage for striping data
3359 info->lti_buf.lb_len = lov_mds_md_size(lo->ldo_stripenr,
3360 lo->ldo_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1);
3362 /* LOD can not choose OST objects for remote objects, i.e.
3363 * stripes must be ready before that. Right now, it can only
3364 * happen during migrate, i.e. migrate process needs to create
3365 * remote regular file (mdd_migrate_create), then the migrate
3366 * process will provide stripeEA. */
3367 LASSERT(lovea != NULL);
3368 info->lti_buf = *lovea;
3371 rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
3372 XATTR_NAME_LOV, 0, th);
3377 * if striping is created with local object's size > 0,
3378 * we have to propagate this size to specific object
3379 * the case is possible only when local object was created previously
3381 if (dt_object_exists(next))
3382 rc = lod_declare_init_size(env, dt, th);
3389 * Implementation of dt_object_operations::do_declare_create.
3391 * The method declares creation of a new object. If the object will be striped,
3392 * then helper functions are called to find FIDs for the stripes, declare
3393 * creation of the stripes and declare initialization of the striping
3394 * information to be stored in the master object.
3396 * \see dt_object_operations::do_declare_create() in the API description
3399 static int lod_declare_object_create(const struct lu_env *env,
3400 struct dt_object *dt,
3401 struct lu_attr *attr,
3402 struct dt_allocation_hint *hint,
3403 struct dt_object_format *dof,
3406 struct dt_object *next = dt_object_child(dt);
3407 struct lod_object *lo = lod_dt_obj(dt);
3416 * first of all, we declare creation of local object
3418 rc = lod_sub_object_declare_create(env, next, attr, hint, dof, th);
3422 if (dof->dof_type == DFT_SYM)
3423 dt->do_body_ops = &lod_body_lnk_ops;
3424 else if (dof->dof_type == DFT_REGULAR)
3425 dt->do_body_ops = &lod_body_ops;
3428 * it's lod_ah_init() that has decided the object will be striped
3430 if (dof->dof_type == DFT_REGULAR) {
3431 /* callers don't want stripes */
3432 /* XXX: all tricky interactions with ->ah_make_hint() decided
3433 * to use striping, then ->declare_create() behaving differently
3434 * should be cleaned */
3435 if (dof->u.dof_reg.striped == 0)
3436 lo->ldo_stripenr = 0;
3437 if (lo->ldo_stripenr > 0)
3438 rc = lod_declare_striped_object(env, dt, attr,
3440 } else if (dof->dof_type == DFT_DIR) {
3441 struct seq_server_site *ss;
3443 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
3445 /* If the parent has default stripeEA, and client
3446 * did not find it before sending create request,
3447 * then MDT will return -EREMOTE, and client will
3448 * retrieve the default stripeEA and re-create the
3451 * Note: if dah_eadata != NULL, it means creating the
3452 * striped directory with specified stripeEA, then it
3453 * should ignore the default stripeEA */
3454 if (hint != NULL && hint->dah_eadata == NULL) {
3455 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
3456 GOTO(out, rc = -EREMOTE);
3458 if (lo->ldo_dir_stripe_offset == -1) {
3459 /* child and parent should be in the same MDT */
3460 if (hint->dah_parent != NULL &&
3461 dt_object_remote(hint->dah_parent))
3462 GOTO(out, rc = -EREMOTE);
3463 } else if (lo->ldo_dir_stripe_offset !=
3465 GOTO(out, rc = -EREMOTE);
3469 /* Orphan object (like migrating object) does not have
3470 * lod_dir_stripe, see lod_ah_init */
3471 if (lo->ldo_dir_stripe != NULL)
3472 rc = lod_declare_dir_striping_create(env, dt, attr,
3480 * Creation of a striped regular object.
3482 * The function is called to create the stripe objects for a regular
3483 * striped file. This can happen at the initial object creation or
3484 * when the caller asks LOD to do so using ->do_xattr_set() method
3485 * (so called late striping). Notice all the information are already
3486 * prepared in the form of the list of objects (ldo_stripe field).
3487 * This is done during declare phase.
3489 * \param[in] env execution environment
3490 * \param[in] dt object
3491 * \param[in] attr attributes the stripes will be created with
3492 * \param[in] dof format of stripes (see OSD API description)
3493 * \param[in] th transaction handle
3495 * \retval 0 on success
3496 * \retval negative if failed
3498 int lod_striping_create(const struct lu_env *env, struct dt_object *dt,
3499 struct lu_attr *attr, struct dt_object_format *dof,
3502 struct lod_object *lo = lod_dt_obj(dt);
3506 LASSERT(lo->ldo_striping_cached == 0);
3508 /* create all underlying objects */
3509 for (i = 0; i < lo->ldo_stripenr; i++) {
3510 LASSERT(lo->ldo_stripe[i]);
3511 rc = lod_sub_object_create(env, lo->ldo_stripe[i], attr, NULL,
3518 rc = lod_generate_and_set_lovea(env, lo, th);
3520 lo->ldo_striping_cached = 1;
3527 * Implementation of dt_object_operations::do_create.
3529 * If any of preceeding methods (like ->do_declare_create(),
3530 * ->do_ah_init(), etc) chose to create a striped object,
3531 * then this method will create the master and the stripes.
3533 * \see dt_object_operations::do_create() in the API description for details.
3535 static int lod_object_create(const struct lu_env *env, struct dt_object *dt,
3536 struct lu_attr *attr,
3537 struct dt_allocation_hint *hint,
3538 struct dt_object_format *dof, struct thandle *th)
3540 struct lod_object *lo = lod_dt_obj(dt);
3544 /* create local object */
3545 rc = lod_sub_object_create(env, dt_object_child(dt), attr, hint, dof,
3550 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3551 lo->ldo_stripe && dof->u.dof_reg.striped != 0)
3552 rc = lod_striping_create(env, dt, attr, dof, th);
3558 * Implementation of dt_object_operations::do_declare_destroy.
3560 * If the object is a striped directory, then the function declares reference
3561 * removal from the master object (this is an index) to the stripes and declares
3562 * destroy of all the stripes. In all the cases, it declares an intention to
3563 * destroy the object itself.
3565 * \see dt_object_operations::do_declare_destroy() in the API description
3568 static int lod_declare_object_destroy(const struct lu_env *env,
3569 struct dt_object *dt,
3572 struct dt_object *next = dt_object_child(dt);
3573 struct lod_object *lo = lod_dt_obj(dt);
3574 struct lod_thread_info *info = lod_env_info(env);
3575 char *stripe_name = info->lti_key;
3580 * load striping information, notice we don't do this when object
3581 * is being initialized as we don't need this information till
3582 * few specific cases like destroy, chown
3584 rc = lod_load_striping(env, lo);
3588 /* declare destroy for all underlying objects */
3589 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3590 rc = next->do_ops->do_index_try(env, next,
3591 &dt_directory_features);
3595 for (i = 0; i < lo->ldo_stripenr; i++) {
3596 rc = lod_sub_object_declare_ref_del(env, next, th);
3600 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3601 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
3603 rc = lod_sub_object_declare_delete(env, next,
3604 (const struct dt_key *)stripe_name, th);
3611 * we declare destroy for the local object
3613 rc = lod_sub_object_declare_destroy(env, next, th);
3617 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ))
3620 /* declare destroy all striped objects */
3621 for (i = 0; i < lo->ldo_stripenr; i++) {
3622 if (lo->ldo_stripe[i] == NULL)
3625 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
3626 rc = lod_sub_object_declare_ref_del(env,
3627 lo->ldo_stripe[i], th);
3629 rc = lod_sub_object_declare_destroy(env, lo->ldo_stripe[i],
3639 * Implementation of dt_object_operations::do_destroy.
3641 * If the object is a striped directory, then the function removes references
3642 * from the master object (this is an index) to the stripes and destroys all
3643 * the stripes. In all the cases, the function destroys the object itself.
3645 * \see dt_object_operations::do_destroy() in the API description for details.
3647 static int lod_object_destroy(const struct lu_env *env,
3648 struct dt_object *dt, struct thandle *th)
3650 struct dt_object *next = dt_object_child(dt);
3651 struct lod_object *lo = lod_dt_obj(dt);
3652 struct lod_thread_info *info = lod_env_info(env);
3653 char *stripe_name = info->lti_key;
3658 /* destroy sub-stripe of master object */
3659 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3660 rc = next->do_ops->do_index_try(env, next,
3661 &dt_directory_features);
3665 for (i = 0; i < lo->ldo_stripenr; i++) {
3666 rc = lod_sub_object_ref_del(env, next, th);
3670 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3671 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
3674 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
3675 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
3676 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
3678 rc = lod_sub_object_delete(env, next,
3679 (const struct dt_key *)stripe_name, th);
3685 rc = lod_sub_object_destroy(env, next, th);
3689 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ))
3692 /* destroy all striped objects */
3693 for (i = 0; i < lo->ldo_stripenr; i++) {
3694 if (likely(lo->ldo_stripe[i] != NULL) &&
3695 (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
3696 i == cfs_fail_val)) {
3697 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3698 dt_write_lock(env, lo->ldo_stripe[i],
3700 rc = lod_sub_object_ref_del(env,
3701 lo->ldo_stripe[i], th);
3702 dt_write_unlock(env, lo->ldo_stripe[i]);
3707 rc = lod_sub_object_destroy(env, lo->ldo_stripe[i], th);
3717 * Implementation of dt_object_operations::do_declare_ref_add.
3719 * \see dt_object_operations::do_declare_ref_add() in the API description
3722 static int lod_declare_ref_add(const struct lu_env *env,
3723 struct dt_object *dt, struct thandle *th)
3725 return lod_sub_object_declare_ref_add(env, dt_object_child(dt), th);
3729 * Implementation of dt_object_operations::do_ref_add.
3731 * \see dt_object_operations::do_ref_add() in the API description for details.
3733 static int lod_ref_add(const struct lu_env *env,
3734 struct dt_object *dt, struct thandle *th)
3736 return lod_sub_object_ref_add(env, dt_object_child(dt), th);
3740 * Implementation of dt_object_operations::do_declare_ref_del.
3742 * \see dt_object_operations::do_declare_ref_del() in the API description
3745 static int lod_declare_ref_del(const struct lu_env *env,
3746 struct dt_object *dt, struct thandle *th)
3748 return lod_sub_object_declare_ref_del(env, dt_object_child(dt), th);
3752 * Implementation of dt_object_operations::do_ref_del
3754 * \see dt_object_operations::do_ref_del() in the API description for details.
3756 static int lod_ref_del(const struct lu_env *env,
3757 struct dt_object *dt, struct thandle *th)
3759 return lod_sub_object_ref_del(env, dt_object_child(dt), th);
3763 * Implementation of dt_object_operations::do_object_sync.
3765 * \see dt_object_operations::do_object_sync() in the API description
3768 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
3769 __u64 start, __u64 end)
3771 return dt_object_sync(env, dt_object_child(dt), start, end);
3774 struct lod_slave_locks {
3776 struct lustre_handle lsl_handle[0];
3780 * Release LDLM locks on the stripes of a striped directory.
3782 * Iterates over all the locks taken on the stripe objects and
3783 * release them using ->do_object_unlock() method.
3785 * \param[in] env execution environment
3786 * \param[in] dt striped object
3787 * \param[in] einfo lock description
3788 * \param[in] policy data describing requested lock
3790 * \retval 0 on success
3791 * \retval negative if failed
3793 static int lod_object_unlock_internal(const struct lu_env *env,
3794 struct dt_object *dt,
3795 struct ldlm_enqueue_info *einfo,
3796 ldlm_policy_data_t *policy)
3798 struct lod_slave_locks *slave_locks = einfo->ei_cbdata;
3803 if (slave_locks == NULL)
3806 for (i = 1; i < slave_locks->lsl_lock_count; i++) {
3807 if (lustre_handle_is_used(&slave_locks->lsl_handle[i]))
3808 ldlm_lock_decref(&slave_locks->lsl_handle[i],
3816 * Implementation of dt_object_operations::do_object_unlock.
3818 * Used to release LDLM lock(s).
3820 * \see dt_object_operations::do_object_unlock() in the API description
3823 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
3824 struct ldlm_enqueue_info *einfo,
3825 union ldlm_policy_data *policy)
3827 struct lod_object *lo = lod_dt_obj(dt);
3828 struct lod_slave_locks *slave_locks = einfo->ei_cbdata;
3829 int slave_locks_size;
3833 if (slave_locks == NULL)
3836 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3839 /* Note: for remote lock for single stripe dir, MDT will cancel
3840 * the lock by lockh directly */
3841 if (lo->ldo_stripenr <= 1 && dt_object_remote(dt_object_child(dt)))
3844 /* Only cancel slave lock for striped dir */
3845 rc = lod_object_unlock_internal(env, dt, einfo, policy);
3847 slave_locks_size = sizeof(*slave_locks) + slave_locks->lsl_lock_count *
3848 sizeof(slave_locks->lsl_handle[0]);
3849 OBD_FREE(slave_locks, slave_locks_size);
3850 einfo->ei_cbdata = NULL;
3856 * Implementation of dt_object_operations::do_object_lock.
3858 * Used to get LDLM lock on the non-striped and striped objects.
3860 * \see dt_object_operations::do_object_lock() in the API description
3863 static int lod_object_lock(const struct lu_env *env,
3864 struct dt_object *dt,
3865 struct lustre_handle *lh,
3866 struct ldlm_enqueue_info *einfo,
3867 union ldlm_policy_data *policy)
3869 struct lod_object *lo = lod_dt_obj(dt);
3872 int slave_locks_size;
3873 struct lod_slave_locks *slave_locks = NULL;
3876 /* remote object lock */
3877 if (!einfo->ei_enq_slave) {
3878 LASSERT(dt_object_remote(dt));
3879 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
3883 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3886 rc = lod_load_striping(env, lo);
3891 if (lo->ldo_stripenr <= 1)
3894 slave_locks_size = sizeof(*slave_locks) + lo->ldo_stripenr *
3895 sizeof(slave_locks->lsl_handle[0]);
3896 /* Freed in lod_object_unlock */
3897 OBD_ALLOC(slave_locks, slave_locks_size);
3898 if (slave_locks == NULL)
3900 slave_locks->lsl_lock_count = lo->ldo_stripenr;
3902 /* striped directory lock */
3903 for (i = 1; i < lo->ldo_stripenr; i++) {
3904 struct lustre_handle lockh;
3905 struct ldlm_res_id *res_id;
3907 res_id = &lod_env_info(env)->lti_res_id;
3908 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
3910 einfo->ei_res_id = res_id;
3912 LASSERT(lo->ldo_stripe[i] != NULL);
3913 if (likely(dt_object_remote(lo->ldo_stripe[i]))) {
3914 rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh,
3917 struct ldlm_namespace *ns = einfo->ei_namespace;
3918 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
3919 ldlm_completion_callback completion = einfo->ei_cb_cp;
3920 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
3922 /* This only happens if there are mulitple stripes
3923 * on the master MDT, i.e. except stripe0, there are
3924 * other stripes on the Master MDT as well, Only
3925 * happens in the test case right now. */
3926 LASSERT(ns != NULL);
3927 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS,
3928 policy, einfo->ei_mode,
3929 &dlmflags, blocking,
3931 NULL, 0, LVB_T_NONE,
3936 slave_locks->lsl_handle[i] = lockh;
3939 einfo->ei_cbdata = slave_locks;
3942 if (rc != 0 && slave_locks != NULL) {
3943 einfo->ei_cbdata = slave_locks;
3944 lod_object_unlock_internal(env, dt, einfo, policy);
3945 OBD_FREE(slave_locks, slave_locks_size);
3946 einfo->ei_cbdata = NULL;
3952 struct dt_object_operations lod_obj_ops = {
3953 .do_read_lock = lod_object_read_lock,
3954 .do_write_lock = lod_object_write_lock,
3955 .do_read_unlock = lod_object_read_unlock,
3956 .do_write_unlock = lod_object_write_unlock,
3957 .do_write_locked = lod_object_write_locked,
3958 .do_attr_get = lod_attr_get,
3959 .do_declare_attr_set = lod_declare_attr_set,
3960 .do_attr_set = lod_attr_set,
3961 .do_xattr_get = lod_xattr_get,
3962 .do_declare_xattr_set = lod_declare_xattr_set,
3963 .do_xattr_set = lod_xattr_set,
3964 .do_declare_xattr_del = lod_declare_xattr_del,
3965 .do_xattr_del = lod_xattr_del,
3966 .do_xattr_list = lod_xattr_list,
3967 .do_ah_init = lod_ah_init,
3968 .do_declare_create = lod_declare_object_create,
3969 .do_create = lod_object_create,
3970 .do_declare_destroy = lod_declare_object_destroy,
3971 .do_destroy = lod_object_destroy,
3972 .do_index_try = lod_index_try,
3973 .do_declare_ref_add = lod_declare_ref_add,
3974 .do_ref_add = lod_ref_add,
3975 .do_declare_ref_del = lod_declare_ref_del,
3976 .do_ref_del = lod_ref_del,
3977 .do_object_sync = lod_object_sync,
3978 .do_object_lock = lod_object_lock,
3979 .do_object_unlock = lod_object_unlock,
3983 * Implementation of dt_body_operations::dbo_read.
3985 * \see dt_body_operations::dbo_read() in the API description for details.
3987 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
3988 struct lu_buf *buf, loff_t *pos)
3990 struct dt_object *next = dt_object_child(dt);
3991 return next->do_body_ops->dbo_read(env, next, buf, pos);
3995 * Implementation of dt_body_operations::dbo_declare_write.
3997 * \see dt_body_operations::dbo_declare_write() in the API description
4000 static ssize_t lod_declare_write(const struct lu_env *env,
4001 struct dt_object *dt,
4002 const struct lu_buf *buf, loff_t pos,
4005 return lod_sub_object_declare_write(env, dt_object_child(dt), buf, pos,
4010 * Implementation of dt_body_operations::dbo_write.
4012 * \see dt_body_operations::dbo_write() in the API description for details.
4014 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
4015 const struct lu_buf *buf, loff_t *pos,
4016 struct thandle *th, int iq)
4018 return lod_sub_object_write(env, dt_object_child(dt), buf, pos, th, iq);
4021 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
4022 __u64 start, __u64 end, struct thandle *th)
4024 if (dt_object_remote(dt))
4027 return lod_sub_object_declare_punch(env, dt_object_child(dt), start,
4031 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
4032 __u64 start, __u64 end, struct thandle *th)
4034 if (dt_object_remote(dt))
4037 return lod_sub_object_punch(env, dt_object_child(dt), start, end, th);
4040 static const struct dt_body_operations lod_body_lnk_ops = {
4041 .dbo_read = lod_read,
4042 .dbo_declare_write = lod_declare_write,
4043 .dbo_write = lod_write
4046 static const struct dt_body_operations lod_body_ops = {
4047 .dbo_read = lod_read,
4048 .dbo_declare_write = lod_declare_write,
4049 .dbo_write = lod_write,
4050 .dbo_declare_punch = lod_declare_punch,
4051 .dbo_punch = lod_punch,
4055 * Implementation of lu_object_operations::loo_object_init.
4057 * The function determines the type and the index of the target device using
4058 * sequence of the object's FID. Then passes control down to the
4059 * corresponding device:
4060 * OSD for the local objects, OSP for remote
4062 * \see lu_object_operations::loo_object_init() in the API description
4065 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
4066 const struct lu_object_conf *conf)
4068 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
4069 struct lu_device *cdev = NULL;
4070 struct lu_object *cobj;
4071 struct lod_tgt_descs *ltd = NULL;
4072 struct lod_tgt_desc *tgt;
4074 int type = LU_SEQ_RANGE_ANY;
4078 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
4080 /* Note: Sometimes, it will Return EAGAIN here, see
4081 * ptrlpc_import_delay_req(), which might confuse
4082 * lu_object_find_at() and make it wait there incorrectly.
4083 * so we convert it to EIO here.*/
4090 if (type == LU_SEQ_RANGE_MDT &&
4091 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
4092 cdev = &lod->lod_child->dd_lu_dev;
4093 } else if (type == LU_SEQ_RANGE_MDT) {
4094 ltd = &lod->lod_mdt_descs;
4096 } else if (type == LU_SEQ_RANGE_OST) {
4097 ltd = &lod->lod_ost_descs;
4104 if (ltd->ltd_tgts_size > idx &&
4105 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
4106 tgt = LTD_TGT(ltd, idx);
4108 LASSERT(tgt != NULL);
4109 LASSERT(tgt->ltd_tgt != NULL);
4111 cdev = &(tgt->ltd_tgt->dd_lu_dev);
4113 lod_putref(lod, ltd);
4116 if (unlikely(cdev == NULL))
4119 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
4120 if (unlikely(cobj == NULL))
4123 lu_object_add(lo, cobj);
4130 * Release resources associated with striping.
4132 * If the object is striped (regular or directory), then release
4133 * the stripe objects references and free the ldo_stripe array.
4135 * \param[in] env execution environment
4136 * \param[in] lo object
4138 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
4142 if (lo->ldo_dir_stripe != NULL) {
4143 OBD_FREE_PTR(lo->ldo_dir_stripe);
4144 lo->ldo_dir_stripe = NULL;
4147 if (lo->ldo_stripe) {
4148 LASSERT(lo->ldo_stripes_allocated > 0);
4150 for (i = 0; i < lo->ldo_stripenr; i++) {
4151 if (lo->ldo_stripe[i])
4152 lu_object_put(env, &lo->ldo_stripe[i]->do_lu);
4155 i = sizeof(struct dt_object *) * lo->ldo_stripes_allocated;
4156 OBD_FREE(lo->ldo_stripe, i);
4157 lo->ldo_stripe = NULL;
4158 lo->ldo_stripes_allocated = 0;
4160 lo->ldo_striping_cached = 0;
4161 lo->ldo_stripenr = 0;
4162 lo->ldo_pattern = 0;
4166 * Implementation of lu_object_operations::loo_object_start.
4168 * \see lu_object_operations::loo_object_start() in the API description
4171 static int lod_object_start(const struct lu_env *env, struct lu_object *o)
4173 if (S_ISLNK(o->lo_header->loh_attr & S_IFMT)) {
4174 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_lnk_ops;
4175 } else if (S_ISREG(o->lo_header->loh_attr & S_IFMT) ||
4176 fid_is_local_file(lu_object_fid(o))) {
4177 /* Note: some local file (like last rcvd) is created
4178 * through bottom layer (OSD), so the object initialization
4179 * comes to lod, it does not set loh_attr yet, so
4180 * set do_body_ops for local file anyway */
4181 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_ops;
4187 * Implementation of lu_object_operations::loo_object_free.
4189 * \see lu_object_operations::loo_object_free() in the API description
4192 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
4194 struct lod_object *mo = lu2lod_obj(o);
4197 * release all underlying object pinned
4200 lod_object_free_striping(env, mo);
4202 lod_object_set_pool(mo, NULL);
4205 OBD_SLAB_FREE_PTR(mo, lod_object_kmem);
4209 * Implementation of lu_object_operations::loo_object_release.
4211 * \see lu_object_operations::loo_object_release() in the API description
4214 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
4216 /* XXX: shouldn't we release everything here in case if object
4217 * creation failed before? */
4221 * Implementation of lu_object_operations::loo_object_print.
4223 * \see lu_object_operations::loo_object_print() in the API description
4226 static int lod_object_print(const struct lu_env *env, void *cookie,
4227 lu_printer_t p, const struct lu_object *l)
4229 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
4231 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
4234 struct lu_object_operations lod_lu_obj_ops = {
4235 .loo_object_init = lod_object_init,
4236 .loo_object_start = lod_object_start,
4237 .loo_object_free = lod_object_free,
4238 .loo_object_release = lod_object_release,
4239 .loo_object_print = lod_object_print,