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, 2016, Intel Corporation.
29 * lustre/lod/lod_object.c
31 * This file contains implementations of methods for the OSD API
32 * for the Logical Object Device (LOD) layer, which provides a virtual
33 * local OSD object interface to the MDD layer, and abstracts the
34 * addressing of local (OSD) and remote (OSP) objects. The API is
35 * described in the file lustre/include/dt_object.h and in
36 * Documentation/osd-api.txt.
38 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
41 #define DEBUG_SUBSYSTEM S_MDS
44 #include <obd_class.h>
45 #include <obd_support.h>
47 #include <lustre_fid.h>
48 #include <lustre_linkea.h>
49 #include <lustre_lmv.h>
50 #include <lustre_param.h>
51 #include <lustre_swab.h>
52 #include <lustre_ver.h>
53 #include <lprocfs_status.h>
54 #include <md_object.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 rc = next->do_ops->do_index_try(env, next, &dt_directory_features);
578 LASSERT(next->do_index_ops != NULL);
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 * Implementation of dt_object_operations::do_declare_attr_set.
1070 * If the object is striped, then apply the changes to all the stripes.
1072 * \see dt_object_operations::do_declare_attr_set() in the API description
1075 static int lod_declare_attr_set(const struct lu_env *env,
1076 struct dt_object *dt,
1077 const struct lu_attr *attr,
1080 struct dt_object *next = dt_object_child(dt);
1081 struct lod_object *lo = lod_dt_obj(dt);
1086 * declare setattr on the local object
1088 rc = lod_sub_object_declare_attr_set(env, next, attr, th);
1092 /* osp_declare_attr_set() ignores all attributes other than
1093 * UID, GID, and size, and osp_attr_set() ignores all but UID
1094 * and GID. Declaration of size attr setting happens through
1095 * lod_declare_init_size(), and not through this function.
1096 * Therefore we need not load striping unless ownership is
1097 * changing. This should save memory and (we hope) speed up
1099 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1100 if (!(attr->la_valid & (LA_UID | LA_GID)))
1103 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1106 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE |
1107 LA_ATIME | LA_MTIME | LA_CTIME |
1112 * load striping information, notice we don't do this when object
1113 * is being initialized as we don't need this information till
1114 * few specific cases like destroy, chown
1116 rc = lod_load_striping(env, lo);
1120 if (lo->ldo_stripenr == 0)
1124 * if object is striped declare changes on the stripes
1126 LASSERT(lo->ldo_stripe);
1127 for (i = 0; i < lo->ldo_stripenr; i++) {
1128 if (lo->ldo_stripe[i] == NULL)
1130 rc = lod_sub_object_declare_attr_set(env,
1131 lo->ldo_stripe[i], attr,
1137 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE) &&
1138 dt_object_exists(next) != 0 &&
1139 dt_object_remote(next) == 0)
1140 lod_sub_object_declare_xattr_del(env, next,
1141 XATTR_NAME_LOV, th);
1143 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) &&
1144 dt_object_exists(next) &&
1145 dt_object_remote(next) == 0 && S_ISREG(attr->la_mode)) {
1146 struct lod_thread_info *info = lod_env_info(env);
1147 struct lu_buf *buf = &info->lti_buf;
1149 buf->lb_buf = info->lti_ea_store;
1150 buf->lb_len = info->lti_ea_store_size;
1151 lod_sub_object_declare_xattr_set(env, next, buf,
1153 LU_XATTR_REPLACE, th);
1160 * Implementation of dt_object_operations::do_attr_set.
1162 * If the object is striped, then apply the changes to all or subset of
1163 * the stripes depending on the object type and specific attributes.
1165 * \see dt_object_operations::do_attr_set() in the API description for details.
1167 static int lod_attr_set(const struct lu_env *env,
1168 struct dt_object *dt,
1169 const struct lu_attr *attr,
1172 struct dt_object *next = dt_object_child(dt);
1173 struct lod_object *lo = lod_dt_obj(dt);
1178 * apply changes to the local object
1180 rc = lod_sub_object_attr_set(env, next, attr, th);
1184 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1185 if (!(attr->la_valid & (LA_UID | LA_GID)))
1188 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1191 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE |
1192 LA_ATIME | LA_MTIME | LA_CTIME |
1197 if (lo->ldo_stripenr == 0)
1201 * if object is striped, apply changes to all the stripes
1203 LASSERT(lo->ldo_stripe);
1204 for (i = 0; i < lo->ldo_stripenr; i++) {
1205 if (unlikely(lo->ldo_stripe[i] == NULL))
1208 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
1209 (dt_object_exists(lo->ldo_stripe[i]) == 0))
1212 rc = lod_sub_object_attr_set(env, lo->ldo_stripe[i], attr, th);
1217 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE) &&
1218 dt_object_exists(next) != 0 &&
1219 dt_object_remote(next) == 0)
1220 rc = lod_sub_object_xattr_del(env, next, 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;
1227 struct ost_id *oi = &info->lti_ostid;
1228 struct lu_fid *fid = &info->lti_fid;
1229 struct lov_mds_md_v1 *lmm;
1230 struct lov_ost_data_v1 *objs;
1234 rc1 = lod_get_lov_ea(env, lo);
1238 buf->lb_buf = info->lti_ea_store;
1239 buf->lb_len = info->lti_ea_store_size;
1240 lmm = info->lti_ea_store;
1241 magic = le32_to_cpu(lmm->lmm_magic);
1242 if (magic == LOV_MAGIC_V1)
1243 objs = &(lmm->lmm_objects[0]);
1245 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1246 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1247 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1249 fid_to_ostid(fid, oi);
1250 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1252 rc = lod_sub_object_xattr_set(env, next, buf, XATTR_NAME_LOV,
1253 LU_XATTR_REPLACE, th);
1260 * Implementation of dt_object_operations::do_xattr_get.
1262 * If LOV EA is requested from the root object and it's not
1263 * found, then return default striping for the filesystem.
1265 * \see dt_object_operations::do_xattr_get() in the API description for details.
1267 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1268 struct lu_buf *buf, const char *name)
1270 struct lod_thread_info *info = lod_env_info(env);
1271 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1275 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1276 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1277 struct lmv_mds_md_v1 *lmv1;
1280 if (rc > (typeof(rc))sizeof(*lmv1))
1283 if (rc < (typeof(rc))sizeof(*lmv1))
1284 RETURN(rc = rc > 0 ? -EINVAL : rc);
1286 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1287 CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1289 info->lti_buf.lb_buf = info->lti_key;
1290 info->lti_buf.lb_len = sizeof(*lmv1);
1291 rc = dt_xattr_get(env, dt_object_child(dt),
1292 &info->lti_buf, name);
1293 if (unlikely(rc != sizeof(*lmv1)))
1294 RETURN(rc = rc > 0 ? -EINVAL : rc);
1296 lmv1 = info->lti_buf.lb_buf;
1297 /* The on-disk LMV EA only contains header, but the
1298 * returned LMV EA size should contain the space for
1299 * the FIDs of all shards of the striped directory. */
1300 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1301 rc = lmv_mds_md_size(
1302 le32_to_cpu(lmv1->lmv_stripe_count),
1305 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1309 RETURN(rc = rc1 != 0 ? rc1 : rc);
1312 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1316 * lod returns default striping on the real root of the device
1317 * this is like the root stores default striping for the whole
1318 * filesystem. historically we've been using a different approach
1319 * and store it in the config.
1321 dt_root_get(env, dev->lod_child, &info->lti_fid);
1322 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1324 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1325 struct lov_user_md *lum = buf->lb_buf;
1326 struct lov_desc *desc = &dev->lod_desc;
1328 if (buf->lb_buf == NULL) {
1330 } else if (buf->lb_len >= sizeof(*lum)) {
1331 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1332 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1333 lmm_oi_set_id(&lum->lmm_oi, 0);
1334 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1335 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1336 lum->lmm_stripe_size = cpu_to_le32(
1337 desc->ld_default_stripe_size);
1338 lum->lmm_stripe_count = cpu_to_le16(
1339 desc->ld_default_stripe_count);
1340 lum->lmm_stripe_offset = cpu_to_le16(
1341 desc->ld_default_stripe_offset);
1354 * Checks that the magic of the stripe is sane.
1356 * \param[in] lod lod device
1357 * \param[in] lum a buffer storing LMV EA to verify
1359 * \retval 0 if the EA is sane
1360 * \retval negative otherwise
1362 static int lod_verify_md_striping(struct lod_device *lod,
1363 const struct lmv_user_md_v1 *lum)
1365 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1366 CERROR("%s: invalid lmv_user_md: magic = %x, "
1367 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1368 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1369 (int)le32_to_cpu(lum->lum_stripe_offset),
1370 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1378 * Initialize LMV EA for a slave.
1380 * Initialize slave's LMV EA from the master's LMV EA.
1382 * \param[in] master_lmv a buffer containing master's EA
1383 * \param[out] slave_lmv a buffer where slave's EA will be stored
1386 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1387 const struct lmv_mds_md_v1 *master_lmv)
1389 *slave_lmv = *master_lmv;
1390 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1396 * Generate LMV EA from the object passed as \a dt. The object must have
1397 * the stripes created and initialized.
1399 * \param[in] env execution environment
1400 * \param[in] dt object
1401 * \param[out] lmv_buf buffer storing generated LMV EA
1403 * \retval 0 on success
1404 * \retval negative if failed
1406 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1407 struct lu_buf *lmv_buf)
1409 struct lod_thread_info *info = lod_env_info(env);
1410 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1411 struct lod_object *lo = lod_dt_obj(dt);
1412 struct lmv_mds_md_v1 *lmm1;
1414 int type = LU_SEQ_RANGE_ANY;
1419 LASSERT(lo->ldo_dir_striped != 0);
1420 LASSERT(lo->ldo_stripenr > 0);
1421 stripe_count = lo->ldo_stripenr;
1422 /* Only store the LMV EA heahder on the disk. */
1423 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1424 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1428 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1431 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1432 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1433 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1434 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1435 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1440 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1441 lmv_buf->lb_buf = info->lti_ea_store;
1442 lmv_buf->lb_len = sizeof(*lmm1);
1448 * Create in-core represenation for a striped directory.
1450 * Parse the buffer containing LMV EA and instantiate LU objects
1451 * representing the stripe objects. The pointers to the objects are
1452 * stored in ldo_stripe field of \a lo. This function is used when
1453 * we need to access an already created object (i.e. load from a disk).
1455 * \param[in] env execution environment
1456 * \param[in] lo lod object
1457 * \param[in] buf buffer containing LMV EA
1459 * \retval 0 on success
1460 * \retval negative if failed
1462 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1463 const struct lu_buf *buf)
1465 struct lod_thread_info *info = lod_env_info(env);
1466 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1467 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1468 struct dt_object **stripe;
1469 union lmv_mds_md *lmm = buf->lb_buf;
1470 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1471 struct lu_fid *fid = &info->lti_fid;
1476 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1479 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1480 lo->ldo_dir_slave_stripe = 1;
1484 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1487 if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1490 LASSERT(lo->ldo_stripe == NULL);
1491 OBD_ALLOC(stripe, sizeof(stripe[0]) *
1492 (le32_to_cpu(lmv1->lmv_stripe_count)));
1496 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1497 struct dt_device *tgt_dt;
1498 struct dt_object *dto;
1499 int type = LU_SEQ_RANGE_ANY;
1502 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1503 if (!fid_is_sane(fid))
1504 GOTO(out, rc = -ESTALE);
1506 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1510 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1511 tgt_dt = lod->lod_child;
1513 struct lod_tgt_desc *tgt;
1515 tgt = LTD_TGT(ltd, idx);
1517 GOTO(out, rc = -ESTALE);
1518 tgt_dt = tgt->ltd_tgt;
1521 dto = dt_locate_at(env, tgt_dt, fid,
1522 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1525 GOTO(out, rc = PTR_ERR(dto));
1530 lo->ldo_stripe = stripe;
1531 lo->ldo_stripenr = le32_to_cpu(lmv1->lmv_stripe_count);
1532 lo->ldo_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1534 lod_object_free_striping(env, lo);
1540 * Declare create a striped directory.
1542 * Declare creating a striped directory with a given stripe pattern on the
1543 * specified MDTs. A striped directory is represented as a regular directory
1544 * - an index listing all the stripes. The stripes point back to the master
1545 * object with ".." and LinkEA. The master object gets LMV EA which
1546 * identifies it as a striped directory. The function allocates FIDs
1549 * \param[in] env execution environment
1550 * \param[in] dt object
1551 * \param[in] attr attributes to initialize the objects with
1552 * \param[in] dof type of objects to be created
1553 * \param[in] th transaction handle
1555 * \retval 0 on success
1556 * \retval negative if failed
1558 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1559 struct dt_object *dt,
1560 struct lu_attr *attr,
1561 struct dt_object_format *dof,
1564 struct lod_thread_info *info = lod_env_info(env);
1565 struct lu_buf lmv_buf;
1566 struct lu_buf slave_lmv_buf;
1567 struct lmv_mds_md_v1 *lmm;
1568 struct lmv_mds_md_v1 *slave_lmm = NULL;
1569 struct dt_insert_rec *rec = &info->lti_dt_rec;
1570 struct lod_object *lo = lod_dt_obj(dt);
1575 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1578 lmm = lmv_buf.lb_buf;
1580 OBD_ALLOC_PTR(slave_lmm);
1581 if (slave_lmm == NULL)
1582 GOTO(out, rc = -ENOMEM);
1584 lod_prep_slave_lmv_md(slave_lmm, lmm);
1585 slave_lmv_buf.lb_buf = slave_lmm;
1586 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1588 if (!dt_try_as_dir(env, dt_object_child(dt)))
1589 GOTO(out, rc = -EINVAL);
1591 rec->rec_type = S_IFDIR;
1592 for (i = 0; i < lo->ldo_stripenr; i++) {
1593 struct dt_object *dto = lo->ldo_stripe[i];
1594 char *stripe_name = info->lti_key;
1595 struct lu_name *sname;
1596 struct linkea_data ldata = { NULL };
1597 struct lu_buf linkea_buf;
1599 rc = lod_sub_object_declare_create(env, dto, attr, NULL,
1604 if (!dt_try_as_dir(env, dto))
1605 GOTO(out, rc = -EINVAL);
1607 rc = lod_sub_object_declare_ref_add(env, dto, th);
1611 rec->rec_fid = lu_object_fid(&dto->do_lu);
1612 rc = lod_sub_object_declare_insert(env, dto,
1613 (const struct dt_rec *)rec,
1614 (const struct dt_key *)dot, th);
1618 /* master stripe FID will be put to .. */
1619 rec->rec_fid = lu_object_fid(&dt->do_lu);
1620 rc = lod_sub_object_declare_insert(env, dto,
1621 (const struct dt_rec *)rec,
1622 (const struct dt_key *)dotdot,
1627 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1628 cfs_fail_val != i) {
1629 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1631 slave_lmm->lmv_master_mdt_index =
1634 slave_lmm->lmv_master_mdt_index =
1636 rc = lod_sub_object_declare_xattr_set(env, dto,
1637 &slave_lmv_buf, XATTR_NAME_LMV, 0, th);
1642 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1644 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1645 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1647 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1648 PFID(lu_object_fid(&dto->do_lu)), i);
1650 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1651 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1652 sname, lu_object_fid(&dt->do_lu));
1656 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1657 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1658 rc = lod_sub_object_declare_xattr_set(env, dto, &linkea_buf,
1659 XATTR_NAME_LINK, 0, th);
1663 rec->rec_fid = lu_object_fid(&dto->do_lu);
1664 rc = lod_sub_object_declare_insert(env, dt_object_child(dt),
1665 (const struct dt_rec *)rec,
1666 (const struct dt_key *)stripe_name,
1671 rc = lod_sub_object_declare_ref_add(env, dt_object_child(dt),
1677 rc = lod_sub_object_declare_xattr_set(env, dt_object_child(dt),
1678 &lmv_buf, XATTR_NAME_LMV, 0, th);
1682 if (slave_lmm != NULL)
1683 OBD_FREE_PTR(slave_lmm);
1688 static int lod_prep_md_striped_create(const struct lu_env *env,
1689 struct dt_object *dt,
1690 struct lu_attr *attr,
1691 const struct lmv_user_md_v1 *lum,
1692 struct dt_object_format *dof,
1695 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1696 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1697 struct lod_object *lo = lod_dt_obj(dt);
1698 struct dt_object **stripe;
1707 /* The lum has been verifed in lod_verify_md_striping */
1708 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
1709 LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1711 stripe_count = le32_to_cpu(lum->lum_stripe_count);
1713 OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1714 if (idx_array == NULL)
1717 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1719 GOTO(out_free, rc = -ENOMEM);
1721 /* Start index will be the master MDT */
1722 master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1723 idx_array[0] = master_index;
1724 for (i = 0; i < stripe_count; i++) {
1725 struct lod_tgt_desc *tgt = NULL;
1726 struct dt_object *dto;
1727 struct lu_fid fid = { 0 };
1729 struct lu_object_conf conf = { 0 };
1730 struct dt_device *tgt_dt = NULL;
1732 /* Try to find next avaible target */
1734 for (j = 0; j < lod->lod_remote_mdt_count;
1735 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1736 bool already_allocated = false;
1739 CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
1740 idx, lod->lod_remote_mdt_count + 1, i);
1741 if (idx == master_index) {
1742 /* Allocate the FID locally */
1743 rc = obd_fid_alloc(env, lod->lod_child_exp,
1747 tgt_dt = lod->lod_child;
1751 /* Find next available target */
1752 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx))
1755 if (likely(!OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
1756 /* check whether the idx already exists
1757 * in current allocated array */
1758 for (k = 0; k < i; k++) {
1759 if (idx_array[k] == idx) {
1760 already_allocated = true;
1765 if (already_allocated)
1769 /* check the status of the OSP */
1770 tgt = LTD_TGT(ltd, idx);
1774 tgt_dt = tgt->ltd_tgt;
1775 rc = dt_statfs(env, tgt_dt, NULL);
1777 /* this OSP doesn't feel well */
1782 rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1791 /* Can not allocate more stripes */
1792 if (j == lod->lod_remote_mdt_count) {
1793 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1794 lod2obd(lod)->obd_name, stripe_count, i - 1);
1798 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
1799 idx, i, PFID(&fid));
1801 /* Set the start index for next stripe allocation */
1802 if (i < stripe_count - 1)
1803 idx_array[i + 1] = (idx + 1) %
1804 (lod->lod_remote_mdt_count + 1);
1805 /* tgt_dt and fid must be ready after search avaible OSP
1806 * in the above loop */
1807 LASSERT(tgt_dt != NULL);
1808 LASSERT(fid_is_sane(&fid));
1809 conf.loc_flags = LOC_F_NEW;
1810 dto = dt_locate_at(env, tgt_dt, &fid,
1811 dt->do_lu.lo_dev->ld_site->ls_top_dev,
1814 GOTO(out_put, rc = PTR_ERR(dto));
1818 lo->ldo_dir_striped = 1;
1819 lo->ldo_stripe = stripe;
1820 lo->ldo_stripenr = i;
1821 lo->ldo_stripes_allocated = stripe_count;
1823 if (lo->ldo_stripenr == 0)
1824 GOTO(out_put, rc = -ENOSPC);
1826 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
1832 for (i = 0; i < stripe_count; i++)
1833 if (stripe[i] != NULL)
1834 lu_object_put(env, &stripe[i]->do_lu);
1835 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
1836 lo->ldo_stripenr = 0;
1837 lo->ldo_stripes_allocated = 0;
1838 lo->ldo_stripe = NULL;
1842 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
1848 * Declare create striped md object.
1850 * The function declares intention to create a striped directory. This is a
1851 * wrapper for lod_prep_md_striped_create(). The only additional functionality
1852 * is to verify pattern \a lum_buf is good. Check that function for the details.
1854 * \param[in] env execution environment
1855 * \param[in] dt object
1856 * \param[in] attr attributes to initialize the objects with
1857 * \param[in] lum_buf a pattern specifying the number of stripes and
1859 * \param[in] dof type of objects to be created
1860 * \param[in] th transaction handle
1862 * \retval 0 on success
1863 * \retval negative if failed
1866 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
1867 struct dt_object *dt,
1868 struct lu_attr *attr,
1869 const struct lu_buf *lum_buf,
1870 struct dt_object_format *dof,
1873 struct lod_object *lo = lod_dt_obj(dt);
1874 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1875 struct lmv_user_md_v1 *lum;
1879 lum = lum_buf->lb_buf;
1880 LASSERT(lum != NULL);
1882 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
1883 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
1884 (int)le32_to_cpu(lum->lum_stripe_offset));
1886 if (le32_to_cpu(lum->lum_stripe_count) == 0)
1889 rc = lod_verify_md_striping(lod, lum);
1893 /* prepare dir striped objects */
1894 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
1896 /* failed to create striping, let's reset
1897 * config so that others don't get confused */
1898 lod_object_free_striping(env, lo);
1906 * Implementation of dt_object_operations::do_declare_xattr_set.
1908 * Used with regular (non-striped) objects. Basically it
1909 * initializes the striping information and applies the
1910 * change to all the stripes.
1912 * \see dt_object_operations::do_declare_xattr_set() in the API description
1915 static int lod_dir_declare_xattr_set(const struct lu_env *env,
1916 struct dt_object *dt,
1917 const struct lu_buf *buf,
1918 const char *name, int fl,
1921 struct dt_object *next = dt_object_child(dt);
1922 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
1923 struct lod_object *lo = lod_dt_obj(dt);
1928 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1929 struct lmv_user_md_v1 *lum;
1931 LASSERT(buf != NULL && buf->lb_buf != NULL);
1933 rc = lod_verify_md_striping(d, lum);
1938 rc = lod_sub_object_declare_xattr_set(env, next, buf, name, fl, th);
1942 /* Note: Do not set LinkEA on sub-stripes, otherwise
1943 * it will confuse the fid2path process(see mdt_path_current()).
1944 * The linkEA between master and sub-stripes is set in
1945 * lod_xattr_set_lmv(). */
1946 if (strcmp(name, XATTR_NAME_LINK) == 0)
1949 /* set xattr to each stripes, if needed */
1950 rc = lod_load_striping(env, lo);
1954 if (lo->ldo_stripenr == 0)
1957 for (i = 0; i < lo->ldo_stripenr; i++) {
1958 LASSERT(lo->ldo_stripe[i]);
1960 rc = lod_sub_object_declare_xattr_set(env, lo->ldo_stripe[i],
1970 * Reset parent FID on OST object
1972 * Replace parent FID with @dt object FID, which is only called during migration
1973 * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
1974 * the FID is changed.
1976 * \param[in] env execution environment
1977 * \param[in] dt dt_object whose stripes's parent FID will be reset
1978 * \parem[in] th thandle
1979 * \param[in] declare if it is declare
1981 * \retval 0 if reset succeeds
1982 * \retval negative errno if reset fais
1984 static int lod_object_replace_parent_fid(const struct lu_env *env,
1985 struct dt_object *dt,
1986 struct thandle *th, bool declare)
1988 struct lod_object *lo = lod_dt_obj(dt);
1989 struct lod_thread_info *info = lod_env_info(env);
1990 struct lu_buf *buf = &info->lti_buf;
1991 struct filter_fid *ff;
1995 LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
1997 /* set xattr to each stripes, if needed */
1998 rc = lod_load_striping(env, lo);
2002 if (lo->ldo_stripenr == 0)
2005 if (info->lti_ea_store_size < sizeof(*ff)) {
2006 rc = lod_ea_store_resize(info, sizeof(*ff));
2011 buf->lb_buf = info->lti_ea_store;
2012 buf->lb_len = info->lti_ea_store_size;
2014 for (i = 0; i < lo->ldo_stripenr; i++) {
2015 if (lo->ldo_stripe[i] == NULL)
2018 rc = dt_xattr_get(env, lo->ldo_stripe[i], buf,
2026 fid_le_to_cpu(&ff->ff_parent, &ff->ff_parent);
2027 ff->ff_parent.f_seq = lu_object_fid(&dt->do_lu)->f_seq;
2028 ff->ff_parent.f_oid = lu_object_fid(&dt->do_lu)->f_oid;
2029 fid_cpu_to_le(&ff->ff_parent, &ff->ff_parent);
2032 rc = lod_sub_object_declare_xattr_set(env,
2033 lo->ldo_stripe[i], buf,
2035 LU_XATTR_REPLACE, th);
2037 rc = lod_sub_object_xattr_set(env, lo->ldo_stripe[i],
2038 buf, XATTR_NAME_FID,
2039 LU_XATTR_REPLACE, th);
2049 * Implementation of dt_object_operations::do_declare_xattr_set.
2051 * \see dt_object_operations::do_declare_xattr_set() in the API description
2054 * the extension to the API:
2055 * - declaring LOVEA requests striping creation
2056 * - LU_XATTR_REPLACE means layout swap
2058 static int lod_declare_xattr_set(const struct lu_env *env,
2059 struct dt_object *dt,
2060 const struct lu_buf *buf,
2061 const char *name, int fl,
2064 struct dt_object *next = dt_object_child(dt);
2065 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2071 * allow to declare predefined striping on a new (!mode) object
2072 * which is supposed to be replay of regular file creation
2073 * (when LOV setting is declared)
2074 * LU_XATTR_REPLACE is set to indicate a layout swap
2076 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2077 if ((S_ISREG(mode) || mode == 0) && strcmp(name, XATTR_NAME_LOV) == 0 &&
2078 !(fl & LU_XATTR_REPLACE)) {
2080 * this is a request to manipulate object's striping
2082 if (dt_object_exists(dt)) {
2083 rc = dt_attr_get(env, next, attr);
2087 memset(attr, 0, sizeof(*attr));
2088 attr->la_valid = LA_TYPE | LA_MODE;
2089 attr->la_mode = S_IFREG;
2091 rc = lod_declare_striped_object(env, dt, attr, buf, th);
2092 } else if (S_ISDIR(mode)) {
2093 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2094 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
2095 rc = lod_object_replace_parent_fid(env, dt, th, true);
2097 rc = lod_sub_object_declare_xattr_set(env, next, buf, name,
2105 * Apply xattr changes to the object.
2107 * Applies xattr changes to the object and the stripes if the latter exist.
2109 * \param[in] env execution environment
2110 * \param[in] dt object
2111 * \param[in] buf buffer pointing to the new value of xattr
2112 * \param[in] name name of xattr
2113 * \param[in] fl flags
2114 * \param[in] th transaction handle
2116 * \retval 0 on success
2117 * \retval negative if failed
2119 static int lod_xattr_set_internal(const struct lu_env *env,
2120 struct dt_object *dt,
2121 const struct lu_buf *buf,
2122 const char *name, int fl,
2125 struct dt_object *next = dt_object_child(dt);
2126 struct lod_object *lo = lod_dt_obj(dt);
2131 rc = lod_sub_object_xattr_set(env, next, buf, name, fl, th);
2132 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2135 /* Note: Do not set LinkEA on sub-stripes, otherwise
2136 * it will confuse the fid2path process(see mdt_path_current()).
2137 * The linkEA between master and sub-stripes is set in
2138 * lod_xattr_set_lmv(). */
2139 if (lo->ldo_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2142 for (i = 0; i < lo->ldo_stripenr; i++) {
2143 LASSERT(lo->ldo_stripe[i]);
2145 rc = lod_sub_object_xattr_set(env, lo->ldo_stripe[i], buf, name,
2155 * Delete an extended attribute.
2157 * Deletes specified xattr from the object and the stripes if the latter exist.
2159 * \param[in] env execution environment
2160 * \param[in] dt object
2161 * \param[in] name name of xattr
2162 * \param[in] th transaction handle
2164 * \retval 0 on success
2165 * \retval negative if failed
2167 static int lod_xattr_del_internal(const struct lu_env *env,
2168 struct dt_object *dt,
2169 const char *name, struct thandle *th)
2171 struct dt_object *next = dt_object_child(dt);
2172 struct lod_object *lo = lod_dt_obj(dt);
2177 rc = lod_sub_object_xattr_del(env, next, name, th);
2178 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2181 if (lo->ldo_stripenr == 0)
2184 for (i = 0; i < lo->ldo_stripenr; i++) {
2185 LASSERT(lo->ldo_stripe[i]);
2187 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name,
2197 * Set default striping on a directory.
2199 * Sets specified striping on a directory object unless it matches the default
2200 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2201 * EA. This striping will be used when regular file is being created in this
2204 * \param[in] env execution environment
2205 * \param[in] dt the striped object
2206 * \param[in] buf buffer with the striping
2207 * \param[in] name name of EA
2208 * \param[in] fl xattr flag (see OSD API description)
2209 * \param[in] th transaction handle
2211 * \retval 0 on success
2212 * \retval negative if failed
2214 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
2215 struct dt_object *dt,
2216 const struct lu_buf *buf,
2217 const char *name, int fl,
2220 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2221 struct lov_user_md_v1 *lum;
2222 struct lov_user_md_v3 *v3 = NULL;
2223 const char *pool_name = NULL;
2227 LASSERT(buf != NULL && buf->lb_buf != NULL);
2230 rc = lod_verify_striping(d, buf, false);
2234 if (lum->lmm_magic == LOV_USER_MAGIC_V3) {
2236 if (v3->lmm_pool_name[0] != '\0')
2237 pool_name = v3->lmm_pool_name;
2240 /* if { size, offset, count } = { 0, -1, 0 } and no pool
2241 * (i.e. all default values specified) then delete default
2242 * striping from dir. */
2244 "set default striping: sz %u # %u offset %d %s %s\n",
2245 (unsigned)lum->lmm_stripe_size,
2246 (unsigned)lum->lmm_stripe_count,
2247 (int)lum->lmm_stripe_offset,
2248 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
2250 if (LOVEA_DELETE_VALUES(lum->lmm_stripe_size, lum->lmm_stripe_count,
2251 lum->lmm_stripe_offset, pool_name)) {
2252 rc = lod_xattr_del_internal(env, dt, name, th);
2256 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2263 * Set default striping on a directory object.
2265 * Sets specified striping on a directory object unless it matches the default
2266 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2267 * EA. This striping will be used when a new directory is being created in the
2270 * \param[in] env execution environment
2271 * \param[in] dt the striped object
2272 * \param[in] buf buffer with the striping
2273 * \param[in] name name of EA
2274 * \param[in] fl xattr flag (see OSD API description)
2275 * \param[in] th transaction handle
2277 * \retval 0 on success
2278 * \retval negative if failed
2280 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
2281 struct dt_object *dt,
2282 const struct lu_buf *buf,
2283 const char *name, int fl,
2286 struct lmv_user_md_v1 *lum;
2290 LASSERT(buf != NULL && buf->lb_buf != NULL);
2293 CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
2294 le32_to_cpu(lum->lum_stripe_count),
2295 (int)le32_to_cpu(lum->lum_stripe_offset));
2297 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
2298 le32_to_cpu(lum->lum_stripe_offset)) &&
2299 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
2300 rc = lod_xattr_del_internal(env, dt, name, th);
2304 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2313 * Turn directory into a striped directory.
2315 * During replay the client sends the striping created before MDT
2316 * failure, then the layer above LOD sends this defined striping
2317 * using ->do_xattr_set(), so LOD uses this method to replay creation
2318 * of the stripes. Notice the original information for the striping
2319 * (#stripes, FIDs, etc) was transferred in declare path.
2321 * \param[in] env execution environment
2322 * \param[in] dt the striped object
2323 * \param[in] buf not used currently
2324 * \param[in] name not used currently
2325 * \param[in] fl xattr flag (see OSD API description)
2326 * \param[in] th transaction handle
2328 * \retval 0 on success
2329 * \retval negative if failed
2331 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
2332 const struct lu_buf *buf, const char *name,
2333 int fl, struct thandle *th)
2335 struct lod_object *lo = lod_dt_obj(dt);
2336 struct lod_thread_info *info = lod_env_info(env);
2337 struct lu_attr *attr = &info->lti_attr;
2338 struct dt_object_format *dof = &info->lti_format;
2339 struct lu_buf lmv_buf;
2340 struct lu_buf slave_lmv_buf;
2341 struct lmv_mds_md_v1 *lmm;
2342 struct lmv_mds_md_v1 *slave_lmm = NULL;
2343 struct dt_insert_rec *rec = &info->lti_dt_rec;
2348 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2351 /* The stripes are supposed to be allocated in declare phase,
2352 * if there are no stripes being allocated, it will skip */
2353 if (lo->ldo_stripenr == 0)
2356 rc = dt_attr_get(env, dt_object_child(dt), attr);
2360 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
2361 LA_MODE | LA_UID | LA_GID | LA_TYPE;
2362 dof->dof_type = DFT_DIR;
2364 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
2367 lmm = lmv_buf.lb_buf;
2369 OBD_ALLOC_PTR(slave_lmm);
2370 if (slave_lmm == NULL)
2373 lod_prep_slave_lmv_md(slave_lmm, lmm);
2374 slave_lmv_buf.lb_buf = slave_lmm;
2375 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
2377 rec->rec_type = S_IFDIR;
2378 for (i = 0; i < lo->ldo_stripenr; i++) {
2379 struct dt_object *dto;
2380 char *stripe_name = info->lti_key;
2381 struct lu_name *sname;
2382 struct linkea_data ldata = { NULL };
2383 struct lu_buf linkea_buf;
2385 dto = lo->ldo_stripe[i];
2387 dt_write_lock(env, dto, MOR_TGT_CHILD);
2388 rc = lod_sub_object_create(env, dto, attr, NULL, dof,
2391 dt_write_unlock(env, dto);
2395 rc = lod_sub_object_ref_add(env, dto, th);
2396 dt_write_unlock(env, dto);
2400 rec->rec_fid = lu_object_fid(&dto->do_lu);
2401 rc = lod_sub_object_index_insert(env, dto,
2402 (const struct dt_rec *)rec,
2403 (const struct dt_key *)dot, th, 0);
2407 rec->rec_fid = lu_object_fid(&dt->do_lu);
2408 rc = lod_sub_object_index_insert(env, dto, (struct dt_rec *)rec,
2409 (const struct dt_key *)dotdot, th, 0);
2413 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
2414 cfs_fail_val != i) {
2415 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
2417 slave_lmm->lmv_master_mdt_index =
2420 slave_lmm->lmv_master_mdt_index =
2423 rc = lod_sub_object_xattr_set(env, dto, &slave_lmv_buf,
2424 XATTR_NAME_LMV, fl, th);
2429 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
2431 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2432 PFID(lu_object_fid(&dto->do_lu)), i + 1);
2434 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2435 PFID(lu_object_fid(&dto->do_lu)), i);
2437 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2438 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
2439 sname, lu_object_fid(&dt->do_lu));
2443 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
2444 linkea_buf.lb_len = ldata.ld_leh->leh_len;
2445 rc = lod_sub_object_xattr_set(env, dto, &linkea_buf,
2446 XATTR_NAME_LINK, 0, th);
2450 rec->rec_fid = lu_object_fid(&dto->do_lu);
2451 rc = lod_sub_object_index_insert(env, dt_object_child(dt),
2452 (const struct dt_rec *)rec,
2453 (const struct dt_key *)stripe_name, th, 0);
2457 rc = lod_sub_object_ref_add(env, dt_object_child(dt), th);
2462 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
2463 rc = lod_sub_object_xattr_set(env, dt_object_child(dt),
2464 &lmv_buf, XATTR_NAME_LMV, fl, th);
2466 if (slave_lmm != NULL)
2467 OBD_FREE_PTR(slave_lmm);
2473 * Helper function to declare/execute creation of a striped directory
2475 * Called in declare/create object path, prepare striping for a directory
2476 * and prepare defaults data striping for the objects to be created in
2477 * that directory. Notice the function calls "declaration" or "execution"
2478 * methods depending on \a declare param. This is a consequence of the
2479 * current approach while we don't have natural distributed transactions:
2480 * we basically execute non-local updates in the declare phase. So, the
2481 * arguments for the both phases are the same and this is the reason for
2482 * this function to exist.
2484 * \param[in] env execution environment
2485 * \param[in] dt object
2486 * \param[in] attr attributes the stripes will be created with
2487 * \param[in] dof format of stripes (see OSD API description)
2488 * \param[in] th transaction handle
2489 * \param[in] declare where to call "declare" or "execute" methods
2491 * \retval 0 on success
2492 * \retval negative if failed
2494 static int lod_dir_striping_create_internal(const struct lu_env *env,
2495 struct dt_object *dt,
2496 struct lu_attr *attr,
2497 struct dt_object_format *dof,
2501 struct lod_thread_info *info = lod_env_info(env);
2502 struct lod_object *lo = lod_dt_obj(dt);
2503 const struct lod_default_striping *lds = lo->ldo_def_striping;
2504 const char *poolname = NULL;
2508 LASSERT(ergo(lds != NULL,
2509 lds->lds_def_striping_set ||
2510 lds->lds_dir_def_striping_set));
2512 if (!LMVEA_DELETE_VALUES(lo->ldo_stripenr,
2513 lo->ldo_dir_stripe_offset)) {
2514 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
2515 int stripe_count = lo->ldo_stripenr;
2517 if (info->lti_ea_store_size < sizeof(*v1)) {
2518 rc = lod_ea_store_resize(info, sizeof(*v1));
2521 v1 = info->lti_ea_store;
2524 memset(v1, 0, sizeof(*v1));
2525 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
2526 v1->lum_stripe_count = cpu_to_le32(stripe_count);
2527 v1->lum_stripe_offset =
2528 cpu_to_le32(lo->ldo_dir_stripe_offset);
2530 info->lti_buf.lb_buf = v1;
2531 info->lti_buf.lb_len = sizeof(*v1);
2534 rc = lod_declare_xattr_set_lmv(env, dt, attr,
2535 &info->lti_buf, dof, th);
2537 rc = lod_xattr_set_lmv(env, dt, &info->lti_buf,
2538 XATTR_NAME_LMV, 0, th);
2543 /* Transfer default LMV striping from the parent */
2544 if (lds != NULL && lds->lds_dir_def_striping_set &&
2545 !LMVEA_DELETE_VALUES(lds->lds_dir_def_stripenr,
2546 lds->lds_dir_def_stripe_offset)) {
2547 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
2549 if (info->lti_ea_store_size < sizeof(*v1)) {
2550 rc = lod_ea_store_resize(info, sizeof(*v1));
2553 v1 = info->lti_ea_store;
2556 memset(v1, 0, sizeof(*v1));
2557 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
2558 v1->lum_stripe_count = cpu_to_le32(lds->lds_dir_def_stripenr);
2559 v1->lum_stripe_offset =
2560 cpu_to_le32(lds->lds_dir_def_stripe_offset);
2562 cpu_to_le32(lds->lds_dir_def_hash_type);
2564 info->lti_buf.lb_buf = v1;
2565 info->lti_buf.lb_len = sizeof(*v1);
2567 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
2568 XATTR_NAME_DEFAULT_LMV,
2571 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
2573 XATTR_NAME_DEFAULT_LMV, 0,
2579 if (lds != NULL && lds->lds_def_pool[0] != '\0')
2580 poolname = lds->lds_def_pool;
2582 /* Transfer default LOV striping from the parent */
2583 if (lds != NULL && lds->lds_def_striping_set &&
2584 !LOVEA_DELETE_VALUES(lds->lds_def_stripe_size,
2585 lds->lds_def_stripenr,
2586 lds->lds_def_stripe_offset,
2588 struct lov_user_md_v3 *v3 = info->lti_ea_store;
2590 if (info->lti_ea_store_size < sizeof(*v3)) {
2591 rc = lod_ea_store_resize(info, sizeof(*v3));
2594 v3 = info->lti_ea_store;
2597 memset(v3, 0, sizeof(*v3));
2598 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
2599 v3->lmm_stripe_count = cpu_to_le16(lds->lds_def_stripenr);
2600 v3->lmm_stripe_offset = cpu_to_le16(lds->lds_def_stripe_offset);
2601 v3->lmm_stripe_size = cpu_to_le32(lds->lds_def_stripe_size);
2602 if (poolname != NULL)
2603 strlcpy(v3->lmm_pool_name, poolname,
2604 sizeof(v3->lmm_pool_name));
2606 info->lti_buf.lb_buf = v3;
2607 info->lti_buf.lb_len = sizeof(*v3);
2610 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
2611 XATTR_NAME_LOV, 0, th);
2613 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
2614 XATTR_NAME_LOV, 0, th);
2622 static int lod_declare_dir_striping_create(const struct lu_env *env,
2623 struct dt_object *dt,
2624 struct lu_attr *attr,
2625 struct dt_object_format *dof,
2628 return lod_dir_striping_create_internal(env, dt, attr, dof, th, true);
2631 static int lod_dir_striping_create(const struct lu_env *env,
2632 struct dt_object *dt,
2633 struct lu_attr *attr,
2634 struct dt_object_format *dof,
2637 return lod_dir_striping_create_internal(env, dt, attr, dof, th, false);
2641 * Implementation of dt_object_operations::do_xattr_set.
2643 * Sets specified extended attribute on the object. Three types of EAs are
2645 * LOV EA - stores striping for a regular file or default striping (when set
2647 * LMV EA - stores a marker for the striped directories
2648 * DMV EA - stores default directory striping
2650 * When striping is applied to a non-striped existing object (this is called
2651 * late striping), then LOD notices the caller wants to turn the object into a
2652 * striped one. The stripe objects are created and appropriate EA is set:
2653 * LOV EA storing all the stripes directly or LMV EA storing just a small header
2654 * with striping configuration.
2656 * \see dt_object_operations::do_xattr_set() in the API description for details.
2658 static int lod_xattr_set(const struct lu_env *env,
2659 struct dt_object *dt, const struct lu_buf *buf,
2660 const char *name, int fl, struct thandle *th)
2662 struct dt_object *next = dt_object_child(dt);
2666 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2667 strcmp(name, XATTR_NAME_LMV) == 0) {
2668 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
2670 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
2671 LMV_HASH_FLAG_MIGRATION)
2672 rc = lod_sub_object_xattr_set(env, next, buf, name, fl,
2675 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
2680 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2681 strcmp(name, XATTR_NAME_LOV) == 0) {
2683 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
2685 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2686 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2688 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
2691 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
2692 !strcmp(name, XATTR_NAME_LOV)) {
2693 /* in case of lov EA swap, just set it
2694 * if not, it is a replay so check striping match what we
2695 * already have during req replay, declare_xattr_set()
2696 * defines striping, then create() does the work */
2697 if (fl & LU_XATTR_REPLACE) {
2698 /* free stripes, then update disk */
2699 lod_object_free_striping(env, lod_dt_obj(dt));
2701 rc = lod_sub_object_xattr_set(env, next, buf, name,
2703 } else if (dt_object_remote(dt)) {
2704 /* This only happens during migration, see
2705 * mdd_migrate_create(), in which Master MDT will
2706 * create a remote target object, and only set
2707 * (migrating) stripe EA on the remote object,
2708 * and does not need creating each stripes. */
2709 rc = lod_sub_object_xattr_set(env, next, buf, name,
2712 rc = lod_striping_create(env, dt, NULL, NULL, th);
2715 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
2716 rc = lod_object_replace_parent_fid(env, dt, th, false);
2721 /* then all other xattr */
2722 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2728 * Implementation of dt_object_operations::do_declare_xattr_del.
2730 * \see dt_object_operations::do_declare_xattr_del() in the API description
2733 static int lod_declare_xattr_del(const struct lu_env *env,
2734 struct dt_object *dt, const char *name,
2737 struct lod_object *lo = lod_dt_obj(dt);
2742 rc = lod_sub_object_declare_xattr_del(env, dt_object_child(dt),
2747 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2750 /* set xattr to each stripes, if needed */
2751 rc = lod_load_striping(env, lo);
2755 if (lo->ldo_stripenr == 0)
2758 for (i = 0; i < lo->ldo_stripenr; i++) {
2759 LASSERT(lo->ldo_stripe[i]);
2760 rc = lod_sub_object_declare_xattr_del(env, lo->ldo_stripe[i],
2770 * Implementation of dt_object_operations::do_xattr_del.
2772 * If EA storing a regular striping is being deleted, then release
2773 * all the references to the stripe objects in core.
2775 * \see dt_object_operations::do_xattr_del() in the API description for details.
2777 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
2778 const char *name, struct thandle *th)
2780 struct dt_object *next = dt_object_child(dt);
2781 struct lod_object *lo = lod_dt_obj(dt);
2786 if (!strcmp(name, XATTR_NAME_LOV))
2787 lod_object_free_striping(env, lod_dt_obj(dt));
2789 rc = lod_sub_object_xattr_del(env, next, name, th);
2790 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2793 if (lo->ldo_stripenr == 0)
2796 for (i = 0; i < lo->ldo_stripenr; i++) {
2797 LASSERT(lo->ldo_stripe[i]);
2799 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name, th);
2808 * Implementation of dt_object_operations::do_xattr_list.
2810 * \see dt_object_operations::do_xattr_list() in the API description
2813 static int lod_xattr_list(const struct lu_env *env,
2814 struct dt_object *dt, const struct lu_buf *buf)
2816 return dt_xattr_list(env, dt_object_child(dt), buf);
2819 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
2821 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
2826 * Get default striping.
2828 * \param[in] env execution environment
2829 * \param[in] lo object
2830 * \param[out] lds default striping
2832 * \retval 0 on success
2833 * \retval negative if failed
2835 static int lod_get_default_lov_striping(const struct lu_env *env,
2836 struct lod_object *lo,
2837 struct lod_default_striping *lds)
2839 struct lod_thread_info *info = lod_env_info(env);
2840 struct lov_user_md_v1 *v1 = NULL;
2841 struct lov_user_md_v3 *v3 = NULL;
2844 rc = lod_get_lov_ea(env, lo);
2848 if (rc < (typeof(rc))sizeof(struct lov_user_md))
2851 v1 = info->lti_ea_store;
2852 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
2853 lustre_swab_lov_user_md_v1(v1);
2854 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
2855 v3 = (struct lov_user_md_v3 *)v1;
2856 lustre_swab_lov_user_md_v3(v3);
2859 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1)
2862 if (v1->lmm_pattern != LOV_PATTERN_RAID0 && v1->lmm_pattern != 0)
2865 lds->lds_def_stripenr = v1->lmm_stripe_count;
2866 lds->lds_def_stripe_size = v1->lmm_stripe_size;
2867 lds->lds_def_stripe_offset = v1->lmm_stripe_offset;
2868 lds->lds_def_striping_set = 1;
2869 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2870 v3 = (struct lov_user_md_v3 *)v1;
2871 if (v3->lmm_pool_name[0] != '\0')
2872 strlcpy(lds->lds_def_pool, v3->lmm_pool_name,
2873 sizeof(lds->lds_def_pool));
2880 * Get default directory striping.
2882 * \param[in] env execution environment
2883 * \param[in] lo object
2884 * \param[out] lds default striping
2886 * \retval 0 on success
2887 * \retval negative if failed
2889 static int lod_get_default_lmv_striping(const struct lu_env *env,
2890 struct lod_object *lo,
2891 struct lod_default_striping *lds)
2893 struct lod_thread_info *info = lod_env_info(env);
2894 struct lmv_user_md_v1 *v1 = NULL;
2897 rc = lod_get_default_lmv_ea(env, lo);
2901 if (rc < (typeof(rc))sizeof(struct lmv_user_md))
2904 v1 = info->lti_ea_store;
2906 lds->lds_dir_def_stripenr = le32_to_cpu(v1->lum_stripe_count);
2907 lds->lds_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
2908 lds->lds_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
2909 lds->lds_dir_def_striping_set = 1;
2915 * Get default striping in the object.
2917 * Get object default striping and default directory striping.
2919 * \param[in] env execution environment
2920 * \param[in] lo object
2921 * \param[out] lds default striping
2923 * \retval 0 on success
2924 * \retval negative if failed
2926 static int lod_get_default_striping(const struct lu_env *env,
2927 struct lod_object *lo,
2928 struct lod_default_striping *lds)
2932 rc = lod_get_default_lov_striping(env, lo, lds);
2934 rc = lod_get_default_lmv_striping(env, lo, lds);
2939 * Apply default striping on object.
2941 * If object striping pattern is not set, set to the one in default striping.
2942 * The default striping is from parent or fs.
2944 * \param[in] lo new object
2945 * \param[in] lds default striping
2946 * \param[in] mode new object's mode
2948 static void lod_striping_from_default(struct lod_object *lo,
2949 const struct lod_default_striping *lds,
2952 if (lds->lds_def_striping_set && S_ISREG(mode)) {
2953 if (lo->ldo_stripenr == 0)
2954 lo->ldo_stripenr = lds->lds_def_stripenr;
2955 if (lo->ldo_stripe_size == 0)
2956 lo->ldo_stripe_size = lds->lds_def_stripe_size;
2957 if (lo->ldo_stripe_offset == LOV_OFFSET_DEFAULT)
2958 lo->ldo_stripe_offset = lds->lds_def_stripe_offset;
2959 if (lo->ldo_pool == NULL && lds->lds_def_pool[0] != '\0')
2960 lod_object_set_pool(lo, lds->lds_def_pool);
2962 CDEBUG(D_INFO, "striping from default: count %hu, size %u, "
2963 "offset %d, pool %s\n",
2964 lo->ldo_stripenr, lo->ldo_stripe_size,
2965 (int)lo->ldo_stripe_offset, lo->ldo_pool ?: "");
2966 } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
2967 if (lo->ldo_stripenr == 0)
2968 lo->ldo_stripenr = lds->lds_dir_def_stripenr;
2969 if (lo->ldo_dir_stripe_offset == -1)
2970 lo->ldo_dir_stripe_offset =
2971 lds->lds_dir_def_stripe_offset;
2972 if (lo->ldo_dir_hash_type == 0)
2973 lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
2975 CDEBUG(D_INFO, "striping from default: count %hu, offset %d, "
2977 lo->ldo_stripenr, (int)lo->ldo_dir_stripe_offset,
2978 lo->ldo_dir_hash_type);
2983 * Implementation of dt_object_operations::do_ah_init.
2985 * This method is used to make a decision on the striping configuration for the
2986 * object being created. It can be taken from the \a parent object if it exists,
2987 * or filesystem's default. The resulting configuration (number of stripes,
2988 * stripe size/offset, pool name, etc) is stored in the object itself and will
2989 * be used by the methods like ->doo_declare_create().
2991 * \see dt_object_operations::do_ah_init() in the API description for details.
2993 static void lod_ah_init(const struct lu_env *env,
2994 struct dt_allocation_hint *ah,
2995 struct dt_object *parent,
2996 struct dt_object *child,
2999 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
3000 struct lod_thread_info *info = lod_env_info(env);
3001 struct lod_default_striping *lds = &info->lti_def_striping;
3002 struct dt_object *nextp = NULL;
3003 struct dt_object *nextc;
3004 struct lod_object *lp = NULL;
3005 struct lod_object *lc;
3006 struct lov_desc *desc;
3011 if (likely(parent)) {
3012 nextp = dt_object_child(parent);
3013 lp = lod_dt_obj(parent);
3016 nextc = dt_object_child(child);
3017 lc = lod_dt_obj(child);
3019 LASSERT(lc->ldo_stripenr == 0);
3020 LASSERT(lc->ldo_stripe == NULL);
3022 if (!dt_object_exists(nextc))
3023 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
3025 if (S_ISDIR(child_mode)) {
3026 /* other default values are 0 */
3027 lc->ldo_dir_stripe_offset = -1;
3029 memset(lds, 0, sizeof(*lds));
3030 lod_get_default_striping(env, lp, lds);
3032 /* inherit parent default striping */
3033 if (lds->lds_def_striping_set || lds->lds_dir_def_striping_set)
3034 lc->ldo_def_striping = lds;
3036 /* It should always honour the specified stripes */
3037 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
3038 lod_verify_md_striping(d, ah->dah_eadata) == 0) {
3039 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
3041 lc->ldo_stripenr = le32_to_cpu(lum1->lum_stripe_count);
3042 lc->ldo_dir_stripe_offset =
3043 le32_to_cpu(lum1->lum_stripe_offset);
3044 lc->ldo_dir_hash_type =
3045 le32_to_cpu(lum1->lum_hash_type);
3046 CDEBUG(D_INFO, "set dir stripe: count %hu, offset %d, "
3049 (int)lc->ldo_dir_stripe_offset,
3050 lc->ldo_dir_hash_type);
3052 lod_striping_from_default(lc, lds, child_mode);
3055 /* shrink the stripe_count to the avaible MDT count */
3056 if (lc->ldo_stripenr > d->lod_remote_mdt_count + 1 &&
3057 !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))
3058 lc->ldo_stripenr = d->lod_remote_mdt_count + 1;
3060 /* Directory will be striped only if stripe_count > 1, if
3061 * stripe_count == 1, let's reset stripenr = 0 to avoid
3062 * create single master stripe and also help to unify the
3063 * stripe handling of directories and files */
3064 if (lc->ldo_stripenr == 1)
3065 lc->ldo_stripenr = 0;
3067 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
3068 lc->ldo_stripenr, (int)lc->ldo_dir_stripe_offset,
3069 lc->ldo_dir_hash_type);
3075 * if object is going to be striped over OSTs, transfer default
3076 * striping information to the child, so that we can use it
3077 * during declaration and creation
3079 if (!lod_object_will_be_striped(S_ISREG(child_mode),
3080 lu_object_fid(&child->do_lu)))
3083 /* other default values are 0 */
3084 lc->ldo_stripe_offset = LOV_OFFSET_DEFAULT;
3086 /* striping from parent default */
3087 if (likely(parent)) {
3088 memset(lds, 0, sizeof(*lds));
3089 lod_get_default_lov_striping(env, lp, lds);
3090 lod_striping_from_default(lc, lds, child_mode);
3093 if (d->lod_md_root == NULL) {
3094 struct dt_object *root;
3095 struct lod_object *lroot;
3097 lu_root_fid(&info->lti_fid);
3098 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
3099 if (!IS_ERR(root)) {
3100 lroot = lod_dt_obj(root);
3102 spin_lock(&d->lod_lock);
3103 if (d->lod_md_root != NULL)
3105 &d->lod_md_root->ldo_obj.do_lu);
3106 d->lod_md_root = lroot;
3107 spin_unlock(&d->lod_lock);
3111 /* if parent doesn't provide all defaults, striping from fs default */
3112 if (d->lod_md_root != NULL &&
3113 (lc->ldo_stripenr == 0 ||
3114 lc->ldo_stripe_size == 0 ||
3115 lc->ldo_stripe_offset == LOV_OFFSET_DEFAULT ||
3116 lc->ldo_pool == NULL)) {
3117 memset(lds, 0, sizeof(*lds));
3118 lod_get_default_lov_striping(env, d->lod_md_root, lds);
3119 lod_striping_from_default(lc, lds, child_mode);
3123 * fs default striping may not be explicitly set, or historically set
3124 * in config log, check striping sanity here and fix to sane values.
3126 desc = &d->lod_desc;
3127 if (lc->ldo_stripenr == 0)
3128 lc->ldo_stripenr = desc->ld_default_stripe_count;
3129 if (lc->ldo_stripe_size == 0)
3130 lc->ldo_stripe_size = desc->ld_default_stripe_size;
3132 CDEBUG(D_INFO, "final striping [%hu %u %d %s]\n",
3133 lc->ldo_stripenr, lc->ldo_stripe_size,
3134 (int)lc->ldo_stripe_offset, lc->ldo_pool ?: "");
3138 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
3140 * Size initialization on late striping.
3142 * Propagate the size of a truncated object to a deferred striping.
3143 * This function handles a special case when truncate was done on a
3144 * non-striped object and now while the striping is being created
3145 * we can't lose that size, so we have to propagate it to the stripes
3148 * \param[in] env execution environment
3149 * \param[in] dt object
3150 * \param[in] th transaction handle
3152 * \retval 0 on success
3153 * \retval negative if failed
3155 static int lod_declare_init_size(const struct lu_env *env,
3156 struct dt_object *dt, struct thandle *th)
3158 struct dt_object *next = dt_object_child(dt);
3159 struct lod_object *lo = lod_dt_obj(dt);
3160 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3161 uint64_t size, offs;
3165 /* XXX: we support the simplest (RAID0) striping so far */
3166 LASSERT(lo->ldo_stripe || lo->ldo_stripenr == 0);
3167 LASSERT(lo->ldo_stripe_size > 0);
3169 if (lo->ldo_stripenr == 0)
3172 rc = dt_attr_get(env, next, attr);
3173 LASSERT(attr->la_valid & LA_SIZE);
3177 size = attr->la_size;
3181 /* ll_do_div64(a, b) returns a % b, and a = a / b */
3182 ll_do_div64(size, (__u64) lo->ldo_stripe_size);
3183 stripe = ll_do_div64(size, (__u64) lo->ldo_stripenr);
3185 size = size * lo->ldo_stripe_size;
3186 offs = attr->la_size;
3187 size += ll_do_div64(offs, lo->ldo_stripe_size);
3189 attr->la_valid = LA_SIZE;
3190 attr->la_size = size;
3192 rc = lod_sub_object_declare_attr_set(env, lo->ldo_stripe[stripe], attr,
3199 * Declare creation of striped object.
3201 * The function declares creation stripes for a regular object. The function
3202 * also declares whether the stripes will be created with non-zero size if
3203 * previously size was set non-zero on the master object. If object \a dt is
3204 * not local, then only fully defined striping can be applied in \a lovea.
3205 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
3208 * \param[in] env execution environment
3209 * \param[in] dt object
3210 * \param[in] attr attributes the stripes will be created with
3211 * \param[in] lovea a buffer containing striping description
3212 * \param[in] th transaction handle
3214 * \retval 0 on success
3215 * \retval negative if failed
3217 int lod_declare_striped_object(const struct lu_env *env, struct dt_object *dt,
3218 struct lu_attr *attr,
3219 const struct lu_buf *lovea, struct thandle *th)
3221 struct lod_thread_info *info = lod_env_info(env);
3222 struct dt_object *next = dt_object_child(dt);
3223 struct lod_object *lo = lod_dt_obj(dt);
3227 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
3228 GOTO(out, rc = -ENOMEM);
3230 if (!dt_object_remote(next)) {
3231 /* choose OST and generate appropriate objects */
3232 rc = lod_qos_prep_create(env, lo, attr, lovea, th);
3237 * declare storage for striping data
3239 info->lti_buf.lb_len = lov_mds_md_size(lo->ldo_stripenr,
3240 lo->ldo_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1);
3242 /* LOD can not choose OST objects for remote objects, i.e.
3243 * stripes must be ready before that. Right now, it can only
3244 * happen during migrate, i.e. migrate process needs to create
3245 * remote regular file (mdd_migrate_create), then the migrate
3246 * process will provide stripeEA. */
3247 LASSERT(lovea != NULL);
3248 info->lti_buf = *lovea;
3251 rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
3252 XATTR_NAME_LOV, 0, th);
3257 * if striping is created with local object's size > 0,
3258 * we have to propagate this size to specific object
3259 * the case is possible only when local object was created previously
3261 if (dt_object_exists(next))
3262 rc = lod_declare_init_size(env, dt, th);
3265 /* failed to create striping or to set initial size, let's reset
3266 * config so that others don't get confused */
3268 lod_object_free_striping(env, lo);
3274 * Implementation of dt_object_operations::do_declare_create.
3276 * The method declares creation of a new object. If the object will be striped,
3277 * then helper functions are called to find FIDs for the stripes, declare
3278 * creation of the stripes and declare initialization of the striping
3279 * information to be stored in the master object.
3281 * \see dt_object_operations::do_declare_create() in the API description
3284 static int lod_declare_object_create(const struct lu_env *env,
3285 struct dt_object *dt,
3286 struct lu_attr *attr,
3287 struct dt_allocation_hint *hint,
3288 struct dt_object_format *dof,
3291 struct dt_object *next = dt_object_child(dt);
3292 struct lod_object *lo = lod_dt_obj(dt);
3301 * first of all, we declare creation of local object
3303 rc = lod_sub_object_declare_create(env, next, attr, hint, dof, th);
3307 if (dof->dof_type == DFT_SYM)
3308 dt->do_body_ops = &lod_body_lnk_ops;
3309 else if (dof->dof_type == DFT_REGULAR)
3310 dt->do_body_ops = &lod_body_ops;
3313 * it's lod_ah_init() that has decided the object will be striped
3315 if (dof->dof_type == DFT_REGULAR) {
3316 /* callers don't want stripes */
3317 /* XXX: all tricky interactions with ->ah_make_hint() decided
3318 * to use striping, then ->declare_create() behaving differently
3319 * should be cleaned */
3320 if (dof->u.dof_reg.striped == 0)
3321 lo->ldo_stripenr = 0;
3322 if (lo->ldo_stripenr > 0)
3323 rc = lod_declare_striped_object(env, dt, attr,
3325 } else if (dof->dof_type == DFT_DIR) {
3326 struct seq_server_site *ss;
3328 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
3330 /* If the parent has default stripeEA, and client
3331 * did not find it before sending create request,
3332 * then MDT will return -EREMOTE, and client will
3333 * retrieve the default stripeEA and re-create the
3336 * Note: if dah_eadata != NULL, it means creating the
3337 * striped directory with specified stripeEA, then it
3338 * should ignore the default stripeEA */
3339 if (hint != NULL && hint->dah_eadata == NULL) {
3340 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
3341 GOTO(out, rc = -EREMOTE);
3343 if (lo->ldo_dir_stripe_offset == -1) {
3344 /* child and parent should be in the same MDT */
3345 if (hint->dah_parent != NULL &&
3346 dt_object_remote(hint->dah_parent))
3347 GOTO(out, rc = -EREMOTE);
3348 } else if (lo->ldo_dir_stripe_offset !=
3350 struct lod_device *lod;
3351 struct lod_tgt_descs *ltd;
3352 struct lod_tgt_desc *tgt = NULL;
3353 bool found_mdt = false;
3356 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
3357 ltd = &lod->lod_mdt_descs;
3358 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
3359 tgt = LTD_TGT(ltd, i);
3360 if (tgt->ltd_index ==
3361 lo->ldo_dir_stripe_offset) {
3367 /* If the MDT indicated by stripe_offset can be
3368 * found, then tell client to resend the create
3369 * request to the correct MDT, otherwise return
3370 * error to client */
3372 GOTO(out, rc = -EREMOTE);
3374 GOTO(out, rc = -EINVAL);
3378 rc = lod_declare_dir_striping_create(env, dt, attr, dof, th);
3385 * Creation of a striped regular object.
3387 * The function is called to create the stripe objects for a regular
3388 * striped file. This can happen at the initial object creation or
3389 * when the caller asks LOD to do so using ->do_xattr_set() method
3390 * (so called late striping). Notice all the information are already
3391 * prepared in the form of the list of objects (ldo_stripe field).
3392 * This is done during declare phase.
3394 * \param[in] env execution environment
3395 * \param[in] dt object
3396 * \param[in] attr attributes the stripes will be created with
3397 * \param[in] dof format of stripes (see OSD API description)
3398 * \param[in] th transaction handle
3400 * \retval 0 on success
3401 * \retval negative if failed
3403 int lod_striping_create(const struct lu_env *env, struct dt_object *dt,
3404 struct lu_attr *attr, struct dt_object_format *dof,
3407 struct lod_object *lo = lod_dt_obj(dt);
3411 /* create all underlying objects */
3412 for (i = 0; i < lo->ldo_stripenr; i++) {
3413 LASSERT(lo->ldo_stripe[i]);
3414 rc = lod_sub_object_create(env, lo->ldo_stripe[i], attr, NULL,
3421 rc = lod_generate_and_set_lovea(env, lo, th);
3427 * Implementation of dt_object_operations::do_create.
3429 * If any of preceeding methods (like ->do_declare_create(),
3430 * ->do_ah_init(), etc) chose to create a striped object,
3431 * then this method will create the master and the stripes.
3433 * \see dt_object_operations::do_create() in the API description for details.
3435 static int lod_object_create(const struct lu_env *env, struct dt_object *dt,
3436 struct lu_attr *attr,
3437 struct dt_allocation_hint *hint,
3438 struct dt_object_format *dof, struct thandle *th)
3440 struct lod_object *lo = lod_dt_obj(dt);
3444 /* create local object */
3445 rc = lod_sub_object_create(env, dt_object_child(dt), attr, hint, dof,
3450 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3451 lo->ldo_stripe && dof->u.dof_reg.striped != 0)
3452 rc = lod_striping_create(env, dt, attr, dof, th);
3458 * Implementation of dt_object_operations::do_declare_destroy.
3460 * If the object is a striped directory, then the function declares reference
3461 * removal from the master object (this is an index) to the stripes and declares
3462 * destroy of all the stripes. In all the cases, it declares an intention to
3463 * destroy the object itself.
3465 * \see dt_object_operations::do_declare_destroy() in the API description
3468 static int lod_declare_object_destroy(const struct lu_env *env,
3469 struct dt_object *dt,
3472 struct dt_object *next = dt_object_child(dt);
3473 struct lod_object *lo = lod_dt_obj(dt);
3474 struct lod_thread_info *info = lod_env_info(env);
3475 char *stripe_name = info->lti_key;
3480 * load striping information, notice we don't do this when object
3481 * is being initialized as we don't need this information till
3482 * few specific cases like destroy, chown
3484 rc = lod_load_striping(env, lo);
3488 /* declare destroy for all underlying objects */
3489 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3490 rc = next->do_ops->do_index_try(env, next,
3491 &dt_directory_features);
3495 for (i = 0; i < lo->ldo_stripenr; i++) {
3496 rc = lod_sub_object_declare_ref_del(env, next, th);
3500 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3501 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
3503 rc = lod_sub_object_declare_delete(env, next,
3504 (const struct dt_key *)stripe_name, th);
3511 * we declare destroy for the local object
3513 rc = lod_sub_object_declare_destroy(env, next, th);
3517 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
3518 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3521 /* declare destroy all striped objects */
3522 for (i = 0; i < lo->ldo_stripenr; i++) {
3523 if (lo->ldo_stripe[i] == NULL)
3526 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
3527 rc = lod_sub_object_declare_ref_del(env,
3528 lo->ldo_stripe[i], th);
3530 rc = lod_sub_object_declare_destroy(env, lo->ldo_stripe[i],
3540 * Implementation of dt_object_operations::do_destroy.
3542 * If the object is a striped directory, then the function removes references
3543 * from the master object (this is an index) to the stripes and destroys all
3544 * the stripes. In all the cases, the function destroys the object itself.
3546 * \see dt_object_operations::do_destroy() in the API description for details.
3548 static int lod_object_destroy(const struct lu_env *env,
3549 struct dt_object *dt, struct thandle *th)
3551 struct dt_object *next = dt_object_child(dt);
3552 struct lod_object *lo = lod_dt_obj(dt);
3553 struct lod_thread_info *info = lod_env_info(env);
3554 char *stripe_name = info->lti_key;
3559 /* destroy sub-stripe of master object */
3560 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3561 rc = next->do_ops->do_index_try(env, next,
3562 &dt_directory_features);
3566 for (i = 0; i < lo->ldo_stripenr; i++) {
3567 rc = lod_sub_object_ref_del(env, next, th);
3571 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3572 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
3575 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
3576 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
3577 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
3579 rc = lod_sub_object_delete(env, next,
3580 (const struct dt_key *)stripe_name, th);
3586 rc = lod_sub_object_destroy(env, next, th);
3590 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
3591 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3594 /* destroy all striped objects */
3595 for (i = 0; i < lo->ldo_stripenr; i++) {
3596 if (likely(lo->ldo_stripe[i] != NULL) &&
3597 (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
3598 i == cfs_fail_val)) {
3599 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3600 dt_write_lock(env, lo->ldo_stripe[i],
3602 rc = lod_sub_object_ref_del(env,
3603 lo->ldo_stripe[i], th);
3604 dt_write_unlock(env, lo->ldo_stripe[i]);
3609 rc = lod_sub_object_destroy(env, lo->ldo_stripe[i], th);
3619 * Implementation of dt_object_operations::do_declare_ref_add.
3621 * \see dt_object_operations::do_declare_ref_add() in the API description
3624 static int lod_declare_ref_add(const struct lu_env *env,
3625 struct dt_object *dt, struct thandle *th)
3627 return lod_sub_object_declare_ref_add(env, dt_object_child(dt), th);
3631 * Implementation of dt_object_operations::do_ref_add.
3633 * \see dt_object_operations::do_ref_add() in the API description for details.
3635 static int lod_ref_add(const struct lu_env *env,
3636 struct dt_object *dt, struct thandle *th)
3638 return lod_sub_object_ref_add(env, dt_object_child(dt), th);
3642 * Implementation of dt_object_operations::do_declare_ref_del.
3644 * \see dt_object_operations::do_declare_ref_del() in the API description
3647 static int lod_declare_ref_del(const struct lu_env *env,
3648 struct dt_object *dt, struct thandle *th)
3650 return lod_sub_object_declare_ref_del(env, dt_object_child(dt), th);
3654 * Implementation of dt_object_operations::do_ref_del
3656 * \see dt_object_operations::do_ref_del() in the API description for details.
3658 static int lod_ref_del(const struct lu_env *env,
3659 struct dt_object *dt, struct thandle *th)
3661 return lod_sub_object_ref_del(env, dt_object_child(dt), th);
3665 * Implementation of dt_object_operations::do_object_sync.
3667 * \see dt_object_operations::do_object_sync() in the API description
3670 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
3671 __u64 start, __u64 end)
3673 return dt_object_sync(env, dt_object_child(dt), start, end);
3677 * Release LDLM locks on the stripes of a striped directory.
3679 * Iterates over all the locks taken on the stripe objects and
3682 * \param[in] env execution environment
3683 * \param[in] dt striped object
3684 * \param[in] einfo lock description
3685 * \param[in] policy data describing requested lock
3687 * \retval 0 on success
3688 * \retval negative if failed
3690 static int lod_object_unlock_internal(const struct lu_env *env,
3691 struct dt_object *dt,
3692 struct ldlm_enqueue_info *einfo,
3693 union ldlm_policy_data *policy)
3695 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
3700 if (slave_locks == NULL)
3703 for (i = 1; i < slave_locks->count; i++) {
3704 if (lustre_handle_is_used(&slave_locks->handles[i]))
3705 ldlm_lock_decref_and_cancel(&slave_locks->handles[i],
3713 * Implementation of dt_object_operations::do_object_unlock.
3715 * Used to release LDLM lock(s).
3717 * \see dt_object_operations::do_object_unlock() in the API description
3720 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
3721 struct ldlm_enqueue_info *einfo,
3722 union ldlm_policy_data *policy)
3724 struct lod_object *lo = lod_dt_obj(dt);
3725 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
3726 int slave_locks_size;
3730 if (slave_locks == NULL)
3733 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
3734 LASSERT(lo->ldo_stripenr > 1);
3735 /* Note: for remote lock for single stripe dir, MDT will cancel
3736 * the lock by lockh directly */
3737 LASSERT(!dt_object_remote(dt_object_child(dt)));
3739 /* locks were unlocked in MDT layer */
3740 for (i = 1; i < slave_locks->count; i++) {
3741 LASSERT(!lustre_handle_is_used(&slave_locks->handles[i]));
3742 dt_invalidate(env, lo->ldo_stripe[i]);
3745 slave_locks_size = sizeof(*slave_locks) + slave_locks->count *
3746 sizeof(slave_locks->handles[0]);
3747 OBD_FREE(slave_locks, slave_locks_size);
3748 einfo->ei_cbdata = NULL;
3754 * Implementation of dt_object_operations::do_object_lock.
3756 * Used to get LDLM lock on the non-striped and striped objects.
3758 * \see dt_object_operations::do_object_lock() in the API description
3761 static int lod_object_lock(const struct lu_env *env,
3762 struct dt_object *dt,
3763 struct lustre_handle *lh,
3764 struct ldlm_enqueue_info *einfo,
3765 union ldlm_policy_data *policy)
3767 struct lod_object *lo = lod_dt_obj(dt);
3770 int slave_locks_size;
3771 struct lustre_handle_array *slave_locks = NULL;
3774 /* remote object lock */
3775 if (!einfo->ei_enq_slave) {
3776 LASSERT(dt_object_remote(dt));
3777 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
3781 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3782 GOTO(out, rc = -ENOTDIR);
3784 rc = lod_load_striping(env, lo);
3789 if (lo->ldo_stripenr <= 1) {
3791 * NB, ei_cbdata stores pointer to slave locks, if no locks
3792 * taken, make sure it's set to NULL, otherwise MDT will try to
3795 einfo->ei_cbdata = NULL;
3799 slave_locks_size = sizeof(*slave_locks) + lo->ldo_stripenr *
3800 sizeof(slave_locks->handles[0]);
3801 /* Freed in lod_object_unlock */
3802 OBD_ALLOC(slave_locks, slave_locks_size);
3803 if (slave_locks == NULL)
3804 GOTO(out, rc = -ENOMEM);
3805 slave_locks->count = lo->ldo_stripenr;
3807 /* striped directory lock */
3808 for (i = 1; i < lo->ldo_stripenr; i++) {
3809 struct lustre_handle lockh;
3810 struct ldlm_res_id *res_id;
3812 res_id = &lod_env_info(env)->lti_res_id;
3813 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
3815 einfo->ei_res_id = res_id;
3817 LASSERT(lo->ldo_stripe[i] != NULL);
3818 if (likely(dt_object_remote(lo->ldo_stripe[i]))) {
3819 rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh,
3822 struct ldlm_namespace *ns = einfo->ei_namespace;
3823 ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
3824 ldlm_completion_callback completion = einfo->ei_cb_cp;
3825 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
3827 if (einfo->ei_mode == LCK_PW ||
3828 einfo->ei_mode == LCK_EX)
3829 dlmflags |= LDLM_FL_COS_INCOMPAT;
3831 /* This only happens if there are mulitple stripes
3832 * on the master MDT, i.e. except stripe0, there are
3833 * other stripes on the Master MDT as well, Only
3834 * happens in the test case right now. */
3835 LASSERT(ns != NULL);
3836 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS,
3837 policy, einfo->ei_mode,
3838 &dlmflags, blocking,
3840 NULL, 0, LVB_T_NONE,
3845 slave_locks->handles[i] = lockh;
3847 einfo->ei_cbdata = slave_locks;
3849 if (rc != 0 && slave_locks != NULL) {
3850 lod_object_unlock_internal(env, dt, einfo, policy);
3851 OBD_FREE(slave_locks, slave_locks_size);
3856 einfo->ei_cbdata = NULL;
3861 * Implementation of dt_object_operations::do_invalidate.
3863 * \see dt_object_operations::do_invalidate() in the API description for details
3865 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
3867 return dt_invalidate(env, dt_object_child(dt));
3870 struct dt_object_operations lod_obj_ops = {
3871 .do_read_lock = lod_object_read_lock,
3872 .do_write_lock = lod_object_write_lock,
3873 .do_read_unlock = lod_object_read_unlock,
3874 .do_write_unlock = lod_object_write_unlock,
3875 .do_write_locked = lod_object_write_locked,
3876 .do_attr_get = lod_attr_get,
3877 .do_declare_attr_set = lod_declare_attr_set,
3878 .do_attr_set = lod_attr_set,
3879 .do_xattr_get = lod_xattr_get,
3880 .do_declare_xattr_set = lod_declare_xattr_set,
3881 .do_xattr_set = lod_xattr_set,
3882 .do_declare_xattr_del = lod_declare_xattr_del,
3883 .do_xattr_del = lod_xattr_del,
3884 .do_xattr_list = lod_xattr_list,
3885 .do_ah_init = lod_ah_init,
3886 .do_declare_create = lod_declare_object_create,
3887 .do_create = lod_object_create,
3888 .do_declare_destroy = lod_declare_object_destroy,
3889 .do_destroy = lod_object_destroy,
3890 .do_index_try = lod_index_try,
3891 .do_declare_ref_add = lod_declare_ref_add,
3892 .do_ref_add = lod_ref_add,
3893 .do_declare_ref_del = lod_declare_ref_del,
3894 .do_ref_del = lod_ref_del,
3895 .do_object_sync = lod_object_sync,
3896 .do_object_lock = lod_object_lock,
3897 .do_object_unlock = lod_object_unlock,
3898 .do_invalidate = lod_invalidate,
3902 * Implementation of dt_body_operations::dbo_read.
3904 * \see dt_body_operations::dbo_read() in the API description for details.
3906 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
3907 struct lu_buf *buf, loff_t *pos)
3909 struct dt_object *next = dt_object_child(dt);
3910 return next->do_body_ops->dbo_read(env, next, buf, pos);
3914 * Implementation of dt_body_operations::dbo_declare_write.
3916 * \see dt_body_operations::dbo_declare_write() in the API description
3919 static ssize_t lod_declare_write(const struct lu_env *env,
3920 struct dt_object *dt,
3921 const struct lu_buf *buf, loff_t pos,
3924 return lod_sub_object_declare_write(env, dt_object_child(dt), buf, pos,
3929 * Implementation of dt_body_operations::dbo_write.
3931 * \see dt_body_operations::dbo_write() in the API description for details.
3933 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
3934 const struct lu_buf *buf, loff_t *pos,
3935 struct thandle *th, int iq)
3937 return lod_sub_object_write(env, dt_object_child(dt), buf, pos, th, iq);
3940 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
3941 __u64 start, __u64 end, struct thandle *th)
3943 if (dt_object_remote(dt))
3946 return lod_sub_object_declare_punch(env, dt_object_child(dt), start,
3950 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
3951 __u64 start, __u64 end, struct thandle *th)
3953 if (dt_object_remote(dt))
3956 return lod_sub_object_punch(env, dt_object_child(dt), start, end, th);
3959 static const struct dt_body_operations lod_body_lnk_ops = {
3960 .dbo_read = lod_read,
3961 .dbo_declare_write = lod_declare_write,
3962 .dbo_write = lod_write
3965 static const struct dt_body_operations lod_body_ops = {
3966 .dbo_read = lod_read,
3967 .dbo_declare_write = lod_declare_write,
3968 .dbo_write = lod_write,
3969 .dbo_declare_punch = lod_declare_punch,
3970 .dbo_punch = lod_punch,
3974 * Implementation of lu_object_operations::loo_object_init.
3976 * The function determines the type and the index of the target device using
3977 * sequence of the object's FID. Then passes control down to the
3978 * corresponding device:
3979 * OSD for the local objects, OSP for remote
3981 * \see lu_object_operations::loo_object_init() in the API description
3984 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
3985 const struct lu_object_conf *conf)
3987 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
3988 struct lu_device *cdev = NULL;
3989 struct lu_object *cobj;
3990 struct lod_tgt_descs *ltd = NULL;
3991 struct lod_tgt_desc *tgt;
3993 int type = LU_SEQ_RANGE_ANY;
3997 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
3999 /* Note: Sometimes, it will Return EAGAIN here, see
4000 * ptrlpc_import_delay_req(), which might confuse
4001 * lu_object_find_at() and make it wait there incorrectly.
4002 * so we convert it to EIO here.*/
4009 if (type == LU_SEQ_RANGE_MDT &&
4010 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
4011 cdev = &lod->lod_child->dd_lu_dev;
4012 } else if (type == LU_SEQ_RANGE_MDT) {
4013 ltd = &lod->lod_mdt_descs;
4015 } else if (type == LU_SEQ_RANGE_OST) {
4016 ltd = &lod->lod_ost_descs;
4023 if (ltd->ltd_tgts_size > idx &&
4024 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
4025 tgt = LTD_TGT(ltd, idx);
4027 LASSERT(tgt != NULL);
4028 LASSERT(tgt->ltd_tgt != NULL);
4030 cdev = &(tgt->ltd_tgt->dd_lu_dev);
4032 lod_putref(lod, ltd);
4035 if (unlikely(cdev == NULL))
4038 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
4039 if (unlikely(cobj == NULL))
4042 lu_object_add(lo, cobj);
4049 * Release resources associated with striping.
4051 * If the object is striped (regular or directory), then release
4052 * the stripe objects references and free the ldo_stripe array.
4054 * \param[in] env execution environment
4055 * \param[in] lo object
4057 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
4061 if (lo->ldo_stripe != NULL) {
4064 LASSERT(lo->ldo_stripes_allocated > 0);
4066 for (i = 0; i < lo->ldo_stripenr; i++) {
4067 if (lo->ldo_stripe[i])
4068 lu_object_put(env, &lo->ldo_stripe[i]->do_lu);
4071 len = sizeof(struct dt_object *) * lo->ldo_stripes_allocated;
4072 OBD_FREE(lo->ldo_stripe, len);
4073 lo->ldo_stripe = NULL;
4074 lo->ldo_stripes_allocated = 0;
4076 lo->ldo_stripenr = 0;
4080 * Implementation of lu_object_operations::loo_object_start.
4082 * \see lu_object_operations::loo_object_start() in the API description
4085 static int lod_object_start(const struct lu_env *env, struct lu_object *o)
4087 if (S_ISLNK(o->lo_header->loh_attr & S_IFMT)) {
4088 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_lnk_ops;
4089 } else if (S_ISREG(o->lo_header->loh_attr & S_IFMT) ||
4090 fid_is_local_file(lu_object_fid(o))) {
4091 /* Note: some local file (like last rcvd) is created
4092 * through bottom layer (OSD), so the object initialization
4093 * comes to lod, it does not set loh_attr yet, so
4094 * set do_body_ops for local file anyway */
4095 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_ops;
4101 * Implementation of lu_object_operations::loo_object_free.
4103 * \see lu_object_operations::loo_object_free() in the API description
4106 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
4108 struct lod_object *lo = lu2lod_obj(o);
4110 lod_object_set_pool(lo, NULL);
4111 /* release all underlying object pinned */
4112 lod_object_free_striping(env, lo);
4114 OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
4118 * Implementation of lu_object_operations::loo_object_release.
4120 * \see lu_object_operations::loo_object_release() in the API description
4123 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
4125 /* XXX: shouldn't we release everything here in case if object
4126 * creation failed before? */
4130 * Implementation of lu_object_operations::loo_object_print.
4132 * \see lu_object_operations::loo_object_print() in the API description
4135 static int lod_object_print(const struct lu_env *env, void *cookie,
4136 lu_printer_t p, const struct lu_object *l)
4138 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
4140 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
4143 struct lu_object_operations lod_lu_obj_ops = {
4144 .loo_object_init = lod_object_init,
4145 .loo_object_start = lod_object_start,
4146 .loo_object_free = lod_object_free,
4147 .loo_object_release = lod_object_release,
4148 .loo_object_print = lod_object_print,