Whamcloud - gitweb
LU-16518 misc: fix clang build errors
[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 const 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_index_operations::dio_lookup
371  *
372  * Used with striped directories.
373  *
374  * \see dt_index_operations::dio_lookup() in the API description for details.
375  */
376 static int lod_striped_lookup(const struct lu_env *env, struct dt_object *dt,
377                       struct dt_rec *rec, const struct dt_key *key)
378 {
379         struct lod_object *lo = lod_dt_obj(dt);
380         struct dt_object *next;
381         const char *name = (const char *)key;
382
383         LASSERT(lo->ldo_dir_stripe_count > 0);
384
385         if (strcmp(name, dot) == 0) {
386                 struct lu_fid *fid = (struct lu_fid *)rec;
387
388                 *fid = *lod_object_fid(lo);
389                 return 1;
390         }
391
392         if (strcmp(name, dotdot) == 0) {
393                 next = dt_object_child(dt);
394         } else {
395                 int index;
396
397                 index = __lmv_name_to_stripe_index(lo->ldo_dir_hash_type,
398                                                    lo->ldo_dir_stripe_count,
399                                                    lo->ldo_dir_migrate_hash,
400                                                    lo->ldo_dir_migrate_offset,
401                                                    name, strlen(name), true);
402                 if (index < 0)
403                         return index;
404
405                 next = lo->ldo_stripe[index];
406                 if (!next || !dt_object_exists(next))
407                         return -ENODEV;
408         }
409
410         return next->do_index_ops->dio_lookup(env, next, rec, key);
411 }
412
413 /**
414  * Implementation of dt_it_ops::init.
415  *
416  * Used with striped objects. Internally just initializes the iterator
417  * on the first stripe.
418  *
419  * \see dt_it_ops::init() in the API description for details.
420  */
421 static struct dt_it *lod_striped_it_init(const struct lu_env *env,
422                                          struct dt_object *dt, __u32 attr)
423 {
424         struct lod_object *lo = lod_dt_obj(dt);
425         struct dt_object *next;
426         struct lod_it *it = &lod_env_info(env)->lti_it;
427         struct dt_it *it_next;
428         __u16 index = 0;
429
430         LASSERT(lo->ldo_dir_stripe_count > 0);
431
432         do {
433                 next = lo->ldo_stripe[index];
434                 if (next && dt_object_exists(next))
435                         break;
436         } while (++index < lo->ldo_dir_stripe_count);
437
438         /* no valid stripe */
439         if (!next || !dt_object_exists(next))
440                 return ERR_PTR(-ENODEV);
441
442         LASSERT(next->do_index_ops != NULL);
443
444         it_next = next->do_index_ops->dio_it.init(env, next, attr);
445         if (IS_ERR(it_next))
446                 return it_next;
447
448         /* currently we do not use more than one iterator per thread
449          * so we store it in thread info. if at some point we need
450          * more active iterators in a single thread, we can allocate
451          * additional ones */
452         LASSERT(it->lit_obj == NULL);
453
454         it->lit_stripe_index = index;
455         it->lit_attr = attr;
456         it->lit_it = it_next;
457         it->lit_obj = dt;
458
459         return (struct dt_it *)it;
460 }
461
462 #define LOD_CHECK_STRIPED_IT(env, it, lo)                               \
463 do {                                                                    \
464         LASSERT((it)->lit_obj != NULL);                                 \
465         LASSERT((it)->lit_it != NULL);                                  \
466         LASSERT((lo)->ldo_dir_stripe_count > 0);                        \
467         LASSERT((it)->lit_stripe_index < (lo)->ldo_dir_stripe_count);   \
468 } while (0)
469
470 /**
471  * Implementation of dt_it_ops::fini.
472  *
473  * Used with striped objects.
474  *
475  * \see dt_it_ops::fini() in the API description for details.
476  */
477 static void lod_striped_it_fini(const struct lu_env *env, struct dt_it *di)
478 {
479         struct lod_it           *it = (struct lod_it *)di;
480         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
481         struct dt_object        *next;
482
483         /* If lit_it == NULL, then it means the sub_it has been finished,
484          * which only happens in failure cases, see lod_striped_it_next() */
485         if (it->lit_it != NULL) {
486                 LOD_CHECK_STRIPED_IT(env, it, lo);
487
488                 next = lo->ldo_stripe[it->lit_stripe_index];
489                 if (next) {
490                         LASSERT(next->do_index_ops != NULL);
491                         next->do_index_ops->dio_it.fini(env, it->lit_it);
492                 }
493         }
494
495         /* the iterator not in use any more */
496         it->lit_obj = NULL;
497         it->lit_it = NULL;
498         it->lit_stripe_index = 0;
499 }
500
501 /**
502  * Implementation of dt_it_ops::get.
503  *
504  * Right now it's not used widely, only to reset the iterator to the
505  * initial position. It should be possible to implement a full version
506  * which chooses a correct stripe to be able to position with any key.
507  *
508  * \see dt_it_ops::get() in the API description for details.
509  */
510 static int lod_striped_it_get(const struct lu_env *env, struct dt_it *di,
511                               const struct dt_key *key)
512 {
513         const struct lod_it *it = (const struct lod_it *)di;
514         struct lod_object *lo = lod_dt_obj(it->lit_obj);
515         struct dt_object *next;
516
517         LOD_CHECK_STRIPED_IT(env, it, lo);
518
519         next = lo->ldo_stripe[it->lit_stripe_index];
520         LASSERT(next != NULL);
521         LASSERT(dt_object_exists(next));
522         LASSERT(next->do_index_ops != NULL);
523
524         return next->do_index_ops->dio_it.get(env, it->lit_it, key);
525 }
526
527 /**
528  * Implementation of dt_it_ops::put.
529  *
530  * Used with striped objects.
531  *
532  * \see dt_it_ops::put() in the API description for details.
533  */
534 static void lod_striped_it_put(const struct lu_env *env, struct dt_it *di)
535 {
536         struct lod_it *it = (struct lod_it *)di;
537         struct lod_object *lo = lod_dt_obj(it->lit_obj);
538         struct dt_object *next;
539
540         /*
541          * If lit_it == NULL, then it means the sub_it has been finished,
542          * which only happens in failure cases, see lod_striped_it_next()
543          */
544         if (!it->lit_it)
545                 return;
546
547         LOD_CHECK_STRIPED_IT(env, it, lo);
548
549         next = lo->ldo_stripe[it->lit_stripe_index];
550         LASSERT(next != NULL);
551         LASSERT(next->do_index_ops != NULL);
552
553         return next->do_index_ops->dio_it.put(env, it->lit_it);
554 }
555
556 /**
557  * Implementation of dt_it_ops::next.
558  *
559  * Used with striped objects. When the end of the current stripe is
560  * reached, the method takes the next stripe's iterator.
561  *
562  * \see dt_it_ops::next() in the API description for details.
563  */
564 static int lod_striped_it_next(const struct lu_env *env, struct dt_it *di)
565 {
566         struct lod_it *it = (struct lod_it *)di;
567         struct lod_object *lo = lod_dt_obj(it->lit_obj);
568         struct dt_object *next;
569         struct dt_it *it_next;
570         __u32 index;
571         int rc;
572
573         ENTRY;
574
575         LOD_CHECK_STRIPED_IT(env, it, lo);
576
577         next = lo->ldo_stripe[it->lit_stripe_index];
578         LASSERT(next != NULL);
579         LASSERT(dt_object_exists(next));
580         LASSERT(next->do_index_ops != NULL);
581 again:
582         rc = next->do_index_ops->dio_it.next(env, it->lit_it);
583         if (rc < 0)
584                 RETURN(rc);
585
586         if (rc == 0 && it->lit_stripe_index == 0)
587                 RETURN(rc);
588
589         if (rc == 0 && it->lit_stripe_index > 0) {
590                 struct lu_dirent *ent;
591
592                 ent = (struct lu_dirent *)lod_env_info(env)->lti_key;
593
594                 rc = next->do_index_ops->dio_it.rec(env, it->lit_it,
595                                                     (struct dt_rec *)ent,
596                                                     it->lit_attr);
597                 if (rc != 0)
598                         RETURN(rc);
599
600                 /* skip . and .. for slave stripe */
601                 if ((strncmp(ent->lde_name, ".",
602                              le16_to_cpu(ent->lde_namelen)) == 0 &&
603                      le16_to_cpu(ent->lde_namelen) == 1) ||
604                     (strncmp(ent->lde_name, "..",
605                              le16_to_cpu(ent->lde_namelen)) == 0 &&
606                      le16_to_cpu(ent->lde_namelen) == 2))
607                         goto again;
608
609                 RETURN(rc);
610         }
611
612         next->do_index_ops->dio_it.put(env, it->lit_it);
613         next->do_index_ops->dio_it.fini(env, it->lit_it);
614         it->lit_it = NULL;
615
616         /* go to next stripe */
617         index = it->lit_stripe_index;
618         while (++index < lo->ldo_dir_stripe_count) {
619                 next = lo->ldo_stripe[index];
620                 if (!next)
621                         continue;
622
623                 if (!dt_object_exists(next))
624                         continue;
625
626                 rc = next->do_ops->do_index_try(env, next,
627                                                 &dt_directory_features);
628                 if (rc != 0)
629                         RETURN(rc);
630
631                 LASSERT(next->do_index_ops != NULL);
632
633                 it_next = next->do_index_ops->dio_it.init(env, next,
634                                                           it->lit_attr);
635                 if (IS_ERR(it_next))
636                         RETURN(PTR_ERR(it_next));
637
638                 rc = next->do_index_ops->dio_it.get(env, it_next,
639                                                     (const struct dt_key *)"");
640                 if (rc <= 0)
641                         RETURN(rc == 0 ? -EIO : rc);
642
643                 it->lit_it = it_next;
644                 it->lit_stripe_index = index;
645                 goto again;
646
647         }
648
649         RETURN(1);
650 }
651
652 /**
653  * Implementation of dt_it_ops::key.
654  *
655  * Used with striped objects.
656  *
657  * \see dt_it_ops::key() in the API description for details.
658  */
659 static struct dt_key *lod_striped_it_key(const struct lu_env *env,
660                                          const struct dt_it *di)
661 {
662         const struct lod_it     *it = (const struct lod_it *)di;
663         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
664         struct dt_object        *next;
665
666         LOD_CHECK_STRIPED_IT(env, it, lo);
667
668         next = lo->ldo_stripe[it->lit_stripe_index];
669         LASSERT(next != NULL);
670         LASSERT(next->do_index_ops != NULL);
671
672         return next->do_index_ops->dio_it.key(env, it->lit_it);
673 }
674
675 /**
676  * Implementation of dt_it_ops::key_size.
677  *
678  * Used with striped objects.
679  *
680  * \see dt_it_ops::size() in the API description for details.
681  */
682 static int lod_striped_it_key_size(const struct lu_env *env,
683                                    const struct dt_it *di)
684 {
685         struct lod_it           *it = (struct lod_it *)di;
686         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
687         struct dt_object        *next;
688
689         LOD_CHECK_STRIPED_IT(env, it, lo);
690
691         next = lo->ldo_stripe[it->lit_stripe_index];
692         LASSERT(next != NULL);
693         LASSERT(next->do_index_ops != NULL);
694
695         return next->do_index_ops->dio_it.key_size(env, it->lit_it);
696 }
697
698 /**
699  * Implementation of dt_it_ops::rec.
700  *
701  * Used with striped objects.
702  *
703  * \see dt_it_ops::rec() in the API description for details.
704  */
705 static int lod_striped_it_rec(const struct lu_env *env, const struct dt_it *di,
706                               struct dt_rec *rec, __u32 attr)
707 {
708         const struct lod_it     *it = (const struct lod_it *)di;
709         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
710         struct dt_object        *next;
711
712         LOD_CHECK_STRIPED_IT(env, it, lo);
713
714         next = lo->ldo_stripe[it->lit_stripe_index];
715         LASSERT(next != NULL);
716         LASSERT(next->do_index_ops != NULL);
717
718         return next->do_index_ops->dio_it.rec(env, it->lit_it, rec, attr);
719 }
720
721 /**
722  * Implementation of dt_it_ops::rec_size.
723  *
724  * Used with striped objects.
725  *
726  * \see dt_it_ops::rec_size() in the API description for details.
727  */
728 static int lod_striped_it_rec_size(const struct lu_env *env,
729                                    const struct dt_it *di, __u32 attr)
730 {
731         struct lod_it           *it = (struct lod_it *)di;
732         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
733         struct dt_object        *next;
734
735         LOD_CHECK_STRIPED_IT(env, it, lo);
736
737         next = lo->ldo_stripe[it->lit_stripe_index];
738         LASSERT(next != NULL);
739         LASSERT(next->do_index_ops != NULL);
740
741         return next->do_index_ops->dio_it.rec_size(env, it->lit_it, attr);
742 }
743
744 /**
745  * Implementation of dt_it_ops::store.
746  *
747  * Used with striped objects.
748  *
749  * \see dt_it_ops::store() in the API description for details.
750  */
751 static __u64 lod_striped_it_store(const struct lu_env *env,
752                                   const struct dt_it *di)
753 {
754         const struct lod_it     *it = (const struct lod_it *)di;
755         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
756         struct dt_object        *next;
757
758         LOD_CHECK_STRIPED_IT(env, it, lo);
759
760         next = lo->ldo_stripe[it->lit_stripe_index];
761         LASSERT(next != NULL);
762         LASSERT(next->do_index_ops != NULL);
763
764         return next->do_index_ops->dio_it.store(env, it->lit_it);
765 }
766
767 /**
768  * Implementation of dt_it_ops::load.
769  *
770  * Used with striped objects.
771  *
772  * \see dt_it_ops::load() in the API description for details.
773  */
774 static int lod_striped_it_load(const struct lu_env *env,
775                                const struct dt_it *di, __u64 hash)
776 {
777         const struct lod_it     *it = (const struct lod_it *)di;
778         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
779         struct dt_object        *next;
780
781         LOD_CHECK_STRIPED_IT(env, it, lo);
782
783         next = lo->ldo_stripe[it->lit_stripe_index];
784         LASSERT(next != NULL);
785         LASSERT(next->do_index_ops != NULL);
786
787         return next->do_index_ops->dio_it.load(env, it->lit_it, hash);
788 }
789
790 static const struct dt_index_operations lod_striped_index_ops = {
791         .dio_lookup             = lod_striped_lookup,
792         .dio_declare_insert     = lod_declare_insert,
793         .dio_insert             = lod_insert,
794         .dio_declare_delete     = lod_declare_delete,
795         .dio_delete             = lod_delete,
796         .dio_it = {
797                 .init           = lod_striped_it_init,
798                 .fini           = lod_striped_it_fini,
799                 .get            = lod_striped_it_get,
800                 .put            = lod_striped_it_put,
801                 .next           = lod_striped_it_next,
802                 .key            = lod_striped_it_key,
803                 .key_size       = lod_striped_it_key_size,
804                 .rec            = lod_striped_it_rec,
805                 .rec_size       = lod_striped_it_rec_size,
806                 .store          = lod_striped_it_store,
807                 .load           = lod_striped_it_load,
808         }
809 };
810
811 /**
812  * Append the FID for each shard of the striped directory after the
813  * given LMV EA header.
814  *
815  * To simplify striped directory and the consistency verification,
816  * we only store the LMV EA header on disk, for both master object
817  * and slave objects. When someone wants to know the whole LMV EA,
818  * such as client readdir(), we can build the entrie LMV EA on the
819  * MDT side (in RAM) via iterating the sub-directory entries that
820  * are contained in the master object of the stripe directory.
821  *
822  * For the master object of the striped directroy, the valid name
823  * for each shard is composed of the ${shard_FID}:${shard_idx}.
824  *
825  * There may be holes in the LMV EA if some shards' name entries
826  * are corrupted or lost.
827  *
828  * \param[in] env       pointer to the thread context
829  * \param[in] lo        pointer to the master object of the striped directory
830  * \param[in] buf       pointer to the lu_buf which will hold the LMV EA
831  * \param[in] resize    whether re-allocate the buffer if it is not big enough
832  *
833  * \retval              positive size of the LMV EA
834  * \retval              0 for nothing to be loaded
835  * \retval              negative error number on failure
836  */
837 int lod_load_lmv_shards(const struct lu_env *env, struct lod_object *lo,
838                         struct lu_buf *buf, bool resize)
839 {
840         struct lu_dirent        *ent    =
841                         (struct lu_dirent *)lod_env_info(env)->lti_key;
842         struct lod_device       *lod    = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
843         struct dt_object        *obj    = dt_object_child(&lo->ldo_obj);
844         struct lmv_mds_md_v1    *lmv1   = buf->lb_buf;
845         struct dt_it            *it;
846         const struct dt_it_ops  *iops;
847         __u32                    stripes;
848         __u32                    magic  = le32_to_cpu(lmv1->lmv_magic);
849         size_t                   lmv1_size;
850         int                      rc;
851         ENTRY;
852
853         if (magic != LMV_MAGIC_V1)
854                 RETURN(0);
855
856         stripes = le32_to_cpu(lmv1->lmv_stripe_count);
857         if (stripes < 1)
858                 RETURN(0);
859
860         rc = lmv_mds_md_size(stripes, magic);
861         if (rc < 0)
862                 RETURN(rc);
863         lmv1_size = rc;
864         if (buf->lb_len < lmv1_size) {
865                 struct lu_buf tbuf;
866
867                 if (!resize)
868                         RETURN(-ERANGE);
869
870                 tbuf = *buf;
871                 buf->lb_buf = NULL;
872                 buf->lb_len = 0;
873                 lu_buf_alloc(buf, lmv1_size);
874                 lmv1 = buf->lb_buf;
875                 if (lmv1 == NULL)
876                         RETURN(-ENOMEM);
877
878                 memcpy(buf->lb_buf, tbuf.lb_buf, tbuf.lb_len);
879         }
880
881         if (unlikely(!dt_try_as_dir(env, obj, true)))
882                 RETURN(-ENOTDIR);
883
884         memset(&lmv1->lmv_stripe_fids[0], 0, stripes * sizeof(struct lu_fid));
885         iops = &obj->do_index_ops->dio_it;
886         it = iops->init(env, obj, LUDA_64BITHASH);
887         if (IS_ERR(it))
888                 RETURN(PTR_ERR(it));
889
890         rc = iops->load(env, it, 0);
891         if (rc == 0)
892                 rc = iops->next(env, it);
893         else if (rc > 0)
894                 rc = 0;
895
896         while (rc == 0) {
897                 char             name[FID_LEN + 2] = "";
898                 struct lu_fid    fid;
899                 __u32            index;
900                 int              len;
901
902                 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
903                 if (rc != 0)
904                         break;
905
906                 rc = -EIO;
907
908                 fid_le_to_cpu(&fid, &ent->lde_fid);
909                 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
910                 if (ent->lde_name[0] == '.') {
911                         if (ent->lde_namelen == 1)
912                                 goto next;
913
914                         if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
915                                 goto next;
916                 }
917
918                 len = scnprintf(name, sizeof(name),
919                                 DFID":", PFID(&ent->lde_fid));
920                 /* The ent->lde_name is composed of ${FID}:${index} */
921                 if (ent->lde_namelen < len + 1 ||
922                     memcmp(ent->lde_name, name, len) != 0) {
923                         CDEBUG_LIMIT(lod->lod_lmv_failout ? D_ERROR : D_INFO,
924                                      "%s: invalid shard name %.*s with the FID "DFID" for the striped directory "DFID", %s\n",
925                                      lod2obd(lod)->obd_name, ent->lde_namelen,
926                                      ent->lde_name, PFID(&fid),
927                                      PFID(lu_object_fid(&obj->do_lu)),
928                                      lod->lod_lmv_failout ? "failout" : "skip");
929
930                         if (lod->lod_lmv_failout)
931                                 break;
932
933                         goto next;
934                 }
935
936                 index = 0;
937                 do {
938                         if (ent->lde_name[len] < '0' ||
939                             ent->lde_name[len] > '9') {
940                                 CDEBUG_LIMIT(lod->lod_lmv_failout ?
941                                              D_ERROR : D_INFO,
942                                              "%s: invalid shard name %.*s with the FID "DFID" for the striped directory "DFID", %s\n",
943                                              lod2obd(lod)->obd_name,
944                                              ent->lde_namelen,
945                                              ent->lde_name, PFID(&fid),
946                                              PFID(lu_object_fid(&obj->do_lu)),
947                                              lod->lod_lmv_failout ?
948                                              "failout" : "skip");
949
950                                 if (lod->lod_lmv_failout)
951                                         break;
952
953                                 goto next;
954                         }
955
956                         index = index * 10 + ent->lde_name[len++] - '0';
957                 } while (len < ent->lde_namelen);
958
959                 if (len == ent->lde_namelen) {
960                         /* Out of LMV EA range. */
961                         if (index >= stripes) {
962                                 CERROR("%s: the shard %.*s for the striped "
963                                        "directory "DFID" is out of the known "
964                                        "LMV EA range [0 - %u], failout\n",
965                                        lod2obd(lod)->obd_name, ent->lde_namelen,
966                                        ent->lde_name,
967                                        PFID(lu_object_fid(&obj->do_lu)),
968                                        stripes - 1);
969
970                                 break;
971                         }
972
973                         /* The slot has been occupied. */
974                         if (!fid_is_zero(&lmv1->lmv_stripe_fids[index])) {
975                                 struct lu_fid fid0;
976
977                                 fid_le_to_cpu(&fid0,
978                                         &lmv1->lmv_stripe_fids[index]);
979                                 CERROR("%s: both the shard "DFID" and "DFID
980                                        " for the striped directory "DFID
981                                        " claim the same LMV EA slot at the "
982                                        "index %d, failout\n",
983                                        lod2obd(lod)->obd_name,
984                                        PFID(&fid0), PFID(&fid),
985                                        PFID(lu_object_fid(&obj->do_lu)), index);
986
987                                 break;
988                         }
989
990                         /* stored as LE mode */
991                         lmv1->lmv_stripe_fids[index] = ent->lde_fid;
992
993 next:
994                         rc = iops->next(env, it);
995                 }
996         }
997
998         iops->put(env, it);
999         iops->fini(env, it);
1000
1001         RETURN(rc > 0 ? lmv_mds_md_size(stripes, magic) : rc);
1002 }
1003
1004 /**
1005  * Implementation of dt_object_operations::do_index_try.
1006  *
1007  * \see dt_object_operations::do_index_try() in the API description for details.
1008  */
1009 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
1010                          const struct dt_index_features *feat)
1011 {
1012         struct lod_object       *lo = lod_dt_obj(dt);
1013         struct dt_object        *next = dt_object_child(dt);
1014         int                     rc;
1015         ENTRY;
1016
1017         LASSERT(next->do_ops);
1018         LASSERT(next->do_ops->do_index_try);
1019
1020         rc = lod_striping_load(env, lo);
1021         if (rc != 0)
1022                 RETURN(rc);
1023
1024         rc = next->do_ops->do_index_try(env, next, feat);
1025         if (rc != 0)
1026                 RETURN(rc);
1027
1028         if (lo->ldo_dir_stripe_count > 0) {
1029                 int i;
1030
1031                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1032                         if (!lo->ldo_stripe[i])
1033                                 continue;
1034                         if (!dt_object_exists(lo->ldo_stripe[i]))
1035                                 continue;
1036                         rc = lo->ldo_stripe[i]->do_ops->do_index_try(env,
1037                                                 lo->ldo_stripe[i], feat);
1038                         if (rc != 0)
1039                                 RETURN(rc);
1040                 }
1041                 dt->do_index_ops = &lod_striped_index_ops;
1042         } else {
1043                 dt->do_index_ops = &lod_index_ops;
1044         }
1045
1046         RETURN(rc);
1047 }
1048
1049 /**
1050  * Implementation of dt_object_operations::do_read_lock.
1051  *
1052  * \see dt_object_operations::do_read_lock() in the API description for details.
1053  */
1054 static void lod_read_lock(const struct lu_env *env, struct dt_object *dt,
1055                           unsigned role)
1056 {
1057         dt_read_lock(env, dt_object_child(dt), role);
1058 }
1059
1060 /**
1061  * Implementation of dt_object_operations::do_write_lock.
1062  *
1063  * \see dt_object_operations::do_write_lock() in the API description for
1064  * details.
1065  */
1066 static void lod_write_lock(const struct lu_env *env, struct dt_object *dt,
1067                            unsigned role)
1068 {
1069         dt_write_lock(env, dt_object_child(dt), role);
1070 }
1071
1072 /**
1073  * Implementation of dt_object_operations::do_read_unlock.
1074  *
1075  * \see dt_object_operations::do_read_unlock() in the API description for
1076  * details.
1077  */
1078 static void lod_read_unlock(const struct lu_env *env, struct dt_object *dt)
1079 {
1080         dt_read_unlock(env, dt_object_child(dt));
1081 }
1082
1083 /**
1084  * Implementation of dt_object_operations::do_write_unlock.
1085  *
1086  * \see dt_object_operations::do_write_unlock() in the API description for
1087  * details.
1088  */
1089 static void lod_write_unlock(const struct lu_env *env, struct dt_object *dt)
1090 {
1091         dt_write_unlock(env, dt_object_child(dt));
1092 }
1093
1094 /**
1095  * Implementation of dt_object_operations::do_write_locked.
1096  *
1097  * \see dt_object_operations::do_write_locked() in the API description for
1098  * details.
1099  */
1100 static int lod_write_locked(const struct lu_env *env, struct dt_object *dt)
1101 {
1102         return dt_write_locked(env, dt_object_child(dt));
1103 }
1104
1105 /**
1106  * Implementation of dt_object_operations::do_attr_get.
1107  *
1108  * \see dt_object_operations::do_attr_get() in the API description for details.
1109  */
1110 static int lod_attr_get(const struct lu_env *env,
1111                         struct dt_object *dt,
1112                         struct lu_attr *attr)
1113 {
1114         /* Note: for striped directory, client will merge attributes
1115          * from all of the sub-stripes see lmv_merge_attr(), and there
1116          * no MDD logic depend on directory nlink/size/time, so we can
1117          * always use master inode nlink and size for now. */
1118         return dt_attr_get(env, dt_object_child(dt), attr);
1119 }
1120
1121 void lod_adjust_stripe_size(struct lod_layout_component *comp,
1122                             __u32 def_stripe_size)
1123 {
1124         __u64 comp_end = comp->llc_extent.e_end;
1125
1126         /* Choose stripe size if not set. Note that default stripe size can't
1127          * be used as is, because it must be multiplier of given component end.
1128          *  - first check if default stripe size can be used
1129          *  - if not than select the lowest set bit from component end and use
1130          *    that value as stripe size
1131          */
1132         if (!comp->llc_stripe_size) {
1133                 if (comp_end == LUSTRE_EOF || !(comp_end % def_stripe_size))
1134                         comp->llc_stripe_size = def_stripe_size;
1135                 else
1136                         comp->llc_stripe_size = comp_end & ~(comp_end - 1);
1137         } else {
1138                 if (comp_end != LUSTRE_EOF &&
1139                     comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
1140                         CWARN("Component end %llu is not a multiple of min size %u\n",
1141                               comp_end, LOV_MIN_STRIPE_SIZE);
1142                         comp_end = round_up(comp_end, LOV_MIN_STRIPE_SIZE);
1143                 }
1144                 /* check stripe size is multiplier of comp_end */
1145                 if (comp_end != LUSTRE_EOF &&
1146                     comp_end != comp->llc_extent.e_start &&
1147                     comp_end % comp->llc_stripe_size) {
1148                         /* fix that even for defined stripe size but warn
1149                          * about the problem, that must not happen
1150                          */
1151                         CWARN("Component end %llu is not aligned by the stripe size %u\n",
1152                               comp_end, comp->llc_stripe_size);
1153                         comp->llc_stripe_size = comp_end & ~(comp_end - 1);
1154                 }
1155         }
1156 }
1157
1158 static inline void lod_adjust_stripe_info(struct lod_layout_component *comp,
1159                                           struct lov_desc *desc,
1160                                           int append_stripes)
1161 {
1162         if (comp->llc_pattern != LOV_PATTERN_MDT) {
1163                 if (append_stripes) {
1164                         comp->llc_stripe_count = append_stripes;
1165                 } else if (!comp->llc_stripe_count) {
1166                         comp->llc_stripe_count =
1167                                 desc->ld_default_stripe_count;
1168                 }
1169         }
1170
1171         lod_adjust_stripe_size(comp, desc->ld_default_stripe_size);
1172 }
1173
1174 int lod_obj_for_each_stripe(const struct lu_env *env, struct lod_object *lo,
1175                             struct thandle *th,
1176                             struct lod_obj_stripe_cb_data *data)
1177 {
1178         struct lod_layout_component *lod_comp;
1179         int i, j, rc = 0;
1180         ENTRY;
1181
1182         mutex_lock(&lo->ldo_layout_mutex);
1183         for (i = 0; i < lo->ldo_comp_cnt; i++) {
1184                 lod_comp = &lo->ldo_comp_entries[i];
1185
1186                 if (lod_comp->llc_stripe == NULL)
1187                         continue;
1188
1189                 /* has stripe but not inited yet, this component has been
1190                  * declared to be created, but hasn't created yet.
1191                  */
1192                 if (!lod_comp_inited(lod_comp))
1193                         continue;
1194
1195                 if (data->locd_comp_skip_cb &&
1196                     data->locd_comp_skip_cb(env, lo, i, data))
1197                         continue;
1198
1199                 if (data->locd_comp_cb) {
1200                         rc = data->locd_comp_cb(env, lo, i, data);
1201                         if (rc)
1202                                 GOTO(unlock, rc);
1203                 }
1204
1205                 /* could used just to do sth about component, not each
1206                  * stripes
1207                  */
1208                 if (!data->locd_stripe_cb)
1209                         continue;
1210
1211                 LASSERT(lod_comp->llc_stripe_count > 0);
1212                 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
1213                         struct dt_object *dt = lod_comp->llc_stripe[j];
1214
1215                         if (dt == NULL)
1216                                 continue;
1217                         rc = data->locd_stripe_cb(env, lo, dt, th, i, j, data);
1218                         if (rc != 0)
1219                                 GOTO(unlock, rc);
1220                 }
1221         }
1222 unlock:
1223         mutex_unlock(&lo->ldo_layout_mutex);
1224         RETURN(rc);
1225 }
1226
1227 static inline int
1228 lod_obj_stripe_attr_set_cb(const struct lu_env *env, struct lod_object *lo,
1229                            struct dt_object *dt, struct thandle *th,
1230                            int comp_idx, int stripe_idx,
1231                            struct lod_obj_stripe_cb_data *data)
1232 {
1233         if (data->locd_declare)
1234                 return lod_sub_declare_attr_set(env, dt, data->locd_attr, th);
1235
1236         if (data->locd_attr->la_valid & LA_LAYOUT_VERSION) {
1237                 CDEBUG(D_LAYOUT, DFID": set layout version: %u, comp_idx: %d\n",
1238                        PFID(lu_object_fid(&dt->do_lu)),
1239                        data->locd_attr->la_layout_version, comp_idx);
1240         }
1241
1242         return lod_sub_attr_set(env, dt, data->locd_attr, th);
1243 }
1244
1245 /**
1246  * Implementation of dt_object_operations::do_declare_attr_set.
1247  *
1248  * If the object is striped, then apply the changes to all the stripes.
1249  *
1250  * \see dt_object_operations::do_declare_attr_set() in the API description
1251  * for details.
1252  */
1253 static int lod_declare_attr_set(const struct lu_env *env,
1254                                 struct dt_object *dt,
1255                                 const struct lu_attr *attr,
1256                                 struct thandle *th)
1257 {
1258         struct dt_object  *next = dt_object_child(dt);
1259         struct lod_object *lo = lod_dt_obj(dt);
1260         int                rc, i;
1261         ENTRY;
1262
1263         /*
1264          * declare setattr on the local object
1265          */
1266         rc = lod_sub_declare_attr_set(env, next, attr, th);
1267         if (rc)
1268                 RETURN(rc);
1269
1270         /* osp_declare_attr_set() ignores all attributes other than
1271          * UID, GID, PROJID, and size, and osp_attr_set() ignores all
1272          * but UID, GID and PROJID. Declaration of size attr setting
1273          * happens through lod_declare_init_size(), and not through
1274          * this function. Therefore we need not load striping unless
1275          * ownership is changing.  This should save memory and (we hope)
1276          * speed up rename().
1277          */
1278         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1279                 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1280                         RETURN(rc);
1281
1282                 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1283                         RETURN(0);
1284         } else {
1285                 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID | LA_MODE |
1286                                         LA_ATIME | LA_MTIME | LA_CTIME |
1287                                         LA_FLAGS)))
1288                         RETURN(rc);
1289         }
1290         /*
1291          * load striping information, notice we don't do this when object
1292          * is being initialized as we don't need this information till
1293          * few specific cases like destroy, chown
1294          */
1295         rc = lod_striping_load(env, lo);
1296         if (rc)
1297                 RETURN(rc);
1298
1299         if (!lod_obj_is_striped(dt))
1300                 RETURN(0);
1301
1302         /*
1303          * if object is striped declare changes on the stripes
1304          */
1305         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1306                 LASSERT(lo->ldo_stripe);
1307                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1308                         if (lo->ldo_stripe[i] == NULL)
1309                                 continue;
1310                         if (!dt_object_exists(lo->ldo_stripe[i]))
1311                                 continue;
1312                         rc = lod_sub_declare_attr_set(env, lo->ldo_stripe[i],
1313                                                       attr, th);
1314                         if (rc != 0)
1315                                 RETURN(rc);
1316                 }
1317         } else {
1318                 struct lod_obj_stripe_cb_data data = { { 0 } };
1319
1320                 data.locd_attr = attr;
1321                 data.locd_declare = true;
1322                 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1323                 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1324         }
1325
1326         if (rc)
1327                 RETURN(rc);
1328
1329         if (!dt_object_exists(next) || dt_object_remote(next) ||
1330             !S_ISREG(attr->la_mode))
1331                 RETURN(0);
1332
1333         if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1334                 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
1335                 RETURN(rc);
1336         }
1337
1338         if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1339             CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1340                 struct lod_thread_info *info = lod_env_info(env);
1341                 struct lu_buf *buf = &info->lti_buf;
1342
1343                 buf->lb_buf = info->lti_ea_store;
1344                 buf->lb_len = info->lti_ea_store_size;
1345                 rc = lod_sub_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1346                                                LU_XATTR_REPLACE, th);
1347         }
1348
1349         RETURN(rc);
1350 }
1351
1352 /**
1353  * Implementation of dt_object_operations::do_attr_set.
1354  *
1355  * If the object is striped, then apply the changes to all or subset of
1356  * the stripes depending on the object type and specific attributes.
1357  *
1358  * \see dt_object_operations::do_attr_set() in the API description for details.
1359  */
1360 static int lod_attr_set(const struct lu_env *env,
1361                         struct dt_object *dt,
1362                         const struct lu_attr *attr,
1363                         struct thandle *th)
1364 {
1365         struct dt_object        *next = dt_object_child(dt);
1366         struct lod_object       *lo = lod_dt_obj(dt);
1367         int                     rc, i;
1368         ENTRY;
1369
1370         /*
1371          * apply changes to the local object
1372          */
1373         rc = lod_sub_attr_set(env, next, attr, th);
1374         if (rc)
1375                 RETURN(rc);
1376
1377         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1378                 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1379                         RETURN(rc);
1380
1381                 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1382                         RETURN(0);
1383         } else {
1384                 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE | LA_PROJID |
1385                                         LA_ATIME | LA_MTIME | LA_CTIME |
1386                                         LA_FLAGS)))
1387                         RETURN(rc);
1388         }
1389
1390         /* FIXME: a tricky case in the code path of mdd_layout_change():
1391          * the in-memory striping information has been freed in lod_xattr_set()
1392          * due to layout change. It has to load stripe here again. It only
1393          * changes flags of layout so declare_attr_set() is still accurate */
1394         rc = lod_striping_load(env, lo);
1395         if (rc)
1396                 RETURN(rc);
1397
1398         if (!lod_obj_is_striped(dt))
1399                 RETURN(0);
1400
1401         /*
1402          * if object is striped, apply changes to all the stripes
1403          */
1404         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1405                 LASSERT(lo->ldo_stripe);
1406                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1407                         if (unlikely(lo->ldo_stripe[i] == NULL))
1408                                 continue;
1409
1410                         if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1411                                 continue;
1412
1413                         rc = lod_sub_attr_set(env, lo->ldo_stripe[i], attr, th);
1414                         if (rc != 0)
1415                                 break;
1416                 }
1417         } else {
1418                 struct lod_obj_stripe_cb_data data = { { 0 } };
1419
1420                 data.locd_attr = attr;
1421                 data.locd_declare = false;
1422                 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1423                 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1424         }
1425
1426         if (rc)
1427                 RETURN(rc);
1428
1429         if (!dt_object_exists(next) || dt_object_remote(next) ||
1430             !S_ISREG(attr->la_mode))
1431                 RETURN(0);
1432
1433         if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1434                 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
1435                 RETURN(rc);
1436         }
1437
1438         if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1439                 struct lod_thread_info *info = lod_env_info(env);
1440                 struct lu_buf *buf = &info->lti_buf;
1441                 struct ost_id *oi = &info->lti_ostid;
1442                 struct lu_fid *fid = &info->lti_fid;
1443                 struct lov_mds_md_v1 *lmm;
1444                 struct lov_ost_data_v1 *objs;
1445                 __u32 magic;
1446
1447                 rc = lod_get_lov_ea(env, lo);
1448                 if (rc <= 0)
1449                         RETURN(rc);
1450
1451                 buf->lb_buf = info->lti_ea_store;
1452                 buf->lb_len = info->lti_ea_store_size;
1453                 lmm = info->lti_ea_store;
1454                 magic = le32_to_cpu(lmm->lmm_magic);
1455                 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
1456                         struct lov_comp_md_v1 *lcm = buf->lb_buf;
1457                         struct lov_comp_md_entry_v1 *lcme =
1458                                                 &lcm->lcm_entries[0];
1459
1460                         lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1461                         magic = le32_to_cpu(lmm->lmm_magic);
1462                 }
1463
1464                 if (magic == LOV_MAGIC_V1)
1465                         objs = &(lmm->lmm_objects[0]);
1466                 else
1467                         objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1468                 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1469                 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1470                 fid->f_oid--;
1471                 fid_to_ostid(fid, oi);
1472                 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1473
1474                 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1475                                        LU_XATTR_REPLACE, th);
1476         } else if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1477                 struct lod_thread_info *info = lod_env_info(env);
1478                 struct lu_buf *buf = &info->lti_buf;
1479                 struct lov_comp_md_v1 *lcm;
1480                 struct lov_comp_md_entry_v1 *lcme;
1481
1482                 rc = lod_get_lov_ea(env, lo);
1483                 if (rc <= 0)
1484                         RETURN(rc);
1485
1486                 buf->lb_buf = info->lti_ea_store;
1487                 buf->lb_len = info->lti_ea_store_size;
1488                 lcm = buf->lb_buf;
1489                 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1 &&
1490                     le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_SEL)
1491                         RETURN(-EINVAL);
1492
1493                 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1494                 lcme = &lcm->lcm_entries[0];
1495                 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1496                 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1497
1498                 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1499                                        LU_XATTR_REPLACE, th);
1500         }
1501
1502         RETURN(rc);
1503 }
1504
1505 /**
1506  * Implementation of dt_object_operations::do_xattr_get.
1507  *
1508  * If LOV EA is requested from the root object and it's not
1509  * found, then return default striping for the filesystem.
1510  *
1511  * \see dt_object_operations::do_xattr_get() in the API description for details.
1512  */
1513 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1514                          struct lu_buf *buf, const char *name)
1515 {
1516         struct lod_thread_info *info = lod_env_info(env);
1517         struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1518         int is_root;
1519         int rc;
1520         ENTRY;
1521
1522         rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1523         if (strcmp(name, XATTR_NAME_LMV) == 0) {
1524                 struct lmv_mds_md_v1    *lmv1;
1525                 struct lmv_foreign_md   *lfm;
1526                 int                      rc1 = 0;
1527
1528                 if (rc > (typeof(rc))sizeof(*lmv1))
1529                         RETURN(rc);
1530
1531                 /* short (<= sizeof(struct lmv_mds_md_v1)) foreign LMV case */
1532                 /* XXX empty foreign LMV is not allowed */
1533                 if (rc <= offsetof(typeof(*lfm), lfm_value))
1534                         RETURN(rc = rc > 0 ? -EINVAL : rc);
1535
1536                 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1537                         BUILD_BUG_ON(sizeof(*lmv1) > sizeof(info->lti_key));
1538
1539                         /* lti_buf is large enough for *lmv1 or a short
1540                          * (<= sizeof(struct lmv_mds_md_v1)) foreign LMV
1541                          */
1542                         info->lti_buf.lb_buf = info->lti_key;
1543                         info->lti_buf.lb_len = sizeof(*lmv1);
1544                         rc = dt_xattr_get(env, dt_object_child(dt),
1545                                           &info->lti_buf, name);
1546                         if (unlikely(rc <= offsetof(typeof(*lfm),
1547                                                     lfm_value)))
1548                                 RETURN(rc = rc > 0 ? -EINVAL : rc);
1549
1550                         lfm = info->lti_buf.lb_buf;
1551                         if (le32_to_cpu(lfm->lfm_magic) == LMV_MAGIC_FOREIGN)
1552                                 RETURN(rc);
1553
1554                         if (unlikely(rc != sizeof(*lmv1)))
1555                                 RETURN(rc = rc > 0 ? -EINVAL : rc);
1556
1557                         lmv1 = info->lti_buf.lb_buf;
1558                         /* The on-disk LMV EA only contains header, but the
1559                          * returned LMV EA size should contain the space for
1560                          * the FIDs of all shards of the striped directory. */
1561                         if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1562                                 rc = lmv_mds_md_size(
1563                                         le32_to_cpu(lmv1->lmv_stripe_count),
1564                                         le32_to_cpu(lmv1->lmv_magic));
1565                 } else {
1566                         lmv1 = buf->lb_buf;
1567                         if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1568                                 RETURN(rc);
1569
1570                         if (rc != sizeof(*lmv1))
1571                                 RETURN(rc = rc > 0 ? -EINVAL : rc);
1572
1573                         rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1574                                                   buf, false);
1575                 }
1576
1577                 RETURN(rc = rc1 != 0 ? rc1 : rc);
1578         }
1579
1580         if ((rc > 0) && buf->lb_buf && strcmp(name, XATTR_NAME_LOV) == 0) {
1581                 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1582
1583                 if (lcm->lcm_magic == cpu_to_le32(LOV_MAGIC_SEL))
1584                         lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1585         }
1586
1587         if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1588                 RETURN(rc);
1589
1590         /*
1591          * XXX: Only used by lfsck
1592          *
1593          * lod returns default striping on the real root of the device
1594          * this is like the root stores default striping for the whole
1595          * filesystem. historically we've been using a different approach
1596          * and store it in the config.
1597          */
1598         dt_root_get(env, dev->lod_child, &info->lti_fid);
1599         is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1600
1601         if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1602                 struct lov_user_md *lum = buf->lb_buf;
1603                 struct lov_desc *desc = &dev->lod_ost_descs.ltd_lov_desc;
1604
1605                 if (buf->lb_buf == NULL) {
1606                         rc = sizeof(*lum);
1607                 } else if (buf->lb_len >= sizeof(*lum)) {
1608                         lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1609                         lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1610                         lmm_oi_set_id(&lum->lmm_oi, 0);
1611                         lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1612                         lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1613                         lum->lmm_stripe_size = cpu_to_le32(
1614                                                 desc->ld_default_stripe_size);
1615                         lum->lmm_stripe_count = cpu_to_le16(
1616                                                 desc->ld_default_stripe_count);
1617                         lum->lmm_stripe_offset = cpu_to_le16(
1618                                                 desc->ld_default_stripe_offset);
1619                         rc = sizeof(*lum);
1620                 } else {
1621                         rc = -ERANGE;
1622                 }
1623         }
1624
1625         RETURN(rc);
1626 }
1627
1628 /**
1629  * Verify LVM EA.
1630  *
1631  * Checks that the magic of the stripe is sane.
1632  *
1633  * \param[in] lod       lod device
1634  * \param[in] lum       a buffer storing LMV EA to verify
1635  *
1636  * \retval              0 if the EA is sane
1637  * \retval              negative otherwise
1638  */
1639 static int lod_verify_md_striping(struct lod_device *lod,
1640                                   const struct lmv_user_md_v1 *lum)
1641 {
1642         if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1643                 CERROR("%s: invalid lmv_user_md: magic = %x, "
1644                        "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1645                        lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1646                        (int)le32_to_cpu(lum->lum_stripe_offset),
1647                        le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1648                 return -EINVAL;
1649         }
1650
1651         return 0;
1652 }
1653
1654 /**
1655  * Initialize LMV EA for a slave.
1656  *
1657  * Initialize slave's LMV EA from the master's LMV EA.
1658  *
1659  * \param[in] master_lmv        a buffer containing master's EA
1660  * \param[out] slave_lmv        a buffer where slave's EA will be stored
1661  *
1662  */
1663 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1664                                   const struct lmv_mds_md_v1 *master_lmv)
1665 {
1666         *slave_lmv = *master_lmv;
1667         slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1668 }
1669
1670 /**
1671  * Generate LMV EA.
1672  *
1673  * Generate LMV EA from the object passed as \a dt. The object must have
1674  * the stripes created and initialized.
1675  *
1676  * \param[in] env       execution environment
1677  * \param[in] dt        object
1678  * \param[out] lmv_buf  buffer storing generated LMV EA
1679  *
1680  * \retval              0 on success
1681  * \retval              negative if failed
1682  */
1683 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1684                            struct lu_buf *lmv_buf)
1685 {
1686         struct lod_thread_info  *info = lod_env_info(env);
1687         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
1688         struct lod_object       *lo = lod_dt_obj(dt);
1689         struct lmv_mds_md_v1    *lmm1;
1690         int                     stripe_count;
1691         int                     type = LU_SEQ_RANGE_ANY;
1692         int                     rc;
1693         __u32                   mdtidx;
1694         ENTRY;
1695
1696         LASSERT(lo->ldo_dir_striped != 0);
1697         LASSERT(lo->ldo_dir_stripe_count > 0);
1698         stripe_count = lo->ldo_dir_stripe_count;
1699         /* Only store the LMV EA heahder on the disk. */
1700         if (info->lti_ea_store_size < sizeof(*lmm1)) {
1701                 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1702                 if (rc != 0)
1703                         RETURN(rc);
1704         } else {
1705                 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1706         }
1707
1708         lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1709         memset(lmm1, 0, sizeof(*lmm1));
1710         lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1711         lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1712         lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1713         lmm1->lmv_layout_version = cpu_to_le32(lo->ldo_dir_layout_version);
1714         if (lod_is_layout_changing(lo)) {
1715                 lmm1->lmv_migrate_hash = cpu_to_le32(lo->ldo_dir_migrate_hash);
1716                 lmm1->lmv_migrate_offset =
1717                         cpu_to_le32(lo->ldo_dir_migrate_offset);
1718         }
1719         rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1720                             &mdtidx, &type);
1721         if (rc != 0)
1722                 RETURN(rc);
1723
1724         lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1725         lmv_buf->lb_buf = info->lti_ea_store;
1726         lmv_buf->lb_len = sizeof(*lmm1);
1727
1728         RETURN(rc);
1729 }
1730
1731 /**
1732  * Create in-core represenation for a striped directory.
1733  *
1734  * Parse the buffer containing LMV EA and instantiate LU objects
1735  * representing the stripe objects. The pointers to the objects are
1736  * stored in ldo_stripe field of \a lo. This function is used when
1737  * we need to access an already created object (i.e. load from a disk).
1738  *
1739  * \param[in] env       execution environment
1740  * \param[in] lo        lod object
1741  * \param[in] buf       buffer containing LMV EA
1742  *
1743  * \retval              0 on success
1744  * \retval              negative if failed
1745  */
1746 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1747                            const struct lu_buf *buf)
1748 {
1749         struct lod_thread_info  *info = lod_env_info(env);
1750         struct lod_device       *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1751         struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
1752         struct dt_object        **stripe;
1753         union lmv_mds_md        *lmm = buf->lb_buf;
1754         struct lmv_mds_md_v1    *lmv1 = &lmm->lmv_md_v1;
1755         struct lu_fid           *fid = &info->lti_fid;
1756         unsigned int            i;
1757         int                     rc = 0;
1758         ENTRY;
1759
1760         LASSERT(mutex_is_locked(&lo->ldo_layout_mutex));
1761
1762         /* XXX may be useless as not called for foreign LMV ?? */
1763         if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_FOREIGN)
1764                 RETURN(0);
1765
1766         if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1767                 lo->ldo_dir_slave_stripe = 1;
1768                 RETURN(0);
1769         }
1770
1771         if (!lmv_is_sane(lmv1))
1772                 RETURN(-EINVAL);
1773
1774         LASSERT(lo->ldo_stripe == NULL);
1775         OBD_ALLOC_PTR_ARRAY(stripe, le32_to_cpu(lmv1->lmv_stripe_count));
1776         if (stripe == NULL)
1777                 RETURN(-ENOMEM);
1778
1779         for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1780                 struct dt_device        *tgt_dt;
1781                 struct dt_object        *dto;
1782                 int                     type = LU_SEQ_RANGE_ANY;
1783                 __u32                   idx;
1784
1785                 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1786                 if (!fid_is_sane(fid)) {
1787                         stripe[i] = NULL;
1788                         continue;
1789                 }
1790
1791                 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1792                 if (rc != 0)
1793                         GOTO(out, rc);
1794
1795                 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1796                         tgt_dt = lod->lod_child;
1797                 } else {
1798                         struct lod_tgt_desc     *tgt;
1799
1800                         tgt = LTD_TGT(ltd, idx);
1801                         if (tgt == NULL)
1802                                 GOTO(out, rc = -ESTALE);
1803                         tgt_dt = tgt->ltd_tgt;
1804                 }
1805
1806                 dto = dt_locate_at(env, tgt_dt, fid,
1807                                   lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1808                                   NULL);
1809                 if (IS_ERR(dto))
1810                         GOTO(out, rc = PTR_ERR(dto));
1811
1812                 stripe[i] = dto;
1813         }
1814 out:
1815         lo->ldo_stripe = stripe;
1816         lo->ldo_is_foreign = 0;
1817         lo->ldo_dir_stripe_count = le32_to_cpu(lmv1->lmv_stripe_count);
1818         lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1819         lo->ldo_dir_layout_version = le32_to_cpu(lmv1->lmv_layout_version);
1820         lo->ldo_dir_migrate_offset = le32_to_cpu(lmv1->lmv_migrate_offset);
1821         lo->ldo_dir_migrate_hash = le32_to_cpu(lmv1->lmv_migrate_hash);
1822         lo->ldo_dir_hash_type = le32_to_cpu(lmv1->lmv_hash_type);
1823         if (rc != 0)
1824                 lod_striping_free_nolock(env, lo);
1825
1826         RETURN(rc);
1827 }
1828
1829 /**
1830  * Declare create a striped directory.
1831  *
1832  * Declare creating a striped directory with a given stripe pattern on the
1833  * specified MDTs. A striped directory is represented as a regular directory
1834  * - an index listing all the stripes. The stripes point back to the master
1835  * object with ".." and LinkEA. The master object gets LMV EA which
1836  * identifies it as a striped directory. The function allocates FIDs
1837  * for all stripes.
1838  *
1839  * \param[in] env       execution environment
1840  * \param[in] dt        object
1841  * \param[in] attr      attributes to initialize the objects with
1842  * \param[in] dof       type of objects to be created
1843  * \param[in] th        transaction handle
1844  *
1845  * \retval              0 on success
1846  * \retval              negative if failed
1847  */
1848 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1849                                           struct dt_object *dt,
1850                                           struct lu_attr *attr,
1851                                           struct dt_object_format *dof,
1852                                           struct thandle *th)
1853 {
1854         struct lod_thread_info  *info = lod_env_info(env);
1855         struct lu_buf           lmv_buf;
1856         struct lu_buf           slave_lmv_buf;
1857         struct lmv_mds_md_v1    *lmm;
1858         struct lmv_mds_md_v1    *slave_lmm = NULL;
1859         struct dt_insert_rec    *rec = &info->lti_dt_rec;
1860         struct lod_object       *lo = lod_dt_obj(dt);
1861         int                     rc;
1862         __u32                   i;
1863         ENTRY;
1864
1865         rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1866         if (rc != 0)
1867                 GOTO(out, rc);
1868         lmm = lmv_buf.lb_buf;
1869
1870         OBD_ALLOC_PTR(slave_lmm);
1871         if (slave_lmm == NULL)
1872                 GOTO(out, rc = -ENOMEM);
1873
1874         lod_prep_slave_lmv_md(slave_lmm, lmm);
1875         slave_lmv_buf.lb_buf = slave_lmm;
1876         slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1877
1878         if (!dt_try_as_dir(env, dt_object_child(dt), false))
1879                 GOTO(out, rc = -EINVAL);
1880
1881         rec->rec_type = S_IFDIR;
1882         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1883                 struct dt_object        *dto = lo->ldo_stripe[i];
1884                 char                    *stripe_name = info->lti_key;
1885                 struct lu_name          *sname;
1886                 struct linkea_data       ldata          = { NULL };
1887                 struct lu_buf           linkea_buf;
1888
1889                 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
1890                 if (!dto)
1891                         continue;
1892
1893                 /* directory split skip create for existing stripes */
1894                 if (!(lod_is_splitting(lo) && i < lo->ldo_dir_split_offset)) {
1895                         rc = lod_sub_declare_create(env, dto, attr, NULL, dof,
1896                                                     th);
1897                         if (rc != 0)
1898                                 GOTO(out, rc);
1899
1900                         if (!dt_try_as_dir(env, dto, false))
1901                                 GOTO(out, rc = -EINVAL);
1902
1903                         rc = lod_sub_declare_ref_add(env, dto, th);
1904                         if (rc != 0)
1905                                 GOTO(out, rc);
1906
1907                         rec->rec_fid = lu_object_fid(&dto->do_lu);
1908                         rc = lod_sub_declare_insert(env, dto,
1909                                                     (const struct dt_rec *)rec,
1910                                                     (const struct dt_key *)dot,
1911                                                     th);
1912                         if (rc != 0)
1913                                 GOTO(out, rc);
1914
1915                         /* master stripe FID will be put to .. */
1916                         rec->rec_fid = lu_object_fid(&dt->do_lu);
1917                         rc = lod_sub_declare_insert(env, dto,
1918                                                   (const struct dt_rec *)rec,
1919                                                   (const struct dt_key *)dotdot,
1920                                                   th);
1921                         if (rc != 0)
1922                                 GOTO(out, rc);
1923
1924                         if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1925                             cfs_fail_val == i)
1926                                 snprintf(stripe_name, sizeof(info->lti_key),
1927                                          DFID":%u",
1928                                          PFID(lu_object_fid(&dto->do_lu)),
1929                                          i + 1);
1930                         else
1931                                 snprintf(stripe_name, sizeof(info->lti_key),
1932                                          DFID":%u",
1933                                          PFID(lu_object_fid(&dto->do_lu)), i);
1934
1935                         sname = lod_name_get(env, stripe_name,
1936                                              strlen(stripe_name));
1937                         rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1938                                               sname, lu_object_fid(&dt->do_lu));
1939                         if (rc != 0)
1940                                 GOTO(out, rc);
1941
1942                         linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1943                         linkea_buf.lb_len = ldata.ld_leh->leh_len;
1944                         rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
1945                                                        XATTR_NAME_LINK, 0, th);
1946                         if (rc != 0)
1947                                 GOTO(out, rc);
1948
1949                         rec->rec_fid = lu_object_fid(&dto->do_lu);
1950                         rc = lod_sub_declare_insert(env, dt_object_child(dt),
1951                                         (const struct dt_rec *)rec,
1952                                         (const struct dt_key *)stripe_name, th);
1953                         if (rc != 0)
1954                                 GOTO(out, rc);
1955
1956                         rc = lod_sub_declare_ref_add(env, dt_object_child(dt),
1957                                                      th);
1958                         if (rc != 0)
1959                                 GOTO(out, rc);
1960                 }
1961
1962                 if (!CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1963                     cfs_fail_val != i) {
1964                         if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1965                             cfs_fail_val == i)
1966                                 slave_lmm->lmv_master_mdt_index =
1967                                                         cpu_to_le32(i + 1);
1968                         else
1969                                 slave_lmm->lmv_master_mdt_index =
1970                                                         cpu_to_le32(i);
1971                         rc = lod_sub_declare_xattr_set(env, dto, &slave_lmv_buf,
1972                                                        XATTR_NAME_LMV, 0, th);
1973                         if (rc != 0)
1974                                 GOTO(out, rc);
1975                 }
1976         }
1977
1978         rc = lod_sub_declare_xattr_set(env, dt_object_child(dt),
1979                                        &lmv_buf, XATTR_NAME_LMV, 0, th);
1980         if (rc != 0)
1981                 GOTO(out, rc);
1982 out:
1983         if (slave_lmm != NULL)
1984                 OBD_FREE_PTR(slave_lmm);
1985
1986         RETURN(rc);
1987 }
1988
1989 /**
1990  * Allocate a striping on a predefined set of MDTs.
1991  *
1992  * Allocates new striping using the MDT index range provided by the data from
1993  * the lum_obejcts contained in the lmv_user_md passed to this method if
1994  * \a is_specific is true; or allocates new layout starting from MDT index in
1995  * lo->ldo_dir_stripe_offset. The exact order of MDTs is not important and
1996  * varies depending on MDT status. The number of stripes needed and stripe
1997  * offset are taken from the object. If that number cannot be met, then the
1998  * function returns an error and then it's the caller's responsibility to
1999  * release the stripes allocated. All the internal structures are protected,
2000  * but no concurrent allocation is allowed on the same objects.
2001  *
2002  * \param[in] env               execution environment for this thread
2003  * \param[in] lo                LOD object
2004  * \param[out] stripes          striping created
2005  * \param[out] mdt_indices      MDT indices of striping created
2006  * \param[in] is_specific       true if the MDTs are provided by lum; false if
2007  *                              only the starting MDT index is provided
2008  *
2009  * \retval positive     stripes allocated, including the first stripe allocated
2010  *                      outside
2011  * \retval negative     errno on failure
2012  */
2013 static int lod_mdt_alloc_specific(const struct lu_env *env,
2014                                   struct lod_object *lo,
2015                                   struct dt_object **stripes,
2016                                   __u32 *mdt_indices, bool is_specific)
2017 {
2018         struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
2019         struct lu_tgt_descs *ltd = &lod->lod_mdt_descs;
2020         struct lu_tgt_desc *tgt = NULL;
2021         struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2022         struct dt_device *tgt_dt = NULL;
2023         struct lu_fid fid = { 0 };
2024         struct dt_object *dto;
2025         u32 master_index;
2026         u32 stripe_count = lo->ldo_dir_stripe_count;
2027         int stripe_idx = 1;
2028         int j;
2029         int idx;
2030         int rc;
2031
2032         master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
2033         if (!is_specific && stripe_count > 1)
2034                 /* Set the start index for the 2nd stripe allocation */
2035                 mdt_indices[1] = (mdt_indices[0] + 1) %
2036                                         (lod->lod_remote_mdt_count + 1);
2037
2038         for (; stripe_idx < stripe_count; stripe_idx++) {
2039                 /* Try to find next avaible target */
2040                 idx = mdt_indices[stripe_idx];
2041                 for (j = 0; j < lod->lod_remote_mdt_count;
2042                      j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
2043                         bool already_allocated = false;
2044                         __u32 k;
2045
2046                         CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
2047                                idx, lod->lod_remote_mdt_count + 1, stripe_idx);
2048
2049                         if (likely(!is_specific &&
2050                                    !CFS_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
2051                                 /* check whether the idx already exists
2052                                  * in current allocated array */
2053                                 for (k = 0; k < stripe_idx; k++) {
2054                                         if (mdt_indices[k] == idx) {
2055                                                 already_allocated = true;
2056                                                 break;
2057                                         }
2058                                 }
2059
2060                                 if (already_allocated)
2061                                         continue;
2062                         }
2063
2064                         /* Sigh, this index is not in the bitmap, let's check
2065                          * next available target */
2066                         if (!test_bit(idx, ltd->ltd_tgt_bitmap) &&
2067                             idx != master_index)
2068                                 continue;
2069
2070                         if (idx == master_index) {
2071                                 /* Allocate the FID locally */
2072                                 tgt_dt = lod->lod_child;
2073                                 rc = dt_fid_alloc(env, tgt_dt, &fid, NULL,
2074                                                   NULL);
2075                                 if (rc < 0)
2076                                         continue;
2077                                 break;
2078                         }
2079
2080                         /* check the status of the OSP */
2081                         tgt = LTD_TGT(ltd, idx);
2082                         if (!tgt)
2083                                 continue;
2084
2085                         tgt_dt = tgt->ltd_tgt;
2086                         if (!tgt->ltd_active)
2087                                 /* this OSP doesn't feel well */
2088                                 continue;
2089
2090                         rc = dt_fid_alloc(env, tgt_dt, &fid, NULL, NULL);
2091                         if (rc < 0)
2092                                 continue;
2093
2094                         break;
2095                 }
2096
2097                 /* Can not allocate more stripes */
2098                 if (j == lod->lod_remote_mdt_count) {
2099                         CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
2100                                lod2obd(lod)->obd_name, stripe_count,
2101                                stripe_idx);
2102                         break;
2103                 }
2104
2105                 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
2106                        idx, stripe_idx, PFID(&fid));
2107                 mdt_indices[stripe_idx] = idx;
2108                 /* Set the start index for next stripe allocation */
2109                 if (!is_specific && stripe_idx < stripe_count - 1) {
2110                         /*
2111                          * for large dir test, put all other slaves on one
2112                          * remote MDT, otherwise we may save too many local
2113                          * slave locks which will exceed RS_MAX_LOCKS.
2114                          */
2115                         if (unlikely(CFS_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)))
2116                                 idx = master_index;
2117                         mdt_indices[stripe_idx + 1] = (idx + 1) %
2118                                            (lod->lod_remote_mdt_count + 1);
2119                 }
2120                 /* tgt_dt and fid must be ready after search avaible OSP
2121                  * in the above loop */
2122                 LASSERT(tgt_dt != NULL);
2123                 LASSERT(fid_is_sane(&fid));
2124
2125                 /* fail a remote stripe FID allocation */
2126                 if (stripe_idx && CFS_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_FID))
2127                         continue;
2128
2129                 dto = dt_locate_at(env, tgt_dt, &fid,
2130                                   lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
2131                                   &conf);
2132                 if (IS_ERR(dto)) {
2133                         rc = PTR_ERR(dto);
2134                         goto error;
2135                 }
2136
2137                 stripes[stripe_idx] = dto;
2138         }
2139
2140         return stripe_idx;
2141
2142 error:
2143         for (j = 1; j < stripe_idx; j++) {
2144                 LASSERT(stripes[j] != NULL);
2145                 dt_object_put(env, stripes[j]);
2146                 stripes[j] = NULL;
2147         }
2148         return rc;
2149 }
2150
2151 static int lod_prep_md_striped_create(const struct lu_env *env,
2152                                       struct dt_object *dt,
2153                                       struct lu_attr *attr,
2154                                       const struct lmv_user_md_v1 *lum,
2155                                       struct dt_object_format *dof,
2156                                       struct thandle *th)
2157 {
2158         struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
2159         struct lod_object *lo = lod_dt_obj(dt);
2160         struct dt_object **stripes;
2161         struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2162         struct lu_fid fid = { 0 };
2163         __u32 stripe_count;
2164         int i;
2165         int rc = 0;
2166
2167         ENTRY;
2168
2169         /* The lum has been verifed in lod_verify_md_striping */
2170         LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC ||
2171                 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC);
2172
2173         stripe_count = lo->ldo_dir_stripe_count;
2174
2175         OBD_ALLOC_PTR_ARRAY(stripes, stripe_count);
2176         if (!stripes)
2177                 RETURN(-ENOMEM);
2178
2179         /* Allocate the first stripe locally */
2180         rc = dt_fid_alloc(env, lod->lod_child, &fid, NULL, NULL);
2181         if (rc < 0)
2182                 GOTO(out, rc);
2183
2184         stripes[0] = dt_locate_at(env, lod->lod_child, &fid,
2185                                   dt->do_lu.lo_dev->ld_site->ls_top_dev, &conf);
2186         if (IS_ERR(stripes[0]))
2187                 GOTO(out, rc = PTR_ERR(stripes[0]));
2188
2189         if (lo->ldo_dir_stripe_offset == LMV_OFFSET_DEFAULT) {
2190                 lod_qos_statfs_update(env, lod, &lod->lod_mdt_descs);
2191                 rc = lod_mdt_alloc_qos(env, lo, stripes, 1, stripe_count);
2192                 if (rc == -EAGAIN)
2193                         rc = lod_mdt_alloc_rr(env, lo, stripes, 1,
2194                                               stripe_count);
2195         } else {
2196                 int *idx_array;
2197                 bool is_specific = false;
2198
2199                 OBD_ALLOC_PTR_ARRAY(idx_array, stripe_count);
2200                 if (!idx_array)
2201                         GOTO(out, rc = -ENOMEM);
2202
2203                 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) {
2204                         is_specific = true;
2205                         for (i = 0; i < stripe_count; i++)
2206                                 idx_array[i] =
2207                                        le32_to_cpu(lum->lum_objects[i].lum_mds);
2208                 }
2209
2210                 /* stripe 0 is local */
2211                 idx_array[0] =
2212                         lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
2213                 rc = lod_mdt_alloc_specific(env, lo, stripes, idx_array,
2214                                             is_specific);
2215                 OBD_FREE_PTR_ARRAY(idx_array, stripe_count);
2216         }
2217
2218         if (rc < 0)
2219                 GOTO(out, rc);
2220
2221         LASSERT(rc > 0);
2222
2223         lo->ldo_dir_striped = 1;
2224         lo->ldo_stripe = stripes;
2225         lo->ldo_dir_stripe_count = rc;
2226         lo->ldo_dir_stripes_allocated = stripe_count;
2227         smp_mb();
2228         lo->ldo_dir_stripe_loaded = 1;
2229
2230         rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
2231         if (rc < 0)
2232                 lod_striping_free(env, lo);
2233
2234         RETURN(rc);
2235
2236 out:
2237         LASSERT(rc < 0);
2238         if (!IS_ERR_OR_NULL(stripes[0]))
2239                 dt_object_put(env, stripes[0]);
2240         for (i = 1; i < stripe_count; i++)
2241                 LASSERT(!stripes[i]);
2242         OBD_FREE_PTR_ARRAY(stripes, stripe_count);
2243
2244         return rc;
2245 }
2246
2247 /**
2248  *
2249  * Alloc cached foreign LOV
2250  *
2251  * \param[in] lo        object
2252  * \param[in] size      size of foreign LOV
2253  *
2254  * \retval              0 on success
2255  * \retval              negative if failed
2256  */
2257 int lod_alloc_foreign_lov(struct lod_object *lo, size_t size)
2258 {
2259         OBD_ALLOC_LARGE(lo->ldo_foreign_lov, size);
2260         if (lo->ldo_foreign_lov == NULL)
2261                 return -ENOMEM;
2262         lo->ldo_foreign_lov_size = size;
2263         lo->ldo_is_foreign = 1;
2264         return 0;
2265 }
2266
2267 /**
2268  *
2269  * Free cached foreign LOV
2270  *
2271  * \param[in] lo        object
2272  */
2273 void lod_free_foreign_lov(struct lod_object *lo)
2274 {
2275         if (lo->ldo_foreign_lov != NULL)
2276                 OBD_FREE_LARGE(lo->ldo_foreign_lov, lo->ldo_foreign_lov_size);
2277         lo->ldo_foreign_lov = NULL;
2278         lo->ldo_foreign_lov_size = 0;
2279         lo->ldo_is_foreign = 0;
2280 }
2281
2282 /**
2283  *
2284  * Alloc cached foreign LMV
2285  *
2286  * \param[in] lo        object
2287  * \param[in] size      size of foreign LMV
2288  *
2289  * \retval              0 on success
2290  * \retval              negative if failed
2291  */
2292 int lod_alloc_foreign_lmv(struct lod_object *lo, size_t size)
2293 {
2294         OBD_ALLOC_LARGE(lo->ldo_foreign_lmv, size);
2295         if (lo->ldo_foreign_lmv == NULL)
2296                 return -ENOMEM;
2297         lo->ldo_foreign_lmv_size = size;
2298         lo->ldo_is_foreign = 1;
2299
2300         return 0;
2301 }
2302
2303 /**
2304  *
2305  * Free cached foreign LMV
2306  *
2307  * \param[in] lo        object
2308  */
2309 void lod_free_foreign_lmv(struct lod_object *lo)
2310 {
2311         if (lo->ldo_foreign_lmv != NULL)
2312                 OBD_FREE_LARGE(lo->ldo_foreign_lmv, lo->ldo_foreign_lmv_size);
2313         lo->ldo_foreign_lmv = NULL;
2314         lo->ldo_foreign_lmv_size = 0;
2315         lo->ldo_is_foreign = 0;
2316 }
2317
2318 /**
2319  * Declare create striped md object.
2320  *
2321  * The function declares intention to create a striped directory. This is a
2322  * wrapper for lod_prep_md_striped_create(). The only additional functionality
2323  * is to verify pattern \a lum_buf is good. Check that function for the details.
2324  *
2325  * \param[in] env       execution environment
2326  * \param[in] dt        object
2327  * \param[in] attr      attributes to initialize the objects with
2328  * \param[in] lum_buf   a pattern specifying the number of stripes and
2329  *                      MDT to start from
2330  * \param[in] dof       type of objects to be created
2331  * \param[in] th        transaction handle
2332  *
2333  * \retval              0 on success
2334  * \retval              negative if failed
2335  *
2336  */
2337 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
2338                                      struct dt_object *dt,
2339                                      struct lu_attr *attr,
2340                                      const struct lu_buf *lum_buf,
2341                                      struct dt_object_format *dof,
2342                                      struct thandle *th)
2343 {
2344         struct lod_object *lo = lod_dt_obj(dt);
2345         struct lmv_user_md_v1 *lum = lum_buf->lb_buf;
2346         int rc;
2347
2348         ENTRY;
2349         LASSERT(lum != NULL);
2350
2351         CDEBUG(D_INFO,
2352                "lum magic=%x hash=%x count=%u offset=%d inherit=%u rr=%u\n",
2353                le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_hash_type),
2354                le32_to_cpu(lum->lum_stripe_count),
2355                (int)le32_to_cpu(lum->lum_stripe_offset),
2356                lum->lum_max_inherit, lum->lum_max_inherit_rr);
2357
2358         if (lo->ldo_dir_stripe_count == 0) {
2359                 if (lo->ldo_is_foreign) {
2360                         rc = lod_alloc_foreign_lmv(lo, lum_buf->lb_len);
2361                         if (rc != 0)
2362                                 GOTO(out, rc);
2363                         memcpy(lo->ldo_foreign_lmv, lum, lum_buf->lb_len);
2364                         lo->ldo_dir_stripe_loaded = 1;
2365                 }
2366                 GOTO(out, rc = 0);
2367         }
2368
2369         /* prepare dir striped objects */
2370         rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2371         if (rc != 0) {
2372                 /* failed to create striping, let's reset
2373                  * config so that others don't get confused */
2374                 lod_striping_free(env, lo);
2375                 GOTO(out, rc);
2376         }
2377 out:
2378         RETURN(rc);
2379 }
2380
2381 /**
2382  * Set or replace striped directory layout, and LFSCK may set layout on a plain
2383  * directory, so don't check stripe count.
2384  *
2385  * \param[in] env       execution environment
2386  * \param[in] dt        target object
2387  * \param[in] lmv_buf   LMV buf which contains source stripe FIDs
2388  * \param[in] fl        set or replace
2389  * \param[in] th        transaction handle
2390  *
2391  * \retval              0 on success
2392  * \retval              negative if failed
2393  */
2394 static int lod_dir_layout_set(const struct lu_env *env,
2395                               struct dt_object *dt,
2396                               const struct lu_buf *lmv_buf,
2397                               int fl,
2398                               struct thandle *th)
2399 {
2400         struct dt_object *next = dt_object_child(dt);
2401         struct lod_object *lo = lod_dt_obj(dt);
2402         struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2403         struct lmv_mds_md_v1 *lmv = lmv_buf->lb_buf;
2404         struct lmv_mds_md_v1 *slave_lmv;
2405         struct lu_buf slave_buf;
2406         int i;
2407         int rc;
2408
2409         ENTRY;
2410
2411         if (!lmv_is_sane2(lmv))
2412                 RETURN(-EINVAL);
2413
2414         /* adjust hash for dir merge, which may not be set in user command */
2415         if (lmv_is_merging(lmv) &&
2416             !(lmv->lmv_migrate_hash & LMV_HASH_TYPE_MASK))
2417                 lmv->lmv_merge_hash |=
2418                         lod->lod_mdt_descs.ltd_lmv_desc.ld_pattern &
2419                         LMV_HASH_TYPE_MASK;
2420
2421         LMV_DEBUG(D_INFO, lmv, "set");
2422
2423         rc = lod_sub_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV, fl, th);
2424         if (rc)
2425                 RETURN(rc);
2426
2427         /* directory restripe may update stripe LMV directly */
2428         if (!lo->ldo_dir_stripe_count)
2429                 RETURN(0);
2430
2431         lo->ldo_dir_hash_type = le32_to_cpu(lmv->lmv_hash_type);
2432         lo->ldo_dir_migrate_offset = le32_to_cpu(lmv->lmv_migrate_offset);
2433         lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_migrate_hash);
2434         lo->ldo_dir_layout_version = le32_to_cpu(lmv->lmv_layout_version);
2435
2436         OBD_ALLOC_PTR(slave_lmv);
2437         if (!slave_lmv)
2438                 RETURN(-ENOMEM);
2439
2440         lod_prep_slave_lmv_md(slave_lmv, lmv);
2441         slave_buf.lb_buf = slave_lmv;
2442         slave_buf.lb_len = sizeof(*slave_lmv);
2443
2444         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2445                 if (!lo->ldo_stripe[i])
2446                         continue;
2447
2448                 if (!dt_object_exists(lo->ldo_stripe[i]))
2449                         continue;
2450
2451                 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], &slave_buf,
2452                                        XATTR_NAME_LMV, fl, th);
2453                 if (rc)
2454                         break;
2455         }
2456
2457         OBD_FREE_PTR(slave_lmv);
2458
2459         RETURN(rc);
2460 }
2461
2462 /**
2463  * Implementation of dt_object_operations::do_declare_xattr_set.
2464  *
2465  * Used with regular (non-striped) objects. Basically it
2466  * initializes the striping information and applies the
2467  * change to all the stripes.
2468  *
2469  * \see dt_object_operations::do_declare_xattr_set() in the API description
2470  * for details.
2471  */
2472 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2473                                      struct dt_object *dt,
2474                                      const struct lu_buf *buf,
2475                                      const char *name, int fl,
2476                                      struct thandle *th)
2477 {
2478         struct dt_object        *next = dt_object_child(dt);
2479         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2480         struct lod_object       *lo = lod_dt_obj(dt);
2481         int                     i;
2482         int                     rc;
2483         ENTRY;
2484
2485         if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2486                 struct lmv_user_md_v1 *lum;
2487
2488                 LASSERT(buf != NULL && buf->lb_buf != NULL);
2489                 lum = buf->lb_buf;
2490                 rc = lod_verify_md_striping(d, lum);
2491                 if (rc != 0)
2492                         RETURN(rc);
2493         } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2494                 rc = lod_verify_striping(env, d, lo, buf, false);
2495                 if (rc != 0)
2496                         RETURN(rc);
2497         }
2498
2499         rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2500         if (rc != 0)
2501                 RETURN(rc);
2502
2503         /* Note: Do not set LinkEA on sub-stripes, otherwise
2504          * it will confuse the fid2path process(see mdt_path_current()).
2505          * The linkEA between master and sub-stripes is set in
2506          * lod_xattr_set_lmv(). */
2507         if (strcmp(name, XATTR_NAME_LINK) == 0)
2508                 RETURN(0);
2509
2510         /* set xattr to each stripes, if needed */
2511         rc = lod_striping_load(env, lo);
2512         if (rc != 0)
2513                 RETURN(rc);
2514
2515         if (lo->ldo_dir_stripe_count == 0)
2516                 RETURN(0);
2517
2518         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2519                 if (!lo->ldo_stripe[i])
2520                         continue;
2521
2522                 if (!dt_object_exists(lo->ldo_stripe[i]))
2523                         continue;
2524
2525                 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2526                                                buf, name, fl, th);
2527                 if (rc != 0)
2528                         break;
2529         }
2530
2531         RETURN(rc);
2532 }
2533
2534 static int
2535 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2536                                      struct lod_object *lo,
2537                                      struct dt_object *dt, struct thandle *th,
2538                                      int comp_idx, int stripe_idx,
2539                                      struct lod_obj_stripe_cb_data *data)
2540 {
2541         struct lod_thread_info *info = lod_env_info(env);
2542         struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
2543         struct filter_fid *ff = &info->lti_ff;
2544         struct lu_buf *buf = &info->lti_buf;
2545         int rc;
2546
2547         buf->lb_buf = ff;
2548         buf->lb_len = sizeof(*ff);
2549         rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2550         if (rc < 0) {
2551                 if (rc == -ENODATA)
2552                         return 0;
2553                 return rc;
2554         }
2555
2556         /*
2557          * locd_buf is set if it's called by dir migration, which doesn't check
2558          * pfid and comp id.
2559          */
2560         if (data->locd_buf) {
2561                 memset(ff, 0, sizeof(*ff));
2562                 ff->ff_parent = *(struct lu_fid *)data->locd_buf->lb_buf;
2563         } else {
2564                 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
2565
2566                 if (lu_fid_eq(lod_object_fid(lo), &ff->ff_parent) &&
2567                     ff->ff_layout.ol_comp_id == comp->llc_id)
2568                         return 0;
2569
2570                 memset(ff, 0, sizeof(*ff));
2571                 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2572         }
2573
2574         /* rewrite filter_fid */
2575         ff->ff_parent.f_ver = stripe_idx;
2576         ff->ff_layout.ol_stripe_size = comp->llc_stripe_size;
2577         ff->ff_layout.ol_stripe_count = comp->llc_stripe_count;
2578         ff->ff_layout.ol_comp_id = comp->llc_id;
2579         ff->ff_layout.ol_comp_start = comp->llc_extent.e_start;
2580         ff->ff_layout.ol_comp_end = comp->llc_extent.e_end;
2581         filter_fid_cpu_to_le(ff, ff, sizeof(*ff));
2582
2583         if (data->locd_declare)
2584                 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2585                                                LU_XATTR_REPLACE, th);
2586         else
2587                 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2588                                        LU_XATTR_REPLACE, th);
2589
2590         return rc;
2591 }
2592
2593 /**
2594  * Reset parent FID on OST object
2595  *
2596  * Replace parent FID with @dt object FID, which is only called during migration
2597  * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2598  * the FID is changed.
2599  *
2600  * \param[in] env execution environment
2601  * \param[in] dt dt_object whose stripes's parent FID will be reset
2602  * \parem[in] th thandle
2603  * \param[in] declare if it is declare
2604  *
2605  * \retval      0 if reset succeeds
2606  * \retval      negative errno if reset fails
2607  */
2608 static int lod_replace_parent_fid(const struct lu_env *env,
2609                                   struct dt_object *dt,
2610                                   const struct lu_buf *buf,
2611                                   struct thandle *th, bool declare)
2612 {
2613         struct lod_object *lo = lod_dt_obj(dt);
2614         struct lod_thread_info  *info = lod_env_info(env);
2615         struct filter_fid *ff;
2616         struct lod_obj_stripe_cb_data data = { { 0 } };
2617         int rc;
2618         ENTRY;
2619
2620         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2621
2622         /* set xattr to each stripes, if needed */
2623         rc = lod_striping_load(env, lo);
2624         if (rc != 0)
2625                 RETURN(rc);
2626
2627         if (!lod_obj_is_striped(dt))
2628                 RETURN(0);
2629
2630         if (info->lti_ea_store_size < sizeof(*ff)) {
2631                 rc = lod_ea_store_resize(info, sizeof(*ff));
2632                 if (rc != 0)
2633                         RETURN(rc);
2634         }
2635
2636         data.locd_declare = declare;
2637         data.locd_stripe_cb = lod_obj_stripe_replace_parent_fid_cb;
2638         data.locd_buf = buf;
2639         rc = lod_obj_for_each_stripe(env, lo, th, &data);
2640
2641         RETURN(rc);
2642 }
2643
2644 __u16 lod_comp_entry_stripe_count(struct lod_object *lo, int comp_idx,
2645                                   bool is_dir)
2646 {
2647         struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2648         struct lod_layout_component *entry;
2649         enum lod_uses_hint flags = LOD_USES_ASSIGNED_STRIPE;
2650
2651         if (is_dir)
2652                 return  0;
2653
2654         entry = &lo->ldo_comp_entries[comp_idx];
2655         if (lod_comp_inited(entry))
2656                 return entry->llc_stripe_count;
2657         if (entry->llc_stripe_count == (__u16)-1)
2658                 return lod_get_stripe_count_plain(lod, lo,
2659                                                   entry->llc_stripe_count,
2660                                                   entry->llc_pattern &
2661                                                       LOV_PATTERN_OVERSTRIPING,
2662                                                   &flags);
2663
2664         return lod_get_stripe_count(lod, lo, comp_idx, entry->llc_stripe_count,
2665                                  entry->llc_pattern & LOV_PATTERN_OVERSTRIPING,
2666                                  &flags);
2667 }
2668
2669 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2670 {
2671         int magic, size = 0, i;
2672         struct lod_layout_component *comp_entries;
2673         __u16 comp_cnt;
2674         bool is_composite, is_foreign = false;
2675
2676         if (is_dir) {
2677                 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2678                 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2679                 is_composite =
2680                         lo->ldo_def_striping->lds_def_striping_is_composite;
2681         } else {
2682                 comp_cnt = lo->ldo_comp_cnt;
2683                 comp_entries = lo->ldo_comp_entries;
2684                 is_composite = lo->ldo_is_composite;
2685                 is_foreign = lo->ldo_is_foreign;
2686         }
2687
2688         if (is_foreign)
2689                 return lo->ldo_foreign_lov_size;
2690
2691         LASSERT(comp_cnt != 0 && comp_entries != NULL);
2692         if (is_composite) {
2693                 size = sizeof(struct lov_comp_md_v1) +
2694                        sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2695                 LASSERT(size % sizeof(__u64) == 0);
2696         }
2697
2698         for (i = 0; i < comp_cnt; i++) {
2699                 __u16 stripe_count;
2700
2701                 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2702                 stripe_count = lod_comp_entry_stripe_count(lo, i, is_dir);
2703                 if (!is_dir && is_composite)
2704                         lod_comp_shrink_stripe_count(&comp_entries[i],
2705                                                      &stripe_count);
2706
2707                 size += lov_user_md_size(stripe_count, magic);
2708                 LASSERT(size % sizeof(__u64) == 0);
2709         }
2710         return size;
2711 }
2712
2713 /**
2714  * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2715  * the xattr value is binary lov_comp_md_v1 which contains component(s)
2716  * to be added.
2717   *
2718  * \param[in] env       execution environment
2719  * \param[in] dt        dt_object to add components on
2720  * \param[in] buf       buffer contains components to be added
2721  * \parem[in] th        thandle
2722  *
2723  * \retval      0 on success
2724  * \retval      negative errno on failure
2725  */
2726 static int lod_declare_layout_add(const struct lu_env *env,
2727                                   struct dt_object *dt,
2728                                   const struct lu_buf *buf,
2729                                   struct thandle *th)
2730 {
2731         struct lod_thread_info  *info = lod_env_info(env);
2732         struct lod_layout_component *comp_array, *lod_comp, *old_array;
2733         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2734         struct dt_object *next = dt_object_child(dt);
2735         struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
2736         struct lod_object *lo = lod_dt_obj(dt);
2737         struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2738         __u32 magic;
2739         int i, rc, array_cnt, old_array_cnt;
2740         ENTRY;
2741
2742         LASSERT(lo->ldo_is_composite);
2743
2744         if (lo->ldo_flr_state != LCM_FL_NONE)
2745                 RETURN(-EBUSY);
2746
2747         rc = lod_verify_striping(env, d, lo, buf, false);
2748         if (rc != 0)
2749                 RETURN(rc);
2750
2751         magic = comp_v1->lcm_magic;
2752         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2753                 lustre_swab_lov_comp_md_v1(comp_v1);
2754                 magic = comp_v1->lcm_magic;
2755         }
2756
2757         if (magic != LOV_USER_MAGIC_COMP_V1)
2758                 RETURN(-EINVAL);
2759
2760         mutex_lock(&lo->ldo_layout_mutex);
2761
2762         array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2763         OBD_ALLOC_PTR_ARRAY(comp_array, array_cnt);
2764         if (comp_array == NULL) {
2765                 mutex_unlock(&lo->ldo_layout_mutex);
2766                 RETURN(-ENOMEM);
2767         }
2768
2769
2770         memcpy(comp_array, lo->ldo_comp_entries,
2771                sizeof(*comp_array) * lo->ldo_comp_cnt);
2772
2773         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2774                 struct lov_user_md_v1 *v1;
2775                 struct lu_extent *ext;
2776
2777                 v1 = (struct lov_user_md *)((char *)comp_v1 +
2778                                 comp_v1->lcm_entries[i].lcme_offset);
2779                 ext = &comp_v1->lcm_entries[i].lcme_extent;
2780
2781                 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2782                 lod_comp->llc_extent.e_start = ext->e_start;
2783                 lod_comp->llc_extent.e_end = ext->e_end;
2784                 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2785                 lod_comp->llc_flags = comp_v1->lcm_entries[i].lcme_flags;
2786
2787                 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2788                 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2789                 lod_adjust_stripe_info(lod_comp, desc, 0);
2790
2791                 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2792                         struct lov_user_md_v3 *v3 = (typeof(*v3) *) v1;
2793
2794                         if (v3->lmm_pool_name[0] != '\0' &&
2795                             !lov_pool_is_ignored(v3->lmm_pool_name)) {
2796                                 rc = lod_set_pool(&lod_comp->llc_pool,
2797                                                   v3->lmm_pool_name);
2798                                 if (rc)
2799                                         GOTO(error, rc);
2800                         }
2801                 }
2802         }
2803
2804         old_array = lo->ldo_comp_entries;
2805         old_array_cnt = lo->ldo_comp_cnt;
2806
2807         lo->ldo_comp_entries = comp_array;
2808         lo->ldo_comp_cnt = array_cnt;
2809
2810         /* No need to increase layout generation here, it will be increased
2811          * later when generating component ID for the new components */
2812
2813         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2814         rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2815                                               XATTR_NAME_LOV, 0, th);
2816         if (rc) {
2817                 lo->ldo_comp_entries = old_array;
2818                 lo->ldo_comp_cnt = old_array_cnt;
2819                 GOTO(error, rc);
2820         }
2821
2822         OBD_FREE_PTR_ARRAY(old_array, old_array_cnt);
2823
2824         LASSERT(lo->ldo_mirror_count == 1);
2825         lo->ldo_mirrors[0].lme_end = array_cnt - 1;
2826
2827         mutex_unlock(&lo->ldo_layout_mutex);
2828
2829         RETURN(0);
2830
2831 error:
2832         for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2833                 lod_comp = &comp_array[i];
2834                 if (lod_comp->llc_pool != NULL) {
2835                         OBD_FREE(lod_comp->llc_pool,
2836                                  strlen(lod_comp->llc_pool) + 1);
2837                         lod_comp->llc_pool = NULL;
2838                 }
2839         }
2840         OBD_FREE_PTR_ARRAY(comp_array, array_cnt);
2841         mutex_unlock(&lo->ldo_layout_mutex);
2842
2843         RETURN(rc);
2844 }
2845
2846 /**
2847  * lod_last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2848  * @mirror_id: Mirror id to be checked.
2849  * @lo:        LOD object.
2850  *
2851  * This function checks if a mirror with specified @mirror_id is the last
2852  * non-stale mirror of a LOD object @lo.
2853  *
2854  * Return: true or false.
2855  */
2856 static inline
2857 bool lod_last_non_stale_mirror(__u16 mirror_id, struct lod_object *lo)
2858 {
2859         struct lod_layout_component *lod_comp;
2860         bool has_stale_flag;
2861         int i;
2862
2863         for (i = 0; i < lo->ldo_mirror_count; i++) {
2864                 if (lo->ldo_mirrors[i].lme_id == mirror_id ||
2865                     lo->ldo_mirrors[i].lme_stale)
2866                         continue;
2867
2868                 has_stale_flag = false;
2869                 lod_foreach_mirror_comp(lod_comp, lo, i) {
2870                         if (lod_comp->llc_flags & LCME_FL_STALE) {
2871                                 has_stale_flag = true;
2872                                 break;
2873                         }
2874                 }
2875                 if (!has_stale_flag)
2876                         return false;
2877         }
2878
2879         return true;
2880 }
2881
2882 /**
2883  * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2884  * the '$field' can only be 'flags' now. The xattr value is binary
2885  * lov_comp_md_v1 which contains the component ID(s) and the value of
2886  * the field to be modified.
2887  * Please update allowed_lustre_lov macro if $field groks more values
2888  * in the future.
2889  *
2890  * \param[in] env       execution environment
2891  * \param[in] dt        dt_object to be modified
2892  * \param[in] op        operation string, like "set.flags"
2893  * \param[in] buf       buffer contains components to be set
2894  * \parem[in] th        thandle
2895  *
2896  * \retval      0 on success
2897  * \retval      negative errno on failure
2898  */
2899 static int lod_declare_layout_set(const struct lu_env *env,
2900                                   struct dt_object *dt,
2901                                   char *op, const struct lu_buf *buf,
2902                                   struct thandle *th)
2903 {
2904         struct lod_layout_component     *lod_comp;
2905         struct lod_thread_info  *info = lod_env_info(env);
2906         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2907         struct lod_object       *lo = lod_dt_obj(dt);
2908         struct lov_comp_md_v1   *comp_v1 = buf->lb_buf;
2909         __u32   magic;
2910         int     i, j, rc;
2911         bool    changed = false;
2912         ENTRY;
2913
2914         /* Please update allowed_lustre_lov macro if op
2915          * groks more values in the future
2916          */
2917         if (strcmp(op, "set.flags") != 0) {
2918                 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2919                        lod2obd(d)->obd_name, op);
2920                 RETURN(-ENOTSUPP);
2921         }
2922
2923         magic = comp_v1->lcm_magic;
2924         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2925                 lustre_swab_lov_comp_md_v1(comp_v1);
2926                 magic = comp_v1->lcm_magic;
2927         }
2928
2929         if (magic != LOV_USER_MAGIC_COMP_V1)
2930                 RETURN(-EINVAL);
2931
2932         if (comp_v1->lcm_entry_count == 0) {
2933                 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2934                        lod2obd(d)->obd_name);
2935                 RETURN(-EINVAL);
2936         }
2937
2938         mutex_lock(&lo->ldo_layout_mutex);
2939         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2940                 __u32 id = comp_v1->lcm_entries[i].lcme_id;
2941                 __u32 flags = comp_v1->lcm_entries[i].lcme_flags;
2942                 __u32 mirror_flag = flags & LCME_MIRROR_FLAGS;
2943                 __u16 mirror_id = mirror_id_of(id);
2944                 bool neg = flags & LCME_FL_NEG;
2945
2946                 if (flags & LCME_FL_INIT) {
2947                         if (changed)
2948                                 lod_striping_free_nolock(env, lo);
2949                         mutex_unlock(&lo->ldo_layout_mutex);
2950                         RETURN(-EINVAL);
2951                 }
2952
2953                 flags &= ~(LCME_MIRROR_FLAGS | LCME_FL_NEG);
2954                 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2955                         lod_comp = &lo->ldo_comp_entries[j];
2956
2957                         /* lfs only put one flag in each entry */
2958                         if ((flags && id != lod_comp->llc_id) ||
2959                             (mirror_flag && mirror_id !=
2960                                             mirror_id_of(lod_comp->llc_id)))
2961                                 continue;
2962
2963                         if (neg) {
2964                                 if (flags)
2965                                         lod_comp->llc_flags &= ~flags;
2966                                 if (mirror_flag)
2967                                         lod_comp->llc_flags &= ~mirror_flag;
2968                         } else {
2969                                 if (flags) {
2970                                         if ((flags & LCME_FL_STALE) &&
2971                                             lod_last_non_stale_mirror(mirror_id,
2972                                                                       lo)) {
2973                                                 mutex_unlock(
2974                                                         &lo->ldo_layout_mutex);
2975                                                 RETURN(-EUCLEAN);
2976                                         }
2977                                         lod_comp->llc_flags |= flags;
2978                                 }
2979                                 if (mirror_flag) {
2980                                         lod_comp->llc_flags |= mirror_flag;
2981                                         if (mirror_flag & LCME_FL_NOSYNC)
2982                                                 lod_comp->llc_timestamp =
2983                                                        ktime_get_real_seconds();
2984                                 }
2985                         }
2986                         changed = true;
2987                 }
2988         }
2989         mutex_unlock(&lo->ldo_layout_mutex);
2990
2991         if (!changed) {
2992                 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2993                        lod2obd(d)->obd_name);
2994                 RETURN(-EINVAL);
2995         }
2996
2997         lod_obj_inc_layout_gen(lo);
2998
2999         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
3000         rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), &info->lti_buf,
3001                                        XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3002         RETURN(rc);
3003 }
3004
3005 /**
3006  * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
3007  * and the xattr value is a unique component ID or a special lcme_id.
3008  *
3009  * \param[in] env       execution environment
3010  * \param[in] dt        dt_object to be operated on
3011  * \param[in] buf       buffer contains component ID or lcme_id
3012  * \parem[in] th        thandle
3013  *
3014  * \retval      0 on success
3015  * \retval      negative errno on failure
3016  */
3017 static int lod_declare_layout_del(const struct lu_env *env,
3018                                   struct dt_object *dt,
3019                                   const struct lu_buf *buf,
3020                                   struct thandle *th)
3021 {
3022         struct lod_thread_info  *info = lod_env_info(env);
3023         struct dt_object *next = dt_object_child(dt);
3024         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3025         struct lod_object *lo = lod_dt_obj(dt);
3026         struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3027         struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
3028         __u32 magic, id, flags, neg_flags = 0;
3029         int rc, i, j, left;
3030         ENTRY;
3031
3032         LASSERT(lo->ldo_is_composite);
3033
3034         if (lo->ldo_flr_state != LCM_FL_NONE)
3035                 RETURN(-EBUSY);
3036
3037         magic = comp_v1->lcm_magic;
3038         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3039                 lustre_swab_lov_comp_md_v1(comp_v1);
3040                 magic = comp_v1->lcm_magic;
3041         }
3042
3043         if (magic != LOV_USER_MAGIC_COMP_V1)
3044                 RETURN(-EINVAL);
3045
3046         id = comp_v1->lcm_entries[0].lcme_id;
3047         flags = comp_v1->lcm_entries[0].lcme_flags;
3048
3049         if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
3050                 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
3051                        lod2obd(d)->obd_name, id, flags);
3052                 RETURN(-EINVAL);
3053         }
3054
3055         if (id != LCME_ID_INVAL && flags != 0) {
3056                 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
3057                        lod2obd(d)->obd_name);
3058                 RETURN(-EINVAL);
3059         }
3060
3061         if (id == LCME_ID_INVAL && !flags) {
3062                 CDEBUG(D_LAYOUT, "%s: no id or flags specified.\n",
3063                        lod2obd(d)->obd_name);
3064                 RETURN(-EINVAL);
3065         }
3066
3067         if (flags & LCME_FL_NEG) {
3068                 neg_flags = flags & ~LCME_FL_NEG;
3069                 flags = 0;
3070         }
3071
3072         mutex_lock(&lo->ldo_layout_mutex);
3073
3074         left = lo->ldo_comp_cnt;
3075         if (left <= 0) {
3076                 mutex_unlock(&lo->ldo_layout_mutex);
3077                 RETURN(-EINVAL);
3078         }
3079
3080         for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3081                 struct lod_layout_component *lod_comp;
3082
3083                 lod_comp = &lo->ldo_comp_entries[i];
3084
3085                 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
3086                         continue;
3087                 else if (flags && !(flags & lod_comp->llc_flags))
3088                         continue;
3089                 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
3090                         continue;
3091
3092                 if (left != (i + 1)) {
3093                         CDEBUG(D_LAYOUT, "%s: this deletion will create "
3094                                "a hole.\n", lod2obd(d)->obd_name);
3095                         mutex_unlock(&lo->ldo_layout_mutex);
3096                         RETURN(-EINVAL);
3097                 }
3098                 left--;
3099
3100                 /* Mark the component as deleted */
3101                 lod_comp->llc_id = LCME_ID_INVAL;
3102
3103                 /* Not instantiated component */
3104                 if (lod_comp->llc_stripe == NULL)
3105                         continue;
3106
3107                 LASSERT(lod_comp->llc_stripe_count > 0);
3108                 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
3109                         struct dt_object *obj = lod_comp->llc_stripe[j];
3110
3111                         if (obj == NULL)
3112                                 continue;
3113                         rc = lod_sub_declare_destroy(env, obj, th);
3114                         if (rc) {
3115                                 mutex_unlock(&lo->ldo_layout_mutex);
3116                                 RETURN(rc);
3117                         }
3118                 }
3119         }
3120
3121         LASSERTF(left >= 0, "left = %d\n", left);
3122         if (left == lo->ldo_comp_cnt) {
3123                 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
3124                        lod2obd(d)->obd_name, id);
3125                 mutex_unlock(&lo->ldo_layout_mutex);
3126                 RETURN(-EINVAL);
3127         }
3128
3129         mutex_unlock(&lo->ldo_layout_mutex);
3130
3131         memset(attr, 0, sizeof(*attr));
3132         attr->la_valid = LA_SIZE;
3133         rc = lod_sub_declare_attr_set(env, next, attr, th);
3134         if (rc)
3135                 RETURN(rc);
3136
3137         if (left > 0) {
3138                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
3139                 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
3140                                                XATTR_NAME_LOV, 0, th);
3141         } else {
3142                 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
3143         }
3144
3145         RETURN(rc);
3146 }
3147
3148 /**
3149  * Declare layout add/set/del operations issued by special xattr names:
3150  *
3151  * XATTR_LUSTRE_LOV.add         add component(s) to existing file
3152  * XATTR_LUSTRE_LOV.del         delete component(s) from existing file
3153  * XATTR_LUSTRE_LOV.set.$field  set specified field of certain component(s)
3154  *
3155  * \param[in] env       execution environment
3156  * \param[in] dt        object
3157  * \param[in] name      name of xattr
3158  * \param[in] buf       lu_buf contains xattr value
3159  * \param[in] th        transaction handle
3160  *
3161  * \retval              0 on success
3162  * \retval              negative if failed
3163  */
3164 static int lod_declare_modify_layout(const struct lu_env *env,
3165                                      struct dt_object *dt,
3166                                      const char *name,
3167                                      const struct lu_buf *buf,
3168                                      struct thandle *th)
3169 {
3170         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3171         struct lod_object *lo = lod_dt_obj(dt);
3172         char *op;
3173         int rc, len = strlen(XATTR_LUSTRE_LOV);
3174         ENTRY;
3175
3176         LASSERT(dt_object_exists(dt));
3177
3178         if (strlen(name) <= len || name[len] != '.') {
3179                 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
3180                        lod2obd(d)->obd_name, name);
3181                 RETURN(-EINVAL);
3182         }
3183         len++;
3184
3185         rc = lod_striping_load(env, lo);
3186         if (rc)
3187                 GOTO(unlock, rc);
3188
3189         /* the layout to be modified must be a composite layout */
3190         if (!lo->ldo_is_composite) {
3191                 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
3192                        lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3193                 GOTO(unlock, rc = -EINVAL);
3194         }
3195
3196         op = (char *)name + len;
3197         if (strcmp(op, "add") == 0) {
3198                 rc = lod_declare_layout_add(env, dt, buf, th);
3199         } else if (strcmp(op, "del") == 0) {
3200                 rc = lod_declare_layout_del(env, dt, buf, th);
3201         } else if (strncmp(op, "set", strlen("set")) == 0) {
3202                 rc = lod_declare_layout_set(env, dt, op, buf, th);
3203         } else  {
3204                 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
3205                        lod2obd(d)->obd_name, name);
3206                 GOTO(unlock, rc = -ENOTSUPP);
3207         }
3208 unlock:
3209         if (rc)
3210                 lod_striping_free(env, lo);
3211
3212         RETURN(rc);
3213 }
3214
3215 /**
3216  * Convert a plain file lov_mds_md to a composite layout.
3217  *
3218  * \param[in,out] info  the thread info::lti_ea_store buffer contains little
3219  *                      endian plain file layout
3220  *
3221  * \retval              0 on success, <0 on failure
3222  */
3223 static int lod_layout_convert(struct lod_thread_info *info)
3224 {
3225         struct lov_mds_md *lmm = info->lti_ea_store;
3226         struct lov_mds_md *lmm_save;
3227         struct lov_comp_md_v1 *lcm;
3228         struct lov_comp_md_entry_v1 *lcme;
3229         size_t size;
3230         __u32 blob_size;
3231         int rc = 0;
3232         ENTRY;
3233
3234         /* realloc buffer to a composite layout which contains one component */
3235         blob_size = lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
3236                                     le32_to_cpu(lmm->lmm_magic));
3237         size = sizeof(*lcm) + sizeof(*lcme) + blob_size;
3238
3239         OBD_ALLOC_LARGE(lmm_save, blob_size);
3240         if (!lmm_save)
3241                 GOTO(out, rc = -ENOMEM);
3242
3243         memcpy(lmm_save, lmm, blob_size);
3244
3245         if (info->lti_ea_store_size < size) {
3246                 rc = lod_ea_store_resize(info, size);
3247                 if (rc)
3248                         GOTO(out, rc);
3249         }
3250
3251         lcm = info->lti_ea_store;
3252         memset(lcm, 0, sizeof(*lcm) + sizeof(*lcme));
3253         lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
3254         lcm->lcm_size = cpu_to_le32(size);
3255         lcm->lcm_layout_gen = cpu_to_le32(le16_to_cpu(
3256                                                 lmm_save->lmm_layout_gen));
3257         lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
3258         lcm->lcm_entry_count = cpu_to_le16(1);
3259
3260         lcme = &lcm->lcm_entries[0];
3261         lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
3262         lcme->lcme_extent.e_start = 0;
3263         lcme->lcme_extent.e_end = cpu_to_le64(OBD_OBJECT_EOF);
3264         lcme->lcme_offset = cpu_to_le32(sizeof(*lcm) + sizeof(*lcme));
3265         lcme->lcme_size = cpu_to_le32(blob_size);
3266
3267         memcpy((char *)lcm + lcme->lcme_offset, (char *)lmm_save, blob_size);
3268
3269         EXIT;
3270 out:
3271         if (lmm_save)
3272                 OBD_FREE_LARGE(lmm_save, blob_size);
3273         return rc;
3274 }
3275
3276 /**
3277  * Merge layouts to form a mirrored file.
3278  */
3279 static int lod_declare_layout_merge(const struct lu_env *env,
3280                 struct dt_object *dt, const struct lu_buf *mbuf,
3281                 struct thandle *th)
3282 {
3283         struct lod_thread_info *info = lod_env_info(env);
3284         struct lu_attr *layout_attr = &info->lti_layout_attr;
3285         struct lu_buf *buf = &info->lti_buf;
3286         struct lod_object *lo = lod_dt_obj(dt);
3287         struct lov_comp_md_v1 *lcm;
3288         struct lov_comp_md_v1 *cur_lcm;
3289         struct lov_comp_md_v1 *merge_lcm;
3290         struct lov_comp_md_entry_v1 *lcme;
3291         struct lov_mds_md_v1 *lmm;
3292         size_t size = 0;
3293         size_t offset;
3294         __u16 cur_entry_count;
3295         __u16 merge_entry_count;
3296         __u32 id = 0;
3297         __u16 mirror_id = 0;
3298         __u32 mirror_count;
3299         int rc, i;
3300         bool merge_has_dom;
3301
3302         ENTRY;
3303
3304         merge_lcm = mbuf->lb_buf;
3305         if (mbuf->lb_len < sizeof(*merge_lcm))
3306                 RETURN(-EINVAL);
3307
3308         /* must be an existing layout from disk */
3309         if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
3310                 RETURN(-EINVAL);
3311
3312         merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
3313
3314         /* do not allow to merge two mirrored files */
3315         if (le16_to_cpu(merge_lcm->lcm_mirror_count))
3316                 RETURN(-EBUSY);
3317
3318         /* verify the target buffer */
3319         rc = lod_get_lov_ea(env, lo);
3320         if (rc <= 0)
3321                 RETURN(rc ? : -ENODATA);
3322
3323         cur_lcm = info->lti_ea_store;
3324         switch (le32_to_cpu(cur_lcm->lcm_magic)) {
3325         case LOV_MAGIC_V1:
3326         case LOV_MAGIC_V3:
3327                 rc = lod_layout_convert(info);
3328                 break;
3329         case LOV_MAGIC_COMP_V1:
3330         case LOV_MAGIC_SEL:
3331                 rc = 0;
3332                 break;
3333         default:
3334                 rc = -EINVAL;
3335         }
3336         if (rc)
3337                 RETURN(rc);
3338
3339         /* info->lti_ea_store could be reallocated in lod_layout_convert() */
3340         cur_lcm = info->lti_ea_store;
3341         cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
3342
3343         /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
3344         mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
3345         if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
3346                 RETURN(-ERANGE);
3347
3348         /* size of new layout */
3349         size = le32_to_cpu(cur_lcm->lcm_size) +
3350                le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
3351
3352         memset(buf, 0, sizeof(*buf));
3353         lu_buf_alloc(buf, size);
3354         if (buf->lb_buf == NULL)
3355                 RETURN(-ENOMEM);
3356
3357         lcm = buf->lb_buf;
3358         memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
3359
3360         offset = sizeof(*lcm) +
3361                  sizeof(*lcme) * (cur_entry_count + merge_entry_count);
3362         for (i = 0; i < cur_entry_count; i++) {
3363                 struct lov_comp_md_entry_v1 *cur_lcme;
3364
3365                 lcme = &lcm->lcm_entries[i];
3366                 cur_lcme = &cur_lcm->lcm_entries[i];
3367
3368                 lcme->lcme_offset = cpu_to_le32(offset);
3369                 memcpy((char *)lcm + offset,
3370                        (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
3371                        le32_to_cpu(lcme->lcme_size));
3372
3373                 offset += le32_to_cpu(lcme->lcme_size);
3374
3375                 if (mirror_count == 1 &&
3376                     mirror_id_of(le32_to_cpu(lcme->lcme_id)) == 0) {
3377                         /* Add mirror from a non-flr file, create new mirror ID.
3378                          * Otherwise, keep existing mirror's component ID, used
3379                          * for mirror extension.
3380                          */
3381                         id = pflr_id(1, i + 1);
3382                         lcme->lcme_id = cpu_to_le32(id);
3383                 }
3384
3385                 id = max(le32_to_cpu(lcme->lcme_id), id);
3386         }
3387
3388         mirror_id = mirror_id_of(id) + 1;
3389
3390         /* check if first entry in new layout is DOM */
3391         lmm = (struct lov_mds_md_v1 *)((char *)merge_lcm +
3392                                         merge_lcm->lcm_entries[0].lcme_offset);
3393         merge_has_dom = lov_pattern(le32_to_cpu(lmm->lmm_pattern)) ==
3394                         LOV_PATTERN_MDT;
3395
3396         for (i = 0; i < merge_entry_count; i++) {
3397                 struct lov_comp_md_entry_v1 *merge_lcme;
3398
3399                 merge_lcme = &merge_lcm->lcm_entries[i];
3400                 lcme = &lcm->lcm_entries[cur_entry_count + i];
3401
3402                 *lcme = *merge_lcme;
3403                 lcme->lcme_offset = cpu_to_le32(offset);
3404                 if (merge_has_dom && i == 0)
3405                         lcme->lcme_flags |= cpu_to_le32(LCME_FL_STALE);
3406
3407                 id = pflr_id(mirror_id, i + 1);
3408                 lcme->lcme_id = cpu_to_le32(id);
3409
3410                 memcpy((char *)lcm + offset,
3411                        (char *)merge_lcm + le32_to_cpu(merge_lcme->lcme_offset),
3412                        le32_to_cpu(lcme->lcme_size));
3413
3414                 offset += le32_to_cpu(lcme->lcme_size);
3415         }
3416
3417         /* fixup layout information */
3418         lcm->lcm_size = cpu_to_le32(size);
3419         lcm->lcm_entry_count = cpu_to_le16(cur_entry_count + merge_entry_count);
3420         lcm->lcm_mirror_count = cpu_to_le16(mirror_count);
3421         if ((le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK) == LCM_FL_NONE)
3422                 lcm->lcm_flags = cpu_to_le32(LCM_FL_RDONLY);
3423
3424         rc = lod_striping_reload(env, lo, buf, 0);
3425         if (rc)
3426                 GOTO(out, rc);
3427
3428         lod_obj_inc_layout_gen(lo);
3429         lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3430
3431         /* transfer layout version to OST objects. */
3432         if (lo->ldo_mirror_count > 1) {
3433                 struct lod_obj_stripe_cb_data data = { {0} };
3434
3435                 layout_attr->la_valid = LA_LAYOUT_VERSION;
3436                 layout_attr->la_layout_version = 0;
3437                 data.locd_attr = layout_attr;
3438                 data.locd_declare = true;
3439                 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
3440                 rc = lod_obj_for_each_stripe(env, lo, th, &data);
3441                 if (rc)
3442                         GOTO(out, rc);
3443         }
3444
3445         rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), buf,
3446                                         XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3447
3448 out:
3449         lu_buf_free(buf);
3450         RETURN(rc);
3451 }
3452
3453 /**
3454  * Split layouts, just set the LOVEA with the layout from mbuf.
3455  */
3456 static int lod_declare_layout_split(const struct lu_env *env,
3457                 struct dt_object *dt, const struct lu_buf *mbuf,
3458                 struct thandle *th)
3459 {
3460         struct lod_thread_info *info = lod_env_info(env);
3461         struct lu_attr *layout_attr = &info->lti_layout_attr;
3462         struct lod_object *lo = lod_dt_obj(dt);
3463         struct lov_comp_md_v1 *lcm = mbuf->lb_buf;
3464         int rc;
3465         ENTRY;
3466
3467         rc = lod_striping_reload(env, lo, mbuf, LVF_ALL_STALE);
3468         if (rc)
3469                 RETURN(rc);
3470
3471         lod_obj_inc_layout_gen(lo);
3472         /* fix on-disk layout gen */
3473         lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3474
3475         /* transfer layout version to OST objects. */
3476         if (lo->ldo_mirror_count > 1) {
3477                 struct lod_obj_stripe_cb_data data = { {0} };
3478
3479                 layout_attr->la_valid = LA_LAYOUT_VERSION;
3480                 layout_attr->la_layout_version = 0;
3481                 data.locd_attr = layout_attr;
3482                 data.locd_declare = true;
3483                 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
3484                 rc = lod_obj_for_each_stripe(env, lo, th, &data);
3485                 if (rc)
3486                         RETURN(rc);
3487         }
3488
3489         rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), mbuf,
3490                                        XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3491         RETURN(rc);
3492 }
3493
3494 static int lod_layout_declare_or_purge_mirror(const struct lu_env *env,
3495                         struct dt_object *dt, const struct lu_buf *buf,
3496                         struct thandle *th, bool declare)
3497 {
3498         struct lod_thread_info *info = lod_env_info(env);
3499         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3500         struct lod_object *lo = lod_dt_obj(dt);
3501         struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
3502         struct lov_comp_md_entry_v1 *entry;
3503         struct lov_mds_md_v1 *lmm;
3504         struct dt_object **sub_objs = NULL;
3505         int rc = 0, i, k, array_count = 0;
3506
3507         ENTRY;
3508
3509         /**
3510          * other ops (like lod_declare_destroy) could destroying sub objects
3511          * as well.
3512          */
3513         mutex_lock(&lo->ldo_layout_mutex);
3514
3515         if (!declare) {
3516                 /* prepare sub-objects array */
3517                 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3518                         entry = &comp_v1->lcm_entries[i];
3519
3520                         if (!(entry->lcme_flags & LCME_FL_INIT))
3521                                 continue;
3522
3523                         lmm = (struct lov_mds_md_v1 *)
3524                                         ((char *)comp_v1 + entry->lcme_offset);
3525                         array_count += lmm->lmm_stripe_count;
3526                 }
3527                 OBD_ALLOC_PTR_ARRAY(sub_objs, array_count);
3528                 if (sub_objs == NULL) {
3529                         mutex_unlock(&lo->ldo_layout_mutex);
3530                         RETURN(-ENOMEM);
3531                 }
3532         }
3533
3534         k = 0;  /* sub_objs index */
3535         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3536                 struct lov_ost_data_v1 *objs;
3537                 struct lu_object *o, *n;
3538                 struct dt_object *dto;
3539                 struct lu_device *nd;
3540                 struct lov_mds_md_v3 *v3;
3541                 __u32 idx;
3542                 int j;
3543
3544                 entry = &comp_v1->lcm_entries[i];
3545
3546                 if (!(entry->lcme_flags & LCME_FL_INIT))
3547                         continue;
3548
3549                 lmm = (struct lov_mds_md_v1 *)
3550                                 ((char *)comp_v1 + entry->lcme_offset);
3551                 v3 = (struct lov_mds_md_v3 *)lmm;
3552                 if (lmm->lmm_magic == LOV_MAGIC_V3)
3553                         objs = &v3->lmm_objects[0];
3554                 else
3555                         objs = &lmm->lmm_objects[0];
3556
3557                 for (j = 0; j < lmm->lmm_stripe_count; j++) {
3558                         idx = objs[j].l_ost_idx;
3559                         rc = ostid_to_fid(&info->lti_fid, &objs[j].l_ost_oi,
3560                                           idx);
3561                         if (rc)
3562                                 GOTO(out, rc);
3563
3564                         if (!fid_is_sane(&info->lti_fid)) {
3565                                 CERROR("%s: sub-object insane fid "DFID"\n",
3566                                        lod2obd(d)->obd_name,
3567                                        PFID(&info->lti_fid));
3568                                 GOTO(out, rc = -EINVAL);
3569                         }
3570
3571                         lod_getref(&d->lod_ost_descs);
3572
3573                         rc = validate_lod_and_idx(d, idx);
3574                         if (unlikely(rc)) {
3575                                 lod_putref(d, &d->lod_ost_descs);
3576                                 GOTO(out, rc);
3577                         }
3578
3579                         nd = &OST_TGT(d, idx)->ltd_tgt->dd_lu_dev;
3580                         lod_putref(d, &d->lod_ost_descs);
3581
3582                         o = lu_object_find_at(env, nd, &info->lti_fid, NULL);
3583                         if (IS_ERR(o))
3584                                 GOTO(out, rc = PTR_ERR(o));
3585
3586                         n = lu_object_locate(o->lo_header, nd->ld_type);
3587                         if (unlikely(!n)) {
3588                                 lu_object_put(env, n);
3589                                 GOTO(out, rc = -ENOENT);
3590                         }
3591
3592                         dto = container_of(n, struct dt_object, do_lu);
3593
3594                         if (declare) {
3595                                 rc = lod_sub_declare_destroy(env, dto, th);
3596                                 dt_object_put(env, dto);
3597                                 if (rc)
3598                                         GOTO(out, rc);
3599                         } else {
3600                                 /**
3601                                  * collect to-be-destroyed sub objects, the
3602                                  * reference would be released after actual
3603                                  * deletion.
3604                                  */
3605                                 sub_objs[k] = dto;
3606                                 k++;
3607                         }
3608                 } /* for each stripe */
3609         } /* for each component in the mirror */
3610 out:
3611         if (!declare) {
3612                 i = 0;
3613                 if (!rc) {
3614                         /* destroy the sub objects */
3615                         for (; i < k; i++) {
3616                                 rc = lod_sub_destroy(env, sub_objs[i], th);
3617                                 if (rc)
3618                                         break;
3619                                 dt_object_put(env, sub_objs[i]);
3620                         }
3621                 }
3622                 /**
3623                  * if a sub object destroy failed, we'd release sub objects
3624                  * reference get from above sub_objs collection.
3625                  */
3626                 for (; i < k; i++)
3627                         dt_object_put(env, sub_objs[i]);
3628
3629                 OBD_FREE_PTR_ARRAY(sub_objs, array_count);
3630         }
3631         mutex_unlock(&lo->ldo_layout_mutex);
3632
3633         RETURN(rc);
3634 }
3635
3636 /**
3637  * Purge layouts, delete sub objects in the mirror stored in the vic_buf,
3638  * and set the LOVEA with the layout from mbuf.
3639  */
3640 static int lod_declare_layout_purge(const struct lu_env *env,
3641                 struct dt_object *dt, const struct lu_buf *buf,
3642                 struct thandle *th)
3643 {
3644         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3645         struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
3646         int rc;
3647
3648         ENTRY;
3649
3650         if (le32_to_cpu(comp_v1->lcm_magic) != LOV_MAGIC_COMP_V1) {
3651                 CERROR("%s: invalid layout magic %#x != %#x\n",
3652                        lod2obd(d)->obd_name, le32_to_cpu(comp_v1->lcm_magic),
3653                        LOV_MAGIC_COMP_V1);
3654                 RETURN(-EINVAL);
3655         }
3656
3657         if (cpu_to_le32(LOV_MAGIC_COMP_V1) != LOV_MAGIC_COMP_V1)
3658                 lustre_swab_lov_comp_md_v1(comp_v1);
3659
3660         /* from now on, @buf contains cpu endian data */
3661
3662         if (comp_v1->lcm_mirror_count != 0) {
3663                 CERROR("%s: can only purge one mirror from "DFID"\n",
3664                        lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3665                 RETURN(-EINVAL);
3666         }
3667
3668         /* delcare sub objects deletion in the mirror stored in @buf */
3669         rc = lod_layout_declare_or_purge_mirror(env, dt, buf, th, true);
3670         RETURN(rc);
3671 }
3672
3673 /* delete sub objects from the mirror stored in @buf */
3674 static int lod_layout_purge(const struct lu_env *env, struct dt_object *dt,
3675                             const struct lu_buf *buf, struct thandle *th)
3676 {
3677         int rc;
3678
3679         ENTRY;
3680         rc = lod_layout_declare_or_purge_mirror(env, dt, buf, th, false);
3681         RETURN(rc);
3682 }
3683
3684 /**
3685  * Implementation of dt_object_operations::do_declare_xattr_set.
3686  *
3687  * \see dt_object_operations::do_declare_xattr_set() in the API description
3688  * for details.
3689  *
3690  * the extension to the API:
3691  *   - declaring LOVEA requests striping creation
3692  *   - LU_XATTR_REPLACE means layout swap
3693  */
3694 static int lod_declare_xattr_set(const struct lu_env *env,
3695                                  struct dt_object *dt,
3696                                  const struct lu_buf *buf,
3697                                  const char *name, int fl,
3698                                  struct thandle *th)
3699 {
3700         struct dt_object *next = dt_object_child(dt);
3701         struct lu_attr   *attr = &lod_env_info(env)->lti_attr;
3702         __u32             mode;
3703         int               rc;
3704         ENTRY;
3705
3706         mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
3707         if ((S_ISREG(mode) || mode == 0) &&
3708             !(fl & (LU_XATTR_REPLACE | LU_XATTR_MERGE | LU_XATTR_SPLIT |
3709                     LU_XATTR_PURGE)) &&
3710             (strcmp(name, XATTR_NAME_LOV) == 0 ||
3711              strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
3712                 /*
3713                  * this is a request to create object's striping.
3714                  *
3715                  * allow to declare predefined striping on a new (!mode) object
3716                  * which is supposed to be replay of regular file creation
3717                  * (when LOV setting is declared)
3718                  *
3719                  * LU_XATTR_REPLACE is set to indicate a layout swap
3720                  */
3721                 if (dt_object_exists(dt)) {
3722                         rc = dt_attr_get(env, next, attr);
3723                         if (rc)
3724                                 RETURN(rc);
3725                 } else {
3726                         memset(attr, 0, sizeof(*attr));
3727                         attr->la_valid = LA_TYPE | LA_MODE;
3728                         attr->la_mode = S_IFREG;
3729                 }
3730                 rc = lod_declare_striped_create(env, dt, attr, buf, th);
3731         } else if (fl & LU_XATTR_MERGE) {
3732                 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3733                         strcmp(name, XATTR_LUSTRE_LOV) == 0);
3734                 rc = lod_declare_layout_merge(env, dt, buf, th);
3735         } else if (fl & LU_XATTR_SPLIT) {
3736                 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3737                         strcmp(name, XATTR_LUSTRE_LOV) == 0);
3738                 rc = lod_declare_layout_split(env, dt, buf, th);
3739         } else if (fl & LU_XATTR_PURGE) {
3740                 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3741                         strcmp(name, XATTR_LUSTRE_LOV) == 0);
3742                 rc = lod_declare_layout_purge(env, dt, buf, th);
3743         } else if (S_ISREG(mode) &&
3744                    strlen(name) >= sizeof(XATTR_LUSTRE_LOV) + 3 &&
3745                    allowed_lustre_lov(name)) {
3746                 /*
3747                  * this is a request to modify object's striping.
3748                  * add/set/del component(s).
3749                  */
3750                 if (!dt_object_exists(dt))
3751                         RETURN(-ENOENT);
3752
3753                 rc = lod_declare_modify_layout(env, dt, name, buf, th);
3754         } else if (S_ISDIR(mode)) {
3755                 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
3756         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3757                 rc = lod_replace_parent_fid(env, dt, buf, th, true);
3758         } else {
3759                 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
3760         }
3761
3762         RETURN(rc);
3763 }
3764
3765 /**
3766  * Apply xattr changes to the object.
3767  *
3768  * Applies xattr changes to the object and the stripes if the latter exist.
3769  *
3770  * \param[in] env       execution environment
3771  * \param[in] dt        object
3772  * \param[in] buf       buffer pointing to the new value of xattr
3773  * \param[in] name      name of xattr
3774  * \param[in] fl        flags
3775  * \param[in] th        transaction handle
3776  *
3777  * \retval              0 on success
3778  * \retval              negative if failed
3779  */
3780 static int lod_xattr_set_internal(const struct lu_env *env,
3781                                   struct dt_object *dt,
3782                                   const struct lu_buf *buf,
3783                                   const char *name, int fl,
3784                                   struct thandle *th)
3785 {
3786         struct dt_object        *next = dt_object_child(dt);
3787         struct lod_object       *lo = lod_dt_obj(dt);
3788         int                     rc;
3789         int                     i;
3790         ENTRY;
3791
3792         rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3793         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3794                 RETURN(rc);
3795
3796         /* Note: Do not set LinkEA on sub-stripes, otherwise
3797          * it will confuse the fid2path process(see mdt_path_current()).
3798          * The linkEA between master and sub-stripes is set in
3799          * lod_xattr_set_lmv(). */
3800         if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
3801                 RETURN(0);
3802
3803         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3804                 if (!lo->ldo_stripe[i])
3805                         continue;
3806
3807                 if (!dt_object_exists(lo->ldo_stripe[i]))
3808                         continue;
3809
3810                 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
3811                                        fl, th);
3812                 if (rc != 0)
3813                         break;
3814         }
3815
3816         RETURN(rc);
3817 }
3818
3819 /**
3820  * Delete an extended attribute.
3821  *
3822  * Deletes specified xattr from the object and the stripes if the latter exist.
3823  *
3824  * \param[in] env       execution environment
3825  * \param[in] dt        object
3826  * \param[in] name      name of xattr
3827  * \param[in] th        transaction handle
3828  *
3829  * \retval              0 on success
3830  * \retval              negative if failed
3831  */
3832 static int lod_xattr_del_internal(const struct lu_env *env,
3833                                   struct dt_object *dt,
3834                                   const char *name, struct thandle *th)
3835 {
3836         struct dt_object *next = dt_object_child(dt);
3837         struct lod_object *lo = lod_dt_obj(dt);
3838         int i;
3839         int rc;
3840
3841         ENTRY;
3842
3843         rc = lod_sub_xattr_del(env, next, name, th);
3844         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3845                 RETURN(rc);
3846
3847         if (lo->ldo_dir_stripe_count == 0)
3848                 RETURN(rc);
3849
3850         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3851                 if (!lo->ldo_stripe[i])
3852                         continue;
3853
3854                 if (!dt_object_exists(lo->ldo_stripe[i]))
3855                         continue;
3856
3857                 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3858                 if (rc != 0)
3859                         break;
3860         }
3861
3862         RETURN(rc);
3863 }
3864
3865 /**
3866  * Set default striping on a directory.
3867  *
3868  * Sets specified striping on a directory object unless it matches the default
3869  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3870  * EA. This striping will be used when regular file is being created in this
3871  * directory.
3872  *
3873  * \param[in] env       execution environment
3874  * \param[in] dt        the striped object
3875  * \param[in] buf       buffer with the striping
3876  * \param[in] name      name of EA
3877  * \param[in] fl        xattr flag (see OSD API description)
3878  * \param[in] th        transaction handle
3879  *
3880  * \retval              0 on success
3881  * \retval              negative if failed
3882  */
3883 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
3884                                     struct dt_object *dt,
3885                                     const struct lu_buf *buf,
3886                                     const char *name, int fl,
3887                                     struct thandle *th)
3888 {
3889         struct lov_user_md_v1   *lum;
3890         struct lov_user_md_v3   *v3 = NULL;
3891         const char              *pool_name = NULL;
3892         int                      rc;
3893         bool                     is_del;
3894         ENTRY;
3895
3896         LASSERT(buf != NULL && buf->lb_buf != NULL);
3897         lum = buf->lb_buf;
3898
3899         switch (lum->lmm_magic) {
3900         case LOV_USER_MAGIC_SPECIFIC:
3901         case LOV_USER_MAGIC_V3:
3902                 v3 = buf->lb_buf;
3903                 if (lov_pool_is_reserved(v3->lmm_pool_name))
3904                         memset(v3->lmm_pool_name, 0, sizeof(v3->lmm_pool_name));
3905                 else if (v3->lmm_pool_name[0] != '\0')
3906                         pool_name = v3->lmm_pool_name;
3907                 fallthrough;
3908         case LOV_USER_MAGIC_V1:
3909                 /* if { size, offset, count } = { 0, -1, 0 } and no pool
3910                  * (i.e. all default values specified) then delete default
3911                  * striping from dir. */
3912                 CDEBUG(D_LAYOUT,
3913                        "set default striping: sz %u # %u offset %d %s %s\n",
3914                        (unsigned)lum->lmm_stripe_size,
3915                        (unsigned)lum->lmm_stripe_count,
3916                        (int)lum->lmm_stripe_offset,
3917                        v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
3918
3919                 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
3920                                              lum->lmm_stripe_count,
3921                                              lum->lmm_stripe_offset,
3922                                              pool_name);
3923                 break;
3924         case LOV_USER_MAGIC_COMP_V1:
3925         {
3926                 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lum;
3927                 struct lov_comp_md_entry_v1 *lcme;
3928                 int i, comp_cnt;
3929
3930                 comp_cnt = le16_to_cpu(lcm->lcm_entry_count);
3931                 for (i = 0; i < comp_cnt; i++) {
3932                         lcme = &lcm->lcm_entries[i];
3933                         if (lcme->lcme_flags & cpu_to_le32(LCME_FL_EXTENSION)) {
3934                                 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_SEL);
3935                                 break;
3936                         }
3937                 }
3938
3939                 is_del = false;
3940                 break;
3941         }
3942         default:
3943                 CERROR("Invalid magic %x\n", lum->lmm_magic);
3944                 RETURN(-EINVAL);
3945         }
3946
3947         if (is_del) {
3948                 rc = lod_xattr_del_internal(env, dt, name, th);
3949                 if (rc == -ENODATA)
3950                         rc = 0;
3951         } else {
3952                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3953         }
3954
3955         RETURN(rc);
3956 }
3957
3958 static int lod_get_default_lov_striping(const struct lu_env *env,
3959                                        struct lod_object *lo,
3960                                        struct lod_default_striping *lds,
3961                                        struct dt_allocation_hint *ah);
3962
3963 /**
3964  * Helper function to convert compound layout to compound layout with
3965  * pool
3966  *
3967  * Copy lcm_entries array of \a src to \a tgt. Replace lov_user_md_v1
3968  * components of \a src with lov_user_md_v3 using \a pool.
3969  *
3970  * \param[in] src       source layout
3971  * \param[in] pool      pool to use in \a tgt
3972  * \param[out] tgt      target layout
3973  */
3974 static void embed_pool_to_comp_v1(const struct lov_comp_md_v1 *src,
3975                                   const char *pool,
3976                                   struct lov_comp_md_v1 *tgt)
3977 {
3978         size_t shift;
3979         struct lov_user_md_v1 *lum;
3980         struct lov_user_md_v3 *lum3;
3981         struct lov_comp_md_entry_v1 *entry;
3982         int i;
3983         __u32 offset;
3984
3985         entry = tgt->lcm_entries;
3986         shift = 0;
3987         for (i = 0; i < le16_to_cpu(src->lcm_entry_count); i++, entry++) {
3988                 *entry = src->lcm_entries[i];
3989                 offset = le32_to_cpu(src->lcm_entries[i].lcme_offset);
3990                 entry->lcme_offset = cpu_to_le32(offset + shift);
3991
3992                 lum = (struct lov_user_md_v1 *)((char *)src + offset);
3993                 lum3 = (struct lov_user_md_v3 *)((char *)tgt + offset + shift);
3994                 *(struct lov_user_md_v1 *)lum3 = *lum;
3995                 if (lum->lmm_pattern == cpu_to_le32(LOV_PATTERN_MDT)) {
3996                         lum3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
3997                 } else {
3998                         lum3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
3999                         entry->lcme_size = cpu_to_le32(sizeof(*lum3));
4000                         strlcpy(lum3->lmm_pool_name, pool,
4001                                 sizeof(lum3->lmm_pool_name));
4002                         shift += sizeof(*lum3) - sizeof(*lum);
4003                 }
4004         }
4005 }
4006
4007 /**
4008  * Set default striping on a directory.
4009  *
4010  * Sets specified striping on a directory object unless it matches the default
4011  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
4012  * EA. This striping will be used when regular file is being created in this
4013  * directory.
4014  * If current default striping includes a pool but specifed striping
4015  * does not - retain the pool if it exists.
4016  *
4017  * \param[in] env       execution environment
4018  * \param[in] dt        the striped object
4019  * \param[in] buf       buffer with the striping
4020  * \param[in] name      name of EA
4021  * \param[in] fl        xattr flag (see OSD API description)
4022  * \param[in] th        transaction handle
4023  *
4024  * \retval              0 on success
4025  * \retval              negative if failed
4026  */
4027 static int lod_xattr_set_default_lov_on_dir(const struct lu_env *env,
4028                                             struct dt_object *dt,
4029                                             const struct lu_buf *buf,
4030                                             const char *name, int fl,
4031                                             struct thandle *th)
4032 {
4033         struct lod_default_striping     *lds = lod_lds_buf_get(env);
4034         struct lov_user_md_v1           *v1 = buf->lb_buf;
4035         char                             pool[LOV_MAXPOOLNAME + 1];
4036         bool                             is_del;
4037         int                              rc;
4038
4039         ENTRY;
4040
4041         /* get existing striping config */
4042         rc = lod_get_default_lov_striping(env, lod_dt_obj(dt), lds, NULL);
4043         if (rc)
4044                 RETURN(rc);
4045
4046         memset(pool, 0, sizeof(pool));
4047         if (lds->lds_def_striping_set == 1)
4048                 lod_layout_get_pool(lds->lds_def_comp_entries,
4049                                     lds->lds_def_comp_cnt, pool,
4050                                     sizeof(pool));
4051
4052         is_del = LOVEA_DELETE_VALUES(v1->lmm_stripe_size,
4053                                      v1->lmm_stripe_count,
4054                                      v1->lmm_stripe_offset,
4055                                      NULL);
4056
4057         /* Retain the pool name if it is not given */
4058         if (v1->lmm_magic == LOV_USER_MAGIC_V1 && pool[0] != '\0' &&
4059             !is_del) {
4060                 struct lod_thread_info *info = lod_env_info(env);
4061                 struct lov_user_md_v3 *v3  = info->lti_ea_store;
4062
4063                 memset(v3, 0, sizeof(*v3));
4064                 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
4065                 v3->lmm_pattern = cpu_to_le32(v1->lmm_pattern);
4066                 v3->lmm_stripe_count = cpu_to_le32(v1->lmm_stripe_count);
4067                 v3->lmm_stripe_offset = cpu_to_le32(v1->lmm_stripe_offset);
4068                 v3->lmm_stripe_size = cpu_to_le32(v1->lmm_stripe_size);
4069
4070                 strlcpy(v3->lmm_pool_name, pool, sizeof(v3->lmm_pool_name));
4071
4072                 info->lti_buf.lb_buf = v3;
4073                 info->lti_buf.lb_len = sizeof(*v3);
4074                 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4075                                               name, fl, th);
4076         } else if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1 &&
4077                    pool[0] != '\0' && !is_del) {
4078                 /*
4079                  * try to retain the pool from default layout if the
4080                  * specified component layout does not provide pool
4081                  * info explicitly
4082                  */
4083                 struct lod_thread_info *info = lod_env_info(env);
4084                 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
4085                 struct lov_comp_md_v1 *comp_v1p;
4086                 struct lov_user_md_v1 *lum;
4087                 int entry_count;
4088                 int i;
4089                 __u32 offset;
4090                 struct lov_comp_md_entry_v1 *entry;
4091                 int size;
4092
4093                 entry_count = le16_to_cpu(comp_v1->lcm_entry_count);
4094                 size = sizeof(*comp_v1) +
4095                         entry_count * sizeof(comp_v1->lcm_entries[0]);
4096                 entry = comp_v1->lcm_entries;
4097                 for (i = 0; i < entry_count; i++, entry++) {
4098                         offset = le32_to_cpu(entry->lcme_offset);
4099                         lum = (struct lov_user_md_v1 *)((char *)comp_v1 +
4100                                                         offset);
4101                         if (le32_to_cpu(lum->lmm_magic) != LOV_USER_MAGIC_V1)
4102                                 /* the i-th component includes pool info */
4103                                 break;
4104                         if (lum->lmm_pattern == cpu_to_le32(LOV_PATTERN_MDT))
4105                                 size += sizeof(struct lov_user_md_v1);
4106                         else
4107                                 size += sizeof(struct lov_user_md_v3);
4108                 }
4109
4110                 if (i == entry_count) {
4111                         /*
4112                          * re-compose the layout to include the pool for
4113                          * each component
4114                          */
4115                         if (info->lti_ea_store_size < size)
4116                                 rc = lod_ea_store_resize(info, size);
4117
4118                         if (rc == 0) {
4119                                 comp_v1p = info->lti_ea_store;
4120                                 *comp_v1p = *comp_v1;
4121                                 comp_v1p->lcm_size = cpu_to_le32(size);
4122                                 embed_pool_to_comp_v1(comp_v1, pool, comp_v1p);
4123
4124                                 info->lti_buf.lb_buf = comp_v1p;
4125                                 info->lti_buf.lb_len = size;
4126                                 rc = lod_xattr_set_lov_on_dir(env, dt,
4127                                                               &info->lti_buf,
4128                                                               name, fl, th);
4129                         }
4130                 } else {
4131                         rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl,
4132                                                       th);
4133                 }
4134         } else {
4135                 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
4136         }
4137
4138         if (lds->lds_def_striping_set == 1 && lds->lds_def_comp_entries != NULL)
4139                 lod_free_def_comp_entries(lds);
4140
4141         RETURN(rc);
4142 }
4143
4144 /**
4145  * Set default striping on a directory object.
4146  *
4147  * Sets specified striping on a directory object unless it matches the default
4148  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
4149  * EA. This striping will be used when a new directory is being created in the
4150  * directory.
4151  *
4152  * \param[in] env       execution environment
4153  * \param[in] dt        the striped object
4154  * \param[in] buf       buffer with the striping
4155  * \param[in] name      name of EA
4156  * \param[in] fl        xattr flag (see OSD API description)
4157  * \param[in] th        transaction handle
4158  *
4159  * \retval              0 on success
4160  * \retval              negative if failed
4161  */
4162 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
4163                                             struct dt_object *dt,
4164                                             const struct lu_buf *buf,
4165                                             const char *name, int fl,
4166                                             struct thandle *th)
4167 {
4168         struct lmv_user_md_v1 *lum;
4169         int rc;
4170
4171         ENTRY;
4172
4173         LASSERT(buf != NULL && buf->lb_buf != NULL);
4174         lum = buf->lb_buf;
4175
4176         CDEBUG(D_INFO,
4177                "set default stripe_count # %u stripe_offset %d hash %u\n",
4178               le32_to_cpu(lum->lum_stripe_count),
4179               (int)le32_to_cpu(lum->lum_stripe_offset),
4180               le32_to_cpu(lum->lum_hash_type));
4181
4182         if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
4183                                  le32_to_cpu(lum->lum_stripe_offset)) &&
4184             le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
4185                 rc = lod_xattr_del_internal(env, dt, name, th);
4186                 if (rc == -ENODATA)
4187                         rc = 0;
4188         } else {
4189                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
4190                 if (rc != 0)
4191                         RETURN(rc);
4192         }
4193
4194         RETURN(rc);
4195 }
4196
4197 /**
4198  * Turn directory into a striped directory.
4199  *
4200  * During replay the client sends the striping created before MDT
4201  * failure, then the layer above LOD sends this defined striping
4202  * using ->do_xattr_set(), so LOD uses this method to replay creation
4203  * of the stripes. Notice the original information for the striping
4204  * (#stripes, FIDs, etc) was transferred in declare path.
4205  *
4206  * \param[in] env       execution environment
4207  * \param[in] dt        the striped object
4208  * \param[in] buf       not used currently
4209  * \param[in] name      not used currently
4210  * \param[in] fl        xattr flag (see OSD API description)
4211  * \param[in] th        transaction handle
4212  *
4213  * \retval              0 on success
4214  * \retval              negative if failed
4215  */
4216 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
4217                              const struct lu_buf *buf, const char *name,
4218                              int fl, struct thandle *th)
4219 {
4220         struct lod_object       *lo = lod_dt_obj(dt);
4221         struct lod_thread_info  *info = lod_env_info(env);
4222         struct lu_attr          *attr = &info->lti_attr;
4223         struct dt_object_format *dof = &info->lti_format;
4224         struct lu_buf           lmv_buf;
4225         struct lu_buf           slave_lmv_buf;
4226         struct lmv_mds_md_v1    *lmm;
4227         struct lmv_mds_md_v1    *slave_lmm = NULL;
4228         struct dt_insert_rec    *rec = &info->lti_dt_rec;
4229         int                     i;
4230         int                     rc;
4231         ENTRY;
4232
4233         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4234                 RETURN(-ENOTDIR);
4235
4236         /* The stripes are supposed to be allocated in declare phase,
4237          * if there are no stripes being allocated, it will skip */
4238         if (lo->ldo_dir_stripe_count == 0) {
4239                 if (lo->ldo_is_foreign) {
4240                         rc = lod_sub_xattr_set(env, dt_object_child(dt), buf,
4241                                                XATTR_NAME_LMV, fl, th);
4242                         if (rc != 0)
4243                                 RETURN(rc);
4244                 }
4245                 RETURN(0);
4246         }
4247
4248         rc = dt_attr_get(env, dt_object_child(dt), attr);
4249         if (rc != 0)
4250                 RETURN(rc);
4251
4252         attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_FLAGS |
4253                          LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
4254         dof->dof_type = DFT_DIR;
4255
4256         rc = lod_prep_lmv_md(env, dt, &lmv_buf);
4257         if (rc != 0)
4258                 RETURN(rc);
4259         lmm = lmv_buf.lb_buf;
4260
4261         OBD_ALLOC_PTR(slave_lmm);
4262         if (slave_lmm == NULL)
4263                 RETURN(-ENOMEM);
4264
4265         lod_prep_slave_lmv_md(slave_lmm, lmm);
4266         slave_lmv_buf.lb_buf = slave_lmm;
4267         slave_lmv_buf.lb_len = sizeof(*slave_lmm);
4268
4269         rec->rec_type = S_IFDIR;
4270         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4271                 struct dt_object *dto = lo->ldo_stripe[i];
4272                 char *stripe_name = info->lti_key;
4273                 struct lu_name *sname;
4274                 struct linkea_data ldata = { NULL };
4275                 struct lu_buf linkea_buf;
4276
4277                 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
4278                 if (!dto)
4279                         continue;
4280
4281                 /* fail a remote stripe creation */
4282                 if (i && CFS_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_CREATE))
4283                         continue;
4284
4285                 /* don't create stripe if:
4286                  * 1. it's source stripe of migrating directory
4287                  * 2. it's existed stripe of splitting directory
4288                  */
4289                 if ((lod_is_migrating(lo) && i >= lo->ldo_dir_migrate_offset) ||
4290                     (lod_is_splitting(lo) && i < lo->ldo_dir_split_offset)) {
4291                         if (!dt_object_exists(dto))
4292                                 GOTO(out, rc = -EINVAL);
4293                 } else {
4294                         dt_write_lock(env, dto, DT_TGT_CHILD);
4295                         rc = lod_sub_create(env, dto, attr, NULL, dof, th);
4296                         if (rc != 0) {
4297                                 dt_write_unlock(env, dto);
4298                                 GOTO(out, rc);
4299                         }
4300
4301                         rc = lod_sub_ref_add(env, dto, th);
4302                         dt_write_unlock(env, dto);
4303                         if (rc != 0)
4304                                 GOTO(out, rc);
4305
4306                         rec->rec_fid = lu_object_fid(&dto->do_lu);
4307                         rc = lod_sub_insert(env, dto,
4308                                             (const struct dt_rec *)rec,
4309                                             (const struct dt_key *)dot, th);
4310                         if (rc != 0)
4311                                 GOTO(out, rc);
4312                 }
4313
4314                 if (!CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
4315                     cfs_fail_val != i) {
4316                         if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
4317                             cfs_fail_val == i)
4318                                 slave_lmm->lmv_master_mdt_index =
4319                                                         cpu_to_le32(i + 1);
4320                         else
4321                                 slave_lmm->lmv_master_mdt_index =
4322                                                         cpu_to_le32(i);
4323
4324                         rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
4325                                                XATTR_NAME_LMV, 0, th);
4326                         if (rc != 0)
4327                                 GOTO(out, rc);
4328                 }
4329
4330                 /* don't insert stripe if it's existed stripe of splitting
4331                  * directory (this directory is striped).
4332                  * NB, plain directory will insert itself as the first
4333                  * stripe in target.
4334                  */
4335                 if (lod_is_splitting(lo) && lo->ldo_dir_split_offset > 1 &&
4336                     lo->ldo_dir_split_offset > i)
4337                         continue;
4338
4339                 rec->rec_fid = lu_object_fid(&dt->do_lu);
4340                 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
4341                                     (const struct dt_key *)dotdot, th);
4342                 if (rc != 0)
4343                         GOTO(out, rc);
4344
4345                 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
4346                     cfs_fail_val == i)
4347                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4348                                  PFID(lu_object_fid(&dto->do_lu)), i + 1);
4349                 else
4350                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4351                                  PFID(lu_object_fid(&dto->do_lu)), i);
4352
4353                 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
4354                 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
4355                                       sname, lu_object_fid(&dt->do_lu));
4356                 if (rc != 0)
4357                         GOTO(out, rc);
4358
4359                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
4360                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
4361                 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
4362                                        XATTR_NAME_LINK, 0, th);
4363                 if (rc != 0)
4364                         GOTO(out, rc);
4365
4366                 rec->rec_fid = lu_object_fid(&dto->do_lu);
4367                 rc = lod_sub_insert(env, dt_object_child(dt),
4368                                     (const struct dt_rec *)rec,
4369                                     (const struct dt_key *)stripe_name, th);
4370                 if (rc != 0)
4371                         GOTO(out, rc);
4372
4373                 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
4374                 if (rc != 0)
4375                         GOTO(out, rc);
4376         }
4377
4378         if (!CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
4379                 rc = lod_sub_xattr_set(env, dt_object_child(dt),
4380                                        &lmv_buf, XATTR_NAME_LMV, fl, th);
4381 out:
4382         if (slave_lmm != NULL)
4383                 OBD_FREE_PTR(slave_lmm);
4384
4385         RETURN(rc);
4386 }
4387
4388 /**
4389  * Helper function to declare/execute creation of a striped directory
4390  *
4391  * Called in declare/create object path, prepare striping for a directory
4392  * and prepare defaults data striping for the objects to be created in
4393  * that directory. Notice the function calls "declaration" or "execution"
4394  * methods depending on \a declare param. This is a consequence of the
4395  * current approach while we don't have natural distributed transactions:
4396  * we basically execute non-local updates in the declare phase. So, the
4397  * arguments for the both phases are the same and this is the reason for
4398  * this function to exist.
4399  *
4400  * \param[in] env       execution environment
4401  * \param[in] dt        object
4402  * \param[in] attr      attributes the stripes will be created with
4403  * \param[in] lmu       lmv_user_md if MDT indices are specified
4404  * \param[in] dof       format of stripes (see OSD API description)
4405  * \param[in] th        transaction handle
4406  * \param[in] declare   where to call "declare" or "execute" methods
4407  *
4408  * \retval              0 on success
4409  * \retval              negative if failed
4410  */
4411 static int lod_dir_striping_create_internal(const struct lu_env *env,
4412                                             struct dt_object *dt,
4413                                             struct lu_attr *attr,
4414                                             const struct lu_buf *lmu,
4415                                             struct dt_object_format *dof,
4416                                             struct thandle *th,
4417                                             bool declare)
4418 {
4419         struct lod_thread_info *info = lod_env_info(env);
4420         struct lod_object *lo = lod_dt_obj(dt);
4421         const struct lod_default_striping *lds = lo->ldo_def_striping;
4422         int rc;
4423         ENTRY;
4424
4425         LASSERT(ergo(lds != NULL,
4426                      lds->lds_def_striping_set ||
4427                      lds->lds_dir_def_striping_set));
4428
4429         if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
4430                                  lo->ldo_dir_stripe_offset)) {
4431                 if (!lmu) {
4432                         struct lmv_user_md_v1 *v1 = info->lti_ea_store;
4433                         int stripe_count = lo->ldo_dir_stripe_count;
4434
4435                         if (info->lti_ea_store_size < sizeof(*v1)) {
4436                                 rc = lod_ea_store_resize(info, sizeof(*v1));
4437                                 if (rc != 0)
4438                                         RETURN(rc);
4439                                 v1 = info->lti_ea_store;
4440                         }
4441
4442                         memset(v1, 0, sizeof(*v1));
4443                         v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
4444                         v1->lum_stripe_count = cpu_to_le32(stripe_count);
4445                         v1->lum_stripe_offset =
4446                                         cpu_to_le32(lo->ldo_dir_stripe_offset);
4447
4448                         info->lti_buf.lb_buf = v1;
4449                         info->lti_buf.lb_len = sizeof(*v1);
4450                         lmu = &info->lti_buf;
4451                 }
4452
4453                 if (declare)
4454                         rc = lod_declare_xattr_set_lmv(env, dt, attr, lmu, dof,
4455                                                        th);
4456                 else
4457                         rc = lod_xattr_set_lmv(env, dt, lmu, XATTR_NAME_LMV, 0,
4458                                                th);
4459                 if (rc != 0)
4460                         RETURN(rc);
4461         } else {
4462                 /* foreign LMV EA case */
4463                 if (lmu) {
4464                         struct lmv_foreign_md *lfm = lmu->lb_buf;
4465
4466                         if (lfm->lfm_magic == LMV_MAGIC_FOREIGN) {
4467                                 rc = lod_declare_xattr_set_lmv(env, dt, attr,
4468                                                                lmu, dof, th);
4469                         }
4470                 } else {
4471                         if (lo->ldo_is_foreign) {
4472                                 LASSERT(lo->ldo_foreign_lmv != NULL &&
4473                                         lo->ldo_foreign_lmv_size > 0);
4474                                 info->lti_buf.lb_buf = lo->ldo_foreign_lmv;
4475                                 info->lti_buf.lb_len = lo->ldo_foreign_lmv_size;
4476                                 lmu = &info->lti_buf;
4477                                 rc = lod_xattr_set_lmv(env, dt, lmu,
4478                                                        XATTR_NAME_LMV, 0, th);
4479                         }
4480                 }
4481         }
4482
4483         /* Transfer default LMV striping from the parent */
4484         if (lds != NULL && lds->lds_dir_def_striping_set &&
4485             lds->lds_dir_def_max_inherit != LMV_INHERIT_END &&
4486             lds->lds_dir_def_max_inherit != LMV_INHERIT_NONE &&
4487             !(LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
4488                                  lds->lds_dir_def_stripe_offset) &&
4489               le32_to_cpu(lds->lds_dir_def_hash_type) !=
4490               LMV_HASH_TYPE_UNKNOWN)) {
4491                 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
4492
4493                 if (info->lti_ea_store_size < sizeof(*v1)) {
4494                         rc = lod_ea_store_resize(info, sizeof(*v1));
4495                         if (rc != 0)
4496                                 RETURN(rc);
4497                         v1 = info->lti_ea_store;
4498                 }
4499
4500                 memset(v1, 0, sizeof(*v1));
4501                 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
4502                 v1->lum_stripe_count =
4503                         cpu_to_le32(lds->lds_dir_def_stripe_count);
4504                 v1->lum_stripe_offset =
4505                         cpu_to_le32(lds->lds_dir_def_stripe_offset);
4506                 v1->lum_hash_type =
4507                         cpu_to_le32(lds->lds_dir_def_hash_type);
4508                 v1->lum_max_inherit =
4509                         lmv_inherit_next(lds->lds_dir_def_max_inherit);
4510                 v1->lum_max_inherit_rr =
4511                         lmv_inherit_rr_next(lds->lds_dir_def_max_inherit_rr);
4512
4513                 info->lti_buf.lb_buf = v1;
4514                 info->lti_buf.lb_len = sizeof(*v1);
4515                 if (declare)
4516                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4517                                                        XATTR_NAME_DEFAULT_LMV,
4518                                                        0, th);
4519                 else
4520                         rc = lod_xattr_set_default_lmv_on_dir(env, dt,
4521                                                   &info->lti_buf,
4522                                                   XATTR_NAME_DEFAULT_LMV, 0,
4523                                                   th);
4524                 if (rc != 0)
4525                         RETURN(rc);
4526         }
4527
4528         /* Transfer default LOV striping from the parent */
4529         if (lds != NULL && lds->lds_def_striping_set &&
4530             lds->lds_def_comp_cnt != 0) {
4531                 struct lov_mds_md *lmm;
4532                 int lmm_size = lod_comp_md_size(lo, true);
4533
4534                 if (info->lti_ea_store_size < lmm_size) {
4535                         rc = lod_ea_store_resize(info, lmm_size);
4536                         if (rc != 0)
4537                                 RETURN(rc);
4538                 }
4539                 lmm = info->lti_ea_store;
4540
4541                 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
4542                 if (rc != 0)
4543                         RETURN(rc);
4544
4545                 info->lti_buf.lb_buf = lmm;
4546                 info->lti_buf.lb_len = lmm_size;
4547
4548                 if (declare)
4549                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4550                                                        XATTR_NAME_LOV, 0, th);
4551                 else
4552                         rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4553                                                       XATTR_NAME_LOV, 0, th);
4554                 if (rc != 0)
4555                         RETURN(rc);
4556         }
4557
4558         /* ldo_def_striping is not allocated, clear after use, in case directory
4559          * layout is changed later.
4560          */
4561         if (!declare)
4562                 lo->ldo_def_striping = NULL;
4563
4564         RETURN(0);
4565 }
4566
4567 static int lod_declare_dir_striping_create(const struct lu_env *env,
4568                                            struct dt_object *dt,
4569                                            struct lu_attr *attr,
4570                                            struct lu_buf *lmu,
4571                                            struct dt_object_format *dof,
4572                                            struct thandle *th)
4573 {
4574         return lod_dir_striping_create_internal(env, dt, attr, lmu, dof, th,
4575                                                 true);
4576 }
4577
4578 static int lod_dir_striping_create(const struct lu_env *env,
4579                                    struct dt_object *dt,
4580                                    struct lu_attr *attr,
4581                                    struct dt_object_format *dof,
4582                                    struct thandle *th)
4583 {
4584         return lod_dir_striping_create_internal(env, dt, attr, NULL, dof, th,
4585                                                 false);
4586 }
4587
4588 /**
4589  * Make LOV EA for striped object.
4590  *
4591  * Generate striping information and store it in the LOV EA of the given
4592  * object. The caller must ensure nobody else is calling the function
4593  * against the object concurrently. The transaction must be started.
4594  * FLDB service must be running as well; it's used to map FID to the target,
4595  * which is stored in LOV EA.
4596  *
4597  * \param[in] env               execution environment for this thread
4598  * \param[in] lo                LOD object
4599  * \param[in] th                transaction handle
4600  *
4601  * \retval                      0 if LOV EA is stored successfully
4602  * \retval                      negative error number on failure
4603  */
4604 static int lod_generate_and_set_lovea(const struct lu_env *env,
4605                                       struct lod_object *lo,
4606                                       struct thandle *th)
4607 {
4608         struct lod_thread_info  *info = lod_env_info(env);
4609         struct dt_object        *next = dt_object_child(&lo->ldo_obj);
4610         struct lov_mds_md_v1    *lmm;
4611         int                      rc, lmm_size;
4612         ENTRY;
4613
4614         LASSERT(lo);
4615
4616         if (lo->ldo_comp_cnt == 0 && !lo->ldo_is_foreign) {
4617                 lod_striping_free_nolock(env, lo);
4618                 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
4619                 RETURN(rc);
4620         }
4621
4622         lmm_size = lod_comp_md_size(lo, false);
4623         if (info->lti_ea_store_size < lmm_size) {
4624                 rc = lod_ea_store_resize(info, lmm_size);
4625                 if (rc)
4626                         RETURN(rc);
4627         }
4628         lmm = info->lti_ea_store;
4629
4630         rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
4631         if (rc)
4632                 RETURN(rc);
4633
4634         info->lti_buf.lb_buf = lmm;
4635         info->lti_buf.lb_len = lmm_size;
4636         rc = lod_sub_xattr_set(env, next, &info->lti_buf,
4637                                XATTR_NAME_LOV, 0, th);
4638         RETURN(rc);
4639 }
4640
4641 static __u32 lod_gen_component_id(struct lod_object *lo,
4642                                   int mirror_id, int comp_idx);
4643
4644 /**
4645  * Repeat an existing component
4646  *
4647  * Creates a new layout by replicating an existing component.  Uses striping
4648  * policy from previous component as a template for the striping for the new
4649  * new component.
4650  *
4651  * New component starts with zero length, will be extended (or removed) before
4652  * returning layout to client.
4653  *
4654  * NB: Reallocates layout components array (lo->ldo_comp_entries), invalidating
4655  * any pre-existing pointers to components.  Handle with care.
4656  *
4657  * \param[in] env       execution environment for this thread
4658  * \param[in,out] lo    object to update the layout of
4659  * \param[in] index     index of component to copy
4660  *
4661  * \retval      0 on success
4662  * \retval      negative errno on error
4663  */
4664 static int lod_layout_repeat_comp(const struct lu_env *env,
4665                                   struct lod_object *lo, int index)
4666 {
4667         struct lod_layout_component *lod_comp;
4668         struct lod_layout_component *new_comp = NULL;
4669         struct lod_layout_component *comp_array;
4670         int rc = 0, i, new_cnt = lo->ldo_comp_cnt + 1;
4671         __u16 mirror_id;
4672         int offset = 0;
4673         ENTRY;
4674
4675         lod_comp = &lo->ldo_comp_entries[index];
4676         LASSERT(lod_comp_inited(lod_comp) && lod_comp->llc_id != LCME_ID_INVAL);
4677
4678         CDEBUG(D_LAYOUT, "repeating component %d\n", index);
4679
4680         OBD_ALLOC_PTR_ARRAY(comp_array, new_cnt);
4681         if (comp_array == NULL)
4682                 GOTO(out, rc = -ENOMEM);
4683
4684         for (i = 0; i < lo->ldo_comp_cnt; i++) {
4685                 memcpy(&comp_array[i + offset], &lo->ldo_comp_entries[i],
4686                        sizeof(*comp_array));
4687
4688                 /* Duplicate this component in to the next slot */
4689                 if (i == index) {
4690                         new_comp = &comp_array[i + 1];
4691                         memcpy(&comp_array[i + 1], &lo->ldo_comp_entries[i],
4692                                sizeof(*comp_array));
4693                         /* We must now skip this new component when copying */
4694                         offset = 1;
4695                 }
4696         }
4697
4698         /* Set up copied component */
4699         new_comp->llc_flags &= ~LCME_FL_INIT;
4700         new_comp->llc_stripe = NULL;
4701         new_comp->llc_stripes_allocated = 0;
4702         new_comp->llc_ost_indices = NULL;
4703         new_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4704         /* for uninstantiated components, layout gen stores default stripe
4705          * offset */
4706         new_comp->llc_layout_gen = lod_comp->llc_stripe_offset;
4707         /* This makes the repeated component zero-length, placed at the end of
4708          * the preceding component */
4709         new_comp->llc_extent.e_start = new_comp->llc_extent.e_end;
4710         new_comp->llc_timestamp = lod_comp->llc_timestamp;
4711         new_comp->llc_pool = NULL;
4712
4713         rc = lod_set_pool(&new_comp->llc_pool, lod_comp->llc_pool);
4714         if (rc)
4715                 GOTO(out, rc);
4716
4717         if (new_comp->llc_ostlist.op_array) {
4718                 __u32 *op_array = NULL;
4719
4720                 OBD_ALLOC(op_array, new_comp->llc_ostlist.op_size);
4721                 if (!op_array)
4722                         GOTO(out, rc = -ENOMEM);
4723                 memcpy(op_array, &new_comp->llc_ostlist.op_array,
4724                        new_comp->llc_ostlist.op_size);
4725                 new_comp->llc_ostlist.op_array = op_array;
4726         }
4727
4728         OBD_FREE_PTR_ARRAY(lo->ldo_comp_entries, lo->ldo_comp_cnt);
4729         lo->ldo_comp_entries = comp_array;
4730         lo->ldo_comp_cnt = new_cnt;
4731
4732         /* Generate an id for the new component */
4733         mirror_id = mirror_id_of(new_comp->llc_id);
4734         new_comp->llc_id = LCME_ID_INVAL;
4735         new_comp->llc_id = lod_gen_component_id(lo, mirror_id, index + 1);
4736         if (new_comp->llc_id == LCME_ID_INVAL)
4737                 GOTO(out, rc = -ERANGE);
4738
4739         EXIT;
4740 out:
4741         if (rc)
4742                 OBD_FREE_PTR_ARRAY(comp_array, new_cnt);
4743
4744         return rc;
4745 }
4746
4747 static int lod_layout_data_init(struct lod_thread_info *info, __u32 comp_cnt)
4748 {
4749         ENTRY;
4750
4751         /* clear memory region that will be used for layout change */
4752         memset(&info->lti_layout_attr, 0, sizeof(struct lu_attr));
4753         info->lti_count = 0;
4754
4755         if (info->lti_comp_size >= comp_cnt)
4756                 RETURN(0);
4757
4758         if (info->lti_comp_size > 0) {
4759                 OBD_FREE_PTR_ARRAY(info->lti_comp_idx, info->lti_comp_size);
4760                 info->lti_comp_size = 0;
4761         }
4762
4763         OBD_ALLOC_PTR_ARRAY(info->lti_comp_idx, comp_cnt);
4764         if (!info->lti_comp_idx)
4765                 RETURN(-ENOMEM);
4766
4767         info->lti_comp_size = comp_cnt;
4768         RETURN(0);
4769 }
4770
4771 /**
4772  * Prepare new layout minus deleted components
4773  *
4774  * Removes components marked for deletion (LCME_ID_INVAL) by copying to a new
4775  * layout and skipping those components.  Removes stripe objects if any exist.
4776  *
4777  * NB:
4778  * Reallocates layout components array (lo->ldo_comp_entries), invalidating
4779  * any pre-existing pointers to components.
4780  *
4781  * Caller is responsible for updating mirror end (ldo_mirror[].lme_end).
4782  *
4783  * \param[in] env       execution environment for this thread
4784  * \param[in,out] lo    object to update the layout of
4785  * \param[in] th        transaction handle for this operation
4786  *
4787  * \retval      # of components deleted
4788  * \retval      negative errno on error
4789  */
4790 static int lod_layout_del_prep_layout(const struct lu_env *env,
4791                                       struct lod_object *lo,
4792                                       struct thandle *th)
4793 {
4794         struct lod_layout_component     *lod_comp;
4795         struct lod_thread_info  *info = lod_env_info(env);
4796         int rc = 0, i, j, deleted = 0;
4797
4798         ENTRY;
4799
4800         LASSERT(lo->ldo_is_composite);
4801         LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
4802
4803         rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
4804         if (rc)
4805                 RETURN(rc);
4806
4807         for (i = 0; i < lo->ldo_comp_cnt; i++) {
4808                 lod_comp = &lo->ldo_comp_entries[i];
4809
4810                 if (lod_comp->llc_id != LCME_ID_INVAL) {
4811                         /* Build array of things to keep */
4812                         info->lti_comp_idx[info->lti_count++] = i;
4813                         continue;
4814                 }
4815
4816                 lod_obj_set_pool(lo, i, NULL);
4817                 if (lod_comp->llc_ostlist.op_array) {
4818                         OBD_FREE(lod_comp->llc_ostlist.op_array,
4819                                  lod_comp->llc_ostlist.op_size);
4820                         lod_comp->llc_ostlist.op_array = NULL;
4821                         lod_comp->llc_ostlist.op_size = 0;
4822                 }
4823
4824                 deleted++;
4825                 CDEBUG(D_LAYOUT, "deleting comp %d, left %d\n", i,
4826                        lo->ldo_comp_cnt - deleted);
4827
4828                 /* No striping info for this component */
4829                 if (lod_comp->llc_stripe == NULL)
4830                         continue;
4831
4832                 LASSERT(lod_comp->llc_stripe_count > 0);
4833                 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4834                         struct dt_object *obj = lod_comp->llc_stripe[j];
4835
4836                         if (obj == NULL)
4837                                 continue;
4838
4839                         /* components which are not init have no sub objects
4840                          * to destroy */
4841                         if (lod_comp_inited(lod_comp)) {
4842                                 rc = lod_sub_destroy(env, obj, th);
4843                                 if (rc)
4844                                         GOTO(out, rc);
4845                         }
4846
4847                         lu_object_put(env, &obj->do_lu);
4848                         lod_comp->llc_stripe[j] = NULL;
4849                 }
4850                 OBD_FREE_PTR_ARRAY(lod_comp->llc_stripe,
4851                                    lod_comp->llc_stripes_allocated);
4852                 lod_comp->llc_stripe = NULL;
4853                 OBD_FREE_PTR_ARRAY(lod_comp->llc_ost_indices,
4854                                    lod_comp->llc_stripes_allocated);
4855                 lod_comp->llc_ost_indices = NULL;
4856                 lod_comp->llc_stripes_allocated = 0;
4857         }
4858
4859         /* info->lti_count has the amount of left components */
4860         LASSERTF(info->lti_count >= 0 && info->lti_count < lo->ldo_comp_cnt,
4861                  "left = %d, lo->ldo_comp_cnt %d\n", (int)info->lti_count,
4862                  (int)lo->ldo_comp_cnt);
4863
4864         if (info->lti_count > 0) {
4865                 struct lod_layout_component *comp_array;
4866
4867                 OBD_ALLOC_PTR_ARRAY(comp_array, info->lti_count);
4868                 if (comp_array == NULL)
4869                         GOTO(out, rc = -ENOMEM);
4870
4871                 for (i = 0; i < info->lti_count; i++) {
4872                         memcpy(&comp_array[i],
4873                                &lo->ldo_comp_entries[info->lti_comp_idx[i]],
4874                                sizeof(*comp_array));
4875                 }
4876
4877                 OBD_FREE_PTR_ARRAY(lo->ldo_comp_entries, lo->ldo_comp_cnt);
4878                 lo->ldo_comp_entries = comp_array;
4879                 lo->ldo_comp_cnt = info->lti_count;
4880         } else {
4881                 lod_free_comp_entries(lo);
4882         }
4883
4884         EXIT;
4885 out:
4886         return rc ? rc : deleted;
4887 }
4888
4889 /**
4890  * Delete layout component(s)
4891  *
4892  * This function sets up the layout data in the env and does the setattrs
4893  * required to write out the new layout.  The layout itself is modified in
4894  * lod_layout_del_prep_layout.
4895  *
4896  * \param[in] env       execution environment for this thread
4897  * \param[in] dt        object
4898  * \param[in] th        transaction handle
4899  *
4900  * \retval      0 on success
4901  * \retval      negative error number on failure
4902  */
4903 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
4904                           struct thandle *th)
4905 {
4906         struct lod_object *lo = lod_dt_obj(dt);
4907         struct dt_object *next = dt_object_child(dt);
4908         struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4909         int rc;
4910
4911         LASSERT(lo->ldo_mirror_count == 1);
4912
4913         mutex_lock(&lo->ldo_layout_mutex);
4914
4915         rc = lod_layout_del_prep_layout(env, lo, th);
4916         if (rc < 0)
4917                 GOTO(out, rc);
4918
4919         /* Only do this if we didn't delete all components */
4920         if (lo->ldo_comp_cnt > 0) {
4921                 lo->ldo_mirrors[0].lme_end = lo->ldo_comp_cnt - 1;
4922                 lod_obj_inc_layout_gen(lo);
4923         }
4924
4925         LASSERT(dt_object_exists(dt));
4926         rc = dt_attr_get(env, next, attr);
4927         if (rc)
4928                 GOTO(out, rc);
4929
4930         if (attr->la_size > 0) {
4931                 attr->la_size = 0;
4932                 attr->la_valid = LA_SIZE;
4933                 rc = lod_sub_attr_set(env, next, attr, th);
4934                 if (rc)
4935                         GOTO(out, rc);
4936         }
4937
4938         rc = lod_generate_and_set_lovea(env, lo, th);
4939         EXIT;
4940 out:
4941         if (rc)
4942                 lod_striping_free_nolock(env, lo);
4943
4944         mutex_unlock(&lo->ldo_layout_mutex);
4945
4946         return rc;
4947 }
4948
4949
4950 /**
4951  * Implementation of dt_object_operations::do_xattr_set.
4952  *
4953  * Sets specified extended attribute on the object. Three types of EAs are
4954  * special:
4955  *   LOV EA - stores striping for a regular file or default striping (when set
4956  *            on a directory)
4957  *   LMV EA - stores a marker for the striped directories
4958  *   DMV EA - stores default directory striping
4959  *
4960  * When striping is applied to a non-striped existing object (this is called
4961  * late striping), then LOD notices the caller wants to turn the object into a
4962  * striped one. The stripe objects are created and appropriate EA is set:
4963  * LOV EA storing all the stripes directly or LMV EA storing just a small header
4964  * with striping configuration.
4965  *
4966  * \see dt_object_operations::do_xattr_set() in the API description for details.
4967  */
4968 static int lod_xattr_set(const struct lu_env *env,
4969                          struct dt_object *dt, const struct lu_buf *buf,
4970                          const char *name, int fl, struct thandle *th)
4971 {
4972         struct dt_object *next = dt_object_child(dt);
4973         struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
4974         struct lod_object *lo = lod_dt_obj(dt);
4975         struct lod_obj_stripe_cb_data data = { {0} };
4976         int rc = 0;
4977
4978         ENTRY;
4979
4980         if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4981             !strcmp(name, XATTR_NAME_LMV)) {
4982                 switch (fl) {
4983                 case LU_XATTR_CREATE:
4984                         rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
4985                         break;
4986                 case 0:
4987                 case LU_XATTR_REPLACE:
4988                         rc = lod_dir_layout_set(env, dt, buf, fl, th);
4989                         break;
4990                 default:
4991                         LBUG();
4992                 }
4993
4994                 RETURN(rc);
4995         } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4996                    strcmp(name, XATTR_NAME_LOV) == 0) {
4997                 rc = lod_xattr_set_default_lov_on_dir(env, dt, buf, name, fl,
4998                                                       th);
4999                 RETURN(rc);
5000         } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
5001                    strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
5002                 /* default LMVEA */
5003                 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
5004                                                       th);
5005                 RETURN(rc);
5006         } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
5007                    (strcmp(name, XATTR_NAME_LOV) == 0 ||
5008                     strcmp(name, XATTR_LUSTRE_LOV) == 0 ||
5009                     allowed_lustre_lov(name))) {
5010                 /* in case of lov EA swap, just set it
5011                  * if not, it is a replay so check striping match what we
5012                  * already have during req replay, declare_xattr_set()
5013                  * defines striping, then create() does the work */
5014                 if (fl & LU_XATTR_REPLACE) {
5015                         /* free stripes, then update disk */
5016                         lod_striping_free(env, lod_dt_obj(dt));
5017
5018                         rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
5019                 } else if (fl & LU_XATTR_SPLIT) {
5020                         rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
5021                         if (rc)
5022                                 RETURN(rc);
5023
5024                         rc = lod_striping_reload(env, lo, buf, LVF_ALL_STALE);
5025                         if (rc)
5026                                 RETURN(rc);
5027
5028                         if (lo->ldo_mirror_count > 1 &&
5029                             layout_attr->la_valid & LA_LAYOUT_VERSION) {
5030                                 /* mirror split */
5031                                 layout_attr->la_layout_version =
5032                                                 lo->ldo_layout_gen;
5033                                 data.locd_attr = layout_attr;
5034                                 data.locd_declare = false;
5035                                 data.locd_stripe_cb =
5036                                                 lod_obj_stripe_attr_set_cb;
5037                                 rc = lod_obj_for_each_stripe(env, lo, th,
5038                                                              &data);
5039                                 if (rc)
5040                                         RETURN(rc);
5041                         }
5042                 } else if (fl & LU_XATTR_PURGE) {
5043                         rc = lod_layout_purge(env, dt, buf, th);
5044                 } else if (dt_object_remote(dt)) {
5045                         /* This only happens during migration, see
5046                          * mdd_migrate_create(), in which Master MDT will
5047                          * create a remote target object, and only set
5048                          * (migrating) stripe EA on the remote object,
5049                          * and does not need creating each stripes. */
5050                         rc = lod_sub_xattr_set(env, next, buf, name,
5051                                                       fl, th);
5052                 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
5053                         /* delete component(s) */
5054                         LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
5055                         rc = lod_layout_del(env, dt, th);
5056                 } else {
5057                         /*
5058                          * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
5059                          * it's going to create create file with specified
5060                          * component(s), the striping must have not being
5061                          * cached in this case;
5062                          *
5063                          * Otherwise, it's going to add/change component(s) to
5064                          * an existing file, the striping must have been cached
5065                          * in this case.
5066                          */
5067                         LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
5068                                      !strcmp(name, XATTR_NAME_LOV),
5069                                 !lod_dt_obj(dt)->ldo_comp_cached));
5070
5071                         rc = lod_striped_create(env, dt, NULL, NULL, th);
5072                         if (rc)
5073                                 RETURN(rc);
5074
5075                         if (fl & LU_XATTR_MERGE && lo->ldo_mirror_count > 1 &&
5076                             layout_attr->la_valid & LA_LAYOUT_VERSION) {
5077                                 /* mirror merge exec phase */
5078                                 layout_attr->la_layout_version =
5079                                                 lo->ldo_layout_gen;
5080                                 data.locd_attr = layout_attr;
5081                                 data.locd_declare = false;
5082                                 data.locd_stripe_cb =
5083                                                 lod_obj_stripe_attr_set_cb;
5084                                 rc = lod_obj_for_each_stripe(env, lo, th,
5085                                                              &data);
5086                                 if (rc)
5087                                         RETURN(rc);
5088                         }
5089                 }
5090                 RETURN(rc);
5091         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
5092                 rc = lod_replace_parent_fid(env, dt, buf, th, false);
5093
5094                 RETURN(rc);
5095         }
5096
5097         /* then all other xattr */
5098         rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
5099
5100         RETURN(rc);
5101 }
5102
5103 /**
5104  * Implementation of dt_object_operations::do_declare_xattr_del.
5105  *
5106  * \see dt_object_operations::do_declare_xattr_del() in the API description
5107  * for details.
5108  */
5109 static int lod_declare_xattr_del(const struct lu_env *env,
5110                                  struct dt_object *dt, const char *name,
5111                                  struct thandle *th)
5112 {
5113         struct lod_object *lo = lod_dt_obj(dt);
5114         struct dt_object *next = dt_object_child(dt);
5115         int i;
5116         int rc;
5117         ENTRY;
5118
5119         rc = lod_sub_declare_xattr_del(env, next, name, th);
5120         if (rc != 0)
5121                 RETURN(rc);
5122
5123         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
5124                 RETURN(0);
5125
5126         /* NB: don't delete stripe LMV, because when we do this, normally we
5127          * will remove stripes, besides, if directory LMV is corrupt, this will
5128          * prevent deleting its LMV and fixing it (via LFSCK).
5129          */
5130         if (!strcmp(name, XATTR_NAME_LMV))
5131                 RETURN(0);
5132
5133         rc = lod_striping_load(env, lo);
5134         if (rc != 0)
5135                 RETURN(rc);
5136
5137         if (lo->ldo_dir_stripe_count == 0)
5138                 RETURN(0);
5139
5140         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5141                 struct dt_object *dto = lo->ldo_stripe[i];
5142
5143                 if (!dto)
5144                         continue;
5145
5146                 if (!dt_object_exists(dto))
5147                         continue;
5148
5149                 rc = lod_sub_declare_xattr_del(env, dto, name, th);
5150                 if (rc != 0)
5151                         break;
5152         }
5153
5154         RETURN(rc);
5155 }
5156
5157 /**
5158  * Implementation of dt_object_operations::do_xattr_del.
5159  *
5160  * If EA storing a regular striping is being deleted, then release
5161  * all the references to the stripe objects in core.
5162  *
5163  * \see dt_object_operations::do_xattr_del() in the API description for details.
5164  */
5165 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
5166                          const char *name, struct thandle *th)
5167 {
5168         int rc;
5169
5170         ENTRY;
5171
5172         if (!strcmp(name, XATTR_NAME_LOV) || !strcmp(name, XATTR_NAME_LMV))
5173                 lod_striping_free(env, lod_dt_obj(dt));
5174
5175         rc = lod_xattr_del_internal(env, dt, name, th);
5176
5177         RETURN(rc);
5178 }
5179
5180 /**
5181  * Implementation of dt_object_operations::do_xattr_list.
5182  *
5183  * \see dt_object_operations::do_xattr_list() in the API description
5184  * for details.
5185  */
5186 static int lod_xattr_list(const struct lu_env *env,
5187                           struct dt_object *dt, const struct lu_buf *buf)
5188 {
5189         return dt_xattr_list(env, dt_object_child(dt), buf);
5190 }
5191
5192 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
5193 {
5194         return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
5195 }
5196
5197 /**
5198  * Copy OST list from layout provided by user.
5199  *
5200  * \param[in] lod_comp          layout_component to be filled
5201  * \param[in] v3                LOV EA V3 user data
5202  *
5203  * \retval              0 on success
5204  * \retval              negative if failed
5205  */
5206 int lod_comp_copy_ost_lists(struct lod_layout_component *lod_comp,
5207                             struct lov_user_md_v3 *v3)
5208 {
5209         int j;
5210
5211         ENTRY;
5212
5213         if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
5214                 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
5215
5216         if (lod_comp->llc_ostlist.op_array) {
5217                 if (lod_comp->llc_ostlist.op_size >=
5218                     v3->lmm_stripe_count * sizeof(__u32))  {
5219                         lod_comp->llc_ostlist.op_count =
5220                                         v3->lmm_stripe_count;
5221                         goto skip;
5222                 }
5223                 OBD_FREE(lod_comp->llc_ostlist.op_array,
5224                          lod_comp->llc_ostlist.op_size);
5225         }
5226
5227         /* copy ost list from lmm */
5228         lod_comp->llc_ostlist.op_count = v3->lmm_stripe_count;
5229         lod_comp->llc_ostlist.op_size = v3->lmm_stripe_count * sizeof(__u32);
5230         OBD_ALLOC(lod_comp->llc_ostlist.op_array,
5231                   lod_comp->llc_ostlist.op_size);
5232         if (!lod_comp->llc_ostlist.op_array)
5233                 RETURN(-ENOMEM);
5234 skip:
5235         for (j = 0; j < v3->lmm_stripe_count; j++) {
5236                 lod_comp->llc_ostlist.op_array[j] =
5237                         v3->lmm_objects[j].l_ost_idx;
5238         }
5239
5240         RETURN(0);
5241 }
5242
5243
5244 /**
5245  * Get default striping.
5246  *
5247  * \param[in] env               execution environment
5248  * \param[in] lo                object
5249  * \param[out] lds              default striping
5250  *
5251  * \retval              0 on success
5252  * \retval              negative if failed
5253  */
5254 static int lod_get_default_lov_striping(const struct lu_env *env,
5255                                         struct lod_object *lo,
5256                                         struct lod_default_striping *lds,
5257                                         struct dt_allocation_hint *dah)
5258 {
5259         struct lod_thread_info *info = lod_env_info(env);
5260         struct lov_user_md_v1 *v1 = NULL;
5261         struct lov_user_md_v3 *v3 = NULL;
5262         struct lov_comp_md_v1 *lcm = NULL;
5263         __u32 magic;
5264         int append_stripe_count = dah != NULL ? dah->dah_append_stripe_count : 0;
5265         const char *append_pool = (dah != NULL &&
5266                                    dah->dah_append_pool != NULL &&
5267                                    dah->dah_append_pool[0] != '\0') ?
5268                                   dah->dah_append_pool : NULL;
5269         __u16 entry_count = 1;
5270         __u16 mirror_count = 0;
5271         bool want_composite = false;
5272         int rc, i, j;
5273
5274         ENTRY;
5275
5276         lds->lds_def_striping_set = 0;
5277
5278         rc = lod_get_lov_ea(env, lo);
5279         if (rc < 0)
5280                 RETURN(rc);
5281
5282         if (rc < (typeof(rc))sizeof(struct lov_user_md))
5283                 RETURN(0);
5284
5285         magic = *(__u32 *)info->lti_ea_store;
5286         if (magic == __swab32(LOV_USER_MAGIC_V1)) {
5287                 lustre_swab_lov_user_md_v1(info->lti_ea_store);
5288         } else if (magic == __swab32(LOV_USER_MAGIC_V3)) {
5289                 lustre_swab_lov_user_md_v3(info->lti_ea_store);
5290         } else if (magic == __swab32(LOV_USER_MAGIC_SPECIFIC)) {
5291                 v3 = (struct lov_user_md_v3 *)info->lti_ea_store;
5292                 lustre_swab_lov_user_md_v3(v3);
5293                 lustre_swab_lov_user_md_objects(v3->lmm_objects,
5294                                                 v3->lmm_stripe_count);
5295         } else if (magic == __swab32(LOV_USER_MAGIC_COMP_V1) ||
5296                    magic == __swab32(LOV_USER_MAGIC_SEL)) {
5297                 lustre_swab_lov_comp_md_v1(info->lti_ea_store);
5298         }
5299
5300         switch (magic) {
5301         case LOV_MAGIC_V1:
5302         case LOV_MAGIC_V3:
5303         case LOV_USER_MAGIC_SPECIFIC:
5304                 v1 = info->lti_ea_store;
5305                 break;
5306         case LOV_MAGIC_COMP_V1:
5307         case LOV_MAGIC_SEL:
5308                 lcm = info->lti_ea_store;
5309                 entry_count = lcm->lcm_entry_count;
5310                 if (entry_count == 0)
5311                         RETURN(-EINVAL);
5312
5313                 mirror_count = lcm->lcm_mirror_count + 1;
5314                 want_composite = true;
5315                 break;
5316         default:
5317                 RETURN(-ENOTSUPP);
5318         }
5319
5320         if (append_stripe_count != 0 || append_pool != NULL) {
5321                 entry_count = 1;
5322                 mirror_count = 0;
5323                 want_composite = false;
5324         }
5325
5326         /* realloc default comp entries if necessary */
5327         rc = lod_def_striping_comp_resize(lds, entry_count);
5328         if (rc < 0)
5329                 RETURN(rc);
5330
5331         lds->lds_def_comp_cnt = entry_count;
5332         lds->lds_def_striping_is_composite = want_composite;
5333         lds->lds_def_mirror_cnt = mirror_count;
5334
5335         for (i = 0; i < entry_count; i++) {
5336                 struct lod_layout_component *llc = &lds->lds_def_comp_entries[i];
5337                 const char *pool;
5338
5339                 /*
5340                  * reset llc values, llc_stripes is always NULL in the
5341                  * default striping template, llc_pool will be reset
5342                  * later below using lod_set_pool().
5343                  *
5344                  * XXX At this point llc_pool may point to valid (!)
5345                  * kmalloced strings from previous RPCs.
5346                  */
5347                 memset(llc, 0, offsetof(typeof(*llc), llc_pool));
5348
5349                 if (lcm != NULL) {
5350                         v1 = (struct lov_user_md *)((char *)lcm +
5351                                                     lcm->lcm_entries[i].lcme_offset);
5352
5353                         if (want_composite) {
5354                                 llc->llc_extent = lcm->lcm_entries[i].lcme_extent;
5355                                 /* We only inherit certain flags from the layout */
5356                                 llc->llc_flags = lcm->lcm_entries[i].lcme_flags &
5357                                         LCME_TEMPLATE_FLAGS;
5358                         }
5359                 }
5360
5361                 CDEBUG(D_LAYOUT, DFID" magic = %#08x, pattern = %#x, stripe_count = %hu, stripe_size = %u, stripe_offset = %hu, append_pool = '%s', append_stripe_count = %d\n",
5362                        PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
5363                        v1->lmm_magic,
5364                        v1->lmm_pattern,
5365                        v1->lmm_stripe_count,
5366                        v1->lmm_stripe_size,
5367                        v1->lmm_stripe_offset,
5368                        append_pool ?: "",
5369                        append_stripe_count);
5370
5371                 if (!lov_pattern_supported(v1->lmm_pattern) &&
5372                     !(v1->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
5373                         lod_free_def_comp_entries(lds);
5374                         RETURN(-EINVAL);
5375                 }
5376
5377                 llc->llc_stripe_count = v1->lmm_stripe_count;
5378                 llc->llc_stripe_size = v1->lmm_stripe_size;
5379                 llc->llc_stripe_offset = v1->lmm_stripe_offset;
5380                 llc->llc_pattern = v1->lmm_pattern;
5381
5382                 if (append_stripe_count != 0 || append_pool != NULL)
5383                         llc->llc_pattern = LOV_PATTERN_RAID0;
5384
5385                 if (append_stripe_count != 0)
5386                         llc->llc_stripe_count = append_stripe_count;
5387
5388                 pool = NULL;
5389                 if (append_pool != NULL) {
5390                         pool = append_pool;
5391                 } else if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
5392                         /* XXX: sanity check here */
5393                         v3 = (struct lov_user_md_v3 *)v1;
5394                         if (v3->lmm_pool_name[0] != '\0')
5395                                 pool = v3->lmm_pool_name;
5396                 }
5397
5398                 lod_set_pool(&llc->llc_pool, pool);
5399
5400                 if (append_stripe_count != 0 || append_pool != NULL) {
5401                         /* Ignore specific striping for append. */
5402                 } else if (v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
5403                         v3 = (struct lov_user_md_v3 *)v1;
5404                         rc = lod_comp_copy_ost_lists(llc, v3);
5405                         if (rc)
5406                                 RETURN(rc);
5407                 } else if (llc->llc_ostlist.op_array &&
5408                            llc->llc_ostlist.op_count) {
5409                         for (j = 0; j < llc->llc_ostlist.op_count; j++)
5410                                 llc->llc_ostlist.op_array[j] = -1;
5411                         llc->llc_ostlist.op_count = 0;
5412                 }
5413         }
5414
5415         lds->lds_def_striping_set = 1;
5416         RETURN(rc);
5417 }
5418
5419 static inline void lod_lum2lds(struct lod_default_striping *lds,
5420                                const struct lmv_user_md *lum)
5421 {
5422         lds->lds_dir_def_stripe_count = le32_to_cpu(lum->lum_stripe_count);
5423         lds->lds_dir_def_stripe_offset = le32_to_cpu(lum->lum_stripe_offset);
5424         lds->lds_dir_def_hash_type = le32_to_cpu(lum->lum_hash_type);
5425         lds->lds_dir_def_max_inherit = lum->lum_max_inherit;
5426         lds->lds_dir_def_max_inherit_rr = lum->lum_max_inherit_rr;
5427         lds->lds_dir_def_striping_set = 1;
5428 }
5429
5430 /**
5431  * Get default directory striping.
5432  *
5433  * \param[in] env               execution environment
5434  * \param[in] lo                object
5435  * \param[out] lds              default striping
5436  *
5437  * \retval              0 on success
5438  * \retval              negative if failed
5439  */
5440 static int lod_get_default_lmv_striping(const struct lu_env *env,
5441                                         struct lod_object *lo,
5442                                         struct lod_default_striping *lds)
5443 {
5444         struct lmv_user_md *lmu;
5445         int rc;
5446
5447         lds->lds_dir_def_striping_set = 0;
5448
5449         rc = lod_get_default_lmv_ea(env, lo);
5450         if (rc < 0)
5451                 return rc;
5452
5453         if (rc >= (int)sizeof(*lmu)) {
5454                 struct lod_thread_info *info = lod_env_info(env);
5455
5456                 lmu = info->lti_ea_store;
5457                 lod_lum2lds(lds, lmu);
5458         }
5459
5460         return 0;
5461 }
5462
5463 /**
5464  * Get default striping in the object.
5465  *
5466  * Get object default striping and default directory striping.
5467  *
5468  * \param[in] env               execution environment
5469  * \param[in] lo                object
5470  * \param[out] lds              default striping
5471  *
5472  * \retval              0 on success
5473  * \retval              negative if failed
5474  */
5475 static int lod_get_default_striping(const struct lu_env *env,
5476                                     struct lod_object *lo,
5477                                     struct dt_allocation_hint *ah,
5478                                     struct lod_default_striping *lds)
5479 {
5480         int rc, rc1;
5481
5482         rc = lod_get_default_lov_striping(env, lo, lds, NULL);
5483         if (lds->lds_def_striping_set) {
5484                 struct lod_thread_info *info = lod_env_info(env);
5485                 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5486
5487                 rc = lod_verify_striping(env, d, lo, &info->lti_buf, false);
5488                 if (rc)
5489                         lds->lds_def_striping_set = 0;
5490         }
5491
5492         if (ah->dah_eadata_is_dmv) {
5493                 lod_lum2lds(lds, ah->dah_eadata);
5494         } else if (ah->dah_dmv_imp_inherit) {
5495                 lds->lds_dir_def_striping_set = 0;
5496         } else {
5497                 rc1 = lod_get_default_lmv_striping(env, lo, lds);
5498                 if (rc == 0 && rc1 < 0)
5499                         rc = rc1;
5500         }
5501
5502         return rc;
5503 }
5504
5505 /**
5506  * Apply default striping on object.
5507  *
5508  * If object striping pattern is not set, set to the one in default striping.
5509  * The default striping is from parent or fs.
5510  *
5511  * \param[in] lo                new object
5512  * \param[in] lds               default striping
5513  * \param[in] mode              new object's mode
5514  */
5515 static void lod_striping_from_default(struct lod_object *lo,
5516                                       const struct lod_default_striping *lds,
5517                                       umode_t mode)
5518 {
5519         struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5520         int i, rc;
5521
5522         if (lds->lds_def_striping_set && S_ISREG(mode)) {
5523                 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
5524
5525                 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
5526                                             lds->lds_def_comp_cnt);
5527                 if (rc != 0)
5528                         return;
5529
5530                 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
5531                 if (lds->lds_def_mirror_cnt > 1)
5532                         lo->ldo_flr_state = LCM_FL_RDONLY;
5533
5534                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5535                         struct lod_layout_component *obj_comp =
5536                                                 &lo->ldo_comp_entries[i];
5537                         struct lod_layout_component *def_comp =
5538                                                 &lds->lds_def_comp_entries[i];
5539
5540                         CDEBUG(D_LAYOUT,
5541                                "inherit "DFID" file layout from default: flags=%#x size=%u nr=%u offset=%u pattern=%#x pool=%s\n",
5542                                PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
5543                                def_comp->llc_flags,
5544                                def_comp->llc_stripe_size,
5545                                def_comp->llc_stripe_count,
5546                                def_comp->llc_stripe_offset,
5547                                def_comp->llc_pattern,
5548                                def_comp->llc_pool ?: "");
5549
5550                         *obj_comp = *def_comp;
5551                         if (def_comp->llc_pool != NULL) {
5552                                 /* pointer was copied from def_comp */
5553                                 obj_comp->llc_pool = NULL;
5554                                 lod_obj_set_pool(lo, i, def_comp->llc_pool);
5555                         }
5556
5557                         /* copy ost list */
5558                         if (def_comp->llc_ostlist.op_array &&
5559                             def_comp->llc_ostlist.op_count) {
5560                                 OBD_ALLOC(obj_comp->llc_ostlist.op_array,
5561                                           obj_comp->llc_ostlist.op_size);
5562                                 if (!obj_comp->llc_ostlist.op_array)
5563                                         return;
5564                                 memcpy(obj_comp->llc_ostlist.op_array,
5565                                        def_comp->llc_ostlist.op_array,
5566                                        obj_comp->llc_ostlist.op_size);
5567                         } else if (def_comp->llc_ostlist.op_array) {
5568                                 obj_comp->llc_ostlist.op_array = NULL;
5569                         }
5570
5571                         /*
5572                          * Don't initialize these fields for plain layout
5573                          * (v1/v3) here, they are inherited in the order of
5574                          * 'parent' -> 'fs default (root)' -> 'global default
5575                          * values for stripe_count & stripe_size'.
5576                          *
5577                          * see lod_ah_init().
5578                          */
5579                         if (!lo->ldo_is_composite)
5580                                 continue;
5581
5582                         lod_adjust_stripe_info(obj_comp, desc, 0);
5583                 }
5584         } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
5585                 if (lo->ldo_dir_stripe_count == 0)
5586                         lo->ldo_dir_stripe_count =
5587                                 lds->lds_dir_def_stripe_count;
5588                 if (lo->ldo_dir_stripe_offset == -1)
5589                         lo->ldo_dir_stripe_offset =
5590                                 lds->lds_dir_def_stripe_offset;
5591                 if (lo->ldo_dir_hash_type == LMV_HASH_TYPE_UNKNOWN)
5592                         lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
5593
5594                 CDEBUG(D_LAYOUT,
5595                        "inherit "DFID" dir layout from default: count=%hu offset=%u hash_type=%x\n",
5596                        PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
5597                        lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
5598                        lo->ldo_dir_hash_type);
5599         }
5600 }
5601
5602 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root,
5603                                          const char *append_pool)
5604 {
5605         struct lod_layout_component *lod_comp;
5606
5607         if (lo->ldo_comp_cnt == 0)
5608                 return true;
5609
5610         if (lo->ldo_is_composite)
5611                 return false;
5612
5613         lod_comp = &lo->ldo_comp_entries[0];
5614
5615         if (lod_comp->llc_stripe_count <= 0 ||
5616             lod_comp->llc_stripe_size <= 0)
5617                 return true;
5618
5619         if (from_root && (lod_comp->llc_pool == NULL ||
5620                           lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
5621                 return true;
5622
5623         if (append_pool && append_pool[0])
5624                 return true;
5625
5626         return false;
5627 }
5628
5629 /**
5630  * Implementation of dt_object_operations::do_ah_init.
5631  *
5632  * This method is used to make a decision on the striping configuration for the
5633  * object being created. It can be taken from the \a parent object if it exists,
5634  * or filesystem's default. The resulting configuration (number of stripes,
5635  * stripe size/offset, pool name, hash_type, etc.) is stored in the object
5636  * itself and will be used by the methods like ->doo_declare_create().
5637  *
5638  * \see dt_object_operations::do_ah_init() in the API description for details.
5639  */
5640 static void lod_ah_init(const struct lu_env *env,
5641                         struct dt_allocation_hint *ah,
5642                         struct dt_object *parent,
5643                         struct dt_object *child,
5644                         umode_t child_mode)
5645 {
5646         struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
5647         struct lod_thread_info *info = lod_env_info(env);
5648         struct lod_default_striping *lds = lod_lds_buf_get(env);
5649         struct dt_object *nextp = NULL;
5650         struct dt_object *nextc;
5651         struct lod_object *lp = NULL;
5652         struct lod_object *lc;
5653         struct lov_desc *desc;
5654         struct lod_layout_component *lod_comp;
5655         int rc;
5656         ENTRY;
5657
5658         LASSERT(child);
5659
5660         if (ah->dah_append_stripe_count == -1)
5661                 ah->dah_append_stripe_count =
5662                         d->lod_ost_descs.ltd_lov_desc.ld_tgt_count;
5663
5664         if (likely(parent)) {
5665                 nextp = dt_object_child(parent);
5666                 lp = lod_dt_obj(parent);
5667         }
5668
5669         nextc = dt_object_child(child);
5670         lc = lod_dt_obj(child);
5671
5672         LASSERT(!lod_obj_is_striped(child));
5673         /* default layout template may have been set on the regular file
5674          * when this is called from mdd_create_data() */
5675         if (S_ISREG(child_mode))
5676                 lod_free_comp_entries(lc);
5677
5678         if (!dt_object_exists(nextc))
5679                 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
5680
5681         if (S_ISDIR(child_mode)) {
5682                 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
5683
5684                 /* other default values are 0 */
5685                 lc->ldo_dir_stripe_offset = -1;
5686
5687                 /* no default striping configuration is needed for
5688                  * foreign dirs
5689                  */
5690                 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5691                     le32_to_cpu(lum1->lum_magic) == LMV_MAGIC_FOREIGN) {
5692                         lc->ldo_is_foreign = true;
5693                         /* keep stripe_count 0 and stripe_offset -1 */
5694                         CDEBUG(D_INFO, "no default striping for foreign dir\n");
5695                         RETURN_EXIT;
5696                 }
5697
5698                 if (likely(lp != NULL))
5699                         lod_get_default_striping(env, lp, ah, lds);
5700
5701                 /* It should always honour the specified stripes */
5702                 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
5703                  * will have old magic. In this case, we should ignore the
5704                  * stripe count and try to create dir by default stripe.
5705                  */
5706                 if (ah->dah_eadata && ah->dah_eadata_len &&
5707                     !ah->dah_eadata_is_dmv &&
5708                     (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
5709                      le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
5710                         lc->ldo_dir_stripe_count =
5711                                 le32_to_cpu(lum1->lum_stripe_count);
5712                         lc->ldo_dir_stripe_offset =
5713                                 le32_to_cpu(lum1->lum_stripe_offset);
5714                         lc->ldo_dir_hash_type =
5715                                 le32_to_cpu(lum1->lum_hash_type);
5716                         CDEBUG(D_INFO,
5717                                "set dirstripe: count %hu, offset %d, hash %x\n",
5718                                 lc->ldo_dir_stripe_count,
5719                                 (int)lc->ldo_dir_stripe_offset,
5720                                 lc->ldo_dir_hash_type);
5721
5722                         if (d->lod_mdt_descs.ltd_lmv_desc.ld_active_tgt_count &&
5723                             lc->ldo_dir_stripe_count < 2 &&
5724                             lum1->lum_max_inherit != LMV_INHERIT_NONE) {
5725                                 /* when filesystem-wide default LMV is set, dirs
5726                                  * will be created on MDT by space usage, but if
5727                                  * dir is created with "lfs mkdir -c 1 ...", its
5728                                  * subdirs should be kept on the same MDT. To
5729                                  * guarantee this, set default LMV for such dir.
5730                                  */
5731                                 lds->lds_dir_def_stripe_count =
5732                                         le32_to_cpu(lum1->lum_stripe_count);
5733                                 /* if "-1" stripe offset is set, save current
5734                                  * MDT index in default LMV.
5735                                  */
5736                                 if (le32_to_cpu(lum1->lum_stripe_offset) ==
5737                                     LMV_OFFSET_DEFAULT)
5738                                         lds->lds_dir_def_stripe_offset =
5739                                                 lod2lu_dev(d)->ld_site->ld_seq_site->ss_node_id;
5740                                 else
5741                                         lds->lds_dir_def_stripe_offset =
5742                                                 le32_to_cpu(lum1->lum_stripe_offset);
5743                                 lds->lds_dir_def_hash_type =
5744                                         le32_to_cpu(lum1->lum_hash_type);
5745                                 lds->lds_dir_def_max_inherit =
5746                                         lum1->lum_max_inherit;
5747                                 /* it will be decreased by 1 later in setting */
5748                                 if (lum1->lum_max_inherit >= LMV_INHERIT_END &&
5749                                     lum1->lum_max_inherit < LMV_INHERIT_MAX)
5750                                         lds->lds_dir_def_max_inherit++;
5751                                 lds->lds_dir_def_max_inherit_rr =
5752                                         lum1->lum_max_inherit_rr;
5753                                 lds->lds_dir_def_striping_set = 1;
5754                                 /* don't inherit LOV from ROOT */
5755                                 if (lds->lds_def_striping_set &&
5756                                     fid_is_root(lod_object_fid(lp)))
5757                                         lds->lds_def_striping_set = 0;
5758                                 lc->ldo_def_striping = lds;
5759                         } else if (lds->lds_def_striping_set &&
5760                                    !fid_is_root(lod_object_fid(lp))) {
5761                                 /* don't inherit default LMV for "lfs mkdir" */
5762                                 lds->lds_dir_def_striping_set = 0;
5763                                 lc->ldo_def_striping = lds;
5764                         }
5765                 } else {
5766                         /* inherit default striping except ROOT */
5767                         if ((lds->lds_def_striping_set ||
5768                              lds->lds_dir_def_striping_set) &&
5769                             !fid_is_root(lod_object_fid(lp)))
5770                                 lc->ldo_def_striping = lds;
5771
5772                         /* transfer defaults LMV to new directory */
5773                         lod_striping_from_default(lc, lds, child_mode);
5774
5775                         /* set count 0 to create normal directory */
5776                         if (lc->ldo_dir_stripe_count == 1)
5777                                 lc->ldo_dir_stripe_count = 0;
5778
5779                         /* do not save default LMV on server */
5780                         if (ah->dah_dmv_imp_inherit) {
5781                                 lds->lds_dir_def_striping_set = 0;
5782                                 if (!lds->lds_def_striping_set)
5783                                         lc->ldo_def_striping = NULL;
5784                         }
5785                 }
5786
5787                 /* shrink the stripe count to max_mdt_stripecount if it is -1
5788                  * and max_mdt_stripecount is not 0
5789                  */
5790                 if (lc->ldo_dir_stripe_count == (__u16)(-1) &&
5791                     d->lod_max_mdt_stripecount)
5792                         lc->ldo_dir_stripe_count = d->lod_max_mdt_stripecount;
5793
5794                 /* shrink the stripe_count to the avaible MDT count */
5795                 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
5796                     !CFS_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)) {
5797                         lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
5798                         if (lc->ldo_dir_stripe_count == 1)
5799                                 lc->ldo_dir_stripe_count = 0;
5800                 }
5801
5802                 if (!lmv_is_known_hash_type(lc->ldo_dir_hash_type))
5803                         lc->ldo_dir_hash_type =
5804                                 (lc->ldo_dir_hash_type & LMV_HASH_FLAG_KNOWN) |
5805                                 d->lod_mdt_descs.ltd_lmv_desc.ld_pattern;
5806
5807                 /* make sure all fscrypt metadata stays on same mdt */
5808                 if (child->do_lu.lo_header->loh_attr & LOHA_FSCRYPT_MD) {
5809                         lc->ldo_dir_stripe_count = 0;
5810                         lds->lds_dir_def_stripe_offset =
5811                                 lod2lu_dev(d)->ld_site->ld_seq_site->ss_node_id;
5812                         lds->lds_dir_def_striping_set = 1;
5813                         lc->ldo_def_striping = lds;
5814                 }
5815
5816                 CDEBUG(D_INFO, "final dir stripe_count=%hu offset=%d hash=%u\n",
5817                        lc->ldo_dir_stripe_count,
5818                        (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
5819
5820                 RETURN_EXIT;
5821         }
5822
5823         /* child object regular file*/
5824
5825         if (!lod_object_will_be_striped(S_ISREG(child_mode),
5826                                         lu_object_fid(&child->do_lu)))
5827                 RETURN_EXIT;
5828
5829         /* If object is going to be striped over OSTs, transfer default
5830          * striping information to the child, so that we can use it
5831          * during declaration and creation.
5832          *
5833          * Try from the parent first.
5834          */
5835         if (likely(lp != NULL)) {
5836                 rc = lod_get_default_lov_striping(env, lp, lds, ah);
5837                 if (rc == 0 && lds->lds_def_striping_set) {
5838                         rc = lod_verify_striping(env, d, lp, &info->lti_buf,
5839                                                  false);
5840                         if (rc == 0)
5841                                 lod_striping_from_default(lc, lds, child_mode);
5842                 }
5843         }
5844
5845         /* Initialize lod_device::lod_md_root object reference */
5846         if (d->lod_md_root == NULL) {
5847                 struct dt_object *root;
5848                 struct lod_object *lroot;
5849
5850                 lu_root_fid(&info->lti_fid);
5851                 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
5852                 if (!IS_ERR(root)) {
5853                         lroot = lod_dt_obj(root);
5854
5855                         spin_lock(&d->lod_lock);
5856                         if (d->lod_md_root != NULL)
5857                                 dt_object_put(env, &d->lod_md_root->ldo_obj);
5858                         d->lod_md_root = lroot;
5859                         spin_unlock(&d->lod_lock);
5860                 }
5861         }
5862
5863         /* try inherit layout from the root object (fs default) when:
5864          *  - parent does not have default layout; or
5865          *  - parent has plain(v1/v3) default layout, and some attributes
5866          *    are not specified in the default layout;
5867          */
5868         if (d->lod_md_root != NULL &&
5869             lod_need_inherit_more(lc, true, ah->dah_append_pool)) {
5870                 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds,
5871                                                   ah);
5872                 if (rc || !lds->lds_def_striping_set)
5873                         goto out;
5874
5875                 rc = lod_verify_striping(env, d, d->lod_md_root, &info->lti_buf,
5876                                          false);
5877                 if (rc)
5878                         goto out;
5879
5880                 if (lc->ldo_comp_cnt == 0) {
5881                         lod_striping_from_default(lc, lds, child_mode);
5882                 } else if (!lds->lds_def_striping_is_composite) {
5883                         struct lod_layout_component *def_comp;
5884
5885                         LASSERT(!lc->ldo_is_composite);
5886                         lod_comp = &lc->ldo_comp_entries[0];
5887                         def_comp = &lds->lds_def_comp_entries[0];
5888
5889                         if (lod_comp->llc_stripe_count <= 0)
5890                                 lod_comp->llc_stripe_count =
5891                                         def_comp->llc_stripe_count;
5892                         if (lod_comp->llc_stripe_size <= 0)
5893                                 lod_comp->llc_stripe_size =
5894                                         def_comp->llc_stripe_size;
5895                         if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT &&
5896                             (!lod_comp->llc_pool || !lod_comp->llc_pool[0]))
5897                                 lod_comp->llc_stripe_offset =
5898                                         def_comp->llc_stripe_offset;
5899                         if (lod_comp->llc_pool == NULL)
5900                                 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
5901                 }
5902         }
5903 out:
5904         /*
5905          * fs default striping may not be explicitly set, or historically set
5906          * in config log, use them.
5907          */
5908         if (lod_need_inherit_more(lc, false, ah->dah_append_pool)) {
5909                 if (lc->ldo_comp_cnt == 0) {
5910                         rc = lod_alloc_comp_entries(lc, 0, 1);
5911                         if (rc)
5912                                 /* fail to allocate memory, will create a
5913                                  * non-striped file. */
5914                                 RETURN_EXIT;
5915                         lc->ldo_is_composite = 0;
5916                         lod_comp = &lc->ldo_comp_entries[0];
5917                         lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
5918                 }
5919                 LASSERT(!lc->ldo_is_composite);
5920                 lod_comp = &lc->ldo_comp_entries[0];
5921                 desc = &d->lod_ost_descs.ltd_lov_desc;
5922                 lod_adjust_stripe_info(lod_comp, desc,
5923                                        ah->dah_append_stripe_count);
5924                 if (ah->dah_append_pool && ah->dah_append_pool[0])
5925                         lod_obj_set_pool(lc, 0, ah->dah_append_pool);
5926         }
5927
5928         EXIT;
5929 }
5930
5931 /**
5932  * Size initialization on late striping.
5933  *
5934  * Propagate the size of a truncated object to a deferred striping.
5935  * This function handles a special case when truncate was done on a
5936  * non-striped object and now while the striping is being created
5937  * we can't lose that size, so we have to propagate it to the stripes
5938  * being created.
5939  *
5940  * \param[in] env       execution environment
5941  * \param[in] dt        object
5942  * \param[in] th        transaction handle
5943  *
5944  * \retval              0 on success
5945  * \retval              negative if failed
5946  */
5947 static int lod_declare_init_size(const struct lu_env *env,
5948                                  struct dt_object *dt, struct thandle *th)
5949 {
5950         struct dt_object        *next = dt_object_child(dt);
5951         struct lod_object       *lo = lod_dt_obj(dt);
5952         struct dt_object        **objects = NULL;
5953         struct lu_attr  *attr = &lod_env_info(env)->lti_attr;
5954         uint64_t        size, offs;
5955         int     i, rc, stripe, stripe_count = 0, stripe_size = 0;
5956         struct lu_extent size_ext;
5957         ENTRY;
5958
5959         if (!lod_obj_is_striped(dt))
5960                 RETURN(0);
5961
5962         rc = dt_attr_get(env, next, attr);
5963         LASSERT(attr->la_valid & LA_SIZE);
5964         if (rc)
5965                 RETURN(rc);
5966
5967         size = attr->la_size;
5968         if (size == 0)
5969                 RETURN(0);
5970
5971         size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
5972         for (i = 0; i < lo->ldo_comp_cnt; i++) {
5973                 struct lod_layout_component *lod_comp;
5974                 struct lu_extent *extent;
5975
5976                 lod_comp = &lo->ldo_comp_entries[i];
5977
5978                 if (lod_comp->llc_stripe == NULL)
5979                         continue;
5980
5981                 extent = &lod_comp->llc_extent;
5982                 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
5983                 if (!lo->ldo_is_composite ||
5984                     lu_extent_is_overlapped(extent, &size_ext)) {
5985                         objects = lod_comp->llc_stripe;
5986                         stripe_count = lod_comp->llc_stripe_count;
5987                         stripe_size = lod_comp->llc_stripe_size;
5988
5989                         /* next mirror */
5990                         if (stripe_count == 0)
5991                                 continue;
5992
5993                         LASSERT(objects != NULL && stripe_size != 0);
5994                         do_div(size, stripe_size);
5995                         stripe = do_div(size, stripe_count);
5996                         LASSERT(objects[stripe] != NULL);
5997
5998                         size = size * stripe_size;
5999                         offs = attr->la_size;
6000                         size += do_div(offs, stripe_size);
6001
6002                         attr->la_valid = LA_SIZE;
6003                         attr->la_size = size;
6004
6005                         rc = lod_sub_declare_attr_set(env, objects[stripe],
6006                                                       attr, th);
6007                 }
6008         }
6009
6010         RETURN(rc);
6011 }
6012
6013 /**
6014  * Declare creation of striped object.
6015  *
6016  * The function declares creation stripes for a regular object. The function
6017  * also declares whether the stripes will be created with non-zero size if
6018  * previously size was set non-zero on the master object. If object \a dt is
6019  * not local, then only fully defined striping can be applied in \a lovea.
6020  * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
6021  * for the details.
6022  *
6023  * \param[in] env       execution environment
6024  * \param[in] dt        object
6025  * \param[in] attr      attributes the stripes will be created with
6026  * \param[in] lovea     a buffer containing striping description
6027  * \param[in] th        transaction handle
6028  *
6029  * \retval              0 on success
6030  * \retval              negative if failed
6031  */
6032 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
6033                                struct lu_attr *attr,
6034                                const struct lu_buf *lovea, struct thandle *th)
6035 {
6036         struct lod_thread_info  *info = lod_env_info(env);
6037         struct dt_object        *next = dt_object_child(dt);
6038         struct lod_object       *lo = lod_dt_obj(dt);
6039         int                      rc;
6040         ENTRY;
6041
6042         if (CFS_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
6043                 GOTO(out, rc = -ENOMEM);
6044
6045         if (!dt_object_remote(next)) {
6046                 /* choose OST and generate appropriate objects */
6047                 rc = lod_prepare_create(env, lo, attr, lovea, th);
6048                 if (rc)
6049                         GOTO(out, rc);
6050
6051                 /*
6052                  * declare storage for striping data
6053                  */
6054                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6055         } else {
6056                 /* LOD can not choose OST objects for remote objects, i.e.
6057                  * stripes must be ready before that. Right now, it can only
6058                  * happen during migrate, i.e. migrate process needs to create
6059                  * remote regular file (mdd_migrate_create), then the migrate
6060                  * process will provide stripeEA. */
6061                 LASSERT(lovea != NULL);
6062                 info->lti_buf = *lovea;
6063         }
6064
6065         rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
6066                                        XATTR_NAME_LOV, 0, th);
6067         if (rc)
6068                 GOTO(out, rc);
6069
6070         /*
6071          * if striping is created with local object's size > 0,
6072          * we have to propagate this size to specific object
6073          * the case is possible only when local object was created previously
6074          */
6075         if (dt_object_exists(next))
6076                 rc = lod_declare_init_size(env, dt, th);
6077
6078 out:
6079         /* failed to create striping or to set initial size, let's reset
6080          * config so that others don't get confused */
6081         if (rc)
6082                 lod_striping_free(env, lo);
6083
6084         RETURN(rc);
6085 }
6086
6087 /*
6088  * Whether subdirectories under \a dt should be created on MDTs by space QoS
6089  *
6090  * If LMV_HASH_FLAG_SPACE is set on directory default layout, its subdirectories
6091  * should be created on MDT by space QoS.
6092  *
6093  * \param[in] env       execution environment
6094  * \param[in] dev       lu device
6095  * \param[in] dt        object
6096  *
6097  * \retval              1 if directory should create subdir by space usage
6098  * \retval              0 if not
6099  * \retval              -ev if failed
6100  */
6101 static inline int dt_object_qos_mkdir(const struct lu_env *env,
6102                                       struct lu_device *dev,
6103                                       struct dt_object *dt)
6104 {
6105         struct lod_thread_info *info = lod_env_info(env);
6106         struct lu_object *obj;
6107         struct lod_object *lo;
6108         struct lmv_user_md *lmu;
6109         int rc;
6110
6111         obj = lu_object_find_slice(env, dev, lu_object_fid(&dt->do_lu), NULL);
6112         if (IS_ERR(obj))
6113                 return PTR_ERR(obj);
6114
6115         lo = lu2lod_obj(obj);
6116
6117         rc = lod_get_default_lmv_ea(env, lo);
6118         dt_object_put(env, dt);
6119         if (rc <= 0)
6120                 return rc;
6121
6122         if (rc < (int)sizeof(*lmu))
6123                 return -EINVAL;
6124
6125         lmu = info->lti_ea_store;
6126         return le32_to_cpu(lmu->lum_stripe_offset) == LMV_OFFSET_DEFAULT;
6127 }
6128
6129 /**
6130  * Implementation of dt_object_operations::do_declare_create.
6131  *
6132  * The method declares creation of a new object. If the object will be striped,
6133  * then helper functions are called to find FIDs for the stripes, declare
6134  * creation of the stripes and declare initialization of the striping
6135  * information to be stored in the master object.
6136  *
6137  * \see dt_object_operations::do_declare_create() in the API description
6138  * for details.
6139  */
6140 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
6141                               struct lu_attr *attr,
6142                               struct dt_allocation_hint *hint,
6143                               struct dt_object_format *dof, struct thandle *th)
6144 {
6145         struct dt_object   *next = dt_object_child(dt);
6146         struct lod_object  *lo = lod_dt_obj(dt);
6147         int                 rc;
6148         ENTRY;
6149
6150         LASSERT(dof);
6151         LASSERT(attr);
6152         LASSERT(th);
6153
6154         /*
6155          * first of all, we declare creation of local object
6156          */
6157         rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
6158         if (rc != 0)
6159                 GOTO(out, rc);
6160
6161         /*
6162          * it's lod_ah_init() that has decided the object will be striped
6163          */
6164         if (dof->dof_type == DFT_REGULAR) {
6165                 /* callers don't want stripes */
6166                 /* XXX: all tricky interactions with ->ah_make_hint() decided
6167                  * to use striping, then ->declare_create() behaving differently
6168                  * should be cleaned */
6169                 if (dof->u.dof_reg.striped != 0)
6170                         rc = lod_declare_striped_create(env, dt, attr,
6171                                                         NULL, th);
6172         } else if (dof->dof_type == DFT_DIR) {
6173                 struct seq_server_site *ss;
6174                 struct lu_buf buf = { NULL };
6175                 struct lu_buf *lmu = NULL;
6176
6177                 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
6178
6179                 /* If the parent has default stripeEA, and client
6180                  * did not find it before sending create request,
6181                  * then MDT will return -EREMOTE, and client will
6182                  * retrieve the default stripeEA and re-create the
6183                  * sub directory.
6184                  *
6185                  * Note: if dah_eadata != NULL, it means creating the
6186                  * striped directory with specified stripeEA, then it
6187                  * should ignore the default stripeEA */
6188                 if (hint != NULL && hint->dah_eadata == NULL) {
6189                         if (CFS_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
6190                                 GOTO(out, rc = -EREMOTE);
6191
6192                         if (lo->ldo_dir_stripe_offset != LMV_OFFSET_DEFAULT &&
6193                             lo->ldo_dir_stripe_offset != ss->ss_node_id) {
6194                                 struct lod_device *lod;
6195                                 struct lu_tgt_desc *mdt = NULL;
6196                                 bool found_mdt = false;
6197
6198                                 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6199                                 lod_foreach_mdt(lod, mdt) {
6200                                         if (mdt->ltd_index ==
6201                                                 lo->ldo_dir_stripe_offset) {
6202                                                 found_mdt = true;
6203                                                 break;
6204                                         }
6205                                 }
6206
6207                                 /* If the MDT indicated by stripe_offset can be
6208                                  * found, then tell client to resend the create
6209                                  * request to the correct MDT, otherwise return
6210                                  * error to client */
6211                                 if (found_mdt)
6212                                         GOTO(out, rc = -EREMOTE);
6213                                 else
6214                                         GOTO(out, rc = -EINVAL);
6215                         }
6216                 } else if (hint && hint->dah_eadata) {
6217                         lmu = &buf;
6218                         lmu->lb_buf = (void *)hint->dah_eadata;
6219                         lmu->lb_len = hint->dah_eadata_len;
6220                 }
6221
6222                 rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
6223                                                      th);
6224         }
6225 out:
6226         /* failed to create striping or to set initial size, let's reset
6227          * config so that others don't get confused */
6228         if (rc)
6229                 lod_striping_free(env, lo);
6230         RETURN(rc);
6231 }
6232
6233 /**
6234  * Generate component ID for new created component.
6235  *
6236  * \param[in] lo                LOD object
6237  * \param[in] comp_idx          index of ldo_comp_entries
6238  *
6239  * \retval                      component ID on success
6240  * \retval                      LCME_ID_INVAL on failure
6241  */
6242 static __u32 lod_gen_component_id(struct lod_object *lo,
6243                                   int mirror_id, int comp_idx)
6244 {
6245         struct lod_layout_component *lod_comp;
6246         __u32   id, start, end;
6247         int     i;
6248
6249         LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
6250
6251         lod_obj_inc_layout_gen(lo);
6252         id = lo->ldo_layout_gen;
6253         if (likely(id <= SEQ_ID_MAX))
6254                 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
6255
6256         /* Layout generation wraps, need to check collisions. */
6257         start = id & SEQ_ID_MASK;
6258         end = SEQ_ID_MAX;
6259 again:
6260         for (id = start; id <= end; id++) {
6261                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6262                         lod_comp = &lo->ldo_comp_entries[i];
6263                         if (pflr_id(mirror_id, id) == lod_comp->llc_id)
6264                                 break;
6265                 }
6266                 /* Found the ununsed ID */
6267                 if (i == lo->ldo_comp_cnt)
6268                         RETURN(pflr_id(mirror_id, id));
6269         }
6270         if (end == LCME_ID_MAX) {
6271                 start = 1;
6272                 end = min(lo->ldo_layout_gen & LCME_ID_MASK,
6273                           (__u32)(LCME_ID_MAX - 1));
6274                 goto again;
6275         }
6276
6277         RETURN(LCME_ID_INVAL);
6278 }
6279
6280 /**
6281  * Creation of a striped regular object.
6282  *
6283  * The function is called to create the stripe objects for a regular
6284  * striped file. This can happen at the initial object creation or
6285  * when the caller asks LOD to do so using ->do_xattr_set() method
6286  * (so called late striping). Notice all the information are already
6287  * prepared in the form of the list of objects (ldo_stripe field).
6288  * This is done during declare phase.
6289  *
6290  * \param[in] env       execution environment
6291  * \param[in] dt        object
6292  * \param[in] attr      attributes the stripes will be created with
6293  * \param[in] dof       format of stripes (see OSD API description)
6294  * \param[in] th        transaction handle
6295  *
6296  * \retval              0 on success
6297  * \retval              negative if failed
6298  */
6299 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
6300                        struct lu_attr *attr, struct dt_object_format *dof,
6301                        struct thandle *th)
6302 {
6303         struct lod_layout_component     *lod_comp;
6304         struct lod_object       *lo = lod_dt_obj(dt);
6305         __u16   mirror_id;
6306         int     rc = 0, i, j;
6307         ENTRY;
6308
6309         mutex_lock(&lo->ldo_layout_mutex);
6310
6311         LASSERT((lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL) ||
6312                 lo->ldo_is_foreign);
6313
6314         mirror_id = 0; /* non-flr file's mirror_id is 0 */
6315         if (lo->ldo_mirror_count > 1) {
6316                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6317                         lod_comp = &lo->ldo_comp_entries[i];
6318                         if (lod_comp->llc_id != LCME_ID_INVAL &&
6319                             mirror_id_of(lod_comp->llc_id) > mirror_id)
6320                                 mirror_id = mirror_id_of(lod_comp->llc_id);
6321                 }
6322         }
6323
6324         /* create all underlying objects */
6325         for (i = 0; i < lo->ldo_comp_cnt; i++) {
6326                 lod_comp = &lo->ldo_comp_entries[i];
6327
6328                 if (lod_comp->llc_id == LCME_ID_INVAL) {
6329                         /* only the component of FLR layout with more than 1
6330                          * mirror has mirror ID in its component ID.
6331                          */
6332                         if (lod_comp->llc_extent.e_start == 0 &&
6333                             lo->ldo_mirror_count > 1)
6334                                 ++mirror_id;
6335
6336                         lod_comp->llc_id = lod_gen_component_id(lo,
6337                                                                 mirror_id, i);
6338                         if (lod_comp->llc_id == LCME_ID_INVAL)
6339                                 GOTO(out, rc = -ERANGE);
6340                 }
6341
6342                 if (lod_comp_inited(lod_comp))
6343                         continue;
6344
6345                 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
6346                         lod_comp_set_init(lod_comp);
6347
6348                 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
6349                         lod_comp_set_init(lod_comp);
6350
6351                 if (lod_comp->llc_stripe == NULL)
6352                         continue;
6353
6354                 LASSERT(lod_comp->llc_stripe_count);
6355                 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6356                         struct dt_object *object = lod_comp->llc_stripe[j];
6357                         LASSERT(object != NULL);
6358                         rc = lod_sub_create(env, object, attr, NULL, dof, th);
6359                         if (rc)
6360                                 GOTO(out, rc);
6361                 }
6362                 lod_comp_set_init(lod_comp);
6363         }
6364
6365         rc = lod_fill_mirrors(lo);
6366         if (rc)
6367                 GOTO(out, rc);
6368
6369         lo->ldo_comp_cached = 1;
6370
6371         rc = lod_generate_and_set_lovea(env, lo, th);
6372         if (rc)
6373                 GOTO(out, rc);
6374
6375         mutex_unlock(&lo->ldo_layout_mutex);
6376
6377         RETURN(0);
6378
6379 out:
6380         lod_striping_free_nolock(env, lo);
6381         mutex_unlock(&lo->ldo_layout_mutex);
6382
6383         RETURN(rc);
6384 }
6385
6386 static inline bool lod_obj_is_dom(struct dt_object *dt)
6387 {
6388         struct lod_object *lo = lod_dt_obj(dt);
6389
6390         if (!dt_object_exists(dt_object_child(dt)))
6391                 return false;
6392
6393         if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
6394                 return false;
6395
6396         if (!lo->ldo_comp_cnt)
6397                 return false;
6398
6399         return (lov_pattern(lo->ldo_comp_entries[0].llc_pattern) ==
6400                 LOV_PATTERN_MDT);
6401 }
6402
6403 /**
6404  * Implementation of dt_object_operations::do_create.
6405  *
6406  * If any of preceeding methods (like ->do_declare_create(),
6407  * ->do_ah_init(), etc) chose to create a striped object,
6408  * then this method will create the master and the stripes.
6409  *
6410  * \see dt_object_operations::do_create() in the API description for details.
6411  */
6412 static int lod_create(const struct lu_env *env, struct dt_object *dt,
6413                       struct lu_attr *attr, struct dt_allocation_hint *hint,
6414                       struct dt_object_format *dof, struct thandle *th)
6415 {
6416         int                 rc;
6417         ENTRY;
6418
6419         /* create local object */
6420         rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
6421         if (rc != 0)
6422                 RETURN(rc);
6423
6424         if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
6425             (lod_obj_is_striped(dt) || lod_obj_is_dom(dt)) &&
6426             dof->u.dof_reg.striped != 0) {
6427                 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
6428                 rc = lod_striped_create(env, dt, attr, dof, th);
6429         }
6430
6431         RETURN(rc);
6432 }
6433
6434 static inline int
6435 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
6436                           struct dt_object *dt, struct thandle *th,
6437                           int comp_idx, int stripe_idx,
6438                           struct lod_obj_stripe_cb_data *data)
6439 {
6440         if (data->locd_declare)
6441                 return lod_sub_declare_destroy(env, dt, th);
6442
6443         if (!CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
6444             stripe_idx == cfs_fail_val)
6445                 return lod_sub_destroy(env, dt, th);
6446
6447         return 0;
6448 }
6449
6450 /**
6451  * Implementation of dt_object_operations::do_declare_destroy.
6452  *
6453  * If the object is a striped directory, then the function declares reference
6454  * removal from the master object (this is an index) to the stripes and declares
6455  * destroy of all the stripes. In all the cases, it declares an intention to
6456  * destroy the object itself.
6457  *
6458  * \see dt_object_operations::do_declare_destroy() in the API description
6459  * for details.
6460  */
6461 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
6462                                struct thandle *th)
6463 {
6464         struct dt_object *next = dt_object_child(dt);
6465         struct lod_object *lo = lod_dt_obj(dt);
6466         struct lod_thread_info *info = lod_env_info(env);
6467         struct dt_object *stripe;
6468         char *stripe_name = info->lti_key;
6469         int rc, i;
6470
6471         ENTRY;
6472
6473         /*
6474          * load striping information, notice we don't do this when object
6475          * is being initialized as we don't need this information till
6476          * few specific cases like destroy, chown
6477          */
6478         rc = lod_striping_load(env, lo);
6479         if (rc)
6480                 RETURN(rc);
6481
6482         /* declare destroy for all underlying objects */
6483         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6484                 rc = next->do_ops->do_index_try(env, next,
6485                                                 &dt_directory_features);
6486                 if (rc != 0)
6487                         RETURN(rc);
6488
6489                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6490                         stripe = lo->ldo_stripe[i];
6491                         if (!stripe)
6492                                 continue;
6493
6494                         rc = lod_sub_declare_ref_del(env, next, th);
6495                         if (rc != 0)
6496                                 RETURN(rc);
6497
6498                         snprintf(stripe_name, sizeof(info->lti_key),
6499                                  DFID":%d",
6500                                  PFID(lu_object_fid(&stripe->do_lu)), i);
6501                         rc = lod_sub_declare_delete(env, next,
6502                                         (const struct dt_key *)stripe_name, th);
6503                         if (rc != 0)
6504                                 RETURN(rc);
6505                 }
6506         }
6507
6508         /*
6509          * we declare destroy for the local object
6510          */
6511         rc = lod_sub_declare_destroy(env, next, th);
6512         if (rc)
6513                 RETURN(rc);
6514
6515         if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
6516             CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
6517                 RETURN(0);
6518
6519         if (!lod_obj_is_striped(dt))
6520                 RETURN(0);
6521
6522         /* declare destroy all striped objects */
6523         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6524                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6525                         stripe = lo->ldo_stripe[i];
6526                         if (!stripe)
6527                                 continue;
6528
6529                         if (!dt_object_exists(stripe))
6530                                 continue;
6531
6532                         rc = lod_sub_declare_ref_del(env, stripe, th);
6533                         if (rc != 0)
6534                                 break;
6535
6536                         rc = lod_sub_declare_destroy(env, stripe, th);
6537                         if (rc != 0)
6538                                 break;
6539                 }
6540         } else {
6541                 struct lod_obj_stripe_cb_data data = { { 0 } };
6542
6543                 data.locd_declare = true;
6544                 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6545                 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6546         }
6547
6548         RETURN(rc);
6549 }
6550
6551 /**
6552  * Implementation of dt_object_operations::do_destroy.
6553  *
6554  * If the object is a striped directory, then the function removes references
6555  * from the master object (this is an index) to the stripes and destroys all
6556  * the stripes. In all the cases, the function destroys the object itself.
6557  *
6558  * \see dt_object_operations::do_destroy() in the API description for details.
6559  */
6560 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
6561                        struct thandle *th)
6562 {
6563         struct dt_object  *next = dt_object_child(dt);
6564         struct lod_object *lo = lod_dt_obj(dt);
6565         struct lod_thread_info *info = lod_env_info(env);
6566         char *stripe_name = info->lti_key;
6567         struct dt_object *stripe;
6568         unsigned int i;
6569         int rc;
6570
6571         ENTRY;
6572
6573         /* destroy sub-stripe of master object */
6574         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6575                 rc = next->do_ops->do_index_try(env, next,
6576                                                 &dt_directory_features);
6577                 if (rc != 0)
6578                         RETURN(rc);
6579
6580                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6581                         stripe = lo->ldo_stripe[i];
6582                         if (!stripe)
6583                                 continue;
6584
6585                         rc = lod_sub_ref_del(env, next, th);
6586                         if (rc != 0)
6587                                 RETURN(rc);
6588
6589                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
6590                                 PFID(lu_object_fid(&stripe->do_lu)), i);
6591
6592                         CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
6593                                PFID(lu_object_fid(&dt->do_lu)), stripe_name,
6594                                PFID(lu_object_fid(&stripe->do_lu)));
6595
6596                         rc = lod_sub_delete(env, next,
6597                                        (const struct dt_key *)stripe_name, th);
6598                         if (rc != 0)
6599                                 RETURN(rc);
6600                 }
6601         }
6602
6603         rc = lod_sub_destroy(env, next, th);
6604         if (rc != 0)
6605                 RETURN(rc);
6606
6607         if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
6608             CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
6609                 RETURN(0);
6610
6611         if (!lod_obj_is_striped(dt))
6612                 RETURN(0);
6613
6614         /* destroy all striped objects */
6615         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6616                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6617                         stripe = lo->ldo_stripe[i];
6618                         if (!stripe)
6619                                 continue;
6620
6621                         if (!dt_object_exists(stripe))
6622                                 continue;
6623
6624                         if (!CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
6625                             i == cfs_fail_val) {
6626                                 dt_write_lock(env, stripe, DT_TGT_CHILD);
6627                                 rc = lod_sub_ref_del(env, stripe, th);
6628                                 dt_write_unlock(env, stripe);
6629                                 if (rc != 0)
6630                                         break;
6631
6632                                 rc = lod_sub_destroy(env, stripe, th);
6633                                 if (rc != 0)
6634                                         break;
6635                         }
6636                 }
6637         } else {
6638                 struct lod_obj_stripe_cb_data data = { { 0 } };
6639
6640                 data.locd_declare = false;
6641                 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6642                 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6643         }
6644
6645         RETURN(rc);
6646 }
6647
6648 /**
6649  * Implementation of dt_object_operations::do_declare_ref_add.
6650  *
6651  * \see dt_object_operations::do_declare_ref_add() in the API description
6652  * for details.
6653  */
6654 static int lod_declare_ref_add(const struct lu_env *env,
6655                                struct dt_object *dt, struct thandle *th)
6656 {
6657         return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
6658 }
6659
6660 /**
6661  * Implementation of dt_object_operations::do_ref_add.
6662  *
6663  * \see dt_object_operations::do_ref_add() in the API description for details.
6664  */
6665 static int lod_ref_add(const struct lu_env *env,
6666                        struct dt_object *dt, struct thandle *th)
6667 {
6668         return lod_sub_ref_add(env, dt_object_child(dt), th);
6669 }
6670
6671 /**
6672  * Implementation of dt_object_operations::do_declare_ref_del.
6673  *
6674  * \see dt_object_operations::do_declare_ref_del() in the API description
6675  * for details.
6676  */
6677 static int lod_declare_ref_del(const struct lu_env *env,
6678                                struct dt_object *dt, struct thandle *th)
6679 {
6680         return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
6681 }
6682
6683 /**
6684  * Implementation of dt_object_operations::do_ref_del
6685  *
6686  * \see dt_object_operations::do_ref_del() in the API description for details.
6687  */
6688 static int lod_ref_del(const struct lu_env *env,
6689                        struct dt_object *dt, struct thandle *th)
6690 {
6691         return lod_sub_ref_del(env, dt_object_child(dt), th);
6692 }
6693
6694 /**
6695  * Implementation of dt_object_operations::do_object_sync.
6696  *
6697  * \see dt_object_operations::do_object_sync() in the API description
6698  * for details.
6699  */
6700 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
6701                            __u64 start, __u64 end)
6702 {
6703         return dt_object_sync(env, dt_object_child(dt), start, end);
6704 }
6705
6706 /**
6707  * Implementation of dt_object_operations::do_object_unlock.
6708  *
6709  * Used to release LDLM lock(s).
6710  *
6711  * \see dt_object_operations::do_object_unlock() in the API description
6712  * for details.
6713  */
6714 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
6715                              struct ldlm_enqueue_info *einfo,
6716                              union ldlm_policy_data *policy)
6717 {
6718         struct lod_object *lo = lod_dt_obj(dt);
6719         struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
6720         int slave_locks_size;
6721         int i;
6722         ENTRY;
6723
6724         if (slave_locks == NULL)
6725                 RETURN(0);
6726
6727         LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
6728         /* Note: for remote lock for single stripe dir, MDT will cancel
6729          * the lock by lockh directly */
6730         LASSERT(!dt_object_remote(dt_object_child(dt)));
6731
6732         /* locks were unlocked in MDT layer */
6733         for (i = 0; i < slave_locks->ha_count; i++)
6734                 LASSERT(!lustre_handle_is_used(&slave_locks->ha_handles[i]));
6735
6736         /*
6737          * NB, ha_count may not equal to ldo_dir_stripe_count, because dir
6738          * layout may change, e.g., shrink dir layout after migration.
6739          */
6740         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6741                 if (lo->ldo_stripe[i])
6742                         dt_invalidate(env, lo->ldo_stripe[i]);
6743         }
6744
6745         slave_locks_size = offsetof(typeof(*slave_locks),
6746                                     ha_handles[slave_locks->ha_count]);
6747         OBD_FREE(slave_locks, slave_locks_size);
6748         einfo->ei_cbdata = NULL;
6749
6750         RETURN(0);
6751 }
6752
6753 /**
6754  * Implementation of dt_object_operations::do_object_lock.
6755  *
6756  * Used to get LDLM lock on the non-striped and striped objects.
6757  *
6758  * \see dt_object_operations::do_object_lock() in the API description
6759  * for details.
6760  */
6761 static int lod_object_lock(const struct lu_env *env,
6762                            struct dt_object *dt,
6763                            struct lustre_handle *lh,
6764                            struct ldlm_enqueue_info *einfo,
6765                            union ldlm_policy_data *policy)
6766 {
6767         struct lod_object *lo = lod_dt_obj(dt);
6768         int slave_locks_size;
6769         struct lustre_handle_array *slave_locks = NULL;
6770         int i;
6771         int rc;
6772         ENTRY;
6773
6774         /* remote object lock */
6775         if (!einfo->ei_enq_slave) {
6776                 LASSERT(dt_object_remote(dt));
6777                 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
6778                                       policy);
6779         }
6780
6781         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
6782                 RETURN(-ENOTDIR);
6783
6784         rc = lod_striping_load(env, lo);
6785         if (rc != 0)
6786                 RETURN(rc);
6787
6788         /* No stripes */
6789         if (lo->ldo_dir_stripe_count <= 1)
6790                 RETURN(0);
6791
6792         slave_locks_size = offsetof(typeof(*slave_locks),
6793                                     ha_handles[lo->ldo_dir_stripe_count]);
6794         /* Freed in lod_object_unlock */
6795         OBD_ALLOC(slave_locks, slave_locks_size);
6796         if (!slave_locks)
6797                 RETURN(-ENOMEM);
6798         slave_locks->ha_count = lo->ldo_dir_stripe_count;
6799
6800         /* striped directory lock */
6801         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6802                 struct lustre_handle lockh;
6803                 struct ldlm_res_id *res_id;
6804                 struct dt_object *stripe;
6805
6806                 stripe = lo->ldo_stripe[i];
6807                 if (!stripe)
6808                         continue;
6809
6810                 res_id = &lod_env_info(env)->lti_res_id;
6811                 fid_build_reg_res_name(lu_object_fid(&stripe->do_lu), res_id);
6812                 einfo->ei_res_id = res_id;
6813
6814                 if (dt_object_remote(stripe)) {
6815                         set_bit(i, (void *)slave_locks->ha_map);
6816                         rc = dt_object_lock(env, stripe, &lockh, einfo, policy);
6817                 } else {
6818                         struct ldlm_namespace *ns = einfo->ei_namespace;
6819                         ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
6820                         ldlm_completion_callback completion = einfo->ei_cb_cp;
6821                         __u64 dlmflags = LDLM_FL_ATOMIC_CB;
6822
6823                         if (einfo->ei_mode == LCK_PW ||
6824                             einfo->ei_mode == LCK_EX)
6825                                 dlmflags |= LDLM_FL_COS_INCOMPAT;
6826
6827                         LASSERT(ns != NULL);
6828                         rc = ldlm_cli_enqueue_local(env, ns, res_id, LDLM_IBITS,
6829                                                     policy, einfo->ei_mode,
6830                                                     &dlmflags, blocking,
6831                                                     completion, NULL,
6832                                                     NULL, 0, LVB_T_NONE,
6833                                                     NULL, &lockh);
6834                 }
6835                 if (rc) {
6836                         while (i--)
6837                                 ldlm_lock_decref_and_cancel(
6838                                                 &slave_locks->ha_handles[i],
6839                                                 einfo->ei_mode);
6840                         OBD_FREE(slave_locks, slave_locks_size);
6841                         RETURN(rc);
6842                 }
6843                 slave_locks->ha_handles[i] = lockh;
6844         }
6845         einfo->ei_cbdata = slave_locks;
6846
6847         RETURN(0);
6848 }
6849
6850 /**
6851  * Implementation of dt_object_operations::do_invalidate.
6852  *
6853  * \see dt_object_operations::do_invalidate() in the API description for details
6854  */
6855 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
6856 {
6857         return dt_invalidate(env, dt_object_child(dt));
6858 }
6859
6860 static int lod_declare_instantiate_components(const struct lu_env *env,
6861                                               struct lod_object *lo,
6862                                               struct thandle *th,
6863                                               __u64 reserve)
6864 {
6865         struct lod_thread_info *info = lod_env_info(env);
6866         int i;
6867         int rc = 0;
6868         ENTRY;
6869
6870         LASSERT(info->lti_count < lo->ldo_comp_cnt);
6871
6872         for (i = 0; i < info->lti_count; i++) {
6873                 rc = lod_qos_prep_create(env, lo, NULL, th,
6874                                          info->lti_comp_idx[i], reserve);
6875                 if (rc)
6876                         break;
6877         }
6878
6879         if (!rc) {
6880                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6881                 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
6882                                 &info->lti_buf, XATTR_NAME_LOV, 0, th);
6883         }
6884
6885         RETURN(rc);
6886 }
6887
6888 /**
6889  * Check OSTs for an existing component for further extension
6890  *
6891  * Checks if OSTs are still healthy and not out of space.  Gets free space
6892  * on OSTs (relative to allocation watermark rmb_low) and compares to
6893  * the proposed new_end for this component.
6894  *
6895  * Decides whether or not to extend a component on its current OSTs.
6896  *
6897  * \param[in] env               execution environment for this thread
6898  * \param[in] lo                object we're checking
6899  * \param[in] index             index of this component
6900  * \param[in] extension_size    extension size for this component
6901  * \param[in] extent            layout extent for requested operation
6902  * \param[in] comp_extent       extension component extent
6903  * \param[in] write             if this is write operation
6904  *
6905  * \retval      true - OK to extend on current OSTs
6906  * \retval      false - do not extend on current OSTs
6907  */
6908 static bool lod_sel_osts_allowed(const struct lu_env *env,
6909                                  struct lod_object *lo,
6910                                  int index, __u64 reserve,
6911                                  struct lu_extent *extent,
6912                                  struct lu_extent *comp_extent, int write)
6913 {
6914         struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[index];
6915         struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6916         struct lod_thread_info *tinfo = lod_env_info(env);
6917         struct obd_statfs *sfs = &tinfo->lti_osfs;
6918         __u64 available = 0;
6919         bool ret = true;
6920         int i, rc;
6921
6922         ENTRY;
6923
6924         LASSERT(lod_comp->llc_stripe_count != 0);
6925
6926         lod_getref(&lod->lod_ost_descs);
6927         for (i = 0; i < lod_comp->llc_stripe_count; i++) {
6928                 int index = lod_comp->llc_ost_indices[i];
6929                 struct lod_tgt_desc *ost = OST_TGT(lod, index);
6930                 struct obd_statfs_info info = { 0 };
6931                 int j, repeated = 0;
6932
6933                 LASSERT(ost);
6934
6935                 /* Get the number of times this OST repeats in this component.
6936                  * Note: inter-component repeats are not counted as this is
6937                  * considered as a rare case: we try to not repeat OST in other
6938                  * components if possible. */
6939                 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6940                         if (index != lod_comp->llc_ost_indices[j])
6941                                 continue;
6942
6943                         /* already handled */
6944                         if (j < i)
6945                                 break;
6946
6947                         repeated++;
6948                 }
6949                 if (j < lod_comp->llc_stripe_count)
6950                         continue;
6951
6952                 if (!test_bit(index, lod->lod_ost_bitmap)) {
6953                         CDEBUG(D_LAYOUT, "ost %d no longer present\n", index);
6954                         ret = false;
6955                         break;
6956                 }
6957
6958                 rc = dt_statfs_info(env, ost->ltd_tgt, sfs, &info);
6959                 if (rc) {
6960                         CDEBUG(D_LAYOUT, "statfs failed for ost %d, error %d\n",
6961                                index, rc);
6962                         ret = false;
6963                         break;
6964                 }
6965
6966                 if (sfs->os_state & OS_STATFS_ENOSPC ||
6967                     sfs->os_state & OS_STATFS_READONLY ||
6968                     sfs->os_state & OS_STATFS_DEGRADED) {
6969                         CDEBUG(D_LAYOUT, "ost %d is not availble for SEL "
6970                                "extension, state %u\n", index, sfs->os_state);
6971                         ret = false;
6972                         break;
6973                 }
6974
6975                 /* In bytes */
6976                 available = sfs->os_bavail * sfs->os_bsize;
6977                 /* 'available' is relative to the allocation threshold */
6978                 available -= (__u64) info.os_reserved_mb_low << 20;
6979
6980                 CDEBUG(D_LAYOUT, "ost %d lowwm: %d highwm: %d, "
6981                        "%llu %% blocks available, %llu %% blocks free\n",
6982                        index, info.os_reserved_mb_low, info.os_reserved_mb_high,
6983                        (100ull * sfs->os_bavail) / sfs->os_blocks,
6984                        (100ull * sfs->os_bfree) / sfs->os_blocks);
6985
6986                 if (reserve * repeated > available) {
6987                         ret = false;
6988                         CDEBUG(D_LAYOUT, "low space on ost %d, available %llu "
6989                                "< extension size %llu repeated %d\n", index,
6990                                available, reserve, repeated);
6991                         break;
6992                 }
6993         }
6994         lod_putref(lod, &lod->lod_ost_descs);
6995
6996         RETURN(ret);
6997 }
6998
6999 /**
7000  * Adjust extents after component removal
7001  *
7002  * When we remove an extension component, we move the start of the next
7003  * component to match the start of the extension component, so no space is left
7004  * without layout.
7005  *
7006  * \param[in] env       execution environment for this thread
7007  * \param[in] lo        object
7008  * \param[in] max_comp  layout component
7009  * \param[in] index     index of this component
7010  *
7011  * \retval              0 on success
7012  * \retval              negative errno on error
7013  */
7014 static void lod_sel_adjust_extents(const struct lu_env *env,
7015                                    struct lod_object *lo,
7016                                    int max_comp, int index)
7017 {
7018         struct lod_layout_component *lod_comp = NULL;
7019         struct lod_layout_component *next = NULL;
7020         struct lod_layout_component *prev = NULL;
7021         __u64 new_start = 0;
7022         __u64 start;
7023         int i;
7024
7025         /* Extension space component */
7026         lod_comp = &lo->ldo_comp_entries[index];
7027         next = &lo->ldo_comp_entries[index + 1];
7028         prev = &lo->ldo_comp_entries[index - 1];
7029
7030         LASSERT(lod_comp != NULL && prev != NULL && next != NULL);
7031         LASSERT(lod_comp->llc_flags & LCME_FL_EXTENSION);
7032
7033         /* Previous is being removed */
7034         if (prev && prev->llc_id == LCME_ID_INVAL)
7035                 new_start = prev->llc_extent.e_start;
7036         else
7037                 new_start = lod_comp->llc_extent.e_start;
7038
7039         for (i = index + 1; i < max_comp; i++) {
7040                 lod_comp = &lo->ldo_comp_entries[i];
7041
7042                 start = lod_comp->llc_extent.e_start;
7043                 lod_comp->llc_extent.e_start = new_start;
7044
7045                 /* We only move zero length extendable components */
7046                 if (!(start == lod_comp->llc_extent.e_end))
7047                         break;
7048
7049                 LASSERT(!(lod_comp->llc_flags & LCME_FL_INIT));
7050
7051                 lod_comp->llc_extent.e_end = new_start;
7052         }
7053 }
7054
7055 /* Calculate the proposed 'new end' for a component we're extending */
7056 static __u64 lod_extension_new_end(__u64 extension_size, __u64 extent_end,
7057                                    __u32 stripe_size, __u64 component_end,
7058                                    __u64 extension_end)
7059 {
7060         __u64 new_end;
7061
7062         LASSERT(extension_size != 0 && stripe_size != 0);
7063
7064         /* Round up to extension size */
7065         if (extent_end == OBD_OBJECT_EOF) {
7066                 new_end = OBD_OBJECT_EOF;
7067         } else {
7068                 /* Add at least extension_size to the previous component_end,
7069                  * covering the req layout extent */
7070                 new_end = max(extent_end - component_end, extension_size);
7071                 new_end = roundup(new_end, extension_size);
7072                 new_end += component_end;
7073
7074                 /* Component end must be min stripe size aligned */
7075                 if (new_end % stripe_size) {
7076                         CDEBUG(D_LAYOUT, "new component end is not aligned "
7077                                "by the stripe size %u: [%llu, %llu) ext size "
7078                                "%llu new end %llu, aligning\n",
7079                                stripe_size, component_end, extent_end,
7080                                extension_size, new_end);
7081                         new_end = roundup(new_end, stripe_size);
7082                 }
7083
7084                 /* Overflow */
7085                 if (new_end < extent_end)
7086                         new_end = OBD_OBJECT_EOF;
7087         }
7088
7089         /* Don't extend past the end of the extension component */
7090         if (new_end > extension_end)
7091                 new_end = extension_end;
7092
7093         return new_end;
7094 }
7095
7096 /**
7097  * Calculate the exact reservation (per-OST extension_size) on the OSTs being
7098  * instantiated. It needs to be calculated in advance and taken into account at
7099  * the instantiation time, because otherwise lod_statfs_and_check() may consider
7100  * an OST as OK, but SEL needs its extension_size to fit the free space and the
7101  * OST may turn out to be low-on-space, thus inappropriate OST may be used and
7102  * ENOSPC occurs.
7103  *
7104  * \param[in] lod_comp          lod component we are checking
7105  *
7106  * \retval      size to reserved on each OST of lod_comp's stripe.
7107  */
7108 static __u64 lod_sel_stripe_reserved(struct lod_layout_component *lod_comp)
7109 {
7110         /* extension_size is file level, so we must divide by stripe count to
7111          * compare it to available space on a single OST */
7112         return  lod_comp->llc_stripe_size * SEL_UNIT_SIZE /
7113                 lod_comp->llc_stripe_count;
7114 }
7115
7116 /* As lod_sel_handler() could be re-entered for the same component several
7117  * times, this is the data for the next call. Fields could be changed to
7118  * component indexes when needed, (e.g. if there is no need to instantiate
7119  * all the previous components up to the current position) to tell the caller
7120  * where to start over from. */
7121 struct sel_data {
7122         int sd_force;
7123         int sd_repeat;
7124 };
7125
7126 /**
7127  * Process extent updates for a particular layout component
7128  *
7129  * Handle layout updates for a particular extension space component touched by
7130  * a layout update operation.  Core function of self-extending PFL feature.
7131  *
7132  * In general, this function processes exactly *one* stage of an extension
7133  * operation, modifying the layout accordingly, then returns to the caller.
7134  * The caller is responsible for restarting processing with the new layout,
7135  * which may repeatedly return to this function until the extension updates
7136  * are complete.
7137  *
7138  * This function does one of a few things to the layout:
7139  * 1. Extends the component before the current extension space component to
7140  * allow it to accomodate the requested operation (if space/policy permit that
7141  * component to continue on its current OSTs)
7142  *
7143  * 2. If extension of the existing component fails, we do one of two things:
7144  *    a. If there is a component after the extension space, we remove the
7145  *       extension space component, move the start of the next component down
7146  *       accordingly, then notify the caller to restart processing w/the new
7147  *       layout.
7148  *    b. If there is no following component, we try repeating the current
7149  *       component, creating a new component using the current one as a
7150  *       template (keeping its stripe properties but not specific striping),
7151  *       and try assigning striping for this component.  If there is sufficient
7152  *       free space on the OSTs chosen for this component, it is instantiated
7153  *       and i/o continues there.
7154  *
7155  *       If there is not sufficient space on the new OSTs, we remove this new
7156  *       component & extend the current component.
7157  *
7158  * Note further that uninited components followed by extension space can be zero
7159  * length meaning that we will try to extend them before initializing them, and
7160  * if that fails, they will be removed without initialization.
7161  *
7162  * 3. If we extend to/beyond the end of an extension space component, that
7163  * component is exhausted (all of its range has been given to real components),
7164  * so we remove it and restart processing.
7165  *
7166  * \param[in] env               execution environment for this thread
7167  * \param[in,out] lo            object to update the layout of
7168  * \param[in] extent            layout extent for requested operation, update
7169  *                              layout to fit this operation
7170  * \param[in] th                transaction handle for this operation
7171  * \param[in,out] max_comp      the highest comp for the portion of the layout
7172  *                              we are operating on (For FLR, the chosen
7173  *                              replica).  Updated because we may remove
7174  *                              components.
7175  * \param[in] index             index of the extension space component we're
7176  *                              working on
7177  * \param[in] write             if this is write op
7178  * \param[in,out] force         if the extension is to be forced; set here
7179                                 to force it on the 2nd call for the same
7180                                 extension component
7181  *
7182  * \retval      0 on success
7183  * \retval      negative errno on error
7184  */
7185 static int lod_sel_handler(const struct lu_env *env,
7186                           struct lod_object *lo,
7187                           struct lu_extent *extent,
7188                           struct thandle *th, int *max_comp,
7189                           int index, int write,
7190                           struct sel_data *sd)
7191 {
7192         struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7193         struct lod_thread_info *info = lod_env_info(env);
7194         struct lod_layout_component *lod_comp;
7195         struct lod_layout_component *prev;
7196         struct lod_layout_component *next = NULL;
7197         __u64 extension_size, reserve;
7198         __u64 new_end = 0;
7199         bool repeated;
7200         int change = 0;
7201         int rc = 0;
7202         ENTRY;
7203
7204         /* First component cannot be extension space */
7205         if (index == 0) {
7206                 CERROR("%s: "DFID" first component cannot be extension space\n",
7207                        lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
7208                 RETURN(-EINVAL);
7209         }
7210
7211         lod_comp = &lo->ldo_comp_entries[index];
7212         prev = &lo->ldo_comp_entries[index - 1];
7213         if ((index + 1) < *max_comp)
7214                 next = &lo->ldo_comp_entries[index + 1];
7215
7216         /* extension size uses the stripe size field as KiB */
7217         extension_size = lod_comp->llc_stripe_size * SEL_UNIT_SIZE;
7218
7219         CDEBUG(D_LAYOUT, "prev start %llu, extension start %llu, extension end"
7220                " %llu, extension size %llu\n", prev->llc_extent.e_start,
7221                lod_comp->llc_extent.e_start, lod_comp->llc_extent.e_end,
7222                extension_size);
7223
7224         /* Two extension space components cannot be adjacent & extension space
7225          * components cannot be init */
7226         if ((prev->llc_flags & LCME_FL_EXTENSION) ||
7227             !(ergo(next, !(next->llc_flags & LCME_FL_EXTENSION))) ||
7228              lod_comp_inited(lod_comp)) {
7229                 CERROR("%s: "DFID" invalid extension space components\n",
7230                        lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
7231                 RETURN(-EINVAL);
7232         }
7233
7234         reserve = lod_sel_stripe_reserved(lod_comp);
7235
7236         if (!prev->llc_stripe) {
7237                 CDEBUG(D_LAYOUT, "Previous component not inited\n");
7238                 info->lti_count = 1;
7239                 info->lti_comp_idx[0] = index - 1;
7240                 rc = lod_declare_instantiate_components(env, lo, th, reserve);
7241                 /* ENOSPC tells us we can't use this component.  If there is
7242                  * a next or we are repeating, we either spill over (next) or
7243                  * extend the original comp (repeat).  Otherwise, return the
7244                  * error to the user. */
7245                 if (rc == -ENOSPC && (next || sd->sd_repeat))
7246                         rc = 1;
7247                 if (rc < 0)
7248                         RETURN(rc);
7249         }
7250
7251         if (sd->sd_force == 0 && rc == 0)
7252                 rc = !lod_sel_osts_allowed(env, lo, index - 1, reserve, extent,
7253                                            &lod_comp->llc_extent, write);
7254
7255         repeated = !!(sd->sd_repeat);
7256         sd->sd_repeat = 0;
7257         sd->sd_force = 0;
7258
7259         /* Extend previous component */
7260         if (rc == 0) {
7261                 new_end = lod_extension_new_end(extension_size, extent->e_end,
7262                                                 prev->llc_stripe_size,
7263                                                 prev->llc_extent.e_end,
7264                                                 lod_comp->llc_extent.e_end);
7265
7266                 CDEBUG(D_LAYOUT, "new end %llu\n", new_end);
7267                 lod_comp->llc_extent.e_start = new_end;
7268                 prev->llc_extent.e_end = new_end;
7269
7270                 if (prev->llc_extent.e_end == lod_comp->llc_extent.e_end) {
7271                         CDEBUG(D_LAYOUT, "Extension component exhausted\n");
7272                         lod_comp->llc_id = LCME_ID_INVAL;
7273                         change--;
7274                 }
7275         } else {
7276                 /* rc == 1, failed to extend current component */
7277                 LASSERT(rc == 1);
7278                 if (next) {
7279                         /* Normal 'spillover' case - Remove the extension
7280                          * space component & bring down the start of the next
7281                          * component. */
7282                         lod_comp->llc_id = LCME_ID_INVAL;
7283                         change--;
7284                         if (!(prev->llc_flags & LCME_FL_INIT)) {
7285                                 prev->llc_id = LCME_ID_INVAL;
7286                                 change--;
7287                         }
7288                         lod_sel_adjust_extents(env, lo, *max_comp, index);
7289                 } else if (lod_comp_inited(prev)) {
7290                         /* If there is no next, and the previous component is
7291                          * INIT'ed, try repeating the previous component. */
7292                         LASSERT(repeated == 0);
7293                         rc = lod_layout_repeat_comp(env, lo, index - 1);
7294                         if (rc < 0)
7295                                 RETURN(rc);
7296                         change++;
7297                         /* The previous component is a repeated component.
7298                          * Record this so we don't keep trying to repeat it. */
7299                         sd->sd_repeat = 1;
7300                 } else {
7301                         /* If the previous component is not INIT'ed, this may
7302                          * be a component we have just instantiated but failed
7303                          * to extend. Or even a repeated component we failed
7304                          * to prepare a striping for. Do not repeat but instead
7305                          * remove the repeated component & force the extention
7306                          * of the original one */
7307                         sd->sd_force = 1;
7308                         if (repeated) {
7309                                 prev->llc_id = LCME_ID_INVAL;
7310                                 change--;
7311                         }
7312                 }
7313         }
7314
7315         if (change < 0) {
7316                 rc = lod_layout_del_prep_layout(env, lo, NULL);
7317                 if (rc < 0)
7318                         RETURN(rc);
7319                 LASSERTF(-rc == change,
7320                          "number deleted %d != requested %d\n", -rc,
7321                          change);
7322         }
7323         *max_comp = *max_comp + change;
7324
7325         /* lod_del_prep_layout reallocates ldo_comp_entries, so we must
7326          * refresh these pointers before using them */
7327         lod_comp = &lo->ldo_comp_entries[index];
7328         prev = &lo->ldo_comp_entries[index - 1];
7329         CDEBUG(D_LAYOUT, "After extent updates: prev start %llu, current start "
7330                "%llu, current end %llu max_comp %d ldo_comp_cnt %d\n",
7331                prev->llc_extent.e_start, lod_comp->llc_extent.e_start,
7332                lod_comp->llc_extent.e_end, *max_comp, lo->ldo_comp_cnt);
7333
7334         /* Layout changed successfully */
7335         RETURN(0);
7336 }
7337
7338 /**
7339  * Declare layout extent updates
7340  *
7341  * Handles extensions.  Identifies extension components touched by current
7342  * operation and passes them to processing function.
7343  *
7344  * Restarts with updated layouts from the processing function until the current
7345  * operation no longer touches an extension space component.
7346  *
7347  * \param[in] env       execution environment for this thread
7348  * \param[in,out] lo    object to update the layout of
7349  * \param[in] extent    layout extent for requested operation, update layout to
7350  *                      fit this operation
7351  * \param[in] th        transaction handle for this operation
7352  * \param[in] pick      identifies chosen mirror for FLR layouts
7353  * \param[in] write     if this is write op
7354  *
7355  * \retval      1 on layout changed, 0 on no change
7356  * \retval      negative errno on error
7357  */
7358 static int lod_declare_update_extents(const struct lu_env *env,
7359                 struct lod_object *lo, struct lu_extent *extent,
7360                 struct thandle *th, int pick, int write)
7361 {
7362         struct lod_thread_info *info = lod_env_info(env);
7363         struct lod_layout_component *lod_comp;
7364         bool layout_changed = false;
7365         struct sel_data sd = { 0 };
7366         int start_index;
7367         int i = 0;
7368         int max_comp = 0;
7369         int rc = 0, rc2;
7370         int change = 0;
7371         ENTRY;
7372
7373         /* This makes us work on the components of the chosen mirror */
7374         start_index = lo->ldo_mirrors[pick].lme_start;
7375         max_comp = lo->ldo_mirrors[pick].lme_end + 1;
7376         if (lo->ldo_flr_state == LCM_FL_NONE)
7377                 LASSERT(start_index == 0 && max_comp == lo->ldo_comp_cnt);
7378
7379         CDEBUG(D_LAYOUT, "extent->e_start %llu, extent->e_end %llu\n",
7380                extent->e_start, extent->e_end);
7381         for (i = start_index; i < max_comp; i++) {
7382                 lod_comp = &lo->ldo_comp_entries[i];
7383
7384                 /* We've passed all components of interest */
7385                 if (lod_comp->llc_extent.e_start >= extent->e_end)
7386                         break;
7387
7388                 if (lod_comp->llc_flags & LCME_FL_EXTENSION) {
7389                         layout_changed = true;
7390                         rc = lod_sel_handler(env, lo, extent, th, &max_comp,
7391                                              i, write, &sd);
7392                         if (rc < 0)
7393                                 GOTO(out, rc);
7394
7395                         /* Nothing has changed behind the prev one */
7396                         i -= 2;
7397                         continue;
7398                 }
7399         }
7400
7401         /* We may have added or removed components.  If so, we must update the
7402          * start & ends of all the mirrors after the current one, and the end
7403          * of the current mirror. */
7404         change = max_comp - 1 - lo->ldo_mirrors[pick].lme_end;
7405         if (change) {
7406                 lo->ldo_mirrors[pick].lme_end += change;
7407                 for (i = pick + 1; i < lo->ldo_mirror_count; i++) {
7408                         lo->ldo_mirrors[i].lme_start += change;
7409                         lo->ldo_mirrors[i].lme_end += change;
7410                 }
7411         }
7412
7413         EXIT;
7414 out:
7415         /* The amount of components has changed, adjust the lti_comp_idx */
7416         rc2 = lod_layout_data_init(info, lo->ldo_comp_cnt);
7417
7418         return rc < 0 ? rc : rc2 < 0 ? rc2 : layout_changed;
7419 }
7420
7421 /* If striping is already instantiated or INIT'ed DOM? */
7422 static bool lod_is_instantiation_needed(struct lod_layout_component *comp)
7423 {
7424         return !(((lov_pattern(comp->llc_pattern) == LOV_PATTERN_MDT) &&
7425                   lod_comp_inited(comp)) || comp->llc_stripe);
7426 }
7427
7428 /**
7429  * Declare layout update for a non-FLR layout.
7430  *
7431  * \param[in] env       execution environment for this thread
7432  * \param[in,out] lo    object to update the layout of
7433  * \param[in] layout    layout intent for requested operation, "update" is
7434  *                      a process of reacting to this
7435  * \param[in] buf       buffer containing lov ea (see comment on usage inline)
7436  * \param[in] th        transaction handle for this operation
7437  *
7438  * \retval      0 on success
7439  * \retval      negative errno on error
7440  */
7441 static int lod_declare_update_plain(const struct lu_env *env,
7442                 struct lod_object *lo, struct layout_intent *layout,
7443                 const struct lu_buf *buf, struct thandle *th)
7444 {
7445         struct lod_thread_info *info = lod_env_info(env);
7446         struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7447         struct lod_layout_component *lod_comp;
7448         struct lov_comp_md_v1 *comp_v1 = NULL;
7449         bool layout_changed = false;
7450         bool replay = false;
7451         int i, rc;
7452         ENTRY;
7453
7454         LASSERT(lo->ldo_flr_state == LCM_FL_NONE);
7455
7456         /*
7457          * In case the client is passing lovea, which only happens during
7458          * the replay of layout intent write RPC for now, we may need to
7459          * parse the lovea and apply new layout configuration.
7460          */
7461         if (buf && buf->lb_len)  {
7462                 struct lov_user_md_v1 *v1 = buf->lb_buf;
7463
7464                 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
7465                     v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
7466                                               LOV_MAGIC_COMP_V1)) {
7467                         CERROR("%s: the replay buffer of layout extend "
7468                                "(magic %#x) does not contain expected "
7469                                "composite layout.\n",
7470                                lod2obd(d)->obd_name, v1->lmm_magic);
7471                         GOTO(out, rc = -EINVAL);
7472                 }
7473
7474                 rc = lod_use_defined_striping(env, lo, buf);
7475                 if (rc)
7476                         GOTO(out, rc);
7477                 lo->ldo_comp_cached = 1;
7478
7479                 rc = lod_get_lov_ea(env, lo);
7480                 if (rc <= 0)
7481                         GOTO(out, rc);
7482                 /* old on-disk EA is stored in info->lti_buf */
7483                 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
7484                 replay = true;
7485                 layout_changed = true;
7486
7487                 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
7488                 if (rc)
7489                         GOTO(out, rc);
7490         } else {
7491                 /* non replay path */
7492                 rc = lod_striping_load(env, lo);
7493                 if (rc)
7494                         GOTO(out, rc);
7495         }
7496
7497         /* Make sure defined layout covers the requested write range. */
7498         lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
7499         if (lo->ldo_comp_cnt > 1 &&
7500             lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
7501             lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
7502                 CDEBUG_LIMIT(replay ? D_ERROR : D_LAYOUT,
7503                              "%s: the defined layout [0, %#llx) does not "
7504                              "covers the write range "DEXT"\n",
7505                              lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
7506                              PEXT(&layout->li_extent));
7507                 GOTO(out, rc = -EINVAL);
7508         }
7509
7510         CDEBUG(D_LAYOUT, "%s: "DFID": update components "DEXT"\n",
7511                lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
7512                PEXT(&layout->li_extent));
7513
7514         if (!replay) {
7515                 rc = lod_declare_update_extents(env, lo, &layout->li_extent,
7516                                 th, 0, layout->li_opc == LAYOUT_INTENT_WRITE);
7517                 if (rc < 0)
7518                         GOTO(out, rc);
7519                 else if (rc)
7520                         layout_changed = true;
7521         }
7522
7523         /*
7524          * Iterate ld->ldo_comp_entries, find the component whose extent under
7525          * the write range and not instantianted.
7526          */
7527         for (i = 0; i < lo->ldo_comp_cnt; i++) {
7528                 lod_comp = &lo->ldo_comp_entries[i];
7529
7530                 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
7531                         break;
7532
7533                 if (!replay) {
7534                         /* If striping is instantiated or INIT'ed DOM skip */
7535                         if (!lod_is_instantiation_needed(lod_comp))
7536                                 continue;
7537                 } else {
7538                         /**
7539                          * In replay path, lod_comp is the EA passed by
7540                          * client replay buffer,  comp_v1 is the pre-recovery
7541                          * on-disk EA, we'd sift out those components which
7542                          * were init-ed in the on-disk EA.
7543                          */
7544                         if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
7545                             LCME_FL_INIT)
7546                                 continue;
7547                 }
7548                 /*
7549                  * this component hasn't instantiated in normal path, or during
7550                  * replay it needs replay the instantiation.
7551                  */
7552
7553                 /* A released component is being extended */
7554                 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
7555                         GOTO(out, rc = -EINVAL);
7556
7557                 LASSERT(info->lti_comp_idx != NULL);
7558                 info->lti_comp_idx[info->lti_count++] = i;
7559                 layout_changed = true;
7560         }
7561
7562         if (!layout_changed)
7563                 RETURN(-EALREADY);
7564
7565         lod_obj_inc_layout_gen(lo);
7566         rc = lod_declare_instantiate_components(env, lo, th, 0);
7567         EXIT;
7568 out:
7569         if (rc)
7570                 lod_striping_free(env, lo);
7571         return rc;
7572 }
7573
7574 static inline int lod_comp_index(struct lod_object *lo,
7575                                  struct lod_layout_component *lod_comp)
7576 {
7577         LASSERT(lod_comp >= lo->ldo_comp_entries &&
7578                 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
7579
7580         return lod_comp - lo->ldo_comp_entries;
7581 }
7582
7583 /**
7584  * Stale other mirrors by writing extent.
7585  */
7586 static int lod_stale_components(const struct lu_env *env, struct lod_object *lo,
7587                                 int primary, struct lu_extent *extent,
7588                                 struct thandle *th)
7589 {
7590         struct lod_layout_component *pri_comp, *lod_comp;
7591         struct lu_extent pri_extent;
7592         int rc = 0;
7593         int i;
7594         ENTRY;
7595
7596         /* The writing extent decides which components in the primary
7597          * are affected... */
7598         CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
7599
7600 restart:
7601         lod_foreach_mirror_comp(pri_comp, lo, primary) {
7602                 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
7603                         continue;
7604
7605                 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
7606                        lod_comp_index(lo, pri_comp),
7607                        PEXT(&pri_comp->llc_extent));
7608
7609                 pri_extent.e_start = pri_comp->llc_extent.e_start;
7610                 pri_extent.e_end = pri_comp->llc_extent.e_end;
7611
7612                 for (i = 0; i < lo->ldo_mirror_count; i++) {
7613                         if (i == primary)
7614                                 continue;
7615                         rc = lod_declare_update_extents(env, lo, &pri_extent,
7616                                                         th, i, 0);
7617                         /* if update_extents changed the layout, it may have
7618                          * reallocated the component array, so start over to
7619                          * avoid using stale pointers */
7620                         if (rc == 1)
7621                                 goto restart;
7622                         if (rc < 0)
7623                                 RETURN(rc);
7624
7625                         /* ... and then stale other components that are
7626                          * overlapping with primary components */
7627                         lod_foreach_mirror_comp(lod_comp, lo, i) {
7628                                 if (!lu_extent_is_overlapped(
7629                                                         &pri_extent,
7630                                                         &lod_comp->llc_extent))
7631                                         continue;
7632
7633                                 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
7634                                       i, lod_comp_index(lo, lod_comp));
7635
7636                                 lod_comp->llc_flags |= LCME_FL_STALE;
7637                                 lo->ldo_mirrors[i].lme_stale = 1;
7638                         }
7639                 }
7640         }
7641
7642         RETURN(rc);
7643 }
7644
7645 /**
7646  * check an OST's availability
7647  * \param[in] env       execution environment
7648  * \param[in] lo        lod object
7649  * \param[in] dt        dt object
7650  * \param[in] index     mirror index
7651  *
7652  * \retval      negative if failed
7653  * \retval      1 if \a dt is available
7654  * \retval      0 if \a dt is not available
7655  */
7656 static inline int lod_check_ost_avail(const struct lu_env *env,
7657                                       struct lod_object *lo,
7658                                       struct dt_object *dt, int index)
7659 {
7660         struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7661         struct lod_tgt_desc *ost;
7662         __u32 idx;
7663         int type = LU_SEQ_RANGE_OST;
7664         int rc;
7665
7666         rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &idx, &type);
7667         if (rc < 0) {
7668                 CERROR("%s: can't locate "DFID":rc = %d\n",
7669                        lod2obd(lod)->obd_name, PFID(lu_object_fid(&dt->do_lu)),
7670                        rc);
7671                 return rc;
7672         }
7673
7674         ost = OST_TGT(lod, idx);
7675         if (ost->ltd_active == 0) {
7676                 CDEBUG(D_LAYOUT, DFID ": mirror %d OST%d unavail\n",
7677                        PFID(lod_object_fid(lo)), index, idx);
7678                 return 0;
7679         }
7680
7681         return 1;
7682 }
7683
7684 /**
7685  * Pick primary mirror for write
7686  * \param[in] env       execution environment
7687  * \param[in] lo        object
7688  * \param[in] extent    write range
7689  */
7690 static int lod_primary_pick(const struct lu_env *env, struct lod_object *lo,
7691                             struct lu_extent *extent)
7692 {
7693         struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7694         unsigned int seq = 0;
7695         struct lod_layout_component *lod_comp;
7696         int i, j, rc;
7697         int picked = -1, second_pick = -1, third_pick = -1;
7698         ENTRY;
7699
7700         if (CFS_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
7701                 get_random_bytes(&seq, sizeof(seq));
7702                 seq %= lo->ldo_mirror_count;
7703         }
7704
7705         /**
7706          * Pick a mirror as the primary, and check the availability of OSTs.
7707          *
7708          * This algo can be revised later after knowing the topology of
7709          * cluster.
7710          */
7711         lod_qos_statfs_update(env, lod, &lod->lod_ost_descs);
7712
7713         rc = lod_fill_mirrors(lo);
7714         if (rc)
7715                 RETURN(rc);
7716
7717         for (i = 0; i < lo->ldo_mirror_count; i++) {
7718                 bool ost_avail = true;
7719                 int index = (i + seq) % lo->ldo_mirror_count;
7720
7721                 if (lo->ldo_mirrors[index].lme_stale) {
7722                         CDEBUG(D_LAYOUT, DFID": mirror %d stale\n",
7723                                PFID(lod_object_fid(lo)), index);
7724                         continue;
7725                 }
7726
7727                 /* 2nd pick is for the primary mirror containing unavail OST */
7728                 if (lo->ldo_mirrors[index].lme_prefer && second_pick < 0)
7729                         second_pick = index;
7730
7731                 /* 3rd pick is for non-primary mirror containing unavail OST */
7732                 if (second_pick < 0 && third_pick < 0)
7733                         third_pick = index;
7734
7735                 /**
7736                  * we found a non-primary 1st pick, we'd like to find a
7737                  * potential pirmary mirror.
7738                  */
7739                 if (picked >= 0 && !lo->ldo_mirrors[index].lme_prefer)
7740                         continue;
7741
7742                 /* check the availability of OSTs */
7743                 lod_foreach_mirror_comp(lod_comp, lo, index) {
7744                         if (!lod_comp_inited(lod_comp) || !lod_comp->llc_stripe)
7745                                 continue;
7746
7747                         for (j = 0; j < lod_comp->llc_stripe_count; j++) {
7748                                 struct dt_object *dt = lod_comp->llc_stripe[j];
7749
7750                                 rc = lod_check_ost_avail(env, lo, dt, index);
7751                                 if (rc < 0)
7752                                         RETURN(rc);
7753
7754                                 ost_avail = !!rc;
7755                                 if (!ost_avail)
7756                                         break;
7757                         } /* for all dt object in one component */
7758                         if (!ost_avail)
7759                                 break;
7760                 } /* for all components in a mirror */
7761
7762                 /**
7763                  * the OSTs where allocated objects locates in the components
7764                  * of the mirror are available.
7765                  */
7766                 if (!ost_avail)
7767                         continue;
7768
7769                 /* this mirror has all OSTs available */
7770                 picked = index;
7771
7772                 /**
7773                  * primary with all OSTs are available, this is the perfect
7774                  * 1st pick.
7775                  */
7776                 if (lo->ldo_mirrors[index].lme_prefer)
7777                         break;
7778         } /* for all mirrors */
7779
7780         /* failed to pick a sound mirror, lower our expectation */
7781         if (picked < 0)
7782                 picked = second_pick;
7783         if (picked < 0)
7784                 picked = third_pick;
7785         if (picked < 0)
7786                 RETURN(-ENODATA);
7787
7788         RETURN(picked);
7789 }
7790
7791 static int lod_prepare_resync_mirror(const struct lu_env *env,
7792                                      struct lod_object *lo,
7793                                      __u16 mirror_id)
7794 {
7795         struct lod_thread_info *info = lod_env_info(env);
7796         struct lod_layout_component *lod_comp;
7797         bool neg = !!(MIRROR_ID_NEG & mirror_id);
7798         int i;
7799
7800         mirror_id &= ~MIRROR_ID_NEG;
7801
7802         for (i = 0; i < lo->ldo_mirror_count; i++) {
7803                 if ((!neg && lo->ldo_mirrors[i].lme_id != mirror_id) ||
7804                     (neg && lo->ldo_mirrors[i].lme_id == mirror_id))
7805                         continue;
7806
7807                 lod_foreach_mirror_comp(lod_comp, lo, i) {
7808                         if (lod_comp_inited(lod_comp))
7809                                 continue;
7810
7811                         info->lti_comp_idx[info->lti_count++] =
7812                                 lod_comp_index(lo, lod_comp);
7813                 }
7814         }
7815
7816         return 0;
7817 }
7818
7819 /**
7820  * figure out the components should be instantiated for resync.
7821  */
7822 static int lod_prepare_resync(const struct lu_env *env, struct lod_object *lo,
7823                               struct lu_extent *extent)
7824 {
7825         struct lod_thread_info *info = lod_env_info(env);
7826         struct lod_layout_component *lod_comp;
7827         unsigned int need_sync = 0;
7828         int i;
7829
7830         CDEBUG(D_LAYOUT,
7831                DFID": instantiate all stale components in "DEXT"\n",
7832                PFID(lod_object_fid(lo)), PEXT(extent));
7833
7834         /**
7835          * instantiate all components within this extent, even non-stale
7836          * components.
7837          */
7838         for (i = 0; i < lo->ldo_mirror_count; i++) {
7839                 if (!lo->ldo_mirrors[i].lme_stale)
7840                         continue;
7841
7842                 lod_foreach_mirror_comp(lod_comp, lo, i) {
7843                         if (!lu_extent_is_overlapped(extent,
7844                                                 &lod_comp->llc_extent))
7845                                 break;
7846
7847                         need_sync++;
7848
7849                         if (lod_comp_inited(lod_comp))
7850                                 continue;
7851
7852                         CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
7853                                i, lod_comp_index(lo, lod_comp));
7854                         info->lti_comp_idx[info->lti_count++] =
7855                                         lod_comp_index(lo, lod_comp);
7856                 }
7857         }
7858
7859         return need_sync ? 0 : -EALREADY;
7860 }
7861
7862 static int lod_declare_update_rdonly(const struct lu_env *env,
7863                 struct lod_object *lo, struct md_layout_change *mlc,
7864                 struct thandle *th)
7865 {
7866         struct lod_thread_info *info = lod_env_info(env);
7867         struct lu_attr *layout_attr = &info->lti_layout_attr;
7868         struct lod_layout_component *lod_comp;
7869         struct lu_extent extent = { 0 };
7870         int rc;
7871         ENTRY;
7872
7873         LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
7874         LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7875                 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7876         LASSERT(lo->ldo_mirror_count > 0);
7877
7878         if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7879                 struct layout_intent *layout = mlc->mlc_intent;
7880                 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7881                 int picked;
7882
7883                 extent = layout->li_extent;
7884                 CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
7885                        PFID(lod_object_fid(lo)), PEXT(&extent));
7886
7887                 picked = lod_primary_pick(env, lo, &extent);
7888                 if (picked < 0)
7889                         RETURN(picked);
7890
7891                 CDEBUG(D_LAYOUT, DFID": picked mirror id %u as primary\n",
7892                        PFID(lod_object_fid(lo)),
7893                        lo->ldo_mirrors[picked].lme_id);
7894
7895                 /* Update extents of primary before staling */
7896                 rc = lod_declare_update_extents(env, lo, &extent, th, picked,
7897                                                 write);
7898                 if (rc < 0)
7899                         GOTO(out, rc);
7900
7901                 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
7902                         /**
7903                          * trunc transfers [0, size) in the intent extent, we'd
7904                          * stale components overlapping [size, eof).
7905                          */
7906                         extent.e_start = extent.e_end;
7907                         extent.e_end = OBD_OBJECT_EOF;
7908                 }
7909
7910                 /* stale overlapping components from other mirrors */
7911                 rc = lod_stale_components(env, lo, picked, &extent, th);
7912                 if (rc < 0)
7913                         GOTO(out, rc);
7914
7915                 /* restore truncate intent extent */
7916                 if (layout->li_opc == LAYOUT_INTENT_TRUNC)
7917                         extent.e_end = extent.e_start;
7918
7919                 /* instantiate components for the picked mirror, start from 0 */
7920                 extent.e_start = 0;
7921
7922                 lod_foreach_mirror_comp(lod_comp, lo, picked) {
7923                         if (!lu_extent_is_overlapped(&extent,
7924                                                      &lod_comp->llc_extent))
7925                                 break;
7926
7927                         if (!lod_is_instantiation_needed(lod_comp))
7928                                 continue;
7929
7930                         info->lti_comp_idx[info->lti_count++] =
7931                                                 lod_comp_index(lo, lod_comp);
7932                 }
7933
7934                 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7935         } else { /* MD_LAYOUT_RESYNC */
7936                 int i;
7937
7938                 /**
7939                  * could contain multiple non-stale mirrors, so we need to
7940                  * prep uninited all components assuming any non-stale mirror
7941                  * could be picked as the primary mirror.
7942                  */
7943                 if (mlc->mlc_mirror_id == 0) {
7944                         /* normal resync */
7945                         for (i = 0; i < lo->ldo_mirror_count; i++) {
7946                                 if (lo->ldo_mirrors[i].lme_stale)
7947                                         continue;
7948
7949                                 lod_foreach_mirror_comp(lod_comp, lo, i) {
7950                                         if (!lod_comp_inited(lod_comp))
7951                                                 break;
7952
7953                                         if (extent.e_end <
7954                                                 lod_comp->llc_extent.e_end)
7955                                                 extent.e_end =
7956                                                      lod_comp->llc_extent.e_end;
7957                                 }
7958                         }
7959                         rc = lod_prepare_resync(env, lo, &extent);
7960                         if (rc)
7961                                 GOTO(out, rc);
7962                 } else {
7963                         /* mirror write, try to init its all components */
7964                         rc = lod_prepare_resync_mirror(env, lo,
7965                                                        mlc->mlc_mirror_id);
7966                         if (rc)
7967                                 GOTO(out, rc);
7968                 }
7969
7970                 /* change the file state to SYNC_PENDING */
7971                 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7972         }
7973
7974         /* Reset the layout version once it's becoming too large.
7975          * This way it can make sure that the layout version is
7976          * monotonously increased in this writing era. */
7977         lod_obj_inc_layout_gen(lo);
7978
7979         rc = lod_declare_instantiate_components(env, lo, th, 0);
7980         if (rc)
7981                 GOTO(out, rc);
7982
7983         layout_attr->la_valid = LA_LAYOUT_VERSION;
7984         layout_attr->la_layout_version = 0;
7985         if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7986                 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7987         rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7988         if (rc)
7989                 GOTO(out, rc);
7990
7991 out:
7992         if (rc)
7993                 lod_striping_free(env, lo);
7994         RETURN(rc);
7995 }
7996
7997 static int lod_declare_update_write_pending(const struct lu_env *env,
7998                 struct lod_object *lo, struct md_layout_change *mlc,
7999                 struct thandle *th)
8000 {
8001         struct lod_thread_info *info = lod_env_info(env);
8002         struct lu_attr *layout_attr = &info->lti_layout_attr;
8003         struct lod_layout_component *lod_comp;
8004         struct lu_extent extent = { 0 };
8005         int primary = -1;
8006         int i;
8007         int rc;
8008         ENTRY;
8009
8010         LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
8011         LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
8012                 mlc->mlc_opc == MD_LAYOUT_RESYNC);
8013
8014         /* look for the first preferred mirror */
8015         for (i = 0; i < lo->ldo_mirror_count; i++) {
8016                 if (lo->ldo_mirrors[i].lme_stale)
8017                         continue;
8018                 if (lo->ldo_mirrors[i].lme_prefer == 0)
8019                         continue;
8020
8021                 primary = i;
8022                 break;
8023         }
8024         if (primary < 0) {
8025                 /* no primary, use any in-sync */
8026                 for (i = 0; i < lo->ldo_mirror_count; i++) {
8027                         if (lo->ldo_mirrors[i].lme_stale)
8028                                 continue;
8029                         primary = i;
8030                         break;
8031                 }
8032                 if (primary < 0) {
8033                         CERROR(DFID ": doesn't have a primary mirror\n",
8034                                PFID(lod_object_fid(lo)));
8035                         GOTO(out, rc = -ENODATA);
8036                 }
8037         }
8038
8039         CDEBUG(D_LAYOUT, DFID": found primary %u\n",
8040                PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
8041
8042         LASSERT(!lo->ldo_mirrors[primary].lme_stale);
8043
8044         /* for LAYOUT_WRITE opc, it has to do the following operations:
8045          * 1. stale overlapping componets from stale mirrors;
8046          * 2. instantiate components of the primary mirror;
8047          * 3. transfter layout version to all objects of the primary;
8048          *
8049          * for LAYOUT_RESYNC opc, it will do:
8050          * 1. instantiate components of all stale mirrors;
8051          * 2. transfer layout version to all objects to close write era. */
8052
8053         if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
8054                 struct layout_intent *layout = mlc->mlc_intent;
8055                 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
8056
8057                 LASSERT(mlc->mlc_intent != NULL);
8058
8059                 extent = mlc->mlc_intent->li_extent;
8060
8061                 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
8062                        PFID(lod_object_fid(lo)), PEXT(&extent));
8063
8064                 /* 1. Update extents of primary before staling */
8065                 rc = lod_declare_update_extents(env, lo, &extent, th, primary,
8066                                                 write);
8067                 if (rc < 0)
8068                         GOTO(out, rc);
8069
8070                 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
8071                         /**
8072                          * trunc transfers [0, size) in the intent extent, we'd
8073                          * stale components overlapping [size, eof).
8074                          */
8075                         extent.e_start = extent.e_end;
8076                         extent.e_end = OBD_OBJECT_EOF;
8077                 }
8078
8079                 /* 2. stale overlapping components */
8080                 rc = lod_stale_components(env, lo, primary, &extent, th);
8081                 if (rc < 0)
8082                         GOTO(out, rc);
8083
8084                 /* 3. find the components which need instantiating.
8085                  * instantiate [0, mlc->mlc_intent->e_end) */
8086
8087                 /* restore truncate intent extent */
8088                 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC)
8089                         extent.e_end = extent.e_start;
8090                 extent.e_start = 0;
8091
8092                 lod_foreach_mirror_comp(lod_comp, lo, primary) {
8093                         if (!lu_extent_is_overlapped(&extent,
8094                                                      &lod_comp->llc_extent))
8095                                 break;
8096
8097                         if (!lod_is_instantiation_needed(lod_comp))
8098                                 continue;
8099
8100                         CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
8101                                primary, lod_comp_index(lo, lod_comp));
8102                         info->lti_comp_idx[info->lti_count++] =
8103                                                 lod_comp_index(lo, lod_comp);
8104                 }
8105         } else { /* MD_LAYOUT_RESYNC */
8106                 if (mlc->mlc_mirror_id == 0) {
8107                         /* normal resync */
8108                         lod_foreach_mirror_comp(lod_comp, lo, primary) {
8109                                 if (!lod_comp_inited(lod_comp))
8110                                         break;
8111
8112                                 extent.e_end = lod_comp->llc_extent.e_end;
8113                         }
8114
8115                         rc = lod_prepare_resync(env, lo, &extent);
8116                         if (rc)
8117                                 GOTO(out, rc);
8118                 } else {
8119                         /* mirror write, try to init its all components */
8120                         rc = lod_prepare_resync_mirror(env, lo,
8121                                                        mlc->mlc_mirror_id);
8122                         if (rc)
8123                                 GOTO(out, rc);
8124                 }
8125
8126                 /* change the file state to SYNC_PENDING */
8127                 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
8128         }
8129
8130         rc = lod_declare_instantiate_components(env, lo, th, 0);
8131         if (rc)
8132                 GOTO(out, rc);
8133
8134         lod_obj_inc_layout_gen(lo);
8135
8136         /* 3. transfer layout version to OST objects.
8137          * transfer new layout version to OST objects so that stale writes
8138          * can be denied. It also ends an era of writing by setting
8139          * LU_LAYOUT_RESYNC. Normal client can never use this bit to
8140          * send write RPC; only resync RPCs could do it. */
8141         layout_attr->la_valid = LA_LAYOUT_VERSION;
8142         layout_attr->la_layout_version = 0;
8143         if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
8144                 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
8145         rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
8146         if (rc)
8147                 GOTO(out, rc);
8148 out:
8149         if (rc)
8150                 lod_striping_free(env, lo);
8151         RETURN(rc);
8152 }
8153
8154 static int lod_declare_update_sync_pending(const struct lu_env *env,
8155                 struct lod_object *lo, struct md_layout_change *mlc,
8156                 struct thandle *th)
8157 {
8158         struct lod_thread_info  *info = lod_env_info(env);
8159         struct lu_attr *layout_attr = &info->lti_layout_attr;
8160         unsigned sync_components = 0;
8161         unsigned resync_components = 0;
8162         int i;
8163         int rc;
8164         ENTRY;
8165
8166         LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
8167         LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
8168                 mlc->mlc_opc == MD_LAYOUT_WRITE);
8169
8170         CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
8171                PFID(lod_object_fid(lo)), mlc->mlc_opc);
8172
8173         if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
8174                 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
8175                        PFID(lod_object_fid(lo)));
8176
8177                 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
8178                 return lod_declare_update_write_pending(env, lo, mlc, th);
8179         }
8180
8181         /* MD_LAYOUT_RESYNC_DONE */
8182
8183         for (i = 0; i < lo->ldo_comp_cnt; i++) {
8184                 struct lod_layout_component *lod_comp;
8185                 int j;
8186
8187                 lod_comp = &lo->ldo_comp_entries[i];
8188
8189                 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
8190                         sync_components++;
8191                         continue;
8192                 }
8193
8194                 for (j = 0; j < mlc->mlc_resync_count; j++) {
8195                         if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
8196                                 continue;
8197
8198                         mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
8199                         lod_comp->llc_flags &= ~LCME_FL_STALE;
8200                         resync_components++;
8201                         break;
8202                 }
8203         }
8204
8205         /* valid check */
8206         for (i = 0; i < mlc->mlc_resync_count; i++) {
8207                 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
8208                         continue;
8209
8210                 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
8211                        "or already synced\n", PFID(lod_object_fid(lo)),
8212                        mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
8213                 GOTO(out, rc = -EINVAL);
8214         }
8215
8216         if (!sync_components || (mlc->mlc_resync_count && !resync_components)) {
8217                 CDEBUG(D_LAYOUT, DFID": no mirror in sync\n",
8218                        PFID(lod_object_fid(lo)));
8219
8220                 /* tend to return an error code here to prevent
8221                  * the MDT from setting SoM attribute */
8222                 GOTO(out, rc = -EINVAL);
8223         }
8224
8225         CDEBUG(D_LAYOUT, DFID": synced %u resynced %u/%zu components\n",
8226                PFID(lod_object_fid(lo)),
8227                sync_components, resync_components, mlc->mlc_resync_count);
8228
8229         lo->ldo_flr_state = LCM_FL_RDONLY;
8230         lod_obj_inc_layout_gen(lo);
8231
8232         layout_attr->la_valid = LA_LAYOUT_VERSION;
8233         layout_attr->la_layout_version = 0;
8234         rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
8235         if (rc)
8236                 GOTO(out, rc);
8237
8238         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
8239         rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
8240                                        &info->lti_buf, XATTR_NAME_LOV, 0, th);
8241         EXIT;
8242
8243 out:
8244         if (rc)
8245                 lod_striping_free(env, lo);
8246         RETURN(rc);
8247 }
8248
8249 typedef int (*mlc_handler)(const struct lu_env *env, struct dt_object *dt,
8250                            const struct md_layout_change *mlc,
8251                            struct thandle *th);
8252
8253 /**
8254  * Attach stripes after target's for migrating directory. NB, we
8255  * only need to declare this, the actual work is done inside
8256  * lod_xattr_set_lmv().
8257  *
8258  * \param[in] env       execution environment
8259  * \param[in] dt        target object
8260  * \param[in] mlc       layout change data
8261  * \param[in] th        transaction handle
8262  *
8263  * \retval              0 on success
8264  * \retval              negative if failed
8265  */
8266 static int lod_dir_declare_layout_attach(const struct lu_env *env,
8267                                          struct dt_object *dt,
8268                                          const struct md_layout_change *mlc,
8269                                          struct thandle *th)
8270 {
8271         struct lod_thread_info *info = lod_env_info(env);
8272         struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
8273         struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
8274         struct lod_object *lo = lod_dt_obj(dt);
8275         struct dt_object *next = dt_object_child(dt);
8276         struct dt_object_format *dof = &info->lti_format;
8277         struct lmv_mds_md_v1 *lmv = mlc->mlc_buf.lb_buf;
8278         struct dt_object **stripes;
8279         __u32 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
8280         struct lu_fid *fid = &info->lti_fid;
8281         struct lod_tgt_desc *tgt;
8282         struct dt_object *dto;
8283         struct dt_device *tgt_dt;
8284         int type = LU_SEQ_RANGE_ANY;
8285         struct dt_insert_rec *rec = &info->lti_dt_rec;
8286         char *stripe_name = info->lti_key;
8287         struct lu_name *sname;
8288         struct linkea_data ldata = { NULL };
8289         struct lu_buf linkea_buf;
8290         __u32 idx;
8291         int i;
8292         int rc;
8293
8294         ENTRY;
8295
8296         if (!lmv_is_sane(lmv))
8297                 RETURN(-EINVAL);
8298
8299         if (!dt_try_as_dir(env, dt, false))
8300                 RETURN(-ENOTDIR);
8301
8302         dof->dof_type = DFT_DIR;
8303
8304         OBD_ALLOC_PTR_ARRAY(stripes, (lo->ldo_dir_stripe_count + stripe_count));
8305         if (!stripes)
8306                 RETURN(-ENOMEM);
8307
8308         for (i = 0; i < lo->ldo_dir_stripe_count; i++)
8309                 stripes[i] = lo->ldo_stripe[i];
8310
8311         rec->rec_type = S_IFDIR;
8312
8313         for (i = 0; i < stripe_count; i++) {
8314                 fid_le_to_cpu(fid,
8315                         &lmv->lmv_stripe_fids[i]);
8316                 if (!fid_is_sane(fid))
8317                         continue;
8318
8319                 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
8320                 if (rc)
8321                         GOTO(out, rc);
8322
8323                 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
8324                         tgt_dt = lod->lod_child;
8325                 } else {
8326                         tgt = LTD_TGT(ltd, idx);
8327                         if (tgt == NULL)
8328                                 GOTO(out, rc = -ESTALE);
8329                         tgt_dt = tgt->ltd_tgt;
8330                 }
8331
8332                 dto = dt_locate_at(env, tgt_dt, fid,
8333                                   lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
8334                                   NULL);
8335                 if (IS_ERR(dto))
8336                         GOTO(out, rc = PTR_ERR(dto));
8337
8338                 stripes[i + lo->ldo_dir_stripe_count] = dto;
8339
8340                 if (!dt_try_as_dir(env, dto, true))
8341                         GOTO(out, rc = -ENOTDIR);
8342
8343                 rc = lod_sub_declare_ref_add(env, dto, th);
8344                 if (rc)
8345                         GOTO(out, rc);
8346
8347                 rec->rec_fid = lu_object_fid(&dto->do_lu);
8348                 rc = lod_sub_declare_insert(env, dto,
8349                                             (const struct dt_rec *)rec,
8350                                             (const struct dt_key *)dot, th);
8351                 if (rc)
8352                         GOTO(out, rc);
8353
8354                 rc = lod_sub_declare_insert(env, dto,
8355                                             (const struct dt_rec *)rec,
8356                                             (const struct dt_key *)dotdot, th);
8357                 if (rc)
8358                         GOTO(out, rc);
8359
8360                 rc = lod_sub_declare_xattr_set(env, dto, &mlc->mlc_buf,
8361                                                 XATTR_NAME_LMV, 0, th);
8362                 if (rc)
8363                         GOTO(out, rc);
8364
8365                 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
8366                          PFID(lu_object_fid(&dto->do_lu)),
8367                          i + lo->ldo_dir_stripe_count);
8368
8369                 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
8370                 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
8371                                       sname, lu_object_fid(&dt->do_lu));
8372                 if (rc)
8373                         GOTO(out, rc);
8374
8375                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
8376                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
8377                 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
8378                                                XATTR_NAME_LINK, 0, th);
8379                 if (rc)
8380                         GOTO(out, rc);
8381
8382                 rc = lod_sub_declare_insert(env, next,
8383                                             (const struct dt_rec *)rec,
8384                                             (const struct dt_key *)stripe_name,
8385                                             th);
8386                 if (rc)
8387                         GOTO(out, rc);
8388
8389                 rc = lod_sub_declare_ref_add(env, next, th);
8390                 if (rc)
8391                         GOTO(out, rc);
8392         }
8393
8394         if (lo->ldo_stripe)
8395                 OBD_FREE_PTR_ARRAY(lo->ldo_stripe,
8396                                    lo->ldo_dir_stripes_allocated);
8397         lo->ldo_stripe = stripes;
8398         lo->ldo_is_foreign = 0;
8399         lo->ldo_dir_migrate_offset = lo->ldo_dir_stripe_count;
8400         lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_hash_type);
8401         lo->ldo_dir_stripe_count += stripe_count;
8402         lo->ldo_dir_stripes_allocated += stripe_count;
8403
8404         /* plain directory split creates target as a plain directory, while
8405          * after source attached as the first stripe, it becomes a striped
8406          * directory, set correct do_index_ops, otherwise it can't be unlinked.
8407          */
8408         dt->do_index_ops = &lod_striped_index_ops;
8409
8410         RETURN(0);
8411 out:
8412         i = lo->ldo_dir_stripe_count;
8413         while (i < lo->ldo_dir_stripe_count + stripe_count && stripes[i])
8414                 dt_object_put(env, stripes[i++]);
8415
8416         OBD_FREE_PTR_ARRAY(stripes, stripe_count + lo->ldo_dir_stripe_count);
8417         return rc;
8418 }
8419
8420 static int lod_dir_declare_layout_detach(const struct lu_env *env,
8421                                          struct dt_object *dt,
8422                                          const struct md_layout_change *unused,
8423                                          struct thandle *th)
8424 {
8425         struct lod_thread_info *info = lod_env_info(env);
8426         struct lod_object *lo = lod_dt_obj(dt);
8427         struct dt_object *next = dt_object_child(dt);
8428         char *stripe_name = info->lti_key;
8429         struct dt_object *dto;
8430         int i;
8431         int rc = 0;
8432
8433         if (!dt_try_as_dir(env, dt, true))
8434                 return -ENOTDIR;
8435
8436         if (!lo->ldo_dir_stripe_count)
8437                 return lod_sub_declare_delete(env, next,
8438                                         (const struct dt_key *)dotdot, th);
8439
8440         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8441                 dto = lo->ldo_stripe[i];
8442                 if (!dto)
8443                         continue;
8444
8445                 if (!dt_try_as_dir(env, dto, true))
8446                         return -ENOTDIR;
8447
8448                 rc = lod_sub_declare_delete(env, dto,
8449                                         (const struct dt_key *)dotdot, th);
8450                 if (rc)
8451                         return rc;
8452
8453                 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8454                          PFID(lu_object_fid(&dto->do_lu)), i);
8455
8456                 rc = lod_sub_declare_delete(env, next,
8457                                         (const struct dt_key *)stripe_name, th);
8458                 if (rc)
8459                         return rc;
8460
8461                 rc = lod_sub_declare_ref_del(env, next, th);
8462                 if (rc)
8463                         return rc;
8464         }
8465
8466         return 0;
8467 }
8468
8469 static int dt_dir_is_empty(const struct lu_env *env,
8470                            struct dt_object *obj)
8471 {
8472         struct dt_it *it;
8473         const struct dt_it_ops *iops;
8474         int rc;
8475
8476         ENTRY;
8477
8478         if (!dt_try_as_dir(env, obj, true))
8479                 RETURN(-ENOTDIR);
8480
8481         iops = &obj->do_index_ops->dio_it;
8482         it = iops->init(env, obj, LUDA_64BITHASH);
8483         if (IS_ERR(it))
8484                 RETURN(PTR_ERR(it));
8485
8486         rc = iops->get(env, it, (const struct dt_key *)"");
8487         if (rc > 0) {
8488                 int i;
8489
8490                 for (rc = 0, i = 0; rc == 0 && i < 3; ++i)
8491                         rc = iops->next(env, it);
8492                 if (!rc)
8493                         rc = -ENOTEMPTY;
8494                 else if (rc == 1)
8495                         rc = 0;
8496         } else if (!rc) {
8497                 /* Huh? Index contains no zero key? */
8498                 rc = -EIO;
8499         }
8500
8501         iops->put(env, it);
8502         iops->fini(env, it);
8503
8504         RETURN(rc);
8505 }
8506
8507 static int lod_dir_declare_layout_shrink(const struct lu_env *env,
8508                                          struct dt_object *dt,
8509                                          const struct md_layout_change *mlc,
8510                                          struct thandle *th)
8511 {
8512         struct lod_thread_info *info = lod_env_info(env);
8513         struct lod_object *lo = lod_dt_obj(dt);
8514         struct dt_object *next = dt_object_child(dt);
8515         struct lmv_user_md *lmu = mlc->mlc_buf.lb_buf;
8516         char *stripe_name = info->lti_key;
8517         struct lu_buf *lmv_buf = &info->lti_buf;
8518         __u32 final_stripe_count;
8519         struct dt_object *dto;
8520         int i;
8521         int rc;
8522
8523         LASSERT(lmu);
8524
8525         if (!dt_try_as_dir(env, dt, true))
8526                 return -ENOTDIR;
8527
8528         /* shouldn't be called on plain directory */
8529         LASSERT(lo->ldo_dir_stripe_count);
8530
8531         lmv_buf->lb_buf = &info->lti_lmv.lmv_md_v1;
8532         lmv_buf->lb_len = sizeof(info->lti_lmv.lmv_md_v1);
8533
8534         final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
8535         LASSERT(final_stripe_count &&
8536                 final_stripe_count < lo->ldo_dir_stripe_count);
8537
8538         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8539                 dto = lo->ldo_stripe[i];
8540                 if (!dto)
8541                         continue;
8542
8543                 if (i < final_stripe_count) {
8544                         rc = lod_sub_declare_xattr_set(env, dto, lmv_buf,
8545                                                        XATTR_NAME_LMV,
8546                                                        LU_XATTR_REPLACE, th);
8547                         if (rc)
8548                                 return rc;
8549
8550                         continue;
8551                 }
8552
8553                 rc = dt_dir_is_empty(env, dto);
8554                 if (rc < 0)
8555                         return rc;
8556
8557                 rc = lod_sub_declare_ref_del(env, dto, th);
8558                 if (rc)
8559                         return rc;
8560
8561                 rc = lod_sub_declare_destroy(env, dto, th);
8562                 if (rc)
8563                         return rc;
8564
8565                 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8566                          PFID(lu_object_fid(&dto->do_lu)), i);
8567
8568                 rc = lod_sub_declare_delete(env, next,
8569                                         (const struct dt_key *)stripe_name, th);
8570                 if (rc)
8571                         return rc;
8572
8573                 rc = lod_sub_declare_ref_del(env, next, th);
8574                 if (rc)
8575                         return rc;
8576         }
8577
8578         rc = lod_sub_declare_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV,
8579                                        LU_XATTR_REPLACE, th);
8580         return rc;
8581 }
8582
8583 /**
8584  * Allocate stripes for split directory.
8585  *
8586  * \param[in] env       execution environment
8587  * \param[in] dt        target object
8588  * \param[in] mlc       layout change data
8589  * \param[in] th        transaction handle
8590  *
8591  * \retval              0 on success
8592  * \retval              negative if failed
8593  */
8594 static int lod_dir_declare_layout_split(const struct lu_env *env,
8595                                         struct dt_object *dt,
8596                                         const struct md_layout_change *mlc,
8597                                         struct thandle *th)
8598 {
8599         struct lod_thread_info *info = lod_env_info(env);
8600         struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
8601         struct lod_object *lo = lod_dt_obj(dt);
8602         struct dt_object_format *dof = &info->lti_format;
8603         struct lmv_user_md_v1 *lum = mlc->mlc_spec->u.sp_ea.eadata;
8604         struct dt_object **stripes;
8605         u32 stripe_count;
8606         u32 saved_count;
8607         int i;
8608         int rc;
8609
8610         ENTRY;
8611
8612         LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
8613         LASSERT(le32_to_cpu(lum->lum_stripe_offset) == LMV_OFFSET_DEFAULT);
8614
8615         saved_count = lo->ldo_dir_stripes_allocated;
8616         stripe_count = le32_to_cpu(lum->lum_stripe_count);
8617         if (stripe_count <= saved_count)
8618                 RETURN(-EINVAL);
8619
8620         dof->dof_type = DFT_DIR;
8621
8622         OBD_ALLOC(stripes, sizeof(*stripes) * stripe_count);
8623         if (!stripes)
8624                 RETURN(-ENOMEM);
8625
8626         for (i = 0; i < lo->ldo_dir_stripes_allocated; i++)
8627                 stripes[i] = lo->ldo_stripe[i];
8628
8629         lod_qos_statfs_update(env, lod, &lod->lod_mdt_descs);
8630         rc = lod_mdt_alloc_qos(env, lo, stripes, saved_count, stripe_count);
8631         if (rc == -EAGAIN)
8632                 rc = lod_mdt_alloc_rr(env, lo, stripes, saved_count,
8633                                       stripe_count);
8634         if (rc < 0) {
8635                 OBD_FREE(stripes, sizeof(*stripes) * stripe_count);
8636                 RETURN(rc);
8637         }
8638
8639         LASSERT(rc > saved_count);
8640         OBD_FREE(lo->ldo_stripe,
8641                  sizeof(*stripes) * lo->ldo_dir_stripes_allocated);
8642         lo->ldo_stripe = stripes;
8643         lo->ldo_is_foreign = 0;
8644         lo->ldo_dir_striped = 1;
8645         lo->ldo_dir_stripe_count = rc;
8646         lo->ldo_dir_stripes_allocated = stripe_count;
8647         lo->ldo_dir_split_hash = lo->ldo_dir_hash_type;
8648         lo->ldo_dir_hash_type = le32_to_cpu(lum->lum_hash_type);
8649         if (!lmv_is_known_hash_type(lo->ldo_dir_hash_type))
8650                 lo->ldo_dir_hash_type =
8651                         lod->lod_mdt_descs.ltd_lmv_desc.ld_pattern;
8652         lo->ldo_dir_hash_type |= LMV_HASH_FLAG_SPLIT | LMV_HASH_FLAG_MIGRATION;
8653         lo->ldo_dir_split_offset = saved_count;
8654         lo->ldo_dir_layout_version++;
8655         lo->ldo_dir_stripe_loaded = 1;
8656
8657         rc = lod_dir_declare_create_stripes(env, dt, mlc->mlc_attr, dof, th);
8658         if (rc)
8659                 lod_striping_free(env, lo);
8660
8661         RETURN(rc);
8662 }
8663
8664 /*
8665  * detach all stripes from dir master object, NB, stripes are not destroyed, but
8666  * deleted from it's parent namespace, this function is called in two places:
8667  * 1. mdd_migrate_mdt() detach stripes from source, and attach them to
8668  *    target.
8669  * 2. mdd_dir_layout_update() detach stripe before turning 1-stripe directory to
8670  *    a plain directory.
8671  *
8672  * \param[in] env       execution environment
8673  * \param[in] dt        target object
8674  * \param[in] mlc       layout change data
8675  * \param[in] th        transaction handle
8676  *
8677  * \retval              0 on success
8678  * \retval              negative if failed
8679  */
8680 static int lod_dir_layout_detach(const struct lu_env *env,
8681                                  struct dt_object *dt,
8682                                  const struct md_layout_change *mlc,
8683                                  struct thandle *th)
8684 {
8685         struct lod_thread_info *info = lod_env_info(env);
8686         struct lod_object *lo = lod_dt_obj(dt);
8687         struct dt_object *next = dt_object_child(dt);
8688         char *stripe_name = info->lti_key;
8689         struct dt_object *dto;
8690         int i;
8691         int rc = 0;
8692
8693         ENTRY;
8694
8695         if (!lo->ldo_dir_stripe_count) {
8696                 /* plain directory delete .. */
8697                 rc = lod_sub_delete(env, next,
8698                                     (const struct dt_key *)dotdot, th);
8699                 RETURN(rc);
8700         }
8701
8702         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8703                 dto = lo->ldo_stripe[i];
8704                 if (!dto)
8705                         continue;
8706
8707                 rc = lod_sub_delete(env, dto,
8708                                     (const struct dt_key *)dotdot, th);
8709                 if (rc)
8710                         break;
8711
8712                 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8713                          PFID(lu_object_fid(&dto->do_lu)), i);
8714
8715                 rc = lod_sub_delete(env, next,
8716                                     (const struct dt_key *)stripe_name, th);
8717                 if (rc)
8718                         break;
8719
8720                 rc = lod_sub_ref_del(env, next, th);
8721                 if (rc)
8722                         break;
8723         }
8724
8725         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8726                 dto = lo->ldo_stripe[i];
8727                 if (dto)
8728                         dt_object_put(env, dto);
8729         }
8730         OBD_FREE_PTR_ARRAY(lo->ldo_stripe, lo->ldo_dir_stripes_allocated);
8731         lo->ldo_stripe = NULL;
8732         lo->ldo_dir_stripes_allocated = 0;
8733         lo->ldo_dir_stripe_count = 0;
8734         dt->do_index_ops = &lod_index_ops;
8735
8736         RETURN(rc);
8737 }
8738
8739 static int lod_dir_layout_shrink(const struct lu_env *env,
8740                                  struct dt_object *dt,
8741                                  const struct md_layout_change *mlc,
8742                                  struct thandle *th)
8743 {
8744         struct lod_thread_info *info = lod_env_info(env);
8745         struct lod_object *lo = lod_dt_obj(dt);
8746         struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
8747         struct dt_object *next = dt_object_child(dt);
8748         struct lmv_user_md *lmu = mlc->mlc_buf.lb_buf;
8749         __u32 final_stripe_count;
8750         char *stripe_name = info->lti_key;
8751         struct dt_object *dto;
8752         struct lu_buf *lmv_buf = &info->lti_buf;
8753         struct lmv_mds_md_v1 *lmv = &info->lti_lmv.lmv_md_v1;
8754         u32 mdtidx;
8755         int type = LU_SEQ_RANGE_ANY;
8756         int i;
8757         int rc;
8758
8759         ENTRY;
8760
8761         final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
8762
8763         lmv_buf->lb_buf = lmv;
8764         lmv_buf->lb_len = sizeof(*lmv);
8765         lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
8766         lmv->lmv_stripe_count = cpu_to_le32(final_stripe_count);
8767         lmv->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type) &
8768                              cpu_to_le32(LMV_HASH_TYPE_MASK |
8769                                          LMV_HASH_FLAG_FIXED);
8770         lmv->lmv_layout_version =
8771                         cpu_to_le32(lo->ldo_dir_layout_version + 1);
8772         lmv->lmv_migrate_offset = 0;
8773         lmv->lmv_migrate_hash = 0;
8774
8775         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8776                 dto = lo->ldo_stripe[i];
8777                 if (!dto)
8778                         continue;
8779
8780                 if (i < final_stripe_count) {
8781                         rc = lod_fld_lookup(env, lod,
8782                                             lu_object_fid(&dto->do_lu),
8783                                             &mdtidx, &type);
8784                         if (rc)
8785                                 RETURN(rc);
8786
8787                         lmv->lmv_master_mdt_index = cpu_to_le32(mdtidx);
8788                         rc = lod_sub_xattr_set(env, dto, lmv_buf,
8789                                                XATTR_NAME_LMV,
8790                                                LU_XATTR_REPLACE, th);
8791                         if (rc)
8792                                 RETURN(rc);
8793
8794                         continue;
8795                 }
8796
8797                 dt_write_lock(env, dto, DT_TGT_CHILD);
8798                 rc = lod_sub_ref_del(env, dto, th);
8799                 dt_write_unlock(env, dto);
8800                 if (rc)
8801                         RETURN(rc);
8802
8803                 rc = lod_sub_destroy(env, dto, th);
8804                 if (rc)
8805                         RETURN(rc);
8806
8807                 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8808                          PFID(lu_object_fid(&dto->do_lu)), i);
8809
8810                 rc = lod_sub_delete(env, next,
8811                                     (const struct dt_key *)stripe_name, th);
8812                 if (rc)
8813                         RETURN(rc);
8814
8815                 rc = lod_sub_ref_del(env, next, th);
8816                 if (rc)
8817                         RETURN(rc);
8818         }
8819
8820         rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &mdtidx,
8821                             &type);
8822         if (rc)
8823                 RETURN(rc);
8824
8825         lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
8826         lmv->lmv_master_mdt_index = cpu_to_le32(mdtidx);
8827         rc = lod_sub_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV,
8828                                LU_XATTR_REPLACE, th);
8829         if (rc)
8830                 RETURN(rc);
8831
8832         for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
8833                 dto = lo->ldo_stripe[i];
8834                 if (dto)
8835                         dt_object_put(env, dto);
8836         }
8837         lo->ldo_dir_stripe_count = final_stripe_count;
8838
8839         RETURN(rc);
8840 }
8841
8842 static mlc_handler dir_mlc_declare_ops[MD_LAYOUT_MAX] = {
8843         [MD_LAYOUT_ATTACH] = lod_dir_declare_layout_attach,
8844         [MD_LAYOUT_DETACH] = lod_dir_declare_layout_detach,
8845         [MD_LAYOUT_SHRINK] = lod_dir_declare_layout_shrink,
8846         [MD_LAYOUT_SPLIT]  = lod_dir_declare_layout_split,
8847 };
8848
8849 static mlc_handler dir_mlc_ops[MD_LAYOUT_MAX] = {
8850         [MD_LAYOUT_DETACH] = lod_dir_layout_detach,
8851         [MD_LAYOUT_SHRINK] = lod_dir_layout_shrink,
8852 };
8853
8854 static int lod_declare_layout_change(const struct lu_env *env,
8855                 struct dt_object *dt, struct md_layout_change *mlc,
8856                 struct thandle *th)
8857 {
8858         struct lod_thread_info  *info = lod_env_info(env);
8859         struct lod_object *lo = lod_dt_obj(dt);
8860         int rc;
8861
8862         ENTRY;
8863
8864         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
8865                 LASSERT(dir_mlc_declare_ops[mlc->mlc_opc]);
8866                 rc = dir_mlc_declare_ops[mlc->mlc_opc](env, dt, mlc, th);
8867                 RETURN(rc);
8868         }
8869
8870         if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
8871             dt_object_remote(dt_object_child(dt)))
8872                 RETURN(-EINVAL);
8873
8874         rc = lod_striping_load(env, lo);
8875         if (rc)
8876                 GOTO(out, rc);
8877
8878         LASSERT(lo->ldo_comp_cnt > 0);
8879
8880         rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
8881         if (rc)
8882                 GOTO(out, rc);
8883
8884         switch (lo->ldo_flr_state) {
8885         case LCM_FL_NONE:
8886                 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
8887                                               &mlc->mlc_buf, th);
8888                 break;
8889         case LCM_FL_RDONLY:
8890                 rc = lod_declare_update_rdonly(env, lo, mlc, th);
8891                 break;
8892         case LCM_FL_WRITE_PENDING:
8893                 rc = lod_declare_update_write_pending(env, lo, mlc, th);
8894                 break;
8895         case LCM_FL_SYNC_PENDING:
8896                 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
8897                 break;
8898         default:
8899                 rc = -ENOTSUPP;
8900                 break;
8901         }
8902 out:
8903         RETURN(rc);
8904 }
8905
8906 /**
8907  * Instantiate layout component objects which covers the intent write offset.
8908  */
8909 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
8910                              struct md_layout_change *mlc, struct thandle *th)
8911 {
8912         struct lu_attr *attr = &lod_env_info(env)->lti_attr;
8913         struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
8914         struct lod_object *lo = lod_dt_obj(dt);
8915         int rc;
8916
8917         ENTRY;
8918
8919         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
8920                 LASSERT(dir_mlc_ops[mlc->mlc_opc]);
8921                 rc = dir_mlc_ops[mlc->mlc_opc](env, dt, mlc, th);
8922                 RETURN(rc);
8923         }
8924
8925         rc = lod_striped_create(env, dt, attr, NULL, th);
8926         if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
8927                 layout_attr->la_layout_version |= lo->ldo_layout_gen;
8928                 rc = lod_attr_set(env, dt, layout_attr, th);
8929         }
8930
8931         RETURN(rc);
8932 }
8933
8934 const struct dt_object_operations lod_obj_ops = {
8935         .do_read_lock           = lod_read_lock,
8936         .do_write_lock          = lod_write_lock,
8937         .do_read_unlock         = lod_read_unlock,
8938         .do_write_unlock        = lod_write_unlock,
8939         .do_write_locked        = lod_write_locked,
8940         .do_attr_get            = lod_attr_get,
8941         .do_declare_attr_set    = lod_declare_attr_set,
8942         .do_attr_set            = lod_attr_set,
8943         .do_xattr_get           = lod_xattr_get,
8944         .do_declare_xattr_set   = lod_declare_xattr_set,
8945         .do_xattr_set           = lod_xattr_set,
8946         .do_declare_xattr_del   = lod_declare_xattr_del,
8947         .do_xattr_del           = lod_xattr_del,
8948         .do_xattr_list          = lod_xattr_list,
8949         .do_ah_init             = lod_ah_init,
8950         .do_declare_create      = lod_declare_create,
8951         .do_create              = lod_create,
8952         .do_declare_destroy     = lod_declare_destroy,
8953         .do_destroy             = lod_destroy,
8954         .do_index_try           = lod_index_try,
8955         .do_declare_ref_add     = lod_declare_ref_add,
8956         .do_ref_add             = lod_ref_add,
8957         .do_declare_ref_del     = lod_declare_ref_del,
8958         .do_ref_del             = lod_ref_del,
8959         .do_object_sync         = lod_object_sync,
8960         .do_object_lock         = lod_object_lock,
8961         .do_object_unlock       = lod_object_unlock,
8962         .do_invalidate          = lod_invalidate,
8963         .do_declare_layout_change = lod_declare_layout_change,
8964         .do_layout_change       = lod_layout_change,
8965 };
8966
8967 /**
8968  * Implementation of dt_body_operations::dbo_read.
8969  *
8970  * \see dt_body_operations::dbo_read() in the API description for details.
8971  */
8972 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
8973                         struct lu_buf *buf, loff_t *pos)
8974 {
8975         struct dt_object *next = dt_object_child(dt);
8976
8977         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
8978                 S_ISLNK(dt->do_lu.lo_header->loh_attr));
8979         return next->do_body_ops->dbo_read(env, next, buf, pos);
8980 }
8981
8982 /**
8983  * Implementation of dt_body_operations::dbo_declare_write.
8984  *
8985  * \see dt_body_operations::dbo_declare_write() in the API description
8986  * for details.
8987  */
8988 static ssize_t lod_declare_write(const struct lu_env *env,
8989                                  struct dt_object *dt,
8990                                  const struct lu_buf *buf, loff_t pos,
8991                                  struct thandle *th)
8992 {
8993         return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
8994 }
8995
8996 /**
8997  * Implementation of dt_body_operations::dbo_write.
8998  *
8999  * \see dt_body_operations::dbo_write() in the API description for details.
9000  */
9001 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
9002                          const struct lu_buf *buf, loff_t *pos,
9003                          struct thandle *th)
9004 {
9005         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
9006                 S_ISLNK(dt->do_lu.lo_header->loh_attr));
9007         return lod_sub_write(env, dt_object_child(dt), buf, pos, th);
9008 }
9009
9010 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
9011                              __u64 start, __u64 end, struct thandle *th)
9012 {
9013         if (dt_object_remote(dt))
9014                 return -ENOTSUPP;
9015
9016         return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
9017 }
9018
9019 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
9020                      __u64 start, __u64 end, struct thandle *th)
9021 {
9022         if (dt_object_remote(dt))
9023                 return -ENOTSUPP;
9024
9025         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
9026         return lod_sub_punch(env, dt_object_child(dt), start, end, th);
9027 }
9028
9029 /*
9030  * different type of files use the same body_ops because object may be created
9031  * in OUT, where there is no chance to set correct body_ops for each type, so
9032  * body_ops themselves will check file type inside, see lod_read/write/punch for
9033  * details.
9034  */
9035 static const struct dt_body_operations lod_body_ops = {
9036         .dbo_read               = lod_read,
9037         .dbo_declare_write      = lod_declare_write,
9038         .dbo_write              = lod_write,
9039         .dbo_declare_punch      = lod_declare_punch,
9040         .dbo_punch              = lod_punch,
9041 };
9042
9043 /**
9044  * Implementation of lu_object_operations::loo_object_init.
9045  *
9046  * The function determines the type and the index of the target device using
9047  * sequence of the object's FID. Then passes control down to the
9048  * corresponding device:
9049  *  OSD for the local objects, OSP for remote
9050  *
9051  * \see lu_object_operations::loo_object_init() in the API description
9052  * for details.
9053  */
9054 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
9055                            const struct lu_object_conf *conf)
9056 {
9057         struct lod_device       *lod    = lu2lod_dev(lo->lo_dev);
9058         struct lu_device        *cdev   = NULL;
9059         struct lu_object        *cobj;
9060         struct lod_tgt_descs    *ltd    = NULL;
9061         struct lod_tgt_desc     *tgt;
9062         u32                      idx    = 0;
9063         int                      type   = LU_SEQ_RANGE_ANY;
9064         int                      rc;
9065         ENTRY;
9066
9067         rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
9068         if (rc != 0)
9069                 RETURN(rc);
9070
9071         if (type == LU_SEQ_RANGE_MDT &&
9072             idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
9073                 cdev = &lod->lod_child->dd_lu_dev;
9074         } else if (type == LU_SEQ_RANGE_MDT) {
9075                 ltd = &lod->lod_mdt_descs;
9076                 lod_getref(ltd);
9077         } else if (type == LU_SEQ_RANGE_OST) {
9078                 ltd = &lod->lod_ost_descs;
9079                 lod_getref(ltd);
9080         } else {
9081                 LBUG();
9082         }
9083
9084         if (ltd != NULL) {
9085                 if (ltd->ltd_tgts_size > idx &&
9086                     test_bit(idx, ltd->ltd_tgt_bitmap)) {
9087                         tgt = LTD_TGT(ltd, idx);
9088
9089                         LASSERT(tgt != NULL);
9090                         LASSERT(tgt->ltd_tgt != NULL);
9091
9092                         cdev = &(tgt->ltd_tgt->dd_lu_dev);
9093                 }
9094                 lod_putref(lod, ltd);
9095         }
9096
9097         if (unlikely(cdev == NULL))
9098                 RETURN(-ENOENT);
9099
9100         cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
9101         if (unlikely(cobj == NULL))
9102                 RETURN(-ENOMEM);
9103
9104         lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
9105
9106         lu_object_add(lo, cobj);
9107
9108         RETURN(0);
9109 }
9110
9111 /**
9112  *
9113  * Release resources associated with striping.
9114  *
9115  * If the object is striped (regular or directory), then release
9116  * the stripe objects references and free the ldo_stripe array.
9117  *
9118  * \param[in] env       execution environment
9119  * \param[in] lo        object
9120  */
9121 void lod_striping_free_nolock(const struct lu_env *env, struct lod_object *lo)
9122 {
9123         struct lod_layout_component *lod_comp;
9124         __u32 obj_attr = lo->ldo_obj.do_lu.lo_header->loh_attr;
9125         int i, j;
9126
9127         if (unlikely(lo->ldo_is_foreign)) {
9128                 if (S_ISREG(obj_attr)) {
9129                         lod_free_foreign_lov(lo);
9130                         lo->ldo_comp_cached = 0;
9131                 } else if (S_ISDIR(obj_attr)) {
9132                         lod_free_foreign_lmv(lo);
9133                         lo->ldo_dir_stripe_loaded = 0;
9134                 }
9135         } else if (lo->ldo_stripe != NULL) {
9136                 LASSERT(lo->ldo_comp_entries == NULL);
9137                 LASSERT(lo->ldo_dir_stripes_allocated > 0);
9138
9139                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
9140                         if (lo->ldo_stripe[i])
9141                                 dt_object_put(env, lo->ldo_stripe[i]);
9142                 }
9143
9144                 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
9145                 OBD_FREE(lo->ldo_stripe, j);
9146                 lo->ldo_stripe = NULL;
9147                 lo->ldo_dir_stripes_allocated = 0;
9148                 lo->ldo_dir_stripe_loaded = 0;
9149                 lo->ldo_dir_stripe_count = 0;
9150                 lo->ldo_obj.do_index_ops = NULL;
9151         } else if (lo->ldo_comp_entries != NULL) {
9152                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
9153                         /* free lod_layout_component::llc_stripe array */
9154                         lod_comp = &lo->ldo_comp_entries[i];
9155
9156                         if (lod_comp->llc_stripe == NULL)
9157                                 continue;
9158                         LASSERT(lod_comp->llc_stripes_allocated != 0);
9159                         for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
9160                                 if (lod_comp->llc_stripe[j] != NULL)
9161                                         lu_object_put(env,
9162                                                &lod_comp->llc_stripe[j]->do_lu);
9163                         }
9164                         OBD_FREE_PTR_ARRAY(lod_comp->llc_stripe,
9165                                            lod_comp->llc_stripes_allocated);
9166                         lod_comp->llc_stripe = NULL;
9167                         OBD_FREE_PTR_ARRAY(lod_comp->llc_ost_indices,
9168                                            lod_comp->llc_stripes_allocated);
9169                         lod_comp->llc_ost_indices = NULL;
9170                         lod_comp->llc_stripes_allocated = 0;
9171                 }
9172                 lod_free_comp_entries(lo);
9173                 lo->ldo_comp_cached = 0;
9174         }
9175 }
9176
9177 void lod_striping_free(const struct lu_env *env, struct lod_object *lo)
9178 {
9179         mutex_lock(&lo->ldo_layout_mutex);
9180         lod_striping_free_nolock(env, lo);
9181         mutex_unlock(&lo->ldo_layout_mutex);
9182 }
9183
9184 /**
9185  * Implementation of lu_object_operations::loo_object_free.
9186  *
9187  * \see lu_object_operations::loo_object_free() in the API description
9188  * for details.
9189  */
9190 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
9191 {
9192         struct lod_object *lo = lu2lod_obj(o);
9193
9194         /* release all underlying object pinned */
9195         lod_striping_free(env, lo);
9196         lu_object_fini(o);
9197         /* lo doesn't contain a lu_object_header, so we don't need call_rcu */
9198         OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
9199 }
9200
9201 /**
9202  * Implementation of lu_object_operations::loo_object_release.
9203  *
9204  * \see lu_object_operations::loo_object_release() in the API description
9205  * for details.
9206  */
9207 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
9208 {
9209         /* XXX: shouldn't we release everything here in case if object
9210          * creation failed before? */
9211 }
9212
9213 /**
9214  * Implementation of lu_object_operations::loo_object_print.
9215  *
9216  * \see lu_object_operations::loo_object_print() in the API description
9217  * for details.
9218  */
9219 static int lod_object_print(const struct lu_env *env, void *cookie,
9220                             lu_printer_t p, const struct lu_object *l)
9221 {
9222         struct lod_object *o = lu2lod_obj((struct lu_object *) l);
9223
9224         return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
9225 }
9226
9227 const struct lu_object_operations lod_lu_obj_ops = {
9228         .loo_object_init        = lod_object_init,
9229         .loo_object_free        = lod_object_free,
9230         .loo_object_release     = lod_object_release,
9231         .loo_object_print       = lod_object_print,
9232 };