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