4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2014, Intel Corporation.
29 * lustre/lod/lod_object.c
31 * This file contains implementations of methods for the OSD API
32 * for the Logical Object Device (LOD) layer, which provides a virtual
33 * local OSD object interface to the MDD layer, and abstracts the
34 * addressing of local (OSD) and remote (OSP) objects. The API is
35 * described in the file lustre/include/dt_object.h and in
36 * lustre/doc/osd-api.txt.
38 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
41 #define DEBUG_SUBSYSTEM S_MDS
44 #include <obd_class.h>
45 #include <lustre_ver.h>
46 #include <obd_support.h>
47 #include <lprocfs_status.h>
49 #include <lustre_fid.h>
50 #include <lustre_param.h>
51 #include <lustre_fid.h>
52 #include <lustre_lmv.h>
53 #include <md_object.h>
54 #include <lustre_linkea.h>
56 #include "lod_internal.h"
58 static const char dot[] = ".";
59 static const char dotdot[] = "..";
61 static const struct dt_body_operations lod_body_lnk_ops;
64 * Implementation of dt_index_operations::dio_lookup
66 * Used with regular (non-striped) objects.
68 * \see dt_index_operations::dio_lookup() in the API description for details.
70 static int lod_index_lookup(const struct lu_env *env, struct dt_object *dt,
71 struct dt_rec *rec, const struct dt_key *key)
73 struct dt_object *next = dt_object_child(dt);
74 return next->do_index_ops->dio_lookup(env, next, rec, key);
78 * Implementation of dt_index_operations::dio_declare_insert.
80 * Used with regular (non-striped) objects.
82 * \see dt_index_operations::dio_declare_insert() in the API description
85 static int lod_declare_index_insert(const struct lu_env *env,
87 const struct dt_rec *rec,
88 const struct dt_key *key,
91 return lod_sub_object_declare_insert(env, dt_object_child(dt),
96 * Implementation of dt_index_operations::dio_insert.
98 * Used with regular (non-striped) objects
100 * \see dt_index_operations::dio_insert() in the API description for details.
102 static int lod_index_insert(const struct lu_env *env,
103 struct dt_object *dt,
104 const struct dt_rec *rec,
105 const struct dt_key *key,
109 return lod_sub_object_index_insert(env, dt_object_child(dt), rec, key,
114 * Implementation of dt_index_operations::dio_declare_delete.
116 * Used with regular (non-striped) objects.
118 * \see dt_index_operations::dio_declare_delete() in the API description
121 static int lod_declare_index_delete(const struct lu_env *env,
122 struct dt_object *dt,
123 const struct dt_key *key,
126 return lod_sub_object_declare_delete(env, dt_object_child(dt), key,
131 * Implementation of dt_index_operations::dio_delete.
133 * Used with regular (non-striped) objects.
135 * \see dt_index_operations::dio_delete() in the API description for details.
137 static int lod_index_delete(const struct lu_env *env,
138 struct dt_object *dt,
139 const struct dt_key *key,
142 return lod_sub_object_delete(env, dt_object_child(dt), key, th);
146 * Implementation of dt_it_ops::init.
148 * Used with regular (non-striped) objects.
150 * \see dt_it_ops::init() in the API description for details.
152 static struct dt_it *lod_it_init(const struct lu_env *env,
153 struct dt_object *dt, __u32 attr)
155 struct dt_object *next = dt_object_child(dt);
156 struct lod_it *it = &lod_env_info(env)->lti_it;
157 struct dt_it *it_next;
159 it_next = next->do_index_ops->dio_it.init(env, next, attr);
163 /* currently we do not use more than one iterator per thread
164 * so we store it in thread info. if at some point we need
165 * more active iterators in a single thread, we can allocate
167 LASSERT(it->lit_obj == NULL);
169 it->lit_it = it_next;
172 return (struct dt_it *)it;
175 #define LOD_CHECK_IT(env, it) \
177 LASSERT((it)->lit_obj != NULL); \
178 LASSERT((it)->lit_it != NULL); \
182 * Implementation of dt_index_operations::dio_it.fini.
184 * Used with regular (non-striped) objects.
186 * \see dt_index_operations::dio_it.fini() in the API description for details.
188 static void lod_it_fini(const struct lu_env *env, struct dt_it *di)
190 struct lod_it *it = (struct lod_it *)di;
192 LOD_CHECK_IT(env, it);
193 it->lit_obj->do_index_ops->dio_it.fini(env, it->lit_it);
195 /* the iterator not in use any more */
201 * Implementation of dt_it_ops::get.
203 * Used with regular (non-striped) objects.
205 * \see dt_it_ops::get() in the API description for details.
207 static int lod_it_get(const struct lu_env *env, struct dt_it *di,
208 const struct dt_key *key)
210 const struct lod_it *it = (const struct lod_it *)di;
212 LOD_CHECK_IT(env, it);
213 return it->lit_obj->do_index_ops->dio_it.get(env, it->lit_it, key);
217 * Implementation of dt_it_ops::put.
219 * Used with regular (non-striped) objects.
221 * \see dt_it_ops::put() in the API description for details.
223 static void lod_it_put(const struct lu_env *env, struct dt_it *di)
225 struct lod_it *it = (struct lod_it *)di;
227 LOD_CHECK_IT(env, it);
228 return it->lit_obj->do_index_ops->dio_it.put(env, it->lit_it);
232 * Implementation of dt_it_ops::next.
234 * Used with regular (non-striped) objects
236 * \see dt_it_ops::next() in the API description for details.
238 static int lod_it_next(const struct lu_env *env, struct dt_it *di)
240 struct lod_it *it = (struct lod_it *)di;
242 LOD_CHECK_IT(env, it);
243 return it->lit_obj->do_index_ops->dio_it.next(env, it->lit_it);
247 * Implementation of dt_it_ops::key.
249 * Used with regular (non-striped) objects.
251 * \see dt_it_ops::key() in the API description for details.
253 static struct dt_key *lod_it_key(const struct lu_env *env,
254 const struct dt_it *di)
256 const struct lod_it *it = (const struct lod_it *)di;
258 LOD_CHECK_IT(env, it);
259 return it->lit_obj->do_index_ops->dio_it.key(env, it->lit_it);
263 * Implementation of dt_it_ops::key_size.
265 * Used with regular (non-striped) objects.
267 * \see dt_it_ops::key_size() in the API description for details.
269 static int lod_it_key_size(const struct lu_env *env, const struct dt_it *di)
271 struct lod_it *it = (struct lod_it *)di;
273 LOD_CHECK_IT(env, it);
274 return it->lit_obj->do_index_ops->dio_it.key_size(env, it->lit_it);
278 * Implementation of dt_it_ops::rec.
280 * Used with regular (non-striped) objects.
282 * \see dt_it_ops::rec() in the API description for details.
284 static int lod_it_rec(const struct lu_env *env, const struct dt_it *di,
285 struct dt_rec *rec, __u32 attr)
287 const struct lod_it *it = (const struct lod_it *)di;
289 LOD_CHECK_IT(env, it);
290 return it->lit_obj->do_index_ops->dio_it.rec(env, it->lit_it, rec,
295 * Implementation of dt_it_ops::rec_size.
297 * Used with regular (non-striped) objects.
299 * \see dt_it_ops::rec_size() in the API description for details.
301 static int lod_it_rec_size(const struct lu_env *env, const struct dt_it *di,
304 const struct lod_it *it = (const struct lod_it *)di;
306 LOD_CHECK_IT(env, it);
307 return it->lit_obj->do_index_ops->dio_it.rec_size(env, it->lit_it,
312 * Implementation of dt_it_ops::store.
314 * Used with regular (non-striped) objects.
316 * \see dt_it_ops::store() in the API description for details.
318 static __u64 lod_it_store(const struct lu_env *env, const struct dt_it *di)
320 const struct lod_it *it = (const struct lod_it *)di;
322 LOD_CHECK_IT(env, it);
323 return it->lit_obj->do_index_ops->dio_it.store(env, it->lit_it);
327 * Implementation of dt_it_ops::load.
329 * Used with regular (non-striped) objects.
331 * \see dt_it_ops::load() in the API description for details.
333 static int lod_it_load(const struct lu_env *env, const struct dt_it *di,
336 const struct lod_it *it = (const struct lod_it *)di;
338 LOD_CHECK_IT(env, it);
339 return it->lit_obj->do_index_ops->dio_it.load(env, it->lit_it, hash);
343 * Implementation of dt_it_ops::key_rec.
345 * Used with regular (non-striped) objects.
347 * \see dt_it_ops::rec() in the API description for details.
349 static int lod_it_key_rec(const struct lu_env *env, const struct dt_it *di,
352 const struct lod_it *it = (const struct lod_it *)di;
354 LOD_CHECK_IT(env, it);
355 return it->lit_obj->do_index_ops->dio_it.key_rec(env, it->lit_it,
359 static struct dt_index_operations lod_index_ops = {
360 .dio_lookup = lod_index_lookup,
361 .dio_declare_insert = lod_declare_index_insert,
362 .dio_insert = lod_index_insert,
363 .dio_declare_delete = lod_declare_index_delete,
364 .dio_delete = lod_index_delete,
372 .key_size = lod_it_key_size,
374 .rec_size = lod_it_rec_size,
375 .store = lod_it_store,
377 .key_rec = lod_it_key_rec,
382 * Implementation of dt_it_ops::init.
384 * Used with striped objects. Internally just initializes the iterator
385 * on the first stripe.
387 * \see dt_it_ops::init() in the API description for details.
389 static struct dt_it *lod_striped_it_init(const struct lu_env *env,
390 struct dt_object *dt, __u32 attr)
392 struct lod_object *lo = lod_dt_obj(dt);
393 struct dt_object *next;
394 struct lod_it *it = &lod_env_info(env)->lti_it;
395 struct dt_it *it_next;
398 LASSERT(lo->ldo_stripenr > 0);
399 next = lo->ldo_stripe[0];
400 LASSERT(next != NULL);
401 LASSERT(next->do_index_ops != NULL);
403 it_next = next->do_index_ops->dio_it.init(env, next, attr);
407 /* currently we do not use more than one iterator per thread
408 * so we store it in thread info. if at some point we need
409 * more active iterators in a single thread, we can allocate
411 LASSERT(it->lit_obj == NULL);
413 it->lit_stripe_index = 0;
415 it->lit_it = it_next;
418 return (struct dt_it *)it;
421 #define LOD_CHECK_STRIPED_IT(env, it, lo) \
423 LASSERT((it)->lit_obj != NULL); \
424 LASSERT((it)->lit_it != NULL); \
425 LASSERT((lo)->ldo_stripenr > 0); \
426 LASSERT((it)->lit_stripe_index < (lo)->ldo_stripenr); \
430 * Implementation of dt_it_ops::fini.
432 * Used with striped objects.
434 * \see dt_it_ops::fini() in the API description for details.
436 static void lod_striped_it_fini(const struct lu_env *env, struct dt_it *di)
438 struct lod_it *it = (struct lod_it *)di;
439 struct lod_object *lo = lod_dt_obj(it->lit_obj);
440 struct dt_object *next;
442 LOD_CHECK_STRIPED_IT(env, it, lo);
444 next = lo->ldo_stripe[it->lit_stripe_index];
445 LASSERT(next != NULL);
446 LASSERT(next->do_index_ops != NULL);
448 next->do_index_ops->dio_it.fini(env, it->lit_it);
450 /* the iterator not in use any more */
453 it->lit_stripe_index = 0;
457 * Implementation of dt_it_ops::get.
459 * Right now it's not used widely, only to reset the iterator to the
460 * initial position. It should be possible to implement a full version
461 * which chooses a correct stripe to be able to position with any key.
463 * \see dt_it_ops::get() in the API description for details.
465 static int lod_striped_it_get(const struct lu_env *env, struct dt_it *di,
466 const struct dt_key *key)
468 const struct lod_it *it = (const struct lod_it *)di;
469 struct lod_object *lo = lod_dt_obj(it->lit_obj);
470 struct dt_object *next;
473 LOD_CHECK_STRIPED_IT(env, it, lo);
475 next = lo->ldo_stripe[it->lit_stripe_index];
476 LASSERT(next != NULL);
477 LASSERT(next->do_index_ops != NULL);
479 return next->do_index_ops->dio_it.get(env, it->lit_it, key);
483 * Implementation of dt_it_ops::put.
485 * Used with striped objects.
487 * \see dt_it_ops::put() in the API description for details.
489 static void lod_striped_it_put(const struct lu_env *env, struct dt_it *di)
491 struct lod_it *it = (struct lod_it *)di;
492 struct lod_object *lo = lod_dt_obj(it->lit_obj);
493 struct dt_object *next;
495 LOD_CHECK_STRIPED_IT(env, it, lo);
497 next = lo->ldo_stripe[it->lit_stripe_index];
498 LASSERT(next != NULL);
499 LASSERT(next->do_index_ops != NULL);
501 return next->do_index_ops->dio_it.put(env, it->lit_it);
505 * Implementation of dt_it_ops::next.
507 * Used with striped objects. When the end of the current stripe is
508 * reached, the method takes the next stripe's iterator.
510 * \see dt_it_ops::next() in the API description for details.
512 static int lod_striped_it_next(const struct lu_env *env, struct dt_it *di)
514 struct lod_it *it = (struct lod_it *)di;
515 struct lod_object *lo = lod_dt_obj(it->lit_obj);
516 struct dt_object *next;
517 struct dt_it *it_next;
521 LOD_CHECK_STRIPED_IT(env, it, lo);
523 next = lo->ldo_stripe[it->lit_stripe_index];
524 LASSERT(next != NULL);
525 LASSERT(next->do_index_ops != NULL);
527 rc = next->do_index_ops->dio_it.next(env, it->lit_it);
531 if (rc == 0 && it->lit_stripe_index == 0)
534 if (rc == 0 && it->lit_stripe_index > 0) {
535 struct lu_dirent *ent;
537 ent = (struct lu_dirent *)lod_env_info(env)->lti_key;
539 rc = next->do_index_ops->dio_it.rec(env, it->lit_it,
540 (struct dt_rec *)ent,
545 /* skip . and .. for slave stripe */
546 if ((strncmp(ent->lde_name, ".",
547 le16_to_cpu(ent->lde_namelen)) == 0 &&
548 le16_to_cpu(ent->lde_namelen) == 1) ||
549 (strncmp(ent->lde_name, "..",
550 le16_to_cpu(ent->lde_namelen)) == 0 &&
551 le16_to_cpu(ent->lde_namelen) == 2))
557 /* go to next stripe */
558 if (it->lit_stripe_index + 1 >= lo->ldo_stripenr)
561 it->lit_stripe_index++;
563 next->do_index_ops->dio_it.put(env, it->lit_it);
564 next->do_index_ops->dio_it.fini(env, it->lit_it);
566 rc = next->do_ops->do_index_try(env, next, &dt_directory_features);
570 next = lo->ldo_stripe[it->lit_stripe_index];
571 LASSERT(next != NULL);
572 LASSERT(next->do_index_ops != NULL);
574 it_next = next->do_index_ops->dio_it.init(env, next, it->lit_attr);
575 if (!IS_ERR(it_next)) {
576 it->lit_it = it_next;
579 rc = PTR_ERR(it_next);
586 * Implementation of dt_it_ops::key.
588 * Used with striped objects.
590 * \see dt_it_ops::key() in the API description for details.
592 static struct dt_key *lod_striped_it_key(const struct lu_env *env,
593 const struct dt_it *di)
595 const struct lod_it *it = (const struct lod_it *)di;
596 struct lod_object *lo = lod_dt_obj(it->lit_obj);
597 struct dt_object *next;
599 LOD_CHECK_STRIPED_IT(env, it, lo);
601 next = lo->ldo_stripe[it->lit_stripe_index];
602 LASSERT(next != NULL);
603 LASSERT(next->do_index_ops != NULL);
605 return next->do_index_ops->dio_it.key(env, it->lit_it);
609 * Implementation of dt_it_ops::key_size.
611 * Used with striped objects.
613 * \see dt_it_ops::size() in the API description for details.
615 static int lod_striped_it_key_size(const struct lu_env *env,
616 const struct dt_it *di)
618 struct lod_it *it = (struct lod_it *)di;
619 struct lod_object *lo = lod_dt_obj(it->lit_obj);
620 struct dt_object *next;
622 LOD_CHECK_STRIPED_IT(env, it, lo);
624 next = lo->ldo_stripe[it->lit_stripe_index];
625 LASSERT(next != NULL);
626 LASSERT(next->do_index_ops != NULL);
628 return next->do_index_ops->dio_it.key_size(env, it->lit_it);
632 * Implementation of dt_it_ops::rec.
634 * Used with striped objects.
636 * \see dt_it_ops::rec() in the API description for details.
638 static int lod_striped_it_rec(const struct lu_env *env, const struct dt_it *di,
639 struct dt_rec *rec, __u32 attr)
641 const struct lod_it *it = (const struct lod_it *)di;
642 struct lod_object *lo = lod_dt_obj(it->lit_obj);
643 struct dt_object *next;
645 LOD_CHECK_STRIPED_IT(env, it, lo);
647 next = lo->ldo_stripe[it->lit_stripe_index];
648 LASSERT(next != NULL);
649 LASSERT(next->do_index_ops != NULL);
651 return next->do_index_ops->dio_it.rec(env, it->lit_it, rec, attr);
655 * Implementation of dt_it_ops::rec_size.
657 * Used with striped objects.
659 * \see dt_it_ops::rec_size() in the API description for details.
661 static int lod_striped_it_rec_size(const struct lu_env *env,
662 const struct dt_it *di, __u32 attr)
664 struct lod_it *it = (struct lod_it *)di;
665 struct lod_object *lo = lod_dt_obj(it->lit_obj);
666 struct dt_object *next;
668 LOD_CHECK_STRIPED_IT(env, it, lo);
670 next = lo->ldo_stripe[it->lit_stripe_index];
671 LASSERT(next != NULL);
672 LASSERT(next->do_index_ops != NULL);
674 return next->do_index_ops->dio_it.rec_size(env, it->lit_it, attr);
678 * Implementation of dt_it_ops::store.
680 * Used with striped objects.
682 * \see dt_it_ops::store() in the API description for details.
684 static __u64 lod_striped_it_store(const struct lu_env *env,
685 const struct dt_it *di)
687 const struct lod_it *it = (const struct lod_it *)di;
688 struct lod_object *lo = lod_dt_obj(it->lit_obj);
689 struct dt_object *next;
691 LOD_CHECK_STRIPED_IT(env, it, lo);
693 next = lo->ldo_stripe[it->lit_stripe_index];
694 LASSERT(next != NULL);
695 LASSERT(next->do_index_ops != NULL);
697 return next->do_index_ops->dio_it.store(env, it->lit_it);
701 * Implementation of dt_it_ops::load.
703 * Used with striped objects.
705 * \see dt_it_ops::load() in the API description for details.
707 static int lod_striped_it_load(const struct lu_env *env,
708 const struct dt_it *di, __u64 hash)
710 const struct lod_it *it = (const struct lod_it *)di;
711 struct lod_object *lo = lod_dt_obj(it->lit_obj);
712 struct dt_object *next;
714 LOD_CHECK_STRIPED_IT(env, it, lo);
716 next = lo->ldo_stripe[it->lit_stripe_index];
717 LASSERT(next != NULL);
718 LASSERT(next->do_index_ops != NULL);
720 return next->do_index_ops->dio_it.load(env, it->lit_it, hash);
723 static struct dt_index_operations lod_striped_index_ops = {
724 .dio_lookup = lod_index_lookup,
725 .dio_declare_insert = lod_declare_index_insert,
726 .dio_insert = lod_index_insert,
727 .dio_declare_delete = lod_declare_index_delete,
728 .dio_delete = lod_index_delete,
730 .init = lod_striped_it_init,
731 .fini = lod_striped_it_fini,
732 .get = lod_striped_it_get,
733 .put = lod_striped_it_put,
734 .next = lod_striped_it_next,
735 .key = lod_striped_it_key,
736 .key_size = lod_striped_it_key_size,
737 .rec = lod_striped_it_rec,
738 .rec_size = lod_striped_it_rec_size,
739 .store = lod_striped_it_store,
740 .load = lod_striped_it_load,
745 * Append the FID for each shard of the striped directory after the
746 * given LMV EA header.
748 * To simplify striped directory and the consistency verification,
749 * we only store the LMV EA header on disk, for both master object
750 * and slave objects. When someone wants to know the whole LMV EA,
751 * such as client readdir(), we can build the entrie LMV EA on the
752 * MDT side (in RAM) via iterating the sub-directory entries that
753 * are contained in the master object of the stripe directory.
755 * For the master object of the striped directroy, the valid name
756 * for each shard is composed of the ${shard_FID}:${shard_idx}.
758 * There may be holes in the LMV EA if some shards' name entries
759 * are corrupted or lost.
761 * \param[in] env pointer to the thread context
762 * \param[in] lo pointer to the master object of the striped directory
763 * \param[in] buf pointer to the lu_buf which will hold the LMV EA
764 * \param[in] resize whether re-allocate the buffer if it is not big enough
766 * \retval positive size of the LMV EA
767 * \retval 0 for nothing to be loaded
768 * \retval negative error number on failure
770 int lod_load_lmv_shards(const struct lu_env *env, struct lod_object *lo,
771 struct lu_buf *buf, bool resize)
773 struct lu_dirent *ent =
774 (struct lu_dirent *)lod_env_info(env)->lti_key;
775 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
776 struct dt_object *obj = dt_object_child(&lo->ldo_obj);
777 struct lmv_mds_md_v1 *lmv1 = buf->lb_buf;
779 const struct dt_it_ops *iops;
781 __u32 magic = le32_to_cpu(lmv1->lmv_magic);
786 /* If it is not a striped directory, then load nothing. */
787 if (magic != LMV_MAGIC_V1)
790 /* If it is in migration (or failure), then load nothing. */
791 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
794 stripes = le32_to_cpu(lmv1->lmv_stripe_count);
798 rc = lmv_mds_md_size(stripes, magic);
802 if (buf->lb_len < lmv1_size) {
811 lu_buf_alloc(buf, lmv1_size);
816 memcpy(buf->lb_buf, tbuf.lb_buf, tbuf.lb_len);
819 if (unlikely(!dt_try_as_dir(env, obj)))
822 memset(&lmv1->lmv_stripe_fids[0], 0, stripes * sizeof(struct lu_fid));
823 iops = &obj->do_index_ops->dio_it;
824 it = iops->init(env, obj, LUDA_64BITHASH);
828 rc = iops->load(env, it, 0);
830 rc = iops->next(env, it);
835 char name[FID_LEN + 2] = "";
840 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
846 fid_le_to_cpu(&fid, &ent->lde_fid);
847 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
848 if (ent->lde_name[0] == '.') {
849 if (ent->lde_namelen == 1)
852 if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
856 len = snprintf(name, FID_LEN + 1, DFID":", PFID(&ent->lde_fid));
857 /* The ent->lde_name is composed of ${FID}:${index} */
858 if (ent->lde_namelen < len + 1 ||
859 memcmp(ent->lde_name, name, len) != 0) {
860 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
861 "%s: invalid shard name %.*s with the FID "DFID
862 " for the striped directory "DFID", %s\n",
863 lod2obd(lod)->obd_name, ent->lde_namelen,
864 ent->lde_name, PFID(&fid),
865 PFID(lu_object_fid(&obj->do_lu)),
866 lod->lod_lmv_failout ? "failout" : "skip");
868 if (lod->lod_lmv_failout)
876 if (ent->lde_name[len] < '0' ||
877 ent->lde_name[len] > '9') {
878 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
879 "%s: invalid shard name %.*s with the "
880 "FID "DFID" for the striped directory "
882 lod2obd(lod)->obd_name, ent->lde_namelen,
883 ent->lde_name, PFID(&fid),
884 PFID(lu_object_fid(&obj->do_lu)),
885 lod->lod_lmv_failout ?
888 if (lod->lod_lmv_failout)
894 index = index * 10 + ent->lde_name[len++] - '0';
895 } while (len < ent->lde_namelen);
897 if (len == ent->lde_namelen) {
898 /* Out of LMV EA range. */
899 if (index >= stripes) {
900 CERROR("%s: the shard %.*s for the striped "
901 "directory "DFID" is out of the known "
902 "LMV EA range [0 - %u], failout\n",
903 lod2obd(lod)->obd_name, ent->lde_namelen,
905 PFID(lu_object_fid(&obj->do_lu)),
911 /* The slot has been occupied. */
912 if (!fid_is_zero(&lmv1->lmv_stripe_fids[index])) {
916 &lmv1->lmv_stripe_fids[index]);
917 CERROR("%s: both the shard "DFID" and "DFID
918 " for the striped directory "DFID
919 " claim the same LMV EA slot at the "
920 "index %d, failout\n",
921 lod2obd(lod)->obd_name,
922 PFID(&fid0), PFID(&fid),
923 PFID(lu_object_fid(&obj->do_lu)), index);
928 /* stored as LE mode */
929 lmv1->lmv_stripe_fids[index] = ent->lde_fid;
932 rc = iops->next(env, it);
939 RETURN(rc > 0 ? lmv_mds_md_size(stripes, magic) : rc);
943 * Implementation of dt_object_operations::do_index_try.
945 * \see dt_object_operations::do_index_try() in the API description for details.
947 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
948 const struct dt_index_features *feat)
950 struct lod_object *lo = lod_dt_obj(dt);
951 struct dt_object *next = dt_object_child(dt);
955 LASSERT(next->do_ops);
956 LASSERT(next->do_ops->do_index_try);
958 rc = lod_load_striping_locked(env, lo);
962 rc = next->do_ops->do_index_try(env, next, feat);
966 if (lo->ldo_stripenr > 0) {
969 for (i = 0; i < lo->ldo_stripenr; i++) {
970 if (dt_object_exists(lo->ldo_stripe[i]) == 0)
972 rc = lo->ldo_stripe[i]->do_ops->do_index_try(env,
973 lo->ldo_stripe[i], feat);
977 dt->do_index_ops = &lod_striped_index_ops;
979 dt->do_index_ops = &lod_index_ops;
986 * Implementation of dt_object_operations::do_read_lock.
988 * \see dt_object_operations::do_read_lock() in the API description for details.
990 static void lod_object_read_lock(const struct lu_env *env,
991 struct dt_object *dt, unsigned role)
993 dt_read_lock(env, dt_object_child(dt), role);
997 * Implementation of dt_object_operations::do_write_lock.
999 * \see dt_object_operations::do_write_lock() in the API description for
1002 static void lod_object_write_lock(const struct lu_env *env,
1003 struct dt_object *dt, unsigned role)
1005 dt_write_lock(env, dt_object_child(dt), role);
1009 * Implementation of dt_object_operations::do_read_unlock.
1011 * \see dt_object_operations::do_read_unlock() in the API description for
1014 static void lod_object_read_unlock(const struct lu_env *env,
1015 struct dt_object *dt)
1017 dt_read_unlock(env, dt_object_child(dt));
1021 * Implementation of dt_object_operations::do_write_unlock.
1023 * \see dt_object_operations::do_write_unlock() in the API description for
1026 static void lod_object_write_unlock(const struct lu_env *env,
1027 struct dt_object *dt)
1029 dt_write_unlock(env, dt_object_child(dt));
1033 * Implementation of dt_object_operations::do_write_locked.
1035 * \see dt_object_operations::do_write_locked() in the API description for
1038 static int lod_object_write_locked(const struct lu_env *env,
1039 struct dt_object *dt)
1041 return dt_write_locked(env, dt_object_child(dt));
1045 * Implementation of dt_object_operations::do_attr_get.
1047 * \see dt_object_operations::do_attr_get() in the API description for details.
1049 static int lod_attr_get(const struct lu_env *env,
1050 struct dt_object *dt,
1051 struct lu_attr *attr)
1053 /* Note: for striped directory, client will merge attributes
1054 * from all of the sub-stripes see lmv_merge_attr(), and there
1055 * no MDD logic depend on directory nlink/size/time, so we can
1056 * always use master inode nlink and size for now. */
1057 return dt_attr_get(env, dt_object_child(dt), attr);
1061 * Mark all of the striped directory sub-stripes dead.
1063 * When a striped object is a subject to removal, we have
1064 * to mark all the stripes to prevent further access to
1065 * them (e.g. create a new file in those). So we mark
1066 * all the stripes with LMV_HASH_FLAG_DEAD. The function
1067 * can be used to declare the changes and to apply them.
1068 * If the object isn't striped, then just return success.
1070 * \param[in] env execution environment
1071 * \param[in] dt the striped object
1072 * \param[in] handle transaction handle
1073 * \param[in] declare whether to declare the change or apply
1075 * \retval 0 on success
1076 * \retval negative if failed
1078 static int lod_mark_dead_object(const struct lu_env *env,
1079 struct dt_object *dt,
1083 struct lod_object *lo = lod_dt_obj(dt);
1084 struct lmv_mds_md_v1 *lmv;
1085 __u32 dead_hash_type;
1091 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
1094 rc = lod_load_striping_locked(env, lo);
1098 if (lo->ldo_stripenr == 0)
1101 rc = lod_get_lmv_ea(env, lo);
1105 lmv = lod_env_info(env)->lti_ea_store;
1106 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1107 dead_hash_type = le32_to_cpu(lmv->lmv_hash_type) | LMV_HASH_FLAG_DEAD;
1108 lmv->lmv_hash_type = cpu_to_le32(dead_hash_type);
1109 for (i = 0; i < lo->ldo_stripenr; i++) {
1112 lmv->lmv_master_mdt_index = i;
1114 buf.lb_len = sizeof(*lmv);
1116 rc = lod_sub_object_declare_xattr_set(env,
1117 lo->ldo_stripe[i], &buf,
1119 LU_XATTR_REPLACE, th);
1121 rc = lod_sub_object_xattr_set(env, lo->ldo_stripe[i],
1122 &buf, XATTR_NAME_LMV,
1123 LU_XATTR_REPLACE, th);
1133 * Implementation of dt_object_operations::do_declare_attr_set.
1135 * If the object is striped, then apply the changes to all the stripes.
1137 * \see dt_object_operations::do_declare_attr_set() in the API description
1140 static int lod_declare_attr_set(const struct lu_env *env,
1141 struct dt_object *dt,
1142 const struct lu_attr *attr,
1145 struct dt_object *next = dt_object_child(dt);
1146 struct lod_object *lo = lod_dt_obj(dt);
1150 /* Set dead object on all other stripes */
1151 if (attr->la_valid & LA_FLAGS && !(attr->la_valid & ~LA_FLAGS) &&
1152 attr->la_flags & LUSTRE_SLAVE_DEAD_FL) {
1153 rc = lod_mark_dead_object(env, dt, th, true);
1158 * declare setattr on the local object
1160 rc = lod_sub_object_declare_attr_set(env, next, attr, th);
1164 /* osp_declare_attr_set() ignores all attributes other than
1165 * UID, GID, and size, and osp_attr_set() ignores all but UID
1166 * and GID. Declaration of size attr setting happens through
1167 * lod_declare_init_size(), and not through this function.
1168 * Therefore we need not load striping unless ownership is
1169 * changing. This should save memory and (we hope) speed up
1171 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1172 if (!(attr->la_valid & (LA_UID | LA_GID)))
1175 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1178 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE |
1179 LA_ATIME | LA_MTIME | LA_CTIME)))
1183 * load striping information, notice we don't do this when object
1184 * is being initialized as we don't need this information till
1185 * few specific cases like destroy, chown
1187 rc = lod_load_striping(env, lo);
1191 if (lo->ldo_stripenr == 0)
1195 * if object is striped declare changes on the stripes
1197 LASSERT(lo->ldo_stripe);
1198 for (i = 0; i < lo->ldo_stripenr; i++) {
1199 if (lo->ldo_stripe[i] == NULL)
1201 rc = lod_sub_object_declare_attr_set(env,
1202 lo->ldo_stripe[i], attr,
1208 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE) &&
1209 dt_object_exists(next) != 0 &&
1210 dt_object_remote(next) == 0)
1211 lod_sub_object_declare_xattr_del(env, next,
1212 XATTR_NAME_LOV, th);
1214 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) &&
1215 dt_object_exists(next) &&
1216 dt_object_remote(next) == 0 && S_ISREG(attr->la_mode)) {
1217 struct lod_thread_info *info = lod_env_info(env);
1218 struct lu_buf *buf = &info->lti_buf;
1220 buf->lb_buf = info->lti_ea_store;
1221 buf->lb_len = info->lti_ea_store_size;
1222 lod_sub_object_declare_xattr_set(env, next, buf,
1224 LU_XATTR_REPLACE, th);
1231 * Implementation of dt_object_operations::do_attr_set.
1233 * If the object is striped, then apply the changes to all or subset of
1234 * the stripes depending on the object type and specific attributes.
1236 * \see dt_object_operations::do_attr_set() in the API description for details.
1238 static int lod_attr_set(const struct lu_env *env,
1239 struct dt_object *dt,
1240 const struct lu_attr *attr,
1243 struct dt_object *next = dt_object_child(dt);
1244 struct lod_object *lo = lod_dt_obj(dt);
1248 /* Set dead object on all other stripes */
1249 if (attr->la_valid & LA_FLAGS && !(attr->la_valid & ~LA_FLAGS) &&
1250 attr->la_flags & LUSTRE_SLAVE_DEAD_FL) {
1251 rc = lod_mark_dead_object(env, dt, th, false);
1256 * apply changes to the local object
1258 rc = lod_sub_object_attr_set(env, next, attr, th);
1262 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1263 if (!(attr->la_valid & (LA_UID | LA_GID)))
1266 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1269 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE |
1270 LA_ATIME | LA_MTIME | LA_CTIME)))
1274 if (lo->ldo_stripenr == 0)
1278 * if object is striped, apply changes to all the stripes
1280 LASSERT(lo->ldo_stripe);
1281 for (i = 0; i < lo->ldo_stripenr; i++) {
1282 if (unlikely(lo->ldo_stripe[i] == NULL))
1285 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
1286 (dt_object_exists(lo->ldo_stripe[i]) == 0))
1289 rc = lod_sub_object_attr_set(env, lo->ldo_stripe[i], attr, th);
1294 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE) &&
1295 dt_object_exists(next) != 0 &&
1296 dt_object_remote(next) == 0)
1297 rc = lod_sub_object_xattr_del(env, next, XATTR_NAME_LOV, th);
1299 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) &&
1300 dt_object_exists(next) &&
1301 dt_object_remote(next) == 0 && S_ISREG(attr->la_mode)) {
1302 struct lod_thread_info *info = lod_env_info(env);
1303 struct lu_buf *buf = &info->lti_buf;
1304 struct ost_id *oi = &info->lti_ostid;
1305 struct lu_fid *fid = &info->lti_fid;
1306 struct lov_mds_md_v1 *lmm;
1307 struct lov_ost_data_v1 *objs;
1311 rc1 = lod_get_lov_ea(env, lo);
1315 buf->lb_buf = info->lti_ea_store;
1316 buf->lb_len = info->lti_ea_store_size;
1317 lmm = info->lti_ea_store;
1318 magic = le32_to_cpu(lmm->lmm_magic);
1319 if (magic == LOV_MAGIC_V1)
1320 objs = &(lmm->lmm_objects[0]);
1322 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1323 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1324 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1326 fid_to_ostid(fid, oi);
1327 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1329 rc = lod_sub_object_xattr_set(env, next, buf, XATTR_NAME_LOV,
1330 LU_XATTR_REPLACE, th);
1337 * Implementation of dt_object_operations::do_xattr_get.
1339 * If LOV EA is requested from the root object and it's not
1340 * found, then return default striping for the filesystem.
1342 * \see dt_object_operations::do_xattr_get() in the API description for details.
1344 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1345 struct lu_buf *buf, const char *name)
1347 struct lod_thread_info *info = lod_env_info(env);
1348 struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1352 rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1353 if (strcmp(name, XATTR_NAME_LMV) == 0) {
1354 struct lmv_mds_md_v1 *lmv1;
1357 if (rc > (typeof(rc))sizeof(*lmv1))
1360 if (rc < (typeof(rc))sizeof(*lmv1))
1361 RETURN(rc = rc > 0 ? -EINVAL : rc);
1363 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1364 CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1366 info->lti_buf.lb_buf = info->lti_key;
1367 info->lti_buf.lb_len = sizeof(*lmv1);
1368 rc = dt_xattr_get(env, dt_object_child(dt),
1369 &info->lti_buf, name);
1370 if (unlikely(rc != sizeof(*lmv1)))
1371 RETURN(rc = rc > 0 ? -EINVAL : rc);
1373 lmv1 = info->lti_buf.lb_buf;
1374 /* The on-disk LMV EA only contains header, but the
1375 * returned LMV EA size should contain the space for
1376 * the FIDs of all shards of the striped directory. */
1377 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1378 rc = lmv_mds_md_size(
1379 le32_to_cpu(lmv1->lmv_stripe_count),
1382 rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1386 RETURN(rc = rc1 != 0 ? rc1 : rc);
1389 if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1393 * lod returns default striping on the real root of the device
1394 * this is like the root stores default striping for the whole
1395 * filesystem. historically we've been using a different approach
1396 * and store it in the config.
1398 dt_root_get(env, dev->lod_child, &info->lti_fid);
1399 is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1401 if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1402 struct lov_user_md *lum = buf->lb_buf;
1403 struct lov_desc *desc = &dev->lod_desc;
1405 if (buf->lb_buf == NULL) {
1407 } else if (buf->lb_len >= sizeof(*lum)) {
1408 lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1409 lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1410 lmm_oi_set_id(&lum->lmm_oi, 0);
1411 lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1412 lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1413 lum->lmm_stripe_size = cpu_to_le32(
1414 desc->ld_default_stripe_size);
1415 lum->lmm_stripe_count = cpu_to_le16(
1416 desc->ld_default_stripe_count);
1417 lum->lmm_stripe_offset = cpu_to_le16(
1418 desc->ld_default_stripe_offset);
1431 * Checks that the magic of the stripe is sane.
1433 * \param[in] lod lod device
1434 * \param[in] lum a buffer storing LMV EA to verify
1436 * \retval 0 if the EA is sane
1437 * \retval negative otherwise
1439 static int lod_verify_md_striping(struct lod_device *lod,
1440 const struct lmv_user_md_v1 *lum)
1442 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1443 CERROR("%s: invalid lmv_user_md: magic = %x, "
1444 "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1445 lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1446 (int)le32_to_cpu(lum->lum_stripe_offset),
1447 le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1455 * Initialize LMV EA for a slave.
1457 * Initialize slave's LMV EA from the master's LMV EA.
1459 * \param[in] master_lmv a buffer containing master's EA
1460 * \param[out] slave_lmv a buffer where slave's EA will be stored
1463 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1464 const struct lmv_mds_md_v1 *master_lmv)
1466 *slave_lmv = *master_lmv;
1467 slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1473 * Generate LMV EA from the object passed as \a dt. The object must have
1474 * the stripes created and initialized.
1476 * \param[in] env execution environment
1477 * \param[in] dt object
1478 * \param[out] lmv_buf buffer storing generated LMV EA
1480 * \retval 0 on success
1481 * \retval negative if failed
1483 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1484 struct lu_buf *lmv_buf)
1486 struct lod_thread_info *info = lod_env_info(env);
1487 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1488 struct lod_object *lo = lod_dt_obj(dt);
1489 struct lmv_mds_md_v1 *lmm1;
1491 int type = LU_SEQ_RANGE_ANY;
1496 LASSERT(lo->ldo_dir_striped != 0);
1497 LASSERT(lo->ldo_stripenr > 0);
1498 stripe_count = lo->ldo_stripenr;
1499 /* Only store the LMV EA heahder on the disk. */
1500 if (info->lti_ea_store_size < sizeof(*lmm1)) {
1501 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1505 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1508 lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1509 lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1510 lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1511 lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1512 rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1517 lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1518 lmv_buf->lb_buf = info->lti_ea_store;
1519 lmv_buf->lb_len = sizeof(*lmm1);
1525 * Create in-core represenation for a striped directory.
1527 * Parse the buffer containing LMV EA and instantiate LU objects
1528 * representing the stripe objects. The pointers to the objects are
1529 * stored in ldo_stripe field of \a lo. This function is used when
1530 * we need to access an already created object (i.e. load from a disk).
1532 * \param[in] env execution environment
1533 * \param[in] lo lod object
1534 * \param[in] buf buffer containing LMV EA
1536 * \retval 0 on success
1537 * \retval negative if failed
1539 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1540 const struct lu_buf *buf)
1542 struct lod_thread_info *info = lod_env_info(env);
1543 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1544 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1545 struct dt_object **stripe;
1546 union lmv_mds_md *lmm = buf->lb_buf;
1547 struct lmv_mds_md_v1 *lmv1 = &lmm->lmv_md_v1;
1548 struct lu_fid *fid = &info->lti_fid;
1553 if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1556 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1557 lo->ldo_dir_slave_stripe = 1;
1561 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1564 if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1567 LASSERT(lo->ldo_stripe == NULL);
1568 OBD_ALLOC(stripe, sizeof(stripe[0]) *
1569 (le32_to_cpu(lmv1->lmv_stripe_count)));
1573 for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1574 struct dt_device *tgt_dt;
1575 struct dt_object *dto;
1576 int type = LU_SEQ_RANGE_ANY;
1579 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1580 if (!fid_is_sane(fid))
1581 GOTO(out, rc = -ESTALE);
1583 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1587 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1588 tgt_dt = lod->lod_child;
1590 struct lod_tgt_desc *tgt;
1592 tgt = LTD_TGT(ltd, idx);
1594 GOTO(out, rc = -ESTALE);
1595 tgt_dt = tgt->ltd_tgt;
1598 dto = dt_locate_at(env, tgt_dt, fid,
1599 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1602 GOTO(out, rc = PTR_ERR(dto));
1607 lo->ldo_stripe = stripe;
1608 lo->ldo_stripenr = le32_to_cpu(lmv1->lmv_stripe_count);
1609 lo->ldo_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1611 lod_object_free_striping(env, lo);
1617 * Create a striped directory.
1619 * Create a striped directory with a given stripe pattern on the specified MDTs.
1620 * A striped directory is represented as a regular directory - an index listing
1621 * all the stripes. The stripes point back to the master object with ".." and
1622 * LinkEA. The master object gets LMV EA which identifies it as a striped
1623 * directory. The function allocates FIDs for all the stripes.
1625 * \param[in] env execution environment
1626 * \param[in] dt object
1627 * \param[in] attr attributes to initialize the objects with
1628 * \param[in] lum a pattern specifying the number of stripes and
1630 * \param[in] dof type of objects to be created
1631 * \param[in] th transaction handle
1633 * \retval 0 on success
1634 * \retval negative if failed
1636 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1637 struct dt_object *dt,
1638 struct lu_attr *attr,
1639 struct dt_object_format *dof,
1642 struct lod_thread_info *info = lod_env_info(env);
1643 struct lu_buf lmv_buf;
1644 struct lu_buf slave_lmv_buf;
1645 struct lmv_mds_md_v1 *lmm;
1646 struct lmv_mds_md_v1 *slave_lmm = NULL;
1647 struct dt_insert_rec *rec = &info->lti_dt_rec;
1648 struct lod_object *lo = lod_dt_obj(dt);
1653 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1656 lmm = lmv_buf.lb_buf;
1658 OBD_ALLOC_PTR(slave_lmm);
1659 if (slave_lmm == NULL)
1660 GOTO(out, rc = -ENOMEM);
1662 lod_prep_slave_lmv_md(slave_lmm, lmm);
1663 slave_lmv_buf.lb_buf = slave_lmm;
1664 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1666 if (!dt_try_as_dir(env, dt_object_child(dt)))
1667 GOTO(out, rc = -EINVAL);
1669 rec->rec_type = S_IFDIR;
1670 for (i = 0; i < lo->ldo_stripenr; i++) {
1671 struct dt_object *dto = lo->ldo_stripe[i];
1672 char *stripe_name = info->lti_key;
1673 struct lu_name *sname;
1674 struct linkea_data ldata = { NULL };
1675 struct lu_buf linkea_buf;
1677 rc = lod_sub_object_declare_create(env, dto, attr, NULL,
1682 if (!dt_try_as_dir(env, dto))
1683 GOTO(out, rc = -EINVAL);
1685 rc = lod_sub_object_declare_ref_add(env, dto, th);
1689 rec->rec_fid = lu_object_fid(&dto->do_lu);
1690 rc = lod_sub_object_declare_insert(env, dto,
1691 (const struct dt_rec *)rec,
1692 (const struct dt_key *)dot, th);
1696 /* master stripe FID will be put to .. */
1697 rec->rec_fid = lu_object_fid(&dt->do_lu);
1698 rc = lod_sub_object_declare_insert(env, dto,
1699 (const struct dt_rec *)rec,
1700 (const struct dt_key *)dotdot,
1705 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1706 cfs_fail_val != i) {
1707 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1709 slave_lmm->lmv_master_mdt_index =
1712 slave_lmm->lmv_master_mdt_index =
1714 rc = lod_sub_object_declare_xattr_set(env, dto,
1715 &slave_lmv_buf, XATTR_NAME_LMV, 0, th);
1720 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1722 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1723 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1725 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1726 PFID(lu_object_fid(&dto->do_lu)), i);
1728 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1729 rc = linkea_data_new(&ldata, &info->lti_linkea_buf);
1733 rc = linkea_add_buf(&ldata, sname, lu_object_fid(&dt->do_lu));
1737 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1738 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1739 rc = lod_sub_object_declare_xattr_set(env, dto, &linkea_buf,
1740 XATTR_NAME_LINK, 0, th);
1744 rec->rec_fid = lu_object_fid(&dto->do_lu);
1745 rc = lod_sub_object_declare_insert(env, dt_object_child(dt),
1746 (const struct dt_rec *)rec,
1747 (const struct dt_key *)stripe_name,
1752 rc = lod_sub_object_declare_ref_add(env, dt_object_child(dt),
1758 rc = lod_sub_object_declare_xattr_set(env, dt_object_child(dt),
1759 &lmv_buf, XATTR_NAME_LMV, 0, th);
1763 if (slave_lmm != NULL)
1764 OBD_FREE_PTR(slave_lmm);
1769 static int lod_prep_md_striped_create(const struct lu_env *env,
1770 struct dt_object *dt,
1771 struct lu_attr *attr,
1772 const struct lmv_user_md_v1 *lum,
1773 struct dt_object_format *dof,
1776 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1777 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1778 struct lod_object *lo = lod_dt_obj(dt);
1779 struct dt_object **stripe;
1787 /* The lum has been verifed in lod_verify_md_striping */
1788 LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
1789 LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1791 stripe_count = le32_to_cpu(lum->lum_stripe_count);
1793 /* shrink the stripe_count to the avaible MDT count */
1794 if (stripe_count > lod->lod_remote_mdt_count + 1)
1795 stripe_count = lod->lod_remote_mdt_count + 1;
1797 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1801 OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1802 if (idx_array == NULL)
1803 GOTO(out_free, rc = -ENOMEM);
1805 for (i = 0; i < stripe_count; i++) {
1806 struct lod_tgt_desc *tgt = NULL;
1807 struct dt_object *dto;
1808 struct lu_fid fid = { 0 };
1810 struct lu_object_conf conf = { 0 };
1811 struct dt_device *tgt_dt = NULL;
1814 /* Right now, master stripe and master object are
1815 * on the same MDT */
1816 idx = le32_to_cpu(lum->lum_stripe_offset);
1817 rc = obd_fid_alloc(env, lod->lod_child_exp, &fid,
1821 tgt_dt = lod->lod_child;
1825 idx = (idx_array[i - 1] + 1) % (lod->lod_remote_mdt_count + 1);
1827 for (j = 0; j < lod->lod_remote_mdt_count;
1828 j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1829 bool already_allocated = false;
1832 CDEBUG(D_INFO, "try idx %d, mdt cnt %u,"
1833 " allocated %u, last allocated %d\n", idx,
1834 lod->lod_remote_mdt_count, i, idx_array[i - 1]);
1836 /* Find next available target */
1837 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx))
1840 /* check whether the idx already exists
1841 * in current allocated array */
1842 for (k = 0; k < i; k++) {
1843 if (idx_array[k] == idx) {
1844 already_allocated = true;
1849 if (already_allocated)
1852 /* check the status of the OSP */
1853 tgt = LTD_TGT(ltd, idx);
1857 tgt_dt = tgt->ltd_tgt;
1858 rc = dt_statfs(env, tgt_dt, NULL);
1860 /* this OSP doesn't feel well */
1865 rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1874 /* Can not allocate more stripes */
1875 if (j == lod->lod_remote_mdt_count) {
1876 CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1877 lod2obd(lod)->obd_name, stripe_count, i - 1);
1881 CDEBUG(D_INFO, "idx %d, mdt cnt %u,"
1882 " allocated %u, last allocated %d\n", idx,
1883 lod->lod_remote_mdt_count, i, idx_array[i - 1]);
1886 /* tgt_dt and fid must be ready after search avaible OSP
1887 * in the above loop */
1888 LASSERT(tgt_dt != NULL);
1889 LASSERT(fid_is_sane(&fid));
1890 conf.loc_flags = LOC_F_NEW;
1891 dto = dt_locate_at(env, tgt_dt, &fid,
1892 dt->do_lu.lo_dev->ld_site->ls_top_dev,
1895 GOTO(out_put, rc = PTR_ERR(dto));
1900 lo->ldo_dir_striped = 1;
1901 lo->ldo_stripe = stripe;
1902 lo->ldo_stripenr = i;
1903 lo->ldo_stripes_allocated = stripe_count;
1905 if (lo->ldo_stripenr == 0)
1906 GOTO(out_put, rc = -ENOSPC);
1908 rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
1914 for (i = 0; i < stripe_count; i++)
1915 if (stripe[i] != NULL)
1916 lu_object_put(env, &stripe[i]->do_lu);
1917 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
1918 lo->ldo_stripenr = 0;
1919 lo->ldo_stripes_allocated = 0;
1920 lo->ldo_stripe = NULL;
1924 if (idx_array != NULL)
1925 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
1931 * Declare create striped md object.
1933 * The function declares intention to create a striped directory. This is a
1934 * wrapper for lod_prep_md_striped_create(). The only additional functionality
1935 * is to verify pattern \a lum_buf is good. Check that function for the details.
1937 * \param[in] env execution environment
1938 * \param[in] dt object
1939 * \param[in] attr attributes to initialize the objects with
1940 * \param[in] lum_buf a pattern specifying the number of stripes and
1942 * \param[in] dof type of objects to be created
1943 * \param[in] th transaction handle
1945 * \retval 0 on success
1946 * \retval negative if failed
1949 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
1950 struct dt_object *dt,
1951 struct lu_attr *attr,
1952 const struct lu_buf *lum_buf,
1953 struct dt_object_format *dof,
1956 struct lod_object *lo = lod_dt_obj(dt);
1957 struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
1958 struct lmv_user_md_v1 *lum;
1962 lum = lum_buf->lb_buf;
1963 LASSERT(lum != NULL);
1965 CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
1966 le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
1967 (int)le32_to_cpu(lum->lum_stripe_offset));
1969 if (le32_to_cpu(lum->lum_stripe_count) == 0)
1972 rc = lod_verify_md_striping(lod, lum);
1976 /* prepare dir striped objects */
1977 rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
1979 /* failed to create striping, let's reset
1980 * config so that others don't get confused */
1981 lod_object_free_striping(env, lo);
1990 * Implementation of dt_object_operations::do_declare_xattr_set.
1992 * Used with regular (non-striped) objects. Basically it
1993 * initializes the striping information and applies the
1994 * change to all the stripes.
1996 * \see dt_object_operations::do_declare_xattr_set() in the API description
1999 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2000 struct dt_object *dt,
2001 const struct lu_buf *buf,
2002 const char *name, int fl,
2005 struct dt_object *next = dt_object_child(dt);
2006 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2007 struct lod_object *lo = lod_dt_obj(dt);
2012 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2013 struct lmv_user_md_v1 *lum;
2015 LASSERT(buf != NULL && buf->lb_buf != NULL);
2017 rc = lod_verify_md_striping(d, lum);
2022 rc = lod_sub_object_declare_xattr_set(env, next, buf, name, fl, th);
2026 /* set xattr to each stripes, if needed */
2027 rc = lod_load_striping(env, lo);
2031 /* Note: Do not set LinkEA on sub-stripes, otherwise
2032 * it will confuse the fid2path process(see mdt_path_current()).
2033 * The linkEA between master and sub-stripes is set in
2034 * lod_xattr_set_lmv(). */
2035 if (lo->ldo_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2038 for (i = 0; i < lo->ldo_stripenr; i++) {
2039 LASSERT(lo->ldo_stripe[i]);
2041 rc = lod_sub_object_declare_xattr_set(env, lo->ldo_stripe[i],
2051 * Implementation of dt_object_operations::do_declare_xattr_set.
2053 * \see dt_object_operations::do_declare_xattr_set() in the API description
2056 * the extension to the API:
2057 * - declaring LOVEA requests striping creation
2058 * - LU_XATTR_REPLACE means layout swap
2060 static int lod_declare_xattr_set(const struct lu_env *env,
2061 struct dt_object *dt,
2062 const struct lu_buf *buf,
2063 const char *name, int fl,
2066 struct dt_object *next = dt_object_child(dt);
2067 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2073 * allow to declare predefined striping on a new (!mode) object
2074 * which is supposed to be replay of regular file creation
2075 * (when LOV setting is declared)
2076 * LU_XATTR_REPLACE is set to indicate a layout swap
2078 mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2079 if ((S_ISREG(mode) || mode == 0) && strcmp(name, XATTR_NAME_LOV) == 0 &&
2080 !(fl & LU_XATTR_REPLACE)) {
2082 * this is a request to manipulate object's striping
2084 if (dt_object_exists(dt)) {
2085 rc = dt_attr_get(env, next, attr);
2089 memset(attr, 0, sizeof(*attr));
2090 attr->la_valid = LA_TYPE | LA_MODE;
2091 attr->la_mode = S_IFREG;
2093 rc = lod_declare_striped_object(env, dt, attr, buf, th);
2094 } else if (S_ISDIR(mode)) {
2095 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2097 rc = lod_sub_object_declare_xattr_set(env, next, buf, name,
2105 * Resets cached default striping in the object.
2107 * \param[in] lo object
2109 static void lod_lov_stripe_cache_clear(struct lod_object *lo)
2111 lo->ldo_def_striping_set = 0;
2112 lo->ldo_def_striping_cached = 0;
2113 lod_object_set_pool(lo, NULL);
2114 lo->ldo_def_stripe_size = 0;
2115 lo->ldo_def_stripenr = 0;
2116 if (lo->ldo_dir_stripe != NULL)
2117 lo->ldo_dir_def_striping_cached = 0;
2121 * Apply xattr changes to the object.
2123 * Applies xattr changes to the object and the stripes if the latter exist.
2125 * \param[in] env execution environment
2126 * \param[in] dt object
2127 * \param[in] buf buffer pointing to the new value of xattr
2128 * \param[in] name name of xattr
2129 * \param[in] fl flags
2130 * \param[in] th transaction handle
2132 * \retval 0 on success
2133 * \retval negative if failed
2135 static int lod_xattr_set_internal(const struct lu_env *env,
2136 struct dt_object *dt,
2137 const struct lu_buf *buf,
2138 const char *name, int fl, struct thandle *th)
2140 struct dt_object *next = dt_object_child(dt);
2141 struct lod_object *lo = lod_dt_obj(dt);
2146 rc = lod_sub_object_xattr_set(env, next, buf, name, fl, th);
2147 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2150 /* Note: Do not set LinkEA on sub-stripes, otherwise
2151 * it will confuse the fid2path process(see mdt_path_current()).
2152 * The linkEA between master and sub-stripes is set in
2153 * lod_xattr_set_lmv(). */
2154 if (lo->ldo_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2157 for (i = 0; i < lo->ldo_stripenr; i++) {
2158 LASSERT(lo->ldo_stripe[i]);
2160 rc = lod_sub_object_xattr_set(env, lo->ldo_stripe[i], buf, name,
2170 * Delete an extended attribute.
2172 * Deletes specified xattr from the object and the stripes if the latter exist.
2174 * \param[in] env execution environment
2175 * \param[in] dt object
2176 * \param[in] name name of xattr
2177 * \param[in] th transaction handle
2179 * \retval 0 on success
2180 * \retval negative if failed
2182 static int lod_xattr_del_internal(const struct lu_env *env,
2183 struct dt_object *dt,
2184 const char *name, struct thandle *th)
2186 struct dt_object *next = dt_object_child(dt);
2187 struct lod_object *lo = lod_dt_obj(dt);
2192 rc = lod_sub_object_xattr_del(env, next, name, th);
2193 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2196 if (lo->ldo_stripenr == 0)
2199 for (i = 0; i < lo->ldo_stripenr; i++) {
2200 LASSERT(lo->ldo_stripe[i]);
2202 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name,
2212 * Set default striping on a directory.
2214 * Sets specified striping on a directory object unless it matches the default
2215 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2216 * EA. This striping will be used when regular file is being created in this
2219 * \param[in] env execution environment
2220 * \param[in] dt the striped object
2221 * \param[in] buf buffer with the striping
2222 * \param[in] name name of EA
2223 * \param[in] fl xattr flag (see OSD API description)
2224 * \param[in] th transaction handle
2226 * \retval 0 on success
2227 * \retval negative if failed
2229 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
2230 struct dt_object *dt,
2231 const struct lu_buf *buf,
2232 const char *name, int fl,
2235 struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2236 struct lod_object *l = lod_dt_obj(dt);
2237 struct lov_user_md_v1 *lum;
2238 struct lov_user_md_v3 *v3 = NULL;
2239 const char *pool_name = NULL;
2243 /* If it is striped dir, we should clear the stripe cache for
2244 * slave stripe as well, but there are no effective way to
2245 * notify the LOD on the slave MDT, so we do not cache stripe
2246 * information for slave stripe for now. XXX*/
2247 lod_lov_stripe_cache_clear(l);
2248 LASSERT(buf != NULL && buf->lb_buf != NULL);
2251 rc = lod_verify_striping(d, buf, false);
2255 if (lum->lmm_magic == LOV_USER_MAGIC_V3) {
2257 if (v3->lmm_pool_name[0] != '\0')
2258 pool_name = v3->lmm_pool_name;
2261 /* if { size, offset, count } = { 0, -1, 0 } and no pool
2262 * (i.e. all default values specified) then delete default
2263 * striping from dir. */
2265 "set default striping: sz %u # %u offset %d %s %s\n",
2266 (unsigned)lum->lmm_stripe_size,
2267 (unsigned)lum->lmm_stripe_count,
2268 (int)lum->lmm_stripe_offset,
2269 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
2271 if (LOVEA_DELETE_VALUES(lum->lmm_stripe_size, lum->lmm_stripe_count,
2272 lum->lmm_stripe_offset, pool_name)) {
2273 rc = lod_xattr_del_internal(env, dt, name, th);
2277 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2284 * Set default striping on a directory object.
2286 * Sets specified striping on a directory object unless it matches the default
2287 * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2288 * EA. This striping will be used when a new directory is being created in the
2291 * \param[in] env execution environment
2292 * \param[in] dt the striped object
2293 * \param[in] buf buffer with the striping
2294 * \param[in] name name of EA
2295 * \param[in] fl xattr flag (see OSD API description)
2296 * \param[in] th transaction handle
2298 * \retval 0 on success
2299 * \retval negative if failed
2301 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
2302 struct dt_object *dt,
2303 const struct lu_buf *buf,
2304 const char *name, int fl,
2307 struct lod_object *l = lod_dt_obj(dt);
2308 struct lmv_user_md_v1 *lum;
2312 LASSERT(buf != NULL && buf->lb_buf != NULL);
2315 CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
2316 le32_to_cpu(lum->lum_stripe_count),
2317 (int)le32_to_cpu(lum->lum_stripe_offset));
2319 if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
2320 le32_to_cpu(lum->lum_stripe_offset)) &&
2321 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
2322 rc = lod_xattr_del_internal(env, dt, name, th);
2326 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2331 /* Update default stripe cache */
2332 if (l->ldo_dir_stripe == NULL) {
2333 OBD_ALLOC_PTR(l->ldo_dir_stripe);
2334 if (l->ldo_dir_stripe == NULL)
2338 l->ldo_dir_def_striping_cached = 0;
2343 * Turn directory into a striped directory.
2345 * During replay the client sends the striping created before MDT
2346 * failure, then the layer above LOD sends this defined striping
2347 * using ->do_xattr_set(), so LOD uses this method to replay creation
2348 * of the stripes. Notice the original information for the striping
2349 * (#stripes, FIDs, etc) was transfered in declare path.
2351 * \param[in] env execution environment
2352 * \param[in] dt the striped object
2353 * \param[in] buf not used currently
2354 * \param[in] name not used currently
2355 * \param[in] fl xattr flag (see OSD API description)
2356 * \param[in] th transaction handle
2358 * \retval 0 on success
2359 * \retval negative if failed
2361 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
2362 const struct lu_buf *buf, const char *name,
2363 int fl, struct thandle *th)
2365 struct lod_object *lo = lod_dt_obj(dt);
2366 struct lod_thread_info *info = lod_env_info(env);
2367 struct lu_attr *attr = &info->lti_attr;
2368 struct dt_object_format *dof = &info->lti_format;
2369 struct lu_buf lmv_buf;
2370 struct lu_buf slave_lmv_buf;
2371 struct lmv_mds_md_v1 *lmm;
2372 struct lmv_mds_md_v1 *slave_lmm = NULL;
2373 struct dt_insert_rec *rec = &info->lti_dt_rec;
2378 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2381 /* The stripes are supposed to be allocated in declare phase,
2382 * if there are no stripes being allocated, it will skip */
2383 if (lo->ldo_stripenr == 0)
2386 rc = dt_attr_get(env, dt_object_child(dt), attr);
2390 attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
2391 LA_MODE | LA_UID | LA_GID | LA_TYPE;
2392 dof->dof_type = DFT_DIR;
2394 rc = lod_prep_lmv_md(env, dt, &lmv_buf);
2397 lmm = lmv_buf.lb_buf;
2399 OBD_ALLOC_PTR(slave_lmm);
2400 if (slave_lmm == NULL)
2403 lod_prep_slave_lmv_md(slave_lmm, lmm);
2404 slave_lmv_buf.lb_buf = slave_lmm;
2405 slave_lmv_buf.lb_len = sizeof(*slave_lmm);
2407 rec->rec_type = S_IFDIR;
2408 for (i = 0; i < lo->ldo_stripenr; i++) {
2409 struct dt_object *dto;
2410 char *stripe_name = info->lti_key;
2411 struct lu_name *sname;
2412 struct linkea_data ldata = { NULL };
2413 struct lu_buf linkea_buf;
2415 dto = lo->ldo_stripe[i];
2417 dt_write_lock(env, dto, MOR_TGT_CHILD);
2418 rc = lod_sub_object_create(env, dto, attr, NULL, dof,
2421 dt_write_unlock(env, dto);
2425 rc = lod_sub_object_ref_add(env, dto, th);
2426 dt_write_unlock(env, dto);
2430 rec->rec_fid = lu_object_fid(&dto->do_lu);
2431 rc = lod_sub_object_index_insert(env, dto,
2432 (const struct dt_rec *)rec,
2433 (const struct dt_key *)dot, th, 0);
2437 rec->rec_fid = lu_object_fid(&dt->do_lu);
2438 rc = lod_sub_object_index_insert(env, dto, (struct dt_rec *)rec,
2439 (const struct dt_key *)dotdot, th, 0);
2443 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
2444 cfs_fail_val != i) {
2445 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
2447 slave_lmm->lmv_master_mdt_index =
2450 slave_lmm->lmv_master_mdt_index =
2453 rc = lod_sub_object_xattr_set(env, dto, &slave_lmv_buf,
2454 XATTR_NAME_LMV, fl, th);
2459 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
2461 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2462 PFID(lu_object_fid(&dto->do_lu)), i + 1);
2464 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2465 PFID(lu_object_fid(&dto->do_lu)), i);
2467 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2468 rc = linkea_data_new(&ldata, &info->lti_linkea_buf);
2472 rc = linkea_add_buf(&ldata, sname, lu_object_fid(&dt->do_lu));
2476 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
2477 linkea_buf.lb_len = ldata.ld_leh->leh_len;
2478 rc = lod_sub_object_xattr_set(env, dto, &linkea_buf,
2479 XATTR_NAME_LINK, 0, th);
2483 rec->rec_fid = lu_object_fid(&dto->do_lu);
2484 rc = lod_sub_object_index_insert(env, dt_object_child(dt),
2485 (const struct dt_rec *)rec,
2486 (const struct dt_key *)stripe_name, th, 0);
2490 rc = lod_sub_object_ref_add(env, dt_object_child(dt), th);
2495 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
2496 rc = lod_sub_object_xattr_set(env, dt_object_child(dt),
2497 &lmv_buf, XATTR_NAME_LMV, fl, th);
2499 if (slave_lmm != NULL)
2500 OBD_FREE_PTR(slave_lmm);
2506 * Helper function to declare/execute creation of a striped directory
2508 * Called in declare/create object path, prepare striping for a directory
2509 * and prepare defaults data striping for the objects to be created in
2510 * that directory. Notice the function calls "declaration" or "execution"
2511 * methods depending on \a declare param. This is a consequence of the
2512 * current approach while we don't have natural distributed transactions:
2513 * we basically execute non-local updates in the declare phase. So, the
2514 * arguments for the both phases are the same and this is the reason for
2515 * this function to exist.
2517 * \param[in] env execution environment
2518 * \param[in] dt object
2519 * \param[in] attr attributes the stripes will be created with
2520 * \param[in] dof format of stripes (see OSD API description)
2521 * \param[in] th transaction handle
2522 * \param[in] declare where to call "declare" or "execute" methods
2524 * \retval 0 on success
2525 * \retval negative if failed
2527 static int lod_dir_striping_create_internal(const struct lu_env *env,
2528 struct dt_object *dt,
2529 struct lu_attr *attr,
2530 struct dt_object_format *dof,
2534 struct lod_thread_info *info = lod_env_info(env);
2535 struct lod_object *lo = lod_dt_obj(dt);
2539 if (!LMVEA_DELETE_VALUES(lo->ldo_stripenr,
2540 lo->ldo_dir_stripe_offset)) {
2541 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
2542 int stripe_count = lo->ldo_stripenr;
2544 if (info->lti_ea_store_size < sizeof(*v1)) {
2545 rc = lod_ea_store_resize(info, sizeof(*v1));
2548 v1 = info->lti_ea_store;
2551 memset(v1, 0, sizeof(*v1));
2552 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
2553 v1->lum_stripe_count = cpu_to_le32(stripe_count);
2554 v1->lum_stripe_offset =
2555 cpu_to_le32(lo->ldo_dir_stripe_offset);
2557 info->lti_buf.lb_buf = v1;
2558 info->lti_buf.lb_len = sizeof(*v1);
2561 rc = lod_declare_xattr_set_lmv(env, dt, attr,
2562 &info->lti_buf, dof, th);
2564 rc = lod_xattr_set_lmv(env, dt, &info->lti_buf,
2565 XATTR_NAME_LMV, 0, th);
2570 /* Transfer default LMV striping from the parent */
2571 if (lo->ldo_dir_def_striping_set &&
2572 !LMVEA_DELETE_VALUES(lo->ldo_dir_def_stripenr,
2573 lo->ldo_dir_def_stripe_offset)) {
2574 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
2575 int def_stripe_count = lo->ldo_dir_def_stripenr;
2577 if (info->lti_ea_store_size < sizeof(*v1)) {
2578 rc = lod_ea_store_resize(info, sizeof(*v1));
2581 v1 = info->lti_ea_store;
2584 memset(v1, 0, sizeof(*v1));
2585 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
2586 v1->lum_stripe_count = cpu_to_le32(def_stripe_count);
2587 v1->lum_stripe_offset =
2588 cpu_to_le32(lo->ldo_dir_def_stripe_offset);
2590 cpu_to_le32(lo->ldo_dir_def_hash_type);
2592 info->lti_buf.lb_buf = v1;
2593 info->lti_buf.lb_len = sizeof(*v1);
2595 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
2596 XATTR_NAME_DEFAULT_LMV,
2599 rc = lod_xattr_set_default_lmv_on_dir(env, dt,
2601 XATTR_NAME_DEFAULT_LMV, 0,
2607 /* Transfer default LOV striping from the parent */
2608 if (lo->ldo_def_striping_set &&
2609 !LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size,
2610 lo->ldo_def_stripenr,
2611 lo->ldo_def_stripe_offset,
2613 struct lov_user_md_v3 *v3 = info->lti_ea_store;
2615 if (info->lti_ea_store_size < sizeof(*v3)) {
2616 rc = lod_ea_store_resize(info, sizeof(*v3));
2619 v3 = info->lti_ea_store;
2622 memset(v3, 0, sizeof(*v3));
2623 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
2624 v3->lmm_stripe_count = cpu_to_le16(lo->ldo_def_stripenr);
2625 v3->lmm_stripe_offset = cpu_to_le16(lo->ldo_def_stripe_offset);
2626 v3->lmm_stripe_size = cpu_to_le32(lo->ldo_def_stripe_size);
2627 if (lo->ldo_pool != NULL)
2628 strlcpy(v3->lmm_pool_name, lo->ldo_pool,
2629 sizeof(v3->lmm_pool_name));
2631 info->lti_buf.lb_buf = v3;
2632 info->lti_buf.lb_len = sizeof(*v3);
2635 rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
2636 XATTR_NAME_LOV, 0, th);
2638 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
2639 XATTR_NAME_LOV, 0, th);
2647 static int lod_declare_dir_striping_create(const struct lu_env *env,
2648 struct dt_object *dt,
2649 struct lu_attr *attr,
2650 struct dt_object_format *dof,
2653 return lod_dir_striping_create_internal(env, dt, attr, dof, th, true);
2656 static int lod_dir_striping_create(const struct lu_env *env,
2657 struct dt_object *dt,
2658 struct lu_attr *attr,
2659 struct dt_object_format *dof,
2662 struct lod_object *lo = lod_dt_obj(dt);
2665 rc = lod_dir_striping_create_internal(env, dt, attr, dof, th, false);
2667 lo->ldo_striping_cached = 1;
2673 * Implementation of dt_object_operations::do_xattr_set.
2675 * Sets specified extended attribute on the object. Three types of EAs are
2677 * LOV EA - stores striping for a regular file or default striping (when set
2679 * LMV EA - stores a marker for the striped directories
2680 * DMV EA - stores default directory striping
2682 * When striping is applied to a non-striped existing object (this is called
2683 * late striping), then LOD notices the caller wants to turn the object into a
2684 * striped one. The stripe objects are created and appropriate EA is set:
2685 * LOV EA storing all the stripes directly or LMV EA storing just a small header
2686 * with striping configuration.
2688 * \see dt_object_operations::do_xattr_set() in the API description for details.
2690 static int lod_xattr_set(const struct lu_env *env,
2691 struct dt_object *dt, const struct lu_buf *buf,
2692 const char *name, int fl, struct thandle *th)
2694 struct dt_object *next = dt_object_child(dt);
2698 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2699 strcmp(name, XATTR_NAME_LMV) == 0) {
2700 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
2702 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
2703 LMV_HASH_FLAG_MIGRATION)
2704 rc = lod_sub_object_xattr_set(env, next, buf, name, fl,
2707 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
2712 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2713 strcmp(name, XATTR_NAME_LOV) == 0) {
2715 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
2717 } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2718 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2720 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
2723 } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
2724 !strcmp(name, XATTR_NAME_LOV)) {
2725 /* in case of lov EA swap, just set it
2726 * if not, it is a replay so check striping match what we
2727 * already have during req replay, declare_xattr_set()
2728 * defines striping, then create() does the work */
2729 if (fl & LU_XATTR_REPLACE) {
2730 /* free stripes, then update disk */
2731 lod_object_free_striping(env, lod_dt_obj(dt));
2733 rc = lod_sub_object_xattr_set(env, next, buf, name,
2736 rc = lod_striping_create(env, dt, NULL, NULL, th);
2741 /* then all other xattr */
2742 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2748 * Implementation of dt_object_operations::do_declare_xattr_del.
2750 * \see dt_object_operations::do_declare_xattr_del() in the API description
2753 static int lod_declare_xattr_del(const struct lu_env *env,
2754 struct dt_object *dt, const char *name,
2757 struct lod_object *lo = lod_dt_obj(dt);
2762 rc = lod_sub_object_declare_xattr_del(env, dt_object_child(dt),
2767 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2770 /* set xattr to each stripes, if needed */
2771 rc = lod_load_striping(env, lo);
2775 if (lo->ldo_stripenr == 0)
2778 for (i = 0; i < lo->ldo_stripenr; i++) {
2779 LASSERT(lo->ldo_stripe[i]);
2780 rc = lod_sub_object_declare_xattr_del(env, lo->ldo_stripe[i],
2790 * Implementation of dt_object_operations::do_xattr_del.
2792 * If EA storing a regular striping is being deleted, then release
2793 * all the references to the stripe objects in core.
2795 * \see dt_object_operations::do_xattr_del() in the API description for details.
2797 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
2798 const char *name, struct thandle *th)
2800 struct dt_object *next = dt_object_child(dt);
2801 struct lod_object *lo = lod_dt_obj(dt);
2806 if (!strcmp(name, XATTR_NAME_LOV))
2807 lod_object_free_striping(env, lod_dt_obj(dt));
2809 rc = lod_sub_object_xattr_del(env, next, name, th);
2810 if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2813 if (lo->ldo_stripenr == 0)
2816 for (i = 0; i < lo->ldo_stripenr; i++) {
2817 LASSERT(lo->ldo_stripe[i]);
2819 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name, th);
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)
2836 return dt_xattr_list(env, dt_object_child(dt), buf);
2840 * Initialize a pool the object belongs to.
2842 * When a striped object is being created, striping configuration
2843 * may demand the stripes are allocated on a limited set of the
2844 * targets. These limited sets are known as "pools". So we copy
2845 * a pool name into the object and later actual creation methods
2846 * (like lod_object_create()) will use this information to allocate
2847 * the stripes properly.
2849 * \param[in] o object
2850 * \param[in] pool pool name
2852 int lod_object_set_pool(struct lod_object *o, char *pool)
2857 len = strlen(o->ldo_pool);
2858 OBD_FREE(o->ldo_pool, len + 1);
2863 OBD_ALLOC(o->ldo_pool, len + 1);
2864 if (o->ldo_pool == NULL)
2866 strcpy(o->ldo_pool, pool);
2871 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
2873 return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
2878 * Cache default regular striping in the object.
2880 * To improve performance of striped regular object creation we cache
2881 * default LOV striping (if it exists) in the parent directory object.
2883 * \param[in] env execution environment
2884 * \param[in] lp object
2886 * \retval 0 on success
2887 * \retval negative if failed
2889 static int lod_cache_parent_lov_striping(const struct lu_env *env,
2890 struct lod_object *lp)
2892 struct lod_thread_info *info = lod_env_info(env);
2893 struct lov_user_md_v1 *v1 = NULL;
2894 struct lov_user_md_v3 *v3 = NULL;
2898 /* called from MDD without parent being write locked,
2900 dt_write_lock(env, dt_object_child(&lp->ldo_obj), 0);
2901 rc = lod_get_lov_ea(env, lp);
2905 if (rc < (typeof(rc))sizeof(struct lov_user_md)) {
2906 /* don't lookup for non-existing or invalid striping */
2907 lp->ldo_def_striping_set = 0;
2908 lp->ldo_def_striping_cached = 1;
2909 lp->ldo_def_stripe_size = 0;
2910 lp->ldo_def_stripenr = 0;
2911 lp->ldo_def_stripe_offset = (typeof(v1->lmm_stripe_offset))(-1);
2912 GOTO(unlock, rc = 0);
2916 v1 = info->lti_ea_store;
2917 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
2918 lustre_swab_lov_user_md_v1(v1);
2919 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
2920 v3 = (struct lov_user_md_v3 *)v1;
2921 lustre_swab_lov_user_md_v3(v3);
2924 if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1)
2925 GOTO(unlock, rc = 0);
2927 if (v1->lmm_pattern != LOV_PATTERN_RAID0 && v1->lmm_pattern != 0)
2928 GOTO(unlock, rc = 0);
2930 CDEBUG(D_INFO, DFID" stripe_count=%d stripe_size=%d stripe_offset=%d\n",
2931 PFID(lu_object_fid(&lp->ldo_obj.do_lu)),
2932 (int)v1->lmm_stripe_count,
2933 (int)v1->lmm_stripe_size, (int)v1->lmm_stripe_offset);
2935 lp->ldo_def_stripenr = v1->lmm_stripe_count;
2936 lp->ldo_def_stripe_size = v1->lmm_stripe_size;
2937 lp->ldo_def_stripe_offset = v1->lmm_stripe_offset;
2938 lp->ldo_def_striping_cached = 1;
2939 lp->ldo_def_striping_set = 1;
2940 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2941 /* XXX: sanity check here */
2942 v3 = (struct lov_user_md_v3 *) v1;
2943 if (v3->lmm_pool_name[0])
2944 lod_object_set_pool(lp, v3->lmm_pool_name);
2948 dt_write_unlock(env, dt_object_child(&lp->ldo_obj));
2954 * Cache default directory striping in the object.
2956 * To improve performance of striped directory creation we cache default
2957 * directory striping (if it exists) in the parent directory object.
2959 * \param[in] env execution environment
2960 * \param[in] lp object
2962 * \retval 0 on success
2963 * \retval negative if failed
2965 static int lod_cache_parent_lmv_striping(const struct lu_env *env,
2966 struct lod_object *lp)
2968 struct lod_thread_info *info = lod_env_info(env);
2969 struct lmv_user_md_v1 *v1 = NULL;
2973 /* called from MDD without parent being write locked,
2975 dt_write_lock(env, dt_object_child(&lp->ldo_obj), 0);
2976 rc = lod_get_default_lmv_ea(env, lp);
2980 if (rc < (typeof(rc))sizeof(struct lmv_user_md)) {
2981 /* don't lookup for non-existing or invalid striping */
2982 lp->ldo_dir_def_striping_set = 0;
2983 lp->ldo_dir_def_striping_cached = 1;
2984 lp->ldo_dir_def_stripenr = 0;
2985 lp->ldo_dir_def_stripe_offset =
2986 (typeof(v1->lum_stripe_offset))(-1);
2987 lp->ldo_dir_def_hash_type = LMV_HASH_TYPE_FNV_1A_64;
2988 GOTO(unlock, rc = 0);
2992 v1 = info->lti_ea_store;
2994 lp->ldo_dir_def_stripenr = le32_to_cpu(v1->lum_stripe_count);
2995 lp->ldo_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
2996 lp->ldo_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
2997 lp->ldo_dir_def_striping_set = 1;
2998 lp->ldo_dir_def_striping_cached = 1;
3002 dt_write_unlock(env, dt_object_child(&lp->ldo_obj));
3007 * Cache default striping in the object.
3009 * To improve performance of striped object creation we cache default striping
3010 * (if it exists) in the parent directory object. We always cache default
3011 * striping for the regular files (stored in LOV EA) and we cache default
3012 * striping for the directories if requested by \a child_mode (when a new
3013 * directory is being created).
3015 * \param[in] env execution environment
3016 * \param[in] lp object
3017 * \param[in] child_mode new object's mode
3019 * \retval 0 on success
3020 * \retval negative if failed
3022 static int lod_cache_parent_striping(const struct lu_env *env,
3023 struct lod_object *lp,
3029 if (!lp->ldo_def_striping_cached) {
3030 /* we haven't tried to get default striping for
3031 * the directory yet, let's cache it in the object */
3032 rc = lod_cache_parent_lov_striping(env, lp);
3037 /* If the parent is on the remote MDT, we should always
3038 * try to refresh the default stripeEA cache, because we
3039 * do not cache default striping information for remote
3041 if (S_ISDIR(child_mode) && (!lp->ldo_dir_def_striping_cached ||
3042 dt_object_remote(&lp->ldo_obj)))
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 LASSERT(lp != NULL);
3110 if (lp->ldo_dir_stripe == NULL) {
3111 OBD_ALLOC_PTR(lp->ldo_dir_stripe);
3112 if (lp->ldo_dir_stripe == NULL)
3116 rc = lod_cache_parent_striping(env, lp, child_mode);
3120 /* transfer defaults to new directory */
3121 if (lp->ldo_def_striping_set) {
3123 lod_object_set_pool(lc, lp->ldo_pool);
3124 lc->ldo_def_stripenr = lp->ldo_def_stripenr;
3125 lc->ldo_def_stripe_size = lp->ldo_def_stripe_size;
3126 lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
3127 lc->ldo_def_striping_set = 1;
3128 lc->ldo_def_striping_cached = 1;
3129 CDEBUG(D_OTHER, "inherite EA sz:%d off:%d nr:%d\n",
3130 (int)lc->ldo_def_stripe_size,
3131 (int)lc->ldo_def_stripe_offset,
3132 (int)lc->ldo_def_stripenr);
3135 /* transfer dir defaults to new directory */
3136 if (lp->ldo_dir_def_striping_set) {
3137 lc->ldo_dir_def_stripenr = lp->ldo_dir_def_stripenr;
3138 lc->ldo_dir_def_stripe_offset =
3139 lp->ldo_dir_def_stripe_offset;
3140 lc->ldo_dir_def_hash_type =
3141 lp->ldo_dir_def_hash_type;
3142 lc->ldo_dir_def_striping_set = 1;
3143 lc->ldo_dir_def_striping_cached = 1;
3144 CDEBUG(D_INFO, "inherit default EA nr:%d off:%d t%u\n",
3145 (int)lc->ldo_dir_def_stripenr,
3146 (int)lc->ldo_dir_def_stripe_offset,
3147 lc->ldo_dir_def_hash_type);
3150 /* It should always honour the specified stripes */
3151 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0) {
3152 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
3154 rc = lod_verify_md_striping(d, lum1);
3156 le32_to_cpu(lum1->lum_stripe_count) > 1) {
3157 /* Directory will be striped only if
3158 * stripe_count > 1 */
3160 le32_to_cpu(lum1->lum_stripe_count);
3161 lc->ldo_dir_stripe_offset =
3162 le32_to_cpu(lum1->lum_stripe_offset);
3163 lc->ldo_dir_hash_type =
3164 le32_to_cpu(lum1->lum_hash_type);
3165 CDEBUG(D_INFO, "set stripe EA nr:%hu off:%d\n",
3167 (int)lc->ldo_dir_stripe_offset);
3169 /* then check whether there is default stripes from parent */
3170 } else if (lp->ldo_dir_def_striping_set) {
3171 /* If there are default dir stripe from parent */
3172 lc->ldo_stripenr = lp->ldo_dir_def_stripenr;
3173 lc->ldo_dir_stripe_offset =
3174 lp->ldo_dir_def_stripe_offset;
3175 lc->ldo_dir_hash_type =
3176 lp->ldo_dir_def_hash_type;
3177 CDEBUG(D_INFO, "inherit EA nr:%hu off:%d\n",
3179 (int)lc->ldo_dir_stripe_offset);
3181 /* set default stripe for this directory */
3182 lc->ldo_stripenr = 0;
3183 lc->ldo_dir_stripe_offset = -1;
3186 CDEBUG(D_INFO, "final striping count:%hu, offset:%d\n",
3187 lc->ldo_stripenr, (int)lc->ldo_dir_stripe_offset);
3193 * if object is going to be striped over OSTs, transfer default
3194 * striping information to the child, so that we can use it
3195 * during declaration and creation
3197 if (!lod_object_will_be_striped(S_ISREG(child_mode),
3198 lu_object_fid(&child->do_lu)))
3201 * try from the parent
3203 if (likely(parent)) {
3204 lod_cache_parent_striping(env, lp, child_mode);
3206 lc->ldo_def_stripe_offset = LOV_OFFSET_DEFAULT;
3208 if (lp->ldo_def_striping_set) {
3210 lod_object_set_pool(lc, lp->ldo_pool);
3211 lc->ldo_stripenr = lp->ldo_def_stripenr;
3212 lc->ldo_stripe_size = lp->ldo_def_stripe_size;
3213 lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
3214 CDEBUG(D_OTHER, "striping from parent: #%d, sz %d %s\n",
3215 lc->ldo_stripenr, lc->ldo_stripe_size,
3216 lp->ldo_pool ? lp->ldo_pool : "");
3221 * if the parent doesn't provide with specific pattern, grab fs-wide one
3223 desc = &d->lod_desc;
3224 if (lc->ldo_stripenr == 0)
3225 lc->ldo_stripenr = desc->ld_default_stripe_count;
3226 if (lc->ldo_stripe_size == 0)
3227 lc->ldo_stripe_size = desc->ld_default_stripe_size;
3228 CDEBUG(D_OTHER, "final striping: # %d stripes, sz %d from %s\n",
3229 lc->ldo_stripenr, lc->ldo_stripe_size,
3230 lc->ldo_pool ? lc->ldo_pool : "");
3233 /* we do not cache stripe information for slave stripe, see
3234 * lod_xattr_set_lov_on_dir */
3235 if (lp != NULL && lp->ldo_dir_slave_stripe)
3236 lod_lov_stripe_cache_clear(lp);
3241 #define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
3243 * Size initialization on late striping.
3245 * Propagate the size of a truncated object to a deferred striping.
3246 * This function handles a special case when truncate was done on a
3247 * non-striped object and now while the striping is being created
3248 * we can't lose that size, so we have to propagate it to the stripes
3251 * \param[in] env execution environment
3252 * \param[in] dt object
3253 * \param[in] th transaction handle
3255 * \retval 0 on success
3256 * \retval negative if failed
3258 static int lod_declare_init_size(const struct lu_env *env,
3259 struct dt_object *dt, struct thandle *th)
3261 struct dt_object *next = dt_object_child(dt);
3262 struct lod_object *lo = lod_dt_obj(dt);
3263 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3264 uint64_t size, offs;
3268 /* XXX: we support the simplest (RAID0) striping so far */
3269 LASSERT(lo->ldo_stripe || lo->ldo_stripenr == 0);
3270 LASSERT(lo->ldo_stripe_size > 0);
3272 rc = dt_attr_get(env, next, attr);
3273 LASSERT(attr->la_valid & LA_SIZE);
3277 size = attr->la_size;
3281 /* ll_do_div64(a, b) returns a % b, and a = a / b */
3282 ll_do_div64(size, (__u64) lo->ldo_stripe_size);
3283 stripe = ll_do_div64(size, (__u64) lo->ldo_stripenr);
3285 size = size * lo->ldo_stripe_size;
3286 offs = attr->la_size;
3287 size += ll_do_div64(offs, lo->ldo_stripe_size);
3289 attr->la_valid = LA_SIZE;
3290 attr->la_size = size;
3292 rc = lod_sub_object_declare_attr_set(env, lo->ldo_stripe[stripe], attr,
3299 * Declare creation of striped object.
3301 * The function declares creation stripes for a regular object. The function
3302 * also declares whether the stripes will be created with non-zero size if
3303 * previously size was set non-zero on the master object. If object \a dt is
3304 * not local, then only fully defined striping can be applied in \a lovea.
3305 * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
3308 * \param[in] env execution environment
3309 * \param[in] dt object
3310 * \param[in] attr attributes the stripes will be created with
3311 * \param[in] lovea a buffer containing striping description
3312 * \param[in] th transaction handle
3314 * \retval 0 on success
3315 * \retval negative if failed
3317 int lod_declare_striped_object(const struct lu_env *env, struct dt_object *dt,
3318 struct lu_attr *attr,
3319 const struct lu_buf *lovea, struct thandle *th)
3321 struct lod_thread_info *info = lod_env_info(env);
3322 struct dt_object *next = dt_object_child(dt);
3323 struct lod_object *lo = lod_dt_obj(dt);
3327 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO)) {
3328 /* failed to create striping, let's reset
3329 * config so that others don't get confused */
3330 lod_object_free_striping(env, lo);
3331 GOTO(out, rc = -ENOMEM);
3334 if (!dt_object_remote(next)) {
3335 /* choose OST and generate appropriate objects */
3336 rc = lod_qos_prep_create(env, lo, attr, lovea, th);
3338 /* failed to create striping, let's reset
3339 * config so that others don't get confused */
3340 lod_object_free_striping(env, lo);
3345 * declare storage for striping data
3347 info->lti_buf.lb_len = lov_mds_md_size(lo->ldo_stripenr,
3348 lo->ldo_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1);
3350 /* LOD can not choose OST objects for remote objects, i.e.
3351 * stripes must be ready before that. Right now, it can only
3352 * happen during migrate, i.e. migrate process needs to create
3353 * remote regular file (mdd_migrate_create), then the migrate
3354 * process will provide stripeEA. */
3355 LASSERT(lovea != NULL);
3356 info->lti_buf = *lovea;
3359 rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
3360 XATTR_NAME_LOV, 0, th);
3365 * if striping is created with local object's size > 0,
3366 * we have to propagate this size to specific object
3367 * the case is possible only when local object was created previously
3369 if (dt_object_exists(next))
3370 rc = lod_declare_init_size(env, dt, th);
3377 * Implementation of dt_object_operations::do_declare_create.
3379 * The method declares creation of a new object. If the object will be striped,
3380 * then helper functions are called to find FIDs for the stripes, declare
3381 * creation of the stripes and declare initialization of the striping
3382 * information to be stored in the master object.
3384 * \see dt_object_operations::do_declare_create() in the API description
3387 static int lod_declare_object_create(const struct lu_env *env,
3388 struct dt_object *dt,
3389 struct lu_attr *attr,
3390 struct dt_allocation_hint *hint,
3391 struct dt_object_format *dof,
3394 struct dt_object *next = dt_object_child(dt);
3395 struct lod_object *lo = lod_dt_obj(dt);
3404 * first of all, we declare creation of local object
3406 rc = lod_sub_object_declare_create(env, next, attr, hint, dof, th);
3410 if (dof->dof_type == DFT_SYM)
3411 dt->do_body_ops = &lod_body_lnk_ops;
3414 * it's lod_ah_init() that has decided the object will be striped
3416 if (dof->dof_type == DFT_REGULAR) {
3417 /* callers don't want stripes */
3418 /* XXX: all tricky interactions with ->ah_make_hint() decided
3419 * to use striping, then ->declare_create() behaving differently
3420 * should be cleaned */
3421 if (dof->u.dof_reg.striped == 0)
3422 lo->ldo_stripenr = 0;
3423 if (lo->ldo_stripenr > 0)
3424 rc = lod_declare_striped_object(env, dt, attr,
3426 } else if (dof->dof_type == DFT_DIR) {
3427 struct seq_server_site *ss;
3429 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
3431 /* If the parent has default stripeEA, and client
3432 * did not find it before sending create request,
3433 * then MDT will return -EREMOTE, and client will
3434 * retrieve the default stripeEA and re-create the
3437 * Note: if dah_eadata != NULL, it means creating the
3438 * striped directory with specified stripeEA, then it
3439 * should ignore the default stripeEA */
3440 if ((hint == NULL || hint->dah_eadata == NULL) &&
3441 lo->ldo_dir_stripe_offset != -1 &&
3442 lo->ldo_dir_stripe_offset != ss->ss_node_id)
3443 GOTO(out, rc = -EREMOTE);
3445 /* Orphan object (like migrating object) does not have
3446 * lod_dir_stripe, see lod_ah_init */
3447 if (lo->ldo_dir_stripe != NULL)
3448 rc = lod_declare_dir_striping_create(env, dt, attr,
3456 * Creation of a striped regular object.
3458 * The function is called to create the stripe objects for a regular
3459 * striped file. This can happen at the initial object creation or
3460 * when the caller asks LOD to do so using ->do_xattr_set() method
3461 * (so called late striping). Notice all the information are already
3462 * prepared in the form of the list of objects (ldo_stripe field).
3463 * This is done during declare phase.
3465 * \param[in] env execution environment
3466 * \param[in] dt object
3467 * \param[in] attr attributes the stripes will be created with
3468 * \param[in] dof format of stripes (see OSD API description)
3469 * \param[in] th transaction handle
3471 * \retval 0 on success
3472 * \retval negative if failed
3474 int lod_striping_create(const struct lu_env *env, struct dt_object *dt,
3475 struct lu_attr *attr, struct dt_object_format *dof,
3478 struct lod_object *lo = lod_dt_obj(dt);
3482 LASSERT(lo->ldo_striping_cached == 0);
3484 /* create all underlying objects */
3485 for (i = 0; i < lo->ldo_stripenr; i++) {
3486 LASSERT(lo->ldo_stripe[i]);
3487 rc = lod_sub_object_create(env, lo->ldo_stripe[i], attr, NULL,
3494 rc = lod_generate_and_set_lovea(env, lo, th);
3496 lo->ldo_striping_cached = 1;
3503 * Implementation of dt_object_operations::do_create.
3505 * If any of preceeding methods (like ->do_declare_create(),
3506 * ->do_ah_init(), etc) chose to create a striped object,
3507 * then this method will create the master and the stripes.
3509 * \see dt_object_operations::do_create() in the API description for details.
3511 static int lod_object_create(const struct lu_env *env, struct dt_object *dt,
3512 struct lu_attr *attr,
3513 struct dt_allocation_hint *hint,
3514 struct dt_object_format *dof, struct thandle *th)
3516 struct lod_object *lo = lod_dt_obj(dt);
3520 /* create local object */
3521 rc = lod_sub_object_create(env, dt_object_child(dt), attr, hint, dof,
3526 if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3527 lo->ldo_stripe && dof->u.dof_reg.striped != 0)
3528 rc = lod_striping_create(env, dt, attr, dof, th);
3534 * Implementation of dt_object_operations::do_declare_destroy.
3536 * If the object is a striped directory, then the function declares reference
3537 * removal from the master object (this is an index) to the stripes and declares
3538 * destroy of all the stripes. In all the cases, it declares an intention to
3539 * destroy the object itself.
3541 * \see dt_object_operations::do_declare_destroy() in the API description
3544 static int lod_declare_object_destroy(const struct lu_env *env,
3545 struct dt_object *dt,
3548 struct dt_object *next = dt_object_child(dt);
3549 struct lod_object *lo = lod_dt_obj(dt);
3550 struct lod_thread_info *info = lod_env_info(env);
3551 char *stripe_name = info->lti_key;
3556 * load striping information, notice we don't do this when object
3557 * is being initialized as we don't need this information till
3558 * few specific cases like destroy, chown
3560 rc = lod_load_striping(env, lo);
3564 /* declare destroy for all underlying objects */
3565 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3566 rc = next->do_ops->do_index_try(env, next,
3567 &dt_directory_features);
3571 for (i = 0; i < lo->ldo_stripenr; i++) {
3572 rc = lod_sub_object_declare_ref_del(env, next, th);
3576 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3577 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
3579 rc = lod_sub_object_declare_delete(env, next,
3580 (const struct dt_key *)stripe_name, th);
3587 * we declare destroy for the local object
3589 rc = lod_sub_object_declare_destroy(env, next, th);
3593 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ))
3596 /* declare destroy all striped objects */
3597 for (i = 0; i < lo->ldo_stripenr; i++) {
3598 if (lo->ldo_stripe[i] == NULL)
3601 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
3602 rc = lod_sub_object_declare_ref_del(env,
3603 lo->ldo_stripe[i], th);
3605 rc = lod_sub_object_declare_destroy(env, lo->ldo_stripe[i],
3615 * Implementation of dt_object_operations::do_destroy.
3617 * If the object is a striped directory, then the function removes references
3618 * from the master object (this is an index) to the stripes and destroys all
3619 * the stripes. In all the cases, the function destroys the object itself.
3621 * \see dt_object_operations::do_destroy() in the API description for details.
3623 static int lod_object_destroy(const struct lu_env *env,
3624 struct dt_object *dt, struct thandle *th)
3626 struct dt_object *next = dt_object_child(dt);
3627 struct lod_object *lo = lod_dt_obj(dt);
3628 struct lod_thread_info *info = lod_env_info(env);
3629 char *stripe_name = info->lti_key;
3634 /* destroy sub-stripe of master object */
3635 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3636 rc = next->do_ops->do_index_try(env, next,
3637 &dt_directory_features);
3641 for (i = 0; i < lo->ldo_stripenr; i++) {
3642 rc = lod_sub_object_ref_del(env, next, th);
3646 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3647 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
3650 CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
3651 PFID(lu_object_fid(&dt->do_lu)), stripe_name,
3652 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
3654 rc = lod_sub_object_delete(env, next,
3655 (const struct dt_key *)stripe_name, th);
3661 rc = lod_sub_object_destroy(env, next, th);
3665 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ))
3668 /* destroy all striped objects */
3669 for (i = 0; i < lo->ldo_stripenr; i++) {
3670 if (likely(lo->ldo_stripe[i] != NULL) &&
3671 (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
3672 i == cfs_fail_val)) {
3673 if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3674 dt_write_lock(env, lo->ldo_stripe[i],
3676 rc = lod_sub_object_ref_del(env,
3677 lo->ldo_stripe[i], th);
3678 dt_write_unlock(env, lo->ldo_stripe[i]);
3683 rc = lod_sub_object_destroy(env, lo->ldo_stripe[i], th);
3693 * Implementation of dt_object_operations::do_declare_ref_add.
3695 * \see dt_object_operations::do_declare_ref_add() in the API description
3698 static int lod_declare_ref_add(const struct lu_env *env,
3699 struct dt_object *dt, struct thandle *th)
3701 return lod_sub_object_declare_ref_add(env, dt_object_child(dt), th);
3705 * Implementation of dt_object_operations::do_ref_add.
3707 * \see dt_object_operations::do_ref_add() in the API description for details.
3709 static int lod_ref_add(const struct lu_env *env,
3710 struct dt_object *dt, struct thandle *th)
3712 return lod_sub_object_ref_add(env, dt_object_child(dt), th);
3716 * Implementation of dt_object_operations::do_declare_ref_del.
3718 * \see dt_object_operations::do_declare_ref_del() in the API description
3721 static int lod_declare_ref_del(const struct lu_env *env,
3722 struct dt_object *dt, struct thandle *th)
3724 return lod_sub_object_declare_ref_del(env, dt_object_child(dt), th);
3728 * Implementation of dt_object_operations::do_ref_del
3730 * \see dt_object_operations::do_ref_del() in the API description for details.
3732 static int lod_ref_del(const struct lu_env *env,
3733 struct dt_object *dt, struct thandle *th)
3735 return lod_sub_object_ref_del(env, dt_object_child(dt), th);
3739 * Implementation of dt_object_operations::do_object_sync.
3741 * \see dt_object_operations::do_object_sync() in the API description
3744 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
3745 __u64 start, __u64 end)
3747 return dt_object_sync(env, dt_object_child(dt), start, end);
3750 struct lod_slave_locks {
3752 struct lustre_handle lsl_handle[0];
3756 * Release LDLM locks on the stripes of a striped directory.
3758 * Iterates over all the locks taken on the stripe objects and
3759 * release them using ->do_object_unlock() method.
3761 * \param[in] env execution environment
3762 * \param[in] dt striped object
3763 * \param[in] einfo lock description
3764 * \param[in] policy data describing requested lock
3766 * \retval 0 on success
3767 * \retval negative if failed
3769 static int lod_object_unlock_internal(const struct lu_env *env,
3770 struct dt_object *dt,
3771 struct ldlm_enqueue_info *einfo,
3772 ldlm_policy_data_t *policy)
3774 struct lod_object *lo = lod_dt_obj(dt);
3775 struct lod_slave_locks *slave_locks = einfo->ei_cbdata;
3780 if (slave_locks == NULL)
3783 for (i = 1; i < slave_locks->lsl_lock_count; i++) {
3784 if (lustre_handle_is_used(&slave_locks->lsl_handle[i])) {
3787 einfo->ei_cbdata = &slave_locks->lsl_handle[i];
3788 rc1 = dt_object_unlock(env, lo->ldo_stripe[i], einfo,
3791 rc = rc == 0 ? rc1 : rc;
3799 * Implementation of dt_object_operations::do_object_unlock.
3801 * Used to release LDLM lock(s).
3803 * \see dt_object_operations::do_object_unlock() in the API description
3806 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
3807 struct ldlm_enqueue_info *einfo,
3808 union ldlm_policy_data *policy)
3810 struct lod_object *lo = lod_dt_obj(dt);
3811 struct lod_slave_locks *slave_locks = einfo->ei_cbdata;
3812 int slave_locks_size;
3816 if (slave_locks == NULL)
3819 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3822 rc = lod_load_striping(env, lo);
3826 /* Note: for remote lock for single stripe dir, MDT will cancel
3827 * the lock by lockh directly */
3828 if (lo->ldo_stripenr <= 1 && dt_object_remote(dt_object_child(dt)))
3831 /* Only cancel slave lock for striped dir */
3832 rc = lod_object_unlock_internal(env, dt, einfo, policy);
3834 slave_locks_size = sizeof(*slave_locks) + slave_locks->lsl_lock_count *
3835 sizeof(slave_locks->lsl_handle[0]);
3836 OBD_FREE(slave_locks, slave_locks_size);
3837 einfo->ei_cbdata = NULL;
3843 * Implementation of dt_object_operations::do_object_lock.
3845 * Used to get LDLM lock on the non-striped and striped objects.
3847 * \see dt_object_operations::do_object_lock() in the API description
3850 static int lod_object_lock(const struct lu_env *env,
3851 struct dt_object *dt,
3852 struct lustre_handle *lh,
3853 struct ldlm_enqueue_info *einfo,
3854 union ldlm_policy_data *policy)
3856 struct lod_object *lo = lod_dt_obj(dt);
3859 int slave_locks_size;
3860 struct lod_slave_locks *slave_locks = NULL;
3863 /* remote object lock */
3864 if (!einfo->ei_enq_slave) {
3865 LASSERT(dt_object_remote(dt));
3866 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
3870 if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3873 rc = lod_load_striping(env, lo);
3878 if (lo->ldo_stripenr <= 1)
3881 slave_locks_size = sizeof(*slave_locks) + lo->ldo_stripenr *
3882 sizeof(slave_locks->lsl_handle[0]);
3883 /* Freed in lod_object_unlock */
3884 OBD_ALLOC(slave_locks, slave_locks_size);
3885 if (slave_locks == NULL)
3887 slave_locks->lsl_lock_count = lo->ldo_stripenr;
3889 /* striped directory lock */
3890 for (i = 1; i < lo->ldo_stripenr; i++) {
3891 struct lustre_handle lockh;
3892 struct ldlm_res_id *res_id;
3894 res_id = &lod_env_info(env)->lti_res_id;
3895 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
3897 einfo->ei_res_id = res_id;
3899 LASSERT(lo->ldo_stripe[i]);
3900 rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh, einfo,
3904 slave_locks->lsl_handle[i] = lockh;
3907 einfo->ei_cbdata = slave_locks;
3910 if (rc != 0 && slave_locks != NULL) {
3911 einfo->ei_cbdata = slave_locks;
3912 lod_object_unlock_internal(env, dt, einfo, policy);
3913 OBD_FREE(slave_locks, slave_locks_size);
3914 einfo->ei_cbdata = NULL;
3920 struct dt_object_operations lod_obj_ops = {
3921 .do_read_lock = lod_object_read_lock,
3922 .do_write_lock = lod_object_write_lock,
3923 .do_read_unlock = lod_object_read_unlock,
3924 .do_write_unlock = lod_object_write_unlock,
3925 .do_write_locked = lod_object_write_locked,
3926 .do_attr_get = lod_attr_get,
3927 .do_declare_attr_set = lod_declare_attr_set,
3928 .do_attr_set = lod_attr_set,
3929 .do_xattr_get = lod_xattr_get,
3930 .do_declare_xattr_set = lod_declare_xattr_set,
3931 .do_xattr_set = lod_xattr_set,
3932 .do_declare_xattr_del = lod_declare_xattr_del,
3933 .do_xattr_del = lod_xattr_del,
3934 .do_xattr_list = lod_xattr_list,
3935 .do_ah_init = lod_ah_init,
3936 .do_declare_create = lod_declare_object_create,
3937 .do_create = lod_object_create,
3938 .do_declare_destroy = lod_declare_object_destroy,
3939 .do_destroy = lod_object_destroy,
3940 .do_index_try = lod_index_try,
3941 .do_declare_ref_add = lod_declare_ref_add,
3942 .do_ref_add = lod_ref_add,
3943 .do_declare_ref_del = lod_declare_ref_del,
3944 .do_ref_del = lod_ref_del,
3945 .do_object_sync = lod_object_sync,
3946 .do_object_lock = lod_object_lock,
3947 .do_object_unlock = lod_object_unlock,
3951 * Implementation of dt_body_operations::dbo_read.
3953 * \see dt_body_operations::dbo_read() in the API description for details.
3955 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
3956 struct lu_buf *buf, loff_t *pos)
3958 struct dt_object *next = dt_object_child(dt);
3959 return next->do_body_ops->dbo_read(env, next, buf, pos);
3963 * Implementation of dt_body_operations::dbo_declare_write.
3965 * \see dt_body_operations::dbo_declare_write() in the API description
3968 static ssize_t lod_declare_write(const struct lu_env *env,
3969 struct dt_object *dt,
3970 const struct lu_buf *buf, loff_t pos,
3973 return lod_sub_object_declare_write(env, dt_object_child(dt), buf, pos,
3978 * Implementation of dt_body_operations::dbo_write.
3980 * \see dt_body_operations::dbo_write() in the API description for details.
3982 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
3983 const struct lu_buf *buf, loff_t *pos,
3984 struct thandle *th, int iq)
3986 return lod_sub_object_write(env, dt_object_child(dt), buf, pos, th, iq);
3989 static const struct dt_body_operations lod_body_lnk_ops = {
3990 .dbo_read = lod_read,
3991 .dbo_declare_write = lod_declare_write,
3992 .dbo_write = lod_write
3996 * Implementation of lu_object_operations::loo_object_init.
3998 * The function determines the type and the index of the target device using
3999 * sequence of the object's FID. Then passes control down to the
4000 * corresponding device:
4001 * OSD for the local objects, OSP for remote
4003 * \see lu_object_operations::loo_object_init() in the API description
4006 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
4007 const struct lu_object_conf *conf)
4009 struct lod_device *lod = lu2lod_dev(lo->lo_dev);
4010 struct lu_device *cdev = NULL;
4011 struct lu_object *cobj;
4012 struct lod_tgt_descs *ltd = NULL;
4013 struct lod_tgt_desc *tgt;
4015 int type = LU_SEQ_RANGE_ANY;
4019 rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
4021 /* Note: Sometimes, it will Return EAGAIN here, see
4022 * ptrlpc_import_delay_req(), which might confuse
4023 * lu_object_find_at() and make it wait there incorrectly.
4024 * so we convert it to EIO here.*/
4031 if (type == LU_SEQ_RANGE_MDT &&
4032 idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
4033 cdev = &lod->lod_child->dd_lu_dev;
4034 } else if (type == LU_SEQ_RANGE_MDT) {
4035 ltd = &lod->lod_mdt_descs;
4037 } else if (type == LU_SEQ_RANGE_OST) {
4038 ltd = &lod->lod_ost_descs;
4045 if (ltd->ltd_tgts_size > idx &&
4046 cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
4047 tgt = LTD_TGT(ltd, idx);
4049 LASSERT(tgt != NULL);
4050 LASSERT(tgt->ltd_tgt != NULL);
4052 cdev = &(tgt->ltd_tgt->dd_lu_dev);
4054 lod_putref(lod, ltd);
4057 if (unlikely(cdev == NULL))
4060 cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
4061 if (unlikely(cobj == NULL))
4064 lu_object_add(lo, cobj);
4071 * Release resources associated with striping.
4073 * If the object is striped (regular or directory), then release
4074 * the stripe objects references and free the ldo_stripe array.
4076 * \param[in] env execution environment
4077 * \param[in] lo object
4079 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
4083 if (lo->ldo_dir_stripe != NULL) {
4084 OBD_FREE_PTR(lo->ldo_dir_stripe);
4085 lo->ldo_dir_stripe = NULL;
4088 if (lo->ldo_stripe) {
4089 LASSERT(lo->ldo_stripes_allocated > 0);
4091 for (i = 0; i < lo->ldo_stripenr; i++) {
4092 if (lo->ldo_stripe[i])
4093 lu_object_put(env, &lo->ldo_stripe[i]->do_lu);
4096 i = sizeof(struct dt_object *) * lo->ldo_stripes_allocated;
4097 OBD_FREE(lo->ldo_stripe, i);
4098 lo->ldo_stripe = NULL;
4099 lo->ldo_stripes_allocated = 0;
4101 lo->ldo_striping_cached = 0;
4102 lo->ldo_stripenr = 0;
4103 lo->ldo_pattern = 0;
4107 * Implementation of lu_object_operations::loo_object_start.
4109 * \see lu_object_operations::loo_object_start() in the API description
4112 static int lod_object_start(const struct lu_env *env, struct lu_object *o)
4114 if (S_ISLNK(o->lo_header->loh_attr & S_IFMT))
4115 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_lnk_ops;
4120 * Implementation of lu_object_operations::loo_object_free.
4122 * \see lu_object_operations::loo_object_free() in the API description
4125 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
4127 struct lod_object *mo = lu2lod_obj(o);
4130 * release all underlying object pinned
4133 lod_object_free_striping(env, mo);
4135 lod_object_set_pool(mo, NULL);
4138 OBD_SLAB_FREE_PTR(mo, lod_object_kmem);
4142 * Implementation of lu_object_operations::loo_object_release.
4144 * \see lu_object_operations::loo_object_release() in the API description
4147 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
4149 /* XXX: shouldn't we release everything here in case if object
4150 * creation failed before? */
4154 * Implementation of lu_object_operations::loo_object_print.
4156 * \see lu_object_operations::loo_object_print() in the API description
4159 static int lod_object_print(const struct lu_env *env, void *cookie,
4160 lu_printer_t p, const struct lu_object *l)
4162 struct lod_object *o = lu2lod_obj((struct lu_object *) l);
4164 return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
4167 struct lu_object_operations lod_lu_obj_ops = {
4168 .loo_object_init = lod_object_init,
4169 .loo_object_start = lod_object_start,
4170 .loo_object_free = lod_object_free,
4171 .loo_object_release = lod_object_release,
4172 .loo_object_print = lod_object_print,