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,
89 struct thandle *handle)
91 return dt_declare_insert(env, dt_object_child(dt), rec, key, handle);
95 * Implementation of dt_index_operations::dio_insert.
97 * Used with regular (non-striped) objects
99 * \see dt_index_operations::dio_insert() in the API description for details.
101 static int lod_index_insert(const struct lu_env *env,
102 struct dt_object *dt,
103 const struct dt_rec *rec,
104 const struct dt_key *key,
108 return dt_insert(env, dt_object_child(dt), rec, key, th, ign);
112 * Implementation of dt_index_operations::dio_declare_delete.
114 * Used with regular (non-striped) objects.
116 * \see dt_index_operations::dio_declare_delete() in the API description
119 static int lod_declare_index_delete(const struct lu_env *env,
120 struct dt_object *dt,
121 const struct dt_key *key,
124 return dt_declare_delete(env, dt_object_child(dt), key, th);
128 * Implementation of dt_index_operations::dio_delete.
130 * Used with regular (non-striped) objects.
132 * \see dt_index_operations::dio_delete() in the API description for details.
134 static int lod_index_delete(const struct lu_env *env,
135 struct dt_object *dt,
136 const struct dt_key *key,
139 return dt_delete(env, dt_object_child(dt), key, th);
143 * Implementation of dt_it_ops::init.
145 * Used with regular (non-striped) objects.
147 * \see dt_it_ops::init() in the API description for details.
149 static struct dt_it *lod_it_init(const struct lu_env *env,
150 struct dt_object *dt, __u32 attr)
152 struct dt_object *next = dt_object_child(dt);
153 struct lod_it *it = &lod_env_info(env)->lti_it;
154 struct dt_it *it_next;
157 it_next = next->do_index_ops->dio_it.init(env, next, attr);
161 /* currently we do not use more than one iterator per thread
162 * so we store it in thread info. if at some point we need
163 * more active iterators in a single thread, we can allocate
165 LASSERT(it->lit_obj == NULL);
167 it->lit_it = it_next;
170 return (struct dt_it *)it;
173 #define LOD_CHECK_IT(env, it) \
175 LASSERT((it)->lit_obj != NULL); \
176 LASSERT((it)->lit_it != NULL); \
180 * Implementation of dt_index_operations::dio_it.fini.
182 * Used with regular (non-striped) objects.
184 * \see dt_index_operations::dio_it.fini() in the API description for details.
186 static void lod_it_fini(const struct lu_env *env, struct dt_it *di)
188 struct lod_it *it = (struct lod_it *)di;
190 LOD_CHECK_IT(env, it);
191 it->lit_obj->do_index_ops->dio_it.fini(env, it->lit_it);
193 /* the iterator not in use any more */
199 * Implementation of dt_it_ops::get.
201 * Used with regular (non-striped) objects.
203 * \see dt_it_ops::get() in the API description for details.
205 static int lod_it_get(const struct lu_env *env, struct dt_it *di,
206 const struct dt_key *key)
208 const struct lod_it *it = (const struct lod_it *)di;
210 LOD_CHECK_IT(env, it);
211 return it->lit_obj->do_index_ops->dio_it.get(env, it->lit_it, key);
215 * Implementation of dt_it_ops::put.
217 * Used with regular (non-striped) objects.
219 * \see dt_it_ops::put() in the API description for details.
221 static void lod_it_put(const struct lu_env *env, struct dt_it *di)
223 struct lod_it *it = (struct lod_it *)di;
225 LOD_CHECK_IT(env, it);
226 return it->lit_obj->do_index_ops->dio_it.put(env, it->lit_it);
230 * Implementation of dt_it_ops::next.
232 * Used with regular (non-striped) objects
234 * \see dt_it_ops::next() in the API description for details.
236 static int lod_it_next(const struct lu_env *env, struct dt_it *di)
238 struct lod_it *it = (struct lod_it *)di;
240 LOD_CHECK_IT(env, it);
241 return it->lit_obj->do_index_ops->dio_it.next(env, it->lit_it);
245 * Implementation of dt_it_ops::key.
247 * Used with regular (non-striped) objects.
249 * \see dt_it_ops::key() in the API description for details.
251 static struct dt_key *lod_it_key(const struct lu_env *env,
252 const struct dt_it *di)
254 const struct lod_it *it = (const struct lod_it *)di;
256 LOD_CHECK_IT(env, it);
257 return it->lit_obj->do_index_ops->dio_it.key(env, it->lit_it);
261 * Implementation of dt_it_ops::key_size.
263 * Used with regular (non-striped) objects.
265 * \see dt_it_ops::key_size() in the API description for details.
267 static int lod_it_key_size(const struct lu_env *env, const struct dt_it *di)
269 struct lod_it *it = (struct lod_it *)di;
271 LOD_CHECK_IT(env, it);
272 return it->lit_obj->do_index_ops->dio_it.key_size(env, it->lit_it);
276 * Implementation of dt_it_ops::rec.
278 * Used with regular (non-striped) objects.
280 * \see dt_it_ops::rec() in the API description for details.
282 static int lod_it_rec(const struct lu_env *env, const struct dt_it *di,
283 struct dt_rec *rec, __u32 attr)
285 const struct lod_it *it = (const struct lod_it *)di;
287 LOD_CHECK_IT(env, it);
288 return it->lit_obj->do_index_ops->dio_it.rec(env, it->lit_it, rec,
293 * Implementation of dt_it_ops::rec_size.
295 * Used with regular (non-striped) objects.
297 * \see dt_it_ops::rec_size() in the API description for details.
299 static int lod_it_rec_size(const struct lu_env *env, const struct dt_it *di,
302 const struct lod_it *it = (const struct lod_it *)di;
304 LOD_CHECK_IT(env, it);
305 return it->lit_obj->do_index_ops->dio_it.rec_size(env, it->lit_it,
310 * Implementation of dt_it_ops::store.
312 * Used with regular (non-striped) objects.
314 * \see dt_it_ops::store() in the API description for details.
316 static __u64 lod_it_store(const struct lu_env *env, const struct dt_it *di)
318 const struct lod_it *it = (const struct lod_it *)di;
320 LOD_CHECK_IT(env, it);
321 return it->lit_obj->do_index_ops->dio_it.store(env, it->lit_it);
325 * Implementation of dt_it_ops::load.
327 * Used with regular (non-striped) objects.
329 * \see dt_it_ops::load() in the API description for details.
331 static int lod_it_load(const struct lu_env *env, const struct dt_it *di,
334 const struct lod_it *it = (const struct lod_it *)di;
336 LOD_CHECK_IT(env, it);
337 return it->lit_obj->do_index_ops->dio_it.load(env, it->lit_it, hash);
341 * Implementation of dt_it_ops::key_rec.
343 * Used with regular (non-striped) objects.
345 * \see dt_it_ops::rec() in the API description for details.
347 static int lod_it_key_rec(const struct lu_env *env, const struct dt_it *di,
350 const struct lod_it *it = (const struct lod_it *)di;
352 LOD_CHECK_IT(env, it);
353 return it->lit_obj->do_index_ops->dio_it.key_rec(env, it->lit_it,
357 static struct dt_index_operations lod_index_ops = {
358 .dio_lookup = lod_index_lookup,
359 .dio_declare_insert = lod_declare_index_insert,
360 .dio_insert = lod_index_insert,
361 .dio_declare_delete = lod_declare_index_delete,
362 .dio_delete = lod_index_delete,
370 .key_size = lod_it_key_size,
372 .rec_size = lod_it_rec_size,
373 .store = lod_it_store,
375 .key_rec = lod_it_key_rec,
380 * Implementation of dt_it_ops::init.
382 * Used with striped objects. Internally just initializes the iterator
383 * on the first stripe.
385 * \see dt_it_ops::init() in the API description for details.
387 static struct dt_it *lod_striped_it_init(const struct lu_env *env,
388 struct dt_object *dt, __u32 attr)
390 struct lod_object *lo = lod_dt_obj(dt);
391 struct dt_object *next;
392 struct lod_it *it = &lod_env_info(env)->lti_it;
393 struct dt_it *it_next;
396 LASSERT(lo->ldo_stripenr > 0);
397 next = lo->ldo_stripe[0];
398 LASSERT(next != NULL);
399 LASSERT(next->do_index_ops != NULL);
401 it_next = next->do_index_ops->dio_it.init(env, next, attr);
405 /* currently we do not use more than one iterator per thread
406 * so we store it in thread info. if at some point we need
407 * more active iterators in a single thread, we can allocate
409 LASSERT(it->lit_obj == NULL);
411 it->lit_stripe_index = 0;
413 it->lit_it = it_next;
416 return (struct dt_it *)it;
419 #define LOD_CHECK_STRIPED_IT(env, it, lo) \
421 LASSERT((it)->lit_obj != NULL); \
422 LASSERT((it)->lit_it != NULL); \
423 LASSERT((lo)->ldo_stripenr > 0); \
424 LASSERT((it)->lit_stripe_index < (lo)->ldo_stripenr); \
428 * Implementation of dt_it_ops::fini.
430 * Used with striped objects.
432 * \see dt_it_ops::fini() in the API description for details.
434 static void lod_striped_it_fini(const struct lu_env *env, struct dt_it *di)
436 struct lod_it *it = (struct lod_it *)di;
437 struct lod_object *lo = lod_dt_obj(it->lit_obj);
438 struct dt_object *next;
440 LOD_CHECK_STRIPED_IT(env, it, lo);
442 next = lo->ldo_stripe[it->lit_stripe_index];
443 LASSERT(next != NULL);
444 LASSERT(next->do_index_ops != NULL);
446 next->do_index_ops->dio_it.fini(env, it->lit_it);
448 /* the iterator not in use any more */
451 it->lit_stripe_index = 0;
455 * Implementation of dt_it_ops::get.
457 * Right now it's not used widely, only to reset the iterator to the
458 * initial position. It should be possible to implement a full version
459 * which chooses a correct stripe to be able to position with any key.
461 * \see dt_it_ops::get() in the API description for details.
463 static int lod_striped_it_get(const struct lu_env *env, struct dt_it *di,
464 const struct dt_key *key)
466 const struct lod_it *it = (const struct lod_it *)di;
467 struct lod_object *lo = lod_dt_obj(it->lit_obj);
468 struct dt_object *next;
471 LOD_CHECK_STRIPED_IT(env, it, lo);
473 next = lo->ldo_stripe[it->lit_stripe_index];
474 LASSERT(next != NULL);
475 LASSERT(next->do_index_ops != NULL);
477 return next->do_index_ops->dio_it.get(env, it->lit_it, key);
481 * Implementation of dt_it_ops::put.
483 * Used with striped objects.
485 * \see dt_it_ops::put() in the API description for details.
487 static void lod_striped_it_put(const struct lu_env *env, struct dt_it *di)
489 struct lod_it *it = (struct lod_it *)di;
490 struct lod_object *lo = lod_dt_obj(it->lit_obj);
491 struct dt_object *next;
493 LOD_CHECK_STRIPED_IT(env, it, lo);
495 next = lo->ldo_stripe[it->lit_stripe_index];
496 LASSERT(next != NULL);
497 LASSERT(next->do_index_ops != NULL);
499 return next->do_index_ops->dio_it.put(env, it->lit_it);
503 * Implementation of dt_it_ops::next.
505 * Used with striped objects. When the end of the current stripe is
506 * reached, the method takes the next stripe's iterator.
508 * \see dt_it_ops::next() in the API description for details.
510 static int lod_striped_it_next(const struct lu_env *env, struct dt_it *di)
512 struct lod_it *it = (struct lod_it *)di;
513 struct lod_object *lo = lod_dt_obj(it->lit_obj);
514 struct dt_object *next;
515 struct dt_it *it_next;
519 LOD_CHECK_STRIPED_IT(env, it, lo);
521 next = lo->ldo_stripe[it->lit_stripe_index];
522 LASSERT(next != NULL);
523 LASSERT(next->do_index_ops != NULL);
525 rc = next->do_index_ops->dio_it.next(env, it->lit_it);
529 if (rc == 0 && it->lit_stripe_index == 0)
532 if (rc == 0 && it->lit_stripe_index > 0) {
533 struct lu_dirent *ent;
535 ent = (struct lu_dirent *)lod_env_info(env)->lti_key;
537 rc = next->do_index_ops->dio_it.rec(env, it->lit_it,
538 (struct dt_rec *)ent,
543 /* skip . and .. for slave stripe */
544 if ((strncmp(ent->lde_name, ".",
545 le16_to_cpu(ent->lde_namelen)) == 0 &&
546 le16_to_cpu(ent->lde_namelen) == 1) ||
547 (strncmp(ent->lde_name, "..",
548 le16_to_cpu(ent->lde_namelen)) == 0 &&
549 le16_to_cpu(ent->lde_namelen) == 2))
555 /* go to next stripe */
556 if (it->lit_stripe_index + 1 >= lo->ldo_stripenr)
559 it->lit_stripe_index++;
561 next->do_index_ops->dio_it.put(env, it->lit_it);
562 next->do_index_ops->dio_it.fini(env, it->lit_it);
564 rc = next->do_ops->do_index_try(env, next, &dt_directory_features);
568 next = lo->ldo_stripe[it->lit_stripe_index];
569 LASSERT(next != NULL);
570 LASSERT(next->do_index_ops != NULL);
572 it_next = next->do_index_ops->dio_it.init(env, next, it->lit_attr);
573 if (!IS_ERR(it_next)) {
574 it->lit_it = it_next;
577 rc = PTR_ERR(it_next);
584 * Implementation of dt_it_ops::key.
586 * Used with striped objects.
588 * \see dt_it_ops::key() in the API description for details.
590 static struct dt_key *lod_striped_it_key(const struct lu_env *env,
591 const struct dt_it *di)
593 const struct lod_it *it = (const struct lod_it *)di;
594 struct lod_object *lo = lod_dt_obj(it->lit_obj);
595 struct dt_object *next;
597 LOD_CHECK_STRIPED_IT(env, it, lo);
599 next = lo->ldo_stripe[it->lit_stripe_index];
600 LASSERT(next != NULL);
601 LASSERT(next->do_index_ops != NULL);
603 return next->do_index_ops->dio_it.key(env, it->lit_it);
607 * Implementation of dt_it_ops::key_size.
609 * Used with striped objects.
611 * \see dt_it_ops::size() in the API description for details.
613 static int lod_striped_it_key_size(const struct lu_env *env,
614 const struct dt_it *di)
616 struct lod_it *it = (struct lod_it *)di;
617 struct lod_object *lo = lod_dt_obj(it->lit_obj);
618 struct dt_object *next;
620 LOD_CHECK_STRIPED_IT(env, it, lo);
622 next = lo->ldo_stripe[it->lit_stripe_index];
623 LASSERT(next != NULL);
624 LASSERT(next->do_index_ops != NULL);
626 return next->do_index_ops->dio_it.key_size(env, it->lit_it);
630 * Implementation of dt_it_ops::rec.
632 * Used with striped objects.
634 * \see dt_it_ops::rec() in the API description for details.
636 static int lod_striped_it_rec(const struct lu_env *env, const struct dt_it *di,
637 struct dt_rec *rec, __u32 attr)
639 const struct lod_it *it = (const struct lod_it *)di;
640 struct lod_object *lo = lod_dt_obj(it->lit_obj);
641 struct dt_object *next;
643 LOD_CHECK_STRIPED_IT(env, it, lo);
645 next = lo->ldo_stripe[it->lit_stripe_index];
646 LASSERT(next != NULL);
647 LASSERT(next->do_index_ops != NULL);
649 return next->do_index_ops->dio_it.rec(env, it->lit_it, rec, attr);
653 * Implementation of dt_it_ops::rec_size.
655 * Used with striped objects.
657 * \see dt_it_ops::rec_size() in the API description for details.
659 static int lod_striped_it_rec_size(const struct lu_env *env,
660 const struct dt_it *di, __u32 attr)
662 struct lod_it *it = (struct lod_it *)di;
663 struct lod_object *lo = lod_dt_obj(it->lit_obj);
664 struct dt_object *next;
666 LOD_CHECK_STRIPED_IT(env, it, lo);
668 next = lo->ldo_stripe[it->lit_stripe_index];
669 LASSERT(next != NULL);
670 LASSERT(next->do_index_ops != NULL);
672 return next->do_index_ops->dio_it.rec_size(env, it->lit_it, attr);
676 * Implementation of dt_it_ops::store.
678 * Used with striped objects.
680 * \see dt_it_ops::store() in the API description for details.
682 static __u64 lod_striped_it_store(const struct lu_env *env,
683 const struct dt_it *di)
685 const struct lod_it *it = (const struct lod_it *)di;
686 struct lod_object *lo = lod_dt_obj(it->lit_obj);
687 struct dt_object *next;
689 LOD_CHECK_STRIPED_IT(env, it, lo);
691 next = lo->ldo_stripe[it->lit_stripe_index];
692 LASSERT(next != NULL);
693 LASSERT(next->do_index_ops != NULL);
695 return next->do_index_ops->dio_it.store(env, it->lit_it);
699 * Implementation of dt_it_ops::load.
701 * Used with striped objects.
703 * \see dt_it_ops::load() in the API description for details.
705 static int lod_striped_it_load(const struct lu_env *env,
706 const struct dt_it *di, __u64 hash)
708 const struct lod_it *it = (const struct lod_it *)di;
709 struct lod_object *lo = lod_dt_obj(it->lit_obj);
710 struct dt_object *next;
712 LOD_CHECK_STRIPED_IT(env, it, lo);
714 next = lo->ldo_stripe[it->lit_stripe_index];
715 LASSERT(next != NULL);
716 LASSERT(next->do_index_ops != NULL);
718 return next->do_index_ops->dio_it.load(env, it->lit_it, hash);
721 static struct dt_index_operations lod_striped_index_ops = {
722 .dio_lookup = lod_index_lookup,
723 .dio_declare_insert = lod_declare_index_insert,
724 .dio_insert = lod_index_insert,
725 .dio_declare_delete = lod_declare_index_delete,
726 .dio_delete = lod_index_delete,
728 .init = lod_striped_it_init,
729 .fini = lod_striped_it_fini,
730 .get = lod_striped_it_get,
731 .put = lod_striped_it_put,
732 .next = lod_striped_it_next,
733 .key = lod_striped_it_key,
734 .key_size = lod_striped_it_key_size,
735 .rec = lod_striped_it_rec,
736 .rec_size = lod_striped_it_rec_size,
737 .store = lod_striped_it_store,
738 .load = lod_striped_it_load,
743 * Append the FID for each shard of the striped directory after the
744 * given LMV EA header.
746 * To simplify striped directory and the consistency verification,
747 * we only store the LMV EA header on disk, for both master object
748 * and slave objects. When someone wants to know the whole LMV EA,
749 * such as client readdir(), we can build the entrie LMV EA on the
750 * MDT side (in RAM) via iterating the sub-directory entries that
751 * are contained in the master object of the stripe directory.
753 * For the master object of the striped directroy, the valid name
754 * for each shard is composed of the ${shard_FID}:${shard_idx}.
756 * There may be holes in the LMV EA if some shards' name entries
757 * are corrupted or lost.
759 * \param[in] env pointer to the thread context
760 * \param[in] lo pointer to the master object of the striped directory
761 * \param[in] buf pointer to the lu_buf which will hold the LMV EA
762 * \param[in] resize whether re-allocate the buffer if it is not big enough
764 * \retval positive size of the LMV EA
765 * \retval 0 for nothing to be loaded
766 * \retval negative error number on failure
768 int lod_load_lmv_shards(const struct lu_env *env, struct lod_object *lo,
769 struct lu_buf *buf, bool resize)
771 struct lu_dirent *ent =
772 (struct lu_dirent *)lod_env_info(env)->lti_key;
773 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
774 struct dt_object *obj = dt_object_child(&lo->ldo_obj);
775 struct lmv_mds_md_v1 *lmv1 = buf->lb_buf;
777 const struct dt_it_ops *iops;
779 __u32 magic = le32_to_cpu(lmv1->lmv_magic);
784 /* If it is not a striped directory, then load nothing. */
785 if (magic != LMV_MAGIC_V1)
788 /* If it is in migration (or failure), then load nothing. */
789 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
792 stripes = le32_to_cpu(lmv1->lmv_stripe_count);
796 rc = lmv_mds_md_size(stripes, magic);
800 if (buf->lb_len < lmv1_size) {
809 lu_buf_alloc(buf, lmv1_size);
814 memcpy(buf->lb_buf, tbuf.lb_buf, tbuf.lb_len);
817 if (unlikely(!dt_try_as_dir(env, obj)))
820 memset(&lmv1->lmv_stripe_fids[0], 0, stripes * sizeof(struct lu_fid));
821 iops = &obj->do_index_ops->dio_it;
822 it = iops->init(env, obj, LUDA_64BITHASH);
826 rc = iops->load(env, it, 0);
828 rc = iops->next(env, it);
833 char name[FID_LEN + 2] = "";
838 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
844 fid_le_to_cpu(&fid, &ent->lde_fid);
845 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
846 if (ent->lde_name[0] == '.') {
847 if (ent->lde_namelen == 1)
850 if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
854 len = snprintf(name, FID_LEN + 1, DFID":", PFID(&ent->lde_fid));
855 /* The ent->lde_name is composed of ${FID}:${index} */
856 if (ent->lde_namelen < len + 1 ||
857 memcmp(ent->lde_name, name, len) != 0) {
858 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
859 "%s: invalid shard name %.*s with the FID "DFID
860 " for the striped directory "DFID", %s\n",
861 lod2obd(lod)->obd_name, ent->lde_namelen,
862 ent->lde_name, PFID(&fid),
863 PFID(lu_object_fid(&obj->do_lu)),
864 lod->lod_lmv_failout ? "failout" : "skip");
866 if (lod->lod_lmv_failout)
874 if (ent->lde_name[len] < '0' ||
875 ent->lde_name[len] > '9') {
876 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
877 "%s: invalid shard name %.*s with the "
878 "FID "DFID" for the striped directory "
880 lod2obd(lod)->obd_name, ent->lde_namelen,
881 ent->lde_name, PFID(&fid),
882 PFID(lu_object_fid(&obj->do_lu)),
883 lod->lod_lmv_failout ?
886 if (lod->lod_lmv_failout)
892 index = index * 10 + ent->lde_name[len++] - '0';
893 } while (len < ent->lde_namelen);
895 if (len == ent->lde_namelen) {
896 /* Out of LMV EA range. */
897 if (index >= stripes) {
898 CERROR("%s: the shard %.*s for the striped "
899 "directory "DFID" is out of the known "
900 "LMV EA range [0 - %u], failout\n",
901 lod2obd(lod)->obd_name, ent->lde_namelen,
903 PFID(lu_object_fid(&obj->do_lu)),
909 /* The slot has been occupied. */
910 if (!fid_is_zero(&lmv1->lmv_stripe_fids[index])) {
914 &lmv1->lmv_stripe_fids[index]);
915 CERROR("%s: both the shard "DFID" and "DFID
916 " for the striped directory "DFID
917 " claim the same LMV EA slot at the "
918 "index %d, failout\n",
919 lod2obd(lod)->obd_name,
920 PFID(&fid0), PFID(&fid),
921 PFID(lu_object_fid(&obj->do_lu)), index);
926 /* stored as LE mode */
927 lmv1->lmv_stripe_fids[index] = ent->lde_fid;
930 rc = iops->next(env, it);
937 RETURN(rc > 0 ? lmv_mds_md_size(stripes, magic) : rc);
941 * Implementation of dt_object_operations::do_index_try.
943 * \see dt_object_operations::do_index_try() in the API description for details.
945 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
946 const struct dt_index_features *feat)
948 struct lod_object *lo = lod_dt_obj(dt);
949 struct dt_object *next = dt_object_child(dt);
953 LASSERT(next->do_ops);
954 LASSERT(next->do_ops->do_index_try);
956 rc = lod_load_striping_locked(env, lo);
960 rc = next->do_ops->do_index_try(env, next, feat);
964 if (lo->ldo_stripenr > 0) {
967 for (i = 0; i < lo->ldo_stripenr; i++) {
968 if (dt_object_exists(lo->ldo_stripe[i]) == 0)
970 rc = lo->ldo_stripe[i]->do_ops->do_index_try(env,
971 lo->ldo_stripe[i], feat);
975 dt->do_index_ops = &lod_striped_index_ops;
977 dt->do_index_ops = &lod_index_ops;
984 * Implementation of dt_object_operations::do_read_lock.
986 * \see dt_object_operations::do_read_lock() in the API description for details.
988 static void lod_object_read_lock(const struct lu_env *env,
989 struct dt_object *dt, unsigned role)
991 dt_read_lock(env, dt_object_child(dt), role);
995 * Implementation of dt_object_operations::do_write_lock.
997 * \see dt_object_operations::do_write_lock() in the API description for
1000 static void lod_object_write_lock(const struct lu_env *env,
1001 struct dt_object *dt, unsigned role)
1003 dt_write_lock(env, dt_object_child(dt), role);
1007 * Implementation of dt_object_operations::do_read_unlock.
1009 * \see dt_object_operations::do_read_unlock() in the API description for
1012 static void lod_object_read_unlock(const struct lu_env *env,
1013 struct dt_object *dt)
1015 dt_read_unlock(env, dt_object_child(dt));
1019 * Implementation of dt_object_operations::do_write_unlock.
1021 * \see dt_object_operations::do_write_unlock() in the API description for
1024 static void lod_object_write_unlock(const struct lu_env *env,
1025 struct dt_object *dt)
1027 dt_write_unlock(env, dt_object_child(dt));
1031 * Implementation of dt_object_operations::do_write_locked.
1033 * \see dt_object_operations::do_write_locked() in the API description for
1036 static int lod_object_write_locked(const struct lu_env *env,
1037 struct dt_object *dt)
1039 return dt_write_locked(env, dt_object_child(dt));
1043 * Implementation of dt_object_operations::do_attr_get.
1045 * \see dt_object_operations::do_attr_get() in the API description for details.
1047 static int lod_attr_get(const struct lu_env *env,
1048 struct dt_object *dt,
1049 struct lu_attr *attr)
1051 /* Note: for striped directory, client will merge attributes
1052 * from all of the sub-stripes see lmv_merge_attr(), and there
1053 * no MDD logic depend on directory nlink/size/time, so we can
1054 * always use master inode nlink and size for now. */
1055 return dt_attr_get(env, dt_object_child(dt), attr);
1059 * Mark all of the striped directory sub-stripes dead.
1061 * When a striped object is a subject to removal, we have
1062 * to mark all the stripes to prevent further access to
1063 * them (e.g. create a new file in those). So we mark
1064 * all the stripes with LMV_HASH_FLAG_DEAD. The function
1065 * can be used to declare the changes and to apply them.
1066 * If the object isn't striped, then just return success.
1068 * \param[in] env execution environment
1069 * \param[in] dt the striped object
1070 * \param[in] handle transaction handle
1071 * \param[in] declare whether to declare the change or apply
1073 * \retval 0 on success
1074 * \retval negative if failed
1076 static int lod_mark_dead_object(const struct lu_env *env,
1077 struct dt_object *dt,
1078 struct thandle *handle,
1081 struct lod_object *lo = lod_dt_obj(dt);
1082 struct lmv_mds_md_v1 *lmv;
1083 __u32 dead_hash_type;
1089 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
1092 rc = lod_load_striping_locked(env, lo);
1096 if (lo->ldo_stripenr == 0)
1099 rc = lod_get_lmv_ea(env, lo);
1103 lmv = lod_env_info(env)->lti_ea_store;
1104 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1105 dead_hash_type = le32_to_cpu(lmv->lmv_hash_type) | LMV_HASH_FLAG_DEAD;
1106 lmv->lmv_hash_type = cpu_to_le32(dead_hash_type);
1107 for (i = 0; i < lo->ldo_stripenr; i++) {
1110 lmv->lmv_master_mdt_index = i;
1112 buf.lb_len = sizeof(*lmv);
1114 rc = dt_declare_xattr_set(env, lo->ldo_stripe[i], &buf,
1116 LU_XATTR_REPLACE, handle);
1118 rc = dt_xattr_set(env, lo->ldo_stripe[i], &buf,
1119 XATTR_NAME_LMV, LU_XATTR_REPLACE,
1130 * Implementation of dt_object_operations::do_declare_attr_set.
1132 * If the object is striped, then apply the changes to all the stripes.
1134 * \see dt_object_operations::do_declare_attr_set() in the API description
1137 static int lod_declare_attr_set(const struct lu_env *env,
1138 struct dt_object *dt,
1139 const struct lu_attr *attr,
1140 struct thandle *handle)
1142 struct dt_object *next = dt_object_child(dt);
1143 struct lod_object *lo = lod_dt_obj(dt);
1147 /* Set dead object on all other stripes */
1148 if (attr->la_valid & LA_FLAGS && !(attr->la_valid & ~LA_FLAGS) &&
1149 attr->la_flags & LUSTRE_SLAVE_DEAD_FL) {
1150 rc = lod_mark_dead_object(env, dt, handle, true);
1155 * declare setattr on the local object
1157 rc = dt_declare_attr_set(env, next, attr, handle);
1161 /* osp_declare_attr_set() ignores all attributes other than
1162 * UID, GID, and size, and osp_attr_set() ignores all but UID
1163 * and GID. Declaration of size attr setting happens through
1164 * lod_declare_init_size(), and not through this function.
1165 * Therefore we need not load striping unless ownership is
1166 * changing. This should save memory and (we hope) speed up
1168 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1169 if (!(attr->la_valid & (LA_UID | LA_GID)))
1172 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1175 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE |
1176 LA_ATIME | LA_MTIME | LA_CTIME)))
1180 * load striping information, notice we don't do this when object
1181 * is being initialized as we don't need this information till
1182 * few specific cases like destroy, chown
1184 rc = lod_load_striping(env, lo);
1188 if (lo->ldo_stripenr == 0)
1192 * if object is striped declare changes on the stripes
1194 LASSERT(lo->ldo_stripe);
1195 for (i = 0; i < lo->ldo_stripenr; i++) {
1196 if (likely(lo->ldo_stripe[i] != NULL)) {
1197 rc = dt_declare_attr_set(env, lo->ldo_stripe[i], attr,
1200 CERROR("failed declaration: %d\n", rc);
1206 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE) &&
1207 dt_object_exists(next) != 0 &&
1208 dt_object_remote(next) == 0)
1209 dt_declare_xattr_del(env, next, XATTR_NAME_LOV, handle);
1211 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) &&
1212 dt_object_exists(next) &&
1213 dt_object_remote(next) == 0 && S_ISREG(attr->la_mode)) {
1214 struct lod_thread_info *info = lod_env_info(env);
1215 struct lu_buf *buf = &info->lti_buf;
1217 buf->lb_buf = info->lti_ea_store;
1218 buf->lb_len = info->lti_ea_store_size;
1219 dt_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1220 LU_XATTR_REPLACE, handle);
1227 * Implementation of dt_object_operations::do_attr_set.
1229 * If the object is striped, then apply the changes to all or subset of
1230 * the stripes depending on the object type and specific attributes.
1232 * \see dt_object_operations::do_attr_set() in the API description for details.
1234 static int lod_attr_set(const struct lu_env *env,
1235 struct dt_object *dt,
1236 const struct lu_attr *attr,
1237 struct thandle *handle)
1239 struct dt_object *next = dt_object_child(dt);
1240 struct lod_object *lo = lod_dt_obj(dt);
1244 /* Set dead object on all other stripes */
1245 if (attr->la_valid & LA_FLAGS && !(attr->la_valid & ~LA_FLAGS) &&
1246 attr->la_flags & LUSTRE_SLAVE_DEAD_FL) {
1247 rc = lod_mark_dead_object(env, dt, handle, false);
1252 * apply changes to the local object
1254 rc = dt_attr_set(env, next, attr, handle);
1258 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1259 if (!(attr->la_valid & (LA_UID | LA_GID)))
1262 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1265 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE |
1266 LA_ATIME | LA_MTIME | LA_CTIME)))
1270 if (lo->ldo_stripenr == 0)
1274 * if object is striped, apply changes to all the stripes
1276 LASSERT(lo->ldo_stripe);
1277 for (i = 0; i < lo->ldo_stripenr; i++) {
1278 if (unlikely(lo->ldo_stripe[i] == NULL))
1280 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
1281 (dt_object_exists(lo->ldo_stripe[i]) == 0))
1284 rc = dt_attr_set(env, lo->ldo_stripe[i], attr, handle);
1286 CERROR("failed declaration: %d\n", rc);
1291 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE) &&
1292 dt_object_exists(next) != 0 &&
1293 dt_object_remote(next) == 0)
1294 dt_xattr_del(env, next, XATTR_NAME_LOV, handle);
1296 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) &&
1297 dt_object_exists(next) &&
1298 dt_object_remote(next) == 0 && S_ISREG(attr->la_mode)) {
1299 struct lod_thread_info *info = lod_env_info(env);
1300 struct lu_buf *buf = &info->lti_buf;
1301 struct ost_id *oi = &info->lti_ostid;
1302 struct lu_fid *fid = &info->lti_fid;
1303 struct lov_mds_md_v1 *lmm;
1304 struct lov_ost_data_v1 *objs;
1308 rc1 = lod_get_lov_ea(env, lo);
1312 buf->lb_buf = info->lti_ea_store;
1313 buf->lb_len = info->lti_ea_store_size;
1314 lmm = info->lti_ea_store;
1315 magic = le32_to_cpu(lmm->lmm_magic);
1316 if (magic == LOV_MAGIC_V1)
1317 objs = &(lmm->lmm_objects[0]);
1319 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1320 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1321 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1323 fid_to_ostid(fid, oi);
1324 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1325 dt_xattr_set(env, next, buf, XATTR_NAME_LOV,
1326 LU_XATTR_REPLACE, handle);
1333 * Implementation of dt_object_operations::do_xattr_get.
1335 * If LOV EA is requested from the root object and it's not
1336 * found, then return default striping for the filesystem.
1338 * \see dt_object_operations::do_xattr_get() in the API description for details.
1340 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1341 struct lu_buf *buf, const char *name)
1343 struct lod_thread_info *info = lod_env_info(env);
1344 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1348 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1349 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1350 struct lmv_mds_md_v1 *lmv1;
1353 if (rc > (typeof(rc))sizeof(*lmv1))
1356 if (rc < (typeof(rc))sizeof(*lmv1))
1357 RETURN(rc = rc > 0 ? -EINVAL : rc);
1359 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1360 CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1362 info->lti_buf.lb_buf = info->lti_key;
1363 info->lti_buf.lb_len = sizeof(*lmv1);
1364 rc = dt_xattr_get(env, dt_object_child(dt),
1365 &info->lti_buf, name);
1366 if (unlikely(rc != sizeof(*lmv1)))
1367 RETURN(rc = rc > 0 ? -EINVAL : rc);
1369 lmv1 = info->lti_buf.lb_buf;
1370 /* The on-disk LMV EA only contains header, but the
1371 * returned LMV EA size should contain the space for
1372 * the FIDs of all shards of the striped directory. */
1373 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1374 rc = lmv_mds_md_size(
1375 le32_to_cpu(lmv1->lmv_stripe_count),
1378 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1382 RETURN(rc = rc1 != 0 ? rc1 : rc);
1385 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1389 * lod returns default striping on the real root of the device
1390 * this is like the root stores default striping for the whole
1391 * filesystem. historically we've been using a different approach
1392 * and store it in the config.
1394 dt_root_get(env, dev->lod_child, &info->lti_fid);
1395 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1397 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1398 struct lov_user_md *lum = buf->lb_buf;
1399 struct lov_desc *desc = &dev->lod_desc;
1401 if (buf->lb_buf == NULL) {
1403 } else if (buf->lb_len >= sizeof(*lum)) {
1404 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1405 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1406 lmm_oi_set_id(&lum->lmm_oi, 0);
1407 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1408 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1409 lum->lmm_stripe_size = cpu_to_le32(
1410 desc->ld_default_stripe_size);
1411 lum->lmm_stripe_count = cpu_to_le16(
1412 desc->ld_default_stripe_count);
1413 lum->lmm_stripe_offset = cpu_to_le16(
1414 desc->ld_default_stripe_offset);
1427 * Checks that the magic of the stripe is sane.
1429 * \param[in] lod lod device
1430 * \param[in] lum a buffer storing LMV EA to verify
1432 * \retval 0 if the EA is sane
1433 * \retval negative otherwise
1435 static int lod_verify_md_striping(struct lod_device *lod,
1436 const struct lmv_user_md_v1 *lum)
1438 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1439 CERROR("%s: invalid lmv_user_md: magic = %x, "
1440 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1441 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1442 (int)le32_to_cpu(lum->lum_stripe_offset),
1443 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1451 * Initialize LMV EA for a slave.
1453 * Initialize slave's LMV EA from the master's LMV EA.
1455 * \param[in] master_lmv a buffer containing master's EA
1456 * \param[out] slave_lmv a buffer where slave's EA will be stored
1459 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1460 const struct lmv_mds_md_v1 *master_lmv)
1462 *slave_lmv = *master_lmv;
1463 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1469 * Generate LMV EA from the object passed as \a dt. The object must have
1470 * the stripes created and initialized.
1472 * \param[in] env execution environment
1473 * \param[in] dt object
1474 * \param[out] lmv_buf buffer storing generated LMV EA
1476 * \retval 0 on success
1477 * \retval negative if failed
1479 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1480 struct lu_buf *lmv_buf)
1482 struct lod_thread_info *info = lod_env_info(env);
1483 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1484 struct lod_object *lo = lod_dt_obj(dt);
1485 struct lmv_mds_md_v1 *lmm1;
1487 int type = LU_SEQ_RANGE_ANY;
1492 LASSERT(lo->ldo_dir_striped != 0);
1493 LASSERT(lo->ldo_stripenr > 0);
1494 stripe_count = lo->ldo_stripenr;
1495 /* Only store the LMV EA heahder on the disk. */
1496 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1497 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1501 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1504 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1505 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1506 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1507 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1508 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1513 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1514 lmv_buf->lb_buf = info->lti_ea_store;
1515 lmv_buf->lb_len = sizeof(*lmm1);
1521 * Create in-core represenation for a striped directory.
1523 * Parse the buffer containing LMV EA and instantiate LU objects
1524 * representing the stripe objects. The pointers to the objects are
1525 * stored in ldo_stripe field of \a lo. This function is used when
1526 * we need to access an already created object (i.e. load from a disk).
1528 * \param[in] env execution environment
1529 * \param[in] lo lod object
1530 * \param[in] buf buffer containing LMV EA
1532 * \retval 0 on success
1533 * \retval negative if failed
1535 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1536 const struct lu_buf *buf)
1538 struct lod_thread_info *info = lod_env_info(env);
1539 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1540 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1541 struct dt_object **stripe;
1542 union lmv_mds_md *lmm = buf->lb_buf;
1543 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1544 struct lu_fid *fid = &info->lti_fid;
1549 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1552 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1553 lo->ldo_dir_slave_stripe = 1;
1557 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1560 if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1563 LASSERT(lo->ldo_stripe == NULL);
1564 OBD_ALLOC(stripe, sizeof(stripe[0]) *
1565 (le32_to_cpu(lmv1->lmv_stripe_count)));
1569 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1570 struct dt_device *tgt_dt;
1571 struct dt_object *dto;
1572 int type = LU_SEQ_RANGE_ANY;
1575 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1576 if (!fid_is_sane(fid))
1577 GOTO(out, rc = -ESTALE);
1579 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1583 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1584 tgt_dt = lod->lod_child;
1586 struct lod_tgt_desc *tgt;
1588 tgt = LTD_TGT(ltd, idx);
1590 GOTO(out, rc = -ESTALE);
1591 tgt_dt = tgt->ltd_tgt;
1594 dto = dt_locate_at(env, tgt_dt, fid,
1595 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1598 GOTO(out, rc = PTR_ERR(dto));
1603 lo->ldo_stripe = stripe;
1604 lo->ldo_stripenr = le32_to_cpu(lmv1->lmv_stripe_count);
1605 lo->ldo_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1607 lod_object_free_striping(env, lo);
1613 * Create a striped directory.
1615 * Create a striped directory with a given stripe pattern on the specified MDTs.
1616 * A striped directory is represented as a regular directory - an index listing
1617 * all the stripes. The stripes point back to the master object with ".." and
1618 * LinkEA. The master object gets LMV EA which identifies it as a striped
1619 * directory. The function allocates FIDs for all the stripes.
1621 * \param[in] env execution environment
1622 * \param[in] dt object
1623 * \param[in] attr attributes to initialize the objects with
1624 * \param[in] lum a pattern specifying the number of stripes and
1626 * \param[in] dof type of objects to be created
1627 * \param[in] th transaction handle
1629 * \retval 0 on success
1630 * \retval negative if failed
1632 static int lod_prep_md_striped_create(const struct lu_env *env,
1633 struct dt_object *dt,
1634 struct lu_attr *attr,
1635 const struct lmv_user_md_v1 *lum,
1636 struct dt_object_format *dof,
1639 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1640 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1641 struct lod_object *lo = lod_dt_obj(dt);
1642 struct lod_thread_info *info = lod_env_info(env);
1643 struct dt_object **stripe;
1644 struct lu_buf lmv_buf;
1645 struct lu_buf slave_lmv_buf;
1646 struct lmv_mds_md_v1 *lmm;
1647 struct lmv_mds_md_v1 *slave_lmm = NULL;
1648 struct dt_insert_rec *rec = &info->lti_dt_rec;
1656 /* The lum has been verifed in lod_verify_md_striping */
1657 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
1658 LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1660 stripe_count = le32_to_cpu(lum->lum_stripe_count);
1662 /* shrink the stripe_count to the avaible MDT count */
1663 if (stripe_count > lod->lod_remote_mdt_count + 1)
1664 stripe_count = lod->lod_remote_mdt_count + 1;
1666 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1670 OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1671 if (idx_array == NULL)
1672 GOTO(out_free, rc = -ENOMEM);
1674 for (i = 0; i < stripe_count; i++) {
1675 struct lod_tgt_desc *tgt = NULL;
1676 struct dt_object *dto;
1677 struct lu_fid fid = { 0 };
1679 struct lu_object_conf conf = { 0 };
1680 struct dt_device *tgt_dt = NULL;
1683 /* Right now, master stripe and master object are
1684 * on the same MDT */
1685 idx = le32_to_cpu(lum->lum_stripe_offset);
1686 rc = obd_fid_alloc(env, lod->lod_child_exp, &fid,
1690 tgt_dt = lod->lod_child;
1694 idx = (idx_array[i - 1] + 1) % (lod->lod_remote_mdt_count + 1);
1696 for (j = 0; j < lod->lod_remote_mdt_count;
1697 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1698 bool already_allocated = false;
1701 CDEBUG(D_INFO, "try idx %d, mdt cnt %u,"
1702 " allocated %u, last allocated %d\n", idx,
1703 lod->lod_remote_mdt_count, i, idx_array[i - 1]);
1705 /* Find next available target */
1706 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx))
1709 /* check whether the idx already exists
1710 * in current allocated array */
1711 for (k = 0; k < i; k++) {
1712 if (idx_array[k] == idx) {
1713 already_allocated = true;
1718 if (already_allocated)
1721 /* check the status of the OSP */
1722 tgt = LTD_TGT(ltd, idx);
1726 tgt_dt = tgt->ltd_tgt;
1727 rc = dt_statfs(env, tgt_dt, NULL);
1729 /* this OSP doesn't feel well */
1734 rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1743 /* Can not allocate more stripes */
1744 if (j == lod->lod_remote_mdt_count) {
1745 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1746 lod2obd(lod)->obd_name, stripe_count, i - 1);
1750 CDEBUG(D_INFO, "idx %d, mdt cnt %u,"
1751 " allocated %u, last allocated %d\n", idx,
1752 lod->lod_remote_mdt_count, i, idx_array[i - 1]);
1755 /* tgt_dt and fid must be ready after search avaible OSP
1756 * in the above loop */
1757 LASSERT(tgt_dt != NULL);
1758 LASSERT(fid_is_sane(&fid));
1759 conf.loc_flags = LOC_F_NEW;
1760 dto = dt_locate_at(env, tgt_dt, &fid,
1761 dt->do_lu.lo_dev->ld_site->ls_top_dev,
1764 GOTO(out_put, rc = PTR_ERR(dto));
1769 lo->ldo_dir_striped = 1;
1770 lo->ldo_stripe = stripe;
1771 lo->ldo_stripenr = i;
1772 lo->ldo_stripes_allocated = stripe_count;
1774 if (lo->ldo_stripenr == 0)
1775 GOTO(out_put, rc = -ENOSPC);
1777 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1780 lmm = lmv_buf.lb_buf;
1782 OBD_ALLOC_PTR(slave_lmm);
1783 if (slave_lmm == NULL)
1784 GOTO(out_put, rc = -ENOMEM);
1786 lod_prep_slave_lmv_md(slave_lmm, lmm);
1787 slave_lmv_buf.lb_buf = slave_lmm;
1788 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1790 if (!dt_try_as_dir(env, dt_object_child(dt)))
1791 GOTO(out_put, rc = -EINVAL);
1793 rec->rec_type = S_IFDIR;
1794 for (i = 0; i < lo->ldo_stripenr; i++) {
1795 struct dt_object *dto = stripe[i];
1796 char *stripe_name = info->lti_key;
1797 struct lu_name *sname;
1798 struct linkea_data ldata = { NULL };
1799 struct lu_buf linkea_buf;
1801 rc = dt_declare_create(env, dto, attr, NULL, dof, th);
1805 if (!dt_try_as_dir(env, dto))
1806 GOTO(out_put, rc = -EINVAL);
1808 rc = dt_declare_ref_add(env, dto, th);
1812 rec->rec_fid = lu_object_fid(&dto->do_lu);
1813 rc = dt_declare_insert(env, dto, (const struct dt_rec *)rec,
1814 (const struct dt_key *)dot, th);
1818 /* master stripe FID will be put to .. */
1819 rec->rec_fid = lu_object_fid(&dt->do_lu);
1820 rc = dt_declare_insert(env, dto, (const struct dt_rec *)rec,
1821 (const struct dt_key *)dotdot, th);
1825 /* probably nothing to inherite */
1826 if (lo->ldo_def_striping_set &&
1827 !LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size,
1828 lo->ldo_def_stripenr,
1829 lo->ldo_def_stripe_offset,
1831 struct lov_user_md_v3 *v3;
1833 /* sigh, lti_ea_store has been used for lmv_buf,
1834 * so we have to allocate buffer for default
1838 GOTO(out_put, rc = -ENOMEM);
1840 memset(v3, 0, sizeof(*v3));
1841 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
1842 v3->lmm_stripe_count =
1843 cpu_to_le16(lo->ldo_def_stripenr);
1844 v3->lmm_stripe_offset =
1845 cpu_to_le16(lo->ldo_def_stripe_offset);
1846 v3->lmm_stripe_size =
1847 cpu_to_le32(lo->ldo_def_stripe_size);
1848 if (lo->ldo_pool != NULL)
1849 strlcpy(v3->lmm_pool_name, lo->ldo_pool,
1850 sizeof(v3->lmm_pool_name));
1852 info->lti_buf.lb_buf = v3;
1853 info->lti_buf.lb_len = sizeof(*v3);
1854 rc = dt_declare_xattr_set(env, dto,
1863 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1864 cfs_fail_val != i) {
1865 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1867 slave_lmm->lmv_master_mdt_index =
1870 slave_lmm->lmv_master_mdt_index =
1872 rc = dt_declare_xattr_set(env, dto, &slave_lmv_buf,
1873 XATTR_NAME_LMV, 0, th);
1878 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1880 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1881 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1883 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1884 PFID(lu_object_fid(&dto->do_lu)), i);
1886 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1887 rc = linkea_data_new(&ldata, &info->lti_linkea_buf);
1891 rc = linkea_add_buf(&ldata, sname, lu_object_fid(&dt->do_lu));
1895 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1896 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1897 rc = dt_declare_xattr_set(env, dto, &linkea_buf,
1898 XATTR_NAME_LINK, 0, th);
1902 rec->rec_fid = lu_object_fid(&dto->do_lu);
1903 rc = dt_declare_insert(env, dt_object_child(dt),
1904 (const struct dt_rec *)rec,
1905 (const struct dt_key *)stripe_name, th);
1909 rc = dt_declare_ref_add(env, dt_object_child(dt), th);
1914 rc = dt_declare_xattr_set(env, dt_object_child(dt), &lmv_buf,
1915 XATTR_NAME_LMV, 0, th);
1921 for (i = 0; i < stripe_count; i++)
1922 if (stripe[i] != NULL)
1923 lu_object_put(env, &stripe[i]->do_lu);
1924 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
1925 lo->ldo_stripenr = 0;
1926 lo->ldo_stripes_allocated = 0;
1927 lo->ldo_stripe = NULL;
1931 if (idx_array != NULL)
1932 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
1933 if (slave_lmm != NULL)
1934 OBD_FREE_PTR(slave_lmm);
1940 * Declare create striped md object.
1942 * The function declares intention to create a striped directory. This is a
1943 * wrapper for lod_prep_md_striped_create(). The only additional functionality
1944 * is to verify pattern \a lum_buf is good. Check that function for the details.
1946 * \param[in] env execution environment
1947 * \param[in] dt object
1948 * \param[in] attr attributes to initialize the objects with
1949 * \param[in] lum_buf a pattern specifying the number of stripes and
1951 * \param[in] dof type of objects to be created
1952 * \param[in] th transaction handle
1954 * \retval 0 on success
1955 * \retval negative if failed
1958 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
1959 struct dt_object *dt,
1960 struct lu_attr *attr,
1961 const struct lu_buf *lum_buf,
1962 struct dt_object_format *dof,
1965 struct lod_object *lo = lod_dt_obj(dt);
1966 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1967 struct lmv_user_md_v1 *lum;
1971 lum = lum_buf->lb_buf;
1972 LASSERT(lum != NULL);
1974 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
1975 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
1976 (int)le32_to_cpu(lum->lum_stripe_offset));
1978 if (le32_to_cpu(lum->lum_stripe_count) == 0)
1981 rc = lod_verify_md_striping(lod, lum);
1985 /* prepare dir striped objects */
1986 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
1988 /* failed to create striping, let's reset
1989 * config so that others don't get confused */
1990 lod_object_free_striping(env, lo);
1999 * Implementation of dt_object_operations::do_declare_xattr_set.
2001 * Used with regular (non-striped) objects. Basically it
2002 * initializes the striping information and applies the
2003 * change to all the stripes.
2005 * \see dt_object_operations::do_declare_xattr_set() in the API description
2008 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2009 struct dt_object *dt,
2010 const struct lu_buf *buf,
2011 const char *name, int fl,
2014 struct dt_object *next = dt_object_child(dt);
2015 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2016 struct lod_object *lo = lod_dt_obj(dt);
2021 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2022 struct lmv_user_md_v1 *lum;
2024 LASSERT(buf != NULL && buf->lb_buf != NULL);
2026 rc = lod_verify_md_striping(d, lum);
2031 rc = dt_declare_xattr_set(env, next, buf, name, fl, th);
2035 /* set xattr to each stripes, if needed */
2036 rc = lod_load_striping(env, lo);
2040 /* Note: Do not set LinkEA on sub-stripes, otherwise
2041 * it will confuse the fid2path process(see mdt_path_current()).
2042 * The linkEA between master and sub-stripes is set in
2043 * lod_xattr_set_lmv(). */
2044 if (lo->ldo_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2047 for (i = 0; i < lo->ldo_stripenr; i++) {
2048 LASSERT(lo->ldo_stripe[i]);
2049 rc = dt_declare_xattr_set(env, lo->ldo_stripe[i], buf,
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 = dt_declare_xattr_set(env, next, buf, name, fl, th);
2112 * Resets cached default striping in the object.
2114 * \param[in] lo object
2116 static void lod_lov_stripe_cache_clear(struct lod_object *lo)
2118 lo->ldo_def_striping_set = 0;
2119 lo->ldo_def_striping_cached = 0;
2120 lod_object_set_pool(lo, NULL);
2121 lo->ldo_def_stripe_size = 0;
2122 lo->ldo_def_stripenr = 0;
2123 if (lo->ldo_dir_stripe != NULL)
2124 lo->ldo_dir_def_striping_cached = 0;
2128 * Apply xattr changes to the object.
2130 * Applies xattr changes to the object and the stripes if the latter exist.
2132 * \param[in] env execution environment
2133 * \param[in] dt object
2134 * \param[in] buf buffer pointing to the new value of xattr
2135 * \param[in] name name of xattr
2136 * \param[in] fl flags
2137 * \param[in] th transaction handle
2139 * \retval 0 on success
2140 * \retval negative if failed
2142 static int lod_xattr_set_internal(const struct lu_env *env,
2143 struct dt_object *dt,
2144 const struct lu_buf *buf,
2145 const char *name, int fl, struct thandle *th)
2147 struct dt_object *next = dt_object_child(dt);
2148 struct lod_object *lo = lod_dt_obj(dt);
2153 rc = dt_xattr_set(env, next, buf, name, fl, th);
2154 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2157 /* Note: Do not set LinkEA on sub-stripes, otherwise
2158 * it will confuse the fid2path process(see mdt_path_current()).
2159 * The linkEA between master and sub-stripes is set in
2160 * lod_xattr_set_lmv(). */
2161 if (lo->ldo_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2164 for (i = 0; i < lo->ldo_stripenr; i++) {
2165 LASSERT(lo->ldo_stripe[i]);
2166 rc = dt_xattr_set(env, lo->ldo_stripe[i], buf, name, fl, th);
2175 * Delete an extended attribute.
2177 * Deletes specified xattr from the object and the stripes if the latter exist.
2179 * \param[in] env execution environment
2180 * \param[in] dt object
2181 * \param[in] name name of xattr
2182 * \param[in] th transaction handle
2184 * \retval 0 on success
2185 * \retval negative if failed
2187 static int lod_xattr_del_internal(const struct lu_env *env,
2188 struct dt_object *dt,
2189 const char *name, struct thandle *th)
2191 struct dt_object *next = dt_object_child(dt);
2192 struct lod_object *lo = lod_dt_obj(dt);
2197 rc = dt_xattr_del(env, next, name, th);
2198 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2201 if (lo->ldo_stripenr == 0)
2204 for (i = 0; i < lo->ldo_stripenr; i++) {
2205 LASSERT(lo->ldo_stripe[i]);
2206 rc = dt_xattr_del(env, lo->ldo_stripe[i], name, th);
2215 * Set default striping on a directory.
2217 * Sets specified striping on a directory object unless it matches the default
2218 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2219 * EA. This striping will be used when regular file is being created in this
2222 * \param[in] env execution environment
2223 * \param[in] dt the striped object
2224 * \param[in] buf buffer with the striping
2225 * \param[in] name name of EA
2226 * \param[in] fl xattr flag (see OSD API description)
2227 * \param[in] th transaction handle
2229 * \retval 0 on success
2230 * \retval negative if failed
2232 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
2233 struct dt_object *dt,
2234 const struct lu_buf *buf,
2235 const char *name, int fl,
2238 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2239 struct lod_object *l = lod_dt_obj(dt);
2240 struct lov_user_md_v1 *lum;
2241 struct lov_user_md_v3 *v3 = NULL;
2242 const char *pool_name = NULL;
2246 /* If it is striped dir, we should clear the stripe cache for
2247 * slave stripe as well, but there are no effective way to
2248 * notify the LOD on the slave MDT, so we do not cache stripe
2249 * information for slave stripe for now. XXX*/
2250 lod_lov_stripe_cache_clear(l);
2251 LASSERT(buf != NULL && buf->lb_buf != NULL);
2254 rc = lod_verify_striping(d, buf, false);
2258 if (lum->lmm_magic == LOV_USER_MAGIC_V3) {
2260 if (v3->lmm_pool_name[0] != '\0')
2261 pool_name = v3->lmm_pool_name;
2264 /* if { size, offset, count } = { 0, -1, 0 } and no pool
2265 * (i.e. all default values specified) then delete default
2266 * striping from dir. */
2268 "set default striping: sz %u # %u offset %d %s %s\n",
2269 (unsigned)lum->lmm_stripe_size,
2270 (unsigned)lum->lmm_stripe_count,
2271 (int)lum->lmm_stripe_offset,
2272 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
2274 if (LOVEA_DELETE_VALUES(lum->lmm_stripe_size, lum->lmm_stripe_count,
2275 lum->lmm_stripe_offset, pool_name)) {
2276 rc = lod_xattr_del_internal(env, dt, name, th);
2280 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2287 * Set default striping on a directory object.
2289 * Sets specified striping on a directory object unless it matches the default
2290 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2291 * EA. This striping will be used when a new directory is being created in the
2294 * \param[in] env execution environment
2295 * \param[in] dt the striped object
2296 * \param[in] buf buffer with the striping
2297 * \param[in] name name of EA
2298 * \param[in] fl xattr flag (see OSD API description)
2299 * \param[in] th transaction handle
2301 * \retval 0 on success
2302 * \retval negative if failed
2304 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
2305 struct dt_object *dt,
2306 const struct lu_buf *buf,
2307 const char *name, int fl,
2310 struct lod_object *l = lod_dt_obj(dt);
2311 struct lmv_user_md_v1 *lum;
2315 LASSERT(buf != NULL && buf->lb_buf != NULL);
2318 CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
2319 le32_to_cpu(lum->lum_stripe_count),
2320 (int)le32_to_cpu(lum->lum_stripe_offset));
2322 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
2323 le32_to_cpu(lum->lum_stripe_offset)) &&
2324 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
2325 rc = lod_xattr_del_internal(env, dt, name, th);
2329 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2334 /* Update default stripe cache */
2335 if (l->ldo_dir_stripe == NULL) {
2336 OBD_ALLOC_PTR(l->ldo_dir_stripe);
2337 if (l->ldo_dir_stripe == NULL)
2341 l->ldo_dir_def_striping_cached = 0;
2346 * Turn directory into a striped directory.
2348 * During replay the client sends the striping created before MDT
2349 * failure, then the layer above LOD sends this defined striping
2350 * using ->do_xattr_set(), so LOD uses this method to replay creation
2351 * of the stripes. Notice the original information for the striping
2352 * (#stripes, FIDs, etc) was transfered in declare path.
2354 * \param[in] env execution environment
2355 * \param[in] dt the striped object
2356 * \param[in] buf not used currently
2357 * \param[in] name not used currently
2358 * \param[in] fl xattr flag (see OSD API description)
2359 * \param[in] th transaction handle
2361 * \retval 0 on success
2362 * \retval negative if failed
2364 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
2365 const struct lu_buf *buf, const char *name,
2366 int fl, struct thandle *th)
2368 struct lod_object *lo = lod_dt_obj(dt);
2369 struct lod_thread_info *info = lod_env_info(env);
2370 struct lu_attr *attr = &info->lti_attr;
2371 struct dt_object_format *dof = &info->lti_format;
2372 struct lu_buf lmv_buf;
2373 struct lu_buf slave_lmv_buf;
2374 struct lmv_mds_md_v1 *lmm;
2375 struct lmv_mds_md_v1 *slave_lmm = NULL;
2376 struct dt_insert_rec *rec = &info->lti_dt_rec;
2381 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2384 /* The stripes are supposed to be allocated in declare phase,
2385 * if there are no stripes being allocated, it will skip */
2386 if (lo->ldo_stripenr == 0)
2389 rc = dt_attr_get(env, dt_object_child(dt), attr);
2393 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
2394 LA_MODE | LA_UID | LA_GID | LA_TYPE;
2395 dof->dof_type = DFT_DIR;
2397 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
2400 lmm = lmv_buf.lb_buf;
2402 OBD_ALLOC_PTR(slave_lmm);
2403 if (slave_lmm == NULL)
2406 lod_prep_slave_lmv_md(slave_lmm, lmm);
2407 slave_lmv_buf.lb_buf = slave_lmm;
2408 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
2410 rec->rec_type = S_IFDIR;
2411 for (i = 0; i < lo->ldo_stripenr; i++) {
2412 struct dt_object *dto;
2413 char *stripe_name = info->lti_key;
2414 struct lu_name *sname;
2415 struct linkea_data ldata = { NULL };
2416 struct lu_buf linkea_buf;
2418 dto = lo->ldo_stripe[i];
2419 dt_write_lock(env, dto, MOR_TGT_CHILD);
2420 rc = dt_create(env, dto, attr, NULL, dof, th);
2422 dt_write_unlock(env, dto);
2426 rc = dt_ref_add(env, dto, th);
2427 dt_write_unlock(env, dto);
2431 rec->rec_fid = lu_object_fid(&dto->do_lu);
2432 rc = dt_insert(env, dto, (const struct dt_rec *)rec,
2433 (const struct dt_key *)dot, th, 0);
2437 rec->rec_fid = lu_object_fid(&dt->do_lu);
2438 rc = dt_insert(env, dto, (struct dt_rec *)rec,
2439 (const struct dt_key *)dotdot, th, 0);
2443 if (lo->ldo_def_striping_set &&
2444 !LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size,
2445 lo->ldo_def_stripenr,
2446 lo->ldo_def_stripe_offset,
2448 struct lov_user_md_v3 *v3;
2450 /* sigh, lti_ea_store has been used for lmv_buf,
2451 * so we have to allocate buffer for default
2457 memset(v3, 0, sizeof(*v3));
2458 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
2459 v3->lmm_stripe_count =
2460 cpu_to_le16(lo->ldo_def_stripenr);
2461 v3->lmm_stripe_offset =
2462 cpu_to_le16(lo->ldo_def_stripe_offset);
2463 v3->lmm_stripe_size =
2464 cpu_to_le32(lo->ldo_def_stripe_size);
2465 if (lo->ldo_pool != NULL)
2466 strlcpy(v3->lmm_pool_name, lo->ldo_pool,
2467 sizeof(v3->lmm_pool_name));
2469 info->lti_buf.lb_buf = v3;
2470 info->lti_buf.lb_len = sizeof(*v3);
2471 rc = dt_xattr_set(env, dto, &info->lti_buf,
2472 XATTR_NAME_LOV, 0, th);
2478 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
2479 cfs_fail_val != i) {
2480 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
2482 slave_lmm->lmv_master_mdt_index =
2485 slave_lmm->lmv_master_mdt_index =
2487 rc = dt_xattr_set(env, dto, &slave_lmv_buf,
2488 XATTR_NAME_LMV, fl, th);
2493 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
2495 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2496 PFID(lu_object_fid(&dto->do_lu)), i + 1);
2498 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2499 PFID(lu_object_fid(&dto->do_lu)), i);
2501 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2502 rc = linkea_data_new(&ldata, &info->lti_linkea_buf);
2506 rc = linkea_add_buf(&ldata, sname, lu_object_fid(&dt->do_lu));
2510 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
2511 linkea_buf.lb_len = ldata.ld_leh->leh_len;
2512 rc = dt_xattr_set(env, dto, &linkea_buf, XATTR_NAME_LINK,
2517 rec->rec_fid = lu_object_fid(&dto->do_lu);
2518 rc = dt_insert(env, dt_object_child(dt),
2519 (const struct dt_rec *)rec,
2520 (const struct dt_key *)stripe_name, th, 0);
2524 rc = dt_ref_add(env, dt_object_child(dt), th);
2529 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
2530 rc = dt_xattr_set(env, dt_object_child(dt), &lmv_buf,
2531 XATTR_NAME_LMV, fl, th);
2534 if (slave_lmm != NULL)
2535 OBD_FREE_PTR(slave_lmm);
2541 * Helper function to declare/execute creation of a striped directory
2543 * Called in declare/create object path, prepare striping for a directory
2544 * and prepare defaults data striping for the objects to be created in
2545 * that directory. Notice the function calls "declaration" or "execution"
2546 * methods depending on \a declare param. This is a consequence of the
2547 * current approach while we don't have natural distributed transactions:
2548 * we basically execute non-local updates in the declare phase. So, the
2549 * arguments for the both phases are the same and this is the reason for
2550 * this function to exist.
2552 * \param[in] env execution environment
2553 * \param[in] dt object
2554 * \param[in] attr attributes the stripes will be created with
2555 * \param[in] dof format of stripes (see OSD API description)
2556 * \param[in] th transaction handle
2557 * \param[in] declare where to call "declare" or "execute" methods
2559 * \retval 0 on success
2560 * \retval negative if failed
2562 static int lod_dir_striping_create_internal(const struct lu_env *env,
2563 struct dt_object *dt,
2564 struct lu_attr *attr,
2565 struct dt_object_format *dof,
2569 struct lod_thread_info *info = lod_env_info(env);
2570 struct lod_object *lo = lod_dt_obj(dt);
2574 if (!LMVEA_DELETE_VALUES(lo->ldo_stripenr,
2575 lo->ldo_dir_stripe_offset)) {
2576 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
2577 int stripe_count = lo->ldo_stripenr;
2579 if (info->lti_ea_store_size < sizeof(*v1)) {
2580 rc = lod_ea_store_resize(info, sizeof(*v1));
2583 v1 = info->lti_ea_store;
2586 memset(v1, 0, sizeof(*v1));
2587 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
2588 v1->lum_stripe_count = cpu_to_le32(stripe_count);
2589 v1->lum_stripe_offset =
2590 cpu_to_le32(lo->ldo_dir_stripe_offset);
2592 info->lti_buf.lb_buf = v1;
2593 info->lti_buf.lb_len = sizeof(*v1);
2596 rc = lod_declare_xattr_set_lmv(env, dt, attr,
2597 &info->lti_buf, dof, th);
2599 rc = lod_xattr_set_lmv(env, dt, &info->lti_buf,
2600 XATTR_NAME_LMV, 0, th);
2605 /* Transfer default LMV striping from the parent */
2606 if (lo->ldo_dir_def_striping_set &&
2607 !LMVEA_DELETE_VALUES(lo->ldo_dir_def_stripenr,
2608 lo->ldo_dir_def_stripe_offset)) {
2609 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
2610 int def_stripe_count = lo->ldo_dir_def_stripenr;
2612 if (info->lti_ea_store_size < sizeof(*v1)) {
2613 rc = lod_ea_store_resize(info, sizeof(*v1));
2616 v1 = info->lti_ea_store;
2619 memset(v1, 0, sizeof(*v1));
2620 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
2621 v1->lum_stripe_count = cpu_to_le32(def_stripe_count);
2622 v1->lum_stripe_offset =
2623 cpu_to_le32(lo->ldo_dir_def_stripe_offset);
2625 cpu_to_le32(lo->ldo_dir_def_hash_type);
2627 info->lti_buf.lb_buf = v1;
2628 info->lti_buf.lb_len = sizeof(*v1);
2630 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
2631 XATTR_NAME_DEFAULT_LMV,
2634 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
2636 XATTR_NAME_DEFAULT_LMV, 0,
2642 /* Transfer default LOV striping from the parent */
2643 if (lo->ldo_def_striping_set &&
2644 !LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size,
2645 lo->ldo_def_stripenr,
2646 lo->ldo_def_stripe_offset,
2648 struct lov_user_md_v3 *v3 = info->lti_ea_store;
2650 if (info->lti_ea_store_size < sizeof(*v3)) {
2651 rc = lod_ea_store_resize(info, sizeof(*v3));
2654 v3 = info->lti_ea_store;
2657 memset(v3, 0, sizeof(*v3));
2658 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
2659 v3->lmm_stripe_count = cpu_to_le16(lo->ldo_def_stripenr);
2660 v3->lmm_stripe_offset = cpu_to_le16(lo->ldo_def_stripe_offset);
2661 v3->lmm_stripe_size = cpu_to_le32(lo->ldo_def_stripe_size);
2662 if (lo->ldo_pool != NULL)
2663 strlcpy(v3->lmm_pool_name, lo->ldo_pool,
2664 sizeof(v3->lmm_pool_name));
2666 info->lti_buf.lb_buf = v3;
2667 info->lti_buf.lb_len = sizeof(*v3);
2670 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
2671 XATTR_NAME_LOV, 0, th);
2673 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
2674 XATTR_NAME_LOV, 0, th);
2682 static int lod_declare_dir_striping_create(const struct lu_env *env,
2683 struct dt_object *dt,
2684 struct lu_attr *attr,
2685 struct dt_object_format *dof,
2688 return lod_dir_striping_create_internal(env, dt, attr, dof, th, true);
2691 static int lod_dir_striping_create(const struct lu_env *env,
2692 struct dt_object *dt,
2693 struct lu_attr *attr,
2694 struct dt_object_format *dof,
2697 struct lod_object *lo = lod_dt_obj(dt);
2700 rc = lod_dir_striping_create_internal(env, dt, attr, dof, th, false);
2702 lo->ldo_striping_cached = 1;
2708 * Implementation of dt_object_operations::do_xattr_set.
2710 * Sets specified extended attribute on the object. Three types of EAs are
2712 * LOV EA - stores striping for a regular file or default striping (when set
2714 * LMV EA - stores a marker for the striped directories
2715 * DMV EA - stores default directory striping
2717 * When striping is applied to a non-striped existing object (this is called
2718 * late striping), then LOD notices the caller wants to turn the object into a
2719 * striped one. The stripe objects are created and appropriate EA is set:
2720 * LOV EA storing all the stripes directly or LMV EA storing just a small header
2721 * with striping configuration.
2723 * \see dt_object_operations::do_xattr_set() in the API description for details.
2725 static int lod_xattr_set(const struct lu_env *env,
2726 struct dt_object *dt, const struct lu_buf *buf,
2727 const char *name, int fl, struct thandle *th)
2729 struct dt_object *next = dt_object_child(dt);
2733 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2734 strcmp(name, XATTR_NAME_LMV) == 0) {
2735 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
2737 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
2738 LMV_HASH_FLAG_MIGRATION)
2739 rc = dt_xattr_set(env, next, buf, name, fl, th);
2741 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
2746 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2747 strcmp(name, XATTR_NAME_LOV) == 0) {
2749 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
2751 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2752 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2754 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
2757 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
2758 !strcmp(name, XATTR_NAME_LOV)) {
2759 /* in case of lov EA swap, just set it
2760 * if not, it is a replay so check striping match what we
2761 * already have during req replay, declare_xattr_set()
2762 * defines striping, then create() does the work
2764 if (fl & LU_XATTR_REPLACE) {
2765 /* free stripes, then update disk */
2766 lod_object_free_striping(env, lod_dt_obj(dt));
2767 rc = dt_xattr_set(env, next, buf, name, fl, th);
2769 rc = lod_striping_create(env, dt, NULL, NULL, th);
2774 /* then all other xattr */
2775 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2781 * Implementation of dt_object_operations::do_declare_xattr_del.
2783 * \see dt_object_operations::do_declare_xattr_del() in the API description
2786 static int lod_declare_xattr_del(const struct lu_env *env,
2787 struct dt_object *dt, const char *name,
2790 struct lod_object *lo = lod_dt_obj(dt);
2795 rc = dt_declare_xattr_del(env, dt_object_child(dt), name, th);
2799 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2802 /* set xattr to each stripes, if needed */
2803 rc = lod_load_striping(env, lo);
2807 if (lo->ldo_stripenr == 0)
2810 for (i = 0; i < lo->ldo_stripenr; i++) {
2811 LASSERT(lo->ldo_stripe[i]);
2812 rc = dt_declare_xattr_del(env, lo->ldo_stripe[i], name, th);
2821 * Implementation of dt_object_operations::do_xattr_del.
2823 * If EA storing a regular striping is being deleted, then release
2824 * all the references to the stripe objects in core.
2826 * \see dt_object_operations::do_xattr_del() in the API description for details.
2828 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
2829 const char *name, struct thandle *th)
2831 struct dt_object *next = dt_object_child(dt);
2832 struct lod_object *lo = lod_dt_obj(dt);
2837 if (!strcmp(name, XATTR_NAME_LOV))
2838 lod_object_free_striping(env, lod_dt_obj(dt));
2840 rc = dt_xattr_del(env, next, name, th);
2841 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2844 if (lo->ldo_stripenr == 0)
2847 for (i = 0; i < lo->ldo_stripenr; i++) {
2848 LASSERT(lo->ldo_stripe[i]);
2849 rc = dt_xattr_del(env, lo->ldo_stripe[i], name, th);
2858 * Implementation of dt_object_operations::do_xattr_list.
2860 * \see dt_object_operations::do_xattr_list() in the API description
2863 static int lod_xattr_list(const struct lu_env *env,
2864 struct dt_object *dt, const struct lu_buf *buf)
2866 return dt_xattr_list(env, dt_object_child(dt), buf);
2870 * Initialize a pool the object belongs to.
2872 * When a striped object is being created, striping configuration
2873 * may demand the stripes are allocated on a limited set of the
2874 * targets. These limited sets are known as "pools". So we copy
2875 * a pool name into the object and later actual creation methods
2876 * (like lod_object_create()) will use this information to allocate
2877 * the stripes properly.
2879 * \param[in] o object
2880 * \param[in] pool pool name
2882 int lod_object_set_pool(struct lod_object *o, char *pool)
2887 len = strlen(o->ldo_pool);
2888 OBD_FREE(o->ldo_pool, len + 1);
2893 OBD_ALLOC(o->ldo_pool, len + 1);
2894 if (o->ldo_pool == NULL)
2896 strcpy(o->ldo_pool, pool);
2901 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
2903 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
2908 * Cache default regular striping in the object.
2910 * To improve performance of striped regular object creation we cache
2911 * default LOV striping (if it exists) in the parent directory object.
2913 * \param[in] env execution environment
2914 * \param[in] lp object
2916 * \retval 0 on success
2917 * \retval negative if failed
2919 static int lod_cache_parent_lov_striping(const struct lu_env *env,
2920 struct lod_object *lp)
2922 struct lod_thread_info *info = lod_env_info(env);
2923 struct lov_user_md_v1 *v1 = NULL;
2924 struct lov_user_md_v3 *v3 = NULL;
2928 /* called from MDD without parent being write locked,
2930 dt_write_lock(env, dt_object_child(&lp->ldo_obj), 0);
2931 rc = lod_get_lov_ea(env, lp);
2935 if (rc < (typeof(rc))sizeof(struct lov_user_md)) {
2936 /* don't lookup for non-existing or invalid striping */
2937 lp->ldo_def_striping_set = 0;
2938 lp->ldo_def_striping_cached = 1;
2939 lp->ldo_def_stripe_size = 0;
2940 lp->ldo_def_stripenr = 0;
2941 lp->ldo_def_stripe_offset = (typeof(v1->lmm_stripe_offset))(-1);
2942 GOTO(unlock, rc = 0);
2946 v1 = info->lti_ea_store;
2947 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
2948 lustre_swab_lov_user_md_v1(v1);
2949 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
2950 v3 = (struct lov_user_md_v3 *)v1;
2951 lustre_swab_lov_user_md_v3(v3);
2954 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1)
2955 GOTO(unlock, rc = 0);
2957 if (v1->lmm_pattern != LOV_PATTERN_RAID0 && v1->lmm_pattern != 0)
2958 GOTO(unlock, rc = 0);
2960 CDEBUG(D_INFO, DFID" stripe_count=%d stripe_size=%d stripe_offset=%d\n",
2961 PFID(lu_object_fid(&lp->ldo_obj.do_lu)),
2962 (int)v1->lmm_stripe_count,
2963 (int)v1->lmm_stripe_size, (int)v1->lmm_stripe_offset);
2965 lp->ldo_def_stripenr = v1->lmm_stripe_count;
2966 lp->ldo_def_stripe_size = v1->lmm_stripe_size;
2967 lp->ldo_def_stripe_offset = v1->lmm_stripe_offset;
2968 lp->ldo_def_striping_cached = 1;
2969 lp->ldo_def_striping_set = 1;
2970 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2971 /* XXX: sanity check here */
2972 v3 = (struct lov_user_md_v3 *) v1;
2973 if (v3->lmm_pool_name[0])
2974 lod_object_set_pool(lp, v3->lmm_pool_name);
2978 dt_write_unlock(env, dt_object_child(&lp->ldo_obj));
2984 * Cache default directory striping in the object.
2986 * To improve performance of striped directory creation we cache default
2987 * directory striping (if it exists) in the parent directory object.
2989 * \param[in] env execution environment
2990 * \param[in] lp object
2992 * \retval 0 on success
2993 * \retval negative if failed
2995 static int lod_cache_parent_lmv_striping(const struct lu_env *env,
2996 struct lod_object *lp)
2998 struct lod_thread_info *info = lod_env_info(env);
2999 struct lmv_user_md_v1 *v1 = NULL;
3003 /* called from MDD without parent being write locked,
3005 dt_write_lock(env, dt_object_child(&lp->ldo_obj), 0);
3006 rc = lod_get_default_lmv_ea(env, lp);
3010 if (rc < (typeof(rc))sizeof(struct lmv_user_md)) {
3011 /* don't lookup for non-existing or invalid striping */
3012 lp->ldo_dir_def_striping_set = 0;
3013 lp->ldo_dir_def_striping_cached = 1;
3014 lp->ldo_dir_def_stripenr = 0;
3015 lp->ldo_dir_def_stripe_offset =
3016 (typeof(v1->lum_stripe_offset))(-1);
3017 lp->ldo_dir_def_hash_type = LMV_HASH_TYPE_FNV_1A_64;
3018 GOTO(unlock, rc = 0);
3022 v1 = info->lti_ea_store;
3024 lp->ldo_dir_def_stripenr = le32_to_cpu(v1->lum_stripe_count);
3025 lp->ldo_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
3026 lp->ldo_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
3027 lp->ldo_dir_def_striping_set = 1;
3028 lp->ldo_dir_def_striping_cached = 1;
3032 dt_write_unlock(env, dt_object_child(&lp->ldo_obj));
3037 * Cache default striping in the object.
3039 * To improve performance of striped object creation we cache default striping
3040 * (if it exists) in the parent directory object. We always cache default
3041 * striping for the regular files (stored in LOV EA) and we cache default
3042 * striping for the directories if requested by \a child_mode (when a new
3043 * directory is being created).
3045 * \param[in] env execution environment
3046 * \param[in] lp object
3047 * \param[in] child_mode new object's mode
3049 * \retval 0 on success
3050 * \retval negative if failed
3052 static int lod_cache_parent_striping(const struct lu_env *env,
3053 struct lod_object *lp,
3059 if (!lp->ldo_def_striping_cached) {
3060 /* we haven't tried to get default striping for
3061 * the directory yet, let's cache it in the object */
3062 rc = lod_cache_parent_lov_striping(env, lp);
3067 /* If the parent is on the remote MDT, we should always
3068 * try to refresh the default stripeEA cache, because we
3069 * do not cache default striping information for remote
3071 if (S_ISDIR(child_mode) && (!lp->ldo_dir_def_striping_cached ||
3072 dt_object_remote(&lp->ldo_obj)))
3073 rc = lod_cache_parent_lmv_striping(env, lp);
3079 * Implementation of dt_object_operations::do_ah_init.
3081 * This method is used to make a decision on the striping configuration for the
3082 * object being created. It can be taken from the \a parent object if it exists,
3083 * or filesystem's default. The resulting configuration (number of stripes,
3084 * stripe size/offset, pool name, etc) is stored in the object itself and will
3085 * be used by the methods like ->doo_declare_create().
3087 * \see dt_object_operations::do_ah_init() in the API description for details.
3089 static void lod_ah_init(const struct lu_env *env,
3090 struct dt_allocation_hint *ah,
3091 struct dt_object *parent,
3092 struct dt_object *child,
3095 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
3096 struct dt_object *nextp = NULL;
3097 struct dt_object *nextc;
3098 struct lod_object *lp = NULL;
3099 struct lod_object *lc;
3100 struct lov_desc *desc;
3106 if (likely(parent)) {
3107 nextp = dt_object_child(parent);
3108 lp = lod_dt_obj(parent);
3109 rc = lod_load_striping(env, lp);
3114 nextc = dt_object_child(child);
3115 lc = lod_dt_obj(child);
3117 LASSERT(lc->ldo_stripenr == 0);
3118 LASSERT(lc->ldo_stripe == NULL);
3121 * local object may want some hints
3122 * in case of late striping creation, ->ah_init()
3123 * can be called with local object existing
3125 if (!dt_object_exists(nextc) || dt_object_remote(nextc)) {
3126 struct dt_object *obj;
3128 obj = (nextp != NULL && dt_object_remote(nextp)) ? NULL : nextp;
3129 nextc->do_ops->do_ah_init(env, ah, obj, nextc, child_mode);
3132 if (S_ISDIR(child_mode)) {
3133 if (lc->ldo_dir_stripe == NULL) {
3134 OBD_ALLOC_PTR(lc->ldo_dir_stripe);
3135 if (lc->ldo_dir_stripe == NULL)
3139 LASSERT(lp != NULL);
3140 if (lp->ldo_dir_stripe == NULL) {
3141 OBD_ALLOC_PTR(lp->ldo_dir_stripe);
3142 if (lp->ldo_dir_stripe == NULL)
3146 rc = lod_cache_parent_striping(env, lp, child_mode);
3150 /* transfer defaults to new directory */
3151 if (lp->ldo_def_striping_set) {
3153 lod_object_set_pool(lc, lp->ldo_pool);
3154 lc->ldo_def_stripenr = lp->ldo_def_stripenr;
3155 lc->ldo_def_stripe_size = lp->ldo_def_stripe_size;
3156 lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
3157 lc->ldo_def_striping_set = 1;
3158 lc->ldo_def_striping_cached = 1;
3159 CDEBUG(D_OTHER, "inherite EA sz:%d off:%d nr:%d\n",
3160 (int)lc->ldo_def_stripe_size,
3161 (int)lc->ldo_def_stripe_offset,
3162 (int)lc->ldo_def_stripenr);
3165 /* transfer dir defaults to new directory */
3166 if (lp->ldo_dir_def_striping_set) {
3167 lc->ldo_dir_def_stripenr = lp->ldo_dir_def_stripenr;
3168 lc->ldo_dir_def_stripe_offset =
3169 lp->ldo_dir_def_stripe_offset;
3170 lc->ldo_dir_def_hash_type =
3171 lp->ldo_dir_def_hash_type;
3172 lc->ldo_dir_def_striping_set = 1;
3173 lc->ldo_dir_def_striping_cached = 1;
3174 CDEBUG(D_INFO, "inherit default EA nr:%d off:%d t%u\n",
3175 (int)lc->ldo_dir_def_stripenr,
3176 (int)lc->ldo_dir_def_stripe_offset,
3177 lc->ldo_dir_def_hash_type);
3180 /* It should always honour the specified stripes */
3181 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0) {
3182 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
3184 rc = lod_verify_md_striping(d, lum1);
3186 le32_to_cpu(lum1->lum_stripe_count) > 1) {
3187 /* Directory will be striped only if
3188 * stripe_count > 1 */
3190 le32_to_cpu(lum1->lum_stripe_count);
3191 lc->ldo_dir_stripe_offset =
3192 le32_to_cpu(lum1->lum_stripe_offset);
3193 lc->ldo_dir_hash_type =
3194 le32_to_cpu(lum1->lum_hash_type);
3195 CDEBUG(D_INFO, "set stripe EA nr:%hu off:%d\n",
3197 (int)lc->ldo_dir_stripe_offset);
3199 /* then check whether there is default stripes from parent */
3200 } else if (lp->ldo_dir_def_striping_set) {
3201 /* If there are default dir stripe from parent */
3202 lc->ldo_stripenr = lp->ldo_dir_def_stripenr;
3203 lc->ldo_dir_stripe_offset =
3204 lp->ldo_dir_def_stripe_offset;
3205 lc->ldo_dir_hash_type =
3206 lp->ldo_dir_def_hash_type;
3207 CDEBUG(D_INFO, "inherit EA nr:%hu off:%d\n",
3209 (int)lc->ldo_dir_stripe_offset);
3211 /* set default stripe for this directory */
3212 lc->ldo_stripenr = 0;
3213 lc->ldo_dir_stripe_offset = -1;
3216 CDEBUG(D_INFO, "final striping count:%hu, offset:%d\n",
3217 lc->ldo_stripenr, (int)lc->ldo_dir_stripe_offset);
3223 * if object is going to be striped over OSTs, transfer default
3224 * striping information to the child, so that we can use it
3225 * during declaration and creation
3227 if (!lod_object_will_be_striped(S_ISREG(child_mode),
3228 lu_object_fid(&child->do_lu)))
3231 * try from the parent
3233 if (likely(parent)) {
3234 lod_cache_parent_striping(env, lp, child_mode);
3236 lc->ldo_def_stripe_offset = LOV_OFFSET_DEFAULT;
3238 if (lp->ldo_def_striping_set) {
3240 lod_object_set_pool(lc, lp->ldo_pool);
3241 lc->ldo_stripenr = lp->ldo_def_stripenr;
3242 lc->ldo_stripe_size = lp->ldo_def_stripe_size;
3243 lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
3244 CDEBUG(D_OTHER, "striping from parent: #%d, sz %d %s\n",
3245 lc->ldo_stripenr, lc->ldo_stripe_size,
3246 lp->ldo_pool ? lp->ldo_pool : "");
3251 * if the parent doesn't provide with specific pattern, grab fs-wide one
3253 desc = &d->lod_desc;
3254 if (lc->ldo_stripenr == 0)
3255 lc->ldo_stripenr = desc->ld_default_stripe_count;
3256 if (lc->ldo_stripe_size == 0)
3257 lc->ldo_stripe_size = desc->ld_default_stripe_size;
3258 CDEBUG(D_OTHER, "final striping: # %d stripes, sz %d from %s\n",
3259 lc->ldo_stripenr, lc->ldo_stripe_size,
3260 lc->ldo_pool ? lc->ldo_pool : "");
3263 /* we do not cache stripe information for slave stripe, see
3264 * lod_xattr_set_lov_on_dir */
3265 if (lp != NULL && lp->ldo_dir_slave_stripe)
3266 lod_lov_stripe_cache_clear(lp);
3271 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
3273 * Size initialization on late striping.
3275 * Propagate the size of a truncated object to a deferred striping.
3276 * This function handles a special case when truncate was done on a
3277 * non-striped object and now while the striping is being created
3278 * we can't lose that size, so we have to propagate it to the stripes
3281 * \param[in] env execution environment
3282 * \param[in] dt object
3283 * \param[in] th transaction handle
3285 * \retval 0 on success
3286 * \retval negative if failed
3288 static int lod_declare_init_size(const struct lu_env *env,
3289 struct dt_object *dt, struct thandle *th)
3291 struct dt_object *next = dt_object_child(dt);
3292 struct lod_object *lo = lod_dt_obj(dt);
3293 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3294 uint64_t size, offs;
3298 /* XXX: we support the simplest (RAID0) striping so far */
3299 LASSERT(lo->ldo_stripe || lo->ldo_stripenr == 0);
3300 LASSERT(lo->ldo_stripe_size > 0);
3302 rc = dt_attr_get(env, next, attr);
3303 LASSERT(attr->la_valid & LA_SIZE);
3307 size = attr->la_size;
3311 /* ll_do_div64(a, b) returns a % b, and a = a / b */
3312 ll_do_div64(size, (__u64) lo->ldo_stripe_size);
3313 stripe = ll_do_div64(size, (__u64) lo->ldo_stripenr);
3315 size = size * lo->ldo_stripe_size;
3316 offs = attr->la_size;
3317 size += ll_do_div64(offs, lo->ldo_stripe_size);
3319 attr->la_valid = LA_SIZE;
3320 attr->la_size = size;
3322 rc = dt_declare_attr_set(env, lo->ldo_stripe[stripe], attr, th);
3328 * Declare creation of striped object.
3330 * The function declares creation stripes for a regular object. The function
3331 * also declares whether the stripes will be created with non-zero size if
3332 * previously size was set non-zero on the master object. If object \a dt is
3333 * not local, then only fully defined striping can be applied in \a lovea.
3334 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
3337 * \param[in] env execution environment
3338 * \param[in] dt object
3339 * \param[in] attr attributes the stripes will be created with
3340 * \param[in] lovea a buffer containing striping description
3341 * \param[in] th transaction handle
3343 * \retval 0 on success
3344 * \retval negative if failed
3346 int lod_declare_striped_object(const struct lu_env *env, struct dt_object *dt,
3347 struct lu_attr *attr,
3348 const struct lu_buf *lovea, struct thandle *th)
3350 struct lod_thread_info *info = lod_env_info(env);
3351 struct dt_object *next = dt_object_child(dt);
3352 struct lod_object *lo = lod_dt_obj(dt);
3356 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO)) {
3357 /* failed to create striping, let's reset
3358 * config so that others don't get confused */
3359 lod_object_free_striping(env, lo);
3360 GOTO(out, rc = -ENOMEM);
3363 if (!dt_object_remote(next)) {
3364 /* choose OST and generate appropriate objects */
3365 rc = lod_qos_prep_create(env, lo, attr, lovea, th);
3367 /* failed to create striping, let's reset
3368 * config so that others don't get confused */
3369 lod_object_free_striping(env, lo);
3374 * declare storage for striping data
3376 info->lti_buf.lb_len = lov_mds_md_size(lo->ldo_stripenr,
3377 lo->ldo_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1);
3379 /* LOD can not choose OST objects for remote objects, i.e.
3380 * stripes must be ready before that. Right now, it can only
3381 * happen during migrate, i.e. migrate process needs to create
3382 * remote regular file (mdd_migrate_create), then the migrate
3383 * process will provide stripeEA. */
3384 LASSERT(lovea != NULL);
3385 info->lti_buf = *lovea;
3388 rc = dt_declare_xattr_set(env, next, &info->lti_buf,
3389 XATTR_NAME_LOV, 0, th);
3394 * if striping is created with local object's size > 0,
3395 * we have to propagate this size to specific object
3396 * the case is possible only when local object was created previously
3398 if (dt_object_exists(next))
3399 rc = lod_declare_init_size(env, dt, th);
3406 * Implementation of dt_object_operations::do_declare_create.
3408 * The method declares creation of a new object. If the object will be striped,
3409 * then helper functions are called to find FIDs for the stripes, declare
3410 * creation of the stripes and declare initialization of the striping
3411 * information to be stored in the master object.
3413 * \see dt_object_operations::do_declare_create() in the API description
3416 static int lod_declare_object_create(const struct lu_env *env,
3417 struct dt_object *dt,
3418 struct lu_attr *attr,
3419 struct dt_allocation_hint *hint,
3420 struct dt_object_format *dof,
3423 struct dt_object *next = dt_object_child(dt);
3424 struct lod_object *lo = lod_dt_obj(dt);
3433 * first of all, we declare creation of local object
3435 rc = dt_declare_create(env, next, attr, hint, dof, th);
3439 if (dof->dof_type == DFT_SYM)
3440 dt->do_body_ops = &lod_body_lnk_ops;
3443 * it's lod_ah_init() that has decided the object will be striped
3445 if (dof->dof_type == DFT_REGULAR) {
3446 /* callers don't want stripes */
3447 /* XXX: all tricky interactions with ->ah_make_hint() decided
3448 * to use striping, then ->declare_create() behaving differently
3449 * should be cleaned */
3450 if (dof->u.dof_reg.striped == 0)
3451 lo->ldo_stripenr = 0;
3452 if (lo->ldo_stripenr > 0)
3453 rc = lod_declare_striped_object(env, dt, attr,
3455 } else if (dof->dof_type == DFT_DIR) {
3456 struct seq_server_site *ss;
3458 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
3460 /* If the parent has default stripeEA, and client
3461 * did not find it before sending create request,
3462 * then MDT will return -EREMOTE, and client will
3463 * retrieve the default stripeEA and re-create the
3466 * Note: if dah_eadata != NULL, it means creating the
3467 * striped directory with specified stripeEA, then it
3468 * should ignore the default stripeEA */
3469 if ((hint == NULL || hint->dah_eadata == NULL) &&
3470 lo->ldo_dir_stripe_offset != -1 &&
3471 lo->ldo_dir_stripe_offset != ss->ss_node_id)
3472 GOTO(out, rc = -EREMOTE);
3474 /* Orphan object (like migrating object) does not have
3475 * lod_dir_stripe, see lod_ah_init */
3476 if (lo->ldo_dir_stripe != NULL)
3477 rc = lod_declare_dir_striping_create(env, dt, attr,
3485 * Creation of a striped regular object.
3487 * The function is called to create the stripe objects for a regular
3488 * striped file. This can happen at the initial object creation or
3489 * when the caller asks LOD to do so using ->do_xattr_set() method
3490 * (so called late striping). Notice all the information are already
3491 * prepared in the form of the list of objects (ldo_stripe field).
3492 * This is done during declare phase.
3494 * \param[in] env execution environment
3495 * \param[in] dt object
3496 * \param[in] attr attributes the stripes will be created with
3497 * \param[in] dof format of stripes (see OSD API description)
3498 * \param[in] th transaction handle
3500 * \retval 0 on success
3501 * \retval negative if failed
3503 int lod_striping_create(const struct lu_env *env, struct dt_object *dt,
3504 struct lu_attr *attr, struct dt_object_format *dof,
3507 struct lod_object *lo = lod_dt_obj(dt);
3511 LASSERT(lo->ldo_striping_cached == 0);
3513 /* create all underlying objects */
3514 for (i = 0; i < lo->ldo_stripenr; i++) {
3515 LASSERT(lo->ldo_stripe[i]);
3516 rc = dt_create(env, lo->ldo_stripe[i], attr, NULL, dof, th);
3523 rc = lod_generate_and_set_lovea(env, lo, th);
3525 lo->ldo_striping_cached = 1;
3532 * Implementation of dt_object_operations::do_create.
3534 * If any of preceeding methods (like ->do_declare_create(),
3535 * ->do_ah_init(), etc) chose to create a striped object,
3536 * then this method will create the master and the stripes.
3538 * \see dt_object_operations::do_create() in the API description for details.
3540 static int lod_object_create(const struct lu_env *env, struct dt_object *dt,
3541 struct lu_attr *attr,
3542 struct dt_allocation_hint *hint,
3543 struct dt_object_format *dof, struct thandle *th)
3545 struct dt_object *next = dt_object_child(dt);
3546 struct lod_object *lo = lod_dt_obj(dt);
3550 /* create local object */
3551 rc = dt_create(env, next, attr, hint, dof, th);
3555 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3556 lo->ldo_stripe && dof->u.dof_reg.striped != 0)
3557 rc = lod_striping_create(env, dt, attr, dof, th);
3563 * Implementation of dt_object_operations::do_declare_destroy.
3565 * If the object is a striped directory, then the function declares reference
3566 * removal from the master object (this is an index) to the stripes and declares
3567 * destroy of all the stripes. In all the cases, it declares an intention to
3568 * destroy the object itself.
3570 * \see dt_object_operations::do_declare_destroy() in the API description
3573 static int lod_declare_object_destroy(const struct lu_env *env,
3574 struct dt_object *dt,
3577 struct dt_object *next = dt_object_child(dt);
3578 struct lod_object *lo = lod_dt_obj(dt);
3579 struct lod_thread_info *info = lod_env_info(env);
3580 char *stripe_name = info->lti_key;
3585 * load striping information, notice we don't do this when object
3586 * is being initialized as we don't need this information till
3587 * few specific cases like destroy, chown
3589 rc = lod_load_striping(env, lo);
3593 /* declare destroy for all underlying objects */
3594 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3595 rc = next->do_ops->do_index_try(env, next,
3596 &dt_directory_features);
3600 for (i = 0; i < lo->ldo_stripenr; i++) {
3601 rc = dt_declare_ref_del(env, next, th);
3604 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3605 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
3607 rc = dt_declare_delete(env, next,
3608 (const struct dt_key *)stripe_name, th);
3614 * we declare destroy for the local object
3616 rc = dt_declare_destroy(env, next, th);
3620 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ))
3623 /* declare destroy all striped objects */
3624 for (i = 0; i < lo->ldo_stripenr; i++) {
3625 if (likely(lo->ldo_stripe[i] != NULL)) {
3626 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3627 rc = dt_declare_ref_del(env, lo->ldo_stripe[i],
3633 rc = dt_declare_destroy(env, lo->ldo_stripe[i], th);
3643 * Implementation of dt_object_operations::do_destroy.
3645 * If the object is a striped directory, then the function removes references
3646 * from the master object (this is an index) to the stripes and destroys all
3647 * the stripes. In all the cases, the function destroys the object itself.
3649 * \see dt_object_operations::do_destroy() in the API description for details.
3651 static int lod_object_destroy(const struct lu_env *env,
3652 struct dt_object *dt, struct thandle *th)
3654 struct dt_object *next = dt_object_child(dt);
3655 struct lod_object *lo = lod_dt_obj(dt);
3656 struct lod_thread_info *info = lod_env_info(env);
3657 char *stripe_name = info->lti_key;
3662 /* destroy sub-stripe of master object */
3663 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3664 rc = next->do_ops->do_index_try(env, next,
3665 &dt_directory_features);
3669 for (i = 0; i < lo->ldo_stripenr; i++) {
3670 rc = dt_ref_del(env, next, th);
3674 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3675 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
3678 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
3679 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
3680 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
3682 rc = dt_delete(env, next,
3683 (const struct dt_key *)stripe_name, th);
3688 rc = dt_destroy(env, next, th);
3692 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ))
3695 /* destroy all striped objects */
3696 for (i = 0; i < lo->ldo_stripenr; i++) {
3697 if (likely(lo->ldo_stripe[i] != NULL) &&
3698 (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
3699 i == cfs_fail_val)) {
3700 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3701 dt_write_lock(env, lo->ldo_stripe[i],
3703 rc = dt_ref_del(env, lo->ldo_stripe[i], th);
3704 dt_write_unlock(env, lo->ldo_stripe[i]);
3709 rc = dt_destroy(env, lo->ldo_stripe[i], th);
3719 * Implementation of dt_object_operations::do_declare_ref_add.
3721 * \see dt_object_operations::do_declare_ref_add() in the API description
3724 static int lod_declare_ref_add(const struct lu_env *env,
3725 struct dt_object *dt, struct thandle *th)
3727 return dt_declare_ref_add(env, dt_object_child(dt), th);
3731 * Implementation of dt_object_operations::do_ref_add.
3733 * \see dt_object_operations::do_ref_add() in the API description for details.
3735 static int lod_ref_add(const struct lu_env *env,
3736 struct dt_object *dt, struct thandle *th)
3738 return dt_ref_add(env, dt_object_child(dt), th);
3742 * Implementation of dt_object_operations::do_declare_ref_del.
3744 * \see dt_object_operations::do_declare_ref_del() in the API description
3747 static int lod_declare_ref_del(const struct lu_env *env,
3748 struct dt_object *dt, struct thandle *th)
3750 return dt_declare_ref_del(env, dt_object_child(dt), th);
3754 * Implementation of dt_object_operations::do_ref_del
3756 * \see dt_object_operations::do_ref_del() in the API description for details.
3758 static int lod_ref_del(const struct lu_env *env,
3759 struct dt_object *dt, struct thandle *th)
3761 return dt_ref_del(env, dt_object_child(dt), th);
3765 * Implementation of dt_object_operations::do_object_sync.
3767 * \see dt_object_operations::do_object_sync() in the API description
3770 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
3771 __u64 start, __u64 end)
3773 return dt_object_sync(env, dt_object_child(dt), start, end);
3776 struct lod_slave_locks {
3778 struct lustre_handle lsl_handle[0];
3782 * Release LDLM locks on the stripes of a striped directory.
3784 * Iterates over all the locks taken on the stripe objects and
3785 * release them using ->do_object_unlock() method.
3787 * \param[in] env execution environment
3788 * \param[in] dt striped object
3789 * \param[in] einfo lock description
3790 * \param[in] policy data describing requested lock
3792 * \retval 0 on success
3793 * \retval negative if failed
3795 static int lod_object_unlock_internal(const struct lu_env *env,
3796 struct dt_object *dt,
3797 struct ldlm_enqueue_info *einfo,
3798 ldlm_policy_data_t *policy)
3800 struct lod_object *lo = lod_dt_obj(dt);
3801 struct lod_slave_locks *slave_locks = einfo->ei_cbdata;
3806 if (slave_locks == NULL)
3809 for (i = 1; i < slave_locks->lsl_lock_count; i++) {
3810 if (lustre_handle_is_used(&slave_locks->lsl_handle[i])) {
3813 einfo->ei_cbdata = &slave_locks->lsl_handle[i];
3814 rc1 = dt_object_unlock(env, lo->ldo_stripe[i], einfo,
3817 rc = rc == 0 ? rc1 : rc;
3825 * Implementation of dt_object_operations::do_object_unlock.
3827 * Used to release LDLM lock(s).
3829 * \see dt_object_operations::do_object_unlock() in the API description
3832 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
3833 struct ldlm_enqueue_info *einfo,
3834 union ldlm_policy_data *policy)
3836 struct lod_object *lo = lod_dt_obj(dt);
3837 struct lod_slave_locks *slave_locks = einfo->ei_cbdata;
3838 int slave_locks_size;
3842 if (slave_locks == NULL)
3845 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3848 rc = lod_load_striping(env, lo);
3852 /* Note: for remote lock for single stripe dir, MDT will cancel
3853 * the lock by lockh directly */
3854 if (lo->ldo_stripenr <= 1 && dt_object_remote(dt_object_child(dt)))
3857 /* Only cancel slave lock for striped dir */
3858 rc = lod_object_unlock_internal(env, dt, einfo, policy);
3860 slave_locks_size = sizeof(*slave_locks) + slave_locks->lsl_lock_count *
3861 sizeof(slave_locks->lsl_handle[0]);
3862 OBD_FREE(slave_locks, slave_locks_size);
3863 einfo->ei_cbdata = NULL;
3869 * Implementation of dt_object_operations::do_object_lock.
3871 * Used to get LDLM lock on the non-striped and striped objects.
3873 * \see dt_object_operations::do_object_lock() in the API description
3876 static int lod_object_lock(const struct lu_env *env,
3877 struct dt_object *dt,
3878 struct lustre_handle *lh,
3879 struct ldlm_enqueue_info *einfo,
3880 union ldlm_policy_data *policy)
3882 struct lod_object *lo = lod_dt_obj(dt);
3885 int slave_locks_size;
3886 struct lod_slave_locks *slave_locks = NULL;
3889 /* remote object lock */
3890 if (!einfo->ei_enq_slave) {
3891 LASSERT(dt_object_remote(dt));
3892 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
3896 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3899 rc = lod_load_striping(env, lo);
3904 if (lo->ldo_stripenr <= 1)
3907 slave_locks_size = sizeof(*slave_locks) + lo->ldo_stripenr *
3908 sizeof(slave_locks->lsl_handle[0]);
3909 /* Freed in lod_object_unlock */
3910 OBD_ALLOC(slave_locks, slave_locks_size);
3911 if (slave_locks == NULL)
3913 slave_locks->lsl_lock_count = lo->ldo_stripenr;
3915 /* striped directory lock */
3916 for (i = 1; i < lo->ldo_stripenr; i++) {
3917 struct lustre_handle lockh;
3918 struct ldlm_res_id *res_id;
3920 res_id = &lod_env_info(env)->lti_res_id;
3921 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
3923 einfo->ei_res_id = res_id;
3925 LASSERT(lo->ldo_stripe[i]);
3926 rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh, einfo,
3930 slave_locks->lsl_handle[i] = lockh;
3933 einfo->ei_cbdata = slave_locks;
3936 if (rc != 0 && slave_locks != NULL) {
3937 einfo->ei_cbdata = slave_locks;
3938 lod_object_unlock_internal(env, dt, einfo, policy);
3939 OBD_FREE(slave_locks, slave_locks_size);
3940 einfo->ei_cbdata = NULL;
3946 struct dt_object_operations lod_obj_ops = {
3947 .do_read_lock = lod_object_read_lock,
3948 .do_write_lock = lod_object_write_lock,
3949 .do_read_unlock = lod_object_read_unlock,
3950 .do_write_unlock = lod_object_write_unlock,
3951 .do_write_locked = lod_object_write_locked,
3952 .do_attr_get = lod_attr_get,
3953 .do_declare_attr_set = lod_declare_attr_set,
3954 .do_attr_set = lod_attr_set,
3955 .do_xattr_get = lod_xattr_get,
3956 .do_declare_xattr_set = lod_declare_xattr_set,
3957 .do_xattr_set = lod_xattr_set,
3958 .do_declare_xattr_del = lod_declare_xattr_del,
3959 .do_xattr_del = lod_xattr_del,
3960 .do_xattr_list = lod_xattr_list,
3961 .do_ah_init = lod_ah_init,
3962 .do_declare_create = lod_declare_object_create,
3963 .do_create = lod_object_create,
3964 .do_declare_destroy = lod_declare_object_destroy,
3965 .do_destroy = lod_object_destroy,
3966 .do_index_try = lod_index_try,
3967 .do_declare_ref_add = lod_declare_ref_add,
3968 .do_ref_add = lod_ref_add,
3969 .do_declare_ref_del = lod_declare_ref_del,
3970 .do_ref_del = lod_ref_del,
3971 .do_object_sync = lod_object_sync,
3972 .do_object_lock = lod_object_lock,
3973 .do_object_unlock = lod_object_unlock,
3977 * Implementation of dt_body_operations::dbo_read.
3979 * \see dt_body_operations::dbo_read() in the API description for details.
3981 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
3982 struct lu_buf *buf, loff_t *pos)
3984 struct dt_object *next = dt_object_child(dt);
3985 return next->do_body_ops->dbo_read(env, next, buf, pos);
3989 * Implementation of dt_body_operations::dbo_declare_write.
3991 * \see dt_body_operations::dbo_declare_write() in the API description
3994 static ssize_t lod_declare_write(const struct lu_env *env,
3995 struct dt_object *dt,
3996 const struct lu_buf *buf, loff_t pos,
3999 return dt_declare_record_write(env, dt_object_child(dt),
4004 * Implementation of dt_body_operations::dbo_write.
4006 * \see dt_body_operations::dbo_write() in the API description for details.
4008 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
4009 const struct lu_buf *buf, loff_t *pos,
4010 struct thandle *th, int iq)
4012 struct dt_object *next = dt_object_child(dt);
4014 return next->do_body_ops->dbo_write(env, next, buf, pos, th, iq);
4017 static const struct dt_body_operations lod_body_lnk_ops = {
4018 .dbo_read = lod_read,
4019 .dbo_declare_write = lod_declare_write,
4020 .dbo_write = lod_write
4024 * Implementation of lu_object_operations::loo_object_init.
4026 * The function determines the type and the index of the target device using
4027 * sequence of the object's FID. Then passes control down to the
4028 * corresponding device:
4029 * OSD for the local objects, OSP for remote
4031 * \see lu_object_operations::loo_object_init() in the API description
4034 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
4035 const struct lu_object_conf *conf)
4037 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
4038 struct lu_device *cdev = NULL;
4039 struct lu_object *cobj;
4040 struct lod_tgt_descs *ltd = NULL;
4041 struct lod_tgt_desc *tgt;
4043 int type = LU_SEQ_RANGE_ANY;
4047 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
4049 /* Note: Sometimes, it will Return EAGAIN here, see
4050 * ptrlpc_import_delay_req(), which might confuse
4051 * lu_object_find_at() and make it wait there incorrectly.
4052 * so we convert it to EIO here.*/
4059 if (type == LU_SEQ_RANGE_MDT &&
4060 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
4061 cdev = &lod->lod_child->dd_lu_dev;
4062 } else if (type == LU_SEQ_RANGE_MDT) {
4063 ltd = &lod->lod_mdt_descs;
4065 } else if (type == LU_SEQ_RANGE_OST) {
4066 ltd = &lod->lod_ost_descs;
4073 if (ltd->ltd_tgts_size > idx &&
4074 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
4075 tgt = LTD_TGT(ltd, idx);
4077 LASSERT(tgt != NULL);
4078 LASSERT(tgt->ltd_tgt != NULL);
4080 cdev = &(tgt->ltd_tgt->dd_lu_dev);
4082 lod_putref(lod, ltd);
4085 if (unlikely(cdev == NULL))
4088 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
4089 if (unlikely(cobj == NULL))
4092 lu_object_add(lo, cobj);
4099 * Release resources associated with striping.
4101 * If the object is striped (regular or directory), then release
4102 * the stripe objects references and free the ldo_stripe array.
4104 * \param[in] env execution environment
4105 * \param[in] lo object
4107 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
4111 if (lo->ldo_dir_stripe != NULL) {
4112 OBD_FREE_PTR(lo->ldo_dir_stripe);
4113 lo->ldo_dir_stripe = NULL;
4116 if (lo->ldo_stripe) {
4117 LASSERT(lo->ldo_stripes_allocated > 0);
4119 for (i = 0; i < lo->ldo_stripenr; i++) {
4120 if (lo->ldo_stripe[i])
4121 lu_object_put(env, &lo->ldo_stripe[i]->do_lu);
4124 i = sizeof(struct dt_object *) * lo->ldo_stripes_allocated;
4125 OBD_FREE(lo->ldo_stripe, i);
4126 lo->ldo_stripe = NULL;
4127 lo->ldo_stripes_allocated = 0;
4129 lo->ldo_striping_cached = 0;
4130 lo->ldo_stripenr = 0;
4131 lo->ldo_pattern = 0;
4135 * Implementation of lu_object_operations::loo_object_start.
4137 * \see lu_object_operations::loo_object_start() in the API description
4140 static int lod_object_start(const struct lu_env *env, struct lu_object *o)
4142 if (S_ISLNK(o->lo_header->loh_attr & S_IFMT))
4143 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_lnk_ops;
4148 * Implementation of lu_object_operations::loo_object_free.
4150 * \see lu_object_operations::loo_object_free() in the API description
4153 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
4155 struct lod_object *mo = lu2lod_obj(o);
4158 * release all underlying object pinned
4161 lod_object_free_striping(env, mo);
4163 lod_object_set_pool(mo, NULL);
4166 OBD_SLAB_FREE_PTR(mo, lod_object_kmem);
4170 * Implementation of lu_object_operations::loo_object_release.
4172 * \see lu_object_operations::loo_object_release() in the API description
4175 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
4177 /* XXX: shouldn't we release everything here in case if object
4178 * creation failed before? */
4182 * Implementation of lu_object_operations::loo_object_print.
4184 * \see lu_object_operations::loo_object_print() in the API description
4187 static int lod_object_print(const struct lu_env *env, void *cookie,
4188 lu_printer_t p, const struct lu_object *l)
4190 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
4192 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
4195 struct lu_object_operations lod_lu_obj_ops = {
4196 .loo_object_init = lod_object_init,
4197 .loo_object_start = lod_object_start,
4198 .loo_object_free = lod_object_free,
4199 .loo_object_release = lod_object_release,
4200 .loo_object_print = lod_object_print,