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