Whamcloud - gitweb
LU-9049 obdclass: change object lookup to no wait mode
[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, 2016, 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_param.h>
51 #include <lustre_swab.h>
52 #include <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_stripenr > 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_stripenr > 0);                            \
412         LASSERT((it)->lit_stripe_index < (lo)->ldo_dir_stripenr);       \
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_stripenr)
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_stripenr > 0) {
959                 int i;
960
961                 for (i = 0; i < lo->ldo_dir_stripenr; 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, lod_obj_stripe_cb_t cb,
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                 LASSERT(lod_comp->llc_stripenr > 0);
1065                 for (j = 0; j < lod_comp->llc_stripenr; j++) {
1066                         struct dt_object *dt = lod_comp->llc_stripe[j];
1067
1068                         if (dt == NULL)
1069                                 continue;
1070                         rc = cb(env, lo, dt, th, j, data);
1071                         if (rc != 0)
1072                                 RETURN(rc);
1073                 }
1074         }
1075         RETURN(0);
1076 }
1077
1078 static inline int
1079 lod_obj_stripe_attr_set_cb(const struct lu_env *env, struct lod_object *lo,
1080                            struct dt_object *dt, struct thandle *th,
1081                            int stripe_idx, struct lod_obj_stripe_cb_data *data)
1082 {
1083         if (data->locd_declare)
1084                 return lod_sub_declare_attr_set(env, dt, data->locd_attr, th);
1085
1086         return lod_sub_attr_set(env, dt, data->locd_attr, th);
1087 }
1088
1089 /**
1090  * Implementation of dt_object_operations::do_declare_attr_set.
1091  *
1092  * If the object is striped, then apply the changes to all the stripes.
1093  *
1094  * \see dt_object_operations::do_declare_attr_set() in the API description
1095  * for details.
1096  */
1097 static int lod_declare_attr_set(const struct lu_env *env,
1098                                 struct dt_object *dt,
1099                                 const struct lu_attr *attr,
1100                                 struct thandle *th)
1101 {
1102         struct dt_object  *next = dt_object_child(dt);
1103         struct lod_object *lo = lod_dt_obj(dt);
1104         int                rc, i;
1105         ENTRY;
1106
1107         /*
1108          * declare setattr on the local object
1109          */
1110         rc = lod_sub_declare_attr_set(env, next, attr, th);
1111         if (rc)
1112                 RETURN(rc);
1113
1114         /* osp_declare_attr_set() ignores all attributes other than
1115          * UID, GID, PROJID, and size, and osp_attr_set() ignores all
1116          * but UID, GID and PROJID. Declaration of size attr setting
1117          * happens through lod_declare_init_size(), and not through
1118          * this function. Therefore we need not load striping unless
1119          * ownership is changing.  This should save memory and (we hope)
1120          * speed up rename().
1121          */
1122         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1123                 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID)))
1124                         RETURN(rc);
1125
1126                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1127                         RETURN(0);
1128         } else {
1129                 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID | LA_MODE |
1130                                         LA_ATIME | LA_MTIME | LA_CTIME |
1131                                         LA_FLAGS)))
1132                         RETURN(rc);
1133         }
1134         /*
1135          * load striping information, notice we don't do this when object
1136          * is being initialized as we don't need this information till
1137          * few specific cases like destroy, chown
1138          */
1139         rc = lod_load_striping(env, lo);
1140         if (rc)
1141                 RETURN(rc);
1142
1143         if (!lod_obj_is_striped(dt))
1144                 RETURN(0);
1145
1146         /*
1147          * if object is striped declare changes on the stripes
1148          */
1149         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1150                 LASSERT(lo->ldo_stripe);
1151                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
1152                         if (lo->ldo_stripe[i] == NULL)
1153                                 continue;
1154                         rc = lod_sub_declare_attr_set(env, lo->ldo_stripe[i],
1155                                                       attr, th);
1156                         if (rc != 0)
1157                                 RETURN(rc);
1158                 }
1159         } else {
1160                 struct lod_obj_stripe_cb_data data;
1161
1162                 data.locd_attr = attr;
1163                 data.locd_declare = true;
1164                 rc = lod_obj_for_each_stripe(env, lo, th,
1165                                 lod_obj_stripe_attr_set_cb, &data);
1166         }
1167
1168         if (rc)
1169                 RETURN(rc);
1170
1171         if (!dt_object_exists(next) || dt_object_remote(next) ||
1172             !S_ISREG(attr->la_mode))
1173                 RETURN(0);
1174
1175         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1176                 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
1177                 RETURN(rc);
1178         }
1179
1180         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1181             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1182                 struct lod_thread_info *info = lod_env_info(env);
1183                 struct lu_buf *buf = &info->lti_buf;
1184
1185                 buf->lb_buf = info->lti_ea_store;
1186                 buf->lb_len = info->lti_ea_store_size;
1187                 rc = lod_sub_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1188                                                LU_XATTR_REPLACE, th);
1189         }
1190
1191         RETURN(rc);
1192 }
1193
1194 /**
1195  * Implementation of dt_object_operations::do_attr_set.
1196  *
1197  * If the object is striped, then apply the changes to all or subset of
1198  * the stripes depending on the object type and specific attributes.
1199  *
1200  * \see dt_object_operations::do_attr_set() in the API description for details.
1201  */
1202 static int lod_attr_set(const struct lu_env *env,
1203                         struct dt_object *dt,
1204                         const struct lu_attr *attr,
1205                         struct thandle *th)
1206 {
1207         struct dt_object        *next = dt_object_child(dt);
1208         struct lod_object       *lo = lod_dt_obj(dt);
1209         int                     rc, i;
1210         ENTRY;
1211
1212         /*
1213          * apply changes to the local object
1214          */
1215         rc = lod_sub_attr_set(env, next, attr, th);
1216         if (rc)
1217                 RETURN(rc);
1218
1219         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1220                 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID)))
1221                         RETURN(rc);
1222
1223                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1224                         RETURN(0);
1225         } else {
1226                 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE | LA_PROJID |
1227                                         LA_ATIME | LA_MTIME | LA_CTIME |
1228                                         LA_FLAGS)))
1229                         RETURN(rc);
1230         }
1231
1232         if (!lod_obj_is_striped(dt))
1233                 RETURN(0);
1234
1235         /*
1236          * if object is striped, apply changes to all the stripes
1237          */
1238         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1239                 LASSERT(lo->ldo_stripe);
1240                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
1241                         if (unlikely(lo->ldo_stripe[i] == NULL))
1242                                 continue;
1243
1244                         if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1245                                 continue;
1246
1247                         rc = lod_sub_attr_set(env, lo->ldo_stripe[i], attr, th);
1248                         if (rc != 0)
1249                                 break;
1250                 }
1251         } else {
1252                 struct lod_obj_stripe_cb_data data;
1253
1254                 data.locd_attr = attr;
1255                 data.locd_declare = false;
1256                 rc = lod_obj_for_each_stripe(env, lo, th,
1257                                 lod_obj_stripe_attr_set_cb, &data);
1258         }
1259
1260         if (rc)
1261                 RETURN(rc);
1262
1263         if (!dt_object_exists(next) || dt_object_remote(next) ||
1264             !S_ISREG(attr->la_mode))
1265                 RETURN(0);
1266
1267         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1268                 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
1269                 RETURN(rc);
1270         }
1271
1272         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1273                 struct lod_thread_info *info = lod_env_info(env);
1274                 struct lu_buf *buf = &info->lti_buf;
1275                 struct ost_id *oi = &info->lti_ostid;
1276                 struct lu_fid *fid = &info->lti_fid;
1277                 struct lov_mds_md_v1 *lmm;
1278                 struct lov_ost_data_v1 *objs;
1279                 __u32 magic;
1280
1281                 rc = lod_get_lov_ea(env, lo);
1282                 if (rc <= 0)
1283                         RETURN(rc);
1284
1285                 buf->lb_buf = info->lti_ea_store;
1286                 buf->lb_len = info->lti_ea_store_size;
1287                 lmm = info->lti_ea_store;
1288                 magic = le32_to_cpu(lmm->lmm_magic);
1289                 if (magic == LOV_MAGIC_COMP_V1) {
1290                         struct lov_comp_md_v1 *lcm = buf->lb_buf;
1291                         struct lov_comp_md_entry_v1 *lcme =
1292                                                 &lcm->lcm_entries[0];
1293
1294                         lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1295                         magic = le32_to_cpu(lmm->lmm_magic);
1296                 }
1297
1298                 if (magic == LOV_MAGIC_V1)
1299                         objs = &(lmm->lmm_objects[0]);
1300                 else
1301                         objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1302                 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1303                 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1304                 fid->f_oid--;
1305                 fid_to_ostid(fid, oi);
1306                 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1307
1308                 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1309                                        LU_XATTR_REPLACE, th);
1310         } else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1311                 struct lod_thread_info *info = lod_env_info(env);
1312                 struct lu_buf *buf = &info->lti_buf;
1313                 struct lov_comp_md_v1 *lcm;
1314                 struct lov_comp_md_entry_v1 *lcme;
1315
1316                 rc = lod_get_lov_ea(env, lo);
1317                 if (rc <= 0)
1318                         RETURN(rc);
1319
1320                 buf->lb_buf = info->lti_ea_store;
1321                 buf->lb_len = info->lti_ea_store_size;
1322                 lcm = buf->lb_buf;
1323                 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
1324                         RETURN(-EINVAL);
1325
1326                 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1327                 lcme = &lcm->lcm_entries[0];
1328                 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1329                 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1330
1331                 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1332                                        LU_XATTR_REPLACE, th);
1333         }
1334
1335         RETURN(rc);
1336 }
1337
1338 /**
1339  * Implementation of dt_object_operations::do_xattr_get.
1340  *
1341  * If LOV EA is requested from the root object and it's not
1342  * found, then return default striping for the filesystem.
1343  *
1344  * \see dt_object_operations::do_xattr_get() in the API description for details.
1345  */
1346 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1347                          struct lu_buf *buf, const char *name)
1348 {
1349         struct lod_thread_info *info = lod_env_info(env);
1350         struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1351         int is_root;
1352         int rc;
1353         ENTRY;
1354
1355         rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1356         if (strcmp(name, XATTR_NAME_LMV) == 0) {
1357                 struct lmv_mds_md_v1    *lmv1;
1358                 int                      rc1 = 0;
1359
1360                 if (rc > (typeof(rc))sizeof(*lmv1))
1361                         RETURN(rc);
1362
1363                 if (rc < (typeof(rc))sizeof(*lmv1))
1364                         RETURN(rc = rc > 0 ? -EINVAL : rc);
1365
1366                 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1367                         CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1368
1369                         info->lti_buf.lb_buf = info->lti_key;
1370                         info->lti_buf.lb_len = sizeof(*lmv1);
1371                         rc = dt_xattr_get(env, dt_object_child(dt),
1372                                           &info->lti_buf, name);
1373                         if (unlikely(rc != sizeof(*lmv1)))
1374                                 RETURN(rc = rc > 0 ? -EINVAL : rc);
1375
1376                         lmv1 = info->lti_buf.lb_buf;
1377                         /* The on-disk LMV EA only contains header, but the
1378                          * returned LMV EA size should contain the space for
1379                          * the FIDs of all shards of the striped directory. */
1380                         if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1381                                 rc = lmv_mds_md_size(
1382                                         le32_to_cpu(lmv1->lmv_stripe_count),
1383                                         LMV_MAGIC_V1);
1384                 } else {
1385                         rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1386                                                   buf, false);
1387                 }
1388
1389                 RETURN(rc = rc1 != 0 ? rc1 : rc);
1390         }
1391
1392         if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1393                 RETURN(rc);
1394
1395         /*
1396          * XXX: Only used by lfsck
1397          *
1398          * lod returns default striping on the real root of the device
1399          * this is like the root stores default striping for the whole
1400          * filesystem. historically we've been using a different approach
1401          * and store it in the config.
1402          */
1403         dt_root_get(env, dev->lod_child, &info->lti_fid);
1404         is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1405
1406         if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1407                 struct lov_user_md *lum = buf->lb_buf;
1408                 struct lov_desc    *desc = &dev->lod_desc;
1409
1410                 if (buf->lb_buf == NULL) {
1411                         rc = sizeof(*lum);
1412                 } else if (buf->lb_len >= sizeof(*lum)) {
1413                         lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1414                         lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1415                         lmm_oi_set_id(&lum->lmm_oi, 0);
1416                         lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1417                         lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1418                         lum->lmm_stripe_size = cpu_to_le32(
1419                                                 desc->ld_default_stripe_size);
1420                         lum->lmm_stripe_count = cpu_to_le16(
1421                                                 desc->ld_default_stripe_count);
1422                         lum->lmm_stripe_offset = cpu_to_le16(
1423                                                 desc->ld_default_stripe_offset);
1424                         rc = sizeof(*lum);
1425                 } else {
1426                         rc = -ERANGE;
1427                 }
1428         }
1429
1430         RETURN(rc);
1431 }
1432
1433 /**
1434  * Verify LVM EA.
1435  *
1436  * Checks that the magic of the stripe is sane.
1437  *
1438  * \param[in] lod       lod device
1439  * \param[in] lum       a buffer storing LMV EA to verify
1440  *
1441  * \retval              0 if the EA is sane
1442  * \retval              negative otherwise
1443  */
1444 static int lod_verify_md_striping(struct lod_device *lod,
1445                                   const struct lmv_user_md_v1 *lum)
1446 {
1447         if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1448                 CERROR("%s: invalid lmv_user_md: magic = %x, "
1449                        "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1450                        lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1451                        (int)le32_to_cpu(lum->lum_stripe_offset),
1452                        le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1453                 return -EINVAL;
1454         }
1455
1456         return 0;
1457 }
1458
1459 /**
1460  * Initialize LMV EA for a slave.
1461  *
1462  * Initialize slave's LMV EA from the master's LMV EA.
1463  *
1464  * \param[in] master_lmv        a buffer containing master's EA
1465  * \param[out] slave_lmv        a buffer where slave's EA will be stored
1466  *
1467  */
1468 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1469                                   const struct lmv_mds_md_v1 *master_lmv)
1470 {
1471         *slave_lmv = *master_lmv;
1472         slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1473 }
1474
1475 /**
1476  * Generate LMV EA.
1477  *
1478  * Generate LMV EA from the object passed as \a dt. The object must have
1479  * the stripes created and initialized.
1480  *
1481  * \param[in] env       execution environment
1482  * \param[in] dt        object
1483  * \param[out] lmv_buf  buffer storing generated LMV EA
1484  *
1485  * \retval              0 on success
1486  * \retval              negative if failed
1487  */
1488 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1489                            struct lu_buf *lmv_buf)
1490 {
1491         struct lod_thread_info  *info = lod_env_info(env);
1492         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
1493         struct lod_object       *lo = lod_dt_obj(dt);
1494         struct lmv_mds_md_v1    *lmm1;
1495         int                     stripe_count;
1496         int                     type = LU_SEQ_RANGE_ANY;
1497         int                     rc;
1498         __u32                   mdtidx;
1499         ENTRY;
1500
1501         LASSERT(lo->ldo_dir_striped != 0);
1502         LASSERT(lo->ldo_dir_stripenr > 0);
1503         stripe_count = lo->ldo_dir_stripenr;
1504         /* Only store the LMV EA heahder on the disk. */
1505         if (info->lti_ea_store_size < sizeof(*lmm1)) {
1506                 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1507                 if (rc != 0)
1508                         RETURN(rc);
1509         } else {
1510                 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1511         }
1512
1513         lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1514         lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1515         lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1516         lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1517         rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1518                             &mdtidx, &type);
1519         if (rc != 0)
1520                 RETURN(rc);
1521
1522         lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1523         lmv_buf->lb_buf = info->lti_ea_store;
1524         lmv_buf->lb_len = sizeof(*lmm1);
1525
1526         RETURN(rc);
1527 }
1528
1529 /**
1530  * Create in-core represenation for a striped directory.
1531  *
1532  * Parse the buffer containing LMV EA and instantiate LU objects
1533  * representing the stripe objects. The pointers to the objects are
1534  * stored in ldo_stripe field of \a lo. This function is used when
1535  * we need to access an already created object (i.e. load from a disk).
1536  *
1537  * \param[in] env       execution environment
1538  * \param[in] lo        lod object
1539  * \param[in] buf       buffer containing LMV EA
1540  *
1541  * \retval              0 on success
1542  * \retval              negative if failed
1543  */
1544 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1545                            const struct lu_buf *buf)
1546 {
1547         struct lod_thread_info  *info = lod_env_info(env);
1548         struct lod_device       *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1549         struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
1550         struct dt_object        **stripe;
1551         union lmv_mds_md        *lmm = buf->lb_buf;
1552         struct lmv_mds_md_v1    *lmv1 = &lmm->lmv_md_v1;
1553         struct lu_fid           *fid = &info->lti_fid;
1554         unsigned int            i;
1555         int                     rc = 0;
1556         ENTRY;
1557
1558         if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1559                 RETURN(0);
1560
1561         if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1562                 lo->ldo_dir_slave_stripe = 1;
1563                 RETURN(0);
1564         }
1565
1566         if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1567                 RETURN(-EINVAL);
1568
1569         if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1570                 RETURN(0);
1571
1572         LASSERT(lo->ldo_stripe == NULL);
1573         OBD_ALLOC(stripe, sizeof(stripe[0]) *
1574                   (le32_to_cpu(lmv1->lmv_stripe_count)));
1575         if (stripe == NULL)
1576                 RETURN(-ENOMEM);
1577
1578         for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1579                 struct dt_device        *tgt_dt;
1580                 struct dt_object        *dto;
1581                 int                     type = LU_SEQ_RANGE_ANY;
1582                 __u32                   idx;
1583
1584                 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1585                 if (!fid_is_sane(fid))
1586                         GOTO(out, rc = -ESTALE);
1587
1588                 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1589                 if (rc != 0)
1590                         GOTO(out, rc);
1591
1592                 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1593                         tgt_dt = lod->lod_child;
1594                 } else {
1595                         struct lod_tgt_desc     *tgt;
1596
1597                         tgt = LTD_TGT(ltd, idx);
1598                         if (tgt == NULL)
1599                                 GOTO(out, rc = -ESTALE);
1600                         tgt_dt = tgt->ltd_tgt;
1601                 }
1602
1603                 dto = dt_locate_at(env, tgt_dt, fid,
1604                                   lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1605                                   NULL);
1606                 if (IS_ERR(dto))
1607                         GOTO(out, rc = PTR_ERR(dto));
1608
1609                 stripe[i] = dto;
1610         }
1611 out:
1612         lo->ldo_stripe = stripe;
1613         lo->ldo_dir_stripenr = le32_to_cpu(lmv1->lmv_stripe_count);
1614         lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1615         if (rc != 0)
1616                 lod_object_free_striping(env, lo);
1617
1618         RETURN(rc);
1619 }
1620
1621 /**
1622  * Declare create a striped directory.
1623  *
1624  * Declare creating a striped directory with a given stripe pattern on the
1625  * specified MDTs. A striped directory is represented as a regular directory
1626  * - an index listing all the stripes. The stripes point back to the master
1627  * object with ".." and LinkEA. The master object gets LMV EA which
1628  * identifies it as a striped directory. The function allocates FIDs
1629  * for all stripes.
1630  *
1631  * \param[in] env       execution environment
1632  * \param[in] dt        object
1633  * \param[in] attr      attributes to initialize the objects with
1634  * \param[in] dof       type of objects to be created
1635  * \param[in] th        transaction handle
1636  *
1637  * \retval              0 on success
1638  * \retval              negative if failed
1639  */
1640 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1641                                           struct dt_object *dt,
1642                                           struct lu_attr *attr,
1643                                           struct dt_object_format *dof,
1644                                           struct thandle *th)
1645 {
1646         struct lod_thread_info  *info = lod_env_info(env);
1647         struct lu_buf           lmv_buf;
1648         struct lu_buf           slave_lmv_buf;
1649         struct lmv_mds_md_v1    *lmm;
1650         struct lmv_mds_md_v1    *slave_lmm = NULL;
1651         struct dt_insert_rec    *rec = &info->lti_dt_rec;
1652         struct lod_object       *lo = lod_dt_obj(dt);
1653         int                     rc;
1654         __u32                   i;
1655         ENTRY;
1656
1657         rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1658         if (rc != 0)
1659                 GOTO(out, rc);
1660         lmm = lmv_buf.lb_buf;
1661
1662         OBD_ALLOC_PTR(slave_lmm);
1663         if (slave_lmm == NULL)
1664                 GOTO(out, rc = -ENOMEM);
1665
1666         lod_prep_slave_lmv_md(slave_lmm, lmm);
1667         slave_lmv_buf.lb_buf = slave_lmm;
1668         slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1669
1670         if (!dt_try_as_dir(env, dt_object_child(dt)))
1671                 GOTO(out, rc = -EINVAL);
1672
1673         rec->rec_type = S_IFDIR;
1674         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
1675                 struct dt_object        *dto = lo->ldo_stripe[i];
1676                 char                    *stripe_name = info->lti_key;
1677                 struct lu_name          *sname;
1678                 struct linkea_data       ldata          = { NULL };
1679                 struct lu_buf           linkea_buf;
1680
1681                 rc = lod_sub_declare_create(env, dto, attr, NULL, dof, th);
1682                 if (rc != 0)
1683                         GOTO(out, rc);
1684
1685                 if (!dt_try_as_dir(env, dto))
1686                         GOTO(out, rc = -EINVAL);
1687
1688                 rc = lod_sub_declare_ref_add(env, dto, th);
1689                 if (rc != 0)
1690                         GOTO(out, rc);
1691
1692                 rec->rec_fid = lu_object_fid(&dto->do_lu);
1693                 rc = lod_sub_declare_insert(env, dto,
1694                                             (const struct dt_rec *)rec,
1695                                             (const struct dt_key *)dot, th);
1696                 if (rc != 0)
1697                         GOTO(out, rc);
1698
1699                 /* master stripe FID will be put to .. */
1700                 rec->rec_fid = lu_object_fid(&dt->do_lu);
1701                 rc = lod_sub_declare_insert(env, dto,
1702                                             (const struct dt_rec *)rec,
1703                                             (const struct dt_key *)dotdot, th);
1704                 if (rc != 0)
1705                         GOTO(out, rc);
1706
1707                 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1708                     cfs_fail_val != i) {
1709                         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1710                             cfs_fail_val == i)
1711                                 slave_lmm->lmv_master_mdt_index =
1712                                                         cpu_to_le32(i + 1);
1713                         else
1714                                 slave_lmm->lmv_master_mdt_index =
1715                                                         cpu_to_le32(i);
1716                         rc = lod_sub_declare_xattr_set(env, dto, &slave_lmv_buf,
1717                                                        XATTR_NAME_LMV, 0, th);
1718                         if (rc != 0)
1719                                 GOTO(out, rc);
1720                 }
1721
1722                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1723                     cfs_fail_val == i)
1724                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1725                                 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1726                 else
1727                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1728                                 PFID(lu_object_fid(&dto->do_lu)), i);
1729
1730                 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1731                 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1732                                       sname, lu_object_fid(&dt->do_lu));
1733                 if (rc != 0)
1734                         GOTO(out, rc);
1735
1736                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1737                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1738                 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
1739                                                XATTR_NAME_LINK, 0, th);
1740                 if (rc != 0)
1741                         GOTO(out, rc);
1742
1743                 rec->rec_fid = lu_object_fid(&dto->do_lu);
1744                 rc = lod_sub_declare_insert(env, dt_object_child(dt),
1745                                             (const struct dt_rec *)rec,
1746                                             (const struct dt_key *)stripe_name,
1747                                             th);
1748                 if (rc != 0)
1749                         GOTO(out, rc);
1750
1751                 rc = lod_sub_declare_ref_add(env, dt_object_child(dt), th);
1752                 if (rc != 0)
1753                         GOTO(out, rc);
1754         }
1755
1756         rc = lod_sub_declare_xattr_set(env, dt_object_child(dt),
1757                                        &lmv_buf, XATTR_NAME_LMV, 0, th);
1758         if (rc != 0)
1759                 GOTO(out, rc);
1760 out:
1761         if (slave_lmm != NULL)
1762                 OBD_FREE_PTR(slave_lmm);
1763
1764         RETURN(rc);
1765 }
1766
1767 static int lod_prep_md_striped_create(const struct lu_env *env,
1768                                       struct dt_object *dt,
1769                                       struct lu_attr *attr,
1770                                       const struct lmv_user_md_v1 *lum,
1771                                       struct dt_object_format *dof,
1772                                       struct thandle *th)
1773 {
1774         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
1775         struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
1776         struct lod_object       *lo = lod_dt_obj(dt);
1777         struct dt_object        **stripe;
1778         __u32                   stripe_count;
1779         int                     *idx_array;
1780         __u32                   master_index;
1781         int                     rc = 0;
1782         __u32                   i;
1783         __u32                   j;
1784         ENTRY;
1785
1786         /* The lum has been verifed in lod_verify_md_striping */
1787         LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
1788         LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1789
1790         stripe_count = le32_to_cpu(lum->lum_stripe_count);
1791
1792         OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1793         if (idx_array == NULL)
1794                 RETURN(-ENOMEM);
1795
1796         OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1797         if (stripe == NULL)
1798                 GOTO(out_free, rc = -ENOMEM);
1799
1800         /* Start index must be the master MDT */
1801         master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1802         idx_array[0] = master_index;
1803         for (i = 0; i < stripe_count; i++) {
1804                 struct lod_tgt_desc     *tgt = NULL;
1805                 struct dt_object        *dto;
1806                 struct lu_fid           fid = { 0 };
1807                 int                     idx;
1808                 struct lu_object_conf   conf = { 0 };
1809                 struct dt_device        *tgt_dt = NULL;
1810
1811                 /* Try to find next avaible target */
1812                 idx = idx_array[i];
1813                 for (j = 0; j < lod->lod_remote_mdt_count;
1814                      j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1815                         bool already_allocated = false;
1816                         __u32 k;
1817
1818                         CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
1819                                idx, lod->lod_remote_mdt_count + 1, i);
1820
1821                         if (likely(!OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
1822                                 /* check whether the idx already exists
1823                                  * in current allocated array */
1824                                 for (k = 0; k < i; k++) {
1825                                         if (idx_array[k] == idx) {
1826                                                 already_allocated = true;
1827                                                 break;
1828                                         }
1829                                 }
1830
1831                                 if (already_allocated)
1832                                         continue;
1833                         }
1834
1835                         /* Sigh, this index is not in the bitmap, let's check
1836                          * next available target */
1837                         if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) &&
1838                             idx != master_index)
1839                                 continue;
1840
1841                         if (idx == master_index) {
1842                                 /* Allocate the FID locally */
1843                                 rc = obd_fid_alloc(env, lod->lod_child_exp,
1844                                                    &fid, NULL);
1845                                 if (rc < 0)
1846                                         GOTO(out_put, rc);
1847                                 tgt_dt = lod->lod_child;
1848                                 break;
1849                         }
1850
1851                         /* check the status of the OSP */
1852                         tgt = LTD_TGT(ltd, idx);
1853                         if (tgt == NULL)
1854                                 continue;
1855
1856                         tgt_dt = tgt->ltd_tgt;
1857                         rc = dt_statfs(env, tgt_dt, NULL);
1858                         if (rc) {
1859                                 /* this OSP doesn't feel well */
1860                                 rc = 0;
1861                                 continue;
1862                         }
1863
1864                         rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1865                         if (rc < 0) {
1866                                 rc = 0;
1867                                 continue;
1868                         }
1869
1870                         break;
1871                 }
1872
1873                 /* Can not allocate more stripes */
1874                 if (j == lod->lod_remote_mdt_count) {
1875                         CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1876                                lod2obd(lod)->obd_name, stripe_count, i);
1877                         break;
1878                 }
1879
1880                 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
1881                        idx, i, PFID(&fid));
1882                 idx_array[i] = idx;
1883                 /* Set the start index for next stripe allocation */
1884                 if (i < stripe_count - 1)
1885                         idx_array[i + 1] = (idx + 1) %
1886                                            (lod->lod_remote_mdt_count + 1);
1887                 /* tgt_dt and fid must be ready after search avaible OSP
1888                  * in the above loop */
1889                 LASSERT(tgt_dt != NULL);
1890                 LASSERT(fid_is_sane(&fid));
1891                 conf.loc_flags = LOC_F_NEW;
1892                 dto = dt_locate_at(env, tgt_dt, &fid,
1893                                    dt->do_lu.lo_dev->ld_site->ls_top_dev,
1894                                    &conf);
1895                 if (IS_ERR(dto))
1896                         GOTO(out_put, rc = PTR_ERR(dto));
1897                 stripe[i] = dto;
1898         }
1899
1900         lo->ldo_dir_striped = 1;
1901         lo->ldo_stripe = stripe;
1902         lo->ldo_dir_stripenr = i;
1903         lo->ldo_dir_stripes_allocated = stripe_count;
1904
1905         if (lo->ldo_dir_stripenr == 0)
1906                 GOTO(out_put, rc = -ENOSPC);
1907
1908         rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
1909         if (rc != 0)
1910                 GOTO(out_put, rc);
1911
1912 out_put:
1913         if (rc < 0) {
1914                 for (i = 0; i < stripe_count; i++)
1915                         if (stripe[i] != NULL)
1916                                 dt_object_put(env, stripe[i]);
1917                 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
1918                 lo->ldo_dir_stripenr = 0;
1919                 lo->ldo_dir_stripes_allocated = 0;
1920                 lo->ldo_stripe = NULL;
1921         }
1922
1923 out_free:
1924         OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
1925
1926         RETURN(rc);
1927 }
1928
1929 /**
1930  * Declare create striped md object.
1931  *
1932  * The function declares intention to create a striped directory. This is a
1933  * wrapper for lod_prep_md_striped_create(). The only additional functionality
1934  * is to verify pattern \a lum_buf is good. Check that function for the details.
1935  *
1936  * \param[in] env       execution environment
1937  * \param[in] dt        object
1938  * \param[in] attr      attributes to initialize the objects with
1939  * \param[in] lum_buf   a pattern specifying the number of stripes and
1940  *                      MDT to start from
1941  * \param[in] dof       type of objects to be created
1942  * \param[in] th        transaction handle
1943  *
1944  * \retval              0 on success
1945  * \retval              negative if failed
1946  *
1947  */
1948 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
1949                                      struct dt_object *dt,
1950                                      struct lu_attr *attr,
1951                                      const struct lu_buf *lum_buf,
1952                                      struct dt_object_format *dof,
1953                                      struct thandle *th)
1954 {
1955         struct lod_object       *lo = lod_dt_obj(dt);
1956         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
1957         struct lmv_user_md_v1   *lum;
1958         int                     rc;
1959         ENTRY;
1960
1961         lum = lum_buf->lb_buf;
1962         LASSERT(lum != NULL);
1963
1964         CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
1965                le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
1966                (int)le32_to_cpu(lum->lum_stripe_offset));
1967
1968         if (le32_to_cpu(lum->lum_stripe_count) == 0)
1969                 GOTO(out, rc = 0);
1970
1971         rc = lod_verify_md_striping(lod, lum);
1972         if (rc != 0)
1973                 GOTO(out, rc);
1974
1975         /* prepare dir striped objects */
1976         rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
1977         if (rc != 0) {
1978                 /* failed to create striping, let's reset
1979                  * config so that others don't get confused */
1980                 lod_object_free_striping(env, lo);
1981                 GOTO(out, rc);
1982         }
1983 out:
1984         RETURN(rc);
1985 }
1986
1987 /**
1988  * Implementation of dt_object_operations::do_declare_xattr_set.
1989  *
1990  * Used with regular (non-striped) objects. Basically it
1991  * initializes the striping information and applies the
1992  * change to all the stripes.
1993  *
1994  * \see dt_object_operations::do_declare_xattr_set() in the API description
1995  * for details.
1996  */
1997 static int lod_dir_declare_xattr_set(const struct lu_env *env,
1998                                      struct dt_object *dt,
1999                                      const struct lu_buf *buf,
2000                                      const char *name, int fl,
2001                                      struct thandle *th)
2002 {
2003         struct dt_object        *next = dt_object_child(dt);
2004         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2005         struct lod_object       *lo = lod_dt_obj(dt);
2006         int                     i;
2007         int                     rc;
2008         ENTRY;
2009
2010         if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2011                 struct lmv_user_md_v1 *lum;
2012
2013                 LASSERT(buf != NULL && buf->lb_buf != NULL);
2014                 lum = buf->lb_buf;
2015                 rc = lod_verify_md_striping(d, lum);
2016                 if (rc != 0)
2017                         RETURN(rc);
2018         } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2019                 rc = lod_verify_striping(d, buf, false, 0);
2020                 if (rc != 0)
2021                         RETURN(rc);
2022         }
2023
2024         rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2025         if (rc != 0)
2026                 RETURN(rc);
2027
2028         /* Note: Do not set LinkEA on sub-stripes, otherwise
2029          * it will confuse the fid2path process(see mdt_path_current()).
2030          * The linkEA between master and sub-stripes is set in
2031          * lod_xattr_set_lmv(). */
2032         if (strcmp(name, XATTR_NAME_LINK) == 0)
2033                 RETURN(0);
2034
2035         /* set xattr to each stripes, if needed */
2036         rc = lod_load_striping(env, lo);
2037         if (rc != 0)
2038                 RETURN(rc);
2039
2040         if (lo->ldo_dir_stripenr == 0)
2041                 RETURN(0);
2042
2043         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2044                 LASSERT(lo->ldo_stripe[i]);
2045
2046                 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2047                                                buf, name, fl, th);
2048                 if (rc != 0)
2049                         break;
2050         }
2051
2052         RETURN(rc);
2053 }
2054
2055 static int
2056 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2057                                      struct lod_object *lo,
2058                                      struct dt_object *dt, struct thandle *th,
2059                                      int stripe_idx,
2060                                      struct lod_obj_stripe_cb_data *data)
2061 {
2062         struct lod_thread_info *info = lod_env_info(env);
2063         struct filter_fid *ff = &info->lti_ff;
2064         struct lu_buf *buf = &info->lti_buf;
2065         int rc;
2066
2067         buf->lb_buf = ff;
2068         buf->lb_len = sizeof(*ff);
2069         rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2070         if (rc == -ENODATA)
2071                 return 0;
2072
2073         if (rc < 0)
2074                 return rc;
2075
2076         ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2077         ff->ff_parent.f_ver = stripe_idx;
2078         fid_cpu_to_le(&ff->ff_parent, &ff->ff_parent);
2079         if (data->locd_declare)
2080                 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2081                                                LU_XATTR_REPLACE, th);
2082         else
2083                 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2084                                        LU_XATTR_REPLACE, th);
2085
2086         return rc;
2087 }
2088
2089 /**
2090  * Reset parent FID on OST object
2091  *
2092  * Replace parent FID with @dt object FID, which is only called during migration
2093  * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2094  * the FID is changed.
2095  *
2096  * \param[in] env execution environment
2097  * \param[in] dt dt_object whose stripes's parent FID will be reset
2098  * \parem[in] th thandle
2099  * \param[in] declare if it is declare
2100  *
2101  * \retval      0 if reset succeeds
2102  * \retval      negative errno if reset fails
2103  */
2104 static int lod_replace_parent_fid(const struct lu_env *env,
2105                                   struct dt_object *dt,
2106                                   struct thandle *th, bool declare)
2107 {
2108         struct lod_object *lo = lod_dt_obj(dt);
2109         struct lod_thread_info  *info = lod_env_info(env);
2110         struct lu_buf *buf = &info->lti_buf;
2111         struct filter_fid *ff;
2112         struct lod_obj_stripe_cb_data data;
2113         int rc;
2114         ENTRY;
2115
2116         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2117
2118         /* set xattr to each stripes, if needed */
2119         rc = lod_load_striping(env, lo);
2120         if (rc != 0)
2121                 RETURN(rc);
2122
2123         if (!lod_obj_is_striped(dt))
2124                 RETURN(0);
2125
2126         if (info->lti_ea_store_size < sizeof(*ff)) {
2127                 rc = lod_ea_store_resize(info, sizeof(*ff));
2128                 if (rc != 0)
2129                         RETURN(rc);
2130         }
2131
2132         buf->lb_buf = info->lti_ea_store;
2133         buf->lb_len = info->lti_ea_store_size;
2134
2135         data.locd_declare = declare;
2136         rc = lod_obj_for_each_stripe(env, lo, th,
2137                         lod_obj_stripe_replace_parent_fid_cb, &data);
2138
2139         RETURN(rc);
2140 }
2141
2142 inline __u16 lod_comp_entry_stripecnt(struct lod_object *lo,
2143                                       struct lod_layout_component *entry,
2144                                       bool is_dir)
2145 {
2146         struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2147
2148         if (is_dir)
2149                 return  0;
2150         else if (lod_comp_inited(entry))
2151                 return entry->llc_stripenr;
2152         else if ((__u16)-1 == entry->llc_stripenr)
2153                 return lod->lod_desc.ld_tgt_count;
2154         else
2155                 return lod_get_stripecnt(lod, lo, entry->llc_stripenr);
2156 }
2157
2158 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2159 {
2160         int magic, size = 0, i;
2161         struct lod_layout_component *comp_entries;
2162         __u16 comp_cnt;
2163         bool is_composite;
2164
2165         if (is_dir) {
2166                 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2167                 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2168                 is_composite =
2169                         lo->ldo_def_striping->lds_def_striping_is_composite;
2170         } else {
2171                 comp_cnt = lo->ldo_comp_cnt;
2172                 comp_entries = lo->ldo_comp_entries;
2173                 is_composite = lo->ldo_is_composite;
2174         }
2175
2176
2177         LASSERT(comp_cnt != 0 && comp_entries != NULL);
2178         if (is_composite) {
2179                 size = sizeof(struct lov_comp_md_v1) +
2180                        sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2181                 LASSERT(size % sizeof(__u64) == 0);
2182         }
2183
2184         for (i = 0; i < comp_cnt; i++) {
2185                 __u16 stripenr;
2186
2187                 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2188                 stripenr = lod_comp_entry_stripecnt(lo, &comp_entries[i],
2189                                                     is_dir);
2190                 if (!is_dir && is_composite)
2191                         lod_comp_shrink_stripecount(&comp_entries[i],
2192                                                     &stripenr);
2193
2194                 size += lov_user_md_size(stripenr, magic);
2195                 LASSERT(size % sizeof(__u64) == 0);
2196         }
2197         return size;
2198 }
2199
2200 /**
2201  * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2202  * the xattr value is binary lov_comp_md_v1 which contains component(s)
2203  * to be added.
2204   *
2205  * \param[in] env       execution environment
2206  * \param[in] dt        dt_object to add components on
2207  * \param[in] buf       buffer contains components to be added
2208  * \parem[in] th        thandle
2209  *
2210  * \retval      0 on success
2211  * \retval      negative errno on failure
2212  */
2213 static int lod_declare_layout_add(const struct lu_env *env,
2214                                   struct dt_object *dt,
2215                                   const struct lu_buf *buf,
2216                                   struct thandle *th)
2217 {
2218         struct lod_thread_info  *info = lod_env_info(env);
2219         struct lod_layout_component *comp_array, *lod_comp;
2220         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2221         struct dt_object *next = dt_object_child(dt);
2222         struct lov_desc         *desc = &d->lod_desc;
2223         struct lod_object       *lo = lod_dt_obj(dt);
2224         struct lov_user_md_v3   *v3;
2225         struct lov_comp_md_v1   *comp_v1 = buf->lb_buf;
2226         __u32   magic;
2227         __u64   prev_end;
2228         int     i, rc, array_cnt;
2229         ENTRY;
2230
2231         LASSERT(lo->ldo_is_composite);
2232
2233         prev_end = lo->ldo_comp_entries[lo->ldo_comp_cnt - 1].llc_extent.e_end;
2234         rc = lod_verify_striping(d, buf, false, prev_end);
2235         if (rc != 0)
2236                 RETURN(rc);
2237
2238         magic = comp_v1->lcm_magic;
2239         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2240                 lustre_swab_lov_comp_md_v1(comp_v1);
2241                 magic = comp_v1->lcm_magic;
2242         }
2243
2244         if (magic != LOV_USER_MAGIC_COMP_V1)
2245                 RETURN(-EINVAL);
2246
2247         array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2248         OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2249         if (comp_array == NULL)
2250                 RETURN(-ENOMEM);
2251
2252         memcpy(comp_array, lo->ldo_comp_entries,
2253                sizeof(*comp_array) * lo->ldo_comp_cnt);
2254
2255         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2256                 struct lov_user_md_v1 *v1;
2257                 struct lu_extent *ext;
2258
2259                 v1 = (struct lov_user_md *)((char *)comp_v1 +
2260                                 comp_v1->lcm_entries[i].lcme_offset);
2261                 ext = &comp_v1->lcm_entries[i].lcme_extent;
2262
2263                 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2264                 lod_comp->llc_extent.e_start = ext->e_start;
2265                 lod_comp->llc_extent.e_end = ext->e_end;
2266                 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2267
2268                 lod_comp->llc_stripenr = v1->lmm_stripe_count;
2269                 if (!lod_comp->llc_stripenr ||
2270                     lod_comp->llc_stripenr == (__u16)-1)
2271                         lod_comp->llc_stripenr = desc->ld_default_stripe_count;
2272                 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2273                 if (!lod_comp->llc_stripe_size)
2274                         lod_comp->llc_stripe_size =
2275                                 desc->ld_default_stripe_size;
2276
2277                 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2278                         v3 = (struct lov_user_md_v3 *) v1;
2279                         if (v3->lmm_pool_name[0] != '\0') {
2280                                 rc = lod_set_pool(&lod_comp->llc_pool,
2281                                                   v3->lmm_pool_name);
2282                                 if (rc)
2283                                         GOTO(error, rc);
2284                         }
2285                 }
2286         }
2287
2288         OBD_FREE(lo->ldo_comp_entries, sizeof(*lod_comp) * lo->ldo_comp_cnt);
2289         lo->ldo_comp_entries = comp_array;
2290         lo->ldo_comp_cnt = array_cnt;
2291         /* No need to increase layout generation here, it will be increased
2292          * later when generating component ID for the new components */
2293
2294         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2295         rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2296                                               XATTR_NAME_LOV, 0, th);
2297         if (rc)
2298                 GOTO(error, rc);
2299
2300         RETURN(0);
2301
2302 error:
2303         for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2304                 lod_comp = &comp_array[i];
2305                 if (lod_comp->llc_pool != NULL) {
2306                         OBD_FREE(lod_comp->llc_pool,
2307                                  strlen(lod_comp->llc_pool) + 1);
2308                         lod_comp->llc_pool = NULL;
2309                 }
2310         }
2311         OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2312         RETURN(rc);
2313 }
2314
2315 /**
2316  * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2317  * the '$field' can only be 'flags' now. The xattr value is binary
2318  * lov_comp_md_v1 which contains the component ID(s) and the value of
2319  * the field to be modified.
2320  *
2321  * \param[in] env       execution environment
2322  * \param[in] dt        dt_object to be modified
2323  * \param[in] op        operation string, like "set.flags"
2324  * \param[in] buf       buffer contains components to be set
2325  * \parem[in] th        thandle
2326  *
2327  * \retval      0 on success
2328  * \retval      negative errno on failure
2329  */
2330 static int lod_declare_layout_set(const struct lu_env *env,
2331                                   struct dt_object *dt,
2332                                   char *op, const struct lu_buf *buf,
2333                                   struct thandle *th)
2334 {
2335         struct lod_layout_component     *lod_comp;
2336         struct lod_thread_info  *info = lod_env_info(env);
2337         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2338         struct lod_object       *lo = lod_dt_obj(dt);
2339         struct lov_comp_md_v1   *comp_v1 = buf->lb_buf;
2340         __u32   magic, id;
2341         int     i, j, rc;
2342         bool    changed = false;
2343         ENTRY;
2344
2345         if (strcmp(op, "set.flags") != 0) {
2346                 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2347                        lod2obd(d)->obd_name, op);
2348                 RETURN(-ENOTSUPP);
2349         }
2350
2351         magic = comp_v1->lcm_magic;
2352         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2353                 lustre_swab_lov_comp_md_v1(comp_v1);
2354                 magic = comp_v1->lcm_magic;
2355         }
2356
2357         if (magic != LOV_USER_MAGIC_COMP_V1)
2358                 RETURN(-EINVAL);
2359
2360         if (comp_v1->lcm_entry_count == 0) {
2361                 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2362                        lod2obd(d)->obd_name);
2363                 RETURN(-EINVAL);
2364         }
2365
2366         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2367                 id = comp_v1->lcm_entries[i].lcme_id;
2368
2369                 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2370                         lod_comp = &lo->ldo_comp_entries[j];
2371                         if (id == lod_comp->llc_id || id == LCME_ID_ALL) {
2372                                 lod_comp->llc_flags =
2373                                         comp_v1->lcm_entries[i].lcme_flags;
2374                                 changed = true;
2375                         }
2376                 }
2377         }
2378
2379         if (!changed) {
2380                 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2381                        lod2obd(d)->obd_name);
2382                 RETURN(-EINVAL);
2383         }
2384
2385         lod_obj_inc_layout_gen(lo);
2386
2387         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2388         rc = lod_sub_declare_xattr_set(env, dt, &info->lti_buf,
2389                                        XATTR_NAME_LOV, 0, th);
2390         RETURN(rc);
2391 }
2392
2393 /**
2394  * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2395  * and the xattr value is a unique component ID or a special lcme_id.
2396  *
2397  * \param[in] env       execution environment
2398  * \param[in] dt        dt_object to be operated on
2399  * \param[in] buf       buffer contains component ID or lcme_id
2400  * \parem[in] th        thandle
2401  *
2402  * \retval      0 on success
2403  * \retval      negative errno on failure
2404  */
2405 static int lod_declare_layout_del(const struct lu_env *env,
2406                                   struct dt_object *dt,
2407                                   const struct lu_buf *buf,
2408                                   struct thandle *th)
2409 {
2410         struct lod_thread_info  *info = lod_env_info(env);
2411         struct dt_object *next = dt_object_child(dt);
2412         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2413         struct lod_object *lo = lod_dt_obj(dt);
2414         struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2415         struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2416         __u32 magic, id, flags, neg_flags = 0;
2417         int rc, i, j, left;
2418         ENTRY;
2419
2420         LASSERT(lo->ldo_is_composite);
2421
2422         rc = lod_verify_striping(d, buf, false, 0);
2423         if (rc != 0)
2424                 RETURN(rc);
2425
2426         magic = comp_v1->lcm_magic;
2427         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2428                 lustre_swab_lov_comp_md_v1(comp_v1);
2429                 magic = comp_v1->lcm_magic;
2430         }
2431
2432         if (magic != LOV_USER_MAGIC_COMP_V1)
2433                 RETURN(-EINVAL);
2434
2435         id = comp_v1->lcm_entries[0].lcme_id;
2436         flags = comp_v1->lcm_entries[0].lcme_flags;
2437
2438         if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
2439                 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
2440                        lod2obd(d)->obd_name, id, flags);
2441                 RETURN(-EINVAL);
2442         }
2443
2444         if (id != LCME_ID_INVAL && flags != 0) {
2445                 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
2446                        lod2obd(d)->obd_name);
2447                 RETURN(-EINVAL);
2448         }
2449
2450         if (flags & LCME_FL_NEG) {
2451                 neg_flags = flags & ~LCME_FL_NEG;
2452                 flags = 0;
2453         }
2454
2455         left = lo->ldo_comp_cnt;
2456         if (left <= 0)
2457                 RETURN(-EINVAL);
2458
2459         for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
2460                 struct lod_layout_component *lod_comp;
2461
2462                 lod_comp = &lo->ldo_comp_entries[i];
2463
2464                 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
2465                         continue;
2466                 else if (flags && !(flags & lod_comp->llc_flags))
2467                         continue;
2468                 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
2469                         continue;
2470
2471                 if (left != (i + 1)) {
2472                         CDEBUG(D_LAYOUT, "%s: this deletion will create "
2473                                "a hole.\n", lod2obd(d)->obd_name);
2474                         RETURN(-EINVAL);
2475                 }
2476                 left--;
2477
2478                 /* Mark the component as deleted */
2479                 lod_comp->llc_id = LCME_ID_INVAL;
2480
2481                 /* Not instantiated component */
2482                 if (lod_comp->llc_stripe == NULL)
2483                         continue;
2484
2485                 LASSERT(lod_comp->llc_stripenr > 0);
2486                 for (j = 0; j < lod_comp->llc_stripenr; j++) {
2487                         struct dt_object *obj = lod_comp->llc_stripe[j];
2488
2489                         if (obj == NULL)
2490                                 continue;
2491                         rc = lod_sub_declare_destroy(env, obj, th);
2492                         if (rc)
2493                                 RETURN(rc);
2494                 }
2495         }
2496
2497         LASSERTF(left >= 0, "left = %d\n", left);
2498         if (left == lo->ldo_comp_cnt) {
2499                 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
2500                        lod2obd(d)->obd_name, id);
2501                 RETURN(-EINVAL);
2502         }
2503
2504         memset(attr, 0, sizeof(*attr));
2505         attr->la_valid = LA_SIZE;
2506         rc = lod_sub_declare_attr_set(env, next, attr, th);
2507         if (rc)
2508                 RETURN(rc);
2509
2510         if (left > 0) {
2511                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2512                 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2513                                                XATTR_NAME_LOV, 0, th);
2514         } else {
2515                 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
2516         }
2517
2518         RETURN(rc);
2519 }
2520
2521 /**
2522  * Declare layout add/set/del operations issued by special xattr names:
2523  *
2524  * XATTR_LUSTRE_LOV.add         add component(s) to existing file
2525  * XATTR_LUSTRE_LOV.del         delete component(s) from existing file
2526  * XATTR_LUSTRE_LOV.set.$field  set specified field of certain component(s)
2527  *
2528  * \param[in] env       execution environment
2529  * \param[in] dt        object
2530  * \param[in] name      name of xattr
2531  * \param[in] buf       lu_buf contains xattr value
2532  * \param[in] th        transaction handle
2533  *
2534  * \retval              0 on success
2535  * \retval              negative if failed
2536  */
2537 static int lod_declare_modify_layout(const struct lu_env *env,
2538                                      struct dt_object *dt,
2539                                      const char *name,
2540                                      const struct lu_buf *buf,
2541                                      struct thandle *th)
2542 {
2543         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2544         struct lod_object *lo = lod_dt_obj(dt);
2545         struct dt_object *next = dt_object_child(&lo->ldo_obj);
2546         char *op;
2547         int rc, len = strlen(XATTR_LUSTRE_LOV);
2548         ENTRY;
2549
2550         LASSERT(dt_object_exists(dt));
2551
2552         if (strlen(name) <= len || name[len] != '.') {
2553                 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
2554                        lod2obd(d)->obd_name, name);
2555                 RETURN(-EINVAL);
2556         }
2557         len++;
2558
2559         dt_write_lock(env, next, 0);
2560         rc = lod_load_striping_locked(env, lo);
2561         if (rc)
2562                 GOTO(unlock, rc);
2563
2564         /* the layout to be modified must be a composite layout */
2565         if (!lo->ldo_is_composite) {
2566                 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
2567                        lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
2568                 GOTO(unlock, rc = -EINVAL);
2569         }
2570
2571         op = (char *)name + len;
2572         if (strcmp(op, "add") == 0) {
2573                 rc = lod_declare_layout_add(env, dt, buf, th);
2574         } else if (strcmp(op, "del") == 0) {
2575                 rc = lod_declare_layout_del(env, dt, buf, th);
2576         } else if (strncmp(op, "set", strlen("set")) == 0) {
2577                 rc = lod_declare_layout_set(env, dt, op, buf, th);
2578         } else  {
2579                 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
2580                        lod2obd(d)->obd_name, name);
2581                 GOTO(unlock, rc = -ENOTSUPP);
2582         }
2583 unlock:
2584         if (rc)
2585                 lod_object_free_striping(env, lo);
2586         dt_write_unlock(env, next);
2587
2588         RETURN(rc);
2589 }
2590
2591 /**
2592  * Implementation of dt_object_operations::do_declare_xattr_set.
2593  *
2594  * \see dt_object_operations::do_declare_xattr_set() in the API description
2595  * for details.
2596  *
2597  * the extension to the API:
2598  *   - declaring LOVEA requests striping creation
2599  *   - LU_XATTR_REPLACE means layout swap
2600  */
2601 static int lod_declare_xattr_set(const struct lu_env *env,
2602                                  struct dt_object *dt,
2603                                  const struct lu_buf *buf,
2604                                  const char *name, int fl,
2605                                  struct thandle *th)
2606 {
2607         struct dt_object *next = dt_object_child(dt);
2608         struct lu_attr   *attr = &lod_env_info(env)->lti_attr;
2609         __u32             mode;
2610         int               rc;
2611         ENTRY;
2612
2613         mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2614         if ((S_ISREG(mode) || mode == 0) && !(fl & LU_XATTR_REPLACE) &&
2615             (strcmp(name, XATTR_NAME_LOV) == 0 ||
2616              strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
2617                 /*
2618                  * this is a request to create object's striping.
2619                  *
2620                  * allow to declare predefined striping on a new (!mode) object
2621                  * which is supposed to be replay of regular file creation
2622                  * (when LOV setting is declared)
2623                  *
2624                  * LU_XATTR_REPLACE is set to indicate a layout swap
2625                  */
2626                 if (dt_object_exists(dt)) {
2627                         rc = dt_attr_get(env, next, attr);
2628                         if (rc)
2629                                 RETURN(rc);
2630                 } else {
2631                         memset(attr, 0, sizeof(*attr));
2632                         attr->la_valid = LA_TYPE | LA_MODE;
2633                         attr->la_mode = S_IFREG;
2634                 }
2635                 rc = lod_declare_striped_create(env, dt, attr, buf, th);
2636         } else if (S_ISREG(mode) &&
2637                    strlen(name) > strlen(XATTR_LUSTRE_LOV) + 1 &&
2638                    strncmp(name, XATTR_LUSTRE_LOV,
2639                            strlen(XATTR_LUSTRE_LOV)) == 0) {
2640                 /*
2641                  * this is a request to modify object's striping.
2642                  * add/set/del component(s).
2643                  */
2644                 if (!dt_object_exists(dt))
2645                         RETURN(-ENOENT);
2646
2647                 rc = lod_declare_modify_layout(env, dt, name, buf, th);
2648         } else if (S_ISDIR(mode)) {
2649                 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2650         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
2651                 rc = lod_replace_parent_fid(env, dt, th, true);
2652         } else {
2653                 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2654         }
2655
2656         RETURN(rc);
2657 }
2658
2659 /**
2660  * Apply xattr changes to the object.
2661  *
2662  * Applies xattr changes to the object and the stripes if the latter exist.
2663  *
2664  * \param[in] env       execution environment
2665  * \param[in] dt        object
2666  * \param[in] buf       buffer pointing to the new value of xattr
2667  * \param[in] name      name of xattr
2668  * \param[in] fl        flags
2669  * \param[in] th        transaction handle
2670  *
2671  * \retval              0 on success
2672  * \retval              negative if failed
2673  */
2674 static int lod_xattr_set_internal(const struct lu_env *env,
2675                                   struct dt_object *dt,
2676                                   const struct lu_buf *buf,
2677                                   const char *name, int fl,
2678                                   struct thandle *th)
2679 {
2680         struct dt_object        *next = dt_object_child(dt);
2681         struct lod_object       *lo = lod_dt_obj(dt);
2682         int                     rc;
2683         int                     i;
2684         ENTRY;
2685
2686         rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
2687         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2688                 RETURN(rc);
2689
2690         /* Note: Do not set LinkEA on sub-stripes, otherwise
2691          * it will confuse the fid2path process(see mdt_path_current()).
2692          * The linkEA between master and sub-stripes is set in
2693          * lod_xattr_set_lmv(). */
2694         if (lo->ldo_dir_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2695                 RETURN(0);
2696
2697         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2698                 LASSERT(lo->ldo_stripe[i]);
2699
2700                 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
2701                                        fl, th);
2702                 if (rc != 0)
2703                         break;
2704         }
2705
2706         RETURN(rc);
2707 }
2708
2709 /**
2710  * Delete an extended attribute.
2711  *
2712  * Deletes specified xattr from the object and the stripes if the latter exist.
2713  *
2714  * \param[in] env       execution environment
2715  * \param[in] dt        object
2716  * \param[in] name      name of xattr
2717  * \param[in] th        transaction handle
2718  *
2719  * \retval              0 on success
2720  * \retval              negative if failed
2721  */
2722 static int lod_xattr_del_internal(const struct lu_env *env,
2723                                   struct dt_object *dt,
2724                                   const char *name, struct thandle *th)
2725 {
2726         struct dt_object        *next = dt_object_child(dt);
2727         struct lod_object       *lo = lod_dt_obj(dt);
2728         int                     rc;
2729         int                     i;
2730         ENTRY;
2731
2732         rc = lod_sub_xattr_del(env, next, name, th);
2733         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2734                 RETURN(rc);
2735
2736         if (lo->ldo_dir_stripenr == 0)
2737                 RETURN(rc);
2738
2739         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2740                 LASSERT(lo->ldo_stripe[i]);
2741
2742                 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
2743                 if (rc != 0)
2744                         break;
2745         }
2746
2747         RETURN(rc);
2748 }
2749
2750 /**
2751  * Set default striping on a directory.
2752  *
2753  * Sets specified striping on a directory object unless it matches the default
2754  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2755  * EA. This striping will be used when regular file is being created in this
2756  * directory.
2757  *
2758  * \param[in] env       execution environment
2759  * \param[in] dt        the striped object
2760  * \param[in] buf       buffer with the striping
2761  * \param[in] name      name of EA
2762  * \param[in] fl        xattr flag (see OSD API description)
2763  * \param[in] th        transaction handle
2764  *
2765  * \retval              0 on success
2766  * \retval              negative if failed
2767  */
2768 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
2769                                     struct dt_object *dt,
2770                                     const struct lu_buf *buf,
2771                                     const char *name, int fl,
2772                                     struct thandle *th)
2773 {
2774         struct lov_user_md_v1   *lum;
2775         struct lov_user_md_v3   *v3 = NULL;
2776         const char              *pool_name = NULL;
2777         int                      rc;
2778         bool                     is_del;
2779         ENTRY;
2780
2781         LASSERT(buf != NULL && buf->lb_buf != NULL);
2782         lum = buf->lb_buf;
2783
2784         switch (lum->lmm_magic) {
2785         case LOV_USER_MAGIC_V3:
2786                 v3 = buf->lb_buf;
2787                 if (v3->lmm_pool_name[0] != '\0')
2788                         pool_name = v3->lmm_pool_name;
2789                 /* fall through */
2790         case LOV_USER_MAGIC_V1:
2791                 /* if { size, offset, count } = { 0, -1, 0 } and no pool
2792                  * (i.e. all default values specified) then delete default
2793                  * striping from dir. */
2794                 CDEBUG(D_LAYOUT,
2795                        "set default striping: sz %u # %u offset %d %s %s\n",
2796                        (unsigned)lum->lmm_stripe_size,
2797                        (unsigned)lum->lmm_stripe_count,
2798                        (int)lum->lmm_stripe_offset,
2799                        v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
2800
2801                 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
2802                                              lum->lmm_stripe_count,
2803                                              lum->lmm_stripe_offset,
2804                                              pool_name);
2805                 break;
2806         case LOV_USER_MAGIC_COMP_V1:
2807                 is_del = false;
2808                 break;
2809         default:
2810                 CERROR("Invalid magic %x\n", lum->lmm_magic);
2811                 RETURN(-EINVAL);
2812         }
2813
2814         if (is_del) {
2815                 rc = lod_xattr_del_internal(env, dt, name, th);
2816                 if (rc == -ENODATA)
2817                         rc = 0;
2818         } else {
2819                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2820         }
2821
2822         RETURN(rc);
2823 }
2824
2825 /**
2826  * Set default striping on a directory object.
2827  *
2828  * Sets specified striping on a directory object unless it matches the default
2829  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2830  * EA. This striping will be used when a new directory is being created in the
2831  * directory.
2832  *
2833  * \param[in] env       execution environment
2834  * \param[in] dt        the striped object
2835  * \param[in] buf       buffer with the striping
2836  * \param[in] name      name of EA
2837  * \param[in] fl        xattr flag (see OSD API description)
2838  * \param[in] th        transaction handle
2839  *
2840  * \retval              0 on success
2841  * \retval              negative if failed
2842  */
2843 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
2844                                             struct dt_object *dt,
2845                                             const struct lu_buf *buf,
2846                                             const char *name, int fl,
2847                                             struct thandle *th)
2848 {
2849         struct lmv_user_md_v1   *lum;
2850         int                      rc;
2851         ENTRY;
2852
2853         LASSERT(buf != NULL && buf->lb_buf != NULL);
2854         lum = buf->lb_buf;
2855
2856         CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
2857               le32_to_cpu(lum->lum_stripe_count),
2858               (int)le32_to_cpu(lum->lum_stripe_offset));
2859
2860         if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
2861                                  le32_to_cpu(lum->lum_stripe_offset)) &&
2862                                 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
2863                 rc = lod_xattr_del_internal(env, dt, name, th);
2864                 if (rc == -ENODATA)
2865                         rc = 0;
2866         } else {
2867                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2868                 if (rc != 0)
2869                         RETURN(rc);
2870         }
2871
2872         RETURN(rc);
2873 }
2874
2875 /**
2876  * Turn directory into a striped directory.
2877  *
2878  * During replay the client sends the striping created before MDT
2879  * failure, then the layer above LOD sends this defined striping
2880  * using ->do_xattr_set(), so LOD uses this method to replay creation
2881  * of the stripes. Notice the original information for the striping
2882  * (#stripes, FIDs, etc) was transferred in declare path.
2883  *
2884  * \param[in] env       execution environment
2885  * \param[in] dt        the striped object
2886  * \param[in] buf       not used currently
2887  * \param[in] name      not used currently
2888  * \param[in] fl        xattr flag (see OSD API description)
2889  * \param[in] th        transaction handle
2890  *
2891  * \retval              0 on success
2892  * \retval              negative if failed
2893  */
2894 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
2895                              const struct lu_buf *buf, const char *name,
2896                              int fl, struct thandle *th)
2897 {
2898         struct lod_object       *lo = lod_dt_obj(dt);
2899         struct lod_thread_info  *info = lod_env_info(env);
2900         struct lu_attr          *attr = &info->lti_attr;
2901         struct dt_object_format *dof = &info->lti_format;
2902         struct lu_buf           lmv_buf;
2903         struct lu_buf           slave_lmv_buf;
2904         struct lmv_mds_md_v1    *lmm;
2905         struct lmv_mds_md_v1    *slave_lmm = NULL;
2906         struct dt_insert_rec    *rec = &info->lti_dt_rec;
2907         int                     i;
2908         int                     rc;
2909         ENTRY;
2910
2911         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2912                 RETURN(-ENOTDIR);
2913
2914         /* The stripes are supposed to be allocated in declare phase,
2915          * if there are no stripes being allocated, it will skip */
2916         if (lo->ldo_dir_stripenr == 0)
2917                 RETURN(0);
2918
2919         rc = dt_attr_get(env, dt_object_child(dt), attr);
2920         if (rc != 0)
2921                 RETURN(rc);
2922
2923         attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
2924                          LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
2925         dof->dof_type = DFT_DIR;
2926
2927         rc = lod_prep_lmv_md(env, dt, &lmv_buf);
2928         if (rc != 0)
2929                 RETURN(rc);
2930         lmm = lmv_buf.lb_buf;
2931
2932         OBD_ALLOC_PTR(slave_lmm);
2933         if (slave_lmm == NULL)
2934                 RETURN(-ENOMEM);
2935
2936         lod_prep_slave_lmv_md(slave_lmm, lmm);
2937         slave_lmv_buf.lb_buf = slave_lmm;
2938         slave_lmv_buf.lb_len = sizeof(*slave_lmm);
2939
2940         rec->rec_type = S_IFDIR;
2941         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2942                 struct dt_object *dto;
2943                 char             *stripe_name = info->lti_key;
2944                 struct lu_name          *sname;
2945                 struct linkea_data       ldata          = { NULL };
2946                 struct lu_buf            linkea_buf;
2947
2948                 dto = lo->ldo_stripe[i];
2949
2950                 dt_write_lock(env, dto, MOR_TGT_CHILD);
2951                 rc = lod_sub_create(env, dto, attr, NULL, dof, th);
2952                 if (rc != 0) {
2953                         dt_write_unlock(env, dto);
2954                         GOTO(out, rc);
2955                 }
2956
2957                 rc = lod_sub_ref_add(env, dto, th);
2958                 dt_write_unlock(env, dto);
2959                 if (rc != 0)
2960                         GOTO(out, rc);
2961
2962                 rec->rec_fid = lu_object_fid(&dto->do_lu);
2963                 rc = lod_sub_insert(env, dto, (const struct dt_rec *)rec,
2964                                     (const struct dt_key *)dot, th, 0);
2965                 if (rc != 0)
2966                         GOTO(out, rc);
2967
2968                 rec->rec_fid = lu_object_fid(&dt->do_lu);
2969                 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
2970                                     (const struct dt_key *)dotdot, th, 0);
2971                 if (rc != 0)
2972                         GOTO(out, rc);
2973
2974                 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
2975                     cfs_fail_val != i) {
2976                         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
2977                             cfs_fail_val == i)
2978                                 slave_lmm->lmv_master_mdt_index =
2979                                                         cpu_to_le32(i + 1);
2980                         else
2981                                 slave_lmm->lmv_master_mdt_index =
2982                                                         cpu_to_le32(i);
2983
2984                         rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
2985                                                XATTR_NAME_LMV, fl, th);
2986                         if (rc != 0)
2987                                 GOTO(out, rc);
2988                 }
2989
2990                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
2991                     cfs_fail_val == i)
2992                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2993                                  PFID(lu_object_fid(&dto->do_lu)), i + 1);
2994                 else
2995                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2996                                  PFID(lu_object_fid(&dto->do_lu)), i);
2997
2998                 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2999                 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
3000                                       sname, lu_object_fid(&dt->do_lu));
3001                 if (rc != 0)
3002                         GOTO(out, rc);
3003
3004                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
3005                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
3006                 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
3007                                        XATTR_NAME_LINK, 0, th);
3008                 if (rc != 0)
3009                         GOTO(out, rc);
3010
3011                 rec->rec_fid = lu_object_fid(&dto->do_lu);
3012                 rc = lod_sub_insert(env, dt_object_child(dt),
3013                                     (const struct dt_rec *)rec,
3014                                     (const struct dt_key *)stripe_name, th, 0);
3015                 if (rc != 0)
3016                         GOTO(out, rc);
3017
3018                 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
3019                 if (rc != 0)
3020                         GOTO(out, rc);
3021         }
3022
3023         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
3024                 rc = lod_sub_xattr_set(env, dt_object_child(dt),
3025                                        &lmv_buf, XATTR_NAME_LMV, fl, th);
3026 out:
3027         if (slave_lmm != NULL)
3028                 OBD_FREE_PTR(slave_lmm);
3029
3030         RETURN(rc);
3031 }
3032
3033 /**
3034  * Helper function to declare/execute creation of a striped directory
3035  *
3036  * Called in declare/create object path, prepare striping for a directory
3037  * and prepare defaults data striping for the objects to be created in
3038  * that directory. Notice the function calls "declaration" or "execution"
3039  * methods depending on \a declare param. This is a consequence of the
3040  * current approach while we don't have natural distributed transactions:
3041  * we basically execute non-local updates in the declare phase. So, the
3042  * arguments for the both phases are the same and this is the reason for
3043  * this function to exist.
3044  *
3045  * \param[in] env       execution environment
3046  * \param[in] dt        object
3047  * \param[in] attr      attributes the stripes will be created with
3048  * \param[in] dof       format of stripes (see OSD API description)
3049  * \param[in] th        transaction handle
3050  * \param[in] declare   where to call "declare" or "execute" methods
3051  *
3052  * \retval              0 on success
3053  * \retval              negative if failed
3054  */
3055 static int lod_dir_striping_create_internal(const struct lu_env *env,
3056                                             struct dt_object *dt,
3057                                             struct lu_attr *attr,
3058                                             struct dt_object_format *dof,
3059                                             struct thandle *th,
3060                                             bool declare)
3061 {
3062         struct lod_thread_info *info = lod_env_info(env);
3063         struct lod_object *lo = lod_dt_obj(dt);
3064         const struct lod_default_striping *lds = lo->ldo_def_striping;
3065         int rc;
3066         ENTRY;
3067
3068         LASSERT(ergo(lds != NULL,
3069                      lds->lds_def_striping_set ||
3070                      lds->lds_dir_def_striping_set));
3071
3072         if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripenr,
3073                                  lo->ldo_dir_stripe_offset)) {
3074                 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3075                 int stripe_count = lo->ldo_dir_stripenr;
3076
3077                 if (info->lti_ea_store_size < sizeof(*v1)) {
3078                         rc = lod_ea_store_resize(info, sizeof(*v1));
3079                         if (rc != 0)
3080                                 RETURN(rc);
3081                         v1 = info->lti_ea_store;
3082                 }
3083
3084                 memset(v1, 0, sizeof(*v1));
3085                 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3086                 v1->lum_stripe_count = cpu_to_le32(stripe_count);
3087                 v1->lum_stripe_offset =
3088                                 cpu_to_le32(lo->ldo_dir_stripe_offset);
3089
3090                 info->lti_buf.lb_buf = v1;
3091                 info->lti_buf.lb_len = sizeof(*v1);
3092
3093                 if (declare)
3094                         rc = lod_declare_xattr_set_lmv(env, dt, attr,
3095                                                        &info->lti_buf, dof, th);
3096                 else
3097                         rc = lod_xattr_set_lmv(env, dt, &info->lti_buf,
3098                                                XATTR_NAME_LMV, 0, th);
3099                 if (rc != 0)
3100                         RETURN(rc);
3101         }
3102
3103         /* Transfer default LMV striping from the parent */
3104         if (lds != NULL && lds->lds_dir_def_striping_set &&
3105             !LMVEA_DELETE_VALUES(lds->lds_dir_def_stripenr,
3106                                  lds->lds_dir_def_stripe_offset)) {
3107                 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3108
3109                 if (info->lti_ea_store_size < sizeof(*v1)) {
3110                         rc = lod_ea_store_resize(info, sizeof(*v1));
3111                         if (rc != 0)
3112                                 RETURN(rc);
3113                         v1 = info->lti_ea_store;
3114                 }
3115
3116                 memset(v1, 0, sizeof(*v1));
3117                 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3118                 v1->lum_stripe_count = cpu_to_le32(lds->lds_dir_def_stripenr);
3119                 v1->lum_stripe_offset =
3120                                 cpu_to_le32(lds->lds_dir_def_stripe_offset);
3121                 v1->lum_hash_type =
3122                                 cpu_to_le32(lds->lds_dir_def_hash_type);
3123
3124                 info->lti_buf.lb_buf = v1;
3125                 info->lti_buf.lb_len = sizeof(*v1);
3126                 if (declare)
3127                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3128                                                        XATTR_NAME_DEFAULT_LMV,
3129                                                        0, th);
3130                 else
3131                         rc = lod_xattr_set_default_lmv_on_dir(env, dt,
3132                                                   &info->lti_buf,
3133                                                   XATTR_NAME_DEFAULT_LMV, 0,
3134                                                   th);
3135                 if (rc != 0)
3136                         RETURN(rc);
3137         }
3138
3139         /* Transfer default LOV striping from the parent */
3140         if (lds != NULL && lds->lds_def_striping_set &&
3141             lds->lds_def_comp_cnt != 0) {
3142                 struct lov_mds_md *lmm;
3143                 int lmm_size = lod_comp_md_size(lo, true);
3144
3145                 if (info->lti_ea_store_size < lmm_size) {
3146                         rc = lod_ea_store_resize(info, lmm_size);
3147                         if (rc != 0)
3148                                 RETURN(rc);
3149                 }
3150                 lmm = info->lti_ea_store;
3151
3152                 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
3153                 if (rc != 0)
3154                         RETURN(rc);
3155
3156                 info->lti_buf.lb_buf = lmm;
3157                 info->lti_buf.lb_len = lmm_size;
3158
3159                 if (declare)
3160                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3161                                                        XATTR_NAME_LOV, 0, th);
3162                 else
3163                         rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
3164                                                       XATTR_NAME_LOV, 0, th);
3165                 if (rc != 0)
3166                         RETURN(rc);
3167         }
3168
3169         RETURN(0);
3170 }
3171
3172 static int lod_declare_dir_striping_create(const struct lu_env *env,
3173                                            struct dt_object *dt,
3174                                            struct lu_attr *attr,
3175                                            struct dt_object_format *dof,
3176                                            struct thandle *th)
3177 {
3178         return lod_dir_striping_create_internal(env, dt, attr, dof, th, true);
3179 }
3180
3181 static int lod_dir_striping_create(const struct lu_env *env,
3182                                    struct dt_object *dt,
3183                                    struct lu_attr *attr,
3184                                    struct dt_object_format *dof,
3185                                    struct thandle *th)
3186 {
3187         return lod_dir_striping_create_internal(env, dt, attr, dof, th, false);
3188 }
3189
3190 /**
3191  * Make LOV EA for striped object.
3192  *
3193  * Generate striping information and store it in the LOV EA of the given
3194  * object. The caller must ensure nobody else is calling the function
3195  * against the object concurrently. The transaction must be started.
3196  * FLDB service must be running as well; it's used to map FID to the target,
3197  * which is stored in LOV EA.
3198  *
3199  * \param[in] env               execution environment for this thread
3200  * \param[in] lo                LOD object
3201  * \param[in] th                transaction handle
3202  *
3203  * \retval                      0 if LOV EA is stored successfully
3204  * \retval                      negative error number on failure
3205  */
3206 static int lod_generate_and_set_lovea(const struct lu_env *env,
3207                                       struct lod_object *lo,
3208                                       struct thandle *th)
3209 {
3210         struct lod_thread_info  *info = lod_env_info(env);
3211         struct dt_object        *next = dt_object_child(&lo->ldo_obj);
3212         struct lov_mds_md_v1    *lmm;
3213         int                      rc, lmm_size;
3214         ENTRY;
3215
3216         LASSERT(lo);
3217
3218         if (lo->ldo_comp_cnt == 0) {
3219                 lod_object_free_striping(env, lo);
3220                 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
3221                 RETURN(rc);
3222         }
3223
3224         lmm_size = lod_comp_md_size(lo, false);
3225         if (info->lti_ea_store_size < lmm_size) {
3226                 rc = lod_ea_store_resize(info, lmm_size);
3227                 if (rc)
3228                         RETURN(rc);
3229         }
3230         lmm = info->lti_ea_store;
3231
3232         rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
3233         if (rc)
3234                 RETURN(rc);
3235
3236         info->lti_buf.lb_buf = lmm;
3237         info->lti_buf.lb_len = lmm_size;
3238         rc = lod_sub_xattr_set(env, next, &info->lti_buf,
3239                                XATTR_NAME_LOV, 0, th);
3240         RETURN(rc);
3241 }
3242
3243 /**
3244  * Delete layout component(s)
3245  *
3246  * \param[in] env       execution environment for this thread
3247  * \param[in] dt        object
3248  * \param[in] th        transaction handle
3249  *
3250  * \retval      0 on success
3251  * \retval      negative error number on failure
3252  */
3253 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
3254                           struct thandle *th)
3255 {
3256         struct lod_layout_component     *lod_comp;
3257         struct lod_object       *lo = lod_dt_obj(dt);
3258         struct dt_object        *next = dt_object_child(dt);
3259         struct lu_attr  *attr = &lod_env_info(env)->lti_attr;
3260         int     rc, i, j, left;
3261
3262         LASSERT(lo->ldo_is_composite);
3263         LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
3264
3265         left = lo->ldo_comp_cnt;
3266         for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3267                 lod_comp = &lo->ldo_comp_entries[i];
3268
3269                 if (lod_comp->llc_id != LCME_ID_INVAL)
3270                         break;
3271                 left--;
3272
3273                 /* Not instantiated component */
3274                 if (lod_comp->llc_stripe == NULL)
3275                         continue;
3276
3277                 LASSERT(lod_comp->llc_stripenr > 0);
3278                 for (j = 0; j < lod_comp->llc_stripenr; j++) {
3279                         struct dt_object *obj = lod_comp->llc_stripe[j];
3280
3281                         if (obj == NULL)
3282                                 continue;
3283                         rc = lod_sub_destroy(env, obj, th);
3284                         if (rc)
3285                                 GOTO(out, rc);
3286
3287                         lu_object_put(env, &obj->do_lu);
3288                         lod_comp->llc_stripe[j] = NULL;
3289                 }
3290                 OBD_FREE(lod_comp->llc_stripe, sizeof(struct dt_object *) *
3291                                         lod_comp->llc_stripes_allocated);
3292                 lod_comp->llc_stripe = NULL;
3293                 lod_comp->llc_stripes_allocated = 0;
3294                 lod_obj_set_pool(lo, i, NULL);
3295                 if (lod_comp->llc_ostlist.op_array) {
3296                         OBD_FREE(lod_comp->llc_ostlist.op_array,
3297                                  lod_comp->llc_ostlist.op_size);
3298                         lod_comp->llc_ostlist.op_array = NULL;
3299                         lod_comp->llc_ostlist.op_size = 0;
3300                 }
3301         }
3302
3303         LASSERTF(left >= 0 && left < lo->ldo_comp_cnt, "left = %d\n", left);
3304         if (left > 0) {
3305                 struct lod_layout_component     *comp_array;
3306
3307                 OBD_ALLOC(comp_array, sizeof(*comp_array) * left);
3308                 if (comp_array == NULL)
3309                         GOTO(out, rc = -ENOMEM);
3310
3311                 memcpy(&comp_array[0], &lo->ldo_comp_entries[0],
3312                        sizeof(*comp_array) * left);
3313
3314                 OBD_FREE(lo->ldo_comp_entries,
3315                          sizeof(*comp_array) * lo->ldo_comp_cnt);
3316                 lo->ldo_comp_entries = comp_array;
3317                 lo->ldo_comp_cnt = left;
3318                 lod_obj_inc_layout_gen(lo);
3319         } else {
3320                 lod_free_comp_entries(lo);
3321         }
3322
3323         LASSERT(dt_object_exists(dt));
3324         rc = dt_attr_get(env, next, attr);
3325         if (rc)
3326                 GOTO(out, rc);
3327
3328         if (attr->la_size > 0) {
3329                 attr->la_size = 0;
3330                 attr->la_valid = LA_SIZE;
3331                 rc = lod_sub_attr_set(env, next, attr, th);
3332                 if (rc)
3333                         GOTO(out, rc);
3334         }
3335
3336         rc = lod_generate_and_set_lovea(env, lo, th);
3337         EXIT;
3338 out:
3339         if (rc)
3340                 lod_object_free_striping(env, lo);
3341         return rc;
3342 }
3343
3344 /**
3345  * Implementation of dt_object_operations::do_xattr_set.
3346  *
3347  * Sets specified extended attribute on the object. Three types of EAs are
3348  * special:
3349  *   LOV EA - stores striping for a regular file or default striping (when set
3350  *            on a directory)
3351  *   LMV EA - stores a marker for the striped directories
3352  *   DMV EA - stores default directory striping
3353  *
3354  * When striping is applied to a non-striped existing object (this is called
3355  * late striping), then LOD notices the caller wants to turn the object into a
3356  * striped one. The stripe objects are created and appropriate EA is set:
3357  * LOV EA storing all the stripes directly or LMV EA storing just a small header
3358  * with striping configuration.
3359  *
3360  * \see dt_object_operations::do_xattr_set() in the API description for details.
3361  */
3362 static int lod_xattr_set(const struct lu_env *env,
3363                          struct dt_object *dt, const struct lu_buf *buf,
3364                          const char *name, int fl, struct thandle *th)
3365 {
3366         struct dt_object        *next = dt_object_child(dt);
3367         int                      rc;
3368         ENTRY;
3369
3370         if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3371             strcmp(name, XATTR_NAME_LMV) == 0) {
3372                 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
3373
3374                 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
3375                                                 LMV_HASH_FLAG_MIGRATION)
3376                         rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3377                 else
3378                         rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
3379
3380                 RETURN(rc);
3381         }
3382
3383         if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3384             strcmp(name, XATTR_NAME_LOV) == 0) {
3385                 /* default LOVEA */
3386                 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
3387                 RETURN(rc);
3388         } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3389                    strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
3390                 /* default LMVEA */
3391                 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
3392                                                       th);
3393                 RETURN(rc);
3394         } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3395                    (!strcmp(name, XATTR_NAME_LOV) ||
3396                     !strncmp(name, XATTR_LUSTRE_LOV,
3397                              strlen(XATTR_LUSTRE_LOV)))) {
3398                 /* in case of lov EA swap, just set it
3399                  * if not, it is a replay so check striping match what we
3400                  * already have during req replay, declare_xattr_set()
3401                  * defines striping, then create() does the work */
3402                 if (fl & LU_XATTR_REPLACE) {
3403                         /* free stripes, then update disk */
3404                         lod_object_free_striping(env, lod_dt_obj(dt));
3405
3406                         rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3407                 } else if (dt_object_remote(dt)) {
3408                         /* This only happens during migration, see
3409                          * mdd_migrate_create(), in which Master MDT will
3410                          * create a remote target object, and only set
3411                          * (migrating) stripe EA on the remote object,
3412                          * and does not need creating each stripes. */
3413                         rc = lod_sub_xattr_set(env, next, buf, name,
3414                                                       fl, th);
3415                 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
3416                         /* delete component(s) */
3417                         LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
3418                         rc = lod_layout_del(env, dt, th);
3419                 } else {
3420                         /*
3421                          * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
3422                          * it's going to create create file with specified
3423                          * component(s), the striping must have not being
3424                          * cached in this case;
3425                          *
3426                          * Otherwise, it's going to add/change component(s) to
3427                          * an existing file, the striping must have been cached
3428                          * in this case.
3429                          */
3430                         LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
3431                                      !strcmp(name, XATTR_NAME_LOV),
3432                                 !lod_dt_obj(dt)->ldo_comp_cached));
3433
3434                         rc = lod_striped_create(env, dt, NULL, NULL, th);
3435                 }
3436                 RETURN(rc);
3437         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3438                 rc = lod_replace_parent_fid(env, dt, th, false);
3439
3440                 RETURN(rc);
3441         }
3442
3443         /* then all other xattr */
3444         rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3445
3446         RETURN(rc);
3447 }
3448
3449 /**
3450  * Implementation of dt_object_operations::do_declare_xattr_del.
3451  *
3452  * \see dt_object_operations::do_declare_xattr_del() in the API description
3453  * for details.
3454  */
3455 static int lod_declare_xattr_del(const struct lu_env *env,
3456                                  struct dt_object *dt, const char *name,
3457                                  struct thandle *th)
3458 {
3459         struct lod_object       *lo = lod_dt_obj(dt);
3460         int                     rc;
3461         int                     i;
3462         ENTRY;
3463
3464         rc = lod_sub_declare_xattr_del(env, dt_object_child(dt), name, th);
3465         if (rc != 0)
3466                 RETURN(rc);
3467
3468         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3469                 RETURN(0);
3470
3471         /* set xattr to each stripes, if needed */
3472         rc = lod_load_striping(env, lo);
3473         if (rc != 0)
3474                 RETURN(rc);
3475
3476         if (lo->ldo_dir_stripenr == 0)
3477                 RETURN(0);
3478
3479         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
3480                 LASSERT(lo->ldo_stripe[i]);
3481                 rc = lod_sub_declare_xattr_del(env, lo->ldo_stripe[i],
3482                                                name, th);
3483                 if (rc != 0)
3484                         break;
3485         }
3486
3487         RETURN(rc);
3488 }
3489
3490 /**
3491  * Implementation of dt_object_operations::do_xattr_del.
3492  *
3493  * If EA storing a regular striping is being deleted, then release
3494  * all the references to the stripe objects in core.
3495  *
3496  * \see dt_object_operations::do_xattr_del() in the API description for details.
3497  */
3498 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
3499                          const char *name, struct thandle *th)
3500 {
3501         struct dt_object        *next = dt_object_child(dt);
3502         struct lod_object       *lo = lod_dt_obj(dt);
3503         int                     rc;
3504         int                     i;
3505         ENTRY;
3506
3507         if (!strcmp(name, XATTR_NAME_LOV))
3508                 lod_object_free_striping(env, lod_dt_obj(dt));
3509
3510         rc = lod_sub_xattr_del(env, next, name, th);
3511         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3512                 RETURN(rc);
3513
3514         if (lo->ldo_dir_stripenr == 0)
3515                 RETURN(0);
3516
3517         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
3518                 LASSERT(lo->ldo_stripe[i]);
3519
3520                 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3521                 if (rc != 0)
3522                         break;
3523         }
3524
3525         RETURN(rc);
3526 }
3527
3528 /**
3529  * Implementation of dt_object_operations::do_xattr_list.
3530  *
3531  * \see dt_object_operations::do_xattr_list() in the API description
3532  * for details.
3533  */
3534 static int lod_xattr_list(const struct lu_env *env,
3535                           struct dt_object *dt, const struct lu_buf *buf)
3536 {
3537         return dt_xattr_list(env, dt_object_child(dt), buf);
3538 }
3539
3540 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
3541 {
3542         return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
3543 }
3544
3545
3546 /**
3547  * Get default striping.
3548  *
3549  * \param[in] env               execution environment
3550  * \param[in] lo                object
3551  * \param[out] lds              default striping
3552  *
3553  * \retval              0 on success
3554  * \retval              negative if failed
3555  */
3556 static int lod_get_default_lov_striping(const struct lu_env *env,
3557                                         struct lod_object *lo,
3558                                         struct lod_default_striping *lds)
3559 {
3560         struct lod_thread_info *info = lod_env_info(env);
3561         struct lov_user_md_v1 *v1 = NULL;
3562         struct lov_user_md_v3 *v3 = NULL;
3563         struct lov_comp_md_v1 *comp_v1 = NULL;
3564         __u16   comp_cnt;
3565         bool    composite;
3566         int     rc, i;
3567         ENTRY;
3568
3569         lds->lds_def_striping_set = 0;
3570
3571         rc = lod_get_lov_ea(env, lo);
3572         if (rc < 0)
3573                 RETURN(rc);
3574
3575         if (rc < (typeof(rc))sizeof(struct lov_user_md))
3576                 RETURN(0);
3577
3578         v1 = info->lti_ea_store;
3579         if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
3580                 lustre_swab_lov_user_md_v1(v1);
3581         } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
3582                 v3 = (struct lov_user_md_v3 *)v1;
3583                 lustre_swab_lov_user_md_v3(v3);
3584         } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3585                 comp_v1 = (struct lov_comp_md_v1 *)v1;
3586                 lustre_swab_lov_comp_md_v1(comp_v1);
3587         }
3588
3589         if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
3590             v1->lmm_magic != LOV_MAGIC_COMP_V1)
3591                 RETURN(-ENOTSUPP);
3592
3593         if (v1->lmm_magic == LOV_MAGIC_COMP_V1) {
3594                 comp_v1 = (struct lov_comp_md_v1 *)v1;
3595                 comp_cnt = comp_v1->lcm_entry_count;
3596                 if (comp_cnt == 0)
3597                         RETURN(-EINVAL);
3598                 composite = true;
3599         } else {
3600                 comp_cnt = 1;
3601                 composite = false;
3602         }
3603
3604         /* realloc default comp entries if necessary */
3605         rc = lod_def_striping_comp_resize(lds, comp_cnt);
3606         if (rc < 0)
3607                 RETURN(rc);
3608
3609         lds->lds_def_comp_cnt = comp_cnt;
3610         lds->lds_def_striping_is_composite = composite ? 1 : 0;
3611
3612         for (i = 0; i < comp_cnt; i++) {
3613                 struct lod_layout_component *lod_comp;
3614                 struct lu_extent *ext;
3615                 char *pool;
3616
3617                 lod_comp = &lds->lds_def_comp_entries[i];
3618                 /*
3619                  * reset lod_comp values, llc_stripes is always NULL in
3620                  * the default striping template, llc_pool will be reset
3621                  * later below.
3622                  */
3623                 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
3624
3625                 if (composite) {
3626                         v1 = (struct lov_user_md *)((char *)comp_v1 +
3627                                         comp_v1->lcm_entries[i].lcme_offset);
3628                         ext = &comp_v1->lcm_entries[i].lcme_extent;
3629                         lod_comp->llc_extent = *ext;
3630                 }
3631
3632                 if (v1->lmm_pattern != LOV_PATTERN_RAID0 &&
3633                     v1->lmm_pattern != 0) {
3634                         lod_free_def_comp_entries(lds);
3635                         RETURN(-EINVAL);
3636                 }
3637
3638                 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d "
3639                        "stripe_offset=%d\n",
3640                        PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
3641                        (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
3642                        (int)v1->lmm_stripe_offset);
3643
3644                 lod_comp->llc_stripenr = v1->lmm_stripe_count;
3645                 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
3646                 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
3647
3648                 pool = NULL;
3649                 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
3650                         /* XXX: sanity check here */
3651                         v3 = (struct lov_user_md_v3 *) v1;
3652                         if (v3->lmm_pool_name[0] != '\0')
3653                                 pool = v3->lmm_pool_name;
3654                 }
3655                 lod_set_def_pool(lds, i, pool);
3656         }
3657
3658         lds->lds_def_striping_set = 1;
3659         RETURN(rc);
3660 }
3661
3662 /**
3663  * Get default directory striping.
3664  *
3665  * \param[in] env               execution environment
3666  * \param[in] lo                object
3667  * \param[out] lds              default striping
3668  *
3669  * \retval              0 on success
3670  * \retval              negative if failed
3671  */
3672 static int lod_get_default_lmv_striping(const struct lu_env *env,
3673                                         struct lod_object *lo,
3674                                         struct lod_default_striping *lds)
3675 {
3676         struct lod_thread_info  *info = lod_env_info(env);
3677         struct lmv_user_md_v1   *v1 = NULL;
3678         int                      rc;
3679         ENTRY;
3680
3681         lds->lds_dir_def_striping_set = 0;
3682         rc = lod_get_default_lmv_ea(env, lo);
3683         if (rc < 0)
3684                 RETURN(rc);
3685
3686         if (rc < (typeof(rc))sizeof(struct lmv_user_md))
3687                 RETURN(0);
3688
3689         v1 = info->lti_ea_store;
3690
3691         lds->lds_dir_def_stripenr = le32_to_cpu(v1->lum_stripe_count);
3692         lds->lds_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
3693         lds->lds_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
3694         lds->lds_dir_def_striping_set = 1;
3695
3696         RETURN(0);
3697 }
3698
3699 /**
3700  * Get default striping in the object.
3701  *
3702  * Get object default striping and default directory striping.
3703  *
3704  * \param[in] env               execution environment
3705  * \param[in] lo                object
3706  * \param[out] lds              default striping
3707  *
3708  * \retval              0 on success
3709  * \retval              negative if failed
3710  */
3711 static int lod_get_default_striping(const struct lu_env *env,
3712                                     struct lod_object *lo,
3713                                     struct lod_default_striping *lds)
3714 {
3715         int rc, rc1;
3716
3717         rc = lod_get_default_lov_striping(env, lo, lds);
3718         rc1 = lod_get_default_lmv_striping(env, lo, lds);
3719         if (rc == 0 && rc1 < 0)
3720                 rc = rc1;
3721
3722         return rc;
3723 }
3724
3725 /**
3726  * Apply default striping on object.
3727  *
3728  * If object striping pattern is not set, set to the one in default striping.
3729  * The default striping is from parent or fs.
3730  *
3731  * \param[in] lo                new object
3732  * \param[in] lds               default striping
3733  * \param[in] mode              new object's mode
3734  */
3735 static void lod_striping_from_default(struct lod_object *lo,
3736                                       const struct lod_default_striping *lds,
3737                                       umode_t mode)
3738 {
3739         struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
3740         struct lov_desc *desc = &d->lod_desc;
3741         int i, rc;
3742
3743         if (lds->lds_def_striping_set && S_ISREG(mode)) {
3744                 rc = lod_alloc_comp_entries(lo, lds->lds_def_comp_cnt);
3745                 if (rc != 0)
3746                         return;
3747
3748                 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
3749
3750                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
3751                         struct lod_layout_component *obj_comp =
3752                                                 &lo->ldo_comp_entries[i];
3753                         struct lod_layout_component *def_comp =
3754                                                 &lds->lds_def_comp_entries[i];
3755
3756                         CDEBUG(D_LAYOUT, "Inherite from default: size:%hu "
3757                                "nr:%u offset:%u %s\n",
3758                                def_comp->llc_stripe_size,
3759                                def_comp->llc_stripenr,
3760                                def_comp->llc_stripe_offset,
3761                                def_comp->llc_pool ?: "");
3762
3763                         *obj_comp = *def_comp;
3764                         if (def_comp->llc_pool != NULL) {
3765                                 /* pointer was copied from def_comp */
3766                                 obj_comp->llc_pool = NULL;
3767                                 lod_obj_set_pool(lo, i, def_comp->llc_pool);
3768                         }
3769
3770                         /*
3771                          * Don't initialize these fields for plain layout
3772                          * (v1/v3) here, they are inherited in the order of
3773                          * 'parent' -> 'fs default (root)' -> 'global default
3774                          * values for stripe_count & stripe_size'.
3775                          *
3776                          * see lod_ah_init().
3777                          */
3778                         if (!lo->ldo_is_composite)
3779                                 continue;
3780
3781                         if (obj_comp->llc_stripenr <= 0)
3782                                 obj_comp->llc_stripenr =
3783                                         desc->ld_default_stripe_count;
3784                         if (obj_comp->llc_stripe_size <= 0)
3785                                 obj_comp->llc_stripe_size =
3786                                         desc->ld_default_stripe_size;
3787                 }
3788         } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
3789                 if (lo->ldo_dir_stripenr == 0)
3790                         lo->ldo_dir_stripenr = lds->lds_dir_def_stripenr;
3791                 if (lo->ldo_dir_stripe_offset == -1)
3792                         lo->ldo_dir_stripe_offset =
3793                                 lds->lds_dir_def_stripe_offset;
3794                 if (lo->ldo_dir_hash_type == 0)
3795                         lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
3796
3797                 CDEBUG(D_LAYOUT, "striping from default dir: nr:%hu, "
3798                        "offset:%u, hash_type:%u\n",
3799                        lo->ldo_dir_stripenr, lo->ldo_dir_stripe_offset,
3800                        lo->ldo_dir_hash_type);
3801         }
3802 }
3803
3804 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root)
3805 {
3806         struct lod_layout_component *lod_comp;
3807
3808         if (lo->ldo_comp_cnt == 0)
3809                 return true;
3810
3811         if (lo->ldo_is_composite)
3812                 return false;
3813
3814         lod_comp = &lo->ldo_comp_entries[0];
3815
3816         if (lod_comp->llc_stripenr <= 0 ||
3817             lod_comp->llc_stripe_size <= 0)
3818                 return true;
3819
3820         if (from_root && (lod_comp->llc_pool == NULL ||
3821                           lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
3822                 return true;
3823
3824         return false;
3825 }
3826
3827 /**
3828  * Implementation of dt_object_operations::do_ah_init.
3829  *
3830  * This method is used to make a decision on the striping configuration for the
3831  * object being created. It can be taken from the \a parent object if it exists,
3832  * or filesystem's default. The resulting configuration (number of stripes,
3833  * stripe size/offset, pool name, etc) is stored in the object itself and will
3834  * be used by the methods like ->doo_declare_create().
3835  *
3836  * \see dt_object_operations::do_ah_init() in the API description for details.
3837  */
3838 static void lod_ah_init(const struct lu_env *env,
3839                         struct dt_allocation_hint *ah,
3840                         struct dt_object *parent,
3841                         struct dt_object *child,
3842                         umode_t child_mode)
3843 {
3844         struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
3845         struct lod_thread_info *info = lod_env_info(env);
3846         struct lod_default_striping *lds = &info->lti_def_striping;
3847         struct dt_object *nextp = NULL;
3848         struct dt_object *nextc;
3849         struct lod_object *lp = NULL;
3850         struct lod_object *lc;
3851         struct lov_desc *desc;
3852         struct lod_layout_component *lod_comp;
3853         int rc;
3854         ENTRY;
3855
3856         LASSERT(child);
3857
3858         if (likely(parent)) {
3859                 nextp = dt_object_child(parent);
3860                 lp = lod_dt_obj(parent);
3861         }
3862
3863         nextc = dt_object_child(child);
3864         lc = lod_dt_obj(child);
3865
3866         LASSERT(!lod_obj_is_striped(child));
3867         /* default layout template may have been set on the regular file
3868          * when this is called from mdd_create_data() */
3869         if (S_ISREG(child_mode))
3870                 lod_free_comp_entries(lc);
3871
3872         if (!dt_object_exists(nextc))
3873                 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
3874
3875         if (S_ISDIR(child_mode)) {
3876                 /* other default values are 0 */
3877                 lc->ldo_dir_stripe_offset = -1;
3878
3879                 /* get default striping from parent object */
3880                 if (likely(lp != NULL))
3881                         lod_get_default_striping(env, lp, lds);
3882
3883                 /* set child default striping info, default value is NULL */
3884                 if (lds->lds_def_striping_set || lds->lds_dir_def_striping_set)
3885                         lc->ldo_def_striping = lds;
3886
3887                 /* It should always honour the specified stripes */
3888                 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
3889                     lod_verify_md_striping(d, ah->dah_eadata) == 0) {
3890                         const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
3891
3892                         lc->ldo_dir_stripenr =
3893                                 le32_to_cpu(lum1->lum_stripe_count);
3894                         lc->ldo_dir_stripe_offset =
3895                                 le32_to_cpu(lum1->lum_stripe_offset);
3896                         lc->ldo_dir_hash_type =
3897                                 le32_to_cpu(lum1->lum_hash_type);
3898                         CDEBUG(D_INFO, "set dir stripe: count %hu, offset %d, "
3899                                 "hash_type %u\n",
3900                                 lc->ldo_dir_stripenr,
3901                                 (int)lc->ldo_dir_stripe_offset,
3902                                 lc->ldo_dir_hash_type);
3903                 } else {
3904                         /* transfer defaults LMV to new directory */
3905                         lod_striping_from_default(lc, lds, child_mode);
3906                 }
3907
3908                 /* shrink the stripe_count to the avaible MDT count */
3909                 if (lc->ldo_dir_stripenr > d->lod_remote_mdt_count + 1 &&
3910                     !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))
3911                         lc->ldo_dir_stripenr = d->lod_remote_mdt_count + 1;
3912
3913                 /* Directory will be striped only if stripe_count > 1, if
3914                  * stripe_count == 1, let's reset stripenr = 0 to avoid
3915                  * create single master stripe and also help to unify the
3916                  * stripe handling of directories and files */
3917                 if (lc->ldo_dir_stripenr == 1)
3918                         lc->ldo_dir_stripenr = 0;
3919
3920                 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
3921                        lc->ldo_dir_stripenr, (int)lc->ldo_dir_stripe_offset,
3922                        lc->ldo_dir_hash_type);
3923
3924                 RETURN_EXIT;
3925         }
3926
3927         /* child object regular file*/
3928
3929         if (!lod_object_will_be_striped(S_ISREG(child_mode),
3930                                         lu_object_fid(&child->do_lu)))
3931                 RETURN_EXIT;
3932
3933         /* If object is going to be striped over OSTs, transfer default
3934          * striping information to the child, so that we can use it
3935          * during declaration and creation.
3936          *
3937          * Try from the parent first.
3938          */
3939         if (likely(lp != NULL)) {
3940                 rc = lod_get_default_lov_striping(env, lp, lds);
3941                 if (rc == 0)
3942                         lod_striping_from_default(lc, lds, child_mode);
3943         }
3944
3945         /* Initialize lod_device::lod_md_root object reference */
3946         if (d->lod_md_root == NULL) {
3947                 struct dt_object *root;
3948                 struct lod_object *lroot;
3949
3950                 lu_root_fid(&info->lti_fid);
3951                 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
3952                 if (!IS_ERR(root)) {
3953                         lroot = lod_dt_obj(root);
3954
3955                         spin_lock(&d->lod_lock);
3956                         if (d->lod_md_root != NULL)
3957                                 dt_object_put(env, &d->lod_md_root->ldo_obj);
3958                         d->lod_md_root = lroot;
3959                         spin_unlock(&d->lod_lock);
3960                 }
3961         }
3962
3963         /* try inherit layout from the root object (fs default) when:
3964          *  - parent does not have default layout; or
3965          *  - parent has plain(v1/v3) default layout, and some attributes
3966          *    are not specified in the default layout;
3967          */
3968         if (d->lod_md_root != NULL && lod_need_inherit_more(lc, true)) {
3969                 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds);
3970                 if (rc)
3971                         goto out;
3972                 if (lc->ldo_comp_cnt == 0) {
3973                         lod_striping_from_default(lc, lds, child_mode);
3974                 } else if (!lds->lds_def_striping_is_composite) {
3975                         struct lod_layout_component *def_comp;
3976
3977                         LASSERT(!lc->ldo_is_composite);
3978                         lod_comp = &lc->ldo_comp_entries[0];
3979                         def_comp = &lds->lds_def_comp_entries[0];
3980
3981                         if (lod_comp->llc_stripenr <= 0)
3982                                 lod_comp->llc_stripenr = def_comp->llc_stripenr;
3983                         if (lod_comp->llc_stripe_size <= 0)
3984                                 lod_comp->llc_stripe_size =
3985                                         def_comp->llc_stripe_size;
3986                         if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
3987                                 lod_comp->llc_stripe_offset =
3988                                         def_comp->llc_stripe_offset;
3989                         if (lod_comp->llc_pool == NULL)
3990                                 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
3991                 }
3992         }
3993 out:
3994         /*
3995          * fs default striping may not be explicitly set, or historically set
3996          * in config log, use them.
3997          */
3998         if (lod_need_inherit_more(lc, false)) {
3999
4000                 if (lc->ldo_comp_cnt == 0) {
4001                         rc = lod_alloc_comp_entries(lc, 1);
4002                         if (rc)
4003                                 /* fail to allocate memory, will create a
4004                                  * non-striped file. */
4005                                 RETURN_EXIT;
4006                         lc->ldo_is_composite = 0;
4007                         lod_comp = &lc->ldo_comp_entries[0];
4008                         lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4009                 }
4010                 LASSERT(!lc->ldo_is_composite);
4011                 lod_comp = &lc->ldo_comp_entries[0];
4012                 desc = &d->lod_desc;
4013                 if (lod_comp->llc_stripenr <= 0)
4014                         lod_comp->llc_stripenr = desc->ld_default_stripe_count;
4015                 if (lod_comp->llc_stripe_size <= 0)
4016                         lod_comp->llc_stripe_size =
4017                                 desc->ld_default_stripe_size;
4018         }
4019
4020         EXIT;
4021 }
4022
4023 #define ll_do_div64(aaa,bbb)    do_div((aaa), (bbb))
4024 /**
4025  * Size initialization on late striping.
4026  *
4027  * Propagate the size of a truncated object to a deferred striping.
4028  * This function handles a special case when truncate was done on a
4029  * non-striped object and now while the striping is being created
4030  * we can't lose that size, so we have to propagate it to the stripes
4031  * being created.
4032  *
4033  * \param[in] env       execution environment
4034  * \param[in] dt        object
4035  * \param[in] th        transaction handle
4036  *
4037  * \retval              0 on success
4038  * \retval              negative if failed
4039  */
4040 static int lod_declare_init_size(const struct lu_env *env,
4041                                  struct dt_object *dt, struct thandle *th)
4042 {
4043         struct dt_object        *next = dt_object_child(dt);
4044         struct lod_object       *lo = lod_dt_obj(dt);
4045         struct dt_object        **objects = NULL;
4046         struct lu_attr  *attr = &lod_env_info(env)->lti_attr;
4047         uint64_t        size, offs;
4048         int     i, rc, stripe, stripenr = 0, stripe_size = 0;
4049         ENTRY;
4050
4051         if (!lod_obj_is_striped(dt))
4052                 RETURN(0);
4053
4054         rc = dt_attr_get(env, next, attr);
4055         LASSERT(attr->la_valid & LA_SIZE);
4056         if (rc)
4057                 RETURN(rc);
4058
4059         size = attr->la_size;
4060         if (size == 0)
4061                 RETURN(0);
4062
4063         for (i = 0; i < lo->ldo_comp_cnt; i++) {
4064                 struct lod_layout_component *lod_comp;
4065                 struct lu_extent *extent;
4066
4067                 lod_comp = &lo->ldo_comp_entries[i];
4068
4069                 if (lod_comp->llc_stripe == NULL)
4070                         continue;
4071
4072                 extent = &lod_comp->llc_extent;
4073                 CDEBUG(D_INFO, "%lld [%lld, %lld)\n",
4074                        size, extent->e_start, extent->e_end);
4075                 if (!lo->ldo_is_composite ||
4076                     (size >= extent->e_start && size < extent->e_end)) {
4077                         objects = lod_comp->llc_stripe;
4078                         stripenr = lod_comp->llc_stripenr;
4079                         stripe_size = lod_comp->llc_stripe_size;
4080                         break;
4081                 }
4082         }
4083
4084         if (stripenr == 0)
4085                 RETURN(0);
4086
4087         LASSERT(objects != NULL && stripe_size != 0);
4088
4089         /* ll_do_div64(a, b) returns a % b, and a = a / b */
4090         ll_do_div64(size, (__u64)stripe_size);
4091         stripe = ll_do_div64(size, (__u64)stripenr);
4092         LASSERT(objects[stripe] != NULL);
4093
4094         size = size * stripe_size;
4095         offs = attr->la_size;
4096         size += ll_do_div64(offs, stripe_size);
4097
4098         attr->la_valid = LA_SIZE;
4099         attr->la_size = size;
4100
4101         rc = lod_sub_declare_attr_set(env, objects[stripe], attr, th);
4102
4103         RETURN(rc);
4104 }
4105
4106 /**
4107  * Declare creation of striped object.
4108  *
4109  * The function declares creation stripes for a regular object. The function
4110  * also declares whether the stripes will be created with non-zero size if
4111  * previously size was set non-zero on the master object. If object \a dt is
4112  * not local, then only fully defined striping can be applied in \a lovea.
4113  * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
4114  * for the details.
4115  *
4116  * \param[in] env       execution environment
4117  * \param[in] dt        object
4118  * \param[in] attr      attributes the stripes will be created with
4119  * \param[in] lovea     a buffer containing striping description
4120  * \param[in] th        transaction handle
4121  *
4122  * \retval              0 on success
4123  * \retval              negative if failed
4124  */
4125 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
4126                                struct lu_attr *attr,
4127                                const struct lu_buf *lovea, struct thandle *th)
4128 {
4129         struct lod_thread_info  *info = lod_env_info(env);
4130         struct dt_object        *next = dt_object_child(dt);
4131         struct lod_object       *lo = lod_dt_obj(dt);
4132         int                      rc;
4133         ENTRY;
4134
4135         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
4136                 GOTO(out, rc = -ENOMEM);
4137
4138         if (!dt_object_remote(next)) {
4139                 /* choose OST and generate appropriate objects */
4140                 rc = lod_prepare_create(env, lo, attr, lovea, th);
4141                 if (rc)
4142                         GOTO(out, rc);
4143
4144                 /*
4145                  * declare storage for striping data
4146                  */
4147                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
4148         } else {
4149                 /* LOD can not choose OST objects for remote objects, i.e.
4150                  * stripes must be ready before that. Right now, it can only
4151                  * happen during migrate, i.e. migrate process needs to create
4152                  * remote regular file (mdd_migrate_create), then the migrate
4153                  * process will provide stripeEA. */
4154                 LASSERT(lovea != NULL);
4155                 info->lti_buf = *lovea;
4156         }
4157
4158         rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
4159                                        XATTR_NAME_LOV, 0, th);
4160         if (rc)
4161                 GOTO(out, rc);
4162
4163         /*
4164          * if striping is created with local object's size > 0,
4165          * we have to propagate this size to specific object
4166          * the case is possible only when local object was created previously
4167          */
4168         if (dt_object_exists(next))
4169                 rc = lod_declare_init_size(env, dt, th);
4170
4171 out:
4172         /* failed to create striping or to set initial size, let's reset
4173          * config so that others don't get confused */
4174         if (rc)
4175                 lod_object_free_striping(env, lo);
4176
4177         RETURN(rc);
4178 }
4179
4180 /**
4181  * Implementation of dt_object_operations::do_declare_create.
4182  *
4183  * The method declares creation of a new object. If the object will be striped,
4184  * then helper functions are called to find FIDs for the stripes, declare
4185  * creation of the stripes and declare initialization of the striping
4186  * information to be stored in the master object.
4187  *
4188  * \see dt_object_operations::do_declare_create() in the API description
4189  * for details.
4190  */
4191 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
4192                               struct lu_attr *attr,
4193                               struct dt_allocation_hint *hint,
4194                               struct dt_object_format *dof, struct thandle *th)
4195 {
4196         struct dt_object   *next = dt_object_child(dt);
4197         struct lod_object  *lo = lod_dt_obj(dt);
4198         int                 rc;
4199         ENTRY;
4200
4201         LASSERT(dof);
4202         LASSERT(attr);
4203         LASSERT(th);
4204
4205         /*
4206          * first of all, we declare creation of local object
4207          */
4208         rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
4209         if (rc != 0)
4210                 GOTO(out, rc);
4211
4212         /*
4213          * it's lod_ah_init() that has decided the object will be striped
4214          */
4215         if (dof->dof_type == DFT_REGULAR) {
4216                 /* callers don't want stripes */
4217                 /* XXX: all tricky interactions with ->ah_make_hint() decided
4218                  * to use striping, then ->declare_create() behaving differently
4219                  * should be cleaned */
4220                 if (dof->u.dof_reg.striped != 0)
4221                         rc = lod_declare_striped_create(env, dt, attr,
4222                                                         NULL, th);
4223         } else if (dof->dof_type == DFT_DIR) {
4224                 struct seq_server_site *ss;
4225
4226                 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
4227
4228                 /* If the parent has default stripeEA, and client
4229                  * did not find it before sending create request,
4230                  * then MDT will return -EREMOTE, and client will
4231                  * retrieve the default stripeEA and re-create the
4232                  * sub directory.
4233                  *
4234                  * Note: if dah_eadata != NULL, it means creating the
4235                  * striped directory with specified stripeEA, then it
4236                  * should ignore the default stripeEA */
4237                 if (hint != NULL && hint->dah_eadata == NULL) {
4238                         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
4239                                 GOTO(out, rc = -EREMOTE);
4240
4241                         if (lo->ldo_dir_stripe_offset == -1) {
4242                                 /* child and parent should be in the same MDT */
4243                                 if (hint->dah_parent != NULL &&
4244                                     dt_object_remote(hint->dah_parent))
4245                                         GOTO(out, rc = -EREMOTE);
4246                         } else if (lo->ldo_dir_stripe_offset !=
4247                                    ss->ss_node_id) {
4248                                 struct lod_device *lod;
4249                                 struct lod_tgt_descs *ltd;
4250                                 struct lod_tgt_desc *tgt = NULL;
4251                                 bool found_mdt = false;
4252                                 int i;
4253
4254                                 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4255                                 ltd = &lod->lod_mdt_descs;
4256                                 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
4257                                         tgt = LTD_TGT(ltd, i);
4258                                         if (tgt->ltd_index ==
4259                                                 lo->ldo_dir_stripe_offset) {
4260                                                 found_mdt = true;
4261                                                 break;
4262                                         }
4263                                 }
4264
4265                                 /* If the MDT indicated by stripe_offset can be
4266                                  * found, then tell client to resend the create
4267                                  * request to the correct MDT, otherwise return
4268                                  * error to client */
4269                                 if (found_mdt)
4270                                         GOTO(out, rc = -EREMOTE);
4271                                 else
4272                                         GOTO(out, rc = -EINVAL);
4273                         }
4274                 }
4275
4276                 rc = lod_declare_dir_striping_create(env, dt, attr, dof, th);
4277         }
4278 out:
4279         /* failed to create striping or to set initial size, let's reset
4280          * config so that others don't get confused */
4281         if (rc)
4282                 lod_object_free_striping(env, lo);
4283         RETURN(rc);
4284 }
4285
4286 /**
4287  * Creation of a striped regular object.
4288  *
4289  * The function is called to create the stripe objects for a regular
4290  * striped file. This can happen at the initial object creation or
4291  * when the caller asks LOD to do so using ->do_xattr_set() method
4292  * (so called late striping). Notice all the information are already
4293  * prepared in the form of the list of objects (ldo_stripe field).
4294  * This is done during declare phase.
4295  *
4296  * \param[in] env       execution environment
4297  * \param[in] dt        object
4298  * \param[in] attr      attributes the stripes will be created with
4299  * \param[in] dof       format of stripes (see OSD API description)
4300  * \param[in] th        transaction handle
4301  *
4302  * \retval              0 on success
4303  * \retval              negative if failed
4304  */
4305 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
4306                        struct lu_attr *attr, struct dt_object_format *dof,
4307                        struct thandle *th)
4308 {
4309         struct lod_layout_component     *lod_comp;
4310         struct lod_object       *lo = lod_dt_obj(dt);
4311         int     rc = 0, i, j;
4312         ENTRY;
4313
4314         LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
4315
4316         /* create all underlying objects */
4317         for (i = 0; i < lo->ldo_comp_cnt; i++) {
4318                 lod_comp = &lo->ldo_comp_entries[i];
4319
4320                 if (lod_comp_inited(lod_comp))
4321                         continue;
4322
4323                 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
4324                         lod_comp_set_init(lod_comp);
4325
4326                 if (lod_comp->llc_stripe == NULL)
4327                         continue;
4328
4329                 LASSERT(lod_comp->llc_stripenr);
4330                 for (j = 0; j < lod_comp->llc_stripenr; j++) {
4331                         struct dt_object *object = lod_comp->llc_stripe[j];
4332                         LASSERT(object != NULL);
4333                         rc = lod_sub_create(env, object, attr, NULL, dof, th);
4334                         if (rc)
4335                                 break;
4336                 }
4337                 lod_comp_set_init(lod_comp);
4338         }
4339
4340         if (rc == 0)
4341                 rc = lod_generate_and_set_lovea(env, lo, th);
4342
4343         if (rc == 0)
4344                 lo->ldo_comp_cached = 1;
4345         else
4346                 lod_object_free_striping(env, lo);
4347
4348         RETURN(rc);
4349 }
4350
4351 /**
4352  * Implementation of dt_object_operations::do_create.
4353  *
4354  * If any of preceeding methods (like ->do_declare_create(),
4355  * ->do_ah_init(), etc) chose to create a striped object,
4356  * then this method will create the master and the stripes.
4357  *
4358  * \see dt_object_operations::do_create() in the API description for details.
4359  */
4360 static int lod_create(const struct lu_env *env, struct dt_object *dt,
4361                       struct lu_attr *attr, struct dt_allocation_hint *hint,
4362                       struct dt_object_format *dof, struct thandle *th)
4363 {
4364         int                 rc;
4365         ENTRY;
4366
4367         /* create local object */
4368         rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
4369         if (rc != 0)
4370                 RETURN(rc);
4371
4372         if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4373             lod_obj_is_striped(dt) && dof->u.dof_reg.striped != 0) {
4374                 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
4375                 rc = lod_striped_create(env, dt, attr, dof, th);
4376         }
4377
4378         RETURN(rc);
4379 }
4380
4381 static inline int
4382 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
4383                           struct dt_object *dt, struct thandle *th,
4384                           int stripe_idx, struct lod_obj_stripe_cb_data *data)
4385 {
4386         if (data->locd_declare)
4387                 return lod_sub_declare_destroy(env, dt, th);
4388         else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4389                  stripe_idx == cfs_fail_val)
4390                 return lod_sub_destroy(env, dt, th);
4391         else
4392                 return 0;
4393 }
4394
4395 /**
4396  * Implementation of dt_object_operations::do_declare_destroy.
4397  *
4398  * If the object is a striped directory, then the function declares reference
4399  * removal from the master object (this is an index) to the stripes and declares
4400  * destroy of all the stripes. In all the cases, it declares an intention to
4401  * destroy the object itself.
4402  *
4403  * \see dt_object_operations::do_declare_destroy() in the API description
4404  * for details.
4405  */
4406 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
4407                                struct thandle *th)
4408 {
4409         struct dt_object   *next = dt_object_child(dt);
4410         struct lod_object  *lo = lod_dt_obj(dt);
4411         struct lod_thread_info *info = lod_env_info(env);
4412         char               *stripe_name = info->lti_key;
4413         int                 rc, i;
4414         ENTRY;
4415
4416         /*
4417          * load striping information, notice we don't do this when object
4418          * is being initialized as we don't need this information till
4419          * few specific cases like destroy, chown
4420          */
4421         rc = lod_load_striping(env, lo);
4422         if (rc)
4423                 RETURN(rc);
4424
4425         /* declare destroy for all underlying objects */
4426         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4427                 rc = next->do_ops->do_index_try(env, next,
4428                                                 &dt_directory_features);
4429                 if (rc != 0)
4430                         RETURN(rc);
4431
4432                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4433                         rc = lod_sub_declare_ref_del(env, next, th);
4434                         if (rc != 0)
4435                                 RETURN(rc);
4436
4437                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4438                                 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4439                                 i);
4440                         rc = lod_sub_declare_delete(env, next,
4441                                         (const struct dt_key *)stripe_name, th);
4442                         if (rc != 0)
4443                                 RETURN(rc);
4444                 }
4445         }
4446
4447         /*
4448          * we declare destroy for the local object
4449          */
4450         rc = lod_sub_declare_destroy(env, next, th);
4451         if (rc)
4452                 RETURN(rc);
4453
4454         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4455             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4456                 RETURN(0);
4457
4458         if (!lod_obj_is_striped(dt))
4459                 RETURN(0);
4460
4461         /* declare destroy all striped objects */
4462         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4463                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4464                         if (lo->ldo_stripe[i] == NULL)
4465                                 continue;
4466
4467                         rc = lod_sub_declare_ref_del(env, lo->ldo_stripe[i],
4468                                                      th);
4469
4470                         rc = lod_sub_declare_destroy(env, lo->ldo_stripe[i],
4471                                                      th);
4472                         if (rc != 0)
4473                                 break;
4474                 }
4475         } else {
4476                 struct lod_obj_stripe_cb_data data;
4477
4478                 data.locd_declare = true;
4479                 rc = lod_obj_for_each_stripe(env, lo, th,
4480                                 lod_obj_stripe_destroy_cb, &data);
4481         }
4482
4483         RETURN(rc);
4484 }
4485
4486 /**
4487  * Implementation of dt_object_operations::do_destroy.
4488  *
4489  * If the object is a striped directory, then the function removes references
4490  * from the master object (this is an index) to the stripes and destroys all
4491  * the stripes. In all the cases, the function destroys the object itself.
4492  *
4493  * \see dt_object_operations::do_destroy() in the API description for details.
4494  */
4495 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
4496                        struct thandle *th)
4497 {
4498         struct dt_object  *next = dt_object_child(dt);
4499         struct lod_object *lo = lod_dt_obj(dt);
4500         struct lod_thread_info *info = lod_env_info(env);
4501         char               *stripe_name = info->lti_key;
4502         unsigned int       i;
4503         int                rc;
4504         ENTRY;
4505
4506         /* destroy sub-stripe of master object */
4507         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4508                 rc = next->do_ops->do_index_try(env, next,
4509                                                 &dt_directory_features);
4510                 if (rc != 0)
4511                         RETURN(rc);
4512
4513                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4514                         rc = lod_sub_ref_del(env, next, th);
4515                         if (rc != 0)
4516                                 RETURN(rc);
4517
4518                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4519                                 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4520                                 i);
4521
4522                         CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
4523                                PFID(lu_object_fid(&dt->do_lu)), stripe_name,
4524                                PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
4525
4526                         rc = lod_sub_delete(env, next,
4527                                        (const struct dt_key *)stripe_name, th);
4528                         if (rc != 0)
4529                                 RETURN(rc);
4530                 }
4531         }
4532
4533         rc = lod_sub_destroy(env, next, th);
4534         if (rc != 0)
4535                 RETURN(rc);
4536
4537         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4538             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4539                 RETURN(0);
4540
4541         if (!lod_obj_is_striped(dt))
4542                 RETURN(0);
4543
4544         /* destroy all striped objects */
4545         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4546                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4547                         if (lo->ldo_stripe[i] == NULL)
4548                                 continue;
4549                         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4550                             i == cfs_fail_val) {
4551                                 dt_write_lock(env, lo->ldo_stripe[i],
4552                                               MOR_TGT_CHILD);
4553                                 rc = lod_sub_ref_del(env, lo->ldo_stripe[i],
4554                                                      th);
4555                                 dt_write_unlock(env, lo->ldo_stripe[i]);
4556                                 if (rc != 0)
4557                                         break;
4558
4559                                 rc = lod_sub_destroy(env, lo->ldo_stripe[i],
4560                                                      th);
4561                                 if (rc != 0)
4562                                         break;
4563                         }
4564                 }
4565         } else {
4566                 struct lod_obj_stripe_cb_data data;
4567
4568                 data.locd_declare = false;
4569                 rc = lod_obj_for_each_stripe(env, lo, th,
4570                                 lod_obj_stripe_destroy_cb, &data);
4571         }
4572
4573         RETURN(rc);
4574 }
4575
4576 /**
4577  * Implementation of dt_object_operations::do_declare_ref_add.
4578  *
4579  * \see dt_object_operations::do_declare_ref_add() in the API description
4580  * for details.
4581  */
4582 static int lod_declare_ref_add(const struct lu_env *env,
4583                                struct dt_object *dt, struct thandle *th)
4584 {
4585         return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
4586 }
4587
4588 /**
4589  * Implementation of dt_object_operations::do_ref_add.
4590  *
4591  * \see dt_object_operations::do_ref_add() in the API description for details.
4592  */
4593 static int lod_ref_add(const struct lu_env *env,
4594                        struct dt_object *dt, struct thandle *th)
4595 {
4596         return lod_sub_ref_add(env, dt_object_child(dt), th);
4597 }
4598
4599 /**
4600  * Implementation of dt_object_operations::do_declare_ref_del.
4601  *
4602  * \see dt_object_operations::do_declare_ref_del() in the API description
4603  * for details.
4604  */
4605 static int lod_declare_ref_del(const struct lu_env *env,
4606                                struct dt_object *dt, struct thandle *th)
4607 {
4608         return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
4609 }
4610
4611 /**
4612  * Implementation of dt_object_operations::do_ref_del
4613  *
4614  * \see dt_object_operations::do_ref_del() in the API description for details.
4615  */
4616 static int lod_ref_del(const struct lu_env *env,
4617                        struct dt_object *dt, struct thandle *th)
4618 {
4619         return lod_sub_ref_del(env, dt_object_child(dt), th);
4620 }
4621
4622 /**
4623  * Implementation of dt_object_operations::do_object_sync.
4624  *
4625  * \see dt_object_operations::do_object_sync() in the API description
4626  * for details.
4627  */
4628 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
4629                            __u64 start, __u64 end)
4630 {
4631         return dt_object_sync(env, dt_object_child(dt), start, end);
4632 }
4633
4634 /**
4635  * Release LDLM locks on the stripes of a striped directory.
4636  *
4637  * Iterates over all the locks taken on the stripe objects and
4638  * cancel them.
4639  *
4640  * \param[in] env       execution environment
4641  * \param[in] dt        striped object
4642  * \param[in] einfo     lock description
4643  * \param[in] policy    data describing requested lock
4644  *
4645  * \retval              0 on success
4646  * \retval              negative if failed
4647  */
4648 static int lod_object_unlock_internal(const struct lu_env *env,
4649                                       struct dt_object *dt,
4650                                       struct ldlm_enqueue_info *einfo,
4651                                       union ldlm_policy_data *policy)
4652 {
4653         struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
4654         int                     rc = 0;
4655         int                     i;
4656         ENTRY;
4657
4658         if (slave_locks == NULL)
4659                 RETURN(0);
4660
4661         for (i = 1; i < slave_locks->count; i++) {
4662                 if (lustre_handle_is_used(&slave_locks->handles[i]))
4663                         ldlm_lock_decref_and_cancel(&slave_locks->handles[i],
4664                                                     einfo->ei_mode);
4665         }
4666
4667         RETURN(rc);
4668 }
4669
4670 /**
4671  * Implementation of dt_object_operations::do_object_unlock.
4672  *
4673  * Used to release LDLM lock(s).
4674  *
4675  * \see dt_object_operations::do_object_unlock() in the API description
4676  * for details.
4677  */
4678 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
4679                              struct ldlm_enqueue_info *einfo,
4680                              union ldlm_policy_data *policy)
4681 {
4682         struct lod_object *lo = lod_dt_obj(dt);
4683         struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
4684         int slave_locks_size;
4685         int i;
4686         ENTRY;
4687
4688         if (slave_locks == NULL)
4689                 RETURN(0);
4690
4691         LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
4692         LASSERT(lo->ldo_dir_stripenr > 1);
4693         /* Note: for remote lock for single stripe dir, MDT will cancel
4694          * the lock by lockh directly */
4695         LASSERT(!dt_object_remote(dt_object_child(dt)));
4696
4697         /* locks were unlocked in MDT layer */
4698         for (i = 1; i < slave_locks->count; i++) {
4699                 LASSERT(!lustre_handle_is_used(&slave_locks->handles[i]));
4700                 dt_invalidate(env, lo->ldo_stripe[i]);
4701         }
4702
4703         slave_locks_size = sizeof(*slave_locks) + slave_locks->count *
4704                            sizeof(slave_locks->handles[0]);
4705         OBD_FREE(slave_locks, slave_locks_size);
4706         einfo->ei_cbdata = NULL;
4707
4708         RETURN(0);
4709 }
4710
4711 /**
4712  * Implementation of dt_object_operations::do_object_lock.
4713  *
4714  * Used to get LDLM lock on the non-striped and striped objects.
4715  *
4716  * \see dt_object_operations::do_object_lock() in the API description
4717  * for details.
4718  */
4719 static int lod_object_lock(const struct lu_env *env,
4720                            struct dt_object *dt,
4721                            struct lustre_handle *lh,
4722                            struct ldlm_enqueue_info *einfo,
4723                            union ldlm_policy_data *policy)
4724 {
4725         struct lod_object       *lo = lod_dt_obj(dt);
4726         int                     rc = 0;
4727         int                     i;
4728         int                     slave_locks_size;
4729         struct lustre_handle_array *slave_locks = NULL;
4730         ENTRY;
4731
4732         /* remote object lock */
4733         if (!einfo->ei_enq_slave) {
4734                 LASSERT(dt_object_remote(dt));
4735                 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
4736                                       policy);
4737         }
4738
4739         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4740                 GOTO(out, rc = -ENOTDIR);
4741
4742         rc = lod_load_striping(env, lo);
4743         if (rc != 0)
4744                 GOTO(out, rc);
4745
4746         /* No stripes */
4747         if (lo->ldo_dir_stripenr <= 1) {
4748                 /*
4749                  * NB, ei_cbdata stores pointer to slave locks, if no locks
4750                  * taken, make sure it's set to NULL, otherwise MDT will try to
4751                  * unlock them.
4752                  */
4753                 einfo->ei_cbdata = NULL;
4754                 GOTO(out, rc = 0);
4755         }
4756
4757         slave_locks_size = sizeof(*slave_locks) + lo->ldo_dir_stripenr *
4758                            sizeof(slave_locks->handles[0]);
4759         /* Freed in lod_object_unlock */
4760         OBD_ALLOC(slave_locks, slave_locks_size);
4761         if (slave_locks == NULL)
4762                 GOTO(out, rc = -ENOMEM);
4763         slave_locks->count = lo->ldo_dir_stripenr;
4764
4765         /* striped directory lock */
4766         for (i = 1; i < lo->ldo_dir_stripenr; i++) {
4767                 struct lustre_handle    lockh;
4768                 struct ldlm_res_id      *res_id;
4769
4770                 res_id = &lod_env_info(env)->lti_res_id;
4771                 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
4772                                        res_id);
4773                 einfo->ei_res_id = res_id;
4774
4775                 LASSERT(lo->ldo_stripe[i] != NULL);
4776                 if (likely(dt_object_remote(lo->ldo_stripe[i]))) {
4777                         rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh,
4778                                             einfo, policy);
4779                 } else {
4780                         struct ldlm_namespace *ns = einfo->ei_namespace;
4781                         ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
4782                         ldlm_completion_callback completion = einfo->ei_cb_cp;
4783                         __u64   dlmflags = LDLM_FL_ATOMIC_CB;
4784
4785                         if (einfo->ei_mode == LCK_PW ||
4786                             einfo->ei_mode == LCK_EX)
4787                                 dlmflags |= LDLM_FL_COS_INCOMPAT;
4788
4789                         /* This only happens if there are mulitple stripes
4790                          * on the master MDT, i.e. except stripe0, there are
4791                          * other stripes on the Master MDT as well, Only
4792                          * happens in the test case right now. */
4793                         LASSERT(ns != NULL);
4794                         rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS,
4795                                                     policy, einfo->ei_mode,
4796                                                     &dlmflags, blocking,
4797                                                     completion, NULL,
4798                                                     NULL, 0, LVB_T_NONE,
4799                                                     NULL, &lockh);
4800                 }
4801                 if (rc != 0)
4802                         break;
4803                 slave_locks->handles[i] = lockh;
4804         }
4805         einfo->ei_cbdata = slave_locks;
4806
4807         if (rc != 0 && slave_locks != NULL) {
4808                 lod_object_unlock_internal(env, dt, einfo, policy);
4809                 OBD_FREE(slave_locks, slave_locks_size);
4810         }
4811         EXIT;
4812 out:
4813         if (rc != 0)
4814                 einfo->ei_cbdata = NULL;
4815         RETURN(rc);
4816 }
4817
4818 /**
4819  * Implementation of dt_object_operations::do_invalidate.
4820  *
4821  * \see dt_object_operations::do_invalidate() in the API description for details
4822  */
4823 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
4824 {
4825         return dt_invalidate(env, dt_object_child(dt));
4826 }
4827
4828 static int lod_declare_layout_change(const struct lu_env *env,
4829                                      struct dt_object *dt,
4830                                      struct layout_intent *layout,
4831                                      const struct lu_buf *buf,
4832                                      struct thandle *th)
4833 {
4834         struct lod_thread_info  *info = lod_env_info(env);
4835         struct lod_object *lo = lod_dt_obj(dt);
4836         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
4837         struct dt_object *next = dt_object_child(dt);
4838         struct ost_pool *inuse = &info->lti_inuse_osts;
4839         struct lod_layout_component *lod_comp;
4840         struct lov_comp_md_v1 *comp_v1 = NULL;
4841         bool replay = false;
4842         bool need_create = false;
4843         int i, rc;
4844         ENTRY;
4845
4846         if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
4847             dt_object_remote(next))
4848                 RETURN(-EINVAL);
4849
4850         dt_write_lock(env, next, 0);
4851         /*
4852          * In case the client is passing lovea, which only happens during
4853          * the replay of layout intent write RPC for now, we may need to
4854          * parse the lovea and apply new layout configuration.
4855          */
4856         if (buf && buf->lb_len)  {
4857                 struct lov_user_md_v1 *v1 = buf->lb_buf;
4858
4859                 if (v1->lmm_magic != (LOV_MAGIC_DEF | LOV_MAGIC_COMP_V1) &&
4860                     v1->lmm_magic !=
4861                                 __swab32(LOV_MAGIC_DEF | LOV_MAGIC_COMP_V1)) {
4862                         CERROR("%s: the replay buffer of layout extend "
4863                                "(magic %#x) does not contain expected "
4864                                "composite layout.\n",
4865                                lod2obd(d)->obd_name, v1->lmm_magic);
4866                         GOTO(out, rc = -EINVAL);
4867                 }
4868
4869                 lod_object_free_striping(env, lo);
4870                 rc = lod_use_defined_striping(env, lo, buf);
4871                 if (rc)
4872                         GOTO(out, rc);
4873
4874                 rc = lod_get_lov_ea(env, lo);
4875                 if (rc <= 0)
4876                         GOTO(out, rc);
4877                 /* old on-disk EA is stored in info->lti_buf */
4878                 comp_v1 = (struct lov_comp_md_v1 *)&info->lti_buf.lb_buf;
4879                 replay = true;
4880         } else {
4881                 /* non replay path */
4882                 rc = lod_load_striping_locked(env, lo);
4883                 if (rc)
4884                         GOTO(out, rc);
4885
4886                 /* Prepare inuse array for composite file */
4887                 rc = lod_prepare_inuse(env, lo);
4888                 if (rc)
4889                         GOTO(out, rc);
4890         }
4891
4892         /* Make sure defined layout covers the requested write range. */
4893         lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
4894         if (lo->ldo_comp_cnt > 1 &&
4895             lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
4896             lod_comp->llc_extent.e_end < layout->li_end) {
4897                 CDEBUG(replay ? D_ERROR : D_LAYOUT,
4898                        "%s: the defined layout [0, %#llx) does not covers "
4899                        "the write range [%#llx, %#llx).\n",
4900                        lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
4901                        layout->li_start, layout->li_end);
4902                 GOTO(out, rc = -EINVAL);
4903         }
4904
4905         /*
4906          * Iterate ld->ldo_comp_entries, find the component whose extent under
4907          * the write range and not instantianted.
4908          */
4909         for (i = 0; i < lo->ldo_comp_cnt; i++) {
4910                 lod_comp = &lo->ldo_comp_entries[i];
4911
4912                 if (lod_comp->llc_extent.e_start >= layout->li_end)
4913                         break;
4914
4915                 if (!replay) {
4916                         if (lod_comp_inited(lod_comp))
4917                                 continue;
4918                 } else {
4919                         /**
4920                          * In replay path, lod_comp is the EA passed by
4921                          * client replay buffer,  comp_v1 is the pre-recovery
4922                          * on-disk EA, we'd sift out those components which
4923                          * were init-ed in the on-disk EA.
4924                          */
4925                         if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
4926                             LCME_FL_INIT)
4927                                 continue;
4928                 }
4929                 /*
4930                  * this component hasn't instantiated in normal path, or during
4931                  * replay it needs replay the instantiation.
4932                  */
4933
4934                 /* A released component is being extended */
4935                 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
4936                         GOTO(out, rc = -EINVAL);
4937
4938                 need_create = true;
4939
4940                 rc = lod_qos_prep_create(env, lo, NULL, th, i, inuse);
4941                 if (rc)
4942                         break;
4943         }
4944
4945         if (need_create)
4946                 lod_obj_inc_layout_gen(lo);
4947         else
4948                 GOTO(unlock, rc = -EALREADY);
4949
4950         if (!rc) {
4951                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
4952                 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
4953                                                XATTR_NAME_LOV, 0, th);
4954         }
4955 out:
4956         if (rc)
4957                 lod_object_free_striping(env, lo);
4958
4959 unlock:
4960         dt_write_unlock(env, next);
4961
4962         RETURN(rc);
4963 }
4964
4965 /**
4966  * Instantiate layout component objects which covers the intent write offset.
4967  */
4968 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
4969                              struct layout_intent *layout,
4970                              const struct lu_buf *buf, struct thandle *th)
4971 {
4972         struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4973
4974         RETURN(lod_striped_create(env, dt, attr, NULL, th));
4975 }
4976
4977 struct dt_object_operations lod_obj_ops = {
4978         .do_read_lock           = lod_read_lock,
4979         .do_write_lock          = lod_write_lock,
4980         .do_read_unlock         = lod_read_unlock,
4981         .do_write_unlock        = lod_write_unlock,
4982         .do_write_locked        = lod_write_locked,
4983         .do_attr_get            = lod_attr_get,
4984         .do_declare_attr_set    = lod_declare_attr_set,
4985         .do_attr_set            = lod_attr_set,
4986         .do_xattr_get           = lod_xattr_get,
4987         .do_declare_xattr_set   = lod_declare_xattr_set,
4988         .do_xattr_set           = lod_xattr_set,
4989         .do_declare_xattr_del   = lod_declare_xattr_del,
4990         .do_xattr_del           = lod_xattr_del,
4991         .do_xattr_list          = lod_xattr_list,
4992         .do_ah_init             = lod_ah_init,
4993         .do_declare_create      = lod_declare_create,
4994         .do_create              = lod_create,
4995         .do_declare_destroy     = lod_declare_destroy,
4996         .do_destroy             = lod_destroy,
4997         .do_index_try           = lod_index_try,
4998         .do_declare_ref_add     = lod_declare_ref_add,
4999         .do_ref_add             = lod_ref_add,
5000         .do_declare_ref_del     = lod_declare_ref_del,
5001         .do_ref_del             = lod_ref_del,
5002         .do_object_sync         = lod_object_sync,
5003         .do_object_lock         = lod_object_lock,
5004         .do_object_unlock       = lod_object_unlock,
5005         .do_invalidate          = lod_invalidate,
5006         .do_declare_layout_change = lod_declare_layout_change,
5007         .do_layout_change       = lod_layout_change,
5008 };
5009
5010 /**
5011  * Implementation of dt_body_operations::dbo_read.
5012  *
5013  * \see dt_body_operations::dbo_read() in the API description for details.
5014  */
5015 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
5016                         struct lu_buf *buf, loff_t *pos)
5017 {
5018         struct dt_object *next = dt_object_child(dt);
5019
5020         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
5021                 S_ISLNK(dt->do_lu.lo_header->loh_attr));
5022         return next->do_body_ops->dbo_read(env, next, buf, pos);
5023 }
5024
5025 /**
5026  * Implementation of dt_body_operations::dbo_declare_write.
5027  *
5028  * \see dt_body_operations::dbo_declare_write() in the API description
5029  * for details.
5030  */
5031 static ssize_t lod_declare_write(const struct lu_env *env,
5032                                  struct dt_object *dt,
5033                                  const struct lu_buf *buf, loff_t pos,
5034                                  struct thandle *th)
5035 {
5036         return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
5037 }
5038
5039 /**
5040  * Implementation of dt_body_operations::dbo_write.
5041  *
5042  * \see dt_body_operations::dbo_write() in the API description for details.
5043  */
5044 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
5045                          const struct lu_buf *buf, loff_t *pos,
5046                          struct thandle *th, int iq)
5047 {
5048         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
5049                 S_ISLNK(dt->do_lu.lo_header->loh_attr));
5050         return lod_sub_write(env, dt_object_child(dt), buf, pos, th, iq);
5051 }
5052
5053 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
5054                              __u64 start, __u64 end, struct thandle *th)
5055 {
5056         if (dt_object_remote(dt))
5057                 return -ENOTSUPP;
5058
5059         return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
5060 }
5061
5062 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
5063                      __u64 start, __u64 end, struct thandle *th)
5064 {
5065         if (dt_object_remote(dt))
5066                 return -ENOTSUPP;
5067
5068         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
5069         return lod_sub_punch(env, dt_object_child(dt), start, end, th);
5070 }
5071
5072 /*
5073  * different type of files use the same body_ops because object may be created
5074  * in OUT, where there is no chance to set correct body_ops for each type, so
5075  * body_ops themselves will check file type inside, see lod_read/write/punch for
5076  * details.
5077  */
5078 const struct dt_body_operations lod_body_ops = {
5079         .dbo_read               = lod_read,
5080         .dbo_declare_write      = lod_declare_write,
5081         .dbo_write              = lod_write,
5082         .dbo_declare_punch      = lod_declare_punch,
5083         .dbo_punch              = lod_punch,
5084 };
5085
5086 /**
5087  * Implementation of lu_object_operations::loo_object_init.
5088  *
5089  * The function determines the type and the index of the target device using
5090  * sequence of the object's FID. Then passes control down to the
5091  * corresponding device:
5092  *  OSD for the local objects, OSP for remote
5093  *
5094  * \see lu_object_operations::loo_object_init() in the API description
5095  * for details.
5096  */
5097 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
5098                            const struct lu_object_conf *conf)
5099 {
5100         struct lod_device       *lod    = lu2lod_dev(lo->lo_dev);
5101         struct lu_device        *cdev   = NULL;
5102         struct lu_object        *cobj;
5103         struct lod_tgt_descs    *ltd    = NULL;
5104         struct lod_tgt_desc     *tgt;
5105         u32                      idx    = 0;
5106         int                      type   = LU_SEQ_RANGE_ANY;
5107         int                      rc;
5108         ENTRY;
5109
5110         rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
5111         if (rc != 0) {
5112                 /* Note: Sometimes, it will Return EAGAIN here, see
5113                  * ptrlpc_import_delay_req(), which might confuse
5114                  * lu_object_find_at() and make it wait there incorrectly.
5115                  * so we convert it to EIO here.*/
5116                 if (rc == -EAGAIN)
5117                         rc = -EIO;
5118
5119                 RETURN(rc);
5120         }
5121
5122         if (type == LU_SEQ_RANGE_MDT &&
5123             idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
5124                 cdev = &lod->lod_child->dd_lu_dev;
5125         } else if (type == LU_SEQ_RANGE_MDT) {
5126                 ltd = &lod->lod_mdt_descs;
5127                 lod_getref(ltd);
5128         } else if (type == LU_SEQ_RANGE_OST) {
5129                 ltd = &lod->lod_ost_descs;
5130                 lod_getref(ltd);
5131         } else {
5132                 LBUG();
5133         }
5134
5135         if (ltd != NULL) {
5136                 if (ltd->ltd_tgts_size > idx &&
5137                     cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
5138                         tgt = LTD_TGT(ltd, idx);
5139
5140                         LASSERT(tgt != NULL);
5141                         LASSERT(tgt->ltd_tgt != NULL);
5142
5143                         cdev = &(tgt->ltd_tgt->dd_lu_dev);
5144                 }
5145                 lod_putref(lod, ltd);
5146         }
5147
5148         if (unlikely(cdev == NULL))
5149                 RETURN(-ENOENT);
5150
5151         cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
5152         if (unlikely(cobj == NULL))
5153                 RETURN(-ENOMEM);
5154
5155         lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
5156
5157         lu_object_add(lo, cobj);
5158
5159         RETURN(0);
5160 }
5161
5162 /**
5163  *
5164  * Release resources associated with striping.
5165  *
5166  * If the object is striped (regular or directory), then release
5167  * the stripe objects references and free the ldo_stripe array.
5168  *
5169  * \param[in] env       execution environment
5170  * \param[in] lo        object
5171  */
5172 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
5173 {
5174         struct lod_layout_component *lod_comp;
5175         int i, j;
5176
5177         if (lo->ldo_stripe != NULL) {
5178                 LASSERT(lo->ldo_comp_entries == NULL);
5179                 LASSERT(lo->ldo_dir_stripes_allocated > 0);
5180
5181                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
5182                         if (lo->ldo_stripe[i])
5183                                 dt_object_put(env, lo->ldo_stripe[i]);
5184                 }
5185
5186                 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
5187                 OBD_FREE(lo->ldo_stripe, j);
5188                 lo->ldo_stripe = NULL;
5189                 lo->ldo_dir_stripes_allocated = 0;
5190                 lo->ldo_dir_stripenr = 0;
5191         } else if (lo->ldo_comp_entries != NULL) {
5192                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5193                         /* free lod_layout_component::llc_stripe array */
5194                         lod_comp = &lo->ldo_comp_entries[i];
5195
5196                         if (lod_comp->llc_stripe == NULL)
5197                                 continue;
5198                         LASSERT(lod_comp->llc_stripes_allocated != 0);
5199                         for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
5200                                 if (lod_comp->llc_stripe[j] != NULL)
5201                                         lu_object_put(env,
5202                                                &lod_comp->llc_stripe[j]->do_lu);
5203                         }
5204                         OBD_FREE(lod_comp->llc_stripe,
5205                                  sizeof(struct dt_object *) *
5206                                  lod_comp->llc_stripes_allocated);
5207                         lod_comp->llc_stripe = NULL;
5208                         lod_comp->llc_stripes_allocated = 0;
5209                 }
5210                 lod_free_comp_entries(lo);
5211                 lo->ldo_comp_cached = 0;
5212         }
5213 }
5214
5215 /**
5216  * Implementation of lu_object_operations::loo_object_free.
5217  *
5218  * \see lu_object_operations::loo_object_free() in the API description
5219  * for details.
5220  */
5221 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
5222 {
5223         struct lod_object *lo = lu2lod_obj(o);
5224
5225         /* release all underlying object pinned */
5226         lod_object_free_striping(env, lo);
5227         lu_object_fini(o);
5228         OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
5229 }
5230
5231 /**
5232  * Implementation of lu_object_operations::loo_object_release.
5233  *
5234  * \see lu_object_operations::loo_object_release() in the API description
5235  * for details.
5236  */
5237 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
5238 {
5239         /* XXX: shouldn't we release everything here in case if object
5240          * creation failed before? */
5241 }
5242
5243 /**
5244  * Implementation of lu_object_operations::loo_object_print.
5245  *
5246  * \see lu_object_operations::loo_object_print() in the API description
5247  * for details.
5248  */
5249 static int lod_object_print(const struct lu_env *env, void *cookie,
5250                             lu_printer_t p, const struct lu_object *l)
5251 {
5252         struct lod_object *o = lu2lod_obj((struct lu_object *) l);
5253
5254         return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
5255 }
5256
5257 struct lu_object_operations lod_lu_obj_ops = {
5258         .loo_object_init        = lod_object_init,
5259         .loo_object_free        = lod_object_free,
5260         .loo_object_release     = lod_object_release,
5261         .loo_object_print       = lod_object_print,
5262 };