4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2014, Intel Corporation.
29 * lustre/lod/lod_object.c
31 * This file contains implementations of methods for the OSD API
32 * for the Logical Object Device (LOD) layer, which provides a virtual
33 * local OSD object interface to the MDD layer, and abstracts the
34 * addressing of local (OSD) and remote (OSP) objects. The API is
35 * described in the file lustre/include/dt_object.h and in
36 * lustre/doc/osd-api.txt.
38 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
41 #define DEBUG_SUBSYSTEM S_MDS
44 #include <obd_class.h>
45 #include <lustre_ver.h>
46 #include <obd_support.h>
47 #include <lprocfs_status.h>
49 #include <lustre_fid.h>
50 #include <lustre_param.h>
51 #include <lustre_fid.h>
52 #include <lustre_lmv.h>
53 #include <md_object.h>
54 #include <lustre_linkea.h>
56 #include "lod_internal.h"
58 static const char dot[] = ".";
59 static const char dotdot[] = "..";
61 static const struct dt_body_operations lod_body_lnk_ops;
64 * Implementation of dt_index_operations::dio_lookup
66 * Used with regular (non-striped) objects.
68 * \see dt_index_operations::dio_lookup() in the API description for details.
70 static int lod_index_lookup(const struct lu_env *env, struct dt_object *dt,
71 struct dt_rec *rec, const struct dt_key *key)
73 struct dt_object *next = dt_object_child(dt);
74 return next->do_index_ops->dio_lookup(env, next, rec, key);
78 * Implementation of dt_index_operations::dio_declare_insert.
80 * Used with regular (non-striped) objects.
82 * \see dt_index_operations::dio_declare_insert() in the API description
85 static int lod_declare_index_insert(const struct lu_env *env,
87 const struct dt_rec *rec,
88 const struct dt_key *key,
91 return lod_sub_object_declare_insert(env, dt_object_child(dt),
96 * Implementation of dt_index_operations::dio_insert.
98 * Used with regular (non-striped) objects
100 * \see dt_index_operations::dio_insert() in the API description for details.
102 static int lod_index_insert(const struct lu_env *env,
103 struct dt_object *dt,
104 const struct dt_rec *rec,
105 const struct dt_key *key,
109 return lod_sub_object_index_insert(env, dt_object_child(dt), rec, key,
114 * Implementation of dt_index_operations::dio_declare_delete.
116 * Used with regular (non-striped) objects.
118 * \see dt_index_operations::dio_declare_delete() in the API description
121 static int lod_declare_index_delete(const struct lu_env *env,
122 struct dt_object *dt,
123 const struct dt_key *key,
126 return lod_sub_object_declare_delete(env, dt_object_child(dt), key,
131 * Implementation of dt_index_operations::dio_delete.
133 * Used with regular (non-striped) objects.
135 * \see dt_index_operations::dio_delete() in the API description for details.
137 static int lod_index_delete(const struct lu_env *env,
138 struct dt_object *dt,
139 const struct dt_key *key,
142 return lod_sub_object_delete(env, dt_object_child(dt), key, th);
146 * Implementation of dt_it_ops::init.
148 * Used with regular (non-striped) objects.
150 * \see dt_it_ops::init() in the API description for details.
152 static struct dt_it *lod_it_init(const struct lu_env *env,
153 struct dt_object *dt, __u32 attr)
155 struct dt_object *next = dt_object_child(dt);
156 struct lod_it *it = &lod_env_info(env)->lti_it;
157 struct dt_it *it_next;
159 it_next = next->do_index_ops->dio_it.init(env, next, attr);
163 /* currently we do not use more than one iterator per thread
164 * so we store it in thread info. if at some point we need
165 * more active iterators in a single thread, we can allocate
167 LASSERT(it->lit_obj == NULL);
169 it->lit_it = it_next;
172 return (struct dt_it *)it;
175 #define LOD_CHECK_IT(env, it) \
177 LASSERT((it)->lit_obj != NULL); \
178 LASSERT((it)->lit_it != NULL); \
182 * Implementation of dt_index_operations::dio_it.fini.
184 * Used with regular (non-striped) objects.
186 * \see dt_index_operations::dio_it.fini() in the API description for details.
188 static void lod_it_fini(const struct lu_env *env, struct dt_it *di)
190 struct lod_it *it = (struct lod_it *)di;
192 LOD_CHECK_IT(env, it);
193 it->lit_obj->do_index_ops->dio_it.fini(env, it->lit_it);
195 /* the iterator not in use any more */
201 * Implementation of dt_it_ops::get.
203 * Used with regular (non-striped) objects.
205 * \see dt_it_ops::get() in the API description for details.
207 static int lod_it_get(const struct lu_env *env, struct dt_it *di,
208 const struct dt_key *key)
210 const struct lod_it *it = (const struct lod_it *)di;
212 LOD_CHECK_IT(env, it);
213 return it->lit_obj->do_index_ops->dio_it.get(env, it->lit_it, key);
217 * Implementation of dt_it_ops::put.
219 * Used with regular (non-striped) objects.
221 * \see dt_it_ops::put() in the API description for details.
223 static void lod_it_put(const struct lu_env *env, struct dt_it *di)
225 struct lod_it *it = (struct lod_it *)di;
227 LOD_CHECK_IT(env, it);
228 return it->lit_obj->do_index_ops->dio_it.put(env, it->lit_it);
232 * Implementation of dt_it_ops::next.
234 * Used with regular (non-striped) objects
236 * \see dt_it_ops::next() in the API description for details.
238 static int lod_it_next(const struct lu_env *env, struct dt_it *di)
240 struct lod_it *it = (struct lod_it *)di;
242 LOD_CHECK_IT(env, it);
243 return it->lit_obj->do_index_ops->dio_it.next(env, it->lit_it);
247 * Implementation of dt_it_ops::key.
249 * Used with regular (non-striped) objects.
251 * \see dt_it_ops::key() in the API description for details.
253 static struct dt_key *lod_it_key(const struct lu_env *env,
254 const struct dt_it *di)
256 const struct lod_it *it = (const struct lod_it *)di;
258 LOD_CHECK_IT(env, it);
259 return it->lit_obj->do_index_ops->dio_it.key(env, it->lit_it);
263 * Implementation of dt_it_ops::key_size.
265 * Used with regular (non-striped) objects.
267 * \see dt_it_ops::key_size() in the API description for details.
269 static int lod_it_key_size(const struct lu_env *env, const struct dt_it *di)
271 struct lod_it *it = (struct lod_it *)di;
273 LOD_CHECK_IT(env, it);
274 return it->lit_obj->do_index_ops->dio_it.key_size(env, it->lit_it);
278 * Implementation of dt_it_ops::rec.
280 * Used with regular (non-striped) objects.
282 * \see dt_it_ops::rec() in the API description for details.
284 static int lod_it_rec(const struct lu_env *env, const struct dt_it *di,
285 struct dt_rec *rec, __u32 attr)
287 const struct lod_it *it = (const struct lod_it *)di;
289 LOD_CHECK_IT(env, it);
290 return it->lit_obj->do_index_ops->dio_it.rec(env, it->lit_it, rec,
295 * Implementation of dt_it_ops::rec_size.
297 * Used with regular (non-striped) objects.
299 * \see dt_it_ops::rec_size() in the API description for details.
301 static int lod_it_rec_size(const struct lu_env *env, const struct dt_it *di,
304 const struct lod_it *it = (const struct lod_it *)di;
306 LOD_CHECK_IT(env, it);
307 return it->lit_obj->do_index_ops->dio_it.rec_size(env, it->lit_it,
312 * Implementation of dt_it_ops::store.
314 * Used with regular (non-striped) objects.
316 * \see dt_it_ops::store() in the API description for details.
318 static __u64 lod_it_store(const struct lu_env *env, const struct dt_it *di)
320 const struct lod_it *it = (const struct lod_it *)di;
322 LOD_CHECK_IT(env, it);
323 return it->lit_obj->do_index_ops->dio_it.store(env, it->lit_it);
327 * Implementation of dt_it_ops::load.
329 * Used with regular (non-striped) objects.
331 * \see dt_it_ops::load() in the API description for details.
333 static int lod_it_load(const struct lu_env *env, const struct dt_it *di,
336 const struct lod_it *it = (const struct lod_it *)di;
338 LOD_CHECK_IT(env, it);
339 return it->lit_obj->do_index_ops->dio_it.load(env, it->lit_it, hash);
343 * Implementation of dt_it_ops::key_rec.
345 * Used with regular (non-striped) objects.
347 * \see dt_it_ops::rec() in the API description for details.
349 static int lod_it_key_rec(const struct lu_env *env, const struct dt_it *di,
352 const struct lod_it *it = (const struct lod_it *)di;
354 LOD_CHECK_IT(env, it);
355 return it->lit_obj->do_index_ops->dio_it.key_rec(env, it->lit_it,
359 static struct dt_index_operations lod_index_ops = {
360 .dio_lookup = lod_index_lookup,
361 .dio_declare_insert = lod_declare_index_insert,
362 .dio_insert = lod_index_insert,
363 .dio_declare_delete = lod_declare_index_delete,
364 .dio_delete = lod_index_delete,
372 .key_size = lod_it_key_size,
374 .rec_size = lod_it_rec_size,
375 .store = lod_it_store,
377 .key_rec = lod_it_key_rec,
382 * Implementation of dt_it_ops::init.
384 * Used with striped objects. Internally just initializes the iterator
385 * on the first stripe.
387 * \see dt_it_ops::init() in the API description for details.
389 static struct dt_it *lod_striped_it_init(const struct lu_env *env,
390 struct dt_object *dt, __u32 attr)
392 struct lod_object *lo = lod_dt_obj(dt);
393 struct dt_object *next;
394 struct lod_it *it = &lod_env_info(env)->lti_it;
395 struct dt_it *it_next;
398 LASSERT(lo->ldo_stripenr > 0);
399 next = lo->ldo_stripe[0];
400 LASSERT(next != NULL);
401 LASSERT(next->do_index_ops != NULL);
403 it_next = next->do_index_ops->dio_it.init(env, next, attr);
407 /* currently we do not use more than one iterator per thread
408 * so we store it in thread info. if at some point we need
409 * more active iterators in a single thread, we can allocate
411 LASSERT(it->lit_obj == NULL);
413 it->lit_stripe_index = 0;
415 it->lit_it = it_next;
418 return (struct dt_it *)it;
421 #define LOD_CHECK_STRIPED_IT(env, it, lo) \
423 LASSERT((it)->lit_obj != NULL); \
424 LASSERT((it)->lit_it != NULL); \
425 LASSERT((lo)->ldo_stripenr > 0); \
426 LASSERT((it)->lit_stripe_index < (lo)->ldo_stripenr); \
430 * Implementation of dt_it_ops::fini.
432 * Used with striped objects.
434 * \see dt_it_ops::fini() in the API description for details.
436 static void lod_striped_it_fini(const struct lu_env *env, struct dt_it *di)
438 struct lod_it *it = (struct lod_it *)di;
439 struct lod_object *lo = lod_dt_obj(it->lit_obj);
440 struct dt_object *next;
442 /* If lit_it == NULL, then it means the sub_it has been finished,
443 * which only happens in failure cases, see lod_striped_it_next() */
444 if (it->lit_it != NULL) {
445 LOD_CHECK_STRIPED_IT(env, it, lo);
447 next = lo->ldo_stripe[it->lit_stripe_index];
448 LASSERT(next != NULL);
449 LASSERT(next->do_index_ops != NULL);
451 next->do_index_ops->dio_it.fini(env, it->lit_it);
454 /* the iterator not in use any more */
457 it->lit_stripe_index = 0;
461 * Implementation of dt_it_ops::get.
463 * Right now it's not used widely, only to reset the iterator to the
464 * initial position. It should be possible to implement a full version
465 * which chooses a correct stripe to be able to position with any key.
467 * \see dt_it_ops::get() in the API description for details.
469 static int lod_striped_it_get(const struct lu_env *env, struct dt_it *di,
470 const struct dt_key *key)
472 const struct lod_it *it = (const struct lod_it *)di;
473 struct lod_object *lo = lod_dt_obj(it->lit_obj);
474 struct dt_object *next;
477 LOD_CHECK_STRIPED_IT(env, it, lo);
479 next = lo->ldo_stripe[it->lit_stripe_index];
480 LASSERT(next != NULL);
481 LASSERT(next->do_index_ops != NULL);
483 return next->do_index_ops->dio_it.get(env, it->lit_it, key);
487 * Implementation of dt_it_ops::put.
489 * Used with striped objects.
491 * \see dt_it_ops::put() in the API description for details.
493 static void lod_striped_it_put(const struct lu_env *env, struct dt_it *di)
495 struct lod_it *it = (struct lod_it *)di;
496 struct lod_object *lo = lod_dt_obj(it->lit_obj);
497 struct dt_object *next;
499 LOD_CHECK_STRIPED_IT(env, it, lo);
501 next = lo->ldo_stripe[it->lit_stripe_index];
502 LASSERT(next != NULL);
503 LASSERT(next->do_index_ops != NULL);
505 return next->do_index_ops->dio_it.put(env, it->lit_it);
509 * Implementation of dt_it_ops::next.
511 * Used with striped objects. When the end of the current stripe is
512 * reached, the method takes the next stripe's iterator.
514 * \see dt_it_ops::next() in the API description for details.
516 static int lod_striped_it_next(const struct lu_env *env, struct dt_it *di)
518 struct lod_it *it = (struct lod_it *)di;
519 struct lod_object *lo = lod_dt_obj(it->lit_obj);
520 struct dt_object *next;
521 struct dt_it *it_next;
525 LOD_CHECK_STRIPED_IT(env, it, lo);
527 next = lo->ldo_stripe[it->lit_stripe_index];
528 LASSERT(next != NULL);
529 LASSERT(next->do_index_ops != NULL);
531 rc = next->do_index_ops->dio_it.next(env, it->lit_it);
535 if (rc == 0 && it->lit_stripe_index == 0)
538 if (rc == 0 && it->lit_stripe_index > 0) {
539 struct lu_dirent *ent;
541 ent = (struct lu_dirent *)lod_env_info(env)->lti_key;
543 rc = next->do_index_ops->dio_it.rec(env, it->lit_it,
544 (struct dt_rec *)ent,
549 /* skip . and .. for slave stripe */
550 if ((strncmp(ent->lde_name, ".",
551 le16_to_cpu(ent->lde_namelen)) == 0 &&
552 le16_to_cpu(ent->lde_namelen) == 1) ||
553 (strncmp(ent->lde_name, "..",
554 le16_to_cpu(ent->lde_namelen)) == 0 &&
555 le16_to_cpu(ent->lde_namelen) == 2))
561 /* go to next stripe */
562 if (it->lit_stripe_index + 1 >= lo->ldo_stripenr)
565 it->lit_stripe_index++;
567 next->do_index_ops->dio_it.put(env, it->lit_it);
568 next->do_index_ops->dio_it.fini(env, it->lit_it);
571 next = lo->ldo_stripe[it->lit_stripe_index];
572 LASSERT(next != NULL);
573 LASSERT(next->do_index_ops != NULL);
575 rc = next->do_ops->do_index_try(env, next, &dt_directory_features);
579 it_next = next->do_index_ops->dio_it.init(env, next, it->lit_attr);
580 if (!IS_ERR(it_next)) {
581 it->lit_it = it_next;
584 rc = PTR_ERR(it_next);
591 * Implementation of dt_it_ops::key.
593 * Used with striped objects.
595 * \see dt_it_ops::key() in the API description for details.
597 static struct dt_key *lod_striped_it_key(const struct lu_env *env,
598 const struct dt_it *di)
600 const struct lod_it *it = (const struct lod_it *)di;
601 struct lod_object *lo = lod_dt_obj(it->lit_obj);
602 struct dt_object *next;
604 LOD_CHECK_STRIPED_IT(env, it, lo);
606 next = lo->ldo_stripe[it->lit_stripe_index];
607 LASSERT(next != NULL);
608 LASSERT(next->do_index_ops != NULL);
610 return next->do_index_ops->dio_it.key(env, it->lit_it);
614 * Implementation of dt_it_ops::key_size.
616 * Used with striped objects.
618 * \see dt_it_ops::size() in the API description for details.
620 static int lod_striped_it_key_size(const struct lu_env *env,
621 const struct dt_it *di)
623 struct lod_it *it = (struct lod_it *)di;
624 struct lod_object *lo = lod_dt_obj(it->lit_obj);
625 struct dt_object *next;
627 LOD_CHECK_STRIPED_IT(env, it, lo);
629 next = lo->ldo_stripe[it->lit_stripe_index];
630 LASSERT(next != NULL);
631 LASSERT(next->do_index_ops != NULL);
633 return next->do_index_ops->dio_it.key_size(env, it->lit_it);
637 * Implementation of dt_it_ops::rec.
639 * Used with striped objects.
641 * \see dt_it_ops::rec() in the API description for details.
643 static int lod_striped_it_rec(const struct lu_env *env, const struct dt_it *di,
644 struct dt_rec *rec, __u32 attr)
646 const struct lod_it *it = (const struct lod_it *)di;
647 struct lod_object *lo = lod_dt_obj(it->lit_obj);
648 struct dt_object *next;
650 LOD_CHECK_STRIPED_IT(env, it, lo);
652 next = lo->ldo_stripe[it->lit_stripe_index];
653 LASSERT(next != NULL);
654 LASSERT(next->do_index_ops != NULL);
656 return next->do_index_ops->dio_it.rec(env, it->lit_it, rec, attr);
660 * Implementation of dt_it_ops::rec_size.
662 * Used with striped objects.
664 * \see dt_it_ops::rec_size() in the API description for details.
666 static int lod_striped_it_rec_size(const struct lu_env *env,
667 const struct dt_it *di, __u32 attr)
669 struct lod_it *it = (struct lod_it *)di;
670 struct lod_object *lo = lod_dt_obj(it->lit_obj);
671 struct dt_object *next;
673 LOD_CHECK_STRIPED_IT(env, it, lo);
675 next = lo->ldo_stripe[it->lit_stripe_index];
676 LASSERT(next != NULL);
677 LASSERT(next->do_index_ops != NULL);
679 return next->do_index_ops->dio_it.rec_size(env, it->lit_it, attr);
683 * Implementation of dt_it_ops::store.
685 * Used with striped objects.
687 * \see dt_it_ops::store() in the API description for details.
689 static __u64 lod_striped_it_store(const struct lu_env *env,
690 const struct dt_it *di)
692 const struct lod_it *it = (const struct lod_it *)di;
693 struct lod_object *lo = lod_dt_obj(it->lit_obj);
694 struct dt_object *next;
696 LOD_CHECK_STRIPED_IT(env, it, lo);
698 next = lo->ldo_stripe[it->lit_stripe_index];
699 LASSERT(next != NULL);
700 LASSERT(next->do_index_ops != NULL);
702 return next->do_index_ops->dio_it.store(env, it->lit_it);
706 * Implementation of dt_it_ops::load.
708 * Used with striped objects.
710 * \see dt_it_ops::load() in the API description for details.
712 static int lod_striped_it_load(const struct lu_env *env,
713 const struct dt_it *di, __u64 hash)
715 const struct lod_it *it = (const struct lod_it *)di;
716 struct lod_object *lo = lod_dt_obj(it->lit_obj);
717 struct dt_object *next;
719 LOD_CHECK_STRIPED_IT(env, it, lo);
721 next = lo->ldo_stripe[it->lit_stripe_index];
722 LASSERT(next != NULL);
723 LASSERT(next->do_index_ops != NULL);
725 return next->do_index_ops->dio_it.load(env, it->lit_it, hash);
728 static struct dt_index_operations lod_striped_index_ops = {
729 .dio_lookup = lod_index_lookup,
730 .dio_declare_insert = lod_declare_index_insert,
731 .dio_insert = lod_index_insert,
732 .dio_declare_delete = lod_declare_index_delete,
733 .dio_delete = lod_index_delete,
735 .init = lod_striped_it_init,
736 .fini = lod_striped_it_fini,
737 .get = lod_striped_it_get,
738 .put = lod_striped_it_put,
739 .next = lod_striped_it_next,
740 .key = lod_striped_it_key,
741 .key_size = lod_striped_it_key_size,
742 .rec = lod_striped_it_rec,
743 .rec_size = lod_striped_it_rec_size,
744 .store = lod_striped_it_store,
745 .load = lod_striped_it_load,
750 * Append the FID for each shard of the striped directory after the
751 * given LMV EA header.
753 * To simplify striped directory and the consistency verification,
754 * we only store the LMV EA header on disk, for both master object
755 * and slave objects. When someone wants to know the whole LMV EA,
756 * such as client readdir(), we can build the entrie LMV EA on the
757 * MDT side (in RAM) via iterating the sub-directory entries that
758 * are contained in the master object of the stripe directory.
760 * For the master object of the striped directroy, the valid name
761 * for each shard is composed of the ${shard_FID}:${shard_idx}.
763 * There may be holes in the LMV EA if some shards' name entries
764 * are corrupted or lost.
766 * \param[in] env pointer to the thread context
767 * \param[in] lo pointer to the master object of the striped directory
768 * \param[in] buf pointer to the lu_buf which will hold the LMV EA
769 * \param[in] resize whether re-allocate the buffer if it is not big enough
771 * \retval positive size of the LMV EA
772 * \retval 0 for nothing to be loaded
773 * \retval negative error number on failure
775 int lod_load_lmv_shards(const struct lu_env *env, struct lod_object *lo,
776 struct lu_buf *buf, bool resize)
778 struct lu_dirent *ent =
779 (struct lu_dirent *)lod_env_info(env)->lti_key;
780 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
781 struct dt_object *obj = dt_object_child(&lo->ldo_obj);
782 struct lmv_mds_md_v1 *lmv1 = buf->lb_buf;
784 const struct dt_it_ops *iops;
786 __u32 magic = le32_to_cpu(lmv1->lmv_magic);
791 /* If it is not a striped directory, then load nothing. */
792 if (magic != LMV_MAGIC_V1)
795 /* If it is in migration (or failure), then load nothing. */
796 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
799 stripes = le32_to_cpu(lmv1->lmv_stripe_count);
803 rc = lmv_mds_md_size(stripes, magic);
807 if (buf->lb_len < lmv1_size) {
816 lu_buf_alloc(buf, lmv1_size);
821 memcpy(buf->lb_buf, tbuf.lb_buf, tbuf.lb_len);
824 if (unlikely(!dt_try_as_dir(env, obj)))
827 memset(&lmv1->lmv_stripe_fids[0], 0, stripes * sizeof(struct lu_fid));
828 iops = &obj->do_index_ops->dio_it;
829 it = iops->init(env, obj, LUDA_64BITHASH);
833 rc = iops->load(env, it, 0);
835 rc = iops->next(env, it);
840 char name[FID_LEN + 2] = "";
845 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
851 fid_le_to_cpu(&fid, &ent->lde_fid);
852 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
853 if (ent->lde_name[0] == '.') {
854 if (ent->lde_namelen == 1)
857 if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
861 len = snprintf(name, sizeof(name),
862 DFID":", PFID(&ent->lde_fid));
863 /* The ent->lde_name is composed of ${FID}:${index} */
864 if (ent->lde_namelen < len + 1 ||
865 memcmp(ent->lde_name, name, len) != 0) {
866 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
867 "%s: invalid shard name %.*s with the FID "DFID
868 " for the striped directory "DFID", %s\n",
869 lod2obd(lod)->obd_name, ent->lde_namelen,
870 ent->lde_name, PFID(&fid),
871 PFID(lu_object_fid(&obj->do_lu)),
872 lod->lod_lmv_failout ? "failout" : "skip");
874 if (lod->lod_lmv_failout)
882 if (ent->lde_name[len] < '0' ||
883 ent->lde_name[len] > '9') {
884 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
885 "%s: invalid shard name %.*s with the "
886 "FID "DFID" for the striped directory "
888 lod2obd(lod)->obd_name, ent->lde_namelen,
889 ent->lde_name, PFID(&fid),
890 PFID(lu_object_fid(&obj->do_lu)),
891 lod->lod_lmv_failout ?
894 if (lod->lod_lmv_failout)
900 index = index * 10 + ent->lde_name[len++] - '0';
901 } while (len < ent->lde_namelen);
903 if (len == ent->lde_namelen) {
904 /* Out of LMV EA range. */
905 if (index >= stripes) {
906 CERROR("%s: the shard %.*s for the striped "
907 "directory "DFID" is out of the known "
908 "LMV EA range [0 - %u], failout\n",
909 lod2obd(lod)->obd_name, ent->lde_namelen,
911 PFID(lu_object_fid(&obj->do_lu)),
917 /* The slot has been occupied. */
918 if (!fid_is_zero(&lmv1->lmv_stripe_fids[index])) {
922 &lmv1->lmv_stripe_fids[index]);
923 CERROR("%s: both the shard "DFID" and "DFID
924 " for the striped directory "DFID
925 " claim the same LMV EA slot at the "
926 "index %d, failout\n",
927 lod2obd(lod)->obd_name,
928 PFID(&fid0), PFID(&fid),
929 PFID(lu_object_fid(&obj->do_lu)), index);
934 /* stored as LE mode */
935 lmv1->lmv_stripe_fids[index] = ent->lde_fid;
938 rc = iops->next(env, it);
945 RETURN(rc > 0 ? lmv_mds_md_size(stripes, magic) : rc);
949 * Implementation of dt_object_operations::do_index_try.
951 * \see dt_object_operations::do_index_try() in the API description for details.
953 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
954 const struct dt_index_features *feat)
956 struct lod_object *lo = lod_dt_obj(dt);
957 struct dt_object *next = dt_object_child(dt);
961 LASSERT(next->do_ops);
962 LASSERT(next->do_ops->do_index_try);
964 rc = lod_load_striping_locked(env, lo);
968 rc = next->do_ops->do_index_try(env, next, feat);
972 if (lo->ldo_stripenr > 0) {
975 for (i = 0; i < lo->ldo_stripenr; i++) {
976 if (dt_object_exists(lo->ldo_stripe[i]) == 0)
978 rc = lo->ldo_stripe[i]->do_ops->do_index_try(env,
979 lo->ldo_stripe[i], feat);
983 dt->do_index_ops = &lod_striped_index_ops;
985 dt->do_index_ops = &lod_index_ops;
992 * Implementation of dt_object_operations::do_read_lock.
994 * \see dt_object_operations::do_read_lock() in the API description for details.
996 static void lod_object_read_lock(const struct lu_env *env,
997 struct dt_object *dt, unsigned role)
999 dt_read_lock(env, dt_object_child(dt), role);
1003 * Implementation of dt_object_operations::do_write_lock.
1005 * \see dt_object_operations::do_write_lock() in the API description for
1008 static void lod_object_write_lock(const struct lu_env *env,
1009 struct dt_object *dt, unsigned role)
1011 dt_write_lock(env, dt_object_child(dt), role);
1015 * Implementation of dt_object_operations::do_read_unlock.
1017 * \see dt_object_operations::do_read_unlock() in the API description for
1020 static void lod_object_read_unlock(const struct lu_env *env,
1021 struct dt_object *dt)
1023 dt_read_unlock(env, dt_object_child(dt));
1027 * Implementation of dt_object_operations::do_write_unlock.
1029 * \see dt_object_operations::do_write_unlock() in the API description for
1032 static void lod_object_write_unlock(const struct lu_env *env,
1033 struct dt_object *dt)
1035 dt_write_unlock(env, dt_object_child(dt));
1039 * Implementation of dt_object_operations::do_write_locked.
1041 * \see dt_object_operations::do_write_locked() in the API description for
1044 static int lod_object_write_locked(const struct lu_env *env,
1045 struct dt_object *dt)
1047 return dt_write_locked(env, dt_object_child(dt));
1051 * Implementation of dt_object_operations::do_attr_get.
1053 * \see dt_object_operations::do_attr_get() in the API description for details.
1055 static int lod_attr_get(const struct lu_env *env,
1056 struct dt_object *dt,
1057 struct lu_attr *attr)
1059 /* Note: for striped directory, client will merge attributes
1060 * from all of the sub-stripes see lmv_merge_attr(), and there
1061 * no MDD logic depend on directory nlink/size/time, so we can
1062 * always use master inode nlink and size for now. */
1063 return dt_attr_get(env, dt_object_child(dt), attr);
1067 * Mark all of the striped directory sub-stripes dead.
1069 * When a striped object is a subject to removal, we have
1070 * to mark all the stripes to prevent further access to
1071 * them (e.g. create a new file in those). So we mark
1072 * all the stripes with LMV_HASH_FLAG_DEAD. The function
1073 * can be used to declare the changes and to apply them.
1074 * If the object isn't striped, then just return success.
1076 * \param[in] env execution environment
1077 * \param[in] dt the striped object
1078 * \param[in] handle transaction handle
1079 * \param[in] declare whether to declare the change or apply
1081 * \retval 0 on success
1082 * \retval negative if failed
1084 static int lod_mark_dead_object(const struct lu_env *env,
1085 struct dt_object *dt,
1089 struct lod_object *lo = lod_dt_obj(dt);
1090 struct lmv_mds_md_v1 *lmv;
1091 __u32 dead_hash_type;
1097 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
1100 rc = lod_load_striping_locked(env, lo);
1104 if (lo->ldo_stripenr == 0)
1107 rc = lod_get_lmv_ea(env, lo);
1111 lmv = lod_env_info(env)->lti_ea_store;
1112 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1113 dead_hash_type = le32_to_cpu(lmv->lmv_hash_type) | LMV_HASH_FLAG_DEAD;
1114 lmv->lmv_hash_type = cpu_to_le32(dead_hash_type);
1115 for (i = 0; i < lo->ldo_stripenr; i++) {
1118 lmv->lmv_master_mdt_index = i;
1120 buf.lb_len = sizeof(*lmv);
1122 rc = lod_sub_object_declare_xattr_set(env,
1123 lo->ldo_stripe[i], &buf,
1125 LU_XATTR_REPLACE, th);
1127 rc = lod_sub_object_xattr_set(env, lo->ldo_stripe[i],
1128 &buf, XATTR_NAME_LMV,
1129 LU_XATTR_REPLACE, th);
1139 * Implementation of dt_object_operations::do_declare_attr_set.
1141 * If the object is striped, then apply the changes to all the stripes.
1143 * \see dt_object_operations::do_declare_attr_set() in the API description
1146 static int lod_declare_attr_set(const struct lu_env *env,
1147 struct dt_object *dt,
1148 const struct lu_attr *attr,
1151 struct dt_object *next = dt_object_child(dt);
1152 struct lod_object *lo = lod_dt_obj(dt);
1156 /* Set dead object on all other stripes */
1157 if (attr->la_valid & LA_FLAGS && !(attr->la_valid & ~LA_FLAGS) &&
1158 attr->la_flags & LUSTRE_SLAVE_DEAD_FL) {
1159 rc = lod_mark_dead_object(env, dt, th, true);
1164 * declare setattr on the local object
1166 rc = lod_sub_object_declare_attr_set(env, next, attr, th);
1170 /* osp_declare_attr_set() ignores all attributes other than
1171 * UID, GID, and size, and osp_attr_set() ignores all but UID
1172 * and GID. Declaration of size attr setting happens through
1173 * lod_declare_init_size(), and not through this function.
1174 * Therefore we need not load striping unless ownership is
1175 * changing. This should save memory and (we hope) speed up
1177 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1178 if (!(attr->la_valid & (LA_UID | LA_GID)))
1181 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1184 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE |
1185 LA_ATIME | LA_MTIME | LA_CTIME |
1190 * load striping information, notice we don't do this when object
1191 * is being initialized as we don't need this information till
1192 * few specific cases like destroy, chown
1194 rc = lod_load_striping(env, lo);
1198 if (lo->ldo_stripenr == 0)
1202 * if object is striped declare changes on the stripes
1204 LASSERT(lo->ldo_stripe);
1205 for (i = 0; i < lo->ldo_stripenr; i++) {
1206 if (lo->ldo_stripe[i] == NULL)
1208 rc = lod_sub_object_declare_attr_set(env,
1209 lo->ldo_stripe[i], attr,
1215 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE) &&
1216 dt_object_exists(next) != 0 &&
1217 dt_object_remote(next) == 0)
1218 lod_sub_object_declare_xattr_del(env, next,
1219 XATTR_NAME_LOV, th);
1221 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) &&
1222 dt_object_exists(next) &&
1223 dt_object_remote(next) == 0 && S_ISREG(attr->la_mode)) {
1224 struct lod_thread_info *info = lod_env_info(env);
1225 struct lu_buf *buf = &info->lti_buf;
1227 buf->lb_buf = info->lti_ea_store;
1228 buf->lb_len = info->lti_ea_store_size;
1229 lod_sub_object_declare_xattr_set(env, next, buf,
1231 LU_XATTR_REPLACE, th);
1238 * Implementation of dt_object_operations::do_attr_set.
1240 * If the object is striped, then apply the changes to all or subset of
1241 * the stripes depending on the object type and specific attributes.
1243 * \see dt_object_operations::do_attr_set() in the API description for details.
1245 static int lod_attr_set(const struct lu_env *env,
1246 struct dt_object *dt,
1247 const struct lu_attr *attr,
1250 struct dt_object *next = dt_object_child(dt);
1251 struct lod_object *lo = lod_dt_obj(dt);
1255 /* Set dead object on all other stripes */
1256 if (attr->la_valid & LA_FLAGS && !(attr->la_valid & ~LA_FLAGS) &&
1257 attr->la_flags & LUSTRE_SLAVE_DEAD_FL) {
1258 rc = lod_mark_dead_object(env, dt, th, false);
1263 * apply changes to the local object
1265 rc = lod_sub_object_attr_set(env, next, attr, th);
1269 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1270 if (!(attr->la_valid & (LA_UID | LA_GID)))
1273 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1276 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE |
1277 LA_ATIME | LA_MTIME | LA_CTIME |
1282 if (lo->ldo_stripenr == 0)
1286 * if object is striped, apply changes to all the stripes
1288 LASSERT(lo->ldo_stripe);
1289 for (i = 0; i < lo->ldo_stripenr; i++) {
1290 if (unlikely(lo->ldo_stripe[i] == NULL))
1293 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
1294 (dt_object_exists(lo->ldo_stripe[i]) == 0))
1297 rc = lod_sub_object_attr_set(env, lo->ldo_stripe[i], attr, th);
1302 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE) &&
1303 dt_object_exists(next) != 0 &&
1304 dt_object_remote(next) == 0)
1305 rc = lod_sub_object_xattr_del(env, next, XATTR_NAME_LOV, th);
1307 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) &&
1308 dt_object_exists(next) &&
1309 dt_object_remote(next) == 0 && S_ISREG(attr->la_mode)) {
1310 struct lod_thread_info *info = lod_env_info(env);
1311 struct lu_buf *buf = &info->lti_buf;
1312 struct ost_id *oi = &info->lti_ostid;
1313 struct lu_fid *fid = &info->lti_fid;
1314 struct lov_mds_md_v1 *lmm;
1315 struct lov_ost_data_v1 *objs;
1319 rc1 = lod_get_lov_ea(env, lo);
1323 buf->lb_buf = info->lti_ea_store;
1324 buf->lb_len = info->lti_ea_store_size;
1325 lmm = info->lti_ea_store;
1326 magic = le32_to_cpu(lmm->lmm_magic);
1327 if (magic == LOV_MAGIC_V1)
1328 objs = &(lmm->lmm_objects[0]);
1330 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1331 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1332 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1334 fid_to_ostid(fid, oi);
1335 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1337 rc = lod_sub_object_xattr_set(env, next, buf, XATTR_NAME_LOV,
1338 LU_XATTR_REPLACE, th);
1345 * Implementation of dt_object_operations::do_xattr_get.
1347 * If LOV EA is requested from the root object and it's not
1348 * found, then return default striping for the filesystem.
1350 * \see dt_object_operations::do_xattr_get() in the API description for details.
1352 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1353 struct lu_buf *buf, const char *name)
1355 struct lod_thread_info *info = lod_env_info(env);
1356 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1360 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1361 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1362 struct lmv_mds_md_v1 *lmv1;
1365 if (rc > (typeof(rc))sizeof(*lmv1))
1368 if (rc < (typeof(rc))sizeof(*lmv1))
1369 RETURN(rc = rc > 0 ? -EINVAL : rc);
1371 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1372 CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1374 info->lti_buf.lb_buf = info->lti_key;
1375 info->lti_buf.lb_len = sizeof(*lmv1);
1376 rc = dt_xattr_get(env, dt_object_child(dt),
1377 &info->lti_buf, name);
1378 if (unlikely(rc != sizeof(*lmv1)))
1379 RETURN(rc = rc > 0 ? -EINVAL : rc);
1381 lmv1 = info->lti_buf.lb_buf;
1382 /* The on-disk LMV EA only contains header, but the
1383 * returned LMV EA size should contain the space for
1384 * the FIDs of all shards of the striped directory. */
1385 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1386 rc = lmv_mds_md_size(
1387 le32_to_cpu(lmv1->lmv_stripe_count),
1390 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1394 RETURN(rc = rc1 != 0 ? rc1 : rc);
1397 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1401 * lod returns default striping on the real root of the device
1402 * this is like the root stores default striping for the whole
1403 * filesystem. historically we've been using a different approach
1404 * and store it in the config.
1406 dt_root_get(env, dev->lod_child, &info->lti_fid);
1407 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1409 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1410 struct lov_user_md *lum = buf->lb_buf;
1411 struct lov_desc *desc = &dev->lod_desc;
1413 if (buf->lb_buf == NULL) {
1415 } else if (buf->lb_len >= sizeof(*lum)) {
1416 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1417 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1418 lmm_oi_set_id(&lum->lmm_oi, 0);
1419 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1420 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1421 lum->lmm_stripe_size = cpu_to_le32(
1422 desc->ld_default_stripe_size);
1423 lum->lmm_stripe_count = cpu_to_le16(
1424 desc->ld_default_stripe_count);
1425 lum->lmm_stripe_offset = cpu_to_le16(
1426 desc->ld_default_stripe_offset);
1439 * Checks that the magic of the stripe is sane.
1441 * \param[in] lod lod device
1442 * \param[in] lum a buffer storing LMV EA to verify
1444 * \retval 0 if the EA is sane
1445 * \retval negative otherwise
1447 static int lod_verify_md_striping(struct lod_device *lod,
1448 const struct lmv_user_md_v1 *lum)
1450 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1451 CERROR("%s: invalid lmv_user_md: magic = %x, "
1452 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1453 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1454 (int)le32_to_cpu(lum->lum_stripe_offset),
1455 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1463 * Initialize LMV EA for a slave.
1465 * Initialize slave's LMV EA from the master's LMV EA.
1467 * \param[in] master_lmv a buffer containing master's EA
1468 * \param[out] slave_lmv a buffer where slave's EA will be stored
1471 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1472 const struct lmv_mds_md_v1 *master_lmv)
1474 *slave_lmv = *master_lmv;
1475 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1481 * Generate LMV EA from the object passed as \a dt. The object must have
1482 * the stripes created and initialized.
1484 * \param[in] env execution environment
1485 * \param[in] dt object
1486 * \param[out] lmv_buf buffer storing generated LMV EA
1488 * \retval 0 on success
1489 * \retval negative if failed
1491 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1492 struct lu_buf *lmv_buf)
1494 struct lod_thread_info *info = lod_env_info(env);
1495 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1496 struct lod_object *lo = lod_dt_obj(dt);
1497 struct lmv_mds_md_v1 *lmm1;
1499 int type = LU_SEQ_RANGE_ANY;
1504 LASSERT(lo->ldo_dir_striped != 0);
1505 LASSERT(lo->ldo_stripenr > 0);
1506 stripe_count = lo->ldo_stripenr;
1507 /* Only store the LMV EA heahder on the disk. */
1508 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1509 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1513 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1516 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1517 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1518 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1519 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1520 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1525 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1526 lmv_buf->lb_buf = info->lti_ea_store;
1527 lmv_buf->lb_len = sizeof(*lmm1);
1533 * Create in-core represenation for a striped directory.
1535 * Parse the buffer containing LMV EA and instantiate LU objects
1536 * representing the stripe objects. The pointers to the objects are
1537 * stored in ldo_stripe field of \a lo. This function is used when
1538 * we need to access an already created object (i.e. load from a disk).
1540 * \param[in] env execution environment
1541 * \param[in] lo lod object
1542 * \param[in] buf buffer containing LMV EA
1544 * \retval 0 on success
1545 * \retval negative if failed
1547 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1548 const struct lu_buf *buf)
1550 struct lod_thread_info *info = lod_env_info(env);
1551 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1552 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1553 struct dt_object **stripe;
1554 union lmv_mds_md *lmm = buf->lb_buf;
1555 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1556 struct lu_fid *fid = &info->lti_fid;
1561 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1564 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1565 lo->ldo_dir_slave_stripe = 1;
1569 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1572 if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1575 LASSERT(lo->ldo_stripe == NULL);
1576 OBD_ALLOC(stripe, sizeof(stripe[0]) *
1577 (le32_to_cpu(lmv1->lmv_stripe_count)));
1581 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1582 struct dt_device *tgt_dt;
1583 struct dt_object *dto;
1584 int type = LU_SEQ_RANGE_ANY;
1587 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1588 if (!fid_is_sane(fid))
1589 GOTO(out, rc = -ESTALE);
1591 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1595 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1596 tgt_dt = lod->lod_child;
1598 struct lod_tgt_desc *tgt;
1600 tgt = LTD_TGT(ltd, idx);
1602 GOTO(out, rc = -ESTALE);
1603 tgt_dt = tgt->ltd_tgt;
1606 dto = dt_locate_at(env, tgt_dt, fid,
1607 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1610 GOTO(out, rc = PTR_ERR(dto));
1615 lo->ldo_stripe = stripe;
1616 lo->ldo_stripenr = le32_to_cpu(lmv1->lmv_stripe_count);
1617 lo->ldo_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1619 lod_object_free_striping(env, lo);
1625 * Create a striped directory.
1627 * Create a striped directory with a given stripe pattern on the specified MDTs.
1628 * A striped directory is represented as a regular directory - an index listing
1629 * all the stripes. The stripes point back to the master object with ".." and
1630 * LinkEA. The master object gets LMV EA which identifies it as a striped
1631 * directory. The function allocates FIDs for all the stripes.
1633 * \param[in] env execution environment
1634 * \param[in] dt object
1635 * \param[in] attr attributes to initialize the objects with
1636 * \param[in] lum a pattern specifying the number of stripes and
1638 * \param[in] dof type of objects to be created
1639 * \param[in] th transaction handle
1641 * \retval 0 on success
1642 * \retval negative if failed
1644 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1645 struct dt_object *dt,
1646 struct lu_attr *attr,
1647 struct dt_object_format *dof,
1650 struct lod_thread_info *info = lod_env_info(env);
1651 struct lu_buf lmv_buf;
1652 struct lu_buf slave_lmv_buf;
1653 struct lmv_mds_md_v1 *lmm;
1654 struct lmv_mds_md_v1 *slave_lmm = NULL;
1655 struct dt_insert_rec *rec = &info->lti_dt_rec;
1656 struct lod_object *lo = lod_dt_obj(dt);
1661 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1664 lmm = lmv_buf.lb_buf;
1666 OBD_ALLOC_PTR(slave_lmm);
1667 if (slave_lmm == NULL)
1668 GOTO(out, rc = -ENOMEM);
1670 lod_prep_slave_lmv_md(slave_lmm, lmm);
1671 slave_lmv_buf.lb_buf = slave_lmm;
1672 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1674 if (!dt_try_as_dir(env, dt_object_child(dt)))
1675 GOTO(out, rc = -EINVAL);
1677 rec->rec_type = S_IFDIR;
1678 for (i = 0; i < lo->ldo_stripenr; i++) {
1679 struct dt_object *dto = lo->ldo_stripe[i];
1680 char *stripe_name = info->lti_key;
1681 struct lu_name *sname;
1682 struct linkea_data ldata = { NULL };
1683 struct lu_buf linkea_buf;
1685 rc = lod_sub_object_declare_create(env, dto, attr, NULL,
1690 if (!dt_try_as_dir(env, dto))
1691 GOTO(out, rc = -EINVAL);
1693 rc = lod_sub_object_declare_ref_add(env, dto, th);
1697 rec->rec_fid = lu_object_fid(&dto->do_lu);
1698 rc = lod_sub_object_declare_insert(env, dto,
1699 (const struct dt_rec *)rec,
1700 (const struct dt_key *)dot, th);
1704 /* master stripe FID will be put to .. */
1705 rec->rec_fid = lu_object_fid(&dt->do_lu);
1706 rc = lod_sub_object_declare_insert(env, dto,
1707 (const struct dt_rec *)rec,
1708 (const struct dt_key *)dotdot,
1713 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1714 cfs_fail_val != i) {
1715 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1717 slave_lmm->lmv_master_mdt_index =
1720 slave_lmm->lmv_master_mdt_index =
1722 rc = lod_sub_object_declare_xattr_set(env, dto,
1723 &slave_lmv_buf, XATTR_NAME_LMV, 0, th);
1728 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1730 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1731 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1733 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1734 PFID(lu_object_fid(&dto->do_lu)), i);
1736 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1737 rc = linkea_data_new(&ldata, &info->lti_linkea_buf);
1741 rc = linkea_add_buf(&ldata, sname, lu_object_fid(&dt->do_lu));
1745 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1746 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1747 rc = lod_sub_object_declare_xattr_set(env, dto, &linkea_buf,
1748 XATTR_NAME_LINK, 0, th);
1752 rec->rec_fid = lu_object_fid(&dto->do_lu);
1753 rc = lod_sub_object_declare_insert(env, dt_object_child(dt),
1754 (const struct dt_rec *)rec,
1755 (const struct dt_key *)stripe_name,
1760 rc = lod_sub_object_declare_ref_add(env, dt_object_child(dt),
1766 rc = lod_sub_object_declare_xattr_set(env, dt_object_child(dt),
1767 &lmv_buf, XATTR_NAME_LMV, 0, th);
1771 if (slave_lmm != NULL)
1772 OBD_FREE_PTR(slave_lmm);
1777 static int lod_prep_md_striped_create(const struct lu_env *env,
1778 struct dt_object *dt,
1779 struct lu_attr *attr,
1780 const struct lmv_user_md_v1 *lum,
1781 struct dt_object_format *dof,
1784 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1785 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1786 struct lod_object *lo = lod_dt_obj(dt);
1787 struct dt_object **stripe;
1795 /* The lum has been verifed in lod_verify_md_striping */
1796 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
1797 LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1799 stripe_count = le32_to_cpu(lum->lum_stripe_count);
1801 /* shrink the stripe_count to the avaible MDT count */
1802 if (stripe_count > lod->lod_remote_mdt_count + 1)
1803 stripe_count = lod->lod_remote_mdt_count + 1;
1805 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1809 OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1810 if (idx_array == NULL)
1811 GOTO(out_free, rc = -ENOMEM);
1813 for (i = 0; i < stripe_count; i++) {
1814 struct lod_tgt_desc *tgt = NULL;
1815 struct dt_object *dto;
1816 struct lu_fid fid = { 0 };
1818 struct lu_object_conf conf = { 0 };
1819 struct dt_device *tgt_dt = NULL;
1822 /* Right now, master stripe and master object are
1823 * on the same MDT */
1824 idx = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1825 rc = obd_fid_alloc(env, lod->lod_child_exp, &fid,
1829 tgt_dt = lod->lod_child;
1833 idx = (idx_array[i - 1] + 1) % (lod->lod_remote_mdt_count + 1);
1835 for (j = 0; j < lod->lod_remote_mdt_count;
1836 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1837 bool already_allocated = false;
1840 CDEBUG(D_INFO, "try idx %d, mdt cnt %u,"
1841 " allocated %u, last allocated %d\n", idx,
1842 lod->lod_remote_mdt_count, i, idx_array[i - 1]);
1844 /* Find next available target */
1845 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx))
1848 /* check whether the idx already exists
1849 * in current allocated array */
1850 for (k = 0; k < i; k++) {
1851 if (idx_array[k] == idx) {
1852 already_allocated = true;
1857 if (already_allocated)
1860 /* check the status of the OSP */
1861 tgt = LTD_TGT(ltd, idx);
1865 tgt_dt = tgt->ltd_tgt;
1866 rc = dt_statfs(env, tgt_dt, NULL);
1868 /* this OSP doesn't feel well */
1873 rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1882 /* Can not allocate more stripes */
1883 if (j == lod->lod_remote_mdt_count) {
1884 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1885 lod2obd(lod)->obd_name, stripe_count, i - 1);
1889 CDEBUG(D_INFO, "idx %d, mdt cnt %u,"
1890 " allocated %u, last allocated %d\n", idx,
1891 lod->lod_remote_mdt_count, i, idx_array[i - 1]);
1894 /* tgt_dt and fid must be ready after search avaible OSP
1895 * in the above loop */
1896 LASSERT(tgt_dt != NULL);
1897 LASSERT(fid_is_sane(&fid));
1898 conf.loc_flags = LOC_F_NEW;
1899 dto = dt_locate_at(env, tgt_dt, &fid,
1900 dt->do_lu.lo_dev->ld_site->ls_top_dev,
1903 GOTO(out_put, rc = PTR_ERR(dto));
1908 lo->ldo_dir_striped = 1;
1909 lo->ldo_stripe = stripe;
1910 lo->ldo_stripenr = i;
1911 lo->ldo_stripes_allocated = stripe_count;
1913 if (lo->ldo_stripenr == 0)
1914 GOTO(out_put, rc = -ENOSPC);
1916 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
1922 for (i = 0; i < stripe_count; i++)
1923 if (stripe[i] != NULL)
1924 lu_object_put(env, &stripe[i]->do_lu);
1925 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
1926 lo->ldo_stripenr = 0;
1927 lo->ldo_stripes_allocated = 0;
1928 lo->ldo_stripe = NULL;
1932 if (idx_array != NULL)
1933 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
1939 * Declare create striped md object.
1941 * The function declares intention to create a striped directory. This is a
1942 * wrapper for lod_prep_md_striped_create(). The only additional functionality
1943 * is to verify pattern \a lum_buf is good. Check that function for the details.
1945 * \param[in] env execution environment
1946 * \param[in] dt object
1947 * \param[in] attr attributes to initialize the objects with
1948 * \param[in] lum_buf a pattern specifying the number of stripes and
1950 * \param[in] dof type of objects to be created
1951 * \param[in] th transaction handle
1953 * \retval 0 on success
1954 * \retval negative if failed
1957 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
1958 struct dt_object *dt,
1959 struct lu_attr *attr,
1960 const struct lu_buf *lum_buf,
1961 struct dt_object_format *dof,
1964 struct lod_object *lo = lod_dt_obj(dt);
1965 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1966 struct lmv_user_md_v1 *lum;
1970 lum = lum_buf->lb_buf;
1971 LASSERT(lum != NULL);
1973 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
1974 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
1975 (int)le32_to_cpu(lum->lum_stripe_offset));
1977 if (le32_to_cpu(lum->lum_stripe_count) == 0)
1980 rc = lod_verify_md_striping(lod, lum);
1984 /* prepare dir striped objects */
1985 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
1987 /* failed to create striping, let's reset
1988 * config so that others don't get confused */
1989 lod_object_free_striping(env, lo);
1998 * Implementation of dt_object_operations::do_declare_xattr_set.
2000 * Used with regular (non-striped) objects. Basically it
2001 * initializes the striping information and applies the
2002 * change to all the stripes.
2004 * \see dt_object_operations::do_declare_xattr_set() in the API description
2007 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2008 struct dt_object *dt,
2009 const struct lu_buf *buf,
2010 const char *name, int fl,
2013 struct dt_object *next = dt_object_child(dt);
2014 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2015 struct lod_object *lo = lod_dt_obj(dt);
2020 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2021 struct lmv_user_md_v1 *lum;
2023 LASSERT(buf != NULL && buf->lb_buf != NULL);
2025 rc = lod_verify_md_striping(d, lum);
2030 rc = lod_sub_object_declare_xattr_set(env, next, buf, name, fl, th);
2034 /* set xattr to each stripes, if needed */
2035 rc = lod_load_striping(env, lo);
2039 /* Note: Do not set LinkEA on sub-stripes, otherwise
2040 * it will confuse the fid2path process(see mdt_path_current()).
2041 * The linkEA between master and sub-stripes is set in
2042 * lod_xattr_set_lmv(). */
2043 if (lo->ldo_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2046 for (i = 0; i < lo->ldo_stripenr; i++) {
2047 LASSERT(lo->ldo_stripe[i]);
2049 rc = lod_sub_object_declare_xattr_set(env, lo->ldo_stripe[i],
2059 * Implementation of dt_object_operations::do_declare_xattr_set.
2061 * \see dt_object_operations::do_declare_xattr_set() in the API description
2064 * the extension to the API:
2065 * - declaring LOVEA requests striping creation
2066 * - LU_XATTR_REPLACE means layout swap
2068 static int lod_declare_xattr_set(const struct lu_env *env,
2069 struct dt_object *dt,
2070 const struct lu_buf *buf,
2071 const char *name, int fl,
2074 struct dt_object *next = dt_object_child(dt);
2075 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2081 * allow to declare predefined striping on a new (!mode) object
2082 * which is supposed to be replay of regular file creation
2083 * (when LOV setting is declared)
2084 * LU_XATTR_REPLACE is set to indicate a layout swap
2086 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2087 if ((S_ISREG(mode) || mode == 0) && strcmp(name, XATTR_NAME_LOV) == 0 &&
2088 !(fl & LU_XATTR_REPLACE)) {
2090 * this is a request to manipulate object's striping
2092 if (dt_object_exists(dt)) {
2093 rc = dt_attr_get(env, next, attr);
2097 memset(attr, 0, sizeof(*attr));
2098 attr->la_valid = LA_TYPE | LA_MODE;
2099 attr->la_mode = S_IFREG;
2101 rc = lod_declare_striped_object(env, dt, attr, buf, th);
2102 } else if (S_ISDIR(mode)) {
2103 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2105 rc = lod_sub_object_declare_xattr_set(env, next, buf, name,
2113 * Resets cached default striping in the object.
2115 * \param[in] lo object
2117 static void lod_lov_stripe_cache_clear(struct lod_object *lo)
2119 lo->ldo_def_striping_set = 0;
2120 lo->ldo_def_striping_cached = 0;
2121 lod_object_set_pool(lo, NULL);
2122 lo->ldo_def_stripe_size = 0;
2123 lo->ldo_def_stripenr = 0;
2124 if (lo->ldo_dir_stripe != NULL)
2125 lo->ldo_dir_def_striping_cached = 0;
2129 * Apply xattr changes to the object.
2131 * Applies xattr changes to the object and the stripes if the latter exist.
2133 * \param[in] env execution environment
2134 * \param[in] dt object
2135 * \param[in] buf buffer pointing to the new value of xattr
2136 * \param[in] name name of xattr
2137 * \param[in] fl flags
2138 * \param[in] th transaction handle
2140 * \retval 0 on success
2141 * \retval negative if failed
2143 static int lod_xattr_set_internal(const struct lu_env *env,
2144 struct dt_object *dt,
2145 const struct lu_buf *buf,
2146 const char *name, int fl, struct thandle *th)
2148 struct dt_object *next = dt_object_child(dt);
2149 struct lod_object *lo = lod_dt_obj(dt);
2154 rc = lod_sub_object_xattr_set(env, next, buf, name, fl, th);
2155 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2158 /* Note: Do not set LinkEA on sub-stripes, otherwise
2159 * it will confuse the fid2path process(see mdt_path_current()).
2160 * The linkEA between master and sub-stripes is set in
2161 * lod_xattr_set_lmv(). */
2162 if (lo->ldo_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2165 for (i = 0; i < lo->ldo_stripenr; i++) {
2166 LASSERT(lo->ldo_stripe[i]);
2168 rc = lod_sub_object_xattr_set(env, lo->ldo_stripe[i], buf, name,
2178 * Delete an extended attribute.
2180 * Deletes specified xattr from the object and the stripes if the latter exist.
2182 * \param[in] env execution environment
2183 * \param[in] dt object
2184 * \param[in] name name of xattr
2185 * \param[in] th transaction handle
2187 * \retval 0 on success
2188 * \retval negative if failed
2190 static int lod_xattr_del_internal(const struct lu_env *env,
2191 struct dt_object *dt,
2192 const char *name, struct thandle *th)
2194 struct dt_object *next = dt_object_child(dt);
2195 struct lod_object *lo = lod_dt_obj(dt);
2200 rc = lod_sub_object_xattr_del(env, next, name, th);
2201 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2204 if (lo->ldo_stripenr == 0)
2207 for (i = 0; i < lo->ldo_stripenr; i++) {
2208 LASSERT(lo->ldo_stripe[i]);
2210 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name,
2220 * Set default striping on a directory.
2222 * Sets specified striping on a directory object unless it matches the default
2223 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2224 * EA. This striping will be used when regular file is being created in this
2227 * \param[in] env execution environment
2228 * \param[in] dt the striped object
2229 * \param[in] buf buffer with the striping
2230 * \param[in] name name of EA
2231 * \param[in] fl xattr flag (see OSD API description)
2232 * \param[in] th transaction handle
2234 * \retval 0 on success
2235 * \retval negative if failed
2237 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
2238 struct dt_object *dt,
2239 const struct lu_buf *buf,
2240 const char *name, int fl,
2243 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2244 struct lod_object *l = lod_dt_obj(dt);
2245 struct lov_user_md_v1 *lum;
2246 struct lov_user_md_v3 *v3 = NULL;
2247 const char *pool_name = NULL;
2251 /* If it is striped dir, we should clear the stripe cache for
2252 * slave stripe as well, but there are no effective way to
2253 * notify the LOD on the slave MDT, so we do not cache stripe
2254 * information for slave stripe for now. XXX*/
2255 lod_lov_stripe_cache_clear(l);
2256 LASSERT(buf != NULL && buf->lb_buf != NULL);
2259 rc = lod_verify_striping(d, buf, false);
2263 if (lum->lmm_magic == LOV_USER_MAGIC_V3) {
2265 if (v3->lmm_pool_name[0] != '\0')
2266 pool_name = v3->lmm_pool_name;
2269 /* if { size, offset, count } = { 0, -1, 0 } and no pool
2270 * (i.e. all default values specified) then delete default
2271 * striping from dir. */
2273 "set default striping: sz %u # %u offset %d %s %s\n",
2274 (unsigned)lum->lmm_stripe_size,
2275 (unsigned)lum->lmm_stripe_count,
2276 (int)lum->lmm_stripe_offset,
2277 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
2279 if (LOVEA_DELETE_VALUES(lum->lmm_stripe_size, lum->lmm_stripe_count,
2280 lum->lmm_stripe_offset, pool_name)) {
2281 rc = lod_xattr_del_internal(env, dt, name, th);
2285 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2292 * Set default striping on a directory object.
2294 * Sets specified striping on a directory object unless it matches the default
2295 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2296 * EA. This striping will be used when a new directory is being created in the
2299 * \param[in] env execution environment
2300 * \param[in] dt the striped object
2301 * \param[in] buf buffer with the striping
2302 * \param[in] name name of EA
2303 * \param[in] fl xattr flag (see OSD API description)
2304 * \param[in] th transaction handle
2306 * \retval 0 on success
2307 * \retval negative if failed
2309 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
2310 struct dt_object *dt,
2311 const struct lu_buf *buf,
2312 const char *name, int fl,
2315 struct lod_object *l = lod_dt_obj(dt);
2316 struct lmv_user_md_v1 *lum;
2320 LASSERT(buf != NULL && buf->lb_buf != NULL);
2323 CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
2324 le32_to_cpu(lum->lum_stripe_count),
2325 (int)le32_to_cpu(lum->lum_stripe_offset));
2327 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
2328 le32_to_cpu(lum->lum_stripe_offset)) &&
2329 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
2330 rc = lod_xattr_del_internal(env, dt, name, th);
2334 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2339 /* Update default stripe cache */
2340 if (l->ldo_dir_stripe == NULL) {
2341 OBD_ALLOC_PTR(l->ldo_dir_stripe);
2342 if (l->ldo_dir_stripe == NULL)
2346 l->ldo_dir_def_striping_cached = 0;
2351 * Turn directory into a striped directory.
2353 * During replay the client sends the striping created before MDT
2354 * failure, then the layer above LOD sends this defined striping
2355 * using ->do_xattr_set(), so LOD uses this method to replay creation
2356 * of the stripes. Notice the original information for the striping
2357 * (#stripes, FIDs, etc) was transferred in declare path.
2359 * \param[in] env execution environment
2360 * \param[in] dt the striped object
2361 * \param[in] buf not used currently
2362 * \param[in] name not used currently
2363 * \param[in] fl xattr flag (see OSD API description)
2364 * \param[in] th transaction handle
2366 * \retval 0 on success
2367 * \retval negative if failed
2369 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
2370 const struct lu_buf *buf, const char *name,
2371 int fl, struct thandle *th)
2373 struct lod_object *lo = lod_dt_obj(dt);
2374 struct lod_thread_info *info = lod_env_info(env);
2375 struct lu_attr *attr = &info->lti_attr;
2376 struct dt_object_format *dof = &info->lti_format;
2377 struct lu_buf lmv_buf;
2378 struct lu_buf slave_lmv_buf;
2379 struct lmv_mds_md_v1 *lmm;
2380 struct lmv_mds_md_v1 *slave_lmm = NULL;
2381 struct dt_insert_rec *rec = &info->lti_dt_rec;
2386 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2389 /* The stripes are supposed to be allocated in declare phase,
2390 * if there are no stripes being allocated, it will skip */
2391 if (lo->ldo_stripenr == 0)
2394 rc = dt_attr_get(env, dt_object_child(dt), attr);
2398 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
2399 LA_MODE | LA_UID | LA_GID | LA_TYPE;
2400 dof->dof_type = DFT_DIR;
2402 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
2405 lmm = lmv_buf.lb_buf;
2407 OBD_ALLOC_PTR(slave_lmm);
2408 if (slave_lmm == NULL)
2411 lod_prep_slave_lmv_md(slave_lmm, lmm);
2412 slave_lmv_buf.lb_buf = slave_lmm;
2413 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
2415 rec->rec_type = S_IFDIR;
2416 for (i = 0; i < lo->ldo_stripenr; i++) {
2417 struct dt_object *dto;
2418 char *stripe_name = info->lti_key;
2419 struct lu_name *sname;
2420 struct linkea_data ldata = { NULL };
2421 struct lu_buf linkea_buf;
2423 dto = lo->ldo_stripe[i];
2425 dt_write_lock(env, dto, MOR_TGT_CHILD);
2426 rc = lod_sub_object_create(env, dto, attr, NULL, dof,
2429 dt_write_unlock(env, dto);
2433 rc = lod_sub_object_ref_add(env, dto, th);
2434 dt_write_unlock(env, dto);
2438 rec->rec_fid = lu_object_fid(&dto->do_lu);
2439 rc = lod_sub_object_index_insert(env, dto,
2440 (const struct dt_rec *)rec,
2441 (const struct dt_key *)dot, th, 0);
2445 rec->rec_fid = lu_object_fid(&dt->do_lu);
2446 rc = lod_sub_object_index_insert(env, dto, (struct dt_rec *)rec,
2447 (const struct dt_key *)dotdot, th, 0);
2451 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
2452 cfs_fail_val != i) {
2453 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
2455 slave_lmm->lmv_master_mdt_index =
2458 slave_lmm->lmv_master_mdt_index =
2461 rc = lod_sub_object_xattr_set(env, dto, &slave_lmv_buf,
2462 XATTR_NAME_LMV, fl, th);
2467 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
2469 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2470 PFID(lu_object_fid(&dto->do_lu)), i + 1);
2472 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2473 PFID(lu_object_fid(&dto->do_lu)), i);
2475 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2476 rc = linkea_data_new(&ldata, &info->lti_linkea_buf);
2480 rc = linkea_add_buf(&ldata, sname, lu_object_fid(&dt->do_lu));
2484 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
2485 linkea_buf.lb_len = ldata.ld_leh->leh_len;
2486 rc = lod_sub_object_xattr_set(env, dto, &linkea_buf,
2487 XATTR_NAME_LINK, 0, th);
2491 rec->rec_fid = lu_object_fid(&dto->do_lu);
2492 rc = lod_sub_object_index_insert(env, dt_object_child(dt),
2493 (const struct dt_rec *)rec,
2494 (const struct dt_key *)stripe_name, th, 0);
2498 rc = lod_sub_object_ref_add(env, dt_object_child(dt), th);
2503 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
2504 rc = lod_sub_object_xattr_set(env, dt_object_child(dt),
2505 &lmv_buf, XATTR_NAME_LMV, fl, th);
2507 if (slave_lmm != NULL)
2508 OBD_FREE_PTR(slave_lmm);
2514 * Helper function to declare/execute creation of a striped directory
2516 * Called in declare/create object path, prepare striping for a directory
2517 * and prepare defaults data striping for the objects to be created in
2518 * that directory. Notice the function calls "declaration" or "execution"
2519 * methods depending on \a declare param. This is a consequence of the
2520 * current approach while we don't have natural distributed transactions:
2521 * we basically execute non-local updates in the declare phase. So, the
2522 * arguments for the both phases are the same and this is the reason for
2523 * this function to exist.
2525 * \param[in] env execution environment
2526 * \param[in] dt object
2527 * \param[in] attr attributes the stripes will be created with
2528 * \param[in] dof format of stripes (see OSD API description)
2529 * \param[in] th transaction handle
2530 * \param[in] declare where to call "declare" or "execute" methods
2532 * \retval 0 on success
2533 * \retval negative if failed
2535 static int lod_dir_striping_create_internal(const struct lu_env *env,
2536 struct dt_object *dt,
2537 struct lu_attr *attr,
2538 struct dt_object_format *dof,
2542 struct lod_thread_info *info = lod_env_info(env);
2543 struct lod_object *lo = lod_dt_obj(dt);
2547 if (!LMVEA_DELETE_VALUES(lo->ldo_stripenr,
2548 lo->ldo_dir_stripe_offset)) {
2549 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
2550 int stripe_count = lo->ldo_stripenr;
2552 if (info->lti_ea_store_size < sizeof(*v1)) {
2553 rc = lod_ea_store_resize(info, sizeof(*v1));
2556 v1 = info->lti_ea_store;
2559 memset(v1, 0, sizeof(*v1));
2560 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
2561 v1->lum_stripe_count = cpu_to_le32(stripe_count);
2562 v1->lum_stripe_offset =
2563 cpu_to_le32(lo->ldo_dir_stripe_offset);
2565 info->lti_buf.lb_buf = v1;
2566 info->lti_buf.lb_len = sizeof(*v1);
2569 rc = lod_declare_xattr_set_lmv(env, dt, attr,
2570 &info->lti_buf, dof, th);
2572 rc = lod_xattr_set_lmv(env, dt, &info->lti_buf,
2573 XATTR_NAME_LMV, 0, th);
2578 /* Transfer default LMV striping from the parent */
2579 if (lo->ldo_dir_def_striping_set &&
2580 !LMVEA_DELETE_VALUES(lo->ldo_dir_def_stripenr,
2581 lo->ldo_dir_def_stripe_offset)) {
2582 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
2583 int def_stripe_count = lo->ldo_dir_def_stripenr;
2585 if (info->lti_ea_store_size < sizeof(*v1)) {
2586 rc = lod_ea_store_resize(info, sizeof(*v1));
2589 v1 = info->lti_ea_store;
2592 memset(v1, 0, sizeof(*v1));
2593 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
2594 v1->lum_stripe_count = cpu_to_le32(def_stripe_count);
2595 v1->lum_stripe_offset =
2596 cpu_to_le32(lo->ldo_dir_def_stripe_offset);
2598 cpu_to_le32(lo->ldo_dir_def_hash_type);
2600 info->lti_buf.lb_buf = v1;
2601 info->lti_buf.lb_len = sizeof(*v1);
2603 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
2604 XATTR_NAME_DEFAULT_LMV,
2607 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
2609 XATTR_NAME_DEFAULT_LMV, 0,
2615 /* Transfer default LOV striping from the parent */
2616 if (lo->ldo_def_striping_set &&
2617 !LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size,
2618 lo->ldo_def_stripenr,
2619 lo->ldo_def_stripe_offset,
2621 struct lov_user_md_v3 *v3 = info->lti_ea_store;
2623 if (info->lti_ea_store_size < sizeof(*v3)) {
2624 rc = lod_ea_store_resize(info, sizeof(*v3));
2627 v3 = info->lti_ea_store;
2630 memset(v3, 0, sizeof(*v3));
2631 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
2632 v3->lmm_stripe_count = cpu_to_le16(lo->ldo_def_stripenr);
2633 v3->lmm_stripe_offset = cpu_to_le16(lo->ldo_def_stripe_offset);
2634 v3->lmm_stripe_size = cpu_to_le32(lo->ldo_def_stripe_size);
2635 if (lo->ldo_pool != NULL)
2636 strlcpy(v3->lmm_pool_name, lo->ldo_pool,
2637 sizeof(v3->lmm_pool_name));
2639 info->lti_buf.lb_buf = v3;
2640 info->lti_buf.lb_len = sizeof(*v3);
2643 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
2644 XATTR_NAME_LOV, 0, th);
2646 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
2647 XATTR_NAME_LOV, 0, th);
2655 static int lod_declare_dir_striping_create(const struct lu_env *env,
2656 struct dt_object *dt,
2657 struct lu_attr *attr,
2658 struct dt_object_format *dof,
2661 return lod_dir_striping_create_internal(env, dt, attr, dof, th, true);
2664 static int lod_dir_striping_create(const struct lu_env *env,
2665 struct dt_object *dt,
2666 struct lu_attr *attr,
2667 struct dt_object_format *dof,
2670 struct lod_object *lo = lod_dt_obj(dt);
2673 rc = lod_dir_striping_create_internal(env, dt, attr, dof, th, false);
2675 lo->ldo_striping_cached = 1;
2681 * Implementation of dt_object_operations::do_xattr_set.
2683 * Sets specified extended attribute on the object. Three types of EAs are
2685 * LOV EA - stores striping for a regular file or default striping (when set
2687 * LMV EA - stores a marker for the striped directories
2688 * DMV EA - stores default directory striping
2690 * When striping is applied to a non-striped existing object (this is called
2691 * late striping), then LOD notices the caller wants to turn the object into a
2692 * striped one. The stripe objects are created and appropriate EA is set:
2693 * LOV EA storing all the stripes directly or LMV EA storing just a small header
2694 * with striping configuration.
2696 * \see dt_object_operations::do_xattr_set() in the API description for details.
2698 static int lod_xattr_set(const struct lu_env *env,
2699 struct dt_object *dt, const struct lu_buf *buf,
2700 const char *name, int fl, struct thandle *th)
2702 struct dt_object *next = dt_object_child(dt);
2706 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2707 strcmp(name, XATTR_NAME_LMV) == 0) {
2708 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
2710 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
2711 LMV_HASH_FLAG_MIGRATION)
2712 rc = lod_sub_object_xattr_set(env, next, buf, name, fl,
2715 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
2720 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2721 strcmp(name, XATTR_NAME_LOV) == 0) {
2723 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
2725 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2726 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2728 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
2731 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
2732 !strcmp(name, XATTR_NAME_LOV)) {
2733 /* in case of lov EA swap, just set it
2734 * if not, it is a replay so check striping match what we
2735 * already have during req replay, declare_xattr_set()
2736 * defines striping, then create() does the work */
2737 if (fl & LU_XATTR_REPLACE) {
2738 /* free stripes, then update disk */
2739 lod_object_free_striping(env, lod_dt_obj(dt));
2741 rc = lod_sub_object_xattr_set(env, next, buf, name,
2743 } else if (dt_object_remote(dt)) {
2744 /* This only happens during migration, see
2745 * mdd_migrate_create(), in which Master MDT will
2746 * create a remote target object, and only set
2747 * (migrating) stripe EA on the remote object,
2748 * and does not need creating each stripes. */
2749 rc = lod_sub_object_xattr_set(env, next, buf, name,
2752 rc = lod_striping_create(env, dt, NULL, NULL, th);
2757 /* then all other xattr */
2758 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2764 * Implementation of dt_object_operations::do_declare_xattr_del.
2766 * \see dt_object_operations::do_declare_xattr_del() in the API description
2769 static int lod_declare_xattr_del(const struct lu_env *env,
2770 struct dt_object *dt, const char *name,
2773 struct lod_object *lo = lod_dt_obj(dt);
2778 rc = lod_sub_object_declare_xattr_del(env, dt_object_child(dt),
2783 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2786 /* set xattr to each stripes, if needed */
2787 rc = lod_load_striping(env, lo);
2791 if (lo->ldo_stripenr == 0)
2794 for (i = 0; i < lo->ldo_stripenr; i++) {
2795 LASSERT(lo->ldo_stripe[i]);
2796 rc = lod_sub_object_declare_xattr_del(env, lo->ldo_stripe[i],
2806 * Implementation of dt_object_operations::do_xattr_del.
2808 * If EA storing a regular striping is being deleted, then release
2809 * all the references to the stripe objects in core.
2811 * \see dt_object_operations::do_xattr_del() in the API description for details.
2813 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
2814 const char *name, struct thandle *th)
2816 struct dt_object *next = dt_object_child(dt);
2817 struct lod_object *lo = lod_dt_obj(dt);
2822 if (!strcmp(name, XATTR_NAME_LOV))
2823 lod_object_free_striping(env, lod_dt_obj(dt));
2825 rc = lod_sub_object_xattr_del(env, next, name, th);
2826 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2829 if (lo->ldo_stripenr == 0)
2832 for (i = 0; i < lo->ldo_stripenr; i++) {
2833 LASSERT(lo->ldo_stripe[i]);
2835 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name, th);
2844 * Implementation of dt_object_operations::do_xattr_list.
2846 * \see dt_object_operations::do_xattr_list() in the API description
2849 static int lod_xattr_list(const struct lu_env *env,
2850 struct dt_object *dt, const struct lu_buf *buf)
2852 return dt_xattr_list(env, dt_object_child(dt), buf);
2856 * Initialize a pool the object belongs to.
2858 * When a striped object is being created, striping configuration
2859 * may demand the stripes are allocated on a limited set of the
2860 * targets. These limited sets are known as "pools". So we copy
2861 * a pool name into the object and later actual creation methods
2862 * (like lod_object_create()) will use this information to allocate
2863 * the stripes properly.
2865 * \param[in] o object
2866 * \param[in] pool pool name
2868 int lod_object_set_pool(struct lod_object *o, char *pool)
2873 len = strlen(o->ldo_pool);
2874 OBD_FREE(o->ldo_pool, len + 1);
2879 OBD_ALLOC(o->ldo_pool, len + 1);
2880 if (o->ldo_pool == NULL)
2882 strcpy(o->ldo_pool, pool);
2887 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
2889 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
2894 * Cache default regular striping in the object.
2896 * To improve performance of striped regular object creation we cache
2897 * default LOV striping (if it exists) in the parent directory object.
2899 * \param[in] env execution environment
2900 * \param[in] lp object
2902 * \retval 0 on success
2903 * \retval negative if failed
2905 static int lod_cache_parent_lov_striping(const struct lu_env *env,
2906 struct lod_object *lp)
2908 struct lod_thread_info *info = lod_env_info(env);
2909 struct lov_user_md_v1 *v1 = NULL;
2910 struct lov_user_md_v3 *v3 = NULL;
2914 /* called from MDD without parent being write locked,
2916 dt_write_lock(env, dt_object_child(&lp->ldo_obj), 0);
2917 rc = lod_get_lov_ea(env, lp);
2921 if (rc < (typeof(rc))sizeof(struct lov_user_md)) {
2922 /* don't lookup for non-existing or invalid striping */
2923 lp->ldo_def_striping_set = 0;
2924 lp->ldo_def_striping_cached = 1;
2925 lp->ldo_def_stripe_size = 0;
2926 lp->ldo_def_stripenr = 0;
2927 lp->ldo_def_stripe_offset = (typeof(v1->lmm_stripe_offset))(-1);
2928 GOTO(unlock, rc = 0);
2932 v1 = info->lti_ea_store;
2933 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
2934 lustre_swab_lov_user_md_v1(v1);
2935 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
2936 v3 = (struct lov_user_md_v3 *)v1;
2937 lustre_swab_lov_user_md_v3(v3);
2940 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1)
2941 GOTO(unlock, rc = 0);
2943 if (v1->lmm_pattern != LOV_PATTERN_RAID0 && v1->lmm_pattern != 0)
2944 GOTO(unlock, rc = 0);
2946 CDEBUG(D_INFO, DFID" stripe_count=%d stripe_size=%d stripe_offset=%d\n",
2947 PFID(lu_object_fid(&lp->ldo_obj.do_lu)),
2948 (int)v1->lmm_stripe_count,
2949 (int)v1->lmm_stripe_size, (int)v1->lmm_stripe_offset);
2951 lp->ldo_def_stripenr = v1->lmm_stripe_count;
2952 lp->ldo_def_stripe_size = v1->lmm_stripe_size;
2953 lp->ldo_def_stripe_offset = v1->lmm_stripe_offset;
2954 lp->ldo_def_striping_cached = 1;
2955 lp->ldo_def_striping_set = 1;
2956 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2957 /* XXX: sanity check here */
2958 v3 = (struct lov_user_md_v3 *) v1;
2959 if (v3->lmm_pool_name[0])
2960 lod_object_set_pool(lp, v3->lmm_pool_name);
2964 dt_write_unlock(env, dt_object_child(&lp->ldo_obj));
2970 * Cache default directory striping in the object.
2972 * To improve performance of striped directory creation we cache default
2973 * directory striping (if it exists) in the parent directory object.
2975 * \param[in] env execution environment
2976 * \param[in] lp object
2978 * \retval 0 on success
2979 * \retval negative if failed
2981 static int lod_cache_parent_lmv_striping(const struct lu_env *env,
2982 struct lod_object *lp)
2984 struct lod_thread_info *info = lod_env_info(env);
2985 struct lmv_user_md_v1 *v1 = NULL;
2989 /* called from MDD without parent being write locked,
2991 dt_write_lock(env, dt_object_child(&lp->ldo_obj), 0);
2992 rc = lod_get_default_lmv_ea(env, lp);
2996 if (rc < (typeof(rc))sizeof(struct lmv_user_md)) {
2997 /* don't lookup for non-existing or invalid striping */
2998 lp->ldo_dir_def_striping_set = 0;
2999 lp->ldo_dir_def_striping_cached = 1;
3000 lp->ldo_dir_def_stripenr = 0;
3001 lp->ldo_dir_def_stripe_offset =
3002 (typeof(v1->lum_stripe_offset))(-1);
3003 lp->ldo_dir_def_hash_type = LMV_HASH_TYPE_FNV_1A_64;
3004 GOTO(unlock, rc = 0);
3008 v1 = info->lti_ea_store;
3010 lp->ldo_dir_def_stripenr = le32_to_cpu(v1->lum_stripe_count);
3011 lp->ldo_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
3012 lp->ldo_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
3013 lp->ldo_dir_def_striping_set = 1;
3014 lp->ldo_dir_def_striping_cached = 1;
3018 dt_write_unlock(env, dt_object_child(&lp->ldo_obj));
3023 * Cache default striping in the object.
3025 * To improve performance of striped object creation we cache default striping
3026 * (if it exists) in the parent directory object. We always cache default
3027 * striping for the regular files (stored in LOV EA) and we cache default
3028 * striping for the directories if requested by \a child_mode (when a new
3029 * directory is being created).
3031 * \param[in] env execution environment
3032 * \param[in] lp object
3033 * \param[in] child_mode new object's mode
3035 * \retval 0 on success
3036 * \retval negative if failed
3038 static int lod_cache_parent_striping(const struct lu_env *env,
3039 struct lod_object *lp,
3045 if (!lp->ldo_def_striping_cached) {
3046 /* we haven't tried to get default striping for
3047 * the directory yet, let's cache it in the object */
3048 rc = lod_cache_parent_lov_striping(env, lp);
3053 /* If the parent is on the remote MDT, we should always
3054 * try to refresh the default stripeEA cache, because we
3055 * do not cache default striping information for remote
3057 if (S_ISDIR(child_mode) && (!lp->ldo_dir_def_striping_cached ||
3058 dt_object_remote(&lp->ldo_obj)))
3059 rc = lod_cache_parent_lmv_striping(env, lp);
3065 * Implementation of dt_object_operations::do_ah_init.
3067 * This method is used to make a decision on the striping configuration for the
3068 * object being created. It can be taken from the \a parent object if it exists,
3069 * or filesystem's default. The resulting configuration (number of stripes,
3070 * stripe size/offset, pool name, etc) is stored in the object itself and will
3071 * be used by the methods like ->doo_declare_create().
3073 * \see dt_object_operations::do_ah_init() in the API description for details.
3075 static void lod_ah_init(const struct lu_env *env,
3076 struct dt_allocation_hint *ah,
3077 struct dt_object *parent,
3078 struct dt_object *child,
3081 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
3082 struct dt_object *nextp = NULL;
3083 struct dt_object *nextc;
3084 struct lod_object *lp = NULL;
3085 struct lod_object *lc;
3086 struct lov_desc *desc;
3092 if (likely(parent)) {
3093 nextp = dt_object_child(parent);
3094 lp = lod_dt_obj(parent);
3095 rc = lod_load_striping(env, lp);
3100 nextc = dt_object_child(child);
3101 lc = lod_dt_obj(child);
3103 LASSERT(lc->ldo_stripenr == 0);
3104 LASSERT(lc->ldo_stripe == NULL);
3107 * local object may want some hints
3108 * in case of late striping creation, ->ah_init()
3109 * can be called with local object existing
3111 if (!dt_object_exists(nextc) || dt_object_remote(nextc)) {
3112 struct dt_object *obj;
3114 obj = (nextp != NULL && dt_object_remote(nextp)) ? NULL : nextp;
3115 nextc->do_ops->do_ah_init(env, ah, obj, nextc, child_mode);
3118 if (S_ISDIR(child_mode)) {
3119 if (lc->ldo_dir_stripe == NULL) {
3120 OBD_ALLOC_PTR(lc->ldo_dir_stripe);
3121 if (lc->ldo_dir_stripe == NULL)
3125 LASSERT(lp != NULL);
3126 if (lp->ldo_dir_stripe == NULL) {
3127 OBD_ALLOC_PTR(lp->ldo_dir_stripe);
3128 if (lp->ldo_dir_stripe == NULL)
3132 rc = lod_cache_parent_striping(env, lp, child_mode);
3136 /* transfer defaults to new directory */
3137 if (lp->ldo_def_striping_set) {
3139 lod_object_set_pool(lc, lp->ldo_pool);
3140 lc->ldo_def_stripenr = lp->ldo_def_stripenr;
3141 lc->ldo_def_stripe_size = lp->ldo_def_stripe_size;
3142 lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
3143 lc->ldo_def_striping_set = 1;
3144 lc->ldo_def_striping_cached = 1;
3145 CDEBUG(D_OTHER, "inherite EA sz:%d off:%d nr:%d\n",
3146 (int)lc->ldo_def_stripe_size,
3147 (int)lc->ldo_def_stripe_offset,
3148 (int)lc->ldo_def_stripenr);
3151 /* transfer dir defaults to new directory */
3152 if (lp->ldo_dir_def_striping_set) {
3153 lc->ldo_dir_def_stripenr = lp->ldo_dir_def_stripenr;
3154 lc->ldo_dir_def_stripe_offset =
3155 lp->ldo_dir_def_stripe_offset;
3156 lc->ldo_dir_def_hash_type =
3157 lp->ldo_dir_def_hash_type;
3158 lc->ldo_dir_def_striping_set = 1;
3159 lc->ldo_dir_def_striping_cached = 1;
3160 CDEBUG(D_INFO, "inherit default EA nr:%d off:%d t%u\n",
3161 (int)lc->ldo_dir_def_stripenr,
3162 (int)lc->ldo_dir_def_stripe_offset,
3163 lc->ldo_dir_def_hash_type);
3166 /* It should always honour the specified stripes */
3167 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0) {
3168 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
3170 rc = lod_verify_md_striping(d, lum1);
3172 le32_to_cpu(lum1->lum_stripe_count) > 1) {
3173 /* Directory will be striped only if
3174 * stripe_count > 1 */
3176 le32_to_cpu(lum1->lum_stripe_count);
3177 lc->ldo_dir_stripe_offset =
3178 le32_to_cpu(lum1->lum_stripe_offset);
3179 lc->ldo_dir_hash_type =
3180 le32_to_cpu(lum1->lum_hash_type);
3181 CDEBUG(D_INFO, "set stripe EA nr:%hu off:%d\n",
3183 (int)lc->ldo_dir_stripe_offset);
3185 /* then check whether there is default stripes from parent */
3186 } else if (lp->ldo_dir_def_striping_set) {
3187 /* If there are default dir stripe from parent */
3188 lc->ldo_stripenr = lp->ldo_dir_def_stripenr;
3189 lc->ldo_dir_stripe_offset =
3190 lp->ldo_dir_def_stripe_offset;
3191 lc->ldo_dir_hash_type =
3192 lp->ldo_dir_def_hash_type;
3193 CDEBUG(D_INFO, "inherit EA nr:%hu off:%d\n",
3195 (int)lc->ldo_dir_stripe_offset);
3197 /* set default stripe for this directory */
3198 lc->ldo_stripenr = 0;
3199 lc->ldo_dir_stripe_offset = -1;
3202 CDEBUG(D_INFO, "final striping count:%hu, offset:%d\n",
3203 lc->ldo_stripenr, (int)lc->ldo_dir_stripe_offset);
3209 * if object is going to be striped over OSTs, transfer default
3210 * striping information to the child, so that we can use it
3211 * during declaration and creation
3213 if (!lod_object_will_be_striped(S_ISREG(child_mode),
3214 lu_object_fid(&child->do_lu)))
3217 * try from the parent
3219 if (likely(parent)) {
3220 lod_cache_parent_striping(env, lp, child_mode);
3222 lc->ldo_def_stripe_offset = LOV_OFFSET_DEFAULT;
3224 if (lp->ldo_def_striping_set) {
3226 lod_object_set_pool(lc, lp->ldo_pool);
3227 lc->ldo_stripenr = lp->ldo_def_stripenr;
3228 lc->ldo_stripe_size = lp->ldo_def_stripe_size;
3229 lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
3230 CDEBUG(D_OTHER, "striping from parent: #%d, sz %d %s\n",
3231 lc->ldo_stripenr, lc->ldo_stripe_size,
3232 lp->ldo_pool ? lp->ldo_pool : "");
3237 * if the parent doesn't provide with specific pattern, grab fs-wide one
3239 desc = &d->lod_desc;
3240 if (lc->ldo_stripenr == 0)
3241 lc->ldo_stripenr = desc->ld_default_stripe_count;
3242 if (lc->ldo_stripe_size == 0)
3243 lc->ldo_stripe_size = desc->ld_default_stripe_size;
3244 CDEBUG(D_OTHER, "final striping: # %d stripes, sz %d from %s\n",
3245 lc->ldo_stripenr, lc->ldo_stripe_size,
3246 lc->ldo_pool ? lc->ldo_pool : "");
3249 /* we do not cache stripe information for slave stripe, see
3250 * lod_xattr_set_lov_on_dir */
3251 if (lp != NULL && lp->ldo_dir_slave_stripe)
3252 lod_lov_stripe_cache_clear(lp);
3257 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
3259 * Size initialization on late striping.
3261 * Propagate the size of a truncated object to a deferred striping.
3262 * This function handles a special case when truncate was done on a
3263 * non-striped object and now while the striping is being created
3264 * we can't lose that size, so we have to propagate it to the stripes
3267 * \param[in] env execution environment
3268 * \param[in] dt object
3269 * \param[in] th transaction handle
3271 * \retval 0 on success
3272 * \retval negative if failed
3274 static int lod_declare_init_size(const struct lu_env *env,
3275 struct dt_object *dt, struct thandle *th)
3277 struct dt_object *next = dt_object_child(dt);
3278 struct lod_object *lo = lod_dt_obj(dt);
3279 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3280 uint64_t size, offs;
3284 /* XXX: we support the simplest (RAID0) striping so far */
3285 LASSERT(lo->ldo_stripe || lo->ldo_stripenr == 0);
3286 LASSERT(lo->ldo_stripe_size > 0);
3288 rc = dt_attr_get(env, next, attr);
3289 LASSERT(attr->la_valid & LA_SIZE);
3293 size = attr->la_size;
3297 /* ll_do_div64(a, b) returns a % b, and a = a / b */
3298 ll_do_div64(size, (__u64) lo->ldo_stripe_size);
3299 stripe = ll_do_div64(size, (__u64) lo->ldo_stripenr);
3301 size = size * lo->ldo_stripe_size;
3302 offs = attr->la_size;
3303 size += ll_do_div64(offs, lo->ldo_stripe_size);
3305 attr->la_valid = LA_SIZE;
3306 attr->la_size = size;
3308 rc = lod_sub_object_declare_attr_set(env, lo->ldo_stripe[stripe], attr,
3315 * Declare creation of striped object.
3317 * The function declares creation stripes for a regular object. The function
3318 * also declares whether the stripes will be created with non-zero size if
3319 * previously size was set non-zero on the master object. If object \a dt is
3320 * not local, then only fully defined striping can be applied in \a lovea.
3321 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
3324 * \param[in] env execution environment
3325 * \param[in] dt object
3326 * \param[in] attr attributes the stripes will be created with
3327 * \param[in] lovea a buffer containing striping description
3328 * \param[in] th transaction handle
3330 * \retval 0 on success
3331 * \retval negative if failed
3333 int lod_declare_striped_object(const struct lu_env *env, struct dt_object *dt,
3334 struct lu_attr *attr,
3335 const struct lu_buf *lovea, struct thandle *th)
3337 struct lod_thread_info *info = lod_env_info(env);
3338 struct dt_object *next = dt_object_child(dt);
3339 struct lod_object *lo = lod_dt_obj(dt);
3343 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO)) {
3344 /* failed to create striping, let's reset
3345 * config so that others don't get confused */
3346 lod_object_free_striping(env, lo);
3347 GOTO(out, rc = -ENOMEM);
3350 if (!dt_object_remote(next)) {
3351 /* choose OST and generate appropriate objects */
3352 rc = lod_qos_prep_create(env, lo, attr, lovea, th);
3354 /* failed to create striping, let's reset
3355 * config so that others don't get confused */
3356 lod_object_free_striping(env, lo);
3361 * declare storage for striping data
3363 info->lti_buf.lb_len = lov_mds_md_size(lo->ldo_stripenr,
3364 lo->ldo_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1);
3366 /* LOD can not choose OST objects for remote objects, i.e.
3367 * stripes must be ready before that. Right now, it can only
3368 * happen during migrate, i.e. migrate process needs to create
3369 * remote regular file (mdd_migrate_create), then the migrate
3370 * process will provide stripeEA. */
3371 LASSERT(lovea != NULL);
3372 info->lti_buf = *lovea;
3375 rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
3376 XATTR_NAME_LOV, 0, th);
3381 * if striping is created with local object's size > 0,
3382 * we have to propagate this size to specific object
3383 * the case is possible only when local object was created previously
3385 if (dt_object_exists(next))
3386 rc = lod_declare_init_size(env, dt, th);
3393 * Implementation of dt_object_operations::do_declare_create.
3395 * The method declares creation of a new object. If the object will be striped,
3396 * then helper functions are called to find FIDs for the stripes, declare
3397 * creation of the stripes and declare initialization of the striping
3398 * information to be stored in the master object.
3400 * \see dt_object_operations::do_declare_create() in the API description
3403 static int lod_declare_object_create(const struct lu_env *env,
3404 struct dt_object *dt,
3405 struct lu_attr *attr,
3406 struct dt_allocation_hint *hint,
3407 struct dt_object_format *dof,
3410 struct dt_object *next = dt_object_child(dt);
3411 struct lod_object *lo = lod_dt_obj(dt);
3420 * first of all, we declare creation of local object
3422 rc = lod_sub_object_declare_create(env, next, attr, hint, dof, th);
3426 if (dof->dof_type == DFT_SYM)
3427 dt->do_body_ops = &lod_body_lnk_ops;
3430 * it's lod_ah_init() that has decided the object will be striped
3432 if (dof->dof_type == DFT_REGULAR) {
3433 /* callers don't want stripes */
3434 /* XXX: all tricky interactions with ->ah_make_hint() decided
3435 * to use striping, then ->declare_create() behaving differently
3436 * should be cleaned */
3437 if (dof->u.dof_reg.striped == 0)
3438 lo->ldo_stripenr = 0;
3439 if (lo->ldo_stripenr > 0)
3440 rc = lod_declare_striped_object(env, dt, attr,
3442 } else if (dof->dof_type == DFT_DIR) {
3443 struct seq_server_site *ss;
3445 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
3447 /* If the parent has default stripeEA, and client
3448 * did not find it before sending create request,
3449 * then MDT will return -EREMOTE, and client will
3450 * retrieve the default stripeEA and re-create the
3453 * Note: if dah_eadata != NULL, it means creating the
3454 * striped directory with specified stripeEA, then it
3455 * should ignore the default stripeEA */
3456 if ((hint == NULL || hint->dah_eadata == NULL) &&
3457 lo->ldo_dir_stripe_offset != -1 &&
3458 lo->ldo_dir_stripe_offset != ss->ss_node_id)
3459 GOTO(out, rc = -EREMOTE);
3461 /* Orphan object (like migrating object) does not have
3462 * lod_dir_stripe, see lod_ah_init */
3463 if (lo->ldo_dir_stripe != NULL)
3464 rc = lod_declare_dir_striping_create(env, dt, attr,
3472 * Creation of a striped regular object.
3474 * The function is called to create the stripe objects for a regular
3475 * striped file. This can happen at the initial object creation or
3476 * when the caller asks LOD to do so using ->do_xattr_set() method
3477 * (so called late striping). Notice all the information are already
3478 * prepared in the form of the list of objects (ldo_stripe field).
3479 * This is done during declare phase.
3481 * \param[in] env execution environment
3482 * \param[in] dt object
3483 * \param[in] attr attributes the stripes will be created with
3484 * \param[in] dof format of stripes (see OSD API description)
3485 * \param[in] th transaction handle
3487 * \retval 0 on success
3488 * \retval negative if failed
3490 int lod_striping_create(const struct lu_env *env, struct dt_object *dt,
3491 struct lu_attr *attr, struct dt_object_format *dof,
3494 struct lod_object *lo = lod_dt_obj(dt);
3498 LASSERT(lo->ldo_striping_cached == 0);
3500 /* create all underlying objects */
3501 for (i = 0; i < lo->ldo_stripenr; i++) {
3502 LASSERT(lo->ldo_stripe[i]);
3503 rc = lod_sub_object_create(env, lo->ldo_stripe[i], attr, NULL,
3510 rc = lod_generate_and_set_lovea(env, lo, th);
3512 lo->ldo_striping_cached = 1;
3519 * Implementation of dt_object_operations::do_create.
3521 * If any of preceeding methods (like ->do_declare_create(),
3522 * ->do_ah_init(), etc) chose to create a striped object,
3523 * then this method will create the master and the stripes.
3525 * \see dt_object_operations::do_create() in the API description for details.
3527 static int lod_object_create(const struct lu_env *env, struct dt_object *dt,
3528 struct lu_attr *attr,
3529 struct dt_allocation_hint *hint,
3530 struct dt_object_format *dof, struct thandle *th)
3532 struct lod_object *lo = lod_dt_obj(dt);
3536 /* create local object */
3537 rc = lod_sub_object_create(env, dt_object_child(dt), attr, hint, dof,
3542 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3543 lo->ldo_stripe && dof->u.dof_reg.striped != 0)
3544 rc = lod_striping_create(env, dt, attr, dof, th);
3550 * Implementation of dt_object_operations::do_declare_destroy.
3552 * If the object is a striped directory, then the function declares reference
3553 * removal from the master object (this is an index) to the stripes and declares
3554 * destroy of all the stripes. In all the cases, it declares an intention to
3555 * destroy the object itself.
3557 * \see dt_object_operations::do_declare_destroy() in the API description
3560 static int lod_declare_object_destroy(const struct lu_env *env,
3561 struct dt_object *dt,
3564 struct dt_object *next = dt_object_child(dt);
3565 struct lod_object *lo = lod_dt_obj(dt);
3566 struct lod_thread_info *info = lod_env_info(env);
3567 char *stripe_name = info->lti_key;
3572 * load striping information, notice we don't do this when object
3573 * is being initialized as we don't need this information till
3574 * few specific cases like destroy, chown
3576 rc = lod_load_striping(env, lo);
3580 /* declare destroy for all underlying objects */
3581 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3582 rc = next->do_ops->do_index_try(env, next,
3583 &dt_directory_features);
3587 for (i = 0; i < lo->ldo_stripenr; i++) {
3588 rc = lod_sub_object_declare_ref_del(env, next, th);
3592 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3593 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
3595 rc = lod_sub_object_declare_delete(env, next,
3596 (const struct dt_key *)stripe_name, th);
3603 * we declare destroy for the local object
3605 rc = lod_sub_object_declare_destroy(env, next, th);
3609 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ))
3612 /* declare destroy all striped objects */
3613 for (i = 0; i < lo->ldo_stripenr; i++) {
3614 if (lo->ldo_stripe[i] == NULL)
3617 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
3618 rc = lod_sub_object_declare_ref_del(env,
3619 lo->ldo_stripe[i], th);
3621 rc = lod_sub_object_declare_destroy(env, lo->ldo_stripe[i],
3631 * Implementation of dt_object_operations::do_destroy.
3633 * If the object is a striped directory, then the function removes references
3634 * from the master object (this is an index) to the stripes and destroys all
3635 * the stripes. In all the cases, the function destroys the object itself.
3637 * \see dt_object_operations::do_destroy() in the API description for details.
3639 static int lod_object_destroy(const struct lu_env *env,
3640 struct dt_object *dt, struct thandle *th)
3642 struct dt_object *next = dt_object_child(dt);
3643 struct lod_object *lo = lod_dt_obj(dt);
3644 struct lod_thread_info *info = lod_env_info(env);
3645 char *stripe_name = info->lti_key;
3650 /* destroy sub-stripe of master object */
3651 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3652 rc = next->do_ops->do_index_try(env, next,
3653 &dt_directory_features);
3657 for (i = 0; i < lo->ldo_stripenr; i++) {
3658 rc = lod_sub_object_ref_del(env, next, th);
3662 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3663 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
3666 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
3667 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
3668 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
3670 rc = lod_sub_object_delete(env, next,
3671 (const struct dt_key *)stripe_name, th);
3677 rc = lod_sub_object_destroy(env, next, th);
3681 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ))
3684 /* destroy all striped objects */
3685 for (i = 0; i < lo->ldo_stripenr; i++) {
3686 if (likely(lo->ldo_stripe[i] != NULL) &&
3687 (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
3688 i == cfs_fail_val)) {
3689 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3690 dt_write_lock(env, lo->ldo_stripe[i],
3692 rc = lod_sub_object_ref_del(env,
3693 lo->ldo_stripe[i], th);
3694 dt_write_unlock(env, lo->ldo_stripe[i]);
3699 rc = lod_sub_object_destroy(env, lo->ldo_stripe[i], th);
3709 * Implementation of dt_object_operations::do_declare_ref_add.
3711 * \see dt_object_operations::do_declare_ref_add() in the API description
3714 static int lod_declare_ref_add(const struct lu_env *env,
3715 struct dt_object *dt, struct thandle *th)
3717 return lod_sub_object_declare_ref_add(env, dt_object_child(dt), th);
3721 * Implementation of dt_object_operations::do_ref_add.
3723 * \see dt_object_operations::do_ref_add() in the API description for details.
3725 static int lod_ref_add(const struct lu_env *env,
3726 struct dt_object *dt, struct thandle *th)
3728 return lod_sub_object_ref_add(env, dt_object_child(dt), th);
3732 * Implementation of dt_object_operations::do_declare_ref_del.
3734 * \see dt_object_operations::do_declare_ref_del() in the API description
3737 static int lod_declare_ref_del(const struct lu_env *env,
3738 struct dt_object *dt, struct thandle *th)
3740 return lod_sub_object_declare_ref_del(env, dt_object_child(dt), th);
3744 * Implementation of dt_object_operations::do_ref_del
3746 * \see dt_object_operations::do_ref_del() in the API description for details.
3748 static int lod_ref_del(const struct lu_env *env,
3749 struct dt_object *dt, struct thandle *th)
3751 return lod_sub_object_ref_del(env, dt_object_child(dt), th);
3755 * Implementation of dt_object_operations::do_object_sync.
3757 * \see dt_object_operations::do_object_sync() in the API description
3760 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
3761 __u64 start, __u64 end)
3763 return dt_object_sync(env, dt_object_child(dt), start, end);
3766 struct lod_slave_locks {
3768 struct lustre_handle lsl_handle[0];
3772 * Release LDLM locks on the stripes of a striped directory.
3774 * Iterates over all the locks taken on the stripe objects and
3775 * release them using ->do_object_unlock() method.
3777 * \param[in] env execution environment
3778 * \param[in] dt striped object
3779 * \param[in] einfo lock description
3780 * \param[in] policy data describing requested lock
3782 * \retval 0 on success
3783 * \retval negative if failed
3785 static int lod_object_unlock_internal(const struct lu_env *env,
3786 struct dt_object *dt,
3787 struct ldlm_enqueue_info *einfo,
3788 ldlm_policy_data_t *policy)
3790 struct lod_object *lo = lod_dt_obj(dt);
3791 struct lod_slave_locks *slave_locks = einfo->ei_cbdata;
3796 if (slave_locks == NULL)
3799 for (i = 1; i < slave_locks->lsl_lock_count; i++) {
3800 if (lustre_handle_is_used(&slave_locks->lsl_handle[i])) {
3803 einfo->ei_cbdata = &slave_locks->lsl_handle[i];
3804 rc1 = dt_object_unlock(env, lo->ldo_stripe[i], einfo,
3807 rc = rc == 0 ? rc1 : rc;
3815 * Implementation of dt_object_operations::do_object_unlock.
3817 * Used to release LDLM lock(s).
3819 * \see dt_object_operations::do_object_unlock() in the API description
3822 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
3823 struct ldlm_enqueue_info *einfo,
3824 union ldlm_policy_data *policy)
3826 struct lod_object *lo = lod_dt_obj(dt);
3827 struct lod_slave_locks *slave_locks = einfo->ei_cbdata;
3828 int slave_locks_size;
3832 if (slave_locks == NULL)
3835 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3838 rc = lod_load_striping(env, lo);
3842 /* Note: for remote lock for single stripe dir, MDT will cancel
3843 * the lock by lockh directly */
3844 if (lo->ldo_stripenr <= 1 && dt_object_remote(dt_object_child(dt)))
3847 /* Only cancel slave lock for striped dir */
3848 rc = lod_object_unlock_internal(env, dt, einfo, policy);
3850 slave_locks_size = sizeof(*slave_locks) + slave_locks->lsl_lock_count *
3851 sizeof(slave_locks->lsl_handle[0]);
3852 OBD_FREE(slave_locks, slave_locks_size);
3853 einfo->ei_cbdata = NULL;
3859 * Implementation of dt_object_operations::do_object_lock.
3861 * Used to get LDLM lock on the non-striped and striped objects.
3863 * \see dt_object_operations::do_object_lock() in the API description
3866 static int lod_object_lock(const struct lu_env *env,
3867 struct dt_object *dt,
3868 struct lustre_handle *lh,
3869 struct ldlm_enqueue_info *einfo,
3870 union ldlm_policy_data *policy)
3872 struct lod_object *lo = lod_dt_obj(dt);
3875 int slave_locks_size;
3876 struct lod_slave_locks *slave_locks = NULL;
3879 /* remote object lock */
3880 if (!einfo->ei_enq_slave) {
3881 LASSERT(dt_object_remote(dt));
3882 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
3886 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3889 rc = lod_load_striping(env, lo);
3894 if (lo->ldo_stripenr <= 1)
3897 slave_locks_size = sizeof(*slave_locks) + lo->ldo_stripenr *
3898 sizeof(slave_locks->lsl_handle[0]);
3899 /* Freed in lod_object_unlock */
3900 OBD_ALLOC(slave_locks, slave_locks_size);
3901 if (slave_locks == NULL)
3903 slave_locks->lsl_lock_count = lo->ldo_stripenr;
3905 /* striped directory lock */
3906 for (i = 1; i < lo->ldo_stripenr; i++) {
3907 struct lustre_handle lockh;
3908 struct ldlm_res_id *res_id;
3910 res_id = &lod_env_info(env)->lti_res_id;
3911 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
3913 einfo->ei_res_id = res_id;
3915 LASSERT(lo->ldo_stripe[i]);
3916 rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh, einfo,
3920 slave_locks->lsl_handle[i] = lockh;
3923 einfo->ei_cbdata = slave_locks;
3926 if (rc != 0 && slave_locks != NULL) {
3927 einfo->ei_cbdata = slave_locks;
3928 lod_object_unlock_internal(env, dt, einfo, policy);
3929 OBD_FREE(slave_locks, slave_locks_size);
3930 einfo->ei_cbdata = NULL;
3936 struct dt_object_operations lod_obj_ops = {
3937 .do_read_lock = lod_object_read_lock,
3938 .do_write_lock = lod_object_write_lock,
3939 .do_read_unlock = lod_object_read_unlock,
3940 .do_write_unlock = lod_object_write_unlock,
3941 .do_write_locked = lod_object_write_locked,
3942 .do_attr_get = lod_attr_get,
3943 .do_declare_attr_set = lod_declare_attr_set,
3944 .do_attr_set = lod_attr_set,
3945 .do_xattr_get = lod_xattr_get,
3946 .do_declare_xattr_set = lod_declare_xattr_set,
3947 .do_xattr_set = lod_xattr_set,
3948 .do_declare_xattr_del = lod_declare_xattr_del,
3949 .do_xattr_del = lod_xattr_del,
3950 .do_xattr_list = lod_xattr_list,
3951 .do_ah_init = lod_ah_init,
3952 .do_declare_create = lod_declare_object_create,
3953 .do_create = lod_object_create,
3954 .do_declare_destroy = lod_declare_object_destroy,
3955 .do_destroy = lod_object_destroy,
3956 .do_index_try = lod_index_try,
3957 .do_declare_ref_add = lod_declare_ref_add,
3958 .do_ref_add = lod_ref_add,
3959 .do_declare_ref_del = lod_declare_ref_del,
3960 .do_ref_del = lod_ref_del,
3961 .do_object_sync = lod_object_sync,
3962 .do_object_lock = lod_object_lock,
3963 .do_object_unlock = lod_object_unlock,
3967 * Implementation of dt_body_operations::dbo_read.
3969 * \see dt_body_operations::dbo_read() in the API description for details.
3971 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
3972 struct lu_buf *buf, loff_t *pos)
3974 struct dt_object *next = dt_object_child(dt);
3975 return next->do_body_ops->dbo_read(env, next, buf, pos);
3979 * Implementation of dt_body_operations::dbo_declare_write.
3981 * \see dt_body_operations::dbo_declare_write() in the API description
3984 static ssize_t lod_declare_write(const struct lu_env *env,
3985 struct dt_object *dt,
3986 const struct lu_buf *buf, loff_t pos,
3989 return lod_sub_object_declare_write(env, dt_object_child(dt), buf, pos,
3994 * Implementation of dt_body_operations::dbo_write.
3996 * \see dt_body_operations::dbo_write() in the API description for details.
3998 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
3999 const struct lu_buf *buf, loff_t *pos,
4000 struct thandle *th, int iq)
4002 return lod_sub_object_write(env, dt_object_child(dt), buf, pos, th, iq);
4005 static const struct dt_body_operations lod_body_lnk_ops = {
4006 .dbo_read = lod_read,
4007 .dbo_declare_write = lod_declare_write,
4008 .dbo_write = lod_write
4012 * Implementation of lu_object_operations::loo_object_init.
4014 * The function determines the type and the index of the target device using
4015 * sequence of the object's FID. Then passes control down to the
4016 * corresponding device:
4017 * OSD for the local objects, OSP for remote
4019 * \see lu_object_operations::loo_object_init() in the API description
4022 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
4023 const struct lu_object_conf *conf)
4025 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
4026 struct lu_device *cdev = NULL;
4027 struct lu_object *cobj;
4028 struct lod_tgt_descs *ltd = NULL;
4029 struct lod_tgt_desc *tgt;
4031 int type = LU_SEQ_RANGE_ANY;
4035 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
4037 /* Note: Sometimes, it will Return EAGAIN here, see
4038 * ptrlpc_import_delay_req(), which might confuse
4039 * lu_object_find_at() and make it wait there incorrectly.
4040 * so we convert it to EIO here.*/
4047 if (type == LU_SEQ_RANGE_MDT &&
4048 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
4049 cdev = &lod->lod_child->dd_lu_dev;
4050 } else if (type == LU_SEQ_RANGE_MDT) {
4051 ltd = &lod->lod_mdt_descs;
4053 } else if (type == LU_SEQ_RANGE_OST) {
4054 ltd = &lod->lod_ost_descs;
4061 if (ltd->ltd_tgts_size > idx &&
4062 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
4063 tgt = LTD_TGT(ltd, idx);
4065 LASSERT(tgt != NULL);
4066 LASSERT(tgt->ltd_tgt != NULL);
4068 cdev = &(tgt->ltd_tgt->dd_lu_dev);
4070 lod_putref(lod, ltd);
4073 if (unlikely(cdev == NULL))
4076 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
4077 if (unlikely(cobj == NULL))
4080 lu_object_add(lo, cobj);
4087 * Release resources associated with striping.
4089 * If the object is striped (regular or directory), then release
4090 * the stripe objects references and free the ldo_stripe array.
4092 * \param[in] env execution environment
4093 * \param[in] lo object
4095 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
4099 if (lo->ldo_dir_stripe != NULL) {
4100 OBD_FREE_PTR(lo->ldo_dir_stripe);
4101 lo->ldo_dir_stripe = NULL;
4104 if (lo->ldo_stripe) {
4105 LASSERT(lo->ldo_stripes_allocated > 0);
4107 for (i = 0; i < lo->ldo_stripenr; i++) {
4108 if (lo->ldo_stripe[i])
4109 lu_object_put(env, &lo->ldo_stripe[i]->do_lu);
4112 i = sizeof(struct dt_object *) * lo->ldo_stripes_allocated;
4113 OBD_FREE(lo->ldo_stripe, i);
4114 lo->ldo_stripe = NULL;
4115 lo->ldo_stripes_allocated = 0;
4117 lo->ldo_striping_cached = 0;
4118 lo->ldo_stripenr = 0;
4119 lo->ldo_pattern = 0;
4123 * Implementation of lu_object_operations::loo_object_start.
4125 * \see lu_object_operations::loo_object_start() in the API description
4128 static int lod_object_start(const struct lu_env *env, struct lu_object *o)
4130 if (S_ISLNK(o->lo_header->loh_attr & S_IFMT))
4131 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_lnk_ops;
4136 * Implementation of lu_object_operations::loo_object_free.
4138 * \see lu_object_operations::loo_object_free() in the API description
4141 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
4143 struct lod_object *mo = lu2lod_obj(o);
4146 * release all underlying object pinned
4149 lod_object_free_striping(env, mo);
4151 lod_object_set_pool(mo, NULL);
4154 OBD_SLAB_FREE_PTR(mo, lod_object_kmem);
4158 * Implementation of lu_object_operations::loo_object_release.
4160 * \see lu_object_operations::loo_object_release() in the API description
4163 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
4165 /* XXX: shouldn't we release everything here in case if object
4166 * creation failed before? */
4170 * Implementation of lu_object_operations::loo_object_print.
4172 * \see lu_object_operations::loo_object_print() in the API description
4175 static int lod_object_print(const struct lu_env *env, void *cookie,
4176 lu_printer_t p, const struct lu_object *l)
4178 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
4180 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
4183 struct lu_object_operations lod_lu_obj_ops = {
4184 .loo_object_init = lod_object_init,
4185 .loo_object_start = lod_object_start,
4186 .loo_object_free = lod_object_free,
4187 .loo_object_release = lod_object_release,
4188 .loo_object_print = lod_object_print,