Whamcloud - gitweb
LU-10308 misc: update Intel copyright messages for 2017
[fs/lustre-release.git] / lustre / lod / lod_object.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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.
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright  2009 Sun Microsystems, Inc. All rights reserved
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * lustre/lod/lod_object.c
30  *
31  * This file contains implementations of methods for the OSD API
32  * for the Logical Object Device (LOD) layer, which provides a virtual
33  * local OSD object interface to the MDD layer, and abstracts the
34  * addressing of local (OSD) and remote (OSP) objects. The API is
35  * described in the file lustre/include/dt_object.h and in
36  * Documentation/osd-api.txt.
37  *
38  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_MDS
42
43 #include <obd.h>
44 #include <obd_class.h>
45 #include <obd_support.h>
46
47 #include <lustre_fid.h>
48 #include <lustre_linkea.h>
49 #include <lustre_lmv.h>
50 #include <uapi/linux/lustre/lustre_param.h>
51 #include <lustre_swab.h>
52 #include <uapi/linux/lustre/lustre_ver.h>
53 #include <lprocfs_status.h>
54 #include <md_object.h>
55
56 #include "lod_internal.h"
57
58 static const char dot[] = ".";
59 static const char dotdot[] = "..";
60
61 /**
62  * Implementation of dt_index_operations::dio_lookup
63  *
64  * Used with regular (non-striped) objects.
65  *
66  * \see dt_index_operations::dio_lookup() in the API description for details.
67  */
68 static int lod_lookup(const struct lu_env *env, struct dt_object *dt,
69                       struct dt_rec *rec, const struct dt_key *key)
70 {
71         struct dt_object *next = dt_object_child(dt);
72         return next->do_index_ops->dio_lookup(env, next, rec, key);
73 }
74
75 /**
76  * Implementation of dt_index_operations::dio_declare_insert.
77  *
78  * Used with regular (non-striped) objects.
79  *
80  * \see dt_index_operations::dio_declare_insert() in the API description
81  * for details.
82  */
83 static int lod_declare_insert(const struct lu_env *env, struct dt_object *dt,
84                               const struct dt_rec *rec,
85                               const struct dt_key *key, struct thandle *th)
86 {
87         return lod_sub_declare_insert(env, dt_object_child(dt), rec, key, th);
88 }
89
90 /**
91  * Implementation of dt_index_operations::dio_insert.
92  *
93  * Used with regular (non-striped) objects
94  *
95  * \see dt_index_operations::dio_insert() in the API description for details.
96  */
97 static int lod_insert(const struct lu_env *env, struct dt_object *dt,
98                       const struct dt_rec *rec, const struct dt_key *key,
99                       struct thandle *th, int ign)
100 {
101         return lod_sub_insert(env, dt_object_child(dt), rec, key, th, ign);
102 }
103
104 /**
105  * Implementation of dt_index_operations::dio_declare_delete.
106  *
107  * Used with regular (non-striped) objects.
108  *
109  * \see dt_index_operations::dio_declare_delete() in the API description
110  * for details.
111  */
112 static int lod_declare_delete(const struct lu_env *env, struct dt_object *dt,
113                               const struct dt_key *key, struct thandle *th)
114 {
115         return lod_sub_declare_delete(env, dt_object_child(dt), key, th);
116 }
117
118 /**
119  * Implementation of dt_index_operations::dio_delete.
120  *
121  * Used with regular (non-striped) objects.
122  *
123  * \see dt_index_operations::dio_delete() in the API description for details.
124  */
125 static int lod_delete(const struct lu_env *env, struct dt_object *dt,
126                       const struct dt_key *key, struct thandle *th)
127 {
128         return lod_sub_delete(env, dt_object_child(dt), key, th);
129 }
130
131 /**
132  * Implementation of dt_it_ops::init.
133  *
134  * Used with regular (non-striped) objects.
135  *
136  * \see dt_it_ops::init() in the API description for details.
137  */
138 static struct dt_it *lod_it_init(const struct lu_env *env,
139                                  struct dt_object *dt, __u32 attr)
140 {
141         struct dt_object        *next = dt_object_child(dt);
142         struct lod_it           *it = &lod_env_info(env)->lti_it;
143         struct dt_it            *it_next;
144
145         it_next = next->do_index_ops->dio_it.init(env, next, attr);
146         if (IS_ERR(it_next))
147                 return it_next;
148
149         /* currently we do not use more than one iterator per thread
150          * so we store it in thread info. if at some point we need
151          * more active iterators in a single thread, we can allocate
152          * additional ones */
153         LASSERT(it->lit_obj == NULL);
154
155         it->lit_it = it_next;
156         it->lit_obj = next;
157
158         return (struct dt_it *)it;
159 }
160
161 #define LOD_CHECK_IT(env, it)                                   \
162 do {                                                            \
163         LASSERT((it)->lit_obj != NULL);                         \
164         LASSERT((it)->lit_it != NULL);                          \
165 } while (0)
166
167 /**
168  * Implementation of dt_index_operations::dio_it.fini.
169  *
170  * Used with regular (non-striped) objects.
171  *
172  * \see dt_index_operations::dio_it.fini() in the API description for details.
173  */
174 static void lod_it_fini(const struct lu_env *env, struct dt_it *di)
175 {
176         struct lod_it *it = (struct lod_it *)di;
177
178         LOD_CHECK_IT(env, it);
179         it->lit_obj->do_index_ops->dio_it.fini(env, it->lit_it);
180
181         /* the iterator not in use any more */
182         it->lit_obj = NULL;
183         it->lit_it = NULL;
184 }
185
186 /**
187  * Implementation of dt_it_ops::get.
188  *
189  * Used with regular (non-striped) objects.
190  *
191  * \see dt_it_ops::get() in the API description for details.
192  */
193 static int lod_it_get(const struct lu_env *env, struct dt_it *di,
194                       const struct dt_key *key)
195 {
196         const struct lod_it *it = (const struct lod_it *)di;
197
198         LOD_CHECK_IT(env, it);
199         return it->lit_obj->do_index_ops->dio_it.get(env, it->lit_it, key);
200 }
201
202 /**
203  * Implementation of dt_it_ops::put.
204  *
205  * Used with regular (non-striped) objects.
206  *
207  * \see dt_it_ops::put() in the API description for details.
208  */
209 static void lod_it_put(const struct lu_env *env, struct dt_it *di)
210 {
211         struct lod_it *it = (struct lod_it *)di;
212
213         LOD_CHECK_IT(env, it);
214         return it->lit_obj->do_index_ops->dio_it.put(env, it->lit_it);
215 }
216
217 /**
218  * Implementation of dt_it_ops::next.
219  *
220  * Used with regular (non-striped) objects
221  *
222  * \see dt_it_ops::next() in the API description for details.
223  */
224 static int lod_it_next(const struct lu_env *env, struct dt_it *di)
225 {
226         struct lod_it *it = (struct lod_it *)di;
227
228         LOD_CHECK_IT(env, it);
229         return it->lit_obj->do_index_ops->dio_it.next(env, it->lit_it);
230 }
231
232 /**
233  * Implementation of dt_it_ops::key.
234  *
235  * Used with regular (non-striped) objects.
236  *
237  * \see dt_it_ops::key() in the API description for details.
238  */
239 static struct dt_key *lod_it_key(const struct lu_env *env,
240                                  const struct dt_it *di)
241 {
242         const struct lod_it *it = (const struct lod_it *)di;
243
244         LOD_CHECK_IT(env, it);
245         return it->lit_obj->do_index_ops->dio_it.key(env, it->lit_it);
246 }
247
248 /**
249  * Implementation of dt_it_ops::key_size.
250  *
251  * Used with regular (non-striped) objects.
252  *
253  * \see dt_it_ops::key_size() in the API description for details.
254  */
255 static int lod_it_key_size(const struct lu_env *env, const struct dt_it *di)
256 {
257         struct lod_it *it = (struct lod_it *)di;
258
259         LOD_CHECK_IT(env, it);
260         return it->lit_obj->do_index_ops->dio_it.key_size(env, it->lit_it);
261 }
262
263 /**
264  * Implementation of dt_it_ops::rec.
265  *
266  * Used with regular (non-striped) objects.
267  *
268  * \see dt_it_ops::rec() in the API description for details.
269  */
270 static int lod_it_rec(const struct lu_env *env, const struct dt_it *di,
271                       struct dt_rec *rec, __u32 attr)
272 {
273         const struct lod_it *it = (const struct lod_it *)di;
274
275         LOD_CHECK_IT(env, it);
276         return it->lit_obj->do_index_ops->dio_it.rec(env, it->lit_it, rec,
277                                                      attr);
278 }
279
280 /**
281  * Implementation of dt_it_ops::rec_size.
282  *
283  * Used with regular (non-striped) objects.
284  *
285  * \see dt_it_ops::rec_size() in the API description for details.
286  */
287 static int lod_it_rec_size(const struct lu_env *env, const struct dt_it *di,
288                            __u32 attr)
289 {
290         const struct lod_it *it = (const struct lod_it *)di;
291
292         LOD_CHECK_IT(env, it);
293         return it->lit_obj->do_index_ops->dio_it.rec_size(env, it->lit_it,
294                                                           attr);
295 }
296
297 /**
298  * Implementation of dt_it_ops::store.
299  *
300  * Used with regular (non-striped) objects.
301  *
302  * \see dt_it_ops::store() in the API description for details.
303  */
304 static __u64 lod_it_store(const struct lu_env *env, const struct dt_it *di)
305 {
306         const struct lod_it *it = (const struct lod_it *)di;
307
308         LOD_CHECK_IT(env, it);
309         return it->lit_obj->do_index_ops->dio_it.store(env, it->lit_it);
310 }
311
312 /**
313  * Implementation of dt_it_ops::load.
314  *
315  * Used with regular (non-striped) objects.
316  *
317  * \see dt_it_ops::load() in the API description for details.
318  */
319 static int lod_it_load(const struct lu_env *env, const struct dt_it *di,
320                        __u64 hash)
321 {
322         const struct lod_it *it = (const struct lod_it *)di;
323
324         LOD_CHECK_IT(env, it);
325         return it->lit_obj->do_index_ops->dio_it.load(env, it->lit_it, hash);
326 }
327
328 /**
329  * Implementation of dt_it_ops::key_rec.
330  *
331  * Used with regular (non-striped) objects.
332  *
333  * \see dt_it_ops::rec() in the API description for details.
334  */
335 static int lod_it_key_rec(const struct lu_env *env, const struct dt_it *di,
336                           void *key_rec)
337 {
338         const struct lod_it *it = (const struct lod_it *)di;
339
340         LOD_CHECK_IT(env, it);
341         return it->lit_obj->do_index_ops->dio_it.key_rec(env, it->lit_it,
342                                                          key_rec);
343 }
344
345 static struct dt_index_operations lod_index_ops = {
346         .dio_lookup             = lod_lookup,
347         .dio_declare_insert     = lod_declare_insert,
348         .dio_insert             = lod_insert,
349         .dio_declare_delete     = lod_declare_delete,
350         .dio_delete             = lod_delete,
351         .dio_it = {
352                 .init           = lod_it_init,
353                 .fini           = lod_it_fini,
354                 .get            = lod_it_get,
355                 .put            = lod_it_put,
356                 .next           = lod_it_next,
357                 .key            = lod_it_key,
358                 .key_size       = lod_it_key_size,
359                 .rec            = lod_it_rec,
360                 .rec_size       = lod_it_rec_size,
361                 .store          = lod_it_store,
362                 .load           = lod_it_load,
363                 .key_rec        = lod_it_key_rec,
364         }
365 };
366
367 /**
368  * Implementation of dt_it_ops::init.
369  *
370  * Used with striped objects. Internally just initializes the iterator
371  * on the first stripe.
372  *
373  * \see dt_it_ops::init() in the API description for details.
374  */
375 static struct dt_it *lod_striped_it_init(const struct lu_env *env,
376                                          struct dt_object *dt, __u32 attr)
377 {
378         struct lod_object       *lo = lod_dt_obj(dt);
379         struct dt_object        *next;
380         struct lod_it           *it = &lod_env_info(env)->lti_it;
381         struct dt_it            *it_next;
382         ENTRY;
383
384         LASSERT(lo->ldo_dir_stripe_count > 0);
385         next = lo->ldo_stripe[0];
386         LASSERT(next != NULL);
387         LASSERT(next->do_index_ops != NULL);
388
389         it_next = next->do_index_ops->dio_it.init(env, next, attr);
390         if (IS_ERR(it_next))
391                 return it_next;
392
393         /* currently we do not use more than one iterator per thread
394          * so we store it in thread info. if at some point we need
395          * more active iterators in a single thread, we can allocate
396          * additional ones */
397         LASSERT(it->lit_obj == NULL);
398
399         it->lit_stripe_index = 0;
400         it->lit_attr = attr;
401         it->lit_it = it_next;
402         it->lit_obj = dt;
403
404         return (struct dt_it *)it;
405 }
406
407 #define LOD_CHECK_STRIPED_IT(env, it, lo)                               \
408 do {                                                                    \
409         LASSERT((it)->lit_obj != NULL);                                 \
410         LASSERT((it)->lit_it != NULL);                                  \
411         LASSERT((lo)->ldo_dir_stripe_count > 0);                        \
412         LASSERT((it)->lit_stripe_index < (lo)->ldo_dir_stripe_count);   \
413 } while (0)
414
415 /**
416  * Implementation of dt_it_ops::fini.
417  *
418  * Used with striped objects.
419  *
420  * \see dt_it_ops::fini() in the API description for details.
421  */
422 static void lod_striped_it_fini(const struct lu_env *env, struct dt_it *di)
423 {
424         struct lod_it           *it = (struct lod_it *)di;
425         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
426         struct dt_object        *next;
427
428         /* If lit_it == NULL, then it means the sub_it has been finished,
429          * which only happens in failure cases, see lod_striped_it_next() */
430         if (it->lit_it != NULL) {
431                 LOD_CHECK_STRIPED_IT(env, it, lo);
432
433                 next = lo->ldo_stripe[it->lit_stripe_index];
434                 LASSERT(next != NULL);
435                 LASSERT(next->do_index_ops != NULL);
436
437                 next->do_index_ops->dio_it.fini(env, it->lit_it);
438         }
439
440         /* the iterator not in use any more */
441         it->lit_obj = NULL;
442         it->lit_it = NULL;
443         it->lit_stripe_index = 0;
444 }
445
446 /**
447  * Implementation of dt_it_ops::get.
448  *
449  * Right now it's not used widely, only to reset the iterator to the
450  * initial position. It should be possible to implement a full version
451  * which chooses a correct stripe to be able to position with any key.
452  *
453  * \see dt_it_ops::get() in the API description for details.
454  */
455 static int lod_striped_it_get(const struct lu_env *env, struct dt_it *di,
456                               const struct dt_key *key)
457 {
458         const struct lod_it     *it = (const struct lod_it *)di;
459         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
460         struct dt_object        *next;
461         ENTRY;
462
463         LOD_CHECK_STRIPED_IT(env, it, lo);
464
465         next = lo->ldo_stripe[it->lit_stripe_index];
466         LASSERT(next != NULL);
467         LASSERT(next->do_index_ops != NULL);
468
469         return next->do_index_ops->dio_it.get(env, it->lit_it, key);
470 }
471
472 /**
473  * Implementation of dt_it_ops::put.
474  *
475  * Used with striped objects.
476  *
477  * \see dt_it_ops::put() in the API description for details.
478  */
479 static void lod_striped_it_put(const struct lu_env *env, struct dt_it *di)
480 {
481         struct lod_it           *it = (struct lod_it *)di;
482         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
483         struct dt_object        *next;
484
485         LOD_CHECK_STRIPED_IT(env, it, lo);
486
487         next = lo->ldo_stripe[it->lit_stripe_index];
488         LASSERT(next != NULL);
489         LASSERT(next->do_index_ops != NULL);
490
491         return next->do_index_ops->dio_it.put(env, it->lit_it);
492 }
493
494 /**
495  * Implementation of dt_it_ops::next.
496  *
497  * Used with striped objects. When the end of the current stripe is
498  * reached, the method takes the next stripe's iterator.
499  *
500  * \see dt_it_ops::next() in the API description for details.
501  */
502 static int lod_striped_it_next(const struct lu_env *env, struct dt_it *di)
503 {
504         struct lod_it           *it = (struct lod_it *)di;
505         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
506         struct dt_object        *next;
507         struct dt_it            *it_next;
508         int                     rc;
509         ENTRY;
510
511         LOD_CHECK_STRIPED_IT(env, it, lo);
512
513         next = lo->ldo_stripe[it->lit_stripe_index];
514         LASSERT(next != NULL);
515         LASSERT(next->do_index_ops != NULL);
516 again:
517         rc = next->do_index_ops->dio_it.next(env, it->lit_it);
518         if (rc < 0)
519                 RETURN(rc);
520
521         if (rc == 0 && it->lit_stripe_index == 0)
522                 RETURN(rc);
523
524         if (rc == 0 && it->lit_stripe_index > 0) {
525                 struct lu_dirent *ent;
526
527                 ent = (struct lu_dirent *)lod_env_info(env)->lti_key;
528
529                 rc = next->do_index_ops->dio_it.rec(env, it->lit_it,
530                                                     (struct dt_rec *)ent,
531                                                     it->lit_attr);
532                 if (rc != 0)
533                         RETURN(rc);
534
535                 /* skip . and .. for slave stripe */
536                 if ((strncmp(ent->lde_name, ".",
537                              le16_to_cpu(ent->lde_namelen)) == 0 &&
538                      le16_to_cpu(ent->lde_namelen) == 1) ||
539                     (strncmp(ent->lde_name, "..",
540                              le16_to_cpu(ent->lde_namelen)) == 0 &&
541                      le16_to_cpu(ent->lde_namelen) == 2))
542                         goto again;
543
544                 RETURN(rc);
545         }
546
547         /* go to next stripe */
548         if (it->lit_stripe_index + 1 >= lo->ldo_dir_stripe_count)
549                 RETURN(1);
550
551         it->lit_stripe_index++;
552
553         next->do_index_ops->dio_it.put(env, it->lit_it);
554         next->do_index_ops->dio_it.fini(env, it->lit_it);
555         it->lit_it = NULL;
556
557         next = lo->ldo_stripe[it->lit_stripe_index];
558         LASSERT(next != NULL);
559         rc = next->do_ops->do_index_try(env, next, &dt_directory_features);
560         if (rc != 0)
561                 RETURN(rc);
562
563         LASSERT(next->do_index_ops != NULL);
564
565         it_next = next->do_index_ops->dio_it.init(env, next, it->lit_attr);
566         if (!IS_ERR(it_next)) {
567                 it->lit_it = it_next;
568                 goto again;
569         } else {
570                 rc = PTR_ERR(it_next);
571         }
572
573         RETURN(rc);
574 }
575
576 /**
577  * Implementation of dt_it_ops::key.
578  *
579  * Used with striped objects.
580  *
581  * \see dt_it_ops::key() in the API description for details.
582  */
583 static struct dt_key *lod_striped_it_key(const struct lu_env *env,
584                                          const struct dt_it *di)
585 {
586         const struct lod_it     *it = (const struct lod_it *)di;
587         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
588         struct dt_object        *next;
589
590         LOD_CHECK_STRIPED_IT(env, it, lo);
591
592         next = lo->ldo_stripe[it->lit_stripe_index];
593         LASSERT(next != NULL);
594         LASSERT(next->do_index_ops != NULL);
595
596         return next->do_index_ops->dio_it.key(env, it->lit_it);
597 }
598
599 /**
600  * Implementation of dt_it_ops::key_size.
601  *
602  * Used with striped objects.
603  *
604  * \see dt_it_ops::size() in the API description for details.
605  */
606 static int lod_striped_it_key_size(const struct lu_env *env,
607                                    const struct dt_it *di)
608 {
609         struct lod_it           *it = (struct lod_it *)di;
610         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
611         struct dt_object        *next;
612
613         LOD_CHECK_STRIPED_IT(env, it, lo);
614
615         next = lo->ldo_stripe[it->lit_stripe_index];
616         LASSERT(next != NULL);
617         LASSERT(next->do_index_ops != NULL);
618
619         return next->do_index_ops->dio_it.key_size(env, it->lit_it);
620 }
621
622 /**
623  * Implementation of dt_it_ops::rec.
624  *
625  * Used with striped objects.
626  *
627  * \see dt_it_ops::rec() in the API description for details.
628  */
629 static int lod_striped_it_rec(const struct lu_env *env, const struct dt_it *di,
630                               struct dt_rec *rec, __u32 attr)
631 {
632         const struct lod_it     *it = (const struct lod_it *)di;
633         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
634         struct dt_object        *next;
635
636         LOD_CHECK_STRIPED_IT(env, it, lo);
637
638         next = lo->ldo_stripe[it->lit_stripe_index];
639         LASSERT(next != NULL);
640         LASSERT(next->do_index_ops != NULL);
641
642         return next->do_index_ops->dio_it.rec(env, it->lit_it, rec, attr);
643 }
644
645 /**
646  * Implementation of dt_it_ops::rec_size.
647  *
648  * Used with striped objects.
649  *
650  * \see dt_it_ops::rec_size() in the API description for details.
651  */
652 static int lod_striped_it_rec_size(const struct lu_env *env,
653                                    const struct dt_it *di, __u32 attr)
654 {
655         struct lod_it           *it = (struct lod_it *)di;
656         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
657         struct dt_object        *next;
658
659         LOD_CHECK_STRIPED_IT(env, it, lo);
660
661         next = lo->ldo_stripe[it->lit_stripe_index];
662         LASSERT(next != NULL);
663         LASSERT(next->do_index_ops != NULL);
664
665         return next->do_index_ops->dio_it.rec_size(env, it->lit_it, attr);
666 }
667
668 /**
669  * Implementation of dt_it_ops::store.
670  *
671  * Used with striped objects.
672  *
673  * \see dt_it_ops::store() in the API description for details.
674  */
675 static __u64 lod_striped_it_store(const struct lu_env *env,
676                                   const struct dt_it *di)
677 {
678         const struct lod_it     *it = (const struct lod_it *)di;
679         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
680         struct dt_object        *next;
681
682         LOD_CHECK_STRIPED_IT(env, it, lo);
683
684         next = lo->ldo_stripe[it->lit_stripe_index];
685         LASSERT(next != NULL);
686         LASSERT(next->do_index_ops != NULL);
687
688         return next->do_index_ops->dio_it.store(env, it->lit_it);
689 }
690
691 /**
692  * Implementation of dt_it_ops::load.
693  *
694  * Used with striped objects.
695  *
696  * \see dt_it_ops::load() in the API description for details.
697  */
698 static int lod_striped_it_load(const struct lu_env *env,
699                                const struct dt_it *di, __u64 hash)
700 {
701         const struct lod_it     *it = (const struct lod_it *)di;
702         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
703         struct dt_object        *next;
704
705         LOD_CHECK_STRIPED_IT(env, it, lo);
706
707         next = lo->ldo_stripe[it->lit_stripe_index];
708         LASSERT(next != NULL);
709         LASSERT(next->do_index_ops != NULL);
710
711         return next->do_index_ops->dio_it.load(env, it->lit_it, hash);
712 }
713
714 static struct dt_index_operations lod_striped_index_ops = {
715         .dio_lookup             = lod_lookup,
716         .dio_declare_insert     = lod_declare_insert,
717         .dio_insert             = lod_insert,
718         .dio_declare_delete     = lod_declare_delete,
719         .dio_delete             = lod_delete,
720         .dio_it = {
721                 .init           = lod_striped_it_init,
722                 .fini           = lod_striped_it_fini,
723                 .get            = lod_striped_it_get,
724                 .put            = lod_striped_it_put,
725                 .next           = lod_striped_it_next,
726                 .key            = lod_striped_it_key,
727                 .key_size       = lod_striped_it_key_size,
728                 .rec            = lod_striped_it_rec,
729                 .rec_size       = lod_striped_it_rec_size,
730                 .store          = lod_striped_it_store,
731                 .load           = lod_striped_it_load,
732         }
733 };
734
735 /**
736  * Append the FID for each shard of the striped directory after the
737  * given LMV EA header.
738  *
739  * To simplify striped directory and the consistency verification,
740  * we only store the LMV EA header on disk, for both master object
741  * and slave objects. When someone wants to know the whole LMV EA,
742  * such as client readdir(), we can build the entrie LMV EA on the
743  * MDT side (in RAM) via iterating the sub-directory entries that
744  * are contained in the master object of the stripe directory.
745  *
746  * For the master object of the striped directroy, the valid name
747  * for each shard is composed of the ${shard_FID}:${shard_idx}.
748  *
749  * There may be holes in the LMV EA if some shards' name entries
750  * are corrupted or lost.
751  *
752  * \param[in] env       pointer to the thread context
753  * \param[in] lo        pointer to the master object of the striped directory
754  * \param[in] buf       pointer to the lu_buf which will hold the LMV EA
755  * \param[in] resize    whether re-allocate the buffer if it is not big enough
756  *
757  * \retval              positive size of the LMV EA
758  * \retval              0 for nothing to be loaded
759  * \retval              negative error number on failure
760  */
761 int lod_load_lmv_shards(const struct lu_env *env, struct lod_object *lo,
762                         struct lu_buf *buf, bool resize)
763 {
764         struct lu_dirent        *ent    =
765                         (struct lu_dirent *)lod_env_info(env)->lti_key;
766         struct lod_device       *lod    = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
767         struct dt_object        *obj    = dt_object_child(&lo->ldo_obj);
768         struct lmv_mds_md_v1    *lmv1   = buf->lb_buf;
769         struct dt_it            *it;
770         const struct dt_it_ops  *iops;
771         __u32                    stripes;
772         __u32                    magic  = le32_to_cpu(lmv1->lmv_magic);
773         size_t                   lmv1_size;
774         int                      rc;
775         ENTRY;
776
777         /* If it is not a striped directory, then load nothing. */
778         if (magic != LMV_MAGIC_V1)
779                 RETURN(0);
780
781         /* If it is in migration (or failure), then load nothing. */
782         if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
783                 RETURN(0);
784
785         stripes = le32_to_cpu(lmv1->lmv_stripe_count);
786         if (stripes < 1)
787                 RETURN(0);
788
789         rc = lmv_mds_md_size(stripes, magic);
790         if (rc < 0)
791                 RETURN(rc);
792         lmv1_size = rc;
793         if (buf->lb_len < lmv1_size) {
794                 struct lu_buf tbuf;
795
796                 if (!resize)
797                         RETURN(-ERANGE);
798
799                 tbuf = *buf;
800                 buf->lb_buf = NULL;
801                 buf->lb_len = 0;
802                 lu_buf_alloc(buf, lmv1_size);
803                 lmv1 = buf->lb_buf;
804                 if (lmv1 == NULL)
805                         RETURN(-ENOMEM);
806
807                 memcpy(buf->lb_buf, tbuf.lb_buf, tbuf.lb_len);
808         }
809
810         if (unlikely(!dt_try_as_dir(env, obj)))
811                 RETURN(-ENOTDIR);
812
813         memset(&lmv1->lmv_stripe_fids[0], 0, stripes * sizeof(struct lu_fid));
814         iops = &obj->do_index_ops->dio_it;
815         it = iops->init(env, obj, LUDA_64BITHASH);
816         if (IS_ERR(it))
817                 RETURN(PTR_ERR(it));
818
819         rc = iops->load(env, it, 0);
820         if (rc == 0)
821                 rc = iops->next(env, it);
822         else if (rc > 0)
823                 rc = 0;
824
825         while (rc == 0) {
826                 char             name[FID_LEN + 2] = "";
827                 struct lu_fid    fid;
828                 __u32            index;
829                 int              len;
830
831                 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
832                 if (rc != 0)
833                         break;
834
835                 rc = -EIO;
836
837                 fid_le_to_cpu(&fid, &ent->lde_fid);
838                 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
839                 if (ent->lde_name[0] == '.') {
840                         if (ent->lde_namelen == 1)
841                                 goto next;
842
843                         if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
844                                 goto next;
845                 }
846
847                 len = snprintf(name, sizeof(name),
848                                DFID":", PFID(&ent->lde_fid));
849                 /* The ent->lde_name is composed of ${FID}:${index} */
850                 if (ent->lde_namelen < len + 1 ||
851                     memcmp(ent->lde_name, name, len) != 0) {
852                         CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
853                                "%s: invalid shard name %.*s with the FID "DFID
854                                " for the striped directory "DFID", %s\n",
855                                lod2obd(lod)->obd_name, ent->lde_namelen,
856                                ent->lde_name, PFID(&fid),
857                                PFID(lu_object_fid(&obj->do_lu)),
858                                lod->lod_lmv_failout ? "failout" : "skip");
859
860                         if (lod->lod_lmv_failout)
861                                 break;
862
863                         goto next;
864                 }
865
866                 index = 0;
867                 do {
868                         if (ent->lde_name[len] < '0' ||
869                             ent->lde_name[len] > '9') {
870                                 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
871                                        "%s: invalid shard name %.*s with the "
872                                        "FID "DFID" for the striped directory "
873                                        DFID", %s\n",
874                                        lod2obd(lod)->obd_name, ent->lde_namelen,
875                                        ent->lde_name, PFID(&fid),
876                                        PFID(lu_object_fid(&obj->do_lu)),
877                                        lod->lod_lmv_failout ?
878                                        "failout" : "skip");
879
880                                 if (lod->lod_lmv_failout)
881                                         break;
882
883                                 goto next;
884                         }
885
886                         index = index * 10 + ent->lde_name[len++] - '0';
887                 } while (len < ent->lde_namelen);
888
889                 if (len == ent->lde_namelen) {
890                         /* Out of LMV EA range. */
891                         if (index >= stripes) {
892                                 CERROR("%s: the shard %.*s for the striped "
893                                        "directory "DFID" is out of the known "
894                                        "LMV EA range [0 - %u], failout\n",
895                                        lod2obd(lod)->obd_name, ent->lde_namelen,
896                                        ent->lde_name,
897                                        PFID(lu_object_fid(&obj->do_lu)),
898                                        stripes - 1);
899
900                                 break;
901                         }
902
903                         /* The slot has been occupied. */
904                         if (!fid_is_zero(&lmv1->lmv_stripe_fids[index])) {
905                                 struct lu_fid fid0;
906
907                                 fid_le_to_cpu(&fid0,
908                                         &lmv1->lmv_stripe_fids[index]);
909                                 CERROR("%s: both the shard "DFID" and "DFID
910                                        " for the striped directory "DFID
911                                        " claim the same LMV EA slot at the "
912                                        "index %d, failout\n",
913                                        lod2obd(lod)->obd_name,
914                                        PFID(&fid0), PFID(&fid),
915                                        PFID(lu_object_fid(&obj->do_lu)), index);
916
917                                 break;
918                         }
919
920                         /* stored as LE mode */
921                         lmv1->lmv_stripe_fids[index] = ent->lde_fid;
922
923 next:
924                         rc = iops->next(env, it);
925                 }
926         }
927
928         iops->put(env, it);
929         iops->fini(env, it);
930
931         RETURN(rc > 0 ? lmv_mds_md_size(stripes, magic) : rc);
932 }
933
934 /**
935  * Implementation of dt_object_operations::do_index_try.
936  *
937  * \see dt_object_operations::do_index_try() in the API description for details.
938  */
939 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
940                          const struct dt_index_features *feat)
941 {
942         struct lod_object       *lo = lod_dt_obj(dt);
943         struct dt_object        *next = dt_object_child(dt);
944         int                     rc;
945         ENTRY;
946
947         LASSERT(next->do_ops);
948         LASSERT(next->do_ops->do_index_try);
949
950         rc = lod_load_striping_locked(env, lo);
951         if (rc != 0)
952                 RETURN(rc);
953
954         rc = next->do_ops->do_index_try(env, next, feat);
955         if (rc != 0)
956                 RETURN(rc);
957
958         if (lo->ldo_dir_stripe_count > 0) {
959                 int i;
960
961                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
962                         if (dt_object_exists(lo->ldo_stripe[i]) == 0)
963                                 continue;
964                         rc = lo->ldo_stripe[i]->do_ops->do_index_try(env,
965                                                 lo->ldo_stripe[i], feat);
966                         if (rc != 0)
967                                 RETURN(rc);
968                 }
969                 dt->do_index_ops = &lod_striped_index_ops;
970         } else {
971                 dt->do_index_ops = &lod_index_ops;
972         }
973
974         RETURN(rc);
975 }
976
977 /**
978  * Implementation of dt_object_operations::do_read_lock.
979  *
980  * \see dt_object_operations::do_read_lock() in the API description for details.
981  */
982 static void lod_read_lock(const struct lu_env *env, struct dt_object *dt,
983                           unsigned role)
984 {
985         dt_read_lock(env, dt_object_child(dt), role);
986 }
987
988 /**
989  * Implementation of dt_object_operations::do_write_lock.
990  *
991  * \see dt_object_operations::do_write_lock() in the API description for
992  * details.
993  */
994 static void lod_write_lock(const struct lu_env *env, struct dt_object *dt,
995                            unsigned role)
996 {
997         dt_write_lock(env, dt_object_child(dt), role);
998 }
999
1000 /**
1001  * Implementation of dt_object_operations::do_read_unlock.
1002  *
1003  * \see dt_object_operations::do_read_unlock() in the API description for
1004  * details.
1005  */
1006 static void lod_read_unlock(const struct lu_env *env, struct dt_object *dt)
1007 {
1008         dt_read_unlock(env, dt_object_child(dt));
1009 }
1010
1011 /**
1012  * Implementation of dt_object_operations::do_write_unlock.
1013  *
1014  * \see dt_object_operations::do_write_unlock() in the API description for
1015  * details.
1016  */
1017 static void lod_write_unlock(const struct lu_env *env, struct dt_object *dt)
1018 {
1019         dt_write_unlock(env, dt_object_child(dt));
1020 }
1021
1022 /**
1023  * Implementation of dt_object_operations::do_write_locked.
1024  *
1025  * \see dt_object_operations::do_write_locked() in the API description for
1026  * details.
1027  */
1028 static int lod_write_locked(const struct lu_env *env, struct dt_object *dt)
1029 {
1030         return dt_write_locked(env, dt_object_child(dt));
1031 }
1032
1033 /**
1034  * Implementation of dt_object_operations::do_attr_get.
1035  *
1036  * \see dt_object_operations::do_attr_get() in the API description for details.
1037  */
1038 static int lod_attr_get(const struct lu_env *env,
1039                         struct dt_object *dt,
1040                         struct lu_attr *attr)
1041 {
1042         /* Note: for striped directory, client will merge attributes
1043          * from all of the sub-stripes see lmv_merge_attr(), and there
1044          * no MDD logic depend on directory nlink/size/time, so we can
1045          * always use master inode nlink and size for now. */
1046         return dt_attr_get(env, dt_object_child(dt), attr);
1047 }
1048
1049 int lod_obj_for_each_stripe(const struct lu_env *env, struct lod_object *lo,
1050                             struct thandle *th,
1051                             struct lod_obj_stripe_cb_data *data)
1052 {
1053         struct lod_layout_component *lod_comp;
1054         int i, j, rc;
1055         ENTRY;
1056
1057         LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
1058         for (i = 0; i < lo->ldo_comp_cnt; i++) {
1059                 lod_comp = &lo->ldo_comp_entries[i];
1060
1061                 if (lod_comp->llc_stripe == NULL)
1062                         continue;
1063
1064                 /* has stripe but not inited yet, this component has been
1065                  * declared to be created, but hasn't created yet.
1066                  */
1067                 if (!lod_comp_inited(lod_comp))
1068                         continue;
1069
1070                 if (data->locd_comp_skip_cb &&
1071                     data->locd_comp_skip_cb(env, lo, i, data))
1072                         continue;
1073
1074                 LASSERT(lod_comp->llc_stripe_count > 0);
1075                 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
1076                         struct dt_object *dt = lod_comp->llc_stripe[j];
1077
1078                         if (dt == NULL)
1079                                 continue;
1080                         rc = data->locd_stripe_cb(env, lo, dt, th, i, j, data);
1081                         if (rc != 0)
1082                                 RETURN(rc);
1083                 }
1084         }
1085         RETURN(0);
1086 }
1087
1088 static bool lod_obj_attr_set_comp_skip_cb(const struct lu_env *env,
1089                 struct lod_object *lo, int comp_idx,
1090                 struct lod_obj_stripe_cb_data *data)
1091 {
1092         struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
1093         bool skipped = false;
1094
1095         if (!(data->locd_attr->la_valid & LA_LAYOUT_VERSION))
1096                 return skipped;
1097
1098         switch (lo->ldo_flr_state) {
1099         case LCM_FL_WRITE_PENDING: {
1100                 int i;
1101
1102                 /* skip stale components */
1103                 if (lod_comp->llc_flags & LCME_FL_STALE) {
1104                         skipped = true;
1105                         break;
1106                 }
1107
1108                 /* skip valid and overlapping components, therefore any
1109                  * attempts to write overlapped components will never succeed
1110                  * because client will get EINPROGRESS. */
1111                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1112                         if (i == comp_idx)
1113                                 continue;
1114
1115                         if (lo->ldo_comp_entries[i].llc_flags & LCME_FL_STALE)
1116                                 continue;
1117
1118                         if (lu_extent_is_overlapped(&lod_comp->llc_extent,
1119                                         &lo->ldo_comp_entries[i].llc_extent)) {
1120                                 skipped = true;
1121                                 break;
1122                         }
1123                 }
1124                 break;
1125         }
1126         default:
1127                 LASSERTF(0, "impossible: %d\n", lo->ldo_flr_state);
1128         case LCM_FL_SYNC_PENDING:
1129                 break;
1130         }
1131
1132         CDEBUG(D_LAYOUT, DFID": %s to set component %x to version: %u\n",
1133                PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
1134                skipped ? "skipped" : "chose", lod_comp->llc_id,
1135                data->locd_attr->la_layout_version);
1136
1137         return skipped;
1138 }
1139
1140 static inline int
1141 lod_obj_stripe_attr_set_cb(const struct lu_env *env, struct lod_object *lo,
1142                            struct dt_object *dt, struct thandle *th,
1143                            int comp_idx, int stripe_idx,
1144                            struct lod_obj_stripe_cb_data *data)
1145 {
1146         if (data->locd_declare)
1147                 return lod_sub_declare_attr_set(env, dt, data->locd_attr, th);
1148
1149         if (data->locd_attr->la_valid & LA_LAYOUT_VERSION) {
1150                 CDEBUG(D_LAYOUT, DFID": set layout version: %u, comp_idx: %d\n",
1151                        PFID(lu_object_fid(&dt->do_lu)),
1152                        data->locd_attr->la_layout_version, comp_idx);
1153         }
1154
1155         return lod_sub_attr_set(env, dt, data->locd_attr, th);
1156 }
1157
1158 /**
1159  * Implementation of dt_object_operations::do_declare_attr_set.
1160  *
1161  * If the object is striped, then apply the changes to all the stripes.
1162  *
1163  * \see dt_object_operations::do_declare_attr_set() in the API description
1164  * for details.
1165  */
1166 static int lod_declare_attr_set(const struct lu_env *env,
1167                                 struct dt_object *dt,
1168                                 const struct lu_attr *attr,
1169                                 struct thandle *th)
1170 {
1171         struct dt_object  *next = dt_object_child(dt);
1172         struct lod_object *lo = lod_dt_obj(dt);
1173         int                rc, i;
1174         ENTRY;
1175
1176         /*
1177          * declare setattr on the local object
1178          */
1179         rc = lod_sub_declare_attr_set(env, next, attr, th);
1180         if (rc)
1181                 RETURN(rc);
1182
1183         /* osp_declare_attr_set() ignores all attributes other than
1184          * UID, GID, PROJID, and size, and osp_attr_set() ignores all
1185          * but UID, GID and PROJID. Declaration of size attr setting
1186          * happens through lod_declare_init_size(), and not through
1187          * this function. Therefore we need not load striping unless
1188          * ownership is changing.  This should save memory and (we hope)
1189          * speed up rename().
1190          */
1191         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1192                 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1193                         RETURN(rc);
1194
1195                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1196                         RETURN(0);
1197         } else {
1198                 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID | LA_MODE |
1199                                         LA_ATIME | LA_MTIME | LA_CTIME |
1200                                         LA_FLAGS)))
1201                         RETURN(rc);
1202         }
1203         /*
1204          * load striping information, notice we don't do this when object
1205          * is being initialized as we don't need this information till
1206          * few specific cases like destroy, chown
1207          */
1208         rc = lod_load_striping(env, lo);
1209         if (rc)
1210                 RETURN(rc);
1211
1212         if (!lod_obj_is_striped(dt))
1213                 RETURN(0);
1214
1215         /*
1216          * if object is striped declare changes on the stripes
1217          */
1218         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1219                 LASSERT(lo->ldo_stripe);
1220                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1221                         if (lo->ldo_stripe[i] == NULL)
1222                                 continue;
1223                         rc = lod_sub_declare_attr_set(env, lo->ldo_stripe[i],
1224                                                       attr, th);
1225                         if (rc != 0)
1226                                 RETURN(rc);
1227                 }
1228         } else {
1229                 struct lod_obj_stripe_cb_data data = { { 0 } };
1230
1231                 data.locd_attr = attr;
1232                 data.locd_declare = true;
1233                 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1234                 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1235         }
1236
1237         if (rc)
1238                 RETURN(rc);
1239
1240         if (!dt_object_exists(next) || dt_object_remote(next) ||
1241             !S_ISREG(attr->la_mode))
1242                 RETURN(0);
1243
1244         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1245                 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
1246                 RETURN(rc);
1247         }
1248
1249         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1250             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1251                 struct lod_thread_info *info = lod_env_info(env);
1252                 struct lu_buf *buf = &info->lti_buf;
1253
1254                 buf->lb_buf = info->lti_ea_store;
1255                 buf->lb_len = info->lti_ea_store_size;
1256                 rc = lod_sub_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1257                                                LU_XATTR_REPLACE, th);
1258         }
1259
1260         RETURN(rc);
1261 }
1262
1263 /**
1264  * Implementation of dt_object_operations::do_attr_set.
1265  *
1266  * If the object is striped, then apply the changes to all or subset of
1267  * the stripes depending on the object type and specific attributes.
1268  *
1269  * \see dt_object_operations::do_attr_set() in the API description for details.
1270  */
1271 static int lod_attr_set(const struct lu_env *env,
1272                         struct dt_object *dt,
1273                         const struct lu_attr *attr,
1274                         struct thandle *th)
1275 {
1276         struct dt_object        *next = dt_object_child(dt);
1277         struct lod_object       *lo = lod_dt_obj(dt);
1278         int                     rc, i;
1279         ENTRY;
1280
1281         /*
1282          * apply changes to the local object
1283          */
1284         rc = lod_sub_attr_set(env, next, attr, th);
1285         if (rc)
1286                 RETURN(rc);
1287
1288         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1289                 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1290                         RETURN(rc);
1291
1292                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1293                         RETURN(0);
1294         } else {
1295                 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE | LA_PROJID |
1296                                         LA_ATIME | LA_MTIME | LA_CTIME |
1297                                         LA_FLAGS)))
1298                         RETURN(rc);
1299         }
1300
1301         /* FIXME: a tricky case in the code path of mdd_layout_change():
1302          * the in-memory striping information has been freed in lod_xattr_set()
1303          * due to layout change. It has to load stripe here again. It only
1304          * changes flags of layout so declare_attr_set() is still accurate */
1305         rc = lod_load_striping_locked(env, lo);
1306         if (rc)
1307                 RETURN(rc);
1308
1309         if (!lod_obj_is_striped(dt))
1310                 RETURN(0);
1311
1312         /*
1313          * if object is striped, apply changes to all the stripes
1314          */
1315         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1316                 LASSERT(lo->ldo_stripe);
1317                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1318                         if (unlikely(lo->ldo_stripe[i] == NULL))
1319                                 continue;
1320
1321                         if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1322                                 continue;
1323
1324                         rc = lod_sub_attr_set(env, lo->ldo_stripe[i], attr, th);
1325                         if (rc != 0)
1326                                 break;
1327                 }
1328         } else {
1329                 struct lod_obj_stripe_cb_data data = { { 0 } };
1330
1331                 data.locd_attr = attr;
1332                 data.locd_declare = false;
1333                 data.locd_comp_skip_cb = lod_obj_attr_set_comp_skip_cb;
1334                 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1335                 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1336         }
1337
1338         if (rc)
1339                 RETURN(rc);
1340
1341         if (!dt_object_exists(next) || dt_object_remote(next) ||
1342             !S_ISREG(attr->la_mode))
1343                 RETURN(0);
1344
1345         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1346                 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
1347                 RETURN(rc);
1348         }
1349
1350         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1351                 struct lod_thread_info *info = lod_env_info(env);
1352                 struct lu_buf *buf = &info->lti_buf;
1353                 struct ost_id *oi = &info->lti_ostid;
1354                 struct lu_fid *fid = &info->lti_fid;
1355                 struct lov_mds_md_v1 *lmm;
1356                 struct lov_ost_data_v1 *objs;
1357                 __u32 magic;
1358
1359                 rc = lod_get_lov_ea(env, lo);
1360                 if (rc <= 0)
1361                         RETURN(rc);
1362
1363                 buf->lb_buf = info->lti_ea_store;
1364                 buf->lb_len = info->lti_ea_store_size;
1365                 lmm = info->lti_ea_store;
1366                 magic = le32_to_cpu(lmm->lmm_magic);
1367                 if (magic == LOV_MAGIC_COMP_V1) {
1368                         struct lov_comp_md_v1 *lcm = buf->lb_buf;
1369                         struct lov_comp_md_entry_v1 *lcme =
1370                                                 &lcm->lcm_entries[0];
1371
1372                         lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1373                         magic = le32_to_cpu(lmm->lmm_magic);
1374                 }
1375
1376                 if (magic == LOV_MAGIC_V1)
1377                         objs = &(lmm->lmm_objects[0]);
1378                 else
1379                         objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1380                 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1381                 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1382                 fid->f_oid--;
1383                 fid_to_ostid(fid, oi);
1384                 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1385
1386                 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1387                                        LU_XATTR_REPLACE, th);
1388         } else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1389                 struct lod_thread_info *info = lod_env_info(env);
1390                 struct lu_buf *buf = &info->lti_buf;
1391                 struct lov_comp_md_v1 *lcm;
1392                 struct lov_comp_md_entry_v1 *lcme;
1393
1394                 rc = lod_get_lov_ea(env, lo);
1395                 if (rc <= 0)
1396                         RETURN(rc);
1397
1398                 buf->lb_buf = info->lti_ea_store;
1399                 buf->lb_len = info->lti_ea_store_size;
1400                 lcm = buf->lb_buf;
1401                 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
1402                         RETURN(-EINVAL);
1403
1404                 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1405                 lcme = &lcm->lcm_entries[0];
1406                 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1407                 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1408
1409                 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1410                                        LU_XATTR_REPLACE, th);
1411         }
1412
1413         RETURN(rc);
1414 }
1415
1416 /**
1417  * Implementation of dt_object_operations::do_xattr_get.
1418  *
1419  * If LOV EA is requested from the root object and it's not
1420  * found, then return default striping for the filesystem.
1421  *
1422  * \see dt_object_operations::do_xattr_get() in the API description for details.
1423  */
1424 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1425                          struct lu_buf *buf, const char *name)
1426 {
1427         struct lod_thread_info *info = lod_env_info(env);
1428         struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1429         int is_root;
1430         int rc;
1431         ENTRY;
1432
1433         rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1434         if (strcmp(name, XATTR_NAME_LMV) == 0) {
1435                 struct lmv_mds_md_v1    *lmv1;
1436                 int                      rc1 = 0;
1437
1438                 if (rc > (typeof(rc))sizeof(*lmv1))
1439                         RETURN(rc);
1440
1441                 if (rc < (typeof(rc))sizeof(*lmv1))
1442                         RETURN(rc = rc > 0 ? -EINVAL : rc);
1443
1444                 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1445                         CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1446
1447                         info->lti_buf.lb_buf = info->lti_key;
1448                         info->lti_buf.lb_len = sizeof(*lmv1);
1449                         rc = dt_xattr_get(env, dt_object_child(dt),
1450                                           &info->lti_buf, name);
1451                         if (unlikely(rc != sizeof(*lmv1)))
1452                                 RETURN(rc = rc > 0 ? -EINVAL : rc);
1453
1454                         lmv1 = info->lti_buf.lb_buf;
1455                         /* The on-disk LMV EA only contains header, but the
1456                          * returned LMV EA size should contain the space for
1457                          * the FIDs of all shards of the striped directory. */
1458                         if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1459                                 rc = lmv_mds_md_size(
1460                                         le32_to_cpu(lmv1->lmv_stripe_count),
1461                                         LMV_MAGIC_V1);
1462                 } else {
1463                         rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1464                                                   buf, false);
1465                 }
1466
1467                 RETURN(rc = rc1 != 0 ? rc1 : rc);
1468         }
1469
1470         if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1471                 RETURN(rc);
1472
1473         /*
1474          * XXX: Only used by lfsck
1475          *
1476          * lod returns default striping on the real root of the device
1477          * this is like the root stores default striping for the whole
1478          * filesystem. historically we've been using a different approach
1479          * and store it in the config.
1480          */
1481         dt_root_get(env, dev->lod_child, &info->lti_fid);
1482         is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1483
1484         if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1485                 struct lov_user_md *lum = buf->lb_buf;
1486                 struct lov_desc    *desc = &dev->lod_desc;
1487
1488                 if (buf->lb_buf == NULL) {
1489                         rc = sizeof(*lum);
1490                 } else if (buf->lb_len >= sizeof(*lum)) {
1491                         lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1492                         lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1493                         lmm_oi_set_id(&lum->lmm_oi, 0);
1494                         lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1495                         lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1496                         lum->lmm_stripe_size = cpu_to_le32(
1497                                                 desc->ld_default_stripe_size);
1498                         lum->lmm_stripe_count = cpu_to_le16(
1499                                                 desc->ld_default_stripe_count);
1500                         lum->lmm_stripe_offset = cpu_to_le16(
1501                                                 desc->ld_default_stripe_offset);
1502                         rc = sizeof(*lum);
1503                 } else {
1504                         rc = -ERANGE;
1505                 }
1506         }
1507
1508         RETURN(rc);
1509 }
1510
1511 /**
1512  * Verify LVM EA.
1513  *
1514  * Checks that the magic of the stripe is sane.
1515  *
1516  * \param[in] lod       lod device
1517  * \param[in] lum       a buffer storing LMV EA to verify
1518  *
1519  * \retval              0 if the EA is sane
1520  * \retval              negative otherwise
1521  */
1522 static int lod_verify_md_striping(struct lod_device *lod,
1523                                   const struct lmv_user_md_v1 *lum)
1524 {
1525         if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1526                 CERROR("%s: invalid lmv_user_md: magic = %x, "
1527                        "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1528                        lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1529                        (int)le32_to_cpu(lum->lum_stripe_offset),
1530                        le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1531                 return -EINVAL;
1532         }
1533
1534         return 0;
1535 }
1536
1537 /**
1538  * Initialize LMV EA for a slave.
1539  *
1540  * Initialize slave's LMV EA from the master's LMV EA.
1541  *
1542  * \param[in] master_lmv        a buffer containing master's EA
1543  * \param[out] slave_lmv        a buffer where slave's EA will be stored
1544  *
1545  */
1546 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1547                                   const struct lmv_mds_md_v1 *master_lmv)
1548 {
1549         *slave_lmv = *master_lmv;
1550         slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1551 }
1552
1553 /**
1554  * Generate LMV EA.
1555  *
1556  * Generate LMV EA from the object passed as \a dt. The object must have
1557  * the stripes created and initialized.
1558  *
1559  * \param[in] env       execution environment
1560  * \param[in] dt        object
1561  * \param[out] lmv_buf  buffer storing generated LMV EA
1562  *
1563  * \retval              0 on success
1564  * \retval              negative if failed
1565  */
1566 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1567                            struct lu_buf *lmv_buf)
1568 {
1569         struct lod_thread_info  *info = lod_env_info(env);
1570         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
1571         struct lod_object       *lo = lod_dt_obj(dt);
1572         struct lmv_mds_md_v1    *lmm1;
1573         int                     stripe_count;
1574         int                     type = LU_SEQ_RANGE_ANY;
1575         int                     rc;
1576         __u32                   mdtidx;
1577         ENTRY;
1578
1579         LASSERT(lo->ldo_dir_striped != 0);
1580         LASSERT(lo->ldo_dir_stripe_count > 0);
1581         stripe_count = lo->ldo_dir_stripe_count;
1582         /* Only store the LMV EA heahder on the disk. */
1583         if (info->lti_ea_store_size < sizeof(*lmm1)) {
1584                 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1585                 if (rc != 0)
1586                         RETURN(rc);
1587         } else {
1588                 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1589         }
1590
1591         lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1592         lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1593         lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1594         lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1595         rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1596                             &mdtidx, &type);
1597         if (rc != 0)
1598                 RETURN(rc);
1599
1600         lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1601         lmv_buf->lb_buf = info->lti_ea_store;
1602         lmv_buf->lb_len = sizeof(*lmm1);
1603
1604         RETURN(rc);
1605 }
1606
1607 /**
1608  * Create in-core represenation for a striped directory.
1609  *
1610  * Parse the buffer containing LMV EA and instantiate LU objects
1611  * representing the stripe objects. The pointers to the objects are
1612  * stored in ldo_stripe field of \a lo. This function is used when
1613  * we need to access an already created object (i.e. load from a disk).
1614  *
1615  * \param[in] env       execution environment
1616  * \param[in] lo        lod object
1617  * \param[in] buf       buffer containing LMV EA
1618  *
1619  * \retval              0 on success
1620  * \retval              negative if failed
1621  */
1622 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1623                            const struct lu_buf *buf)
1624 {
1625         struct lod_thread_info  *info = lod_env_info(env);
1626         struct lod_device       *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1627         struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
1628         struct dt_object        **stripe;
1629         union lmv_mds_md        *lmm = buf->lb_buf;
1630         struct lmv_mds_md_v1    *lmv1 = &lmm->lmv_md_v1;
1631         struct lu_fid           *fid = &info->lti_fid;
1632         unsigned int            i;
1633         int                     rc = 0;
1634         ENTRY;
1635
1636         if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1637                 RETURN(0);
1638
1639         if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1640                 lo->ldo_dir_slave_stripe = 1;
1641                 RETURN(0);
1642         }
1643
1644         if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1645                 RETURN(-EINVAL);
1646
1647         if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1648                 RETURN(0);
1649
1650         LASSERT(lo->ldo_stripe == NULL);
1651         OBD_ALLOC(stripe, sizeof(stripe[0]) *
1652                   (le32_to_cpu(lmv1->lmv_stripe_count)));
1653         if (stripe == NULL)
1654                 RETURN(-ENOMEM);
1655
1656         for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1657                 struct dt_device        *tgt_dt;
1658                 struct dt_object        *dto;
1659                 int                     type = LU_SEQ_RANGE_ANY;
1660                 __u32                   idx;
1661
1662                 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1663                 if (!fid_is_sane(fid))
1664                         GOTO(out, rc = -ESTALE);
1665
1666                 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1667                 if (rc != 0)
1668                         GOTO(out, rc);
1669
1670                 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1671                         tgt_dt = lod->lod_child;
1672                 } else {
1673                         struct lod_tgt_desc     *tgt;
1674
1675                         tgt = LTD_TGT(ltd, idx);
1676                         if (tgt == NULL)
1677                                 GOTO(out, rc = -ESTALE);
1678                         tgt_dt = tgt->ltd_tgt;
1679                 }
1680
1681                 dto = dt_locate_at(env, tgt_dt, fid,
1682                                   lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1683                                   NULL);
1684                 if (IS_ERR(dto))
1685                         GOTO(out, rc = PTR_ERR(dto));
1686
1687                 stripe[i] = dto;
1688         }
1689 out:
1690         lo->ldo_stripe = stripe;
1691         lo->ldo_dir_stripe_count = le32_to_cpu(lmv1->lmv_stripe_count);
1692         lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1693         if (rc != 0)
1694                 lod_object_free_striping(env, lo);
1695
1696         RETURN(rc);
1697 }
1698
1699 /**
1700  * Declare create a striped directory.
1701  *
1702  * Declare creating a striped directory with a given stripe pattern on the
1703  * specified MDTs. A striped directory is represented as a regular directory
1704  * - an index listing all the stripes. The stripes point back to the master
1705  * object with ".." and LinkEA. The master object gets LMV EA which
1706  * identifies it as a striped directory. The function allocates FIDs
1707  * for all stripes.
1708  *
1709  * \param[in] env       execution environment
1710  * \param[in] dt        object
1711  * \param[in] attr      attributes to initialize the objects with
1712  * \param[in] dof       type of objects to be created
1713  * \param[in] th        transaction handle
1714  *
1715  * \retval              0 on success
1716  * \retval              negative if failed
1717  */
1718 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1719                                           struct dt_object *dt,
1720                                           struct lu_attr *attr,
1721                                           struct dt_object_format *dof,
1722                                           struct thandle *th)
1723 {
1724         struct lod_thread_info  *info = lod_env_info(env);
1725         struct lu_buf           lmv_buf;
1726         struct lu_buf           slave_lmv_buf;
1727         struct lmv_mds_md_v1    *lmm;
1728         struct lmv_mds_md_v1    *slave_lmm = NULL;
1729         struct dt_insert_rec    *rec = &info->lti_dt_rec;
1730         struct lod_object       *lo = lod_dt_obj(dt);
1731         int                     rc;
1732         __u32                   i;
1733         ENTRY;
1734
1735         rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1736         if (rc != 0)
1737                 GOTO(out, rc);
1738         lmm = lmv_buf.lb_buf;
1739
1740         OBD_ALLOC_PTR(slave_lmm);
1741         if (slave_lmm == NULL)
1742                 GOTO(out, rc = -ENOMEM);
1743
1744         lod_prep_slave_lmv_md(slave_lmm, lmm);
1745         slave_lmv_buf.lb_buf = slave_lmm;
1746         slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1747
1748         if (!dt_try_as_dir(env, dt_object_child(dt)))
1749                 GOTO(out, rc = -EINVAL);
1750
1751         rec->rec_type = S_IFDIR;
1752         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1753                 struct dt_object        *dto = lo->ldo_stripe[i];
1754                 char                    *stripe_name = info->lti_key;
1755                 struct lu_name          *sname;
1756                 struct linkea_data       ldata          = { NULL };
1757                 struct lu_buf           linkea_buf;
1758
1759                 rc = lod_sub_declare_create(env, dto, attr, NULL, dof, th);
1760                 if (rc != 0)
1761                         GOTO(out, rc);
1762
1763                 if (!dt_try_as_dir(env, dto))
1764                         GOTO(out, rc = -EINVAL);
1765
1766                 rc = lod_sub_declare_ref_add(env, dto, th);
1767                 if (rc != 0)
1768                         GOTO(out, rc);
1769
1770                 rec->rec_fid = lu_object_fid(&dto->do_lu);
1771                 rc = lod_sub_declare_insert(env, dto,
1772                                             (const struct dt_rec *)rec,
1773                                             (const struct dt_key *)dot, th);
1774                 if (rc != 0)
1775                         GOTO(out, rc);
1776
1777                 /* master stripe FID will be put to .. */
1778                 rec->rec_fid = lu_object_fid(&dt->do_lu);
1779                 rc = lod_sub_declare_insert(env, dto,
1780                                             (const struct dt_rec *)rec,
1781                                             (const struct dt_key *)dotdot, th);
1782                 if (rc != 0)
1783                         GOTO(out, rc);
1784
1785                 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1786                     cfs_fail_val != i) {
1787                         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1788                             cfs_fail_val == i)
1789                                 slave_lmm->lmv_master_mdt_index =
1790                                                         cpu_to_le32(i + 1);
1791                         else
1792                                 slave_lmm->lmv_master_mdt_index =
1793                                                         cpu_to_le32(i);
1794                         rc = lod_sub_declare_xattr_set(env, dto, &slave_lmv_buf,
1795                                                        XATTR_NAME_LMV, 0, th);
1796                         if (rc != 0)
1797                                 GOTO(out, rc);
1798                 }
1799
1800                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1801                     cfs_fail_val == i)
1802                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1803                                 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1804                 else
1805                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1806                                 PFID(lu_object_fid(&dto->do_lu)), i);
1807
1808                 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1809                 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1810                                       sname, lu_object_fid(&dt->do_lu));
1811                 if (rc != 0)
1812                         GOTO(out, rc);
1813
1814                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1815                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1816                 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
1817                                                XATTR_NAME_LINK, 0, th);
1818                 if (rc != 0)
1819                         GOTO(out, rc);
1820
1821                 rec->rec_fid = lu_object_fid(&dto->do_lu);
1822                 rc = lod_sub_declare_insert(env, dt_object_child(dt),
1823                                             (const struct dt_rec *)rec,
1824                                             (const struct dt_key *)stripe_name,
1825                                             th);
1826                 if (rc != 0)
1827                         GOTO(out, rc);
1828
1829                 rc = lod_sub_declare_ref_add(env, dt_object_child(dt), th);
1830                 if (rc != 0)
1831                         GOTO(out, rc);
1832         }
1833
1834         rc = lod_sub_declare_xattr_set(env, dt_object_child(dt),
1835                                        &lmv_buf, XATTR_NAME_LMV, 0, th);
1836         if (rc != 0)
1837                 GOTO(out, rc);
1838 out:
1839         if (slave_lmm != NULL)
1840                 OBD_FREE_PTR(slave_lmm);
1841
1842         RETURN(rc);
1843 }
1844
1845 static int lod_prep_md_striped_create(const struct lu_env *env,
1846                                       struct dt_object *dt,
1847                                       struct lu_attr *attr,
1848                                       const struct lmv_user_md_v1 *lum,
1849                                       struct dt_object_format *dof,
1850                                       struct thandle *th)
1851 {
1852         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
1853         struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
1854         struct lod_object       *lo = lod_dt_obj(dt);
1855         struct dt_object        **stripe;
1856         __u32                   stripe_count;
1857         int                     *idx_array;
1858         __u32                   master_index;
1859         int                     rc = 0;
1860         __u32                   i;
1861         __u32                   j;
1862         bool                    is_specific = false;
1863         ENTRY;
1864
1865         /* The lum has been verifed in lod_verify_md_striping */
1866         LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC ||
1867                 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC);
1868         LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1869
1870         stripe_count = le32_to_cpu(lum->lum_stripe_count);
1871
1872         OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1873         if (idx_array == NULL)
1874                 RETURN(-ENOMEM);
1875
1876         OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1877         if (stripe == NULL)
1878                 GOTO(out_free, rc = -ENOMEM);
1879
1880         /* Start index must be the master MDT */
1881         master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1882         idx_array[0] = master_index;
1883         if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) {
1884                 is_specific = true;
1885                 for (i = 1; i < stripe_count; i++)
1886                         idx_array[i] = le32_to_cpu(lum->lum_objects[i].lum_mds);
1887         }
1888
1889         for (i = 0; i < stripe_count; i++) {
1890                 struct lod_tgt_desc     *tgt = NULL;
1891                 struct dt_object        *dto;
1892                 struct lu_fid           fid = { 0 };
1893                 int                     idx;
1894                 struct lu_object_conf   conf = { 0 };
1895                 struct dt_device        *tgt_dt = NULL;
1896
1897                 /* Try to find next avaible target */
1898                 idx = idx_array[i];
1899                 for (j = 0; j < lod->lod_remote_mdt_count;
1900                      j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1901                         bool already_allocated = false;
1902                         __u32 k;
1903
1904                         CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
1905                                idx, lod->lod_remote_mdt_count + 1, i);
1906
1907                         if (likely(!is_specific &&
1908                                    !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
1909                                 /* check whether the idx already exists
1910                                  * in current allocated array */
1911                                 for (k = 0; k < i; k++) {
1912                                         if (idx_array[k] == idx) {
1913                                                 already_allocated = true;
1914                                                 break;
1915                                         }
1916                                 }
1917
1918                                 if (already_allocated)
1919                                         continue;
1920                         }
1921
1922                         /* Sigh, this index is not in the bitmap, let's check
1923                          * next available target */
1924                         if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) &&
1925                             idx != master_index)
1926                                 continue;
1927
1928                         if (idx == master_index) {
1929                                 /* Allocate the FID locally */
1930                                 rc = obd_fid_alloc(env, lod->lod_child_exp,
1931                                                    &fid, NULL);
1932                                 if (rc < 0)
1933                                         GOTO(out_put, rc);
1934                                 tgt_dt = lod->lod_child;
1935                                 break;
1936                         }
1937
1938                         /* check the status of the OSP */
1939                         tgt = LTD_TGT(ltd, idx);
1940                         if (tgt == NULL)
1941                                 continue;
1942
1943                         tgt_dt = tgt->ltd_tgt;
1944                         rc = dt_statfs(env, tgt_dt, NULL);
1945                         if (rc) {
1946                                 /* this OSP doesn't feel well */
1947                                 rc = 0;
1948                                 continue;
1949                         }
1950
1951                         rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1952                         if (rc < 0) {
1953                                 rc = 0;
1954                                 continue;
1955                         }
1956
1957                         break;
1958                 }
1959
1960                 /* Can not allocate more stripes */
1961                 if (j == lod->lod_remote_mdt_count) {
1962                         CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1963                                lod2obd(lod)->obd_name, stripe_count, i);
1964                         break;
1965                 }
1966
1967                 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
1968                        idx, i, PFID(&fid));
1969                 idx_array[i] = idx;
1970                 /* Set the start index for next stripe allocation */
1971                 if (!is_specific && i < stripe_count - 1)
1972                         idx_array[i + 1] = (idx + 1) %
1973                                            (lod->lod_remote_mdt_count + 1);
1974                 /* tgt_dt and fid must be ready after search avaible OSP
1975                  * in the above loop */
1976                 LASSERT(tgt_dt != NULL);
1977                 LASSERT(fid_is_sane(&fid));
1978                 conf.loc_flags = LOC_F_NEW;
1979                 dto = dt_locate_at(env, tgt_dt, &fid,
1980                                    dt->do_lu.lo_dev->ld_site->ls_top_dev,
1981                                    &conf);
1982                 if (IS_ERR(dto))
1983                         GOTO(out_put, rc = PTR_ERR(dto));
1984                 stripe[i] = dto;
1985         }
1986
1987         lo->ldo_dir_stripe_loaded = 1;
1988         lo->ldo_dir_striped = 1;
1989         lo->ldo_stripe = stripe;
1990         lo->ldo_dir_stripe_count = i;
1991         lo->ldo_dir_stripes_allocated = stripe_count;
1992
1993         if (lo->ldo_dir_stripe_count == 0)
1994                 GOTO(out_put, rc = -ENOSPC);
1995
1996         rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
1997         if (rc != 0)
1998                 GOTO(out_put, rc);
1999
2000 out_put:
2001         if (rc < 0) {
2002                 for (i = 0; i < stripe_count; i++)
2003                         if (stripe[i] != NULL)
2004                                 dt_object_put(env, stripe[i]);
2005                 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
2006                 lo->ldo_dir_stripe_count = 0;
2007                 lo->ldo_dir_stripes_allocated = 0;
2008                 lo->ldo_stripe = NULL;
2009         }
2010
2011 out_free:
2012         OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
2013
2014         RETURN(rc);
2015 }
2016
2017 /**
2018  * Declare create striped md object.
2019  *
2020  * The function declares intention to create a striped directory. This is a
2021  * wrapper for lod_prep_md_striped_create(). The only additional functionality
2022  * is to verify pattern \a lum_buf is good. Check that function for the details.
2023  *
2024  * \param[in] env       execution environment
2025  * \param[in] dt        object
2026  * \param[in] attr      attributes to initialize the objects with
2027  * \param[in] lum_buf   a pattern specifying the number of stripes and
2028  *                      MDT to start from
2029  * \param[in] dof       type of objects to be created
2030  * \param[in] th        transaction handle
2031  *
2032  * \retval              0 on success
2033  * \retval              negative if failed
2034  *
2035  */
2036 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
2037                                      struct dt_object *dt,
2038                                      struct lu_attr *attr,
2039                                      const struct lu_buf *lum_buf,
2040                                      struct dt_object_format *dof,
2041                                      struct thandle *th)
2042 {
2043         struct lod_object       *lo = lod_dt_obj(dt);
2044         struct lmv_user_md_v1   *lum = lum_buf->lb_buf;
2045         int                     rc;
2046         ENTRY;
2047
2048         LASSERT(lum != NULL);
2049
2050         CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
2051                le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
2052                (int)le32_to_cpu(lum->lum_stripe_offset));
2053
2054         if (lo->ldo_dir_stripe_count == 0)
2055                 GOTO(out, rc = 0);
2056
2057         /* prepare dir striped objects */
2058         rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2059         if (rc != 0) {
2060                 /* failed to create striping, let's reset
2061                  * config so that others don't get confused */
2062                 lod_object_free_striping(env, lo);
2063                 GOTO(out, rc);
2064         }
2065 out:
2066         RETURN(rc);
2067 }
2068
2069 /**
2070  * Implementation of dt_object_operations::do_declare_xattr_set.
2071  *
2072  * Used with regular (non-striped) objects. Basically it
2073  * initializes the striping information and applies the
2074  * change to all the stripes.
2075  *
2076  * \see dt_object_operations::do_declare_xattr_set() in the API description
2077  * for details.
2078  */
2079 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2080                                      struct dt_object *dt,
2081                                      const struct lu_buf *buf,
2082                                      const char *name, int fl,
2083                                      struct thandle *th)
2084 {
2085         struct dt_object        *next = dt_object_child(dt);
2086         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2087         struct lod_object       *lo = lod_dt_obj(dt);
2088         int                     i;
2089         int                     rc;
2090         ENTRY;
2091
2092         if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2093                 struct lmv_user_md_v1 *lum;
2094
2095                 LASSERT(buf != NULL && buf->lb_buf != NULL);
2096                 lum = buf->lb_buf;
2097                 rc = lod_verify_md_striping(d, lum);
2098                 if (rc != 0)
2099                         RETURN(rc);
2100         } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2101                 rc = lod_verify_striping(d, lo, buf, false);
2102                 if (rc != 0)
2103                         RETURN(rc);
2104         }
2105
2106         rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2107         if (rc != 0)
2108                 RETURN(rc);
2109
2110         /* Note: Do not set LinkEA on sub-stripes, otherwise
2111          * it will confuse the fid2path process(see mdt_path_current()).
2112          * The linkEA between master and sub-stripes is set in
2113          * lod_xattr_set_lmv(). */
2114         if (strcmp(name, XATTR_NAME_LINK) == 0)
2115                 RETURN(0);
2116
2117         /* set xattr to each stripes, if needed */
2118         rc = lod_load_striping(env, lo);
2119         if (rc != 0)
2120                 RETURN(rc);
2121
2122         if (lo->ldo_dir_stripe_count == 0)
2123                 RETURN(0);
2124
2125         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2126                 LASSERT(lo->ldo_stripe[i]);
2127
2128                 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2129                                                buf, name, fl, th);
2130                 if (rc != 0)
2131                         break;
2132         }
2133
2134         RETURN(rc);
2135 }
2136
2137 static int
2138 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2139                                      struct lod_object *lo,
2140                                      struct dt_object *dt, struct thandle *th,
2141                                      int comp_idx, int stripe_idx,
2142                                      struct lod_obj_stripe_cb_data *data)
2143 {
2144         struct lod_thread_info *info = lod_env_info(env);
2145         struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
2146         struct filter_fid *ff = &info->lti_ff;
2147         struct lu_buf *buf = &info->lti_buf;
2148         int rc;
2149
2150         buf->lb_buf = ff;
2151         buf->lb_len = sizeof(*ff);
2152         rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2153         if (rc < 0) {
2154                 if (rc == -ENODATA)
2155                         return 0;
2156                 return rc;
2157         }
2158
2159         filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
2160         if (lu_fid_eq(lu_object_fid(&lo->ldo_obj.do_lu), &ff->ff_parent) &&
2161             ff->ff_layout.ol_comp_id == comp->llc_id)
2162                 return 0;
2163
2164         /* rewrite filter_fid */
2165         memset(ff, 0, sizeof(*ff));
2166         ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2167         ff->ff_parent.f_ver = stripe_idx;
2168         ff->ff_layout.ol_stripe_size = comp->llc_stripe_size;
2169         ff->ff_layout.ol_stripe_count = comp->llc_stripe_count;
2170         ff->ff_layout.ol_comp_id = comp->llc_id;
2171         ff->ff_layout.ol_comp_start = comp->llc_extent.e_start;
2172         ff->ff_layout.ol_comp_end = comp->llc_extent.e_end;
2173         filter_fid_cpu_to_le(ff, ff, sizeof(*ff));
2174
2175         if (data->locd_declare)
2176                 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2177                                                LU_XATTR_REPLACE, th);
2178         else
2179                 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2180                                        LU_XATTR_REPLACE, th);
2181
2182         return rc;
2183 }
2184
2185 /**
2186  * Reset parent FID on OST object
2187  *
2188  * Replace parent FID with @dt object FID, which is only called during migration
2189  * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2190  * the FID is changed.
2191  *
2192  * \param[in] env execution environment
2193  * \param[in] dt dt_object whose stripes's parent FID will be reset
2194  * \parem[in] th thandle
2195  * \param[in] declare if it is declare
2196  *
2197  * \retval      0 if reset succeeds
2198  * \retval      negative errno if reset fails
2199  */
2200 static int lod_replace_parent_fid(const struct lu_env *env,
2201                                   struct dt_object *dt,
2202                                   struct thandle *th, bool declare)
2203 {
2204         struct lod_object *lo = lod_dt_obj(dt);
2205         struct lod_thread_info  *info = lod_env_info(env);
2206         struct lu_buf *buf = &info->lti_buf;
2207         struct filter_fid *ff;
2208         struct lod_obj_stripe_cb_data data = { { 0 } };
2209         int rc;
2210         ENTRY;
2211
2212         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2213
2214         /* set xattr to each stripes, if needed */
2215         rc = lod_load_striping(env, lo);
2216         if (rc != 0)
2217                 RETURN(rc);
2218
2219         if (!lod_obj_is_striped(dt))
2220                 RETURN(0);
2221
2222         if (info->lti_ea_store_size < sizeof(*ff)) {
2223                 rc = lod_ea_store_resize(info, sizeof(*ff));
2224                 if (rc != 0)
2225                         RETURN(rc);
2226         }
2227
2228         buf->lb_buf = info->lti_ea_store;
2229         buf->lb_len = info->lti_ea_store_size;
2230
2231         data.locd_declare = declare;
2232         data.locd_stripe_cb = lod_obj_stripe_replace_parent_fid_cb;
2233         rc = lod_obj_for_each_stripe(env, lo, th, &data);
2234
2235         RETURN(rc);
2236 }
2237
2238 inline __u16 lod_comp_entry_stripe_count(struct lod_object *lo,
2239                                          struct lod_layout_component *entry,
2240                                          bool is_dir)
2241 {
2242         struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2243
2244         if (is_dir)
2245                 return  0;
2246         else if (lod_comp_inited(entry))
2247                 return entry->llc_stripe_count;
2248         else if ((__u16)-1 == entry->llc_stripe_count)
2249                 return lod->lod_desc.ld_tgt_count;
2250         else
2251                 return lod_get_stripe_count(lod, lo, entry->llc_stripe_count);
2252 }
2253
2254 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2255 {
2256         int magic, size = 0, i;
2257         struct lod_layout_component *comp_entries;
2258         __u16 comp_cnt;
2259         bool is_composite;
2260
2261         if (is_dir) {
2262                 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2263                 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2264                 is_composite =
2265                         lo->ldo_def_striping->lds_def_striping_is_composite;
2266         } else {
2267                 comp_cnt = lo->ldo_comp_cnt;
2268                 comp_entries = lo->ldo_comp_entries;
2269                 is_composite = lo->ldo_is_composite;
2270         }
2271
2272
2273         LASSERT(comp_cnt != 0 && comp_entries != NULL);
2274         if (is_composite) {
2275                 size = sizeof(struct lov_comp_md_v1) +
2276                        sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2277                 LASSERT(size % sizeof(__u64) == 0);
2278         }
2279
2280         for (i = 0; i < comp_cnt; i++) {
2281                 __u16 stripe_count;
2282
2283                 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2284                 stripe_count = lod_comp_entry_stripe_count(lo, &comp_entries[i],
2285                                                            is_dir);
2286                 if (!is_dir && is_composite)
2287                         lod_comp_shrink_stripe_count(&comp_entries[i],
2288                                                      &stripe_count);
2289
2290                 size += lov_user_md_size(stripe_count, magic);
2291                 LASSERT(size % sizeof(__u64) == 0);
2292         }
2293         return size;
2294 }
2295
2296 /**
2297  * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2298  * the xattr value is binary lov_comp_md_v1 which contains component(s)
2299  * to be added.
2300   *
2301  * \param[in] env       execution environment
2302  * \param[in] dt        dt_object to add components on
2303  * \param[in] buf       buffer contains components to be added
2304  * \parem[in] th        thandle
2305  *
2306  * \retval      0 on success
2307  * \retval      negative errno on failure
2308  */
2309 static int lod_declare_layout_add(const struct lu_env *env,
2310                                   struct dt_object *dt,
2311                                   const struct lu_buf *buf,
2312                                   struct thandle *th)
2313 {
2314         struct lod_thread_info  *info = lod_env_info(env);
2315         struct lod_layout_component *comp_array, *lod_comp, *old_array;
2316         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2317         struct dt_object *next = dt_object_child(dt);
2318         struct lov_desc         *desc = &d->lod_desc;
2319         struct lod_object       *lo = lod_dt_obj(dt);
2320         struct lov_user_md_v3   *v3;
2321         struct lov_comp_md_v1   *comp_v1 = buf->lb_buf;
2322         __u32   magic;
2323         int     i, rc, array_cnt, old_array_cnt;
2324         ENTRY;
2325
2326         LASSERT(lo->ldo_is_composite);
2327
2328         if (lo->ldo_flr_state != LCM_FL_NOT_FLR)
2329                 RETURN(-EBUSY);
2330
2331         rc = lod_verify_striping(d, lo, buf, false);
2332         if (rc != 0)
2333                 RETURN(rc);
2334
2335         magic = comp_v1->lcm_magic;
2336         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2337                 lustre_swab_lov_comp_md_v1(comp_v1);
2338                 magic = comp_v1->lcm_magic;
2339         }
2340
2341         if (magic != LOV_USER_MAGIC_COMP_V1)
2342                 RETURN(-EINVAL);
2343
2344         array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2345         OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2346         if (comp_array == NULL)
2347                 RETURN(-ENOMEM);
2348
2349         memcpy(comp_array, lo->ldo_comp_entries,
2350                sizeof(*comp_array) * lo->ldo_comp_cnt);
2351
2352         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2353                 struct lov_user_md_v1 *v1;
2354                 struct lu_extent *ext;
2355
2356                 v1 = (struct lov_user_md *)((char *)comp_v1 +
2357                                 comp_v1->lcm_entries[i].lcme_offset);
2358                 ext = &comp_v1->lcm_entries[i].lcme_extent;
2359
2360                 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2361                 lod_comp->llc_extent.e_start = ext->e_start;
2362                 lod_comp->llc_extent.e_end = ext->e_end;
2363                 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2364                 lod_comp->llc_flags = comp_v1->lcm_entries[i].lcme_flags;
2365
2366                 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2367                 if (!lod_comp->llc_stripe_count ||
2368                     lod_comp->llc_stripe_count == (__u16)-1)
2369                         lod_comp->llc_stripe_count =
2370                                 desc->ld_default_stripe_count;
2371                 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2372                 if (!lod_comp->llc_stripe_size)
2373                         lod_comp->llc_stripe_size =
2374                                 desc->ld_default_stripe_size;
2375
2376                 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2377                         v3 = (struct lov_user_md_v3 *) v1;
2378                         if (v3->lmm_pool_name[0] != '\0') {
2379                                 rc = lod_set_pool(&lod_comp->llc_pool,
2380                                                   v3->lmm_pool_name);
2381                                 if (rc)
2382                                         GOTO(error, rc);
2383                         }
2384                 }
2385         }
2386
2387         old_array = lo->ldo_comp_entries;
2388         old_array_cnt = lo->ldo_comp_cnt;
2389
2390         lo->ldo_comp_entries = comp_array;
2391         lo->ldo_comp_cnt = array_cnt;
2392
2393         /* No need to increase layout generation here, it will be increased
2394          * later when generating component ID for the new components */
2395
2396         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2397         rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2398                                               XATTR_NAME_LOV, 0, th);
2399         if (rc) {
2400                 lo->ldo_comp_entries = old_array;
2401                 lo->ldo_comp_cnt = old_array_cnt;
2402                 GOTO(error, rc);
2403         }
2404
2405         OBD_FREE(old_array, sizeof(*lod_comp) * old_array_cnt);
2406
2407         LASSERT(lo->ldo_mirror_count == 1);
2408         lo->ldo_mirrors[0].lme_end = array_cnt - 1;
2409
2410         RETURN(0);
2411
2412 error:
2413         for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2414                 lod_comp = &comp_array[i];
2415                 if (lod_comp->llc_pool != NULL) {
2416                         OBD_FREE(lod_comp->llc_pool,
2417                                  strlen(lod_comp->llc_pool) + 1);
2418                         lod_comp->llc_pool = NULL;
2419                 }
2420         }
2421         OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2422         RETURN(rc);
2423 }
2424
2425 /**
2426  * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2427  * the '$field' can only be 'flags' now. The xattr value is binary
2428  * lov_comp_md_v1 which contains the component ID(s) and the value of
2429  * the field to be modified.
2430  *
2431  * \param[in] env       execution environment
2432  * \param[in] dt        dt_object to be modified
2433  * \param[in] op        operation string, like "set.flags"
2434  * \param[in] buf       buffer contains components to be set
2435  * \parem[in] th        thandle
2436  *
2437  * \retval      0 on success
2438  * \retval      negative errno on failure
2439  */
2440 static int lod_declare_layout_set(const struct lu_env *env,
2441                                   struct dt_object *dt,
2442                                   char *op, const struct lu_buf *buf,
2443                                   struct thandle *th)
2444 {
2445         struct lod_layout_component     *lod_comp;
2446         struct lod_thread_info  *info = lod_env_info(env);
2447         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2448         struct lod_object       *lo = lod_dt_obj(dt);
2449         struct lov_comp_md_v1   *comp_v1 = buf->lb_buf;
2450         __u32   magic, id;
2451         int     i, j, rc;
2452         bool    changed = false;
2453         ENTRY;
2454
2455         if (strcmp(op, "set.flags") != 0) {
2456                 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2457                        lod2obd(d)->obd_name, op);
2458                 RETURN(-ENOTSUPP);
2459         }
2460
2461         magic = comp_v1->lcm_magic;
2462         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2463                 lustre_swab_lov_comp_md_v1(comp_v1);
2464                 magic = comp_v1->lcm_magic;
2465         }
2466
2467         if (magic != LOV_USER_MAGIC_COMP_V1)
2468                 RETURN(-EINVAL);
2469
2470         if (comp_v1->lcm_entry_count == 0) {
2471                 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2472                        lod2obd(d)->obd_name);
2473                 RETURN(-EINVAL);
2474         }
2475
2476         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2477                 id = comp_v1->lcm_entries[i].lcme_id;
2478
2479                 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2480                         lod_comp = &lo->ldo_comp_entries[j];
2481                         if (id == lod_comp->llc_id || id == LCME_ID_ALL) {
2482                                 lod_comp->llc_flags =
2483                                         comp_v1->lcm_entries[i].lcme_flags;
2484                                 changed = true;
2485                         }
2486                 }
2487         }
2488
2489         if (!changed) {
2490                 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2491                        lod2obd(d)->obd_name);
2492                 RETURN(-EINVAL);
2493         }
2494
2495         lod_obj_inc_layout_gen(lo);
2496
2497         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2498         rc = lod_sub_declare_xattr_set(env, dt, &info->lti_buf,
2499                                        XATTR_NAME_LOV, 0, th);
2500         RETURN(rc);
2501 }
2502
2503 /**
2504  * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2505  * and the xattr value is a unique component ID or a special lcme_id.
2506  *
2507  * \param[in] env       execution environment
2508  * \param[in] dt        dt_object to be operated on
2509  * \param[in] buf       buffer contains component ID or lcme_id
2510  * \parem[in] th        thandle
2511  *
2512  * \retval      0 on success
2513  * \retval      negative errno on failure
2514  */
2515 static int lod_declare_layout_del(const struct lu_env *env,
2516                                   struct dt_object *dt,
2517                                   const struct lu_buf *buf,
2518                                   struct thandle *th)
2519 {
2520         struct lod_thread_info  *info = lod_env_info(env);
2521         struct dt_object *next = dt_object_child(dt);
2522         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2523         struct lod_object *lo = lod_dt_obj(dt);
2524         struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2525         struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2526         __u32 magic, id, flags, neg_flags = 0;
2527         int rc, i, j, left;
2528         ENTRY;
2529
2530         LASSERT(lo->ldo_is_composite);
2531
2532         if (lo->ldo_flr_state != LCM_FL_NOT_FLR)
2533                 RETURN(-EBUSY);
2534
2535         magic = comp_v1->lcm_magic;
2536         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2537                 lustre_swab_lov_comp_md_v1(comp_v1);
2538                 magic = comp_v1->lcm_magic;
2539         }
2540
2541         if (magic != LOV_USER_MAGIC_COMP_V1)
2542                 RETURN(-EINVAL);
2543
2544         id = comp_v1->lcm_entries[0].lcme_id;
2545         flags = comp_v1->lcm_entries[0].lcme_flags;
2546
2547         if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
2548                 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
2549                        lod2obd(d)->obd_name, id, flags);
2550                 RETURN(-EINVAL);
2551         }
2552
2553         if (id != LCME_ID_INVAL && flags != 0) {
2554                 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
2555                        lod2obd(d)->obd_name);
2556                 RETURN(-EINVAL);
2557         }
2558
2559         if (flags & LCME_FL_NEG) {
2560                 neg_flags = flags & ~LCME_FL_NEG;
2561                 flags = 0;
2562         }
2563
2564         left = lo->ldo_comp_cnt;
2565         if (left <= 0)
2566                 RETURN(-EINVAL);
2567
2568         for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
2569                 struct lod_layout_component *lod_comp;
2570
2571                 lod_comp = &lo->ldo_comp_entries[i];
2572
2573                 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
2574                         continue;
2575                 else if (flags && !(flags & lod_comp->llc_flags))
2576                         continue;
2577                 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
2578                         continue;
2579
2580                 if (left != (i + 1)) {
2581                         CDEBUG(D_LAYOUT, "%s: this deletion will create "
2582                                "a hole.\n", lod2obd(d)->obd_name);
2583                         RETURN(-EINVAL);
2584                 }
2585                 left--;
2586
2587                 /* Mark the component as deleted */
2588                 lod_comp->llc_id = LCME_ID_INVAL;
2589
2590                 /* Not instantiated component */
2591                 if (lod_comp->llc_stripe == NULL)
2592                         continue;
2593
2594                 LASSERT(lod_comp->llc_stripe_count > 0);
2595                 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
2596                         struct dt_object *obj = lod_comp->llc_stripe[j];
2597
2598                         if (obj == NULL)
2599                                 continue;
2600                         rc = lod_sub_declare_destroy(env, obj, th);
2601                         if (rc)
2602                                 RETURN(rc);
2603                 }
2604         }
2605
2606         LASSERTF(left >= 0, "left = %d\n", left);
2607         if (left == lo->ldo_comp_cnt) {
2608                 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
2609                        lod2obd(d)->obd_name, id);
2610                 RETURN(-EINVAL);
2611         }
2612
2613         memset(attr, 0, sizeof(*attr));
2614         attr->la_valid = LA_SIZE;
2615         rc = lod_sub_declare_attr_set(env, next, attr, th);
2616         if (rc)
2617                 RETURN(rc);
2618
2619         if (left > 0) {
2620                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2621                 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2622                                                XATTR_NAME_LOV, 0, th);
2623         } else {
2624                 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
2625         }
2626
2627         RETURN(rc);
2628 }
2629
2630 /**
2631  * Declare layout add/set/del operations issued by special xattr names:
2632  *
2633  * XATTR_LUSTRE_LOV.add         add component(s) to existing file
2634  * XATTR_LUSTRE_LOV.del         delete component(s) from existing file
2635  * XATTR_LUSTRE_LOV.set.$field  set specified field of certain component(s)
2636  *
2637  * \param[in] env       execution environment
2638  * \param[in] dt        object
2639  * \param[in] name      name of xattr
2640  * \param[in] buf       lu_buf contains xattr value
2641  * \param[in] th        transaction handle
2642  *
2643  * \retval              0 on success
2644  * \retval              negative if failed
2645  */
2646 static int lod_declare_modify_layout(const struct lu_env *env,
2647                                      struct dt_object *dt,
2648                                      const char *name,
2649                                      const struct lu_buf *buf,
2650                                      struct thandle *th)
2651 {
2652         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2653         struct lod_object *lo = lod_dt_obj(dt);
2654         struct dt_object *next = dt_object_child(&lo->ldo_obj);
2655         char *op;
2656         int rc, len = strlen(XATTR_LUSTRE_LOV);
2657         ENTRY;
2658
2659         LASSERT(dt_object_exists(dt));
2660
2661         if (strlen(name) <= len || name[len] != '.') {
2662                 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
2663                        lod2obd(d)->obd_name, name);
2664                 RETURN(-EINVAL);
2665         }
2666         len++;
2667
2668         dt_write_lock(env, next, 0);
2669         rc = lod_load_striping_locked(env, lo);
2670         if (rc)
2671                 GOTO(unlock, rc);
2672
2673         /* the layout to be modified must be a composite layout */
2674         if (!lo->ldo_is_composite) {
2675                 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
2676                        lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
2677                 GOTO(unlock, rc = -EINVAL);
2678         }
2679
2680         op = (char *)name + len;
2681         if (strcmp(op, "add") == 0) {
2682                 rc = lod_declare_layout_add(env, dt, buf, th);
2683         } else if (strcmp(op, "del") == 0) {
2684                 rc = lod_declare_layout_del(env, dt, buf, th);
2685         } else if (strncmp(op, "set", strlen("set")) == 0) {
2686                 rc = lod_declare_layout_set(env, dt, op, buf, th);
2687         } else  {
2688                 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
2689                        lod2obd(d)->obd_name, name);
2690                 GOTO(unlock, rc = -ENOTSUPP);
2691         }
2692 unlock:
2693         if (rc)
2694                 lod_object_free_striping(env, lo);
2695         dt_write_unlock(env, next);
2696
2697         RETURN(rc);
2698 }
2699
2700 /**
2701  * Convert a plain file lov_mds_md to a composite layout.
2702  *
2703  * \param[in,out] info  the thread info::lti_ea_store buffer contains little
2704  *                      endian plain file layout
2705  *
2706  * \retval              0 on success, <0 on failure
2707  */
2708 static int lod_layout_convert(struct lod_thread_info *info)
2709 {
2710         struct lov_mds_md *lmm = info->lti_ea_store;
2711         struct lov_mds_md *lmm_save;
2712         struct lov_comp_md_v1 *lcm;
2713         struct lov_comp_md_entry_v1 *lcme;
2714         size_t size;
2715         __u32 blob_size;
2716         int rc = 0;
2717         ENTRY;
2718
2719         /* realloc buffer to a composite layout which contains one component */
2720         blob_size = lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
2721                                     le32_to_cpu(lmm->lmm_magic));
2722         size = sizeof(*lcm) + sizeof(*lcme) + blob_size;
2723
2724         OBD_ALLOC_LARGE(lmm_save, blob_size);
2725         if (!lmm_save)
2726                 GOTO(out, rc = -ENOMEM);
2727
2728         memcpy(lmm_save, lmm, blob_size);
2729
2730         if (info->lti_ea_store_size < size) {
2731                 rc = lod_ea_store_resize(info, size);
2732                 if (rc)
2733                         GOTO(out, rc);
2734         }
2735
2736         lcm = info->lti_ea_store;
2737         lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
2738         lcm->lcm_size = cpu_to_le32(size);
2739         lcm->lcm_layout_gen = cpu_to_le32(le16_to_cpu(
2740                                                 lmm_save->lmm_layout_gen));
2741         lcm->lcm_flags = cpu_to_le16(LCM_FL_NOT_FLR);
2742         lcm->lcm_entry_count = cpu_to_le16(1);
2743         lcm->lcm_mirror_count = 0;
2744
2745         lcme = &lcm->lcm_entries[0];
2746         lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
2747         lcme->lcme_extent.e_start = 0;
2748         lcme->lcme_extent.e_end = cpu_to_le64(OBD_OBJECT_EOF);
2749         lcme->lcme_offset = cpu_to_le32(sizeof(*lcm) + sizeof(*lcme));
2750         lcme->lcme_size = cpu_to_le32(blob_size);
2751
2752         memcpy((char *)lcm + lcme->lcme_offset, (char *)lmm_save, blob_size);
2753
2754         EXIT;
2755 out:
2756         if (lmm_save)
2757                 OBD_FREE_LARGE(lmm_save, blob_size);
2758         return rc;
2759 }
2760
2761 /**
2762  * Merge layouts to form a mirrored file.
2763  */
2764 static int lod_declare_layout_merge(const struct lu_env *env,
2765                 struct dt_object *dt, const struct lu_buf *mbuf,
2766                 struct thandle *th)
2767 {
2768         struct lod_thread_info  *info = lod_env_info(env);
2769         struct lu_buf           *buf = &info->lti_buf;
2770         struct lod_object       *lo = lod_dt_obj(dt);
2771         struct lov_comp_md_v1   *lcm;
2772         struct lov_comp_md_v1   *cur_lcm;
2773         struct lov_comp_md_v1   *merge_lcm;
2774         struct lov_comp_md_entry_v1     *lcme;
2775         size_t size = 0;
2776         size_t offset;
2777         __u16 cur_entry_count;
2778         __u16 merge_entry_count;
2779         __u32 id = 0;
2780         __u16 mirror_id = 0;
2781         __u32 mirror_count;
2782         int     rc, i;
2783         ENTRY;
2784
2785         merge_lcm = mbuf->lb_buf;
2786         if (mbuf->lb_len < sizeof(*merge_lcm))
2787                 RETURN(-EINVAL);
2788
2789         /* must be an existing layout from disk */
2790         if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
2791                 RETURN(-EINVAL);
2792
2793         merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
2794
2795         /* do not allow to merge two mirrored files */
2796         if (le16_to_cpu(merge_lcm->lcm_mirror_count))
2797                 RETURN(-EBUSY);
2798
2799         /* verify the target buffer */
2800         rc = lod_get_lov_ea(env, lo);
2801         if (rc <= 0)
2802                 RETURN(rc ? : -ENODATA);
2803
2804         cur_lcm = info->lti_ea_store;
2805         switch (le32_to_cpu(cur_lcm->lcm_magic)) {
2806         case LOV_MAGIC_V1:
2807         case LOV_MAGIC_V3:
2808                 rc = lod_layout_convert(info);
2809                 break;
2810         case LOV_MAGIC_COMP_V1:
2811                 rc = 0;
2812                 break;
2813         default:
2814                 rc = -EINVAL;
2815         }
2816         if (rc)
2817                 RETURN(rc);
2818
2819         /* info->lti_ea_store could be reallocated in lod_layout_convert() */
2820         cur_lcm = info->lti_ea_store;
2821         cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
2822
2823         /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
2824         mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
2825         if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
2826                 RETURN(-ERANGE);
2827
2828         /* size of new layout */
2829         size = le32_to_cpu(cur_lcm->lcm_size) +
2830                le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
2831
2832         memset(buf, 0, sizeof(*buf));
2833         lu_buf_alloc(buf, size);
2834         if (buf->lb_buf == NULL)
2835                 RETURN(-ENOMEM);
2836
2837         lcm = buf->lb_buf;
2838         memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
2839
2840         offset = sizeof(*lcm) +
2841                  sizeof(*lcme) * (cur_entry_count + merge_entry_count);
2842         for (i = 0; i < cur_entry_count; i++) {
2843                 struct lov_comp_md_entry_v1 *cur_lcme;
2844
2845                 lcme = &lcm->lcm_entries[i];
2846                 cur_lcme = &cur_lcm->lcm_entries[i];
2847
2848                 lcme->lcme_offset = cpu_to_le32(offset);
2849                 memcpy((char *)lcm + offset,
2850                        (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
2851                        le32_to_cpu(lcme->lcme_size));
2852
2853                 offset += le32_to_cpu(lcme->lcme_size);
2854
2855                 if (mirror_count == 1) {
2856                         /* new mirrored file, create new mirror ID */
2857                         id = pflr_id(1, i + 1);
2858                         lcme->lcme_id = cpu_to_le32(id);
2859                 }
2860
2861                 id = MAX(le32_to_cpu(lcme->lcme_id), id);
2862         }
2863
2864         mirror_id = mirror_id_of(id) + 1;
2865         for (i = 0; i < merge_entry_count; i++) {
2866                 struct lov_comp_md_entry_v1 *merge_lcme;
2867
2868                 merge_lcme = &merge_lcm->lcm_entries[i];
2869                 lcme = &lcm->lcm_entries[cur_entry_count + i];
2870
2871                 *lcme = *merge_lcme;
2872                 lcme->lcme_offset = cpu_to_le32(offset);
2873
2874                 id = pflr_id(mirror_id, i + 1);
2875                 lcme->lcme_id = cpu_to_le32(id);
2876
2877                 memcpy((char *)lcm + offset,
2878                        (char *)merge_lcm + le32_to_cpu(merge_lcme->lcme_offset),
2879                        le32_to_cpu(lcme->lcme_size));
2880
2881                 offset += le32_to_cpu(lcme->lcme_size);
2882         }
2883
2884         /* fixup layout information */
2885         lod_obj_inc_layout_gen(lo);
2886         lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
2887         lcm->lcm_size = cpu_to_le32(size);
2888         lcm->lcm_entry_count = cpu_to_le16(cur_entry_count + merge_entry_count);
2889         lcm->lcm_mirror_count = cpu_to_le16(mirror_count);
2890         if ((le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK) == LCM_FL_NOT_FLR)
2891                 lcm->lcm_flags = cpu_to_le32(LCM_FL_RDONLY);
2892
2893         LASSERT(dt_write_locked(env, dt_object_child(dt)));
2894         lod_object_free_striping(env, lo);
2895         rc = lod_parse_striping(env, lo, buf);
2896         if (rc)
2897                 GOTO(out, rc);
2898
2899         rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), buf,
2900                                         XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
2901
2902 out:
2903         lu_buf_free(buf);
2904         RETURN(rc);
2905 }
2906
2907 /**
2908  * Implementation of dt_object_operations::do_declare_xattr_set.
2909  *
2910  * \see dt_object_operations::do_declare_xattr_set() in the API description
2911  * for details.
2912  *
2913  * the extension to the API:
2914  *   - declaring LOVEA requests striping creation
2915  *   - LU_XATTR_REPLACE means layout swap
2916  */
2917 static int lod_declare_xattr_set(const struct lu_env *env,
2918                                  struct dt_object *dt,
2919                                  const struct lu_buf *buf,
2920                                  const char *name, int fl,
2921                                  struct thandle *th)
2922 {
2923         struct dt_object *next = dt_object_child(dt);
2924         struct lu_attr   *attr = &lod_env_info(env)->lti_attr;
2925         __u32             mode;
2926         int               rc;
2927         ENTRY;
2928
2929         mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2930         if ((S_ISREG(mode) || mode == 0) &&
2931             !(fl & (LU_XATTR_REPLACE | LU_XATTR_MERGE)) &&
2932             (strcmp(name, XATTR_NAME_LOV) == 0 ||
2933              strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
2934                 /*
2935                  * this is a request to create object's striping.
2936                  *
2937                  * allow to declare predefined striping on a new (!mode) object
2938                  * which is supposed to be replay of regular file creation
2939                  * (when LOV setting is declared)
2940                  *
2941                  * LU_XATTR_REPLACE is set to indicate a layout swap
2942                  */
2943                 if (dt_object_exists(dt)) {
2944                         rc = dt_attr_get(env, next, attr);
2945                         if (rc)
2946                                 RETURN(rc);
2947                 } else {
2948                         memset(attr, 0, sizeof(*attr));
2949                         attr->la_valid = LA_TYPE | LA_MODE;
2950                         attr->la_mode = S_IFREG;
2951                 }
2952                 rc = lod_declare_striped_create(env, dt, attr, buf, th);
2953         } else if (fl & LU_XATTR_MERGE) {
2954                 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
2955                         strcmp(name, XATTR_LUSTRE_LOV) == 0);
2956                 rc = lod_declare_layout_merge(env, dt, buf, th);
2957         } else if (S_ISREG(mode) &&
2958                    strlen(name) > strlen(XATTR_LUSTRE_LOV) + 1 &&
2959                    strncmp(name, XATTR_LUSTRE_LOV,
2960                            strlen(XATTR_LUSTRE_LOV)) == 0) {
2961                 /*
2962                  * this is a request to modify object's striping.
2963                  * add/set/del component(s).
2964                  */
2965                 if (!dt_object_exists(dt))
2966                         RETURN(-ENOENT);
2967
2968                 rc = lod_declare_modify_layout(env, dt, name, buf, th);
2969         } else if (S_ISDIR(mode)) {
2970                 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2971         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
2972                 rc = lod_replace_parent_fid(env, dt, th, true);
2973         } else {
2974                 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2975         }
2976
2977         RETURN(rc);
2978 }
2979
2980 /**
2981  * Apply xattr changes to the object.
2982  *
2983  * Applies xattr changes to the object and the stripes if the latter exist.
2984  *
2985  * \param[in] env       execution environment
2986  * \param[in] dt        object
2987  * \param[in] buf       buffer pointing to the new value of xattr
2988  * \param[in] name      name of xattr
2989  * \param[in] fl        flags
2990  * \param[in] th        transaction handle
2991  *
2992  * \retval              0 on success
2993  * \retval              negative if failed
2994  */
2995 static int lod_xattr_set_internal(const struct lu_env *env,
2996                                   struct dt_object *dt,
2997                                   const struct lu_buf *buf,
2998                                   const char *name, int fl,
2999                                   struct thandle *th)
3000 {
3001         struct dt_object        *next = dt_object_child(dt);
3002         struct lod_object       *lo = lod_dt_obj(dt);
3003         int                     rc;
3004         int                     i;
3005         ENTRY;
3006
3007         rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3008         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3009                 RETURN(rc);
3010
3011         /* Note: Do not set LinkEA on sub-stripes, otherwise
3012          * it will confuse the fid2path process(see mdt_path_current()).
3013          * The linkEA between master and sub-stripes is set in
3014          * lod_xattr_set_lmv(). */
3015         if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
3016                 RETURN(0);
3017
3018         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3019                 LASSERT(lo->ldo_stripe[i]);
3020
3021                 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
3022                                        fl, th);
3023                 if (rc != 0)
3024                         break;
3025         }
3026
3027         RETURN(rc);
3028 }
3029
3030 /**
3031  * Delete an extended attribute.
3032  *
3033  * Deletes specified xattr from the object and the stripes if the latter exist.
3034  *
3035  * \param[in] env       execution environment
3036  * \param[in] dt        object
3037  * \param[in] name      name of xattr
3038  * \param[in] th        transaction handle
3039  *
3040  * \retval              0 on success
3041  * \retval              negative if failed
3042  */
3043 static int lod_xattr_del_internal(const struct lu_env *env,
3044                                   struct dt_object *dt,
3045                                   const char *name, struct thandle *th)
3046 {
3047         struct dt_object        *next = dt_object_child(dt);
3048         struct lod_object       *lo = lod_dt_obj(dt);
3049         int                     rc;
3050         int                     i;
3051         ENTRY;
3052
3053         rc = lod_sub_xattr_del(env, next, name, th);
3054         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3055                 RETURN(rc);
3056
3057         if (lo->ldo_dir_stripe_count == 0)
3058                 RETURN(rc);
3059
3060         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3061                 LASSERT(lo->ldo_stripe[i]);
3062
3063                 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3064                 if (rc != 0)
3065                         break;
3066         }
3067
3068         RETURN(rc);
3069 }
3070
3071 /**
3072  * Set default striping on a directory.
3073  *
3074  * Sets specified striping on a directory object unless it matches the default
3075  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3076  * EA. This striping will be used when regular file is being created in this
3077  * directory.
3078  *
3079  * \param[in] env       execution environment
3080  * \param[in] dt        the striped object
3081  * \param[in] buf       buffer with the striping
3082  * \param[in] name      name of EA
3083  * \param[in] fl        xattr flag (see OSD API description)
3084  * \param[in] th        transaction handle
3085  *
3086  * \retval              0 on success
3087  * \retval              negative if failed
3088  */
3089 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
3090                                     struct dt_object *dt,
3091                                     const struct lu_buf *buf,
3092                                     const char *name, int fl,
3093                                     struct thandle *th)
3094 {
3095         struct lov_user_md_v1   *lum;
3096         struct lov_user_md_v3   *v3 = NULL;
3097         const char              *pool_name = NULL;
3098         int                      rc;
3099         bool                     is_del;
3100         ENTRY;
3101
3102         LASSERT(buf != NULL && buf->lb_buf != NULL);
3103         lum = buf->lb_buf;
3104
3105         switch (lum->lmm_magic) {
3106         case LOV_USER_MAGIC_V3:
3107                 v3 = buf->lb_buf;
3108                 if (v3->lmm_pool_name[0] != '\0')
3109                         pool_name = v3->lmm_pool_name;
3110                 /* fall through */
3111         case LOV_USER_MAGIC_V1:
3112                 /* if { size, offset, count } = { 0, -1, 0 } and no pool
3113                  * (i.e. all default values specified) then delete default
3114                  * striping from dir. */
3115                 CDEBUG(D_LAYOUT,
3116                        "set default striping: sz %u # %u offset %d %s %s\n",
3117                        (unsigned)lum->lmm_stripe_size,
3118                        (unsigned)lum->lmm_stripe_count,
3119                        (int)lum->lmm_stripe_offset,
3120                        v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
3121
3122                 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
3123                                              lum->lmm_stripe_count,
3124                                              lum->lmm_stripe_offset,
3125                                              pool_name);
3126                 break;
3127         case LOV_USER_MAGIC_COMP_V1:
3128                 is_del = false;
3129                 break;
3130         default:
3131                 CERROR("Invalid magic %x\n", lum->lmm_magic);
3132                 RETURN(-EINVAL);
3133         }
3134
3135         if (is_del) {
3136                 rc = lod_xattr_del_internal(env, dt, name, th);
3137                 if (rc == -ENODATA)
3138                         rc = 0;
3139         } else {
3140                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3141         }
3142
3143         RETURN(rc);
3144 }
3145
3146 /**
3147  * Set default striping on a directory object.
3148  *
3149  * Sets specified striping on a directory object unless it matches the default
3150  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3151  * EA. This striping will be used when a new directory is being created in the
3152  * directory.
3153  *
3154  * \param[in] env       execution environment
3155  * \param[in] dt        the striped object
3156  * \param[in] buf       buffer with the striping
3157  * \param[in] name      name of EA
3158  * \param[in] fl        xattr flag (see OSD API description)
3159  * \param[in] th        transaction handle
3160  *
3161  * \retval              0 on success
3162  * \retval              negative if failed
3163  */
3164 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
3165                                             struct dt_object *dt,
3166                                             const struct lu_buf *buf,
3167                                             const char *name, int fl,
3168                                             struct thandle *th)
3169 {
3170         struct lmv_user_md_v1   *lum;
3171         int                      rc;
3172         ENTRY;
3173
3174         LASSERT(buf != NULL && buf->lb_buf != NULL);
3175         lum = buf->lb_buf;
3176
3177         CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
3178               le32_to_cpu(lum->lum_stripe_count),
3179               (int)le32_to_cpu(lum->lum_stripe_offset));
3180
3181         if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
3182                                  le32_to_cpu(lum->lum_stripe_offset)) &&
3183                                 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
3184                 rc = lod_xattr_del_internal(env, dt, name, th);
3185                 if (rc == -ENODATA)
3186                         rc = 0;
3187         } else {
3188                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3189                 if (rc != 0)
3190                         RETURN(rc);
3191         }
3192
3193         RETURN(rc);
3194 }
3195
3196 /**
3197  * Turn directory into a striped directory.
3198  *
3199  * During replay the client sends the striping created before MDT
3200  * failure, then the layer above LOD sends this defined striping
3201  * using ->do_xattr_set(), so LOD uses this method to replay creation
3202  * of the stripes. Notice the original information for the striping
3203  * (#stripes, FIDs, etc) was transferred in declare path.
3204  *
3205  * \param[in] env       execution environment
3206  * \param[in] dt        the striped object
3207  * \param[in] buf       not used currently
3208  * \param[in] name      not used currently
3209  * \param[in] fl        xattr flag (see OSD API description)
3210  * \param[in] th        transaction handle
3211  *
3212  * \retval              0 on success
3213  * \retval              negative if failed
3214  */
3215 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
3216                              const struct lu_buf *buf, const char *name,
3217                              int fl, struct thandle *th)
3218 {
3219         struct lod_object       *lo = lod_dt_obj(dt);
3220         struct lod_thread_info  *info = lod_env_info(env);
3221         struct lu_attr          *attr = &info->lti_attr;
3222         struct dt_object_format *dof = &info->lti_format;
3223         struct lu_buf           lmv_buf;
3224         struct lu_buf           slave_lmv_buf;
3225         struct lmv_mds_md_v1    *lmm;
3226         struct lmv_mds_md_v1    *slave_lmm = NULL;
3227         struct dt_insert_rec    *rec = &info->lti_dt_rec;
3228         int                     i;
3229         int                     rc;
3230         ENTRY;
3231
3232         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3233                 RETURN(-ENOTDIR);
3234
3235         /* The stripes are supposed to be allocated in declare phase,
3236          * if there are no stripes being allocated, it will skip */
3237         if (lo->ldo_dir_stripe_count == 0)
3238                 RETURN(0);
3239
3240         rc = dt_attr_get(env, dt_object_child(dt), attr);
3241         if (rc != 0)
3242                 RETURN(rc);
3243
3244         attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
3245                          LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
3246         dof->dof_type = DFT_DIR;
3247
3248         rc = lod_prep_lmv_md(env, dt, &lmv_buf);
3249         if (rc != 0)
3250                 RETURN(rc);
3251         lmm = lmv_buf.lb_buf;
3252
3253         OBD_ALLOC_PTR(slave_lmm);
3254         if (slave_lmm == NULL)
3255                 RETURN(-ENOMEM);
3256
3257         lod_prep_slave_lmv_md(slave_lmm, lmm);
3258         slave_lmv_buf.lb_buf = slave_lmm;
3259         slave_lmv_buf.lb_len = sizeof(*slave_lmm);
3260
3261         rec->rec_type = S_IFDIR;
3262         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3263                 struct dt_object *dto;
3264                 char             *stripe_name = info->lti_key;
3265                 struct lu_name          *sname;
3266                 struct linkea_data       ldata          = { NULL };
3267                 struct lu_buf            linkea_buf;
3268
3269                 dto = lo->ldo_stripe[i];
3270
3271                 dt_write_lock(env, dto, MOR_TGT_CHILD);
3272                 rc = lod_sub_create(env, dto, attr, NULL, dof, th);
3273                 if (rc != 0) {
3274                         dt_write_unlock(env, dto);
3275                         GOTO(out, rc);
3276                 }
3277
3278                 rc = lod_sub_ref_add(env, dto, th);
3279                 dt_write_unlock(env, dto);
3280                 if (rc != 0)
3281                         GOTO(out, rc);
3282
3283                 rec->rec_fid = lu_object_fid(&dto->do_lu);
3284                 rc = lod_sub_insert(env, dto, (const struct dt_rec *)rec,
3285                                     (const struct dt_key *)dot, th, 0);
3286                 if (rc != 0)
3287                         GOTO(out, rc);
3288
3289                 rec->rec_fid = lu_object_fid(&dt->do_lu);
3290                 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
3291                                     (const struct dt_key *)dotdot, th, 0);
3292                 if (rc != 0)
3293                         GOTO(out, rc);
3294
3295                 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
3296                     cfs_fail_val != i) {
3297                         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
3298                             cfs_fail_val == i)
3299                                 slave_lmm->lmv_master_mdt_index =
3300                                                         cpu_to_le32(i + 1);
3301                         else
3302                                 slave_lmm->lmv_master_mdt_index =
3303                                                         cpu_to_le32(i);
3304
3305                         rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
3306                                                XATTR_NAME_LMV, fl, th);
3307                         if (rc != 0)
3308                                 GOTO(out, rc);
3309                 }
3310
3311                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
3312                     cfs_fail_val == i)
3313                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3314                                  PFID(lu_object_fid(&dto->do_lu)), i + 1);
3315                 else
3316                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3317                                  PFID(lu_object_fid(&dto->do_lu)), i);
3318
3319                 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
3320                 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
3321                                       sname, lu_object_fid(&dt->do_lu));
3322                 if (rc != 0)
3323                         GOTO(out, rc);
3324
3325                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
3326                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
3327                 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
3328                                        XATTR_NAME_LINK, 0, th);
3329                 if (rc != 0)
3330                         GOTO(out, rc);
3331
3332                 rec->rec_fid = lu_object_fid(&dto->do_lu);
3333                 rc = lod_sub_insert(env, dt_object_child(dt),
3334                                     (const struct dt_rec *)rec,
3335                                     (const struct dt_key *)stripe_name, th, 0);
3336                 if (rc != 0)
3337                         GOTO(out, rc);
3338
3339                 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
3340                 if (rc != 0)
3341                         GOTO(out, rc);
3342         }
3343
3344         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
3345                 rc = lod_sub_xattr_set(env, dt_object_child(dt),
3346                                        &lmv_buf, XATTR_NAME_LMV, fl, th);
3347 out:
3348         if (slave_lmm != NULL)
3349                 OBD_FREE_PTR(slave_lmm);
3350
3351         RETURN(rc);
3352 }
3353
3354 /**
3355  * Helper function to declare/execute creation of a striped directory
3356  *
3357  * Called in declare/create object path, prepare striping for a directory
3358  * and prepare defaults data striping for the objects to be created in
3359  * that directory. Notice the function calls "declaration" or "execution"
3360  * methods depending on \a declare param. This is a consequence of the
3361  * current approach while we don't have natural distributed transactions:
3362  * we basically execute non-local updates in the declare phase. So, the
3363  * arguments for the both phases are the same and this is the reason for
3364  * this function to exist.
3365  *
3366  * \param[in] env       execution environment
3367  * \param[in] dt        object
3368  * \param[in] attr      attributes the stripes will be created with
3369  * \param[in] lmu       lmv_user_md if MDT indices are specified
3370  * \param[in] dof       format of stripes (see OSD API description)
3371  * \param[in] th        transaction handle
3372  * \param[in] declare   where to call "declare" or "execute" methods
3373  *
3374  * \retval              0 on success
3375  * \retval              negative if failed
3376  */
3377 static int lod_dir_striping_create_internal(const struct lu_env *env,
3378                                             struct dt_object *dt,
3379                                             struct lu_attr *attr,
3380                                             const struct lu_buf *lmu,
3381                                             struct dt_object_format *dof,
3382                                             struct thandle *th,
3383                                             bool declare)
3384 {
3385         struct lod_thread_info *info = lod_env_info(env);
3386         struct lod_object *lo = lod_dt_obj(dt);
3387         const struct lod_default_striping *lds = lo->ldo_def_striping;
3388         int rc;
3389         ENTRY;
3390
3391         LASSERT(ergo(lds != NULL,
3392                      lds->lds_def_striping_set ||
3393                      lds->lds_dir_def_striping_set));
3394
3395         if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
3396                                  lo->ldo_dir_stripe_offset)) {
3397                 if (!lmu) {
3398                         struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3399                         int stripe_count = lo->ldo_dir_stripe_count;
3400
3401                         if (info->lti_ea_store_size < sizeof(*v1)) {
3402                                 rc = lod_ea_store_resize(info, sizeof(*v1));
3403                                 if (rc != 0)
3404                                         RETURN(rc);
3405                                 v1 = info->lti_ea_store;
3406                         }
3407
3408                         memset(v1, 0, sizeof(*v1));
3409                         v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3410                         v1->lum_stripe_count = cpu_to_le32(stripe_count);
3411                         v1->lum_stripe_offset =
3412                                         cpu_to_le32(lo->ldo_dir_stripe_offset);
3413
3414                         info->lti_buf.lb_buf = v1;
3415                         info->lti_buf.lb_len = sizeof(*v1);
3416                         lmu = &info->lti_buf;
3417                 }
3418
3419                 if (declare)
3420                         rc = lod_declare_xattr_set_lmv(env, dt, attr, lmu, dof,
3421                                                        th);
3422                 else
3423                         rc = lod_xattr_set_lmv(env, dt, lmu, XATTR_NAME_LMV, 0,
3424                                                th);
3425                 if (rc != 0)
3426                         RETURN(rc);
3427         }
3428
3429         /* Transfer default LMV striping from the parent */
3430         if (lds != NULL && lds->lds_dir_def_striping_set &&
3431             !LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
3432                                  lds->lds_dir_def_stripe_offset)) {
3433                 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3434
3435                 if (info->lti_ea_store_size < sizeof(*v1)) {
3436                         rc = lod_ea_store_resize(info, sizeof(*v1));
3437                         if (rc != 0)
3438                                 RETURN(rc);
3439                         v1 = info->lti_ea_store;
3440                 }
3441
3442                 memset(v1, 0, sizeof(*v1));
3443                 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3444                 v1->lum_stripe_count =
3445                         cpu_to_le32(lds->lds_dir_def_stripe_count);
3446                 v1->lum_stripe_offset =
3447                         cpu_to_le32(lds->lds_dir_def_stripe_offset);
3448                 v1->lum_hash_type =
3449                         cpu_to_le32(lds->lds_dir_def_hash_type);
3450
3451                 info->lti_buf.lb_buf = v1;
3452                 info->lti_buf.lb_len = sizeof(*v1);
3453                 if (declare)
3454                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3455                                                        XATTR_NAME_DEFAULT_LMV,
3456                                                        0, th);
3457                 else
3458                         rc = lod_xattr_set_default_lmv_on_dir(env, dt,
3459                                                   &info->lti_buf,
3460                                                   XATTR_NAME_DEFAULT_LMV, 0,
3461                                                   th);
3462                 if (rc != 0)
3463                         RETURN(rc);
3464         }
3465
3466         /* Transfer default LOV striping from the parent */
3467         if (lds != NULL && lds->lds_def_striping_set &&
3468             lds->lds_def_comp_cnt != 0) {
3469                 struct lov_mds_md *lmm;
3470                 int lmm_size = lod_comp_md_size(lo, true);
3471
3472                 if (info->lti_ea_store_size < lmm_size) {
3473                         rc = lod_ea_store_resize(info, lmm_size);
3474                         if (rc != 0)
3475                                 RETURN(rc);
3476                 }
3477                 lmm = info->lti_ea_store;
3478
3479                 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
3480                 if (rc != 0)
3481                         RETURN(rc);
3482
3483                 info->lti_buf.lb_buf = lmm;
3484                 info->lti_buf.lb_len = lmm_size;
3485
3486                 if (declare)
3487                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3488                                                        XATTR_NAME_LOV, 0, th);
3489                 else
3490                         rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
3491                                                       XATTR_NAME_LOV, 0, th);
3492                 if (rc != 0)
3493                         RETURN(rc);
3494         }
3495
3496         RETURN(0);
3497 }
3498
3499 static int lod_declare_dir_striping_create(const struct lu_env *env,
3500                                            struct dt_object *dt,
3501                                            struct lu_attr *attr,
3502                                            struct lu_buf *lmu,
3503                                            struct dt_object_format *dof,
3504                                            struct thandle *th)
3505 {
3506         return lod_dir_striping_create_internal(env, dt, attr, lmu, dof, th,
3507                                                 true);
3508 }
3509
3510 static int lod_dir_striping_create(const struct lu_env *env,
3511                                    struct dt_object *dt,
3512                                    struct lu_attr *attr,
3513                                    struct dt_object_format *dof,
3514                                    struct thandle *th)
3515 {
3516         return lod_dir_striping_create_internal(env, dt, attr, NULL, dof, th,
3517                                                 false);
3518 }
3519
3520 /**
3521  * Make LOV EA for striped object.
3522  *
3523  * Generate striping information and store it in the LOV EA of the given
3524  * object. The caller must ensure nobody else is calling the function
3525  * against the object concurrently. The transaction must be started.
3526  * FLDB service must be running as well; it's used to map FID to the target,
3527  * which is stored in LOV EA.
3528  *
3529  * \param[in] env               execution environment for this thread
3530  * \param[in] lo                LOD object
3531  * \param[in] th                transaction handle
3532  *
3533  * \retval                      0 if LOV EA is stored successfully
3534  * \retval                      negative error number on failure
3535  */
3536 static int lod_generate_and_set_lovea(const struct lu_env *env,
3537                                       struct lod_object *lo,
3538                                       struct thandle *th)
3539 {
3540         struct lod_thread_info  *info = lod_env_info(env);
3541         struct dt_object        *next = dt_object_child(&lo->ldo_obj);
3542         struct lov_mds_md_v1    *lmm;
3543         int                      rc, lmm_size;
3544         ENTRY;
3545
3546         LASSERT(lo);
3547
3548         if (lo->ldo_comp_cnt == 0) {
3549                 lod_object_free_striping(env, lo);
3550                 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
3551                 RETURN(rc);
3552         }
3553
3554         lmm_size = lod_comp_md_size(lo, false);
3555         if (info->lti_ea_store_size < lmm_size) {
3556                 rc = lod_ea_store_resize(info, lmm_size);
3557                 if (rc)
3558                         RETURN(rc);
3559         }
3560         lmm = info->lti_ea_store;
3561
3562         rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
3563         if (rc)
3564                 RETURN(rc);
3565
3566         info->lti_buf.lb_buf = lmm;
3567         info->lti_buf.lb_len = lmm_size;
3568         rc = lod_sub_xattr_set(env, next, &info->lti_buf,
3569                                XATTR_NAME_LOV, 0, th);
3570         RETURN(rc);
3571 }
3572
3573 /**
3574  * Delete layout component(s)
3575  *
3576  * \param[in] env       execution environment for this thread
3577  * \param[in] dt        object
3578  * \param[in] th        transaction handle
3579  *
3580  * \retval      0 on success
3581  * \retval      negative error number on failure
3582  */
3583 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
3584                           struct thandle *th)
3585 {
3586         struct lod_layout_component     *lod_comp;
3587         struct lod_object       *lo = lod_dt_obj(dt);
3588         struct dt_object        *next = dt_object_child(dt);
3589         struct lu_attr  *attr = &lod_env_info(env)->lti_attr;
3590         int     rc, i, j, left;
3591
3592         LASSERT(lo->ldo_is_composite);
3593         LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
3594
3595         left = lo->ldo_comp_cnt;
3596         for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3597                 lod_comp = &lo->ldo_comp_entries[i];
3598
3599                 if (lod_comp->llc_id != LCME_ID_INVAL)
3600                         break;
3601                 left--;
3602
3603                 /* Not instantiated component */
3604                 if (lod_comp->llc_stripe == NULL)
3605                         continue;
3606
3607                 LASSERT(lod_comp->llc_stripe_count > 0);
3608                 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
3609                         struct dt_object *obj = lod_comp->llc_stripe[j];
3610
3611                         if (obj == NULL)
3612                                 continue;
3613                         rc = lod_sub_destroy(env, obj, th);
3614                         if (rc)
3615                                 GOTO(out, rc);
3616
3617                         lu_object_put(env, &obj->do_lu);
3618                         lod_comp->llc_stripe[j] = NULL;
3619                 }
3620                 OBD_FREE(lod_comp->llc_stripe, sizeof(struct dt_object *) *
3621                                         lod_comp->llc_stripes_allocated);
3622                 lod_comp->llc_stripe = NULL;
3623                 lod_comp->llc_stripes_allocated = 0;
3624                 lod_obj_set_pool(lo, i, NULL);
3625                 if (lod_comp->llc_ostlist.op_array) {
3626                         OBD_FREE(lod_comp->llc_ostlist.op_array,
3627                                  lod_comp->llc_ostlist.op_size);
3628                         lod_comp->llc_ostlist.op_array = NULL;
3629                         lod_comp->llc_ostlist.op_size = 0;
3630                 }
3631         }
3632
3633         LASSERTF(left >= 0 && left < lo->ldo_comp_cnt, "left = %d\n", left);
3634         if (left > 0) {
3635                 struct lod_layout_component     *comp_array;
3636
3637                 OBD_ALLOC(comp_array, sizeof(*comp_array) * left);
3638                 if (comp_array == NULL)
3639                         GOTO(out, rc = -ENOMEM);
3640
3641                 memcpy(&comp_array[0], &lo->ldo_comp_entries[0],
3642                        sizeof(*comp_array) * left);
3643
3644                 OBD_FREE(lo->ldo_comp_entries,
3645                          sizeof(*comp_array) * lo->ldo_comp_cnt);
3646                 lo->ldo_comp_entries = comp_array;
3647                 lo->ldo_comp_cnt = left;
3648
3649                 LASSERT(lo->ldo_mirror_count == 1);
3650                 lo->ldo_mirrors[0].lme_end = left - 1;
3651                 lod_obj_inc_layout_gen(lo);
3652         } else {
3653                 lod_free_comp_entries(lo);
3654         }
3655
3656         LASSERT(dt_object_exists(dt));
3657         rc = dt_attr_get(env, next, attr);
3658         if (rc)
3659                 GOTO(out, rc);
3660
3661         if (attr->la_size > 0) {
3662                 attr->la_size = 0;
3663                 attr->la_valid = LA_SIZE;
3664                 rc = lod_sub_attr_set(env, next, attr, th);
3665                 if (rc)
3666                         GOTO(out, rc);
3667         }
3668
3669         rc = lod_generate_and_set_lovea(env, lo, th);
3670         EXIT;
3671 out:
3672         if (rc)
3673                 lod_object_free_striping(env, lo);
3674         return rc;
3675 }
3676
3677 /**
3678  * Implementation of dt_object_operations::do_xattr_set.
3679  *
3680  * Sets specified extended attribute on the object. Three types of EAs are
3681  * special:
3682  *   LOV EA - stores striping for a regular file or default striping (when set
3683  *            on a directory)
3684  *   LMV EA - stores a marker for the striped directories
3685  *   DMV EA - stores default directory striping
3686  *
3687  * When striping is applied to a non-striped existing object (this is called
3688  * late striping), then LOD notices the caller wants to turn the object into a
3689  * striped one. The stripe objects are created and appropriate EA is set:
3690  * LOV EA storing all the stripes directly or LMV EA storing just a small header
3691  * with striping configuration.
3692  *
3693  * \see dt_object_operations::do_xattr_set() in the API description for details.
3694  */
3695 static int lod_xattr_set(const struct lu_env *env,
3696                          struct dt_object *dt, const struct lu_buf *buf,
3697                          const char *name, int fl, struct thandle *th)
3698 {
3699         struct dt_object        *next = dt_object_child(dt);
3700         int                      rc;
3701         ENTRY;
3702
3703         if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3704             strcmp(name, XATTR_NAME_LMV) == 0) {
3705                 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
3706
3707                 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
3708                                                 LMV_HASH_FLAG_MIGRATION)
3709                         rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3710                 else
3711                         rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
3712
3713                 RETURN(rc);
3714         }
3715
3716         if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3717             strcmp(name, XATTR_NAME_LOV) == 0) {
3718                 /* default LOVEA */
3719                 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
3720                 RETURN(rc);
3721         } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3722                    strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
3723                 /* default LMVEA */
3724                 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
3725                                                       th);
3726                 RETURN(rc);
3727         } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3728                    (!strcmp(name, XATTR_NAME_LOV) ||
3729                     !strncmp(name, XATTR_LUSTRE_LOV,
3730                              strlen(XATTR_LUSTRE_LOV)))) {
3731                 /* in case of lov EA swap, just set it
3732                  * if not, it is a replay so check striping match what we
3733                  * already have during req replay, declare_xattr_set()
3734                  * defines striping, then create() does the work */
3735                 if (fl & LU_XATTR_REPLACE) {
3736                         /* free stripes, then update disk */
3737                         lod_object_free_striping(env, lod_dt_obj(dt));
3738
3739                         rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3740                 } else if (dt_object_remote(dt)) {
3741                         /* This only happens during migration, see
3742                          * mdd_migrate_create(), in which Master MDT will
3743                          * create a remote target object, and only set
3744                          * (migrating) stripe EA on the remote object,
3745                          * and does not need creating each stripes. */
3746                         rc = lod_sub_xattr_set(env, next, buf, name,
3747                                                       fl, th);
3748                 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
3749                         /* delete component(s) */
3750                         LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
3751                         rc = lod_layout_del(env, dt, th);
3752                 } else {
3753                         /*
3754                          * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
3755                          * it's going to create create file with specified
3756                          * component(s), the striping must have not being
3757                          * cached in this case;
3758                          *
3759                          * Otherwise, it's going to add/change component(s) to
3760                          * an existing file, the striping must have been cached
3761                          * in this case.
3762                          */
3763                         LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
3764                                      !strcmp(name, XATTR_NAME_LOV),
3765                                 !lod_dt_obj(dt)->ldo_comp_cached));
3766
3767                         rc = lod_striped_create(env, dt, NULL, NULL, th);
3768                 }
3769                 RETURN(rc);
3770         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3771                 rc = lod_replace_parent_fid(env, dt, th, false);
3772
3773                 RETURN(rc);
3774         }
3775
3776         /* then all other xattr */
3777         rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3778
3779         RETURN(rc);
3780 }
3781
3782 /**
3783  * Implementation of dt_object_operations::do_declare_xattr_del.
3784  *
3785  * \see dt_object_operations::do_declare_xattr_del() in the API description
3786  * for details.
3787  */
3788 static int lod_declare_xattr_del(const struct lu_env *env,
3789                                  struct dt_object *dt, const char *name,
3790                                  struct thandle *th)
3791 {
3792         struct lod_object       *lo = lod_dt_obj(dt);
3793         int                     rc;
3794         int                     i;
3795         ENTRY;
3796
3797         rc = lod_sub_declare_xattr_del(env, dt_object_child(dt), name, th);
3798         if (rc != 0)
3799                 RETURN(rc);
3800
3801         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3802                 RETURN(0);
3803
3804         /* set xattr to each stripes, if needed */
3805         rc = lod_load_striping(env, lo);
3806         if (rc != 0)
3807                 RETURN(rc);
3808
3809         if (lo->ldo_dir_stripe_count == 0)
3810                 RETURN(0);
3811
3812         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3813                 LASSERT(lo->ldo_stripe[i]);
3814                 rc = lod_sub_declare_xattr_del(env, lo->ldo_stripe[i],
3815                                                name, th);
3816                 if (rc != 0)
3817                         break;
3818         }
3819
3820         RETURN(rc);
3821 }
3822
3823 /**
3824  * Implementation of dt_object_operations::do_xattr_del.
3825  *
3826  * If EA storing a regular striping is being deleted, then release
3827  * all the references to the stripe objects in core.
3828  *
3829  * \see dt_object_operations::do_xattr_del() in the API description for details.
3830  */
3831 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
3832                          const char *name, struct thandle *th)
3833 {
3834         struct dt_object        *next = dt_object_child(dt);
3835         struct lod_object       *lo = lod_dt_obj(dt);
3836         int                     rc;
3837         int                     i;
3838         ENTRY;
3839
3840         if (!strcmp(name, XATTR_NAME_LOV))
3841                 lod_object_free_striping(env, lod_dt_obj(dt));
3842
3843         rc = lod_sub_xattr_del(env, next, name, th);
3844         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3845                 RETURN(rc);
3846
3847         if (lo->ldo_dir_stripe_count == 0)
3848                 RETURN(0);
3849
3850         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3851                 LASSERT(lo->ldo_stripe[i]);
3852
3853                 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3854                 if (rc != 0)
3855                         break;
3856         }
3857
3858         RETURN(rc);
3859 }
3860
3861 /**
3862  * Implementation of dt_object_operations::do_xattr_list.
3863  *
3864  * \see dt_object_operations::do_xattr_list() in the API description
3865  * for details.
3866  */
3867 static int lod_xattr_list(const struct lu_env *env,
3868                           struct dt_object *dt, const struct lu_buf *buf)
3869 {
3870         return dt_xattr_list(env, dt_object_child(dt), buf);
3871 }
3872
3873 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
3874 {
3875         return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
3876 }
3877
3878
3879 /**
3880  * Get default striping.
3881  *
3882  * \param[in] env               execution environment
3883  * \param[in] lo                object
3884  * \param[out] lds              default striping
3885  *
3886  * \retval              0 on success
3887  * \retval              negative if failed
3888  */
3889 static int lod_get_default_lov_striping(const struct lu_env *env,
3890                                         struct lod_object *lo,
3891                                         struct lod_default_striping *lds)
3892 {
3893         struct lod_thread_info *info = lod_env_info(env);
3894         struct lov_user_md_v1 *v1 = NULL;
3895         struct lov_user_md_v3 *v3 = NULL;
3896         struct lov_comp_md_v1 *comp_v1 = NULL;
3897         __u16   comp_cnt;
3898         __u16   mirror_cnt;
3899         bool    composite;
3900         int     rc, i;
3901         ENTRY;
3902
3903         lds->lds_def_striping_set = 0;
3904
3905         rc = lod_get_lov_ea(env, lo);
3906         if (rc < 0)
3907                 RETURN(rc);
3908
3909         if (rc < (typeof(rc))sizeof(struct lov_user_md))
3910                 RETURN(0);
3911
3912         v1 = info->lti_ea_store;
3913         if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
3914                 lustre_swab_lov_user_md_v1(v1);
3915         } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
3916                 v3 = (struct lov_user_md_v3 *)v1;
3917                 lustre_swab_lov_user_md_v3(v3);
3918         } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3919                 comp_v1 = (struct lov_comp_md_v1 *)v1;
3920                 lustre_swab_lov_comp_md_v1(comp_v1);
3921         }
3922
3923         if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
3924             v1->lmm_magic != LOV_MAGIC_COMP_V1)
3925                 RETURN(-ENOTSUPP);
3926
3927         if (v1->lmm_magic == LOV_MAGIC_COMP_V1) {
3928                 comp_v1 = (struct lov_comp_md_v1 *)v1;
3929                 comp_cnt = comp_v1->lcm_entry_count;
3930                 if (comp_cnt == 0)
3931                         RETURN(-EINVAL);
3932                 mirror_cnt = comp_v1->lcm_mirror_count + 1;
3933                 composite = true;
3934         } else {
3935                 comp_cnt = 1;
3936                 mirror_cnt = 0;
3937                 composite = false;
3938         }
3939
3940         /* realloc default comp entries if necessary */
3941         rc = lod_def_striping_comp_resize(lds, comp_cnt);
3942         if (rc < 0)
3943                 RETURN(rc);
3944
3945         lds->lds_def_comp_cnt = comp_cnt;
3946         lds->lds_def_striping_is_composite = composite;
3947         lds->lds_def_mirror_cnt = mirror_cnt;
3948
3949         for (i = 0; i < comp_cnt; i++) {
3950                 struct lod_layout_component *lod_comp;
3951                 struct lu_extent *ext;
3952                 char *pool;
3953
3954                 lod_comp = &lds->lds_def_comp_entries[i];
3955                 /*
3956                  * reset lod_comp values, llc_stripes is always NULL in
3957                  * the default striping template, llc_pool will be reset
3958                  * later below.
3959                  */
3960                 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
3961
3962                 if (composite) {
3963                         v1 = (struct lov_user_md *)((char *)comp_v1 +
3964                                         comp_v1->lcm_entries[i].lcme_offset);
3965                         ext = &comp_v1->lcm_entries[i].lcme_extent;
3966                         lod_comp->llc_extent = *ext;
3967                 }
3968
3969                 if (v1->lmm_pattern != LOV_PATTERN_RAID0 &&
3970                     v1->lmm_pattern != LOV_PATTERN_MDT &&
3971                     v1->lmm_pattern != 0) {
3972                         lod_free_def_comp_entries(lds);
3973                         RETURN(-EINVAL);
3974                 }
3975
3976                 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d "
3977                        "stripe_offset=%d\n",
3978                        PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
3979                        (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
3980                        (int)v1->lmm_stripe_offset);
3981
3982                 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
3983                 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
3984                 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
3985                 lod_comp->llc_pattern = v1->lmm_pattern;
3986
3987                 pool = NULL;
3988                 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
3989                         /* XXX: sanity check here */
3990                         v3 = (struct lov_user_md_v3 *) v1;
3991                         if (v3->lmm_pool_name[0] != '\0')
3992                                 pool = v3->lmm_pool_name;
3993                 }
3994                 lod_set_def_pool(lds, i, pool);
3995         }
3996
3997         lds->lds_def_striping_set = 1;
3998         RETURN(rc);
3999 }
4000
4001 /**
4002  * Get default directory striping.
4003  *
4004  * \param[in] env               execution environment
4005  * \param[in] lo                object
4006  * \param[out] lds              default striping
4007  *
4008  * \retval              0 on success
4009  * \retval              negative if failed
4010  */
4011 static int lod_get_default_lmv_striping(const struct lu_env *env,
4012                                         struct lod_object *lo,
4013                                         struct lod_default_striping *lds)
4014 {
4015         struct lod_thread_info  *info = lod_env_info(env);
4016         struct lmv_user_md_v1   *v1 = NULL;
4017         int                      rc;
4018         ENTRY;
4019
4020         lds->lds_dir_def_striping_set = 0;
4021         rc = lod_get_default_lmv_ea(env, lo);
4022         if (rc < 0)
4023                 RETURN(rc);
4024
4025         if (rc < (typeof(rc))sizeof(struct lmv_user_md))
4026                 RETURN(0);
4027
4028         v1 = info->lti_ea_store;
4029
4030         lds->lds_dir_def_stripe_count = le32_to_cpu(v1->lum_stripe_count);
4031         lds->lds_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
4032         lds->lds_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
4033         lds->lds_dir_def_striping_set = 1;
4034
4035         RETURN(0);
4036 }
4037
4038 /**
4039  * Get default striping in the object.
4040  *
4041  * Get object default striping and default directory striping.
4042  *
4043  * \param[in] env               execution environment
4044  * \param[in] lo                object
4045  * \param[out] lds              default striping
4046  *
4047  * \retval              0 on success
4048  * \retval              negative if failed
4049  */
4050 static int lod_get_default_striping(const struct lu_env *env,
4051                                     struct lod_object *lo,
4052                                     struct lod_default_striping *lds)
4053 {
4054         int rc, rc1;
4055
4056         rc = lod_get_default_lov_striping(env, lo, lds);
4057         rc1 = lod_get_default_lmv_striping(env, lo, lds);
4058         if (rc == 0 && rc1 < 0)
4059                 rc = rc1;
4060
4061         return rc;
4062 }
4063
4064 /**
4065  * Apply default striping on object.
4066  *
4067  * If object striping pattern is not set, set to the one in default striping.
4068  * The default striping is from parent or fs.
4069  *
4070  * \param[in] lo                new object
4071  * \param[in] lds               default striping
4072  * \param[in] mode              new object's mode
4073  */
4074 static void lod_striping_from_default(struct lod_object *lo,
4075                                       const struct lod_default_striping *lds,
4076                                       umode_t mode)
4077 {
4078         struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4079         struct lov_desc *desc = &d->lod_desc;
4080         int i, rc;
4081
4082         if (lds->lds_def_striping_set && S_ISREG(mode)) {
4083                 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
4084                                             lds->lds_def_comp_cnt);
4085                 if (rc != 0)
4086                         return;
4087
4088                 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
4089                 if (lds->lds_def_mirror_cnt > 1)
4090                         lo->ldo_flr_state = LCM_FL_RDONLY;
4091
4092                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4093                         struct lod_layout_component *obj_comp =
4094                                                 &lo->ldo_comp_entries[i];
4095                         struct lod_layout_component *def_comp =
4096                                                 &lds->lds_def_comp_entries[i];
4097
4098                         CDEBUG(D_LAYOUT, "Inherite from default: size:%hu "
4099                                "nr:%u offset:%u pattern %#x %s\n",
4100                                def_comp->llc_stripe_size,
4101                                def_comp->llc_stripe_count,
4102                                def_comp->llc_stripe_offset,
4103                                def_comp->llc_pattern,
4104                                def_comp->llc_pool ?: "");
4105
4106                         *obj_comp = *def_comp;
4107                         if (def_comp->llc_pool != NULL) {
4108                                 /* pointer was copied from def_comp */
4109                                 obj_comp->llc_pool = NULL;
4110                                 lod_obj_set_pool(lo, i, def_comp->llc_pool);
4111                         }
4112
4113                         /*
4114                          * Don't initialize these fields for plain layout
4115                          * (v1/v3) here, they are inherited in the order of
4116                          * 'parent' -> 'fs default (root)' -> 'global default
4117                          * values for stripe_count & stripe_size'.
4118                          *
4119                          * see lod_ah_init().
4120                          */
4121                         if (!lo->ldo_is_composite)
4122                                 continue;
4123
4124                         if (obj_comp->llc_stripe_count <= 0 &&
4125                             obj_comp->llc_pattern != LOV_PATTERN_MDT)
4126                                 obj_comp->llc_stripe_count =
4127                                         desc->ld_default_stripe_count;
4128                         if (obj_comp->llc_stripe_size <= 0)
4129                                 obj_comp->llc_stripe_size =
4130                                         desc->ld_default_stripe_size;
4131                 }
4132         } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
4133                 if (lo->ldo_dir_stripe_count == 0)
4134                         lo->ldo_dir_stripe_count =
4135                                 lds->lds_dir_def_stripe_count;
4136                 if (lo->ldo_dir_stripe_offset == -1)
4137                         lo->ldo_dir_stripe_offset =
4138                                 lds->lds_dir_def_stripe_offset;
4139                 if (lo->ldo_dir_hash_type == 0)
4140                         lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
4141
4142                 CDEBUG(D_LAYOUT, "striping from default dir: count:%hu, "
4143                        "offset:%u, hash_type:%u\n",
4144                        lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
4145                        lo->ldo_dir_hash_type);
4146         }
4147 }
4148
4149 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root)
4150 {
4151         struct lod_layout_component *lod_comp;
4152
4153         if (lo->ldo_comp_cnt == 0)
4154                 return true;
4155
4156         if (lo->ldo_is_composite)
4157                 return false;
4158
4159         lod_comp = &lo->ldo_comp_entries[0];
4160
4161         if (lod_comp->llc_stripe_count <= 0 ||
4162             lod_comp->llc_stripe_size <= 0)
4163                 return true;
4164
4165         if (from_root && (lod_comp->llc_pool == NULL ||
4166                           lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
4167                 return true;
4168
4169         return false;
4170 }
4171
4172 /**
4173  * Implementation of dt_object_operations::do_ah_init.
4174  *
4175  * This method is used to make a decision on the striping configuration for the
4176  * object being created. It can be taken from the \a parent object if it exists,
4177  * or filesystem's default. The resulting configuration (number of stripes,
4178  * stripe size/offset, pool name, etc) is stored in the object itself and will
4179  * be used by the methods like ->doo_declare_create().
4180  *
4181  * \see dt_object_operations::do_ah_init() in the API description for details.
4182  */
4183 static void lod_ah_init(const struct lu_env *env,
4184                         struct dt_allocation_hint *ah,
4185                         struct dt_object *parent,
4186                         struct dt_object *child,
4187                         umode_t child_mode)
4188 {
4189         struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
4190         struct lod_thread_info *info = lod_env_info(env);
4191         struct lod_default_striping *lds = &info->lti_def_striping;
4192         struct dt_object *nextp = NULL;
4193         struct dt_object *nextc;
4194         struct lod_object *lp = NULL;
4195         struct lod_object *lc;
4196         struct lov_desc *desc;
4197         struct lod_layout_component *lod_comp;
4198         int rc;
4199         ENTRY;
4200
4201         LASSERT(child);
4202
4203         if (likely(parent)) {
4204                 nextp = dt_object_child(parent);
4205                 lp = lod_dt_obj(parent);
4206         }
4207
4208         nextc = dt_object_child(child);
4209         lc = lod_dt_obj(child);
4210
4211         LASSERT(!lod_obj_is_striped(child));
4212         /* default layout template may have been set on the regular file
4213          * when this is called from mdd_create_data() */
4214         if (S_ISREG(child_mode))
4215                 lod_free_comp_entries(lc);
4216
4217         if (!dt_object_exists(nextc))
4218                 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
4219
4220         if (S_ISDIR(child_mode)) {
4221                 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
4222
4223                 /* other default values are 0 */
4224                 lc->ldo_dir_stripe_offset = -1;
4225
4226                 /* get default striping from parent object */
4227                 if (likely(lp != NULL))
4228                         lod_get_default_striping(env, lp, lds);
4229
4230                 /* set child default striping info, default value is NULL */
4231                 if (lds->lds_def_striping_set || lds->lds_dir_def_striping_set)
4232                         lc->ldo_def_striping = lds;
4233
4234                 /* It should always honour the specified stripes */
4235                 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
4236                  * will have old magic. In this case, we should ignore the
4237                  * stripe count and try to create dir by default stripe.
4238                  */
4239                 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
4240                     (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
4241                      le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
4242                         lc->ldo_dir_stripe_count =
4243                                 le32_to_cpu(lum1->lum_stripe_count);
4244                         lc->ldo_dir_stripe_offset =
4245                                 le32_to_cpu(lum1->lum_stripe_offset);
4246                         lc->ldo_dir_hash_type =
4247                                 le32_to_cpu(lum1->lum_hash_type);
4248                         CDEBUG(D_INFO,
4249                                "set dirstripe: count %hu, offset %d, hash %u\n",
4250                                 lc->ldo_dir_stripe_count,
4251                                 (int)lc->ldo_dir_stripe_offset,
4252                                 lc->ldo_dir_hash_type);
4253                 } else {
4254                         /* transfer defaults LMV to new directory */
4255                         lod_striping_from_default(lc, lds, child_mode);
4256                 }
4257
4258                 /* shrink the stripe_count to the avaible MDT count */
4259                 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
4260                     !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))
4261                         lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
4262
4263                 /* Directory will be striped only if stripe_count > 1, if
4264                  * stripe_count == 1, let's reset stripe_count = 0 to avoid
4265                  * create single master stripe and also help to unify the
4266                  * stripe handling of directories and files */
4267                 if (lc->ldo_dir_stripe_count == 1)
4268                         lc->ldo_dir_stripe_count = 0;
4269
4270                 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
4271                        lc->ldo_dir_stripe_count,
4272                        (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
4273
4274                 RETURN_EXIT;
4275         }
4276
4277         /* child object regular file*/
4278
4279         if (!lod_object_will_be_striped(S_ISREG(child_mode),
4280                                         lu_object_fid(&child->do_lu)))
4281                 RETURN_EXIT;
4282
4283         /* If object is going to be striped over OSTs, transfer default
4284          * striping information to the child, so that we can use it
4285          * during declaration and creation.
4286          *
4287          * Try from the parent first.
4288          */
4289         if (likely(lp != NULL)) {
4290                 rc = lod_get_default_lov_striping(env, lp, lds);
4291                 if (rc == 0)
4292                         lod_striping_from_default(lc, lds, child_mode);
4293         }
4294
4295         /* Initialize lod_device::lod_md_root object reference */
4296         if (d->lod_md_root == NULL) {
4297                 struct dt_object *root;
4298                 struct lod_object *lroot;
4299
4300                 lu_root_fid(&info->lti_fid);
4301                 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
4302                 if (!IS_ERR(root)) {
4303                         lroot = lod_dt_obj(root);
4304
4305                         spin_lock(&d->lod_lock);
4306                         if (d->lod_md_root != NULL)
4307                                 dt_object_put(env, &d->lod_md_root->ldo_obj);
4308                         d->lod_md_root = lroot;
4309                         spin_unlock(&d->lod_lock);
4310                 }
4311         }
4312
4313         /* try inherit layout from the root object (fs default) when:
4314          *  - parent does not have default layout; or
4315          *  - parent has plain(v1/v3) default layout, and some attributes
4316          *    are not specified in the default layout;
4317          */
4318         if (d->lod_md_root != NULL && lod_need_inherit_more(lc, true)) {
4319                 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds);
4320                 if (rc)
4321                         goto out;
4322                 if (lc->ldo_comp_cnt == 0) {
4323                         lod_striping_from_default(lc, lds, child_mode);
4324                 } else if (!lds->lds_def_striping_is_composite) {
4325                         struct lod_layout_component *def_comp;
4326
4327                         LASSERT(!lc->ldo_is_composite);
4328                         lod_comp = &lc->ldo_comp_entries[0];
4329                         def_comp = &lds->lds_def_comp_entries[0];
4330
4331                         if (lod_comp->llc_stripe_count <= 0)
4332                                 lod_comp->llc_stripe_count =
4333                                         def_comp->llc_stripe_count;
4334                         if (lod_comp->llc_stripe_size <= 0)
4335                                 lod_comp->llc_stripe_size =
4336                                         def_comp->llc_stripe_size;
4337                         if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
4338                                 lod_comp->llc_stripe_offset =
4339                                         def_comp->llc_stripe_offset;
4340                         if (lod_comp->llc_pool == NULL)
4341                                 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
4342                 }
4343         }
4344 out:
4345         /*
4346          * fs default striping may not be explicitly set, or historically set
4347          * in config log, use them.
4348          */
4349         if (lod_need_inherit_more(lc, false)) {
4350                 if (lc->ldo_comp_cnt == 0) {
4351                         rc = lod_alloc_comp_entries(lc, 0, 1);
4352                         if (rc)
4353                                 /* fail to allocate memory, will create a
4354                                  * non-striped file. */
4355                                 RETURN_EXIT;
4356                         lc->ldo_is_composite = 0;
4357                         lod_comp = &lc->ldo_comp_entries[0];
4358                         lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4359                 }
4360                 LASSERT(!lc->ldo_is_composite);
4361                 lod_comp = &lc->ldo_comp_entries[0];
4362                 desc = &d->lod_desc;
4363                 if (lod_comp->llc_stripe_count <= 0)
4364                         lod_comp->llc_stripe_count =
4365                                 desc->ld_default_stripe_count;
4366                 if (lod_comp->llc_stripe_size <= 0)
4367                         lod_comp->llc_stripe_size =
4368                                 desc->ld_default_stripe_size;
4369         }
4370
4371         EXIT;
4372 }
4373
4374 #define ll_do_div64(aaa,bbb)    do_div((aaa), (bbb))
4375 /**
4376  * Size initialization on late striping.
4377  *
4378  * Propagate the size of a truncated object to a deferred striping.
4379  * This function handles a special case when truncate was done on a
4380  * non-striped object and now while the striping is being created
4381  * we can't lose that size, so we have to propagate it to the stripes
4382  * being created.
4383  *
4384  * \param[in] env       execution environment
4385  * \param[in] dt        object
4386  * \param[in] th        transaction handle
4387  *
4388  * \retval              0 on success
4389  * \retval              negative if failed
4390  */
4391 static int lod_declare_init_size(const struct lu_env *env,
4392                                  struct dt_object *dt, struct thandle *th)
4393 {
4394         struct dt_object        *next = dt_object_child(dt);
4395         struct lod_object       *lo = lod_dt_obj(dt);
4396         struct dt_object        **objects = NULL;
4397         struct lu_attr  *attr = &lod_env_info(env)->lti_attr;
4398         uint64_t        size, offs;
4399         int     i, rc, stripe, stripe_count = 0, stripe_size = 0;
4400         struct lu_extent size_ext;
4401         ENTRY;
4402
4403         if (!lod_obj_is_striped(dt))
4404                 RETURN(0);
4405
4406         rc = dt_attr_get(env, next, attr);
4407         LASSERT(attr->la_valid & LA_SIZE);
4408         if (rc)
4409                 RETURN(rc);
4410
4411         size = attr->la_size;
4412         if (size == 0)
4413                 RETURN(0);
4414
4415         size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
4416         for (i = 0; i < lo->ldo_comp_cnt; i++) {
4417                 struct lod_layout_component *lod_comp;
4418                 struct lu_extent *extent;
4419
4420                 lod_comp = &lo->ldo_comp_entries[i];
4421
4422                 if (lod_comp->llc_stripe == NULL)
4423                         continue;
4424
4425                 extent = &lod_comp->llc_extent;
4426                 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
4427                 if (!lo->ldo_is_composite ||
4428                     lu_extent_is_overlapped(extent, &size_ext)) {
4429                         objects = lod_comp->llc_stripe;
4430                         stripe_count = lod_comp->llc_stripe_count;
4431                         stripe_size = lod_comp->llc_stripe_size;
4432
4433                         /* next mirror */
4434                         if (stripe_count == 0)
4435                                 continue;
4436
4437                         LASSERT(objects != NULL && stripe_size != 0);
4438                         /* ll_do_div64(a, b) returns a % b, and a = a / b */
4439                         ll_do_div64(size, (__u64)stripe_size);
4440                         stripe = ll_do_div64(size, (__u64)stripe_count);
4441                         LASSERT(objects[stripe] != NULL);
4442
4443                         size = size * stripe_size;
4444                         offs = attr->la_size;
4445                         size += ll_do_div64(offs, stripe_size);
4446
4447                         attr->la_valid = LA_SIZE;
4448                         attr->la_size = size;
4449
4450                         rc = lod_sub_declare_attr_set(env, objects[stripe],
4451                                                       attr, th);
4452                 }
4453         }
4454
4455         RETURN(rc);
4456 }
4457
4458 /**
4459  * Declare creation of striped object.
4460  *
4461  * The function declares creation stripes for a regular object. The function
4462  * also declares whether the stripes will be created with non-zero size if
4463  * previously size was set non-zero on the master object. If object \a dt is
4464  * not local, then only fully defined striping can be applied in \a lovea.
4465  * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
4466  * for the details.
4467  *
4468  * \param[in] env       execution environment
4469  * \param[in] dt        object
4470  * \param[in] attr      attributes the stripes will be created with
4471  * \param[in] lovea     a buffer containing striping description
4472  * \param[in] th        transaction handle
4473  *
4474  * \retval              0 on success
4475  * \retval              negative if failed
4476  */
4477 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
4478                                struct lu_attr *attr,
4479                                const struct lu_buf *lovea, struct thandle *th)
4480 {
4481         struct lod_thread_info  *info = lod_env_info(env);
4482         struct dt_object        *next = dt_object_child(dt);
4483         struct lod_object       *lo = lod_dt_obj(dt);
4484         int                      rc;
4485         ENTRY;
4486
4487         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
4488                 GOTO(out, rc = -ENOMEM);
4489
4490         if (!dt_object_remote(next)) {
4491                 /* choose OST and generate appropriate objects */
4492                 rc = lod_prepare_create(env, lo, attr, lovea, th);
4493                 if (rc)
4494                         GOTO(out, rc);
4495
4496                 /*
4497                  * declare storage for striping data
4498                  */
4499                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
4500         } else {
4501                 /* LOD can not choose OST objects for remote objects, i.e.
4502                  * stripes must be ready before that. Right now, it can only
4503                  * happen during migrate, i.e. migrate process needs to create
4504                  * remote regular file (mdd_migrate_create), then the migrate
4505                  * process will provide stripeEA. */
4506                 LASSERT(lovea != NULL);
4507                 info->lti_buf = *lovea;
4508         }
4509
4510         rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
4511                                        XATTR_NAME_LOV, 0, th);
4512         if (rc)
4513                 GOTO(out, rc);
4514
4515         /*
4516          * if striping is created with local object's size > 0,
4517          * we have to propagate this size to specific object
4518          * the case is possible only when local object was created previously
4519          */
4520         if (dt_object_exists(next))
4521                 rc = lod_declare_init_size(env, dt, th);
4522
4523 out:
4524         /* failed to create striping or to set initial size, let's reset
4525          * config so that others don't get confused */
4526         if (rc)
4527                 lod_object_free_striping(env, lo);
4528
4529         RETURN(rc);
4530 }
4531
4532 /**
4533  * Implementation of dt_object_operations::do_declare_create.
4534  *
4535  * The method declares creation of a new object. If the object will be striped,
4536  * then helper functions are called to find FIDs for the stripes, declare
4537  * creation of the stripes and declare initialization of the striping
4538  * information to be stored in the master object.
4539  *
4540  * \see dt_object_operations::do_declare_create() in the API description
4541  * for details.
4542  */
4543 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
4544                               struct lu_attr *attr,
4545                               struct dt_allocation_hint *hint,
4546                               struct dt_object_format *dof, struct thandle *th)
4547 {
4548         struct dt_object   *next = dt_object_child(dt);
4549         struct lod_object  *lo = lod_dt_obj(dt);
4550         int                 rc;
4551         ENTRY;
4552
4553         LASSERT(dof);
4554         LASSERT(attr);
4555         LASSERT(th);
4556
4557         /*
4558          * first of all, we declare creation of local object
4559          */
4560         rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
4561         if (rc != 0)
4562                 GOTO(out, rc);
4563
4564         /*
4565          * it's lod_ah_init() that has decided the object will be striped
4566          */
4567         if (dof->dof_type == DFT_REGULAR) {
4568                 /* callers don't want stripes */
4569                 /* XXX: all tricky interactions with ->ah_make_hint() decided
4570                  * to use striping, then ->declare_create() behaving differently
4571                  * should be cleaned */
4572                 if (dof->u.dof_reg.striped != 0)
4573                         rc = lod_declare_striped_create(env, dt, attr,
4574                                                         NULL, th);
4575         } else if (dof->dof_type == DFT_DIR) {
4576                 struct seq_server_site *ss;
4577                 struct lu_buf buf = { NULL };
4578                 struct lu_buf *lmu = NULL;
4579
4580                 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
4581
4582                 /* If the parent has default stripeEA, and client
4583                  * did not find it before sending create request,
4584                  * then MDT will return -EREMOTE, and client will
4585                  * retrieve the default stripeEA and re-create the
4586                  * sub directory.
4587                  *
4588                  * Note: if dah_eadata != NULL, it means creating the
4589                  * striped directory with specified stripeEA, then it
4590                  * should ignore the default stripeEA */
4591                 if (hint != NULL && hint->dah_eadata == NULL) {
4592                         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
4593                                 GOTO(out, rc = -EREMOTE);
4594
4595                         if (lo->ldo_dir_stripe_offset == -1) {
4596                                 /* child and parent should be in the same MDT */
4597                                 if (hint->dah_parent != NULL &&
4598                                     dt_object_remote(hint->dah_parent))
4599                                         GOTO(out, rc = -EREMOTE);
4600                         } else if (lo->ldo_dir_stripe_offset !=
4601                                    ss->ss_node_id) {
4602                                 struct lod_device *lod;
4603                                 struct lod_tgt_descs *ltd;
4604                                 struct lod_tgt_desc *tgt = NULL;
4605                                 bool found_mdt = false;
4606                                 int i;
4607
4608                                 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4609                                 ltd = &lod->lod_mdt_descs;
4610                                 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
4611                                         tgt = LTD_TGT(ltd, i);
4612                                         if (tgt->ltd_index ==
4613                                                 lo->ldo_dir_stripe_offset) {
4614                                                 found_mdt = true;
4615                                                 break;
4616                                         }
4617                                 }
4618
4619                                 /* If the MDT indicated by stripe_offset can be
4620                                  * found, then tell client to resend the create
4621                                  * request to the correct MDT, otherwise return
4622                                  * error to client */
4623                                 if (found_mdt)
4624                                         GOTO(out, rc = -EREMOTE);
4625                                 else
4626                                         GOTO(out, rc = -EINVAL);
4627                         }
4628                 } else if (hint && hint->dah_eadata) {
4629                         lmu = &buf;
4630                         lmu->lb_buf = (void *)hint->dah_eadata;
4631                         lmu->lb_len = hint->dah_eadata_len;
4632                 }
4633
4634                 rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
4635                                                      th);
4636         }
4637 out:
4638         /* failed to create striping or to set initial size, let's reset
4639          * config so that others don't get confused */
4640         if (rc)
4641                 lod_object_free_striping(env, lo);
4642         RETURN(rc);
4643 }
4644
4645 /**
4646  * Generate component ID for new created component.
4647  *
4648  * \param[in] lo                LOD object
4649  * \param[in] comp_idx          index of ldo_comp_entries
4650  *
4651  * \retval                      component ID on success
4652  * \retval                      LCME_ID_INVAL on failure
4653  */
4654 static __u32 lod_gen_component_id(struct lod_object *lo,
4655                                   int mirror_id, int comp_idx)
4656 {
4657         struct lod_layout_component *lod_comp;
4658         __u32   id, start, end;
4659         int     i;
4660
4661         LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
4662
4663         lod_obj_inc_layout_gen(lo);
4664         id = lo->ldo_layout_gen;
4665         if (likely(id <= SEQ_ID_MAX))
4666                 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
4667
4668         /* Layout generation wraps, need to check collisions. */
4669         start = id & SEQ_ID_MASK;
4670         end = SEQ_ID_MAX;
4671 again:
4672         for (id = start; id <= end; id++) {
4673                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4674                         lod_comp = &lo->ldo_comp_entries[i];
4675                         if (pflr_id(mirror_id, id) == lod_comp->llc_id)
4676                                 break;
4677                 }
4678                 /* Found the ununsed ID */
4679                 if (i == lo->ldo_comp_cnt)
4680                         RETURN(pflr_id(mirror_id, id));
4681         }
4682         if (end == LCME_ID_MAX) {
4683                 start = 1;
4684                 end = min(lo->ldo_layout_gen & LCME_ID_MASK,
4685                           (__u32)(LCME_ID_MAX - 1));
4686                 goto again;
4687         }
4688
4689         RETURN(LCME_ID_INVAL);
4690 }
4691
4692 /**
4693  * Creation of a striped regular object.
4694  *
4695  * The function is called to create the stripe objects for a regular
4696  * striped file. This can happen at the initial object creation or
4697  * when the caller asks LOD to do so using ->do_xattr_set() method
4698  * (so called late striping). Notice all the information are already
4699  * prepared in the form of the list of objects (ldo_stripe field).
4700  * This is done during declare phase.
4701  *
4702  * \param[in] env       execution environment
4703  * \param[in] dt        object
4704  * \param[in] attr      attributes the stripes will be created with
4705  * \param[in] dof       format of stripes (see OSD API description)
4706  * \param[in] th        transaction handle
4707  *
4708  * \retval              0 on success
4709  * \retval              negative if failed
4710  */
4711 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
4712                        struct lu_attr *attr, struct dt_object_format *dof,
4713                        struct thandle *th)
4714 {
4715         struct lod_layout_component     *lod_comp;
4716         struct lod_object       *lo = lod_dt_obj(dt);
4717         __u16   mirror_id;
4718         int     rc = 0, i, j;
4719         ENTRY;
4720
4721         LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
4722
4723         mirror_id = lo->ldo_mirror_count > 1 ? 1 : 0;
4724
4725         /* create all underlying objects */
4726         for (i = 0; i < lo->ldo_comp_cnt; i++) {
4727                 lod_comp = &lo->ldo_comp_entries[i];
4728
4729                 if (lod_comp->llc_extent.e_start == 0 && i > 0) /* new mirror */
4730                         ++mirror_id;
4731
4732                 if (lod_comp->llc_id == LCME_ID_INVAL) {
4733                         lod_comp->llc_id = lod_gen_component_id(lo,
4734                                                                 mirror_id, i);
4735                         if (lod_comp->llc_id == LCME_ID_INVAL)
4736                                 GOTO(out, rc = -ERANGE);
4737                 }
4738
4739                 if (lod_comp_inited(lod_comp))
4740                         continue;
4741
4742                 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
4743                         lod_comp_set_init(lod_comp);
4744
4745                 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
4746                         lod_comp_set_init(lod_comp);
4747
4748                 if (lod_comp->llc_stripe == NULL)
4749                         continue;
4750
4751                 LASSERT(lod_comp->llc_stripe_count);
4752                 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4753                         struct dt_object *object = lod_comp->llc_stripe[j];
4754                         LASSERT(object != NULL);
4755                         rc = lod_sub_create(env, object, attr, NULL, dof, th);
4756                         if (rc)
4757                                 GOTO(out, rc);
4758                 }
4759                 lod_comp_set_init(lod_comp);
4760         }
4761
4762         rc = lod_fill_mirrors(lo);
4763         if (rc)
4764                 GOTO(out, rc);
4765
4766         rc = lod_generate_and_set_lovea(env, lo, th);
4767         if (rc)
4768                 GOTO(out, rc);
4769
4770         lo->ldo_comp_cached = 1;
4771         RETURN(0);
4772
4773 out:
4774         lod_object_free_striping(env, lo);
4775         RETURN(rc);
4776 }
4777
4778 /**
4779  * Implementation of dt_object_operations::do_create.
4780  *
4781  * If any of preceeding methods (like ->do_declare_create(),
4782  * ->do_ah_init(), etc) chose to create a striped object,
4783  * then this method will create the master and the stripes.
4784  *
4785  * \see dt_object_operations::do_create() in the API description for details.
4786  */
4787 static int lod_create(const struct lu_env *env, struct dt_object *dt,
4788                       struct lu_attr *attr, struct dt_allocation_hint *hint,
4789                       struct dt_object_format *dof, struct thandle *th)
4790 {
4791         int                 rc;
4792         ENTRY;
4793
4794         /* create local object */
4795         rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
4796         if (rc != 0)
4797                 RETURN(rc);
4798
4799         if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4800             lod_obj_is_striped(dt) && dof->u.dof_reg.striped != 0) {
4801                 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
4802                 rc = lod_striped_create(env, dt, attr, dof, th);
4803         }
4804
4805         RETURN(rc);
4806 }
4807
4808 static inline int
4809 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
4810                           struct dt_object *dt, struct thandle *th,
4811                           int comp_idx, int stripe_idx,
4812                           struct lod_obj_stripe_cb_data *data)
4813 {
4814         if (data->locd_declare)
4815                 return lod_sub_declare_destroy(env, dt, th);
4816         else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4817                  stripe_idx == cfs_fail_val)
4818                 return lod_sub_destroy(env, dt, th);
4819         else
4820                 return 0;
4821 }
4822
4823 /**
4824  * Implementation of dt_object_operations::do_declare_destroy.
4825  *
4826  * If the object is a striped directory, then the function declares reference
4827  * removal from the master object (this is an index) to the stripes and declares
4828  * destroy of all the stripes. In all the cases, it declares an intention to
4829  * destroy the object itself.
4830  *
4831  * \see dt_object_operations::do_declare_destroy() in the API description
4832  * for details.
4833  */
4834 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
4835                                struct thandle *th)
4836 {
4837         struct dt_object   *next = dt_object_child(dt);
4838         struct lod_object  *lo = lod_dt_obj(dt);
4839         struct lod_thread_info *info = lod_env_info(env);
4840         char               *stripe_name = info->lti_key;
4841         int                 rc, i;
4842         ENTRY;
4843
4844         /*
4845          * load striping information, notice we don't do this when object
4846          * is being initialized as we don't need this information till
4847          * few specific cases like destroy, chown
4848          */
4849         rc = lod_load_striping(env, lo);
4850         if (rc)
4851                 RETURN(rc);
4852
4853         /* declare destroy for all underlying objects */
4854         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4855                 rc = next->do_ops->do_index_try(env, next,
4856                                                 &dt_directory_features);
4857                 if (rc != 0)
4858                         RETURN(rc);
4859
4860                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4861                         rc = lod_sub_declare_ref_del(env, next, th);
4862                         if (rc != 0)
4863                                 RETURN(rc);
4864
4865                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4866                                 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4867                                 i);
4868                         rc = lod_sub_declare_delete(env, next,
4869                                         (const struct dt_key *)stripe_name, th);
4870                         if (rc != 0)
4871                                 RETURN(rc);
4872                 }
4873         }
4874
4875         /*
4876          * we declare destroy for the local object
4877          */
4878         rc = lod_sub_declare_destroy(env, next, th);
4879         if (rc)
4880                 RETURN(rc);
4881
4882         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4883             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4884                 RETURN(0);
4885
4886         if (!lod_obj_is_striped(dt))
4887                 RETURN(0);
4888
4889         /* declare destroy all striped objects */
4890         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4891                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4892                         if (lo->ldo_stripe[i] == NULL)
4893                                 continue;
4894
4895                         rc = lod_sub_declare_ref_del(env, lo->ldo_stripe[i],
4896                                                      th);
4897
4898                         rc = lod_sub_declare_destroy(env, lo->ldo_stripe[i],
4899                                                      th);
4900                         if (rc != 0)
4901                                 break;
4902                 }
4903         } else {
4904                 struct lod_obj_stripe_cb_data data = { { 0 } };
4905
4906                 data.locd_declare = true;
4907                 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
4908                 rc = lod_obj_for_each_stripe(env, lo, th, &data);
4909         }
4910
4911         RETURN(rc);
4912 }
4913
4914 /**
4915  * Implementation of dt_object_operations::do_destroy.
4916  *
4917  * If the object is a striped directory, then the function removes references
4918  * from the master object (this is an index) to the stripes and destroys all
4919  * the stripes. In all the cases, the function destroys the object itself.
4920  *
4921  * \see dt_object_operations::do_destroy() in the API description for details.
4922  */
4923 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
4924                        struct thandle *th)
4925 {
4926         struct dt_object  *next = dt_object_child(dt);
4927         struct lod_object *lo = lod_dt_obj(dt);
4928         struct lod_thread_info *info = lod_env_info(env);
4929         char               *stripe_name = info->lti_key;
4930         unsigned int       i;
4931         int                rc;
4932         ENTRY;
4933
4934         /* destroy sub-stripe of master object */
4935         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4936                 rc = next->do_ops->do_index_try(env, next,
4937                                                 &dt_directory_features);
4938                 if (rc != 0)
4939                         RETURN(rc);
4940
4941                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4942                         rc = lod_sub_ref_del(env, next, th);
4943                         if (rc != 0)
4944                                 RETURN(rc);
4945
4946                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4947                                 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4948                                 i);
4949
4950                         CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
4951                                PFID(lu_object_fid(&dt->do_lu)), stripe_name,
4952                                PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
4953
4954                         rc = lod_sub_delete(env, next,
4955                                        (const struct dt_key *)stripe_name, th);
4956                         if (rc != 0)
4957                                 RETURN(rc);
4958                 }
4959         }
4960
4961         rc = lod_sub_destroy(env, next, th);
4962         if (rc != 0)
4963                 RETURN(rc);
4964
4965         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4966             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4967                 RETURN(0);
4968
4969         if (!lod_obj_is_striped(dt))
4970                 RETURN(0);
4971
4972         /* destroy all striped objects */
4973         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4974                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4975                         if (lo->ldo_stripe[i] == NULL)
4976                                 continue;
4977                         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4978                             i == cfs_fail_val) {
4979                                 dt_write_lock(env, lo->ldo_stripe[i],
4980                                               MOR_TGT_CHILD);
4981                                 rc = lod_sub_ref_del(env, lo->ldo_stripe[i],
4982                                                      th);
4983                                 dt_write_unlock(env, lo->ldo_stripe[i]);
4984                                 if (rc != 0)
4985                                         break;
4986
4987                                 rc = lod_sub_destroy(env, lo->ldo_stripe[i],
4988                                                      th);
4989                                 if (rc != 0)
4990                                         break;
4991                         }
4992                 }
4993         } else {
4994                 struct lod_obj_stripe_cb_data data = { { 0 } };
4995
4996                 data.locd_declare = false;
4997                 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
4998                 rc = lod_obj_for_each_stripe(env, lo, th, &data);
4999         }
5000
5001         RETURN(rc);
5002 }
5003
5004 /**
5005  * Implementation of dt_object_operations::do_declare_ref_add.
5006  *
5007  * \see dt_object_operations::do_declare_ref_add() in the API description
5008  * for details.
5009  */
5010 static int lod_declare_ref_add(const struct lu_env *env,
5011                                struct dt_object *dt, struct thandle *th)
5012 {
5013         return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
5014 }
5015
5016 /**
5017  * Implementation of dt_object_operations::do_ref_add.
5018  *
5019  * \see dt_object_operations::do_ref_add() in the API description for details.
5020  */
5021 static int lod_ref_add(const struct lu_env *env,
5022                        struct dt_object *dt, struct thandle *th)
5023 {
5024         return lod_sub_ref_add(env, dt_object_child(dt), th);
5025 }
5026
5027 /**
5028  * Implementation of dt_object_operations::do_declare_ref_del.
5029  *
5030  * \see dt_object_operations::do_declare_ref_del() in the API description
5031  * for details.
5032  */
5033 static int lod_declare_ref_del(const struct lu_env *env,
5034                                struct dt_object *dt, struct thandle *th)
5035 {
5036         return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
5037 }
5038
5039 /**
5040  * Implementation of dt_object_operations::do_ref_del
5041  *
5042  * \see dt_object_operations::do_ref_del() in the API description for details.
5043  */
5044 static int lod_ref_del(const struct lu_env *env,
5045                        struct dt_object *dt, struct thandle *th)
5046 {
5047         return lod_sub_ref_del(env, dt_object_child(dt), th);
5048 }
5049
5050 /**
5051  * Implementation of dt_object_operations::do_object_sync.
5052  *
5053  * \see dt_object_operations::do_object_sync() in the API description
5054  * for details.
5055  */
5056 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
5057                            __u64 start, __u64 end)
5058 {
5059         return dt_object_sync(env, dt_object_child(dt), start, end);
5060 }
5061
5062 /**
5063  * Release LDLM locks on the stripes of a striped directory.
5064  *
5065  * Iterates over all the locks taken on the stripe objects and
5066  * cancel them.
5067  *
5068  * \param[in] env       execution environment
5069  * \param[in] dt        striped object
5070  * \param[in] einfo     lock description
5071  * \param[in] policy    data describing requested lock
5072  *
5073  * \retval              0 on success
5074  * \retval              negative if failed
5075  */
5076 static int lod_object_unlock_internal(const struct lu_env *env,
5077                                       struct dt_object *dt,
5078                                       struct ldlm_enqueue_info *einfo,
5079                                       union ldlm_policy_data *policy)
5080 {
5081         struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
5082         int                     rc = 0;
5083         int                     i;
5084         ENTRY;
5085
5086         if (slave_locks == NULL)
5087                 RETURN(0);
5088
5089         for (i = 1; i < slave_locks->count; i++) {
5090                 if (lustre_handle_is_used(&slave_locks->handles[i]))
5091                         ldlm_lock_decref_and_cancel(&slave_locks->handles[i],
5092                                                     einfo->ei_mode);
5093         }
5094
5095         RETURN(rc);
5096 }
5097
5098 /**
5099  * Implementation of dt_object_operations::do_object_unlock.
5100  *
5101  * Used to release LDLM lock(s).
5102  *
5103  * \see dt_object_operations::do_object_unlock() in the API description
5104  * for details.
5105  */
5106 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
5107                              struct ldlm_enqueue_info *einfo,
5108                              union ldlm_policy_data *policy)
5109 {
5110         struct lod_object *lo = lod_dt_obj(dt);
5111         struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
5112         int slave_locks_size;
5113         int i;
5114         ENTRY;
5115
5116         if (slave_locks == NULL)
5117                 RETURN(0);
5118
5119         LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
5120         LASSERT(lo->ldo_dir_stripe_count > 1);
5121         /* Note: for remote lock for single stripe dir, MDT will cancel
5122          * the lock by lockh directly */
5123         LASSERT(!dt_object_remote(dt_object_child(dt)));
5124
5125         /* locks were unlocked in MDT layer */
5126         for (i = 1; i < slave_locks->count; i++) {
5127                 LASSERT(!lustre_handle_is_used(&slave_locks->handles[i]));
5128                 dt_invalidate(env, lo->ldo_stripe[i]);
5129         }
5130
5131         slave_locks_size = sizeof(*slave_locks) + slave_locks->count *
5132                            sizeof(slave_locks->handles[0]);
5133         OBD_FREE(slave_locks, slave_locks_size);
5134         einfo->ei_cbdata = NULL;
5135
5136         RETURN(0);
5137 }
5138
5139 /**
5140  * Implementation of dt_object_operations::do_object_lock.
5141  *
5142  * Used to get LDLM lock on the non-striped and striped objects.
5143  *
5144  * \see dt_object_operations::do_object_lock() in the API description
5145  * for details.
5146  */
5147 static int lod_object_lock(const struct lu_env *env,
5148                            struct dt_object *dt,
5149                            struct lustre_handle *lh,
5150                            struct ldlm_enqueue_info *einfo,
5151                            union ldlm_policy_data *policy)
5152 {
5153         struct lod_object       *lo = lod_dt_obj(dt);
5154         int                     rc = 0;
5155         int                     i;
5156         int                     slave_locks_size;
5157         struct lustre_handle_array *slave_locks = NULL;
5158         ENTRY;
5159
5160         /* remote object lock */
5161         if (!einfo->ei_enq_slave) {
5162                 LASSERT(dt_object_remote(dt));
5163                 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
5164                                       policy);
5165         }
5166
5167         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
5168                 GOTO(out, rc = -ENOTDIR);
5169
5170         rc = lod_load_striping(env, lo);
5171         if (rc != 0)
5172                 GOTO(out, rc);
5173
5174         /* No stripes */
5175         if (lo->ldo_dir_stripe_count <= 1) {
5176                 /*
5177                  * NB, ei_cbdata stores pointer to slave locks, if no locks
5178                  * taken, make sure it's set to NULL, otherwise MDT will try to
5179                  * unlock them.
5180                  */
5181                 einfo->ei_cbdata = NULL;
5182                 GOTO(out, rc = 0);
5183         }
5184
5185         slave_locks_size = sizeof(*slave_locks) + lo->ldo_dir_stripe_count *
5186                            sizeof(slave_locks->handles[0]);
5187         /* Freed in lod_object_unlock */
5188         OBD_ALLOC(slave_locks, slave_locks_size);
5189         if (slave_locks == NULL)
5190                 GOTO(out, rc = -ENOMEM);
5191         slave_locks->count = lo->ldo_dir_stripe_count;
5192
5193         /* striped directory lock */
5194         for (i = 1; i < lo->ldo_dir_stripe_count; i++) {
5195                 struct lustre_handle    lockh;
5196                 struct ldlm_res_id      *res_id;
5197
5198                 res_id = &lod_env_info(env)->lti_res_id;
5199                 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
5200                                        res_id);
5201                 einfo->ei_res_id = res_id;
5202
5203                 LASSERT(lo->ldo_stripe[i] != NULL);
5204                 if (likely(dt_object_remote(lo->ldo_stripe[i]))) {
5205                         rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh,
5206                                             einfo, policy);
5207                 } else {
5208                         struct ldlm_namespace *ns = einfo->ei_namespace;
5209                         ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
5210                         ldlm_completion_callback completion = einfo->ei_cb_cp;
5211                         __u64   dlmflags = LDLM_FL_ATOMIC_CB;
5212
5213                         if (einfo->ei_mode == LCK_PW ||
5214                             einfo->ei_mode == LCK_EX)
5215                                 dlmflags |= LDLM_FL_COS_INCOMPAT;
5216
5217                         /* This only happens if there are mulitple stripes
5218                          * on the master MDT, i.e. except stripe0, there are
5219                          * other stripes on the Master MDT as well, Only
5220                          * happens in the test case right now. */
5221                         LASSERT(ns != NULL);
5222                         rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS,
5223                                                     policy, einfo->ei_mode,
5224                                                     &dlmflags, blocking,
5225                                                     completion, NULL,
5226                                                     NULL, 0, LVB_T_NONE,
5227                                                     NULL, &lockh);
5228                 }
5229                 if (rc != 0)
5230                         break;
5231                 slave_locks->handles[i] = lockh;
5232         }
5233         einfo->ei_cbdata = slave_locks;
5234
5235         if (rc != 0 && slave_locks != NULL) {
5236                 lod_object_unlock_internal(env, dt, einfo, policy);
5237                 OBD_FREE(slave_locks, slave_locks_size);
5238         }
5239         EXIT;
5240 out:
5241         if (rc != 0)
5242                 einfo->ei_cbdata = NULL;
5243         RETURN(rc);
5244 }
5245
5246 /**
5247  * Implementation of dt_object_operations::do_invalidate.
5248  *
5249  * \see dt_object_operations::do_invalidate() in the API description for details
5250  */
5251 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
5252 {
5253         return dt_invalidate(env, dt_object_child(dt));
5254 }
5255
5256 static int lod_layout_data_init(struct lod_thread_info *info, __u32 comp_cnt)
5257 {
5258         ENTRY;
5259
5260         /* clear memory region that will be used for layout change */
5261         memset(&info->lti_layout_attr, 0, sizeof(struct lu_attr));
5262         info->lti_count = 0;
5263
5264         if (info->lti_comp_size >= comp_cnt)
5265                 RETURN(0);
5266
5267         if (info->lti_comp_size > 0) {
5268                 OBD_FREE(info->lti_comp_idx,
5269                          info->lti_comp_size * sizeof(__u32));
5270                 info->lti_comp_size = 0;
5271         }
5272
5273         OBD_ALLOC(info->lti_comp_idx, comp_cnt * sizeof(__u32));
5274         if (!info->lti_comp_idx)
5275                 RETURN(-ENOMEM);
5276
5277         info->lti_comp_size = comp_cnt;
5278         RETURN(0);
5279 }
5280
5281 static int lod_declare_instantiate_components(const struct lu_env *env,
5282                 struct lod_object *lo, struct thandle *th)
5283 {
5284         struct lod_thread_info *info = lod_env_info(env);
5285         struct ost_pool *inuse = &info->lti_inuse_osts;
5286         int i;
5287         int rc = 0;
5288         ENTRY;
5289
5290         LASSERT(info->lti_count < lo->ldo_comp_cnt);
5291         if (info->lti_count > 0) {
5292                 /* Prepare inuse array for composite file */
5293                 rc = lod_prepare_inuse(env, lo);
5294                 if (rc)
5295                         RETURN(rc);
5296         }
5297
5298         for (i = 0; i < info->lti_count; i++) {
5299                 rc = lod_qos_prep_create(env, lo, NULL, th,
5300                                          info->lti_comp_idx[i], inuse);
5301                 if (rc)
5302                         break;
5303         }
5304
5305         if (!rc) {
5306                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5307                 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
5308                                 &info->lti_buf, XATTR_NAME_LOV, 0, th);
5309         }
5310
5311         RETURN(rc);
5312 }
5313
5314 static int lod_declare_update_plain(const struct lu_env *env,
5315                 struct lod_object *lo, struct layout_intent *layout,
5316                 const struct lu_buf *buf, struct thandle *th)
5317 {
5318         struct lod_thread_info *info = lod_env_info(env);
5319         struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5320         struct lod_layout_component *lod_comp;
5321         struct lov_comp_md_v1 *comp_v1 = NULL;
5322         bool replay = false;
5323         int i, rc;
5324         ENTRY;
5325
5326         LASSERT(lo->ldo_flr_state == LCM_FL_NOT_FLR);
5327
5328         /*
5329          * In case the client is passing lovea, which only happens during
5330          * the replay of layout intent write RPC for now, we may need to
5331          * parse the lovea and apply new layout configuration.
5332          */
5333         if (buf && buf->lb_len)  {
5334                 struct lov_user_md_v1 *v1 = buf->lb_buf;
5335
5336                 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
5337                     v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
5338                                               LOV_MAGIC_COMP_V1)) {
5339                         CERROR("%s: the replay buffer of layout extend "
5340                                "(magic %#x) does not contain expected "
5341                                "composite layout.\n",
5342                                lod2obd(d)->obd_name, v1->lmm_magic);
5343                         GOTO(out, rc = -EINVAL);
5344                 }
5345
5346                 lod_object_free_striping(env, lo);
5347                 rc = lod_use_defined_striping(env, lo, buf);
5348                 if (rc)
5349                         GOTO(out, rc);
5350
5351                 rc = lod_get_lov_ea(env, lo);
5352                 if (rc <= 0)
5353                         GOTO(out, rc);
5354                 /* old on-disk EA is stored in info->lti_buf */
5355                 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
5356                 replay = true;
5357         } else {
5358                 /* non replay path */
5359                 rc = lod_load_striping_locked(env, lo);
5360                 if (rc)
5361                         GOTO(out, rc);
5362         }
5363
5364         if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
5365                 /**
5366                  * trunc transfers [size, eof) in the intent extent, while
5367                  * we'd instantiated components covers [0, size).
5368                  */
5369                 layout->li_extent.e_end = layout->li_extent.e_start;
5370                 layout->li_extent.e_start = 0;
5371         }
5372
5373         /* Make sure defined layout covers the requested write range. */
5374         lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
5375         if (lo->ldo_comp_cnt > 1 &&
5376             lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
5377             lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
5378                 CDEBUG(replay ? D_ERROR : D_LAYOUT,
5379                        "%s: the defined layout [0, %#llx) does not covers "
5380                        "the write range "DEXT"\n",
5381                        lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
5382                        PEXT(&layout->li_extent));
5383                 GOTO(out, rc = -EINVAL);
5384         }
5385
5386         CDEBUG(D_LAYOUT, "%s: "DFID": instantiate components "DEXT"\n",
5387                lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
5388                PEXT(&layout->li_extent));
5389
5390         /*
5391          * Iterate ld->ldo_comp_entries, find the component whose extent under
5392          * the write range and not instantianted.
5393          */
5394         for (i = 0; i < lo->ldo_comp_cnt; i++) {
5395                 lod_comp = &lo->ldo_comp_entries[i];
5396
5397                 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
5398                         break;
5399
5400                 if (!replay) {
5401                         if (lod_comp_inited(lod_comp))
5402                                 continue;
5403                 } else {
5404                         /**
5405                          * In replay path, lod_comp is the EA passed by
5406                          * client replay buffer,  comp_v1 is the pre-recovery
5407                          * on-disk EA, we'd sift out those components which
5408                          * were init-ed in the on-disk EA.
5409                          */
5410                         if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
5411                             LCME_FL_INIT)
5412                                 continue;
5413                 }
5414                 /*
5415                  * this component hasn't instantiated in normal path, or during
5416                  * replay it needs replay the instantiation.
5417                  */
5418
5419                 /* A released component is being extended */
5420                 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
5421                         GOTO(out, rc = -EINVAL);
5422
5423                 LASSERT(info->lti_comp_idx != NULL);
5424                 info->lti_comp_idx[info->lti_count++] = i;
5425         }
5426
5427         if (info->lti_count == 0)
5428                 RETURN(-EALREADY);
5429
5430         lod_obj_inc_layout_gen(lo);
5431         rc = lod_declare_instantiate_components(env, lo, th);
5432 out:
5433         if (rc)
5434                 lod_object_free_striping(env, lo);
5435         RETURN(rc);
5436 }
5437
5438 #define lod_foreach_mirror_comp(comp, lo, mirror_idx)                      \
5439 for (comp = &lo->ldo_comp_entries[lo->ldo_mirrors[mirror_idx].lme_start];  \
5440      comp <= &lo->ldo_comp_entries[lo->ldo_mirrors[mirror_idx].lme_end];   \
5441      comp++)
5442
5443 static inline int lod_comp_index(struct lod_object *lo,
5444                                  struct lod_layout_component *lod_comp)
5445 {
5446         LASSERT(lod_comp >= lo->ldo_comp_entries &&
5447                 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
5448
5449         return lod_comp - lo->ldo_comp_entries;
5450 }
5451
5452 /**
5453  * Stale other mirrors by writing extent.
5454  */
5455 static void lod_stale_components(struct lod_object *lo, int primary,
5456                                  struct lu_extent *extent)
5457 {
5458         struct lod_layout_component *pri_comp, *lod_comp;
5459         int i;
5460
5461         /* The writing extent decides which components in the primary
5462          * are affected... */
5463         CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
5464         lod_foreach_mirror_comp(pri_comp, lo, primary) {
5465                 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
5466                         continue;
5467
5468                 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
5469                        lod_comp_index(lo, pri_comp),
5470                        PEXT(&pri_comp->llc_extent));
5471
5472                 for (i = 0; i < lo->ldo_mirror_count; i++) {
5473                         if (i == primary)
5474                                 continue;
5475
5476                         /* ... and then stale other components that are
5477                          * overlapping with primary components */
5478                         lod_foreach_mirror_comp(lod_comp, lo, i) {
5479                                 if (!lu_extent_is_overlapped(
5480                                                         &pri_comp->llc_extent,
5481                                                         &lod_comp->llc_extent))
5482                                         continue;
5483
5484                                 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
5485                                       i, lod_comp_index(lo, lod_comp));
5486
5487                                 lod_comp->llc_flags |= LCME_FL_STALE;
5488                                 lo->ldo_mirrors[i].lme_stale = 1;
5489                         }
5490                 }
5491         }
5492 }
5493
5494 static int lod_declare_update_rdonly(const struct lu_env *env,
5495                 struct lod_object *lo, struct md_layout_change *mlc,
5496                 struct thandle *th)
5497 {
5498         struct lod_thread_info *info = lod_env_info(env);
5499         struct lu_attr *layout_attr = &info->lti_layout_attr;
5500         struct lod_layout_component *lod_comp;
5501         struct layout_intent *layout = mlc->mlc_intent;
5502         struct lu_extent extent = layout->li_extent;
5503         unsigned int seq = 0;
5504         int picked;
5505         int i;
5506         int rc;
5507         ENTRY;
5508
5509         LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE);
5510         LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
5511         LASSERT(lo->ldo_mirror_count > 0);
5512
5513         CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
5514                PFID(lod_object_fid(lo)), PEXT(&extent));
5515
5516         if (OBD_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
5517                 get_random_bytes(&seq, sizeof(seq));
5518                 seq %= lo->ldo_mirror_count;
5519         }
5520
5521         /**
5522          * Pick a mirror as the primary.
5523          * Now it only picks the first mirror, this algo can be
5524          * revised later after knowing the topology of cluster or
5525          * the availability of OSTs.
5526          */
5527         for (picked = -1, i = 0; i < lo->ldo_mirror_count; i++) {
5528                 int index = (i + seq) % lo->ldo_mirror_count;
5529
5530                 if (!lo->ldo_mirrors[index].lme_stale) {
5531                         picked = index;
5532                         break;
5533                 }
5534         }
5535         if (picked < 0) /* failed to pick a primary */
5536                 RETURN(-ENODATA);
5537
5538         CDEBUG(D_LAYOUT, DFID": picked mirror %u as primary\n",
5539                PFID(lod_object_fid(lo)), lo->ldo_mirrors[picked].lme_id);
5540
5541         /* stale overlapping components from other mirrors */
5542         lod_stale_components(lo, picked, &extent);
5543
5544         /* instantiate components for the picked mirror, start from 0 */
5545         if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
5546                 /**
5547                  * trunc transfers [size, eof) in the intent extent, we'd
5548                  * stale components overlapping [size, eof), while we'd
5549                  * instantiated components covers [0, size).
5550                  */
5551                 extent.e_end = extent.e_start;
5552         }
5553         extent.e_start = 0;
5554
5555         lod_foreach_mirror_comp(lod_comp, lo, picked) {
5556                 if (!lu_extent_is_overlapped(&extent,
5557                                              &lod_comp->llc_extent))
5558                         break;
5559
5560                 if (lod_comp_inited(lod_comp))
5561                         continue;
5562
5563                 CDEBUG(D_LAYOUT, "instantiate: %u / %u\n",
5564                        i, lod_comp_index(lo, lod_comp));
5565
5566                 info->lti_comp_idx[info->lti_count++] =
5567                                                 lod_comp_index(lo, lod_comp);
5568         }
5569
5570         lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
5571
5572         /* Reset the layout version once it's becoming too large.
5573          * This way it can make sure that the layout version is
5574          * monotonously increased in this writing era. */
5575         lod_obj_inc_layout_gen(lo);
5576         if (lo->ldo_layout_gen > (LCME_ID_MAX >> 1)) {
5577                 __u32 layout_version;
5578
5579                 cfs_get_random_bytes(&layout_version, sizeof(layout_version));
5580                 lo->ldo_layout_gen = layout_version & 0xffff;
5581         }
5582
5583         rc = lod_declare_instantiate_components(env, lo, th);
5584         if (rc)
5585                 GOTO(out, rc);
5586
5587         layout_attr->la_valid = LA_LAYOUT_VERSION;
5588         layout_attr->la_layout_version = 0; /* set current version */
5589         rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
5590         if (rc)
5591                 GOTO(out, rc);
5592
5593 out:
5594         if (rc)
5595                 lod_object_free_striping(env, lo);
5596         RETURN(rc);
5597 }
5598
5599 static int lod_declare_update_write_pending(const struct lu_env *env,
5600                 struct lod_object *lo, struct md_layout_change *mlc,
5601                 struct thandle *th)
5602 {
5603         struct lod_thread_info *info = lod_env_info(env);
5604         struct lu_attr *layout_attr = &info->lti_layout_attr;
5605         struct lod_layout_component *lod_comp;
5606         struct lu_extent extent = { 0 };
5607         int primary = -1;
5608         int i;
5609         int rc;
5610         ENTRY;
5611
5612         LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
5613         LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
5614                 mlc->mlc_opc == MD_LAYOUT_RESYNC);
5615
5616         /* look for the primary mirror */
5617         for (i = 0; i < lo->ldo_mirror_count; i++) {
5618                 if (lo->ldo_mirrors[i].lme_stale)
5619                         continue;
5620
5621                 LASSERTF(primary < 0, DFID " has multiple primary: %u / %u",
5622                          PFID(lod_object_fid(lo)),
5623                          lo->ldo_mirrors[i].lme_id,
5624                          lo->ldo_mirrors[primary].lme_id);
5625
5626                 primary = i;
5627         }
5628         if (primary < 0) {
5629                 CERROR(DFID ": doesn't have a primary mirror\n",
5630                        PFID(lod_object_fid(lo)));
5631                 GOTO(out, rc = -ENODATA);
5632         }
5633
5634         CDEBUG(D_LAYOUT, DFID": found primary %u\n",
5635                PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
5636
5637         LASSERT(!lo->ldo_mirrors[primary].lme_stale);
5638
5639         /* for LAYOUT_WRITE opc, it has to do the following operations:
5640          * 1. stale overlapping componets from stale mirrors;
5641          * 2. instantiate components of the primary mirror;
5642          * 3. transfter layout version to all objects of the primary;
5643          *
5644          * for LAYOUT_RESYNC opc, it will do:
5645          * 1. instantiate components of all stale mirrors;
5646          * 2. transfer layout version to all objects to close write era. */
5647
5648         if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
5649                 LASSERT(mlc->mlc_intent != NULL);
5650
5651                 extent = mlc->mlc_intent->li_extent;
5652
5653                 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
5654                        PFID(lod_object_fid(lo)), PEXT(&extent));
5655
5656                 /* 1. stale overlapping components */
5657                 lod_stale_components(lo, primary, &extent);
5658
5659                 /* 2. find out the components need instantiating.
5660                  * instantiate [0, mlc->mlc_intent->e_end) */
5661                 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
5662                         /**
5663                          * trunc transfers [size, eof) in the intent extent,
5664                          * we'd stale components overlapping [size, eof),
5665                          * while we'd instantiated components covers [0, size).
5666                          */
5667                         extent.e_end = extent.e_start;
5668                 }
5669                 extent.e_start = 0;
5670
5671                 lod_foreach_mirror_comp(lod_comp, lo, primary) {
5672                         if (!lu_extent_is_overlapped(&extent,
5673                                                      &lod_comp->llc_extent))
5674                                 break;
5675
5676                         if (lod_comp_inited(lod_comp))
5677                                 continue;
5678
5679                         CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
5680                                primary, lod_comp_index(lo, lod_comp));
5681                         info->lti_comp_idx[info->lti_count++] =
5682                                                 lod_comp_index(lo, lod_comp);
5683                 }
5684         } else { /* MD_LAYOUT_RESYNC */
5685                 /* figure out the components that have been instantiated in
5686                  * in primary to decide what components should be instantiated
5687                  * in stale mirrors */
5688                 lod_foreach_mirror_comp(lod_comp, lo, primary) {
5689                         if (!lod_comp_inited(lod_comp))
5690                                 break;
5691
5692                         extent.e_end = lod_comp->llc_extent.e_end;
5693                 }
5694
5695                 CDEBUG(D_LAYOUT,
5696                        DFID": instantiate all stale components in "DEXT"\n",
5697                        PFID(lod_object_fid(lo)), PEXT(&extent));
5698
5699                 /* 1. instantiate all components within this extent, even
5700                  * non-stale components so that it won't need to instantiate
5701                  * those components for mirror truncate later. */
5702                 for (i = 0; i < lo->ldo_mirror_count; i++) {
5703                         if (primary == i)
5704                                 continue;
5705
5706                         LASSERTF(lo->ldo_mirrors[i].lme_stale,
5707                                  "both %d and %d are primary\n", i, primary);
5708
5709                         lod_foreach_mirror_comp(lod_comp, lo, i) {
5710                                 if (!lu_extent_is_overlapped(&extent,
5711                                                         &lod_comp->llc_extent))
5712                                         break;
5713
5714                                 if (lod_comp_inited(lod_comp))
5715                                         continue;
5716
5717                                 CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
5718                                        i, lod_comp_index(lo, lod_comp));
5719
5720                                 info->lti_comp_idx[info->lti_count++] =
5721                                                 lod_comp_index(lo, lod_comp);
5722                         }
5723                 }
5724
5725                 /* change the file state to SYNC_PENDING */
5726                 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
5727         }
5728
5729         rc = lod_declare_instantiate_components(env, lo, th);
5730         if (rc)
5731                 GOTO(out, rc);
5732
5733         /* 3. transfer layout version to OST objects.
5734          * transfer new layout version to OST objects so that stale writes
5735          * can be denied. It also ends an era of writing by setting
5736          * LU_LAYOUT_RESYNC. Normal client can never use this bit to
5737          * send write RPC; only resync RPCs could do it. */
5738         layout_attr->la_valid = LA_LAYOUT_VERSION;
5739         layout_attr->la_layout_version = 0; /* set current version */
5740         if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
5741                 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
5742         rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
5743         if (rc)
5744                 GOTO(out, rc);
5745
5746         lod_obj_inc_layout_gen(lo);
5747 out:
5748         if (rc)
5749                 lod_object_free_striping(env, lo);
5750         RETURN(rc);
5751 }
5752
5753 static int lod_declare_update_sync_pending(const struct lu_env *env,
5754                 struct lod_object *lo, struct md_layout_change *mlc,
5755                 struct thandle *th)
5756 {
5757         struct lod_thread_info  *info = lod_env_info(env);
5758         unsigned sync_components = 0;
5759         unsigned resync_components = 0;
5760         int i;
5761         int rc;
5762         ENTRY;
5763
5764         LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
5765         LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
5766                 mlc->mlc_opc == MD_LAYOUT_WRITE);
5767
5768         CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
5769                PFID(lod_object_fid(lo)), mlc->mlc_opc);
5770
5771         if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
5772                 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
5773                        PFID(lod_object_fid(lo)));
5774
5775                 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
5776                 return lod_declare_update_write_pending(env, lo, mlc, th);
5777         }
5778
5779         /* MD_LAYOUT_RESYNC_DONE */
5780
5781         for (i = 0; i < lo->ldo_comp_cnt; i++) {
5782                 struct lod_layout_component *lod_comp;
5783                 int j;
5784
5785                 lod_comp = &lo->ldo_comp_entries[i];
5786
5787                 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
5788                         sync_components++;
5789                         continue;
5790                 }
5791
5792                 for (j = 0; j < mlc->mlc_resync_count; j++) {
5793                         if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
5794                                 continue;
5795
5796                         mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
5797                         lod_comp->llc_flags &= ~LCME_FL_STALE;
5798                         resync_components++;
5799                         break;
5800                 }
5801         }
5802
5803         /* valid check */
5804         for (i = 0; i < mlc->mlc_resync_count; i++) {
5805                 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
5806                         continue;
5807
5808                 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
5809                        "or already synced\n", PFID(lod_object_fid(lo)),
5810                        mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
5811                 GOTO(out, rc = -EINVAL);
5812         }
5813
5814         if (!sync_components || !resync_components) {
5815                 CDEBUG(D_LAYOUT, DFID": no mirror in sync or resync\n",
5816                        PFID(lod_object_fid(lo)));
5817
5818                 /* tend to return an error code here to prevent
5819                  * the MDT from setting SoM attribute */
5820                 GOTO(out, rc = -EINVAL);
5821         }
5822
5823         CDEBUG(D_LAYOUT, DFID": resynced %u/%zu components\n",
5824                PFID(lod_object_fid(lo)),
5825                resync_components, mlc->mlc_resync_count);
5826
5827         lo->ldo_flr_state = LCM_FL_RDONLY;
5828         lod_obj_inc_layout_gen(lo);
5829
5830         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5831         rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
5832                                        &info->lti_buf, XATTR_NAME_LOV, 0, th);
5833         EXIT;
5834
5835 out:
5836         if (rc)
5837                 lod_object_free_striping(env, lo);
5838         RETURN(rc);
5839 }
5840
5841 static int lod_declare_layout_change(const struct lu_env *env,
5842                 struct dt_object *dt, struct md_layout_change *mlc,
5843                 struct thandle *th)
5844 {
5845         struct lod_thread_info  *info = lod_env_info(env);
5846         struct lod_object *lo = lod_dt_obj(dt);
5847         int rc;
5848         ENTRY;
5849
5850         if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
5851             dt_object_remote(dt_object_child(dt)))
5852                 RETURN(-EINVAL);
5853
5854         lod_write_lock(env, dt, 0);
5855         rc = lod_load_striping_locked(env, lo);
5856         if (rc)
5857                 GOTO(out, rc);
5858
5859         LASSERT(lo->ldo_comp_cnt > 0);
5860
5861         rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
5862         if (rc)
5863                 GOTO(out, rc);
5864
5865         switch (lo->ldo_flr_state) {
5866         case LCM_FL_NOT_FLR:
5867                 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
5868                                               &mlc->mlc_buf, th);
5869                 break;
5870         case LCM_FL_RDONLY:
5871                 rc = lod_declare_update_rdonly(env, lo, mlc, th);
5872                 break;
5873         case LCM_FL_WRITE_PENDING:
5874                 rc = lod_declare_update_write_pending(env, lo, mlc, th);
5875                 break;
5876         case LCM_FL_SYNC_PENDING:
5877                 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
5878                 break;
5879         default:
5880                 rc = -ENOTSUPP;
5881                 break;
5882         }
5883 out:
5884         dt_write_unlock(env, dt);
5885         RETURN(rc);
5886 }
5887
5888 /**
5889  * Instantiate layout component objects which covers the intent write offset.
5890  */
5891 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
5892                              struct md_layout_change *mlc, struct thandle *th)
5893 {
5894         struct lu_attr *attr = &lod_env_info(env)->lti_attr;
5895         struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
5896         struct lod_object *lo = lod_dt_obj(dt);
5897         int rc;
5898
5899         rc = lod_striped_create(env, dt, attr, NULL, th);
5900         if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
5901                 layout_attr->la_layout_version |= lo->ldo_layout_gen;
5902                 rc = lod_attr_set(env, dt, layout_attr, th);
5903         }
5904
5905         return rc;
5906 }
5907
5908 struct dt_object_operations lod_obj_ops = {
5909         .do_read_lock           = lod_read_lock,
5910         .do_write_lock          = lod_write_lock,
5911         .do_read_unlock         = lod_read_unlock,
5912         .do_write_unlock        = lod_write_unlock,
5913         .do_write_locked        = lod_write_locked,
5914         .do_attr_get            = lod_attr_get,
5915         .do_declare_attr_set    = lod_declare_attr_set,
5916         .do_attr_set            = lod_attr_set,
5917         .do_xattr_get           = lod_xattr_get,
5918         .do_declare_xattr_set   = lod_declare_xattr_set,
5919         .do_xattr_set           = lod_xattr_set,
5920         .do_declare_xattr_del   = lod_declare_xattr_del,
5921         .do_xattr_del           = lod_xattr_del,
5922         .do_xattr_list          = lod_xattr_list,
5923         .do_ah_init             = lod_ah_init,
5924         .do_declare_create      = lod_declare_create,
5925         .do_create              = lod_create,
5926         .do_declare_destroy     = lod_declare_destroy,
5927         .do_destroy             = lod_destroy,
5928         .do_index_try           = lod_index_try,
5929         .do_declare_ref_add     = lod_declare_ref_add,
5930         .do_ref_add             = lod_ref_add,
5931         .do_declare_ref_del     = lod_declare_ref_del,
5932         .do_ref_del             = lod_ref_del,
5933         .do_object_sync         = lod_object_sync,
5934         .do_object_lock         = lod_object_lock,
5935         .do_object_unlock       = lod_object_unlock,
5936         .do_invalidate          = lod_invalidate,
5937         .do_declare_layout_change = lod_declare_layout_change,
5938         .do_layout_change       = lod_layout_change,
5939 };
5940
5941 /**
5942  * Implementation of dt_body_operations::dbo_read.
5943  *
5944  * \see dt_body_operations::dbo_read() in the API description for details.
5945  */
5946 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
5947                         struct lu_buf *buf, loff_t *pos)
5948 {
5949         struct dt_object *next = dt_object_child(dt);
5950
5951         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
5952                 S_ISLNK(dt->do_lu.lo_header->loh_attr));
5953         return next->do_body_ops->dbo_read(env, next, buf, pos);
5954 }
5955
5956 /**
5957  * Implementation of dt_body_operations::dbo_declare_write.
5958  *
5959  * \see dt_body_operations::dbo_declare_write() in the API description
5960  * for details.
5961  */
5962 static ssize_t lod_declare_write(const struct lu_env *env,
5963                                  struct dt_object *dt,
5964                                  const struct lu_buf *buf, loff_t pos,
5965                                  struct thandle *th)
5966 {
5967         return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
5968 }
5969
5970 /**
5971  * Implementation of dt_body_operations::dbo_write.
5972  *
5973  * \see dt_body_operations::dbo_write() in the API description for details.
5974  */
5975 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
5976                          const struct lu_buf *buf, loff_t *pos,
5977                          struct thandle *th, int iq)
5978 {
5979         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
5980                 S_ISLNK(dt->do_lu.lo_header->loh_attr));
5981         return lod_sub_write(env, dt_object_child(dt), buf, pos, th, iq);
5982 }
5983
5984 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
5985                              __u64 start, __u64 end, struct thandle *th)
5986 {
5987         if (dt_object_remote(dt))
5988                 return -ENOTSUPP;
5989
5990         return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
5991 }
5992
5993 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
5994                      __u64 start, __u64 end, struct thandle *th)
5995 {
5996         if (dt_object_remote(dt))
5997                 return -ENOTSUPP;
5998
5999         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
6000         return lod_sub_punch(env, dt_object_child(dt), start, end, th);
6001 }
6002
6003 /*
6004  * different type of files use the same body_ops because object may be created
6005  * in OUT, where there is no chance to set correct body_ops for each type, so
6006  * body_ops themselves will check file type inside, see lod_read/write/punch for
6007  * details.
6008  */
6009 const struct dt_body_operations lod_body_ops = {
6010         .dbo_read               = lod_read,
6011         .dbo_declare_write      = lod_declare_write,
6012         .dbo_write              = lod_write,
6013         .dbo_declare_punch      = lod_declare_punch,
6014         .dbo_punch              = lod_punch,
6015 };
6016
6017 /**
6018  * Implementation of lu_object_operations::loo_object_init.
6019  *
6020  * The function determines the type and the index of the target device using
6021  * sequence of the object's FID. Then passes control down to the
6022  * corresponding device:
6023  *  OSD for the local objects, OSP for remote
6024  *
6025  * \see lu_object_operations::loo_object_init() in the API description
6026  * for details.
6027  */
6028 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
6029                            const struct lu_object_conf *conf)
6030 {
6031         struct lod_device       *lod    = lu2lod_dev(lo->lo_dev);
6032         struct lu_device        *cdev   = NULL;
6033         struct lu_object        *cobj;
6034         struct lod_tgt_descs    *ltd    = NULL;
6035         struct lod_tgt_desc     *tgt;
6036         u32                      idx    = 0;
6037         int                      type   = LU_SEQ_RANGE_ANY;
6038         int                      rc;
6039         ENTRY;
6040
6041         rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
6042         if (rc != 0) {
6043                 /* Note: Sometimes, it will Return EAGAIN here, see
6044                  * ptrlpc_import_delay_req(), which might confuse
6045                  * lu_object_find_at() and make it wait there incorrectly.
6046                  * so we convert it to EIO here.*/
6047                 if (rc == -EAGAIN)
6048                         rc = -EIO;
6049
6050                 RETURN(rc);
6051         }
6052
6053         if (type == LU_SEQ_RANGE_MDT &&
6054             idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
6055                 cdev = &lod->lod_child->dd_lu_dev;
6056         } else if (type == LU_SEQ_RANGE_MDT) {
6057                 ltd = &lod->lod_mdt_descs;
6058                 lod_getref(ltd);
6059         } else if (type == LU_SEQ_RANGE_OST) {
6060                 ltd = &lod->lod_ost_descs;
6061                 lod_getref(ltd);
6062         } else {
6063                 LBUG();
6064         }
6065
6066         if (ltd != NULL) {
6067                 if (ltd->ltd_tgts_size > idx &&
6068                     cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
6069                         tgt = LTD_TGT(ltd, idx);
6070
6071                         LASSERT(tgt != NULL);
6072                         LASSERT(tgt->ltd_tgt != NULL);
6073
6074                         cdev = &(tgt->ltd_tgt->dd_lu_dev);
6075                 }
6076                 lod_putref(lod, ltd);
6077         }
6078
6079         if (unlikely(cdev == NULL))
6080                 RETURN(-ENOENT);
6081
6082         cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
6083         if (unlikely(cobj == NULL))
6084                 RETURN(-ENOMEM);
6085
6086         lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
6087
6088         lu_object_add(lo, cobj);
6089
6090         RETURN(0);
6091 }
6092
6093 /**
6094  *
6095  * Release resources associated with striping.
6096  *
6097  * If the object is striped (regular or directory), then release
6098  * the stripe objects references and free the ldo_stripe array.
6099  *
6100  * \param[in] env       execution environment
6101  * \param[in] lo        object
6102  */
6103 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
6104 {
6105         struct lod_layout_component *lod_comp;
6106         int i, j;
6107
6108         if (lo->ldo_stripe != NULL) {
6109                 LASSERT(lo->ldo_comp_entries == NULL);
6110                 LASSERT(lo->ldo_dir_stripes_allocated > 0);
6111
6112                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6113                         if (lo->ldo_stripe[i])
6114                                 dt_object_put(env, lo->ldo_stripe[i]);
6115                 }
6116
6117                 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
6118                 OBD_FREE(lo->ldo_stripe, j);
6119                 lo->ldo_stripe = NULL;
6120                 lo->ldo_dir_stripes_allocated = 0;
6121                 lo->ldo_dir_stripe_loaded = 0;
6122                 lo->ldo_dir_stripe_count = 0;
6123         } else if (lo->ldo_comp_entries != NULL) {
6124                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6125                         /* free lod_layout_component::llc_stripe array */
6126                         lod_comp = &lo->ldo_comp_entries[i];
6127
6128                         if (lod_comp->llc_stripe == NULL)
6129                                 continue;
6130                         LASSERT(lod_comp->llc_stripes_allocated != 0);
6131                         for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
6132                                 if (lod_comp->llc_stripe[j] != NULL)
6133                                         lu_object_put(env,
6134                                                &lod_comp->llc_stripe[j]->do_lu);
6135                         }
6136                         OBD_FREE(lod_comp->llc_stripe,
6137                                  sizeof(struct dt_object *) *
6138                                  lod_comp->llc_stripes_allocated);
6139                         lod_comp->llc_stripe = NULL;
6140                         lod_comp->llc_stripes_allocated = 0;
6141                 }
6142                 lod_free_comp_entries(lo);
6143                 lo->ldo_comp_cached = 0;
6144         }
6145 }
6146
6147 /**
6148  * Implementation of lu_object_operations::loo_object_free.
6149  *
6150  * \see lu_object_operations::loo_object_free() in the API description
6151  * for details.
6152  */
6153 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
6154 {
6155         struct lod_object *lo = lu2lod_obj(o);
6156
6157         /* release all underlying object pinned */
6158         lod_object_free_striping(env, lo);
6159         lu_object_fini(o);
6160         OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
6161 }
6162
6163 /**
6164  * Implementation of lu_object_operations::loo_object_release.
6165  *
6166  * \see lu_object_operations::loo_object_release() in the API description
6167  * for details.
6168  */
6169 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
6170 {
6171         /* XXX: shouldn't we release everything here in case if object
6172          * creation failed before? */
6173 }
6174
6175 /**
6176  * Implementation of lu_object_operations::loo_object_print.
6177  *
6178  * \see lu_object_operations::loo_object_print() in the API description
6179  * for details.
6180  */
6181 static int lod_object_print(const struct lu_env *env, void *cookie,
6182                             lu_printer_t p, const struct lu_object *l)
6183 {
6184         struct lod_object *o = lu2lod_obj((struct lu_object *) l);
6185
6186         return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
6187 }
6188
6189 struct lu_object_operations lod_lu_obj_ops = {
6190         .loo_object_init        = lod_object_init,
6191         .loo_object_free        = lod_object_free,
6192         .loo_object_release     = lod_object_release,
6193         .loo_object_print       = lod_object_print,
6194 };