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,
72 struct lustre_capa *capa)
74 struct dt_object *next = dt_object_child(dt);
75 return next->do_index_ops->dio_lookup(env, next, rec, key, capa);
79 * Implementation of dt_index_operations::dio_declare_insert.
81 * Used with regular (non-striped) objects.
83 * \see dt_index_operations::dio_declare_insert() in the API description
86 static int lod_declare_index_insert(const struct lu_env *env,
88 const struct dt_rec *rec,
89 const struct dt_key *key,
90 struct thandle *handle)
92 return dt_declare_insert(env, dt_object_child(dt), rec, key, handle);
96 * Implementation of dt_index_operations::dio_insert.
98 * Used with regular (non-striped) objects
100 * \see dt_index_operations::dio_insert() in the API description for details.
102 static int lod_index_insert(const struct lu_env *env,
103 struct dt_object *dt,
104 const struct dt_rec *rec,
105 const struct dt_key *key,
107 struct lustre_capa *capa,
110 return dt_insert(env, dt_object_child(dt), rec, key, th, capa, ign);
114 * Implementation of dt_index_operations::dio_declare_delete.
116 * Used with regular (non-striped) objects.
118 * \see dt_index_operations::dio_declare_delete() in the API description
121 static int lod_declare_index_delete(const struct lu_env *env,
122 struct dt_object *dt,
123 const struct dt_key *key,
126 return dt_declare_delete(env, dt_object_child(dt), key, th);
130 * Implementation of dt_index_operations::dio_delete.
132 * Used with regular (non-striped) objects.
134 * \see dt_index_operations::dio_delete() in the API description for details.
136 static int lod_index_delete(const struct lu_env *env,
137 struct dt_object *dt,
138 const struct dt_key *key,
140 struct lustre_capa *capa)
142 return dt_delete(env, dt_object_child(dt), key, th, capa);
146 * Implementation of dt_it_ops::init.
148 * Used with regular (non-striped) objects.
150 * \see dt_it_ops::init() in the API description for details.
152 static struct dt_it *lod_it_init(const struct lu_env *env,
153 struct dt_object *dt, __u32 attr,
154 struct lustre_capa *capa)
156 struct dt_object *next = dt_object_child(dt);
157 struct lod_it *it = &lod_env_info(env)->lti_it;
158 struct dt_it *it_next;
161 it_next = next->do_index_ops->dio_it.init(env, next, attr, capa);
165 /* currently we do not use more than one iterator per thread
166 * so we store it in thread info. if at some point we need
167 * more active iterators in a single thread, we can allocate
169 LASSERT(it->lit_obj == NULL);
171 it->lit_it = it_next;
174 return (struct dt_it *)it;
177 #define LOD_CHECK_IT(env, it) \
179 LASSERT((it)->lit_obj != NULL); \
180 LASSERT((it)->lit_it != NULL); \
184 * Implementation of dt_index_operations::dio_it.fini.
186 * Used with regular (non-striped) objects.
188 * \see dt_index_operations::dio_it.fini() in the API description for details.
190 static void lod_it_fini(const struct lu_env *env, struct dt_it *di)
192 struct lod_it *it = (struct lod_it *)di;
194 LOD_CHECK_IT(env, it);
195 it->lit_obj->do_index_ops->dio_it.fini(env, it->lit_it);
197 /* the iterator not in use any more */
203 * Implementation of dt_it_ops::get.
205 * Used with regular (non-striped) objects.
207 * \see dt_it_ops::get() in the API description for details.
209 static int lod_it_get(const struct lu_env *env, struct dt_it *di,
210 const struct dt_key *key)
212 const struct lod_it *it = (const struct lod_it *)di;
214 LOD_CHECK_IT(env, it);
215 return it->lit_obj->do_index_ops->dio_it.get(env, it->lit_it, key);
219 * Implementation of dt_it_ops::put.
221 * Used with regular (non-striped) objects.
223 * \see dt_it_ops::put() in the API description for details.
225 static void lod_it_put(const struct lu_env *env, struct dt_it *di)
227 struct lod_it *it = (struct lod_it *)di;
229 LOD_CHECK_IT(env, it);
230 return it->lit_obj->do_index_ops->dio_it.put(env, it->lit_it);
234 * Implementation of dt_it_ops::next.
236 * Used with regular (non-striped) objects
238 * \see dt_it_ops::next() in the API description for details.
240 static int lod_it_next(const struct lu_env *env, struct dt_it *di)
242 struct lod_it *it = (struct lod_it *)di;
244 LOD_CHECK_IT(env, it);
245 return it->lit_obj->do_index_ops->dio_it.next(env, it->lit_it);
249 * Implementation of dt_it_ops::key.
251 * Used with regular (non-striped) objects.
253 * \see dt_it_ops::key() in the API description for details.
255 static struct dt_key *lod_it_key(const struct lu_env *env,
256 const struct dt_it *di)
258 const struct lod_it *it = (const struct lod_it *)di;
260 LOD_CHECK_IT(env, it);
261 return it->lit_obj->do_index_ops->dio_it.key(env, it->lit_it);
265 * Implementation of dt_it_ops::key_size.
267 * Used with regular (non-striped) objects.
269 * \see dt_it_ops::key_size() in the API description for details.
271 static int lod_it_key_size(const struct lu_env *env, const struct dt_it *di)
273 struct lod_it *it = (struct lod_it *)di;
275 LOD_CHECK_IT(env, it);
276 return it->lit_obj->do_index_ops->dio_it.key_size(env, it->lit_it);
280 * Implementation of dt_it_ops::rec.
282 * Used with regular (non-striped) objects.
284 * \see dt_it_ops::rec() in the API description for details.
286 static int lod_it_rec(const struct lu_env *env, const struct dt_it *di,
287 struct dt_rec *rec, __u32 attr)
289 const struct lod_it *it = (const struct lod_it *)di;
291 LOD_CHECK_IT(env, it);
292 return it->lit_obj->do_index_ops->dio_it.rec(env, it->lit_it, rec,
297 * Implementation of dt_it_ops::rec_size.
299 * Used with regular (non-striped) objects.
301 * \see dt_it_ops::rec_size() in the API description for details.
303 static int lod_it_rec_size(const struct lu_env *env, const struct dt_it *di,
306 const struct lod_it *it = (const struct lod_it *)di;
308 LOD_CHECK_IT(env, it);
309 return it->lit_obj->do_index_ops->dio_it.rec_size(env, it->lit_it,
314 * Implementation of dt_it_ops::store.
316 * Used with regular (non-striped) objects.
318 * \see dt_it_ops::store() in the API description for details.
320 static __u64 lod_it_store(const struct lu_env *env, const struct dt_it *di)
322 const struct lod_it *it = (const struct lod_it *)di;
324 LOD_CHECK_IT(env, it);
325 return it->lit_obj->do_index_ops->dio_it.store(env, it->lit_it);
329 * Implementation of dt_it_ops::load.
331 * Used with regular (non-striped) objects.
333 * \see dt_it_ops::load() in the API description for details.
335 static int lod_it_load(const struct lu_env *env, const struct dt_it *di,
338 const struct lod_it *it = (const struct lod_it *)di;
340 LOD_CHECK_IT(env, it);
341 return it->lit_obj->do_index_ops->dio_it.load(env, it->lit_it, hash);
345 * Implementation of dt_it_ops::key_rec.
347 * Used with regular (non-striped) objects.
349 * \see dt_it_ops::rec() in the API description for details.
351 static int lod_it_key_rec(const struct lu_env *env, const struct dt_it *di,
354 const struct lod_it *it = (const struct lod_it *)di;
356 LOD_CHECK_IT(env, it);
357 return it->lit_obj->do_index_ops->dio_it.key_rec(env, it->lit_it,
361 static struct dt_index_operations lod_index_ops = {
362 .dio_lookup = lod_index_lookup,
363 .dio_declare_insert = lod_declare_index_insert,
364 .dio_insert = lod_index_insert,
365 .dio_declare_delete = lod_declare_index_delete,
366 .dio_delete = lod_index_delete,
374 .key_size = lod_it_key_size,
376 .rec_size = lod_it_rec_size,
377 .store = lod_it_store,
379 .key_rec = lod_it_key_rec,
384 * Implementation of dt_it_ops::init.
386 * Used with striped objects. Internally just initializes the iterator
387 * on the first stripe.
389 * \see dt_it_ops::init() in the API description for details.
391 static struct dt_it *lod_striped_it_init(const struct lu_env *env,
392 struct dt_object *dt, __u32 attr,
393 struct lustre_capa *capa)
395 struct lod_object *lo = lod_dt_obj(dt);
396 struct dt_object *next;
397 struct lod_it *it = &lod_env_info(env)->lti_it;
398 struct dt_it *it_next;
401 LASSERT(lo->ldo_stripenr > 0);
402 next = lo->ldo_stripe[0];
403 LASSERT(next != NULL);
404 LASSERT(next->do_index_ops != NULL);
406 it_next = next->do_index_ops->dio_it.init(env, next, attr, capa);
410 /* currently we do not use more than one iterator per thread
411 * so we store it in thread info. if at some point we need
412 * more active iterators in a single thread, we can allocate
414 LASSERT(it->lit_obj == NULL);
416 it->lit_stripe_index = 0;
418 it->lit_it = it_next;
421 return (struct dt_it *)it;
424 #define LOD_CHECK_STRIPED_IT(env, it, lo) \
426 LASSERT((it)->lit_obj != NULL); \
427 LASSERT((it)->lit_it != NULL); \
428 LASSERT((lo)->ldo_stripenr > 0); \
429 LASSERT((it)->lit_stripe_index < (lo)->ldo_stripenr); \
433 * Implementation of dt_it_ops::fini.
435 * Used with striped objects.
437 * \see dt_it_ops::fini() in the API description for details.
439 static void lod_striped_it_fini(const struct lu_env *env, struct dt_it *di)
441 struct lod_it *it = (struct lod_it *)di;
442 struct lod_object *lo = lod_dt_obj(it->lit_obj);
443 struct dt_object *next;
445 LOD_CHECK_STRIPED_IT(env, it, lo);
447 next = lo->ldo_stripe[it->lit_stripe_index];
448 LASSERT(next != NULL);
449 LASSERT(next->do_index_ops != NULL);
451 next->do_index_ops->dio_it.fini(env, it->lit_it);
453 /* the iterator not in use any more */
456 it->lit_stripe_index = 0;
460 * Implementation of dt_it_ops::get.
462 * Right now it's not used widely, only to reset the iterator to the
463 * initial position. It should be possible to implement a full version
464 * which chooses a correct stripe to be able to position with any key.
466 * \see dt_it_ops::get() in the API description for details.
468 static int lod_striped_it_get(const struct lu_env *env, struct dt_it *di,
469 const struct dt_key *key)
471 const struct lod_it *it = (const struct lod_it *)di;
472 struct lod_object *lo = lod_dt_obj(it->lit_obj);
473 struct dt_object *next;
476 LOD_CHECK_STRIPED_IT(env, it, lo);
478 next = lo->ldo_stripe[it->lit_stripe_index];
479 LASSERT(next != NULL);
480 LASSERT(next->do_index_ops != NULL);
482 return next->do_index_ops->dio_it.get(env, it->lit_it, key);
486 * Implementation of dt_it_ops::put.
488 * Used with striped objects.
490 * \see dt_it_ops::put() in the API description for details.
492 static void lod_striped_it_put(const struct lu_env *env, struct dt_it *di)
494 struct lod_it *it = (struct lod_it *)di;
495 struct lod_object *lo = lod_dt_obj(it->lit_obj);
496 struct dt_object *next;
498 LOD_CHECK_STRIPED_IT(env, it, lo);
500 next = lo->ldo_stripe[it->lit_stripe_index];
501 LASSERT(next != NULL);
502 LASSERT(next->do_index_ops != NULL);
504 return next->do_index_ops->dio_it.put(env, it->lit_it);
508 * Implementation of dt_it_ops::next.
510 * Used with striped objects. When the end of the current stripe is
511 * reached, the method takes the next stripe's iterator.
513 * \see dt_it_ops::next() in the API description for details.
515 static int lod_striped_it_next(const struct lu_env *env, struct dt_it *di)
517 struct lod_it *it = (struct lod_it *)di;
518 struct lod_object *lo = lod_dt_obj(it->lit_obj);
519 struct dt_object *next;
520 struct dt_it *it_next;
524 LOD_CHECK_STRIPED_IT(env, it, lo);
526 next = lo->ldo_stripe[it->lit_stripe_index];
527 LASSERT(next != NULL);
528 LASSERT(next->do_index_ops != NULL);
530 rc = next->do_index_ops->dio_it.next(env, it->lit_it);
534 if (rc == 0 && it->lit_stripe_index == 0)
537 if (rc == 0 && it->lit_stripe_index > 0) {
538 struct lu_dirent *ent;
540 ent = (struct lu_dirent *)lod_env_info(env)->lti_key;
542 rc = next->do_index_ops->dio_it.rec(env, it->lit_it,
543 (struct dt_rec *)ent,
548 /* skip . and .. for slave stripe */
549 if ((strncmp(ent->lde_name, ".",
550 le16_to_cpu(ent->lde_namelen)) == 0 &&
551 le16_to_cpu(ent->lde_namelen) == 1) ||
552 (strncmp(ent->lde_name, "..",
553 le16_to_cpu(ent->lde_namelen)) == 0 &&
554 le16_to_cpu(ent->lde_namelen) == 2))
560 /* go to next stripe */
561 if (it->lit_stripe_index + 1 >= lo->ldo_stripenr)
564 it->lit_stripe_index++;
566 next->do_index_ops->dio_it.put(env, it->lit_it);
567 next->do_index_ops->dio_it.fini(env, it->lit_it);
569 rc = next->do_ops->do_index_try(env, next, &dt_directory_features);
573 next = lo->ldo_stripe[it->lit_stripe_index];
574 LASSERT(next != NULL);
575 LASSERT(next->do_index_ops != NULL);
577 it_next = next->do_index_ops->dio_it.init(env, next, it->lit_attr,
579 if (!IS_ERR(it_next)) {
580 it->lit_it = it_next;
583 rc = PTR_ERR(it_next);
590 * Implementation of dt_it_ops::key.
592 * Used with striped objects.
594 * \see dt_it_ops::key() in the API description for details.
596 static struct dt_key *lod_striped_it_key(const struct lu_env *env,
597 const struct dt_it *di)
599 const struct lod_it *it = (const struct lod_it *)di;
600 struct lod_object *lo = lod_dt_obj(it->lit_obj);
601 struct dt_object *next;
603 LOD_CHECK_STRIPED_IT(env, it, lo);
605 next = lo->ldo_stripe[it->lit_stripe_index];
606 LASSERT(next != NULL);
607 LASSERT(next->do_index_ops != NULL);
609 return next->do_index_ops->dio_it.key(env, it->lit_it);
613 * Implementation of dt_it_ops::key_size.
615 * Used with striped objects.
617 * \see dt_it_ops::size() in the API description for details.
619 static int lod_striped_it_key_size(const struct lu_env *env,
620 const struct dt_it *di)
622 struct lod_it *it = (struct lod_it *)di;
623 struct lod_object *lo = lod_dt_obj(it->lit_obj);
624 struct dt_object *next;
626 LOD_CHECK_STRIPED_IT(env, it, lo);
628 next = lo->ldo_stripe[it->lit_stripe_index];
629 LASSERT(next != NULL);
630 LASSERT(next->do_index_ops != NULL);
632 return next->do_index_ops->dio_it.key_size(env, it->lit_it);
636 * Implementation of dt_it_ops::rec.
638 * Used with striped objects.
640 * \see dt_it_ops::rec() in the API description for details.
642 static int lod_striped_it_rec(const struct lu_env *env, const struct dt_it *di,
643 struct dt_rec *rec, __u32 attr)
645 const struct lod_it *it = (const struct lod_it *)di;
646 struct lod_object *lo = lod_dt_obj(it->lit_obj);
647 struct dt_object *next;
649 LOD_CHECK_STRIPED_IT(env, it, lo);
651 next = lo->ldo_stripe[it->lit_stripe_index];
652 LASSERT(next != NULL);
653 LASSERT(next->do_index_ops != NULL);
655 return next->do_index_ops->dio_it.rec(env, it->lit_it, rec, attr);
659 * Implementation of dt_it_ops::rec_size.
661 * Used with striped objects.
663 * \see dt_it_ops::rec_size() in the API description for details.
665 static int lod_striped_it_rec_size(const struct lu_env *env,
666 const struct dt_it *di, __u32 attr)
668 struct lod_it *it = (struct lod_it *)di;
669 struct lod_object *lo = lod_dt_obj(it->lit_obj);
670 struct dt_object *next;
672 LOD_CHECK_STRIPED_IT(env, it, lo);
674 next = lo->ldo_stripe[it->lit_stripe_index];
675 LASSERT(next != NULL);
676 LASSERT(next->do_index_ops != NULL);
678 return next->do_index_ops->dio_it.rec_size(env, it->lit_it, attr);
682 * Implementation of dt_it_ops::store.
684 * Used with striped objects.
686 * \see dt_it_ops::store() in the API description for details.
688 static __u64 lod_striped_it_store(const struct lu_env *env,
689 const struct dt_it *di)
691 const struct lod_it *it = (const struct lod_it *)di;
692 struct lod_object *lo = lod_dt_obj(it->lit_obj);
693 struct dt_object *next;
695 LOD_CHECK_STRIPED_IT(env, it, lo);
697 next = lo->ldo_stripe[it->lit_stripe_index];
698 LASSERT(next != NULL);
699 LASSERT(next->do_index_ops != NULL);
701 return next->do_index_ops->dio_it.store(env, it->lit_it);
705 * Implementation of dt_it_ops::load.
707 * Used with striped objects.
709 * \see dt_it_ops::load() in the API description for details.
711 static int lod_striped_it_load(const struct lu_env *env,
712 const struct dt_it *di, __u64 hash)
714 const struct lod_it *it = (const struct lod_it *)di;
715 struct lod_object *lo = lod_dt_obj(it->lit_obj);
716 struct dt_object *next;
718 LOD_CHECK_STRIPED_IT(env, it, lo);
720 next = lo->ldo_stripe[it->lit_stripe_index];
721 LASSERT(next != NULL);
722 LASSERT(next->do_index_ops != NULL);
724 return next->do_index_ops->dio_it.load(env, it->lit_it, hash);
727 static struct dt_index_operations lod_striped_index_ops = {
728 .dio_lookup = lod_index_lookup,
729 .dio_declare_insert = lod_declare_index_insert,
730 .dio_insert = lod_index_insert,
731 .dio_declare_delete = lod_declare_index_delete,
732 .dio_delete = lod_index_delete,
734 .init = lod_striped_it_init,
735 .fini = lod_striped_it_fini,
736 .get = lod_striped_it_get,
737 .put = lod_striped_it_put,
738 .next = lod_striped_it_next,
739 .key = lod_striped_it_key,
740 .key_size = lod_striped_it_key_size,
741 .rec = lod_striped_it_rec,
742 .rec_size = lod_striped_it_rec_size,
743 .store = lod_striped_it_store,
744 .load = lod_striped_it_load,
749 * Append the FID for each shard of the striped directory after the
750 * given LMV EA header.
752 * To simplify striped directory and the consistency verification,
753 * we only store the LMV EA header on disk, for both master object
754 * and slave objects. When someone wants to know the whole LMV EA,
755 * such as client readdir(), we can build the entrie LMV EA on the
756 * MDT side (in RAM) via iterating the sub-directory entries that
757 * are contained in the master object of the stripe directory.
759 * For the master object of the striped directroy, the valid name
760 * for each shard is composed of the ${shard_FID}:${shard_idx}.
762 * There may be holes in the LMV EA if some shards' name entries
763 * are corrupted or lost.
765 * \param[in] env pointer to the thread context
766 * \param[in] lo pointer to the master object of the striped directory
767 * \param[in] buf pointer to the lu_buf which will hold the LMV EA
768 * \param[in] resize whether re-allocate the buffer if it is not big enough
770 * \retval positive size of the LMV EA
771 * \retval 0 for nothing to be loaded
772 * \retval negative error number on failure
774 int lod_load_lmv_shards(const struct lu_env *env, struct lod_object *lo,
775 struct lu_buf *buf, bool resize)
777 struct lu_dirent *ent =
778 (struct lu_dirent *)lod_env_info(env)->lti_key;
779 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
780 struct dt_object *obj = dt_object_child(&lo->ldo_obj);
781 struct lmv_mds_md_v1 *lmv1 = buf->lb_buf;
783 const struct dt_it_ops *iops;
785 __u32 magic = le32_to_cpu(lmv1->lmv_magic);
790 /* If it is not a striped directory, then load nothing. */
791 if (magic != LMV_MAGIC_V1)
794 /* If it is in migration (or failure), then load nothing. */
795 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
798 stripes = le32_to_cpu(lmv1->lmv_stripe_count);
802 rc = lmv_mds_md_size(stripes, magic);
806 if (buf->lb_len < lmv1_size) {
815 lu_buf_alloc(buf, lmv1_size);
820 memcpy(buf->lb_buf, tbuf.lb_buf, tbuf.lb_len);
823 if (unlikely(!dt_try_as_dir(env, obj)))
826 memset(&lmv1->lmv_stripe_fids[0], 0, stripes * sizeof(struct lu_fid));
827 iops = &obj->do_index_ops->dio_it;
828 it = iops->init(env, obj, LUDA_64BITHASH, BYPASS_CAPA);
832 rc = iops->load(env, it, 0);
834 rc = iops->next(env, it);
839 char name[FID_LEN + 2] = "";
844 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
850 fid_le_to_cpu(&fid, &ent->lde_fid);
851 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
852 if (ent->lde_name[0] == '.') {
853 if (ent->lde_namelen == 1)
856 if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
860 len = snprintf(name, FID_LEN + 1, DFID":", PFID(&ent->lde_fid));
861 /* The ent->lde_name is composed of ${FID}:${index} */
862 if (ent->lde_namelen < len + 1 ||
863 memcmp(ent->lde_name, name, len) != 0) {
864 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
865 "%s: invalid shard name %.*s with the FID "DFID
866 " for the striped directory "DFID", %s\n",
867 lod2obd(lod)->obd_name, ent->lde_namelen,
868 ent->lde_name, PFID(&fid),
869 PFID(lu_object_fid(&obj->do_lu)),
870 lod->lod_lmv_failout ? "failout" : "skip");
872 if (lod->lod_lmv_failout)
880 if (ent->lde_name[len] < '0' ||
881 ent->lde_name[len] > '9') {
882 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
883 "%s: invalid shard name %.*s with the "
884 "FID "DFID" for the striped directory "
886 lod2obd(lod)->obd_name, ent->lde_namelen,
887 ent->lde_name, PFID(&fid),
888 PFID(lu_object_fid(&obj->do_lu)),
889 lod->lod_lmv_failout ?
892 if (lod->lod_lmv_failout)
898 index = index * 10 + ent->lde_name[len++] - '0';
899 } while (len < ent->lde_namelen);
901 if (len == ent->lde_namelen) {
902 /* Out of LMV EA range. */
903 if (index >= stripes) {
904 CERROR("%s: the shard %.*s for the striped "
905 "directory "DFID" is out of the known "
906 "LMV EA range [0 - %u], failout\n",
907 lod2obd(lod)->obd_name, ent->lde_namelen,
909 PFID(lu_object_fid(&obj->do_lu)),
915 /* The slot has been occupied. */
916 if (!fid_is_zero(&lmv1->lmv_stripe_fids[index])) {
920 &lmv1->lmv_stripe_fids[index]);
921 CERROR("%s: both the shard "DFID" and "DFID
922 " for the striped directory "DFID
923 " claim the same LMV EA slot at the "
924 "index %d, failout\n",
925 lod2obd(lod)->obd_name,
926 PFID(&fid0), PFID(&fid),
927 PFID(lu_object_fid(&obj->do_lu)), index);
932 /* stored as LE mode */
933 lmv1->lmv_stripe_fids[index] = ent->lde_fid;
936 rc = iops->next(env, it);
943 RETURN(rc > 0 ? lmv_mds_md_size(stripes, magic) : rc);
947 * Implementation of dt_object_operations::do_index_try.
949 * \see dt_object_operations::do_index_try() in the API description for details.
951 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
952 const struct dt_index_features *feat)
954 struct lod_object *lo = lod_dt_obj(dt);
955 struct dt_object *next = dt_object_child(dt);
959 LASSERT(next->do_ops);
960 LASSERT(next->do_ops->do_index_try);
962 rc = lod_load_striping_locked(env, lo);
966 rc = next->do_ops->do_index_try(env, next, feat);
970 if (lo->ldo_stripenr > 0) {
973 for (i = 0; i < lo->ldo_stripenr; i++) {
974 if (dt_object_exists(lo->ldo_stripe[i]) == 0)
976 rc = lo->ldo_stripe[i]->do_ops->do_index_try(env,
977 lo->ldo_stripe[i], feat);
981 dt->do_index_ops = &lod_striped_index_ops;
983 dt->do_index_ops = &lod_index_ops;
990 * Implementation of dt_object_operations::do_read_lock.
992 * \see dt_object_operations::do_read_lock() in the API description for details.
994 static void lod_object_read_lock(const struct lu_env *env,
995 struct dt_object *dt, unsigned role)
997 dt_read_lock(env, dt_object_child(dt), role);
1001 * Implementation of dt_object_operations::do_write_lock.
1003 * \see dt_object_operations::do_write_lock() in the API description for
1006 static void lod_object_write_lock(const struct lu_env *env,
1007 struct dt_object *dt, unsigned role)
1009 dt_write_lock(env, dt_object_child(dt), role);
1013 * Implementation of dt_object_operations::do_read_unlock.
1015 * \see dt_object_operations::do_read_unlock() in the API description for
1018 static void lod_object_read_unlock(const struct lu_env *env,
1019 struct dt_object *dt)
1021 dt_read_unlock(env, dt_object_child(dt));
1025 * Implementation of dt_object_operations::do_write_unlock.
1027 * \see dt_object_operations::do_write_unlock() in the API description for
1030 static void lod_object_write_unlock(const struct lu_env *env,
1031 struct dt_object *dt)
1033 dt_write_unlock(env, dt_object_child(dt));
1037 * Implementation of dt_object_operations::do_write_locked.
1039 * \see dt_object_operations::do_write_locked() in the API description for
1042 static int lod_object_write_locked(const struct lu_env *env,
1043 struct dt_object *dt)
1045 return dt_write_locked(env, dt_object_child(dt));
1049 * Implementation of dt_object_operations::do_attr_get.
1051 * \see dt_object_operations::do_attr_get() in the API description for details.
1053 static int lod_attr_get(const struct lu_env *env,
1054 struct dt_object *dt,
1055 struct lu_attr *attr,
1056 struct lustre_capa *capa)
1058 /* Note: for striped directory, client will merge attributes
1059 * from all of the sub-stripes see lmv_merge_attr(), and there
1060 * no MDD logic depend on directory nlink/size/time, so we can
1061 * always use master inode nlink and size for now. */
1062 return dt_attr_get(env, dt_object_child(dt), attr, capa);
1066 * Mark all of the striped directory sub-stripes dead.
1068 * When a striped object is a subject to removal, we have
1069 * to mark all the stripes to prevent further access to
1070 * them (e.g. create a new file in those). So we mark
1071 * all the stripes with LMV_HASH_FLAG_DEAD. The function
1072 * can be used to declare the changes and to apply them.
1073 * If the object isn't striped, then just return success.
1075 * \param[in] env execution environment
1076 * \param[in] dt the striped object
1077 * \param[in] handle transaction handle
1078 * \param[in] declare whether to declare the change or apply
1080 * \retval 0 on success
1081 * \retval negative if failed
1083 static int lod_mark_dead_object(const struct lu_env *env,
1084 struct dt_object *dt,
1085 struct thandle *handle,
1088 struct lod_object *lo = lod_dt_obj(dt);
1089 struct lmv_mds_md_v1 *lmv;
1090 __u32 dead_hash_type;
1096 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
1099 rc = lod_load_striping_locked(env, lo);
1103 if (lo->ldo_stripenr == 0)
1106 rc = lod_get_lmv_ea(env, lo);
1110 lmv = lod_env_info(env)->lti_ea_store;
1111 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1112 dead_hash_type = le32_to_cpu(lmv->lmv_hash_type) | LMV_HASH_FLAG_DEAD;
1113 lmv->lmv_hash_type = cpu_to_le32(dead_hash_type);
1114 for (i = 0; i < lo->ldo_stripenr; i++) {
1117 lmv->lmv_master_mdt_index = i;
1119 buf.lb_len = sizeof(*lmv);
1121 rc = dt_declare_xattr_set(env, lo->ldo_stripe[i], &buf,
1123 LU_XATTR_REPLACE, handle);
1125 rc = dt_xattr_set(env, lo->ldo_stripe[i], &buf,
1126 XATTR_NAME_LMV, LU_XATTR_REPLACE,
1127 handle, BYPASS_CAPA);
1137 * Implementation of dt_object_operations::do_declare_attr_set.
1139 * If the object is striped, then apply the changes to all the stripes.
1141 * \see dt_object_operations::do_declare_attr_set() in the API description
1144 static int lod_declare_attr_set(const struct lu_env *env,
1145 struct dt_object *dt,
1146 const struct lu_attr *attr,
1147 struct thandle *handle)
1149 struct dt_object *next = dt_object_child(dt);
1150 struct lod_object *lo = lod_dt_obj(dt);
1154 /* Set dead object on all other stripes */
1155 if (attr->la_valid & LA_FLAGS && !(attr->la_valid & ~LA_FLAGS) &&
1156 attr->la_flags & LUSTRE_SLAVE_DEAD_FL) {
1157 rc = lod_mark_dead_object(env, dt, handle, true);
1162 * declare setattr on the local object
1164 rc = dt_declare_attr_set(env, next, attr, handle);
1168 /* osp_declare_attr_set() ignores all attributes other than
1169 * UID, GID, and size, and osp_attr_set() ignores all but UID
1170 * and GID. Declaration of size attr setting happens through
1171 * lod_declare_init_size(), and not through this function.
1172 * Therefore we need not load striping unless ownership is
1173 * changing. This should save memory and (we hope) speed up
1175 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1176 if (!(attr->la_valid & (LA_UID | LA_GID)))
1179 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1182 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE |
1183 LA_ATIME | LA_MTIME | LA_CTIME)))
1187 * load striping information, notice we don't do this when object
1188 * is being initialized as we don't need this information till
1189 * few specific cases like destroy, chown
1191 rc = lod_load_striping(env, lo);
1195 if (lo->ldo_stripenr == 0)
1199 * if object is striped declare changes on the stripes
1201 LASSERT(lo->ldo_stripe);
1202 for (i = 0; i < lo->ldo_stripenr; i++) {
1203 if (likely(lo->ldo_stripe[i] != NULL)) {
1204 rc = dt_declare_attr_set(env, lo->ldo_stripe[i], attr,
1207 CERROR("failed declaration: %d\n", rc);
1213 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE) &&
1214 dt_object_exists(next) != 0 &&
1215 dt_object_remote(next) == 0)
1216 dt_declare_xattr_del(env, next, XATTR_NAME_LOV, handle);
1218 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) &&
1219 dt_object_exists(next) &&
1220 dt_object_remote(next) == 0 && S_ISREG(attr->la_mode)) {
1221 struct lod_thread_info *info = lod_env_info(env);
1222 struct lu_buf *buf = &info->lti_buf;
1224 buf->lb_buf = info->lti_ea_store;
1225 buf->lb_len = info->lti_ea_store_size;
1226 dt_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1227 LU_XATTR_REPLACE, handle);
1234 * Implementation of dt_object_operations::do_attr_set.
1236 * If the object is striped, then apply the changes to all or subset of
1237 * the stripes depending on the object type and specific attributes.
1239 * \see dt_object_operations::do_attr_set() in the API description for details.
1241 static int lod_attr_set(const struct lu_env *env,
1242 struct dt_object *dt,
1243 const struct lu_attr *attr,
1244 struct thandle *handle,
1245 struct lustre_capa *capa)
1247 struct dt_object *next = dt_object_child(dt);
1248 struct lod_object *lo = lod_dt_obj(dt);
1252 /* Set dead object on all other stripes */
1253 if (attr->la_valid & LA_FLAGS && !(attr->la_valid & ~LA_FLAGS) &&
1254 attr->la_flags & LUSTRE_SLAVE_DEAD_FL) {
1255 rc = lod_mark_dead_object(env, dt, handle, false);
1260 * apply changes to the local object
1262 rc = dt_attr_set(env, next, attr, handle, capa);
1266 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1267 if (!(attr->la_valid & (LA_UID | LA_GID)))
1270 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1273 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE |
1274 LA_ATIME | LA_MTIME | LA_CTIME)))
1278 if (lo->ldo_stripenr == 0)
1282 * if object is striped, apply changes to all the stripes
1284 LASSERT(lo->ldo_stripe);
1285 for (i = 0; i < lo->ldo_stripenr; i++) {
1286 if (unlikely(lo->ldo_stripe[i] == NULL))
1288 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
1289 (dt_object_exists(lo->ldo_stripe[i]) == 0))
1292 rc = dt_attr_set(env, lo->ldo_stripe[i], attr, handle, capa);
1294 CERROR("failed declaration: %d\n", rc);
1299 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE) &&
1300 dt_object_exists(next) != 0 &&
1301 dt_object_remote(next) == 0)
1302 dt_xattr_del(env, next, XATTR_NAME_LOV, handle, BYPASS_CAPA);
1304 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) &&
1305 dt_object_exists(next) &&
1306 dt_object_remote(next) == 0 && S_ISREG(attr->la_mode)) {
1307 struct lod_thread_info *info = lod_env_info(env);
1308 struct lu_buf *buf = &info->lti_buf;
1309 struct ost_id *oi = &info->lti_ostid;
1310 struct lu_fid *fid = &info->lti_fid;
1311 struct lov_mds_md_v1 *lmm;
1312 struct lov_ost_data_v1 *objs;
1316 rc1 = lod_get_lov_ea(env, lo);
1320 buf->lb_buf = info->lti_ea_store;
1321 buf->lb_len = info->lti_ea_store_size;
1322 lmm = info->lti_ea_store;
1323 magic = le32_to_cpu(lmm->lmm_magic);
1324 if (magic == LOV_MAGIC_V1)
1325 objs = &(lmm->lmm_objects[0]);
1327 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1328 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1329 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1331 fid_to_ostid(fid, oi);
1332 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1333 dt_xattr_set(env, next, buf, XATTR_NAME_LOV,
1334 LU_XATTR_REPLACE, handle, BYPASS_CAPA);
1341 * Implementation of dt_object_operations::do_xattr_get.
1343 * If LOV EA is requested from the root object and it's not
1344 * found, then return default striping for the filesystem.
1346 * \see dt_object_operations::do_xattr_get() in the API description for details.
1348 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1349 struct lu_buf *buf, const char *name,
1350 struct lustre_capa *capa)
1352 struct lod_thread_info *info = lod_env_info(env);
1353 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1357 rc = dt_xattr_get(env, dt_object_child(dt), buf, name, capa);
1358 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1359 struct lmv_mds_md_v1 *lmv1;
1362 if (rc > (typeof(rc))sizeof(*lmv1))
1365 if (rc < (typeof(rc))sizeof(*lmv1))
1366 RETURN(rc = rc > 0 ? -EINVAL : rc);
1368 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1369 CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1371 info->lti_buf.lb_buf = info->lti_key;
1372 info->lti_buf.lb_len = sizeof(*lmv1);
1373 rc = dt_xattr_get(env, dt_object_child(dt),
1374 &info->lti_buf, name, capa);
1375 if (unlikely(rc != sizeof(*lmv1)))
1376 RETURN(rc = rc > 0 ? -EINVAL : rc);
1378 lmv1 = info->lti_buf.lb_buf;
1379 /* The on-disk LMV EA only contains header, but the
1380 * returned LMV EA size should contain the space for
1381 * the FIDs of all shards of the striped directory. */
1382 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1383 rc = lmv_mds_md_size(
1384 le32_to_cpu(lmv1->lmv_stripe_count),
1387 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1391 RETURN(rc = rc1 != 0 ? rc1 : rc);
1394 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1398 * lod returns default striping on the real root of the device
1399 * this is like the root stores default striping for the whole
1400 * filesystem. historically we've been using a different approach
1401 * and store it in the config.
1403 dt_root_get(env, dev->lod_child, &info->lti_fid);
1404 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1406 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1407 struct lov_user_md *lum = buf->lb_buf;
1408 struct lov_desc *desc = &dev->lod_desc;
1410 if (buf->lb_buf == NULL) {
1412 } else if (buf->lb_len >= sizeof(*lum)) {
1413 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1414 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1415 lmm_oi_set_id(&lum->lmm_oi, 0);
1416 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1417 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1418 lum->lmm_stripe_size = cpu_to_le32(
1419 desc->ld_default_stripe_size);
1420 lum->lmm_stripe_count = cpu_to_le16(
1421 desc->ld_default_stripe_count);
1422 lum->lmm_stripe_offset = cpu_to_le16(
1423 desc->ld_default_stripe_offset);
1436 * Checks that the magic and the number of the stripes are sane.
1438 * \param[in] lod lod device
1439 * \param[in] lum a buffer storing LMV EA to verify
1441 * \retval 0 if the EA is sane
1442 * \retval negative otherwise
1444 static int lod_verify_md_striping(struct lod_device *lod,
1445 const struct lmv_user_md_v1 *lum)
1450 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC))
1451 GOTO(out, rc = -EINVAL);
1453 if (unlikely(le32_to_cpu(lum->lum_stripe_count) == 0))
1454 GOTO(out, rc = -EINVAL);
1457 CERROR("%s: invalid lmv_user_md: magic = %x, "
1458 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1459 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1460 (int)le32_to_cpu(lum->lum_stripe_offset),
1461 le32_to_cpu(lum->lum_stripe_count), rc);
1466 * Initialize LMV EA for a slave.
1468 * Initialize slave's LMV EA from the master's LMV EA.
1470 * \param[in] master_lmv a buffer containing master's EA
1471 * \param[out] slave_lmv a buffer where slave's EA will be stored
1474 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1475 const struct lmv_mds_md_v1 *master_lmv)
1477 *slave_lmv = *master_lmv;
1478 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1484 * Generate LMV EA from the object passed as \a dt. The object must have
1485 * the stripes created and initialized.
1487 * \param[in] env execution environment
1488 * \param[in] dt object
1489 * \param[out] lmv_buf buffer storing generated LMV EA
1491 * \retval 0 on success
1492 * \retval negative if failed
1494 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1495 struct lu_buf *lmv_buf)
1497 struct lod_thread_info *info = lod_env_info(env);
1498 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1499 struct lod_object *lo = lod_dt_obj(dt);
1500 struct lmv_mds_md_v1 *lmm1;
1502 int type = LU_SEQ_RANGE_ANY;
1507 LASSERT(lo->ldo_dir_striped != 0);
1508 LASSERT(lo->ldo_stripenr > 0);
1509 stripe_count = lo->ldo_stripenr;
1510 /* Only store the LMV EA heahder on the disk. */
1511 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1512 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1516 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1519 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1520 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1521 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1522 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1523 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1528 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1529 lmv_buf->lb_buf = info->lti_ea_store;
1530 lmv_buf->lb_len = sizeof(*lmm1);
1531 lo->ldo_dir_striping_cached = 1;
1537 * Create in-core represenation for a striped directory.
1539 * Parse the buffer containing LMV EA and instantiate LU objects
1540 * representing the stripe objects. The pointers to the objects are
1541 * stored in ldo_stripe field of \a lo. This function is used when
1542 * we need to access an already created object (i.e. load from a disk).
1544 * \param[in] env execution environment
1545 * \param[in] lo lod object
1546 * \param[in] buf buffer containing LMV EA
1548 * \retval 0 on success
1549 * \retval negative if failed
1551 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1552 const struct lu_buf *buf)
1554 struct lod_thread_info *info = lod_env_info(env);
1555 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1556 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1557 struct dt_object **stripe;
1558 union lmv_mds_md *lmm = buf->lb_buf;
1559 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1560 struct lu_fid *fid = &info->lti_fid;
1565 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1568 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1569 lo->ldo_dir_slave_stripe = 1;
1573 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1576 if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1579 LASSERT(lo->ldo_stripe == NULL);
1580 OBD_ALLOC(stripe, sizeof(stripe[0]) *
1581 (le32_to_cpu(lmv1->lmv_stripe_count)));
1585 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1586 struct dt_device *tgt_dt;
1587 struct dt_object *dto;
1588 int type = LU_SEQ_RANGE_ANY;
1591 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1592 if (!fid_is_sane(fid))
1593 GOTO(out, rc = -ESTALE);
1595 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1599 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1600 tgt_dt = lod->lod_child;
1602 struct lod_tgt_desc *tgt;
1604 tgt = LTD_TGT(ltd, idx);
1606 GOTO(out, rc = -ESTALE);
1607 tgt_dt = tgt->ltd_tgt;
1610 dto = dt_locate_at(env, tgt_dt, fid,
1611 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1614 GOTO(out, rc = PTR_ERR(dto));
1619 lo->ldo_stripe = stripe;
1620 lo->ldo_stripenr = le32_to_cpu(lmv1->lmv_stripe_count);
1621 lo->ldo_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1623 lod_object_free_striping(env, lo);
1629 * Create a striped directory.
1631 * Create a striped directory with a given stripe pattern on the specified MDTs.
1632 * A striped directory is represented as a regular directory - an index listing
1633 * all the stripes. The stripes point back to the master object with ".." and
1634 * LinkEA. The master object gets LMV EA which identifies it as a striped
1635 * directory. The function allocates FIDs for all the stripes.
1637 * \param[in] env execution environment
1638 * \param[in] dt object
1639 * \param[in] attr attributes to initialize the objects with
1640 * \param[in] lum a pattern specifying the number of stripes and
1642 * \param[in] dof type of objects to be created
1643 * \param[in] th transaction handle
1645 * \retval 0 on success
1646 * \retval negative if failed
1648 static int lod_prep_md_striped_create(const struct lu_env *env,
1649 struct dt_object *dt,
1650 struct lu_attr *attr,
1651 const struct lmv_user_md_v1 *lum,
1652 struct dt_object_format *dof,
1655 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1656 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1657 struct lod_object *lo = lod_dt_obj(dt);
1658 struct lod_thread_info *info = lod_env_info(env);
1659 struct dt_object **stripe;
1660 struct lu_buf lmv_buf;
1661 struct lu_buf slave_lmv_buf;
1662 struct lmv_mds_md_v1 *lmm;
1663 struct lmv_mds_md_v1 *slave_lmm = NULL;
1664 struct dt_insert_rec *rec = &info->lti_dt_rec;
1672 /* The lum has been verifed in lod_verify_md_striping */
1673 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
1674 LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1676 stripe_count = le32_to_cpu(lum->lum_stripe_count);
1678 /* shrink the stripe_count to the avaible MDT count */
1679 if (stripe_count > lod->lod_remote_mdt_count + 1)
1680 stripe_count = lod->lod_remote_mdt_count + 1;
1682 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1686 OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1687 if (idx_array == NULL)
1688 GOTO(out_free, rc = -ENOMEM);
1690 for (i = 0; i < stripe_count; i++) {
1691 struct lod_tgt_desc *tgt = NULL;
1692 struct dt_object *dto;
1693 struct lu_fid fid = { 0 };
1695 struct lu_object_conf conf = { 0 };
1696 struct dt_device *tgt_dt = NULL;
1699 /* Right now, master stripe and master object are
1700 * on the same MDT */
1701 idx = le32_to_cpu(lum->lum_stripe_offset);
1702 rc = obd_fid_alloc(env, lod->lod_child_exp, &fid,
1706 tgt_dt = lod->lod_child;
1710 idx = (idx_array[i - 1] + 1) % (lod->lod_remote_mdt_count + 1);
1712 for (j = 0; j < lod->lod_remote_mdt_count;
1713 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1714 bool already_allocated = false;
1717 CDEBUG(D_INFO, "try idx %d, mdt cnt %u,"
1718 " allocated %u, last allocated %d\n", idx,
1719 lod->lod_remote_mdt_count, i, idx_array[i - 1]);
1721 /* Find next available target */
1722 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx))
1725 /* check whether the idx already exists
1726 * in current allocated array */
1727 for (k = 0; k < i; k++) {
1728 if (idx_array[k] == idx) {
1729 already_allocated = true;
1734 if (already_allocated)
1737 /* check the status of the OSP */
1738 tgt = LTD_TGT(ltd, idx);
1742 tgt_dt = tgt->ltd_tgt;
1743 rc = dt_statfs(env, tgt_dt, NULL);
1745 /* this OSP doesn't feel well */
1750 rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1759 /* Can not allocate more stripes */
1760 if (j == lod->lod_remote_mdt_count) {
1761 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1762 lod2obd(lod)->obd_name, stripe_count, i - 1);
1766 CDEBUG(D_INFO, "idx %d, mdt cnt %u,"
1767 " allocated %u, last allocated %d\n", idx,
1768 lod->lod_remote_mdt_count, i, idx_array[i - 1]);
1771 /* tgt_dt and fid must be ready after search avaible OSP
1772 * in the above loop */
1773 LASSERT(tgt_dt != NULL);
1774 LASSERT(fid_is_sane(&fid));
1775 conf.loc_flags = LOC_F_NEW;
1776 dto = dt_locate_at(env, tgt_dt, &fid,
1777 dt->do_lu.lo_dev->ld_site->ls_top_dev,
1780 GOTO(out_put, rc = PTR_ERR(dto));
1785 lo->ldo_dir_striped = 1;
1786 lo->ldo_stripe = stripe;
1787 lo->ldo_stripenr = i;
1788 lo->ldo_stripes_allocated = stripe_count;
1790 if (lo->ldo_stripenr == 0)
1791 GOTO(out_put, rc = -ENOSPC);
1793 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1796 lmm = lmv_buf.lb_buf;
1798 OBD_ALLOC_PTR(slave_lmm);
1799 if (slave_lmm == NULL)
1800 GOTO(out_put, rc = -ENOMEM);
1802 lod_prep_slave_lmv_md(slave_lmm, lmm);
1803 slave_lmv_buf.lb_buf = slave_lmm;
1804 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1806 if (!dt_try_as_dir(env, dt_object_child(dt)))
1807 GOTO(out_put, rc = -EINVAL);
1809 rec->rec_type = S_IFDIR;
1810 for (i = 0; i < lo->ldo_stripenr; i++) {
1811 struct dt_object *dto = stripe[i];
1812 char *stripe_name = info->lti_key;
1813 struct lu_name *sname;
1814 struct linkea_data ldata = { NULL };
1815 struct lu_buf linkea_buf;
1817 rc = dt_declare_create(env, dto, attr, NULL, dof, th);
1821 if (!dt_try_as_dir(env, dto))
1822 GOTO(out_put, rc = -EINVAL);
1824 rec->rec_fid = lu_object_fid(&dto->do_lu);
1825 rc = dt_declare_insert(env, dto, (const struct dt_rec *)rec,
1826 (const struct dt_key *)dot, th);
1830 /* master stripe FID will be put to .. */
1831 rec->rec_fid = lu_object_fid(&dt->do_lu);
1832 rc = dt_declare_insert(env, dto, (const struct dt_rec *)rec,
1833 (const struct dt_key *)dotdot, th);
1837 /* probably nothing to inherite */
1838 if (lo->ldo_striping_cached &&
1839 !LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size,
1840 lo->ldo_def_stripenr,
1841 lo->ldo_def_stripe_offset,
1843 struct lov_user_md_v3 *v3;
1845 /* sigh, lti_ea_store has been used for lmv_buf,
1846 * so we have to allocate buffer for default
1850 GOTO(out_put, rc = -ENOMEM);
1852 memset(v3, 0, sizeof(*v3));
1853 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
1854 v3->lmm_stripe_count =
1855 cpu_to_le16(lo->ldo_def_stripenr);
1856 v3->lmm_stripe_offset =
1857 cpu_to_le16(lo->ldo_def_stripe_offset);
1858 v3->lmm_stripe_size =
1859 cpu_to_le32(lo->ldo_def_stripe_size);
1860 if (lo->ldo_pool != NULL)
1861 strlcpy(v3->lmm_pool_name, lo->ldo_pool,
1862 sizeof(v3->lmm_pool_name));
1864 info->lti_buf.lb_buf = v3;
1865 info->lti_buf.lb_len = sizeof(*v3);
1866 rc = dt_declare_xattr_set(env, dto,
1875 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1876 cfs_fail_val != i) {
1877 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1879 slave_lmm->lmv_master_mdt_index =
1882 slave_lmm->lmv_master_mdt_index =
1884 rc = dt_declare_xattr_set(env, dto, &slave_lmv_buf,
1885 XATTR_NAME_LMV, 0, th);
1890 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1892 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1893 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1895 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1896 PFID(lu_object_fid(&dto->do_lu)), i);
1898 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1899 rc = linkea_data_new(&ldata, &info->lti_linkea_buf);
1903 rc = linkea_add_buf(&ldata, sname, lu_object_fid(&dt->do_lu));
1907 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1908 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1909 rc = dt_declare_xattr_set(env, dto, &linkea_buf,
1910 XATTR_NAME_LINK, 0, th);
1914 rec->rec_fid = lu_object_fid(&dto->do_lu);
1915 rc = dt_declare_insert(env, dt_object_child(dt),
1916 (const struct dt_rec *)rec,
1917 (const struct dt_key *)stripe_name, th);
1921 rc = dt_declare_ref_add(env, dt_object_child(dt), th);
1926 rc = dt_declare_xattr_set(env, dt_object_child(dt), &lmv_buf,
1927 XATTR_NAME_LMV, 0, th);
1933 for (i = 0; i < stripe_count; i++)
1934 if (stripe[i] != NULL)
1935 lu_object_put(env, &stripe[i]->do_lu);
1936 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
1937 lo->ldo_stripenr = 0;
1938 lo->ldo_stripes_allocated = 0;
1939 lo->ldo_stripe = NULL;
1943 if (idx_array != NULL)
1944 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
1945 if (slave_lmm != NULL)
1946 OBD_FREE_PTR(slave_lmm);
1952 * Declare create striped md object.
1954 * The function declares intention to create a striped directory. This is a
1955 * wrapper for lod_prep_md_striped_create(). The only additional functionality
1956 * is to verify pattern \a lum_buf is good. Check that function for the details.
1958 * \param[in] env execution environment
1959 * \param[in] dt object
1960 * \param[in] attr attributes to initialize the objects with
1961 * \param[in] lum_buf a pattern specifying the number of stripes and
1963 * \param[in] dof type of objects to be created
1964 * \param[in] th transaction handle
1966 * \retval 0 on success
1967 * \retval negative if failed
1970 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
1971 struct dt_object *dt,
1972 struct lu_attr *attr,
1973 const struct lu_buf *lum_buf,
1974 struct dt_object_format *dof,
1977 struct lod_object *lo = lod_dt_obj(dt);
1978 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1979 struct lmv_user_md_v1 *lum;
1983 lum = lum_buf->lb_buf;
1984 LASSERT(lum != NULL);
1986 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
1987 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
1988 (int)le32_to_cpu(lum->lum_stripe_offset));
1990 if (le32_to_cpu(lum->lum_stripe_count) == 0)
1993 rc = lod_verify_md_striping(lod, lum);
1997 /* prepare dir striped objects */
1998 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2000 /* failed to create striping, let's reset
2001 * config so that others don't get confused */
2002 lod_object_free_striping(env, lo);
2011 * Implementation of dt_object_operations::do_declare_xattr_set.
2013 * Used with regular (non-striped) objects. Basically it
2014 * initializes the striping information and applies the
2015 * change to all the stripes.
2017 * \see dt_object_operations::do_declare_xattr_set() in the API description
2020 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2021 struct dt_object *dt,
2022 const struct lu_buf *buf,
2023 const char *name, int fl,
2026 struct dt_object *next = dt_object_child(dt);
2027 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2028 struct lod_object *lo = lod_dt_obj(dt);
2033 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2034 struct lmv_user_md_v1 *lum;
2036 LASSERT(buf != NULL && buf->lb_buf != NULL);
2038 rc = lod_verify_md_striping(d, lum);
2043 rc = dt_declare_xattr_set(env, next, buf, name, fl, th);
2047 /* set xattr to each stripes, if needed */
2048 rc = lod_load_striping(env, lo);
2052 /* Note: Do not set LinkEA on sub-stripes, otherwise
2053 * it will confuse the fid2path process(see mdt_path_current()).
2054 * The linkEA between master and sub-stripes is set in
2055 * lod_xattr_set_lmv(). */
2056 if (lo->ldo_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2059 for (i = 0; i < lo->ldo_stripenr; i++) {
2060 LASSERT(lo->ldo_stripe[i]);
2061 rc = dt_declare_xattr_set(env, lo->ldo_stripe[i], buf,
2071 * Implementation of dt_object_operations::do_declare_xattr_set.
2073 * \see dt_object_operations::do_declare_xattr_set() in the API description
2076 * the extension to the API:
2077 * - declaring LOVEA requests striping creation
2078 * - LU_XATTR_REPLACE means layout swap
2080 static int lod_declare_xattr_set(const struct lu_env *env,
2081 struct dt_object *dt,
2082 const struct lu_buf *buf,
2083 const char *name, int fl,
2086 struct dt_object *next = dt_object_child(dt);
2087 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2093 * allow to declare predefined striping on a new (!mode) object
2094 * which is supposed to be replay of regular file creation
2095 * (when LOV setting is declared)
2096 * LU_XATTR_REPLACE is set to indicate a layout swap
2098 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2099 if ((S_ISREG(mode) || mode == 0) && strcmp(name, XATTR_NAME_LOV) == 0 &&
2100 !(fl & LU_XATTR_REPLACE)) {
2102 * this is a request to manipulate object's striping
2104 if (dt_object_exists(dt)) {
2105 rc = dt_attr_get(env, next, attr, BYPASS_CAPA);
2109 memset(attr, 0, sizeof(*attr));
2110 attr->la_valid = LA_TYPE | LA_MODE;
2111 attr->la_mode = S_IFREG;
2113 rc = lod_declare_striped_object(env, dt, attr, buf, th);
2114 } else if (S_ISDIR(mode)) {
2115 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2117 rc = dt_declare_xattr_set(env, next, buf, name, fl, th);
2124 * Resets cached default striping in the object.
2126 * \param[in] lo object
2128 static void lod_lov_stripe_cache_clear(struct lod_object *lo)
2130 lo->ldo_striping_cached = 0;
2131 lo->ldo_def_striping_set = 0;
2132 lod_object_set_pool(lo, NULL);
2133 lo->ldo_def_stripe_size = 0;
2134 lo->ldo_def_stripenr = 0;
2135 if (lo->ldo_dir_stripe != NULL)
2136 lo->ldo_dir_striping_cached = 0;
2140 * Apply xattr changes to the object.
2142 * Applies xattr changes to the object and the stripes if the latter exist.
2144 * \param[in] env execution environment
2145 * \param[in] dt object
2146 * \param[in] buf buffer pointing to the new value of xattr
2147 * \param[in] name name of xattr
2148 * \param[in] fl flags
2149 * \param[in] th transaction handle
2150 * \param[in] capa not used currently
2152 * \retval 0 on success
2153 * \retval negative if failed
2155 static int lod_xattr_set_internal(const struct lu_env *env,
2156 struct dt_object *dt,
2157 const struct lu_buf *buf,
2158 const char *name, int fl, struct thandle *th,
2159 struct lustre_capa *capa)
2161 struct dt_object *next = dt_object_child(dt);
2162 struct lod_object *lo = lod_dt_obj(dt);
2167 rc = dt_xattr_set(env, next, buf, name, fl, th, capa);
2168 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2171 /* Note: Do not set LinkEA on sub-stripes, otherwise
2172 * it will confuse the fid2path process(see mdt_path_current()).
2173 * The linkEA between master and sub-stripes is set in
2174 * lod_xattr_set_lmv(). */
2175 if (lo->ldo_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2178 for (i = 0; i < lo->ldo_stripenr; i++) {
2179 LASSERT(lo->ldo_stripe[i]);
2180 rc = dt_xattr_set(env, lo->ldo_stripe[i], buf, name, fl, th,
2190 * Delete an extended attribute.
2192 * Deletes specified xattr from the object and the stripes if the latter exist.
2194 * \param[in] env execution environment
2195 * \param[in] dt object
2196 * \param[in] name name of xattr
2197 * \param[in] th transaction handle
2198 * \param[in] capa not used currently
2200 * \retval 0 on success
2201 * \retval negative if failed
2203 static int lod_xattr_del_internal(const struct lu_env *env,
2204 struct dt_object *dt,
2205 const char *name, struct thandle *th,
2206 struct lustre_capa *capa)
2208 struct dt_object *next = dt_object_child(dt);
2209 struct lod_object *lo = lod_dt_obj(dt);
2214 rc = dt_xattr_del(env, next, name, th, capa);
2215 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2218 if (lo->ldo_stripenr == 0)
2221 for (i = 0; i < lo->ldo_stripenr; i++) {
2222 LASSERT(lo->ldo_stripe[i]);
2223 rc = dt_xattr_del(env, lo->ldo_stripe[i], name, th,
2233 * Set default striping on a directory.
2235 * Sets specified striping on a directory object unless it matches the default
2236 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2237 * EA. This striping will be used when regular file is being created in this
2240 * \param[in] env execution environment
2241 * \param[in] dt the striped object
2242 * \param[in] buf buffer with the striping
2243 * \param[in] name name of EA
2244 * \param[in] fl xattr flag (see OSD API description)
2245 * \param[in] th transaction handle
2246 * \param[in] capa not used
2248 * \retval 0 on success
2249 * \retval negative if failed
2251 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
2252 struct dt_object *dt,
2253 const struct lu_buf *buf,
2254 const char *name, int fl,
2256 struct lustre_capa *capa)
2258 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2259 struct lod_object *l = lod_dt_obj(dt);
2260 struct lov_user_md_v1 *lum;
2261 struct lov_user_md_v3 *v3 = NULL;
2262 const char *pool_name = NULL;
2266 /* If it is striped dir, we should clear the stripe cache for
2267 * slave stripe as well, but there are no effective way to
2268 * notify the LOD on the slave MDT, so we do not cache stripe
2269 * information for slave stripe for now. XXX*/
2270 lod_lov_stripe_cache_clear(l);
2271 LASSERT(buf != NULL && buf->lb_buf != NULL);
2274 rc = lod_verify_striping(d, buf, false);
2278 if (lum->lmm_magic == LOV_USER_MAGIC_V3) {
2280 if (v3->lmm_pool_name[0] != '\0')
2281 pool_name = v3->lmm_pool_name;
2284 /* if { size, offset, count } = { 0, -1, 0 } and no pool
2285 * (i.e. all default values specified) then delete default
2286 * striping from dir. */
2288 "set default striping: sz %u # %u offset %d %s %s\n",
2289 (unsigned)lum->lmm_stripe_size,
2290 (unsigned)lum->lmm_stripe_count,
2291 (int)lum->lmm_stripe_offset,
2292 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
2294 if (LOVEA_DELETE_VALUES(lum->lmm_stripe_size, lum->lmm_stripe_count,
2295 lum->lmm_stripe_offset, pool_name)) {
2296 rc = lod_xattr_del_internal(env, dt, name, th, capa);
2300 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th, capa);
2307 * Set default striping on a directory object.
2309 * Sets specified striping on a directory object unless it matches the default
2310 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2311 * EA. This striping will be used when a new directory is being created in the
2314 * \param[in] env execution environment
2315 * \param[in] dt the striped object
2316 * \param[in] buf buffer with the striping
2317 * \param[in] name name of EA
2318 * \param[in] fl xattr flag (see OSD API description)
2319 * \param[in] th transaction handle
2320 * \param[in] capa not used
2322 * \retval 0 on success
2323 * \retval negative if failed
2325 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
2326 struct dt_object *dt,
2327 const struct lu_buf *buf,
2328 const char *name, int fl,
2330 struct lustre_capa *capa)
2332 struct lod_object *l = lod_dt_obj(dt);
2333 struct lmv_user_md_v1 *lum;
2337 LASSERT(buf != NULL && buf->lb_buf != NULL);
2340 CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
2341 le32_to_cpu(lum->lum_stripe_count),
2342 (int)le32_to_cpu(lum->lum_stripe_offset));
2344 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
2345 le32_to_cpu(lum->lum_stripe_offset)) &&
2346 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
2347 rc = lod_xattr_del_internal(env, dt, name, th, capa);
2351 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th, capa);
2356 /* Update default stripe cache */
2357 if (l->ldo_dir_stripe == NULL) {
2358 OBD_ALLOC_PTR(l->ldo_dir_stripe);
2359 if (l->ldo_dir_stripe == NULL)
2363 l->ldo_dir_striping_cached = 0;
2364 l->ldo_dir_def_striping_set = 1;
2365 l->ldo_dir_def_stripenr = le32_to_cpu(lum->lum_stripe_count);
2371 * Turn directory into a striped directory.
2373 * During replay the client sends the striping created before MDT
2374 * failure, then the layer above LOD sends this defined striping
2375 * using ->do_xattr_set(), so LOD uses this method to replay creation
2376 * of the stripes. Notice the original information for the striping
2377 * (#stripes, FIDs, etc) was transfered in declare path.
2379 * \param[in] env execution environment
2380 * \param[in] dt the striped object
2381 * \param[in] buf not used currently
2382 * \param[in] name not used currently
2383 * \param[in] fl xattr flag (see OSD API description)
2384 * \param[in] th transaction handle
2385 * \param[in] capa not used
2387 * \retval 0 on success
2388 * \retval negative if failed
2390 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
2391 const struct lu_buf *buf, const char *name,
2392 int fl, struct thandle *th,
2393 struct lustre_capa *capa)
2395 struct lod_object *lo = lod_dt_obj(dt);
2396 struct lod_thread_info *info = lod_env_info(env);
2397 struct lu_attr *attr = &info->lti_attr;
2398 struct dt_object_format *dof = &info->lti_format;
2399 struct lu_buf lmv_buf;
2400 struct lu_buf slave_lmv_buf;
2401 struct lmv_mds_md_v1 *lmm;
2402 struct lmv_mds_md_v1 *slave_lmm = NULL;
2403 struct dt_insert_rec *rec = &info->lti_dt_rec;
2408 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2411 /* The stripes are supposed to be allocated in declare phase,
2412 * if there are no stripes being allocated, it will skip */
2413 if (lo->ldo_stripenr == 0)
2416 rc = dt_attr_get(env, dt_object_child(dt), attr, BYPASS_CAPA);
2420 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
2421 LA_MODE | LA_UID | LA_GID | LA_TYPE;
2422 dof->dof_type = DFT_DIR;
2424 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
2427 lmm = lmv_buf.lb_buf;
2429 OBD_ALLOC_PTR(slave_lmm);
2430 if (slave_lmm == NULL)
2433 lod_prep_slave_lmv_md(slave_lmm, lmm);
2434 slave_lmv_buf.lb_buf = slave_lmm;
2435 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
2437 rec->rec_type = S_IFDIR;
2438 for (i = 0; i < lo->ldo_stripenr; i++) {
2439 struct dt_object *dto;
2440 char *stripe_name = info->lti_key;
2441 struct lu_name *sname;
2442 struct linkea_data ldata = { NULL };
2443 struct lu_buf linkea_buf;
2445 dto = lo->ldo_stripe[i];
2446 dt_write_lock(env, dto, MOR_TGT_CHILD);
2447 rc = dt_create(env, dto, attr, NULL, dof, th);
2448 dt_write_unlock(env, dto);
2452 rec->rec_fid = lu_object_fid(&dto->do_lu);
2453 rc = dt_insert(env, dto, (const struct dt_rec *)rec,
2454 (const struct dt_key *)dot, th, capa, 0);
2458 rec->rec_fid = lu_object_fid(&dt->do_lu);
2459 rc = dt_insert(env, dto, (struct dt_rec *)rec,
2460 (const struct dt_key *)dotdot, th, capa, 0);
2464 if (lo->ldo_striping_cached &&
2465 !LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size,
2466 lo->ldo_def_stripenr,
2467 lo->ldo_def_stripe_offset,
2469 struct lov_user_md_v3 *v3;
2471 /* sigh, lti_ea_store has been used for lmv_buf,
2472 * so we have to allocate buffer for default
2478 memset(v3, 0, sizeof(*v3));
2479 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
2480 v3->lmm_stripe_count =
2481 cpu_to_le16(lo->ldo_def_stripenr);
2482 v3->lmm_stripe_offset =
2483 cpu_to_le16(lo->ldo_def_stripe_offset);
2484 v3->lmm_stripe_size =
2485 cpu_to_le32(lo->ldo_def_stripe_size);
2486 if (lo->ldo_pool != NULL)
2487 strlcpy(v3->lmm_pool_name, lo->ldo_pool,
2488 sizeof(v3->lmm_pool_name));
2490 info->lti_buf.lb_buf = v3;
2491 info->lti_buf.lb_len = sizeof(*v3);
2492 rc = dt_xattr_set(env, dto, &info->lti_buf,
2493 XATTR_NAME_LOV, 0, th, capa);
2499 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
2500 cfs_fail_val != i) {
2501 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
2503 slave_lmm->lmv_master_mdt_index =
2506 slave_lmm->lmv_master_mdt_index =
2508 rc = dt_xattr_set(env, dto, &slave_lmv_buf,
2509 XATTR_NAME_LMV, fl, th, capa);
2514 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
2516 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2517 PFID(lu_object_fid(&dto->do_lu)), i + 1);
2519 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2520 PFID(lu_object_fid(&dto->do_lu)), i);
2522 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2523 rc = linkea_data_new(&ldata, &info->lti_linkea_buf);
2527 rc = linkea_add_buf(&ldata, sname, lu_object_fid(&dt->do_lu));
2531 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
2532 linkea_buf.lb_len = ldata.ld_leh->leh_len;
2533 rc = dt_xattr_set(env, dto, &linkea_buf, XATTR_NAME_LINK,
2534 0, th, BYPASS_CAPA);
2538 rec->rec_fid = lu_object_fid(&dto->do_lu);
2539 rc = dt_insert(env, dt_object_child(dt),
2540 (const struct dt_rec *)rec,
2541 (const struct dt_key *)stripe_name, th, capa, 0);
2545 rc = dt_ref_add(env, dt_object_child(dt), th);
2550 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
2551 rc = dt_xattr_set(env, dt_object_child(dt), &lmv_buf,
2552 XATTR_NAME_LMV, fl, th, capa);
2555 if (slave_lmm != NULL)
2556 OBD_FREE_PTR(slave_lmm);
2562 * Helper function to declare/execute creation of a striped directory
2564 * Called in declare/create object path, prepare striping for a directory
2565 * and prepare defaults data striping for the objects to be created in
2566 * that directory. Notice the function calls "declaration" or "execution"
2567 * methods depending on \a declare param. This is a consequence of the
2568 * current approach while we don't have natural distributed transactions:
2569 * we basically execute non-local updates in the declare phase. So, the
2570 * arguments for the both phases are the same and this is the reason for
2571 * this function to exist.
2573 * \param[in] env execution environment
2574 * \param[in] dt object
2575 * \param[in] attr attributes the stripes will be created with
2576 * \param[in] dof format of stripes (see OSD API description)
2577 * \param[in] th transaction handle
2578 * \param[in] declare where to call "declare" or "execute" methods
2580 * \retval 0 on success
2581 * \retval negative if failed
2583 static int lod_dir_striping_create_internal(const struct lu_env *env,
2584 struct dt_object *dt,
2585 struct lu_attr *attr,
2586 struct dt_object_format *dof,
2590 struct lod_thread_info *info = lod_env_info(env);
2591 struct lod_object *lo = lod_dt_obj(dt);
2595 if (!LMVEA_DELETE_VALUES(lo->ldo_stripenr,
2596 lo->ldo_dir_stripe_offset)) {
2597 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
2598 int stripe_count = lo->ldo_stripenr;
2600 if (info->lti_ea_store_size < sizeof(*v1)) {
2601 rc = lod_ea_store_resize(info, sizeof(*v1));
2604 v1 = info->lti_ea_store;
2607 memset(v1, 0, sizeof(*v1));
2608 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
2609 v1->lum_stripe_count = cpu_to_le32(stripe_count);
2610 v1->lum_stripe_offset =
2611 cpu_to_le32(lo->ldo_dir_stripe_offset);
2613 info->lti_buf.lb_buf = v1;
2614 info->lti_buf.lb_len = sizeof(*v1);
2617 rc = lod_declare_xattr_set_lmv(env, dt, attr,
2618 &info->lti_buf, dof, th);
2620 rc = lod_xattr_set_lmv(env, dt, &info->lti_buf,
2621 XATTR_NAME_LMV, 0, th,
2627 /* Transfer default LMV striping from the parent */
2628 if (lo->ldo_dir_striping_cached &&
2629 !LMVEA_DELETE_VALUES(lo->ldo_dir_def_stripenr,
2630 lo->ldo_dir_def_stripe_offset)) {
2631 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
2632 int def_stripe_count = lo->ldo_dir_def_stripenr;
2634 if (info->lti_ea_store_size < sizeof(*v1)) {
2635 rc = lod_ea_store_resize(info, sizeof(*v1));
2638 v1 = info->lti_ea_store;
2641 memset(v1, 0, sizeof(*v1));
2642 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
2643 v1->lum_stripe_count = cpu_to_le32(def_stripe_count);
2644 v1->lum_stripe_offset =
2645 cpu_to_le32(lo->ldo_dir_def_stripe_offset);
2647 cpu_to_le32(lo->ldo_dir_def_hash_type);
2649 info->lti_buf.lb_buf = v1;
2650 info->lti_buf.lb_len = sizeof(*v1);
2652 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
2653 XATTR_NAME_DEFAULT_LMV,
2656 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
2658 XATTR_NAME_DEFAULT_LMV, 0,
2664 /* Transfer default LOV striping from the parent */
2665 if (lo->ldo_striping_cached &&
2666 !LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size,
2667 lo->ldo_def_stripenr,
2668 lo->ldo_def_stripe_offset,
2670 struct lov_user_md_v3 *v3 = info->lti_ea_store;
2672 if (info->lti_ea_store_size < sizeof(*v3)) {
2673 rc = lod_ea_store_resize(info, sizeof(*v3));
2676 v3 = info->lti_ea_store;
2679 memset(v3, 0, sizeof(*v3));
2680 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
2681 v3->lmm_stripe_count = cpu_to_le16(lo->ldo_def_stripenr);
2682 v3->lmm_stripe_offset = cpu_to_le16(lo->ldo_def_stripe_offset);
2683 v3->lmm_stripe_size = cpu_to_le32(lo->ldo_def_stripe_size);
2684 if (lo->ldo_pool != NULL)
2685 strlcpy(v3->lmm_pool_name, lo->ldo_pool,
2686 sizeof(v3->lmm_pool_name));
2688 info->lti_buf.lb_buf = v3;
2689 info->lti_buf.lb_len = sizeof(*v3);
2692 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
2693 XATTR_NAME_LOV, 0, th);
2695 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
2696 XATTR_NAME_LOV, 0, th,
2705 static int lod_declare_dir_striping_create(const struct lu_env *env,
2706 struct dt_object *dt,
2707 struct lu_attr *attr,
2708 struct dt_object_format *dof,
2711 return lod_dir_striping_create_internal(env, dt, attr, dof, th, true);
2714 static int lod_dir_striping_create(const struct lu_env *env,
2715 struct dt_object *dt,
2716 struct lu_attr *attr,
2717 struct dt_object_format *dof,
2720 return lod_dir_striping_create_internal(env, dt, attr, dof, th, false);
2724 * Implementation of dt_object_operations::do_xattr_set.
2726 * Sets specified extended attribute on the object. Three types of EAs are
2728 * LOV EA - stores striping for a regular file or default striping (when set
2730 * LMV EA - stores a marker for the striped directories
2731 * DMV EA - stores default directory striping
2733 * When striping is applied to a non-striped existing object (this is called
2734 * late striping), then LOD notices the caller wants to turn the object into a
2735 * striped one. The stripe objects are created and appropriate EA is set:
2736 * LOV EA storing all the stripes directly or LMV EA storing just a small header
2737 * with striping configuration.
2739 * \see dt_object_operations::do_xattr_set() in the API description for details.
2741 static int lod_xattr_set(const struct lu_env *env,
2742 struct dt_object *dt, const struct lu_buf *buf,
2743 const char *name, int fl, struct thandle *th,
2744 struct lustre_capa *capa)
2746 struct dt_object *next = dt_object_child(dt);
2750 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2751 strcmp(name, XATTR_NAME_LMV) == 0) {
2752 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
2754 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
2755 LMV_HASH_FLAG_MIGRATION)
2756 rc = dt_xattr_set(env, next, buf, name, fl, th, capa);
2758 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
2763 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2764 strcmp(name, XATTR_NAME_LOV) == 0) {
2766 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th, capa);
2768 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2769 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2771 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
2774 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
2775 !strcmp(name, XATTR_NAME_LOV)) {
2776 /* in case of lov EA swap, just set it
2777 * if not, it is a replay so check striping match what we
2778 * already have during req replay, declare_xattr_set()
2779 * defines striping, then create() does the work
2781 if (fl & LU_XATTR_REPLACE) {
2782 /* free stripes, then update disk */
2783 lod_object_free_striping(env, lod_dt_obj(dt));
2784 rc = dt_xattr_set(env, next, buf, name, fl, th, capa);
2786 rc = lod_striping_create(env, dt, NULL, NULL, th);
2791 /* then all other xattr */
2792 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th, capa);
2798 * Implementation of dt_object_operations::do_declare_xattr_del.
2800 * \see dt_object_operations::do_declare_xattr_del() in the API description
2803 static int lod_declare_xattr_del(const struct lu_env *env,
2804 struct dt_object *dt, const char *name,
2807 return dt_declare_xattr_del(env, dt_object_child(dt), name, th);
2811 * Implementation of dt_object_operations::do_xattr_del.
2813 * If EA storing a regular striping is being deleted, then release
2814 * all the references to the stripe objects in core.
2816 * \see dt_object_operations::do_xattr_del() in the API description for details.
2818 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
2819 const char *name, struct thandle *th,
2820 struct lustre_capa *capa)
2822 if (!strcmp(name, XATTR_NAME_LOV))
2823 lod_object_free_striping(env, lod_dt_obj(dt));
2824 return dt_xattr_del(env, dt_object_child(dt), name, th, capa);
2828 * Implementation of dt_object_operations::do_xattr_list.
2830 * \see dt_object_operations::do_xattr_list() in the API description
2833 static int lod_xattr_list(const struct lu_env *env,
2834 struct dt_object *dt, const struct lu_buf *buf,
2835 struct lustre_capa *capa)
2837 return dt_xattr_list(env, dt_object_child(dt), buf, capa);
2841 * Initialize a pool the object belongs to.
2843 * When a striped object is being created, striping configuration
2844 * may demand the stripes are allocated on a limited set of the
2845 * targets. These limited sets are known as "pools". So we copy
2846 * a pool name into the object and later actual creation methods
2847 * (like lod_object_create()) will use this information to allocate
2848 * the stripes properly.
2850 * \param[in] o object
2851 * \param[in] pool pool name
2853 int lod_object_set_pool(struct lod_object *o, char *pool)
2858 len = strlen(o->ldo_pool);
2859 OBD_FREE(o->ldo_pool, len + 1);
2864 OBD_ALLOC(o->ldo_pool, len + 1);
2865 if (o->ldo_pool == NULL)
2867 strcpy(o->ldo_pool, pool);
2872 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
2874 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
2879 * Cache default regular striping in the object.
2881 * To improve performance of striped regular object creation we cache
2882 * default LOV striping (if it exists) in the parent directory object.
2884 * \param[in] env execution environment
2885 * \param[in] lp object
2887 * \retval 0 on success
2888 * \retval negative if failed
2890 static int lod_cache_parent_lov_striping(const struct lu_env *env,
2891 struct lod_object *lp)
2893 struct lod_thread_info *info = lod_env_info(env);
2894 struct lov_user_md_v1 *v1 = NULL;
2895 struct lov_user_md_v3 *v3 = NULL;
2899 /* called from MDD without parent being write locked,
2901 dt_write_lock(env, dt_object_child(&lp->ldo_obj), 0);
2902 rc = lod_get_lov_ea(env, lp);
2906 if (rc < (typeof(rc))sizeof(struct lov_user_md)) {
2907 /* don't lookup for non-existing or invalid striping */
2908 lp->ldo_def_striping_set = 0;
2909 lp->ldo_striping_cached = 1;
2910 lp->ldo_def_stripe_size = 0;
2911 lp->ldo_def_stripenr = 0;
2912 lp->ldo_def_stripe_offset = (typeof(v1->lmm_stripe_offset))(-1);
2913 GOTO(unlock, rc = 0);
2917 v1 = info->lti_ea_store;
2918 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
2919 lustre_swab_lov_user_md_v1(v1);
2920 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
2921 v3 = (struct lov_user_md_v3 *)v1;
2922 lustre_swab_lov_user_md_v3(v3);
2925 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1)
2926 GOTO(unlock, rc = 0);
2928 if (v1->lmm_pattern != LOV_PATTERN_RAID0 && v1->lmm_pattern != 0)
2929 GOTO(unlock, rc = 0);
2931 CDEBUG(D_INFO, DFID" stripe_count=%d stripe_size=%d stripe_offset=%d\n",
2932 PFID(lu_object_fid(&lp->ldo_obj.do_lu)),
2933 (int)v1->lmm_stripe_count,
2934 (int)v1->lmm_stripe_size, (int)v1->lmm_stripe_offset);
2936 lp->ldo_def_stripenr = v1->lmm_stripe_count;
2937 lp->ldo_def_stripe_size = v1->lmm_stripe_size;
2938 lp->ldo_def_stripe_offset = v1->lmm_stripe_offset;
2939 lp->ldo_striping_cached = 1;
2940 lp->ldo_def_striping_set = 1;
2941 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2942 /* XXX: sanity check here */
2943 v3 = (struct lov_user_md_v3 *) v1;
2944 if (v3->lmm_pool_name[0])
2945 lod_object_set_pool(lp, v3->lmm_pool_name);
2949 dt_write_unlock(env, dt_object_child(&lp->ldo_obj));
2955 * Cache default directory striping in the object.
2957 * To improve performance of striped directory creation we cache default
2958 * directory striping (if it exists) in the parent directory object.
2960 * \param[in] env execution environment
2961 * \param[in] lp object
2963 * \retval 0 on success
2964 * \retval negative if failed
2966 static int lod_cache_parent_lmv_striping(const struct lu_env *env,
2967 struct lod_object *lp)
2969 struct lod_thread_info *info = lod_env_info(env);
2970 struct lmv_user_md_v1 *v1 = NULL;
2974 /* called from MDD without parent being write locked,
2976 dt_write_lock(env, dt_object_child(&lp->ldo_obj), 0);
2977 rc = lod_get_default_lmv_ea(env, lp);
2981 if (rc < (typeof(rc))sizeof(struct lmv_user_md)) {
2982 /* don't lookup for non-existing or invalid striping */
2983 lp->ldo_dir_def_striping_set = 0;
2984 lp->ldo_dir_striping_cached = 1;
2985 lp->ldo_dir_def_stripenr = 0;
2986 lp->ldo_dir_def_stripe_offset =
2987 (typeof(v1->lum_stripe_offset))(-1);
2988 lp->ldo_dir_def_hash_type = LMV_HASH_TYPE_FNV_1A_64;
2989 GOTO(unlock, rc = 0);
2993 v1 = info->lti_ea_store;
2995 lp->ldo_dir_def_stripenr = le32_to_cpu(v1->lum_stripe_count);
2996 lp->ldo_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
2997 lp->ldo_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
2998 lp->ldo_dir_def_striping_set = 1;
2999 lp->ldo_dir_striping_cached = 1;
3003 dt_write_unlock(env, dt_object_child(&lp->ldo_obj));
3008 * Cache default striping in the object.
3010 * To improve performance of striped object creation we cache default striping
3011 * (if it exists) in the parent directory object. We always cache default
3012 * striping for the regular files (stored in LOV EA) and we cache default
3013 * striping for the directories if requested by \a child_mode (when a new
3014 * directory is being created).
3016 * \param[in] env execution environment
3017 * \param[in] lp object
3018 * \param[in] child_mode new object's mode
3020 * \retval 0 on success
3021 * \retval negative if failed
3023 static int lod_cache_parent_striping(const struct lu_env *env,
3024 struct lod_object *lp,
3030 rc = lod_load_striping(env, lp);
3034 if (!lp->ldo_striping_cached) {
3035 /* we haven't tried to get default striping for
3036 * the directory yet, let's cache it in the object */
3037 rc = lod_cache_parent_lov_striping(env, lp);
3042 if (S_ISDIR(child_mode) && !lp->ldo_dir_striping_cached)
3043 rc = lod_cache_parent_lmv_striping(env, lp);
3049 * Implementation of dt_object_operations::do_ah_init.
3051 * This method is used to make a decision on the striping configuration for the
3052 * object being created. It can be taken from the \a parent object if it exists,
3053 * or filesystem's default. The resulting configuration (number of stripes,
3054 * stripe size/offset, pool name, etc) is stored in the object itself and will
3055 * be used by the methods like ->doo_declare_create().
3057 * \see dt_object_operations::do_ah_init() in the API description for details.
3059 static void lod_ah_init(const struct lu_env *env,
3060 struct dt_allocation_hint *ah,
3061 struct dt_object *parent,
3062 struct dt_object *child,
3065 struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
3066 struct dt_object *nextp = NULL;
3067 struct dt_object *nextc;
3068 struct lod_object *lp = NULL;
3069 struct lod_object *lc;
3070 struct lov_desc *desc;
3076 if (likely(parent)) {
3077 nextp = dt_object_child(parent);
3078 lp = lod_dt_obj(parent);
3079 rc = lod_load_striping(env, lp);
3084 nextc = dt_object_child(child);
3085 lc = lod_dt_obj(child);
3087 LASSERT(lc->ldo_stripenr == 0);
3088 LASSERT(lc->ldo_stripe == NULL);
3091 * local object may want some hints
3092 * in case of late striping creation, ->ah_init()
3093 * can be called with local object existing
3095 if (!dt_object_exists(nextc) || dt_object_remote(nextc)) {
3096 struct dt_object *obj;
3098 obj = (nextp != NULL && dt_object_remote(nextp)) ? NULL : nextp;
3099 nextc->do_ops->do_ah_init(env, ah, obj, nextc, child_mode);
3102 if (S_ISDIR(child_mode)) {
3103 if (lc->ldo_dir_stripe == NULL) {
3104 OBD_ALLOC_PTR(lc->ldo_dir_stripe);
3105 if (lc->ldo_dir_stripe == NULL)
3109 if (lp->ldo_dir_stripe == NULL) {
3110 OBD_ALLOC_PTR(lp->ldo_dir_stripe);
3111 if (lp->ldo_dir_stripe == NULL)
3115 rc = lod_cache_parent_striping(env, lp, child_mode);
3119 /* transfer defaults to new directory */
3120 if (lp->ldo_striping_cached) {
3122 lod_object_set_pool(lc, lp->ldo_pool);
3123 lc->ldo_def_stripenr = lp->ldo_def_stripenr;
3124 lc->ldo_def_stripe_size = lp->ldo_def_stripe_size;
3125 lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
3126 lc->ldo_striping_cached = 1;
3127 lc->ldo_def_striping_set = 1;
3128 CDEBUG(D_OTHER, "inherite EA sz:%d off:%d nr:%d\n",
3129 (int)lc->ldo_def_stripe_size,
3130 (int)lc->ldo_def_stripe_offset,
3131 (int)lc->ldo_def_stripenr);
3134 /* transfer dir defaults to new directory */
3135 if (lp->ldo_dir_striping_cached) {
3136 lc->ldo_dir_def_stripenr = lp->ldo_dir_def_stripenr;
3137 lc->ldo_dir_def_stripe_offset =
3138 lp->ldo_dir_def_stripe_offset;
3139 lc->ldo_dir_def_hash_type =
3140 lp->ldo_dir_def_hash_type;
3141 lc->ldo_dir_striping_cached = 1;
3142 lc->ldo_dir_def_striping_set = 1;
3143 CDEBUG(D_INFO, "inherit default EA nr:%d off:%d t%u\n",
3144 (int)lc->ldo_dir_def_stripenr,
3145 (int)lc->ldo_dir_def_stripe_offset,
3146 lc->ldo_dir_def_hash_type);
3149 /* It should always honour the specified stripes */
3150 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0) {
3151 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
3153 rc = lod_verify_md_striping(d, lum1);
3155 le32_to_cpu(lum1->lum_stripe_count) > 1) {
3156 /* Directory will be striped only if
3157 * stripe_count > 1 */
3159 le32_to_cpu(lum1->lum_stripe_count);
3160 lc->ldo_dir_stripe_offset =
3161 le32_to_cpu(lum1->lum_stripe_offset);
3162 lc->ldo_dir_hash_type =
3163 le32_to_cpu(lum1->lum_hash_type);
3164 CDEBUG(D_INFO, "set stripe EA nr:%hu off:%d\n",
3166 (int)lc->ldo_dir_stripe_offset);
3168 /* then check whether there is default stripes from parent */
3169 } else if (lp->ldo_dir_def_striping_set) {
3170 /* If there are default dir stripe from parent */
3171 lc->ldo_stripenr = lp->ldo_dir_def_stripenr;
3172 lc->ldo_dir_stripe_offset =
3173 lp->ldo_dir_def_stripe_offset;
3174 lc->ldo_dir_hash_type =
3175 lp->ldo_dir_def_hash_type;
3176 CDEBUG(D_INFO, "inherit EA nr:%hu off:%d\n",
3178 (int)lc->ldo_dir_stripe_offset);
3180 /* set default stripe for this directory */
3181 lc->ldo_stripenr = 0;
3182 lc->ldo_dir_stripe_offset = -1;
3185 CDEBUG(D_INFO, "final striping count:%hu, offset:%d\n",
3186 lc->ldo_stripenr, (int)lc->ldo_dir_stripe_offset);
3192 * if object is going to be striped over OSTs, transfer default
3193 * striping information to the child, so that we can use it
3194 * during declaration and creation
3196 if (!lod_object_will_be_striped(S_ISREG(child_mode),
3197 lu_object_fid(&child->do_lu)))
3200 * try from the parent
3202 if (likely(parent)) {
3203 lod_cache_parent_striping(env, lp, child_mode);
3205 lc->ldo_def_stripe_offset = LOV_OFFSET_DEFAULT;
3207 if (lp->ldo_def_striping_set) {
3209 lod_object_set_pool(lc, lp->ldo_pool);
3210 lc->ldo_stripenr = lp->ldo_def_stripenr;
3211 lc->ldo_stripe_size = lp->ldo_def_stripe_size;
3212 lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
3213 CDEBUG(D_OTHER, "striping from parent: #%d, sz %d %s\n",
3214 lc->ldo_stripenr, lc->ldo_stripe_size,
3215 lp->ldo_pool ? lp->ldo_pool : "");
3220 * if the parent doesn't provide with specific pattern, grab fs-wide one
3222 desc = &d->lod_desc;
3223 if (lc->ldo_stripenr == 0)
3224 lc->ldo_stripenr = desc->ld_default_stripe_count;
3225 if (lc->ldo_stripe_size == 0)
3226 lc->ldo_stripe_size = desc->ld_default_stripe_size;
3227 CDEBUG(D_OTHER, "final striping: # %d stripes, sz %d from %s\n",
3228 lc->ldo_stripenr, lc->ldo_stripe_size,
3229 lc->ldo_pool ? lc->ldo_pool : "");
3232 /* we do not cache stripe information for slave stripe, see
3233 * lod_xattr_set_lov_on_dir */
3234 if (lp != NULL && lp->ldo_dir_slave_stripe)
3235 lod_lov_stripe_cache_clear(lp);
3240 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
3242 * Size initialization on late striping.
3244 * Propagate the size of a truncated object to a deferred striping.
3245 * This function handles a special case when truncate was done on a
3246 * non-striped object and now while the striping is being created
3247 * we can't lose that size, so we have to propagate it to the stripes
3250 * \param[in] env execution environment
3251 * \param[in] dt object
3252 * \param[in] th transaction handle
3254 * \retval 0 on success
3255 * \retval negative if failed
3257 static int lod_declare_init_size(const struct lu_env *env,
3258 struct dt_object *dt, struct thandle *th)
3260 struct dt_object *next = dt_object_child(dt);
3261 struct lod_object *lo = lod_dt_obj(dt);
3262 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3263 uint64_t size, offs;
3267 /* XXX: we support the simplest (RAID0) striping so far */
3268 LASSERT(lo->ldo_stripe || lo->ldo_stripenr == 0);
3269 LASSERT(lo->ldo_stripe_size > 0);
3271 rc = dt_attr_get(env, next, attr, BYPASS_CAPA);
3272 LASSERT(attr->la_valid & LA_SIZE);
3276 size = attr->la_size;
3280 /* ll_do_div64(a, b) returns a % b, and a = a / b */
3281 ll_do_div64(size, (__u64) lo->ldo_stripe_size);
3282 stripe = ll_do_div64(size, (__u64) lo->ldo_stripenr);
3284 size = size * lo->ldo_stripe_size;
3285 offs = attr->la_size;
3286 size += ll_do_div64(offs, lo->ldo_stripe_size);
3288 attr->la_valid = LA_SIZE;
3289 attr->la_size = size;
3291 rc = dt_declare_attr_set(env, lo->ldo_stripe[stripe], attr, th);
3297 * Declare creation of striped object.
3299 * The function declares creation stripes for a regular object. The function
3300 * also declares whether the stripes will be created with non-zero size if
3301 * previously size was set non-zero on the master object. If object \a dt is
3302 * not local, then only fully defined striping can be applied in \a lovea.
3303 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
3306 * \param[in] env execution environment
3307 * \param[in] dt object
3308 * \param[in] attr attributes the stripes will be created with
3309 * \param[in] lovea a buffer containing striping description
3310 * \param[in] th transaction handle
3312 * \retval 0 on success
3313 * \retval negative if failed
3315 int lod_declare_striped_object(const struct lu_env *env, struct dt_object *dt,
3316 struct lu_attr *attr,
3317 const struct lu_buf *lovea, struct thandle *th)
3319 struct lod_thread_info *info = lod_env_info(env);
3320 struct dt_object *next = dt_object_child(dt);
3321 struct lod_object *lo = lod_dt_obj(dt);
3325 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO)) {
3326 /* failed to create striping, let's reset
3327 * config so that others don't get confused */
3328 lod_object_free_striping(env, lo);
3329 GOTO(out, rc = -ENOMEM);
3332 if (!dt_object_remote(next)) {
3333 /* choose OST and generate appropriate objects */
3334 rc = lod_qos_prep_create(env, lo, attr, lovea, th);
3336 /* failed to create striping, let's reset
3337 * config so that others don't get confused */
3338 lod_object_free_striping(env, lo);
3343 * declare storage for striping data
3345 info->lti_buf.lb_len = lov_mds_md_size(lo->ldo_stripenr,
3346 lo->ldo_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1);
3348 /* LOD can not choose OST objects for remote objects, i.e.
3349 * stripes must be ready before that. Right now, it can only
3350 * happen during migrate, i.e. migrate process needs to create
3351 * remote regular file (mdd_migrate_create), then the migrate
3352 * process will provide stripeEA. */
3353 LASSERT(lovea != NULL);
3354 info->lti_buf = *lovea;
3357 rc = dt_declare_xattr_set(env, next, &info->lti_buf,
3358 XATTR_NAME_LOV, 0, th);
3363 * if striping is created with local object's size > 0,
3364 * we have to propagate this size to specific object
3365 * the case is possible only when local object was created previously
3367 if (dt_object_exists(next))
3368 rc = lod_declare_init_size(env, dt, th);
3375 * Implementation of dt_object_operations::do_declare_create.
3377 * The method declares creation of a new object. If the object will be striped,
3378 * then helper functions are called to find FIDs for the stripes, declare
3379 * creation of the stripes and declare initialization of the striping
3380 * information to be stored in the master object.
3382 * \see dt_object_operations::do_declare_create() in the API description
3385 static int lod_declare_object_create(const struct lu_env *env,
3386 struct dt_object *dt,
3387 struct lu_attr *attr,
3388 struct dt_allocation_hint *hint,
3389 struct dt_object_format *dof,
3392 struct dt_object *next = dt_object_child(dt);
3393 struct lod_object *lo = lod_dt_obj(dt);
3402 * first of all, we declare creation of local object
3404 rc = dt_declare_create(env, next, attr, hint, dof, th);
3408 if (dof->dof_type == DFT_SYM)
3409 dt->do_body_ops = &lod_body_lnk_ops;
3412 * it's lod_ah_init() who has decided the object will striped
3414 if (dof->dof_type == DFT_REGULAR) {
3415 /* callers don't want stripes */
3416 /* XXX: all tricky interactions with ->ah_make_hint() decided
3417 * to use striping, then ->declare_create() behaving differently
3418 * should be cleaned */
3419 if (dof->u.dof_reg.striped == 0)
3420 lo->ldo_stripenr = 0;
3421 if (lo->ldo_stripenr > 0)
3422 rc = lod_declare_striped_object(env, dt, attr,
3424 } else if (dof->dof_type == DFT_DIR) {
3425 /* Orphan object (like migrating object) does not have
3426 * lod_dir_stripe, see lod_ah_init */
3427 if (lo->ldo_dir_stripe != NULL)
3428 rc = lod_declare_dir_striping_create(env, dt, attr,
3436 * Creation of a striped regular object.
3438 * The function is called to create the stripe objects for a regular
3439 * striped file. This can happen at the initial object creation or
3440 * when the caller asks LOD to do so using ->do_xattr_set() method
3441 * (so called late striping). Notice all the information are already
3442 * prepared in the form of the list of objects (ldo_stripe field).
3443 * This is done during declare phase.
3445 * \param[in] env execution environment
3446 * \param[in] dt object
3447 * \param[in] attr attributes the stripes will be created with
3448 * \param[in] dof format of stripes (see OSD API description)
3449 * \param[in] th transaction handle
3451 * \retval 0 on success
3452 * \retval negative if failed
3454 int lod_striping_create(const struct lu_env *env, struct dt_object *dt,
3455 struct lu_attr *attr, struct dt_object_format *dof,
3458 struct lod_object *lo = lod_dt_obj(dt);
3462 LASSERT(lo->ldo_striping_cached == 0);
3464 /* create all underlying objects */
3465 for (i = 0; i < lo->ldo_stripenr; i++) {
3466 LASSERT(lo->ldo_stripe[i]);
3467 rc = dt_create(env, lo->ldo_stripe[i], attr, NULL, dof, th);
3473 rc = lod_generate_and_set_lovea(env, lo, th);
3479 * Implementation of dt_object_operations::do_create.
3481 * If any of preceeding methods (like ->do_declare_create(),
3482 * ->do_ah_init(), etc) chose to create a striped object,
3483 * then this method will create the master and the stripes.
3485 * \see dt_object_operations::do_create() in the API description for details.
3487 static int lod_object_create(const struct lu_env *env, struct dt_object *dt,
3488 struct lu_attr *attr,
3489 struct dt_allocation_hint *hint,
3490 struct dt_object_format *dof, struct thandle *th)
3492 struct dt_object *next = dt_object_child(dt);
3493 struct lod_object *lo = lod_dt_obj(dt);
3497 /* create local object */
3498 rc = dt_create(env, next, attr, hint, dof, th);
3502 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3503 lo->ldo_stripe && dof->u.dof_reg.striped != 0)
3504 rc = lod_striping_create(env, dt, attr, dof, th);
3510 * Implementation of dt_object_operations::do_declare_destroy.
3512 * If the object is a striped directory, then the function declares reference
3513 * removal from the master object (this is an index) to the stripes and declares
3514 * destroy of all the stripes. In all the cases, it declares an intention to
3515 * destroy the object itself.
3517 * \see dt_object_operations::do_declare_destroy() in the API description
3520 static int lod_declare_object_destroy(const struct lu_env *env,
3521 struct dt_object *dt,
3524 struct dt_object *next = dt_object_child(dt);
3525 struct lod_object *lo = lod_dt_obj(dt);
3526 struct lod_thread_info *info = lod_env_info(env);
3527 char *stripe_name = info->lti_key;
3532 * load striping information, notice we don't do this when object
3533 * is being initialized as we don't need this information till
3534 * few specific cases like destroy, chown
3536 rc = lod_load_striping(env, lo);
3540 /* declare destroy for all underlying objects */
3541 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3542 rc = next->do_ops->do_index_try(env, next,
3543 &dt_directory_features);
3547 for (i = 0; i < lo->ldo_stripenr; i++) {
3548 rc = dt_declare_ref_del(env, next, th);
3551 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3552 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
3554 rc = dt_declare_delete(env, next,
3555 (const struct dt_key *)stripe_name, th);
3561 * we declare destroy for the local object
3563 rc = dt_declare_destroy(env, next, th);
3567 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ))
3570 /* declare destroy all striped objects */
3571 for (i = 0; i < lo->ldo_stripenr; i++) {
3572 if (likely(lo->ldo_stripe[i] != NULL)) {
3573 rc = dt_declare_destroy(env, lo->ldo_stripe[i], th);
3583 * Implementation of dt_object_operations::do_destroy.
3585 * If the object is a striped directory, then the function removes references
3586 * from the master object (this is an index) to the stripes and destroys all
3587 * the stripes. In all the cases, the function destroys the object itself.
3589 * \see dt_object_operations::do_destroy() in the API description for details.
3591 static int lod_object_destroy(const struct lu_env *env,
3592 struct dt_object *dt, struct thandle *th)
3594 struct dt_object *next = dt_object_child(dt);
3595 struct lod_object *lo = lod_dt_obj(dt);
3596 struct lod_thread_info *info = lod_env_info(env);
3597 char *stripe_name = info->lti_key;
3602 /* destroy sub-stripe of master object */
3603 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3604 rc = next->do_ops->do_index_try(env, next,
3605 &dt_directory_features);
3609 for (i = 0; i < lo->ldo_stripenr; i++) {
3610 rc = dt_ref_del(env, next, th);
3614 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3615 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
3618 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
3619 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
3620 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
3622 rc = dt_delete(env, next,
3623 (const struct dt_key *)stripe_name,
3629 rc = dt_destroy(env, next, th);
3633 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ))
3636 /* destroy all striped objects */
3637 for (i = 0; i < lo->ldo_stripenr; i++) {
3638 if (likely(lo->ldo_stripe[i] != NULL) &&
3639 (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
3640 i == cfs_fail_val)) {
3641 rc = dt_destroy(env, lo->ldo_stripe[i], th);
3651 * Implementation of dt_object_operations::do_declare_ref_add.
3653 * \see dt_object_operations::do_declare_ref_add() in the API description
3656 static int lod_declare_ref_add(const struct lu_env *env,
3657 struct dt_object *dt, struct thandle *th)
3659 return dt_declare_ref_add(env, dt_object_child(dt), th);
3663 * Implementation of dt_object_operations::do_ref_add.
3665 * \see dt_object_operations::do_ref_add() in the API description for details.
3667 static int lod_ref_add(const struct lu_env *env,
3668 struct dt_object *dt, struct thandle *th)
3670 return dt_ref_add(env, dt_object_child(dt), th);
3674 * Implementation of dt_object_operations::do_declare_ref_del.
3676 * \see dt_object_operations::do_declare_ref_del() in the API description
3679 static int lod_declare_ref_del(const struct lu_env *env,
3680 struct dt_object *dt, struct thandle *th)
3682 return dt_declare_ref_del(env, dt_object_child(dt), th);
3686 * Implementation of dt_object_operations::do_ref_del
3688 * \see dt_object_operations::do_ref_del() in the API description for details.
3690 static int lod_ref_del(const struct lu_env *env,
3691 struct dt_object *dt, struct thandle *th)
3693 return dt_ref_del(env, dt_object_child(dt), th);
3697 * Implementation of dt_object_operations::do_capa_get.
3699 * \see dt_object_operations::do_capa_get() in the API description for details.
3701 static struct obd_capa *lod_capa_get(const struct lu_env *env,
3702 struct dt_object *dt,
3703 struct lustre_capa *old, __u64 opc)
3705 return dt_capa_get(env, dt_object_child(dt), old, opc);
3709 * Implementation of dt_object_operations::do_object_sync.
3711 * \see dt_object_operations::do_object_sync() in the API description
3714 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
3715 __u64 start, __u64 end)
3717 return dt_object_sync(env, dt_object_child(dt), start, end);
3720 struct lod_slave_locks {
3722 struct lustre_handle lsl_handle[0];
3726 * Release LDLM locks on the stripes of a striped directory.
3728 * Iterates over all the locks taken on the stripe objects and
3729 * release them using ->do_object_unlock() method.
3731 * \param[in] env execution environment
3732 * \param[in] dt striped object
3733 * \param[in] einfo lock description
3734 * \param[in] policy data describing requested lock
3736 * \retval 0 on success
3737 * \retval negative if failed
3739 static int lod_object_unlock_internal(const struct lu_env *env,
3740 struct dt_object *dt,
3741 struct ldlm_enqueue_info *einfo,
3742 ldlm_policy_data_t *policy)
3744 struct lod_object *lo = lod_dt_obj(dt);
3745 struct lod_slave_locks *slave_locks = einfo->ei_cbdata;
3750 if (slave_locks == NULL)
3753 for (i = 1; i < slave_locks->lsl_lock_count; i++) {
3754 if (lustre_handle_is_used(&slave_locks->lsl_handle[i])) {
3757 einfo->ei_cbdata = &slave_locks->lsl_handle[i];
3758 rc1 = dt_object_unlock(env, lo->ldo_stripe[i], einfo,
3761 rc = rc == 0 ? rc1 : rc;
3769 * Implementation of dt_object_operations::do_object_unlock.
3771 * Used to release LDLM lock(s).
3773 * \see dt_object_operations::do_object_unlock() in the API description
3776 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
3777 struct ldlm_enqueue_info *einfo,
3778 union ldlm_policy_data *policy)
3780 struct lod_object *lo = lod_dt_obj(dt);
3781 struct lod_slave_locks *slave_locks = einfo->ei_cbdata;
3782 int slave_locks_size;
3786 if (slave_locks == NULL)
3789 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3792 rc = lod_load_striping(env, lo);
3796 /* Note: for remote lock for single stripe dir, MDT will cancel
3797 * the lock by lockh directly */
3798 if (lo->ldo_stripenr <= 1 && dt_object_remote(dt_object_child(dt)))
3801 /* Only cancel slave lock for striped dir */
3802 rc = lod_object_unlock_internal(env, dt, einfo, policy);
3804 slave_locks_size = sizeof(*slave_locks) + slave_locks->lsl_lock_count *
3805 sizeof(slave_locks->lsl_handle[0]);
3806 OBD_FREE(slave_locks, slave_locks_size);
3807 einfo->ei_cbdata = NULL;
3813 * Implementation of dt_object_operations::do_object_lock.
3815 * Used to get LDLM lock on the non-striped and striped objects.
3817 * \see dt_object_operations::do_object_lock() in the API description
3820 static int lod_object_lock(const struct lu_env *env,
3821 struct dt_object *dt,
3822 struct lustre_handle *lh,
3823 struct ldlm_enqueue_info *einfo,
3824 union ldlm_policy_data *policy)
3826 struct lod_object *lo = lod_dt_obj(dt);
3829 int slave_locks_size;
3830 struct lod_slave_locks *slave_locks = NULL;
3833 /* remote object lock */
3834 if (!einfo->ei_enq_slave) {
3835 LASSERT(dt_object_remote(dt));
3836 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
3840 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3843 rc = lod_load_striping(env, lo);
3848 if (lo->ldo_stripenr <= 1)
3851 slave_locks_size = sizeof(*slave_locks) + lo->ldo_stripenr *
3852 sizeof(slave_locks->lsl_handle[0]);
3853 /* Freed in lod_object_unlock */
3854 OBD_ALLOC(slave_locks, slave_locks_size);
3855 if (slave_locks == NULL)
3857 slave_locks->lsl_lock_count = lo->ldo_stripenr;
3859 /* striped directory lock */
3860 for (i = 1; i < lo->ldo_stripenr; i++) {
3861 struct lustre_handle lockh;
3862 struct ldlm_res_id *res_id;
3864 res_id = &lod_env_info(env)->lti_res_id;
3865 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
3867 einfo->ei_res_id = res_id;
3869 LASSERT(lo->ldo_stripe[i]);
3870 rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh, einfo,
3874 slave_locks->lsl_handle[i] = lockh;
3877 einfo->ei_cbdata = slave_locks;
3880 if (rc != 0 && slave_locks != NULL) {
3881 einfo->ei_cbdata = slave_locks;
3882 lod_object_unlock_internal(env, dt, einfo, policy);
3883 OBD_FREE(slave_locks, slave_locks_size);
3884 einfo->ei_cbdata = NULL;
3890 struct dt_object_operations lod_obj_ops = {
3891 .do_read_lock = lod_object_read_lock,
3892 .do_write_lock = lod_object_write_lock,
3893 .do_read_unlock = lod_object_read_unlock,
3894 .do_write_unlock = lod_object_write_unlock,
3895 .do_write_locked = lod_object_write_locked,
3896 .do_attr_get = lod_attr_get,
3897 .do_declare_attr_set = lod_declare_attr_set,
3898 .do_attr_set = lod_attr_set,
3899 .do_xattr_get = lod_xattr_get,
3900 .do_declare_xattr_set = lod_declare_xattr_set,
3901 .do_xattr_set = lod_xattr_set,
3902 .do_declare_xattr_del = lod_declare_xattr_del,
3903 .do_xattr_del = lod_xattr_del,
3904 .do_xattr_list = lod_xattr_list,
3905 .do_ah_init = lod_ah_init,
3906 .do_declare_create = lod_declare_object_create,
3907 .do_create = lod_object_create,
3908 .do_declare_destroy = lod_declare_object_destroy,
3909 .do_destroy = lod_object_destroy,
3910 .do_index_try = lod_index_try,
3911 .do_declare_ref_add = lod_declare_ref_add,
3912 .do_ref_add = lod_ref_add,
3913 .do_declare_ref_del = lod_declare_ref_del,
3914 .do_ref_del = lod_ref_del,
3915 .do_capa_get = lod_capa_get,
3916 .do_object_sync = lod_object_sync,
3917 .do_object_lock = lod_object_lock,
3918 .do_object_unlock = lod_object_unlock,
3922 * Implementation of dt_body_operations::dbo_read.
3924 * \see dt_body_operations::dbo_read() in the API description for details.
3926 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
3927 struct lu_buf *buf, loff_t *pos,
3928 struct lustre_capa *capa)
3930 struct dt_object *next = dt_object_child(dt);
3931 return next->do_body_ops->dbo_read(env, next, buf, pos, capa);
3935 * Implementation of dt_body_operations::dbo_declare_write.
3937 * \see dt_body_operations::dbo_declare_write() in the API description
3940 static ssize_t lod_declare_write(const struct lu_env *env,
3941 struct dt_object *dt,
3942 const struct lu_buf *buf, loff_t pos,
3945 return dt_declare_record_write(env, dt_object_child(dt),
3950 * Implementation of dt_body_operations::dbo_write.
3952 * \see dt_body_operations::dbo_write() in the API description for details.
3954 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
3955 const struct lu_buf *buf, loff_t *pos,
3956 struct thandle *th, struct lustre_capa *capa, int iq)
3958 struct dt_object *next = dt_object_child(dt);
3960 return next->do_body_ops->dbo_write(env, next, buf, pos, th, capa, iq);
3963 static const struct dt_body_operations lod_body_lnk_ops = {
3964 .dbo_read = lod_read,
3965 .dbo_declare_write = lod_declare_write,
3966 .dbo_write = lod_write
3970 * Implementation of lu_object_operations::loo_object_init.
3972 * The function determines the type and the index of the target device using
3973 * sequence of the object's FID. Then passes control down to the
3974 * corresponding device:
3975 * OSD for the local objects, OSP for remote
3977 * \see lu_object_operations::loo_object_init() in the API description
3980 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
3981 const struct lu_object_conf *conf)
3983 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
3984 struct lu_device *cdev = NULL;
3985 struct lu_object *cobj;
3986 struct lod_tgt_descs *ltd = NULL;
3987 struct lod_tgt_desc *tgt;
3989 int type = LU_SEQ_RANGE_ANY;
3993 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
3995 /* Note: Sometimes, it will Return EAGAIN here, see
3996 * ptrlpc_import_delay_req(), which might confuse
3997 * lu_object_find_at() and make it wait there incorrectly.
3998 * so we convert it to EIO here.*/
4005 if (type == LU_SEQ_RANGE_MDT &&
4006 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
4007 cdev = &lod->lod_child->dd_lu_dev;
4008 } else if (type == LU_SEQ_RANGE_MDT) {
4009 ltd = &lod->lod_mdt_descs;
4011 } else if (type == LU_SEQ_RANGE_OST) {
4012 ltd = &lod->lod_ost_descs;
4019 if (ltd->ltd_tgts_size > idx &&
4020 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
4021 tgt = LTD_TGT(ltd, idx);
4023 LASSERT(tgt != NULL);
4024 LASSERT(tgt->ltd_tgt != NULL);
4026 cdev = &(tgt->ltd_tgt->dd_lu_dev);
4028 lod_putref(lod, ltd);
4031 if (unlikely(cdev == NULL))
4034 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
4035 if (unlikely(cobj == NULL))
4038 lu_object_add(lo, cobj);
4045 * Release resources associated with striping.
4047 * If the object is striped (regular or directory), then release
4048 * the stripe objects references and free the ldo_stripe array.
4050 * \param[in] env execution environment
4051 * \param[in] lo object
4053 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
4057 if (lo->ldo_dir_stripe != NULL) {
4058 OBD_FREE_PTR(lo->ldo_dir_stripe);
4059 lo->ldo_dir_stripe = NULL;
4062 if (lo->ldo_stripe) {
4063 LASSERT(lo->ldo_stripes_allocated > 0);
4065 for (i = 0; i < lo->ldo_stripenr; i++) {
4066 if (lo->ldo_stripe[i])
4067 lu_object_put(env, &lo->ldo_stripe[i]->do_lu);
4070 i = sizeof(struct dt_object *) * lo->ldo_stripes_allocated;
4071 OBD_FREE(lo->ldo_stripe, i);
4072 lo->ldo_stripe = NULL;
4073 lo->ldo_stripes_allocated = 0;
4075 lo->ldo_stripenr = 0;
4076 lo->ldo_pattern = 0;
4080 * Implementation of lu_object_operations::loo_object_start.
4082 * \see lu_object_operations::loo_object_start() in the API description
4085 static int lod_object_start(const struct lu_env *env, struct lu_object *o)
4087 if (S_ISLNK(o->lo_header->loh_attr & S_IFMT))
4088 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_lnk_ops;
4093 * Implementation of lu_object_operations::loo_object_free.
4095 * \see lu_object_operations::loo_object_free() in the API description
4098 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
4100 struct lod_object *mo = lu2lod_obj(o);
4103 * release all underlying object pinned
4106 lod_object_free_striping(env, mo);
4108 lod_object_set_pool(mo, NULL);
4111 OBD_SLAB_FREE_PTR(mo, lod_object_kmem);
4115 * Implementation of lu_object_operations::loo_object_release.
4117 * \see lu_object_operations::loo_object_release() in the API description
4120 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
4122 /* XXX: shouldn't we release everything here in case if object
4123 * creation failed before? */
4127 * Implementation of lu_object_operations::loo_object_print.
4129 * \see lu_object_operations::loo_object_print() in the API description
4132 static int lod_object_print(const struct lu_env *env, void *cookie,
4133 lu_printer_t p, const struct lu_object *l)
4135 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
4137 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
4140 struct lu_object_operations lod_lu_obj_ops = {
4141 .loo_object_init = lod_object_init,
4142 .loo_object_start = lod_object_start,
4143 .loo_object_free = lod_object_free,
4144 .loo_object_release = lod_object_release,
4145 .loo_object_print = lod_object_print,