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