Whamcloud - gitweb
LU-11757 lod: use calculated stripe count
[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
1896         stripe_count = lo->ldo_dir_stripe_count;
1897
1898         OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1899         if (idx_array == NULL)
1900                 RETURN(-ENOMEM);
1901
1902         OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1903         if (stripe == NULL)
1904                 GOTO(out_free, rc = -ENOMEM);
1905
1906         /* Start index must be the master MDT */
1907         master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1908         idx_array[0] = master_index;
1909         if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) {
1910                 is_specific = true;
1911                 for (i = 1; i < stripe_count; i++)
1912                         idx_array[i] = le32_to_cpu(lum->lum_objects[i].lum_mds);
1913         }
1914
1915         for (i = 0; i < stripe_count; i++) {
1916                 struct lod_tgt_desc     *tgt = NULL;
1917                 struct dt_object        *dto;
1918                 struct lu_fid           fid = { 0 };
1919                 int                     idx;
1920                 struct lu_object_conf   conf = { 0 };
1921                 struct dt_device        *tgt_dt = NULL;
1922
1923                 /* Try to find next avaible target */
1924                 idx = idx_array[i];
1925                 for (j = 0; j < lod->lod_remote_mdt_count;
1926                      j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1927                         bool already_allocated = false;
1928                         __u32 k;
1929
1930                         CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
1931                                idx, lod->lod_remote_mdt_count + 1, i);
1932
1933                         if (likely(!is_specific &&
1934                                    !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
1935                                 /* check whether the idx already exists
1936                                  * in current allocated array */
1937                                 for (k = 0; k < i; k++) {
1938                                         if (idx_array[k] == idx) {
1939                                                 already_allocated = true;
1940                                                 break;
1941                                         }
1942                                 }
1943
1944                                 if (already_allocated)
1945                                         continue;
1946                         }
1947
1948                         /* Sigh, this index is not in the bitmap, let's check
1949                          * next available target */
1950                         if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) &&
1951                             idx != master_index)
1952                                 continue;
1953
1954                         if (idx == master_index) {
1955                                 /* Allocate the FID locally */
1956                                 rc = obd_fid_alloc(env, lod->lod_child_exp,
1957                                                    &fid, NULL);
1958                                 if (rc < 0)
1959                                         GOTO(out_put, rc);
1960                                 tgt_dt = lod->lod_child;
1961                                 break;
1962                         }
1963
1964                         /* check the status of the OSP */
1965                         tgt = LTD_TGT(ltd, idx);
1966                         if (tgt == NULL)
1967                                 continue;
1968
1969                         tgt_dt = tgt->ltd_tgt;
1970                         rc = dt_statfs(env, tgt_dt, &info->lti_osfs);
1971                         if (rc) {
1972                                 /* this OSP doesn't feel well */
1973                                 rc = 0;
1974                                 continue;
1975                         }
1976
1977                         rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1978                         if (rc < 0) {
1979                                 rc = 0;
1980                                 continue;
1981                         }
1982
1983                         break;
1984                 }
1985
1986                 /* Can not allocate more stripes */
1987                 if (j == lod->lod_remote_mdt_count) {
1988                         CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1989                                lod2obd(lod)->obd_name, stripe_count, i);
1990                         break;
1991                 }
1992
1993                 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
1994                        idx, i, PFID(&fid));
1995                 idx_array[i] = idx;
1996                 /* Set the start index for next stripe allocation */
1997                 if (!is_specific && i < stripe_count - 1) {
1998                         /*
1999                          * for large dir test, put all other slaves on one
2000                          * remote MDT, otherwise we may save too many local
2001                          * slave locks which will exceed RS_MAX_LOCKS.
2002                          */
2003                         if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)))
2004                                 idx = master_index;
2005                         idx_array[i + 1] = (idx + 1) %
2006                                            (lod->lod_remote_mdt_count + 1);
2007                 }
2008                 /* tgt_dt and fid must be ready after search avaible OSP
2009                  * in the above loop */
2010                 LASSERT(tgt_dt != NULL);
2011                 LASSERT(fid_is_sane(&fid));
2012                 conf.loc_flags = LOC_F_NEW;
2013                 dto = dt_locate_at(env, tgt_dt, &fid,
2014                                    dt->do_lu.lo_dev->ld_site->ls_top_dev,
2015                                    &conf);
2016                 if (IS_ERR(dto))
2017                         GOTO(out_put, rc = PTR_ERR(dto));
2018                 stripe[i] = dto;
2019         }
2020
2021         lo->ldo_dir_striped = 1;
2022         lo->ldo_stripe = stripe;
2023         lo->ldo_dir_stripe_count = i;
2024         lo->ldo_dir_stripes_allocated = stripe_count;
2025         smp_mb();
2026         lo->ldo_dir_stripe_loaded = 1;
2027
2028         if (lo->ldo_dir_stripe_count == 0)
2029                 GOTO(out_put, rc = -ENOSPC);
2030
2031         rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
2032         if (rc != 0)
2033                 GOTO(out_put, rc);
2034
2035 out_put:
2036         if (rc < 0) {
2037                 for (i = 0; i < stripe_count; i++)
2038                         if (stripe[i] != NULL)
2039                                 dt_object_put(env, stripe[i]);
2040                 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
2041                 lo->ldo_dir_stripe_count = 0;
2042                 lo->ldo_dir_stripes_allocated = 0;
2043                 lo->ldo_stripe = NULL;
2044         }
2045
2046 out_free:
2047         OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
2048
2049         RETURN(rc);
2050 }
2051
2052 /**
2053  * Declare create striped md object.
2054  *
2055  * The function declares intention to create a striped directory. This is a
2056  * wrapper for lod_prep_md_striped_create(). The only additional functionality
2057  * is to verify pattern \a lum_buf is good. Check that function for the details.
2058  *
2059  * \param[in] env       execution environment
2060  * \param[in] dt        object
2061  * \param[in] attr      attributes to initialize the objects with
2062  * \param[in] lum_buf   a pattern specifying the number of stripes and
2063  *                      MDT to start from
2064  * \param[in] dof       type of objects to be created
2065  * \param[in] th        transaction handle
2066  *
2067  * \retval              0 on success
2068  * \retval              negative if failed
2069  *
2070  */
2071 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
2072                                      struct dt_object *dt,
2073                                      struct lu_attr *attr,
2074                                      const struct lu_buf *lum_buf,
2075                                      struct dt_object_format *dof,
2076                                      struct thandle *th)
2077 {
2078         struct lod_object       *lo = lod_dt_obj(dt);
2079         struct lmv_user_md_v1   *lum = lum_buf->lb_buf;
2080         int                     rc;
2081         ENTRY;
2082
2083         LASSERT(lum != NULL);
2084
2085         CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
2086                le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
2087                (int)le32_to_cpu(lum->lum_stripe_offset));
2088
2089         if (lo->ldo_dir_stripe_count == 0)
2090                 GOTO(out, rc = 0);
2091
2092         /* prepare dir striped objects */
2093         rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2094         if (rc != 0) {
2095                 /* failed to create striping, let's reset
2096                  * config so that others don't get confused */
2097                 lod_striping_free(env, lo);
2098                 GOTO(out, rc);
2099         }
2100 out:
2101         RETURN(rc);
2102 }
2103
2104 /**
2105  * Append source stripes after target stripes for migrating directory. NB, we
2106  * only need to declare this, the append is done inside lod_xattr_set_lmv().
2107  *
2108  * \param[in] env       execution environment
2109  * \param[in] dt        target object
2110  * \param[in] buf       LMV buf which contains source stripe fids
2111  * \param[in] th        transaction handle
2112  *
2113  * \retval              0 on success
2114  * \retval              negative if failed
2115  */
2116 static int lod_dir_declare_layout_add(const struct lu_env *env,
2117                                       struct dt_object *dt,
2118                                       const struct lu_buf *buf,
2119                                       struct thandle *th)
2120 {
2121         struct lod_thread_info *info = lod_env_info(env);
2122         struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
2123         struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
2124         struct lod_object *lo = lod_dt_obj(dt);
2125         struct dt_object *next = dt_object_child(dt);
2126         struct dt_object_format *dof = &info->lti_format;
2127         struct lmv_mds_md_v1 *lmv = buf->lb_buf;
2128         struct dt_object **stripe;
2129         __u32 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
2130         struct lu_fid *fid = &info->lti_fid;
2131         struct lod_tgt_desc *tgt;
2132         struct dt_object *dto;
2133         struct dt_device *tgt_dt;
2134         int type = LU_SEQ_RANGE_ANY;
2135         struct dt_insert_rec *rec = &info->lti_dt_rec;
2136         char *stripe_name = info->lti_key;
2137         struct lu_name *sname;
2138         struct linkea_data ldata = { NULL };
2139         struct lu_buf linkea_buf;
2140         __u32 idx;
2141         int i;
2142         int rc;
2143
2144         ENTRY;
2145
2146         if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
2147                 RETURN(-EINVAL);
2148
2149         if (stripe_count == 0)
2150                 RETURN(-EINVAL);
2151
2152         dof->dof_type = DFT_DIR;
2153
2154         OBD_ALLOC(stripe,
2155                   sizeof(*stripe) * (lo->ldo_dir_stripe_count + stripe_count));
2156         if (stripe == NULL)
2157                 RETURN(-ENOMEM);
2158
2159         for (i = 0; i < lo->ldo_dir_stripe_count; i++)
2160                 stripe[i] = lo->ldo_stripe[i];
2161
2162         for (i = 0; i < stripe_count; i++) {
2163                 fid_le_to_cpu(fid,
2164                         &lmv->lmv_stripe_fids[i]);
2165                 if (!fid_is_sane(fid))
2166                         GOTO(out, rc = -ESTALE);
2167
2168                 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
2169                 if (rc)
2170                         GOTO(out, rc);
2171
2172                 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
2173                         tgt_dt = lod->lod_child;
2174                 } else {
2175                         tgt = LTD_TGT(ltd, idx);
2176                         if (tgt == NULL)
2177                                 GOTO(out, rc = -ESTALE);
2178                         tgt_dt = tgt->ltd_tgt;
2179                 }
2180
2181                 dto = dt_locate_at(env, tgt_dt, fid,
2182                                   lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
2183                                   NULL);
2184                 if (IS_ERR(dto))
2185                         GOTO(out, rc = PTR_ERR(dto));
2186
2187                 stripe[i + lo->ldo_dir_stripe_count] = dto;
2188
2189                 if (!dt_try_as_dir(env, dto))
2190                         GOTO(out, rc = -ENOTDIR);
2191
2192                 rc = lod_sub_declare_ref_add(env, dto, th);
2193                 if (rc)
2194                         GOTO(out, rc);
2195
2196                 rc = lod_sub_declare_insert(env, dto,
2197                                             (const struct dt_rec *)rec,
2198                                             (const struct dt_key *)dot, th);
2199                 if (rc)
2200                         GOTO(out, rc);
2201
2202                 rc = lod_sub_declare_insert(env, dto,
2203                                             (const struct dt_rec *)rec,
2204                                             (const struct dt_key *)dotdot, th);
2205                 if (rc)
2206                         GOTO(out, rc);
2207
2208                 rc = lod_sub_declare_xattr_set(env, dto, buf,
2209                                                 XATTR_NAME_LMV, 0, th);
2210                 if (rc)
2211                         GOTO(out, rc);
2212
2213                 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
2214                          PFID(lu_object_fid(&dto->do_lu)),
2215                          i + lo->ldo_dir_stripe_count);
2216
2217                 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2218                 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
2219                                       sname, lu_object_fid(&dt->do_lu));
2220                 if (rc)
2221                         GOTO(out, rc);
2222
2223                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
2224                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
2225                 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
2226                                                XATTR_NAME_LINK, 0, th);
2227                 if (rc)
2228                         GOTO(out, rc);
2229
2230                 rc = lod_sub_declare_insert(env, next,
2231                                             (const struct dt_rec *)rec,
2232                                             (const struct dt_key *)stripe_name,
2233                                             th);
2234                 if (rc)
2235                         GOTO(out, rc);
2236
2237                 rc = lod_sub_declare_ref_add(env, next, th);
2238                 if (rc)
2239                         GOTO(out, rc);
2240         }
2241
2242         if (lo->ldo_stripe)
2243                 OBD_FREE(lo->ldo_stripe,
2244                          sizeof(*stripe) * lo->ldo_dir_stripes_allocated);
2245         lo->ldo_stripe = stripe;
2246         lo->ldo_dir_migrate_offset = lo->ldo_dir_stripe_count;
2247         lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_hash_type);
2248         lo->ldo_dir_stripe_count += stripe_count;
2249         lo->ldo_dir_stripes_allocated += stripe_count;
2250         lo->ldo_dir_hash_type |= LMV_HASH_FLAG_MIGRATION;
2251
2252         RETURN(0);
2253 out:
2254         i = lo->ldo_dir_stripe_count;
2255         while (i < lo->ldo_dir_stripe_count + stripe_count && stripe[i])
2256                 dt_object_put(env, stripe[i++]);
2257
2258         OBD_FREE(stripe,
2259                  sizeof(*stripe) * (stripe_count + lo->ldo_dir_stripe_count));
2260         RETURN(rc);
2261 }
2262
2263 static int lod_dir_declare_layout_delete(const struct lu_env *env,
2264                                          struct dt_object *dt,
2265                                          const struct lu_buf *buf,
2266                                          struct thandle *th)
2267 {
2268         struct lod_thread_info *info = lod_env_info(env);
2269         struct lod_object *lo = lod_dt_obj(dt);
2270         struct dt_object *next = dt_object_child(dt);
2271         struct lmv_user_md *lmu = buf->lb_buf;
2272         __u32 final_stripe_count;
2273         char *stripe_name = info->lti_key;
2274         struct dt_object *dto;
2275         int i;
2276         int rc = 0;
2277
2278         if (!lmu)
2279                 return -EINVAL;
2280
2281         final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
2282         if (final_stripe_count >= lo->ldo_dir_stripe_count)
2283                 return -EINVAL;
2284
2285         for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
2286                 dto = lo->ldo_stripe[i];
2287                 LASSERT(dto);
2288
2289                 if (!dt_try_as_dir(env, dto))
2290                         return -ENOTDIR;
2291
2292                 rc = lod_sub_declare_delete(env, dto,
2293                                             (const struct dt_key *)dot, th);
2294                 if (rc)
2295                         return rc;
2296
2297                 rc = lod_sub_declare_ref_del(env, dto, th);
2298                 if (rc)
2299                         return rc;
2300
2301                 rc = lod_sub_declare_delete(env, dto,
2302                                         (const struct dt_key *)dotdot, th);
2303                 if (rc)
2304                         return rc;
2305
2306                 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2307                          PFID(lu_object_fid(&dto->do_lu)), i);
2308
2309                 rc = lod_sub_declare_delete(env, next,
2310                                         (const struct dt_key *)stripe_name, th);
2311                 if (rc)
2312                         return rc;
2313
2314                 rc = lod_sub_declare_ref_del(env, next, th);
2315                 if (rc)
2316                         return rc;
2317         }
2318
2319         return 0;
2320 }
2321
2322 /*
2323  * delete stripes from dir master object, the lum_stripe_count in argument is
2324  * the final stripe count, the stripes after that will be deleted, NB, they
2325  * are not destroyed, but deleted from it's parent namespace, this function
2326  * will be called in two places:
2327  * 1. mdd_migrate_create() delete stripes from source, and append them to
2328  *    target.
2329  * 2. mdd_dir_layout_shrink() delete stripes from source, and destroy them.
2330  */
2331 static int lod_dir_layout_delete(const struct lu_env *env,
2332                                  struct dt_object *dt,
2333                                  const struct lu_buf *buf,
2334                                  struct thandle *th)
2335 {
2336         struct lod_thread_info *info = lod_env_info(env);
2337         struct lod_object *lo = lod_dt_obj(dt);
2338         struct dt_object *next = dt_object_child(dt);
2339         struct lmv_user_md *lmu = buf->lb_buf;
2340         __u32 final_stripe_count;
2341         char *stripe_name = info->lti_key;
2342         struct dt_object *dto;
2343         int i;
2344         int rc = 0;
2345
2346         ENTRY;
2347
2348         if (!lmu)
2349                 RETURN(-EINVAL);
2350
2351         final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
2352         if (final_stripe_count >= lo->ldo_dir_stripe_count)
2353                 RETURN(-EINVAL);
2354
2355         for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
2356                 dto = lo->ldo_stripe[i];
2357                 LASSERT(dto);
2358
2359                 rc = lod_sub_delete(env, dto,
2360                                     (const struct dt_key *)dotdot, th);
2361                 if (rc)
2362                         break;
2363
2364                 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2365                          PFID(lu_object_fid(&dto->do_lu)), i);
2366
2367                 rc = lod_sub_delete(env, next,
2368                                     (const struct dt_key *)stripe_name, th);
2369                 if (rc)
2370                         break;
2371
2372                 rc = lod_sub_ref_del(env, next, th);
2373                 if (rc)
2374                         break;
2375         }
2376
2377         lod_striping_free(env, lod_dt_obj(dt));
2378
2379         RETURN(rc);
2380 }
2381
2382 /**
2383  * Implementation of dt_object_operations::do_declare_xattr_set.
2384  *
2385  * Used with regular (non-striped) objects. Basically it
2386  * initializes the striping information and applies the
2387  * change to all the stripes.
2388  *
2389  * \see dt_object_operations::do_declare_xattr_set() in the API description
2390  * for details.
2391  */
2392 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2393                                      struct dt_object *dt,
2394                                      const struct lu_buf *buf,
2395                                      const char *name, int fl,
2396                                      struct thandle *th)
2397 {
2398         struct dt_object        *next = dt_object_child(dt);
2399         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2400         struct lod_object       *lo = lod_dt_obj(dt);
2401         int                     i;
2402         int                     rc;
2403         ENTRY;
2404
2405         if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2406                 struct lmv_user_md_v1 *lum;
2407
2408                 LASSERT(buf != NULL && buf->lb_buf != NULL);
2409                 lum = buf->lb_buf;
2410                 rc = lod_verify_md_striping(d, lum);
2411                 if (rc != 0)
2412                         RETURN(rc);
2413         } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2414                 rc = lod_verify_striping(d, lo, buf, false);
2415                 if (rc != 0)
2416                         RETURN(rc);
2417         }
2418
2419         rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2420         if (rc != 0)
2421                 RETURN(rc);
2422
2423         /* Note: Do not set LinkEA on sub-stripes, otherwise
2424          * it will confuse the fid2path process(see mdt_path_current()).
2425          * The linkEA between master and sub-stripes is set in
2426          * lod_xattr_set_lmv(). */
2427         if (strcmp(name, XATTR_NAME_LINK) == 0)
2428                 RETURN(0);
2429
2430         /* set xattr to each stripes, if needed */
2431         rc = lod_striping_load(env, lo);
2432         if (rc != 0)
2433                 RETURN(rc);
2434
2435         if (lo->ldo_dir_stripe_count == 0)
2436                 RETURN(0);
2437
2438         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2439                 LASSERT(lo->ldo_stripe[i]);
2440
2441                 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2442                                                buf, name, fl, th);
2443                 if (rc != 0)
2444                         break;
2445         }
2446
2447         RETURN(rc);
2448 }
2449
2450 static int
2451 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2452                                      struct lod_object *lo,
2453                                      struct dt_object *dt, struct thandle *th,
2454                                      int comp_idx, int stripe_idx,
2455                                      struct lod_obj_stripe_cb_data *data)
2456 {
2457         struct lod_thread_info *info = lod_env_info(env);
2458         struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
2459         struct filter_fid *ff = &info->lti_ff;
2460         struct lu_buf *buf = &info->lti_buf;
2461         int rc;
2462
2463         buf->lb_buf = ff;
2464         buf->lb_len = sizeof(*ff);
2465         rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2466         if (rc < 0) {
2467                 if (rc == -ENODATA)
2468                         return 0;
2469                 return rc;
2470         }
2471
2472         /*
2473          * locd_buf is set if it's called by dir migration, which doesn't check
2474          * pfid and comp id.
2475          */
2476         if (data->locd_buf) {
2477                 memset(ff, 0, sizeof(*ff));
2478                 ff->ff_parent = *(struct lu_fid *)data->locd_buf->lb_buf;
2479         } else {
2480                 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
2481
2482                 if (lu_fid_eq(lod_object_fid(lo), &ff->ff_parent) &&
2483                     ff->ff_layout.ol_comp_id == comp->llc_id)
2484                         return 0;
2485
2486                 memset(ff, 0, sizeof(*ff));
2487                 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2488         }
2489
2490         /* rewrite filter_fid */
2491         ff->ff_parent.f_ver = stripe_idx;
2492         ff->ff_layout.ol_stripe_size = comp->llc_stripe_size;
2493         ff->ff_layout.ol_stripe_count = comp->llc_stripe_count;
2494         ff->ff_layout.ol_comp_id = comp->llc_id;
2495         ff->ff_layout.ol_comp_start = comp->llc_extent.e_start;
2496         ff->ff_layout.ol_comp_end = comp->llc_extent.e_end;
2497         filter_fid_cpu_to_le(ff, ff, sizeof(*ff));
2498
2499         if (data->locd_declare)
2500                 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2501                                                LU_XATTR_REPLACE, th);
2502         else
2503                 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2504                                        LU_XATTR_REPLACE, th);
2505
2506         return rc;
2507 }
2508
2509 /**
2510  * Reset parent FID on OST object
2511  *
2512  * Replace parent FID with @dt object FID, which is only called during migration
2513  * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2514  * the FID is changed.
2515  *
2516  * \param[in] env execution environment
2517  * \param[in] dt dt_object whose stripes's parent FID will be reset
2518  * \parem[in] th thandle
2519  * \param[in] declare if it is declare
2520  *
2521  * \retval      0 if reset succeeds
2522  * \retval      negative errno if reset fails
2523  */
2524 static int lod_replace_parent_fid(const struct lu_env *env,
2525                                   struct dt_object *dt,
2526                                   const struct lu_buf *buf,
2527                                   struct thandle *th, bool declare)
2528 {
2529         struct lod_object *lo = lod_dt_obj(dt);
2530         struct lod_thread_info  *info = lod_env_info(env);
2531         struct filter_fid *ff;
2532         struct lod_obj_stripe_cb_data data = { { 0 } };
2533         int rc;
2534         ENTRY;
2535
2536         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2537
2538         /* set xattr to each stripes, if needed */
2539         rc = lod_striping_load(env, lo);
2540         if (rc != 0)
2541                 RETURN(rc);
2542
2543         if (!lod_obj_is_striped(dt))
2544                 RETURN(0);
2545
2546         if (info->lti_ea_store_size < sizeof(*ff)) {
2547                 rc = lod_ea_store_resize(info, sizeof(*ff));
2548                 if (rc != 0)
2549                         RETURN(rc);
2550         }
2551
2552         data.locd_declare = declare;
2553         data.locd_stripe_cb = lod_obj_stripe_replace_parent_fid_cb;
2554         data.locd_buf = buf;
2555         rc = lod_obj_for_each_stripe(env, lo, th, &data);
2556
2557         RETURN(rc);
2558 }
2559
2560 inline __u16 lod_comp_entry_stripe_count(struct lod_object *lo,
2561                                          struct lod_layout_component *entry,
2562                                          bool is_dir)
2563 {
2564         struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2565
2566         if (is_dir)
2567                 return  0;
2568         else if (lod_comp_inited(entry))
2569                 return entry->llc_stripe_count;
2570         else if ((__u16)-1 == entry->llc_stripe_count)
2571                 return lod->lod_desc.ld_tgt_count;
2572         else
2573                 return lod_get_stripe_count(lod, lo, entry->llc_stripe_count);
2574 }
2575
2576 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2577 {
2578         int magic, size = 0, i;
2579         struct lod_layout_component *comp_entries;
2580         __u16 comp_cnt;
2581         bool is_composite;
2582
2583         if (is_dir) {
2584                 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2585                 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2586                 is_composite =
2587                         lo->ldo_def_striping->lds_def_striping_is_composite;
2588         } else {
2589                 comp_cnt = lo->ldo_comp_cnt;
2590                 comp_entries = lo->ldo_comp_entries;
2591                 is_composite = lo->ldo_is_composite;
2592         }
2593
2594
2595         LASSERT(comp_cnt != 0 && comp_entries != NULL);
2596         if (is_composite) {
2597                 size = sizeof(struct lov_comp_md_v1) +
2598                        sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2599                 LASSERT(size % sizeof(__u64) == 0);
2600         }
2601
2602         for (i = 0; i < comp_cnt; i++) {
2603                 __u16 stripe_count;
2604
2605                 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2606                 stripe_count = lod_comp_entry_stripe_count(lo, &comp_entries[i],
2607                                                            is_dir);
2608                 if (!is_dir && is_composite)
2609                         lod_comp_shrink_stripe_count(&comp_entries[i],
2610                                                      &stripe_count);
2611
2612                 size += lov_user_md_size(stripe_count, magic);
2613                 LASSERT(size % sizeof(__u64) == 0);
2614         }
2615         return size;
2616 }
2617
2618 /**
2619  * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2620  * the xattr value is binary lov_comp_md_v1 which contains component(s)
2621  * to be added.
2622   *
2623  * \param[in] env       execution environment
2624  * \param[in] dt        dt_object to add components on
2625  * \param[in] buf       buffer contains components to be added
2626  * \parem[in] th        thandle
2627  *
2628  * \retval      0 on success
2629  * \retval      negative errno on failure
2630  */
2631 static int lod_declare_layout_add(const struct lu_env *env,
2632                                   struct dt_object *dt,
2633                                   const struct lu_buf *buf,
2634                                   struct thandle *th)
2635 {
2636         struct lod_thread_info  *info = lod_env_info(env);
2637         struct lod_layout_component *comp_array, *lod_comp, *old_array;
2638         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2639         struct dt_object *next = dt_object_child(dt);
2640         struct lov_desc         *desc = &d->lod_desc;
2641         struct lod_object       *lo = lod_dt_obj(dt);
2642         struct lov_user_md_v3   *v3;
2643         struct lov_comp_md_v1   *comp_v1 = buf->lb_buf;
2644         __u32   magic;
2645         int     i, rc, array_cnt, old_array_cnt;
2646         ENTRY;
2647
2648         LASSERT(lo->ldo_is_composite);
2649
2650         if (lo->ldo_flr_state != LCM_FL_NONE)
2651                 RETURN(-EBUSY);
2652
2653         rc = lod_verify_striping(d, lo, buf, false);
2654         if (rc != 0)
2655                 RETURN(rc);
2656
2657         magic = comp_v1->lcm_magic;
2658         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2659                 lustre_swab_lov_comp_md_v1(comp_v1);
2660                 magic = comp_v1->lcm_magic;
2661         }
2662
2663         if (magic != LOV_USER_MAGIC_COMP_V1)
2664                 RETURN(-EINVAL);
2665
2666         array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2667         OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2668         if (comp_array == NULL)
2669                 RETURN(-ENOMEM);
2670
2671         memcpy(comp_array, lo->ldo_comp_entries,
2672                sizeof(*comp_array) * lo->ldo_comp_cnt);
2673
2674         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2675                 struct lov_user_md_v1 *v1;
2676                 struct lu_extent *ext;
2677
2678                 v1 = (struct lov_user_md *)((char *)comp_v1 +
2679                                 comp_v1->lcm_entries[i].lcme_offset);
2680                 ext = &comp_v1->lcm_entries[i].lcme_extent;
2681
2682                 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2683                 lod_comp->llc_extent.e_start = ext->e_start;
2684                 lod_comp->llc_extent.e_end = ext->e_end;
2685                 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2686                 lod_comp->llc_flags = comp_v1->lcm_entries[i].lcme_flags;
2687
2688                 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2689                 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2690                 lod_adjust_stripe_info(lod_comp, desc);
2691
2692                 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2693                         v3 = (struct lov_user_md_v3 *) v1;
2694                         if (v3->lmm_pool_name[0] != '\0') {
2695                                 rc = lod_set_pool(&lod_comp->llc_pool,
2696                                                   v3->lmm_pool_name);
2697                                 if (rc)
2698                                         GOTO(error, rc);
2699                         }
2700                 }
2701         }
2702
2703         old_array = lo->ldo_comp_entries;
2704         old_array_cnt = lo->ldo_comp_cnt;
2705
2706         lo->ldo_comp_entries = comp_array;
2707         lo->ldo_comp_cnt = array_cnt;
2708
2709         /* No need to increase layout generation here, it will be increased
2710          * later when generating component ID for the new components */
2711
2712         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2713         rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2714                                               XATTR_NAME_LOV, 0, th);
2715         if (rc) {
2716                 lo->ldo_comp_entries = old_array;
2717                 lo->ldo_comp_cnt = old_array_cnt;
2718                 GOTO(error, rc);
2719         }
2720
2721         OBD_FREE(old_array, sizeof(*lod_comp) * old_array_cnt);
2722
2723         LASSERT(lo->ldo_mirror_count == 1);
2724         lo->ldo_mirrors[0].lme_end = array_cnt - 1;
2725
2726         RETURN(0);
2727
2728 error:
2729         for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2730                 lod_comp = &comp_array[i];
2731                 if (lod_comp->llc_pool != NULL) {
2732                         OBD_FREE(lod_comp->llc_pool,
2733                                  strlen(lod_comp->llc_pool) + 1);
2734                         lod_comp->llc_pool = NULL;
2735                 }
2736         }
2737         OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2738         RETURN(rc);
2739 }
2740
2741 /**
2742  * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2743  * the '$field' can only be 'flags' now. The xattr value is binary
2744  * lov_comp_md_v1 which contains the component ID(s) and the value of
2745  * the field to be modified.
2746  *
2747  * \param[in] env       execution environment
2748  * \param[in] dt        dt_object to be modified
2749  * \param[in] op        operation string, like "set.flags"
2750  * \param[in] buf       buffer contains components to be set
2751  * \parem[in] th        thandle
2752  *
2753  * \retval      0 on success
2754  * \retval      negative errno on failure
2755  */
2756 static int lod_declare_layout_set(const struct lu_env *env,
2757                                   struct dt_object *dt,
2758                                   char *op, const struct lu_buf *buf,
2759                                   struct thandle *th)
2760 {
2761         struct lod_layout_component     *lod_comp;
2762         struct lod_thread_info  *info = lod_env_info(env);
2763         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2764         struct lod_object       *lo = lod_dt_obj(dt);
2765         struct lov_comp_md_v1   *comp_v1 = buf->lb_buf;
2766         __u32   magic;
2767         int     i, j, rc;
2768         bool    changed = false;
2769         ENTRY;
2770
2771         if (strcmp(op, "set.flags") != 0) {
2772                 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2773                        lod2obd(d)->obd_name, op);
2774                 RETURN(-ENOTSUPP);
2775         }
2776
2777         magic = comp_v1->lcm_magic;
2778         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2779                 lustre_swab_lov_comp_md_v1(comp_v1);
2780                 magic = comp_v1->lcm_magic;
2781         }
2782
2783         if (magic != LOV_USER_MAGIC_COMP_V1)
2784                 RETURN(-EINVAL);
2785
2786         if (comp_v1->lcm_entry_count == 0) {
2787                 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2788                        lod2obd(d)->obd_name);
2789                 RETURN(-EINVAL);
2790         }
2791
2792         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2793                 __u32 id = comp_v1->lcm_entries[i].lcme_id;
2794                 __u32 flags = comp_v1->lcm_entries[i].lcme_flags;
2795                 __u32 mirror_flag = flags & LCME_MIRROR_FLAGS;
2796                 bool neg = flags & LCME_FL_NEG;
2797
2798                 if (flags & LCME_FL_INIT) {
2799                         if (changed)
2800                                 lod_striping_free(env, lo);
2801                         RETURN(-EINVAL);
2802                 }
2803
2804                 flags &= ~(LCME_MIRROR_FLAGS | LCME_FL_NEG);
2805                 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2806                         lod_comp = &lo->ldo_comp_entries[j];
2807
2808                         /* lfs only put one flag in each entry */
2809                         if ((flags && id != lod_comp->llc_id) ||
2810                             (mirror_flag && mirror_id_of(id) !=
2811                                             mirror_id_of(lod_comp->llc_id)))
2812                                 continue;
2813
2814                         if (neg) {
2815                                 if (flags)
2816                                         lod_comp->llc_flags &= ~flags;
2817                                 if (mirror_flag)
2818                                         lod_comp->llc_flags &= ~mirror_flag;
2819                         } else {
2820                                 if (flags)
2821                                         lod_comp->llc_flags |= flags;
2822                                 if (mirror_flag) {
2823                                         lod_comp->llc_flags |= mirror_flag;
2824                                         if (mirror_flag & LCME_FL_NOSYNC)
2825                                                 lod_comp->llc_timestamp =
2826                                                        ktime_get_real_seconds();
2827                                 }
2828                         }
2829                         changed = true;
2830                 }
2831         }
2832
2833         if (!changed) {
2834                 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2835                        lod2obd(d)->obd_name);
2836                 RETURN(-EINVAL);
2837         }
2838
2839         lod_obj_inc_layout_gen(lo);
2840
2841         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2842         rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), &info->lti_buf,
2843                                        XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
2844         RETURN(rc);
2845 }
2846
2847 /**
2848  * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2849  * and the xattr value is a unique component ID or a special lcme_id.
2850  *
2851  * \param[in] env       execution environment
2852  * \param[in] dt        dt_object to be operated on
2853  * \param[in] buf       buffer contains component ID or lcme_id
2854  * \parem[in] th        thandle
2855  *
2856  * \retval      0 on success
2857  * \retval      negative errno on failure
2858  */
2859 static int lod_declare_layout_del(const struct lu_env *env,
2860                                   struct dt_object *dt,
2861                                   const struct lu_buf *buf,
2862                                   struct thandle *th)
2863 {
2864         struct lod_thread_info  *info = lod_env_info(env);
2865         struct dt_object *next = dt_object_child(dt);
2866         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2867         struct lod_object *lo = lod_dt_obj(dt);
2868         struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2869         struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2870         __u32 magic, id, flags, neg_flags = 0;
2871         int rc, i, j, left;
2872         ENTRY;
2873
2874         LASSERT(lo->ldo_is_composite);
2875
2876         if (lo->ldo_flr_state != LCM_FL_NONE)
2877                 RETURN(-EBUSY);
2878
2879         magic = comp_v1->lcm_magic;
2880         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2881                 lustre_swab_lov_comp_md_v1(comp_v1);
2882                 magic = comp_v1->lcm_magic;
2883         }
2884
2885         if (magic != LOV_USER_MAGIC_COMP_V1)
2886                 RETURN(-EINVAL);
2887
2888         id = comp_v1->lcm_entries[0].lcme_id;
2889         flags = comp_v1->lcm_entries[0].lcme_flags;
2890
2891         if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
2892                 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
2893                        lod2obd(d)->obd_name, id, flags);
2894                 RETURN(-EINVAL);
2895         }
2896
2897         if (id != LCME_ID_INVAL && flags != 0) {
2898                 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
2899                        lod2obd(d)->obd_name);
2900                 RETURN(-EINVAL);
2901         }
2902
2903         if (id == LCME_ID_INVAL && !flags) {
2904                 CDEBUG(D_LAYOUT, "%s: no id or flags specified.\n",
2905                        lod2obd(d)->obd_name);
2906                 RETURN(-EINVAL);
2907         }
2908
2909         if (flags & LCME_FL_NEG) {
2910                 neg_flags = flags & ~LCME_FL_NEG;
2911                 flags = 0;
2912         }
2913
2914         left = lo->ldo_comp_cnt;
2915         if (left <= 0)
2916                 RETURN(-EINVAL);
2917
2918         for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
2919                 struct lod_layout_component *lod_comp;
2920
2921                 lod_comp = &lo->ldo_comp_entries[i];
2922
2923                 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
2924                         continue;
2925                 else if (flags && !(flags & lod_comp->llc_flags))
2926                         continue;
2927                 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
2928                         continue;
2929
2930                 if (left != (i + 1)) {
2931                         CDEBUG(D_LAYOUT, "%s: this deletion will create "
2932                                "a hole.\n", lod2obd(d)->obd_name);
2933                         RETURN(-EINVAL);
2934                 }
2935                 left--;
2936
2937                 /* Mark the component as deleted */
2938                 lod_comp->llc_id = LCME_ID_INVAL;
2939
2940                 /* Not instantiated component */
2941                 if (lod_comp->llc_stripe == NULL)
2942                         continue;
2943
2944                 LASSERT(lod_comp->llc_stripe_count > 0);
2945                 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
2946                         struct dt_object *obj = lod_comp->llc_stripe[j];
2947
2948                         if (obj == NULL)
2949                                 continue;
2950                         rc = lod_sub_declare_destroy(env, obj, th);
2951                         if (rc)
2952                                 RETURN(rc);
2953                 }
2954         }
2955
2956         LASSERTF(left >= 0, "left = %d\n", left);
2957         if (left == lo->ldo_comp_cnt) {
2958                 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
2959                        lod2obd(d)->obd_name, id);
2960                 RETURN(-EINVAL);
2961         }
2962
2963         memset(attr, 0, sizeof(*attr));
2964         attr->la_valid = LA_SIZE;
2965         rc = lod_sub_declare_attr_set(env, next, attr, th);
2966         if (rc)
2967                 RETURN(rc);
2968
2969         if (left > 0) {
2970                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2971                 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2972                                                XATTR_NAME_LOV, 0, th);
2973         } else {
2974                 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
2975         }
2976
2977         RETURN(rc);
2978 }
2979
2980 /**
2981  * Declare layout add/set/del operations issued by special xattr names:
2982  *
2983  * XATTR_LUSTRE_LOV.add         add component(s) to existing file
2984  * XATTR_LUSTRE_LOV.del         delete component(s) from existing file
2985  * XATTR_LUSTRE_LOV.set.$field  set specified field of certain component(s)
2986  *
2987  * \param[in] env       execution environment
2988  * \param[in] dt        object
2989  * \param[in] name      name of xattr
2990  * \param[in] buf       lu_buf contains xattr value
2991  * \param[in] th        transaction handle
2992  *
2993  * \retval              0 on success
2994  * \retval              negative if failed
2995  */
2996 static int lod_declare_modify_layout(const struct lu_env *env,
2997                                      struct dt_object *dt,
2998                                      const char *name,
2999                                      const struct lu_buf *buf,
3000                                      struct thandle *th)
3001 {
3002         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3003         struct lod_object *lo = lod_dt_obj(dt);
3004         char *op;
3005         int rc, len = strlen(XATTR_LUSTRE_LOV);
3006         ENTRY;
3007
3008         LASSERT(dt_object_exists(dt));
3009
3010         if (strlen(name) <= len || name[len] != '.') {
3011                 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
3012                        lod2obd(d)->obd_name, name);
3013                 RETURN(-EINVAL);
3014         }
3015         len++;
3016
3017         rc = lod_striping_load(env, lo);
3018         if (rc)
3019                 GOTO(unlock, rc);
3020
3021         /* the layout to be modified must be a composite layout */
3022         if (!lo->ldo_is_composite) {
3023                 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
3024                        lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3025                 GOTO(unlock, rc = -EINVAL);
3026         }
3027
3028         op = (char *)name + len;
3029         if (strcmp(op, "add") == 0) {
3030                 rc = lod_declare_layout_add(env, dt, buf, th);
3031         } else if (strcmp(op, "del") == 0) {
3032                 rc = lod_declare_layout_del(env, dt, buf, th);
3033         } else if (strncmp(op, "set", strlen("set")) == 0) {
3034                 rc = lod_declare_layout_set(env, dt, op, buf, th);
3035         } else  {
3036                 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
3037                        lod2obd(d)->obd_name, name);
3038                 GOTO(unlock, rc = -ENOTSUPP);
3039         }
3040 unlock:
3041         if (rc)
3042                 lod_striping_free(env, lo);
3043
3044         RETURN(rc);
3045 }
3046
3047 /**
3048  * Convert a plain file lov_mds_md to a composite layout.
3049  *
3050  * \param[in,out] info  the thread info::lti_ea_store buffer contains little
3051  *                      endian plain file layout
3052  *
3053  * \retval              0 on success, <0 on failure
3054  */
3055 static int lod_layout_convert(struct lod_thread_info *info)
3056 {
3057         struct lov_mds_md *lmm = info->lti_ea_store;
3058         struct lov_mds_md *lmm_save;
3059         struct lov_comp_md_v1 *lcm;
3060         struct lov_comp_md_entry_v1 *lcme;
3061         size_t size;
3062         __u32 blob_size;
3063         int rc = 0;
3064         ENTRY;
3065
3066         /* realloc buffer to a composite layout which contains one component */
3067         blob_size = lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
3068                                     le32_to_cpu(lmm->lmm_magic));
3069         size = sizeof(*lcm) + sizeof(*lcme) + blob_size;
3070
3071         OBD_ALLOC_LARGE(lmm_save, blob_size);
3072         if (!lmm_save)
3073                 GOTO(out, rc = -ENOMEM);
3074
3075         memcpy(lmm_save, lmm, blob_size);
3076
3077         if (info->lti_ea_store_size < size) {
3078                 rc = lod_ea_store_resize(info, size);
3079                 if (rc)
3080                         GOTO(out, rc);
3081         }
3082
3083         lcm = info->lti_ea_store;
3084         lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
3085         lcm->lcm_size = cpu_to_le32(size);
3086         lcm->lcm_layout_gen = cpu_to_le32(le16_to_cpu(
3087                                                 lmm_save->lmm_layout_gen));
3088         lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
3089         lcm->lcm_entry_count = cpu_to_le16(1);
3090         lcm->lcm_mirror_count = 0;
3091
3092         lcme = &lcm->lcm_entries[0];
3093         lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
3094         lcme->lcme_extent.e_start = 0;
3095         lcme->lcme_extent.e_end = cpu_to_le64(OBD_OBJECT_EOF);
3096         lcme->lcme_offset = cpu_to_le32(sizeof(*lcm) + sizeof(*lcme));
3097         lcme->lcme_size = cpu_to_le32(blob_size);
3098
3099         memcpy((char *)lcm + lcme->lcme_offset, (char *)lmm_save, blob_size);
3100
3101         EXIT;
3102 out:
3103         if (lmm_save)
3104                 OBD_FREE_LARGE(lmm_save, blob_size);
3105         return rc;
3106 }
3107
3108 /**
3109  * Merge layouts to form a mirrored file.
3110  */
3111 static int lod_declare_layout_merge(const struct lu_env *env,
3112                 struct dt_object *dt, const struct lu_buf *mbuf,
3113                 struct thandle *th)
3114 {
3115         struct lod_thread_info  *info = lod_env_info(env);
3116         struct lu_buf           *buf = &info->lti_buf;
3117         struct lod_object       *lo = lod_dt_obj(dt);
3118         struct lov_comp_md_v1   *lcm;
3119         struct lov_comp_md_v1   *cur_lcm;
3120         struct lov_comp_md_v1   *merge_lcm;
3121         struct lov_comp_md_entry_v1     *lcme;
3122         size_t size = 0;
3123         size_t offset;
3124         __u16 cur_entry_count;
3125         __u16 merge_entry_count;
3126         __u32 id = 0;
3127         __u16 mirror_id = 0;
3128         __u32 mirror_count;
3129         int     rc, i;
3130         ENTRY;
3131
3132         merge_lcm = mbuf->lb_buf;
3133         if (mbuf->lb_len < sizeof(*merge_lcm))
3134                 RETURN(-EINVAL);
3135
3136         /* must be an existing layout from disk */
3137         if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
3138                 RETURN(-EINVAL);
3139
3140         merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
3141
3142         /* do not allow to merge two mirrored files */
3143         if (le16_to_cpu(merge_lcm->lcm_mirror_count))
3144                 RETURN(-EBUSY);
3145
3146         /* verify the target buffer */
3147         rc = lod_get_lov_ea(env, lo);
3148         if (rc <= 0)
3149                 RETURN(rc ? : -ENODATA);
3150
3151         cur_lcm = info->lti_ea_store;
3152         switch (le32_to_cpu(cur_lcm->lcm_magic)) {
3153         case LOV_MAGIC_V1:
3154         case LOV_MAGIC_V3:
3155                 rc = lod_layout_convert(info);
3156                 break;
3157         case LOV_MAGIC_COMP_V1:
3158                 rc = 0;
3159                 break;
3160         default:
3161                 rc = -EINVAL;
3162         }
3163         if (rc)
3164                 RETURN(rc);
3165
3166         /* info->lti_ea_store could be reallocated in lod_layout_convert() */
3167         cur_lcm = info->lti_ea_store;
3168         cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
3169
3170         /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
3171         mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
3172         if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
3173                 RETURN(-ERANGE);
3174
3175         /* size of new layout */
3176         size = le32_to_cpu(cur_lcm->lcm_size) +
3177                le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
3178
3179         memset(buf, 0, sizeof(*buf));
3180         lu_buf_alloc(buf, size);
3181         if (buf->lb_buf == NULL)
3182                 RETURN(-ENOMEM);
3183
3184         lcm = buf->lb_buf;
3185         memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
3186
3187         offset = sizeof(*lcm) +
3188                  sizeof(*lcme) * (cur_entry_count + merge_entry_count);
3189         for (i = 0; i < cur_entry_count; i++) {
3190                 struct lov_comp_md_entry_v1 *cur_lcme;
3191
3192                 lcme = &lcm->lcm_entries[i];
3193                 cur_lcme = &cur_lcm->lcm_entries[i];
3194
3195                 lcme->lcme_offset = cpu_to_le32(offset);
3196                 memcpy((char *)lcm + offset,
3197                        (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
3198                        le32_to_cpu(lcme->lcme_size));
3199
3200                 offset += le32_to_cpu(lcme->lcme_size);
3201
3202                 if (mirror_count == 1 &&
3203                     mirror_id_of(le32_to_cpu(lcme->lcme_id)) == 0) {
3204                         /* Add mirror from a non-flr file, create new mirror ID.
3205                          * Otherwise, keep existing mirror's component ID, used
3206                          * for mirror extension.
3207                          */
3208                         id = pflr_id(1, i + 1);
3209                         lcme->lcme_id = cpu_to_le32(id);
3210                 }
3211
3212                 id = MAX(le32_to_cpu(lcme->lcme_id), id);
3213         }
3214
3215         mirror_id = mirror_id_of(id) + 1;
3216         for (i = 0; i < merge_entry_count; i++) {
3217                 struct lov_comp_md_entry_v1 *merge_lcme;
3218
3219                 merge_lcme = &merge_lcm->lcm_entries[i];
3220                 lcme = &lcm->lcm_entries[cur_entry_count + i];
3221
3222                 *lcme = *merge_lcme;
3223                 lcme->lcme_offset = cpu_to_le32(of